4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2014
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_dn
,
514 ¤t_usn
, NULL
, NULL
);
515 if (ret
!= LDB_SUCCESS
) {
516 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
517 __location__
": Searching for schema USN failed: %s\n",
519 return ldb_operr(ldb
);
522 /* current_usn can be lesser than msds_intid_struct-> if there is
523 * uncommited changes.
525 if (current_usn
> msds_intid_struct
->usn
) {
526 /* oups something has changed, someone/something
527 * else is modifying or has modified the schema
528 * we'd better check this intid is the database directly
531 DEBUG(2, ("Schema has changed, searching the database for the unicity of %d\n",
534 ret
= dsdb_module_search(ac
->module
, ac
,
536 schema_dn
, LDB_SCOPE_ONELEVEL
, NULL
,
537 DSDB_FLAG_NEXT_MODULE
,
539 "(msDS-IntId=%d)", msds_intid
);
540 if (ret
!= LDB_SUCCESS
) {
541 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
542 __location__
": Searching for msDS-IntId=%d failed - %s\n",
545 return ldb_operr(ldb
);
547 id_exists
= (ldb_res
->count
> 0);
548 talloc_free(ldb_res
);
554 msds_intid_struct
->msds_intid
= msds_intid
;
555 ldb_set_opaque(ldb
, SAMLDB_MSDS_INTID_OPAQUE
, msds_intid_struct
);
557 return samdb_msg_add_int(ldb
, ac
->msg
, ac
->msg
, "msDS-IntId",
563 * samldb_add_entry (async)
566 static int samldb_add_entry_callback(struct ldb_request
*req
,
567 struct ldb_reply
*ares
)
569 struct ldb_context
*ldb
;
570 struct samldb_ctx
*ac
;
573 ac
= talloc_get_type(req
->context
, struct samldb_ctx
);
574 ldb
= ldb_module_get_ctx(ac
->module
);
577 return ldb_module_done(ac
->req
, NULL
, NULL
,
578 LDB_ERR_OPERATIONS_ERROR
);
581 if (ares
->type
== LDB_REPLY_REFERRAL
) {
582 return ldb_module_send_referral(ac
->req
, ares
->referral
);
585 if (ares
->error
!= LDB_SUCCESS
) {
586 return ldb_module_done(ac
->req
, ares
->controls
,
587 ares
->response
, ares
->error
);
589 if (ares
->type
!= LDB_REPLY_DONE
) {
590 ldb_asprintf_errstring(ldb
, "Invalid LDB reply type %d", ares
->type
);
591 return ldb_module_done(ac
->req
, NULL
, NULL
,
592 LDB_ERR_OPERATIONS_ERROR
);
595 /* The caller may wish to get controls back from the add */
596 ac
->ares
= talloc_steal(ac
, ares
);
598 ret
= samldb_next_step(ac
);
599 if (ret
!= LDB_SUCCESS
) {
600 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
605 static int samldb_add_entry(struct samldb_ctx
*ac
)
607 struct ldb_context
*ldb
;
608 struct ldb_request
*req
;
611 ldb
= ldb_module_get_ctx(ac
->module
);
613 ret
= ldb_build_add_req(&req
, ldb
, ac
,
616 ac
, samldb_add_entry_callback
,
618 LDB_REQ_SET_LOCATION(req
);
619 if (ret
!= LDB_SUCCESS
) {
623 return ldb_next_request(ac
->module
, req
);
627 * return true if msg carries an attributeSchema that is intended to be RODC
628 * filtered but is also a system-critical attribute.
630 static bool check_rodc_critical_attribute(struct ldb_message
*msg
)
632 uint32_t schemaFlagsEx
, searchFlags
, rodc_filtered_flags
;
634 schemaFlagsEx
= ldb_msg_find_attr_as_uint(msg
, "schemaFlagsEx", 0);
635 searchFlags
= ldb_msg_find_attr_as_uint(msg
, "searchFlags", 0);
636 rodc_filtered_flags
= (SEARCH_FLAG_RODC_ATTRIBUTE
637 | SEARCH_FLAG_CONFIDENTIAL
);
639 if ((schemaFlagsEx
& SCHEMA_FLAG_ATTR_IS_CRITICAL
) &&
640 ((searchFlags
& rodc_filtered_flags
) == rodc_filtered_flags
)) {
648 static int samldb_fill_object(struct samldb_ctx
*ac
)
650 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
653 /* Add information for the different account types */
655 case SAMLDB_TYPE_USER
: {
656 struct ldb_control
*rodc_control
= ldb_request_get_control(ac
->req
,
657 LDB_CONTROL_RODC_DCPROMO_OID
);
658 if (rodc_control
!= NULL
) {
659 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
660 rodc_control
->critical
= false;
661 ret
= samldb_add_step(ac
, samldb_rodc_add
);
662 if (ret
!= LDB_SUCCESS
) return ret
;
665 /* check if we have a valid sAMAccountName */
666 ret
= samldb_add_step(ac
, samldb_check_sAMAccountName
);
667 if (ret
!= LDB_SUCCESS
) return ret
;
669 ret
= samldb_add_step(ac
, samldb_add_entry
);
670 if (ret
!= LDB_SUCCESS
) return ret
;
674 case SAMLDB_TYPE_GROUP
: {
675 /* check if we have a valid sAMAccountName */
676 ret
= samldb_add_step(ac
, samldb_check_sAMAccountName
);
677 if (ret
!= LDB_SUCCESS
) return ret
;
679 ret
= samldb_add_step(ac
, samldb_add_entry
);
680 if (ret
!= LDB_SUCCESS
) return ret
;
684 case SAMLDB_TYPE_CLASS
: {
685 const struct ldb_val
*rdn_value
, *def_obj_cat_val
;
686 unsigned int v
= ldb_msg_find_attr_as_uint(ac
->msg
, "objectClassCategory", -2);
688 /* As discussed with Microsoft through dochelp in April 2012 this is the behavior of windows*/
689 if (!ldb_msg_find_element(ac
->msg
, "subClassOf")) {
690 ret
= ldb_msg_add_string(ac
->msg
, "subClassOf", "top");
691 if (ret
!= LDB_SUCCESS
) return ret
;
694 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
696 if (ret
!= LDB_SUCCESS
) return ret
;
698 /* do not allow to mark an attributeSchema as RODC filtered if it
699 * is system-critical */
700 if (check_rodc_critical_attribute(ac
->msg
)) {
701 ldb_asprintf_errstring(ldb
, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
702 ldb_dn_get_linearized(ac
->msg
->dn
));
703 return LDB_ERR_UNWILLING_TO_PERFORM
;
706 rdn_value
= ldb_dn_get_rdn_val(ac
->msg
->dn
);
707 if (rdn_value
== NULL
) {
708 return ldb_operr(ldb
);
710 if (!ldb_msg_find_element(ac
->msg
, "lDAPDisplayName")) {
711 /* the RDN has prefix "CN" */
712 ret
= ldb_msg_add_string(ac
->msg
, "lDAPDisplayName",
713 samdb_cn_to_lDAPDisplayName(ac
->msg
,
714 (const char *) rdn_value
->data
));
715 if (ret
!= LDB_SUCCESS
) {
721 if (!ldb_msg_find_element(ac
->msg
, "schemaIDGUID")) {
724 guid
= GUID_random();
725 ret
= dsdb_msg_add_guid(ac
->msg
, &guid
, "schemaIDGUID");
726 if (ret
!= LDB_SUCCESS
) {
732 def_obj_cat_val
= ldb_msg_find_ldb_val(ac
->msg
,
733 "defaultObjectCategory");
734 if (def_obj_cat_val
!= NULL
) {
735 /* "defaultObjectCategory" has been set by the caller.
736 * Do some checks for consistency.
737 * NOTE: The real constraint check (that
738 * 'defaultObjectCategory' is the DN of the new
739 * objectclass or any parent of it) is still incomplete.
740 * For now we say that 'defaultObjectCategory' is valid
741 * if it exists and it is of objectclass "classSchema".
743 ac
->dn
= ldb_dn_from_ldb_val(ac
, ldb
, def_obj_cat_val
);
744 if (ac
->dn
== NULL
) {
745 ldb_set_errstring(ldb
,
746 "Invalid DN for 'defaultObjectCategory'!");
747 return LDB_ERR_CONSTRAINT_VIOLATION
;
750 /* "defaultObjectCategory" has not been set by the
751 * caller. Use the entry DN for it. */
752 ac
->dn
= ac
->msg
->dn
;
754 ret
= ldb_msg_add_string(ac
->msg
, "defaultObjectCategory",
755 ldb_dn_alloc_linearized(ac
->msg
, ac
->dn
));
756 if (ret
!= LDB_SUCCESS
) {
762 ret
= samldb_add_step(ac
, samldb_add_entry
);
763 if (ret
!= LDB_SUCCESS
) return ret
;
765 /* Now perform the checks for the 'defaultObjectCategory'. The
766 * lookup DN was already saved in "ac->dn" */
767 ret
= samldb_add_step(ac
, samldb_find_for_defaultObjectCategory
);
768 if (ret
!= LDB_SUCCESS
) return ret
;
770 /* -2 is not a valid objectClassCategory so it means the attribute wasn't present */
772 /* Windows 2003 does this*/
773 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
, "objectClassCategory", 0);
774 if (ret
!= LDB_SUCCESS
) {
781 case SAMLDB_TYPE_ATTRIBUTE
: {
782 const struct ldb_val
*rdn_value
;
783 struct ldb_message_element
*el
;
784 rdn_value
= ldb_dn_get_rdn_val(ac
->msg
->dn
);
785 if (rdn_value
== NULL
) {
786 return ldb_operr(ldb
);
788 if (!ldb_msg_find_element(ac
->msg
, "lDAPDisplayName")) {
789 /* the RDN has prefix "CN" */
790 ret
= ldb_msg_add_string(ac
->msg
, "lDAPDisplayName",
791 samdb_cn_to_lDAPDisplayName(ac
->msg
,
792 (const char *) rdn_value
->data
));
793 if (ret
!= LDB_SUCCESS
) {
799 /* do not allow to mark an attributeSchema as RODC filtered if it
800 * is system-critical */
801 if (check_rodc_critical_attribute(ac
->msg
)) {
802 ldb_asprintf_errstring(ldb
,
803 "samldb: refusing schema add of %s - cannot combine critical attribute with RODC filtering",
804 ldb_dn_get_linearized(ac
->msg
->dn
));
805 return LDB_ERR_UNWILLING_TO_PERFORM
;
808 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
809 "isSingleValued", "FALSE");
810 if (ret
!= LDB_SUCCESS
) return ret
;
812 if (!ldb_msg_find_element(ac
->msg
, "schemaIDGUID")) {
815 guid
= GUID_random();
816 ret
= dsdb_msg_add_guid(ac
->msg
, &guid
, "schemaIDGUID");
817 if (ret
!= LDB_SUCCESS
) {
823 el
= ldb_msg_find_element(ac
->msg
, "attributeSyntax");
826 * No need to scream if there isn't as we have code later on
827 * that will take care of it.
829 const struct dsdb_syntax
*syntax
= find_syntax_map_by_ad_oid((const char *)el
->values
[0].data
);
831 DEBUG(9, ("Can't find dsdb_syntax object for attributeSyntax %s\n",
832 (const char *)el
->values
[0].data
));
834 unsigned int v
= ldb_msg_find_attr_as_uint(ac
->msg
, "oMSyntax", 0);
835 const struct ldb_val
*val
= ldb_msg_find_ldb_val(ac
->msg
, "oMObjectClass");
838 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
, "oMSyntax", syntax
->oMSyntax
);
839 if (ret
!= LDB_SUCCESS
) {
844 struct ldb_val val2
= ldb_val_dup(ldb
, &syntax
->oMObjectClass
);
845 if (val2
.length
> 0) {
846 ret
= ldb_msg_add_value(ac
->msg
, "oMObjectClass", &val2
, NULL
);
847 if (ret
!= LDB_SUCCESS
) {
855 /* handle msDS-IntID attribute */
856 ret
= samldb_add_handle_msDS_IntId(ac
);
857 if (ret
!= LDB_SUCCESS
) return ret
;
859 ret
= samldb_add_step(ac
, samldb_add_entry
);
860 if (ret
!= LDB_SUCCESS
) return ret
;
865 ldb_asprintf_errstring(ldb
, "Invalid entry type!");
866 return LDB_ERR_OPERATIONS_ERROR
;
870 return samldb_first_step(ac
);
873 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx
*ac
)
875 struct ldb_context
*ldb
;
876 const struct ldb_val
*rdn_value
;
880 ldb
= ldb_module_get_ctx(ac
->module
);
882 sid
= samdb_result_dom_sid(ac
->msg
, ac
->msg
, "objectSid");
884 rdn_value
= ldb_dn_get_rdn_val(ac
->msg
->dn
);
885 if (rdn_value
== NULL
) {
886 return ldb_operr(ldb
);
888 sid
= dom_sid_parse_talloc(ac
->msg
,
889 (const char *)rdn_value
->data
);
891 ldb_set_errstring(ldb
,
892 "samldb: No valid SID found in ForeignSecurityPrincipal CN!");
893 return LDB_ERR_CONSTRAINT_VIOLATION
;
895 if (! samldb_msg_add_sid(ac
->msg
, "objectSid", sid
)) {
896 return ldb_operr(ldb
);
900 /* finally proceed with adding the entry */
901 ret
= samldb_add_step(ac
, samldb_add_entry
);
902 if (ret
!= LDB_SUCCESS
) return ret
;
904 return samldb_first_step(ac
);
907 static int samldb_schema_info_update(struct samldb_ctx
*ac
)
910 struct ldb_context
*ldb
;
911 struct dsdb_schema
*schema
;
913 /* replicated update should always go through */
914 if (ldb_request_get_control(ac
->req
,
915 DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
919 /* do not update schemaInfo during provisioning */
920 if (ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
)) {
924 ldb
= ldb_module_get_ctx(ac
->module
);
925 schema
= dsdb_get_schema(ldb
, NULL
);
927 ldb_debug_set(ldb
, LDB_DEBUG_FATAL
,
928 "samldb_schema_info_update: no dsdb_schema loaded");
929 DEBUG(0,(__location__
": %s\n", ldb_errstring(ldb
)));
930 return ldb_operr(ldb
);
933 ret
= dsdb_module_schema_info_update(ac
->module
, schema
,
934 DSDB_FLAG_NEXT_MODULE
|
937 if (ret
!= LDB_SUCCESS
) {
938 ldb_asprintf_errstring(ldb
,
939 "samldb_schema_info_update: dsdb_module_schema_info_update failed with %s",
947 static int samldb_prim_group_tester(struct samldb_ctx
*ac
, uint32_t rid
);
948 static int samldb_check_user_account_control_rules(struct samldb_ctx
*ac
,
950 uint32_t user_account_control
,
951 uint32_t user_account_control_old
);
954 * "Objectclass" trigger (MS-SAMR 3.1.1.8.1)
956 * Has to be invoked on "add" and "modify" operations on "user", "computer" and
958 * ac->msg contains the "add"/"modify" message
959 * ac->type contains the object type (main objectclass)
961 static int samldb_objectclass_trigger(struct samldb_ctx
*ac
)
963 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
964 void *skip_allocate_sids
= ldb_get_opaque(ldb
,
965 "skip_allocate_sids");
966 struct ldb_message_element
*el
, *el2
;
970 /* make sure that "sAMAccountType" is not specified */
971 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
973 ldb_set_errstring(ldb
,
974 "samldb: sAMAccountType must not be specified!");
975 return LDB_ERR_UNWILLING_TO_PERFORM
;
978 /* Step 1: objectSid assignment */
980 /* Don't allow the objectSid to be changed. But beside the RELAX
981 * control we have also to guarantee that it can always be set with
982 * SYSTEM permissions. This is needed for the "samba3sam" backend. */
983 sid
= samdb_result_dom_sid(ac
, ac
->msg
, "objectSid");
984 if ((sid
!= NULL
) && (!dsdb_module_am_system(ac
->module
)) &&
985 (ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
) == NULL
)) {
986 ldb_set_errstring(ldb
,
987 "samldb: objectSid must not be specified!");
988 return LDB_ERR_UNWILLING_TO_PERFORM
;
991 /* but generate a new SID when we do have an add operations */
992 if ((sid
== NULL
) && (ac
->req
->operation
== LDB_ADD
) && !skip_allocate_sids
) {
993 ret
= samldb_add_step(ac
, samldb_allocate_sid
);
994 if (ret
!= LDB_SUCCESS
) return ret
;
998 case SAMLDB_TYPE_USER
: {
999 bool uac_generated
= false, uac_add_flags
= false;
1001 /* Step 1.2: Default values */
1002 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1003 "accountExpires", "9223372036854775807");
1004 if (ret
!= LDB_SUCCESS
) return ret
;
1005 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1006 "badPasswordTime", "0");
1007 if (ret
!= LDB_SUCCESS
) return ret
;
1008 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1009 "badPwdCount", "0");
1010 if (ret
!= LDB_SUCCESS
) return ret
;
1011 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1013 if (ret
!= LDB_SUCCESS
) return ret
;
1014 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1015 "countryCode", "0");
1016 if (ret
!= LDB_SUCCESS
) return ret
;
1017 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1019 if (ret
!= LDB_SUCCESS
) return ret
;
1020 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1022 if (ret
!= LDB_SUCCESS
) return ret
;
1023 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1025 if (ret
!= LDB_SUCCESS
) return ret
;
1026 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1028 if (ret
!= LDB_SUCCESS
) return ret
;
1030 /* On add operations we might need to generate a
1031 * "userAccountControl" (if it isn't specified). */
1032 el
= ldb_msg_find_element(ac
->msg
, "userAccountControl");
1033 if ((el
== NULL
) && (ac
->req
->operation
== LDB_ADD
)) {
1034 ret
= samdb_msg_set_uint(ldb
, ac
->msg
, ac
->msg
,
1035 "userAccountControl",
1037 if (ret
!= LDB_SUCCESS
) {
1040 uac_generated
= true;
1041 uac_add_flags
= true;
1044 el
= ldb_msg_find_element(ac
->msg
, "userAccountControl");
1046 uint32_t user_account_control
, account_type
;
1047 /* Step 1.3: "userAccountControl" -> "sAMAccountType" mapping */
1048 user_account_control
= ldb_msg_find_attr_as_uint(ac
->msg
,
1049 "userAccountControl",
1051 /* "userAccountControl" = 0 or missing one of the types means "UF_NORMAL_ACCOUNT" */
1052 if ((user_account_control
& UF_ACCOUNT_TYPE_MASK
) == 0) {
1053 user_account_control
= UF_NORMAL_ACCOUNT
| user_account_control
;
1054 uac_generated
= true;
1058 * As per MS-SAMR 3.1.1.8.10 these flags have not to be set
1060 if ((user_account_control
& UF_LOCKOUT
) != 0) {
1061 user_account_control
&= ~UF_LOCKOUT
;
1062 uac_generated
= true;
1064 if ((user_account_control
& UF_PASSWORD_EXPIRED
) != 0) {
1065 user_account_control
&= ~UF_PASSWORD_EXPIRED
;
1066 uac_generated
= true;
1069 ret
= samldb_check_user_account_control_rules(ac
, NULL
,
1070 user_account_control
, 0);
1071 if (ret
!= LDB_SUCCESS
) {
1075 /* Workstation and (read-only) DC objects do need objectclass "computer" */
1076 if ((samdb_find_attribute(ldb
, ac
->msg
,
1077 "objectclass", "computer") == NULL
) &&
1078 (user_account_control
&
1079 (UF_SERVER_TRUST_ACCOUNT
| UF_WORKSTATION_TRUST_ACCOUNT
))) {
1080 ldb_set_errstring(ldb
,
1081 "samldb: Requested account type does need objectclass 'computer'!");
1082 return LDB_ERR_OBJECT_CLASS_VIOLATION
;
1085 account_type
= ds_uf2atype(user_account_control
);
1086 if (account_type
== 0) {
1087 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
1088 return LDB_ERR_UNWILLING_TO_PERFORM
;
1090 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1093 if (ret
!= LDB_SUCCESS
) {
1096 el2
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
1097 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1099 /* "isCriticalSystemObject" might be set */
1100 if (user_account_control
&
1101 (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
)) {
1102 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
1104 if (ret
!= LDB_SUCCESS
) {
1107 el2
= ldb_msg_find_element(ac
->msg
,
1108 "isCriticalSystemObject");
1109 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1110 } else if (user_account_control
& UF_WORKSTATION_TRUST_ACCOUNT
) {
1111 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
1113 if (ret
!= LDB_SUCCESS
) {
1116 el2
= ldb_msg_find_element(ac
->msg
,
1117 "isCriticalSystemObject");
1118 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1121 /* Step 1.4: "userAccountControl" -> "primaryGroupID" mapping */
1122 if (!ldb_msg_find_element(ac
->msg
, "primaryGroupID")) {
1123 uint32_t rid
= ds_uf2prim_group_rid(user_account_control
);
1126 * Older AD deployments don't know about the
1129 if (rid
== DOMAIN_RID_READONLY_DCS
) {
1130 ret
= samldb_prim_group_tester(ac
, rid
);
1131 if (ret
!= LDB_SUCCESS
) {
1136 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1137 "primaryGroupID", rid
);
1138 if (ret
!= LDB_SUCCESS
) {
1141 el2
= ldb_msg_find_element(ac
->msg
,
1143 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1146 /* Step 1.5: Add additional flags when needed */
1147 /* Obviously this is done when the "userAccountControl"
1148 * has been generated here (tested against Windows
1150 if (uac_generated
) {
1151 if (uac_add_flags
) {
1152 user_account_control
|= UF_ACCOUNTDISABLE
;
1153 user_account_control
|= UF_PASSWD_NOTREQD
;
1156 ret
= samdb_msg_set_uint(ldb
, ac
->msg
, ac
->msg
,
1157 "userAccountControl",
1158 user_account_control
);
1159 if (ret
!= LDB_SUCCESS
) {
1168 case SAMLDB_TYPE_GROUP
: {
1169 const char *tempstr
;
1171 /* Step 2.2: Default values */
1172 tempstr
= talloc_asprintf(ac
->msg
, "%d",
1173 GTYPE_SECURITY_GLOBAL_GROUP
);
1174 if (tempstr
== NULL
) return ldb_operr(ldb
);
1175 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1176 "groupType", tempstr
);
1177 if (ret
!= LDB_SUCCESS
) return ret
;
1179 /* Step 2.3: "groupType" -> "sAMAccountType" */
1180 el
= ldb_msg_find_element(ac
->msg
, "groupType");
1182 uint32_t group_type
, account_type
;
1184 group_type
= ldb_msg_find_attr_as_uint(ac
->msg
,
1187 /* The creation of builtin groups requires the
1189 if (group_type
== GTYPE_SECURITY_BUILTIN_LOCAL_GROUP
) {
1190 if (ldb_request_get_control(ac
->req
,
1191 LDB_CONTROL_RELAX_OID
) == NULL
) {
1192 return LDB_ERR_UNWILLING_TO_PERFORM
;
1196 account_type
= ds_gtype2atype(group_type
);
1197 if (account_type
== 0) {
1198 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
1199 return LDB_ERR_UNWILLING_TO_PERFORM
;
1201 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1204 if (ret
!= LDB_SUCCESS
) {
1207 el2
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
1208 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1214 ldb_asprintf_errstring(ldb
,
1215 "Invalid entry type!");
1216 return LDB_ERR_OPERATIONS_ERROR
;
1224 * "Primary group ID" trigger (MS-SAMR 3.1.1.8.2)
1226 * Has to be invoked on "add" and "modify" operations on "user" and "computer"
1228 * ac->msg contains the "add"/"modify" message
1231 static int samldb_prim_group_tester(struct samldb_ctx
*ac
, uint32_t rid
)
1233 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1234 struct dom_sid
*sid
;
1235 struct ldb_result
*res
;
1237 const char * const noattrs
[] = { NULL
};
1239 sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), rid
);
1241 return ldb_operr(ldb
);
1244 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
1245 ldb_get_default_basedn(ldb
),
1247 noattrs
, DSDB_FLAG_NEXT_MODULE
,
1250 ldap_encode_ndr_dom_sid(ac
, sid
));
1251 if (ret
!= LDB_SUCCESS
) {
1254 if (res
->count
!= 1) {
1256 ldb_asprintf_errstring(ldb
,
1257 "Failed to find primary group with RID %u!",
1259 return LDB_ERR_UNWILLING_TO_PERFORM
;
1266 static int samldb_prim_group_set(struct samldb_ctx
*ac
)
1268 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1271 rid
= ldb_msg_find_attr_as_uint(ac
->msg
, "primaryGroupID", (uint32_t) -1);
1272 if (rid
== (uint32_t) -1) {
1273 /* we aren't affected of any primary group set */
1276 } else if (!ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
)) {
1277 ldb_set_errstring(ldb
,
1278 "The primary group isn't settable on add operations!");
1279 return LDB_ERR_UNWILLING_TO_PERFORM
;
1282 return samldb_prim_group_tester(ac
, rid
);
1285 static int samldb_prim_group_change(struct samldb_ctx
*ac
)
1287 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1288 const char * const attrs
[] = { "primaryGroupID", "memberOf", NULL
};
1289 struct ldb_result
*res
, *group_res
;
1290 struct ldb_message_element
*el
;
1291 struct ldb_message
*msg
;
1292 uint32_t prev_rid
, new_rid
;
1293 struct dom_sid
*prev_sid
, *new_sid
;
1294 struct ldb_dn
*prev_prim_group_dn
, *new_prim_group_dn
;
1296 const char * const noattrs
[] = { NULL
};
1298 el
= dsdb_get_single_valued_attr(ac
->msg
, "primaryGroupID",
1299 ac
->req
->operation
);
1301 /* we are not affected */
1305 /* Fetch information from the existing object */
1307 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, attrs
,
1308 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1309 if (ret
!= LDB_SUCCESS
) {
1313 /* Finds out the DN of the old primary group */
1315 prev_rid
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "primaryGroupID",
1317 if (prev_rid
== (uint32_t) -1) {
1318 /* User objects do always have a mandatory "primaryGroupID"
1319 * attribute. If this doesn't exist then the object is of the
1320 * wrong type. This is the exact Windows error code */
1321 return LDB_ERR_OBJECT_CLASS_VIOLATION
;
1324 prev_sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), prev_rid
);
1325 if (prev_sid
== NULL
) {
1326 return ldb_operr(ldb
);
1329 /* Finds out the DN of the new primary group
1330 * Notice: in order to parse the primary group ID correctly we create
1331 * a temporary message here. */
1333 msg
= ldb_msg_new(ac
->msg
);
1335 return ldb_module_oom(ac
->module
);
1337 ret
= ldb_msg_add(msg
, el
, 0);
1338 if (ret
!= LDB_SUCCESS
) {
1341 new_rid
= ldb_msg_find_attr_as_uint(msg
, "primaryGroupID", (uint32_t) -1);
1343 if (new_rid
== (uint32_t) -1) {
1344 /* we aren't affected of any primary group change */
1348 if (prev_rid
== new_rid
) {
1352 ret
= dsdb_module_search(ac
->module
, ac
, &group_res
,
1353 ldb_get_default_basedn(ldb
),
1355 noattrs
, DSDB_FLAG_NEXT_MODULE
,
1358 ldap_encode_ndr_dom_sid(ac
, prev_sid
));
1359 if (ret
!= LDB_SUCCESS
) {
1362 if (group_res
->count
!= 1) {
1363 return ldb_operr(ldb
);
1365 prev_prim_group_dn
= group_res
->msgs
[0]->dn
;
1367 new_sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), new_rid
);
1368 if (new_sid
== NULL
) {
1369 return ldb_operr(ldb
);
1372 ret
= dsdb_module_search(ac
->module
, ac
, &group_res
,
1373 ldb_get_default_basedn(ldb
),
1375 noattrs
, DSDB_FLAG_NEXT_MODULE
,
1378 ldap_encode_ndr_dom_sid(ac
, new_sid
));
1379 if (ret
!= LDB_SUCCESS
) {
1382 if (group_res
->count
!= 1) {
1383 /* Here we know if the specified new primary group candidate is
1385 return LDB_ERR_UNWILLING_TO_PERFORM
;
1387 new_prim_group_dn
= group_res
->msgs
[0]->dn
;
1389 /* We need to be already a normal member of the new primary
1390 * group in order to be successful. */
1391 el
= samdb_find_attribute(ldb
, res
->msgs
[0], "memberOf",
1392 ldb_dn_get_linearized(new_prim_group_dn
));
1394 return LDB_ERR_UNWILLING_TO_PERFORM
;
1397 /* Remove the "member" attribute on the new primary group */
1398 msg
= ldb_msg_new(ac
->msg
);
1400 return ldb_module_oom(ac
->module
);
1402 msg
->dn
= new_prim_group_dn
;
1404 ret
= samdb_msg_add_delval(ldb
, msg
, msg
, "member",
1405 ldb_dn_get_linearized(ac
->msg
->dn
));
1406 if (ret
!= LDB_SUCCESS
) {
1410 ret
= dsdb_module_modify(ac
->module
, msg
, DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1411 if (ret
!= LDB_SUCCESS
) {
1416 /* Add a "member" attribute for the previous primary group */
1417 msg
= ldb_msg_new(ac
->msg
);
1419 return ldb_module_oom(ac
->module
);
1421 msg
->dn
= prev_prim_group_dn
;
1423 ret
= samdb_msg_add_addval(ldb
, msg
, msg
, "member",
1424 ldb_dn_get_linearized(ac
->msg
->dn
));
1425 if (ret
!= LDB_SUCCESS
) {
1429 ret
= dsdb_module_modify(ac
->module
, msg
, DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1430 if (ret
!= LDB_SUCCESS
) {
1438 static int samldb_prim_group_trigger(struct samldb_ctx
*ac
)
1442 if (ac
->req
->operation
== LDB_ADD
) {
1443 ret
= samldb_prim_group_set(ac
);
1445 ret
= samldb_prim_group_change(ac
);
1451 static int samldb_check_user_account_control_invariants(struct samldb_ctx
*ac
,
1452 uint32_t user_account_control
)
1455 bool need_check
= false;
1456 const struct uac_to_guid
{
1461 const char *error_string
;
1464 .uac
= UF_TEMP_DUPLICATE_ACCOUNT
,
1466 .error_string
= "Updating the UF_TEMP_DUPLICATE_ACCOUNT flag is never allowed"
1469 .uac
= UF_PARTIAL_SECRETS_ACCOUNT
,
1470 .needs
= UF_WORKSTATION_TRUST_ACCOUNT
,
1471 .error_string
= "Setting UF_PARTIAL_SECRETS_ACCOUNT only permitted with UF_WORKSTATION_TRUST_ACCOUNT"
1474 .uac
= UF_TRUSTED_FOR_DELEGATION
,
1475 .not_with
= UF_PARTIAL_SECRETS_ACCOUNT
,
1476 .error_string
= "Setting UF_TRUSTED_FOR_DELEGATION not allowed with UF_PARTIAL_SECRETS_ACCOUNT"
1479 .uac
= UF_NORMAL_ACCOUNT
,
1480 .not_with
= UF_ACCOUNT_TYPE_MASK
& ~UF_NORMAL_ACCOUNT
,
1481 .error_string
= "Setting more than one account type not permitted"
1484 .uac
= UF_WORKSTATION_TRUST_ACCOUNT
,
1485 .not_with
= UF_ACCOUNT_TYPE_MASK
& ~UF_WORKSTATION_TRUST_ACCOUNT
,
1486 .error_string
= "Setting more than one account type not permitted"
1489 .uac
= UF_INTERDOMAIN_TRUST_ACCOUNT
,
1490 .not_with
= UF_ACCOUNT_TYPE_MASK
& ~UF_INTERDOMAIN_TRUST_ACCOUNT
,
1491 .error_string
= "Setting more than one account type not permitted"
1494 .uac
= UF_SERVER_TRUST_ACCOUNT
,
1495 .not_with
= UF_ACCOUNT_TYPE_MASK
& ~UF_SERVER_TRUST_ACCOUNT
,
1496 .error_string
= "Setting more than one account type not permitted"
1499 .uac
= UF_TRUSTED_FOR_DELEGATION
,
1500 .not_with
= UF_PARTIAL_SECRETS_ACCOUNT
,
1501 .error_string
= "Setting UF_TRUSTED_FOR_DELEGATION not allowed with UF_PARTIAL_SECRETS_ACCOUNT"
1505 for (i
= 0; i
< ARRAY_SIZE(map
); i
++) {
1506 if (user_account_control
& map
[i
].uac
) {
1511 if (need_check
== false) {
1515 for (i
= 0; i
< ARRAY_SIZE(map
); i
++) {
1516 uint32_t this_uac
= user_account_control
& map
[i
].uac
;
1517 if (this_uac
!= 0) {
1519 ret
= LDB_ERR_OTHER
;
1521 } else if (map
[i
].needs
!= 0) {
1522 if ((map
[i
].needs
& user_account_control
) == 0) {
1523 ret
= LDB_ERR_OTHER
;
1526 } else if (map
[i
].not_with
!= 0) {
1527 if ((map
[i
].not_with
& user_account_control
) != 0) {
1528 ret
= LDB_ERR_OTHER
;
1534 if (ret
!= LDB_SUCCESS
) {
1535 switch (ac
->req
->operation
) {
1537 ldb_asprintf_errstring(ldb_module_get_ctx(ac
->module
),
1538 "Failed to add %s: %s",
1539 ldb_dn_get_linearized(ac
->msg
->dn
),
1540 map
[i
].error_string
);
1543 ldb_asprintf_errstring(ldb_module_get_ctx(ac
->module
),
1544 "Failed to modify %s: %s",
1545 ldb_dn_get_linearized(ac
->msg
->dn
),
1546 map
[i
].error_string
);
1549 return ldb_module_operr(ac
->module
);
1556 * Validate that the restriction in point 5 of MS-SAMR 3.1.1.8.10 userAccountControl is honoured
1559 static int samldb_check_user_account_control_acl(struct samldb_ctx
*ac
,
1560 struct dom_sid
*sid
,
1561 uint32_t user_account_control
,
1562 uint32_t user_account_control_old
)
1565 bool need_acl_check
= false;
1566 struct ldb_result
*res
;
1567 const char * const sd_attrs
[] = {"ntSecurityDescriptor", NULL
};
1568 struct security_token
*user_token
;
1569 struct security_descriptor
*domain_sd
;
1570 struct ldb_dn
*domain_dn
= ldb_get_default_basedn(ldb_module_get_ctx(ac
->module
));
1571 const struct uac_to_guid
{
1575 enum sec_privilege privilege
;
1576 bool delete_is_privileged
;
1577 const char *error_string
;
1580 .uac
= UF_PASSWD_NOTREQD
,
1581 .guid
= GUID_DRS_UPDATE_PASSWORD_NOT_REQUIRED_BIT
,
1582 .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"
1585 .uac
= UF_DONT_EXPIRE_PASSWD
,
1586 .guid
= GUID_DRS_UNEXPIRE_PASSWORD
,
1587 .error_string
= "Adding the UF_DONT_EXPIRE_PASSWD bit in userAccountControl requires the Unexpire-Password right that was not given on the Domain object"
1590 .uac
= UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED
,
1591 .guid
= GUID_DRS_ENABLE_PER_USER_REVERSIBLY_ENCRYPTED_PASSWORD
,
1592 .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"
1595 .uac
= UF_SERVER_TRUST_ACCOUNT
,
1596 .guid
= GUID_DRS_DS_INSTALL_REPLICA
,
1597 .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"
1600 .uac
= UF_PARTIAL_SECRETS_ACCOUNT
,
1601 .guid
= GUID_DRS_DS_INSTALL_REPLICA
,
1602 .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"
1605 .uac
= UF_INTERDOMAIN_TRUST_ACCOUNT
,
1606 .oid
= DSDB_CONTROL_PERMIT_INTERDOMAIN_TRUST_UAC_OID
,
1607 .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",
1608 .delete_is_privileged
= true
1611 .uac
= UF_TRUSTED_FOR_DELEGATION
,
1612 .privilege
= SEC_PRIV_ENABLE_DELEGATION
,
1613 .delete_is_privileged
= true,
1614 .error_string
= "Updating the UF_TRUSTED_FOR_DELEGATION bit in userAccountControl is not permitted without the SeEnableDelegationPrivilege"
1617 .uac
= UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION
,
1618 .privilege
= SEC_PRIV_ENABLE_DELEGATION
,
1619 .delete_is_privileged
= true,
1620 .error_string
= "Updating the UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION bit in userAccountControl is not permitted without the SeEnableDelegationPrivilege"
1625 if (dsdb_module_am_system(ac
->module
)) {
1629 for (i
= 0; i
< ARRAY_SIZE(map
); i
++) {
1630 if (user_account_control
& map
[i
].uac
) {
1631 need_acl_check
= true;
1635 if (need_acl_check
== false) {
1639 user_token
= acl_user_token(ac
->module
);
1640 if (user_token
== NULL
) {
1641 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1644 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
,
1647 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
,
1649 if (ret
!= LDB_SUCCESS
) {
1652 if (res
->count
!= 1) {
1653 return ldb_module_operr(ac
->module
);
1656 ret
= dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(ac
->module
),
1657 ac
, res
->msgs
[0], &domain_sd
);
1659 if (ret
!= LDB_SUCCESS
) {
1663 for (i
= 0; i
< ARRAY_SIZE(map
); i
++) {
1664 uint32_t this_uac_new
= user_account_control
& map
[i
].uac
;
1665 uint32_t this_uac_old
= user_account_control_old
& map
[i
].uac
;
1666 if (this_uac_new
!= this_uac_old
) {
1667 if (this_uac_old
!= 0) {
1668 if (map
[i
].delete_is_privileged
== false) {
1673 struct ldb_control
*control
= ldb_request_get_control(ac
->req
, map
[i
].oid
);
1674 if (control
== NULL
) {
1675 ret
= LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1677 } else if (map
[i
].privilege
!= SEC_PRIV_INVALID
) {
1678 bool have_priv
= security_token_has_privilege(user_token
,
1680 if (have_priv
== false) {
1681 ret
= LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1684 ret
= acl_check_extended_right(ac
, domain_sd
,
1687 SEC_ADS_CONTROL_ACCESS
,
1690 if (ret
!= LDB_SUCCESS
) {
1695 if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
1696 switch (ac
->req
->operation
) {
1698 ldb_asprintf_errstring(ldb_module_get_ctx(ac
->module
),
1699 "Failed to add %s: %s",
1700 ldb_dn_get_linearized(ac
->msg
->dn
),
1701 map
[i
].error_string
);
1704 ldb_asprintf_errstring(ldb_module_get_ctx(ac
->module
),
1705 "Failed to modify %s: %s",
1706 ldb_dn_get_linearized(ac
->msg
->dn
),
1707 map
[i
].error_string
);
1710 return ldb_module_operr(ac
->module
);
1713 dsdb_acl_debug(domain_sd
, acl_user_token(ac
->module
),
1722 static int samldb_check_user_account_control_rules(struct samldb_ctx
*ac
,
1723 struct dom_sid
*sid
,
1724 uint32_t user_account_control
,
1725 uint32_t user_account_control_old
)
1728 ret
= samldb_check_user_account_control_invariants(ac
, user_account_control
);
1729 if (ret
!= LDB_SUCCESS
) {
1732 ret
= samldb_check_user_account_control_acl(ac
, sid
, user_account_control
, user_account_control_old
);
1733 if (ret
!= LDB_SUCCESS
) {
1741 * This function is called on LDB modify operations. It performs some additions/
1742 * replaces on the current LDB message when "userAccountControl" changes.
1744 static int samldb_user_account_control_change(struct samldb_ctx
*ac
)
1746 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1759 NTTIME old_lockoutTime
;
1760 struct ldb_message_element
*el
;
1761 struct ldb_val
*val
;
1762 struct ldb_val computer_val
;
1763 struct ldb_message
*tmp_msg
;
1764 struct dom_sid
*sid
;
1766 struct ldb_result
*res
;
1767 const char * const attrs
[] = {
1769 "isCriticalSystemObject",
1770 "userAccountControl",
1771 "msDS-User-Account-Control-Computed",
1776 bool is_computer
= false;
1777 bool old_is_critical
= false;
1778 bool new_is_critical
= false;
1780 el
= dsdb_get_single_valued_attr(ac
->msg
, "userAccountControl",
1781 ac
->req
->operation
);
1782 if (el
== NULL
|| el
->num_values
== 0) {
1783 ldb_asprintf_errstring(ldb
,
1784 "%08X: samldb: 'userAccountControl' can't be deleted!",
1785 W_ERROR_V(WERR_DS_ILLEGAL_MOD_OPERATION
));
1786 return LDB_ERR_UNWILLING_TO_PERFORM
;
1789 /* Create a temporary message for fetching the "userAccountControl" */
1790 tmp_msg
= ldb_msg_new(ac
->msg
);
1791 if (tmp_msg
== NULL
) {
1792 return ldb_module_oom(ac
->module
);
1794 ret
= ldb_msg_add(tmp_msg
, el
, 0);
1795 if (ret
!= LDB_SUCCESS
) {
1798 raw_uac
= ldb_msg_find_attr_as_uint(tmp_msg
,
1799 "userAccountControl",
1801 new_acb
= samdb_result_acct_flags(tmp_msg
, NULL
);
1802 talloc_free(tmp_msg
);
1804 * UF_LOCKOUT and UF_PASSWORD_EXPIRED are only generated
1805 * and not stored. We ignore them almost completely.
1807 * The only exception is the resulting ACB_AUTOLOCK in clear_acb.
1809 new_uac
= raw_uac
& ~(UF_LOCKOUT
|UF_PASSWORD_EXPIRED
);
1811 /* Fetch the old "userAccountControl" and "objectClass" */
1812 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, attrs
,
1813 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1814 if (ret
!= LDB_SUCCESS
) {
1817 old_uac
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "userAccountControl", 0);
1819 return ldb_operr(ldb
);
1821 old_acb
= samdb_result_acct_flags(res
->msgs
[0],
1822 "msDS-User-Account-Control-Computed");
1823 old_lockoutTime
= ldb_msg_find_attr_as_int64(res
->msgs
[0],
1825 old_is_critical
= ldb_msg_find_attr_as_bool(res
->msgs
[0],
1826 "isCriticalSystemObject", 0);
1827 /* When we do not have objectclass "omputer" we cannot switch to a (read-only) DC */
1828 el
= ldb_msg_find_element(res
->msgs
[0], "objectClass");
1830 return ldb_operr(ldb
);
1832 computer_val
= data_blob_string_const("computer");
1833 val
= ldb_msg_find_val(el
, &computer_val
);
1838 old_ufa
= old_uac
& UF_ACCOUNT_TYPE_MASK
;
1839 old_atype
= ds_uf2atype(old_ufa
);
1840 old_pgrid
= ds_uf2prim_group_rid(old_uac
);
1842 new_ufa
= new_uac
& UF_ACCOUNT_TYPE_MASK
;
1845 * When there is no account type embedded in "userAccountControl"
1846 * fall back to UF_NORMAL_ACCOUNT.
1848 new_ufa
= UF_NORMAL_ACCOUNT
;
1851 sid
= samdb_result_dom_sid(res
, res
->msgs
[0], "objectSid");
1853 return ldb_module_operr(ac
->module
);
1856 ret
= samldb_check_user_account_control_rules(ac
, sid
, new_uac
, old_uac
);
1857 if (ret
!= LDB_SUCCESS
) {
1861 new_atype
= ds_uf2atype(new_ufa
);
1862 new_pgrid
= ds_uf2prim_group_rid(new_uac
);
1864 clear_acb
= old_acb
& ~new_acb
;
1867 case UF_NORMAL_ACCOUNT
:
1868 new_is_critical
= old_is_critical
;
1871 case UF_INTERDOMAIN_TRUST_ACCOUNT
:
1872 new_is_critical
= true;
1875 case UF_WORKSTATION_TRUST_ACCOUNT
:
1876 new_is_critical
= false;
1877 if (new_uac
& UF_PARTIAL_SECRETS_ACCOUNT
) {
1879 ldb_asprintf_errstring(ldb
,
1880 "%08X: samldb: UF_PARTIAL_SECRETS_ACCOUNT "
1881 "requires objectclass 'computer'!",
1882 W_ERROR_V(WERR_DS_MACHINE_ACCOUNT_CREATED_PRENT4
));
1883 return LDB_ERR_UNWILLING_TO_PERFORM
;
1885 new_is_critical
= true;
1889 case UF_SERVER_TRUST_ACCOUNT
:
1891 ldb_asprintf_errstring(ldb
,
1892 "%08X: samldb: UF_SERVER_TRUST_ACCOUNT "
1893 "requires objectclass 'computer'!",
1894 W_ERROR_V(WERR_DS_MACHINE_ACCOUNT_CREATED_PRENT4
));
1895 return LDB_ERR_UNWILLING_TO_PERFORM
;
1897 new_is_critical
= true;
1901 ldb_asprintf_errstring(ldb
,
1902 "%08X: samldb: invalid userAccountControl[0x%08X]",
1903 W_ERROR_V(WERR_INVALID_PARAMETER
), raw_uac
);
1904 return LDB_ERR_OTHER
;
1907 if (old_atype
!= new_atype
) {
1908 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1909 "sAMAccountType", new_atype
);
1910 if (ret
!= LDB_SUCCESS
) {
1913 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
1914 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1917 /* As per MS-SAMR 3.1.1.8.10 these flags have not to be set */
1918 if ((clear_acb
& ACB_AUTOLOCK
) && (old_lockoutTime
!= 0)) {
1919 /* "pwdLastSet" reset as password expiration has been forced */
1920 ldb_msg_remove_attr(ac
->msg
, "lockoutTime");
1921 ret
= samdb_msg_add_uint64(ldb
, ac
->msg
, ac
->msg
, "lockoutTime",
1923 if (ret
!= LDB_SUCCESS
) {
1926 el
= ldb_msg_find_element(ac
->msg
, "lockoutTime");
1927 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1930 /* "isCriticalSystemObject" might be set/changed */
1931 if (old_is_critical
!= new_is_critical
) {
1932 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
1933 new_is_critical
? "TRUE": "FALSE");
1934 if (ret
!= LDB_SUCCESS
) {
1937 el
= ldb_msg_find_element(ac
->msg
,
1938 "isCriticalSystemObject");
1939 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1942 if (!ldb_msg_find_element(ac
->msg
, "primaryGroupID") &&
1943 (old_pgrid
!= new_pgrid
)) {
1944 /* Older AD deployments don't know about the RODC group */
1945 if (new_pgrid
== DOMAIN_RID_READONLY_DCS
) {
1946 ret
= samldb_prim_group_tester(ac
, new_pgrid
);
1947 if (ret
!= LDB_SUCCESS
) {
1952 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1953 "primaryGroupID", new_pgrid
);
1954 if (ret
!= LDB_SUCCESS
) {
1957 el
= ldb_msg_find_element(ac
->msg
,
1959 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1962 /* Propagate eventual "userAccountControl" attribute changes */
1963 if (old_uac
!= new_uac
) {
1964 char *tempstr
= talloc_asprintf(ac
->msg
, "%d",
1966 if (tempstr
== NULL
) {
1967 return ldb_module_oom(ac
->module
);
1970 /* Overwrite "userAccountControl" correctly */
1971 el
= dsdb_get_single_valued_attr(ac
->msg
, "userAccountControl",
1972 ac
->req
->operation
);
1973 el
->values
[0].data
= (uint8_t *) tempstr
;
1974 el
->values
[0].length
= strlen(tempstr
);
1976 ldb_msg_remove_attr(ac
->msg
, "userAccountControl");
1982 static int samldb_lockout_time(struct samldb_ctx
*ac
)
1984 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1986 struct ldb_message_element
*el
;
1987 struct ldb_message
*tmp_msg
;
1990 el
= dsdb_get_single_valued_attr(ac
->msg
, "lockoutTime",
1991 ac
->req
->operation
);
1992 if (el
== NULL
|| el
->num_values
== 0) {
1993 ldb_asprintf_errstring(ldb
,
1994 "%08X: samldb: 'lockoutTime' can't be deleted!",
1995 W_ERROR_V(WERR_DS_ILLEGAL_MOD_OPERATION
));
1996 return LDB_ERR_UNWILLING_TO_PERFORM
;
1999 /* Create a temporary message for fetching the "lockoutTime" */
2000 tmp_msg
= ldb_msg_new(ac
->msg
);
2001 if (tmp_msg
== NULL
) {
2002 return ldb_module_oom(ac
->module
);
2004 ret
= ldb_msg_add(tmp_msg
, el
, 0);
2005 if (ret
!= LDB_SUCCESS
) {
2008 lockoutTime
= ldb_msg_find_attr_as_int64(tmp_msg
,
2011 talloc_free(tmp_msg
);
2013 if (lockoutTime
!= 0) {
2017 /* lockoutTime == 0 resets badPwdCount */
2018 ldb_msg_remove_attr(ac
->msg
, "badPwdCount");
2019 ret
= samdb_msg_add_int(ldb
, ac
->msg
, ac
->msg
,
2021 if (ret
!= LDB_SUCCESS
) {
2024 el
= ldb_msg_find_element(ac
->msg
, "badPwdCount");
2025 el
->flags
= LDB_FLAG_MOD_REPLACE
;
2030 static int samldb_group_type_change(struct samldb_ctx
*ac
)
2032 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
2033 uint32_t group_type
, old_group_type
, account_type
;
2034 struct ldb_message_element
*el
;
2035 struct ldb_message
*tmp_msg
;
2037 struct ldb_result
*res
;
2038 const char * const attrs
[] = { "groupType", NULL
};
2040 el
= dsdb_get_single_valued_attr(ac
->msg
, "groupType",
2041 ac
->req
->operation
);
2043 /* we are not affected */
2047 /* Create a temporary message for fetching the "groupType" */
2048 tmp_msg
= ldb_msg_new(ac
->msg
);
2049 if (tmp_msg
== NULL
) {
2050 return ldb_module_oom(ac
->module
);
2052 ret
= ldb_msg_add(tmp_msg
, el
, 0);
2053 if (ret
!= LDB_SUCCESS
) {
2056 group_type
= ldb_msg_find_attr_as_uint(tmp_msg
, "groupType", 0);
2057 talloc_free(tmp_msg
);
2059 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, attrs
,
2060 DSDB_FLAG_NEXT_MODULE
|
2061 DSDB_SEARCH_SHOW_DELETED
, ac
->req
);
2062 if (ret
!= LDB_SUCCESS
) {
2065 old_group_type
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "groupType", 0);
2066 if (old_group_type
== 0) {
2067 return ldb_operr(ldb
);
2070 /* Group type switching isn't so easy as it seems: We can only
2071 * change in this directions: global <-> universal <-> local
2072 * On each step also the group type itself
2073 * (security/distribution) is variable. */
2075 if (ldb_request_get_control(ac
->req
, LDB_CONTROL_PROVISION_OID
) == NULL
) {
2076 switch (group_type
) {
2077 case GTYPE_SECURITY_GLOBAL_GROUP
:
2078 case GTYPE_DISTRIBUTION_GLOBAL_GROUP
:
2079 /* change to "universal" allowed */
2080 if ((old_group_type
== GTYPE_SECURITY_DOMAIN_LOCAL_GROUP
) ||
2081 (old_group_type
== GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP
)) {
2082 ldb_set_errstring(ldb
,
2083 "samldb: Change from security/distribution local group forbidden!");
2084 return LDB_ERR_UNWILLING_TO_PERFORM
;
2088 case GTYPE_SECURITY_UNIVERSAL_GROUP
:
2089 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP
:
2090 /* each change allowed */
2092 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP
:
2093 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP
:
2094 /* change to "universal" allowed */
2095 if ((old_group_type
== GTYPE_SECURITY_GLOBAL_GROUP
) ||
2096 (old_group_type
== GTYPE_DISTRIBUTION_GLOBAL_GROUP
)) {
2097 ldb_set_errstring(ldb
,
2098 "samldb: Change from security/distribution global group forbidden!");
2099 return LDB_ERR_UNWILLING_TO_PERFORM
;
2103 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP
:
2105 /* we don't allow this "groupType" values */
2106 return LDB_ERR_UNWILLING_TO_PERFORM
;
2111 account_type
= ds_gtype2atype(group_type
);
2112 if (account_type
== 0) {
2113 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
2114 return LDB_ERR_UNWILLING_TO_PERFORM
;
2116 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
, "sAMAccountType",
2118 if (ret
!= LDB_SUCCESS
) {
2121 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
2122 el
->flags
= LDB_FLAG_MOD_REPLACE
;
2127 static int samldb_sam_accountname_check(struct samldb_ctx
*ac
)
2129 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
2130 const char * const no_attrs
[] = { NULL
};
2131 struct ldb_result
*res
;
2132 const char *sam_accountname
, *enc_str
;
2133 struct ldb_message_element
*el
;
2134 struct ldb_message
*tmp_msg
;
2137 el
= dsdb_get_single_valued_attr(ac
->msg
, "sAMAccountName",
2138 ac
->req
->operation
);
2140 /* we are not affected */
2144 /* Create a temporary message for fetching the "sAMAccountName" */
2145 tmp_msg
= ldb_msg_new(ac
->msg
);
2146 if (tmp_msg
== NULL
) {
2147 return ldb_module_oom(ac
->module
);
2149 ret
= ldb_msg_add(tmp_msg
, el
, 0);
2150 if (ret
!= LDB_SUCCESS
) {
2154 /* We must not steal the original string, it belongs to the caller! */
2155 sam_accountname
= talloc_strdup(ac
,
2156 ldb_msg_find_attr_as_string(tmp_msg
, "sAMAccountName", NULL
));
2157 talloc_free(tmp_msg
);
2159 if (sam_accountname
== NULL
) {
2160 /* The "sAMAccountName" cannot be nothing */
2161 ldb_set_errstring(ldb
,
2162 "samldb: Empty account names aren't allowed!");
2163 return LDB_ERR_UNWILLING_TO_PERFORM
;
2166 enc_str
= ldb_binary_encode_string(ac
, sam_accountname
);
2167 if (enc_str
== NULL
) {
2168 return ldb_module_oom(ac
->module
);
2171 /* Make sure that a "sAMAccountName" is only used once */
2173 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
2174 ldb_get_default_basedn(ldb
),
2175 LDB_SCOPE_SUBTREE
, no_attrs
,
2176 DSDB_FLAG_NEXT_MODULE
, ac
->req
,
2177 "(sAMAccountName=%s)", enc_str
);
2178 if (ret
!= LDB_SUCCESS
) {
2181 if (res
->count
> 1) {
2182 return ldb_operr(ldb
);
2183 } else if (res
->count
== 1) {
2184 if (ldb_dn_compare(res
->msgs
[0]->dn
, ac
->msg
->dn
) != 0) {
2185 ldb_asprintf_errstring(ldb
,
2186 "samldb: Account name (sAMAccountName) '%s' already in use!",
2188 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
2196 static int samldb_member_check(struct samldb_ctx
*ac
)
2198 const char * const attrs
[] = { "objectSid", NULL
};
2199 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
2200 struct ldb_message_element
*el
;
2201 struct ldb_dn
*member_dn
;
2202 struct dom_sid
*sid
;
2203 struct ldb_result
*res
;
2204 struct dom_sid
*group_sid
;
2208 /* Fetch information from the existing object */
2210 ret
= dsdb_module_search(ac
->module
, ac
, &res
, ac
->msg
->dn
, LDB_SCOPE_BASE
, attrs
,
2211 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
, ac
->req
, NULL
);
2212 if (ret
!= LDB_SUCCESS
) {
2215 if (res
->count
!= 1) {
2216 return ldb_operr(ldb
);
2219 group_sid
= samdb_result_dom_sid(res
, res
->msgs
[0], "objectSid");
2220 if (group_sid
== NULL
) {
2221 return ldb_operr(ldb
);
2224 /* We've to walk over all modification entries and consider the "member"
2226 for (i
= 0; i
< ac
->msg
->num_elements
; i
++) {
2227 if (ldb_attr_cmp(ac
->msg
->elements
[i
].name
, "member") != 0) {
2231 el
= &ac
->msg
->elements
[i
];
2232 for (j
= 0; j
< el
->num_values
; j
++) {
2233 struct ldb_result
*group_res
;
2234 const char *group_attrs
[] = { "primaryGroupID" , NULL
};
2235 uint32_t prim_group_rid
;
2237 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_DELETE
) {
2238 /* Deletes will be handled in
2239 * repl_meta_data, and deletes not
2240 * matching a member will return
2241 * LDB_ERR_UNWILLING_TO_PERFORM
2246 member_dn
= ldb_dn_from_ldb_val(ac
, ldb
,
2248 if (!ldb_dn_validate(member_dn
)) {
2249 return ldb_operr(ldb
);
2252 /* Denies to add "member"s to groups which are primary
2253 * ones for them - in this case return
2254 * ERR_ENTRY_ALREADY_EXISTS. */
2256 ret
= dsdb_module_search_dn(ac
->module
, ac
, &group_res
,
2257 member_dn
, group_attrs
,
2258 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
2259 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
2260 /* member DN doesn't exist yet */
2263 if (ret
!= LDB_SUCCESS
) {
2266 prim_group_rid
= ldb_msg_find_attr_as_uint(group_res
->msgs
[0], "primaryGroupID", (uint32_t)-1);
2267 if (prim_group_rid
== (uint32_t) -1) {
2268 /* the member hasn't to be a user account ->
2269 * therefore no check needed in this case. */
2273 sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
),
2276 return ldb_operr(ldb
);
2279 if (dom_sid_equal(group_sid
, sid
)) {
2280 ldb_asprintf_errstring(ldb
,
2281 "samldb: member %s already set via primaryGroupID %u",
2282 ldb_dn_get_linearized(member_dn
), prim_group_rid
);
2283 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
2293 /* SAM objects have special rules regarding the "description" attribute on
2294 * modify operations. */
2295 static int samldb_description_check(struct samldb_ctx
*ac
, bool *modified
)
2297 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
2298 const char * const attrs
[] = { "objectClass", "description", NULL
};
2299 struct ldb_result
*res
;
2303 /* Fetch information from the existing object */
2304 ret
= dsdb_module_search(ac
->module
, ac
, &res
, ac
->msg
->dn
, LDB_SCOPE_BASE
, attrs
,
2305 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
, ac
->req
,
2306 "(|(objectclass=user)(objectclass=group)(objectclass=samDomain)(objectclass=samServer))");
2307 if (ret
!= LDB_SUCCESS
) {
2308 /* don't treat it specially ... let normal error codes
2309 happen from other places */
2310 ldb_reset_err_string(ldb
);
2313 if (res
->count
== 0) {
2314 /* we didn't match the filter */
2319 /* We've to walk over all modification entries and consider the
2320 * "description" ones. */
2321 for (i
= 0; i
< ac
->msg
->num_elements
; i
++) {
2322 if (ldb_attr_cmp(ac
->msg
->elements
[i
].name
, "description") == 0) {
2323 ac
->msg
->elements
[i
].flags
|= LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK
;
2333 /* This trigger adapts the "servicePrincipalName" attributes if the
2334 * "dNSHostName" and/or "sAMAccountName" attribute change(s) */
2335 static int samldb_service_principal_names_change(struct samldb_ctx
*ac
)
2337 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
2338 struct ldb_message_element
*el
= NULL
, *el2
= NULL
;
2339 struct ldb_message
*msg
;
2340 const char * const attrs
[] = { "servicePrincipalName", NULL
};
2341 struct ldb_result
*res
;
2342 const char *dns_hostname
= NULL
, *old_dns_hostname
= NULL
,
2343 *sam_accountname
= NULL
, *old_sam_accountname
= NULL
;
2347 el
= dsdb_get_single_valued_attr(ac
->msg
, "dNSHostName",
2348 ac
->req
->operation
);
2349 el2
= dsdb_get_single_valued_attr(ac
->msg
, "sAMAccountName",
2350 ac
->req
->operation
);
2351 if ((el
== NULL
) && (el2
== NULL
)) {
2352 /* we are not affected */
2356 /* Create a temporary message for fetching the "dNSHostName" */
2358 const char *dns_attrs
[] = { "dNSHostName", NULL
};
2359 msg
= ldb_msg_new(ac
->msg
);
2361 return ldb_module_oom(ac
->module
);
2363 ret
= ldb_msg_add(msg
, el
, 0);
2364 if (ret
!= LDB_SUCCESS
) {
2367 dns_hostname
= talloc_strdup(ac
,
2368 ldb_msg_find_attr_as_string(msg
, "dNSHostName", NULL
));
2369 if (dns_hostname
== NULL
) {
2370 return ldb_module_oom(ac
->module
);
2375 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
,
2376 dns_attrs
, DSDB_FLAG_NEXT_MODULE
, ac
->req
);
2377 if (ret
== LDB_SUCCESS
) {
2378 old_dns_hostname
= ldb_msg_find_attr_as_string(res
->msgs
[0], "dNSHostName", NULL
);
2382 /* Create a temporary message for fetching the "sAMAccountName" */
2384 char *tempstr
, *tempstr2
= NULL
;
2385 const char *acct_attrs
[] = { "sAMAccountName", NULL
};
2387 msg
= ldb_msg_new(ac
->msg
);
2389 return ldb_module_oom(ac
->module
);
2391 ret
= ldb_msg_add(msg
, el2
, 0);
2392 if (ret
!= LDB_SUCCESS
) {
2395 tempstr
= talloc_strdup(ac
,
2396 ldb_msg_find_attr_as_string(msg
, "sAMAccountName", NULL
));
2399 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, acct_attrs
,
2400 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
2401 if (ret
== LDB_SUCCESS
) {
2402 tempstr2
= talloc_strdup(ac
,
2403 ldb_msg_find_attr_as_string(res
->msgs
[0],
2404 "sAMAccountName", NULL
));
2408 /* The "sAMAccountName" needs some additional trimming: we need
2409 * to remove the trailing "$"s if they exist. */
2410 if ((tempstr
!= NULL
) && (tempstr
[0] != '\0') &&
2411 (tempstr
[strlen(tempstr
) - 1] == '$')) {
2412 tempstr
[strlen(tempstr
) - 1] = '\0';
2414 if ((tempstr2
!= NULL
) && (tempstr2
[0] != '\0') &&
2415 (tempstr2
[strlen(tempstr2
) - 1] == '$')) {
2416 tempstr2
[strlen(tempstr2
) - 1] = '\0';
2418 sam_accountname
= tempstr
;
2419 old_sam_accountname
= tempstr2
;
2422 if (old_dns_hostname
== NULL
) {
2423 /* we cannot change when the old name is unknown */
2424 dns_hostname
= NULL
;
2426 if ((old_dns_hostname
!= NULL
) && (dns_hostname
!= NULL
) &&
2427 (strcasecmp_m(old_dns_hostname
, dns_hostname
) == 0)) {
2428 /* The "dNSHostName" didn't change */
2429 dns_hostname
= NULL
;
2432 if (old_sam_accountname
== NULL
) {
2433 /* we cannot change when the old name is unknown */
2434 sam_accountname
= NULL
;
2436 if ((old_sam_accountname
!= NULL
) && (sam_accountname
!= NULL
) &&
2437 (strcasecmp_m(old_sam_accountname
, sam_accountname
) == 0)) {
2438 /* The "sAMAccountName" didn't change */
2439 sam_accountname
= NULL
;
2442 if ((dns_hostname
== NULL
) && (sam_accountname
== NULL
)) {
2443 /* Well, there are information missing (old name(s)) or the
2444 * names didn't change. We've nothing to do and can exit here */
2448 /* Potential "servicePrincipalName" changes in the same request have to
2449 * be handled before the update (Windows behaviour). */
2450 el
= ldb_msg_find_element(ac
->msg
, "servicePrincipalName");
2452 msg
= ldb_msg_new(ac
->msg
);
2454 return ldb_module_oom(ac
->module
);
2456 msg
->dn
= ac
->msg
->dn
;
2459 ret
= ldb_msg_add(msg
, el
, el
->flags
);
2460 if (ret
!= LDB_SUCCESS
) {
2464 ldb_msg_remove_element(ac
->msg
, el
);
2466 el
= ldb_msg_find_element(ac
->msg
,
2467 "servicePrincipalName");
2468 } while (el
!= NULL
);
2470 ret
= dsdb_module_modify(ac
->module
, msg
,
2471 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
2472 if (ret
!= LDB_SUCCESS
) {
2478 /* Fetch the "servicePrincipalName"s if any */
2479 ret
= dsdb_module_search(ac
->module
, ac
, &res
, ac
->msg
->dn
, LDB_SCOPE_BASE
, attrs
,
2480 DSDB_FLAG_NEXT_MODULE
, ac
->req
, NULL
);
2481 if (ret
!= LDB_SUCCESS
) {
2484 if ((res
->count
!= 1) || (res
->msgs
[0]->num_elements
> 1)) {
2485 return ldb_operr(ldb
);
2488 if (res
->msgs
[0]->num_elements
== 1) {
2490 * Yes, we do have "servicePrincipalName"s. First we update them
2491 * locally, that means we do always substitute the current
2492 * "dNSHostName" with the new one and/or "sAMAccountName"
2493 * without "$" with the new one and then we append the
2494 * modified "servicePrincipalName"s as a message element
2495 * replace to the modification request (Windows behaviour). We
2496 * need also to make sure that the values remain case-
2497 * insensitively unique.
2500 ret
= ldb_msg_add_empty(ac
->msg
, "servicePrincipalName",
2501 LDB_FLAG_MOD_REPLACE
, &el
);
2502 if (ret
!= LDB_SUCCESS
) {
2506 for (i
= 0; i
< res
->msgs
[0]->elements
[0].num_values
; i
++) {
2507 char *old_str
, *new_str
, *pos
;
2509 struct ldb_val
*vals
;
2513 res
->msgs
[0]->elements
[0].values
[i
].data
;
2515 new_str
= talloc_strdup(ac
->msg
,
2516 strtok_r(old_str
, "/", &pos
));
2517 if (new_str
== NULL
) {
2518 return ldb_module_oom(ac
->module
);
2521 while ((tok
= strtok_r(NULL
, "/", &pos
)) != NULL
) {
2522 if ((dns_hostname
!= NULL
) &&
2523 (strcasecmp_m(tok
, old_dns_hostname
) == 0)) {
2526 if ((sam_accountname
!= NULL
) &&
2527 (strcasecmp_m(tok
, old_sam_accountname
) == 0)) {
2528 tok
= sam_accountname
;
2531 new_str
= talloc_asprintf(ac
->msg
, "%s/%s",
2533 if (new_str
== NULL
) {
2534 return ldb_module_oom(ac
->module
);
2538 /* Uniqueness check */
2539 for (j
= 0; (!found
) && (j
< el
->num_values
); j
++) {
2540 if (strcasecmp_m((char *)el
->values
[j
].data
,
2550 * append the new "servicePrincipalName" -
2551 * code derived from ldb_msg_add_value().
2553 * Open coded to make it clear that we must
2554 * append to the MOD_REPLACE el created above.
2556 vals
= talloc_realloc(ac
->msg
, el
->values
,
2558 el
->num_values
+ 1);
2560 return ldb_module_oom(ac
->module
);
2563 el
->values
[el
->num_values
] = data_blob_string_const(new_str
);
2573 /* This checks the "fSMORoleOwner" attributes */
2574 static int samldb_fsmo_role_owner_check(struct samldb_ctx
*ac
)
2576 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
2577 const char * const no_attrs
[] = { NULL
};
2578 struct ldb_message_element
*el
;
2579 struct ldb_message
*tmp_msg
;
2580 struct ldb_dn
*res_dn
;
2581 struct ldb_result
*res
;
2584 el
= dsdb_get_single_valued_attr(ac
->msg
, "fSMORoleOwner",
2585 ac
->req
->operation
);
2587 /* we are not affected */
2591 /* Create a temporary message for fetching the "fSMORoleOwner" */
2592 tmp_msg
= ldb_msg_new(ac
->msg
);
2593 if (tmp_msg
== NULL
) {
2594 return ldb_module_oom(ac
->module
);
2596 ret
= ldb_msg_add(tmp_msg
, el
, 0);
2597 if (ret
!= LDB_SUCCESS
) {
2600 res_dn
= ldb_msg_find_attr_as_dn(ldb
, ac
, tmp_msg
, "fSMORoleOwner");
2601 talloc_free(tmp_msg
);
2603 if (res_dn
== NULL
) {
2604 ldb_set_errstring(ldb
,
2605 "samldb: 'fSMORoleOwner' attributes have to reference 'nTDSDSA' entries!");
2606 if (ac
->req
->operation
== LDB_ADD
) {
2607 return LDB_ERR_CONSTRAINT_VIOLATION
;
2609 return LDB_ERR_UNWILLING_TO_PERFORM
;
2613 /* Fetched DN has to reference a "nTDSDSA" entry */
2614 ret
= dsdb_module_search(ac
->module
, ac
, &res
, res_dn
, LDB_SCOPE_BASE
,
2616 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
,
2617 ac
->req
, "(objectClass=nTDSDSA)");
2618 if (ret
!= LDB_SUCCESS
) {
2621 if (res
->count
!= 1) {
2622 ldb_set_errstring(ldb
,
2623 "samldb: 'fSMORoleOwner' attributes have to reference 'nTDSDSA' entries!");
2624 return LDB_ERR_UNWILLING_TO_PERFORM
;
2634 static int samldb_add(struct ldb_module
*module
, struct ldb_request
*req
)
2636 struct ldb_context
*ldb
;
2637 struct samldb_ctx
*ac
;
2638 struct ldb_message_element
*el
;
2641 ldb
= ldb_module_get_ctx(module
);
2642 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "samldb_add\n");
2644 /* do not manipulate our control entries */
2645 if (ldb_dn_is_special(req
->op
.add
.message
->dn
)) {
2646 return ldb_next_request(module
, req
);
2649 el
= ldb_msg_find_element(req
->op
.add
.message
, "userParameters");
2650 if (el
!= NULL
&& ldb_req_is_untrusted(req
)) {
2651 const char *reason
= "samldb_add: "
2652 "setting userParameters is not supported over LDAP, "
2653 "see https://bugzilla.samba.org/show_bug.cgi?id=8077";
2654 ldb_debug(ldb
, LDB_DEBUG_WARNING
, "%s", reason
);
2655 return ldb_error(ldb
, LDB_ERR_CONSTRAINT_VIOLATION
, reason
);
2658 ac
= samldb_ctx_init(module
, req
);
2660 return ldb_operr(ldb
);
2663 /* build the new msg */
2664 ac
->msg
= ldb_msg_copy_shallow(ac
, req
->op
.add
.message
);
2665 if (ac
->msg
== NULL
) {
2667 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
2668 "samldb_add: ldb_msg_copy_shallow failed!\n");
2669 return ldb_operr(ldb
);
2672 el
= ldb_msg_find_element(ac
->msg
, "fSMORoleOwner");
2674 ret
= samldb_fsmo_role_owner_check(ac
);
2675 if (ret
!= LDB_SUCCESS
) {
2680 if (samdb_find_attribute(ldb
, ac
->msg
,
2681 "objectclass", "user") != NULL
) {
2682 ac
->type
= SAMLDB_TYPE_USER
;
2684 ret
= samldb_prim_group_trigger(ac
);
2685 if (ret
!= LDB_SUCCESS
) {
2689 ret
= samldb_objectclass_trigger(ac
);
2690 if (ret
!= LDB_SUCCESS
) {
2694 return samldb_fill_object(ac
);
2697 if (samdb_find_attribute(ldb
, ac
->msg
,
2698 "objectclass", "group") != NULL
) {
2699 ac
->type
= SAMLDB_TYPE_GROUP
;
2701 ret
= samldb_objectclass_trigger(ac
);
2702 if (ret
!= LDB_SUCCESS
) {
2706 return samldb_fill_object(ac
);
2709 /* perhaps a foreignSecurityPrincipal? */
2710 if (samdb_find_attribute(ldb
, ac
->msg
,
2712 "foreignSecurityPrincipal") != NULL
) {
2713 return samldb_fill_foreignSecurityPrincipal_object(ac
);
2716 if (samdb_find_attribute(ldb
, ac
->msg
,
2717 "objectclass", "classSchema") != NULL
) {
2718 ret
= samldb_schema_info_update(ac
);
2719 if (ret
!= LDB_SUCCESS
) {
2724 ac
->type
= SAMLDB_TYPE_CLASS
;
2725 return samldb_fill_object(ac
);
2728 if (samdb_find_attribute(ldb
, ac
->msg
,
2729 "objectclass", "attributeSchema") != NULL
) {
2730 ret
= samldb_schema_info_update(ac
);
2731 if (ret
!= LDB_SUCCESS
) {
2736 ac
->type
= SAMLDB_TYPE_ATTRIBUTE
;
2737 return samldb_fill_object(ac
);
2742 /* nothing matched, go on */
2743 return ldb_next_request(module
, req
);
2747 static int samldb_modify(struct ldb_module
*module
, struct ldb_request
*req
)
2749 struct ldb_context
*ldb
;
2750 struct samldb_ctx
*ac
;
2751 struct ldb_message_element
*el
, *el2
;
2752 bool modified
= false;
2755 if (ldb_dn_is_special(req
->op
.mod
.message
->dn
)) {
2756 /* do not manipulate our control entries */
2757 return ldb_next_request(module
, req
);
2760 ldb
= ldb_module_get_ctx(module
);
2762 /* make sure that "objectSid" is not specified */
2763 el
= ldb_msg_find_element(req
->op
.mod
.message
, "objectSid");
2765 if (ldb_request_get_control(req
, LDB_CONTROL_PROVISION_OID
) == NULL
) {
2766 ldb_set_errstring(ldb
,
2767 "samldb: objectSid must not be specified!");
2768 return LDB_ERR_UNWILLING_TO_PERFORM
;
2771 /* make sure that "sAMAccountType" is not specified */
2772 el
= ldb_msg_find_element(req
->op
.mod
.message
, "sAMAccountType");
2774 ldb_set_errstring(ldb
,
2775 "samldb: sAMAccountType must not be specified!");
2776 return LDB_ERR_UNWILLING_TO_PERFORM
;
2778 /* make sure that "isCriticalSystemObject" is not specified */
2779 el
= ldb_msg_find_element(req
->op
.mod
.message
, "isCriticalSystemObject");
2781 if (ldb_request_get_control(req
, LDB_CONTROL_RELAX_OID
) == NULL
) {
2782 ldb_set_errstring(ldb
,
2783 "samldb: isCriticalSystemObject must not be specified!");
2784 return LDB_ERR_UNWILLING_TO_PERFORM
;
2788 /* msDS-IntId is not allowed to be modified
2789 * except when modification comes from replication */
2790 if (ldb_msg_find_element(req
->op
.mod
.message
, "msDS-IntId")) {
2791 if (!ldb_request_get_control(req
,
2792 DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
2793 return LDB_ERR_CONSTRAINT_VIOLATION
;
2797 el
= ldb_msg_find_element(req
->op
.mod
.message
, "userParameters");
2798 if (el
!= NULL
&& ldb_req_is_untrusted(req
)) {
2799 const char *reason
= "samldb: "
2800 "setting userParameters is not supported over LDAP, "
2801 "see https://bugzilla.samba.org/show_bug.cgi?id=8077";
2802 ldb_debug(ldb
, LDB_DEBUG_WARNING
, "%s", reason
);
2803 return ldb_error(ldb
, LDB_ERR_CONSTRAINT_VIOLATION
, reason
);
2806 ac
= samldb_ctx_init(module
, req
);
2808 return ldb_operr(ldb
);
2811 /* build the new msg */
2812 ac
->msg
= ldb_msg_copy_shallow(ac
, req
->op
.mod
.message
);
2813 if (ac
->msg
== NULL
) {
2815 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
2816 "samldb_modify: ldb_msg_copy_shallow failed!\n");
2817 return ldb_operr(ldb
);
2820 el
= ldb_msg_find_element(ac
->msg
, "primaryGroupID");
2822 ret
= samldb_prim_group_trigger(ac
);
2823 if (ret
!= LDB_SUCCESS
) {
2828 el
= ldb_msg_find_element(ac
->msg
, "userAccountControl");
2831 ret
= samldb_user_account_control_change(ac
);
2832 if (ret
!= LDB_SUCCESS
) {
2837 el
= ldb_msg_find_element(ac
->msg
, "lockoutTime");
2840 ret
= samldb_lockout_time(ac
);
2841 if (ret
!= LDB_SUCCESS
) {
2846 el
= ldb_msg_find_element(ac
->msg
, "groupType");
2849 ret
= samldb_group_type_change(ac
);
2850 if (ret
!= LDB_SUCCESS
) {
2855 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountName");
2857 ret
= samldb_sam_accountname_check(ac
);
2858 if (ret
!= LDB_SUCCESS
) {
2863 el
= ldb_msg_find_element(ac
->msg
, "member");
2865 ret
= samldb_member_check(ac
);
2866 if (ret
!= LDB_SUCCESS
) {
2871 el
= ldb_msg_find_element(ac
->msg
, "description");
2873 ret
= samldb_description_check(ac
, &modified
);
2874 if (ret
!= LDB_SUCCESS
) {
2879 el
= ldb_msg_find_element(ac
->msg
, "dNSHostName");
2880 el2
= ldb_msg_find_element(ac
->msg
, "sAMAccountName");
2881 if ((el
!= NULL
) || (el2
!= NULL
)) {
2883 ret
= samldb_service_principal_names_change(ac
);
2884 if (ret
!= LDB_SUCCESS
) {
2889 el
= ldb_msg_find_element(ac
->msg
, "fSMORoleOwner");
2891 ret
= samldb_fsmo_role_owner_check(ac
);
2892 if (ret
!= LDB_SUCCESS
) {
2898 struct ldb_request
*child_req
;
2900 /* Now perform the real modifications as a child request */
2901 ret
= ldb_build_mod_req(&child_req
, ldb
, ac
,
2904 req
, dsdb_next_callback
,
2906 LDB_REQ_SET_LOCATION(child_req
);
2907 if (ret
!= LDB_SUCCESS
) {
2911 return ldb_next_request(module
, child_req
);
2916 /* no change which interests us, go on */
2917 return ldb_next_request(module
, req
);
2922 static int samldb_prim_group_users_check(struct samldb_ctx
*ac
)
2924 struct ldb_context
*ldb
;
2925 struct dom_sid
*sid
;
2929 struct ldb_result
*res
;
2930 const char * const attrs
[] = { "objectSid", "isDeleted", NULL
};
2931 const char * const noattrs
[] = { NULL
};
2933 ldb
= ldb_module_get_ctx(ac
->module
);
2935 /* Finds out the SID/RID of the SAM object */
2936 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->req
->op
.del
.dn
,
2938 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
,
2940 if (ret
!= LDB_SUCCESS
) {
2944 if (ldb_msg_check_string_attribute(res
->msgs
[0], "isDeleted", "TRUE")) {
2948 sid
= samdb_result_dom_sid(ac
, res
->msgs
[0], "objectSid");
2950 /* No SID - it might not be a SAM object - therefore ok */
2953 status
= dom_sid_split_rid(ac
, sid
, NULL
, &rid
);
2954 if (!NT_STATUS_IS_OK(status
)) {
2955 return ldb_operr(ldb
);
2958 /* Special object (security principal?) */
2961 /* do not allow deletion of well-known sids */
2962 if (rid
< DSDB_SAMDB_MINIMUM_ALLOWED_RID
&&
2963 (ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
) == NULL
)) {
2964 return LDB_ERR_OTHER
;
2967 /* Deny delete requests from groups which are primary ones */
2968 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
2969 ldb_get_default_basedn(ldb
),
2970 LDB_SCOPE_SUBTREE
, noattrs
,
2971 DSDB_FLAG_NEXT_MODULE
,
2973 "(&(primaryGroupID=%u)(objectClass=user))", rid
);
2974 if (ret
!= LDB_SUCCESS
) {
2977 if (res
->count
> 0) {
2978 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
2984 static int samldb_delete(struct ldb_module
*module
, struct ldb_request
*req
)
2986 struct samldb_ctx
*ac
;
2989 if (ldb_dn_is_special(req
->op
.del
.dn
)) {
2990 /* do not manipulate our control entries */
2991 return ldb_next_request(module
, req
);
2994 ac
= samldb_ctx_init(module
, req
);
2996 return ldb_operr(ldb_module_get_ctx(module
));
2999 ret
= samldb_prim_group_users_check(ac
);
3000 if (ret
!= LDB_SUCCESS
) {
3006 return ldb_next_request(module
, req
);
3011 static int check_rename_constraints(struct ldb_message
*msg
,
3012 struct samldb_ctx
*ac
,
3013 struct ldb_dn
*olddn
, struct ldb_dn
*newdn
)
3015 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
3016 struct ldb_dn
*dn1
, *dn2
, *nc_root
;
3017 int32_t systemFlags
;
3018 bool move_op
= false;
3019 bool rename_op
= false;
3022 /* Skip the checks if old and new DN are the same, or if we have the
3023 * relax control specified or if the returned objects is already
3024 * deleted and needs only to be moved for consistency. */
3026 if (ldb_dn_compare(olddn
, newdn
) == 0) {
3029 if (ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
) != NULL
) {
3032 if (ldb_msg_find_attr_as_bool(msg
, "isDeleted", false)) {
3036 /* Objects under CN=System */
3038 dn1
= ldb_dn_copy(ac
, ldb_get_default_basedn(ldb
));
3039 if (dn1
== NULL
) return ldb_oom(ldb
);
3041 if ( ! ldb_dn_add_child_fmt(dn1
, "CN=System")) {
3043 return LDB_ERR_OPERATIONS_ERROR
;
3046 if ((ldb_dn_compare_base(dn1
, olddn
) == 0) &&
3047 (ldb_dn_compare_base(dn1
, newdn
) != 0)) {
3049 ldb_asprintf_errstring(ldb
,
3050 "subtree_rename: Cannot move/rename %s. Objects under CN=System have to stay under it!",
3051 ldb_dn_get_linearized(olddn
));
3052 return LDB_ERR_OTHER
;
3059 if ((samdb_find_attribute(ldb
, msg
, "objectClass", "secret") != NULL
) ||
3060 (samdb_find_attribute(ldb
, msg
, "objectClass", "trustedDomain") != NULL
)) {
3061 ldb_asprintf_errstring(ldb
,
3062 "subtree_rename: Cannot move/rename %s. It's an LSA-specific object!",
3063 ldb_dn_get_linearized(olddn
));
3064 return LDB_ERR_UNWILLING_TO_PERFORM
;
3069 dn1
= ldb_dn_get_parent(ac
, olddn
);
3070 if (dn1
== NULL
) return ldb_oom(ldb
);
3071 dn2
= ldb_dn_get_parent(ac
, newdn
);
3072 if (dn2
== NULL
) return ldb_oom(ldb
);
3074 if (ldb_dn_compare(dn1
, dn2
) == 0) {
3083 systemFlags
= ldb_msg_find_attr_as_int(msg
, "systemFlags", 0);
3085 /* Fetch name context */
3087 ret
= dsdb_find_nc_root(ldb
, ac
, olddn
, &nc_root
);
3088 if (ret
!= LDB_SUCCESS
) {
3092 if (ldb_dn_compare(nc_root
, ldb_get_schema_basedn(ldb
)) == 0) {
3094 ldb_asprintf_errstring(ldb
,
3095 "subtree_rename: Cannot move %s within schema partition",
3096 ldb_dn_get_linearized(olddn
));
3097 return LDB_ERR_UNWILLING_TO_PERFORM
;
3100 (systemFlags
& SYSTEM_FLAG_SCHEMA_BASE_OBJECT
) != 0) {
3101 ldb_asprintf_errstring(ldb
,
3102 "subtree_rename: Cannot rename %s within schema partition",
3103 ldb_dn_get_linearized(olddn
));
3104 return LDB_ERR_UNWILLING_TO_PERFORM
;
3106 } else if (ldb_dn_compare(nc_root
, ldb_get_config_basedn(ldb
)) == 0) {
3108 (systemFlags
& SYSTEM_FLAG_CONFIG_ALLOW_MOVE
) == 0) {
3109 /* Here we have to do more: control the
3110 * "ALLOW_LIMITED_MOVE" flag. This means that the
3111 * grand-grand-parents of two objects have to be equal
3112 * in order to perform the move (this is used for
3113 * moving "server" objects in the "sites" container). */
3115 systemFlags
& SYSTEM_FLAG_CONFIG_ALLOW_LIMITED_MOVE
;
3118 dn1
= ldb_dn_copy(ac
, olddn
);
3119 if (dn1
== NULL
) return ldb_oom(ldb
);
3120 dn2
= ldb_dn_copy(ac
, newdn
);
3121 if (dn2
== NULL
) return ldb_oom(ldb
);
3123 limited_move
&= ldb_dn_remove_child_components(dn1
, 3);
3124 limited_move
&= ldb_dn_remove_child_components(dn2
, 3);
3125 limited_move
&= ldb_dn_compare(dn1
, dn2
) == 0;
3131 if (!limited_move
) {
3132 ldb_asprintf_errstring(ldb
,
3133 "subtree_rename: Cannot move %s to %s in config partition",
3134 ldb_dn_get_linearized(olddn
), ldb_dn_get_linearized(newdn
));
3135 return LDB_ERR_UNWILLING_TO_PERFORM
;
3139 (systemFlags
& SYSTEM_FLAG_CONFIG_ALLOW_RENAME
) == 0) {
3140 ldb_asprintf_errstring(ldb
,
3141 "subtree_rename: Cannot rename %s to %s within config partition",
3142 ldb_dn_get_linearized(olddn
), ldb_dn_get_linearized(newdn
));
3143 return LDB_ERR_UNWILLING_TO_PERFORM
;
3145 } else if (ldb_dn_compare(nc_root
, ldb_get_default_basedn(ldb
)) == 0) {
3147 (systemFlags
& SYSTEM_FLAG_DOMAIN_DISALLOW_MOVE
) != 0) {
3148 ldb_asprintf_errstring(ldb
,
3149 "subtree_rename: Cannot move %s to %s - DISALLOW_MOVE set",
3150 ldb_dn_get_linearized(olddn
), ldb_dn_get_linearized(newdn
));
3151 return LDB_ERR_UNWILLING_TO_PERFORM
;
3154 (systemFlags
& SYSTEM_FLAG_DOMAIN_DISALLOW_RENAME
) != 0) {
3155 ldb_asprintf_errstring(ldb
,
3156 "subtree_rename: Cannot rename %s to %s - DISALLOW_RENAME set",
3157 ldb_dn_get_linearized(olddn
), ldb_dn_get_linearized(newdn
));
3158 return LDB_ERR_UNWILLING_TO_PERFORM
;
3162 talloc_free(nc_root
);
3168 static int samldb_rename_search_base_callback(struct ldb_request
*req
,
3169 struct ldb_reply
*ares
)
3171 struct samldb_ctx
*ac
;
3174 ac
= talloc_get_type(req
->context
, struct samldb_ctx
);
3177 return ldb_module_done(ac
->req
, NULL
, NULL
,
3178 LDB_ERR_OPERATIONS_ERROR
);
3180 if (ares
->error
!= LDB_SUCCESS
) {
3181 return ldb_module_done(ac
->req
, ares
->controls
,
3182 ares
->response
, ares
->error
);
3185 switch (ares
->type
) {
3186 case LDB_REPLY_ENTRY
:
3188 * This is the root entry of the originating move
3189 * respectively rename request. It has been already
3190 * stored in the list using "subtree_rename_search()".
3191 * Only this one is subject to constraint checking.
3193 ret
= check_rename_constraints(ares
->message
, ac
,
3194 ac
->req
->op
.rename
.olddn
,
3195 ac
->req
->op
.rename
.newdn
);
3196 if (ret
!= LDB_SUCCESS
) {
3197 return ldb_module_done(ac
->req
, NULL
, NULL
,
3202 case LDB_REPLY_REFERRAL
:
3206 case LDB_REPLY_DONE
:
3209 * Great, no problem with the rename, so go ahead as
3210 * if we never were here
3212 ret
= ldb_next_request(ac
->module
, ac
->req
);
3223 static int samldb_rename(struct ldb_module
*module
, struct ldb_request
*req
)
3225 struct ldb_context
*ldb
;
3226 static const char * const attrs
[] = { "objectClass", "systemFlags",
3227 "isDeleted", NULL
};
3228 struct ldb_request
*search_req
;
3229 struct samldb_ctx
*ac
;
3232 if (ldb_dn_is_special(req
->op
.rename
.olddn
)) { /* do not manipulate our control entries */
3233 return ldb_next_request(module
, req
);
3236 ldb
= ldb_module_get_ctx(module
);
3238 ac
= samldb_ctx_init(module
, req
);
3240 return ldb_oom(ldb
);
3243 ret
= ldb_build_search_req(&search_req
, ldb
, ac
,
3244 req
->op
.rename
.olddn
,
3250 samldb_rename_search_base_callback
,
3252 LDB_REQ_SET_LOCATION(search_req
);
3253 if (ret
!= LDB_SUCCESS
) {
3257 ret
= ldb_request_add_control(search_req
, LDB_CONTROL_SHOW_RECYCLED_OID
,
3259 if (ret
!= LDB_SUCCESS
) {
3263 return ldb_next_request(ac
->module
, search_req
);
3268 static int samldb_extended_allocate_rid_pool(struct ldb_module
*module
, struct ldb_request
*req
)
3270 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
3271 struct dsdb_fsmo_extended_op
*exop
;
3274 exop
= talloc_get_type(req
->op
.extended
.data
,
3275 struct dsdb_fsmo_extended_op
);
3277 ldb_set_errstring(ldb
,
3278 "samldb_extended_allocate_rid_pool: invalid extended data");
3279 return LDB_ERR_PROTOCOL_ERROR
;
3282 ret
= ridalloc_allocate_rid_pool_fsmo(module
, exop
, req
);
3283 if (ret
!= LDB_SUCCESS
) {
3287 return ldb_module_done(req
, NULL
, NULL
, LDB_SUCCESS
);
3290 static int samldb_extended(struct ldb_module
*module
, struct ldb_request
*req
)
3292 if (strcmp(req
->op
.extended
.oid
, DSDB_EXTENDED_ALLOCATE_RID_POOL
) == 0) {
3293 return samldb_extended_allocate_rid_pool(module
, req
);
3296 return ldb_next_request(module
, req
);
3300 static const struct ldb_module_ops ldb_samldb_module_ops
= {
3303 .modify
= samldb_modify
,
3304 .del
= samldb_delete
,
3305 .rename
= samldb_rename
,
3306 .extended
= samldb_extended
3310 int ldb_samldb_module_init(const char *version
)
3312 LDB_MODULE_CHECK_VERSION(version
);
3313 return ldb_register_module(&ldb_samldb_module_ops
);