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 /* generic counter - used in "samldb_member_check" */
88 /* all the async steps necessary to complete the operation */
89 struct samldb_step *steps;
90 struct samldb_step *curstep;
92 /* If someone set an ares to forward controls and response back to the caller */
93 struct ldb_reply *ares;
96 static struct samldb_ctx *samldb_ctx_init(struct ldb_module *module,
97 struct ldb_request *req)
99 struct ldb_context *ldb;
100 struct samldb_ctx *ac;
102 ldb = ldb_module_get_ctx(module);
104 ac = talloc_zero(req, struct samldb_ctx);
116 static int samldb_add_step(struct samldb_ctx *ac, samldb_step_fn_t fn)
118 struct samldb_step *step, *stepper;
120 step = talloc_zero(ac, struct samldb_step);
122 return LDB_ERR_OPERATIONS_ERROR;
127 if (ac->steps == NULL) {
131 if (ac->curstep == NULL)
132 return LDB_ERR_OPERATIONS_ERROR;
133 for (stepper = ac->curstep; stepper->next != NULL;
134 stepper = stepper->next);
135 stepper->next = step;
141 static int samldb_first_step(struct samldb_ctx *ac)
143 if (ac->steps == NULL) {
144 return LDB_ERR_OPERATIONS_ERROR;
147 ac->curstep = ac->steps;
148 return ac->curstep->fn(ac);
151 static int samldb_next_step(struct samldb_ctx *ac)
153 if (ac->curstep->next) {
154 ac->curstep = ac->curstep->next;
155 return ac->curstep->fn(ac);
158 /* we exit the samldb module here */
159 /* If someone set an ares to forward controls and response back to the caller, use them */
161 return ldb_module_done(ac->req, ac->ares->controls,
162 ac->ares->response, LDB_SUCCESS);
164 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
168 static int samldb_generate_samAccountName(struct ldb_message *msg)
172 /* Format: $000000-000000000000 */
174 name = talloc_asprintf(msg, "$%.6X-%.6X%.6X",
175 (unsigned int)generate_random(),
176 (unsigned int)generate_random(),
177 (unsigned int)generate_random());
179 return LDB_ERR_OPERATIONS_ERROR;
181 return ldb_msg_add_steal_string(msg, "samAccountName", name);
185 * samldb_check_samAccountName (async)
188 static int samldb_check_samAccountName_callback(struct ldb_request *req,
189 struct ldb_reply *ares)
191 struct samldb_ctx *ac;
194 ac = talloc_get_type(req->context, struct samldb_ctx);
196 if (ares->error != LDB_SUCCESS) {
197 return ldb_module_done(ac->req, ares->controls,
198 ares->response, ares->error);
201 switch (ares->type) {
202 case LDB_REPLY_ENTRY:
203 /* if we get an entry it means this samAccountName
205 return ldb_module_done(ac->req, NULL, NULL,
206 LDB_ERR_ENTRY_ALREADY_EXISTS);
208 case LDB_REPLY_REFERRAL:
215 /* not found, go on */
217 ret = samldb_next_step(ac);
221 if (ret != LDB_SUCCESS) {
222 return ldb_module_done(ac->req, NULL, NULL, ret);
228 static int samldb_check_samAccountName(struct samldb_ctx *ac)
230 struct ldb_context *ldb;
231 struct ldb_request *req;
236 ldb = ldb_module_get_ctx(ac->module);
238 if (ldb_msg_find_element(ac->msg, "samAccountName") == NULL) {
239 ret = samldb_generate_samAccountName(ac->msg);
240 if (ret != LDB_SUCCESS) {
245 name = ldb_msg_find_attr_as_string(ac->msg, "samAccountName", NULL);
247 return LDB_ERR_OPERATIONS_ERROR;
249 filter = talloc_asprintf(ac, "samAccountName=%s",
250 ldb_binary_encode_string(ac, name));
251 if (filter == NULL) {
252 return LDB_ERR_OPERATIONS_ERROR;
255 ret = ldb_build_search_req(&req, ldb, ac,
256 ldb_get_default_basedn(ldb),
260 ac, samldb_check_samAccountName_callback,
263 if (ret != LDB_SUCCESS) {
266 return ldb_next_request(ac->module, req);
270 static int samldb_check_samAccountType(struct samldb_ctx *ac)
272 struct ldb_context *ldb;
273 unsigned int account_type;
274 unsigned int group_type;
278 ldb = ldb_module_get_ctx(ac->module);
280 /* make sure sAMAccountType is not specified */
281 if (ldb_msg_find_element(ac->msg, "sAMAccountType") != NULL) {
282 ldb_asprintf_errstring(ldb,
283 "sAMAccountType must not be specified!");
284 return LDB_ERR_UNWILLING_TO_PERFORM;
287 if (strcmp("user", ac->type) == 0) {
288 uac = samdb_result_uint(ac->msg, "userAccountControl", 0);
290 ldb_asprintf_errstring(ldb,
291 "userAccountControl invalid!");
292 return LDB_ERR_UNWILLING_TO_PERFORM;
294 account_type = ds_uf2atype(uac);
295 ret = samdb_msg_add_uint(ldb,
299 if (ret != LDB_SUCCESS) {
304 if (strcmp("group", ac->type) == 0) {
306 group_type = samdb_result_uint(ac->msg, "groupType", 0);
307 if (group_type == 0) {
308 ldb_asprintf_errstring(ldb,
309 "groupType invalid!\n");
310 return LDB_ERR_UNWILLING_TO_PERFORM;
312 account_type = ds_gtype2atype(group_type);
313 ret = samdb_msg_add_uint(ldb,
317 if (ret != LDB_SUCCESS) {
323 return samldb_next_step(ac);
326 static bool samldb_msg_add_sid(struct ldb_message *msg,
328 const struct dom_sid *sid)
331 enum ndr_err_code ndr_err;
333 ndr_err = ndr_push_struct_blob(&v, msg, NULL, sid,
334 (ndr_push_flags_fn_t)ndr_push_dom_sid);
335 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
338 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
342 /* allocate a SID using our RID Set */
343 static int samldb_allocate_sid(struct samldb_ctx *ac)
347 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
349 ret = ridalloc_allocate_rid(ac->module, &rid);
350 if (ret != LDB_SUCCESS) {
354 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
355 if (ac->sid == NULL) {
356 ldb_module_oom(ac->module);
357 return LDB_ERR_OPERATIONS_ERROR;
360 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", ac->sid)) {
361 return LDB_ERR_OPERATIONS_ERROR;
364 return samldb_next_step(ac);
368 * samldb_dn_from_sid (async)
371 static int samldb_dn_from_sid(struct samldb_ctx *ac);
373 static int samldb_dn_from_sid_callback(struct ldb_request *req,
374 struct ldb_reply *ares)
376 struct ldb_context *ldb;
377 struct samldb_ctx *ac;
380 ac = talloc_get_type(req->context, struct samldb_ctx);
381 ldb = ldb_module_get_ctx(ac->module);
384 ret = LDB_ERR_OPERATIONS_ERROR;
387 if (ares->error != LDB_SUCCESS) {
388 return ldb_module_done(ac->req, ares->controls,
389 ares->response, ares->error);
392 switch (ares->type) {
393 case LDB_REPLY_ENTRY:
395 if (ac->res_dn != NULL) {
397 ldb_set_errstring(ldb,
398 "Invalid number of results while searching "
399 "for domain objects!");
400 ret = LDB_ERR_OPERATIONS_ERROR;
403 ac->res_dn = ldb_dn_copy(ac, ares->message->dn);
409 case LDB_REPLY_REFERRAL:
418 /* found or not found, go on */
419 ret = samldb_next_step(ac);
424 if (ret != LDB_SUCCESS) {
425 return ldb_module_done(ac->req, NULL, NULL, ret);
431 /* Finds the DN "res_dn" of an object with a given SID "sid" */
432 static int samldb_dn_from_sid(struct samldb_ctx *ac)
434 struct ldb_context *ldb;
435 static const char * const attrs[] = { NULL };
436 struct ldb_request *req;
440 ldb = ldb_module_get_ctx(ac->module);
443 return LDB_ERR_OPERATIONS_ERROR;
445 filter = talloc_asprintf(ac, "(objectSid=%s)",
446 ldap_encode_ndr_dom_sid(ac, ac->sid));
448 return LDB_ERR_OPERATIONS_ERROR;
450 ret = ldb_build_search_req(&req, ldb, ac,
451 ldb_get_default_basedn(ldb),
455 ac, samldb_dn_from_sid_callback,
457 if (ret != LDB_SUCCESS)
460 return ldb_next_request(ac->module, req);
464 static int samldb_check_primaryGroupID_1(struct samldb_ctx *ac)
466 struct ldb_context *ldb;
469 ldb = ldb_module_get_ctx(ac->module);
471 rid = samdb_result_uint(ac->msg, "primaryGroupID", ~0);
472 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
474 return LDB_ERR_OPERATIONS_ERROR;
477 return samldb_next_step(ac);
480 static int samldb_check_primaryGroupID_2(struct samldb_ctx *ac)
482 if (ac->res_dn == NULL) {
483 struct ldb_context *ldb;
484 ldb = ldb_module_get_ctx(ac->module);
485 ldb_asprintf_errstring(ldb,
486 "Failed to find group sid %s!",
487 dom_sid_string(ac->sid, ac->sid));
488 return LDB_ERR_UNWILLING_TO_PERFORM;
491 return samldb_next_step(ac);
496 * samldb_set_defaultObjectCategory_callback (async)
499 static int samldb_set_defaultObjectCategory_callback(struct ldb_request *req,
500 struct ldb_reply *ares)
502 struct ldb_context *ldb;
503 struct samldb_ctx *ac;
506 ac = talloc_get_type(req->context, struct samldb_ctx);
507 ldb = ldb_module_get_ctx(ac->module);
510 ret = LDB_ERR_OPERATIONS_ERROR;
514 if (ares->type == LDB_REPLY_REFERRAL) {
515 return ldb_module_send_referral(ac->req, ares->referral);
518 if (ares->error != LDB_SUCCESS) {
519 return ldb_module_done(ac->req, ares->controls,
520 ares->response, ares->error);
522 if (ares->type != LDB_REPLY_DONE) {
523 ldb_set_errstring(ldb,
524 "Invalid reply type!");
525 ret = LDB_ERR_OPERATIONS_ERROR;
529 ret = samldb_next_step(ac);
532 if (ret != LDB_SUCCESS) {
533 return ldb_module_done(ac->req, NULL, NULL, ret);
539 static int samldb_set_defaultObjectCategory(struct samldb_ctx *ac)
541 struct ldb_context *ldb;
542 struct ldb_message *msg;
543 struct ldb_request *req;
546 ldb = ldb_module_get_ctx(ac->module);
548 /* (Re)set the default object category to have it set to the DN in the
550 msg = ldb_msg_new(ac);
551 msg->dn = ac->msg->dn;
552 ldb_msg_add_empty(msg, "defaultObjectCategory",
553 LDB_FLAG_MOD_REPLACE, NULL);
554 ldb_msg_add_steal_string(msg, "defaultObjectCategory",
555 ldb_dn_alloc_linearized(msg, ac->dn));
557 ret = ldb_build_mod_req(&req, ldb, ac,
560 samldb_set_defaultObjectCategory_callback,
562 if (ret != LDB_SUCCESS) {
567 return ldb_next_request(ac->module, req);
571 * samldb_find_for_defaultObjectCategory (async)
574 static int samldb_find_for_defaultObjectCategory_callback(struct ldb_request *req,
575 struct ldb_reply *ares)
577 struct ldb_context *ldb;
578 struct samldb_ctx *ac;
581 ac = talloc_get_type(req->context, struct samldb_ctx);
582 ldb = ldb_module_get_ctx(ac->module);
585 ret = LDB_ERR_OPERATIONS_ERROR;
588 if (ares->error != LDB_SUCCESS) {
589 if (ares->error == LDB_ERR_NO_SUCH_OBJECT) {
590 if (ldb_request_get_control(ac->req,
591 LDB_CONTROL_RELAX_OID) != NULL) {
592 /* Don't be pricky when the DN doesn't exist */
593 /* if we have the RELAX control specified */
594 ac->dn = req->op.search.base;
595 return samldb_next_step(ac);
597 ldb_set_errstring(ldb,
598 "samldb_find_defaultObjectCategory: "
599 "Invalid DN for 'defaultObjectCategory'!");
600 ares->error = LDB_ERR_CONSTRAINT_VIOLATION;
604 return ldb_module_done(ac->req, ares->controls,
605 ares->response, ares->error);
608 switch (ares->type) {
609 case LDB_REPLY_ENTRY:
610 ac->dn = talloc_steal(ac, ares->message->dn);
615 case LDB_REPLY_REFERRAL:
624 if (ac->dn != NULL) {
625 /* when found go on */
626 ret = samldb_next_step(ac);
628 ret = LDB_ERR_OPERATIONS_ERROR;
634 if (ret != LDB_SUCCESS) {
635 return ldb_module_done(ac->req, NULL, NULL, ret);
641 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
643 struct ldb_context *ldb;
644 struct ldb_request *req;
645 static const char *no_attrs[] = { NULL };
647 const struct ldb_val *val;
648 struct ldb_dn *def_obj_cat_dn;
650 ldb = ldb_module_get_ctx(ac->module);
654 val = ldb_msg_find_ldb_val(ac->msg, "defaultObjectCategory");
656 /* "defaultObjectCategory" has been set by the caller. Do some
657 * checks for consistency.
658 * NOTE: The real constraint check (that 'defaultObjectCategory'
659 * is the DN of the new objectclass or any parent of it) is
661 * For now we say that 'defaultObjectCategory' is valid if it
662 * exists and it is of objectclass "classSchema". */
663 def_obj_cat_dn = ldb_dn_from_ldb_val(ac, ldb, val);
664 if (def_obj_cat_dn == NULL) {
665 ldb_set_errstring(ldb,
666 "samldb_find_defaultObjectCategory: Invalid DN "
667 "for 'defaultObjectCategory'!");
668 return LDB_ERR_CONSTRAINT_VIOLATION;
671 /* "defaultObjectCategory" has not been set by the caller. Use
672 * the entry DN for it. */
673 def_obj_cat_dn = ac->msg->dn;
676 ret = ldb_build_search_req(&req, ldb, ac,
677 def_obj_cat_dn, LDB_SCOPE_BASE,
678 "objectClass=classSchema", no_attrs,
680 ac, samldb_find_for_defaultObjectCategory_callback,
682 if (ret != LDB_SUCCESS) {
686 ret = dsdb_request_add_controls(req,
687 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT);
688 if (ret != LDB_SUCCESS) {
692 return ldb_next_request(ac->module, req);
696 * msDS-IntId attributeSchema attribute handling
697 * during LDB_ADD request processing
699 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
704 uint32_t system_flags;
705 struct ldb_context *ldb;
706 struct ldb_result *ldb_res;
707 struct ldb_dn *schema_dn;
709 ldb = ldb_module_get_ctx(ac->module);
710 schema_dn = ldb_get_schema_basedn(ldb);
712 /* replicated update should always go through */
713 if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
717 /* msDS-IntId is handled by system and should never be
718 * passed by clients */
719 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
720 return LDB_ERR_UNWILLING_TO_PERFORM;
723 /* do not generate msDS-IntId if Relax control is passed */
724 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
728 /* check Functional Level */
729 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
733 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
734 system_flags = ldb_msg_find_attr_as_uint(ac->msg, "systemFlags", 0);
735 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
739 /* Generate new value for msDs-IntId
740 * Value should be in 0x80000000..0xBFFFFFFF range */
741 msds_intid = generate_random() % 0X3FFFFFFF;
742 msds_intid += 0x80000000;
744 /* probe id values until unique one is found */
747 if (msds_intid > 0xBFFFFFFF) {
748 msds_intid = 0x80000001;
751 ret = dsdb_module_search(ac->module, ac,
753 schema_dn, LDB_SCOPE_ONELEVEL, NULL, 0,
754 "(msDS-IntId=%d)", msds_intid);
755 if (ret != LDB_SUCCESS) {
756 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
757 __location__": Searching for msDS-IntId=%d failed - %s\n",
760 return LDB_ERR_OPERATIONS_ERROR;
762 id_exists = (ldb_res->count > 0);
764 talloc_free(ldb_res);
767 return ldb_msg_add_fmt(ac->msg, "msDS-IntId", "%d", msds_intid);
772 * samldb_add_entry (async)
775 static int samldb_add_entry_callback(struct ldb_request *req,
776 struct ldb_reply *ares)
778 struct ldb_context *ldb;
779 struct samldb_ctx *ac;
782 ac = talloc_get_type(req->context, struct samldb_ctx);
783 ldb = ldb_module_get_ctx(ac->module);
786 return ldb_module_done(ac->req, NULL, NULL,
787 LDB_ERR_OPERATIONS_ERROR);
790 if (ares->type == LDB_REPLY_REFERRAL) {
791 return ldb_module_send_referral(ac->req, ares->referral);
794 if (ares->error != LDB_SUCCESS) {
795 return ldb_module_done(ac->req, ares->controls,
796 ares->response, ares->error);
798 if (ares->type != LDB_REPLY_DONE) {
799 ldb_set_errstring(ldb,
800 "Invalid reply type!\n");
801 return ldb_module_done(ac->req, NULL, NULL,
802 LDB_ERR_OPERATIONS_ERROR);
805 /* The caller may wish to get controls back from the add */
806 ac->ares = talloc_steal(ac, ares);
808 ret = samldb_next_step(ac);
809 if (ret != LDB_SUCCESS) {
810 return ldb_module_done(ac->req, NULL, NULL, ret);
815 static int samldb_add_entry(struct samldb_ctx *ac)
817 struct ldb_context *ldb;
818 struct ldb_request *req;
821 ldb = ldb_module_get_ctx(ac->module);
823 ret = ldb_build_add_req(&req, ldb, ac,
826 ac, samldb_add_entry_callback,
828 if (ret != LDB_SUCCESS) {
832 return ldb_next_request(ac->module, req);
836 * return true if msg carries an attributeSchema that is intended to be RODC
837 * filtered but is also a system-critical attribute.
839 static bool check_rodc_critical_attribute(struct ldb_message *msg)
841 uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
843 schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
844 searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
845 rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE | SEARCH_FLAG_CONFIDENTIAL);
847 if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
848 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
856 static int samldb_fill_object(struct samldb_ctx *ac, const char *type)
858 struct ldb_context *ldb;
859 struct loadparm_context *lp_ctx;
860 enum sid_generator sid_generator;
863 ldb = ldb_module_get_ctx(ac->module);
865 /* Add informations for the different account types */
867 if (strcmp(ac->type, "user") == 0) {
868 ret = samdb_find_or_add_attribute(ldb, ac->msg,
869 "userAccountControl", "546");
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,
879 if (ret != LDB_SUCCESS) return ret;
880 ret = samdb_find_or_add_attribute(ldb, ac->msg,
881 "badPasswordTime", "0");
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 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
893 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
894 "primaryGroupID", DOMAIN_RID_USERS);
895 if (ret != LDB_SUCCESS) return ret;
897 ret = samdb_find_or_add_attribute(ldb, ac->msg,
898 "accountExpires", "9223372036854775807");
899 if (ret != LDB_SUCCESS) return ret;
900 ret = samdb_find_or_add_attribute(ldb, ac->msg,
902 if (ret != LDB_SUCCESS) return ret;
903 } else if (strcmp(ac->type, "group") == 0) {
904 ret = samdb_find_or_add_attribute(ldb, ac->msg,
905 "groupType", "-2147483646");
906 if (ret != LDB_SUCCESS) return ret;
907 } else if (strcmp(ac->type, "classSchema") == 0) {
908 const struct ldb_val *rdn_value;
910 ret = samdb_find_or_add_attribute(ldb, ac->msg,
912 if (ret != LDB_SUCCESS) return ret;
914 /* do not allow to mark an attributeSchema as RODC filtered if it
915 * is system-critical */
916 if (check_rodc_critical_attribute(ac->msg)) {
917 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
918 ldb_dn_get_linearized(ac->msg->dn));
919 return LDB_ERR_UNWILLING_TO_PERFORM;
923 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
924 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
925 /* the RDN has prefix "CN" */
926 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
927 samdb_cn_to_lDAPDisplayName(ac,
928 (const char *) rdn_value->data));
929 if (ret != LDB_SUCCESS) {
935 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
938 guid = GUID_random();
939 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
940 if (ret != LDB_SUCCESS) {
946 ret = samldb_add_step(ac, samldb_add_entry);
947 if (ret != LDB_SUCCESS) return ret;
949 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
950 if (ret != LDB_SUCCESS) return ret;
952 ret = samldb_add_step(ac, samldb_set_defaultObjectCategory);
953 if (ret != LDB_SUCCESS) return ret;
955 return samldb_first_step(ac);
956 } else if (strcmp(ac->type, "attributeSchema") == 0) {
957 const struct ldb_val *rdn_value;
958 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
959 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
960 /* the RDN has prefix "CN" */
961 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
962 samdb_cn_to_lDAPDisplayName(ac,
963 (const char *) rdn_value->data));
964 if (ret != LDB_SUCCESS) {
970 /* do not allow to mark an attributeSchema as RODC filtered if it
971 * is system-critical */
972 if (check_rodc_critical_attribute(ac->msg)) {
973 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical attribute with RODC filtering",
974 ldb_dn_get_linearized(ac->msg->dn));
975 return LDB_ERR_UNWILLING_TO_PERFORM;
978 ret = samdb_find_or_add_attribute(ldb, ac->msg,
979 "isSingleValued", "FALSE");
980 if (ret != LDB_SUCCESS) return ret;
982 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
985 guid = GUID_random();
986 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
987 if (ret != LDB_SUCCESS) {
993 /* handle msDS-IntID attribute */
994 ret = samldb_add_handle_msDS_IntId(ac);
995 if (ret != LDB_SUCCESS) return ret;
997 ret = samldb_add_step(ac, samldb_add_entry);
998 if (ret != LDB_SUCCESS) return ret;
1000 return samldb_first_step(ac);
1002 ldb_asprintf_errstring(ldb,
1003 "Invalid entry type!");
1004 return LDB_ERR_OPERATIONS_ERROR;
1007 /* check if we have a valid samAccountName */
1008 ret = samldb_add_step(ac, samldb_check_samAccountName);
1009 if (ret != LDB_SUCCESS) return ret;
1011 /* check account_type/group_type */
1012 ret = samldb_add_step(ac, samldb_check_samAccountType);
1013 if (ret != LDB_SUCCESS) return ret;
1015 /* check if we have a valid primary group ID */
1016 if (strcmp(ac->type, "user") == 0) {
1017 ret = samldb_add_step(ac, samldb_check_primaryGroupID_1);
1018 if (ret != LDB_SUCCESS) return ret;
1019 ret = samldb_add_step(ac, samldb_dn_from_sid);
1020 if (ret != LDB_SUCCESS) return ret;
1021 ret = samldb_add_step(ac, samldb_check_primaryGroupID_2);
1022 if (ret != LDB_SUCCESS) return ret;
1025 lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
1026 struct loadparm_context);
1028 /* don't allow objectSID to be specified without the RELAX control */
1029 ac->sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
1030 if (ac->sid && !ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) &&
1031 !dsdb_module_am_system(ac->module)) {
1032 ldb_asprintf_errstring(ldb, "No SID may be specified in user/group creation for %s",
1033 ldb_dn_get_linearized(ac->msg->dn));
1034 return LDB_ERR_UNWILLING_TO_PERFORM;
1038 sid_generator = lp_sid_generator(lp_ctx);
1039 if (sid_generator == SID_GENERATOR_INTERNAL) {
1040 ret = samldb_add_step(ac, samldb_allocate_sid);
1041 if (ret != LDB_SUCCESS) return ret;
1045 /* finally proceed with adding the entry */
1046 ret = samldb_add_step(ac, samldb_add_entry);
1047 if (ret != LDB_SUCCESS) return ret;
1049 return samldb_first_step(ac);
1052 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
1054 struct ldb_context *ldb;
1057 ldb = ldb_module_get_ctx(ac->module);
1059 ac->sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
1060 if (ac->sid == NULL) {
1061 ac->sid = dom_sid_parse_talloc(ac->msg,
1062 (const char *)ldb_dn_get_rdn_val(ac->msg->dn)->data);
1064 ldb_set_errstring(ldb,
1065 "No valid SID found in "
1066 "ForeignSecurityPrincipal CN!");
1068 return LDB_ERR_CONSTRAINT_VIOLATION;
1070 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", ac->sid)) {
1072 return LDB_ERR_OPERATIONS_ERROR;
1076 /* finally proceed with adding the entry */
1077 ret = samldb_add_step(ac, samldb_add_entry);
1078 if (ret != LDB_SUCCESS) return ret;
1080 return samldb_first_step(ac);
1083 static int samldb_check_rdn(struct ldb_module *module, struct ldb_dn *dn)
1085 struct ldb_context *ldb;
1086 const char *rdn_name;
1088 ldb = ldb_module_get_ctx(module);
1089 rdn_name = ldb_dn_get_rdn_name(dn);
1091 if (strcasecmp(rdn_name, "cn") != 0) {
1092 ldb_asprintf_errstring(ldb,
1093 "Bad RDN (%s=) for samldb object, "
1094 "should be CN=!", rdn_name);
1095 return LDB_ERR_CONSTRAINT_VIOLATION;
1101 static int samldb_schema_info_update(struct samldb_ctx *ac)
1104 struct ldb_context *ldb;
1105 struct dsdb_schema *schema;
1107 /* replicated update should always go through */
1108 if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1112 /* do not update schemaInfo during provisioning */
1113 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
1117 ldb = ldb_module_get_ctx(ac->module);
1118 schema = dsdb_get_schema(ldb, NULL);
1120 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
1121 "samldb_schema_info_update: no dsdb_schema loaded");
1122 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
1123 return LDB_ERR_OPERATIONS_ERROR;
1126 werr = dsdb_module_schema_info_update(ac->module, schema, 0);
1127 if (!W_ERROR_IS_OK(werr)) {
1128 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
1129 "samldb_schema_info_update: "
1130 "dsdb_module_schema_info_update failed with %s",
1132 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
1133 return LDB_ERR_OPERATIONS_ERROR;
1140 * samldb_user_dn_to_prim_group_rid (async)
1143 static int samldb_user_dn_to_prim_group_rid(struct samldb_ctx *ac);
1145 static int samldb_user_dn_to_prim_group_rid_callback(struct ldb_request *req,
1146 struct ldb_reply *ares)
1148 struct ldb_context *ldb;
1149 struct samldb_ctx *ac;
1152 ac = talloc_get_type(req->context, struct samldb_ctx);
1153 ldb = ldb_module_get_ctx(ac->module);
1156 ret = LDB_ERR_OPERATIONS_ERROR;
1159 if (ares->error != LDB_SUCCESS) {
1160 return ldb_module_done(ac->req, ares->controls,
1161 ares->response, ares->error);
1164 switch (ares->type) {
1165 case LDB_REPLY_ENTRY:
1167 if (ac->prim_group_rid != 0) {
1169 ldb_set_errstring(ldb,
1170 "Invalid number of results while searching "
1171 "for domain objects!");
1172 ret = LDB_ERR_OPERATIONS_ERROR;
1175 ac->prim_group_rid = samdb_result_uint(ares->message,
1176 "primaryGroupID", ~0);
1182 case LDB_REPLY_REFERRAL:
1188 case LDB_REPLY_DONE:
1190 if (ac->prim_group_rid == 0) {
1191 ldb_asprintf_errstring(ldb,
1192 "Unable to get the primary group RID!");
1193 ret = LDB_ERR_OPERATIONS_ERROR;
1198 ret = samldb_next_step(ac);
1203 if (ret != LDB_SUCCESS) {
1204 return ldb_module_done(ac->req, NULL, NULL, ret);
1210 /* Locates the "primaryGroupID" attribute from a certain user specified as
1211 * "user_dn". Saves the result in "prim_group_rid". */
1212 static int samldb_user_dn_to_prim_group_rid(struct samldb_ctx *ac)
1214 struct ldb_context *ldb;
1215 static const char * const attrs[] = { "primaryGroupID", NULL };
1216 struct ldb_request *req;
1219 ldb = ldb_module_get_ctx(ac->module);
1221 if (ac->user_dn == NULL)
1222 return LDB_ERR_OPERATIONS_ERROR;
1224 ret = ldb_build_search_req(&req, ldb, ac,
1229 ac, samldb_user_dn_to_prim_group_rid_callback,
1231 if (ret != LDB_SUCCESS)
1234 return ldb_next_request(ac->module, req);
1238 * samldb_group_add_member (async)
1239 * samldb_group_del_member (async)
1242 static int samldb_group_add_del_member_callback(struct ldb_request *req,
1243 struct ldb_reply *ares)
1245 struct ldb_context *ldb;
1246 struct samldb_ctx *ac;
1249 ac = talloc_get_type(req->context, struct samldb_ctx);
1250 ldb = ldb_module_get_ctx(ac->module);
1253 ret = LDB_ERR_OPERATIONS_ERROR;
1257 if (ares->type == LDB_REPLY_REFERRAL) {
1258 return ldb_module_send_referral(ac->req, ares->referral);
1261 if (ares->error != LDB_SUCCESS) {
1262 if (ares->error == LDB_ERR_NO_SUCH_ATTRIBUTE) {
1263 /* On error "NO_SUCH_ATTRIBUTE" (delete of an invalid
1264 * "member" attribute) return "UNWILLING_TO_PERFORM" */
1265 ares->error = LDB_ERR_UNWILLING_TO_PERFORM;
1267 return ldb_module_done(ac->req, ares->controls,
1268 ares->response, ares->error);
1270 if (ares->type != LDB_REPLY_DONE) {
1271 ldb_set_errstring(ldb,
1272 "Invalid reply type!");
1273 ret = LDB_ERR_OPERATIONS_ERROR;
1277 ret = samldb_next_step(ac);
1280 if (ret != LDB_SUCCESS) {
1281 return ldb_module_done(ac->req, NULL, NULL, ret);
1287 /* Adds a member with DN "member_dn" to a group with DN "group_dn" */
1288 static int samldb_group_add_member(struct samldb_ctx *ac)
1290 struct ldb_context *ldb;
1291 struct ldb_request *req;
1292 struct ldb_message *msg;
1295 ldb = ldb_module_get_ctx(ac->module);
1297 if ((ac->group_dn == NULL) || (ac->member_dn == NULL))
1298 return LDB_ERR_OPERATIONS_ERROR;
1300 msg = ldb_msg_new(ac);
1301 msg->dn = ac->group_dn;
1302 samdb_msg_add_addval(ldb, ac, msg, "member",
1303 ldb_dn_get_linearized(ac->member_dn));
1305 ret = ldb_build_mod_req(&req, ldb, ac,
1307 ac, samldb_group_add_del_member_callback,
1309 if (ret != LDB_SUCCESS)
1312 return ldb_next_request(ac->module, req);
1315 /* Removes a member with DN "member_dn" from a group with DN "group_dn" */
1316 static int samldb_group_del_member(struct samldb_ctx *ac)
1318 struct ldb_context *ldb;
1319 struct ldb_request *req;
1320 struct ldb_message *msg;
1323 ldb = ldb_module_get_ctx(ac->module);
1325 if ((ac->group_dn == NULL) || (ac->member_dn == NULL))
1326 return LDB_ERR_OPERATIONS_ERROR;
1328 msg = ldb_msg_new(ac);
1329 msg->dn = ac->group_dn;
1330 samdb_msg_add_delval(ldb, ac, msg, "member",
1331 ldb_dn_get_linearized(ac->member_dn));
1333 ret = ldb_build_mod_req(&req, ldb, ac,
1335 ac, samldb_group_add_del_member_callback,
1337 if (ret != LDB_SUCCESS)
1340 return ldb_next_request(ac->module, req);
1344 static int samldb_prim_group_change_1(struct samldb_ctx *ac)
1346 struct ldb_context *ldb;
1349 ldb = ldb_module_get_ctx(ac->module);
1351 ac->user_dn = ac->msg->dn;
1353 rid = samdb_result_uint(ac->msg, "primaryGroupID", ~0);
1354 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1355 if (ac->sid == NULL)
1356 return LDB_ERR_OPERATIONS_ERROR;
1359 ac->prim_group_rid = 0;
1361 return samldb_next_step(ac);
1364 static int samldb_prim_group_change_2(struct samldb_ctx *ac)
1366 struct ldb_context *ldb;
1368 ldb = ldb_module_get_ctx(ac->module);
1370 if (ac->res_dn != NULL)
1371 ac->new_prim_group_dn = ac->res_dn;
1373 return LDB_ERR_UNWILLING_TO_PERFORM;
1375 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1376 ac->prim_group_rid);
1377 if (ac->sid == NULL)
1378 return LDB_ERR_OPERATIONS_ERROR;
1381 return samldb_next_step(ac);
1384 static int samldb_prim_group_change_4(struct samldb_ctx *ac);
1385 static int samldb_prim_group_change_5(struct samldb_ctx *ac);
1386 static int samldb_prim_group_change_6(struct samldb_ctx *ac);
1388 static int samldb_prim_group_change_3(struct samldb_ctx *ac)
1392 if (ac->res_dn != NULL)
1393 ac->old_prim_group_dn = ac->res_dn;
1395 return LDB_ERR_UNWILLING_TO_PERFORM;
1397 /* Only update when the primary group changed */
1398 if (ldb_dn_compare(ac->old_prim_group_dn, ac->new_prim_group_dn) != 0) {
1399 ac->member_dn = ac->user_dn;
1400 /* Remove the "member" attribute of the actual (new) primary
1403 ret = samldb_add_step(ac, samldb_prim_group_change_4);
1404 if (ret != LDB_SUCCESS) return ret;
1406 ret = samldb_add_step(ac, samldb_group_del_member);
1407 if (ret != LDB_SUCCESS) return ret;
1409 /* Add a "member" attribute for the previous primary group */
1411 ret = samldb_add_step(ac, samldb_prim_group_change_5);
1412 if (ret != LDB_SUCCESS) return ret;
1414 ret = samldb_add_step(ac, samldb_group_add_member);
1415 if (ret != LDB_SUCCESS) return ret;
1418 ret = samldb_add_step(ac, samldb_prim_group_change_6);
1419 if (ret != LDB_SUCCESS) return ret;
1421 return samldb_next_step(ac);
1424 static int samldb_prim_group_change_4(struct samldb_ctx *ac)
1426 ac->group_dn = ac->new_prim_group_dn;
1428 return samldb_next_step(ac);
1431 static int samldb_prim_group_change_5(struct samldb_ctx *ac)
1433 ac->group_dn = ac->old_prim_group_dn;
1435 return samldb_next_step(ac);
1438 static int samldb_prim_group_change_6(struct samldb_ctx *ac)
1440 return ldb_next_request(ac->module, ac->req);
1443 static int samldb_prim_group_change(struct samldb_ctx *ac)
1447 /* Finds out the DN of the new primary group */
1449 ret = samldb_add_step(ac, samldb_prim_group_change_1);
1450 if (ret != LDB_SUCCESS) return ret;
1452 ret = samldb_add_step(ac, samldb_dn_from_sid);
1453 if (ret != LDB_SUCCESS) return ret;
1455 ret = samldb_add_step(ac, samldb_user_dn_to_prim_group_rid);
1456 if (ret != LDB_SUCCESS) return ret;
1458 /* Finds out the DN of the old primary group */
1460 ret = samldb_add_step(ac, samldb_prim_group_change_2);
1461 if (ret != LDB_SUCCESS) return ret;
1463 ret = samldb_add_step(ac, samldb_dn_from_sid);
1464 if (ret != LDB_SUCCESS) return ret;
1466 ret = samldb_add_step(ac, samldb_prim_group_change_3);
1467 if (ret != LDB_SUCCESS) return ret;
1469 return samldb_first_step(ac);
1473 static int samldb_member_check_1(struct samldb_ctx *ac)
1475 struct ldb_context *ldb;
1476 struct ldb_message_element *el;
1478 ldb = ldb_module_get_ctx(ac->module);
1480 el = ldb_msg_find_element(ac->msg, "member");
1482 ac->user_dn = ldb_dn_from_ldb_val(ac, ldb, &el->values[ac->cnt]);
1483 if (!ldb_dn_validate(ac->user_dn))
1484 return LDB_ERR_OPERATIONS_ERROR;
1485 ac->prim_group_rid = 0;
1487 return samldb_next_step(ac);
1490 static int samldb_member_check_2(struct samldb_ctx *ac)
1492 struct ldb_context *ldb;
1494 ldb = ldb_module_get_ctx(ac->module);
1496 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1497 ac->prim_group_rid);
1498 if (ac->sid == NULL)
1499 return LDB_ERR_OPERATIONS_ERROR;
1502 return samldb_next_step(ac);
1505 static int samldb_member_check_3(struct samldb_ctx *ac)
1507 if (ldb_dn_compare(ac->res_dn, ac->msg->dn) == 0)
1508 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1512 return samldb_next_step(ac);
1515 static int samldb_member_check_4(struct samldb_ctx *ac)
1517 return ldb_next_request(ac->module, ac->req);
1520 static int samldb_member_check(struct samldb_ctx *ac)
1522 struct ldb_message_element *el;
1525 el = ldb_msg_find_element(ac->msg, "member");
1527 for (i = 0; i < el->num_values; i++) {
1528 /* Denies to add "member"s to groups which are primary ones
1530 ret = samldb_add_step(ac, samldb_member_check_1);
1531 if (ret != LDB_SUCCESS) return ret;
1533 ret = samldb_add_step(ac, samldb_user_dn_to_prim_group_rid);
1534 if (ret != LDB_SUCCESS) return ret;
1536 ret = samldb_add_step(ac, samldb_member_check_2);
1537 if (ret != LDB_SUCCESS) return ret;
1539 ret = samldb_add_step(ac, samldb_dn_from_sid);
1540 if (ret != LDB_SUCCESS) return ret;
1542 ret = samldb_add_step(ac, samldb_member_check_3);
1543 if (ret != LDB_SUCCESS) return ret;
1546 ret = samldb_add_step(ac, samldb_member_check_4);
1547 if (ret != LDB_SUCCESS) return ret;
1549 return samldb_first_step(ac);
1553 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
1555 struct ldb_context *ldb;
1556 struct dom_sid *sid;
1561 ldb = ldb_module_get_ctx(ac->module);
1563 /* Finds out the SID/RID of the SAM object */
1564 sid = samdb_search_dom_sid(ldb, ac, ac->req->op.del.dn, "objectSID",
1567 /* No SID - it might not be a SAM object - therefore ok */
1568 return ldb_next_request(ac->module, ac->req);
1570 status = dom_sid_split_rid(ac, sid, NULL, &rid);
1571 if (!NT_STATUS_IS_OK(status)) {
1572 return LDB_ERR_OPERATIONS_ERROR;
1575 /* Special object (security principal?) */
1576 return ldb_next_request(ac->module, ac->req);
1579 /* Deny delete requests from groups which are primary ones */
1580 count = samdb_search_count(ldb, NULL,
1581 "(&(primaryGroupID=%u)(objectClass=user))",
1584 return LDB_ERR_OPERATIONS_ERROR;
1587 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1590 return ldb_next_request(ac->module, ac->req);
1595 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
1597 struct ldb_context *ldb;
1598 struct samldb_ctx *ac;
1601 ldb = ldb_module_get_ctx(module);
1602 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
1604 /* do not manipulate our control entries */
1605 if (ldb_dn_is_special(req->op.add.message->dn)) {
1606 return ldb_next_request(module, req);
1609 ac = samldb_ctx_init(module, req);
1611 return LDB_ERR_OPERATIONS_ERROR;
1614 /* build the new msg */
1615 ac->msg = ldb_msg_copy(ac, ac->req->op.add.message);
1618 ldb_debug(ldb, LDB_DEBUG_FATAL,
1619 "samldb_add: ldb_msg_copy failed!\n");
1620 return LDB_ERR_OPERATIONS_ERROR;
1623 if (samdb_find_attribute(ldb, ac->msg,
1624 "objectclass", "computer") != NULL) {
1626 /* make sure the computer object also has the 'user'
1627 * objectclass so it will be handled by the next call */
1628 ret = samdb_find_or_add_value(ldb, ac->msg,
1629 "objectclass", "user");
1630 if (ret != LDB_SUCCESS) {
1636 if (samdb_find_attribute(ldb, ac->msg,
1637 "objectclass", "user") != NULL) {
1639 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1640 if (ret != LDB_SUCCESS) {
1645 return samldb_fill_object(ac, "user");
1648 if (samdb_find_attribute(ldb, ac->msg,
1649 "objectclass", "group") != NULL) {
1651 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1652 if (ret != LDB_SUCCESS) {
1657 return samldb_fill_object(ac, "group");
1660 /* perhaps a foreignSecurityPrincipal? */
1661 if (samdb_find_attribute(ldb, ac->msg,
1663 "foreignSecurityPrincipal") != NULL) {
1665 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1666 if (ret != LDB_SUCCESS) {
1671 return samldb_fill_foreignSecurityPrincipal_object(ac);
1674 if (samdb_find_attribute(ldb, ac->msg,
1675 "objectclass", "classSchema") != NULL) {
1677 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1678 if (ret != LDB_SUCCESS) {
1683 ret = samldb_schema_info_update(ac);
1684 if (ret != LDB_SUCCESS) {
1689 return samldb_fill_object(ac, "classSchema");
1692 if (samdb_find_attribute(ldb, ac->msg,
1693 "objectclass", "attributeSchema") != NULL) {
1695 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1696 if (ret != LDB_SUCCESS) {
1701 ret = samldb_schema_info_update(ac);
1702 if (ret != LDB_SUCCESS) {
1707 return samldb_fill_object(ac, "attributeSchema");
1712 /* nothing matched, go on */
1713 return ldb_next_request(module, req);
1717 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
1719 struct ldb_context *ldb;
1720 struct ldb_message *msg;
1721 struct ldb_message_element *el, *el2;
1723 uint32_t account_type;
1725 if (ldb_dn_is_special(req->op.mod.message->dn)) {
1726 /* do not manipulate our control entries */
1727 return ldb_next_request(module, req);
1730 ldb = ldb_module_get_ctx(module);
1732 if (ldb_msg_find_element(req->op.mod.message, "sAMAccountType") != NULL) {
1733 ldb_asprintf_errstring(ldb,
1734 "sAMAccountType must not be specified!");
1735 return LDB_ERR_UNWILLING_TO_PERFORM;
1738 /* msDS-IntId is not allowed to be modified
1739 * except when modification comes from replication */
1740 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
1741 if (!ldb_request_get_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1742 return LDB_ERR_CONSTRAINT_VIOLATION;
1746 /* TODO: do not modify original request, create a new one */
1748 el = ldb_msg_find_element(req->op.mod.message, "groupType");
1749 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1750 uint32_t group_type;
1752 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1753 req->op.mod.message);
1755 group_type = strtoul((const char *)el->values[0].data, NULL, 0);
1756 account_type = ds_gtype2atype(group_type);
1757 ret = samdb_msg_add_uint(ldb, msg, msg,
1760 if (ret != LDB_SUCCESS) {
1763 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1764 el2->flags = LDB_FLAG_MOD_REPLACE;
1767 el = ldb_msg_find_element(req->op.mod.message, "primaryGroupID");
1768 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1769 struct samldb_ctx *ac;
1771 ac = samldb_ctx_init(module, req);
1773 return LDB_ERR_OPERATIONS_ERROR;
1775 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1776 req->op.mod.message);
1778 return samldb_prim_group_change(ac);
1781 el = ldb_msg_find_element(req->op.mod.message, "userAccountControl");
1782 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1783 uint32_t user_account_control;
1785 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1786 req->op.mod.message);
1788 user_account_control = strtoul((const char *)el->values[0].data,
1790 account_type = ds_uf2atype(user_account_control);
1791 ret = samdb_msg_add_uint(ldb, msg, msg,
1794 if (ret != LDB_SUCCESS) {
1797 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1798 el2->flags = LDB_FLAG_MOD_REPLACE;
1800 if (user_account_control & UF_SERVER_TRUST_ACCOUNT) {
1801 ret = samdb_msg_add_string(ldb, msg, msg,
1802 "isCriticalSystemObject", "TRUE");
1803 if (ret != LDB_SUCCESS) {
1806 el2 = ldb_msg_find_element(msg, "isCriticalSystemObject");
1807 el2->flags = LDB_FLAG_MOD_REPLACE;
1809 /* DCs have primaryGroupID of DOMAIN_RID_DCS */
1810 if (!ldb_msg_find_element(msg, "primaryGroupID")) {
1811 ret = samdb_msg_add_uint(ldb, msg, msg,
1812 "primaryGroupID", DOMAIN_RID_DCS);
1813 if (ret != LDB_SUCCESS) {
1816 el2 = ldb_msg_find_element(msg, "primaryGroupID");
1817 el2->flags = LDB_FLAG_MOD_REPLACE;
1822 el = ldb_msg_find_element(req->op.mod.message, "member");
1823 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1824 struct samldb_ctx *ac;
1826 ac = samldb_ctx_init(module, req);
1828 return LDB_ERR_OPERATIONS_ERROR;
1830 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1831 req->op.mod.message);
1833 return samldb_member_check(ac);
1836 /* nothing matched, go on */
1837 return ldb_next_request(module, req);
1841 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
1843 struct samldb_ctx *ac;
1845 if (ldb_dn_is_special(req->op.del.dn)) {
1846 /* do not manipulate our control entries */
1847 return ldb_next_request(module, req);
1850 ac = samldb_ctx_init(module, req);
1852 return LDB_ERR_OPERATIONS_ERROR;
1854 return samldb_prim_group_users_check(ac);
1857 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
1859 struct ldb_context *ldb = ldb_module_get_ctx(module);
1860 struct dsdb_fsmo_extended_op *exop;
1863 exop = talloc_get_type(req->op.extended.data, struct dsdb_fsmo_extended_op);
1865 ldb_debug(ldb, LDB_DEBUG_FATAL, "samldb_extended_allocate_rid_pool: invalid extended data\n");
1866 return LDB_ERR_PROTOCOL_ERROR;
1869 ret = ridalloc_allocate_rid_pool_fsmo(module, exop);
1870 if (ret != LDB_SUCCESS) {
1874 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
1877 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
1879 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
1880 return samldb_extended_allocate_rid_pool(module, req);
1883 return ldb_next_request(module, req);
1887 _PUBLIC_ const struct ldb_module_ops ldb_samldb_module_ops = {
1890 .modify = samldb_modify,
1891 .del = samldb_delete,
1892 .extended = samldb_extended