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: add embedded user/group creation functionality
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 "libcli/security/security.h"
38 #include "librpc/gen_ndr/ndr_security.h"
39 #include "../lib/util/util_ldb.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 /* holds the entry SID */
65 /* holds a generic dn */
68 /* used in conjunction with "sid" in "samldb_dn_from_sid" */
69 struct ldb_dn *res_dn;
71 /* used in conjunction with "dn" in "samldb_sid_from_dn" */
72 struct dom_sid *res_sid;
74 /* used in "samldb_user_dn_to_prim_group_rid" */
75 uint32_t prim_group_rid;
77 /* used in "samldb_group_add_member" and "samldb_group_del_member" */
78 struct ldb_dn *group_dn;
79 struct ldb_dn *member_dn;
81 /* used in "samldb_primary_group_change" */
82 struct ldb_dn *user_dn;
83 struct ldb_dn *old_prim_group_dn, *new_prim_group_dn;
85 /* all the async steps necessary to complete the operation */
86 struct samldb_step *steps;
87 struct samldb_step *curstep;
89 /* If someone set an ares to forward controls and response back to the caller */
90 struct ldb_reply *ares;
93 static struct samldb_ctx *samldb_ctx_init(struct ldb_module *module,
94 struct ldb_request *req)
96 struct ldb_context *ldb;
97 struct samldb_ctx *ac;
99 ldb = ldb_module_get_ctx(module);
101 ac = talloc_zero(req, struct samldb_ctx);
113 static int samldb_add_step(struct samldb_ctx *ac, samldb_step_fn_t fn)
115 struct samldb_step *step, *stepper;
117 step = talloc_zero(ac, struct samldb_step);
119 return LDB_ERR_OPERATIONS_ERROR;
124 if (ac->steps == NULL) {
128 if (ac->curstep == NULL)
129 return LDB_ERR_OPERATIONS_ERROR;
130 for (stepper = ac->curstep; stepper->next != NULL;
131 stepper = stepper->next);
132 stepper->next = step;
138 static int samldb_first_step(struct samldb_ctx *ac)
140 if (ac->steps == NULL) {
141 return LDB_ERR_OPERATIONS_ERROR;
144 ac->curstep = ac->steps;
145 return ac->curstep->fn(ac);
148 static int samldb_next_step(struct samldb_ctx *ac)
150 if (ac->curstep->next) {
151 ac->curstep = ac->curstep->next;
152 return ac->curstep->fn(ac);
155 /* we exit the samldb module here */
156 /* If someone set an ares to forward controls and response back to the caller, use them */
158 return ldb_module_done(ac->req, ac->ares->controls,
159 ac->ares->response, LDB_SUCCESS);
161 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
165 static int samldb_generate_samAccountName(struct ldb_message *msg)
169 /* Format: $000000-000000000000 */
171 name = talloc_asprintf(msg, "$%.6X-%.6X%.6X",
172 (unsigned int)generate_random(),
173 (unsigned int)generate_random(),
174 (unsigned int)generate_random());
176 return LDB_ERR_OPERATIONS_ERROR;
178 return ldb_msg_add_steal_string(msg, "samAccountName", name);
182 * samldb_check_samAccountName (async)
185 static int samldb_check_samAccountName_callback(struct ldb_request *req,
186 struct ldb_reply *ares)
188 struct samldb_ctx *ac;
191 ac = talloc_get_type(req->context, struct samldb_ctx);
193 if (ares->error != LDB_SUCCESS) {
194 return ldb_module_done(ac->req, ares->controls,
195 ares->response, ares->error);
198 switch (ares->type) {
199 case LDB_REPLY_ENTRY:
200 /* if we get an entry it means this samAccountName
202 return ldb_module_done(ac->req, NULL, NULL,
203 LDB_ERR_ENTRY_ALREADY_EXISTS);
205 case LDB_REPLY_REFERRAL:
212 /* not found, go on */
214 ret = samldb_next_step(ac);
218 if (ret != LDB_SUCCESS) {
219 return ldb_module_done(ac->req, NULL, NULL, ret);
225 static int samldb_check_samAccountName(struct samldb_ctx *ac)
227 struct ldb_context *ldb;
228 struct ldb_request *req;
233 ldb = ldb_module_get_ctx(ac->module);
235 if (ldb_msg_find_element(ac->msg, "samAccountName") == NULL) {
236 ret = samldb_generate_samAccountName(ac->msg);
237 if (ret != LDB_SUCCESS) {
242 name = ldb_msg_find_attr_as_string(ac->msg, "samAccountName", NULL);
244 return LDB_ERR_OPERATIONS_ERROR;
246 filter = talloc_asprintf(ac, "samAccountName=%s",
247 ldb_binary_encode_string(ac, name));
248 if (filter == NULL) {
249 return LDB_ERR_OPERATIONS_ERROR;
252 ret = ldb_build_search_req(&req, ldb, ac,
253 ldb_get_default_basedn(ldb),
257 ac, samldb_check_samAccountName_callback,
260 if (ret != LDB_SUCCESS) {
263 return ldb_next_request(ac->module, req);
267 static int samldb_check_samAccountType(struct samldb_ctx *ac)
269 struct ldb_context *ldb;
270 unsigned int account_type;
271 unsigned int group_type;
275 ldb = ldb_module_get_ctx(ac->module);
277 /* make sure sAMAccountType is not specified */
278 if (ldb_msg_find_element(ac->msg, "sAMAccountType") != NULL) {
279 ldb_asprintf_errstring(ldb,
280 "sAMAccountType must not be specified!");
281 return LDB_ERR_UNWILLING_TO_PERFORM;
284 if (strcmp("user", ac->type) == 0) {
285 uac = samdb_result_uint(ac->msg, "userAccountControl", 0);
287 ldb_asprintf_errstring(ldb,
288 "userAccountControl invalid!");
289 return LDB_ERR_UNWILLING_TO_PERFORM;
291 account_type = ds_uf2atype(uac);
292 ret = samdb_msg_add_uint(ldb,
296 if (ret != LDB_SUCCESS) {
301 if (strcmp("group", ac->type) == 0) {
303 group_type = samdb_result_uint(ac->msg, "groupType", 0);
304 if (group_type == 0) {
305 ldb_asprintf_errstring(ldb,
306 "groupType invalid!\n");
307 return LDB_ERR_UNWILLING_TO_PERFORM;
309 account_type = ds_gtype2atype(group_type);
310 ret = samdb_msg_add_uint(ldb,
314 if (ret != LDB_SUCCESS) {
320 return samldb_next_step(ac);
323 static bool samldb_msg_add_sid(struct ldb_message *msg,
325 const struct dom_sid *sid)
328 enum ndr_err_code ndr_err;
330 ndr_err = ndr_push_struct_blob(&v, msg, NULL, sid,
331 (ndr_push_flags_fn_t)ndr_push_dom_sid);
332 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
335 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
339 /* allocate a SID using our RID Set */
340 static int samldb_allocate_sid(struct samldb_ctx *ac)
344 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
346 ret = ridalloc_allocate_rid(ac->module, &rid);
347 if (ret != LDB_SUCCESS) {
351 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
352 if (ac->sid == NULL) {
353 ldb_module_oom(ac->module);
354 return LDB_ERR_OPERATIONS_ERROR;
357 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", ac->sid)) {
358 return LDB_ERR_OPERATIONS_ERROR;
361 return samldb_next_step(ac);
365 * samldb_dn_from_sid (async)
368 static int samldb_dn_from_sid(struct samldb_ctx *ac);
370 static int samldb_dn_from_sid_callback(struct ldb_request *req,
371 struct ldb_reply *ares)
373 struct ldb_context *ldb;
374 struct samldb_ctx *ac;
377 ac = talloc_get_type(req->context, struct samldb_ctx);
378 ldb = ldb_module_get_ctx(ac->module);
381 ret = LDB_ERR_OPERATIONS_ERROR;
384 if (ares->error != LDB_SUCCESS) {
385 return ldb_module_done(ac->req, ares->controls,
386 ares->response, ares->error);
389 switch (ares->type) {
390 case LDB_REPLY_ENTRY:
392 if (ac->res_dn != NULL) {
394 ldb_set_errstring(ldb,
395 "Invalid number of results while searching "
396 "for domain objects!");
397 ret = LDB_ERR_OPERATIONS_ERROR;
400 ac->res_dn = ldb_dn_copy(ac, ares->message->dn);
406 case LDB_REPLY_REFERRAL:
415 /* found or not found, go on */
416 ret = samldb_next_step(ac);
421 if (ret != LDB_SUCCESS) {
422 return ldb_module_done(ac->req, NULL, NULL, ret);
428 /* Finds the DN "res_dn" of an object with a given SID "sid" */
429 static int samldb_dn_from_sid(struct samldb_ctx *ac)
431 struct ldb_context *ldb;
432 static const char * const attrs[] = { NULL };
433 struct ldb_request *req;
437 ldb = ldb_module_get_ctx(ac->module);
440 return LDB_ERR_OPERATIONS_ERROR;
442 filter = talloc_asprintf(ac, "(objectSid=%s)",
443 ldap_encode_ndr_dom_sid(ac, ac->sid));
445 return LDB_ERR_OPERATIONS_ERROR;
447 ret = ldb_build_search_req(&req, ldb, ac,
448 ldb_get_default_basedn(ldb),
452 ac, samldb_dn_from_sid_callback,
454 if (ret != LDB_SUCCESS)
457 return ldb_next_request(ac->module, req);
461 static int samldb_check_primaryGroupID_1(struct samldb_ctx *ac)
463 struct ldb_context *ldb;
466 ldb = ldb_module_get_ctx(ac->module);
468 rid = samdb_result_uint(ac->msg, "primaryGroupID", ~0);
469 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
471 return LDB_ERR_OPERATIONS_ERROR;
474 return samldb_next_step(ac);
477 static int samldb_check_primaryGroupID_2(struct samldb_ctx *ac)
479 if (ac->res_dn == NULL) {
480 struct ldb_context *ldb;
481 ldb = ldb_module_get_ctx(ac->module);
482 ldb_asprintf_errstring(ldb,
483 "Failed to find group sid %s!",
484 dom_sid_string(ac->sid, ac->sid));
485 return LDB_ERR_UNWILLING_TO_PERFORM;
488 return samldb_next_step(ac);
493 * samldb_set_defaultObjectCategory_callback (async)
496 static int samldb_set_defaultObjectCategory_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 ret = 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!");
522 ret = LDB_ERR_OPERATIONS_ERROR;
526 ret = samldb_next_step(ac);
529 if (ret != LDB_SUCCESS) {
530 return ldb_module_done(ac->req, NULL, NULL, ret);
536 static int samldb_set_defaultObjectCategory(struct samldb_ctx *ac)
538 struct ldb_context *ldb;
539 struct ldb_message *msg;
540 struct ldb_request *req;
543 ldb = ldb_module_get_ctx(ac->module);
545 /* (Re)set the default object category to have it set to the DN in the
547 msg = ldb_msg_new(ac);
548 msg->dn = ac->msg->dn;
549 ldb_msg_add_empty(msg, "defaultObjectCategory",
550 LDB_FLAG_MOD_REPLACE, NULL);
551 ldb_msg_add_steal_string(msg, "defaultObjectCategory",
552 ldb_dn_alloc_linearized(msg, ac->dn));
554 ret = ldb_build_mod_req(&req, ldb, ac,
557 samldb_set_defaultObjectCategory_callback,
559 if (ret != LDB_SUCCESS) {
564 return ldb_next_request(ac->module, req);
568 * samldb_find_for_defaultObjectCategory (async)
571 static int samldb_find_for_defaultObjectCategory_callback(struct ldb_request *req,
572 struct ldb_reply *ares)
574 struct ldb_context *ldb;
575 struct samldb_ctx *ac;
578 ac = talloc_get_type(req->context, struct samldb_ctx);
579 ldb = ldb_module_get_ctx(ac->module);
582 ret = LDB_ERR_OPERATIONS_ERROR;
585 if (ares->error != LDB_SUCCESS) {
586 if (ares->error == LDB_ERR_NO_SUCH_OBJECT) {
587 if (ldb_request_get_control(ac->req,
588 LDB_CONTROL_RELAX_OID) != NULL) {
589 /* Don't be pricky when the DN doesn't exist */
590 /* if we have the RELAX control specified */
591 ac->dn = req->op.search.base;
592 return samldb_next_step(ac);
594 ldb_set_errstring(ldb,
595 "samldb_find_defaultObjectCategory: "
596 "Invalid DN for 'defaultObjectCategory'!");
597 ares->error = LDB_ERR_CONSTRAINT_VIOLATION;
601 return ldb_module_done(ac->req, ares->controls,
602 ares->response, ares->error);
605 switch (ares->type) {
606 case LDB_REPLY_ENTRY:
607 ac->dn = talloc_steal(ac, ares->message->dn);
612 case LDB_REPLY_REFERRAL:
621 if (ac->dn != NULL) {
622 /* when found go on */
623 ret = samldb_next_step(ac);
625 ret = LDB_ERR_OPERATIONS_ERROR;
631 if (ret != LDB_SUCCESS) {
632 return ldb_module_done(ac->req, NULL, NULL, ret);
638 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
640 struct ldb_context *ldb;
641 struct ldb_request *req;
642 static const char *no_attrs[] = { NULL };
644 const struct ldb_val *val;
645 struct ldb_dn *def_obj_cat_dn;
647 ldb = ldb_module_get_ctx(ac->module);
651 val = ldb_msg_find_ldb_val(ac->msg, "defaultObjectCategory");
653 /* "defaultObjectCategory" has been set by the caller. Do some
654 * checks for consistency.
655 * NOTE: The real constraint check (that 'defaultObjectCategory'
656 * is the DN of the new objectclass or any parent of it) is
658 * For now we say that 'defaultObjectCategory' is valid if it
659 * exists and it is of objectclass "classSchema". */
660 def_obj_cat_dn = ldb_dn_from_ldb_val(ac, ldb, val);
661 if (def_obj_cat_dn == NULL) {
662 ldb_set_errstring(ldb,
663 "samldb_find_defaultObjectCategory: Invalid DN "
664 "for 'defaultObjectCategory'!");
665 return LDB_ERR_CONSTRAINT_VIOLATION;
668 /* "defaultObjectCategory" has not been set by the caller. Use
669 * the entry DN for it. */
670 def_obj_cat_dn = ac->msg->dn;
673 ret = ldb_build_search_req(&req, ldb, ac,
674 def_obj_cat_dn, LDB_SCOPE_BASE,
675 "objectClass=classSchema", no_attrs,
677 ac, samldb_find_for_defaultObjectCategory_callback,
679 if (ret != LDB_SUCCESS) {
683 ret = dsdb_request_add_controls(req,
684 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT);
685 if (ret != LDB_SUCCESS) {
689 return ldb_next_request(ac->module, req);
693 * msDS-IntId attributeSchema attribute handling
694 * during LDB_ADD request processing
696 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
701 uint32_t system_flags;
702 struct ldb_context *ldb;
703 struct ldb_result *ldb_res;
704 struct ldb_dn *schema_dn;
706 ldb = ldb_module_get_ctx(ac->module);
707 schema_dn = ldb_get_schema_basedn(ldb);
709 /* replicated update should always go through */
710 if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
714 /* msDS-IntId is handled by system and should never be
715 * passed by clients */
716 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
717 return LDB_ERR_UNWILLING_TO_PERFORM;
720 /* do not generate msDS-IntId if Relax control is passed */
721 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
725 /* check Functional Level */
726 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
730 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
731 system_flags = ldb_msg_find_attr_as_uint(ac->msg, "systemFlags", 0);
732 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
736 /* Generate new value for msDs-IntId
737 * Value should be in 0x80000000..0xBFFFFFFF range */
738 msds_intid = generate_random() % 0X3FFFFFFF;
739 msds_intid += 0x80000000;
741 /* probe id values until unique one is found */
744 if (msds_intid > 0xBFFFFFFF) {
745 msds_intid = 0x80000001;
748 ret = dsdb_module_search(ac->module, ac,
750 schema_dn, LDB_SCOPE_ONELEVEL, NULL, 0,
751 "(msDS-IntId=%d)", msds_intid);
752 if (ret != LDB_SUCCESS) {
753 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
754 __location__": Searching for msDS-IntId=%d failed - %s\n",
757 return LDB_ERR_OPERATIONS_ERROR;
759 id_exists = (ldb_res->count > 0);
761 talloc_free(ldb_res);
764 return ldb_msg_add_fmt(ac->msg, "msDS-IntId", "%d", msds_intid);
769 * samldb_add_entry (async)
772 static int samldb_add_entry_callback(struct ldb_request *req,
773 struct ldb_reply *ares)
775 struct ldb_context *ldb;
776 struct samldb_ctx *ac;
779 ac = talloc_get_type(req->context, struct samldb_ctx);
780 ldb = ldb_module_get_ctx(ac->module);
783 return ldb_module_done(ac->req, NULL, NULL,
784 LDB_ERR_OPERATIONS_ERROR);
787 if (ares->type == LDB_REPLY_REFERRAL) {
788 return ldb_module_send_referral(ac->req, ares->referral);
791 if (ares->error != LDB_SUCCESS) {
792 return ldb_module_done(ac->req, ares->controls,
793 ares->response, ares->error);
795 if (ares->type != LDB_REPLY_DONE) {
796 ldb_set_errstring(ldb,
797 "Invalid reply type!\n");
798 return ldb_module_done(ac->req, NULL, NULL,
799 LDB_ERR_OPERATIONS_ERROR);
802 /* The caller may wish to get controls back from the add */
803 ac->ares = talloc_steal(ac, ares);
805 ret = samldb_next_step(ac);
806 if (ret != LDB_SUCCESS) {
807 return ldb_module_done(ac->req, NULL, NULL, ret);
812 static int samldb_add_entry(struct samldb_ctx *ac)
814 struct ldb_context *ldb;
815 struct ldb_request *req;
818 ldb = ldb_module_get_ctx(ac->module);
820 ret = ldb_build_add_req(&req, ldb, ac,
823 ac, samldb_add_entry_callback,
825 if (ret != LDB_SUCCESS) {
829 return ldb_next_request(ac->module, req);
833 * return true if msg carries an attributeSchema that is intended to be RODC
834 * filtered but is also a system-critical attribute.
836 static bool check_rodc_critical_attribute(struct ldb_message *msg)
838 uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
840 schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
841 searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
842 rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE | SEARCH_FLAG_CONFIDENTIAL);
844 if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
845 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
853 static int samldb_fill_object(struct samldb_ctx *ac, const char *type)
855 struct ldb_context *ldb;
856 struct loadparm_context *lp_ctx;
857 enum sid_generator sid_generator;
860 ldb = ldb_module_get_ctx(ac->module);
862 /* Add informations for the different account types */
864 if (strcmp(ac->type, "user") == 0) {
865 ret = samdb_find_or_add_attribute(ldb, ac->msg,
866 "userAccountControl", "546");
867 if (ret != LDB_SUCCESS) return ret;
868 ret = samdb_find_or_add_attribute(ldb, ac->msg,
870 if (ret != LDB_SUCCESS) return ret;
871 ret = samdb_find_or_add_attribute(ldb, ac->msg,
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,
878 "badPasswordTime", "0");
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 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
890 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
891 "primaryGroupID", DOMAIN_RID_USERS);
892 if (ret != LDB_SUCCESS) return ret;
894 ret = samdb_find_or_add_attribute(ldb, ac->msg,
895 "accountExpires", "9223372036854775807");
896 if (ret != LDB_SUCCESS) return ret;
897 ret = samdb_find_or_add_attribute(ldb, ac->msg,
899 if (ret != LDB_SUCCESS) return ret;
900 } else if (strcmp(ac->type, "group") == 0) {
901 ret = samdb_find_or_add_attribute(ldb, ac->msg,
902 "groupType", "-2147483646");
903 if (ret != LDB_SUCCESS) return ret;
904 } else if (strcmp(ac->type, "classSchema") == 0) {
905 const struct ldb_val *rdn_value;
907 ret = samdb_find_or_add_attribute(ldb, ac->msg,
909 if (ret != LDB_SUCCESS) return ret;
911 /* do not allow to mark an attributeSchema as RODC filtered if it
912 * is system-critical */
913 if (check_rodc_critical_attribute(ac->msg)) {
914 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
915 ldb_dn_get_linearized(ac->msg->dn));
916 return LDB_ERR_UNWILLING_TO_PERFORM;
920 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
921 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
922 /* the RDN has prefix "CN" */
923 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
924 samdb_cn_to_lDAPDisplayName(ac,
925 (const char *) rdn_value->data));
926 if (ret != LDB_SUCCESS) {
932 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
935 guid = GUID_random();
936 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
937 if (ret != LDB_SUCCESS) {
943 ret = samldb_add_step(ac, samldb_add_entry);
944 if (ret != LDB_SUCCESS) return ret;
946 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
947 if (ret != LDB_SUCCESS) return ret;
949 ret = samldb_add_step(ac, samldb_set_defaultObjectCategory);
950 if (ret != LDB_SUCCESS) return ret;
952 return samldb_first_step(ac);
953 } else if (strcmp(ac->type, "attributeSchema") == 0) {
954 const struct ldb_val *rdn_value;
955 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
956 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
957 /* the RDN has prefix "CN" */
958 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
959 samdb_cn_to_lDAPDisplayName(ac,
960 (const char *) rdn_value->data));
961 if (ret != LDB_SUCCESS) {
967 /* do not allow to mark an attributeSchema as RODC filtered if it
968 * is system-critical */
969 if (check_rodc_critical_attribute(ac->msg)) {
970 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical attribute with RODC filtering",
971 ldb_dn_get_linearized(ac->msg->dn));
972 return LDB_ERR_UNWILLING_TO_PERFORM;
975 ret = samdb_find_or_add_attribute(ldb, ac->msg,
976 "isSingleValued", "FALSE");
977 if (ret != LDB_SUCCESS) return ret;
979 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
982 guid = GUID_random();
983 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
984 if (ret != LDB_SUCCESS) {
990 /* handle msDS-IntID attribute */
991 ret = samldb_add_handle_msDS_IntId(ac);
992 if (ret != LDB_SUCCESS) return ret;
994 ret = samldb_add_step(ac, samldb_add_entry);
995 if (ret != LDB_SUCCESS) return ret;
997 return samldb_first_step(ac);
999 ldb_asprintf_errstring(ldb,
1000 "Invalid entry type!");
1001 return LDB_ERR_OPERATIONS_ERROR;
1004 /* check if we have a valid samAccountName */
1005 ret = samldb_add_step(ac, samldb_check_samAccountName);
1006 if (ret != LDB_SUCCESS) return ret;
1008 /* check account_type/group_type */
1009 ret = samldb_add_step(ac, samldb_check_samAccountType);
1010 if (ret != LDB_SUCCESS) return ret;
1012 /* check if we have a valid primary group ID */
1013 if (strcmp(ac->type, "user") == 0) {
1014 ret = samldb_add_step(ac, samldb_check_primaryGroupID_1);
1015 if (ret != LDB_SUCCESS) return ret;
1016 ret = samldb_add_step(ac, samldb_dn_from_sid);
1017 if (ret != LDB_SUCCESS) return ret;
1018 ret = samldb_add_step(ac, samldb_check_primaryGroupID_2);
1019 if (ret != LDB_SUCCESS) return ret;
1022 lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
1023 struct loadparm_context);
1025 /* don't allow objectSID to be specified without the RELAX control */
1026 ac->sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
1027 if (ac->sid && !ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) &&
1028 !dsdb_module_am_system(ac->module)) {
1029 ldb_asprintf_errstring(ldb, "No SID may be specified in user/group creation for %s",
1030 ldb_dn_get_linearized(ac->msg->dn));
1031 return LDB_ERR_UNWILLING_TO_PERFORM;
1035 sid_generator = lp_sid_generator(lp_ctx);
1036 if (sid_generator == SID_GENERATOR_INTERNAL) {
1037 ret = samldb_add_step(ac, samldb_allocate_sid);
1038 if (ret != LDB_SUCCESS) return ret;
1042 /* finally proceed with adding the entry */
1043 ret = samldb_add_step(ac, samldb_add_entry);
1044 if (ret != LDB_SUCCESS) return ret;
1046 return samldb_first_step(ac);
1049 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
1051 struct ldb_context *ldb;
1054 ldb = ldb_module_get_ctx(ac->module);
1056 ac->sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
1057 if (ac->sid == NULL) {
1058 ac->sid = dom_sid_parse_talloc(ac->msg,
1059 (const char *)ldb_dn_get_rdn_val(ac->msg->dn)->data);
1061 ldb_set_errstring(ldb,
1062 "No valid SID found in "
1063 "ForeignSecurityPrincipal CN!");
1065 return LDB_ERR_CONSTRAINT_VIOLATION;
1067 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", ac->sid)) {
1069 return LDB_ERR_OPERATIONS_ERROR;
1073 /* finally proceed with adding the entry */
1074 ret = samldb_add_step(ac, samldb_add_entry);
1075 if (ret != LDB_SUCCESS) return ret;
1077 return samldb_first_step(ac);
1080 static int samldb_check_rdn(struct ldb_module *module, struct ldb_dn *dn)
1082 struct ldb_context *ldb;
1083 const char *rdn_name;
1085 ldb = ldb_module_get_ctx(module);
1086 rdn_name = ldb_dn_get_rdn_name(dn);
1088 if (strcasecmp(rdn_name, "cn") != 0) {
1089 ldb_asprintf_errstring(ldb,
1090 "Bad RDN (%s=) for samldb object, "
1091 "should be CN=!", rdn_name);
1092 return LDB_ERR_CONSTRAINT_VIOLATION;
1098 static int samldb_schema_info_update(struct samldb_ctx *ac)
1101 struct ldb_context *ldb;
1102 struct dsdb_schema *schema;
1104 /* replicated update should always go through */
1105 if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1109 /* do not update schemaInfo during provisioning */
1110 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
1114 ldb = ldb_module_get_ctx(ac->module);
1115 schema = dsdb_get_schema(ldb, NULL);
1117 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
1118 "samldb_schema_info_update: no dsdb_schema loaded");
1119 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
1120 return LDB_ERR_OPERATIONS_ERROR;
1123 werr = dsdb_module_schema_info_update(ac->module, schema, 0);
1124 if (!W_ERROR_IS_OK(werr)) {
1125 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
1126 "samldb_schema_info_update: "
1127 "dsdb_module_schema_info_update failed with %s",
1129 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
1130 return LDB_ERR_OPERATIONS_ERROR;
1137 * samldb_user_dn_to_prim_group_rid (async)
1140 static int samldb_user_dn_to_prim_group_rid(struct samldb_ctx *ac);
1142 static int samldb_user_dn_to_prim_group_rid_callback(struct ldb_request *req,
1143 struct ldb_reply *ares)
1145 struct ldb_context *ldb;
1146 struct samldb_ctx *ac;
1149 ac = talloc_get_type(req->context, struct samldb_ctx);
1150 ldb = ldb_module_get_ctx(ac->module);
1153 ret = LDB_ERR_OPERATIONS_ERROR;
1156 if (ares->error != LDB_SUCCESS) {
1157 return ldb_module_done(ac->req, ares->controls,
1158 ares->response, ares->error);
1161 switch (ares->type) {
1162 case LDB_REPLY_ENTRY:
1164 if (ac->prim_group_rid != 0) {
1166 ldb_set_errstring(ldb,
1167 "Invalid number of results while searching "
1168 "for domain objects!");
1169 ret = LDB_ERR_OPERATIONS_ERROR;
1172 ac->prim_group_rid = samdb_result_uint(ares->message,
1173 "primaryGroupID", ~0);
1179 case LDB_REPLY_REFERRAL:
1185 case LDB_REPLY_DONE:
1187 if (ac->prim_group_rid == 0) {
1188 ldb_asprintf_errstring(ldb,
1189 "Unable to get the primary group RID!");
1190 ret = LDB_ERR_OPERATIONS_ERROR;
1195 ret = samldb_next_step(ac);
1200 if (ret != LDB_SUCCESS) {
1201 return ldb_module_done(ac->req, NULL, NULL, ret);
1207 /* Locates the "primaryGroupID" attribute from a certain user specified as
1208 * "user_dn". Saves the result in "prim_group_rid". */
1209 static int samldb_user_dn_to_prim_group_rid(struct samldb_ctx *ac)
1211 struct ldb_context *ldb;
1212 static const char * const attrs[] = { "primaryGroupID", NULL };
1213 struct ldb_request *req;
1216 ldb = ldb_module_get_ctx(ac->module);
1218 if (ac->user_dn == NULL)
1219 return LDB_ERR_OPERATIONS_ERROR;
1221 ret = ldb_build_search_req(&req, ldb, ac,
1226 ac, samldb_user_dn_to_prim_group_rid_callback,
1228 if (ret != LDB_SUCCESS)
1231 return ldb_next_request(ac->module, req);
1235 * samldb_group_add_member (async)
1236 * samldb_group_del_member (async)
1239 static int samldb_group_add_del_member_callback(struct ldb_request *req,
1240 struct ldb_reply *ares)
1242 struct ldb_context *ldb;
1243 struct samldb_ctx *ac;
1246 ac = talloc_get_type(req->context, struct samldb_ctx);
1247 ldb = ldb_module_get_ctx(ac->module);
1250 ret = LDB_ERR_OPERATIONS_ERROR;
1254 if (ares->type == LDB_REPLY_REFERRAL) {
1255 return ldb_module_send_referral(ac->req, ares->referral);
1258 if (ares->error != LDB_SUCCESS) {
1259 if (ares->error == LDB_ERR_NO_SUCH_ATTRIBUTE) {
1260 /* On error "NO_SUCH_ATTRIBUTE" (delete of an invalid
1261 * "member" attribute) return "UNWILLING_TO_PERFORM" */
1262 ares->error = LDB_ERR_UNWILLING_TO_PERFORM;
1264 return ldb_module_done(ac->req, ares->controls,
1265 ares->response, ares->error);
1267 if (ares->type != LDB_REPLY_DONE) {
1268 ldb_set_errstring(ldb,
1269 "Invalid reply type!");
1270 ret = LDB_ERR_OPERATIONS_ERROR;
1274 ret = samldb_next_step(ac);
1277 if (ret != LDB_SUCCESS) {
1278 return ldb_module_done(ac->req, NULL, NULL, ret);
1284 /* Adds a member with DN "member_dn" to a group with DN "group_dn" */
1285 static int samldb_group_add_member(struct samldb_ctx *ac)
1287 struct ldb_context *ldb;
1288 struct ldb_request *req;
1289 struct ldb_message *msg;
1292 ldb = ldb_module_get_ctx(ac->module);
1294 if ((ac->group_dn == NULL) || (ac->member_dn == NULL))
1295 return LDB_ERR_OPERATIONS_ERROR;
1297 msg = ldb_msg_new(ac);
1298 msg->dn = ac->group_dn;
1299 samdb_msg_add_addval(ldb, ac, msg, "member",
1300 ldb_dn_get_linearized(ac->member_dn));
1302 ret = ldb_build_mod_req(&req, ldb, ac,
1304 ac, samldb_group_add_del_member_callback,
1306 if (ret != LDB_SUCCESS)
1309 return ldb_next_request(ac->module, req);
1312 /* Removes a member with DN "member_dn" from a group with DN "group_dn" */
1313 static int samldb_group_del_member(struct samldb_ctx *ac)
1315 struct ldb_context *ldb;
1316 struct ldb_request *req;
1317 struct ldb_message *msg;
1320 ldb = ldb_module_get_ctx(ac->module);
1322 if ((ac->group_dn == NULL) || (ac->member_dn == NULL))
1323 return LDB_ERR_OPERATIONS_ERROR;
1325 msg = ldb_msg_new(ac);
1326 msg->dn = ac->group_dn;
1327 samdb_msg_add_delval(ldb, ac, msg, "member",
1328 ldb_dn_get_linearized(ac->member_dn));
1330 ret = ldb_build_mod_req(&req, ldb, ac,
1332 ac, samldb_group_add_del_member_callback,
1334 if (ret != LDB_SUCCESS)
1337 return ldb_next_request(ac->module, req);
1341 static int samldb_prim_group_change_1(struct samldb_ctx *ac)
1343 struct ldb_context *ldb;
1346 ldb = ldb_module_get_ctx(ac->module);
1348 ac->user_dn = ac->msg->dn;
1350 rid = samdb_result_uint(ac->msg, "primaryGroupID", ~0);
1351 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1352 if (ac->sid == NULL)
1353 return LDB_ERR_OPERATIONS_ERROR;
1356 ac->prim_group_rid = 0;
1358 return samldb_next_step(ac);
1361 static int samldb_prim_group_change_2(struct samldb_ctx *ac)
1363 struct ldb_context *ldb;
1365 ldb = ldb_module_get_ctx(ac->module);
1367 if (ac->res_dn != NULL)
1368 ac->new_prim_group_dn = ac->res_dn;
1370 return LDB_ERR_UNWILLING_TO_PERFORM;
1372 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1373 ac->prim_group_rid);
1374 if (ac->sid == NULL)
1375 return LDB_ERR_OPERATIONS_ERROR;
1378 return samldb_next_step(ac);
1381 static int samldb_prim_group_change_4(struct samldb_ctx *ac);
1382 static int samldb_prim_group_change_5(struct samldb_ctx *ac);
1383 static int samldb_prim_group_change_6(struct samldb_ctx *ac);
1385 static int samldb_prim_group_change_3(struct samldb_ctx *ac)
1389 if (ac->res_dn != NULL)
1390 ac->old_prim_group_dn = ac->res_dn;
1392 return LDB_ERR_UNWILLING_TO_PERFORM;
1394 /* Only update when the primary group changed */
1395 if (ldb_dn_compare(ac->old_prim_group_dn, ac->new_prim_group_dn) != 0) {
1396 ac->member_dn = ac->user_dn;
1397 /* Remove the "member" attribute of the actual (new) primary
1400 ret = samldb_add_step(ac, samldb_prim_group_change_4);
1401 if (ret != LDB_SUCCESS) return ret;
1403 ret = samldb_add_step(ac, samldb_group_del_member);
1404 if (ret != LDB_SUCCESS) return ret;
1406 /* Add a "member" attribute for the previous primary group */
1408 ret = samldb_add_step(ac, samldb_prim_group_change_5);
1409 if (ret != LDB_SUCCESS) return ret;
1411 ret = samldb_add_step(ac, samldb_group_add_member);
1412 if (ret != LDB_SUCCESS) return ret;
1415 ret = samldb_add_step(ac, samldb_prim_group_change_6);
1416 if (ret != LDB_SUCCESS) return ret;
1418 return samldb_next_step(ac);
1421 static int samldb_prim_group_change_4(struct samldb_ctx *ac)
1423 ac->group_dn = ac->new_prim_group_dn;
1425 return samldb_next_step(ac);
1428 static int samldb_prim_group_change_5(struct samldb_ctx *ac)
1430 ac->group_dn = ac->old_prim_group_dn;
1432 return samldb_next_step(ac);
1435 static int samldb_prim_group_change_6(struct samldb_ctx *ac)
1437 return ldb_next_request(ac->module, ac->req);
1440 static int samldb_prim_group_change(struct samldb_ctx *ac)
1444 /* Finds out the DN of the new primary group */
1446 ret = samldb_add_step(ac, samldb_prim_group_change_1);
1447 if (ret != LDB_SUCCESS) return ret;
1449 ret = samldb_add_step(ac, samldb_dn_from_sid);
1450 if (ret != LDB_SUCCESS) return ret;
1452 ret = samldb_add_step(ac, samldb_user_dn_to_prim_group_rid);
1453 if (ret != LDB_SUCCESS) return ret;
1455 /* Finds out the DN of the old primary group */
1457 ret = samldb_add_step(ac, samldb_prim_group_change_2);
1458 if (ret != LDB_SUCCESS) return ret;
1460 ret = samldb_add_step(ac, samldb_dn_from_sid);
1461 if (ret != LDB_SUCCESS) return ret;
1463 ret = samldb_add_step(ac, samldb_prim_group_change_3);
1464 if (ret != LDB_SUCCESS) return ret;
1466 return samldb_first_step(ac);
1470 static int samldb_member_check(struct samldb_ctx *ac)
1472 struct ldb_context *ldb;
1473 struct ldb_message_element *el;
1474 struct ldb_dn *member_dn, *group_dn;
1475 uint32_t prim_group_rid;
1476 struct dom_sid *sid;
1479 ldb = ldb_module_get_ctx(ac->module);
1481 el = ldb_msg_find_element(ac->msg, "member");
1482 for (i = 0; i < el->num_values; i++) {
1483 /* Denies to add "member"s to groups which are primary ones
1485 member_dn = ldb_dn_from_ldb_val(ac, ldb, &el->values[i]);
1486 if (!ldb_dn_validate(member_dn)) {
1487 return LDB_ERR_OPERATIONS_ERROR;
1490 prim_group_rid = samdb_search_uint(ldb, ac, (uint32_t) -1,
1491 member_dn, "primaryGroupID",
1493 if (prim_group_rid == (uint32_t) -1) {
1494 /* the member hasn't to be a user account -> therefore
1495 * no check needed in this case. */
1499 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1502 return LDB_ERR_OPERATIONS_ERROR;
1505 group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSID=%s)",
1506 dom_sid_string(ac, sid));
1507 if (group_dn == NULL) {
1508 return LDB_ERR_OPERATIONS_ERROR;
1511 if (ldb_dn_compare(group_dn, ac->msg->dn) == 0) {
1512 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1516 return ldb_next_request(ac->module, ac->req);
1520 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
1522 struct ldb_context *ldb;
1523 struct dom_sid *sid;
1528 ldb = ldb_module_get_ctx(ac->module);
1530 /* Finds out the SID/RID of the SAM object */
1531 sid = samdb_search_dom_sid(ldb, ac, ac->req->op.del.dn, "objectSID",
1534 /* No SID - it might not be a SAM object - therefore ok */
1535 return ldb_next_request(ac->module, ac->req);
1537 status = dom_sid_split_rid(ac, sid, NULL, &rid);
1538 if (!NT_STATUS_IS_OK(status)) {
1539 return LDB_ERR_OPERATIONS_ERROR;
1542 /* Special object (security principal?) */
1543 return ldb_next_request(ac->module, ac->req);
1546 /* Deny delete requests from groups which are primary ones */
1547 count = samdb_search_count(ldb, NULL,
1548 "(&(primaryGroupID=%u)(objectClass=user))",
1551 return LDB_ERR_OPERATIONS_ERROR;
1554 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1557 return ldb_next_request(ac->module, ac->req);
1562 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
1564 struct ldb_context *ldb;
1565 struct samldb_ctx *ac;
1568 ldb = ldb_module_get_ctx(module);
1569 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
1571 /* do not manipulate our control entries */
1572 if (ldb_dn_is_special(req->op.add.message->dn)) {
1573 return ldb_next_request(module, req);
1576 ac = samldb_ctx_init(module, req);
1578 return LDB_ERR_OPERATIONS_ERROR;
1581 /* build the new msg */
1582 ac->msg = ldb_msg_copy(ac, ac->req->op.add.message);
1585 ldb_debug(ldb, LDB_DEBUG_FATAL,
1586 "samldb_add: ldb_msg_copy failed!\n");
1587 return LDB_ERR_OPERATIONS_ERROR;
1590 if (samdb_find_attribute(ldb, ac->msg,
1591 "objectclass", "computer") != NULL) {
1593 /* make sure the computer object also has the 'user'
1594 * objectclass so it will be handled by the next call */
1595 ret = samdb_find_or_add_value(ldb, ac->msg,
1596 "objectclass", "user");
1597 if (ret != LDB_SUCCESS) {
1603 if (samdb_find_attribute(ldb, ac->msg,
1604 "objectclass", "user") != NULL) {
1606 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1607 if (ret != LDB_SUCCESS) {
1612 return samldb_fill_object(ac, "user");
1615 if (samdb_find_attribute(ldb, ac->msg,
1616 "objectclass", "group") != NULL) {
1618 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1619 if (ret != LDB_SUCCESS) {
1624 return samldb_fill_object(ac, "group");
1627 /* perhaps a foreignSecurityPrincipal? */
1628 if (samdb_find_attribute(ldb, ac->msg,
1630 "foreignSecurityPrincipal") != NULL) {
1632 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1633 if (ret != LDB_SUCCESS) {
1638 return samldb_fill_foreignSecurityPrincipal_object(ac);
1641 if (samdb_find_attribute(ldb, ac->msg,
1642 "objectclass", "classSchema") != NULL) {
1644 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1645 if (ret != LDB_SUCCESS) {
1650 ret = samldb_schema_info_update(ac);
1651 if (ret != LDB_SUCCESS) {
1656 return samldb_fill_object(ac, "classSchema");
1659 if (samdb_find_attribute(ldb, ac->msg,
1660 "objectclass", "attributeSchema") != NULL) {
1662 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1663 if (ret != LDB_SUCCESS) {
1668 ret = samldb_schema_info_update(ac);
1669 if (ret != LDB_SUCCESS) {
1674 return samldb_fill_object(ac, "attributeSchema");
1679 /* nothing matched, go on */
1680 return ldb_next_request(module, req);
1684 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
1686 struct ldb_context *ldb;
1687 struct ldb_message *msg;
1688 struct ldb_message_element *el, *el2;
1690 uint32_t account_type;
1692 if (ldb_dn_is_special(req->op.mod.message->dn)) {
1693 /* do not manipulate our control entries */
1694 return ldb_next_request(module, req);
1697 ldb = ldb_module_get_ctx(module);
1699 if (ldb_msg_find_element(req->op.mod.message, "sAMAccountType") != NULL) {
1700 ldb_asprintf_errstring(ldb,
1701 "sAMAccountType must not be specified!");
1702 return LDB_ERR_UNWILLING_TO_PERFORM;
1705 /* msDS-IntId is not allowed to be modified
1706 * except when modification comes from replication */
1707 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
1708 if (!ldb_request_get_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1709 return LDB_ERR_CONSTRAINT_VIOLATION;
1713 /* TODO: do not modify original request, create a new one */
1715 el = ldb_msg_find_element(req->op.mod.message, "groupType");
1716 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1717 uint32_t group_type;
1719 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1720 req->op.mod.message);
1722 group_type = strtoul((const char *)el->values[0].data, NULL, 0);
1723 account_type = ds_gtype2atype(group_type);
1724 ret = samdb_msg_add_uint(ldb, msg, msg,
1727 if (ret != LDB_SUCCESS) {
1730 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1731 el2->flags = LDB_FLAG_MOD_REPLACE;
1734 el = ldb_msg_find_element(req->op.mod.message, "primaryGroupID");
1735 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1736 struct samldb_ctx *ac;
1738 ac = samldb_ctx_init(module, req);
1740 return LDB_ERR_OPERATIONS_ERROR;
1742 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1743 req->op.mod.message);
1745 return samldb_prim_group_change(ac);
1748 el = ldb_msg_find_element(req->op.mod.message, "userAccountControl");
1749 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1750 uint32_t user_account_control;
1752 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1753 req->op.mod.message);
1755 user_account_control = strtoul((const char *)el->values[0].data,
1757 account_type = ds_uf2atype(user_account_control);
1758 ret = samdb_msg_add_uint(ldb, msg, msg,
1761 if (ret != LDB_SUCCESS) {
1764 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1765 el2->flags = LDB_FLAG_MOD_REPLACE;
1767 if (user_account_control & UF_SERVER_TRUST_ACCOUNT) {
1768 ret = samdb_msg_add_string(ldb, msg, msg,
1769 "isCriticalSystemObject", "TRUE");
1770 if (ret != LDB_SUCCESS) {
1773 el2 = ldb_msg_find_element(msg, "isCriticalSystemObject");
1774 el2->flags = LDB_FLAG_MOD_REPLACE;
1776 /* DCs have primaryGroupID of DOMAIN_RID_DCS */
1777 if (!ldb_msg_find_element(msg, "primaryGroupID")) {
1778 ret = samdb_msg_add_uint(ldb, msg, msg,
1779 "primaryGroupID", DOMAIN_RID_DCS);
1780 if (ret != LDB_SUCCESS) {
1783 el2 = ldb_msg_find_element(msg, "primaryGroupID");
1784 el2->flags = LDB_FLAG_MOD_REPLACE;
1789 el = ldb_msg_find_element(req->op.mod.message, "member");
1790 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1791 struct samldb_ctx *ac;
1793 ac = samldb_ctx_init(module, req);
1795 return LDB_ERR_OPERATIONS_ERROR;
1797 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1798 req->op.mod.message);
1800 return samldb_member_check(ac);
1803 /* nothing matched, go on */
1804 return ldb_next_request(module, req);
1808 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
1810 struct samldb_ctx *ac;
1812 if (ldb_dn_is_special(req->op.del.dn)) {
1813 /* do not manipulate our control entries */
1814 return ldb_next_request(module, req);
1817 ac = samldb_ctx_init(module, req);
1819 return LDB_ERR_OPERATIONS_ERROR;
1821 return samldb_prim_group_users_check(ac);
1824 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
1826 struct ldb_context *ldb = ldb_module_get_ctx(module);
1827 struct dsdb_fsmo_extended_op *exop;
1830 exop = talloc_get_type(req->op.extended.data, struct dsdb_fsmo_extended_op);
1832 ldb_debug(ldb, LDB_DEBUG_FATAL, "samldb_extended_allocate_rid_pool: invalid extended data\n");
1833 return LDB_ERR_PROTOCOL_ERROR;
1836 ret = ridalloc_allocate_rid_pool_fsmo(module, exop);
1837 if (ret != LDB_SUCCESS) {
1841 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
1844 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
1846 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
1847 return samldb_extended_allocate_rid_pool(module, req);
1850 return ldb_next_request(module, req);
1854 _PUBLIC_ const struct ldb_module_ops ldb_samldb_module_ops = {
1857 .modify = samldb_modify,
1858 .del = samldb_delete,
1859 .extended = samldb_extended