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"
46 typedef int (*samldb_step_fn_t
)(struct samldb_ctx
*);
49 struct samldb_step
*next
;
54 struct ldb_module
*module
;
55 struct ldb_request
*req
;
57 /* used for add operations */
60 /* the resulting message */
61 struct ldb_message
*msg
;
63 /* used in "samldb_find_for_defaultObjectCategory" */
64 struct ldb_dn
*dn
, *res_dn
;
66 /* all the async steps necessary to complete the operation */
67 struct samldb_step
*steps
;
68 struct samldb_step
*curstep
;
70 /* If someone set an ares to forward controls and response back to the caller */
71 struct ldb_reply
*ares
;
74 static struct samldb_ctx
*samldb_ctx_init(struct ldb_module
*module
,
75 struct ldb_request
*req
)
77 struct ldb_context
*ldb
;
78 struct samldb_ctx
*ac
;
80 ldb
= ldb_module_get_ctx(module
);
82 ac
= talloc_zero(req
, struct samldb_ctx
);
94 static int samldb_add_step(struct samldb_ctx
*ac
, samldb_step_fn_t fn
)
96 struct samldb_step
*step
, *stepper
;
98 step
= talloc_zero(ac
, struct samldb_step
);
100 return ldb_oom(ldb_module_get_ctx(ac
->module
));
105 if (ac
->steps
== NULL
) {
109 if (ac
->curstep
== NULL
)
110 return ldb_operr(ldb_module_get_ctx(ac
->module
));
111 for (stepper
= ac
->curstep
; stepper
->next
!= NULL
;
112 stepper
= stepper
->next
);
113 stepper
->next
= step
;
119 static int samldb_first_step(struct samldb_ctx
*ac
)
121 if (ac
->steps
== NULL
) {
122 return ldb_operr(ldb_module_get_ctx(ac
->module
));
125 ac
->curstep
= ac
->steps
;
126 return ac
->curstep
->fn(ac
);
129 static int samldb_next_step(struct samldb_ctx
*ac
)
131 if (ac
->curstep
->next
) {
132 ac
->curstep
= ac
->curstep
->next
;
133 return ac
->curstep
->fn(ac
);
136 /* We exit the samldb module here. If someone set an "ares" to forward
137 * controls and response back to the caller, use them. */
139 return ldb_module_done(ac
->req
, ac
->ares
->controls
,
140 ac
->ares
->response
, LDB_SUCCESS
);
142 return ldb_module_done(ac
->req
, NULL
, NULL
, LDB_SUCCESS
);
147 /* sAMAccountName handling */
149 static int samldb_generate_sAMAccountName(struct ldb_context
*ldb
,
150 struct ldb_message
*msg
)
154 /* Format: $000000-000000000000 */
156 name
= talloc_asprintf(msg
, "$%.6X-%.6X%.6X",
157 (unsigned int)generate_random(),
158 (unsigned int)generate_random(),
159 (unsigned int)generate_random());
163 return ldb_msg_add_steal_string(msg
, "sAMAccountName", name
);
166 static int samldb_check_sAMAccountName(struct samldb_ctx
*ac
)
168 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
171 struct ldb_result
*res
;
172 const char *noattrs
[] = { NULL
};
174 if (ldb_msg_find_element(ac
->msg
, "sAMAccountName") == NULL
) {
175 ret
= samldb_generate_sAMAccountName(ldb
, ac
->msg
);
176 if (ret
!= LDB_SUCCESS
) {
181 name
= ldb_msg_find_attr_as_string(ac
->msg
, "sAMAccountName", NULL
);
183 /* The "sAMAccountName" cannot be nothing */
184 ldb_set_errstring(ldb
,
185 "samldb: Empty account names aren't allowed!");
186 return LDB_ERR_CONSTRAINT_VIOLATION
;
189 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
190 NULL
, LDB_SCOPE_SUBTREE
, noattrs
,
191 DSDB_FLAG_NEXT_MODULE
,
193 "(sAMAccountName=%s)",
194 ldb_binary_encode_string(ac
, name
));
195 if (ret
!= LDB_SUCCESS
) {
198 if (res
->count
!= 0) {
199 ldb_asprintf_errstring(ldb
,
200 "samldb: Account name (sAMAccountName) '%s' already in use!",
203 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
207 return samldb_next_step(ac
);
211 static bool samldb_msg_add_sid(struct ldb_message
*msg
,
213 const struct dom_sid
*sid
)
216 enum ndr_err_code ndr_err
;
218 ndr_err
= ndr_push_struct_blob(&v
, msg
, sid
,
219 (ndr_push_flags_fn_t
)ndr_push_dom_sid
);
220 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
223 return (ldb_msg_add_value(msg
, name
, &v
, NULL
) == 0);
227 /* allocate a SID using our RID Set */
228 static int samldb_allocate_sid(struct samldb_ctx
*ac
)
232 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
235 ret
= ridalloc_allocate_rid(ac
->module
, &rid
, ac
->req
);
236 if (ret
!= LDB_SUCCESS
) {
240 sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), rid
);
242 return ldb_module_oom(ac
->module
);
245 if ( ! samldb_msg_add_sid(ac
->msg
, "objectSid", sid
)) {
246 return ldb_operr(ldb
);
249 return samldb_next_step(ac
);
253 see if a krbtgt_number is available
255 static bool samldb_krbtgtnumber_available(struct samldb_ctx
*ac
,
256 uint32_t krbtgt_number
)
258 TALLOC_CTX
*tmp_ctx
= talloc_new(ac
);
259 struct ldb_result
*res
;
260 const char *no_attrs
[] = { NULL
};
263 ret
= dsdb_module_search(ac
->module
, tmp_ctx
, &res
, NULL
,
264 LDB_SCOPE_SUBTREE
, no_attrs
,
265 DSDB_FLAG_NEXT_MODULE
,
267 "(msDC-SecondaryKrbTgtNumber=%u)",
269 if (ret
== LDB_SUCCESS
&& res
->count
== 0) {
270 talloc_free(tmp_ctx
);
273 talloc_free(tmp_ctx
);
277 /* special handling for add in RODC join */
278 static int samldb_rodc_add(struct samldb_ctx
*ac
)
280 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
281 uint32_t krbtgt_number
, i_start
, i
;
284 struct ldb_val newpass_utf16
;
286 /* find a unused msDC-SecondaryKrbTgtNumber */
287 i_start
= generate_random() & 0xFFFF;
292 for (i
=i_start
; i
<=0xFFFF; i
++) {
293 if (samldb_krbtgtnumber_available(ac
, i
)) {
298 for (i
=1; i
<i_start
; i
++) {
299 if (samldb_krbtgtnumber_available(ac
, i
)) {
305 ldb_asprintf_errstring(ldb
,
306 "%08X: Unable to find available msDS-SecondaryKrbTgtNumber",
307 W_ERROR_V(WERR_NO_SYSTEM_RESOURCES
));
308 return LDB_ERR_OTHER
;
311 ret
= ldb_msg_add_empty(ac
->msg
, "msDS-SecondaryKrbTgtNumber",
312 LDB_FLAG_INTERNAL_DISABLE_VALIDATION
, NULL
);
313 if (ret
!= LDB_SUCCESS
) {
314 return ldb_operr(ldb
);
317 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
318 "msDS-SecondaryKrbTgtNumber", krbtgt_number
);
319 if (ret
!= LDB_SUCCESS
) {
320 return ldb_operr(ldb
);
323 ret
= ldb_msg_add_fmt(ac
->msg
, "sAMAccountName", "krbtgt_%u",
325 if (ret
!= LDB_SUCCESS
) {
326 return ldb_operr(ldb
);
329 newpass
= generate_random_password(ac
->msg
, 128, 255);
330 if (newpass
== NULL
) {
331 return ldb_operr(ldb
);
334 if (!convert_string_talloc(ac
,
336 newpass
, strlen(newpass
),
337 (void *)&newpass_utf16
.data
,
338 &newpass_utf16
.length
)) {
339 ldb_asprintf_errstring(ldb
,
341 "failed to generate UTF16 password from random password");
342 return LDB_ERR_OPERATIONS_ERROR
;
344 ret
= ldb_msg_add_steal_value(ac
->msg
, "clearTextPassword", &newpass_utf16
);
345 if (ret
!= LDB_SUCCESS
) {
346 return ldb_operr(ldb
);
349 return samldb_next_step(ac
);
352 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx
*ac
)
354 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
355 struct ldb_result
*res
;
356 const char *no_attrs
[] = { NULL
};
361 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
362 ac
->dn
, LDB_SCOPE_BASE
, no_attrs
,
363 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
364 | DSDB_FLAG_NEXT_MODULE
,
366 "(objectClass=classSchema)");
367 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
368 /* Don't be pricky when the DN doesn't exist if we have the */
369 /* RELAX control specified */
370 if (ldb_request_get_control(ac
->req
,
371 LDB_CONTROL_RELAX_OID
) == NULL
) {
372 ldb_set_errstring(ldb
,
373 "samldb_find_defaultObjectCategory: "
374 "Invalid DN for 'defaultObjectCategory'!");
375 return LDB_ERR_CONSTRAINT_VIOLATION
;
378 if ((ret
!= LDB_ERR_NO_SUCH_OBJECT
) && (ret
!= LDB_SUCCESS
)) {
382 if (ret
== LDB_SUCCESS
) {
383 /* ensure the defaultObjectCategory has a full GUID */
384 struct ldb_message
*m
;
385 m
= ldb_msg_new(ac
->msg
);
390 if (ldb_msg_add_string(m
, "defaultObjectCategory",
391 ldb_dn_get_extended_linearized(m
, res
->msgs
[0]->dn
, 1)) !=
395 m
->elements
[0].flags
= LDB_FLAG_MOD_REPLACE
;
397 ret
= dsdb_module_modify(ac
->module
, m
,
398 DSDB_FLAG_NEXT_MODULE
,
400 if (ret
!= LDB_SUCCESS
) {
408 return samldb_next_step(ac
);
412 * msDS-IntId attributeSchema attribute handling
413 * during LDB_ADD request processing
415 static int samldb_add_handle_msDS_IntId(struct samldb_ctx
*ac
)
420 int32_t system_flags
;
421 struct ldb_context
*ldb
;
422 struct ldb_result
*ldb_res
;
423 struct ldb_dn
*schema_dn
;
425 ldb
= ldb_module_get_ctx(ac
->module
);
426 schema_dn
= ldb_get_schema_basedn(ldb
);
428 /* replicated update should always go through */
429 if (ldb_request_get_control(ac
->req
,
430 DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
434 /* msDS-IntId is handled by system and should never be
435 * passed by clients */
436 if (ldb_msg_find_element(ac
->msg
, "msDS-IntId")) {
437 return LDB_ERR_UNWILLING_TO_PERFORM
;
440 /* do not generate msDS-IntId if Relax control is passed */
441 if (ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
)) {
445 /* check Functional Level */
446 if (dsdb_functional_level(ldb
) < DS_DOMAIN_FUNCTION_2003
) {
450 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
451 system_flags
= ldb_msg_find_attr_as_int(ac
->msg
, "systemFlags", 0);
452 if (system_flags
& SYSTEM_FLAG_SCHEMA_BASE_OBJECT
) {
456 /* Generate new value for msDs-IntId
457 * Value should be in 0x80000000..0xBFFFFFFF range */
458 msds_intid
= generate_random() % 0X3FFFFFFF;
459 msds_intid
+= 0x80000000;
461 /* probe id values until unique one is found */
464 if (msds_intid
> 0xBFFFFFFF) {
465 msds_intid
= 0x80000001;
468 ret
= dsdb_module_search(ac
->module
, ac
,
470 schema_dn
, LDB_SCOPE_ONELEVEL
, NULL
,
471 DSDB_FLAG_NEXT_MODULE
,
473 "(msDS-IntId=%d)", msds_intid
);
474 if (ret
!= LDB_SUCCESS
) {
475 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
476 __location__
": Searching for msDS-IntId=%d failed - %s\n",
479 return ldb_operr(ldb
);
481 id_exists
= (ldb_res
->count
> 0);
483 talloc_free(ldb_res
);
486 return samdb_msg_add_int(ldb
, ac
->msg
, ac
->msg
, "msDS-IntId",
492 * samldb_add_entry (async)
495 static int samldb_add_entry_callback(struct ldb_request
*req
,
496 struct ldb_reply
*ares
)
498 struct ldb_context
*ldb
;
499 struct samldb_ctx
*ac
;
502 ac
= talloc_get_type(req
->context
, struct samldb_ctx
);
503 ldb
= ldb_module_get_ctx(ac
->module
);
506 return ldb_module_done(ac
->req
, NULL
, NULL
,
507 LDB_ERR_OPERATIONS_ERROR
);
510 if (ares
->type
== LDB_REPLY_REFERRAL
) {
511 return ldb_module_send_referral(ac
->req
, ares
->referral
);
514 if (ares
->error
!= LDB_SUCCESS
) {
515 return ldb_module_done(ac
->req
, ares
->controls
,
516 ares
->response
, ares
->error
);
518 if (ares
->type
!= LDB_REPLY_DONE
) {
519 ldb_set_errstring(ldb
,
520 "Invalid reply type!\n");
521 return ldb_module_done(ac
->req
, NULL
, NULL
,
522 LDB_ERR_OPERATIONS_ERROR
);
525 /* The caller may wish to get controls back from the add */
526 ac
->ares
= talloc_steal(ac
, ares
);
528 ret
= samldb_next_step(ac
);
529 if (ret
!= LDB_SUCCESS
) {
530 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
535 static int samldb_add_entry(struct samldb_ctx
*ac
)
537 struct ldb_context
*ldb
;
538 struct ldb_request
*req
;
541 ldb
= ldb_module_get_ctx(ac
->module
);
543 ret
= ldb_build_add_req(&req
, ldb
, ac
,
546 ac
, samldb_add_entry_callback
,
548 LDB_REQ_SET_LOCATION(req
);
549 if (ret
!= LDB_SUCCESS
) {
553 return ldb_next_request(ac
->module
, req
);
557 * return true if msg carries an attributeSchema that is intended to be RODC
558 * filtered but is also a system-critical attribute.
560 static bool check_rodc_critical_attribute(struct ldb_message
*msg
)
562 uint32_t schemaFlagsEx
, searchFlags
, rodc_filtered_flags
;
564 schemaFlagsEx
= ldb_msg_find_attr_as_uint(msg
, "schemaFlagsEx", 0);
565 searchFlags
= ldb_msg_find_attr_as_uint(msg
, "searchFlags", 0);
566 rodc_filtered_flags
= (SEARCH_FLAG_RODC_ATTRIBUTE
567 | SEARCH_FLAG_CONFIDENTIAL
);
569 if ((schemaFlagsEx
& SCHEMA_FLAG_ATTR_IS_CRITICAL
) &&
570 ((searchFlags
& rodc_filtered_flags
) == rodc_filtered_flags
)) {
578 static int samldb_fill_object(struct samldb_ctx
*ac
)
580 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
583 /* Add information for the different account types */
584 if (strcmp(ac
->type
, "user") == 0) {
585 struct ldb_control
*rodc_control
= ldb_request_get_control(ac
->req
,
586 LDB_CONTROL_RODC_DCPROMO_OID
);
587 if (rodc_control
!= NULL
) {
588 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
589 rodc_control
->critical
= false;
590 ret
= samldb_add_step(ac
, samldb_rodc_add
);
591 if (ret
!= LDB_SUCCESS
) return ret
;
594 /* check if we have a valid sAMAccountName */
595 ret
= samldb_add_step(ac
, samldb_check_sAMAccountName
);
596 if (ret
!= LDB_SUCCESS
) return ret
;
598 ret
= samldb_add_step(ac
, samldb_add_entry
);
599 if (ret
!= LDB_SUCCESS
) return ret
;
601 } else if (strcmp(ac
->type
, "group") == 0) {
602 /* check if we have a valid sAMAccountName */
603 ret
= samldb_add_step(ac
, samldb_check_sAMAccountName
);
604 if (ret
!= LDB_SUCCESS
) return ret
;
606 ret
= samldb_add_step(ac
, samldb_add_entry
);
607 if (ret
!= LDB_SUCCESS
) return ret
;
609 } else if (strcmp(ac
->type
, "classSchema") == 0) {
610 const struct ldb_val
*rdn_value
, *def_obj_cat_val
;
612 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
614 if (ret
!= LDB_SUCCESS
) return ret
;
616 /* do not allow to mark an attributeSchema as RODC filtered if it
617 * is system-critical */
618 if (check_rodc_critical_attribute(ac
->msg
)) {
619 ldb_asprintf_errstring(ldb
, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
620 ldb_dn_get_linearized(ac
->msg
->dn
));
621 return LDB_ERR_UNWILLING_TO_PERFORM
;
624 rdn_value
= ldb_dn_get_rdn_val(ac
->msg
->dn
);
625 if (rdn_value
== NULL
) {
626 return ldb_operr(ldb
);
628 if (!ldb_msg_find_element(ac
->msg
, "lDAPDisplayName")) {
629 /* the RDN has prefix "CN" */
630 ret
= ldb_msg_add_string(ac
->msg
, "lDAPDisplayName",
631 samdb_cn_to_lDAPDisplayName(ac
->msg
,
632 (const char *) rdn_value
->data
));
633 if (ret
!= LDB_SUCCESS
) {
639 if (!ldb_msg_find_element(ac
->msg
, "schemaIDGUID")) {
642 guid
= GUID_random();
643 ret
= dsdb_msg_add_guid(ac
->msg
, &guid
, "schemaIDGUID");
644 if (ret
!= LDB_SUCCESS
) {
650 def_obj_cat_val
= ldb_msg_find_ldb_val(ac
->msg
,
651 "defaultObjectCategory");
652 if (def_obj_cat_val
!= NULL
) {
653 /* "defaultObjectCategory" has been set by the caller.
654 * Do some checks for consistency.
655 * NOTE: The real constraint check (that
656 * 'defaultObjectCategory' is the DN of the new
657 * objectclass or any parent of it) is still incomplete.
658 * For now we say that 'defaultObjectCategory' is valid
659 * if it exists and it is of objectclass "classSchema".
661 ac
->dn
= ldb_dn_from_ldb_val(ac
, ldb
, def_obj_cat_val
);
662 if (ac
->dn
== NULL
) {
663 ldb_set_errstring(ldb
,
664 "Invalid DN for 'defaultObjectCategory'!");
665 return LDB_ERR_CONSTRAINT_VIOLATION
;
668 /* "defaultObjectCategory" has not been set by the
669 * caller. Use the entry DN for it. */
670 ac
->dn
= ac
->msg
->dn
;
672 ret
= ldb_msg_add_string(ac
->msg
, "defaultObjectCategory",
673 ldb_dn_alloc_linearized(ac
->msg
, ac
->dn
));
674 if (ret
!= LDB_SUCCESS
) {
680 ret
= samldb_add_step(ac
, samldb_add_entry
);
681 if (ret
!= LDB_SUCCESS
) return ret
;
683 /* Now perform the checks for the 'defaultObjectCategory'. The
684 * lookup DN was already saved in "ac->dn" */
685 ret
= samldb_add_step(ac
, samldb_find_for_defaultObjectCategory
);
686 if (ret
!= LDB_SUCCESS
) return ret
;
688 } else if (strcmp(ac
->type
, "attributeSchema") == 0) {
689 const struct ldb_val
*rdn_value
;
690 rdn_value
= ldb_dn_get_rdn_val(ac
->msg
->dn
);
691 if (rdn_value
== NULL
) {
692 return ldb_operr(ldb
);
694 if (!ldb_msg_find_element(ac
->msg
, "lDAPDisplayName")) {
695 /* the RDN has prefix "CN" */
696 ret
= ldb_msg_add_string(ac
->msg
, "lDAPDisplayName",
697 samdb_cn_to_lDAPDisplayName(ac
->msg
,
698 (const char *) rdn_value
->data
));
699 if (ret
!= LDB_SUCCESS
) {
705 /* do not allow to mark an attributeSchema as RODC filtered if it
706 * is system-critical */
707 if (check_rodc_critical_attribute(ac
->msg
)) {
708 ldb_asprintf_errstring(ldb
,
709 "samldb: refusing schema add of %s - cannot combine critical attribute with RODC filtering",
710 ldb_dn_get_linearized(ac
->msg
->dn
));
711 return LDB_ERR_UNWILLING_TO_PERFORM
;
714 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
715 "isSingleValued", "FALSE");
716 if (ret
!= LDB_SUCCESS
) return ret
;
718 if (!ldb_msg_find_element(ac
->msg
, "schemaIDGUID")) {
721 guid
= GUID_random();
722 ret
= dsdb_msg_add_guid(ac
->msg
, &guid
, "schemaIDGUID");
723 if (ret
!= LDB_SUCCESS
) {
729 /* handle msDS-IntID attribute */
730 ret
= samldb_add_handle_msDS_IntId(ac
);
731 if (ret
!= LDB_SUCCESS
) return ret
;
733 ret
= samldb_add_step(ac
, samldb_add_entry
);
734 if (ret
!= LDB_SUCCESS
) return ret
;
737 ldb_asprintf_errstring(ldb
,
738 "Invalid entry type!");
739 return LDB_ERR_OPERATIONS_ERROR
;
742 return samldb_first_step(ac
);
745 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx
*ac
)
747 struct ldb_context
*ldb
;
748 const struct ldb_val
*rdn_value
;
752 ldb
= ldb_module_get_ctx(ac
->module
);
754 sid
= samdb_result_dom_sid(ac
->msg
, ac
->msg
, "objectSid");
756 rdn_value
= ldb_dn_get_rdn_val(ac
->msg
->dn
);
757 if (rdn_value
== NULL
) {
758 return ldb_operr(ldb
);
760 sid
= dom_sid_parse_talloc(ac
->msg
,
761 (const char *)rdn_value
->data
);
763 ldb_set_errstring(ldb
,
764 "samldb: No valid SID found in ForeignSecurityPrincipal CN!");
765 return LDB_ERR_CONSTRAINT_VIOLATION
;
767 if (! samldb_msg_add_sid(ac
->msg
, "objectSid", sid
)) {
768 return ldb_operr(ldb
);
772 /* finally proceed with adding the entry */
773 ret
= samldb_add_step(ac
, samldb_add_entry
);
774 if (ret
!= LDB_SUCCESS
) return ret
;
776 return samldb_first_step(ac
);
779 static int samldb_schema_info_update(struct samldb_ctx
*ac
)
782 struct ldb_context
*ldb
;
783 struct dsdb_schema
*schema
;
785 /* replicated update should always go through */
786 if (ldb_request_get_control(ac
->req
,
787 DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
791 /* do not update schemaInfo during provisioning */
792 if (ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
)) {
796 ldb
= ldb_module_get_ctx(ac
->module
);
797 schema
= dsdb_get_schema(ldb
, NULL
);
799 ldb_debug_set(ldb
, LDB_DEBUG_FATAL
,
800 "samldb_schema_info_update: no dsdb_schema loaded");
801 DEBUG(0,(__location__
": %s\n", ldb_errstring(ldb
)));
802 return ldb_operr(ldb
);
805 ret
= dsdb_module_schema_info_update(ac
->module
, schema
,
806 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
807 if (ret
!= LDB_SUCCESS
) {
808 ldb_asprintf_errstring(ldb
,
809 "samldb_schema_info_update: dsdb_module_schema_info_update failed with %s",
817 static int samldb_prim_group_tester(struct samldb_ctx
*ac
, uint32_t rid
);
820 * "Objectclass" trigger (MS-SAMR 3.1.1.8.1)
822 * Has to be invoked on "add" and "modify" operations on "user", "computer" and
824 * ac->msg contains the "add"/"modify" message
825 * ac->type contains the object type (main objectclass)
827 static int samldb_objectclass_trigger(struct samldb_ctx
*ac
)
829 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
830 void *skip_allocate_sids
= ldb_get_opaque(ldb
,
831 "skip_allocate_sids");
832 struct ldb_message_element
*el
, *el2
;
836 /* make sure that "sAMAccountType" is not specified */
837 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
839 ldb_set_errstring(ldb
,
840 "samldb: sAMAccountType must not be specified!");
841 return LDB_ERR_UNWILLING_TO_PERFORM
;
844 /* Step 1: objectSid assignment */
846 /* Don't allow the objectSid to be changed. But beside the RELAX
847 * control we have also to guarantee that it can always be set with
848 * SYSTEM permissions. This is needed for the "samba3sam" backend. */
849 sid
= samdb_result_dom_sid(ac
, ac
->msg
, "objectSid");
850 if ((sid
!= NULL
) && (!dsdb_module_am_system(ac
->module
)) &&
851 (ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
) == NULL
)) {
852 ldb_set_errstring(ldb
,
853 "samldb: objectSid must not be specified!");
854 return LDB_ERR_UNWILLING_TO_PERFORM
;
857 /* but generate a new SID when we do have an add operations */
858 if ((sid
== NULL
) && (ac
->req
->operation
== LDB_ADD
) && !skip_allocate_sids
) {
859 ret
= samldb_add_step(ac
, samldb_allocate_sid
);
860 if (ret
!= LDB_SUCCESS
) return ret
;
863 if (strcmp(ac
->type
, "user") == 0) {
864 bool uac_generated
= false;
866 /* Step 1.2: Default values */
867 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
868 "accountExpires", "9223372036854775807");
869 if (ret
!= LDB_SUCCESS
) return ret
;
870 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
871 "badPasswordTime", "0");
872 if (ret
!= LDB_SUCCESS
) return ret
;
873 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
875 if (ret
!= LDB_SUCCESS
) return ret
;
876 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
878 if (ret
!= LDB_SUCCESS
) return ret
;
879 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
881 if (ret
!= LDB_SUCCESS
) return ret
;
882 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
884 if (ret
!= LDB_SUCCESS
) return ret
;
885 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
887 if (ret
!= LDB_SUCCESS
) return ret
;
888 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
890 if (ret
!= LDB_SUCCESS
) return ret
;
891 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
893 if (ret
!= LDB_SUCCESS
) return ret
;
895 /* On add operations we might need to generate a
896 * "userAccountControl" (if it isn't specified). */
897 el
= ldb_msg_find_element(ac
->msg
, "userAccountControl");
898 if ((el
== NULL
) && (ac
->req
->operation
== LDB_ADD
)) {
899 ret
= samdb_msg_set_uint(ldb
, ac
->msg
, ac
->msg
,
900 "userAccountControl",
902 if (ret
!= LDB_SUCCESS
) {
905 uac_generated
= true;
908 el
= ldb_msg_find_element(ac
->msg
, "userAccountControl");
910 uint32_t user_account_control
, account_type
;
912 /* Step 1.3: "userAccountControl" -> "sAMAccountType" mapping */
913 user_account_control
= ldb_msg_find_attr_as_uint(ac
->msg
,
914 "userAccountControl",
917 /* Temporary duplicate accounts aren't allowed */
918 if ((user_account_control
& UF_TEMP_DUPLICATE_ACCOUNT
) != 0) {
919 return LDB_ERR_OTHER
;
922 /* Workstation and (read-only) DC objects do need objectclass "computer" */
923 if ((samdb_find_attribute(ldb
, ac
->msg
,
924 "objectclass", "computer") == NULL
) &&
925 (user_account_control
&
926 (UF_SERVER_TRUST_ACCOUNT
| UF_WORKSTATION_TRUST_ACCOUNT
))) {
927 ldb_set_errstring(ldb
,
928 "samldb: Requested account type does need objectclass 'computer'!");
929 return LDB_ERR_OBJECT_CLASS_VIOLATION
;
932 account_type
= ds_uf2atype(user_account_control
);
933 if (account_type
== 0) {
934 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
935 return LDB_ERR_UNWILLING_TO_PERFORM
;
937 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
940 if (ret
!= LDB_SUCCESS
) {
943 el2
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
944 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
946 /* "isCriticalSystemObject" might be set */
947 if (user_account_control
&
948 (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
)) {
949 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
951 if (ret
!= LDB_SUCCESS
) {
954 el2
= ldb_msg_find_element(ac
->msg
,
955 "isCriticalSystemObject");
956 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
957 } else if (user_account_control
& UF_WORKSTATION_TRUST_ACCOUNT
) {
958 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
960 if (ret
!= LDB_SUCCESS
) {
963 el2
= ldb_msg_find_element(ac
->msg
,
964 "isCriticalSystemObject");
965 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
968 /* Step 1.4: "userAccountControl" -> "primaryGroupID" mapping */
969 if (!ldb_msg_find_element(ac
->msg
, "primaryGroupID")) {
970 uint32_t rid
= ds_uf2prim_group_rid(user_account_control
);
973 * Older AD deployments don't know about the
976 if (rid
== DOMAIN_RID_READONLY_DCS
) {
977 ret
= samldb_prim_group_tester(ac
, rid
);
978 if (ret
!= LDB_SUCCESS
) {
983 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
984 "primaryGroupID", rid
);
985 if (ret
!= LDB_SUCCESS
) {
988 el2
= ldb_msg_find_element(ac
->msg
,
990 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
993 /* Step 1.5: Add additional flags when needed */
994 /* Obviously this is done when the "userAccountControl"
995 * has been generated here (tested against Windows
998 user_account_control
|= UF_ACCOUNTDISABLE
;
999 user_account_control
|= UF_PASSWD_NOTREQD
;
1001 ret
= samdb_msg_set_uint(ldb
, ac
->msg
, ac
->msg
,
1002 "userAccountControl",
1003 user_account_control
);
1004 if (ret
!= LDB_SUCCESS
) {
1010 } else if (strcmp(ac
->type
, "group") == 0) {
1011 const char *tempstr
;
1013 /* Step 2.2: Default values */
1014 tempstr
= talloc_asprintf(ac
->msg
, "%d",
1015 GTYPE_SECURITY_GLOBAL_GROUP
);
1016 if (tempstr
== NULL
) return ldb_operr(ldb
);
1017 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1018 "groupType", tempstr
);
1019 if (ret
!= LDB_SUCCESS
) return ret
;
1021 /* Step 2.3: "groupType" -> "sAMAccountType" */
1022 el
= ldb_msg_find_element(ac
->msg
, "groupType");
1024 uint32_t group_type
, account_type
;
1026 group_type
= ldb_msg_find_attr_as_uint(ac
->msg
,
1029 /* The creation of builtin groups requires the
1031 if (group_type
== GTYPE_SECURITY_BUILTIN_LOCAL_GROUP
) {
1032 if (ldb_request_get_control(ac
->req
,
1033 LDB_CONTROL_RELAX_OID
) == NULL
) {
1034 return LDB_ERR_UNWILLING_TO_PERFORM
;
1038 account_type
= ds_gtype2atype(group_type
);
1039 if (account_type
== 0) {
1040 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
1041 return LDB_ERR_UNWILLING_TO_PERFORM
;
1043 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1046 if (ret
!= LDB_SUCCESS
) {
1049 el2
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
1050 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1058 * "Primary group ID" trigger (MS-SAMR 3.1.1.8.2)
1060 * Has to be invoked on "add" and "modify" operations on "user" and "computer"
1062 * ac->msg contains the "add"/"modify" message
1065 static int samldb_prim_group_tester(struct samldb_ctx
*ac
, uint32_t rid
)
1067 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1068 struct dom_sid
*sid
;
1069 struct ldb_result
*res
;
1071 const char *noattrs
[] = { NULL
};
1073 sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), rid
);
1075 return ldb_operr(ldb
);
1078 ret
= dsdb_module_search(ac
->module
, ac
, &res
, NULL
, LDB_SCOPE_SUBTREE
,
1079 noattrs
, DSDB_FLAG_NEXT_MODULE
,
1082 ldap_encode_ndr_dom_sid(ac
, sid
));
1083 if (ret
!= LDB_SUCCESS
) {
1086 if (res
->count
!= 1) {
1088 ldb_asprintf_errstring(ldb
,
1089 "Failed to find primary group with RID %u!",
1091 return LDB_ERR_UNWILLING_TO_PERFORM
;
1098 static int samldb_prim_group_set(struct samldb_ctx
*ac
)
1100 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1103 rid
= ldb_msg_find_attr_as_uint(ac
->msg
, "primaryGroupID", (uint32_t) -1);
1104 if (rid
== (uint32_t) -1) {
1105 /* we aren't affected of any primary group set */
1108 } else if (!ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
)) {
1109 ldb_set_errstring(ldb
,
1110 "The primary group isn't settable on add operations!");
1111 return LDB_ERR_UNWILLING_TO_PERFORM
;
1114 return samldb_prim_group_tester(ac
, rid
);
1117 static int samldb_prim_group_change(struct samldb_ctx
*ac
)
1119 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1120 const char * attrs
[] = { "primaryGroupID", "memberOf", NULL
};
1121 struct ldb_result
*res
, *group_res
;
1122 struct ldb_message_element
*el
;
1123 struct ldb_message
*msg
;
1124 uint32_t prev_rid
, new_rid
;
1125 struct dom_sid
*prev_sid
, *new_sid
;
1126 struct ldb_dn
*prev_prim_group_dn
, *new_prim_group_dn
;
1128 const char *noattrs
[] = { NULL
};
1130 el
= dsdb_get_single_valued_attr(ac
->msg
, "primaryGroupID",
1131 ac
->req
->operation
);
1133 /* we are not affected */
1137 /* Fetch information from the existing object */
1139 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, attrs
,
1140 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1141 if (ret
!= LDB_SUCCESS
) {
1145 /* Finds out the DN of the old primary group */
1147 prev_rid
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "primaryGroupID",
1149 if (prev_rid
== (uint32_t) -1) {
1150 /* User objects do always have a mandatory "primaryGroupID"
1151 * attribute. If this doesn't exist then the object is of the
1152 * wrong type. This is the exact Windows error code */
1153 return LDB_ERR_OBJECT_CLASS_VIOLATION
;
1156 prev_sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), prev_rid
);
1157 if (prev_sid
== NULL
) {
1158 return ldb_operr(ldb
);
1161 /* Finds out the DN of the new primary group
1162 * Notice: in order to parse the primary group ID correctly we create
1163 * a temporary message here. */
1165 msg
= ldb_msg_new(ac
->msg
);
1167 return ldb_module_oom(ac
->module
);
1169 ret
= ldb_msg_add(msg
, el
, 0);
1170 if (ret
!= LDB_SUCCESS
) {
1173 new_rid
= ldb_msg_find_attr_as_uint(msg
, "primaryGroupID", (uint32_t) -1);
1175 if (new_rid
== (uint32_t) -1) {
1176 /* we aren't affected of any primary group change */
1180 if (prev_rid
== new_rid
) {
1184 ret
= dsdb_module_search(ac
->module
, ac
, &group_res
, NULL
, LDB_SCOPE_SUBTREE
,
1185 noattrs
, DSDB_FLAG_NEXT_MODULE
,
1188 ldap_encode_ndr_dom_sid(ac
, prev_sid
));
1189 if (ret
!= LDB_SUCCESS
) {
1192 if (group_res
->count
!= 1) {
1193 return ldb_operr(ldb
);
1195 prev_prim_group_dn
= group_res
->msgs
[0]->dn
;
1197 new_sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), new_rid
);
1198 if (new_sid
== NULL
) {
1199 return ldb_operr(ldb
);
1202 ret
= dsdb_module_search(ac
->module
, ac
, &group_res
, NULL
, LDB_SCOPE_SUBTREE
,
1203 noattrs
, DSDB_FLAG_NEXT_MODULE
,
1206 ldap_encode_ndr_dom_sid(ac
, new_sid
));
1207 if (ret
!= LDB_SUCCESS
) {
1210 if (group_res
->count
!= 1) {
1211 /* Here we know if the specified new primary group candidate is
1213 return LDB_ERR_UNWILLING_TO_PERFORM
;
1215 new_prim_group_dn
= group_res
->msgs
[0]->dn
;
1217 /* We need to be already a normal member of the new primary
1218 * group in order to be successful. */
1219 el
= samdb_find_attribute(ldb
, res
->msgs
[0], "memberOf",
1220 ldb_dn_get_linearized(new_prim_group_dn
));
1222 return LDB_ERR_UNWILLING_TO_PERFORM
;
1225 /* Remove the "member" attribute on the new primary group */
1226 msg
= ldb_msg_new(ac
->msg
);
1228 return ldb_module_oom(ac
->module
);
1230 msg
->dn
= new_prim_group_dn
;
1232 ret
= samdb_msg_add_delval(ldb
, msg
, msg
, "member",
1233 ldb_dn_get_linearized(ac
->msg
->dn
));
1234 if (ret
!= LDB_SUCCESS
) {
1238 ret
= dsdb_module_modify(ac
->module
, msg
, DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1239 if (ret
!= LDB_SUCCESS
) {
1244 /* Add a "member" attribute for the previous primary group */
1245 msg
= ldb_msg_new(ac
->msg
);
1247 return ldb_module_oom(ac
->module
);
1249 msg
->dn
= prev_prim_group_dn
;
1251 ret
= samdb_msg_add_addval(ldb
, msg
, msg
, "member",
1252 ldb_dn_get_linearized(ac
->msg
->dn
));
1253 if (ret
!= LDB_SUCCESS
) {
1257 ret
= dsdb_module_modify(ac
->module
, msg
, DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1258 if (ret
!= LDB_SUCCESS
) {
1266 static int samldb_prim_group_trigger(struct samldb_ctx
*ac
)
1270 if (ac
->req
->operation
== LDB_ADD
) {
1271 ret
= samldb_prim_group_set(ac
);
1273 ret
= samldb_prim_group_change(ac
);
1281 * This function is called on LDB modify operations. It performs some additions/
1282 * replaces on the current LDB message when "userAccountControl" changes.
1284 static int samldb_user_account_control_change(struct samldb_ctx
*ac
)
1286 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1287 uint32_t user_account_control
, old_user_account_control
, account_type
;
1288 struct ldb_message_element
*el
;
1289 struct ldb_message
*tmp_msg
;
1291 struct ldb_result
*res
;
1292 const char *attrs
[] = { "userAccountControl", "objectClass", NULL
};
1294 bool is_computer
= false;
1296 el
= dsdb_get_single_valued_attr(ac
->msg
, "userAccountControl",
1297 ac
->req
->operation
);
1299 /* we are not affected */
1303 /* Create a temporary message for fetching the "userAccountControl" */
1304 tmp_msg
= ldb_msg_new(ac
->msg
);
1305 if (tmp_msg
== NULL
) {
1306 return ldb_module_oom(ac
->module
);
1308 ret
= ldb_msg_add(tmp_msg
, el
, 0);
1309 if (ret
!= LDB_SUCCESS
) {
1312 user_account_control
= ldb_msg_find_attr_as_uint(tmp_msg
,
1313 "userAccountControl",
1315 talloc_free(tmp_msg
);
1317 /* Temporary duplicate accounts aren't allowed */
1318 if ((user_account_control
& UF_TEMP_DUPLICATE_ACCOUNT
) != 0) {
1319 return LDB_ERR_OTHER
;
1322 /* Fetch the old "userAccountControl" and "objectClass" */
1323 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, attrs
,
1324 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1325 if (ret
!= LDB_SUCCESS
) {
1328 old_user_account_control
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "userAccountControl", 0);
1329 if (old_user_account_control
== 0) {
1330 return ldb_operr(ldb
);
1332 el
= ldb_msg_find_element(res
->msgs
[0], "objectClass");
1334 return ldb_operr(ldb
);
1337 /* When we do not have objectclass "computer" we cannot switch to a (read-only) DC */
1338 for (i
= 0; i
< el
->num_values
; i
++) {
1339 if (ldb_attr_cmp((char *)el
->values
[i
].data
, "computer") == 0) {
1345 (user_account_control
& (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
))) {
1346 ldb_set_errstring(ldb
,
1347 "samldb: Requested account type does need objectclass 'computer'!");
1348 return LDB_ERR_UNWILLING_TO_PERFORM
;
1352 * The functions "ds_uf2atype" and "ds_uf2prim_group_rid" are used as
1353 * detectors for account type changes.
1354 * So if the account type does change then we need to adjust the
1355 * "sAMAccountType", the "isCriticalSystemObject" and the
1356 * "primaryGroupID" attribute.
1358 if ((ds_uf2atype(user_account_control
)
1359 == ds_uf2atype(old_user_account_control
)) &&
1360 (ds_uf2prim_group_rid(user_account_control
)
1361 == ds_uf2prim_group_rid(old_user_account_control
))) {
1365 account_type
= ds_uf2atype(user_account_control
);
1366 if (account_type
== 0) {
1367 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
1368 return LDB_ERR_UNWILLING_TO_PERFORM
;
1370 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
, "sAMAccountType",
1372 if (ret
!= LDB_SUCCESS
) {
1375 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
1376 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1378 /* "isCriticalSystemObject" might be set/changed */
1379 if (user_account_control
1380 & (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
)) {
1381 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
1383 if (ret
!= LDB_SUCCESS
) {
1386 el
= ldb_msg_find_element(ac
->msg
,
1387 "isCriticalSystemObject");
1388 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1389 } else if (user_account_control
& UF_WORKSTATION_TRUST_ACCOUNT
) {
1390 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
1392 if (ret
!= LDB_SUCCESS
) {
1395 el
= ldb_msg_find_element(ac
->msg
,
1396 "isCriticalSystemObject");
1397 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1400 if (!ldb_msg_find_element(ac
->msg
, "primaryGroupID")) {
1401 uint32_t rid
= ds_uf2prim_group_rid(user_account_control
);
1403 /* Older AD deployments don't know about the RODC group */
1404 if (rid
== DOMAIN_RID_READONLY_DCS
) {
1405 ret
= samldb_prim_group_tester(ac
, rid
);
1406 if (ret
!= LDB_SUCCESS
) {
1411 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1412 "primaryGroupID", rid
);
1413 if (ret
!= LDB_SUCCESS
) {
1416 el
= ldb_msg_find_element(ac
->msg
,
1418 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1424 static int samldb_group_type_change(struct samldb_ctx
*ac
)
1426 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1427 uint32_t group_type
, old_group_type
, account_type
;
1428 struct ldb_message_element
*el
;
1429 struct ldb_message
*tmp_msg
;
1431 struct ldb_result
*res
;
1432 const char *attrs
[] = { "groupType", NULL
};
1434 el
= dsdb_get_single_valued_attr(ac
->msg
, "groupType",
1435 ac
->req
->operation
);
1437 /* we are not affected */
1441 /* Create a temporary message for fetching the "groupType" */
1442 tmp_msg
= ldb_msg_new(ac
->msg
);
1443 if (tmp_msg
== NULL
) {
1444 return ldb_module_oom(ac
->module
);
1446 ret
= ldb_msg_add(tmp_msg
, el
, 0);
1447 if (ret
!= LDB_SUCCESS
) {
1450 group_type
= ldb_msg_find_attr_as_uint(tmp_msg
, "groupType", 0);
1451 talloc_free(tmp_msg
);
1453 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, attrs
,
1454 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1455 if (ret
!= LDB_SUCCESS
) {
1458 old_group_type
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "groupType", 0);
1459 if (old_group_type
== 0) {
1460 return ldb_operr(ldb
);
1463 /* Group type switching isn't so easy as it seems: We can only
1464 * change in this directions: global <-> universal <-> local
1465 * On each step also the group type itself
1466 * (security/distribution) is variable. */
1468 if (ldb_request_get_control(ac
->req
, LDB_CONTROL_PROVISION_OID
) == NULL
) {
1469 switch (group_type
) {
1470 case GTYPE_SECURITY_GLOBAL_GROUP
:
1471 case GTYPE_DISTRIBUTION_GLOBAL_GROUP
:
1472 /* change to "universal" allowed */
1473 if ((old_group_type
== GTYPE_SECURITY_DOMAIN_LOCAL_GROUP
) ||
1474 (old_group_type
== GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP
)) {
1475 ldb_set_errstring(ldb
,
1476 "samldb: Change from security/distribution local group forbidden!");
1477 return LDB_ERR_UNWILLING_TO_PERFORM
;
1481 case GTYPE_SECURITY_UNIVERSAL_GROUP
:
1482 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP
:
1483 /* each change allowed */
1485 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP
:
1486 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP
:
1487 /* change to "universal" allowed */
1488 if ((old_group_type
== GTYPE_SECURITY_GLOBAL_GROUP
) ||
1489 (old_group_type
== GTYPE_DISTRIBUTION_GLOBAL_GROUP
)) {
1490 ldb_set_errstring(ldb
,
1491 "samldb: Change from security/distribution global group forbidden!");
1492 return LDB_ERR_UNWILLING_TO_PERFORM
;
1496 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP
:
1498 /* we don't allow this "groupType" values */
1499 return LDB_ERR_UNWILLING_TO_PERFORM
;
1504 account_type
= ds_gtype2atype(group_type
);
1505 if (account_type
== 0) {
1506 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
1507 return LDB_ERR_UNWILLING_TO_PERFORM
;
1509 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
, "sAMAccountType",
1511 if (ret
!= LDB_SUCCESS
) {
1514 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
1515 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1520 static int samldb_sam_accountname_check(struct samldb_ctx
*ac
)
1522 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1523 const char *no_attrs
[] = { NULL
};
1524 struct ldb_result
*res
;
1525 const char *sam_accountname
, *enc_str
;
1526 struct ldb_message_element
*el
;
1527 struct ldb_message
*tmp_msg
;
1530 el
= dsdb_get_single_valued_attr(ac
->msg
, "sAMAccountName",
1531 ac
->req
->operation
);
1533 /* we are not affected */
1537 /* Create a temporary message for fetching the "sAMAccountName" */
1538 tmp_msg
= ldb_msg_new(ac
->msg
);
1539 if (tmp_msg
== NULL
) {
1540 return ldb_module_oom(ac
->module
);
1542 ret
= ldb_msg_add(tmp_msg
, el
, 0);
1543 if (ret
!= LDB_SUCCESS
) {
1546 sam_accountname
= talloc_steal(ac
,
1547 ldb_msg_find_attr_as_string(tmp_msg
, "sAMAccountName", NULL
));
1548 talloc_free(tmp_msg
);
1550 if (sam_accountname
== NULL
) {
1551 /* The "sAMAccountName" cannot be nothing */
1552 ldb_set_errstring(ldb
,
1553 "samldb: Empty account names aren't allowed!");
1554 return LDB_ERR_UNWILLING_TO_PERFORM
;
1557 enc_str
= ldb_binary_encode_string(ac
, sam_accountname
);
1558 if (enc_str
== NULL
) {
1559 return ldb_module_oom(ac
->module
);
1562 /* Make sure that a "sAMAccountName" is only used once */
1564 ret
= dsdb_module_search(ac
->module
, ac
, &res
, NULL
, LDB_SCOPE_SUBTREE
, no_attrs
,
1565 DSDB_FLAG_NEXT_MODULE
, ac
->req
,
1566 "(sAMAccountName=%s)", enc_str
);
1567 if (ret
!= LDB_SUCCESS
) {
1570 if (res
->count
> 1) {
1571 return ldb_operr(ldb
);
1572 } else if (res
->count
== 1) {
1573 if (ldb_dn_compare(res
->msgs
[0]->dn
, ac
->msg
->dn
) != 0) {
1574 ldb_asprintf_errstring(ldb
,
1575 "samldb: Account name (sAMAccountName) '%s' already in use!",
1577 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
1585 static int samldb_member_check(struct samldb_ctx
*ac
)
1587 static const char * const attrs
[] = { "objectSid", "member", NULL
};
1588 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1589 struct ldb_message_element
*el
;
1590 struct ldb_dn
*member_dn
;
1591 struct dom_sid
*sid
;
1592 struct ldb_result
*res
;
1593 struct dom_sid
*group_sid
;
1597 /* Fetch information from the existing object */
1599 ret
= dsdb_module_search(ac
->module
, ac
, &res
, ac
->msg
->dn
, LDB_SCOPE_BASE
, attrs
,
1600 DSDB_FLAG_NEXT_MODULE
, ac
->req
, NULL
);
1601 if (ret
!= LDB_SUCCESS
) {
1604 if (res
->count
!= 1) {
1605 return ldb_operr(ldb
);
1608 group_sid
= samdb_result_dom_sid(res
, res
->msgs
[0], "objectSid");
1609 if (group_sid
== NULL
) {
1610 return ldb_operr(ldb
);
1613 /* We've to walk over all modification entries and consider the "member"
1615 for (i
= 0; i
< ac
->msg
->num_elements
; i
++) {
1616 if (ldb_attr_cmp(ac
->msg
->elements
[i
].name
, "member") != 0) {
1620 el
= &ac
->msg
->elements
[i
];
1621 for (j
= 0; j
< el
->num_values
; j
++) {
1622 struct ldb_result
*group_res
;
1623 const char *group_attrs
[] = { "primaryGroupID" , NULL
};
1624 uint32_t prim_group_rid
;
1626 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_DELETE
) {
1627 /* Deletes will be handled in
1628 * repl_meta_data, and deletes not
1629 * matching a member will return
1630 * LDB_ERR_UNWILLING_TO_PERFORM
1635 member_dn
= ldb_dn_from_ldb_val(ac
, ldb
,
1637 if (!ldb_dn_validate(member_dn
)) {
1638 return ldb_operr(ldb
);
1641 /* Denies to add "member"s to groups which are primary
1642 * ones for them - in this case return
1643 * ERR_ENTRY_ALREADY_EXISTS. */
1645 ret
= dsdb_module_search_dn(ac
->module
, ac
, &group_res
,
1646 member_dn
, group_attrs
,
1647 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1648 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
1649 /* member DN doesn't exist yet */
1652 if (ret
!= LDB_SUCCESS
) {
1655 prim_group_rid
= ldb_msg_find_attr_as_uint(group_res
->msgs
[0], "primaryGroupID", (uint32_t)-1);
1656 if (prim_group_rid
== (uint32_t) -1) {
1657 /* the member hasn't to be a user account ->
1658 * therefore no check needed in this case. */
1662 sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
),
1665 return ldb_operr(ldb
);
1668 if (dom_sid_equal(group_sid
, sid
)) {
1669 ldb_asprintf_errstring(ldb
,
1670 "samldb: member %s already set via primaryGroupID %u",
1671 ldb_dn_get_linearized(member_dn
), prim_group_rid
);
1672 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
1682 /* SAM objects have special rules regarding the "description" attribute on
1683 * modify operations. */
1684 static int samldb_description_check(struct samldb_ctx
*ac
, bool *modified
)
1686 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1687 const char * const attrs
[] = { "objectClass", "description", NULL
};
1688 struct ldb_result
*res
;
1692 /* Fetch information from the existing object */
1693 ret
= dsdb_module_search(ac
->module
, ac
, &res
, ac
->msg
->dn
, LDB_SCOPE_BASE
, attrs
,
1694 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
, ac
->req
,
1695 "(|(objectclass=user)(objectclass=group)(objectclass=samDomain)(objectclass=samServer))");
1696 if (ret
!= LDB_SUCCESS
) {
1697 /* don't treat it specially ... let normal error codes
1698 happen from other places */
1699 ldb_reset_err_string(ldb
);
1702 if (res
->count
== 0) {
1703 /* we didn't match the filter */
1708 /* We've to walk over all modification entries and consider the
1709 * "description" ones. */
1710 for (i
= 0; i
< ac
->msg
->num_elements
; i
++) {
1711 if (ldb_attr_cmp(ac
->msg
->elements
[i
].name
, "description") == 0) {
1712 ac
->msg
->elements
[i
].flags
|= LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK
;
1722 /* This trigger adapts the "servicePrincipalName" attributes if the
1723 * "dNSHostName" and/or "sAMAccountName" attribute change(s) */
1724 static int samldb_service_principal_names_change(struct samldb_ctx
*ac
)
1726 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1727 struct ldb_message_element
*el
= NULL
, *el2
= NULL
;
1728 struct ldb_message
*msg
;
1729 const char *attrs
[] = { "servicePrincipalName", NULL
};
1730 struct ldb_result
*res
;
1731 const char *dns_hostname
= NULL
, *old_dns_hostname
= NULL
,
1732 *sam_accountname
= NULL
, *old_sam_accountname
= NULL
;
1736 el
= dsdb_get_single_valued_attr(ac
->msg
, "dNSHostName",
1737 ac
->req
->operation
);
1738 el2
= dsdb_get_single_valued_attr(ac
->msg
, "sAMAccountName",
1739 ac
->req
->operation
);
1740 if ((el
== NULL
) && (el2
== NULL
)) {
1741 /* we are not affected */
1745 /* Create a temporary message for fetching the "dNSHostName" */
1747 const char *dns_attrs
[] = { "dNSHostName", NULL
};
1748 msg
= ldb_msg_new(ac
->msg
);
1750 return ldb_module_oom(ac
->module
);
1752 ret
= ldb_msg_add(msg
, el
, 0);
1753 if (ret
!= LDB_SUCCESS
) {
1756 dns_hostname
= talloc_steal(ac
,
1757 ldb_msg_find_attr_as_string(msg
, "dNSHostName", NULL
));
1760 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
,
1761 dns_attrs
, DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1762 if (ret
== LDB_SUCCESS
) {
1763 old_dns_hostname
= ldb_msg_find_attr_as_string(res
->msgs
[0], "dNSHostName", NULL
);
1767 /* Create a temporary message for fetching the "sAMAccountName" */
1769 char *tempstr
, *tempstr2
;
1770 const char *acct_attrs
[] = { "sAMAccountName", NULL
};
1772 msg
= ldb_msg_new(ac
->msg
);
1774 return ldb_module_oom(ac
->module
);
1776 ret
= ldb_msg_add(msg
, el2
, 0);
1777 if (ret
!= LDB_SUCCESS
) {
1780 tempstr
= talloc_strdup(ac
,
1781 ldb_msg_find_attr_as_string(msg
, "sAMAccountName", NULL
));
1784 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, acct_attrs
,
1785 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1786 if (ret
== LDB_SUCCESS
) {
1787 tempstr2
= talloc_strdup(ac
,
1788 ldb_msg_find_attr_as_string(res
->msgs
[0],
1789 "sAMAccountName", NULL
));
1793 /* The "sAMAccountName" needs some additional trimming: we need
1794 * to remove the trailing "$"s if they exist. */
1795 if ((tempstr
!= NULL
) && (tempstr
[0] != '\0') &&
1796 (tempstr
[strlen(tempstr
) - 1] == '$')) {
1797 tempstr
[strlen(tempstr
) - 1] = '\0';
1799 if ((tempstr2
!= NULL
) && (tempstr2
[0] != '\0') &&
1800 (tempstr2
[strlen(tempstr2
) - 1] == '$')) {
1801 tempstr2
[strlen(tempstr2
) - 1] = '\0';
1803 sam_accountname
= tempstr
;
1804 old_sam_accountname
= tempstr2
;
1807 if (old_dns_hostname
== NULL
) {
1808 /* we cannot change when the old name is unknown */
1809 dns_hostname
= NULL
;
1811 if ((old_dns_hostname
!= NULL
) && (dns_hostname
!= NULL
) &&
1812 (strcasecmp(old_dns_hostname
, dns_hostname
) == 0)) {
1813 /* The "dNSHostName" didn't change */
1814 dns_hostname
= NULL
;
1817 if (old_sam_accountname
== NULL
) {
1818 /* we cannot change when the old name is unknown */
1819 sam_accountname
= NULL
;
1821 if ((old_sam_accountname
!= NULL
) && (sam_accountname
!= NULL
) &&
1822 (strcasecmp(old_sam_accountname
, sam_accountname
) == 0)) {
1823 /* The "sAMAccountName" didn't change */
1824 sam_accountname
= NULL
;
1827 if ((dns_hostname
== NULL
) && (sam_accountname
== NULL
)) {
1828 /* Well, there are information missing (old name(s)) or the
1829 * names didn't change. We've nothing to do and can exit here */
1833 /* Potential "servicePrincipalName" changes in the same request have to
1834 * be handled before the update (Windows behaviour). */
1835 el
= ldb_msg_find_element(ac
->msg
, "servicePrincipalName");
1837 msg
= ldb_msg_new(ac
->msg
);
1839 return ldb_module_oom(ac
->module
);
1841 msg
->dn
= ac
->msg
->dn
;
1844 ret
= ldb_msg_add(msg
, el
, el
->flags
);
1845 if (ret
!= LDB_SUCCESS
) {
1849 ldb_msg_remove_element(ac
->msg
, el
);
1851 el
= ldb_msg_find_element(ac
->msg
,
1852 "servicePrincipalName");
1853 } while (el
!= NULL
);
1855 ret
= dsdb_module_modify(ac
->module
, msg
,
1856 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1857 if (ret
!= LDB_SUCCESS
) {
1863 /* Fetch the "servicePrincipalName"s if any */
1864 ret
= dsdb_module_search(ac
->module
, ac
, &res
, ac
->msg
->dn
, LDB_SCOPE_BASE
, attrs
,
1865 DSDB_FLAG_NEXT_MODULE
, ac
->req
, NULL
);
1866 if (ret
!= LDB_SUCCESS
) {
1869 if ((res
->count
!= 1) || (res
->msgs
[0]->num_elements
> 1)) {
1870 return ldb_operr(ldb
);
1873 if (res
->msgs
[0]->num_elements
== 1) {
1874 /* Yes, we do have "servicePrincipalName"s. First we update them
1875 * locally, that means we do always substitute the current
1876 * "dNSHostName" with the new one and/or "sAMAccountName"
1877 * without "$" with the new one and then we append this to the
1878 * modification request (Windows behaviour). */
1880 for (i
= 0; i
< res
->msgs
[0]->elements
[0].num_values
; i
++) {
1881 char *old_str
, *new_str
, *pos
;
1885 res
->msgs
[0]->elements
[0].values
[i
].data
;
1887 new_str
= talloc_strdup(ac
->msg
,
1888 strtok_r(old_str
, "/", &pos
));
1889 if (new_str
== NULL
) {
1890 return ldb_module_oom(ac
->module
);
1893 while ((tok
= strtok_r(NULL
, "/", &pos
)) != NULL
) {
1894 if ((dns_hostname
!= NULL
) &&
1895 (strcasecmp(tok
, old_dns_hostname
) == 0)) {
1898 if ((sam_accountname
!= NULL
) &&
1899 (strcasecmp(tok
, old_sam_accountname
) == 0)) {
1900 tok
= sam_accountname
;
1903 new_str
= talloc_asprintf(ac
->msg
, "%s/%s",
1905 if (new_str
== NULL
) {
1906 return ldb_module_oom(ac
->module
);
1910 ret
= ldb_msg_add_string(ac
->msg
,
1911 "servicePrincipalName",
1913 if (ret
!= LDB_SUCCESS
) {
1918 el
= ldb_msg_find_element(ac
->msg
, "servicePrincipalName");
1919 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1929 static int samldb_add(struct ldb_module
*module
, struct ldb_request
*req
)
1931 struct ldb_context
*ldb
;
1932 struct samldb_ctx
*ac
;
1935 ldb
= ldb_module_get_ctx(module
);
1936 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "samldb_add\n");
1938 /* do not manipulate our control entries */
1939 if (ldb_dn_is_special(req
->op
.add
.message
->dn
)) {
1940 return ldb_next_request(module
, req
);
1943 ac
= samldb_ctx_init(module
, req
);
1945 return ldb_operr(ldb
);
1948 /* build the new msg */
1949 ac
->msg
= ldb_msg_copy_shallow(ac
, req
->op
.add
.message
);
1950 if (ac
->msg
== NULL
) {
1952 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
1953 "samldb_add: ldb_msg_copy_shallow failed!\n");
1954 return ldb_operr(ldb
);
1957 if (samdb_find_attribute(ldb
, ac
->msg
,
1958 "objectclass", "user") != NULL
) {
1961 ret
= samldb_prim_group_trigger(ac
);
1962 if (ret
!= LDB_SUCCESS
) {
1966 ret
= samldb_objectclass_trigger(ac
);
1967 if (ret
!= LDB_SUCCESS
) {
1971 return samldb_fill_object(ac
);
1974 if (samdb_find_attribute(ldb
, ac
->msg
,
1975 "objectclass", "group") != NULL
) {
1978 ret
= samldb_objectclass_trigger(ac
);
1979 if (ret
!= LDB_SUCCESS
) {
1983 return samldb_fill_object(ac
);
1986 /* perhaps a foreignSecurityPrincipal? */
1987 if (samdb_find_attribute(ldb
, ac
->msg
,
1989 "foreignSecurityPrincipal") != NULL
) {
1990 return samldb_fill_foreignSecurityPrincipal_object(ac
);
1993 if (samdb_find_attribute(ldb
, ac
->msg
,
1994 "objectclass", "classSchema") != NULL
) {
1995 ret
= samldb_schema_info_update(ac
);
1996 if (ret
!= LDB_SUCCESS
) {
2001 ac
->type
= "classSchema";
2002 return samldb_fill_object(ac
);
2005 if (samdb_find_attribute(ldb
, ac
->msg
,
2006 "objectclass", "attributeSchema") != NULL
) {
2007 ret
= samldb_schema_info_update(ac
);
2008 if (ret
!= LDB_SUCCESS
) {
2013 ac
->type
= "attributeSchema";
2014 return samldb_fill_object(ac
);
2019 /* nothing matched, go on */
2020 return ldb_next_request(module
, req
);
2024 static int samldb_modify(struct ldb_module
*module
, struct ldb_request
*req
)
2026 struct ldb_context
*ldb
;
2027 struct samldb_ctx
*ac
;
2028 struct ldb_message_element
*el
, *el2
;
2029 bool modified
= false;
2032 if (ldb_dn_is_special(req
->op
.mod
.message
->dn
)) {
2033 /* do not manipulate our control entries */
2034 return ldb_next_request(module
, req
);
2037 ldb
= ldb_module_get_ctx(module
);
2039 /* make sure that "objectSid" is not specified */
2040 el
= ldb_msg_find_element(req
->op
.mod
.message
, "objectSid");
2042 if (ldb_request_get_control(req
, LDB_CONTROL_PROVISION_OID
) == NULL
) {
2043 ldb_set_errstring(ldb
,
2044 "samldb: objectSid must not be specified!");
2045 return LDB_ERR_UNWILLING_TO_PERFORM
;
2048 /* make sure that "sAMAccountType" is not specified */
2049 el
= ldb_msg_find_element(req
->op
.mod
.message
, "sAMAccountType");
2051 ldb_set_errstring(ldb
,
2052 "samldb: sAMAccountType must not be specified!");
2053 return LDB_ERR_UNWILLING_TO_PERFORM
;
2055 /* make sure that "isCriticalSystemObject" is not specified */
2056 el
= ldb_msg_find_element(req
->op
.mod
.message
, "isCriticalSystemObject");
2058 if (ldb_request_get_control(req
, LDB_CONTROL_RELAX_OID
) == NULL
) {
2059 ldb_set_errstring(ldb
,
2060 "samldb: isCriticalSystemObject must not be specified!");
2061 return LDB_ERR_UNWILLING_TO_PERFORM
;
2065 /* msDS-IntId is not allowed to be modified
2066 * except when modification comes from replication */
2067 if (ldb_msg_find_element(req
->op
.mod
.message
, "msDS-IntId")) {
2068 if (!ldb_request_get_control(req
,
2069 DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
2070 return LDB_ERR_CONSTRAINT_VIOLATION
;
2074 ac
= samldb_ctx_init(module
, req
);
2076 return ldb_operr(ldb
);
2079 /* build the new msg */
2080 ac
->msg
= ldb_msg_copy_shallow(ac
, req
->op
.mod
.message
);
2081 if (ac
->msg
== NULL
) {
2083 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
2084 "samldb_modify: ldb_msg_copy_shallow failed!\n");
2085 return ldb_operr(ldb
);
2088 el
= ldb_msg_find_element(ac
->msg
, "primaryGroupID");
2090 ret
= samldb_prim_group_trigger(ac
);
2091 if (ret
!= LDB_SUCCESS
) {
2096 el
= ldb_msg_find_element(ac
->msg
, "userAccountControl");
2099 ret
= samldb_user_account_control_change(ac
);
2100 if (ret
!= LDB_SUCCESS
) {
2105 el
= ldb_msg_find_element(ac
->msg
, "groupType");
2108 ret
= samldb_group_type_change(ac
);
2109 if (ret
!= LDB_SUCCESS
) {
2114 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountName");
2116 ret
= samldb_sam_accountname_check(ac
);
2117 if (ret
!= LDB_SUCCESS
) {
2122 el
= ldb_msg_find_element(ac
->msg
, "member");
2124 ret
= samldb_member_check(ac
);
2125 if (ret
!= LDB_SUCCESS
) {
2130 el
= ldb_msg_find_element(ac
->msg
, "description");
2132 ret
= samldb_description_check(ac
, &modified
);
2133 if (ret
!= LDB_SUCCESS
) {
2138 el
= ldb_msg_find_element(ac
->msg
, "dNSHostName");
2139 el2
= ldb_msg_find_element(ac
->msg
, "sAMAccountName");
2140 if ((el
!= NULL
) || (el2
!= NULL
)) {
2142 ret
= samldb_service_principal_names_change(ac
);
2143 if (ret
!= LDB_SUCCESS
) {
2149 struct ldb_request
*child_req
;
2151 /* Now perform the real modifications as a child request */
2152 ret
= ldb_build_mod_req(&child_req
, ldb
, ac
,
2155 req
, dsdb_next_callback
,
2157 LDB_REQ_SET_LOCATION(child_req
);
2158 if (ret
!= LDB_SUCCESS
) {
2162 return ldb_next_request(module
, child_req
);
2167 /* no change which interests us, go on */
2168 return ldb_next_request(module
, req
);
2173 static int samldb_prim_group_users_check(struct samldb_ctx
*ac
)
2175 struct ldb_context
*ldb
;
2176 struct dom_sid
*sid
;
2180 struct ldb_result
*res
;
2181 const char *attrs
[] = { "objectSid", "isDeleted", NULL
};
2182 const char *noattrs
[] = { NULL
};
2184 ldb
= ldb_module_get_ctx(ac
->module
);
2186 /* Finds out the SID/RID of the SAM object */
2187 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->req
->op
.del
.dn
,
2189 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
,
2191 if (ret
!= LDB_SUCCESS
) {
2195 if (ldb_msg_check_string_attribute(res
->msgs
[0], "isDeleted", "TRUE")) {
2199 sid
= samdb_result_dom_sid(ac
, res
->msgs
[0], "objectSid");
2201 /* No SID - it might not be a SAM object - therefore ok */
2204 status
= dom_sid_split_rid(ac
, sid
, NULL
, &rid
);
2205 if (!NT_STATUS_IS_OK(status
)) {
2206 return ldb_operr(ldb
);
2209 /* Special object (security principal?) */
2213 /* Deny delete requests from groups which are primary ones */
2214 ret
= dsdb_module_search(ac
->module
, ac
, &res
, NULL
, LDB_SCOPE_SUBTREE
, noattrs
,
2215 DSDB_FLAG_NEXT_MODULE
,
2217 "(&(primaryGroupID=%u)(objectClass=user))", rid
);
2218 if (ret
!= LDB_SUCCESS
) {
2221 if (res
->count
> 0) {
2222 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
2228 static int samldb_delete(struct ldb_module
*module
, struct ldb_request
*req
)
2230 struct samldb_ctx
*ac
;
2233 if (ldb_dn_is_special(req
->op
.del
.dn
)) {
2234 /* do not manipulate our control entries */
2235 return ldb_next_request(module
, req
);
2238 ac
= samldb_ctx_init(module
, req
);
2240 return ldb_operr(ldb_module_get_ctx(module
));
2243 ret
= samldb_prim_group_users_check(ac
);
2244 if (ret
!= LDB_SUCCESS
) {
2250 return ldb_next_request(module
, req
);
2255 static int samldb_extended_allocate_rid_pool(struct ldb_module
*module
, struct ldb_request
*req
)
2257 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
2258 struct dsdb_fsmo_extended_op
*exop
;
2261 exop
= talloc_get_type(req
->op
.extended
.data
,
2262 struct dsdb_fsmo_extended_op
);
2264 ldb_set_errstring(ldb
,
2265 "samldb_extended_allocate_rid_pool: invalid extended data");
2266 return LDB_ERR_PROTOCOL_ERROR
;
2269 ret
= ridalloc_allocate_rid_pool_fsmo(module
, exop
, req
);
2270 if (ret
!= LDB_SUCCESS
) {
2274 return ldb_module_done(req
, NULL
, NULL
, LDB_SUCCESS
);
2277 static int samldb_extended(struct ldb_module
*module
, struct ldb_request
*req
)
2279 if (strcmp(req
->op
.extended
.oid
, DSDB_EXTENDED_ALLOCATE_RID_POOL
) == 0) {
2280 return samldb_extended_allocate_rid_pool(module
, req
);
2283 return ldb_next_request(module
, req
);
2287 static const struct ldb_module_ops ldb_samldb_module_ops
= {
2290 .modify
= samldb_modify
,
2291 .del
= samldb_delete
,
2292 .extended
= samldb_extended
2296 int ldb_samldb_module_init(const char *version
)
2298 LDB_MODULE_CHECK_VERSION(version
);
2299 return ldb_register_module(&ldb_samldb_module_ops
);