4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
5 Copyright (C) Simo Sorce 2004-2008
6 Copyright (C) Matthias Dieter Wallnöfer 2009-2010
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"
45 typedef int (*samldb_step_fn_t)(struct samldb_ctx *);
48 struct samldb_step *next;
53 struct ldb_module *module;
54 struct ldb_request *req;
56 /* used for add operations */
59 /* the resulting message */
60 struct ldb_message *msg;
62 /* used in "samldb_find_for_defaultObjectCategory" */
63 struct ldb_dn *dn, *res_dn;
65 /* all the async steps necessary to complete the operation */
66 struct samldb_step *steps;
67 struct samldb_step *curstep;
69 /* If someone set an ares to forward controls and response back to the caller */
70 struct ldb_reply *ares;
73 static struct samldb_ctx *samldb_ctx_init(struct ldb_module *module,
74 struct ldb_request *req)
76 struct ldb_context *ldb;
77 struct samldb_ctx *ac;
79 ldb = ldb_module_get_ctx(module);
81 ac = talloc_zero(req, struct samldb_ctx);
93 static int samldb_add_step(struct samldb_ctx *ac, samldb_step_fn_t fn)
95 struct samldb_step *step, *stepper;
97 step = talloc_zero(ac, struct samldb_step);
99 return ldb_oom(ldb_module_get_ctx(ac->module));
104 if (ac->steps == NULL) {
108 if (ac->curstep == NULL)
109 return ldb_operr(ldb_module_get_ctx(ac->module));
110 for (stepper = ac->curstep; stepper->next != NULL;
111 stepper = stepper->next);
112 stepper->next = step;
118 static int samldb_first_step(struct samldb_ctx *ac)
120 if (ac->steps == NULL) {
121 return ldb_operr(ldb_module_get_ctx(ac->module));
124 ac->curstep = ac->steps;
125 return ac->curstep->fn(ac);
128 static int samldb_next_step(struct samldb_ctx *ac)
130 if (ac->curstep->next) {
131 ac->curstep = ac->curstep->next;
132 return ac->curstep->fn(ac);
135 /* We exit the samldb module here. If someone set an "ares" to forward
136 * controls and response back to the caller, use them. */
138 return ldb_module_done(ac->req, ac->ares->controls,
139 ac->ares->response, LDB_SUCCESS);
141 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
146 /* sAMAccountName handling */
148 static int samldb_generate_sAMAccountName(struct ldb_context *ldb,
149 struct ldb_message *msg)
153 /* Format: $000000-000000000000 */
155 name = talloc_asprintf(msg, "$%.6X-%.6X%.6X",
156 (unsigned int)generate_random(),
157 (unsigned int)generate_random(),
158 (unsigned int)generate_random());
162 return ldb_msg_add_steal_string(msg, "sAMAccountName", name);
165 static int samldb_check_sAMAccountName(struct samldb_ctx *ac)
167 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
171 if (ldb_msg_find_element(ac->msg, "sAMAccountName") == NULL) {
172 ret = samldb_generate_sAMAccountName(ldb, ac->msg);
173 if (ret != LDB_SUCCESS) {
178 name = ldb_msg_find_attr_as_string(ac->msg, "sAMAccountName", NULL);
180 return ldb_operr(ldb);
183 ret = samdb_search_count(ldb, ac, NULL, "(sAMAccountName=%s)",
184 ldb_binary_encode_string(ac, name));
185 if ((ret < 0) || (ret > 1)) {
186 return ldb_operr(ldb);
189 ldb_asprintf_errstring(ldb,
190 "samldb: Account name (sAMAccountName) '%s' already in use!",
192 return LDB_ERR_ENTRY_ALREADY_EXISTS;
195 return samldb_next_step(ac);
199 static bool samldb_msg_add_sid(struct ldb_message *msg,
201 const struct dom_sid *sid)
204 enum ndr_err_code ndr_err;
206 ndr_err = ndr_push_struct_blob(&v, msg, sid,
207 (ndr_push_flags_fn_t)ndr_push_dom_sid);
208 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
211 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
215 /* allocate a SID using our RID Set */
216 static int samldb_allocate_sid(struct samldb_ctx *ac)
220 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
223 ret = ridalloc_allocate_rid(ac->module, &rid);
224 if (ret != LDB_SUCCESS) {
228 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
230 return ldb_module_oom(ac->module);
233 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
234 return ldb_operr(ldb);
237 return samldb_next_step(ac);
241 see if a krbtgt_number is available
243 static bool samldb_krbtgtnumber_available(struct samldb_ctx *ac,
244 uint32_t krbtgt_number)
246 TALLOC_CTX *tmp_ctx = talloc_new(ac);
247 struct ldb_result *res;
248 const char *no_attrs[] = { NULL };
251 ret = dsdb_module_search(ac->module, tmp_ctx, &res, NULL,
252 LDB_SCOPE_SUBTREE, no_attrs,
253 DSDB_FLAG_NEXT_MODULE,
254 "(msDC-SecondaryKrbTgtNumber=%u)",
256 if (ret == LDB_SUCCESS && res->count == 0) {
257 talloc_free(tmp_ctx);
260 talloc_free(tmp_ctx);
264 /* special handling for add in RODC join */
265 static int samldb_rodc_add(struct samldb_ctx *ac)
267 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
268 uint32_t krbtgt_number, i_start, i;
272 /* find a unused msDC-SecondaryKrbTgtNumber */
273 i_start = generate_random() & 0xFFFF;
278 for (i=i_start; i<=0xFFFF; i++) {
279 if (samldb_krbtgtnumber_available(ac, i)) {
284 for (i=1; i<i_start; i++) {
285 if (samldb_krbtgtnumber_available(ac, i)) {
291 ldb_asprintf_errstring(ldb,
292 "%08X: Unable to find available msDS-SecondaryKrbTgtNumber",
293 W_ERROR_V(WERR_NO_SYSTEM_RESOURCES));
294 return LDB_ERR_OTHER;
297 ret = ldb_msg_add_empty(ac->msg, "msDS-SecondaryKrbTgtNumber",
298 LDB_FLAG_INTERNAL_DISABLE_VALIDATION, NULL);
299 if (ret != LDB_SUCCESS) {
300 return ldb_operr(ldb);
303 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
304 "msDS-SecondaryKrbTgtNumber", krbtgt_number);
305 if (ret != LDB_SUCCESS) {
306 return ldb_operr(ldb);
309 ret = ldb_msg_add_fmt(ac->msg, "sAMAccountName", "krbtgt_%u",
311 if (ret != LDB_SUCCESS) {
312 return ldb_operr(ldb);
315 newpass = generate_random_password(ac->msg, 128, 255);
316 if (newpass == NULL) {
317 return ldb_operr(ldb);
320 ret = ldb_msg_add_steal_string(ac->msg, "clearTextPassword", newpass);
321 if (ret != LDB_SUCCESS) {
322 return ldb_operr(ldb);
325 return samldb_next_step(ac);
328 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
330 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
331 struct ldb_result *res;
332 const char *no_attrs[] = { NULL };
337 ret = dsdb_module_search(ac->module, ac, &res,
338 ac->dn, LDB_SCOPE_BASE, no_attrs,
339 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
340 | DSDB_FLAG_NEXT_MODULE,
341 "(objectClass=classSchema)");
342 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
343 /* Don't be pricky when the DN doesn't exist if we have the */
344 /* RELAX control specified */
345 if (ldb_request_get_control(ac->req,
346 LDB_CONTROL_RELAX_OID) == NULL) {
347 ldb_set_errstring(ldb,
348 "samldb_find_defaultObjectCategory: "
349 "Invalid DN for 'defaultObjectCategory'!");
350 return LDB_ERR_CONSTRAINT_VIOLATION;
353 if ((ret != LDB_ERR_NO_SUCH_OBJECT) && (ret != LDB_SUCCESS)) {
359 return samldb_next_step(ac);
363 * msDS-IntId attributeSchema attribute handling
364 * during LDB_ADD request processing
366 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
371 int32_t system_flags;
372 struct ldb_context *ldb;
373 struct ldb_result *ldb_res;
374 struct ldb_dn *schema_dn;
376 ldb = ldb_module_get_ctx(ac->module);
377 schema_dn = ldb_get_schema_basedn(ldb);
379 /* replicated update should always go through */
380 if (ldb_request_get_control(ac->req,
381 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
385 /* msDS-IntId is handled by system and should never be
386 * passed by clients */
387 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
388 return LDB_ERR_UNWILLING_TO_PERFORM;
391 /* do not generate msDS-IntId if Relax control is passed */
392 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
396 /* check Functional Level */
397 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
401 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
402 system_flags = ldb_msg_find_attr_as_int(ac->msg, "systemFlags", 0);
403 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
407 /* Generate new value for msDs-IntId
408 * Value should be in 0x80000000..0xBFFFFFFF range */
409 msds_intid = generate_random() % 0X3FFFFFFF;
410 msds_intid += 0x80000000;
412 /* probe id values until unique one is found */
415 if (msds_intid > 0xBFFFFFFF) {
416 msds_intid = 0x80000001;
419 ret = dsdb_module_search(ac->module, ac,
421 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
422 DSDB_FLAG_NEXT_MODULE,
423 "(msDS-IntId=%d)", msds_intid);
424 if (ret != LDB_SUCCESS) {
425 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
426 __location__": Searching for msDS-IntId=%d failed - %s\n",
429 return ldb_operr(ldb);
431 id_exists = (ldb_res->count > 0);
433 talloc_free(ldb_res);
436 return samdb_msg_add_int(ldb, ac->msg, ac->msg, "msDS-IntId",
442 * samldb_add_entry (async)
445 static int samldb_add_entry_callback(struct ldb_request *req,
446 struct ldb_reply *ares)
448 struct ldb_context *ldb;
449 struct samldb_ctx *ac;
452 ac = talloc_get_type(req->context, struct samldb_ctx);
453 ldb = ldb_module_get_ctx(ac->module);
456 return ldb_module_done(ac->req, NULL, NULL,
457 LDB_ERR_OPERATIONS_ERROR);
460 if (ares->type == LDB_REPLY_REFERRAL) {
461 return ldb_module_send_referral(ac->req, ares->referral);
464 if (ares->error != LDB_SUCCESS) {
465 return ldb_module_done(ac->req, ares->controls,
466 ares->response, ares->error);
468 if (ares->type != LDB_REPLY_DONE) {
469 ldb_set_errstring(ldb,
470 "Invalid reply type!\n");
471 return ldb_module_done(ac->req, NULL, NULL,
472 LDB_ERR_OPERATIONS_ERROR);
475 /* The caller may wish to get controls back from the add */
476 ac->ares = talloc_steal(ac, ares);
478 ret = samldb_next_step(ac);
479 if (ret != LDB_SUCCESS) {
480 return ldb_module_done(ac->req, NULL, NULL, ret);
485 static int samldb_add_entry(struct samldb_ctx *ac)
487 struct ldb_context *ldb;
488 struct ldb_request *req;
491 ldb = ldb_module_get_ctx(ac->module);
493 ret = ldb_build_add_req(&req, ldb, ac,
496 ac, samldb_add_entry_callback,
498 LDB_REQ_SET_LOCATION(req);
499 if (ret != LDB_SUCCESS) {
503 return ldb_next_request(ac->module, req);
507 * return true if msg carries an attributeSchema that is intended to be RODC
508 * filtered but is also a system-critical attribute.
510 static bool check_rodc_critical_attribute(struct ldb_message *msg)
512 uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
514 schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
515 searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
516 rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE
517 | SEARCH_FLAG_CONFIDENTIAL);
519 if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
520 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
528 static int samldb_fill_object(struct samldb_ctx *ac)
530 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
533 /* Add informations for the different account types */
534 if (strcmp(ac->type, "user") == 0) {
535 struct ldb_control *rodc_control = ldb_request_get_control(ac->req,
536 LDB_CONTROL_RODC_DCPROMO_OID);
537 if (rodc_control != NULL) {
538 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
539 rodc_control->critical = false;
540 ret = samldb_add_step(ac, samldb_rodc_add);
541 if (ret != LDB_SUCCESS) return ret;
544 /* check if we have a valid sAMAccountName */
545 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
546 if (ret != LDB_SUCCESS) return ret;
548 ret = samldb_add_step(ac, samldb_add_entry);
549 if (ret != LDB_SUCCESS) return ret;
551 } else if (strcmp(ac->type, "group") == 0) {
552 /* check if we have a valid sAMAccountName */
553 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
554 if (ret != LDB_SUCCESS) return ret;
556 ret = samldb_add_step(ac, samldb_add_entry);
557 if (ret != LDB_SUCCESS) return ret;
559 } else if (strcmp(ac->type, "classSchema") == 0) {
560 const struct ldb_val *rdn_value, *def_obj_cat_val;
562 ret = samdb_find_or_add_attribute(ldb, ac->msg,
564 if (ret != LDB_SUCCESS) return ret;
566 /* do not allow to mark an attributeSchema as RODC filtered if it
567 * is system-critical */
568 if (check_rodc_critical_attribute(ac->msg)) {
569 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
570 ldb_dn_get_linearized(ac->msg->dn));
571 return LDB_ERR_UNWILLING_TO_PERFORM;
574 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
575 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
576 /* the RDN has prefix "CN" */
577 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
578 samdb_cn_to_lDAPDisplayName(ac->msg,
579 (const char *) rdn_value->data));
580 if (ret != LDB_SUCCESS) {
586 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
589 guid = GUID_random();
590 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
591 if (ret != LDB_SUCCESS) {
597 def_obj_cat_val = ldb_msg_find_ldb_val(ac->msg,
598 "defaultObjectCategory");
599 if (def_obj_cat_val != NULL) {
600 /* "defaultObjectCategory" has been set by the caller.
601 * Do some checks for consistency.
602 * NOTE: The real constraint check (that
603 * 'defaultObjectCategory' is the DN of the new
604 * objectclass or any parent of it) is still incomplete.
605 * For now we say that 'defaultObjectCategory' is valid
606 * if it exists and it is of objectclass "classSchema".
608 ac->dn = ldb_dn_from_ldb_val(ac, ldb, def_obj_cat_val);
609 if (ac->dn == NULL) {
610 ldb_set_errstring(ldb,
611 "Invalid DN for 'defaultObjectCategory'!");
612 return LDB_ERR_CONSTRAINT_VIOLATION;
615 /* "defaultObjectCategory" has not been set by the
616 * caller. Use the entry DN for it. */
617 ac->dn = ac->msg->dn;
619 ret = ldb_msg_add_string(ac->msg, "defaultObjectCategory",
620 ldb_dn_alloc_linearized(ac->msg, ac->dn));
621 if (ret != LDB_SUCCESS) {
627 ret = samldb_add_step(ac, samldb_add_entry);
628 if (ret != LDB_SUCCESS) return ret;
630 /* Now perform the checks for the 'defaultObjectCategory'. The
631 * lookup DN was already saved in "ac->dn" */
632 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
633 if (ret != LDB_SUCCESS) return ret;
635 } else if (strcmp(ac->type, "attributeSchema") == 0) {
636 const struct ldb_val *rdn_value;
637 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
638 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
639 /* the RDN has prefix "CN" */
640 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
641 samdb_cn_to_lDAPDisplayName(ac->msg,
642 (const char *) rdn_value->data));
643 if (ret != LDB_SUCCESS) {
649 /* do not allow to mark an attributeSchema as RODC filtered if it
650 * is system-critical */
651 if (check_rodc_critical_attribute(ac->msg)) {
652 ldb_asprintf_errstring(ldb,
653 "samldb: refusing schema add of %s - cannot combine critical attribute with RODC filtering",
654 ldb_dn_get_linearized(ac->msg->dn));
655 return LDB_ERR_UNWILLING_TO_PERFORM;
658 ret = samdb_find_or_add_attribute(ldb, ac->msg,
659 "isSingleValued", "FALSE");
660 if (ret != LDB_SUCCESS) return ret;
662 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
665 guid = GUID_random();
666 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
667 if (ret != LDB_SUCCESS) {
673 /* handle msDS-IntID attribute */
674 ret = samldb_add_handle_msDS_IntId(ac);
675 if (ret != LDB_SUCCESS) return ret;
677 ret = samldb_add_step(ac, samldb_add_entry);
678 if (ret != LDB_SUCCESS) return ret;
681 ldb_asprintf_errstring(ldb,
682 "Invalid entry type!");
683 return LDB_ERR_OPERATIONS_ERROR;
686 return samldb_first_step(ac);
689 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
691 struct ldb_context *ldb;
695 ldb = ldb_module_get_ctx(ac->module);
697 sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
699 sid = dom_sid_parse_talloc(ac->msg,
700 (const char *)ldb_dn_get_rdn_val(ac->msg->dn)->data);
702 ldb_set_errstring(ldb,
703 "samldb: No valid SID found in ForeignSecurityPrincipal CN!");
704 return LDB_ERR_CONSTRAINT_VIOLATION;
706 if (! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
707 return ldb_operr(ldb);
711 /* finally proceed with adding the entry */
712 ret = samldb_add_step(ac, samldb_add_entry);
713 if (ret != LDB_SUCCESS) return ret;
715 return samldb_first_step(ac);
718 static int samldb_schema_info_update(struct samldb_ctx *ac)
721 struct ldb_context *ldb;
722 struct dsdb_schema *schema;
724 /* replicated update should always go through */
725 if (ldb_request_get_control(ac->req,
726 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
730 /* do not update schemaInfo during provisioning */
731 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
735 ldb = ldb_module_get_ctx(ac->module);
736 schema = dsdb_get_schema(ldb, NULL);
738 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
739 "samldb_schema_info_update: no dsdb_schema loaded");
740 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
741 return ldb_operr(ldb);
744 ret = dsdb_module_schema_info_update(ac->module, schema,
745 DSDB_FLAG_NEXT_MODULE);
746 if (ret != LDB_SUCCESS) {
747 ldb_asprintf_errstring(ldb,
748 "samldb_schema_info_update: dsdb_module_schema_info_update failed with %s",
757 * Gets back a single-valued attribute by the rules of the SAM triggers when
758 * performing a modify operation
760 static int samldb_get_single_valued_attr(struct samldb_ctx *ac,
761 const char *attr_name,
762 struct ldb_message_element **attr)
764 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
765 struct ldb_message_element *el = NULL;
768 /* We've to walk over all modification entries and consider the
771 * 1.) Add operations aren't allowed and there is returned
772 * "ATTRIBUTE_OR_VALUE_EXISTS".
773 * 2.) Replace operations are allowed but the last one is taken
774 * 3.) Delete operations are also not allowed and there is returned
775 * "UNWILLING_TO_PERFORM".
777 * If "el" is afterwards NULL then that means we've nothing to do here.
779 for (i = 0; i < ac->msg->num_elements; i++) {
780 if (ldb_attr_cmp(ac->msg->elements[i].name, attr_name) != 0) {
784 el = &ac->msg->elements[i];
785 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
786 ldb_asprintf_errstring(ldb,
787 "samldb: attribute '%s' already exists!",
789 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
791 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
792 ldb_asprintf_errstring(ldb,
793 "samldb: attribute '%s' cannot be deleted!",
795 return LDB_ERR_UNWILLING_TO_PERFORM;
804 * "Objectclass" trigger (MS-SAMR 3.1.1.8.1)
806 * Has to be invoked on "add" and "modify" operations on "user", "computer" and
808 * ac->msg contains the "add"/"modify" message
809 * ac->type contains the object type (main objectclass)
811 static int samldb_objectclass_trigger(struct samldb_ctx *ac)
813 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
814 struct loadparm_context *lp_ctx = talloc_get_type(ldb_get_opaque(ldb,
815 "loadparm"), struct loadparm_context);
816 struct ldb_message_element *el, *el2;
817 enum sid_generator sid_generator;
822 /* make sure that "sAMAccountType" is not specified */
823 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
825 ldb_set_errstring(ldb,
826 "samldb: sAMAccountType must not be specified!");
827 return LDB_ERR_UNWILLING_TO_PERFORM;
830 /* Step 1: objectSid assignment */
832 /* Don't allow the objectSid to be changed. But beside the RELAX
833 * control we have also to guarantee that it can always be set with
834 * SYSTEM permissions. This is needed for the "samba3sam" backend. */
835 sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
836 if ((sid != NULL) && (!dsdb_module_am_system(ac->module)) &&
837 (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
838 ldb_asprintf_errstring(ldb,
839 "samldb: no SID may be specified in user/group modifications for %s",
840 ldb_dn_get_linearized(ac->msg->dn));
841 return LDB_ERR_UNWILLING_TO_PERFORM;
844 /* but generate a new SID when we do have an add operations */
845 if ((sid == NULL) && (ac->req->operation == LDB_ADD)) {
846 sid_generator = lpcfg_sid_generator(lp_ctx);
847 if (sid_generator == SID_GENERATOR_INTERNAL) {
848 ret = samldb_add_step(ac, samldb_allocate_sid);
849 if (ret != LDB_SUCCESS) return ret;
853 if (strcmp(ac->type, "user") == 0) {
854 /* Step 1.2: Default values */
855 tempstr = talloc_asprintf(ac->msg, "%d", UF_NORMAL_ACCOUNT);
856 if (tempstr == NULL) return ldb_operr(ldb);
857 ret = samdb_find_or_add_attribute(ldb, ac->msg,
858 "userAccountControl", tempstr);
859 if (ret != LDB_SUCCESS) return ret;
860 ret = samdb_find_or_add_attribute(ldb, ac->msg,
862 if (ret != LDB_SUCCESS) return ret;
863 ret = samdb_find_or_add_attribute(ldb, ac->msg,
865 if (ret != LDB_SUCCESS) return ret;
866 ret = samdb_find_or_add_attribute(ldb, ac->msg,
868 if (ret != LDB_SUCCESS) return ret;
869 ret = samdb_find_or_add_attribute(ldb, ac->msg,
870 "badPasswordTime", "0");
871 if (ret != LDB_SUCCESS) return ret;
872 ret = samdb_find_or_add_attribute(ldb, ac->msg,
874 if (ret != LDB_SUCCESS) return ret;
875 ret = samdb_find_or_add_attribute(ldb, ac->msg,
877 if (ret != LDB_SUCCESS) return ret;
878 ret = samdb_find_or_add_attribute(ldb, ac->msg,
880 if (ret != LDB_SUCCESS) return ret;
881 ret = samdb_find_or_add_attribute(ldb, ac->msg,
882 "accountExpires", "9223372036854775807");
883 if (ret != LDB_SUCCESS) return ret;
884 ret = samdb_find_or_add_attribute(ldb, ac->msg,
886 if (ret != LDB_SUCCESS) return ret;
888 el = ldb_msg_find_element(ac->msg, "userAccountControl");
890 uint32_t user_account_control, account_type;
892 /* Step 1.3: "userAccountControl" -> "sAMAccountType" mapping */
893 user_account_control = ldb_msg_find_attr_as_uint(ac->msg,
894 "userAccountControl",
897 /* Temporary duplicate accounts aren't allowed */
898 if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
899 return LDB_ERR_OTHER;
902 account_type = ds_uf2atype(user_account_control);
903 if (account_type == 0) {
904 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
905 return LDB_ERR_UNWILLING_TO_PERFORM;
907 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
910 if (ret != LDB_SUCCESS) {
913 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
914 el2->flags = LDB_FLAG_MOD_REPLACE;
916 if (user_account_control &
917 (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
918 ret = samdb_msg_set_string(ldb, ac->msg, ac->msg,
919 "isCriticalSystemObject",
921 if (ret != LDB_SUCCESS) {
924 el2 = ldb_msg_find_element(ac->msg,
925 "isCriticalSystemObject");
926 el2->flags = LDB_FLAG_MOD_REPLACE;
929 /* Step 1.4: "userAccountControl" -> "primaryGroupID" mapping */
930 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
931 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
932 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
933 "primaryGroupID", rid);
934 if (ret != LDB_SUCCESS) {
937 el2 = ldb_msg_find_element(ac->msg,
939 el2->flags = LDB_FLAG_MOD_REPLACE;
942 /* Step 1.5: Add additional flags when needed */
943 if ((user_account_control & UF_NORMAL_ACCOUNT) &&
944 (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
945 user_account_control |= UF_ACCOUNTDISABLE;
946 user_account_control |= UF_PASSWD_NOTREQD;
948 ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
949 "userAccountControl",
950 user_account_control);
951 if (ret != LDB_SUCCESS) {
957 } else if (strcmp(ac->type, "group") == 0) {
958 /* Step 2.2: Default values */
959 tempstr = talloc_asprintf(ac->msg, "%d",
960 GTYPE_SECURITY_GLOBAL_GROUP);
961 if (tempstr == NULL) return ldb_operr(ldb);
962 ret = samdb_find_or_add_attribute(ldb, ac->msg,
963 "groupType", tempstr);
964 if (ret != LDB_SUCCESS) return ret;
966 /* Step 2.3: "groupType" -> "sAMAccountType" */
967 el = ldb_msg_find_element(ac->msg, "groupType");
969 uint32_t group_type, account_type;
971 group_type = ldb_msg_find_attr_as_uint(ac->msg,
974 /* The creation of builtin groups requires the
976 if (group_type == GTYPE_SECURITY_BUILTIN_LOCAL_GROUP) {
977 if (ldb_request_get_control(ac->req,
978 LDB_CONTROL_RELAX_OID) == NULL) {
979 return LDB_ERR_UNWILLING_TO_PERFORM;
983 account_type = ds_gtype2atype(group_type);
984 if (account_type == 0) {
985 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
986 return LDB_ERR_UNWILLING_TO_PERFORM;
988 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
991 if (ret != LDB_SUCCESS) {
994 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
995 el2->flags = LDB_FLAG_MOD_REPLACE;
1003 * "Primary group ID" trigger (MS-SAMR 3.1.1.8.2)
1005 * Has to be invoked on "add" and "modify" operations on "user" and "computer"
1007 * ac->msg contains the "add"/"modify" message
1010 static int samldb_prim_group_set(struct samldb_ctx *ac)
1012 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1013 struct ldb_dn *prim_group_dn;
1015 struct dom_sid *sid;
1017 rid = ldb_msg_find_attr_as_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
1018 if (rid == (uint32_t) -1) {
1019 /* we aren't affected of any primary group set */
1022 } else if (!ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
1023 ldb_set_errstring(ldb,
1024 "The primary group isn't settable on add operations!");
1025 return LDB_ERR_UNWILLING_TO_PERFORM;
1028 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1030 return ldb_operr(ldb);
1033 prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
1034 ldap_encode_ndr_dom_sid(ac, sid));
1035 if (prim_group_dn == NULL) {
1036 ldb_asprintf_errstring(ldb,
1037 "Failed to find primary group with RID %u!",
1039 return LDB_ERR_UNWILLING_TO_PERFORM;
1045 static int samldb_prim_group_change(struct samldb_ctx *ac)
1047 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1048 const char * attrs[] = { "primaryGroupID", "memberOf", NULL };
1049 struct ldb_result *res;
1050 struct ldb_message_element *el;
1051 struct ldb_message *msg;
1053 struct dom_sid *sid;
1054 struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
1057 ret = samldb_get_single_valued_attr(ac, "primaryGroupID", &el);
1058 if (ret != LDB_SUCCESS) {
1062 /* we are not affected */
1066 /* Fetch informations from the existing object */
1068 ret = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1070 if (ret != LDB_SUCCESS) {
1073 if (res->count != 1) {
1074 return ldb_operr(ldb);
1077 /* Finds out the DN of the old primary group */
1079 rid = ldb_msg_find_attr_as_uint(res->msgs[0], "primaryGroupID", (uint32_t) -1);
1080 if (rid == (uint32_t) -1) {
1081 /* User objects do always have a mandatory "primaryGroupID"
1082 * attribute. If this doesn't exist then the object is of the
1083 * wrong type. This is the exact Windows error code */
1084 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1087 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1089 return ldb_operr(ldb);
1092 prev_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
1093 ldap_encode_ndr_dom_sid(ac, sid));
1094 if (prev_prim_group_dn == NULL) {
1095 return ldb_operr(ldb);
1098 /* Finds out the DN of the new primary group
1099 * Notice: in order to parse the primary group ID correctly we create
1100 * a temporary message here. */
1102 msg = ldb_msg_new(ac->msg);
1104 return ldb_module_oom(ac->module);
1106 ret = ldb_msg_add(msg, el, 0);
1107 if (ret != LDB_SUCCESS) {
1110 rid = ldb_msg_find_attr_as_uint(msg, "primaryGroupID", (uint32_t) -1);
1112 if (rid == (uint32_t) -1) {
1113 /* we aren't affected of any primary group change */
1117 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1119 return ldb_operr(ldb);
1122 new_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
1123 ldap_encode_ndr_dom_sid(ac, sid));
1124 if (new_prim_group_dn == NULL) {
1125 /* Here we know if the specified new primary group candidate is
1127 return LDB_ERR_UNWILLING_TO_PERFORM;
1130 /* Only update the "member" attributes when we really do have a change */
1131 if (ldb_dn_compare(new_prim_group_dn, prev_prim_group_dn) != 0) {
1132 /* We need to be already a normal member of the new primary
1133 * group in order to be successful. */
1134 el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
1135 ldb_dn_get_linearized(new_prim_group_dn));
1137 return LDB_ERR_UNWILLING_TO_PERFORM;
1140 /* Remove the "member" attribute on the new primary group */
1141 msg = ldb_msg_new(ac->msg);
1143 return ldb_module_oom(ac->module);
1145 msg->dn = new_prim_group_dn;
1147 ret = samdb_msg_add_delval(ldb, msg, msg, "member",
1148 ldb_dn_get_linearized(ac->msg->dn));
1149 if (ret != LDB_SUCCESS) {
1153 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1154 if (ret != LDB_SUCCESS) {
1159 /* Add a "member" attribute for the previous primary group */
1160 msg = ldb_msg_new(ac->msg);
1162 return ldb_module_oom(ac->module);
1164 msg->dn = prev_prim_group_dn;
1166 ret = samdb_msg_add_addval(ldb, msg, msg, "member",
1167 ldb_dn_get_linearized(ac->msg->dn));
1168 if (ret != LDB_SUCCESS) {
1172 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1173 if (ret != LDB_SUCCESS) {
1184 static int samldb_prim_group_trigger(struct samldb_ctx *ac)
1188 if (ac->req->operation == LDB_ADD) {
1189 ret = samldb_prim_group_set(ac);
1191 ret = samldb_prim_group_change(ac);
1197 static int samldb_user_account_control_change(struct samldb_ctx *ac)
1199 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1200 uint32_t user_account_control, account_type;
1201 struct ldb_message_element *el;
1202 struct ldb_message *tmp_msg;
1205 ret = samldb_get_single_valued_attr(ac, "userAccountControl", &el);
1206 if (ret != LDB_SUCCESS) {
1210 /* we are not affected */
1214 /* Create a temporary message for fetching the "userAccountControl" */
1215 tmp_msg = ldb_msg_new(ac->msg);
1216 if (tmp_msg == NULL) {
1217 return ldb_module_oom(ac->module);
1219 ret = ldb_msg_add(tmp_msg, el, 0);
1220 if (ret != LDB_SUCCESS) {
1223 user_account_control = ldb_msg_find_attr_as_uint(tmp_msg,
1224 "userAccountControl",
1226 talloc_free(tmp_msg);
1228 /* Temporary duplicate accounts aren't allowed */
1229 if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
1230 return LDB_ERR_OTHER;
1233 account_type = ds_uf2atype(user_account_control);
1234 if (account_type == 0) {
1235 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1236 return LDB_ERR_UNWILLING_TO_PERFORM;
1238 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
1240 if (ret != LDB_SUCCESS) {
1243 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
1244 el->flags = LDB_FLAG_MOD_REPLACE;
1246 if (user_account_control
1247 & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
1248 ret = samdb_msg_add_string(ldb, ac->msg, ac->msg,
1249 "isCriticalSystemObject", "TRUE");
1250 if (ret != LDB_SUCCESS) {
1253 el = ldb_msg_find_element(ac->msg,
1254 "isCriticalSystemObject");
1255 el->flags = LDB_FLAG_MOD_REPLACE;
1258 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
1259 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
1260 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1261 "primaryGroupID", rid);
1262 if (ret != LDB_SUCCESS) {
1265 el = ldb_msg_find_element(ac->msg,
1267 el->flags = LDB_FLAG_MOD_REPLACE;
1273 static int samldb_group_type_change(struct samldb_ctx *ac)
1275 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1276 uint32_t group_type, old_group_type, account_type;
1277 struct ldb_message_element *el;
1278 struct ldb_message *tmp_msg;
1281 ret = samldb_get_single_valued_attr(ac, "groupType", &el);
1282 if (ret != LDB_SUCCESS) {
1286 /* we are not affected */
1290 /* Create a temporary message for fetching the "groupType" */
1291 tmp_msg = ldb_msg_new(ac->msg);
1292 if (tmp_msg == NULL) {
1293 return ldb_module_oom(ac->module);
1295 ret = ldb_msg_add(tmp_msg, el, 0);
1296 if (ret != LDB_SUCCESS) {
1299 group_type = ldb_msg_find_attr_as_uint(tmp_msg, "groupType", 0);
1300 talloc_free(tmp_msg);
1302 old_group_type = samdb_search_uint(ldb, ac, 0, ac->msg->dn,
1304 if (old_group_type == 0) {
1305 return ldb_operr(ldb);
1308 /* Group type switching isn't so easy as it seems: We can only
1309 * change in this directions: global <-> universal <-> local
1310 * On each step also the group type itself
1311 * (security/distribution) is variable. */
1313 switch (group_type) {
1314 case GTYPE_SECURITY_GLOBAL_GROUP:
1315 case GTYPE_DISTRIBUTION_GLOBAL_GROUP:
1316 /* change to "universal" allowed */
1317 if ((old_group_type == GTYPE_SECURITY_DOMAIN_LOCAL_GROUP) ||
1318 (old_group_type == GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP)) {
1319 return LDB_ERR_UNWILLING_TO_PERFORM;
1323 case GTYPE_SECURITY_UNIVERSAL_GROUP:
1324 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP:
1325 /* each change allowed */
1328 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP:
1329 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP:
1330 /* change to "universal" allowed */
1331 if ((old_group_type == GTYPE_SECURITY_GLOBAL_GROUP) ||
1332 (old_group_type == GTYPE_DISTRIBUTION_GLOBAL_GROUP)) {
1333 return LDB_ERR_UNWILLING_TO_PERFORM;
1337 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP:
1339 /* we don't allow this "groupType" values */
1340 return LDB_ERR_UNWILLING_TO_PERFORM;
1344 account_type = ds_gtype2atype(group_type);
1345 if (account_type == 0) {
1346 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1347 return LDB_ERR_UNWILLING_TO_PERFORM;
1349 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
1351 if (ret != LDB_SUCCESS) {
1354 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
1355 el->flags = LDB_FLAG_MOD_REPLACE;
1360 static int samldb_sam_accountname_check(struct samldb_ctx *ac)
1362 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1363 const char *no_attrs[] = { NULL };
1364 struct ldb_result *res;
1365 const char *sam_accountname, *enc_str;
1366 struct ldb_message_element *el;
1367 struct ldb_message *tmp_msg;
1370 ret = samldb_get_single_valued_attr(ac, "sAMAccountName", &el);
1371 if (ret != LDB_SUCCESS) {
1375 /* we are not affected */
1379 /* Create a temporary message for fetching the "sAMAccountName" */
1380 tmp_msg = ldb_msg_new(ac->msg);
1381 if (tmp_msg == NULL) {
1382 return ldb_module_oom(ac->module);
1384 ret = ldb_msg_add(tmp_msg, el, 0);
1385 if (ret != LDB_SUCCESS) {
1388 sam_accountname = ldb_msg_find_attr_as_string(tmp_msg, "sAMAccountName",
1390 talloc_free(tmp_msg);
1392 if (sam_accountname == NULL) {
1393 return ldb_operr(ldb);
1396 enc_str = ldb_binary_encode_string(ac, sam_accountname);
1397 if (enc_str == NULL) {
1398 return ldb_module_oom(ac->module);
1401 /* Make sure that a "sAMAccountName" is only used once */
1403 ret = ldb_search(ldb, ac, &res, NULL, LDB_SCOPE_SUBTREE, no_attrs,
1404 "(sAMAccountName=%s)", enc_str);
1405 if (ret != LDB_SUCCESS) {
1408 if (res->count > 1) {
1409 return ldb_operr(ldb);
1410 } else if (res->count == 1) {
1411 if (ldb_dn_compare(res->msgs[0]->dn, ac->msg->dn) != 0) {
1412 ldb_asprintf_errstring(ldb,
1413 "samldb: Account name (sAMAccountName) '%s' already in use!",
1415 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1423 static int samldb_member_check(struct samldb_ctx *ac)
1425 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1426 struct ldb_message_element *el;
1427 struct ldb_dn *member_dn, *group_dn;
1428 uint32_t prim_group_rid;
1429 struct dom_sid *sid;
1433 /* We've to walk over all modification entries and consider the "member"
1435 for (i = 0; i < ac->msg->num_elements; i++) {
1436 if (ldb_attr_cmp(ac->msg->elements[i].name, "member") != 0) {
1440 el = &ac->msg->elements[i];
1441 for (j = 0; j < el->num_values; j++) {
1442 member_dn = ldb_dn_from_ldb_val(ac, ldb,
1444 if (!ldb_dn_validate(member_dn)) {
1445 return ldb_operr(ldb);
1448 /* The "member" attribute can be modified with the
1449 * following restrictions (beside a valid DN):
1451 * - "add" operations can only be performed when the
1452 * member still doesn't exist - if not then return
1453 * ERR_ENTRY_ALREADY_EXISTS (not
1454 * ERR_ATTRIBUTE_OR_VALUE_EXISTS!)
1455 * - "delete" operations can only be performed when the
1456 * member does exist - if not then return
1457 * ERR_UNWILLING_TO_PERFORM (not
1458 * ERR_NO_SUCH_ATTRIBUTE!)
1459 * - primary group check
1461 cnt = samdb_search_count(ldb, ac, ac->msg->dn,
1463 ldb_dn_get_linearized(member_dn));
1465 return ldb_operr(ldb);
1467 if ((cnt > 0) && (LDB_FLAG_MOD_TYPE(el->flags)
1468 == LDB_FLAG_MOD_ADD)) {
1469 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1471 if ((cnt == 0) && LDB_FLAG_MOD_TYPE(el->flags)
1472 == LDB_FLAG_MOD_DELETE) {
1473 return LDB_ERR_UNWILLING_TO_PERFORM;
1476 /* Denies to add "member"s to groups which are primary
1477 * ones for them - in this case return
1478 * ERR_ENTRY_ALREADY_EXISTS. */
1480 prim_group_rid = samdb_search_uint(ldb, ac,
1485 if (prim_group_rid == (uint32_t) -1) {
1486 /* the member hasn't to be a user account ->
1487 * therefore no check needed in this case. */
1491 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1494 return ldb_operr(ldb);
1497 group_dn = samdb_search_dn(ldb, ac, NULL,
1499 ldap_encode_ndr_dom_sid(ac, sid));
1500 if (group_dn == NULL) {
1501 return ldb_operr(ldb);
1504 if (ldb_dn_compare(group_dn, ac->msg->dn) == 0) {
1505 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1513 /* This trigger adapts the "servicePrincipalName" attributes if the
1514 * "dNSHostName" attribute changes */
1515 static int samldb_service_principal_names_change(struct samldb_ctx *ac)
1517 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1518 struct ldb_message_element *el = NULL;
1519 struct ldb_message *msg;
1520 const char *attrs[] = { "servicePrincipalName", NULL };
1521 struct ldb_result *res;
1522 const char *dns_hostname, *old_dns_hostname;
1526 /* Here it's not the same logic as with "samldb_get_single_valued_attr".
1529 * - consider "add" and "replace" operations - the last value we take
1530 * - ignore "delete" operations - obviously this attribute isn't
1533 for (i = 0; i < ac->msg->num_elements; i++) {
1534 if (ldb_attr_cmp(ac->msg->elements[i].name,
1535 "dNSHostName") != 0) {
1539 if (LDB_FLAG_MOD_TYPE(ac->msg->elements[i].flags)
1540 != LDB_FLAG_MOD_DELETE) {
1541 el = &ac->msg->elements[i];
1545 /* we are not affected */
1549 /* Create a temporary message for fetching the "dNSHostName" */
1550 msg = ldb_msg_new(ac->msg);
1552 return ldb_module_oom(ac->module);
1554 ret = ldb_msg_add(msg, el, 0);
1555 if (ret != LDB_SUCCESS) {
1558 dns_hostname = ldb_msg_find_attr_as_string(msg, "dNSHostName", "");
1561 old_dns_hostname = samdb_search_string(ldb, ac, ac->msg->dn,
1562 "dNSHostName", NULL);
1563 if ((old_dns_hostname == NULL) || (strcasecmp(old_dns_hostname,
1564 dns_hostname) == 0)) {
1565 /* Well, if there's no old DNS hostname then we cannot do this.
1566 * And if old and new DNS name do match we are also finished
1571 /* Potential "servicePrincipalName" changes in the same request have to
1572 * be handled before the update (Windows behaviour). */
1573 el = ldb_msg_find_element(ac->msg, "servicePrincipalName");
1575 msg = ldb_msg_new(ac->msg);
1577 return ldb_module_oom(ac->module);
1579 msg->dn = ac->msg->dn;
1582 ret = ldb_msg_add(msg, el, el->flags);
1583 if (ret != LDB_SUCCESS) {
1587 ldb_msg_remove_element(ac->msg, el);
1589 el = ldb_msg_find_element(ac->msg,
1590 "servicePrincipalName");
1591 } while (el != NULL);
1593 ret = dsdb_module_modify(ac->module, msg,
1594 DSDB_FLAG_NEXT_MODULE);
1595 if (ret != LDB_SUCCESS) {
1601 /* Fetch the "servicePrincipalName"s if any */
1602 ret = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1604 if (ret != LDB_SUCCESS) {
1607 if ((res->count != 1) || (res->msgs[0]->num_elements > 1)) {
1608 return ldb_operr(ldb);
1611 if (res->msgs[0]->num_elements == 1) {
1612 /* Yes, we do have "servicePrincipalName"s. First we update them
1613 * locally, that means we do always substitute the current
1614 * "dNSHostName" with the new one and then we append this to the
1615 * modification request (Windows behaviour). */
1617 for (i = 0; i < res->msgs[0]->elements[0].num_values; i++) {
1618 char *old_str, *new_str, *pos;
1622 res->msgs[0]->elements[0].values[i].data;
1624 new_str = talloc_strdup(ac->msg,
1625 strtok_r(old_str, "/", &pos));
1626 if (new_str == NULL) {
1627 return ldb_module_oom(ac->module);
1630 while ((tok = strtok_r(NULL, "/", &pos)) != NULL) {
1631 if (strcasecmp(tok, old_dns_hostname) == 0) {
1635 new_str = talloc_asprintf(ac->msg, "%s/%s",
1637 if (new_str == NULL) {
1638 return ldb_module_oom(ac->module);
1642 ret = ldb_msg_add_string(ac->msg,
1643 "servicePrincipalName",
1645 if (ret != LDB_SUCCESS) {
1650 el = ldb_msg_find_element(ac->msg, "servicePrincipalName");
1651 el->flags = LDB_FLAG_MOD_REPLACE;
1661 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
1663 struct ldb_context *ldb;
1664 struct samldb_ctx *ac;
1667 ldb = ldb_module_get_ctx(module);
1668 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
1670 /* do not manipulate our control entries */
1671 if (ldb_dn_is_special(req->op.add.message->dn)) {
1672 return ldb_next_request(module, req);
1675 ac = samldb_ctx_init(module, req);
1677 return ldb_operr(ldb);
1680 /* build the new msg */
1681 ac->msg = ldb_msg_copy_shallow(ac, req->op.add.message);
1682 if (ac->msg == NULL) {
1684 ldb_debug(ldb, LDB_DEBUG_FATAL,
1685 "samldb_add: ldb_msg_copy_shallow failed!\n");
1686 return ldb_operr(ldb);
1689 if (samdb_find_attribute(ldb, ac->msg,
1690 "objectclass", "user") != NULL) {
1693 ret = samldb_prim_group_trigger(ac);
1694 if (ret != LDB_SUCCESS) {
1698 ret = samldb_objectclass_trigger(ac);
1699 if (ret != LDB_SUCCESS) {
1703 return samldb_fill_object(ac);
1706 if (samdb_find_attribute(ldb, ac->msg,
1707 "objectclass", "group") != NULL) {
1710 ret = samldb_objectclass_trigger(ac);
1711 if (ret != LDB_SUCCESS) {
1715 return samldb_fill_object(ac);
1718 /* perhaps a foreignSecurityPrincipal? */
1719 if (samdb_find_attribute(ldb, ac->msg,
1721 "foreignSecurityPrincipal") != NULL) {
1722 return samldb_fill_foreignSecurityPrincipal_object(ac);
1725 if (samdb_find_attribute(ldb, ac->msg,
1726 "objectclass", "classSchema") != NULL) {
1727 ret = samldb_schema_info_update(ac);
1728 if (ret != LDB_SUCCESS) {
1733 ac->type = "classSchema";
1734 return samldb_fill_object(ac);
1737 if (samdb_find_attribute(ldb, ac->msg,
1738 "objectclass", "attributeSchema") != NULL) {
1739 ret = samldb_schema_info_update(ac);
1740 if (ret != LDB_SUCCESS) {
1745 ac->type = "attributeSchema";
1746 return samldb_fill_object(ac);
1751 /* nothing matched, go on */
1752 return ldb_next_request(module, req);
1756 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
1758 struct ldb_context *ldb;
1759 struct samldb_ctx *ac;
1760 struct ldb_message_element *el;
1761 bool modified = false;
1764 if (ldb_dn_is_special(req->op.mod.message->dn)) {
1765 /* do not manipulate our control entries */
1766 return ldb_next_request(module, req);
1769 ldb = ldb_module_get_ctx(module);
1771 /* make sure that "objectSid" is not specified */
1772 el = ldb_msg_find_element(req->op.mod.message, "objectSid");
1774 ldb_set_errstring(ldb,
1775 "samldb: objectSid must not be specified!");
1776 return LDB_ERR_UNWILLING_TO_PERFORM;
1778 /* make sure that "sAMAccountType" is not specified */
1779 el = ldb_msg_find_element(req->op.mod.message, "sAMAccountType");
1781 ldb_set_errstring(ldb,
1782 "samldb: sAMAccountType must not be specified!");
1783 return LDB_ERR_UNWILLING_TO_PERFORM;
1785 /* make sure that "isCriticalSystemObject" is not specified */
1786 el = ldb_msg_find_element(req->op.mod.message, "isCriticalSystemObject");
1788 if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID) == NULL) {
1789 ldb_set_errstring(ldb,
1790 "samldb: isCriticalSystemObject must not be specified!");
1791 return LDB_ERR_UNWILLING_TO_PERFORM;
1795 /* msDS-IntId is not allowed to be modified
1796 * except when modification comes from replication */
1797 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
1798 if (!ldb_request_get_control(req,
1799 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1800 return LDB_ERR_CONSTRAINT_VIOLATION;
1804 ac = samldb_ctx_init(module, req);
1806 return ldb_operr(ldb);
1809 /* build the new msg */
1810 ac->msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
1811 if (ac->msg == NULL) {
1813 ldb_debug(ldb, LDB_DEBUG_FATAL,
1814 "samldb_modify: ldb_msg_copy_shallow failed!\n");
1815 return ldb_operr(ldb);
1818 el = ldb_msg_find_element(ac->msg, "primaryGroupID");
1820 ret = samldb_prim_group_change(ac);
1821 if (ret != LDB_SUCCESS) {
1826 el = ldb_msg_find_element(ac->msg, "userAccountControl");
1829 ret = samldb_user_account_control_change(ac);
1830 if (ret != LDB_SUCCESS) {
1835 el = ldb_msg_find_element(ac->msg, "groupType");
1838 ret = samldb_group_type_change(ac);
1839 if (ret != LDB_SUCCESS) {
1844 el = ldb_msg_find_element(ac->msg, "sAMAccountName");
1846 ret = samldb_sam_accountname_check(ac);
1847 if (ret != LDB_SUCCESS) {
1852 el = ldb_msg_find_element(ac->msg, "member");
1854 ret = samldb_member_check(ac);
1855 if (ret != LDB_SUCCESS) {
1860 el = ldb_msg_find_element(ac->msg, "dNSHostName");
1863 ret = samldb_service_principal_names_change(ac);
1864 if (ret != LDB_SUCCESS) {
1870 struct ldb_request *child_req;
1872 /* Now perform the real modifications as a child request */
1873 ret = ldb_build_mod_req(&child_req, ldb, ac,
1876 req, dsdb_next_callback,
1878 LDB_REQ_SET_LOCATION(child_req);
1879 if (ret != LDB_SUCCESS) {
1883 return ldb_next_request(module, child_req);
1888 /* no change which interests us, go on */
1889 return ldb_next_request(module, req);
1894 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
1896 struct ldb_context *ldb;
1897 struct dom_sid *sid;
1902 ldb = ldb_module_get_ctx(ac->module);
1904 /* Finds out the SID/RID of the SAM object */
1905 sid = samdb_search_dom_sid(ldb, ac, ac->req->op.del.dn, "objectSid",
1908 /* No SID - it might not be a SAM object - therefore ok */
1911 status = dom_sid_split_rid(ac, sid, NULL, &rid);
1912 if (!NT_STATUS_IS_OK(status)) {
1913 return ldb_operr(ldb);
1916 /* Special object (security principal?) */
1920 /* Deny delete requests from groups which are primary ones */
1921 count = samdb_search_count(ldb, ac, NULL,
1922 "(&(primaryGroupID=%u)(objectClass=user))",
1925 return ldb_operr(ldb);
1928 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1934 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
1936 struct samldb_ctx *ac;
1939 if (ldb_dn_is_special(req->op.del.dn)) {
1940 /* do not manipulate our control entries */
1941 return ldb_next_request(module, req);
1944 ac = samldb_ctx_init(module, req);
1946 return ldb_operr(ldb_module_get_ctx(module));
1949 ret = samldb_prim_group_users_check(ac);
1950 if (ret != LDB_SUCCESS) {
1956 return ldb_next_request(module, req);
1961 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
1963 struct ldb_context *ldb = ldb_module_get_ctx(module);
1964 struct dsdb_fsmo_extended_op *exop;
1967 exop = talloc_get_type(req->op.extended.data,
1968 struct dsdb_fsmo_extended_op);
1970 ldb_set_errstring(ldb,
1971 "samldb_extended_allocate_rid_pool: invalid extended data");
1972 return LDB_ERR_PROTOCOL_ERROR;
1975 ret = ridalloc_allocate_rid_pool_fsmo(module, exop);
1976 if (ret != LDB_SUCCESS) {
1980 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
1983 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
1985 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
1986 return samldb_extended_allocate_rid_pool(module, req);
1989 return ldb_next_request(module, req);
1993 static const struct ldb_module_ops ldb_samldb_module_ops = {
1996 .modify = samldb_modify,
1997 .del = samldb_delete,
1998 .extended = samldb_extended
2002 int ldb_samldb_module_init(const char *version)
2004 return ldb_register_module(&ldb_samldb_module_ops);