4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
5 Copyright (C) Simo Sorce 2004-2008
6 Copyright (C) Matthias Dieter Wallnöfer 2009-2011
7 Copyright (C) Matthieu Patou 2012
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 * Component: ldb samldb module
28 * Description: various internal DSDB triggers - most for SAM specific objects
34 #include "libcli/ldap/ldap_ndr.h"
35 #include "ldb_module.h"
36 #include "auth/auth.h"
37 #include "dsdb/samdb/samdb.h"
38 #include "dsdb/samdb/ldb_modules/util.h"
39 #include "dsdb/samdb/ldb_modules/ridalloc.h"
40 #include "libcli/security/security.h"
41 #include "librpc/gen_ndr/ndr_security.h"
43 #include "param/param.h"
44 #include "libds/common/flag_mapping.h"
47 enum samldb_add_type
{
54 typedef int (*samldb_step_fn_t
)(struct samldb_ctx
*);
57 struct samldb_step
*next
;
62 struct ldb_module
*module
;
63 struct ldb_request
*req
;
65 /* used for add operations */
66 enum samldb_add_type type
;
68 /* the resulting message */
69 struct ldb_message
*msg
;
71 /* used in "samldb_find_for_defaultObjectCategory" */
72 struct ldb_dn
*dn
, *res_dn
;
74 /* all the async steps necessary to complete the operation */
75 struct samldb_step
*steps
;
76 struct samldb_step
*curstep
;
78 /* If someone set an ares to forward controls and response back to the caller */
79 struct ldb_reply
*ares
;
82 static struct samldb_ctx
*samldb_ctx_init(struct ldb_module
*module
,
83 struct ldb_request
*req
)
85 struct ldb_context
*ldb
;
86 struct samldb_ctx
*ac
;
88 ldb
= ldb_module_get_ctx(module
);
90 ac
= talloc_zero(req
, struct samldb_ctx
);
102 static int samldb_add_step(struct samldb_ctx
*ac
, samldb_step_fn_t fn
)
104 struct samldb_step
*step
, *stepper
;
106 step
= talloc_zero(ac
, struct samldb_step
);
108 return ldb_oom(ldb_module_get_ctx(ac
->module
));
113 if (ac
->steps
== NULL
) {
117 if (ac
->curstep
== NULL
)
118 return ldb_operr(ldb_module_get_ctx(ac
->module
));
119 for (stepper
= ac
->curstep
; stepper
->next
!= NULL
;
120 stepper
= stepper
->next
);
121 stepper
->next
= step
;
127 static int samldb_first_step(struct samldb_ctx
*ac
)
129 if (ac
->steps
== NULL
) {
130 return ldb_operr(ldb_module_get_ctx(ac
->module
));
133 ac
->curstep
= ac
->steps
;
134 return ac
->curstep
->fn(ac
);
137 static int samldb_next_step(struct samldb_ctx
*ac
)
139 if (ac
->curstep
->next
) {
140 ac
->curstep
= ac
->curstep
->next
;
141 return ac
->curstep
->fn(ac
);
144 /* We exit the samldb module here. If someone set an "ares" to forward
145 * controls and response back to the caller, use them. */
147 return ldb_module_done(ac
->req
, ac
->ares
->controls
,
148 ac
->ares
->response
, LDB_SUCCESS
);
150 return ldb_module_done(ac
->req
, NULL
, NULL
, LDB_SUCCESS
);
155 /* sAMAccountName handling */
157 static int samldb_generate_sAMAccountName(struct ldb_context
*ldb
,
158 struct ldb_message
*msg
)
162 /* Format: $000000-000000000000 */
164 name
= talloc_asprintf(msg
, "$%.6X-%.6X%.6X",
165 (unsigned int)generate_random(),
166 (unsigned int)generate_random(),
167 (unsigned int)generate_random());
171 return ldb_msg_add_steal_string(msg
, "sAMAccountName", name
);
174 static int samldb_check_sAMAccountName(struct samldb_ctx
*ac
)
176 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
179 struct ldb_result
*res
;
180 const char * const noattrs
[] = { NULL
};
182 if (ldb_msg_find_element(ac
->msg
, "sAMAccountName") == NULL
) {
183 ret
= samldb_generate_sAMAccountName(ldb
, ac
->msg
);
184 if (ret
!= LDB_SUCCESS
) {
189 name
= ldb_msg_find_attr_as_string(ac
->msg
, "sAMAccountName", NULL
);
191 /* The "sAMAccountName" cannot be nothing */
192 ldb_set_errstring(ldb
,
193 "samldb: Empty account names aren't allowed!");
194 return LDB_ERR_CONSTRAINT_VIOLATION
;
197 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
198 ldb_get_default_basedn(ldb
), LDB_SCOPE_SUBTREE
, noattrs
,
199 DSDB_FLAG_NEXT_MODULE
,
201 "(sAMAccountName=%s)",
202 ldb_binary_encode_string(ac
, name
));
203 if (ret
!= LDB_SUCCESS
) {
206 if (res
->count
!= 0) {
207 ldb_asprintf_errstring(ldb
,
208 "samldb: Account name (sAMAccountName) '%s' already in use!",
211 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
215 return samldb_next_step(ac
);
219 static bool samldb_msg_add_sid(struct ldb_message
*msg
,
221 const struct dom_sid
*sid
)
224 enum ndr_err_code ndr_err
;
226 ndr_err
= ndr_push_struct_blob(&v
, msg
, sid
,
227 (ndr_push_flags_fn_t
)ndr_push_dom_sid
);
228 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
231 return (ldb_msg_add_value(msg
, name
, &v
, NULL
) == 0);
235 /* allocate a SID using our RID Set */
236 static int samldb_allocate_sid(struct samldb_ctx
*ac
)
240 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
243 ret
= ridalloc_allocate_rid(ac
->module
, &rid
, ac
->req
);
244 if (ret
!= LDB_SUCCESS
) {
248 sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), rid
);
250 return ldb_module_oom(ac
->module
);
253 if ( ! samldb_msg_add_sid(ac
->msg
, "objectSid", sid
)) {
254 return ldb_operr(ldb
);
257 return samldb_next_step(ac
);
261 see if a krbtgt_number is available
263 static bool samldb_krbtgtnumber_available(struct samldb_ctx
*ac
,
264 uint32_t krbtgt_number
)
266 TALLOC_CTX
*tmp_ctx
= talloc_new(ac
);
267 struct ldb_result
*res
;
268 const char * const no_attrs
[] = { NULL
};
271 ret
= dsdb_module_search(ac
->module
, tmp_ctx
, &res
,
272 ldb_get_default_basedn(ldb_module_get_ctx(ac
->module
)),
273 LDB_SCOPE_SUBTREE
, no_attrs
,
274 DSDB_FLAG_NEXT_MODULE
,
276 "(msDC-SecondaryKrbTgtNumber=%u)",
278 if (ret
== LDB_SUCCESS
&& res
->count
== 0) {
279 talloc_free(tmp_ctx
);
282 talloc_free(tmp_ctx
);
286 /* special handling for add in RODC join */
287 static int samldb_rodc_add(struct samldb_ctx
*ac
)
289 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
290 uint32_t krbtgt_number
, i_start
, i
;
293 struct ldb_val newpass_utf16
;
295 /* find a unused msDC-SecondaryKrbTgtNumber */
296 i_start
= generate_random() & 0xFFFF;
301 for (i
=i_start
; i
<=0xFFFF; i
++) {
302 if (samldb_krbtgtnumber_available(ac
, i
)) {
307 for (i
=1; i
<i_start
; i
++) {
308 if (samldb_krbtgtnumber_available(ac
, i
)) {
314 ldb_asprintf_errstring(ldb
,
315 "%08X: Unable to find available msDS-SecondaryKrbTgtNumber",
316 W_ERROR_V(WERR_NO_SYSTEM_RESOURCES
));
317 return LDB_ERR_OTHER
;
320 ret
= ldb_msg_add_empty(ac
->msg
, "msDS-SecondaryKrbTgtNumber",
321 LDB_FLAG_INTERNAL_DISABLE_VALIDATION
, NULL
);
322 if (ret
!= LDB_SUCCESS
) {
323 return ldb_operr(ldb
);
326 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
327 "msDS-SecondaryKrbTgtNumber", krbtgt_number
);
328 if (ret
!= LDB_SUCCESS
) {
329 return ldb_operr(ldb
);
332 ret
= ldb_msg_add_fmt(ac
->msg
, "sAMAccountName", "krbtgt_%u",
334 if (ret
!= LDB_SUCCESS
) {
335 return ldb_operr(ldb
);
338 newpass
= generate_random_password(ac
->msg
, 128, 255);
339 if (newpass
== NULL
) {
340 return ldb_operr(ldb
);
343 if (!convert_string_talloc(ac
,
345 newpass
, strlen(newpass
),
346 (void *)&newpass_utf16
.data
,
347 &newpass_utf16
.length
)) {
348 ldb_asprintf_errstring(ldb
,
350 "failed to generate UTF16 password from random password");
351 return LDB_ERR_OPERATIONS_ERROR
;
353 ret
= ldb_msg_add_steal_value(ac
->msg
, "clearTextPassword", &newpass_utf16
);
354 if (ret
!= LDB_SUCCESS
) {
355 return ldb_operr(ldb
);
358 return samldb_next_step(ac
);
361 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx
*ac
)
363 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
364 struct ldb_result
*res
;
365 const char * const no_attrs
[] = { NULL
};
370 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
371 ac
->dn
, LDB_SCOPE_BASE
, no_attrs
,
372 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
373 | DSDB_FLAG_NEXT_MODULE
,
375 "(objectClass=classSchema)");
376 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
377 /* Don't be pricky when the DN doesn't exist if we have the */
378 /* RELAX control specified */
379 if (ldb_request_get_control(ac
->req
,
380 LDB_CONTROL_RELAX_OID
) == NULL
) {
381 ldb_set_errstring(ldb
,
382 "samldb_find_defaultObjectCategory: "
383 "Invalid DN for 'defaultObjectCategory'!");
384 return LDB_ERR_CONSTRAINT_VIOLATION
;
387 if ((ret
!= LDB_ERR_NO_SUCH_OBJECT
) && (ret
!= LDB_SUCCESS
)) {
391 if (ret
== LDB_SUCCESS
) {
392 /* ensure the defaultObjectCategory has a full GUID */
393 struct ldb_message
*m
;
394 m
= ldb_msg_new(ac
->msg
);
399 if (ldb_msg_add_string(m
, "defaultObjectCategory",
400 ldb_dn_get_extended_linearized(m
, res
->msgs
[0]->dn
, 1)) !=
404 m
->elements
[0].flags
= LDB_FLAG_MOD_REPLACE
;
406 ret
= dsdb_module_modify(ac
->module
, m
,
407 DSDB_FLAG_NEXT_MODULE
,
409 if (ret
!= LDB_SUCCESS
) {
417 return samldb_next_step(ac
);
421 * msDS-IntId attributeSchema attribute handling
422 * during LDB_ADD request processing
424 static int samldb_add_handle_msDS_IntId(struct samldb_ctx
*ac
)
429 int32_t system_flags
;
430 struct ldb_context
*ldb
;
431 struct ldb_result
*ldb_res
;
432 struct ldb_dn
*schema_dn
;
433 struct samldb_msds_intid_persistant
*msds_intid_struct
;
434 struct dsdb_schema
*schema
;
436 ldb
= ldb_module_get_ctx(ac
->module
);
437 schema_dn
= ldb_get_schema_basedn(ldb
);
439 /* replicated update should always go through */
440 if (ldb_request_get_control(ac
->req
,
441 DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
445 /* msDS-IntId is handled by system and should never be
446 * passed by clients */
447 if (ldb_msg_find_element(ac
->msg
, "msDS-IntId")) {
448 return LDB_ERR_UNWILLING_TO_PERFORM
;
451 /* do not generate msDS-IntId if Relax control is passed */
452 if (ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
)) {
456 /* check Functional Level */
457 if (dsdb_functional_level(ldb
) < DS_DOMAIN_FUNCTION_2003
) {
461 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
462 system_flags
= ldb_msg_find_attr_as_int(ac
->msg
, "systemFlags", 0);
463 if (system_flags
& SYSTEM_FLAG_SCHEMA_BASE_OBJECT
) {
466 schema
= dsdb_get_schema(ldb
, NULL
);
468 ldb_debug_set(ldb
, LDB_DEBUG_FATAL
,
469 "samldb_schema_info_update: no dsdb_schema loaded");
470 DEBUG(0,(__location__
": %s\n", ldb_errstring(ldb
)));
471 return ldb_operr(ldb
);
474 msds_intid_struct
= (struct samldb_msds_intid_persistant
*) ldb_get_opaque(ldb
, SAMLDB_MSDS_INTID_OPAQUE
);
475 if (!msds_intid_struct
) {
476 msds_intid_struct
= talloc(ldb
, struct samldb_msds_intid_persistant
);
477 /* Generate new value for msDs-IntId
478 * Value should be in 0x80000000..0xBFFFFFFF range */
479 msds_intid
= generate_random() % 0X3FFFFFFF;
480 msds_intid
+= 0x80000000;
481 msds_intid_struct
->msds_intid
= msds_intid
;
482 msds_intid_struct
->usn
= schema
->loaded_usn
;
483 DEBUG(2, ("No samldb_msds_intid_persistant struct, allocating a new one\n"));
485 msds_intid
= msds_intid_struct
->msds_intid
;
488 /* probe id values until unique one is found */
490 uint64_t current_usn
;
492 if (msds_intid
> 0xBFFFFFFF) {
493 msds_intid
= 0x80000001;
496 * Alternative strategy to a costly (even indexed search) to the
498 * We search in the schema if we have already this intid (using dsdb_attribute_by_attributeID_id because
499 * in the range 0x80000000 0xBFFFFFFFF, attributeID is a DSDB_ATTID_TYPE_INTID).
500 * If so generate another random value.
501 * If not check if the highest USN in the database for the schema partition is the
503 * If so it means that's only this ldb context that is touching the schema in the database.
504 * If not it means that's someone else has modified the database while we are doing our changes too
505 * (this case should be very bery rare) in order to be sure do the search in the database.
507 if (dsdb_attribute_by_attributeID_id(schema
, msds_intid
)) {
508 msds_intid
= generate_random() % 0X3FFFFFFF;
509 msds_intid
+= 0x80000000;
513 ret
= dsdb_module_load_partition_usn(ac
->module
, schema
->base_dn
, ¤t_usn
, NULL
, NULL
);
514 if (ret
!= LDB_SUCCESS
) {
515 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
516 __location__
": Searching for schema USN failed: %s\n",
518 return ldb_operr(ldb
);
521 /* current_usn can be lesser than msds_intid_struct-> if there is
522 * uncommited changes.
524 if (current_usn
> msds_intid_struct
->usn
) {
525 /* oups something has changed, someone/something
526 * else is modifying or has modified the schema
527 * we'd better check this intid is the database directly
530 DEBUG(2, ("Schema has changed, searching the database for the unicity of %d\n",
533 ret
= dsdb_module_search(ac
->module
, ac
,
535 schema_dn
, LDB_SCOPE_ONELEVEL
, NULL
,
536 DSDB_FLAG_NEXT_MODULE
,
538 "(msDS-IntId=%d)", msds_intid
);
539 if (ret
!= LDB_SUCCESS
) {
540 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
541 __location__
": Searching for msDS-IntId=%d failed - %s\n",
544 return ldb_operr(ldb
);
546 id_exists
= (ldb_res
->count
> 0);
547 talloc_free(ldb_res
);
553 msds_intid_struct
->msds_intid
= msds_intid
;
554 ldb_set_opaque(ldb
, SAMLDB_MSDS_INTID_OPAQUE
, msds_intid_struct
);
556 return samdb_msg_add_int(ldb
, ac
->msg
, ac
->msg
, "msDS-IntId",
562 * samldb_add_entry (async)
565 static int samldb_add_entry_callback(struct ldb_request
*req
,
566 struct ldb_reply
*ares
)
568 struct ldb_context
*ldb
;
569 struct samldb_ctx
*ac
;
572 ac
= talloc_get_type(req
->context
, struct samldb_ctx
);
573 ldb
= ldb_module_get_ctx(ac
->module
);
576 return ldb_module_done(ac
->req
, NULL
, NULL
,
577 LDB_ERR_OPERATIONS_ERROR
);
580 if (ares
->type
== LDB_REPLY_REFERRAL
) {
581 return ldb_module_send_referral(ac
->req
, ares
->referral
);
584 if (ares
->error
!= LDB_SUCCESS
) {
585 return ldb_module_done(ac
->req
, ares
->controls
,
586 ares
->response
, ares
->error
);
588 if (ares
->type
!= LDB_REPLY_DONE
) {
589 ldb_asprintf_errstring(ldb
, "Invalid LDB reply type %d", ares
->type
);
590 return ldb_module_done(ac
->req
, NULL
, NULL
,
591 LDB_ERR_OPERATIONS_ERROR
);
594 /* The caller may wish to get controls back from the add */
595 ac
->ares
= talloc_steal(ac
, ares
);
597 ret
= samldb_next_step(ac
);
598 if (ret
!= LDB_SUCCESS
) {
599 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
604 static int samldb_add_entry(struct samldb_ctx
*ac
)
606 struct ldb_context
*ldb
;
607 struct ldb_request
*req
;
610 ldb
= ldb_module_get_ctx(ac
->module
);
612 ret
= ldb_build_add_req(&req
, ldb
, ac
,
615 ac
, samldb_add_entry_callback
,
617 LDB_REQ_SET_LOCATION(req
);
618 if (ret
!= LDB_SUCCESS
) {
622 return ldb_next_request(ac
->module
, req
);
626 * return true if msg carries an attributeSchema that is intended to be RODC
627 * filtered but is also a system-critical attribute.
629 static bool check_rodc_critical_attribute(struct ldb_message
*msg
)
631 uint32_t schemaFlagsEx
, searchFlags
, rodc_filtered_flags
;
633 schemaFlagsEx
= ldb_msg_find_attr_as_uint(msg
, "schemaFlagsEx", 0);
634 searchFlags
= ldb_msg_find_attr_as_uint(msg
, "searchFlags", 0);
635 rodc_filtered_flags
= (SEARCH_FLAG_RODC_ATTRIBUTE
636 | SEARCH_FLAG_CONFIDENTIAL
);
638 if ((schemaFlagsEx
& SCHEMA_FLAG_ATTR_IS_CRITICAL
) &&
639 ((searchFlags
& rodc_filtered_flags
) == rodc_filtered_flags
)) {
647 static int samldb_fill_object(struct samldb_ctx
*ac
)
649 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
652 /* Add information for the different account types */
654 case SAMLDB_TYPE_USER
: {
655 struct ldb_control
*rodc_control
= ldb_request_get_control(ac
->req
,
656 LDB_CONTROL_RODC_DCPROMO_OID
);
657 if (rodc_control
!= NULL
) {
658 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
659 rodc_control
->critical
= false;
660 ret
= samldb_add_step(ac
, samldb_rodc_add
);
661 if (ret
!= LDB_SUCCESS
) return ret
;
664 /* check if we have a valid sAMAccountName */
665 ret
= samldb_add_step(ac
, samldb_check_sAMAccountName
);
666 if (ret
!= LDB_SUCCESS
) return ret
;
668 ret
= samldb_add_step(ac
, samldb_add_entry
);
669 if (ret
!= LDB_SUCCESS
) return ret
;
673 case SAMLDB_TYPE_GROUP
: {
674 /* check if we have a valid sAMAccountName */
675 ret
= samldb_add_step(ac
, samldb_check_sAMAccountName
);
676 if (ret
!= LDB_SUCCESS
) return ret
;
678 ret
= samldb_add_step(ac
, samldb_add_entry
);
679 if (ret
!= LDB_SUCCESS
) return ret
;
683 case SAMLDB_TYPE_CLASS
: {
684 const struct ldb_val
*rdn_value
, *def_obj_cat_val
;
685 unsigned int v
= ldb_msg_find_attr_as_uint(ac
->msg
, "objectClassCategory", -2);
687 /* As discussed with Microsoft through dochelp in April 2012 this is the behavior of windows*/
688 if (!ldb_msg_find_element(ac
->msg
, "subClassOf")) {
689 ret
= ldb_msg_add_string(ac
->msg
, "subClassOf", "top");
690 if (ret
!= LDB_SUCCESS
) return ret
;
693 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
695 if (ret
!= LDB_SUCCESS
) return ret
;
697 /* do not allow to mark an attributeSchema as RODC filtered if it
698 * is system-critical */
699 if (check_rodc_critical_attribute(ac
->msg
)) {
700 ldb_asprintf_errstring(ldb
, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
701 ldb_dn_get_linearized(ac
->msg
->dn
));
702 return LDB_ERR_UNWILLING_TO_PERFORM
;
705 rdn_value
= ldb_dn_get_rdn_val(ac
->msg
->dn
);
706 if (rdn_value
== NULL
) {
707 return ldb_operr(ldb
);
709 if (!ldb_msg_find_element(ac
->msg
, "lDAPDisplayName")) {
710 /* the RDN has prefix "CN" */
711 ret
= ldb_msg_add_string(ac
->msg
, "lDAPDisplayName",
712 samdb_cn_to_lDAPDisplayName(ac
->msg
,
713 (const char *) rdn_value
->data
));
714 if (ret
!= LDB_SUCCESS
) {
720 if (!ldb_msg_find_element(ac
->msg
, "schemaIDGUID")) {
723 guid
= GUID_random();
724 ret
= dsdb_msg_add_guid(ac
->msg
, &guid
, "schemaIDGUID");
725 if (ret
!= LDB_SUCCESS
) {
731 def_obj_cat_val
= ldb_msg_find_ldb_val(ac
->msg
,
732 "defaultObjectCategory");
733 if (def_obj_cat_val
!= NULL
) {
734 /* "defaultObjectCategory" has been set by the caller.
735 * Do some checks for consistency.
736 * NOTE: The real constraint check (that
737 * 'defaultObjectCategory' is the DN of the new
738 * objectclass or any parent of it) is still incomplete.
739 * For now we say that 'defaultObjectCategory' is valid
740 * if it exists and it is of objectclass "classSchema".
742 ac
->dn
= ldb_dn_from_ldb_val(ac
, ldb
, def_obj_cat_val
);
743 if (ac
->dn
== NULL
) {
744 ldb_set_errstring(ldb
,
745 "Invalid DN for 'defaultObjectCategory'!");
746 return LDB_ERR_CONSTRAINT_VIOLATION
;
749 /* "defaultObjectCategory" has not been set by the
750 * caller. Use the entry DN for it. */
751 ac
->dn
= ac
->msg
->dn
;
753 ret
= ldb_msg_add_string(ac
->msg
, "defaultObjectCategory",
754 ldb_dn_alloc_linearized(ac
->msg
, ac
->dn
));
755 if (ret
!= LDB_SUCCESS
) {
761 ret
= samldb_add_step(ac
, samldb_add_entry
);
762 if (ret
!= LDB_SUCCESS
) return ret
;
764 /* Now perform the checks for the 'defaultObjectCategory'. The
765 * lookup DN was already saved in "ac->dn" */
766 ret
= samldb_add_step(ac
, samldb_find_for_defaultObjectCategory
);
767 if (ret
!= LDB_SUCCESS
) return ret
;
769 /* -2 is not a valid objectClassCategory so it means the attribute wasn't present */
771 /* Windows 2003 does this*/
772 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
, "objectClassCategory", 0);
773 if (ret
!= LDB_SUCCESS
) {
780 case SAMLDB_TYPE_ATTRIBUTE
: {
781 const struct ldb_val
*rdn_value
;
782 struct ldb_message_element
*el
;
783 rdn_value
= ldb_dn_get_rdn_val(ac
->msg
->dn
);
784 if (rdn_value
== NULL
) {
785 return ldb_operr(ldb
);
787 if (!ldb_msg_find_element(ac
->msg
, "lDAPDisplayName")) {
788 /* the RDN has prefix "CN" */
789 ret
= ldb_msg_add_string(ac
->msg
, "lDAPDisplayName",
790 samdb_cn_to_lDAPDisplayName(ac
->msg
,
791 (const char *) rdn_value
->data
));
792 if (ret
!= LDB_SUCCESS
) {
798 /* do not allow to mark an attributeSchema as RODC filtered if it
799 * is system-critical */
800 if (check_rodc_critical_attribute(ac
->msg
)) {
801 ldb_asprintf_errstring(ldb
,
802 "samldb: refusing schema add of %s - cannot combine critical attribute with RODC filtering",
803 ldb_dn_get_linearized(ac
->msg
->dn
));
804 return LDB_ERR_UNWILLING_TO_PERFORM
;
807 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
808 "isSingleValued", "FALSE");
809 if (ret
!= LDB_SUCCESS
) return ret
;
811 if (!ldb_msg_find_element(ac
->msg
, "schemaIDGUID")) {
814 guid
= GUID_random();
815 ret
= dsdb_msg_add_guid(ac
->msg
, &guid
, "schemaIDGUID");
816 if (ret
!= LDB_SUCCESS
) {
822 el
= ldb_msg_find_element(ac
->msg
, "attributeSyntax");
825 * No need to scream if there isn't as we have code later on
826 * that will take care of it.
828 const struct dsdb_syntax
*syntax
= find_syntax_map_by_ad_oid((const char *)el
->values
[0].data
);
830 DEBUG(9, ("Can't find dsdb_syntax object for attributeSyntax %s\n",
831 (const char *)el
->values
[0].data
));
833 unsigned int v
= ldb_msg_find_attr_as_uint(ac
->msg
, "oMSyntax", 0);
834 const struct ldb_val
*val
= ldb_msg_find_ldb_val(ac
->msg
, "oMObjectClass");
837 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
, "oMSyntax", syntax
->oMSyntax
);
838 if (ret
!= LDB_SUCCESS
) {
843 struct ldb_val val2
= ldb_val_dup(ldb
, &syntax
->oMObjectClass
);
844 if (val2
.length
> 0) {
845 ret
= ldb_msg_add_value(ac
->msg
, "oMObjectClass", &val2
, NULL
);
846 if (ret
!= LDB_SUCCESS
) {
854 /* handle msDS-IntID attribute */
855 ret
= samldb_add_handle_msDS_IntId(ac
);
856 if (ret
!= LDB_SUCCESS
) return ret
;
858 ret
= samldb_add_step(ac
, samldb_add_entry
);
859 if (ret
!= LDB_SUCCESS
) return ret
;
864 ldb_asprintf_errstring(ldb
, "Invalid entry type!");
865 return LDB_ERR_OPERATIONS_ERROR
;
869 return samldb_first_step(ac
);
872 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx
*ac
)
874 struct ldb_context
*ldb
;
875 const struct ldb_val
*rdn_value
;
879 ldb
= ldb_module_get_ctx(ac
->module
);
881 sid
= samdb_result_dom_sid(ac
->msg
, ac
->msg
, "objectSid");
883 rdn_value
= ldb_dn_get_rdn_val(ac
->msg
->dn
);
884 if (rdn_value
== NULL
) {
885 return ldb_operr(ldb
);
887 sid
= dom_sid_parse_talloc(ac
->msg
,
888 (const char *)rdn_value
->data
);
890 ldb_set_errstring(ldb
,
891 "samldb: No valid SID found in ForeignSecurityPrincipal CN!");
892 return LDB_ERR_CONSTRAINT_VIOLATION
;
894 if (! samldb_msg_add_sid(ac
->msg
, "objectSid", sid
)) {
895 return ldb_operr(ldb
);
899 /* finally proceed with adding the entry */
900 ret
= samldb_add_step(ac
, samldb_add_entry
);
901 if (ret
!= LDB_SUCCESS
) return ret
;
903 return samldb_first_step(ac
);
906 static int samldb_schema_info_update(struct samldb_ctx
*ac
)
909 struct ldb_context
*ldb
;
910 struct dsdb_schema
*schema
;
912 /* replicated update should always go through */
913 if (ldb_request_get_control(ac
->req
,
914 DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
918 /* do not update schemaInfo during provisioning */
919 if (ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
)) {
923 ldb
= ldb_module_get_ctx(ac
->module
);
924 schema
= dsdb_get_schema(ldb
, NULL
);
926 ldb_debug_set(ldb
, LDB_DEBUG_FATAL
,
927 "samldb_schema_info_update: no dsdb_schema loaded");
928 DEBUG(0,(__location__
": %s\n", ldb_errstring(ldb
)));
929 return ldb_operr(ldb
);
932 ret
= dsdb_module_schema_info_update(ac
->module
, schema
,
933 DSDB_FLAG_NEXT_MODULE
|
936 if (ret
!= LDB_SUCCESS
) {
937 ldb_asprintf_errstring(ldb
,
938 "samldb_schema_info_update: dsdb_module_schema_info_update failed with %s",
946 static int samldb_prim_group_tester(struct samldb_ctx
*ac
, uint32_t rid
);
947 static int samldb_check_user_account_control_acl(struct samldb_ctx
*ac
,
949 uint32_t user_account_control
,
950 uint32_t user_account_control_old
);
953 * "Objectclass" trigger (MS-SAMR 3.1.1.8.1)
955 * Has to be invoked on "add" and "modify" operations on "user", "computer" and
957 * ac->msg contains the "add"/"modify" message
958 * ac->type contains the object type (main objectclass)
960 static int samldb_objectclass_trigger(struct samldb_ctx
*ac
)
962 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
963 void *skip_allocate_sids
= ldb_get_opaque(ldb
,
964 "skip_allocate_sids");
965 struct ldb_message_element
*el
, *el2
;
969 /* make sure that "sAMAccountType" is not specified */
970 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
972 ldb_set_errstring(ldb
,
973 "samldb: sAMAccountType must not be specified!");
974 return LDB_ERR_UNWILLING_TO_PERFORM
;
977 /* Step 1: objectSid assignment */
979 /* Don't allow the objectSid to be changed. But beside the RELAX
980 * control we have also to guarantee that it can always be set with
981 * SYSTEM permissions. This is needed for the "samba3sam" backend. */
982 sid
= samdb_result_dom_sid(ac
, ac
->msg
, "objectSid");
983 if ((sid
!= NULL
) && (!dsdb_module_am_system(ac
->module
)) &&
984 (ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
) == NULL
)) {
985 ldb_set_errstring(ldb
,
986 "samldb: objectSid must not be specified!");
987 return LDB_ERR_UNWILLING_TO_PERFORM
;
990 /* but generate a new SID when we do have an add operations */
991 if ((sid
== NULL
) && (ac
->req
->operation
== LDB_ADD
) && !skip_allocate_sids
) {
992 ret
= samldb_add_step(ac
, samldb_allocate_sid
);
993 if (ret
!= LDB_SUCCESS
) return ret
;
997 case SAMLDB_TYPE_USER
: {
998 bool uac_generated
= false;
1000 /* Step 1.2: Default values */
1001 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1002 "accountExpires", "9223372036854775807");
1003 if (ret
!= LDB_SUCCESS
) return ret
;
1004 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1005 "badPasswordTime", "0");
1006 if (ret
!= LDB_SUCCESS
) return ret
;
1007 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1008 "badPwdCount", "0");
1009 if (ret
!= LDB_SUCCESS
) return ret
;
1010 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1012 if (ret
!= LDB_SUCCESS
) return ret
;
1013 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1014 "countryCode", "0");
1015 if (ret
!= LDB_SUCCESS
) return ret
;
1016 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1018 if (ret
!= LDB_SUCCESS
) return ret
;
1019 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1021 if (ret
!= LDB_SUCCESS
) return ret
;
1022 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1024 if (ret
!= LDB_SUCCESS
) return ret
;
1025 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1027 if (ret
!= LDB_SUCCESS
) return ret
;
1029 /* On add operations we might need to generate a
1030 * "userAccountControl" (if it isn't specified). */
1031 el
= ldb_msg_find_element(ac
->msg
, "userAccountControl");
1032 if ((el
== NULL
) && (ac
->req
->operation
== LDB_ADD
)) {
1033 ret
= samdb_msg_set_uint(ldb
, ac
->msg
, ac
->msg
,
1034 "userAccountControl",
1036 if (ret
!= LDB_SUCCESS
) {
1039 uac_generated
= true;
1042 el
= ldb_msg_find_element(ac
->msg
, "userAccountControl");
1044 uint32_t user_account_control
, account_type
;
1045 /* Step 1.3: "userAccountControl" -> "sAMAccountType" mapping */
1046 user_account_control
= ldb_msg_find_attr_as_uint(ac
->msg
,
1047 "userAccountControl",
1050 /* Temporary duplicate accounts aren't allowed */
1051 if ((user_account_control
& UF_TEMP_DUPLICATE_ACCOUNT
) != 0) {
1052 return LDB_ERR_OTHER
;
1055 /* Workstation and (read-only) DC objects do need objectclass "computer" */
1056 if ((samdb_find_attribute(ldb
, ac
->msg
,
1057 "objectclass", "computer") == NULL
) &&
1058 (user_account_control
&
1059 (UF_SERVER_TRUST_ACCOUNT
| UF_WORKSTATION_TRUST_ACCOUNT
))) {
1060 ldb_set_errstring(ldb
,
1061 "samldb: Requested account type does need objectclass 'computer'!");
1062 return LDB_ERR_OBJECT_CLASS_VIOLATION
;
1065 account_type
= ds_uf2atype(user_account_control
);
1066 if (account_type
== 0) {
1067 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
1068 return LDB_ERR_UNWILLING_TO_PERFORM
;
1070 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1073 if (ret
!= LDB_SUCCESS
) {
1076 el2
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
1077 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1079 /* "isCriticalSystemObject" might be set */
1080 if (user_account_control
&
1081 (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
)) {
1082 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
1084 if (ret
!= LDB_SUCCESS
) {
1087 el2
= ldb_msg_find_element(ac
->msg
,
1088 "isCriticalSystemObject");
1089 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1090 } else if (user_account_control
& UF_WORKSTATION_TRUST_ACCOUNT
) {
1091 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
1093 if (ret
!= LDB_SUCCESS
) {
1096 el2
= ldb_msg_find_element(ac
->msg
,
1097 "isCriticalSystemObject");
1098 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1101 /* Step 1.4: "userAccountControl" -> "primaryGroupID" mapping */
1102 if (!ldb_msg_find_element(ac
->msg
, "primaryGroupID")) {
1103 uint32_t rid
= ds_uf2prim_group_rid(user_account_control
);
1106 * Older AD deployments don't know about the
1109 if (rid
== DOMAIN_RID_READONLY_DCS
) {
1110 ret
= samldb_prim_group_tester(ac
, rid
);
1111 if (ret
!= LDB_SUCCESS
) {
1116 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1117 "primaryGroupID", rid
);
1118 if (ret
!= LDB_SUCCESS
) {
1121 el2
= ldb_msg_find_element(ac
->msg
,
1123 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1126 /* Step 1.5: Add additional flags when needed */
1127 /* Obviously this is done when the "userAccountControl"
1128 * has been generated here (tested against Windows
1130 if (uac_generated
) {
1131 user_account_control
|= UF_ACCOUNTDISABLE
;
1132 user_account_control
|= UF_PASSWD_NOTREQD
;
1134 ret
= samdb_msg_set_uint(ldb
, ac
->msg
, ac
->msg
,
1135 "userAccountControl",
1136 user_account_control
);
1137 if (ret
!= LDB_SUCCESS
) {
1142 ret
= samldb_check_user_account_control_acl(ac
, NULL
,
1143 user_account_control
, 0);
1144 if (ret
!= LDB_SUCCESS
) {
1151 case SAMLDB_TYPE_GROUP
: {
1152 const char *tempstr
;
1154 /* Step 2.2: Default values */
1155 tempstr
= talloc_asprintf(ac
->msg
, "%d",
1156 GTYPE_SECURITY_GLOBAL_GROUP
);
1157 if (tempstr
== NULL
) return ldb_operr(ldb
);
1158 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1159 "groupType", tempstr
);
1160 if (ret
!= LDB_SUCCESS
) return ret
;
1162 /* Step 2.3: "groupType" -> "sAMAccountType" */
1163 el
= ldb_msg_find_element(ac
->msg
, "groupType");
1165 uint32_t group_type
, account_type
;
1167 group_type
= ldb_msg_find_attr_as_uint(ac
->msg
,
1170 /* The creation of builtin groups requires the
1172 if (group_type
== GTYPE_SECURITY_BUILTIN_LOCAL_GROUP
) {
1173 if (ldb_request_get_control(ac
->req
,
1174 LDB_CONTROL_RELAX_OID
) == NULL
) {
1175 return LDB_ERR_UNWILLING_TO_PERFORM
;
1179 account_type
= ds_gtype2atype(group_type
);
1180 if (account_type
== 0) {
1181 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
1182 return LDB_ERR_UNWILLING_TO_PERFORM
;
1184 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1187 if (ret
!= LDB_SUCCESS
) {
1190 el2
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
1191 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1197 ldb_asprintf_errstring(ldb
,
1198 "Invalid entry type!");
1199 return LDB_ERR_OPERATIONS_ERROR
;
1207 * "Primary group ID" trigger (MS-SAMR 3.1.1.8.2)
1209 * Has to be invoked on "add" and "modify" operations on "user" and "computer"
1211 * ac->msg contains the "add"/"modify" message
1214 static int samldb_prim_group_tester(struct samldb_ctx
*ac
, uint32_t rid
)
1216 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1217 struct dom_sid
*sid
;
1218 struct ldb_result
*res
;
1220 const char * const noattrs
[] = { NULL
};
1222 sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), rid
);
1224 return ldb_operr(ldb
);
1227 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
1228 ldb_get_default_basedn(ldb
),
1230 noattrs
, DSDB_FLAG_NEXT_MODULE
,
1233 ldap_encode_ndr_dom_sid(ac
, sid
));
1234 if (ret
!= LDB_SUCCESS
) {
1237 if (res
->count
!= 1) {
1239 ldb_asprintf_errstring(ldb
,
1240 "Failed to find primary group with RID %u!",
1242 return LDB_ERR_UNWILLING_TO_PERFORM
;
1249 static int samldb_prim_group_set(struct samldb_ctx
*ac
)
1251 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1254 rid
= ldb_msg_find_attr_as_uint(ac
->msg
, "primaryGroupID", (uint32_t) -1);
1255 if (rid
== (uint32_t) -1) {
1256 /* we aren't affected of any primary group set */
1259 } else if (!ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
)) {
1260 ldb_set_errstring(ldb
,
1261 "The primary group isn't settable on add operations!");
1262 return LDB_ERR_UNWILLING_TO_PERFORM
;
1265 return samldb_prim_group_tester(ac
, rid
);
1268 static int samldb_prim_group_change(struct samldb_ctx
*ac
)
1270 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1271 const char * const attrs
[] = { "primaryGroupID", "memberOf", NULL
};
1272 struct ldb_result
*res
, *group_res
;
1273 struct ldb_message_element
*el
;
1274 struct ldb_message
*msg
;
1275 uint32_t prev_rid
, new_rid
;
1276 struct dom_sid
*prev_sid
, *new_sid
;
1277 struct ldb_dn
*prev_prim_group_dn
, *new_prim_group_dn
;
1279 const char * const noattrs
[] = { NULL
};
1281 el
= dsdb_get_single_valued_attr(ac
->msg
, "primaryGroupID",
1282 ac
->req
->operation
);
1284 /* we are not affected */
1288 /* Fetch information from the existing object */
1290 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, attrs
,
1291 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1292 if (ret
!= LDB_SUCCESS
) {
1296 /* Finds out the DN of the old primary group */
1298 prev_rid
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "primaryGroupID",
1300 if (prev_rid
== (uint32_t) -1) {
1301 /* User objects do always have a mandatory "primaryGroupID"
1302 * attribute. If this doesn't exist then the object is of the
1303 * wrong type. This is the exact Windows error code */
1304 return LDB_ERR_OBJECT_CLASS_VIOLATION
;
1307 prev_sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), prev_rid
);
1308 if (prev_sid
== NULL
) {
1309 return ldb_operr(ldb
);
1312 /* Finds out the DN of the new primary group
1313 * Notice: in order to parse the primary group ID correctly we create
1314 * a temporary message here. */
1316 msg
= ldb_msg_new(ac
->msg
);
1318 return ldb_module_oom(ac
->module
);
1320 ret
= ldb_msg_add(msg
, el
, 0);
1321 if (ret
!= LDB_SUCCESS
) {
1324 new_rid
= ldb_msg_find_attr_as_uint(msg
, "primaryGroupID", (uint32_t) -1);
1326 if (new_rid
== (uint32_t) -1) {
1327 /* we aren't affected of any primary group change */
1331 if (prev_rid
== new_rid
) {
1335 ret
= dsdb_module_search(ac
->module
, ac
, &group_res
,
1336 ldb_get_default_basedn(ldb
),
1338 noattrs
, DSDB_FLAG_NEXT_MODULE
,
1341 ldap_encode_ndr_dom_sid(ac
, prev_sid
));
1342 if (ret
!= LDB_SUCCESS
) {
1345 if (group_res
->count
!= 1) {
1346 return ldb_operr(ldb
);
1348 prev_prim_group_dn
= group_res
->msgs
[0]->dn
;
1350 new_sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), new_rid
);
1351 if (new_sid
== NULL
) {
1352 return ldb_operr(ldb
);
1355 ret
= dsdb_module_search(ac
->module
, ac
, &group_res
,
1356 ldb_get_default_basedn(ldb
),
1358 noattrs
, DSDB_FLAG_NEXT_MODULE
,
1361 ldap_encode_ndr_dom_sid(ac
, new_sid
));
1362 if (ret
!= LDB_SUCCESS
) {
1365 if (group_res
->count
!= 1) {
1366 /* Here we know if the specified new primary group candidate is
1368 return LDB_ERR_UNWILLING_TO_PERFORM
;
1370 new_prim_group_dn
= group_res
->msgs
[0]->dn
;
1372 /* We need to be already a normal member of the new primary
1373 * group in order to be successful. */
1374 el
= samdb_find_attribute(ldb
, res
->msgs
[0], "memberOf",
1375 ldb_dn_get_linearized(new_prim_group_dn
));
1377 return LDB_ERR_UNWILLING_TO_PERFORM
;
1380 /* Remove the "member" attribute on the new primary group */
1381 msg
= ldb_msg_new(ac
->msg
);
1383 return ldb_module_oom(ac
->module
);
1385 msg
->dn
= new_prim_group_dn
;
1387 ret
= samdb_msg_add_delval(ldb
, msg
, msg
, "member",
1388 ldb_dn_get_linearized(ac
->msg
->dn
));
1389 if (ret
!= LDB_SUCCESS
) {
1393 ret
= dsdb_module_modify(ac
->module
, msg
, DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1394 if (ret
!= LDB_SUCCESS
) {
1399 /* Add a "member" attribute for the previous primary group */
1400 msg
= ldb_msg_new(ac
->msg
);
1402 return ldb_module_oom(ac
->module
);
1404 msg
->dn
= prev_prim_group_dn
;
1406 ret
= samdb_msg_add_addval(ldb
, msg
, msg
, "member",
1407 ldb_dn_get_linearized(ac
->msg
->dn
));
1408 if (ret
!= LDB_SUCCESS
) {
1412 ret
= dsdb_module_modify(ac
->module
, msg
, DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1413 if (ret
!= LDB_SUCCESS
) {
1421 static int samldb_prim_group_trigger(struct samldb_ctx
*ac
)
1425 if (ac
->req
->operation
== LDB_ADD
) {
1426 ret
= samldb_prim_group_set(ac
);
1428 ret
= samldb_prim_group_change(ac
);
1435 * Validate that the restriction in point 5 of MS-SAMR 3.1.1.8.10 userAccountControl is honoured
1438 static int samldb_check_user_account_control_acl(struct samldb_ctx
*ac
,
1439 struct dom_sid
*sid
,
1440 uint32_t user_account_control
,
1441 uint32_t user_account_control_old
)
1444 bool need_acl_check
= false;
1445 struct ldb_result
*res
;
1446 const char * const sd_attrs
[] = {"ntSecurityDescriptor", NULL
};
1447 struct security_token
*user_token
;
1448 struct security_descriptor
*domain_sd
;
1449 struct ldb_dn
*domain_dn
= ldb_get_default_basedn(ldb_module_get_ctx(ac
->module
));
1450 const struct uac_to_guid
{
1454 enum sec_privilege privilege
;
1455 bool delete_is_privileged
;
1456 const char *error_string
;
1459 .uac
= UF_PASSWD_NOTREQD
,
1460 .guid
= GUID_DRS_UPDATE_PASSWORD_NOT_REQUIRED_BIT
,
1461 .error_string
= "Adding the UF_PASSWD_NOTREQD bit in userAccountControl requires the Update-Password-Not-Required-Bit right that was not given on the Domain object"
1464 .uac
= UF_DONT_EXPIRE_PASSWD
,
1465 .guid
= GUID_DRS_UNEXPIRE_PASSWORD
,
1466 .error_string
= "Adding the UF_DONT_EXPIRE_PASSWD bit in userAccountControl requires the Unexpire-Password right that was not given on the Domain object"
1469 .uac
= UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED
,
1470 .guid
= GUID_DRS_ENABLE_PER_USER_REVERSIBLY_ENCRYPTED_PASSWORD
,
1471 .error_string
= "Adding the UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED bit in userAccountControl requires the Enable-Per-User-Reversibly-Encrypted-Password right that was not given on the Domain object"
1474 .uac
= UF_SERVER_TRUST_ACCOUNT
,
1475 .guid
= GUID_DRS_DS_INSTALL_REPLICA
,
1476 .error_string
= "Adding the UF_SERVER_TRUST_ACCOUNT bit in userAccountControl requires the DS-Install-Replica right that was not given on the Domain object"
1479 .uac
= UF_PARTIAL_SECRETS_ACCOUNT
,
1480 .guid
= GUID_DRS_DS_INSTALL_REPLICA
,
1481 .error_string
= "Adding the UF_PARTIAL_SECRETS_ACCOUNT bit in userAccountControl requires the DS-Install-Replica right that was not given on the Domain object"
1484 .uac
= UF_INTERDOMAIN_TRUST_ACCOUNT
,
1485 .oid
= DSDB_CONTROL_PERMIT_INTERDOMAIN_TRUST_UAC_OID
,
1486 .error_string
= "Updating the UF_INTERDOMAIN_TRUST_ACCOUNT bit in userAccountControl is not permitted over LDAP. This bit is restricted to the LSA CreateTrustedDomain interface",
1487 .delete_is_privileged
= true
1490 .uac
= UF_TRUSTED_FOR_DELEGATION
,
1491 .privilege
= SEC_PRIV_ENABLE_DELEGATION
,
1492 .delete_is_privileged
= true,
1493 .error_string
= "Updating the UF_TRUSTED_FOR_DELEGATION bit in userAccountControl is not permitted without the SeEnableDelegationPrivilege"
1496 .uac
= UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION
,
1497 .privilege
= SEC_PRIV_ENABLE_DELEGATION
,
1498 .delete_is_privileged
= true,
1499 .error_string
= "Updating the UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION bit in userAccountControl is not permitted without the SeEnableDelegationPrivilege"
1504 if (dsdb_module_am_system(ac
->module
)) {
1508 for (i
= 0; i
< ARRAY_SIZE(map
); i
++) {
1509 if (user_account_control
& map
[i
].uac
) {
1510 need_acl_check
= true;
1514 if (need_acl_check
== false) {
1518 user_token
= acl_user_token(ac
->module
);
1519 if (user_token
== NULL
) {
1520 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1523 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
,
1526 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
,
1528 if (ret
!= LDB_SUCCESS
) {
1531 if (res
->count
!= 1) {
1532 return ldb_module_operr(ac
->module
);
1535 ret
= dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(ac
->module
),
1536 ac
, res
->msgs
[0], &domain_sd
);
1538 if (ret
!= LDB_SUCCESS
) {
1542 for (i
= 0; i
< ARRAY_SIZE(map
); i
++) {
1543 uint32_t this_uac_new
= user_account_control
& map
[i
].uac
;
1544 uint32_t this_uac_old
= user_account_control_old
& map
[i
].uac
;
1545 if (this_uac_new
!= this_uac_old
) {
1546 if (this_uac_old
!= 0) {
1547 if (map
[i
].delete_is_privileged
== false) {
1552 struct ldb_control
*control
= ldb_request_get_control(ac
->req
, map
[i
].oid
);
1553 if (control
== NULL
) {
1554 ret
= LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1556 } else if (map
[i
].privilege
!= SEC_PRIV_INVALID
) {
1557 bool have_priv
= security_token_has_privilege(user_token
,
1559 if (have_priv
== false) {
1560 ret
= LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1563 ret
= acl_check_extended_right(ac
, domain_sd
,
1566 SEC_ADS_CONTROL_ACCESS
,
1569 if (ret
!= LDB_SUCCESS
) {
1574 if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
1575 switch (ac
->req
->operation
) {
1577 ldb_asprintf_errstring(ldb_module_get_ctx(ac
->module
),
1578 "Failed to add %s: %s",
1579 ldb_dn_get_linearized(ac
->msg
->dn
),
1580 map
[i
].error_string
);
1583 ldb_asprintf_errstring(ldb_module_get_ctx(ac
->module
),
1584 "Failed to modify %s: %s",
1585 ldb_dn_get_linearized(ac
->msg
->dn
),
1586 map
[i
].error_string
);
1589 return ldb_module_operr(ac
->module
);
1592 dsdb_acl_debug(domain_sd
, acl_user_token(ac
->module
),
1602 * This function is called on LDB modify operations. It performs some additions/
1603 * replaces on the current LDB message when "userAccountControl" changes.
1605 static int samldb_user_account_control_change(struct samldb_ctx
*ac
)
1607 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1608 uint32_t user_account_control
, old_user_account_control
, account_type
;
1609 struct ldb_message_element
*el
;
1610 struct ldb_message
*tmp_msg
;
1611 struct dom_sid
*sid
;
1613 struct ldb_result
*res
;
1614 const char * const attrs
[] = { "userAccountControl", "objectClass", "objectSid", NULL
};
1616 bool is_computer
= false;
1618 el
= dsdb_get_single_valued_attr(ac
->msg
, "userAccountControl",
1619 ac
->req
->operation
);
1621 /* we are not affected */
1625 /* Create a temporary message for fetching the "userAccountControl" */
1626 tmp_msg
= ldb_msg_new(ac
->msg
);
1627 if (tmp_msg
== NULL
) {
1628 return ldb_module_oom(ac
->module
);
1630 ret
= ldb_msg_add(tmp_msg
, el
, 0);
1631 if (ret
!= LDB_SUCCESS
) {
1634 user_account_control
= ldb_msg_find_attr_as_uint(tmp_msg
,
1635 "userAccountControl",
1637 talloc_free(tmp_msg
);
1639 /* Temporary duplicate accounts aren't allowed */
1640 if ((user_account_control
& UF_TEMP_DUPLICATE_ACCOUNT
) != 0) {
1641 return LDB_ERR_OTHER
;
1644 /* Fetch the old "userAccountControl" and "objectClass" */
1645 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, attrs
,
1646 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1647 if (ret
!= LDB_SUCCESS
) {
1650 old_user_account_control
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "userAccountControl", 0);
1651 if (old_user_account_control
== 0) {
1652 return ldb_operr(ldb
);
1654 el
= ldb_msg_find_element(res
->msgs
[0], "objectClass");
1656 return ldb_operr(ldb
);
1659 /* When we do not have objectclass "computer" we cannot switch to a (read-only) DC */
1660 for (i
= 0; i
< el
->num_values
; i
++) {
1661 if (ldb_attr_cmp((char *)el
->values
[i
].data
, "computer") == 0) {
1667 (user_account_control
& (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
))) {
1668 ldb_set_errstring(ldb
,
1669 "samldb: Requested account type does need objectclass 'computer'!");
1670 return LDB_ERR_UNWILLING_TO_PERFORM
;
1674 * The functions "ds_uf2atype" and "ds_uf2prim_group_rid" are used as
1675 * detectors for account type changes.
1676 * So if the account type does change then we need to adjust the
1677 * "sAMAccountType", the "isCriticalSystemObject" and the
1678 * "primaryGroupID" attribute.
1680 if ((ds_uf2atype(user_account_control
)
1681 == ds_uf2atype(old_user_account_control
)) &&
1682 (ds_uf2prim_group_rid(user_account_control
)
1683 == ds_uf2prim_group_rid(old_user_account_control
))) {
1687 account_type
= ds_uf2atype(user_account_control
);
1688 if (account_type
== 0) {
1689 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
1690 return LDB_ERR_UNWILLING_TO_PERFORM
;
1692 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
, "sAMAccountType",
1694 if (ret
!= LDB_SUCCESS
) {
1697 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
1698 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1700 /* "isCriticalSystemObject" might be set/changed */
1701 if (user_account_control
1702 & (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
)) {
1703 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
1705 if (ret
!= LDB_SUCCESS
) {
1708 el
= ldb_msg_find_element(ac
->msg
,
1709 "isCriticalSystemObject");
1710 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1711 } else if (user_account_control
& UF_WORKSTATION_TRUST_ACCOUNT
) {
1712 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
1714 if (ret
!= LDB_SUCCESS
) {
1717 el
= ldb_msg_find_element(ac
->msg
,
1718 "isCriticalSystemObject");
1719 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1722 if (!ldb_msg_find_element(ac
->msg
, "primaryGroupID")) {
1723 uint32_t rid
= ds_uf2prim_group_rid(user_account_control
);
1725 /* Older AD deployments don't know about the RODC group */
1726 if (rid
== DOMAIN_RID_READONLY_DCS
) {
1727 ret
= samldb_prim_group_tester(ac
, rid
);
1728 if (ret
!= LDB_SUCCESS
) {
1733 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1734 "primaryGroupID", rid
);
1735 if (ret
!= LDB_SUCCESS
) {
1738 el
= ldb_msg_find_element(ac
->msg
,
1740 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1743 sid
= samdb_result_dom_sid(res
, res
->msgs
[0], "objectSid");
1745 return ldb_module_operr(ac
->module
);
1748 ret
= samldb_check_user_account_control_acl(ac
, sid
, user_account_control
,
1749 old_user_account_control
);
1750 if (ret
!= LDB_SUCCESS
) {
1757 static int samldb_group_type_change(struct samldb_ctx
*ac
)
1759 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1760 uint32_t group_type
, old_group_type
, account_type
;
1761 struct ldb_message_element
*el
;
1762 struct ldb_message
*tmp_msg
;
1764 struct ldb_result
*res
;
1765 const char * const attrs
[] = { "groupType", NULL
};
1767 el
= dsdb_get_single_valued_attr(ac
->msg
, "groupType",
1768 ac
->req
->operation
);
1770 /* we are not affected */
1774 /* Create a temporary message for fetching the "groupType" */
1775 tmp_msg
= ldb_msg_new(ac
->msg
);
1776 if (tmp_msg
== NULL
) {
1777 return ldb_module_oom(ac
->module
);
1779 ret
= ldb_msg_add(tmp_msg
, el
, 0);
1780 if (ret
!= LDB_SUCCESS
) {
1783 group_type
= ldb_msg_find_attr_as_uint(tmp_msg
, "groupType", 0);
1784 talloc_free(tmp_msg
);
1786 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, attrs
,
1787 DSDB_FLAG_NEXT_MODULE
|
1788 DSDB_SEARCH_SHOW_DELETED
, ac
->req
);
1789 if (ret
!= LDB_SUCCESS
) {
1792 old_group_type
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "groupType", 0);
1793 if (old_group_type
== 0) {
1794 return ldb_operr(ldb
);
1797 /* Group type switching isn't so easy as it seems: We can only
1798 * change in this directions: global <-> universal <-> local
1799 * On each step also the group type itself
1800 * (security/distribution) is variable. */
1802 if (ldb_request_get_control(ac
->req
, LDB_CONTROL_PROVISION_OID
) == NULL
) {
1803 switch (group_type
) {
1804 case GTYPE_SECURITY_GLOBAL_GROUP
:
1805 case GTYPE_DISTRIBUTION_GLOBAL_GROUP
:
1806 /* change to "universal" allowed */
1807 if ((old_group_type
== GTYPE_SECURITY_DOMAIN_LOCAL_GROUP
) ||
1808 (old_group_type
== GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP
)) {
1809 ldb_set_errstring(ldb
,
1810 "samldb: Change from security/distribution local group forbidden!");
1811 return LDB_ERR_UNWILLING_TO_PERFORM
;
1815 case GTYPE_SECURITY_UNIVERSAL_GROUP
:
1816 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP
:
1817 /* each change allowed */
1819 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP
:
1820 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP
:
1821 /* change to "universal" allowed */
1822 if ((old_group_type
== GTYPE_SECURITY_GLOBAL_GROUP
) ||
1823 (old_group_type
== GTYPE_DISTRIBUTION_GLOBAL_GROUP
)) {
1824 ldb_set_errstring(ldb
,
1825 "samldb: Change from security/distribution global group forbidden!");
1826 return LDB_ERR_UNWILLING_TO_PERFORM
;
1830 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP
:
1832 /* we don't allow this "groupType" values */
1833 return LDB_ERR_UNWILLING_TO_PERFORM
;
1838 account_type
= ds_gtype2atype(group_type
);
1839 if (account_type
== 0) {
1840 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
1841 return LDB_ERR_UNWILLING_TO_PERFORM
;
1843 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
, "sAMAccountType",
1845 if (ret
!= LDB_SUCCESS
) {
1848 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
1849 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1854 static int samldb_sam_accountname_check(struct samldb_ctx
*ac
)
1856 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1857 const char * const no_attrs
[] = { NULL
};
1858 struct ldb_result
*res
;
1859 const char *sam_accountname
, *enc_str
;
1860 struct ldb_message_element
*el
;
1861 struct ldb_message
*tmp_msg
;
1864 el
= dsdb_get_single_valued_attr(ac
->msg
, "sAMAccountName",
1865 ac
->req
->operation
);
1867 /* we are not affected */
1871 /* Create a temporary message for fetching the "sAMAccountName" */
1872 tmp_msg
= ldb_msg_new(ac
->msg
);
1873 if (tmp_msg
== NULL
) {
1874 return ldb_module_oom(ac
->module
);
1876 ret
= ldb_msg_add(tmp_msg
, el
, 0);
1877 if (ret
!= LDB_SUCCESS
) {
1881 /* We must not steal the original string, it belongs to the caller! */
1882 sam_accountname
= talloc_strdup(ac
,
1883 ldb_msg_find_attr_as_string(tmp_msg
, "sAMAccountName", NULL
));
1884 talloc_free(tmp_msg
);
1886 if (sam_accountname
== NULL
) {
1887 /* The "sAMAccountName" cannot be nothing */
1888 ldb_set_errstring(ldb
,
1889 "samldb: Empty account names aren't allowed!");
1890 return LDB_ERR_UNWILLING_TO_PERFORM
;
1893 enc_str
= ldb_binary_encode_string(ac
, sam_accountname
);
1894 if (enc_str
== NULL
) {
1895 return ldb_module_oom(ac
->module
);
1898 /* Make sure that a "sAMAccountName" is only used once */
1900 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
1901 ldb_get_default_basedn(ldb
),
1902 LDB_SCOPE_SUBTREE
, no_attrs
,
1903 DSDB_FLAG_NEXT_MODULE
, ac
->req
,
1904 "(sAMAccountName=%s)", enc_str
);
1905 if (ret
!= LDB_SUCCESS
) {
1908 if (res
->count
> 1) {
1909 return ldb_operr(ldb
);
1910 } else if (res
->count
== 1) {
1911 if (ldb_dn_compare(res
->msgs
[0]->dn
, ac
->msg
->dn
) != 0) {
1912 ldb_asprintf_errstring(ldb
,
1913 "samldb: Account name (sAMAccountName) '%s' already in use!",
1915 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
1923 static int samldb_member_check(struct samldb_ctx
*ac
)
1925 const char * const attrs
[] = { "objectSid", NULL
};
1926 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1927 struct ldb_message_element
*el
;
1928 struct ldb_dn
*member_dn
;
1929 struct dom_sid
*sid
;
1930 struct ldb_result
*res
;
1931 struct dom_sid
*group_sid
;
1935 /* Fetch information from the existing object */
1937 ret
= dsdb_module_search(ac
->module
, ac
, &res
, ac
->msg
->dn
, LDB_SCOPE_BASE
, attrs
,
1938 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
, ac
->req
, NULL
);
1939 if (ret
!= LDB_SUCCESS
) {
1942 if (res
->count
!= 1) {
1943 return ldb_operr(ldb
);
1946 group_sid
= samdb_result_dom_sid(res
, res
->msgs
[0], "objectSid");
1947 if (group_sid
== NULL
) {
1948 return ldb_operr(ldb
);
1951 /* We've to walk over all modification entries and consider the "member"
1953 for (i
= 0; i
< ac
->msg
->num_elements
; i
++) {
1954 if (ldb_attr_cmp(ac
->msg
->elements
[i
].name
, "member") != 0) {
1958 el
= &ac
->msg
->elements
[i
];
1959 for (j
= 0; j
< el
->num_values
; j
++) {
1960 struct ldb_result
*group_res
;
1961 const char *group_attrs
[] = { "primaryGroupID" , NULL
};
1962 uint32_t prim_group_rid
;
1964 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_DELETE
) {
1965 /* Deletes will be handled in
1966 * repl_meta_data, and deletes not
1967 * matching a member will return
1968 * LDB_ERR_UNWILLING_TO_PERFORM
1973 member_dn
= ldb_dn_from_ldb_val(ac
, ldb
,
1975 if (!ldb_dn_validate(member_dn
)) {
1976 return ldb_operr(ldb
);
1979 /* Denies to add "member"s to groups which are primary
1980 * ones for them - in this case return
1981 * ERR_ENTRY_ALREADY_EXISTS. */
1983 ret
= dsdb_module_search_dn(ac
->module
, ac
, &group_res
,
1984 member_dn
, group_attrs
,
1985 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1986 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
1987 /* member DN doesn't exist yet */
1990 if (ret
!= LDB_SUCCESS
) {
1993 prim_group_rid
= ldb_msg_find_attr_as_uint(group_res
->msgs
[0], "primaryGroupID", (uint32_t)-1);
1994 if (prim_group_rid
== (uint32_t) -1) {
1995 /* the member hasn't to be a user account ->
1996 * therefore no check needed in this case. */
2000 sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
),
2003 return ldb_operr(ldb
);
2006 if (dom_sid_equal(group_sid
, sid
)) {
2007 ldb_asprintf_errstring(ldb
,
2008 "samldb: member %s already set via primaryGroupID %u",
2009 ldb_dn_get_linearized(member_dn
), prim_group_rid
);
2010 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
2020 /* SAM objects have special rules regarding the "description" attribute on
2021 * modify operations. */
2022 static int samldb_description_check(struct samldb_ctx
*ac
, bool *modified
)
2024 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
2025 const char * const attrs
[] = { "objectClass", "description", NULL
};
2026 struct ldb_result
*res
;
2030 /* Fetch information from the existing object */
2031 ret
= dsdb_module_search(ac
->module
, ac
, &res
, ac
->msg
->dn
, LDB_SCOPE_BASE
, attrs
,
2032 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
, ac
->req
,
2033 "(|(objectclass=user)(objectclass=group)(objectclass=samDomain)(objectclass=samServer))");
2034 if (ret
!= LDB_SUCCESS
) {
2035 /* don't treat it specially ... let normal error codes
2036 happen from other places */
2037 ldb_reset_err_string(ldb
);
2040 if (res
->count
== 0) {
2041 /* we didn't match the filter */
2046 /* We've to walk over all modification entries and consider the
2047 * "description" ones. */
2048 for (i
= 0; i
< ac
->msg
->num_elements
; i
++) {
2049 if (ldb_attr_cmp(ac
->msg
->elements
[i
].name
, "description") == 0) {
2050 ac
->msg
->elements
[i
].flags
|= LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK
;
2060 /* This trigger adapts the "servicePrincipalName" attributes if the
2061 * "dNSHostName" and/or "sAMAccountName" attribute change(s) */
2062 static int samldb_service_principal_names_change(struct samldb_ctx
*ac
)
2064 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
2065 struct ldb_message_element
*el
= NULL
, *el2
= NULL
;
2066 struct ldb_message
*msg
;
2067 const char * const attrs
[] = { "servicePrincipalName", NULL
};
2068 struct ldb_result
*res
;
2069 const char *dns_hostname
= NULL
, *old_dns_hostname
= NULL
,
2070 *sam_accountname
= NULL
, *old_sam_accountname
= NULL
;
2074 el
= dsdb_get_single_valued_attr(ac
->msg
, "dNSHostName",
2075 ac
->req
->operation
);
2076 el2
= dsdb_get_single_valued_attr(ac
->msg
, "sAMAccountName",
2077 ac
->req
->operation
);
2078 if ((el
== NULL
) && (el2
== NULL
)) {
2079 /* we are not affected */
2083 /* Create a temporary message for fetching the "dNSHostName" */
2085 const char *dns_attrs
[] = { "dNSHostName", NULL
};
2086 msg
= ldb_msg_new(ac
->msg
);
2088 return ldb_module_oom(ac
->module
);
2090 ret
= ldb_msg_add(msg
, el
, 0);
2091 if (ret
!= LDB_SUCCESS
) {
2094 dns_hostname
= talloc_strdup(ac
,
2095 ldb_msg_find_attr_as_string(msg
, "dNSHostName", NULL
));
2096 if (dns_hostname
== NULL
) {
2097 return ldb_module_oom(ac
->module
);
2102 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
,
2103 dns_attrs
, DSDB_FLAG_NEXT_MODULE
, ac
->req
);
2104 if (ret
== LDB_SUCCESS
) {
2105 old_dns_hostname
= ldb_msg_find_attr_as_string(res
->msgs
[0], "dNSHostName", NULL
);
2109 /* Create a temporary message for fetching the "sAMAccountName" */
2111 char *tempstr
, *tempstr2
;
2112 const char *acct_attrs
[] = { "sAMAccountName", NULL
};
2114 msg
= ldb_msg_new(ac
->msg
);
2116 return ldb_module_oom(ac
->module
);
2118 ret
= ldb_msg_add(msg
, el2
, 0);
2119 if (ret
!= LDB_SUCCESS
) {
2122 tempstr
= talloc_strdup(ac
,
2123 ldb_msg_find_attr_as_string(msg
, "sAMAccountName", NULL
));
2126 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, acct_attrs
,
2127 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
2128 if (ret
== LDB_SUCCESS
) {
2129 tempstr2
= talloc_strdup(ac
,
2130 ldb_msg_find_attr_as_string(res
->msgs
[0],
2131 "sAMAccountName", NULL
));
2135 /* The "sAMAccountName" needs some additional trimming: we need
2136 * to remove the trailing "$"s if they exist. */
2137 if ((tempstr
!= NULL
) && (tempstr
[0] != '\0') &&
2138 (tempstr
[strlen(tempstr
) - 1] == '$')) {
2139 tempstr
[strlen(tempstr
) - 1] = '\0';
2141 if ((tempstr2
!= NULL
) && (tempstr2
[0] != '\0') &&
2142 (tempstr2
[strlen(tempstr2
) - 1] == '$')) {
2143 tempstr2
[strlen(tempstr2
) - 1] = '\0';
2145 sam_accountname
= tempstr
;
2146 old_sam_accountname
= tempstr2
;
2149 if (old_dns_hostname
== NULL
) {
2150 /* we cannot change when the old name is unknown */
2151 dns_hostname
= NULL
;
2153 if ((old_dns_hostname
!= NULL
) && (dns_hostname
!= NULL
) &&
2154 (strcasecmp_m(old_dns_hostname
, dns_hostname
) == 0)) {
2155 /* The "dNSHostName" didn't change */
2156 dns_hostname
= NULL
;
2159 if (old_sam_accountname
== NULL
) {
2160 /* we cannot change when the old name is unknown */
2161 sam_accountname
= NULL
;
2163 if ((old_sam_accountname
!= NULL
) && (sam_accountname
!= NULL
) &&
2164 (strcasecmp_m(old_sam_accountname
, sam_accountname
) == 0)) {
2165 /* The "sAMAccountName" didn't change */
2166 sam_accountname
= NULL
;
2169 if ((dns_hostname
== NULL
) && (sam_accountname
== NULL
)) {
2170 /* Well, there are information missing (old name(s)) or the
2171 * names didn't change. We've nothing to do and can exit here */
2175 /* Potential "servicePrincipalName" changes in the same request have to
2176 * be handled before the update (Windows behaviour). */
2177 el
= ldb_msg_find_element(ac
->msg
, "servicePrincipalName");
2179 msg
= ldb_msg_new(ac
->msg
);
2181 return ldb_module_oom(ac
->module
);
2183 msg
->dn
= ac
->msg
->dn
;
2186 ret
= ldb_msg_add(msg
, el
, el
->flags
);
2187 if (ret
!= LDB_SUCCESS
) {
2191 ldb_msg_remove_element(ac
->msg
, el
);
2193 el
= ldb_msg_find_element(ac
->msg
,
2194 "servicePrincipalName");
2195 } while (el
!= NULL
);
2197 ret
= dsdb_module_modify(ac
->module
, msg
,
2198 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
2199 if (ret
!= LDB_SUCCESS
) {
2205 /* Fetch the "servicePrincipalName"s if any */
2206 ret
= dsdb_module_search(ac
->module
, ac
, &res
, ac
->msg
->dn
, LDB_SCOPE_BASE
, attrs
,
2207 DSDB_FLAG_NEXT_MODULE
, ac
->req
, NULL
);
2208 if (ret
!= LDB_SUCCESS
) {
2211 if ((res
->count
!= 1) || (res
->msgs
[0]->num_elements
> 1)) {
2212 return ldb_operr(ldb
);
2215 if (res
->msgs
[0]->num_elements
== 1) {
2217 * Yes, we do have "servicePrincipalName"s. First we update them
2218 * locally, that means we do always substitute the current
2219 * "dNSHostName" with the new one and/or "sAMAccountName"
2220 * without "$" with the new one and then we append the
2221 * modified "servicePrincipalName"s as a message element
2222 * replace to the modification request (Windows behaviour). We
2223 * need also to make sure that the values remain case-
2224 * insensitively unique.
2227 ret
= ldb_msg_add_empty(ac
->msg
, "servicePrincipalName",
2228 LDB_FLAG_MOD_REPLACE
, &el
);
2229 if (ret
!= LDB_SUCCESS
) {
2233 for (i
= 0; i
< res
->msgs
[0]->elements
[0].num_values
; i
++) {
2234 char *old_str
, *new_str
, *pos
;
2236 struct ldb_val
*vals
;
2240 res
->msgs
[0]->elements
[0].values
[i
].data
;
2242 new_str
= talloc_strdup(ac
->msg
,
2243 strtok_r(old_str
, "/", &pos
));
2244 if (new_str
== NULL
) {
2245 return ldb_module_oom(ac
->module
);
2248 while ((tok
= strtok_r(NULL
, "/", &pos
)) != NULL
) {
2249 if ((dns_hostname
!= NULL
) &&
2250 (strcasecmp_m(tok
, old_dns_hostname
) == 0)) {
2253 if ((sam_accountname
!= NULL
) &&
2254 (strcasecmp_m(tok
, old_sam_accountname
) == 0)) {
2255 tok
= sam_accountname
;
2258 new_str
= talloc_asprintf(ac
->msg
, "%s/%s",
2260 if (new_str
== NULL
) {
2261 return ldb_module_oom(ac
->module
);
2265 /* Uniqueness check */
2266 for (j
= 0; (!found
) && (j
< el
->num_values
); j
++) {
2267 if (strcasecmp_m((char *)el
->values
[j
].data
,
2277 * append the new "servicePrincipalName" -
2278 * code derived from ldb_msg_add_value().
2280 * Open coded to make it clear that we must
2281 * append to the MOD_REPLACE el created above.
2283 vals
= talloc_realloc(ac
->msg
, el
->values
,
2285 el
->num_values
+ 1);
2287 return ldb_module_oom(ac
->module
);
2290 el
->values
[el
->num_values
] = data_blob_string_const(new_str
);
2300 /* This checks the "fSMORoleOwner" attributes */
2301 static int samldb_fsmo_role_owner_check(struct samldb_ctx
*ac
)
2303 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
2304 const char * const no_attrs
[] = { NULL
};
2305 struct ldb_message_element
*el
;
2306 struct ldb_message
*tmp_msg
;
2307 struct ldb_dn
*res_dn
;
2308 struct ldb_result
*res
;
2311 el
= dsdb_get_single_valued_attr(ac
->msg
, "fSMORoleOwner",
2312 ac
->req
->operation
);
2314 /* we are not affected */
2318 /* Create a temporary message for fetching the "fSMORoleOwner" */
2319 tmp_msg
= ldb_msg_new(ac
->msg
);
2320 if (tmp_msg
== NULL
) {
2321 return ldb_module_oom(ac
->module
);
2323 ret
= ldb_msg_add(tmp_msg
, el
, 0);
2324 if (ret
!= LDB_SUCCESS
) {
2327 res_dn
= ldb_msg_find_attr_as_dn(ldb
, ac
, tmp_msg
, "fSMORoleOwner");
2328 talloc_free(tmp_msg
);
2330 if (res_dn
== NULL
) {
2331 ldb_set_errstring(ldb
,
2332 "samldb: 'fSMORoleOwner' attributes have to reference 'nTDSDSA' entries!");
2333 if (ac
->req
->operation
== LDB_ADD
) {
2334 return LDB_ERR_CONSTRAINT_VIOLATION
;
2336 return LDB_ERR_UNWILLING_TO_PERFORM
;
2340 /* Fetched DN has to reference a "nTDSDSA" entry */
2341 ret
= dsdb_module_search(ac
->module
, ac
, &res
, res_dn
, LDB_SCOPE_BASE
,
2343 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
,
2344 ac
->req
, "(objectClass=nTDSDSA)");
2345 if (ret
!= LDB_SUCCESS
) {
2348 if (res
->count
!= 1) {
2349 ldb_set_errstring(ldb
,
2350 "samldb: 'fSMORoleOwner' attributes have to reference 'nTDSDSA' entries!");
2351 return LDB_ERR_UNWILLING_TO_PERFORM
;
2361 static int samldb_add(struct ldb_module
*module
, struct ldb_request
*req
)
2363 struct ldb_context
*ldb
;
2364 struct samldb_ctx
*ac
;
2365 struct ldb_message_element
*el
;
2368 ldb
= ldb_module_get_ctx(module
);
2369 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "samldb_add\n");
2371 /* do not manipulate our control entries */
2372 if (ldb_dn_is_special(req
->op
.add
.message
->dn
)) {
2373 return ldb_next_request(module
, req
);
2376 ac
= samldb_ctx_init(module
, req
);
2378 return ldb_operr(ldb
);
2381 /* build the new msg */
2382 ac
->msg
= ldb_msg_copy_shallow(ac
, req
->op
.add
.message
);
2383 if (ac
->msg
== NULL
) {
2385 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
2386 "samldb_add: ldb_msg_copy_shallow failed!\n");
2387 return ldb_operr(ldb
);
2390 el
= ldb_msg_find_element(ac
->msg
, "fSMORoleOwner");
2392 ret
= samldb_fsmo_role_owner_check(ac
);
2393 if (ret
!= LDB_SUCCESS
) {
2398 if (samdb_find_attribute(ldb
, ac
->msg
,
2399 "objectclass", "user") != NULL
) {
2400 ac
->type
= SAMLDB_TYPE_USER
;
2402 ret
= samldb_prim_group_trigger(ac
);
2403 if (ret
!= LDB_SUCCESS
) {
2407 ret
= samldb_objectclass_trigger(ac
);
2408 if (ret
!= LDB_SUCCESS
) {
2412 return samldb_fill_object(ac
);
2415 if (samdb_find_attribute(ldb
, ac
->msg
,
2416 "objectclass", "group") != NULL
) {
2417 ac
->type
= SAMLDB_TYPE_GROUP
;
2419 ret
= samldb_objectclass_trigger(ac
);
2420 if (ret
!= LDB_SUCCESS
) {
2424 return samldb_fill_object(ac
);
2427 /* perhaps a foreignSecurityPrincipal? */
2428 if (samdb_find_attribute(ldb
, ac
->msg
,
2430 "foreignSecurityPrincipal") != NULL
) {
2431 return samldb_fill_foreignSecurityPrincipal_object(ac
);
2434 if (samdb_find_attribute(ldb
, ac
->msg
,
2435 "objectclass", "classSchema") != NULL
) {
2436 ret
= samldb_schema_info_update(ac
);
2437 if (ret
!= LDB_SUCCESS
) {
2442 ac
->type
= SAMLDB_TYPE_CLASS
;
2443 return samldb_fill_object(ac
);
2446 if (samdb_find_attribute(ldb
, ac
->msg
,
2447 "objectclass", "attributeSchema") != NULL
) {
2448 ret
= samldb_schema_info_update(ac
);
2449 if (ret
!= LDB_SUCCESS
) {
2454 ac
->type
= SAMLDB_TYPE_ATTRIBUTE
;
2455 return samldb_fill_object(ac
);
2460 /* nothing matched, go on */
2461 return ldb_next_request(module
, req
);
2465 static int samldb_modify(struct ldb_module
*module
, struct ldb_request
*req
)
2467 struct ldb_context
*ldb
;
2468 struct samldb_ctx
*ac
;
2469 struct ldb_message_element
*el
, *el2
;
2470 bool modified
= false;
2473 if (ldb_dn_is_special(req
->op
.mod
.message
->dn
)) {
2474 /* do not manipulate our control entries */
2475 return ldb_next_request(module
, req
);
2478 ldb
= ldb_module_get_ctx(module
);
2480 /* make sure that "objectSid" is not specified */
2481 el
= ldb_msg_find_element(req
->op
.mod
.message
, "objectSid");
2483 if (ldb_request_get_control(req
, LDB_CONTROL_PROVISION_OID
) == NULL
) {
2484 ldb_set_errstring(ldb
,
2485 "samldb: objectSid must not be specified!");
2486 return LDB_ERR_UNWILLING_TO_PERFORM
;
2489 /* make sure that "sAMAccountType" is not specified */
2490 el
= ldb_msg_find_element(req
->op
.mod
.message
, "sAMAccountType");
2492 ldb_set_errstring(ldb
,
2493 "samldb: sAMAccountType must not be specified!");
2494 return LDB_ERR_UNWILLING_TO_PERFORM
;
2496 /* make sure that "isCriticalSystemObject" is not specified */
2497 el
= ldb_msg_find_element(req
->op
.mod
.message
, "isCriticalSystemObject");
2499 if (ldb_request_get_control(req
, LDB_CONTROL_RELAX_OID
) == NULL
) {
2500 ldb_set_errstring(ldb
,
2501 "samldb: isCriticalSystemObject must not be specified!");
2502 return LDB_ERR_UNWILLING_TO_PERFORM
;
2506 /* msDS-IntId is not allowed to be modified
2507 * except when modification comes from replication */
2508 if (ldb_msg_find_element(req
->op
.mod
.message
, "msDS-IntId")) {
2509 if (!ldb_request_get_control(req
,
2510 DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
2511 return LDB_ERR_CONSTRAINT_VIOLATION
;
2515 ac
= samldb_ctx_init(module
, req
);
2517 return ldb_operr(ldb
);
2520 /* build the new msg */
2521 ac
->msg
= ldb_msg_copy_shallow(ac
, req
->op
.mod
.message
);
2522 if (ac
->msg
== NULL
) {
2524 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
2525 "samldb_modify: ldb_msg_copy_shallow failed!\n");
2526 return ldb_operr(ldb
);
2529 el
= ldb_msg_find_element(ac
->msg
, "primaryGroupID");
2531 ret
= samldb_prim_group_trigger(ac
);
2532 if (ret
!= LDB_SUCCESS
) {
2537 el
= ldb_msg_find_element(ac
->msg
, "userAccountControl");
2540 ret
= samldb_user_account_control_change(ac
);
2541 if (ret
!= LDB_SUCCESS
) {
2546 el
= ldb_msg_find_element(ac
->msg
, "groupType");
2549 ret
= samldb_group_type_change(ac
);
2550 if (ret
!= LDB_SUCCESS
) {
2555 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountName");
2557 ret
= samldb_sam_accountname_check(ac
);
2558 if (ret
!= LDB_SUCCESS
) {
2563 el
= ldb_msg_find_element(ac
->msg
, "member");
2565 ret
= samldb_member_check(ac
);
2566 if (ret
!= LDB_SUCCESS
) {
2571 el
= ldb_msg_find_element(ac
->msg
, "description");
2573 ret
= samldb_description_check(ac
, &modified
);
2574 if (ret
!= LDB_SUCCESS
) {
2579 el
= ldb_msg_find_element(ac
->msg
, "dNSHostName");
2580 el2
= ldb_msg_find_element(ac
->msg
, "sAMAccountName");
2581 if ((el
!= NULL
) || (el2
!= NULL
)) {
2583 ret
= samldb_service_principal_names_change(ac
);
2584 if (ret
!= LDB_SUCCESS
) {
2589 el
= ldb_msg_find_element(ac
->msg
, "fSMORoleOwner");
2591 ret
= samldb_fsmo_role_owner_check(ac
);
2592 if (ret
!= LDB_SUCCESS
) {
2598 struct ldb_request
*child_req
;
2600 /* Now perform the real modifications as a child request */
2601 ret
= ldb_build_mod_req(&child_req
, ldb
, ac
,
2604 req
, dsdb_next_callback
,
2606 LDB_REQ_SET_LOCATION(child_req
);
2607 if (ret
!= LDB_SUCCESS
) {
2611 return ldb_next_request(module
, child_req
);
2616 /* no change which interests us, go on */
2617 return ldb_next_request(module
, req
);
2622 static int samldb_prim_group_users_check(struct samldb_ctx
*ac
)
2624 struct ldb_context
*ldb
;
2625 struct dom_sid
*sid
;
2629 struct ldb_result
*res
;
2630 const char * const attrs
[] = { "objectSid", "isDeleted", NULL
};
2631 const char * const noattrs
[] = { NULL
};
2633 ldb
= ldb_module_get_ctx(ac
->module
);
2635 /* Finds out the SID/RID of the SAM object */
2636 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->req
->op
.del
.dn
,
2638 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
,
2640 if (ret
!= LDB_SUCCESS
) {
2644 if (ldb_msg_check_string_attribute(res
->msgs
[0], "isDeleted", "TRUE")) {
2648 sid
= samdb_result_dom_sid(ac
, res
->msgs
[0], "objectSid");
2650 /* No SID - it might not be a SAM object - therefore ok */
2653 status
= dom_sid_split_rid(ac
, sid
, NULL
, &rid
);
2654 if (!NT_STATUS_IS_OK(status
)) {
2655 return ldb_operr(ldb
);
2658 /* Special object (security principal?) */
2662 /* Deny delete requests from groups which are primary ones */
2663 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
2664 ldb_get_default_basedn(ldb
),
2665 LDB_SCOPE_SUBTREE
, noattrs
,
2666 DSDB_FLAG_NEXT_MODULE
,
2668 "(&(primaryGroupID=%u)(objectClass=user))", rid
);
2669 if (ret
!= LDB_SUCCESS
) {
2672 if (res
->count
> 0) {
2673 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
2679 static int samldb_delete(struct ldb_module
*module
, struct ldb_request
*req
)
2681 struct samldb_ctx
*ac
;
2684 if (ldb_dn_is_special(req
->op
.del
.dn
)) {
2685 /* do not manipulate our control entries */
2686 return ldb_next_request(module
, req
);
2689 ac
= samldb_ctx_init(module
, req
);
2691 return ldb_operr(ldb_module_get_ctx(module
));
2694 ret
= samldb_prim_group_users_check(ac
);
2695 if (ret
!= LDB_SUCCESS
) {
2701 return ldb_next_request(module
, req
);
2706 static int samldb_extended_allocate_rid_pool(struct ldb_module
*module
, struct ldb_request
*req
)
2708 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
2709 struct dsdb_fsmo_extended_op
*exop
;
2712 exop
= talloc_get_type(req
->op
.extended
.data
,
2713 struct dsdb_fsmo_extended_op
);
2715 ldb_set_errstring(ldb
,
2716 "samldb_extended_allocate_rid_pool: invalid extended data");
2717 return LDB_ERR_PROTOCOL_ERROR
;
2720 ret
= ridalloc_allocate_rid_pool_fsmo(module
, exop
, req
);
2721 if (ret
!= LDB_SUCCESS
) {
2725 return ldb_module_done(req
, NULL
, NULL
, LDB_SUCCESS
);
2728 static int samldb_extended(struct ldb_module
*module
, struct ldb_request
*req
)
2730 if (strcmp(req
->op
.extended
.oid
, DSDB_EXTENDED_ALLOCATE_RID_POOL
) == 0) {
2731 return samldb_extended_allocate_rid_pool(module
, req
);
2734 return ldb_next_request(module
, req
);
2738 static const struct ldb_module_ops ldb_samldb_module_ops
= {
2741 .modify
= samldb_modify
,
2742 .del
= samldb_delete
,
2743 .extended
= samldb_extended
2747 int ldb_samldb_module_init(const char *version
)
2749 LDB_MODULE_CHECK_VERSION(version
);
2750 return ldb_register_module(&ldb_samldb_module_ops
);