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
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * Component: ldb samldb module
27 * Description: various internal DSDB triggers - most for SAM specific objects
33 #include "libcli/ldap/ldap_ndr.h"
34 #include "ldb_module.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "dsdb/samdb/ldb_modules/util.h"
37 #include "dsdb/samdb/ldb_modules/ridalloc.h"
38 #include "libcli/security/security.h"
39 #include "librpc/gen_ndr/ndr_security.h"
41 #include "param/param.h"
42 #include "libds/common/flag_mapping.h"
45 enum samldb_add_type
{
52 typedef int (*samldb_step_fn_t
)(struct samldb_ctx
*);
55 struct samldb_step
*next
;
60 struct ldb_module
*module
;
61 struct ldb_request
*req
;
63 /* used for add operations */
64 enum samldb_add_type type
;
66 /* the resulting message */
67 struct ldb_message
*msg
;
69 /* used in "samldb_find_for_defaultObjectCategory" */
70 struct ldb_dn
*dn
, *res_dn
;
72 /* all the async steps necessary to complete the operation */
73 struct samldb_step
*steps
;
74 struct samldb_step
*curstep
;
76 /* If someone set an ares to forward controls and response back to the caller */
77 struct ldb_reply
*ares
;
80 static struct samldb_ctx
*samldb_ctx_init(struct ldb_module
*module
,
81 struct ldb_request
*req
)
83 struct ldb_context
*ldb
;
84 struct samldb_ctx
*ac
;
86 ldb
= ldb_module_get_ctx(module
);
88 ac
= talloc_zero(req
, struct samldb_ctx
);
100 static int samldb_add_step(struct samldb_ctx
*ac
, samldb_step_fn_t fn
)
102 struct samldb_step
*step
, *stepper
;
104 step
= talloc_zero(ac
, struct samldb_step
);
106 return ldb_oom(ldb_module_get_ctx(ac
->module
));
111 if (ac
->steps
== NULL
) {
115 if (ac
->curstep
== NULL
)
116 return ldb_operr(ldb_module_get_ctx(ac
->module
));
117 for (stepper
= ac
->curstep
; stepper
->next
!= NULL
;
118 stepper
= stepper
->next
);
119 stepper
->next
= step
;
125 static int samldb_first_step(struct samldb_ctx
*ac
)
127 if (ac
->steps
== NULL
) {
128 return ldb_operr(ldb_module_get_ctx(ac
->module
));
131 ac
->curstep
= ac
->steps
;
132 return ac
->curstep
->fn(ac
);
135 static int samldb_next_step(struct samldb_ctx
*ac
)
137 if (ac
->curstep
->next
) {
138 ac
->curstep
= ac
->curstep
->next
;
139 return ac
->curstep
->fn(ac
);
142 /* We exit the samldb module here. If someone set an "ares" to forward
143 * controls and response back to the caller, use them. */
145 return ldb_module_done(ac
->req
, ac
->ares
->controls
,
146 ac
->ares
->response
, LDB_SUCCESS
);
148 return ldb_module_done(ac
->req
, NULL
, NULL
, LDB_SUCCESS
);
153 /* sAMAccountName handling */
155 static int samldb_generate_sAMAccountName(struct ldb_context
*ldb
,
156 struct ldb_message
*msg
)
160 /* Format: $000000-000000000000 */
162 name
= talloc_asprintf(msg
, "$%.6X-%.6X%.6X",
163 (unsigned int)generate_random(),
164 (unsigned int)generate_random(),
165 (unsigned int)generate_random());
169 return ldb_msg_add_steal_string(msg
, "sAMAccountName", name
);
172 static int samldb_check_sAMAccountName(struct samldb_ctx
*ac
)
174 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
177 struct ldb_result
*res
;
178 const char * const noattrs
[] = { NULL
};
180 if (ldb_msg_find_element(ac
->msg
, "sAMAccountName") == NULL
) {
181 ret
= samldb_generate_sAMAccountName(ldb
, ac
->msg
);
182 if (ret
!= LDB_SUCCESS
) {
187 name
= ldb_msg_find_attr_as_string(ac
->msg
, "sAMAccountName", NULL
);
189 /* The "sAMAccountName" cannot be nothing */
190 ldb_set_errstring(ldb
,
191 "samldb: Empty account names aren't allowed!");
192 return LDB_ERR_CONSTRAINT_VIOLATION
;
195 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
196 ldb_get_default_basedn(ldb
), LDB_SCOPE_SUBTREE
, noattrs
,
197 DSDB_FLAG_NEXT_MODULE
,
199 "(sAMAccountName=%s)",
200 ldb_binary_encode_string(ac
, name
));
201 if (ret
!= LDB_SUCCESS
) {
204 if (res
->count
!= 0) {
205 ldb_asprintf_errstring(ldb
,
206 "samldb: Account name (sAMAccountName) '%s' already in use!",
209 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
213 return samldb_next_step(ac
);
217 static bool samldb_msg_add_sid(struct ldb_message
*msg
,
219 const struct dom_sid
*sid
)
222 enum ndr_err_code ndr_err
;
224 ndr_err
= ndr_push_struct_blob(&v
, msg
, sid
,
225 (ndr_push_flags_fn_t
)ndr_push_dom_sid
);
226 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
229 return (ldb_msg_add_value(msg
, name
, &v
, NULL
) == 0);
233 /* allocate a SID using our RID Set */
234 static int samldb_allocate_sid(struct samldb_ctx
*ac
)
238 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
241 ret
= ridalloc_allocate_rid(ac
->module
, &rid
, ac
->req
);
242 if (ret
!= LDB_SUCCESS
) {
246 sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), rid
);
248 return ldb_module_oom(ac
->module
);
251 if ( ! samldb_msg_add_sid(ac
->msg
, "objectSid", sid
)) {
252 return ldb_operr(ldb
);
255 return samldb_next_step(ac
);
259 see if a krbtgt_number is available
261 static bool samldb_krbtgtnumber_available(struct samldb_ctx
*ac
,
262 uint32_t krbtgt_number
)
264 TALLOC_CTX
*tmp_ctx
= talloc_new(ac
);
265 struct ldb_result
*res
;
266 const char * const no_attrs
[] = { NULL
};
269 ret
= dsdb_module_search(ac
->module
, tmp_ctx
, &res
,
270 ldb_get_default_basedn(ldb_module_get_ctx(ac
->module
)),
271 LDB_SCOPE_SUBTREE
, no_attrs
,
272 DSDB_FLAG_NEXT_MODULE
,
274 "(msDC-SecondaryKrbTgtNumber=%u)",
276 if (ret
== LDB_SUCCESS
&& res
->count
== 0) {
277 talloc_free(tmp_ctx
);
280 talloc_free(tmp_ctx
);
284 /* special handling for add in RODC join */
285 static int samldb_rodc_add(struct samldb_ctx
*ac
)
287 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
288 uint32_t krbtgt_number
, i_start
, i
;
291 struct ldb_val newpass_utf16
;
293 /* find a unused msDC-SecondaryKrbTgtNumber */
294 i_start
= generate_random() & 0xFFFF;
299 for (i
=i_start
; i
<=0xFFFF; i
++) {
300 if (samldb_krbtgtnumber_available(ac
, i
)) {
305 for (i
=1; i
<i_start
; i
++) {
306 if (samldb_krbtgtnumber_available(ac
, i
)) {
312 ldb_asprintf_errstring(ldb
,
313 "%08X: Unable to find available msDS-SecondaryKrbTgtNumber",
314 W_ERROR_V(WERR_NO_SYSTEM_RESOURCES
));
315 return LDB_ERR_OTHER
;
318 ret
= ldb_msg_add_empty(ac
->msg
, "msDS-SecondaryKrbTgtNumber",
319 LDB_FLAG_INTERNAL_DISABLE_VALIDATION
, NULL
);
320 if (ret
!= LDB_SUCCESS
) {
321 return ldb_operr(ldb
);
324 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
325 "msDS-SecondaryKrbTgtNumber", krbtgt_number
);
326 if (ret
!= LDB_SUCCESS
) {
327 return ldb_operr(ldb
);
330 ret
= ldb_msg_add_fmt(ac
->msg
, "sAMAccountName", "krbtgt_%u",
332 if (ret
!= LDB_SUCCESS
) {
333 return ldb_operr(ldb
);
336 newpass
= generate_random_password(ac
->msg
, 128, 255);
337 if (newpass
== NULL
) {
338 return ldb_operr(ldb
);
341 if (!convert_string_talloc(ac
,
343 newpass
, strlen(newpass
),
344 (void *)&newpass_utf16
.data
,
345 &newpass_utf16
.length
)) {
346 ldb_asprintf_errstring(ldb
,
348 "failed to generate UTF16 password from random password");
349 return LDB_ERR_OPERATIONS_ERROR
;
351 ret
= ldb_msg_add_steal_value(ac
->msg
, "clearTextPassword", &newpass_utf16
);
352 if (ret
!= LDB_SUCCESS
) {
353 return ldb_operr(ldb
);
356 return samldb_next_step(ac
);
359 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx
*ac
)
361 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
362 struct ldb_result
*res
;
363 const char * const no_attrs
[] = { NULL
};
368 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
369 ac
->dn
, LDB_SCOPE_BASE
, no_attrs
,
370 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
371 | DSDB_FLAG_NEXT_MODULE
,
373 "(objectClass=classSchema)");
374 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
375 /* Don't be pricky when the DN doesn't exist if we have the */
376 /* RELAX control specified */
377 if (ldb_request_get_control(ac
->req
,
378 LDB_CONTROL_RELAX_OID
) == NULL
) {
379 ldb_set_errstring(ldb
,
380 "samldb_find_defaultObjectCategory: "
381 "Invalid DN for 'defaultObjectCategory'!");
382 return LDB_ERR_CONSTRAINT_VIOLATION
;
385 if ((ret
!= LDB_ERR_NO_SUCH_OBJECT
) && (ret
!= LDB_SUCCESS
)) {
389 if (ret
== LDB_SUCCESS
) {
390 /* ensure the defaultObjectCategory has a full GUID */
391 struct ldb_message
*m
;
392 m
= ldb_msg_new(ac
->msg
);
397 if (ldb_msg_add_string(m
, "defaultObjectCategory",
398 ldb_dn_get_extended_linearized(m
, res
->msgs
[0]->dn
, 1)) !=
402 m
->elements
[0].flags
= LDB_FLAG_MOD_REPLACE
;
404 ret
= dsdb_module_modify(ac
->module
, m
,
405 DSDB_FLAG_NEXT_MODULE
,
407 if (ret
!= LDB_SUCCESS
) {
415 return samldb_next_step(ac
);
419 * msDS-IntId attributeSchema attribute handling
420 * during LDB_ADD request processing
422 static int samldb_add_handle_msDS_IntId(struct samldb_ctx
*ac
)
427 int32_t system_flags
;
428 struct ldb_context
*ldb
;
429 struct ldb_result
*ldb_res
;
430 struct ldb_dn
*schema_dn
;
432 ldb
= ldb_module_get_ctx(ac
->module
);
433 schema_dn
= ldb_get_schema_basedn(ldb
);
435 /* replicated update should always go through */
436 if (ldb_request_get_control(ac
->req
,
437 DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
441 /* msDS-IntId is handled by system and should never be
442 * passed by clients */
443 if (ldb_msg_find_element(ac
->msg
, "msDS-IntId")) {
444 return LDB_ERR_UNWILLING_TO_PERFORM
;
447 /* do not generate msDS-IntId if Relax control is passed */
448 if (ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
)) {
452 /* check Functional Level */
453 if (dsdb_functional_level(ldb
) < DS_DOMAIN_FUNCTION_2003
) {
457 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
458 system_flags
= ldb_msg_find_attr_as_int(ac
->msg
, "systemFlags", 0);
459 if (system_flags
& SYSTEM_FLAG_SCHEMA_BASE_OBJECT
) {
463 /* Generate new value for msDs-IntId
464 * Value should be in 0x80000000..0xBFFFFFFF range */
465 msds_intid
= generate_random() % 0X3FFFFFFF;
466 msds_intid
+= 0x80000000;
468 /* probe id values until unique one is found */
471 if (msds_intid
> 0xBFFFFFFF) {
472 msds_intid
= 0x80000001;
475 ret
= dsdb_module_search(ac
->module
, ac
,
477 schema_dn
, LDB_SCOPE_ONELEVEL
, NULL
,
478 DSDB_FLAG_NEXT_MODULE
,
480 "(msDS-IntId=%d)", msds_intid
);
481 if (ret
!= LDB_SUCCESS
) {
482 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
483 __location__
": Searching for msDS-IntId=%d failed - %s\n",
486 return ldb_operr(ldb
);
488 id_exists
= (ldb_res
->count
> 0);
490 talloc_free(ldb_res
);
493 return samdb_msg_add_int(ldb
, ac
->msg
, ac
->msg
, "msDS-IntId",
499 * samldb_add_entry (async)
502 static int samldb_add_entry_callback(struct ldb_request
*req
,
503 struct ldb_reply
*ares
)
505 struct ldb_context
*ldb
;
506 struct samldb_ctx
*ac
;
509 ac
= talloc_get_type(req
->context
, struct samldb_ctx
);
510 ldb
= ldb_module_get_ctx(ac
->module
);
513 return ldb_module_done(ac
->req
, NULL
, NULL
,
514 LDB_ERR_OPERATIONS_ERROR
);
517 if (ares
->type
== LDB_REPLY_REFERRAL
) {
518 return ldb_module_send_referral(ac
->req
, ares
->referral
);
521 if (ares
->error
!= LDB_SUCCESS
) {
522 return ldb_module_done(ac
->req
, ares
->controls
,
523 ares
->response
, ares
->error
);
525 if (ares
->type
!= LDB_REPLY_DONE
) {
526 ldb_asprintf_errstring(ldb
, "Invalid LDB reply type %d", ares
->type
);
527 return ldb_module_done(ac
->req
, NULL
, NULL
,
528 LDB_ERR_OPERATIONS_ERROR
);
531 /* The caller may wish to get controls back from the add */
532 ac
->ares
= talloc_steal(ac
, ares
);
534 ret
= samldb_next_step(ac
);
535 if (ret
!= LDB_SUCCESS
) {
536 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
541 static int samldb_add_entry(struct samldb_ctx
*ac
)
543 struct ldb_context
*ldb
;
544 struct ldb_request
*req
;
547 ldb
= ldb_module_get_ctx(ac
->module
);
549 ret
= ldb_build_add_req(&req
, ldb
, ac
,
552 ac
, samldb_add_entry_callback
,
554 LDB_REQ_SET_LOCATION(req
);
555 if (ret
!= LDB_SUCCESS
) {
559 return ldb_next_request(ac
->module
, req
);
563 * return true if msg carries an attributeSchema that is intended to be RODC
564 * filtered but is also a system-critical attribute.
566 static bool check_rodc_critical_attribute(struct ldb_message
*msg
)
568 uint32_t schemaFlagsEx
, searchFlags
, rodc_filtered_flags
;
570 schemaFlagsEx
= ldb_msg_find_attr_as_uint(msg
, "schemaFlagsEx", 0);
571 searchFlags
= ldb_msg_find_attr_as_uint(msg
, "searchFlags", 0);
572 rodc_filtered_flags
= (SEARCH_FLAG_RODC_ATTRIBUTE
573 | SEARCH_FLAG_CONFIDENTIAL
);
575 if ((schemaFlagsEx
& SCHEMA_FLAG_ATTR_IS_CRITICAL
) &&
576 ((searchFlags
& rodc_filtered_flags
) == rodc_filtered_flags
)) {
584 static int samldb_fill_object(struct samldb_ctx
*ac
)
586 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
589 /* Add information for the different account types */
591 case SAMLDB_TYPE_USER
: {
592 struct ldb_control
*rodc_control
= ldb_request_get_control(ac
->req
,
593 LDB_CONTROL_RODC_DCPROMO_OID
);
594 if (rodc_control
!= NULL
) {
595 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
596 rodc_control
->critical
= false;
597 ret
= samldb_add_step(ac
, samldb_rodc_add
);
598 if (ret
!= LDB_SUCCESS
) return ret
;
601 /* check if we have a valid sAMAccountName */
602 ret
= samldb_add_step(ac
, samldb_check_sAMAccountName
);
603 if (ret
!= LDB_SUCCESS
) return ret
;
605 ret
= samldb_add_step(ac
, samldb_add_entry
);
606 if (ret
!= LDB_SUCCESS
) return ret
;
610 case SAMLDB_TYPE_GROUP
: {
611 /* check if we have a valid sAMAccountName */
612 ret
= samldb_add_step(ac
, samldb_check_sAMAccountName
);
613 if (ret
!= LDB_SUCCESS
) return ret
;
615 ret
= samldb_add_step(ac
, samldb_add_entry
);
616 if (ret
!= LDB_SUCCESS
) return ret
;
620 case SAMLDB_TYPE_CLASS
: {
621 const struct ldb_val
*rdn_value
, *def_obj_cat_val
;
622 unsigned int v
= ldb_msg_find_attr_as_uint(ac
->msg
, "objectClassCategory", -2);
624 /* As discussed with Microsoft through dochelp in April 2012 this is the behavior of windows*/
625 if (!ldb_msg_find_element(ac
->msg
, "subClassOf")) {
626 ret
= ldb_msg_add_string(ac
->msg
, "subClassOf", "top");
627 if (ret
!= LDB_SUCCESS
) return ret
;
630 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
632 if (ret
!= LDB_SUCCESS
) return ret
;
634 /* do not allow to mark an attributeSchema as RODC filtered if it
635 * is system-critical */
636 if (check_rodc_critical_attribute(ac
->msg
)) {
637 ldb_asprintf_errstring(ldb
, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
638 ldb_dn_get_linearized(ac
->msg
->dn
));
639 return LDB_ERR_UNWILLING_TO_PERFORM
;
642 rdn_value
= ldb_dn_get_rdn_val(ac
->msg
->dn
);
643 if (rdn_value
== NULL
) {
644 return ldb_operr(ldb
);
646 if (!ldb_msg_find_element(ac
->msg
, "lDAPDisplayName")) {
647 /* the RDN has prefix "CN" */
648 ret
= ldb_msg_add_string(ac
->msg
, "lDAPDisplayName",
649 samdb_cn_to_lDAPDisplayName(ac
->msg
,
650 (const char *) rdn_value
->data
));
651 if (ret
!= LDB_SUCCESS
) {
657 if (!ldb_msg_find_element(ac
->msg
, "schemaIDGUID")) {
660 guid
= GUID_random();
661 ret
= dsdb_msg_add_guid(ac
->msg
, &guid
, "schemaIDGUID");
662 if (ret
!= LDB_SUCCESS
) {
668 def_obj_cat_val
= ldb_msg_find_ldb_val(ac
->msg
,
669 "defaultObjectCategory");
670 if (def_obj_cat_val
!= NULL
) {
671 /* "defaultObjectCategory" has been set by the caller.
672 * Do some checks for consistency.
673 * NOTE: The real constraint check (that
674 * 'defaultObjectCategory' is the DN of the new
675 * objectclass or any parent of it) is still incomplete.
676 * For now we say that 'defaultObjectCategory' is valid
677 * if it exists and it is of objectclass "classSchema".
679 ac
->dn
= ldb_dn_from_ldb_val(ac
, ldb
, def_obj_cat_val
);
680 if (ac
->dn
== NULL
) {
681 ldb_set_errstring(ldb
,
682 "Invalid DN for 'defaultObjectCategory'!");
683 return LDB_ERR_CONSTRAINT_VIOLATION
;
686 /* "defaultObjectCategory" has not been set by the
687 * caller. Use the entry DN for it. */
688 ac
->dn
= ac
->msg
->dn
;
690 ret
= ldb_msg_add_string(ac
->msg
, "defaultObjectCategory",
691 ldb_dn_alloc_linearized(ac
->msg
, ac
->dn
));
692 if (ret
!= LDB_SUCCESS
) {
698 ret
= samldb_add_step(ac
, samldb_add_entry
);
699 if (ret
!= LDB_SUCCESS
) return ret
;
701 /* Now perform the checks for the 'defaultObjectCategory'. The
702 * lookup DN was already saved in "ac->dn" */
703 ret
= samldb_add_step(ac
, samldb_find_for_defaultObjectCategory
);
704 if (ret
!= LDB_SUCCESS
) return ret
;
706 /* -2 is not a valid objectClassCategory so it means the attribute wasn't present */
708 /* Windows 2003 does this*/
709 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
, "objectClassCategory", 0);
710 if (ret
!= LDB_SUCCESS
) {
717 case SAMLDB_TYPE_ATTRIBUTE
: {
718 const struct ldb_val
*rdn_value
;
719 struct ldb_message_element
*el
;
720 rdn_value
= ldb_dn_get_rdn_val(ac
->msg
->dn
);
721 if (rdn_value
== NULL
) {
722 return ldb_operr(ldb
);
724 if (!ldb_msg_find_element(ac
->msg
, "lDAPDisplayName")) {
725 /* the RDN has prefix "CN" */
726 ret
= ldb_msg_add_string(ac
->msg
, "lDAPDisplayName",
727 samdb_cn_to_lDAPDisplayName(ac
->msg
,
728 (const char *) rdn_value
->data
));
729 if (ret
!= LDB_SUCCESS
) {
735 /* do not allow to mark an attributeSchema as RODC filtered if it
736 * is system-critical */
737 if (check_rodc_critical_attribute(ac
->msg
)) {
738 ldb_asprintf_errstring(ldb
,
739 "samldb: refusing schema add of %s - cannot combine critical attribute with RODC filtering",
740 ldb_dn_get_linearized(ac
->msg
->dn
));
741 return LDB_ERR_UNWILLING_TO_PERFORM
;
744 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
745 "isSingleValued", "FALSE");
746 if (ret
!= LDB_SUCCESS
) return ret
;
748 if (!ldb_msg_find_element(ac
->msg
, "schemaIDGUID")) {
751 guid
= GUID_random();
752 ret
= dsdb_msg_add_guid(ac
->msg
, &guid
, "schemaIDGUID");
753 if (ret
!= LDB_SUCCESS
) {
759 el
= ldb_msg_find_element(ac
->msg
, "attributeSyntax");
762 * No need to scream if there isn't as we have code later on
763 * that will take care of it.
765 const struct dsdb_syntax
*syntax
= find_syntax_map_by_ad_oid((const char *)el
->values
[0].data
);
767 DEBUG(9, ("Can't find dsdb_syntax object for attributeSyntax %s\n",
768 (const char *)el
->values
[0].data
));
770 unsigned int v
= ldb_msg_find_attr_as_uint(ac
->msg
, "oMSyntax", 0);
771 const struct ldb_val
*val
= ldb_msg_find_ldb_val(ac
->msg
, "oMObjectClass");
774 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
, "oMSyntax", syntax
->oMSyntax
);
775 if (ret
!= LDB_SUCCESS
) {
780 struct ldb_val val2
= ldb_val_dup(ldb
, &syntax
->oMObjectClass
);
781 if (val2
.length
> 0) {
782 ret
= ldb_msg_add_value(ac
->msg
, "oMObjectClass", &val2
, NULL
);
783 if (ret
!= LDB_SUCCESS
) {
791 /* handle msDS-IntID attribute */
792 ret
= samldb_add_handle_msDS_IntId(ac
);
793 if (ret
!= LDB_SUCCESS
) return ret
;
795 ret
= samldb_add_step(ac
, samldb_add_entry
);
796 if (ret
!= LDB_SUCCESS
) return ret
;
801 ldb_asprintf_errstring(ldb
, "Invalid entry type!");
802 return LDB_ERR_OPERATIONS_ERROR
;
806 return samldb_first_step(ac
);
809 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx
*ac
)
811 struct ldb_context
*ldb
;
812 const struct ldb_val
*rdn_value
;
816 ldb
= ldb_module_get_ctx(ac
->module
);
818 sid
= samdb_result_dom_sid(ac
->msg
, ac
->msg
, "objectSid");
820 rdn_value
= ldb_dn_get_rdn_val(ac
->msg
->dn
);
821 if (rdn_value
== NULL
) {
822 return ldb_operr(ldb
);
824 sid
= dom_sid_parse_talloc(ac
->msg
,
825 (const char *)rdn_value
->data
);
827 ldb_set_errstring(ldb
,
828 "samldb: No valid SID found in ForeignSecurityPrincipal CN!");
829 return LDB_ERR_CONSTRAINT_VIOLATION
;
831 if (! samldb_msg_add_sid(ac
->msg
, "objectSid", sid
)) {
832 return ldb_operr(ldb
);
836 /* finally proceed with adding the entry */
837 ret
= samldb_add_step(ac
, samldb_add_entry
);
838 if (ret
!= LDB_SUCCESS
) return ret
;
840 return samldb_first_step(ac
);
843 static int samldb_schema_info_update(struct samldb_ctx
*ac
)
846 struct ldb_context
*ldb
;
847 struct dsdb_schema
*schema
;
849 /* replicated update should always go through */
850 if (ldb_request_get_control(ac
->req
,
851 DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
855 /* do not update schemaInfo during provisioning */
856 if (ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
)) {
860 ldb
= ldb_module_get_ctx(ac
->module
);
861 schema
= dsdb_get_schema(ldb
, NULL
);
863 ldb_debug_set(ldb
, LDB_DEBUG_FATAL
,
864 "samldb_schema_info_update: no dsdb_schema loaded");
865 DEBUG(0,(__location__
": %s\n", ldb_errstring(ldb
)));
866 return ldb_operr(ldb
);
869 ret
= dsdb_module_schema_info_update(ac
->module
, schema
,
870 DSDB_FLAG_NEXT_MODULE
|
873 if (ret
!= LDB_SUCCESS
) {
874 ldb_asprintf_errstring(ldb
,
875 "samldb_schema_info_update: dsdb_module_schema_info_update failed with %s",
883 static int samldb_prim_group_tester(struct samldb_ctx
*ac
, uint32_t rid
);
886 * "Objectclass" trigger (MS-SAMR 3.1.1.8.1)
888 * Has to be invoked on "add" and "modify" operations on "user", "computer" and
890 * ac->msg contains the "add"/"modify" message
891 * ac->type contains the object type (main objectclass)
893 static int samldb_objectclass_trigger(struct samldb_ctx
*ac
)
895 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
896 void *skip_allocate_sids
= ldb_get_opaque(ldb
,
897 "skip_allocate_sids");
898 struct ldb_message_element
*el
, *el2
;
902 /* make sure that "sAMAccountType" is not specified */
903 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
905 ldb_set_errstring(ldb
,
906 "samldb: sAMAccountType must not be specified!");
907 return LDB_ERR_UNWILLING_TO_PERFORM
;
910 /* Step 1: objectSid assignment */
912 /* Don't allow the objectSid to be changed. But beside the RELAX
913 * control we have also to guarantee that it can always be set with
914 * SYSTEM permissions. This is needed for the "samba3sam" backend. */
915 sid
= samdb_result_dom_sid(ac
, ac
->msg
, "objectSid");
916 if ((sid
!= NULL
) && (!dsdb_module_am_system(ac
->module
)) &&
917 (ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
) == NULL
)) {
918 ldb_set_errstring(ldb
,
919 "samldb: objectSid must not be specified!");
920 return LDB_ERR_UNWILLING_TO_PERFORM
;
923 /* but generate a new SID when we do have an add operations */
924 if ((sid
== NULL
) && (ac
->req
->operation
== LDB_ADD
) && !skip_allocate_sids
) {
925 ret
= samldb_add_step(ac
, samldb_allocate_sid
);
926 if (ret
!= LDB_SUCCESS
) return ret
;
930 case SAMLDB_TYPE_USER
: {
931 bool uac_generated
= false;
933 /* Step 1.2: Default values */
934 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
935 "accountExpires", "9223372036854775807");
936 if (ret
!= LDB_SUCCESS
) return ret
;
937 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
938 "badPasswordTime", "0");
939 if (ret
!= LDB_SUCCESS
) return ret
;
940 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
942 if (ret
!= LDB_SUCCESS
) return ret
;
943 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
945 if (ret
!= LDB_SUCCESS
) return ret
;
946 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
948 if (ret
!= LDB_SUCCESS
) return ret
;
949 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
951 if (ret
!= LDB_SUCCESS
) return ret
;
952 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
954 if (ret
!= LDB_SUCCESS
) return ret
;
955 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
957 if (ret
!= LDB_SUCCESS
) return ret
;
958 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
960 if (ret
!= LDB_SUCCESS
) return ret
;
962 /* On add operations we might need to generate a
963 * "userAccountControl" (if it isn't specified). */
964 el
= ldb_msg_find_element(ac
->msg
, "userAccountControl");
965 if ((el
== NULL
) && (ac
->req
->operation
== LDB_ADD
)) {
966 ret
= samdb_msg_set_uint(ldb
, ac
->msg
, ac
->msg
,
967 "userAccountControl",
969 if (ret
!= LDB_SUCCESS
) {
972 uac_generated
= true;
975 el
= ldb_msg_find_element(ac
->msg
, "userAccountControl");
977 uint32_t user_account_control
, account_type
;
979 /* Step 1.3: "userAccountControl" -> "sAMAccountType" mapping */
980 user_account_control
= ldb_msg_find_attr_as_uint(ac
->msg
,
981 "userAccountControl",
984 /* Temporary duplicate accounts aren't allowed */
985 if ((user_account_control
& UF_TEMP_DUPLICATE_ACCOUNT
) != 0) {
986 return LDB_ERR_OTHER
;
989 /* Workstation and (read-only) DC objects do need objectclass "computer" */
990 if ((samdb_find_attribute(ldb
, ac
->msg
,
991 "objectclass", "computer") == NULL
) &&
992 (user_account_control
&
993 (UF_SERVER_TRUST_ACCOUNT
| UF_WORKSTATION_TRUST_ACCOUNT
))) {
994 ldb_set_errstring(ldb
,
995 "samldb: Requested account type does need objectclass 'computer'!");
996 return LDB_ERR_OBJECT_CLASS_VIOLATION
;
999 account_type
= ds_uf2atype(user_account_control
);
1000 if (account_type
== 0) {
1001 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
1002 return LDB_ERR_UNWILLING_TO_PERFORM
;
1004 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1007 if (ret
!= LDB_SUCCESS
) {
1010 el2
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
1011 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1013 /* "isCriticalSystemObject" might be set */
1014 if (user_account_control
&
1015 (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
)) {
1016 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
1018 if (ret
!= LDB_SUCCESS
) {
1021 el2
= ldb_msg_find_element(ac
->msg
,
1022 "isCriticalSystemObject");
1023 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1024 } else if (user_account_control
& UF_WORKSTATION_TRUST_ACCOUNT
) {
1025 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
1027 if (ret
!= LDB_SUCCESS
) {
1030 el2
= ldb_msg_find_element(ac
->msg
,
1031 "isCriticalSystemObject");
1032 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1035 /* Step 1.4: "userAccountControl" -> "primaryGroupID" mapping */
1036 if (!ldb_msg_find_element(ac
->msg
, "primaryGroupID")) {
1037 uint32_t rid
= ds_uf2prim_group_rid(user_account_control
);
1040 * Older AD deployments don't know about the
1043 if (rid
== DOMAIN_RID_READONLY_DCS
) {
1044 ret
= samldb_prim_group_tester(ac
, rid
);
1045 if (ret
!= LDB_SUCCESS
) {
1050 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1051 "primaryGroupID", rid
);
1052 if (ret
!= LDB_SUCCESS
) {
1055 el2
= ldb_msg_find_element(ac
->msg
,
1057 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1060 /* Step 1.5: Add additional flags when needed */
1061 /* Obviously this is done when the "userAccountControl"
1062 * has been generated here (tested against Windows
1064 if (uac_generated
) {
1065 user_account_control
|= UF_ACCOUNTDISABLE
;
1066 user_account_control
|= UF_PASSWD_NOTREQD
;
1068 ret
= samdb_msg_set_uint(ldb
, ac
->msg
, ac
->msg
,
1069 "userAccountControl",
1070 user_account_control
);
1071 if (ret
!= LDB_SUCCESS
) {
1079 case SAMLDB_TYPE_GROUP
: {
1080 const char *tempstr
;
1082 /* Step 2.2: Default values */
1083 tempstr
= talloc_asprintf(ac
->msg
, "%d",
1084 GTYPE_SECURITY_GLOBAL_GROUP
);
1085 if (tempstr
== NULL
) return ldb_operr(ldb
);
1086 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1087 "groupType", tempstr
);
1088 if (ret
!= LDB_SUCCESS
) return ret
;
1090 /* Step 2.3: "groupType" -> "sAMAccountType" */
1091 el
= ldb_msg_find_element(ac
->msg
, "groupType");
1093 uint32_t group_type
, account_type
;
1095 group_type
= ldb_msg_find_attr_as_uint(ac
->msg
,
1098 /* The creation of builtin groups requires the
1100 if (group_type
== GTYPE_SECURITY_BUILTIN_LOCAL_GROUP
) {
1101 if (ldb_request_get_control(ac
->req
,
1102 LDB_CONTROL_RELAX_OID
) == NULL
) {
1103 return LDB_ERR_UNWILLING_TO_PERFORM
;
1107 account_type
= ds_gtype2atype(group_type
);
1108 if (account_type
== 0) {
1109 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
1110 return LDB_ERR_UNWILLING_TO_PERFORM
;
1112 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1115 if (ret
!= LDB_SUCCESS
) {
1118 el2
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
1119 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1125 ldb_asprintf_errstring(ldb
,
1126 "Invalid entry type!");
1127 return LDB_ERR_OPERATIONS_ERROR
;
1135 * "Primary group ID" trigger (MS-SAMR 3.1.1.8.2)
1137 * Has to be invoked on "add" and "modify" operations on "user" and "computer"
1139 * ac->msg contains the "add"/"modify" message
1142 static int samldb_prim_group_tester(struct samldb_ctx
*ac
, uint32_t rid
)
1144 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1145 struct dom_sid
*sid
;
1146 struct ldb_result
*res
;
1148 const char * const noattrs
[] = { NULL
};
1150 sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), rid
);
1152 return ldb_operr(ldb
);
1155 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
1156 ldb_get_default_basedn(ldb
),
1158 noattrs
, DSDB_FLAG_NEXT_MODULE
,
1161 ldap_encode_ndr_dom_sid(ac
, sid
));
1162 if (ret
!= LDB_SUCCESS
) {
1165 if (res
->count
!= 1) {
1167 ldb_asprintf_errstring(ldb
,
1168 "Failed to find primary group with RID %u!",
1170 return LDB_ERR_UNWILLING_TO_PERFORM
;
1177 static int samldb_prim_group_set(struct samldb_ctx
*ac
)
1179 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1182 rid
= ldb_msg_find_attr_as_uint(ac
->msg
, "primaryGroupID", (uint32_t) -1);
1183 if (rid
== (uint32_t) -1) {
1184 /* we aren't affected of any primary group set */
1187 } else if (!ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
)) {
1188 ldb_set_errstring(ldb
,
1189 "The primary group isn't settable on add operations!");
1190 return LDB_ERR_UNWILLING_TO_PERFORM
;
1193 return samldb_prim_group_tester(ac
, rid
);
1196 static int samldb_prim_group_change(struct samldb_ctx
*ac
)
1198 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1199 const char * const attrs
[] = { "primaryGroupID", "memberOf", NULL
};
1200 struct ldb_result
*res
, *group_res
;
1201 struct ldb_message_element
*el
;
1202 struct ldb_message
*msg
;
1203 uint32_t prev_rid
, new_rid
;
1204 struct dom_sid
*prev_sid
, *new_sid
;
1205 struct ldb_dn
*prev_prim_group_dn
, *new_prim_group_dn
;
1207 const char * const noattrs
[] = { NULL
};
1209 el
= dsdb_get_single_valued_attr(ac
->msg
, "primaryGroupID",
1210 ac
->req
->operation
);
1212 /* we are not affected */
1216 /* Fetch information from the existing object */
1218 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, attrs
,
1219 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1220 if (ret
!= LDB_SUCCESS
) {
1224 /* Finds out the DN of the old primary group */
1226 prev_rid
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "primaryGroupID",
1228 if (prev_rid
== (uint32_t) -1) {
1229 /* User objects do always have a mandatory "primaryGroupID"
1230 * attribute. If this doesn't exist then the object is of the
1231 * wrong type. This is the exact Windows error code */
1232 return LDB_ERR_OBJECT_CLASS_VIOLATION
;
1235 prev_sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), prev_rid
);
1236 if (prev_sid
== NULL
) {
1237 return ldb_operr(ldb
);
1240 /* Finds out the DN of the new primary group
1241 * Notice: in order to parse the primary group ID correctly we create
1242 * a temporary message here. */
1244 msg
= ldb_msg_new(ac
->msg
);
1246 return ldb_module_oom(ac
->module
);
1248 ret
= ldb_msg_add(msg
, el
, 0);
1249 if (ret
!= LDB_SUCCESS
) {
1252 new_rid
= ldb_msg_find_attr_as_uint(msg
, "primaryGroupID", (uint32_t) -1);
1254 if (new_rid
== (uint32_t) -1) {
1255 /* we aren't affected of any primary group change */
1259 if (prev_rid
== new_rid
) {
1263 ret
= dsdb_module_search(ac
->module
, ac
, &group_res
,
1264 ldb_get_default_basedn(ldb
),
1266 noattrs
, DSDB_FLAG_NEXT_MODULE
,
1269 ldap_encode_ndr_dom_sid(ac
, prev_sid
));
1270 if (ret
!= LDB_SUCCESS
) {
1273 if (group_res
->count
!= 1) {
1274 return ldb_operr(ldb
);
1276 prev_prim_group_dn
= group_res
->msgs
[0]->dn
;
1278 new_sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), new_rid
);
1279 if (new_sid
== NULL
) {
1280 return ldb_operr(ldb
);
1283 ret
= dsdb_module_search(ac
->module
, ac
, &group_res
,
1284 ldb_get_default_basedn(ldb
),
1286 noattrs
, DSDB_FLAG_NEXT_MODULE
,
1289 ldap_encode_ndr_dom_sid(ac
, new_sid
));
1290 if (ret
!= LDB_SUCCESS
) {
1293 if (group_res
->count
!= 1) {
1294 /* Here we know if the specified new primary group candidate is
1296 return LDB_ERR_UNWILLING_TO_PERFORM
;
1298 new_prim_group_dn
= group_res
->msgs
[0]->dn
;
1300 /* We need to be already a normal member of the new primary
1301 * group in order to be successful. */
1302 el
= samdb_find_attribute(ldb
, res
->msgs
[0], "memberOf",
1303 ldb_dn_get_linearized(new_prim_group_dn
));
1305 return LDB_ERR_UNWILLING_TO_PERFORM
;
1308 /* Remove the "member" attribute on the new primary group */
1309 msg
= ldb_msg_new(ac
->msg
);
1311 return ldb_module_oom(ac
->module
);
1313 msg
->dn
= new_prim_group_dn
;
1315 ret
= samdb_msg_add_delval(ldb
, msg
, msg
, "member",
1316 ldb_dn_get_linearized(ac
->msg
->dn
));
1317 if (ret
!= LDB_SUCCESS
) {
1321 ret
= dsdb_module_modify(ac
->module
, msg
, DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1322 if (ret
!= LDB_SUCCESS
) {
1327 /* Add a "member" attribute for the previous primary group */
1328 msg
= ldb_msg_new(ac
->msg
);
1330 return ldb_module_oom(ac
->module
);
1332 msg
->dn
= prev_prim_group_dn
;
1334 ret
= samdb_msg_add_addval(ldb
, msg
, msg
, "member",
1335 ldb_dn_get_linearized(ac
->msg
->dn
));
1336 if (ret
!= LDB_SUCCESS
) {
1340 ret
= dsdb_module_modify(ac
->module
, msg
, DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1341 if (ret
!= LDB_SUCCESS
) {
1349 static int samldb_prim_group_trigger(struct samldb_ctx
*ac
)
1353 if (ac
->req
->operation
== LDB_ADD
) {
1354 ret
= samldb_prim_group_set(ac
);
1356 ret
= samldb_prim_group_change(ac
);
1364 * This function is called on LDB modify operations. It performs some additions/
1365 * replaces on the current LDB message when "userAccountControl" changes.
1367 static int samldb_user_account_control_change(struct samldb_ctx
*ac
)
1369 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1370 uint32_t user_account_control
, old_user_account_control
, account_type
;
1371 struct ldb_message_element
*el
;
1372 struct ldb_message
*tmp_msg
;
1374 struct ldb_result
*res
;
1375 const char * const attrs
[] = { "userAccountControl", "objectClass", NULL
};
1377 bool is_computer
= false;
1379 el
= dsdb_get_single_valued_attr(ac
->msg
, "userAccountControl",
1380 ac
->req
->operation
);
1382 /* we are not affected */
1386 /* Create a temporary message for fetching the "userAccountControl" */
1387 tmp_msg
= ldb_msg_new(ac
->msg
);
1388 if (tmp_msg
== NULL
) {
1389 return ldb_module_oom(ac
->module
);
1391 ret
= ldb_msg_add(tmp_msg
, el
, 0);
1392 if (ret
!= LDB_SUCCESS
) {
1395 user_account_control
= ldb_msg_find_attr_as_uint(tmp_msg
,
1396 "userAccountControl",
1398 talloc_free(tmp_msg
);
1400 /* Temporary duplicate accounts aren't allowed */
1401 if ((user_account_control
& UF_TEMP_DUPLICATE_ACCOUNT
) != 0) {
1402 return LDB_ERR_OTHER
;
1405 /* Fetch the old "userAccountControl" and "objectClass" */
1406 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, attrs
,
1407 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1408 if (ret
!= LDB_SUCCESS
) {
1411 old_user_account_control
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "userAccountControl", 0);
1412 if (old_user_account_control
== 0) {
1413 return ldb_operr(ldb
);
1415 el
= ldb_msg_find_element(res
->msgs
[0], "objectClass");
1417 return ldb_operr(ldb
);
1420 /* When we do not have objectclass "computer" we cannot switch to a (read-only) DC */
1421 for (i
= 0; i
< el
->num_values
; i
++) {
1422 if (ldb_attr_cmp((char *)el
->values
[i
].data
, "computer") == 0) {
1428 (user_account_control
& (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
))) {
1429 ldb_set_errstring(ldb
,
1430 "samldb: Requested account type does need objectclass 'computer'!");
1431 return LDB_ERR_UNWILLING_TO_PERFORM
;
1435 * The functions "ds_uf2atype" and "ds_uf2prim_group_rid" are used as
1436 * detectors for account type changes.
1437 * So if the account type does change then we need to adjust the
1438 * "sAMAccountType", the "isCriticalSystemObject" and the
1439 * "primaryGroupID" attribute.
1441 if ((ds_uf2atype(user_account_control
)
1442 == ds_uf2atype(old_user_account_control
)) &&
1443 (ds_uf2prim_group_rid(user_account_control
)
1444 == ds_uf2prim_group_rid(old_user_account_control
))) {
1448 account_type
= ds_uf2atype(user_account_control
);
1449 if (account_type
== 0) {
1450 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
1451 return LDB_ERR_UNWILLING_TO_PERFORM
;
1453 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
, "sAMAccountType",
1455 if (ret
!= LDB_SUCCESS
) {
1458 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
1459 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1461 /* "isCriticalSystemObject" might be set/changed */
1462 if (user_account_control
1463 & (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
)) {
1464 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
1466 if (ret
!= LDB_SUCCESS
) {
1469 el
= ldb_msg_find_element(ac
->msg
,
1470 "isCriticalSystemObject");
1471 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1472 } else if (user_account_control
& UF_WORKSTATION_TRUST_ACCOUNT
) {
1473 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
1475 if (ret
!= LDB_SUCCESS
) {
1478 el
= ldb_msg_find_element(ac
->msg
,
1479 "isCriticalSystemObject");
1480 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1483 if (!ldb_msg_find_element(ac
->msg
, "primaryGroupID")) {
1484 uint32_t rid
= ds_uf2prim_group_rid(user_account_control
);
1486 /* Older AD deployments don't know about the RODC group */
1487 if (rid
== DOMAIN_RID_READONLY_DCS
) {
1488 ret
= samldb_prim_group_tester(ac
, rid
);
1489 if (ret
!= LDB_SUCCESS
) {
1494 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1495 "primaryGroupID", rid
);
1496 if (ret
!= LDB_SUCCESS
) {
1499 el
= ldb_msg_find_element(ac
->msg
,
1501 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1507 static int samldb_group_type_change(struct samldb_ctx
*ac
)
1509 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1510 uint32_t group_type
, old_group_type
, account_type
;
1511 struct ldb_message_element
*el
;
1512 struct ldb_message
*tmp_msg
;
1514 struct ldb_result
*res
;
1515 const char * const attrs
[] = { "groupType", NULL
};
1517 el
= dsdb_get_single_valued_attr(ac
->msg
, "groupType",
1518 ac
->req
->operation
);
1520 /* we are not affected */
1524 /* Create a temporary message for fetching the "groupType" */
1525 tmp_msg
= ldb_msg_new(ac
->msg
);
1526 if (tmp_msg
== NULL
) {
1527 return ldb_module_oom(ac
->module
);
1529 ret
= ldb_msg_add(tmp_msg
, el
, 0);
1530 if (ret
!= LDB_SUCCESS
) {
1533 group_type
= ldb_msg_find_attr_as_uint(tmp_msg
, "groupType", 0);
1534 talloc_free(tmp_msg
);
1536 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, attrs
,
1537 DSDB_FLAG_NEXT_MODULE
|
1538 DSDB_SEARCH_SHOW_DELETED
, ac
->req
);
1539 if (ret
!= LDB_SUCCESS
) {
1542 old_group_type
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "groupType", 0);
1543 if (old_group_type
== 0) {
1544 return ldb_operr(ldb
);
1547 /* Group type switching isn't so easy as it seems: We can only
1548 * change in this directions: global <-> universal <-> local
1549 * On each step also the group type itself
1550 * (security/distribution) is variable. */
1552 if (ldb_request_get_control(ac
->req
, LDB_CONTROL_PROVISION_OID
) == NULL
) {
1553 switch (group_type
) {
1554 case GTYPE_SECURITY_GLOBAL_GROUP
:
1555 case GTYPE_DISTRIBUTION_GLOBAL_GROUP
:
1556 /* change to "universal" allowed */
1557 if ((old_group_type
== GTYPE_SECURITY_DOMAIN_LOCAL_GROUP
) ||
1558 (old_group_type
== GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP
)) {
1559 ldb_set_errstring(ldb
,
1560 "samldb: Change from security/distribution local group forbidden!");
1561 return LDB_ERR_UNWILLING_TO_PERFORM
;
1565 case GTYPE_SECURITY_UNIVERSAL_GROUP
:
1566 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP
:
1567 /* each change allowed */
1569 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP
:
1570 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP
:
1571 /* change to "universal" allowed */
1572 if ((old_group_type
== GTYPE_SECURITY_GLOBAL_GROUP
) ||
1573 (old_group_type
== GTYPE_DISTRIBUTION_GLOBAL_GROUP
)) {
1574 ldb_set_errstring(ldb
,
1575 "samldb: Change from security/distribution global group forbidden!");
1576 return LDB_ERR_UNWILLING_TO_PERFORM
;
1580 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP
:
1582 /* we don't allow this "groupType" values */
1583 return LDB_ERR_UNWILLING_TO_PERFORM
;
1588 account_type
= ds_gtype2atype(group_type
);
1589 if (account_type
== 0) {
1590 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
1591 return LDB_ERR_UNWILLING_TO_PERFORM
;
1593 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
, "sAMAccountType",
1595 if (ret
!= LDB_SUCCESS
) {
1598 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
1599 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1604 static int samldb_sam_accountname_check(struct samldb_ctx
*ac
)
1606 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1607 const char * const no_attrs
[] = { NULL
};
1608 struct ldb_result
*res
;
1609 const char *sam_accountname
, *enc_str
;
1610 struct ldb_message_element
*el
;
1611 struct ldb_message
*tmp_msg
;
1614 el
= dsdb_get_single_valued_attr(ac
->msg
, "sAMAccountName",
1615 ac
->req
->operation
);
1617 /* we are not affected */
1621 /* Create a temporary message for fetching the "sAMAccountName" */
1622 tmp_msg
= ldb_msg_new(ac
->msg
);
1623 if (tmp_msg
== NULL
) {
1624 return ldb_module_oom(ac
->module
);
1626 ret
= ldb_msg_add(tmp_msg
, el
, 0);
1627 if (ret
!= LDB_SUCCESS
) {
1630 sam_accountname
= talloc_steal(ac
,
1631 ldb_msg_find_attr_as_string(tmp_msg
, "sAMAccountName", NULL
));
1632 talloc_free(tmp_msg
);
1634 if (sam_accountname
== NULL
) {
1635 /* The "sAMAccountName" cannot be nothing */
1636 ldb_set_errstring(ldb
,
1637 "samldb: Empty account names aren't allowed!");
1638 return LDB_ERR_UNWILLING_TO_PERFORM
;
1641 enc_str
= ldb_binary_encode_string(ac
, sam_accountname
);
1642 if (enc_str
== NULL
) {
1643 return ldb_module_oom(ac
->module
);
1646 /* Make sure that a "sAMAccountName" is only used once */
1648 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
1649 ldb_get_default_basedn(ldb
),
1650 LDB_SCOPE_SUBTREE
, no_attrs
,
1651 DSDB_FLAG_NEXT_MODULE
, ac
->req
,
1652 "(sAMAccountName=%s)", enc_str
);
1653 if (ret
!= LDB_SUCCESS
) {
1656 if (res
->count
> 1) {
1657 return ldb_operr(ldb
);
1658 } else if (res
->count
== 1) {
1659 if (ldb_dn_compare(res
->msgs
[0]->dn
, ac
->msg
->dn
) != 0) {
1660 ldb_asprintf_errstring(ldb
,
1661 "samldb: Account name (sAMAccountName) '%s' already in use!",
1663 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
1671 static int samldb_member_check(struct samldb_ctx
*ac
)
1673 const char * const attrs
[] = { "objectSid", "member", NULL
};
1674 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1675 struct ldb_message_element
*el
;
1676 struct ldb_dn
*member_dn
;
1677 struct dom_sid
*sid
;
1678 struct ldb_result
*res
;
1679 struct dom_sid
*group_sid
;
1683 /* Fetch information from the existing object */
1685 ret
= dsdb_module_search(ac
->module
, ac
, &res
, ac
->msg
->dn
, LDB_SCOPE_BASE
, attrs
,
1686 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
, ac
->req
, NULL
);
1687 if (ret
!= LDB_SUCCESS
) {
1690 if (res
->count
!= 1) {
1691 return ldb_operr(ldb
);
1694 group_sid
= samdb_result_dom_sid(res
, res
->msgs
[0], "objectSid");
1695 if (group_sid
== NULL
) {
1696 return ldb_operr(ldb
);
1699 /* We've to walk over all modification entries and consider the "member"
1701 for (i
= 0; i
< ac
->msg
->num_elements
; i
++) {
1702 if (ldb_attr_cmp(ac
->msg
->elements
[i
].name
, "member") != 0) {
1706 el
= &ac
->msg
->elements
[i
];
1707 for (j
= 0; j
< el
->num_values
; j
++) {
1708 struct ldb_result
*group_res
;
1709 const char *group_attrs
[] = { "primaryGroupID" , NULL
};
1710 uint32_t prim_group_rid
;
1712 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_DELETE
) {
1713 /* Deletes will be handled in
1714 * repl_meta_data, and deletes not
1715 * matching a member will return
1716 * LDB_ERR_UNWILLING_TO_PERFORM
1721 member_dn
= ldb_dn_from_ldb_val(ac
, ldb
,
1723 if (!ldb_dn_validate(member_dn
)) {
1724 return ldb_operr(ldb
);
1727 /* Denies to add "member"s to groups which are primary
1728 * ones for them - in this case return
1729 * ERR_ENTRY_ALREADY_EXISTS. */
1731 ret
= dsdb_module_search_dn(ac
->module
, ac
, &group_res
,
1732 member_dn
, group_attrs
,
1733 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1734 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
1735 /* member DN doesn't exist yet */
1738 if (ret
!= LDB_SUCCESS
) {
1741 prim_group_rid
= ldb_msg_find_attr_as_uint(group_res
->msgs
[0], "primaryGroupID", (uint32_t)-1);
1742 if (prim_group_rid
== (uint32_t) -1) {
1743 /* the member hasn't to be a user account ->
1744 * therefore no check needed in this case. */
1748 sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
),
1751 return ldb_operr(ldb
);
1754 if (dom_sid_equal(group_sid
, sid
)) {
1755 ldb_asprintf_errstring(ldb
,
1756 "samldb: member %s already set via primaryGroupID %u",
1757 ldb_dn_get_linearized(member_dn
), prim_group_rid
);
1758 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
1768 /* SAM objects have special rules regarding the "description" attribute on
1769 * modify operations. */
1770 static int samldb_description_check(struct samldb_ctx
*ac
, bool *modified
)
1772 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1773 const char * const attrs
[] = { "objectClass", "description", NULL
};
1774 struct ldb_result
*res
;
1778 /* Fetch information from the existing object */
1779 ret
= dsdb_module_search(ac
->module
, ac
, &res
, ac
->msg
->dn
, LDB_SCOPE_BASE
, attrs
,
1780 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
, ac
->req
,
1781 "(|(objectclass=user)(objectclass=group)(objectclass=samDomain)(objectclass=samServer))");
1782 if (ret
!= LDB_SUCCESS
) {
1783 /* don't treat it specially ... let normal error codes
1784 happen from other places */
1785 ldb_reset_err_string(ldb
);
1788 if (res
->count
== 0) {
1789 /* we didn't match the filter */
1794 /* We've to walk over all modification entries and consider the
1795 * "description" ones. */
1796 for (i
= 0; i
< ac
->msg
->num_elements
; i
++) {
1797 if (ldb_attr_cmp(ac
->msg
->elements
[i
].name
, "description") == 0) {
1798 ac
->msg
->elements
[i
].flags
|= LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK
;
1808 /* This trigger adapts the "servicePrincipalName" attributes if the
1809 * "dNSHostName" and/or "sAMAccountName" attribute change(s) */
1810 static int samldb_service_principal_names_change(struct samldb_ctx
*ac
)
1812 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1813 struct ldb_message_element
*el
= NULL
, *el2
= NULL
;
1814 struct ldb_message
*msg
;
1815 const char * const attrs
[] = { "servicePrincipalName", NULL
};
1816 struct ldb_result
*res
;
1817 const char *dns_hostname
= NULL
, *old_dns_hostname
= NULL
,
1818 *sam_accountname
= NULL
, *old_sam_accountname
= NULL
;
1822 el
= dsdb_get_single_valued_attr(ac
->msg
, "dNSHostName",
1823 ac
->req
->operation
);
1824 el2
= dsdb_get_single_valued_attr(ac
->msg
, "sAMAccountName",
1825 ac
->req
->operation
);
1826 if ((el
== NULL
) && (el2
== NULL
)) {
1827 /* we are not affected */
1831 /* Create a temporary message for fetching the "dNSHostName" */
1833 const char *dns_attrs
[] = { "dNSHostName", NULL
};
1834 msg
= ldb_msg_new(ac
->msg
);
1836 return ldb_module_oom(ac
->module
);
1838 ret
= ldb_msg_add(msg
, el
, 0);
1839 if (ret
!= LDB_SUCCESS
) {
1842 dns_hostname
= talloc_steal(ac
,
1843 ldb_msg_find_attr_as_string(msg
, "dNSHostName", NULL
));
1846 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
,
1847 dns_attrs
, DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1848 if (ret
== LDB_SUCCESS
) {
1849 old_dns_hostname
= ldb_msg_find_attr_as_string(res
->msgs
[0], "dNSHostName", NULL
);
1853 /* Create a temporary message for fetching the "sAMAccountName" */
1855 char *tempstr
, *tempstr2
;
1856 const char *acct_attrs
[] = { "sAMAccountName", NULL
};
1858 msg
= ldb_msg_new(ac
->msg
);
1860 return ldb_module_oom(ac
->module
);
1862 ret
= ldb_msg_add(msg
, el2
, 0);
1863 if (ret
!= LDB_SUCCESS
) {
1866 tempstr
= talloc_strdup(ac
,
1867 ldb_msg_find_attr_as_string(msg
, "sAMAccountName", NULL
));
1870 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, acct_attrs
,
1871 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1872 if (ret
== LDB_SUCCESS
) {
1873 tempstr2
= talloc_strdup(ac
,
1874 ldb_msg_find_attr_as_string(res
->msgs
[0],
1875 "sAMAccountName", NULL
));
1879 /* The "sAMAccountName" needs some additional trimming: we need
1880 * to remove the trailing "$"s if they exist. */
1881 if ((tempstr
!= NULL
) && (tempstr
[0] != '\0') &&
1882 (tempstr
[strlen(tempstr
) - 1] == '$')) {
1883 tempstr
[strlen(tempstr
) - 1] = '\0';
1885 if ((tempstr2
!= NULL
) && (tempstr2
[0] != '\0') &&
1886 (tempstr2
[strlen(tempstr2
) - 1] == '$')) {
1887 tempstr2
[strlen(tempstr2
) - 1] = '\0';
1889 sam_accountname
= tempstr
;
1890 old_sam_accountname
= tempstr2
;
1893 if (old_dns_hostname
== NULL
) {
1894 /* we cannot change when the old name is unknown */
1895 dns_hostname
= NULL
;
1897 if ((old_dns_hostname
!= NULL
) && (dns_hostname
!= NULL
) &&
1898 (strcasecmp(old_dns_hostname
, dns_hostname
) == 0)) {
1899 /* The "dNSHostName" didn't change */
1900 dns_hostname
= NULL
;
1903 if (old_sam_accountname
== NULL
) {
1904 /* we cannot change when the old name is unknown */
1905 sam_accountname
= NULL
;
1907 if ((old_sam_accountname
!= NULL
) && (sam_accountname
!= NULL
) &&
1908 (strcasecmp(old_sam_accountname
, sam_accountname
) == 0)) {
1909 /* The "sAMAccountName" didn't change */
1910 sam_accountname
= NULL
;
1913 if ((dns_hostname
== NULL
) && (sam_accountname
== NULL
)) {
1914 /* Well, there are information missing (old name(s)) or the
1915 * names didn't change. We've nothing to do and can exit here */
1919 /* Potential "servicePrincipalName" changes in the same request have to
1920 * be handled before the update (Windows behaviour). */
1921 el
= ldb_msg_find_element(ac
->msg
, "servicePrincipalName");
1923 msg
= ldb_msg_new(ac
->msg
);
1925 return ldb_module_oom(ac
->module
);
1927 msg
->dn
= ac
->msg
->dn
;
1930 ret
= ldb_msg_add(msg
, el
, el
->flags
);
1931 if (ret
!= LDB_SUCCESS
) {
1935 ldb_msg_remove_element(ac
->msg
, el
);
1937 el
= ldb_msg_find_element(ac
->msg
,
1938 "servicePrincipalName");
1939 } while (el
!= NULL
);
1941 ret
= dsdb_module_modify(ac
->module
, msg
,
1942 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1943 if (ret
!= LDB_SUCCESS
) {
1949 /* Fetch the "servicePrincipalName"s if any */
1950 ret
= dsdb_module_search(ac
->module
, ac
, &res
, ac
->msg
->dn
, LDB_SCOPE_BASE
, attrs
,
1951 DSDB_FLAG_NEXT_MODULE
, ac
->req
, NULL
);
1952 if (ret
!= LDB_SUCCESS
) {
1955 if ((res
->count
!= 1) || (res
->msgs
[0]->num_elements
> 1)) {
1956 return ldb_operr(ldb
);
1959 if (res
->msgs
[0]->num_elements
== 1) {
1960 /* Yes, we do have "servicePrincipalName"s. First we update them
1961 * locally, that means we do always substitute the current
1962 * "dNSHostName" with the new one and/or "sAMAccountName"
1963 * without "$" with the new one and then we append this to the
1964 * modification request (Windows behaviour). */
1966 for (i
= 0; i
< res
->msgs
[0]->elements
[0].num_values
; i
++) {
1967 char *old_str
, *new_str
, *pos
;
1971 res
->msgs
[0]->elements
[0].values
[i
].data
;
1973 new_str
= talloc_strdup(ac
->msg
,
1974 strtok_r(old_str
, "/", &pos
));
1975 if (new_str
== NULL
) {
1976 return ldb_module_oom(ac
->module
);
1979 while ((tok
= strtok_r(NULL
, "/", &pos
)) != NULL
) {
1980 if ((dns_hostname
!= NULL
) &&
1981 (strcasecmp(tok
, old_dns_hostname
) == 0)) {
1984 if ((sam_accountname
!= NULL
) &&
1985 (strcasecmp(tok
, old_sam_accountname
) == 0)) {
1986 tok
= sam_accountname
;
1989 new_str
= talloc_asprintf(ac
->msg
, "%s/%s",
1991 if (new_str
== NULL
) {
1992 return ldb_module_oom(ac
->module
);
1996 ret
= ldb_msg_add_string(ac
->msg
,
1997 "servicePrincipalName",
1999 if (ret
!= LDB_SUCCESS
) {
2004 el
= ldb_msg_find_element(ac
->msg
, "servicePrincipalName");
2005 el
->flags
= LDB_FLAG_MOD_REPLACE
;
2013 /* This checks the "fSMORoleOwner" attributes */
2014 static int samldb_fsmo_role_owner_check(struct samldb_ctx
*ac
)
2016 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
2017 const char * const no_attrs
[] = { NULL
};
2018 struct ldb_message_element
*el
;
2019 struct ldb_message
*tmp_msg
;
2020 struct ldb_dn
*res_dn
;
2021 struct ldb_result
*res
;
2024 el
= dsdb_get_single_valued_attr(ac
->msg
, "fSMORoleOwner",
2025 ac
->req
->operation
);
2027 /* we are not affected */
2031 /* Create a temporary message for fetching the "fSMORoleOwner" */
2032 tmp_msg
= ldb_msg_new(ac
->msg
);
2033 if (tmp_msg
== NULL
) {
2034 return ldb_module_oom(ac
->module
);
2036 ret
= ldb_msg_add(tmp_msg
, el
, 0);
2037 if (ret
!= LDB_SUCCESS
) {
2040 res_dn
= ldb_msg_find_attr_as_dn(ldb
, ac
, tmp_msg
, "fSMORoleOwner");
2041 talloc_free(tmp_msg
);
2043 if (res_dn
== NULL
) {
2044 ldb_set_errstring(ldb
,
2045 "samldb: 'fSMORoleOwner' attributes have to reference 'nTDSDSA' entries!");
2046 if (ac
->req
->operation
== LDB_ADD
) {
2047 return LDB_ERR_CONSTRAINT_VIOLATION
;
2049 return LDB_ERR_UNWILLING_TO_PERFORM
;
2053 /* Fetched DN has to reference a "nTDSDSA" entry */
2054 ret
= dsdb_module_search(ac
->module
, ac
, &res
, res_dn
, LDB_SCOPE_BASE
,
2056 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
,
2057 ac
->req
, "(objectClass=nTDSDSA)");
2058 if (ret
!= LDB_SUCCESS
) {
2061 if (res
->count
!= 1) {
2062 ldb_set_errstring(ldb
,
2063 "samldb: 'fSMORoleOwner' attributes have to reference 'nTDSDSA' entries!");
2064 return LDB_ERR_UNWILLING_TO_PERFORM
;
2074 static int samldb_add(struct ldb_module
*module
, struct ldb_request
*req
)
2076 struct ldb_context
*ldb
;
2077 struct samldb_ctx
*ac
;
2078 struct ldb_message_element
*el
;
2081 ldb
= ldb_module_get_ctx(module
);
2082 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "samldb_add\n");
2084 /* do not manipulate our control entries */
2085 if (ldb_dn_is_special(req
->op
.add
.message
->dn
)) {
2086 return ldb_next_request(module
, req
);
2089 ac
= samldb_ctx_init(module
, req
);
2091 return ldb_operr(ldb
);
2094 /* build the new msg */
2095 ac
->msg
= ldb_msg_copy_shallow(ac
, req
->op
.add
.message
);
2096 if (ac
->msg
== NULL
) {
2098 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
2099 "samldb_add: ldb_msg_copy_shallow failed!\n");
2100 return ldb_operr(ldb
);
2103 el
= ldb_msg_find_element(ac
->msg
, "fSMORoleOwner");
2105 ret
= samldb_fsmo_role_owner_check(ac
);
2106 if (ret
!= LDB_SUCCESS
) {
2111 if (samdb_find_attribute(ldb
, ac
->msg
,
2112 "objectclass", "user") != NULL
) {
2113 ac
->type
= SAMLDB_TYPE_USER
;
2115 ret
= samldb_prim_group_trigger(ac
);
2116 if (ret
!= LDB_SUCCESS
) {
2120 ret
= samldb_objectclass_trigger(ac
);
2121 if (ret
!= LDB_SUCCESS
) {
2125 return samldb_fill_object(ac
);
2128 if (samdb_find_attribute(ldb
, ac
->msg
,
2129 "objectclass", "group") != NULL
) {
2130 ac
->type
= SAMLDB_TYPE_GROUP
;
2132 ret
= samldb_objectclass_trigger(ac
);
2133 if (ret
!= LDB_SUCCESS
) {
2137 return samldb_fill_object(ac
);
2140 /* perhaps a foreignSecurityPrincipal? */
2141 if (samdb_find_attribute(ldb
, ac
->msg
,
2143 "foreignSecurityPrincipal") != NULL
) {
2144 return samldb_fill_foreignSecurityPrincipal_object(ac
);
2147 if (samdb_find_attribute(ldb
, ac
->msg
,
2148 "objectclass", "classSchema") != NULL
) {
2149 ret
= samldb_schema_info_update(ac
);
2150 if (ret
!= LDB_SUCCESS
) {
2155 ac
->type
= SAMLDB_TYPE_CLASS
;
2156 return samldb_fill_object(ac
);
2159 if (samdb_find_attribute(ldb
, ac
->msg
,
2160 "objectclass", "attributeSchema") != NULL
) {
2161 ret
= samldb_schema_info_update(ac
);
2162 if (ret
!= LDB_SUCCESS
) {
2167 ac
->type
= SAMLDB_TYPE_ATTRIBUTE
;
2168 return samldb_fill_object(ac
);
2173 /* nothing matched, go on */
2174 return ldb_next_request(module
, req
);
2178 static int samldb_modify(struct ldb_module
*module
, struct ldb_request
*req
)
2180 struct ldb_context
*ldb
;
2181 struct samldb_ctx
*ac
;
2182 struct ldb_message_element
*el
, *el2
;
2183 bool modified
= false;
2186 if (ldb_dn_is_special(req
->op
.mod
.message
->dn
)) {
2187 /* do not manipulate our control entries */
2188 return ldb_next_request(module
, req
);
2191 ldb
= ldb_module_get_ctx(module
);
2193 /* make sure that "objectSid" is not specified */
2194 el
= ldb_msg_find_element(req
->op
.mod
.message
, "objectSid");
2196 if (ldb_request_get_control(req
, LDB_CONTROL_PROVISION_OID
) == NULL
) {
2197 ldb_set_errstring(ldb
,
2198 "samldb: objectSid must not be specified!");
2199 return LDB_ERR_UNWILLING_TO_PERFORM
;
2202 /* make sure that "sAMAccountType" is not specified */
2203 el
= ldb_msg_find_element(req
->op
.mod
.message
, "sAMAccountType");
2205 ldb_set_errstring(ldb
,
2206 "samldb: sAMAccountType must not be specified!");
2207 return LDB_ERR_UNWILLING_TO_PERFORM
;
2209 /* make sure that "isCriticalSystemObject" is not specified */
2210 el
= ldb_msg_find_element(req
->op
.mod
.message
, "isCriticalSystemObject");
2212 if (ldb_request_get_control(req
, LDB_CONTROL_RELAX_OID
) == NULL
) {
2213 ldb_set_errstring(ldb
,
2214 "samldb: isCriticalSystemObject must not be specified!");
2215 return LDB_ERR_UNWILLING_TO_PERFORM
;
2219 /* msDS-IntId is not allowed to be modified
2220 * except when modification comes from replication */
2221 if (ldb_msg_find_element(req
->op
.mod
.message
, "msDS-IntId")) {
2222 if (!ldb_request_get_control(req
,
2223 DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
2224 return LDB_ERR_CONSTRAINT_VIOLATION
;
2228 ac
= samldb_ctx_init(module
, req
);
2230 return ldb_operr(ldb
);
2233 /* build the new msg */
2234 ac
->msg
= ldb_msg_copy_shallow(ac
, req
->op
.mod
.message
);
2235 if (ac
->msg
== NULL
) {
2237 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
2238 "samldb_modify: ldb_msg_copy_shallow failed!\n");
2239 return ldb_operr(ldb
);
2242 el
= ldb_msg_find_element(ac
->msg
, "primaryGroupID");
2244 ret
= samldb_prim_group_trigger(ac
);
2245 if (ret
!= LDB_SUCCESS
) {
2250 el
= ldb_msg_find_element(ac
->msg
, "userAccountControl");
2253 ret
= samldb_user_account_control_change(ac
);
2254 if (ret
!= LDB_SUCCESS
) {
2259 el
= ldb_msg_find_element(ac
->msg
, "groupType");
2262 ret
= samldb_group_type_change(ac
);
2263 if (ret
!= LDB_SUCCESS
) {
2268 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountName");
2270 ret
= samldb_sam_accountname_check(ac
);
2271 if (ret
!= LDB_SUCCESS
) {
2276 el
= ldb_msg_find_element(ac
->msg
, "member");
2278 ret
= samldb_member_check(ac
);
2279 if (ret
!= LDB_SUCCESS
) {
2284 el
= ldb_msg_find_element(ac
->msg
, "description");
2286 ret
= samldb_description_check(ac
, &modified
);
2287 if (ret
!= LDB_SUCCESS
) {
2292 el
= ldb_msg_find_element(ac
->msg
, "dNSHostName");
2293 el2
= ldb_msg_find_element(ac
->msg
, "sAMAccountName");
2294 if ((el
!= NULL
) || (el2
!= NULL
)) {
2296 ret
= samldb_service_principal_names_change(ac
);
2297 if (ret
!= LDB_SUCCESS
) {
2302 el
= ldb_msg_find_element(ac
->msg
, "fSMORoleOwner");
2304 ret
= samldb_fsmo_role_owner_check(ac
);
2305 if (ret
!= LDB_SUCCESS
) {
2311 struct ldb_request
*child_req
;
2313 /* Now perform the real modifications as a child request */
2314 ret
= ldb_build_mod_req(&child_req
, ldb
, ac
,
2317 req
, dsdb_next_callback
,
2319 LDB_REQ_SET_LOCATION(child_req
);
2320 if (ret
!= LDB_SUCCESS
) {
2324 return ldb_next_request(module
, child_req
);
2329 /* no change which interests us, go on */
2330 return ldb_next_request(module
, req
);
2335 static int samldb_prim_group_users_check(struct samldb_ctx
*ac
)
2337 struct ldb_context
*ldb
;
2338 struct dom_sid
*sid
;
2342 struct ldb_result
*res
;
2343 const char * const attrs
[] = { "objectSid", "isDeleted", NULL
};
2344 const char * const noattrs
[] = { NULL
};
2346 ldb
= ldb_module_get_ctx(ac
->module
);
2348 /* Finds out the SID/RID of the SAM object */
2349 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->req
->op
.del
.dn
,
2351 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
,
2353 if (ret
!= LDB_SUCCESS
) {
2357 if (ldb_msg_check_string_attribute(res
->msgs
[0], "isDeleted", "TRUE")) {
2361 sid
= samdb_result_dom_sid(ac
, res
->msgs
[0], "objectSid");
2363 /* No SID - it might not be a SAM object - therefore ok */
2366 status
= dom_sid_split_rid(ac
, sid
, NULL
, &rid
);
2367 if (!NT_STATUS_IS_OK(status
)) {
2368 return ldb_operr(ldb
);
2371 /* Special object (security principal?) */
2375 /* Deny delete requests from groups which are primary ones */
2376 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
2377 ldb_get_default_basedn(ldb
),
2378 LDB_SCOPE_SUBTREE
, noattrs
,
2379 DSDB_FLAG_NEXT_MODULE
,
2381 "(&(primaryGroupID=%u)(objectClass=user))", rid
);
2382 if (ret
!= LDB_SUCCESS
) {
2385 if (res
->count
> 0) {
2386 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
2392 static int samldb_delete(struct ldb_module
*module
, struct ldb_request
*req
)
2394 struct samldb_ctx
*ac
;
2397 if (ldb_dn_is_special(req
->op
.del
.dn
)) {
2398 /* do not manipulate our control entries */
2399 return ldb_next_request(module
, req
);
2402 ac
= samldb_ctx_init(module
, req
);
2404 return ldb_operr(ldb_module_get_ctx(module
));
2407 ret
= samldb_prim_group_users_check(ac
);
2408 if (ret
!= LDB_SUCCESS
) {
2414 return ldb_next_request(module
, req
);
2419 static int samldb_extended_allocate_rid_pool(struct ldb_module
*module
, struct ldb_request
*req
)
2421 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
2422 struct dsdb_fsmo_extended_op
*exop
;
2425 exop
= talloc_get_type(req
->op
.extended
.data
,
2426 struct dsdb_fsmo_extended_op
);
2428 ldb_set_errstring(ldb
,
2429 "samldb_extended_allocate_rid_pool: invalid extended data");
2430 return LDB_ERR_PROTOCOL_ERROR
;
2433 ret
= ridalloc_allocate_rid_pool_fsmo(module
, exop
, req
);
2434 if (ret
!= LDB_SUCCESS
) {
2438 return ldb_module_done(req
, NULL
, NULL
, LDB_SUCCESS
);
2441 static int samldb_extended(struct ldb_module
*module
, struct ldb_request
*req
)
2443 if (strcmp(req
->op
.extended
.oid
, DSDB_EXTENDED_ALLOCATE_RID_POOL
) == 0) {
2444 return samldb_extended_allocate_rid_pool(module
, req
);
2447 return ldb_next_request(module
, req
);
2451 static const struct ldb_module_ops ldb_samldb_module_ops
= {
2454 .modify
= samldb_modify
,
2455 .del
= samldb_delete
,
2456 .extended
= samldb_extended
2460 int ldb_samldb_module_init(const char *version
)
2462 LDB_MODULE_CHECK_VERSION(version
);
2463 return ldb_register_module(&ldb_samldb_module_ops
);