4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
5 Copyright (C) Simo Sorce 2004-2008
6 Copyright (C) Matthias Dieter Wallnöfer 2009-2011
7 Copyright (C) Matthieu Patou 2012
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 * Component: ldb samldb module
28 * Description: various internal DSDB triggers - most for SAM specific objects
34 #include "libcli/ldap/ldap_ndr.h"
35 #include "ldb_module.h"
36 #include "dsdb/samdb/samdb.h"
37 #include "dsdb/samdb/ldb_modules/util.h"
38 #include "dsdb/samdb/ldb_modules/ridalloc.h"
39 #include "libcli/security/security.h"
40 #include "librpc/gen_ndr/ndr_security.h"
42 #include "param/param.h"
43 #include "libds/common/flag_mapping.h"
46 enum samldb_add_type
{
53 typedef int (*samldb_step_fn_t
)(struct samldb_ctx
*);
56 struct samldb_step
*next
;
61 struct ldb_module
*module
;
62 struct ldb_request
*req
;
64 /* used for add operations */
65 enum samldb_add_type type
;
67 /* the resulting message */
68 struct ldb_message
*msg
;
70 /* used in "samldb_find_for_defaultObjectCategory" */
71 struct ldb_dn
*dn
, *res_dn
;
73 /* all the async steps necessary to complete the operation */
74 struct samldb_step
*steps
;
75 struct samldb_step
*curstep
;
77 /* If someone set an ares to forward controls and response back to the caller */
78 struct ldb_reply
*ares
;
81 static struct samldb_ctx
*samldb_ctx_init(struct ldb_module
*module
,
82 struct ldb_request
*req
)
84 struct ldb_context
*ldb
;
85 struct samldb_ctx
*ac
;
87 ldb
= ldb_module_get_ctx(module
);
89 ac
= talloc_zero(req
, struct samldb_ctx
);
101 static int samldb_add_step(struct samldb_ctx
*ac
, samldb_step_fn_t fn
)
103 struct samldb_step
*step
, *stepper
;
105 step
= talloc_zero(ac
, struct samldb_step
);
107 return ldb_oom(ldb_module_get_ctx(ac
->module
));
112 if (ac
->steps
== NULL
) {
116 if (ac
->curstep
== NULL
)
117 return ldb_operr(ldb_module_get_ctx(ac
->module
));
118 for (stepper
= ac
->curstep
; stepper
->next
!= NULL
;
119 stepper
= stepper
->next
);
120 stepper
->next
= step
;
126 static int samldb_first_step(struct samldb_ctx
*ac
)
128 if (ac
->steps
== NULL
) {
129 return ldb_operr(ldb_module_get_ctx(ac
->module
));
132 ac
->curstep
= ac
->steps
;
133 return ac
->curstep
->fn(ac
);
136 static int samldb_next_step(struct samldb_ctx
*ac
)
138 if (ac
->curstep
->next
) {
139 ac
->curstep
= ac
->curstep
->next
;
140 return ac
->curstep
->fn(ac
);
143 /* We exit the samldb module here. If someone set an "ares" to forward
144 * controls and response back to the caller, use them. */
146 return ldb_module_done(ac
->req
, ac
->ares
->controls
,
147 ac
->ares
->response
, LDB_SUCCESS
);
149 return ldb_module_done(ac
->req
, NULL
, NULL
, LDB_SUCCESS
);
154 /* sAMAccountName handling */
156 static int samldb_generate_sAMAccountName(struct ldb_context
*ldb
,
157 struct ldb_message
*msg
)
161 /* Format: $000000-000000000000 */
163 name
= talloc_asprintf(msg
, "$%.6X-%.6X%.6X",
164 (unsigned int)generate_random(),
165 (unsigned int)generate_random(),
166 (unsigned int)generate_random());
170 return ldb_msg_add_steal_string(msg
, "sAMAccountName", name
);
173 static int samldb_check_sAMAccountName(struct samldb_ctx
*ac
)
175 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
178 struct ldb_result
*res
;
179 const char * const noattrs
[] = { NULL
};
181 if (ldb_msg_find_element(ac
->msg
, "sAMAccountName") == NULL
) {
182 ret
= samldb_generate_sAMAccountName(ldb
, ac
->msg
);
183 if (ret
!= LDB_SUCCESS
) {
188 name
= ldb_msg_find_attr_as_string(ac
->msg
, "sAMAccountName", NULL
);
190 /* The "sAMAccountName" cannot be nothing */
191 ldb_set_errstring(ldb
,
192 "samldb: Empty account names aren't allowed!");
193 return LDB_ERR_CONSTRAINT_VIOLATION
;
196 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
197 ldb_get_default_basedn(ldb
), LDB_SCOPE_SUBTREE
, noattrs
,
198 DSDB_FLAG_NEXT_MODULE
,
200 "(sAMAccountName=%s)",
201 ldb_binary_encode_string(ac
, name
));
202 if (ret
!= LDB_SUCCESS
) {
205 if (res
->count
!= 0) {
206 ldb_asprintf_errstring(ldb
,
207 "samldb: Account name (sAMAccountName) '%s' already in use!",
210 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
214 return samldb_next_step(ac
);
218 static bool samldb_msg_add_sid(struct ldb_message
*msg
,
220 const struct dom_sid
*sid
)
223 enum ndr_err_code ndr_err
;
225 ndr_err
= ndr_push_struct_blob(&v
, msg
, sid
,
226 (ndr_push_flags_fn_t
)ndr_push_dom_sid
);
227 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
230 return (ldb_msg_add_value(msg
, name
, &v
, NULL
) == 0);
234 /* allocate a SID using our RID Set */
235 static int samldb_allocate_sid(struct samldb_ctx
*ac
)
239 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
242 ret
= ridalloc_allocate_rid(ac
->module
, &rid
, ac
->req
);
243 if (ret
!= LDB_SUCCESS
) {
247 sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), rid
);
249 return ldb_module_oom(ac
->module
);
252 if ( ! samldb_msg_add_sid(ac
->msg
, "objectSid", sid
)) {
253 return ldb_operr(ldb
);
256 return samldb_next_step(ac
);
260 see if a krbtgt_number is available
262 static bool samldb_krbtgtnumber_available(struct samldb_ctx
*ac
,
263 uint32_t krbtgt_number
)
265 TALLOC_CTX
*tmp_ctx
= talloc_new(ac
);
266 struct ldb_result
*res
;
267 const char * const no_attrs
[] = { NULL
};
270 ret
= dsdb_module_search(ac
->module
, tmp_ctx
, &res
,
271 ldb_get_default_basedn(ldb_module_get_ctx(ac
->module
)),
272 LDB_SCOPE_SUBTREE
, no_attrs
,
273 DSDB_FLAG_NEXT_MODULE
,
275 "(msDC-SecondaryKrbTgtNumber=%u)",
277 if (ret
== LDB_SUCCESS
&& res
->count
== 0) {
278 talloc_free(tmp_ctx
);
281 talloc_free(tmp_ctx
);
285 /* special handling for add in RODC join */
286 static int samldb_rodc_add(struct samldb_ctx
*ac
)
288 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
289 uint32_t krbtgt_number
, i_start
, i
;
292 struct ldb_val newpass_utf16
;
294 /* find a unused msDC-SecondaryKrbTgtNumber */
295 i_start
= generate_random() & 0xFFFF;
300 for (i
=i_start
; i
<=0xFFFF; i
++) {
301 if (samldb_krbtgtnumber_available(ac
, i
)) {
306 for (i
=1; i
<i_start
; i
++) {
307 if (samldb_krbtgtnumber_available(ac
, i
)) {
313 ldb_asprintf_errstring(ldb
,
314 "%08X: Unable to find available msDS-SecondaryKrbTgtNumber",
315 W_ERROR_V(WERR_NO_SYSTEM_RESOURCES
));
316 return LDB_ERR_OTHER
;
319 ret
= ldb_msg_add_empty(ac
->msg
, "msDS-SecondaryKrbTgtNumber",
320 LDB_FLAG_INTERNAL_DISABLE_VALIDATION
, NULL
);
321 if (ret
!= LDB_SUCCESS
) {
322 return ldb_operr(ldb
);
325 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
326 "msDS-SecondaryKrbTgtNumber", krbtgt_number
);
327 if (ret
!= LDB_SUCCESS
) {
328 return ldb_operr(ldb
);
331 ret
= ldb_msg_add_fmt(ac
->msg
, "sAMAccountName", "krbtgt_%u",
333 if (ret
!= LDB_SUCCESS
) {
334 return ldb_operr(ldb
);
337 newpass
= generate_random_password(ac
->msg
, 128, 255);
338 if (newpass
== NULL
) {
339 return ldb_operr(ldb
);
342 if (!convert_string_talloc(ac
,
344 newpass
, strlen(newpass
),
345 (void *)&newpass_utf16
.data
,
346 &newpass_utf16
.length
)) {
347 ldb_asprintf_errstring(ldb
,
349 "failed to generate UTF16 password from random password");
350 return LDB_ERR_OPERATIONS_ERROR
;
352 ret
= ldb_msg_add_steal_value(ac
->msg
, "clearTextPassword", &newpass_utf16
);
353 if (ret
!= LDB_SUCCESS
) {
354 return ldb_operr(ldb
);
357 return samldb_next_step(ac
);
360 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx
*ac
)
362 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
363 struct ldb_result
*res
;
364 const char * const no_attrs
[] = { NULL
};
369 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
370 ac
->dn
, LDB_SCOPE_BASE
, no_attrs
,
371 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
372 | DSDB_FLAG_NEXT_MODULE
,
374 "(objectClass=classSchema)");
375 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
376 /* Don't be pricky when the DN doesn't exist if we have the */
377 /* RELAX control specified */
378 if (ldb_request_get_control(ac
->req
,
379 LDB_CONTROL_RELAX_OID
) == NULL
) {
380 ldb_set_errstring(ldb
,
381 "samldb_find_defaultObjectCategory: "
382 "Invalid DN for 'defaultObjectCategory'!");
383 return LDB_ERR_CONSTRAINT_VIOLATION
;
386 if ((ret
!= LDB_ERR_NO_SUCH_OBJECT
) && (ret
!= LDB_SUCCESS
)) {
390 if (ret
== LDB_SUCCESS
) {
391 /* ensure the defaultObjectCategory has a full GUID */
392 struct ldb_message
*m
;
393 m
= ldb_msg_new(ac
->msg
);
398 if (ldb_msg_add_string(m
, "defaultObjectCategory",
399 ldb_dn_get_extended_linearized(m
, res
->msgs
[0]->dn
, 1)) !=
403 m
->elements
[0].flags
= LDB_FLAG_MOD_REPLACE
;
405 ret
= dsdb_module_modify(ac
->module
, m
,
406 DSDB_FLAG_NEXT_MODULE
,
408 if (ret
!= LDB_SUCCESS
) {
416 return samldb_next_step(ac
);
420 * msDS-IntId attributeSchema attribute handling
421 * during LDB_ADD request processing
423 static int samldb_add_handle_msDS_IntId(struct samldb_ctx
*ac
)
428 int32_t system_flags
;
429 struct ldb_context
*ldb
;
430 struct ldb_result
*ldb_res
;
431 struct ldb_dn
*schema_dn
;
432 struct samldb_msds_intid_persistant
*msds_intid_struct
;
433 struct dsdb_schema
*schema
;
435 ldb
= ldb_module_get_ctx(ac
->module
);
436 schema_dn
= ldb_get_schema_basedn(ldb
);
438 /* replicated update should always go through */
439 if (ldb_request_get_control(ac
->req
,
440 DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
444 /* msDS-IntId is handled by system and should never be
445 * passed by clients */
446 if (ldb_msg_find_element(ac
->msg
, "msDS-IntId")) {
447 return LDB_ERR_UNWILLING_TO_PERFORM
;
450 /* do not generate msDS-IntId if Relax control is passed */
451 if (ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
)) {
455 /* check Functional Level */
456 if (dsdb_functional_level(ldb
) < DS_DOMAIN_FUNCTION_2003
) {
460 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
461 system_flags
= ldb_msg_find_attr_as_int(ac
->msg
, "systemFlags", 0);
462 if (system_flags
& SYSTEM_FLAG_SCHEMA_BASE_OBJECT
) {
465 schema
= dsdb_get_schema(ldb
, NULL
);
467 ldb_debug_set(ldb
, LDB_DEBUG_FATAL
,
468 "samldb_schema_info_update: no dsdb_schema loaded");
469 DEBUG(0,(__location__
": %s\n", ldb_errstring(ldb
)));
470 return ldb_operr(ldb
);
473 msds_intid_struct
= (struct samldb_msds_intid_persistant
*) ldb_get_opaque(ldb
, SAMLDB_MSDS_INTID_OPAQUE
);
474 if (!msds_intid_struct
) {
475 msds_intid_struct
= talloc(ldb
, struct samldb_msds_intid_persistant
);
476 /* Generate new value for msDs-IntId
477 * Value should be in 0x80000000..0xBFFFFFFF range */
478 msds_intid
= generate_random() % 0X3FFFFFFF;
479 msds_intid
+= 0x80000000;
480 msds_intid_struct
->msds_intid
= msds_intid
;
481 msds_intid_struct
->usn
= schema
->loaded_usn
;
482 DEBUG(2, ("No samldb_msds_intid_persistant struct, allocating a new one\n"));
484 msds_intid
= msds_intid_struct
->msds_intid
;
487 /* probe id values until unique one is found */
489 uint64_t current_usn
;
491 if (msds_intid
> 0xBFFFFFFF) {
492 msds_intid
= 0x80000001;
495 * Alternative strategy to a costly (even indexed search) to the
497 * We search in the schema if we have already this intid (using dsdb_attribute_by_attributeID_id because
498 * in the range 0x80000000 0xBFFFFFFFF, attributeID is a DSDB_ATTID_TYPE_INTID).
499 * If so generate another random value.
500 * If not check if the highest USN in the database for the schema partition is the
502 * If so it means that's only this ldb context that is touching the schema in the database.
503 * If not it means that's someone else has modified the database while we are doing our changes too
504 * (this case should be very bery rare) in order to be sure do the search in the database.
506 if (dsdb_attribute_by_attributeID_id(schema
, msds_intid
)) {
507 msds_intid
= generate_random() % 0X3FFFFFFF;
508 msds_intid
+= 0x80000000;
512 ret
= dsdb_module_load_partition_usn(ac
->module
, schema
->base_dn
, ¤t_usn
, NULL
, NULL
);
513 if (ret
!= LDB_SUCCESS
) {
514 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
515 __location__
": Searching for schema USN failed: %s\n",
517 return ldb_operr(ldb
);
520 /* current_usn can be lesser than msds_intid_struct-> if there is
521 * uncommited changes.
523 if (current_usn
> msds_intid_struct
->usn
) {
524 /* oups something has changed, someone/something
525 * else is modifying or has modified the schema
526 * we'd better check this intid is the database directly
529 DEBUG(2, ("Schema has changed, searching the database for the unicity of %d\n",
532 ret
= dsdb_module_search(ac
->module
, ac
,
534 schema_dn
, LDB_SCOPE_ONELEVEL
, NULL
,
535 DSDB_FLAG_NEXT_MODULE
,
537 "(msDS-IntId=%d)", msds_intid
);
538 if (ret
!= LDB_SUCCESS
) {
539 ldb_debug_set(ldb
, LDB_DEBUG_ERROR
,
540 __location__
": Searching for msDS-IntId=%d failed - %s\n",
543 return ldb_operr(ldb
);
545 id_exists
= (ldb_res
->count
> 0);
546 talloc_free(ldb_res
);
552 msds_intid_struct
->msds_intid
= msds_intid
;
553 ldb_set_opaque(ldb
, SAMLDB_MSDS_INTID_OPAQUE
, msds_intid_struct
);
555 return samdb_msg_add_int(ldb
, ac
->msg
, ac
->msg
, "msDS-IntId",
561 * samldb_add_entry (async)
564 static int samldb_add_entry_callback(struct ldb_request
*req
,
565 struct ldb_reply
*ares
)
567 struct ldb_context
*ldb
;
568 struct samldb_ctx
*ac
;
571 ac
= talloc_get_type(req
->context
, struct samldb_ctx
);
572 ldb
= ldb_module_get_ctx(ac
->module
);
575 return ldb_module_done(ac
->req
, NULL
, NULL
,
576 LDB_ERR_OPERATIONS_ERROR
);
579 if (ares
->type
== LDB_REPLY_REFERRAL
) {
580 return ldb_module_send_referral(ac
->req
, ares
->referral
);
583 if (ares
->error
!= LDB_SUCCESS
) {
584 return ldb_module_done(ac
->req
, ares
->controls
,
585 ares
->response
, ares
->error
);
587 if (ares
->type
!= LDB_REPLY_DONE
) {
588 ldb_asprintf_errstring(ldb
, "Invalid LDB reply type %d", ares
->type
);
589 return ldb_module_done(ac
->req
, NULL
, NULL
,
590 LDB_ERR_OPERATIONS_ERROR
);
593 /* The caller may wish to get controls back from the add */
594 ac
->ares
= talloc_steal(ac
, ares
);
596 ret
= samldb_next_step(ac
);
597 if (ret
!= LDB_SUCCESS
) {
598 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
603 static int samldb_add_entry(struct samldb_ctx
*ac
)
605 struct ldb_context
*ldb
;
606 struct ldb_request
*req
;
609 ldb
= ldb_module_get_ctx(ac
->module
);
611 ret
= ldb_build_add_req(&req
, ldb
, ac
,
614 ac
, samldb_add_entry_callback
,
616 LDB_REQ_SET_LOCATION(req
);
617 if (ret
!= LDB_SUCCESS
) {
621 return ldb_next_request(ac
->module
, req
);
625 * return true if msg carries an attributeSchema that is intended to be RODC
626 * filtered but is also a system-critical attribute.
628 static bool check_rodc_critical_attribute(struct ldb_message
*msg
)
630 uint32_t schemaFlagsEx
, searchFlags
, rodc_filtered_flags
;
632 schemaFlagsEx
= ldb_msg_find_attr_as_uint(msg
, "schemaFlagsEx", 0);
633 searchFlags
= ldb_msg_find_attr_as_uint(msg
, "searchFlags", 0);
634 rodc_filtered_flags
= (SEARCH_FLAG_RODC_ATTRIBUTE
635 | SEARCH_FLAG_CONFIDENTIAL
);
637 if ((schemaFlagsEx
& SCHEMA_FLAG_ATTR_IS_CRITICAL
) &&
638 ((searchFlags
& rodc_filtered_flags
) == rodc_filtered_flags
)) {
646 static int samldb_fill_object(struct samldb_ctx
*ac
)
648 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
651 /* Add information for the different account types */
653 case SAMLDB_TYPE_USER
: {
654 struct ldb_control
*rodc_control
= ldb_request_get_control(ac
->req
,
655 LDB_CONTROL_RODC_DCPROMO_OID
);
656 if (rodc_control
!= NULL
) {
657 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
658 rodc_control
->critical
= false;
659 ret
= samldb_add_step(ac
, samldb_rodc_add
);
660 if (ret
!= LDB_SUCCESS
) return ret
;
663 /* check if we have a valid sAMAccountName */
664 ret
= samldb_add_step(ac
, samldb_check_sAMAccountName
);
665 if (ret
!= LDB_SUCCESS
) return ret
;
667 ret
= samldb_add_step(ac
, samldb_add_entry
);
668 if (ret
!= LDB_SUCCESS
) return ret
;
672 case SAMLDB_TYPE_GROUP
: {
673 /* check if we have a valid sAMAccountName */
674 ret
= samldb_add_step(ac
, samldb_check_sAMAccountName
);
675 if (ret
!= LDB_SUCCESS
) return ret
;
677 ret
= samldb_add_step(ac
, samldb_add_entry
);
678 if (ret
!= LDB_SUCCESS
) return ret
;
682 case SAMLDB_TYPE_CLASS
: {
683 const struct ldb_val
*rdn_value
, *def_obj_cat_val
;
684 unsigned int v
= ldb_msg_find_attr_as_uint(ac
->msg
, "objectClassCategory", -2);
686 /* As discussed with Microsoft through dochelp in April 2012 this is the behavior of windows*/
687 if (!ldb_msg_find_element(ac
->msg
, "subClassOf")) {
688 ret
= ldb_msg_add_string(ac
->msg
, "subClassOf", "top");
689 if (ret
!= LDB_SUCCESS
) return ret
;
692 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
694 if (ret
!= LDB_SUCCESS
) return ret
;
696 /* do not allow to mark an attributeSchema as RODC filtered if it
697 * is system-critical */
698 if (check_rodc_critical_attribute(ac
->msg
)) {
699 ldb_asprintf_errstring(ldb
, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
700 ldb_dn_get_linearized(ac
->msg
->dn
));
701 return LDB_ERR_UNWILLING_TO_PERFORM
;
704 rdn_value
= ldb_dn_get_rdn_val(ac
->msg
->dn
);
705 if (rdn_value
== NULL
) {
706 return ldb_operr(ldb
);
708 if (!ldb_msg_find_element(ac
->msg
, "lDAPDisplayName")) {
709 /* the RDN has prefix "CN" */
710 ret
= ldb_msg_add_string(ac
->msg
, "lDAPDisplayName",
711 samdb_cn_to_lDAPDisplayName(ac
->msg
,
712 (const char *) rdn_value
->data
));
713 if (ret
!= LDB_SUCCESS
) {
719 if (!ldb_msg_find_element(ac
->msg
, "schemaIDGUID")) {
722 guid
= GUID_random();
723 ret
= dsdb_msg_add_guid(ac
->msg
, &guid
, "schemaIDGUID");
724 if (ret
!= LDB_SUCCESS
) {
730 def_obj_cat_val
= ldb_msg_find_ldb_val(ac
->msg
,
731 "defaultObjectCategory");
732 if (def_obj_cat_val
!= NULL
) {
733 /* "defaultObjectCategory" has been set by the caller.
734 * Do some checks for consistency.
735 * NOTE: The real constraint check (that
736 * 'defaultObjectCategory' is the DN of the new
737 * objectclass or any parent of it) is still incomplete.
738 * For now we say that 'defaultObjectCategory' is valid
739 * if it exists and it is of objectclass "classSchema".
741 ac
->dn
= ldb_dn_from_ldb_val(ac
, ldb
, def_obj_cat_val
);
742 if (ac
->dn
== NULL
) {
743 ldb_set_errstring(ldb
,
744 "Invalid DN for 'defaultObjectCategory'!");
745 return LDB_ERR_CONSTRAINT_VIOLATION
;
748 /* "defaultObjectCategory" has not been set by the
749 * caller. Use the entry DN for it. */
750 ac
->dn
= ac
->msg
->dn
;
752 ret
= ldb_msg_add_string(ac
->msg
, "defaultObjectCategory",
753 ldb_dn_alloc_linearized(ac
->msg
, ac
->dn
));
754 if (ret
!= LDB_SUCCESS
) {
760 ret
= samldb_add_step(ac
, samldb_add_entry
);
761 if (ret
!= LDB_SUCCESS
) return ret
;
763 /* Now perform the checks for the 'defaultObjectCategory'. The
764 * lookup DN was already saved in "ac->dn" */
765 ret
= samldb_add_step(ac
, samldb_find_for_defaultObjectCategory
);
766 if (ret
!= LDB_SUCCESS
) return ret
;
768 /* -2 is not a valid objectClassCategory so it means the attribute wasn't present */
770 /* Windows 2003 does this*/
771 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
, "objectClassCategory", 0);
772 if (ret
!= LDB_SUCCESS
) {
779 case SAMLDB_TYPE_ATTRIBUTE
: {
780 const struct ldb_val
*rdn_value
;
781 struct ldb_message_element
*el
;
782 rdn_value
= ldb_dn_get_rdn_val(ac
->msg
->dn
);
783 if (rdn_value
== NULL
) {
784 return ldb_operr(ldb
);
786 if (!ldb_msg_find_element(ac
->msg
, "lDAPDisplayName")) {
787 /* the RDN has prefix "CN" */
788 ret
= ldb_msg_add_string(ac
->msg
, "lDAPDisplayName",
789 samdb_cn_to_lDAPDisplayName(ac
->msg
,
790 (const char *) rdn_value
->data
));
791 if (ret
!= LDB_SUCCESS
) {
797 /* do not allow to mark an attributeSchema as RODC filtered if it
798 * is system-critical */
799 if (check_rodc_critical_attribute(ac
->msg
)) {
800 ldb_asprintf_errstring(ldb
,
801 "samldb: refusing schema add of %s - cannot combine critical attribute with RODC filtering",
802 ldb_dn_get_linearized(ac
->msg
->dn
));
803 return LDB_ERR_UNWILLING_TO_PERFORM
;
806 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
807 "isSingleValued", "FALSE");
808 if (ret
!= LDB_SUCCESS
) return ret
;
810 if (!ldb_msg_find_element(ac
->msg
, "schemaIDGUID")) {
813 guid
= GUID_random();
814 ret
= dsdb_msg_add_guid(ac
->msg
, &guid
, "schemaIDGUID");
815 if (ret
!= LDB_SUCCESS
) {
821 el
= ldb_msg_find_element(ac
->msg
, "attributeSyntax");
824 * No need to scream if there isn't as we have code later on
825 * that will take care of it.
827 const struct dsdb_syntax
*syntax
= find_syntax_map_by_ad_oid((const char *)el
->values
[0].data
);
829 DEBUG(9, ("Can't find dsdb_syntax object for attributeSyntax %s\n",
830 (const char *)el
->values
[0].data
));
832 unsigned int v
= ldb_msg_find_attr_as_uint(ac
->msg
, "oMSyntax", 0);
833 const struct ldb_val
*val
= ldb_msg_find_ldb_val(ac
->msg
, "oMObjectClass");
836 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
, "oMSyntax", syntax
->oMSyntax
);
837 if (ret
!= LDB_SUCCESS
) {
842 struct ldb_val val2
= ldb_val_dup(ldb
, &syntax
->oMObjectClass
);
843 if (val2
.length
> 0) {
844 ret
= ldb_msg_add_value(ac
->msg
, "oMObjectClass", &val2
, NULL
);
845 if (ret
!= LDB_SUCCESS
) {
853 /* handle msDS-IntID attribute */
854 ret
= samldb_add_handle_msDS_IntId(ac
);
855 if (ret
!= LDB_SUCCESS
) return ret
;
857 ret
= samldb_add_step(ac
, samldb_add_entry
);
858 if (ret
!= LDB_SUCCESS
) return ret
;
863 ldb_asprintf_errstring(ldb
, "Invalid entry type!");
864 return LDB_ERR_OPERATIONS_ERROR
;
868 return samldb_first_step(ac
);
871 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx
*ac
)
873 struct ldb_context
*ldb
;
874 const struct ldb_val
*rdn_value
;
878 ldb
= ldb_module_get_ctx(ac
->module
);
880 sid
= samdb_result_dom_sid(ac
->msg
, ac
->msg
, "objectSid");
882 rdn_value
= ldb_dn_get_rdn_val(ac
->msg
->dn
);
883 if (rdn_value
== NULL
) {
884 return ldb_operr(ldb
);
886 sid
= dom_sid_parse_talloc(ac
->msg
,
887 (const char *)rdn_value
->data
);
889 ldb_set_errstring(ldb
,
890 "samldb: No valid SID found in ForeignSecurityPrincipal CN!");
891 return LDB_ERR_CONSTRAINT_VIOLATION
;
893 if (! samldb_msg_add_sid(ac
->msg
, "objectSid", sid
)) {
894 return ldb_operr(ldb
);
898 /* finally proceed with adding the entry */
899 ret
= samldb_add_step(ac
, samldb_add_entry
);
900 if (ret
!= LDB_SUCCESS
) return ret
;
902 return samldb_first_step(ac
);
905 static int samldb_schema_info_update(struct samldb_ctx
*ac
)
908 struct ldb_context
*ldb
;
909 struct dsdb_schema
*schema
;
911 /* replicated update should always go through */
912 if (ldb_request_get_control(ac
->req
,
913 DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
917 /* do not update schemaInfo during provisioning */
918 if (ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
)) {
922 ldb
= ldb_module_get_ctx(ac
->module
);
923 schema
= dsdb_get_schema(ldb
, NULL
);
925 ldb_debug_set(ldb
, LDB_DEBUG_FATAL
,
926 "samldb_schema_info_update: no dsdb_schema loaded");
927 DEBUG(0,(__location__
": %s\n", ldb_errstring(ldb
)));
928 return ldb_operr(ldb
);
931 ret
= dsdb_module_schema_info_update(ac
->module
, schema
,
932 DSDB_FLAG_NEXT_MODULE
|
935 if (ret
!= LDB_SUCCESS
) {
936 ldb_asprintf_errstring(ldb
,
937 "samldb_schema_info_update: dsdb_module_schema_info_update failed with %s",
945 static int samldb_prim_group_tester(struct samldb_ctx
*ac
, uint32_t rid
);
948 * "Objectclass" trigger (MS-SAMR 3.1.1.8.1)
950 * Has to be invoked on "add" and "modify" operations on "user", "computer" and
952 * ac->msg contains the "add"/"modify" message
953 * ac->type contains the object type (main objectclass)
955 static int samldb_objectclass_trigger(struct samldb_ctx
*ac
)
957 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
958 void *skip_allocate_sids
= ldb_get_opaque(ldb
,
959 "skip_allocate_sids");
960 struct ldb_message_element
*el
, *el2
;
964 /* make sure that "sAMAccountType" is not specified */
965 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
967 ldb_set_errstring(ldb
,
968 "samldb: sAMAccountType must not be specified!");
969 return LDB_ERR_UNWILLING_TO_PERFORM
;
972 /* Step 1: objectSid assignment */
974 /* Don't allow the objectSid to be changed. But beside the RELAX
975 * control we have also to guarantee that it can always be set with
976 * SYSTEM permissions. This is needed for the "samba3sam" backend. */
977 sid
= samdb_result_dom_sid(ac
, ac
->msg
, "objectSid");
978 if ((sid
!= NULL
) && (!dsdb_module_am_system(ac
->module
)) &&
979 (ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
) == NULL
)) {
980 ldb_set_errstring(ldb
,
981 "samldb: objectSid must not be specified!");
982 return LDB_ERR_UNWILLING_TO_PERFORM
;
985 /* but generate a new SID when we do have an add operations */
986 if ((sid
== NULL
) && (ac
->req
->operation
== LDB_ADD
) && !skip_allocate_sids
) {
987 ret
= samldb_add_step(ac
, samldb_allocate_sid
);
988 if (ret
!= LDB_SUCCESS
) return ret
;
992 case SAMLDB_TYPE_USER
: {
993 bool uac_generated
= false;
995 /* Step 1.2: Default values */
996 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
997 "accountExpires", "9223372036854775807");
998 if (ret
!= LDB_SUCCESS
) return ret
;
999 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1000 "badPasswordTime", "0");
1001 if (ret
!= LDB_SUCCESS
) return ret
;
1002 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1003 "badPwdCount", "0");
1004 if (ret
!= LDB_SUCCESS
) return ret
;
1005 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1007 if (ret
!= LDB_SUCCESS
) return ret
;
1008 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1009 "countryCode", "0");
1010 if (ret
!= LDB_SUCCESS
) return ret
;
1011 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1013 if (ret
!= LDB_SUCCESS
) return ret
;
1014 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1016 if (ret
!= LDB_SUCCESS
) return ret
;
1017 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1019 if (ret
!= LDB_SUCCESS
) return ret
;
1020 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1022 if (ret
!= LDB_SUCCESS
) return ret
;
1024 /* On add operations we might need to generate a
1025 * "userAccountControl" (if it isn't specified). */
1026 el
= ldb_msg_find_element(ac
->msg
, "userAccountControl");
1027 if ((el
== NULL
) && (ac
->req
->operation
== LDB_ADD
)) {
1028 ret
= samdb_msg_set_uint(ldb
, ac
->msg
, ac
->msg
,
1029 "userAccountControl",
1031 if (ret
!= LDB_SUCCESS
) {
1034 uac_generated
= true;
1037 el
= ldb_msg_find_element(ac
->msg
, "userAccountControl");
1039 uint32_t user_account_control
, account_type
;
1041 /* Step 1.3: "userAccountControl" -> "sAMAccountType" mapping */
1042 user_account_control
= ldb_msg_find_attr_as_uint(ac
->msg
,
1043 "userAccountControl",
1046 /* Temporary duplicate accounts aren't allowed */
1047 if ((user_account_control
& UF_TEMP_DUPLICATE_ACCOUNT
) != 0) {
1048 return LDB_ERR_OTHER
;
1051 /* Workstation and (read-only) DC objects do need objectclass "computer" */
1052 if ((samdb_find_attribute(ldb
, ac
->msg
,
1053 "objectclass", "computer") == NULL
) &&
1054 (user_account_control
&
1055 (UF_SERVER_TRUST_ACCOUNT
| UF_WORKSTATION_TRUST_ACCOUNT
))) {
1056 ldb_set_errstring(ldb
,
1057 "samldb: Requested account type does need objectclass 'computer'!");
1058 return LDB_ERR_OBJECT_CLASS_VIOLATION
;
1061 account_type
= ds_uf2atype(user_account_control
);
1062 if (account_type
== 0) {
1063 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
1064 return LDB_ERR_UNWILLING_TO_PERFORM
;
1066 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1069 if (ret
!= LDB_SUCCESS
) {
1072 el2
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
1073 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1075 /* "isCriticalSystemObject" might be set */
1076 if (user_account_control
&
1077 (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
)) {
1078 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
1080 if (ret
!= LDB_SUCCESS
) {
1083 el2
= ldb_msg_find_element(ac
->msg
,
1084 "isCriticalSystemObject");
1085 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1086 } else if (user_account_control
& UF_WORKSTATION_TRUST_ACCOUNT
) {
1087 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
1089 if (ret
!= LDB_SUCCESS
) {
1092 el2
= ldb_msg_find_element(ac
->msg
,
1093 "isCriticalSystemObject");
1094 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1097 /* Step 1.4: "userAccountControl" -> "primaryGroupID" mapping */
1098 if (!ldb_msg_find_element(ac
->msg
, "primaryGroupID")) {
1099 uint32_t rid
= ds_uf2prim_group_rid(user_account_control
);
1102 * Older AD deployments don't know about the
1105 if (rid
== DOMAIN_RID_READONLY_DCS
) {
1106 ret
= samldb_prim_group_tester(ac
, rid
);
1107 if (ret
!= LDB_SUCCESS
) {
1112 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1113 "primaryGroupID", rid
);
1114 if (ret
!= LDB_SUCCESS
) {
1117 el2
= ldb_msg_find_element(ac
->msg
,
1119 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1122 /* Step 1.5: Add additional flags when needed */
1123 /* Obviously this is done when the "userAccountControl"
1124 * has been generated here (tested against Windows
1126 if (uac_generated
) {
1127 user_account_control
|= UF_ACCOUNTDISABLE
;
1128 user_account_control
|= UF_PASSWD_NOTREQD
;
1130 ret
= samdb_msg_set_uint(ldb
, ac
->msg
, ac
->msg
,
1131 "userAccountControl",
1132 user_account_control
);
1133 if (ret
!= LDB_SUCCESS
) {
1141 case SAMLDB_TYPE_GROUP
: {
1142 const char *tempstr
;
1144 /* Step 2.2: Default values */
1145 tempstr
= talloc_asprintf(ac
->msg
, "%d",
1146 GTYPE_SECURITY_GLOBAL_GROUP
);
1147 if (tempstr
== NULL
) return ldb_operr(ldb
);
1148 ret
= samdb_find_or_add_attribute(ldb
, ac
->msg
,
1149 "groupType", tempstr
);
1150 if (ret
!= LDB_SUCCESS
) return ret
;
1152 /* Step 2.3: "groupType" -> "sAMAccountType" */
1153 el
= ldb_msg_find_element(ac
->msg
, "groupType");
1155 uint32_t group_type
, account_type
;
1157 group_type
= ldb_msg_find_attr_as_uint(ac
->msg
,
1160 /* The creation of builtin groups requires the
1162 if (group_type
== GTYPE_SECURITY_BUILTIN_LOCAL_GROUP
) {
1163 if (ldb_request_get_control(ac
->req
,
1164 LDB_CONTROL_RELAX_OID
) == NULL
) {
1165 return LDB_ERR_UNWILLING_TO_PERFORM
;
1169 account_type
= ds_gtype2atype(group_type
);
1170 if (account_type
== 0) {
1171 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
1172 return LDB_ERR_UNWILLING_TO_PERFORM
;
1174 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1177 if (ret
!= LDB_SUCCESS
) {
1180 el2
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
1181 el2
->flags
= LDB_FLAG_MOD_REPLACE
;
1187 ldb_asprintf_errstring(ldb
,
1188 "Invalid entry type!");
1189 return LDB_ERR_OPERATIONS_ERROR
;
1197 * "Primary group ID" trigger (MS-SAMR 3.1.1.8.2)
1199 * Has to be invoked on "add" and "modify" operations on "user" and "computer"
1201 * ac->msg contains the "add"/"modify" message
1204 static int samldb_prim_group_tester(struct samldb_ctx
*ac
, uint32_t rid
)
1206 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1207 struct dom_sid
*sid
;
1208 struct ldb_result
*res
;
1210 const char * const noattrs
[] = { NULL
};
1212 sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), rid
);
1214 return ldb_operr(ldb
);
1217 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
1218 ldb_get_default_basedn(ldb
),
1220 noattrs
, DSDB_FLAG_NEXT_MODULE
,
1223 ldap_encode_ndr_dom_sid(ac
, sid
));
1224 if (ret
!= LDB_SUCCESS
) {
1227 if (res
->count
!= 1) {
1229 ldb_asprintf_errstring(ldb
,
1230 "Failed to find primary group with RID %u!",
1232 return LDB_ERR_UNWILLING_TO_PERFORM
;
1239 static int samldb_prim_group_set(struct samldb_ctx
*ac
)
1241 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1244 rid
= ldb_msg_find_attr_as_uint(ac
->msg
, "primaryGroupID", (uint32_t) -1);
1245 if (rid
== (uint32_t) -1) {
1246 /* we aren't affected of any primary group set */
1249 } else if (!ldb_request_get_control(ac
->req
, LDB_CONTROL_RELAX_OID
)) {
1250 ldb_set_errstring(ldb
,
1251 "The primary group isn't settable on add operations!");
1252 return LDB_ERR_UNWILLING_TO_PERFORM
;
1255 return samldb_prim_group_tester(ac
, rid
);
1258 static int samldb_prim_group_change(struct samldb_ctx
*ac
)
1260 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1261 const char * const attrs
[] = { "primaryGroupID", "memberOf", NULL
};
1262 struct ldb_result
*res
, *group_res
;
1263 struct ldb_message_element
*el
;
1264 struct ldb_message
*msg
;
1265 uint32_t prev_rid
, new_rid
;
1266 struct dom_sid
*prev_sid
, *new_sid
;
1267 struct ldb_dn
*prev_prim_group_dn
, *new_prim_group_dn
;
1269 const char * const noattrs
[] = { NULL
};
1271 el
= dsdb_get_single_valued_attr(ac
->msg
, "primaryGroupID",
1272 ac
->req
->operation
);
1274 /* we are not affected */
1278 /* Fetch information from the existing object */
1280 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, attrs
,
1281 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1282 if (ret
!= LDB_SUCCESS
) {
1286 /* Finds out the DN of the old primary group */
1288 prev_rid
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "primaryGroupID",
1290 if (prev_rid
== (uint32_t) -1) {
1291 /* User objects do always have a mandatory "primaryGroupID"
1292 * attribute. If this doesn't exist then the object is of the
1293 * wrong type. This is the exact Windows error code */
1294 return LDB_ERR_OBJECT_CLASS_VIOLATION
;
1297 prev_sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), prev_rid
);
1298 if (prev_sid
== NULL
) {
1299 return ldb_operr(ldb
);
1302 /* Finds out the DN of the new primary group
1303 * Notice: in order to parse the primary group ID correctly we create
1304 * a temporary message here. */
1306 msg
= ldb_msg_new(ac
->msg
);
1308 return ldb_module_oom(ac
->module
);
1310 ret
= ldb_msg_add(msg
, el
, 0);
1311 if (ret
!= LDB_SUCCESS
) {
1314 new_rid
= ldb_msg_find_attr_as_uint(msg
, "primaryGroupID", (uint32_t) -1);
1316 if (new_rid
== (uint32_t) -1) {
1317 /* we aren't affected of any primary group change */
1321 if (prev_rid
== new_rid
) {
1325 ret
= dsdb_module_search(ac
->module
, ac
, &group_res
,
1326 ldb_get_default_basedn(ldb
),
1328 noattrs
, DSDB_FLAG_NEXT_MODULE
,
1331 ldap_encode_ndr_dom_sid(ac
, prev_sid
));
1332 if (ret
!= LDB_SUCCESS
) {
1335 if (group_res
->count
!= 1) {
1336 return ldb_operr(ldb
);
1338 prev_prim_group_dn
= group_res
->msgs
[0]->dn
;
1340 new_sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
), new_rid
);
1341 if (new_sid
== NULL
) {
1342 return ldb_operr(ldb
);
1345 ret
= dsdb_module_search(ac
->module
, ac
, &group_res
,
1346 ldb_get_default_basedn(ldb
),
1348 noattrs
, DSDB_FLAG_NEXT_MODULE
,
1351 ldap_encode_ndr_dom_sid(ac
, new_sid
));
1352 if (ret
!= LDB_SUCCESS
) {
1355 if (group_res
->count
!= 1) {
1356 /* Here we know if the specified new primary group candidate is
1358 return LDB_ERR_UNWILLING_TO_PERFORM
;
1360 new_prim_group_dn
= group_res
->msgs
[0]->dn
;
1362 /* We need to be already a normal member of the new primary
1363 * group in order to be successful. */
1364 el
= samdb_find_attribute(ldb
, res
->msgs
[0], "memberOf",
1365 ldb_dn_get_linearized(new_prim_group_dn
));
1367 return LDB_ERR_UNWILLING_TO_PERFORM
;
1370 /* Remove the "member" attribute on the new primary group */
1371 msg
= ldb_msg_new(ac
->msg
);
1373 return ldb_module_oom(ac
->module
);
1375 msg
->dn
= new_prim_group_dn
;
1377 ret
= samdb_msg_add_delval(ldb
, msg
, msg
, "member",
1378 ldb_dn_get_linearized(ac
->msg
->dn
));
1379 if (ret
!= LDB_SUCCESS
) {
1383 ret
= dsdb_module_modify(ac
->module
, msg
, DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1384 if (ret
!= LDB_SUCCESS
) {
1389 /* Add a "member" attribute for the previous primary group */
1390 msg
= ldb_msg_new(ac
->msg
);
1392 return ldb_module_oom(ac
->module
);
1394 msg
->dn
= prev_prim_group_dn
;
1396 ret
= samdb_msg_add_addval(ldb
, msg
, msg
, "member",
1397 ldb_dn_get_linearized(ac
->msg
->dn
));
1398 if (ret
!= LDB_SUCCESS
) {
1402 ret
= dsdb_module_modify(ac
->module
, msg
, DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1403 if (ret
!= LDB_SUCCESS
) {
1411 static int samldb_prim_group_trigger(struct samldb_ctx
*ac
)
1415 if (ac
->req
->operation
== LDB_ADD
) {
1416 ret
= samldb_prim_group_set(ac
);
1418 ret
= samldb_prim_group_change(ac
);
1426 * This function is called on LDB modify operations. It performs some additions/
1427 * replaces on the current LDB message when "userAccountControl" changes.
1429 static int samldb_user_account_control_change(struct samldb_ctx
*ac
)
1431 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1432 uint32_t user_account_control
, old_user_account_control
, account_type
;
1433 struct ldb_message_element
*el
;
1434 struct ldb_message
*tmp_msg
;
1436 struct ldb_result
*res
;
1437 const char * const attrs
[] = { "userAccountControl", "objectClass", NULL
};
1439 bool is_computer
= false;
1441 el
= dsdb_get_single_valued_attr(ac
->msg
, "userAccountControl",
1442 ac
->req
->operation
);
1444 /* we are not affected */
1448 /* Create a temporary message for fetching the "userAccountControl" */
1449 tmp_msg
= ldb_msg_new(ac
->msg
);
1450 if (tmp_msg
== NULL
) {
1451 return ldb_module_oom(ac
->module
);
1453 ret
= ldb_msg_add(tmp_msg
, el
, 0);
1454 if (ret
!= LDB_SUCCESS
) {
1457 user_account_control
= ldb_msg_find_attr_as_uint(tmp_msg
,
1458 "userAccountControl",
1460 talloc_free(tmp_msg
);
1462 /* Temporary duplicate accounts aren't allowed */
1463 if ((user_account_control
& UF_TEMP_DUPLICATE_ACCOUNT
) != 0) {
1464 return LDB_ERR_OTHER
;
1467 /* Fetch the old "userAccountControl" and "objectClass" */
1468 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, attrs
,
1469 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1470 if (ret
!= LDB_SUCCESS
) {
1473 old_user_account_control
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "userAccountControl", 0);
1474 if (old_user_account_control
== 0) {
1475 return ldb_operr(ldb
);
1477 el
= ldb_msg_find_element(res
->msgs
[0], "objectClass");
1479 return ldb_operr(ldb
);
1482 /* When we do not have objectclass "computer" we cannot switch to a (read-only) DC */
1483 for (i
= 0; i
< el
->num_values
; i
++) {
1484 if (ldb_attr_cmp((char *)el
->values
[i
].data
, "computer") == 0) {
1490 (user_account_control
& (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
))) {
1491 ldb_set_errstring(ldb
,
1492 "samldb: Requested account type does need objectclass 'computer'!");
1493 return LDB_ERR_UNWILLING_TO_PERFORM
;
1497 * The functions "ds_uf2atype" and "ds_uf2prim_group_rid" are used as
1498 * detectors for account type changes.
1499 * So if the account type does change then we need to adjust the
1500 * "sAMAccountType", the "isCriticalSystemObject" and the
1501 * "primaryGroupID" attribute.
1503 if ((ds_uf2atype(user_account_control
)
1504 == ds_uf2atype(old_user_account_control
)) &&
1505 (ds_uf2prim_group_rid(user_account_control
)
1506 == ds_uf2prim_group_rid(old_user_account_control
))) {
1510 account_type
= ds_uf2atype(user_account_control
);
1511 if (account_type
== 0) {
1512 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
1513 return LDB_ERR_UNWILLING_TO_PERFORM
;
1515 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
, "sAMAccountType",
1517 if (ret
!= LDB_SUCCESS
) {
1520 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
1521 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1523 /* "isCriticalSystemObject" might be set/changed */
1524 if (user_account_control
1525 & (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
)) {
1526 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
1528 if (ret
!= LDB_SUCCESS
) {
1531 el
= ldb_msg_find_element(ac
->msg
,
1532 "isCriticalSystemObject");
1533 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1534 } else if (user_account_control
& UF_WORKSTATION_TRUST_ACCOUNT
) {
1535 ret
= ldb_msg_add_string(ac
->msg
, "isCriticalSystemObject",
1537 if (ret
!= LDB_SUCCESS
) {
1540 el
= ldb_msg_find_element(ac
->msg
,
1541 "isCriticalSystemObject");
1542 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1545 if (!ldb_msg_find_element(ac
->msg
, "primaryGroupID")) {
1546 uint32_t rid
= ds_uf2prim_group_rid(user_account_control
);
1548 /* Older AD deployments don't know about the RODC group */
1549 if (rid
== DOMAIN_RID_READONLY_DCS
) {
1550 ret
= samldb_prim_group_tester(ac
, rid
);
1551 if (ret
!= LDB_SUCCESS
) {
1556 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
,
1557 "primaryGroupID", rid
);
1558 if (ret
!= LDB_SUCCESS
) {
1561 el
= ldb_msg_find_element(ac
->msg
,
1563 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1569 static int samldb_group_type_change(struct samldb_ctx
*ac
)
1571 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1572 uint32_t group_type
, old_group_type
, account_type
;
1573 struct ldb_message_element
*el
;
1574 struct ldb_message
*tmp_msg
;
1576 struct ldb_result
*res
;
1577 const char * const attrs
[] = { "groupType", NULL
};
1579 el
= dsdb_get_single_valued_attr(ac
->msg
, "groupType",
1580 ac
->req
->operation
);
1582 /* we are not affected */
1586 /* Create a temporary message for fetching the "groupType" */
1587 tmp_msg
= ldb_msg_new(ac
->msg
);
1588 if (tmp_msg
== NULL
) {
1589 return ldb_module_oom(ac
->module
);
1591 ret
= ldb_msg_add(tmp_msg
, el
, 0);
1592 if (ret
!= LDB_SUCCESS
) {
1595 group_type
= ldb_msg_find_attr_as_uint(tmp_msg
, "groupType", 0);
1596 talloc_free(tmp_msg
);
1598 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, attrs
,
1599 DSDB_FLAG_NEXT_MODULE
|
1600 DSDB_SEARCH_SHOW_DELETED
, ac
->req
);
1601 if (ret
!= LDB_SUCCESS
) {
1604 old_group_type
= ldb_msg_find_attr_as_uint(res
->msgs
[0], "groupType", 0);
1605 if (old_group_type
== 0) {
1606 return ldb_operr(ldb
);
1609 /* Group type switching isn't so easy as it seems: We can only
1610 * change in this directions: global <-> universal <-> local
1611 * On each step also the group type itself
1612 * (security/distribution) is variable. */
1614 if (ldb_request_get_control(ac
->req
, LDB_CONTROL_PROVISION_OID
) == NULL
) {
1615 switch (group_type
) {
1616 case GTYPE_SECURITY_GLOBAL_GROUP
:
1617 case GTYPE_DISTRIBUTION_GLOBAL_GROUP
:
1618 /* change to "universal" allowed */
1619 if ((old_group_type
== GTYPE_SECURITY_DOMAIN_LOCAL_GROUP
) ||
1620 (old_group_type
== GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP
)) {
1621 ldb_set_errstring(ldb
,
1622 "samldb: Change from security/distribution local group forbidden!");
1623 return LDB_ERR_UNWILLING_TO_PERFORM
;
1627 case GTYPE_SECURITY_UNIVERSAL_GROUP
:
1628 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP
:
1629 /* each change allowed */
1631 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP
:
1632 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP
:
1633 /* change to "universal" allowed */
1634 if ((old_group_type
== GTYPE_SECURITY_GLOBAL_GROUP
) ||
1635 (old_group_type
== GTYPE_DISTRIBUTION_GLOBAL_GROUP
)) {
1636 ldb_set_errstring(ldb
,
1637 "samldb: Change from security/distribution global group forbidden!");
1638 return LDB_ERR_UNWILLING_TO_PERFORM
;
1642 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP
:
1644 /* we don't allow this "groupType" values */
1645 return LDB_ERR_UNWILLING_TO_PERFORM
;
1650 account_type
= ds_gtype2atype(group_type
);
1651 if (account_type
== 0) {
1652 ldb_set_errstring(ldb
, "samldb: Unrecognized account type!");
1653 return LDB_ERR_UNWILLING_TO_PERFORM
;
1655 ret
= samdb_msg_add_uint(ldb
, ac
->msg
, ac
->msg
, "sAMAccountType",
1657 if (ret
!= LDB_SUCCESS
) {
1660 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountType");
1661 el
->flags
= LDB_FLAG_MOD_REPLACE
;
1666 static int samldb_sam_accountname_check(struct samldb_ctx
*ac
)
1668 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1669 const char * const no_attrs
[] = { NULL
};
1670 struct ldb_result
*res
;
1671 const char *sam_accountname
, *enc_str
;
1672 struct ldb_message_element
*el
;
1673 struct ldb_message
*tmp_msg
;
1676 el
= dsdb_get_single_valued_attr(ac
->msg
, "sAMAccountName",
1677 ac
->req
->operation
);
1679 /* we are not affected */
1683 /* Create a temporary message for fetching the "sAMAccountName" */
1684 tmp_msg
= ldb_msg_new(ac
->msg
);
1685 if (tmp_msg
== NULL
) {
1686 return ldb_module_oom(ac
->module
);
1688 ret
= ldb_msg_add(tmp_msg
, el
, 0);
1689 if (ret
!= LDB_SUCCESS
) {
1693 /* We must not steal the original string, it belongs to the caller! */
1694 sam_accountname
= talloc_strdup(ac
,
1695 ldb_msg_find_attr_as_string(tmp_msg
, "sAMAccountName", NULL
));
1696 talloc_free(tmp_msg
);
1698 if (sam_accountname
== NULL
) {
1699 /* The "sAMAccountName" cannot be nothing */
1700 ldb_set_errstring(ldb
,
1701 "samldb: Empty account names aren't allowed!");
1702 return LDB_ERR_UNWILLING_TO_PERFORM
;
1705 enc_str
= ldb_binary_encode_string(ac
, sam_accountname
);
1706 if (enc_str
== NULL
) {
1707 return ldb_module_oom(ac
->module
);
1710 /* Make sure that a "sAMAccountName" is only used once */
1712 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
1713 ldb_get_default_basedn(ldb
),
1714 LDB_SCOPE_SUBTREE
, no_attrs
,
1715 DSDB_FLAG_NEXT_MODULE
, ac
->req
,
1716 "(sAMAccountName=%s)", enc_str
);
1717 if (ret
!= LDB_SUCCESS
) {
1720 if (res
->count
> 1) {
1721 return ldb_operr(ldb
);
1722 } else if (res
->count
== 1) {
1723 if (ldb_dn_compare(res
->msgs
[0]->dn
, ac
->msg
->dn
) != 0) {
1724 ldb_asprintf_errstring(ldb
,
1725 "samldb: Account name (sAMAccountName) '%s' already in use!",
1727 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
1735 static int samldb_member_check(struct samldb_ctx
*ac
)
1737 const char * const attrs
[] = { "objectSid", NULL
};
1738 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1739 struct ldb_message_element
*el
;
1740 struct ldb_dn
*member_dn
;
1741 struct dom_sid
*sid
;
1742 struct ldb_result
*res
;
1743 struct dom_sid
*group_sid
;
1747 /* Fetch information from the existing object */
1749 ret
= dsdb_module_search(ac
->module
, ac
, &res
, ac
->msg
->dn
, LDB_SCOPE_BASE
, attrs
,
1750 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
, ac
->req
, NULL
);
1751 if (ret
!= LDB_SUCCESS
) {
1754 if (res
->count
!= 1) {
1755 return ldb_operr(ldb
);
1758 group_sid
= samdb_result_dom_sid(res
, res
->msgs
[0], "objectSid");
1759 if (group_sid
== NULL
) {
1760 return ldb_operr(ldb
);
1763 /* We've to walk over all modification entries and consider the "member"
1765 for (i
= 0; i
< ac
->msg
->num_elements
; i
++) {
1766 if (ldb_attr_cmp(ac
->msg
->elements
[i
].name
, "member") != 0) {
1770 el
= &ac
->msg
->elements
[i
];
1771 for (j
= 0; j
< el
->num_values
; j
++) {
1772 struct ldb_result
*group_res
;
1773 const char *group_attrs
[] = { "primaryGroupID" , NULL
};
1774 uint32_t prim_group_rid
;
1776 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_DELETE
) {
1777 /* Deletes will be handled in
1778 * repl_meta_data, and deletes not
1779 * matching a member will return
1780 * LDB_ERR_UNWILLING_TO_PERFORM
1785 member_dn
= ldb_dn_from_ldb_val(ac
, ldb
,
1787 if (!ldb_dn_validate(member_dn
)) {
1788 return ldb_operr(ldb
);
1791 /* Denies to add "member"s to groups which are primary
1792 * ones for them - in this case return
1793 * ERR_ENTRY_ALREADY_EXISTS. */
1795 ret
= dsdb_module_search_dn(ac
->module
, ac
, &group_res
,
1796 member_dn
, group_attrs
,
1797 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1798 if (ret
== LDB_ERR_NO_SUCH_OBJECT
) {
1799 /* member DN doesn't exist yet */
1802 if (ret
!= LDB_SUCCESS
) {
1805 prim_group_rid
= ldb_msg_find_attr_as_uint(group_res
->msgs
[0], "primaryGroupID", (uint32_t)-1);
1806 if (prim_group_rid
== (uint32_t) -1) {
1807 /* the member hasn't to be a user account ->
1808 * therefore no check needed in this case. */
1812 sid
= dom_sid_add_rid(ac
, samdb_domain_sid(ldb
),
1815 return ldb_operr(ldb
);
1818 if (dom_sid_equal(group_sid
, sid
)) {
1819 ldb_asprintf_errstring(ldb
,
1820 "samldb: member %s already set via primaryGroupID %u",
1821 ldb_dn_get_linearized(member_dn
), prim_group_rid
);
1822 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
1832 /* SAM objects have special rules regarding the "description" attribute on
1833 * modify operations. */
1834 static int samldb_description_check(struct samldb_ctx
*ac
, bool *modified
)
1836 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1837 const char * const attrs
[] = { "objectClass", "description", NULL
};
1838 struct ldb_result
*res
;
1842 /* Fetch information from the existing object */
1843 ret
= dsdb_module_search(ac
->module
, ac
, &res
, ac
->msg
->dn
, LDB_SCOPE_BASE
, attrs
,
1844 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
, ac
->req
,
1845 "(|(objectclass=user)(objectclass=group)(objectclass=samDomain)(objectclass=samServer))");
1846 if (ret
!= LDB_SUCCESS
) {
1847 /* don't treat it specially ... let normal error codes
1848 happen from other places */
1849 ldb_reset_err_string(ldb
);
1852 if (res
->count
== 0) {
1853 /* we didn't match the filter */
1858 /* We've to walk over all modification entries and consider the
1859 * "description" ones. */
1860 for (i
= 0; i
< ac
->msg
->num_elements
; i
++) {
1861 if (ldb_attr_cmp(ac
->msg
->elements
[i
].name
, "description") == 0) {
1862 ac
->msg
->elements
[i
].flags
|= LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK
;
1872 /* This trigger adapts the "servicePrincipalName" attributes if the
1873 * "dNSHostName" and/or "sAMAccountName" attribute change(s) */
1874 static int samldb_service_principal_names_change(struct samldb_ctx
*ac
)
1876 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
1877 struct ldb_message_element
*el
= NULL
, *el2
= NULL
;
1878 struct ldb_message
*msg
;
1879 const char * const attrs
[] = { "servicePrincipalName", NULL
};
1880 struct ldb_result
*res
;
1881 const char *dns_hostname
= NULL
, *old_dns_hostname
= NULL
,
1882 *sam_accountname
= NULL
, *old_sam_accountname
= NULL
;
1886 el
= dsdb_get_single_valued_attr(ac
->msg
, "dNSHostName",
1887 ac
->req
->operation
);
1888 el2
= dsdb_get_single_valued_attr(ac
->msg
, "sAMAccountName",
1889 ac
->req
->operation
);
1890 if ((el
== NULL
) && (el2
== NULL
)) {
1891 /* we are not affected */
1895 /* Create a temporary message for fetching the "dNSHostName" */
1897 const char *dns_attrs
[] = { "dNSHostName", NULL
};
1898 msg
= ldb_msg_new(ac
->msg
);
1900 return ldb_module_oom(ac
->module
);
1902 ret
= ldb_msg_add(msg
, el
, 0);
1903 if (ret
!= LDB_SUCCESS
) {
1906 dns_hostname
= talloc_strdup(ac
,
1907 ldb_msg_find_attr_as_string(msg
, "dNSHostName", NULL
));
1908 if (dns_hostname
== NULL
) {
1909 return ldb_module_oom(ac
->module
);
1914 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
,
1915 dns_attrs
, DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1916 if (ret
== LDB_SUCCESS
) {
1917 old_dns_hostname
= ldb_msg_find_attr_as_string(res
->msgs
[0], "dNSHostName", NULL
);
1921 /* Create a temporary message for fetching the "sAMAccountName" */
1923 char *tempstr
, *tempstr2
;
1924 const char *acct_attrs
[] = { "sAMAccountName", NULL
};
1926 msg
= ldb_msg_new(ac
->msg
);
1928 return ldb_module_oom(ac
->module
);
1930 ret
= ldb_msg_add(msg
, el2
, 0);
1931 if (ret
!= LDB_SUCCESS
) {
1934 tempstr
= talloc_strdup(ac
,
1935 ldb_msg_find_attr_as_string(msg
, "sAMAccountName", NULL
));
1938 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->msg
->dn
, acct_attrs
,
1939 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
1940 if (ret
== LDB_SUCCESS
) {
1941 tempstr2
= talloc_strdup(ac
,
1942 ldb_msg_find_attr_as_string(res
->msgs
[0],
1943 "sAMAccountName", NULL
));
1947 /* The "sAMAccountName" needs some additional trimming: we need
1948 * to remove the trailing "$"s if they exist. */
1949 if ((tempstr
!= NULL
) && (tempstr
[0] != '\0') &&
1950 (tempstr
[strlen(tempstr
) - 1] == '$')) {
1951 tempstr
[strlen(tempstr
) - 1] = '\0';
1953 if ((tempstr2
!= NULL
) && (tempstr2
[0] != '\0') &&
1954 (tempstr2
[strlen(tempstr2
) - 1] == '$')) {
1955 tempstr2
[strlen(tempstr2
) - 1] = '\0';
1957 sam_accountname
= tempstr
;
1958 old_sam_accountname
= tempstr2
;
1961 if (old_dns_hostname
== NULL
) {
1962 /* we cannot change when the old name is unknown */
1963 dns_hostname
= NULL
;
1965 if ((old_dns_hostname
!= NULL
) && (dns_hostname
!= NULL
) &&
1966 (strcasecmp_m(old_dns_hostname
, dns_hostname
) == 0)) {
1967 /* The "dNSHostName" didn't change */
1968 dns_hostname
= NULL
;
1971 if (old_sam_accountname
== NULL
) {
1972 /* we cannot change when the old name is unknown */
1973 sam_accountname
= NULL
;
1975 if ((old_sam_accountname
!= NULL
) && (sam_accountname
!= NULL
) &&
1976 (strcasecmp_m(old_sam_accountname
, sam_accountname
) == 0)) {
1977 /* The "sAMAccountName" didn't change */
1978 sam_accountname
= NULL
;
1981 if ((dns_hostname
== NULL
) && (sam_accountname
== NULL
)) {
1982 /* Well, there are information missing (old name(s)) or the
1983 * names didn't change. We've nothing to do and can exit here */
1987 /* Potential "servicePrincipalName" changes in the same request have to
1988 * be handled before the update (Windows behaviour). */
1989 el
= ldb_msg_find_element(ac
->msg
, "servicePrincipalName");
1991 msg
= ldb_msg_new(ac
->msg
);
1993 return ldb_module_oom(ac
->module
);
1995 msg
->dn
= ac
->msg
->dn
;
1998 ret
= ldb_msg_add(msg
, el
, el
->flags
);
1999 if (ret
!= LDB_SUCCESS
) {
2003 ldb_msg_remove_element(ac
->msg
, el
);
2005 el
= ldb_msg_find_element(ac
->msg
,
2006 "servicePrincipalName");
2007 } while (el
!= NULL
);
2009 ret
= dsdb_module_modify(ac
->module
, msg
,
2010 DSDB_FLAG_NEXT_MODULE
, ac
->req
);
2011 if (ret
!= LDB_SUCCESS
) {
2017 /* Fetch the "servicePrincipalName"s if any */
2018 ret
= dsdb_module_search(ac
->module
, ac
, &res
, ac
->msg
->dn
, LDB_SCOPE_BASE
, attrs
,
2019 DSDB_FLAG_NEXT_MODULE
, ac
->req
, NULL
);
2020 if (ret
!= LDB_SUCCESS
) {
2023 if ((res
->count
!= 1) || (res
->msgs
[0]->num_elements
> 1)) {
2024 return ldb_operr(ldb
);
2027 if (res
->msgs
[0]->num_elements
== 1) {
2029 * Yes, we do have "servicePrincipalName"s. First we update them
2030 * locally, that means we do always substitute the current
2031 * "dNSHostName" with the new one and/or "sAMAccountName"
2032 * without "$" with the new one and then we append the
2033 * modified "servicePrincipalName"s as a message element
2034 * replace to the modification request (Windows behaviour). We
2035 * need also to make sure that the values remain case-
2036 * insensitively unique.
2039 ret
= ldb_msg_add_empty(ac
->msg
, "servicePrincipalName",
2040 LDB_FLAG_MOD_REPLACE
, &el
);
2041 if (ret
!= LDB_SUCCESS
) {
2045 for (i
= 0; i
< res
->msgs
[0]->elements
[0].num_values
; i
++) {
2046 char *old_str
, *new_str
, *pos
;
2048 struct ldb_val
*vals
;
2052 res
->msgs
[0]->elements
[0].values
[i
].data
;
2054 new_str
= talloc_strdup(ac
->msg
,
2055 strtok_r(old_str
, "/", &pos
));
2056 if (new_str
== NULL
) {
2057 return ldb_module_oom(ac
->module
);
2060 while ((tok
= strtok_r(NULL
, "/", &pos
)) != NULL
) {
2061 if ((dns_hostname
!= NULL
) &&
2062 (strcasecmp_m(tok
, old_dns_hostname
) == 0)) {
2065 if ((sam_accountname
!= NULL
) &&
2066 (strcasecmp_m(tok
, old_sam_accountname
) == 0)) {
2067 tok
= sam_accountname
;
2070 new_str
= talloc_asprintf(ac
->msg
, "%s/%s",
2072 if (new_str
== NULL
) {
2073 return ldb_module_oom(ac
->module
);
2077 /* Uniqueness check */
2078 for (j
= 0; (!found
) && (j
< el
->num_values
); j
++) {
2079 if (strcasecmp_m((char *)el
->values
[j
].data
,
2089 * append the new "servicePrincipalName" -
2090 * code derived from ldb_msg_add_value().
2092 * Open coded to make it clear that we must
2093 * append to the MOD_REPLACE el created above.
2095 vals
= talloc_realloc(ac
->msg
, el
->values
,
2097 el
->num_values
+ 1);
2099 return ldb_module_oom(ac
->module
);
2102 el
->values
[el
->num_values
] = data_blob_string_const(new_str
);
2112 /* This checks the "fSMORoleOwner" attributes */
2113 static int samldb_fsmo_role_owner_check(struct samldb_ctx
*ac
)
2115 struct ldb_context
*ldb
= ldb_module_get_ctx(ac
->module
);
2116 const char * const no_attrs
[] = { NULL
};
2117 struct ldb_message_element
*el
;
2118 struct ldb_message
*tmp_msg
;
2119 struct ldb_dn
*res_dn
;
2120 struct ldb_result
*res
;
2123 el
= dsdb_get_single_valued_attr(ac
->msg
, "fSMORoleOwner",
2124 ac
->req
->operation
);
2126 /* we are not affected */
2130 /* Create a temporary message for fetching the "fSMORoleOwner" */
2131 tmp_msg
= ldb_msg_new(ac
->msg
);
2132 if (tmp_msg
== NULL
) {
2133 return ldb_module_oom(ac
->module
);
2135 ret
= ldb_msg_add(tmp_msg
, el
, 0);
2136 if (ret
!= LDB_SUCCESS
) {
2139 res_dn
= ldb_msg_find_attr_as_dn(ldb
, ac
, tmp_msg
, "fSMORoleOwner");
2140 talloc_free(tmp_msg
);
2142 if (res_dn
== NULL
) {
2143 ldb_set_errstring(ldb
,
2144 "samldb: 'fSMORoleOwner' attributes have to reference 'nTDSDSA' entries!");
2145 if (ac
->req
->operation
== LDB_ADD
) {
2146 return LDB_ERR_CONSTRAINT_VIOLATION
;
2148 return LDB_ERR_UNWILLING_TO_PERFORM
;
2152 /* Fetched DN has to reference a "nTDSDSA" entry */
2153 ret
= dsdb_module_search(ac
->module
, ac
, &res
, res_dn
, LDB_SCOPE_BASE
,
2155 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
,
2156 ac
->req
, "(objectClass=nTDSDSA)");
2157 if (ret
!= LDB_SUCCESS
) {
2160 if (res
->count
!= 1) {
2161 ldb_set_errstring(ldb
,
2162 "samldb: 'fSMORoleOwner' attributes have to reference 'nTDSDSA' entries!");
2163 return LDB_ERR_UNWILLING_TO_PERFORM
;
2173 static int samldb_add(struct ldb_module
*module
, struct ldb_request
*req
)
2175 struct ldb_context
*ldb
;
2176 struct samldb_ctx
*ac
;
2177 struct ldb_message_element
*el
;
2180 ldb
= ldb_module_get_ctx(module
);
2181 ldb_debug(ldb
, LDB_DEBUG_TRACE
, "samldb_add\n");
2183 /* do not manipulate our control entries */
2184 if (ldb_dn_is_special(req
->op
.add
.message
->dn
)) {
2185 return ldb_next_request(module
, req
);
2188 ac
= samldb_ctx_init(module
, req
);
2190 return ldb_operr(ldb
);
2193 /* build the new msg */
2194 ac
->msg
= ldb_msg_copy_shallow(ac
, req
->op
.add
.message
);
2195 if (ac
->msg
== NULL
) {
2197 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
2198 "samldb_add: ldb_msg_copy_shallow failed!\n");
2199 return ldb_operr(ldb
);
2202 el
= ldb_msg_find_element(ac
->msg
, "fSMORoleOwner");
2204 ret
= samldb_fsmo_role_owner_check(ac
);
2205 if (ret
!= LDB_SUCCESS
) {
2210 if (samdb_find_attribute(ldb
, ac
->msg
,
2211 "objectclass", "user") != NULL
) {
2212 ac
->type
= SAMLDB_TYPE_USER
;
2214 ret
= samldb_prim_group_trigger(ac
);
2215 if (ret
!= LDB_SUCCESS
) {
2219 ret
= samldb_objectclass_trigger(ac
);
2220 if (ret
!= LDB_SUCCESS
) {
2224 return samldb_fill_object(ac
);
2227 if (samdb_find_attribute(ldb
, ac
->msg
,
2228 "objectclass", "group") != NULL
) {
2229 ac
->type
= SAMLDB_TYPE_GROUP
;
2231 ret
= samldb_objectclass_trigger(ac
);
2232 if (ret
!= LDB_SUCCESS
) {
2236 return samldb_fill_object(ac
);
2239 /* perhaps a foreignSecurityPrincipal? */
2240 if (samdb_find_attribute(ldb
, ac
->msg
,
2242 "foreignSecurityPrincipal") != NULL
) {
2243 return samldb_fill_foreignSecurityPrincipal_object(ac
);
2246 if (samdb_find_attribute(ldb
, ac
->msg
,
2247 "objectclass", "classSchema") != NULL
) {
2248 ret
= samldb_schema_info_update(ac
);
2249 if (ret
!= LDB_SUCCESS
) {
2254 ac
->type
= SAMLDB_TYPE_CLASS
;
2255 return samldb_fill_object(ac
);
2258 if (samdb_find_attribute(ldb
, ac
->msg
,
2259 "objectclass", "attributeSchema") != NULL
) {
2260 ret
= samldb_schema_info_update(ac
);
2261 if (ret
!= LDB_SUCCESS
) {
2266 ac
->type
= SAMLDB_TYPE_ATTRIBUTE
;
2267 return samldb_fill_object(ac
);
2272 /* nothing matched, go on */
2273 return ldb_next_request(module
, req
);
2277 static int samldb_modify(struct ldb_module
*module
, struct ldb_request
*req
)
2279 struct ldb_context
*ldb
;
2280 struct samldb_ctx
*ac
;
2281 struct ldb_message_element
*el
, *el2
;
2282 bool modified
= false;
2285 if (ldb_dn_is_special(req
->op
.mod
.message
->dn
)) {
2286 /* do not manipulate our control entries */
2287 return ldb_next_request(module
, req
);
2290 ldb
= ldb_module_get_ctx(module
);
2292 /* make sure that "objectSid" is not specified */
2293 el
= ldb_msg_find_element(req
->op
.mod
.message
, "objectSid");
2295 if (ldb_request_get_control(req
, LDB_CONTROL_PROVISION_OID
) == NULL
) {
2296 ldb_set_errstring(ldb
,
2297 "samldb: objectSid must not be specified!");
2298 return LDB_ERR_UNWILLING_TO_PERFORM
;
2301 /* make sure that "sAMAccountType" is not specified */
2302 el
= ldb_msg_find_element(req
->op
.mod
.message
, "sAMAccountType");
2304 ldb_set_errstring(ldb
,
2305 "samldb: sAMAccountType must not be specified!");
2306 return LDB_ERR_UNWILLING_TO_PERFORM
;
2308 /* make sure that "isCriticalSystemObject" is not specified */
2309 el
= ldb_msg_find_element(req
->op
.mod
.message
, "isCriticalSystemObject");
2311 if (ldb_request_get_control(req
, LDB_CONTROL_RELAX_OID
) == NULL
) {
2312 ldb_set_errstring(ldb
,
2313 "samldb: isCriticalSystemObject must not be specified!");
2314 return LDB_ERR_UNWILLING_TO_PERFORM
;
2318 /* msDS-IntId is not allowed to be modified
2319 * except when modification comes from replication */
2320 if (ldb_msg_find_element(req
->op
.mod
.message
, "msDS-IntId")) {
2321 if (!ldb_request_get_control(req
,
2322 DSDB_CONTROL_REPLICATED_UPDATE_OID
)) {
2323 return LDB_ERR_CONSTRAINT_VIOLATION
;
2327 ac
= samldb_ctx_init(module
, req
);
2329 return ldb_operr(ldb
);
2332 /* build the new msg */
2333 ac
->msg
= ldb_msg_copy_shallow(ac
, req
->op
.mod
.message
);
2334 if (ac
->msg
== NULL
) {
2336 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
2337 "samldb_modify: ldb_msg_copy_shallow failed!\n");
2338 return ldb_operr(ldb
);
2341 el
= ldb_msg_find_element(ac
->msg
, "primaryGroupID");
2343 ret
= samldb_prim_group_trigger(ac
);
2344 if (ret
!= LDB_SUCCESS
) {
2349 el
= ldb_msg_find_element(ac
->msg
, "userAccountControl");
2352 ret
= samldb_user_account_control_change(ac
);
2353 if (ret
!= LDB_SUCCESS
) {
2358 el
= ldb_msg_find_element(ac
->msg
, "groupType");
2361 ret
= samldb_group_type_change(ac
);
2362 if (ret
!= LDB_SUCCESS
) {
2367 el
= ldb_msg_find_element(ac
->msg
, "sAMAccountName");
2369 ret
= samldb_sam_accountname_check(ac
);
2370 if (ret
!= LDB_SUCCESS
) {
2375 el
= ldb_msg_find_element(ac
->msg
, "member");
2377 ret
= samldb_member_check(ac
);
2378 if (ret
!= LDB_SUCCESS
) {
2383 el
= ldb_msg_find_element(ac
->msg
, "description");
2385 ret
= samldb_description_check(ac
, &modified
);
2386 if (ret
!= LDB_SUCCESS
) {
2391 el
= ldb_msg_find_element(ac
->msg
, "dNSHostName");
2392 el2
= ldb_msg_find_element(ac
->msg
, "sAMAccountName");
2393 if ((el
!= NULL
) || (el2
!= NULL
)) {
2395 ret
= samldb_service_principal_names_change(ac
);
2396 if (ret
!= LDB_SUCCESS
) {
2401 el
= ldb_msg_find_element(ac
->msg
, "fSMORoleOwner");
2403 ret
= samldb_fsmo_role_owner_check(ac
);
2404 if (ret
!= LDB_SUCCESS
) {
2410 struct ldb_request
*child_req
;
2412 /* Now perform the real modifications as a child request */
2413 ret
= ldb_build_mod_req(&child_req
, ldb
, ac
,
2416 req
, dsdb_next_callback
,
2418 LDB_REQ_SET_LOCATION(child_req
);
2419 if (ret
!= LDB_SUCCESS
) {
2423 return ldb_next_request(module
, child_req
);
2428 /* no change which interests us, go on */
2429 return ldb_next_request(module
, req
);
2434 static int samldb_prim_group_users_check(struct samldb_ctx
*ac
)
2436 struct ldb_context
*ldb
;
2437 struct dom_sid
*sid
;
2441 struct ldb_result
*res
;
2442 const char * const attrs
[] = { "objectSid", "isDeleted", NULL
};
2443 const char * const noattrs
[] = { NULL
};
2445 ldb
= ldb_module_get_ctx(ac
->module
);
2447 /* Finds out the SID/RID of the SAM object */
2448 ret
= dsdb_module_search_dn(ac
->module
, ac
, &res
, ac
->req
->op
.del
.dn
,
2450 DSDB_FLAG_NEXT_MODULE
| DSDB_SEARCH_SHOW_DELETED
,
2452 if (ret
!= LDB_SUCCESS
) {
2456 if (ldb_msg_check_string_attribute(res
->msgs
[0], "isDeleted", "TRUE")) {
2460 sid
= samdb_result_dom_sid(ac
, res
->msgs
[0], "objectSid");
2462 /* No SID - it might not be a SAM object - therefore ok */
2465 status
= dom_sid_split_rid(ac
, sid
, NULL
, &rid
);
2466 if (!NT_STATUS_IS_OK(status
)) {
2467 return ldb_operr(ldb
);
2470 /* Special object (security principal?) */
2474 /* Deny delete requests from groups which are primary ones */
2475 ret
= dsdb_module_search(ac
->module
, ac
, &res
,
2476 ldb_get_default_basedn(ldb
),
2477 LDB_SCOPE_SUBTREE
, noattrs
,
2478 DSDB_FLAG_NEXT_MODULE
,
2480 "(&(primaryGroupID=%u)(objectClass=user))", rid
);
2481 if (ret
!= LDB_SUCCESS
) {
2484 if (res
->count
> 0) {
2485 return LDB_ERR_ENTRY_ALREADY_EXISTS
;
2491 static int samldb_delete(struct ldb_module
*module
, struct ldb_request
*req
)
2493 struct samldb_ctx
*ac
;
2496 if (ldb_dn_is_special(req
->op
.del
.dn
)) {
2497 /* do not manipulate our control entries */
2498 return ldb_next_request(module
, req
);
2501 ac
= samldb_ctx_init(module
, req
);
2503 return ldb_operr(ldb_module_get_ctx(module
));
2506 ret
= samldb_prim_group_users_check(ac
);
2507 if (ret
!= LDB_SUCCESS
) {
2513 return ldb_next_request(module
, req
);
2518 static int samldb_extended_allocate_rid_pool(struct ldb_module
*module
, struct ldb_request
*req
)
2520 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
2521 struct dsdb_fsmo_extended_op
*exop
;
2524 exop
= talloc_get_type(req
->op
.extended
.data
,
2525 struct dsdb_fsmo_extended_op
);
2527 ldb_set_errstring(ldb
,
2528 "samldb_extended_allocate_rid_pool: invalid extended data");
2529 return LDB_ERR_PROTOCOL_ERROR
;
2532 ret
= ridalloc_allocate_rid_pool_fsmo(module
, exop
, req
);
2533 if (ret
!= LDB_SUCCESS
) {
2537 return ldb_module_done(req
, NULL
, NULL
, LDB_SUCCESS
);
2540 static int samldb_extended(struct ldb_module
*module
, struct ldb_request
*req
)
2542 if (strcmp(req
->op
.extended
.oid
, DSDB_EXTENDED_ALLOCATE_RID_POOL
) == 0) {
2543 return samldb_extended_allocate_rid_pool(module
, req
);
2546 return ldb_next_request(module
, req
);
2550 static const struct ldb_module_ops ldb_samldb_module_ops
= {
2553 .modify
= samldb_modify
,
2554 .del
= samldb_delete
,
2555 .extended
= samldb_extended
2559 int ldb_samldb_module_init(const char *version
)
2561 LDB_MODULE_CHECK_VERSION(version
);
2562 return ldb_register_module(&ldb_samldb_module_ops
);