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 ldb_get_default_basedn(ldb), 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,
264 ldb_get_default_basedn(ldb_module_get_ctx(ac->module)),
265 LDB_SCOPE_SUBTREE, no_attrs,
266 DSDB_FLAG_NEXT_MODULE,
268 "(msDC-SecondaryKrbTgtNumber=%u)",
270 if (ret == LDB_SUCCESS && res->count == 0) {
271 talloc_free(tmp_ctx);
274 talloc_free(tmp_ctx);
278 /* special handling for add in RODC join */
279 static int samldb_rodc_add(struct samldb_ctx *ac)
281 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
282 uint32_t krbtgt_number, i_start, i;
285 struct ldb_val newpass_utf16;
287 /* find a unused msDC-SecondaryKrbTgtNumber */
288 i_start = generate_random() & 0xFFFF;
293 for (i=i_start; i<=0xFFFF; i++) {
294 if (samldb_krbtgtnumber_available(ac, i)) {
299 for (i=1; i<i_start; i++) {
300 if (samldb_krbtgtnumber_available(ac, i)) {
306 ldb_asprintf_errstring(ldb,
307 "%08X: Unable to find available msDS-SecondaryKrbTgtNumber",
308 W_ERROR_V(WERR_NO_SYSTEM_RESOURCES));
309 return LDB_ERR_OTHER;
312 ret = ldb_msg_add_empty(ac->msg, "msDS-SecondaryKrbTgtNumber",
313 LDB_FLAG_INTERNAL_DISABLE_VALIDATION, NULL);
314 if (ret != LDB_SUCCESS) {
315 return ldb_operr(ldb);
318 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
319 "msDS-SecondaryKrbTgtNumber", krbtgt_number);
320 if (ret != LDB_SUCCESS) {
321 return ldb_operr(ldb);
324 ret = ldb_msg_add_fmt(ac->msg, "sAMAccountName", "krbtgt_%u",
326 if (ret != LDB_SUCCESS) {
327 return ldb_operr(ldb);
330 newpass = generate_random_password(ac->msg, 128, 255);
331 if (newpass == NULL) {
332 return ldb_operr(ldb);
335 if (!convert_string_talloc(ac,
337 newpass, strlen(newpass),
338 (void *)&newpass_utf16.data,
339 &newpass_utf16.length)) {
340 ldb_asprintf_errstring(ldb,
342 "failed to generate UTF16 password from random password");
343 return LDB_ERR_OPERATIONS_ERROR;
345 ret = ldb_msg_add_steal_value(ac->msg, "clearTextPassword", &newpass_utf16);
346 if (ret != LDB_SUCCESS) {
347 return ldb_operr(ldb);
350 return samldb_next_step(ac);
353 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
355 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
356 struct ldb_result *res;
357 const char *no_attrs[] = { NULL };
362 ret = dsdb_module_search(ac->module, ac, &res,
363 ac->dn, LDB_SCOPE_BASE, no_attrs,
364 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
365 | DSDB_FLAG_NEXT_MODULE,
367 "(objectClass=classSchema)");
368 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
369 /* Don't be pricky when the DN doesn't exist if we have the */
370 /* RELAX control specified */
371 if (ldb_request_get_control(ac->req,
372 LDB_CONTROL_RELAX_OID) == NULL) {
373 ldb_set_errstring(ldb,
374 "samldb_find_defaultObjectCategory: "
375 "Invalid DN for 'defaultObjectCategory'!");
376 return LDB_ERR_CONSTRAINT_VIOLATION;
379 if ((ret != LDB_ERR_NO_SUCH_OBJECT) && (ret != LDB_SUCCESS)) {
383 if (ret == LDB_SUCCESS) {
384 /* ensure the defaultObjectCategory has a full GUID */
385 struct ldb_message *m;
386 m = ldb_msg_new(ac->msg);
391 if (ldb_msg_add_string(m, "defaultObjectCategory",
392 ldb_dn_get_extended_linearized(m, res->msgs[0]->dn, 1)) !=
396 m->elements[0].flags = LDB_FLAG_MOD_REPLACE;
398 ret = dsdb_module_modify(ac->module, m,
399 DSDB_FLAG_NEXT_MODULE,
401 if (ret != LDB_SUCCESS) {
409 return samldb_next_step(ac);
413 * msDS-IntId attributeSchema attribute handling
414 * during LDB_ADD request processing
416 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
421 int32_t system_flags;
422 struct ldb_context *ldb;
423 struct ldb_result *ldb_res;
424 struct ldb_dn *schema_dn;
426 ldb = ldb_module_get_ctx(ac->module);
427 schema_dn = ldb_get_schema_basedn(ldb);
429 /* replicated update should always go through */
430 if (ldb_request_get_control(ac->req,
431 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
435 /* msDS-IntId is handled by system and should never be
436 * passed by clients */
437 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
438 return LDB_ERR_UNWILLING_TO_PERFORM;
441 /* do not generate msDS-IntId if Relax control is passed */
442 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
446 /* check Functional Level */
447 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
451 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
452 system_flags = ldb_msg_find_attr_as_int(ac->msg, "systemFlags", 0);
453 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
457 /* Generate new value for msDs-IntId
458 * Value should be in 0x80000000..0xBFFFFFFF range */
459 msds_intid = generate_random() % 0X3FFFFFFF;
460 msds_intid += 0x80000000;
462 /* probe id values until unique one is found */
465 if (msds_intid > 0xBFFFFFFF) {
466 msds_intid = 0x80000001;
469 ret = dsdb_module_search(ac->module, ac,
471 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
472 DSDB_FLAG_NEXT_MODULE,
474 "(msDS-IntId=%d)", msds_intid);
475 if (ret != LDB_SUCCESS) {
476 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
477 __location__": Searching for msDS-IntId=%d failed - %s\n",
480 return ldb_operr(ldb);
482 id_exists = (ldb_res->count > 0);
484 talloc_free(ldb_res);
487 return samdb_msg_add_int(ldb, ac->msg, ac->msg, "msDS-IntId",
493 * samldb_add_entry (async)
496 static int samldb_add_entry_callback(struct ldb_request *req,
497 struct ldb_reply *ares)
499 struct ldb_context *ldb;
500 struct samldb_ctx *ac;
503 ac = talloc_get_type(req->context, struct samldb_ctx);
504 ldb = ldb_module_get_ctx(ac->module);
507 return ldb_module_done(ac->req, NULL, NULL,
508 LDB_ERR_OPERATIONS_ERROR);
511 if (ares->type == LDB_REPLY_REFERRAL) {
512 return ldb_module_send_referral(ac->req, ares->referral);
515 if (ares->error != LDB_SUCCESS) {
516 return ldb_module_done(ac->req, ares->controls,
517 ares->response, ares->error);
519 if (ares->type != LDB_REPLY_DONE) {
520 ldb_set_errstring(ldb,
521 "Invalid reply type!\n");
522 return ldb_module_done(ac->req, NULL, NULL,
523 LDB_ERR_OPERATIONS_ERROR);
526 /* The caller may wish to get controls back from the add */
527 ac->ares = talloc_steal(ac, ares);
529 ret = samldb_next_step(ac);
530 if (ret != LDB_SUCCESS) {
531 return ldb_module_done(ac->req, NULL, NULL, ret);
536 static int samldb_add_entry(struct samldb_ctx *ac)
538 struct ldb_context *ldb;
539 struct ldb_request *req;
542 ldb = ldb_module_get_ctx(ac->module);
544 ret = ldb_build_add_req(&req, ldb, ac,
547 ac, samldb_add_entry_callback,
549 LDB_REQ_SET_LOCATION(req);
550 if (ret != LDB_SUCCESS) {
554 return ldb_next_request(ac->module, req);
558 * return true if msg carries an attributeSchema that is intended to be RODC
559 * filtered but is also a system-critical attribute.
561 static bool check_rodc_critical_attribute(struct ldb_message *msg)
563 uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
565 schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
566 searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
567 rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE
568 | SEARCH_FLAG_CONFIDENTIAL);
570 if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
571 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
579 static int samldb_fill_object(struct samldb_ctx *ac)
581 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
584 /* Add information for the different account types */
585 if (strcmp(ac->type, "user") == 0) {
586 struct ldb_control *rodc_control = ldb_request_get_control(ac->req,
587 LDB_CONTROL_RODC_DCPROMO_OID);
588 if (rodc_control != NULL) {
589 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
590 rodc_control->critical = false;
591 ret = samldb_add_step(ac, samldb_rodc_add);
592 if (ret != LDB_SUCCESS) return ret;
595 /* check if we have a valid sAMAccountName */
596 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
597 if (ret != LDB_SUCCESS) return ret;
599 ret = samldb_add_step(ac, samldb_add_entry);
600 if (ret != LDB_SUCCESS) return ret;
602 } else if (strcmp(ac->type, "group") == 0) {
603 /* check if we have a valid sAMAccountName */
604 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
605 if (ret != LDB_SUCCESS) return ret;
607 ret = samldb_add_step(ac, samldb_add_entry);
608 if (ret != LDB_SUCCESS) return ret;
610 } else if (strcmp(ac->type, "classSchema") == 0) {
611 const struct ldb_val *rdn_value, *def_obj_cat_val;
613 ret = samdb_find_or_add_attribute(ldb, ac->msg,
615 if (ret != LDB_SUCCESS) return ret;
617 /* do not allow to mark an attributeSchema as RODC filtered if it
618 * is system-critical */
619 if (check_rodc_critical_attribute(ac->msg)) {
620 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
621 ldb_dn_get_linearized(ac->msg->dn));
622 return LDB_ERR_UNWILLING_TO_PERFORM;
625 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
626 if (rdn_value == NULL) {
627 return ldb_operr(ldb);
629 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
630 /* the RDN has prefix "CN" */
631 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
632 samdb_cn_to_lDAPDisplayName(ac->msg,
633 (const char *) rdn_value->data));
634 if (ret != LDB_SUCCESS) {
640 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
643 guid = GUID_random();
644 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
645 if (ret != LDB_SUCCESS) {
651 def_obj_cat_val = ldb_msg_find_ldb_val(ac->msg,
652 "defaultObjectCategory");
653 if (def_obj_cat_val != NULL) {
654 /* "defaultObjectCategory" has been set by the caller.
655 * Do some checks for consistency.
656 * NOTE: The real constraint check (that
657 * 'defaultObjectCategory' is the DN of the new
658 * objectclass or any parent of it) is still incomplete.
659 * For now we say that 'defaultObjectCategory' is valid
660 * if it exists and it is of objectclass "classSchema".
662 ac->dn = ldb_dn_from_ldb_val(ac, ldb, def_obj_cat_val);
663 if (ac->dn == NULL) {
664 ldb_set_errstring(ldb,
665 "Invalid DN for 'defaultObjectCategory'!");
666 return LDB_ERR_CONSTRAINT_VIOLATION;
669 /* "defaultObjectCategory" has not been set by the
670 * caller. Use the entry DN for it. */
671 ac->dn = ac->msg->dn;
673 ret = ldb_msg_add_string(ac->msg, "defaultObjectCategory",
674 ldb_dn_alloc_linearized(ac->msg, ac->dn));
675 if (ret != LDB_SUCCESS) {
681 ret = samldb_add_step(ac, samldb_add_entry);
682 if (ret != LDB_SUCCESS) return ret;
684 /* Now perform the checks for the 'defaultObjectCategory'. The
685 * lookup DN was already saved in "ac->dn" */
686 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
687 if (ret != LDB_SUCCESS) return ret;
689 } else if (strcmp(ac->type, "attributeSchema") == 0) {
690 const struct ldb_val *rdn_value;
691 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
692 if (rdn_value == NULL) {
693 return ldb_operr(ldb);
695 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
696 /* the RDN has prefix "CN" */
697 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
698 samdb_cn_to_lDAPDisplayName(ac->msg,
699 (const char *) rdn_value->data));
700 if (ret != LDB_SUCCESS) {
706 /* do not allow to mark an attributeSchema as RODC filtered if it
707 * is system-critical */
708 if (check_rodc_critical_attribute(ac->msg)) {
709 ldb_asprintf_errstring(ldb,
710 "samldb: refusing schema add of %s - cannot combine critical attribute with RODC filtering",
711 ldb_dn_get_linearized(ac->msg->dn));
712 return LDB_ERR_UNWILLING_TO_PERFORM;
715 ret = samdb_find_or_add_attribute(ldb, ac->msg,
716 "isSingleValued", "FALSE");
717 if (ret != LDB_SUCCESS) return ret;
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 /* handle msDS-IntID attribute */
731 ret = samldb_add_handle_msDS_IntId(ac);
732 if (ret != LDB_SUCCESS) return ret;
734 ret = samldb_add_step(ac, samldb_add_entry);
735 if (ret != LDB_SUCCESS) return ret;
738 ldb_asprintf_errstring(ldb,
739 "Invalid entry type!");
740 return LDB_ERR_OPERATIONS_ERROR;
743 return samldb_first_step(ac);
746 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
748 struct ldb_context *ldb;
749 const struct ldb_val *rdn_value;
753 ldb = ldb_module_get_ctx(ac->module);
755 sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
757 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
758 if (rdn_value == NULL) {
759 return ldb_operr(ldb);
761 sid = dom_sid_parse_talloc(ac->msg,
762 (const char *)rdn_value->data);
764 ldb_set_errstring(ldb,
765 "samldb: No valid SID found in ForeignSecurityPrincipal CN!");
766 return LDB_ERR_CONSTRAINT_VIOLATION;
768 if (! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
769 return ldb_operr(ldb);
773 /* finally proceed with adding the entry */
774 ret = samldb_add_step(ac, samldb_add_entry);
775 if (ret != LDB_SUCCESS) return ret;
777 return samldb_first_step(ac);
780 static int samldb_schema_info_update(struct samldb_ctx *ac)
783 struct ldb_context *ldb;
784 struct dsdb_schema *schema;
786 /* replicated update should always go through */
787 if (ldb_request_get_control(ac->req,
788 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
792 /* do not update schemaInfo during provisioning */
793 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
797 ldb = ldb_module_get_ctx(ac->module);
798 schema = dsdb_get_schema(ldb, NULL);
800 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
801 "samldb_schema_info_update: no dsdb_schema loaded");
802 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
803 return ldb_operr(ldb);
806 ret = dsdb_module_schema_info_update(ac->module, schema,
807 DSDB_FLAG_NEXT_MODULE, ac->req);
808 if (ret != LDB_SUCCESS) {
809 ldb_asprintf_errstring(ldb,
810 "samldb_schema_info_update: dsdb_module_schema_info_update failed with %s",
818 static int samldb_prim_group_tester(struct samldb_ctx *ac, uint32_t rid);
821 * "Objectclass" trigger (MS-SAMR 3.1.1.8.1)
823 * Has to be invoked on "add" and "modify" operations on "user", "computer" and
825 * ac->msg contains the "add"/"modify" message
826 * ac->type contains the object type (main objectclass)
828 static int samldb_objectclass_trigger(struct samldb_ctx *ac)
830 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
831 void *skip_allocate_sids = ldb_get_opaque(ldb,
832 "skip_allocate_sids");
833 struct ldb_message_element *el, *el2;
837 /* make sure that "sAMAccountType" is not specified */
838 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
840 ldb_set_errstring(ldb,
841 "samldb: sAMAccountType must not be specified!");
842 return LDB_ERR_UNWILLING_TO_PERFORM;
845 /* Step 1: objectSid assignment */
847 /* Don't allow the objectSid to be changed. But beside the RELAX
848 * control we have also to guarantee that it can always be set with
849 * SYSTEM permissions. This is needed for the "samba3sam" backend. */
850 sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
851 if ((sid != NULL) && (!dsdb_module_am_system(ac->module)) &&
852 (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
853 ldb_set_errstring(ldb,
854 "samldb: objectSid must not be specified!");
855 return LDB_ERR_UNWILLING_TO_PERFORM;
858 /* but generate a new SID when we do have an add operations */
859 if ((sid == NULL) && (ac->req->operation == LDB_ADD) && !skip_allocate_sids) {
860 ret = samldb_add_step(ac, samldb_allocate_sid);
861 if (ret != LDB_SUCCESS) return ret;
864 if (strcmp(ac->type, "user") == 0) {
865 bool uac_generated = false;
867 /* Step 1.2: Default values */
868 ret = samdb_find_or_add_attribute(ldb, ac->msg,
869 "accountExpires", "9223372036854775807");
870 if (ret != LDB_SUCCESS) return ret;
871 ret = samdb_find_or_add_attribute(ldb, ac->msg,
872 "badPasswordTime", "0");
873 if (ret != LDB_SUCCESS) return ret;
874 ret = samdb_find_or_add_attribute(ldb, ac->msg,
876 if (ret != LDB_SUCCESS) return ret;
877 ret = samdb_find_or_add_attribute(ldb, ac->msg,
879 if (ret != LDB_SUCCESS) return ret;
880 ret = samdb_find_or_add_attribute(ldb, ac->msg,
882 if (ret != LDB_SUCCESS) return ret;
883 ret = samdb_find_or_add_attribute(ldb, ac->msg,
885 if (ret != LDB_SUCCESS) return ret;
886 ret = samdb_find_or_add_attribute(ldb, ac->msg,
888 if (ret != LDB_SUCCESS) return ret;
889 ret = samdb_find_or_add_attribute(ldb, ac->msg,
891 if (ret != LDB_SUCCESS) return ret;
892 ret = samdb_find_or_add_attribute(ldb, ac->msg,
894 if (ret != LDB_SUCCESS) return ret;
896 /* On add operations we might need to generate a
897 * "userAccountControl" (if it isn't specified). */
898 el = ldb_msg_find_element(ac->msg, "userAccountControl");
899 if ((el == NULL) && (ac->req->operation == LDB_ADD)) {
900 ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
901 "userAccountControl",
903 if (ret != LDB_SUCCESS) {
906 uac_generated = true;
909 el = ldb_msg_find_element(ac->msg, "userAccountControl");
911 uint32_t user_account_control, account_type;
913 /* Step 1.3: "userAccountControl" -> "sAMAccountType" mapping */
914 user_account_control = ldb_msg_find_attr_as_uint(ac->msg,
915 "userAccountControl",
918 /* Temporary duplicate accounts aren't allowed */
919 if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
920 return LDB_ERR_OTHER;
923 /* Workstation and (read-only) DC objects do need objectclass "computer" */
924 if ((samdb_find_attribute(ldb, ac->msg,
925 "objectclass", "computer") == NULL) &&
926 (user_account_control &
927 (UF_SERVER_TRUST_ACCOUNT | UF_WORKSTATION_TRUST_ACCOUNT))) {
928 ldb_set_errstring(ldb,
929 "samldb: Requested account type does need objectclass 'computer'!");
930 return LDB_ERR_OBJECT_CLASS_VIOLATION;
933 account_type = ds_uf2atype(user_account_control);
934 if (account_type == 0) {
935 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
936 return LDB_ERR_UNWILLING_TO_PERFORM;
938 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
941 if (ret != LDB_SUCCESS) {
944 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
945 el2->flags = LDB_FLAG_MOD_REPLACE;
947 /* "isCriticalSystemObject" might be set */
948 if (user_account_control &
949 (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
950 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
952 if (ret != LDB_SUCCESS) {
955 el2 = ldb_msg_find_element(ac->msg,
956 "isCriticalSystemObject");
957 el2->flags = LDB_FLAG_MOD_REPLACE;
958 } else if (user_account_control & UF_WORKSTATION_TRUST_ACCOUNT) {
959 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
961 if (ret != LDB_SUCCESS) {
964 el2 = ldb_msg_find_element(ac->msg,
965 "isCriticalSystemObject");
966 el2->flags = LDB_FLAG_MOD_REPLACE;
969 /* Step 1.4: "userAccountControl" -> "primaryGroupID" mapping */
970 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
971 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
974 * Older AD deployments don't know about the
977 if (rid == DOMAIN_RID_READONLY_DCS) {
978 ret = samldb_prim_group_tester(ac, rid);
979 if (ret != LDB_SUCCESS) {
984 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
985 "primaryGroupID", rid);
986 if (ret != LDB_SUCCESS) {
989 el2 = ldb_msg_find_element(ac->msg,
991 el2->flags = LDB_FLAG_MOD_REPLACE;
994 /* Step 1.5: Add additional flags when needed */
995 /* Obviously this is done when the "userAccountControl"
996 * has been generated here (tested against Windows
999 user_account_control |= UF_ACCOUNTDISABLE;
1000 user_account_control |= UF_PASSWD_NOTREQD;
1002 ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
1003 "userAccountControl",
1004 user_account_control);
1005 if (ret != LDB_SUCCESS) {
1011 } else if (strcmp(ac->type, "group") == 0) {
1012 const char *tempstr;
1014 /* Step 2.2: Default values */
1015 tempstr = talloc_asprintf(ac->msg, "%d",
1016 GTYPE_SECURITY_GLOBAL_GROUP);
1017 if (tempstr == NULL) return ldb_operr(ldb);
1018 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1019 "groupType", tempstr);
1020 if (ret != LDB_SUCCESS) return ret;
1022 /* Step 2.3: "groupType" -> "sAMAccountType" */
1023 el = ldb_msg_find_element(ac->msg, "groupType");
1025 uint32_t group_type, account_type;
1027 group_type = ldb_msg_find_attr_as_uint(ac->msg,
1030 /* The creation of builtin groups requires the
1032 if (group_type == GTYPE_SECURITY_BUILTIN_LOCAL_GROUP) {
1033 if (ldb_request_get_control(ac->req,
1034 LDB_CONTROL_RELAX_OID) == NULL) {
1035 return LDB_ERR_UNWILLING_TO_PERFORM;
1039 account_type = ds_gtype2atype(group_type);
1040 if (account_type == 0) {
1041 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1042 return LDB_ERR_UNWILLING_TO_PERFORM;
1044 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1047 if (ret != LDB_SUCCESS) {
1050 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
1051 el2->flags = LDB_FLAG_MOD_REPLACE;
1059 * "Primary group ID" trigger (MS-SAMR 3.1.1.8.2)
1061 * Has to be invoked on "add" and "modify" operations on "user" and "computer"
1063 * ac->msg contains the "add"/"modify" message
1066 static int samldb_prim_group_tester(struct samldb_ctx *ac, uint32_t rid)
1068 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1069 struct dom_sid *sid;
1070 struct ldb_result *res;
1072 const char *noattrs[] = { NULL };
1074 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1076 return ldb_operr(ldb);
1079 ret = dsdb_module_search(ac->module, ac, &res,
1080 ldb_get_default_basedn(ldb),
1082 noattrs, DSDB_FLAG_NEXT_MODULE,
1085 ldap_encode_ndr_dom_sid(ac, sid));
1086 if (ret != LDB_SUCCESS) {
1089 if (res->count != 1) {
1091 ldb_asprintf_errstring(ldb,
1092 "Failed to find primary group with RID %u!",
1094 return LDB_ERR_UNWILLING_TO_PERFORM;
1101 static int samldb_prim_group_set(struct samldb_ctx *ac)
1103 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1106 rid = ldb_msg_find_attr_as_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
1107 if (rid == (uint32_t) -1) {
1108 /* we aren't affected of any primary group set */
1111 } else if (!ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
1112 ldb_set_errstring(ldb,
1113 "The primary group isn't settable on add operations!");
1114 return LDB_ERR_UNWILLING_TO_PERFORM;
1117 return samldb_prim_group_tester(ac, rid);
1120 static int samldb_prim_group_change(struct samldb_ctx *ac)
1122 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1123 const char * attrs[] = { "primaryGroupID", "memberOf", NULL };
1124 struct ldb_result *res, *group_res;
1125 struct ldb_message_element *el;
1126 struct ldb_message *msg;
1127 uint32_t prev_rid, new_rid;
1128 struct dom_sid *prev_sid, *new_sid;
1129 struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
1131 const char *noattrs[] = { NULL };
1133 el = dsdb_get_single_valued_attr(ac->msg, "primaryGroupID",
1134 ac->req->operation);
1136 /* we are not affected */
1140 /* Fetch information from the existing object */
1142 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1143 DSDB_FLAG_NEXT_MODULE, ac->req);
1144 if (ret != LDB_SUCCESS) {
1148 /* Finds out the DN of the old primary group */
1150 prev_rid = ldb_msg_find_attr_as_uint(res->msgs[0], "primaryGroupID",
1152 if (prev_rid == (uint32_t) -1) {
1153 /* User objects do always have a mandatory "primaryGroupID"
1154 * attribute. If this doesn't exist then the object is of the
1155 * wrong type. This is the exact Windows error code */
1156 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1159 prev_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), prev_rid);
1160 if (prev_sid == NULL) {
1161 return ldb_operr(ldb);
1164 /* Finds out the DN of the new primary group
1165 * Notice: in order to parse the primary group ID correctly we create
1166 * a temporary message here. */
1168 msg = ldb_msg_new(ac->msg);
1170 return ldb_module_oom(ac->module);
1172 ret = ldb_msg_add(msg, el, 0);
1173 if (ret != LDB_SUCCESS) {
1176 new_rid = ldb_msg_find_attr_as_uint(msg, "primaryGroupID", (uint32_t) -1);
1178 if (new_rid == (uint32_t) -1) {
1179 /* we aren't affected of any primary group change */
1183 if (prev_rid == new_rid) {
1187 ret = dsdb_module_search(ac->module, ac, &group_res,
1188 ldb_get_default_basedn(ldb),
1190 noattrs, DSDB_FLAG_NEXT_MODULE,
1193 ldap_encode_ndr_dom_sid(ac, prev_sid));
1194 if (ret != LDB_SUCCESS) {
1197 if (group_res->count != 1) {
1198 return ldb_operr(ldb);
1200 prev_prim_group_dn = group_res->msgs[0]->dn;
1202 new_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), new_rid);
1203 if (new_sid == NULL) {
1204 return ldb_operr(ldb);
1207 ret = dsdb_module_search(ac->module, ac, &group_res,
1208 ldb_get_default_basedn(ldb),
1210 noattrs, DSDB_FLAG_NEXT_MODULE,
1213 ldap_encode_ndr_dom_sid(ac, new_sid));
1214 if (ret != LDB_SUCCESS) {
1217 if (group_res->count != 1) {
1218 /* Here we know if the specified new primary group candidate is
1220 return LDB_ERR_UNWILLING_TO_PERFORM;
1222 new_prim_group_dn = group_res->msgs[0]->dn;
1224 /* We need to be already a normal member of the new primary
1225 * group in order to be successful. */
1226 el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
1227 ldb_dn_get_linearized(new_prim_group_dn));
1229 return LDB_ERR_UNWILLING_TO_PERFORM;
1232 /* Remove the "member" attribute on the new primary group */
1233 msg = ldb_msg_new(ac->msg);
1235 return ldb_module_oom(ac->module);
1237 msg->dn = new_prim_group_dn;
1239 ret = samdb_msg_add_delval(ldb, msg, msg, "member",
1240 ldb_dn_get_linearized(ac->msg->dn));
1241 if (ret != LDB_SUCCESS) {
1245 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE, ac->req);
1246 if (ret != LDB_SUCCESS) {
1251 /* Add a "member" attribute for the previous primary group */
1252 msg = ldb_msg_new(ac->msg);
1254 return ldb_module_oom(ac->module);
1256 msg->dn = prev_prim_group_dn;
1258 ret = samdb_msg_add_addval(ldb, msg, msg, "member",
1259 ldb_dn_get_linearized(ac->msg->dn));
1260 if (ret != LDB_SUCCESS) {
1264 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE, ac->req);
1265 if (ret != LDB_SUCCESS) {
1273 static int samldb_prim_group_trigger(struct samldb_ctx *ac)
1277 if (ac->req->operation == LDB_ADD) {
1278 ret = samldb_prim_group_set(ac);
1280 ret = samldb_prim_group_change(ac);
1288 * This function is called on LDB modify operations. It performs some additions/
1289 * replaces on the current LDB message when "userAccountControl" changes.
1291 static int samldb_user_account_control_change(struct samldb_ctx *ac)
1293 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1294 uint32_t user_account_control, old_user_account_control, account_type;
1295 struct ldb_message_element *el;
1296 struct ldb_message *tmp_msg;
1298 struct ldb_result *res;
1299 const char *attrs[] = { "userAccountControl", "objectClass", NULL };
1301 bool is_computer = false;
1303 el = dsdb_get_single_valued_attr(ac->msg, "userAccountControl",
1304 ac->req->operation);
1306 /* we are not affected */
1310 /* Create a temporary message for fetching the "userAccountControl" */
1311 tmp_msg = ldb_msg_new(ac->msg);
1312 if (tmp_msg == NULL) {
1313 return ldb_module_oom(ac->module);
1315 ret = ldb_msg_add(tmp_msg, el, 0);
1316 if (ret != LDB_SUCCESS) {
1319 user_account_control = ldb_msg_find_attr_as_uint(tmp_msg,
1320 "userAccountControl",
1322 talloc_free(tmp_msg);
1324 /* Temporary duplicate accounts aren't allowed */
1325 if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
1326 return LDB_ERR_OTHER;
1329 /* Fetch the old "userAccountControl" and "objectClass" */
1330 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1331 DSDB_FLAG_NEXT_MODULE, ac->req);
1332 if (ret != LDB_SUCCESS) {
1335 old_user_account_control = ldb_msg_find_attr_as_uint(res->msgs[0], "userAccountControl", 0);
1336 if (old_user_account_control == 0) {
1337 return ldb_operr(ldb);
1339 el = ldb_msg_find_element(res->msgs[0], "objectClass");
1341 return ldb_operr(ldb);
1344 /* When we do not have objectclass "computer" we cannot switch to a (read-only) DC */
1345 for (i = 0; i < el->num_values; i++) {
1346 if (ldb_attr_cmp((char *)el->values[i].data, "computer") == 0) {
1352 (user_account_control & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT))) {
1353 ldb_set_errstring(ldb,
1354 "samldb: Requested account type does need objectclass 'computer'!");
1355 return LDB_ERR_UNWILLING_TO_PERFORM;
1359 * The functions "ds_uf2atype" and "ds_uf2prim_group_rid" are used as
1360 * detectors for account type changes.
1361 * So if the account type does change then we need to adjust the
1362 * "sAMAccountType", the "isCriticalSystemObject" and the
1363 * "primaryGroupID" attribute.
1365 if ((ds_uf2atype(user_account_control)
1366 == ds_uf2atype(old_user_account_control)) &&
1367 (ds_uf2prim_group_rid(user_account_control)
1368 == ds_uf2prim_group_rid(old_user_account_control))) {
1372 account_type = ds_uf2atype(user_account_control);
1373 if (account_type == 0) {
1374 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1375 return LDB_ERR_UNWILLING_TO_PERFORM;
1377 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
1379 if (ret != LDB_SUCCESS) {
1382 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
1383 el->flags = LDB_FLAG_MOD_REPLACE;
1385 /* "isCriticalSystemObject" might be set/changed */
1386 if (user_account_control
1387 & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
1388 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1390 if (ret != LDB_SUCCESS) {
1393 el = ldb_msg_find_element(ac->msg,
1394 "isCriticalSystemObject");
1395 el->flags = LDB_FLAG_MOD_REPLACE;
1396 } else if (user_account_control & UF_WORKSTATION_TRUST_ACCOUNT) {
1397 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1399 if (ret != LDB_SUCCESS) {
1402 el = ldb_msg_find_element(ac->msg,
1403 "isCriticalSystemObject");
1404 el->flags = LDB_FLAG_MOD_REPLACE;
1407 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
1408 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
1410 /* Older AD deployments don't know about the RODC group */
1411 if (rid == DOMAIN_RID_READONLY_DCS) {
1412 ret = samldb_prim_group_tester(ac, rid);
1413 if (ret != LDB_SUCCESS) {
1418 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1419 "primaryGroupID", rid);
1420 if (ret != LDB_SUCCESS) {
1423 el = ldb_msg_find_element(ac->msg,
1425 el->flags = LDB_FLAG_MOD_REPLACE;
1431 static int samldb_group_type_change(struct samldb_ctx *ac)
1433 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1434 uint32_t group_type, old_group_type, account_type;
1435 struct ldb_message_element *el;
1436 struct ldb_message *tmp_msg;
1438 struct ldb_result *res;
1439 const char *attrs[] = { "groupType", NULL };
1441 el = dsdb_get_single_valued_attr(ac->msg, "groupType",
1442 ac->req->operation);
1444 /* we are not affected */
1448 /* Create a temporary message for fetching the "groupType" */
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 group_type = ldb_msg_find_attr_as_uint(tmp_msg, "groupType", 0);
1458 talloc_free(tmp_msg);
1460 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1461 DSDB_FLAG_NEXT_MODULE |
1462 DSDB_SEARCH_SHOW_DELETED, ac->req);
1463 if (ret != LDB_SUCCESS) {
1466 old_group_type = ldb_msg_find_attr_as_uint(res->msgs[0], "groupType", 0);
1467 if (old_group_type == 0) {
1468 return ldb_operr(ldb);
1471 /* Group type switching isn't so easy as it seems: We can only
1472 * change in this directions: global <-> universal <-> local
1473 * On each step also the group type itself
1474 * (security/distribution) is variable. */
1476 if (ldb_request_get_control(ac->req, LDB_CONTROL_PROVISION_OID) == NULL) {
1477 switch (group_type) {
1478 case GTYPE_SECURITY_GLOBAL_GROUP:
1479 case GTYPE_DISTRIBUTION_GLOBAL_GROUP:
1480 /* change to "universal" allowed */
1481 if ((old_group_type == GTYPE_SECURITY_DOMAIN_LOCAL_GROUP) ||
1482 (old_group_type == GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP)) {
1483 ldb_set_errstring(ldb,
1484 "samldb: Change from security/distribution local group forbidden!");
1485 return LDB_ERR_UNWILLING_TO_PERFORM;
1489 case GTYPE_SECURITY_UNIVERSAL_GROUP:
1490 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP:
1491 /* each change allowed */
1493 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP:
1494 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP:
1495 /* change to "universal" allowed */
1496 if ((old_group_type == GTYPE_SECURITY_GLOBAL_GROUP) ||
1497 (old_group_type == GTYPE_DISTRIBUTION_GLOBAL_GROUP)) {
1498 ldb_set_errstring(ldb,
1499 "samldb: Change from security/distribution global group forbidden!");
1500 return LDB_ERR_UNWILLING_TO_PERFORM;
1504 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP:
1506 /* we don't allow this "groupType" values */
1507 return LDB_ERR_UNWILLING_TO_PERFORM;
1512 account_type = ds_gtype2atype(group_type);
1513 if (account_type == 0) {
1514 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1515 return LDB_ERR_UNWILLING_TO_PERFORM;
1517 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
1519 if (ret != LDB_SUCCESS) {
1522 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
1523 el->flags = LDB_FLAG_MOD_REPLACE;
1528 static int samldb_sam_accountname_check(struct samldb_ctx *ac)
1530 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1531 const char *no_attrs[] = { NULL };
1532 struct ldb_result *res;
1533 const char *sam_accountname, *enc_str;
1534 struct ldb_message_element *el;
1535 struct ldb_message *tmp_msg;
1538 el = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName",
1539 ac->req->operation);
1541 /* we are not affected */
1545 /* Create a temporary message for fetching the "sAMAccountName" */
1546 tmp_msg = ldb_msg_new(ac->msg);
1547 if (tmp_msg == NULL) {
1548 return ldb_module_oom(ac->module);
1550 ret = ldb_msg_add(tmp_msg, el, 0);
1551 if (ret != LDB_SUCCESS) {
1554 sam_accountname = talloc_steal(ac,
1555 ldb_msg_find_attr_as_string(tmp_msg, "sAMAccountName", NULL));
1556 talloc_free(tmp_msg);
1558 if (sam_accountname == NULL) {
1559 /* The "sAMAccountName" cannot be nothing */
1560 ldb_set_errstring(ldb,
1561 "samldb: Empty account names aren't allowed!");
1562 return LDB_ERR_UNWILLING_TO_PERFORM;
1565 enc_str = ldb_binary_encode_string(ac, sam_accountname);
1566 if (enc_str == NULL) {
1567 return ldb_module_oom(ac->module);
1570 /* Make sure that a "sAMAccountName" is only used once */
1572 ret = dsdb_module_search(ac->module, ac, &res,
1573 ldb_get_default_basedn(ldb),
1574 LDB_SCOPE_SUBTREE, no_attrs,
1575 DSDB_FLAG_NEXT_MODULE, ac->req,
1576 "(sAMAccountName=%s)", enc_str);
1577 if (ret != LDB_SUCCESS) {
1580 if (res->count > 1) {
1581 return ldb_operr(ldb);
1582 } else if (res->count == 1) {
1583 if (ldb_dn_compare(res->msgs[0]->dn, ac->msg->dn) != 0) {
1584 ldb_asprintf_errstring(ldb,
1585 "samldb: Account name (sAMAccountName) '%s' already in use!",
1587 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1595 static int samldb_member_check(struct samldb_ctx *ac)
1597 static const char * const attrs[] = { "objectSid", "member", NULL };
1598 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1599 struct ldb_message_element *el;
1600 struct ldb_dn *member_dn;
1601 struct dom_sid *sid;
1602 struct ldb_result *res;
1603 struct dom_sid *group_sid;
1607 /* Fetch information from the existing object */
1609 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1610 DSDB_FLAG_NEXT_MODULE, ac->req, NULL);
1611 if (ret != LDB_SUCCESS) {
1614 if (res->count != 1) {
1615 return ldb_operr(ldb);
1618 group_sid = samdb_result_dom_sid(res, res->msgs[0], "objectSid");
1619 if (group_sid == NULL) {
1620 return ldb_operr(ldb);
1623 /* We've to walk over all modification entries and consider the "member"
1625 for (i = 0; i < ac->msg->num_elements; i++) {
1626 if (ldb_attr_cmp(ac->msg->elements[i].name, "member") != 0) {
1630 el = &ac->msg->elements[i];
1631 for (j = 0; j < el->num_values; j++) {
1632 struct ldb_result *group_res;
1633 const char *group_attrs[] = { "primaryGroupID" , NULL };
1634 uint32_t prim_group_rid;
1636 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
1637 /* Deletes will be handled in
1638 * repl_meta_data, and deletes not
1639 * matching a member will return
1640 * LDB_ERR_UNWILLING_TO_PERFORM
1645 member_dn = ldb_dn_from_ldb_val(ac, ldb,
1647 if (!ldb_dn_validate(member_dn)) {
1648 return ldb_operr(ldb);
1651 /* Denies to add "member"s to groups which are primary
1652 * ones for them - in this case return
1653 * ERR_ENTRY_ALREADY_EXISTS. */
1655 ret = dsdb_module_search_dn(ac->module, ac, &group_res,
1656 member_dn, group_attrs,
1657 DSDB_FLAG_NEXT_MODULE, ac->req);
1658 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1659 /* member DN doesn't exist yet */
1662 if (ret != LDB_SUCCESS) {
1665 prim_group_rid = ldb_msg_find_attr_as_uint(group_res->msgs[0], "primaryGroupID", (uint32_t)-1);
1666 if (prim_group_rid == (uint32_t) -1) {
1667 /* the member hasn't to be a user account ->
1668 * therefore no check needed in this case. */
1672 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1675 return ldb_operr(ldb);
1678 if (dom_sid_equal(group_sid, sid)) {
1679 ldb_asprintf_errstring(ldb,
1680 "samldb: member %s already set via primaryGroupID %u",
1681 ldb_dn_get_linearized(member_dn), prim_group_rid);
1682 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1692 /* SAM objects have special rules regarding the "description" attribute on
1693 * modify operations. */
1694 static int samldb_description_check(struct samldb_ctx *ac, bool *modified)
1696 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1697 const char * const attrs[] = { "objectClass", "description", NULL };
1698 struct ldb_result *res;
1702 /* Fetch information from the existing object */
1703 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1704 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED, ac->req,
1705 "(|(objectclass=user)(objectclass=group)(objectclass=samDomain)(objectclass=samServer))");
1706 if (ret != LDB_SUCCESS) {
1707 /* don't treat it specially ... let normal error codes
1708 happen from other places */
1709 ldb_reset_err_string(ldb);
1712 if (res->count == 0) {
1713 /* we didn't match the filter */
1718 /* We've to walk over all modification entries and consider the
1719 * "description" ones. */
1720 for (i = 0; i < ac->msg->num_elements; i++) {
1721 if (ldb_attr_cmp(ac->msg->elements[i].name, "description") == 0) {
1722 ac->msg->elements[i].flags |= LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK;
1732 /* This trigger adapts the "servicePrincipalName" attributes if the
1733 * "dNSHostName" and/or "sAMAccountName" attribute change(s) */
1734 static int samldb_service_principal_names_change(struct samldb_ctx *ac)
1736 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1737 struct ldb_message_element *el = NULL, *el2 = NULL;
1738 struct ldb_message *msg;
1739 const char *attrs[] = { "servicePrincipalName", NULL };
1740 struct ldb_result *res;
1741 const char *dns_hostname = NULL, *old_dns_hostname = NULL,
1742 *sam_accountname = NULL, *old_sam_accountname = NULL;
1746 el = dsdb_get_single_valued_attr(ac->msg, "dNSHostName",
1747 ac->req->operation);
1748 el2 = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName",
1749 ac->req->operation);
1750 if ((el == NULL) && (el2 == NULL)) {
1751 /* we are not affected */
1755 /* Create a temporary message for fetching the "dNSHostName" */
1757 const char *dns_attrs[] = { "dNSHostName", NULL };
1758 msg = ldb_msg_new(ac->msg);
1760 return ldb_module_oom(ac->module);
1762 ret = ldb_msg_add(msg, el, 0);
1763 if (ret != LDB_SUCCESS) {
1766 dns_hostname = talloc_steal(ac,
1767 ldb_msg_find_attr_as_string(msg, "dNSHostName", NULL));
1770 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn,
1771 dns_attrs, DSDB_FLAG_NEXT_MODULE, ac->req);
1772 if (ret == LDB_SUCCESS) {
1773 old_dns_hostname = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
1777 /* Create a temporary message for fetching the "sAMAccountName" */
1779 char *tempstr, *tempstr2;
1780 const char *acct_attrs[] = { "sAMAccountName", NULL };
1782 msg = ldb_msg_new(ac->msg);
1784 return ldb_module_oom(ac->module);
1786 ret = ldb_msg_add(msg, el2, 0);
1787 if (ret != LDB_SUCCESS) {
1790 tempstr = talloc_strdup(ac,
1791 ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL));
1794 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, acct_attrs,
1795 DSDB_FLAG_NEXT_MODULE, ac->req);
1796 if (ret == LDB_SUCCESS) {
1797 tempstr2 = talloc_strdup(ac,
1798 ldb_msg_find_attr_as_string(res->msgs[0],
1799 "sAMAccountName", NULL));
1803 /* The "sAMAccountName" needs some additional trimming: we need
1804 * to remove the trailing "$"s if they exist. */
1805 if ((tempstr != NULL) && (tempstr[0] != '\0') &&
1806 (tempstr[strlen(tempstr) - 1] == '$')) {
1807 tempstr[strlen(tempstr) - 1] = '\0';
1809 if ((tempstr2 != NULL) && (tempstr2[0] != '\0') &&
1810 (tempstr2[strlen(tempstr2) - 1] == '$')) {
1811 tempstr2[strlen(tempstr2) - 1] = '\0';
1813 sam_accountname = tempstr;
1814 old_sam_accountname = tempstr2;
1817 if (old_dns_hostname == NULL) {
1818 /* we cannot change when the old name is unknown */
1819 dns_hostname = NULL;
1821 if ((old_dns_hostname != NULL) && (dns_hostname != NULL) &&
1822 (strcasecmp(old_dns_hostname, dns_hostname) == 0)) {
1823 /* The "dNSHostName" didn't change */
1824 dns_hostname = NULL;
1827 if (old_sam_accountname == NULL) {
1828 /* we cannot change when the old name is unknown */
1829 sam_accountname = NULL;
1831 if ((old_sam_accountname != NULL) && (sam_accountname != NULL) &&
1832 (strcasecmp(old_sam_accountname, sam_accountname) == 0)) {
1833 /* The "sAMAccountName" didn't change */
1834 sam_accountname = NULL;
1837 if ((dns_hostname == NULL) && (sam_accountname == NULL)) {
1838 /* Well, there are information missing (old name(s)) or the
1839 * names didn't change. We've nothing to do and can exit here */
1843 /* Potential "servicePrincipalName" changes in the same request have to
1844 * be handled before the update (Windows behaviour). */
1845 el = ldb_msg_find_element(ac->msg, "servicePrincipalName");
1847 msg = ldb_msg_new(ac->msg);
1849 return ldb_module_oom(ac->module);
1851 msg->dn = ac->msg->dn;
1854 ret = ldb_msg_add(msg, el, el->flags);
1855 if (ret != LDB_SUCCESS) {
1859 ldb_msg_remove_element(ac->msg, el);
1861 el = ldb_msg_find_element(ac->msg,
1862 "servicePrincipalName");
1863 } while (el != NULL);
1865 ret = dsdb_module_modify(ac->module, msg,
1866 DSDB_FLAG_NEXT_MODULE, ac->req);
1867 if (ret != LDB_SUCCESS) {
1873 /* Fetch the "servicePrincipalName"s if any */
1874 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1875 DSDB_FLAG_NEXT_MODULE, ac->req, NULL);
1876 if (ret != LDB_SUCCESS) {
1879 if ((res->count != 1) || (res->msgs[0]->num_elements > 1)) {
1880 return ldb_operr(ldb);
1883 if (res->msgs[0]->num_elements == 1) {
1884 /* Yes, we do have "servicePrincipalName"s. First we update them
1885 * locally, that means we do always substitute the current
1886 * "dNSHostName" with the new one and/or "sAMAccountName"
1887 * without "$" with the new one and then we append this to the
1888 * modification request (Windows behaviour). */
1890 for (i = 0; i < res->msgs[0]->elements[0].num_values; i++) {
1891 char *old_str, *new_str, *pos;
1895 res->msgs[0]->elements[0].values[i].data;
1897 new_str = talloc_strdup(ac->msg,
1898 strtok_r(old_str, "/", &pos));
1899 if (new_str == NULL) {
1900 return ldb_module_oom(ac->module);
1903 while ((tok = strtok_r(NULL, "/", &pos)) != NULL) {
1904 if ((dns_hostname != NULL) &&
1905 (strcasecmp(tok, old_dns_hostname) == 0)) {
1908 if ((sam_accountname != NULL) &&
1909 (strcasecmp(tok, old_sam_accountname) == 0)) {
1910 tok = sam_accountname;
1913 new_str = talloc_asprintf(ac->msg, "%s/%s",
1915 if (new_str == NULL) {
1916 return ldb_module_oom(ac->module);
1920 ret = ldb_msg_add_string(ac->msg,
1921 "servicePrincipalName",
1923 if (ret != LDB_SUCCESS) {
1928 el = ldb_msg_find_element(ac->msg, "servicePrincipalName");
1929 el->flags = LDB_FLAG_MOD_REPLACE;
1939 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
1941 struct ldb_context *ldb;
1942 struct samldb_ctx *ac;
1945 ldb = ldb_module_get_ctx(module);
1946 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
1948 /* do not manipulate our control entries */
1949 if (ldb_dn_is_special(req->op.add.message->dn)) {
1950 return ldb_next_request(module, req);
1953 ac = samldb_ctx_init(module, req);
1955 return ldb_operr(ldb);
1958 /* build the new msg */
1959 ac->msg = ldb_msg_copy_shallow(ac, req->op.add.message);
1960 if (ac->msg == NULL) {
1962 ldb_debug(ldb, LDB_DEBUG_FATAL,
1963 "samldb_add: ldb_msg_copy_shallow failed!\n");
1964 return ldb_operr(ldb);
1967 if (samdb_find_attribute(ldb, ac->msg,
1968 "objectclass", "user") != NULL) {
1971 ret = samldb_prim_group_trigger(ac);
1972 if (ret != LDB_SUCCESS) {
1976 ret = samldb_objectclass_trigger(ac);
1977 if (ret != LDB_SUCCESS) {
1981 return samldb_fill_object(ac);
1984 if (samdb_find_attribute(ldb, ac->msg,
1985 "objectclass", "group") != NULL) {
1988 ret = samldb_objectclass_trigger(ac);
1989 if (ret != LDB_SUCCESS) {
1993 return samldb_fill_object(ac);
1996 /* perhaps a foreignSecurityPrincipal? */
1997 if (samdb_find_attribute(ldb, ac->msg,
1999 "foreignSecurityPrincipal") != NULL) {
2000 return samldb_fill_foreignSecurityPrincipal_object(ac);
2003 if (samdb_find_attribute(ldb, ac->msg,
2004 "objectclass", "classSchema") != NULL) {
2005 ret = samldb_schema_info_update(ac);
2006 if (ret != LDB_SUCCESS) {
2011 ac->type = "classSchema";
2012 return samldb_fill_object(ac);
2015 if (samdb_find_attribute(ldb, ac->msg,
2016 "objectclass", "attributeSchema") != NULL) {
2017 ret = samldb_schema_info_update(ac);
2018 if (ret != LDB_SUCCESS) {
2023 ac->type = "attributeSchema";
2024 return samldb_fill_object(ac);
2029 /* nothing matched, go on */
2030 return ldb_next_request(module, req);
2034 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
2036 struct ldb_context *ldb;
2037 struct samldb_ctx *ac;
2038 struct ldb_message_element *el, *el2;
2039 bool modified = false;
2042 if (ldb_dn_is_special(req->op.mod.message->dn)) {
2043 /* do not manipulate our control entries */
2044 return ldb_next_request(module, req);
2047 ldb = ldb_module_get_ctx(module);
2049 /* make sure that "objectSid" is not specified */
2050 el = ldb_msg_find_element(req->op.mod.message, "objectSid");
2052 if (ldb_request_get_control(req, LDB_CONTROL_PROVISION_OID) == NULL) {
2053 ldb_set_errstring(ldb,
2054 "samldb: objectSid must not be specified!");
2055 return LDB_ERR_UNWILLING_TO_PERFORM;
2058 /* make sure that "sAMAccountType" is not specified */
2059 el = ldb_msg_find_element(req->op.mod.message, "sAMAccountType");
2061 ldb_set_errstring(ldb,
2062 "samldb: sAMAccountType must not be specified!");
2063 return LDB_ERR_UNWILLING_TO_PERFORM;
2065 /* make sure that "isCriticalSystemObject" is not specified */
2066 el = ldb_msg_find_element(req->op.mod.message, "isCriticalSystemObject");
2068 if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID) == NULL) {
2069 ldb_set_errstring(ldb,
2070 "samldb: isCriticalSystemObject must not be specified!");
2071 return LDB_ERR_UNWILLING_TO_PERFORM;
2075 /* msDS-IntId is not allowed to be modified
2076 * except when modification comes from replication */
2077 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
2078 if (!ldb_request_get_control(req,
2079 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
2080 return LDB_ERR_CONSTRAINT_VIOLATION;
2084 ac = samldb_ctx_init(module, req);
2086 return ldb_operr(ldb);
2089 /* build the new msg */
2090 ac->msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
2091 if (ac->msg == NULL) {
2093 ldb_debug(ldb, LDB_DEBUG_FATAL,
2094 "samldb_modify: ldb_msg_copy_shallow failed!\n");
2095 return ldb_operr(ldb);
2098 el = ldb_msg_find_element(ac->msg, "primaryGroupID");
2100 ret = samldb_prim_group_trigger(ac);
2101 if (ret != LDB_SUCCESS) {
2106 el = ldb_msg_find_element(ac->msg, "userAccountControl");
2109 ret = samldb_user_account_control_change(ac);
2110 if (ret != LDB_SUCCESS) {
2115 el = ldb_msg_find_element(ac->msg, "groupType");
2118 ret = samldb_group_type_change(ac);
2119 if (ret != LDB_SUCCESS) {
2124 el = ldb_msg_find_element(ac->msg, "sAMAccountName");
2126 ret = samldb_sam_accountname_check(ac);
2127 if (ret != LDB_SUCCESS) {
2132 el = ldb_msg_find_element(ac->msg, "member");
2134 ret = samldb_member_check(ac);
2135 if (ret != LDB_SUCCESS) {
2140 el = ldb_msg_find_element(ac->msg, "description");
2142 ret = samldb_description_check(ac, &modified);
2143 if (ret != LDB_SUCCESS) {
2148 el = ldb_msg_find_element(ac->msg, "dNSHostName");
2149 el2 = ldb_msg_find_element(ac->msg, "sAMAccountName");
2150 if ((el != NULL) || (el2 != NULL)) {
2152 ret = samldb_service_principal_names_change(ac);
2153 if (ret != LDB_SUCCESS) {
2159 struct ldb_request *child_req;
2161 /* Now perform the real modifications as a child request */
2162 ret = ldb_build_mod_req(&child_req, ldb, ac,
2165 req, dsdb_next_callback,
2167 LDB_REQ_SET_LOCATION(child_req);
2168 if (ret != LDB_SUCCESS) {
2172 return ldb_next_request(module, child_req);
2177 /* no change which interests us, go on */
2178 return ldb_next_request(module, req);
2183 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
2185 struct ldb_context *ldb;
2186 struct dom_sid *sid;
2190 struct ldb_result *res;
2191 const char *attrs[] = { "objectSid", "isDeleted", NULL };
2192 const char *noattrs[] = { NULL };
2194 ldb = ldb_module_get_ctx(ac->module);
2196 /* Finds out the SID/RID of the SAM object */
2197 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->req->op.del.dn,
2199 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED,
2201 if (ret != LDB_SUCCESS) {
2205 if (ldb_msg_check_string_attribute(res->msgs[0], "isDeleted", "TRUE")) {
2209 sid = samdb_result_dom_sid(ac, res->msgs[0], "objectSid");
2211 /* No SID - it might not be a SAM object - therefore ok */
2214 status = dom_sid_split_rid(ac, sid, NULL, &rid);
2215 if (!NT_STATUS_IS_OK(status)) {
2216 return ldb_operr(ldb);
2219 /* Special object (security principal?) */
2223 /* Deny delete requests from groups which are primary ones */
2224 ret = dsdb_module_search(ac->module, ac, &res,
2225 ldb_get_default_basedn(ldb),
2226 LDB_SCOPE_SUBTREE, noattrs,
2227 DSDB_FLAG_NEXT_MODULE,
2229 "(&(primaryGroupID=%u)(objectClass=user))", rid);
2230 if (ret != LDB_SUCCESS) {
2233 if (res->count > 0) {
2234 return LDB_ERR_ENTRY_ALREADY_EXISTS;
2240 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
2242 struct samldb_ctx *ac;
2245 if (ldb_dn_is_special(req->op.del.dn)) {
2246 /* do not manipulate our control entries */
2247 return ldb_next_request(module, req);
2250 ac = samldb_ctx_init(module, req);
2252 return ldb_operr(ldb_module_get_ctx(module));
2255 ret = samldb_prim_group_users_check(ac);
2256 if (ret != LDB_SUCCESS) {
2262 return ldb_next_request(module, req);
2267 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
2269 struct ldb_context *ldb = ldb_module_get_ctx(module);
2270 struct dsdb_fsmo_extended_op *exop;
2273 exop = talloc_get_type(req->op.extended.data,
2274 struct dsdb_fsmo_extended_op);
2276 ldb_set_errstring(ldb,
2277 "samldb_extended_allocate_rid_pool: invalid extended data");
2278 return LDB_ERR_PROTOCOL_ERROR;
2281 ret = ridalloc_allocate_rid_pool_fsmo(module, exop, req);
2282 if (ret != LDB_SUCCESS) {
2286 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
2289 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
2291 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
2292 return samldb_extended_allocate_rid_pool(module, req);
2295 return ldb_next_request(module, req);
2299 static const struct ldb_module_ops ldb_samldb_module_ops = {
2302 .modify = samldb_modify,
2303 .del = samldb_delete,
2304 .extended = samldb_extended
2308 int ldb_samldb_module_init(const char *version)
2310 LDB_MODULE_CHECK_VERSION(version);
2311 return ldb_register_module(&ldb_samldb_module_ops);