4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
5 Copyright (C) Simo Sorce 2004-2008
6 Copyright (C) Matthias Dieter Wallnöfer 2009
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 conjunction with "prim_group_rid" in
78 * "samldb_prim_group_rid_to_users_cnt" */
79 unsigned int users_cnt;
81 /* used in "samldb_group_add_member" and "samldb_group_del_member" */
82 struct ldb_dn *group_dn;
83 struct ldb_dn *member_dn;
85 /* used in "samldb_primary_group_change" */
86 struct ldb_dn *user_dn;
87 struct ldb_dn *old_prim_group_dn, *new_prim_group_dn;
89 /* generic counter - used in "samldb_member_check" */
92 /* all the async steps necessary to complete the operation */
93 struct samldb_step *steps;
94 struct samldb_step *curstep;
96 /* If someone set an ares to forward controls and response back to the caller */
97 struct ldb_reply *ares;
100 static struct samldb_ctx *samldb_ctx_init(struct ldb_module *module,
101 struct ldb_request *req)
103 struct ldb_context *ldb;
104 struct samldb_ctx *ac;
106 ldb = ldb_module_get_ctx(module);
108 ac = talloc_zero(req, struct samldb_ctx);
120 static int samldb_add_step(struct samldb_ctx *ac, samldb_step_fn_t fn)
122 struct samldb_step *step, *stepper;
124 step = talloc_zero(ac, struct samldb_step);
126 return LDB_ERR_OPERATIONS_ERROR;
131 if (ac->steps == NULL) {
135 if (ac->curstep == NULL)
136 return LDB_ERR_OPERATIONS_ERROR;
137 for (stepper = ac->curstep; stepper->next != NULL;
138 stepper = stepper->next);
139 stepper->next = step;
145 static int samldb_first_step(struct samldb_ctx *ac)
147 if (ac->steps == NULL) {
148 return LDB_ERR_OPERATIONS_ERROR;
151 ac->curstep = ac->steps;
152 return ac->curstep->fn(ac);
155 static int samldb_next_step(struct samldb_ctx *ac)
157 if (ac->curstep->next) {
158 ac->curstep = ac->curstep->next;
159 return ac->curstep->fn(ac);
162 /* we exit the samldb module here */
163 /* If someone set an ares to forward controls and response back to the caller, use them */
165 return ldb_module_done(ac->req, ac->ares->controls,
166 ac->ares->response, LDB_SUCCESS);
168 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
172 static int samldb_generate_samAccountName(struct ldb_message *msg)
176 /* Format: $000000-000000000000 */
178 name = talloc_asprintf(msg, "$%.6X-%.6X%.6X",
179 (unsigned int)generate_random(),
180 (unsigned int)generate_random(),
181 (unsigned int)generate_random());
183 return LDB_ERR_OPERATIONS_ERROR;
185 return ldb_msg_add_steal_string(msg, "samAccountName", name);
189 * samldb_check_samAccountName (async)
192 static int samldb_check_samAccountName_callback(struct ldb_request *req,
193 struct ldb_reply *ares)
195 struct samldb_ctx *ac;
198 ac = talloc_get_type(req->context, struct samldb_ctx);
200 if (ares->error != LDB_SUCCESS) {
201 return ldb_module_done(ac->req, ares->controls,
202 ares->response, ares->error);
205 switch (ares->type) {
206 case LDB_REPLY_ENTRY:
207 /* if we get an entry it means this samAccountName
209 return ldb_module_done(ac->req, NULL, NULL,
210 LDB_ERR_ENTRY_ALREADY_EXISTS);
212 case LDB_REPLY_REFERRAL:
213 /* this should not happen */
214 return ldb_module_done(ac->req, NULL, NULL,
215 LDB_ERR_OPERATIONS_ERROR);
218 /* not found, go on */
220 ret = samldb_next_step(ac);
224 if (ret != LDB_SUCCESS) {
225 return ldb_module_done(ac->req, NULL, NULL, ret);
231 static int samldb_check_samAccountName(struct samldb_ctx *ac)
233 struct ldb_context *ldb;
234 struct ldb_request *req;
239 ldb = ldb_module_get_ctx(ac->module);
241 if (ldb_msg_find_element(ac->msg, "samAccountName") == NULL) {
242 ret = samldb_generate_samAccountName(ac->msg);
243 if (ret != LDB_SUCCESS) {
248 name = ldb_msg_find_attr_as_string(ac->msg, "samAccountName", NULL);
250 return LDB_ERR_OPERATIONS_ERROR;
252 filter = talloc_asprintf(ac, "samAccountName=%s",
253 ldb_binary_encode_string(ac, name));
254 if (filter == NULL) {
255 return LDB_ERR_OPERATIONS_ERROR;
258 ret = ldb_build_search_req(&req, ldb, ac,
259 samdb_base_dn(ldb), LDB_SCOPE_SUBTREE,
262 ac, samldb_check_samAccountName_callback,
265 if (ret != LDB_SUCCESS) {
268 return ldb_next_request(ac->module, req);
272 static int samldb_check_samAccountType(struct samldb_ctx *ac)
274 struct ldb_context *ldb;
275 unsigned int account_type;
276 unsigned int group_type;
280 ldb = ldb_module_get_ctx(ac->module);
282 /* make sure sAMAccountType is not specified */
283 if (ldb_msg_find_element(ac->msg, "sAMAccountType") != NULL) {
284 ldb_asprintf_errstring(ldb,
285 "sAMAccountType must not be specified!");
286 return LDB_ERR_UNWILLING_TO_PERFORM;
289 if (strcmp("user", ac->type) == 0) {
290 uac = samdb_result_uint(ac->msg, "userAccountControl", 0);
292 ldb_asprintf_errstring(ldb,
293 "userAccountControl invalid!");
294 return LDB_ERR_UNWILLING_TO_PERFORM;
296 account_type = ds_uf2atype(uac);
297 ret = samdb_msg_add_uint(ldb,
301 if (ret != LDB_SUCCESS) {
306 if (strcmp("group", ac->type) == 0) {
308 group_type = samdb_result_uint(ac->msg, "groupType", 0);
309 if (group_type == 0) {
310 ldb_asprintf_errstring(ldb,
311 "groupType invalid!\n");
312 return LDB_ERR_UNWILLING_TO_PERFORM;
314 account_type = ds_gtype2atype(group_type);
315 ret = samdb_msg_add_uint(ldb,
319 if (ret != LDB_SUCCESS) {
325 return samldb_next_step(ac);
328 static bool samldb_msg_add_sid(struct ldb_message *msg,
330 const struct dom_sid *sid)
333 enum ndr_err_code ndr_err;
335 ndr_err = ndr_push_struct_blob(&v, msg, NULL, sid,
336 (ndr_push_flags_fn_t)ndr_push_dom_sid);
337 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
340 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
344 /* allocate a SID using our RID Set */
345 static int samldb_allocate_sid(struct samldb_ctx *ac)
349 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
351 ret = ridalloc_allocate_rid(ac->module, &rid);
352 if (ret != LDB_SUCCESS) {
356 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
357 if (ac->sid == NULL) {
358 ldb_module_oom(ac->module);
359 return LDB_ERR_OPERATIONS_ERROR;
362 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", ac->sid)) {
363 return LDB_ERR_OPERATIONS_ERROR;
366 return samldb_next_step(ac);
370 * samldb_dn_from_sid (async)
373 static int samldb_dn_from_sid(struct samldb_ctx *ac);
375 static int samldb_dn_from_sid_callback(struct ldb_request *req,
376 struct ldb_reply *ares)
378 struct ldb_context *ldb;
379 struct samldb_ctx *ac;
382 ac = talloc_get_type(req->context, struct samldb_ctx);
383 ldb = ldb_module_get_ctx(ac->module);
386 ret = LDB_ERR_OPERATIONS_ERROR;
389 if (ares->error != LDB_SUCCESS) {
390 return ldb_module_done(ac->req, ares->controls,
391 ares->response, ares->error);
394 switch (ares->type) {
395 case LDB_REPLY_ENTRY:
397 if (ac->res_dn != NULL) {
399 ldb_set_errstring(ldb,
400 "Invalid number of results while searching "
401 "for domain objects!");
402 ret = LDB_ERR_OPERATIONS_ERROR;
405 ac->res_dn = ldb_dn_copy(ac, ares->message->dn);
411 case LDB_REPLY_REFERRAL:
420 /* found or not found, go on */
421 ret = samldb_next_step(ac);
426 if (ret != LDB_SUCCESS) {
427 return ldb_module_done(ac->req, NULL, NULL, ret);
433 /* Finds the DN "res_dn" of an object with a given SID "sid" */
434 static int samldb_dn_from_sid(struct samldb_ctx *ac)
436 struct ldb_context *ldb;
437 static const char * const attrs[] = { NULL };
438 struct ldb_request *req;
442 ldb = ldb_module_get_ctx(ac->module);
445 return LDB_ERR_OPERATIONS_ERROR;
447 filter = talloc_asprintf(ac, "(objectSid=%s)",
448 ldap_encode_ndr_dom_sid(ac, ac->sid));
450 return LDB_ERR_OPERATIONS_ERROR;
452 ret = ldb_build_search_req(&req, ldb, ac,
453 ldb_get_default_basedn(ldb),
457 ac, samldb_dn_from_sid_callback,
459 if (ret != LDB_SUCCESS)
462 return ldb_next_request(ac->module, req);
466 static int samldb_check_primaryGroupID_1(struct samldb_ctx *ac)
468 struct ldb_context *ldb;
471 ldb = ldb_module_get_ctx(ac->module);
473 rid = samdb_result_uint(ac->msg, "primaryGroupID", ~0);
474 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
476 return LDB_ERR_OPERATIONS_ERROR;
479 return samldb_next_step(ac);
482 static int samldb_check_primaryGroupID_2(struct samldb_ctx *ac)
484 if (ac->res_dn == NULL) {
485 struct ldb_context *ldb;
486 ldb = ldb_module_get_ctx(ac->module);
487 ldb_asprintf_errstring(ldb,
488 "Failed to find group sid %s!",
489 dom_sid_string(ac->sid, ac->sid));
490 return LDB_ERR_UNWILLING_TO_PERFORM;
493 return samldb_next_step(ac);
498 * samldb_set_defaultObjectCategory_callback (async)
501 static int samldb_set_defaultObjectCategory_callback(struct ldb_request *req,
502 struct ldb_reply *ares)
504 struct ldb_context *ldb;
505 struct samldb_ctx *ac;
508 ac = talloc_get_type(req->context, struct samldb_ctx);
509 ldb = ldb_module_get_ctx(ac->module);
512 ret = LDB_ERR_OPERATIONS_ERROR;
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:
613 /* this should not happen */
615 ret = LDB_ERR_OPERATIONS_ERROR;
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);
694 * samldb_add_entry (async)
697 static int samldb_add_entry_callback(struct ldb_request *req,
698 struct ldb_reply *ares)
700 struct ldb_context *ldb;
701 struct samldb_ctx *ac;
704 ac = talloc_get_type(req->context, struct samldb_ctx);
705 ldb = ldb_module_get_ctx(ac->module);
708 return ldb_module_done(ac->req, NULL, NULL,
709 LDB_ERR_OPERATIONS_ERROR);
711 if (ares->error != LDB_SUCCESS) {
712 return ldb_module_done(ac->req, ares->controls,
713 ares->response, ares->error);
715 if (ares->type != LDB_REPLY_DONE) {
716 ldb_set_errstring(ldb,
717 "Invalid reply type!\n");
718 return ldb_module_done(ac->req, NULL, NULL,
719 LDB_ERR_OPERATIONS_ERROR);
722 /* The caller may wish to get controls back from the add */
723 ac->ares = talloc_steal(ac, ares);
725 ret = samldb_next_step(ac);
726 if (ret != LDB_SUCCESS) {
727 return ldb_module_done(ac->req, NULL, NULL, ret);
732 static int samldb_add_entry(struct samldb_ctx *ac)
734 struct ldb_context *ldb;
735 struct ldb_request *req;
738 ldb = ldb_module_get_ctx(ac->module);
740 ret = ldb_build_add_req(&req, ldb, ac,
743 ac, samldb_add_entry_callback,
745 if (ret != LDB_SUCCESS) {
749 return ldb_next_request(ac->module, req);
753 static int samldb_fill_object(struct samldb_ctx *ac, const char *type)
755 struct ldb_context *ldb;
756 struct loadparm_context *lp_ctx;
757 enum sid_generator sid_generator;
760 ldb = ldb_module_get_ctx(ac->module);
762 /* Add informations for the different account types */
764 if (strcmp(ac->type, "user") == 0) {
765 ret = samdb_find_or_add_attribute(ldb, ac->msg,
766 "userAccountControl", "546");
767 if (ret != LDB_SUCCESS) return ret;
768 ret = samdb_find_or_add_attribute(ldb, ac->msg,
770 if (ret != LDB_SUCCESS) return ret;
771 ret = samdb_find_or_add_attribute(ldb, ac->msg,
773 if (ret != LDB_SUCCESS) return ret;
774 ret = samdb_find_or_add_attribute(ldb, ac->msg,
776 if (ret != LDB_SUCCESS) return ret;
777 ret = samdb_find_or_add_attribute(ldb, ac->msg,
778 "badPasswordTime", "0");
779 if (ret != LDB_SUCCESS) return ret;
780 ret = samdb_find_or_add_attribute(ldb, ac->msg,
782 if (ret != LDB_SUCCESS) return ret;
783 ret = samdb_find_or_add_attribute(ldb, ac->msg,
785 if (ret != LDB_SUCCESS) return ret;
786 ret = samdb_find_or_add_attribute(ldb, ac->msg,
788 if (ret != LDB_SUCCESS) return ret;
789 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
790 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
791 "primaryGroupID", DOMAIN_RID_USERS);
792 if (ret != LDB_SUCCESS) return ret;
794 ret = samdb_find_or_add_attribute(ldb, ac->msg,
795 "accountExpires", "9223372036854775807");
796 if (ret != LDB_SUCCESS) return ret;
797 ret = samdb_find_or_add_attribute(ldb, ac->msg,
799 if (ret != LDB_SUCCESS) return ret;
800 } else if (strcmp(ac->type, "group") == 0) {
801 ret = samdb_find_or_add_attribute(ldb, ac->msg,
802 "groupType", "-2147483646");
803 if (ret != LDB_SUCCESS) return ret;
804 } else if (strcmp(ac->type, "classSchema") == 0) {
805 const struct ldb_val *rdn_value;
807 ret = samdb_find_or_add_attribute(ldb, ac->msg,
809 if (ret != LDB_SUCCESS) return ret;
811 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
812 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
813 /* the RDN has prefix "CN" */
814 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
815 samdb_cn_to_lDAPDisplayName(ac,
816 (const char *) rdn_value->data));
817 if (ret != LDB_SUCCESS) {
823 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
826 guid = GUID_random();
827 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
828 if (ret != LDB_SUCCESS) {
834 ret = samldb_add_step(ac, samldb_add_entry);
835 if (ret != LDB_SUCCESS) return ret;
837 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
838 if (ret != LDB_SUCCESS) return ret;
840 ret = samldb_add_step(ac, samldb_set_defaultObjectCategory);
841 if (ret != LDB_SUCCESS) return ret;
843 return samldb_first_step(ac);
844 } else if (strcmp(ac->type, "attributeSchema") == 0) {
845 const struct ldb_val *rdn_value;
846 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
847 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
848 /* the RDN has prefix "CN" */
849 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
850 samdb_cn_to_lDAPDisplayName(ac,
851 (const char *) rdn_value->data));
852 if (ret != LDB_SUCCESS) {
858 ret = samdb_find_or_add_attribute(ldb, ac->msg,
859 "isSingleValued", "FALSE");
860 if (ret != LDB_SUCCESS) return ret;
862 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
865 guid = GUID_random();
866 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
867 if (ret != LDB_SUCCESS) {
873 ret = samldb_add_step(ac, samldb_add_entry);
874 if (ret != LDB_SUCCESS) return ret;
876 return samldb_first_step(ac);
878 ldb_asprintf_errstring(ldb,
879 "Invalid entry type!");
880 return LDB_ERR_OPERATIONS_ERROR;
883 /* check if we have a valid samAccountName */
884 ret = samldb_add_step(ac, samldb_check_samAccountName);
885 if (ret != LDB_SUCCESS) return ret;
887 /* check account_type/group_type */
888 ret = samldb_add_step(ac, samldb_check_samAccountType);
889 if (ret != LDB_SUCCESS) return ret;
891 /* check if we have a valid primary group ID */
892 if (strcmp(ac->type, "user") == 0) {
893 ret = samldb_add_step(ac, samldb_check_primaryGroupID_1);
894 if (ret != LDB_SUCCESS) return ret;
895 ret = samldb_add_step(ac, samldb_dn_from_sid);
896 if (ret != LDB_SUCCESS) return ret;
897 ret = samldb_add_step(ac, samldb_check_primaryGroupID_2);
898 if (ret != LDB_SUCCESS) return ret;
901 lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
902 struct loadparm_context);
904 /* don't allow objectSID to be specified without the RELAX control */
905 ac->sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
906 if (ac->sid && !ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) &&
907 !dsdb_module_am_system(ac->module)) {
908 ldb_asprintf_errstring(ldb, "No SID may be specified in user/group creation for %s",
909 ldb_dn_get_linearized(ac->msg->dn));
910 return LDB_ERR_UNWILLING_TO_PERFORM;
914 sid_generator = lp_sid_generator(lp_ctx);
915 if (sid_generator == SID_GENERATOR_INTERNAL) {
916 ret = samldb_add_step(ac, samldb_allocate_sid);
917 if (ret != LDB_SUCCESS) return ret;
921 /* finally proceed with adding the entry */
922 ret = samldb_add_step(ac, samldb_add_entry);
923 if (ret != LDB_SUCCESS) return ret;
925 return samldb_first_step(ac);
928 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
930 struct ldb_context *ldb;
933 ldb = ldb_module_get_ctx(ac->module);
935 ac->sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
936 if (ac->sid == NULL) {
937 ac->sid = dom_sid_parse_talloc(ac->msg,
938 (const char *)ldb_dn_get_rdn_val(ac->msg->dn)->data);
940 ldb_set_errstring(ldb,
941 "No valid SID found in "
942 "ForeignSecurityPrincipal CN!");
944 return LDB_ERR_CONSTRAINT_VIOLATION;
946 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", ac->sid)) {
948 return LDB_ERR_OPERATIONS_ERROR;
952 /* finally proceed with adding the entry */
953 ret = samldb_add_step(ac, samldb_add_entry);
954 if (ret != LDB_SUCCESS) return ret;
956 return samldb_first_step(ac);
959 static int samldb_check_rdn(struct ldb_module *module, struct ldb_dn *dn)
961 struct ldb_context *ldb;
962 const char *rdn_name;
964 ldb = ldb_module_get_ctx(module);
965 rdn_name = ldb_dn_get_rdn_name(dn);
967 if (strcasecmp(rdn_name, "cn") != 0) {
968 ldb_asprintf_errstring(ldb,
969 "Bad RDN (%s=) for samldb object, "
970 "should be CN=!", rdn_name);
971 return LDB_ERR_CONSTRAINT_VIOLATION;
978 * samldb_sid_from_dn (async)
981 static int samldb_sid_from_dn(struct samldb_ctx *ac);
983 static int samldb_sid_from_dn_callback(struct ldb_request *req,
984 struct ldb_reply *ares)
986 struct ldb_context *ldb;
987 struct samldb_ctx *ac;
990 ac = talloc_get_type(req->context, struct samldb_ctx);
991 ldb = ldb_module_get_ctx(ac->module);
994 ret = LDB_ERR_OPERATIONS_ERROR;
997 if (ares->error != LDB_SUCCESS) {
998 return ldb_module_done(ac->req, ares->controls,
999 ares->response, ares->error);
1002 switch (ares->type) {
1003 case LDB_REPLY_ENTRY:
1005 if (ac->res_sid != NULL) {
1007 ldb_set_errstring(ldb,
1008 "Invalid number of results while searching "
1009 "for domain objects!");
1010 ret = LDB_ERR_OPERATIONS_ERROR;
1013 ac->res_sid = samdb_result_dom_sid(ac, ares->message,
1020 case LDB_REPLY_REFERRAL:
1026 case LDB_REPLY_DONE:
1029 /* found or not found, go on */
1030 ret = samldb_next_step(ac);
1035 if (ret != LDB_SUCCESS) {
1036 return ldb_module_done(ac->req, NULL, NULL, ret);
1042 /* Finds the SID "res_sid" of an object with a given DN "dn" */
1043 static int samldb_sid_from_dn(struct samldb_ctx *ac)
1045 struct ldb_context *ldb;
1046 static const char * const attrs[] = { "objectSid", NULL };
1047 struct ldb_request *req;
1050 ldb = ldb_module_get_ctx(ac->module);
1053 return LDB_ERR_OPERATIONS_ERROR;
1055 ret = ldb_build_search_req(&req, ldb, ac,
1060 ac, samldb_sid_from_dn_callback,
1062 if (ret != LDB_SUCCESS)
1065 return ldb_next_request(ac->module, req);
1069 * samldb_user_dn_to_prim_group_rid (async)
1072 static int samldb_user_dn_to_prim_group_rid(struct samldb_ctx *ac);
1074 static int samldb_user_dn_to_prim_group_rid_callback(struct ldb_request *req,
1075 struct ldb_reply *ares)
1077 struct ldb_context *ldb;
1078 struct samldb_ctx *ac;
1081 ac = talloc_get_type(req->context, struct samldb_ctx);
1082 ldb = ldb_module_get_ctx(ac->module);
1085 ret = LDB_ERR_OPERATIONS_ERROR;
1088 if (ares->error != LDB_SUCCESS) {
1089 return ldb_module_done(ac->req, ares->controls,
1090 ares->response, ares->error);
1093 switch (ares->type) {
1094 case LDB_REPLY_ENTRY:
1096 if (ac->prim_group_rid != 0) {
1098 ldb_set_errstring(ldb,
1099 "Invalid number of results while searching "
1100 "for domain objects!");
1101 ret = LDB_ERR_OPERATIONS_ERROR;
1104 ac->prim_group_rid = samdb_result_uint(ares->message,
1105 "primaryGroupID", ~0);
1111 case LDB_REPLY_REFERRAL:
1117 case LDB_REPLY_DONE:
1119 if (ac->prim_group_rid == 0) {
1120 ldb_asprintf_errstring(ldb,
1121 "Unable to get the primary group RID!");
1122 ret = LDB_ERR_OPERATIONS_ERROR;
1127 ret = samldb_next_step(ac);
1132 if (ret != LDB_SUCCESS) {
1133 return ldb_module_done(ac->req, NULL, NULL, ret);
1139 /* Locates the "primaryGroupID" attribute from a certain user specified as
1140 * "user_dn". Saves the result in "prim_group_rid". */
1141 static int samldb_user_dn_to_prim_group_rid(struct samldb_ctx *ac)
1143 struct ldb_context *ldb;
1144 static const char * const attrs[] = { "primaryGroupID", NULL };
1145 struct ldb_request *req;
1148 ldb = ldb_module_get_ctx(ac->module);
1150 if (ac->user_dn == NULL)
1151 return LDB_ERR_OPERATIONS_ERROR;
1153 ret = ldb_build_search_req(&req, ldb, ac,
1158 ac, samldb_user_dn_to_prim_group_rid_callback,
1160 if (ret != LDB_SUCCESS)
1163 return ldb_next_request(ac->module, req);
1167 * samldb_prim_group_rid_to_users_cnt (async)
1170 static int samldb_prim_group_rid_to_users_cnt(struct samldb_ctx *ac);
1172 static int samldb_prim_group_rid_to_users_cnt_callback(struct ldb_request *req,
1173 struct ldb_reply *ares)
1175 struct ldb_context *ldb;
1176 struct samldb_ctx *ac;
1179 ac = talloc_get_type(req->context, struct samldb_ctx);
1180 ldb = ldb_module_get_ctx(ac->module);
1183 ret = LDB_ERR_OPERATIONS_ERROR;
1186 if (ares->error != LDB_SUCCESS) {
1187 return ldb_module_done(ac->req, ares->controls,
1188 ares->response, ares->error);
1191 switch (ares->type) {
1192 case LDB_REPLY_ENTRY:
1200 case LDB_REPLY_REFERRAL:
1206 case LDB_REPLY_DONE:
1209 /* found or not found, go on */
1210 ret = samldb_next_step(ac);
1215 if (ret != LDB_SUCCESS) {
1216 return ldb_module_done(ac->req, NULL, NULL, ret);
1222 /* Finds the amount of users which have the primary group "prim_group_rid" and
1223 * save the result in "users_cnt" */
1224 static int samldb_prim_group_rid_to_users_cnt(struct samldb_ctx *ac)
1226 struct ldb_context *ldb;
1227 static const char * const attrs[] = { NULL };
1228 struct ldb_request *req;
1232 ldb = ldb_module_get_ctx(ac->module);
1234 if ((ac->prim_group_rid == 0) || (ac->users_cnt != 0))
1235 return LDB_ERR_OPERATIONS_ERROR;
1237 filter = talloc_asprintf(ac, "(&(primaryGroupID=%u)(objectclass=user))",
1238 ac->prim_group_rid);
1240 return LDB_ERR_OPERATIONS_ERROR;
1242 ret = ldb_build_search_req(&req, ldb, ac,
1243 ldb_get_default_basedn(ldb),
1248 samldb_prim_group_rid_to_users_cnt_callback,
1250 if (ret != LDB_SUCCESS)
1253 return ldb_next_request(ac->module, req);
1257 * samldb_group_add_member (async)
1258 * samldb_group_del_member (async)
1261 static int samldb_group_add_del_member_callback(struct ldb_request *req,
1262 struct ldb_reply *ares)
1264 struct ldb_context *ldb;
1265 struct samldb_ctx *ac;
1268 ac = talloc_get_type(req->context, struct samldb_ctx);
1269 ldb = ldb_module_get_ctx(ac->module);
1272 ret = LDB_ERR_OPERATIONS_ERROR;
1275 if (ares->error != LDB_SUCCESS) {
1276 if (ares->error == LDB_ERR_NO_SUCH_ATTRIBUTE) {
1277 /* On error "NO_SUCH_ATTRIBUTE" (delete of an invalid
1278 * "member" attribute) return "UNWILLING_TO_PERFORM" */
1279 ares->error = LDB_ERR_UNWILLING_TO_PERFORM;
1281 return ldb_module_done(ac->req, ares->controls,
1282 ares->response, ares->error);
1284 if (ares->type != LDB_REPLY_DONE) {
1285 ldb_set_errstring(ldb,
1286 "Invalid reply type!");
1287 ret = LDB_ERR_OPERATIONS_ERROR;
1291 ret = samldb_next_step(ac);
1294 if (ret != LDB_SUCCESS) {
1295 return ldb_module_done(ac->req, NULL, NULL, ret);
1301 /* Adds a member with DN "member_dn" to a group with DN "group_dn" */
1302 static int samldb_group_add_member(struct samldb_ctx *ac)
1304 struct ldb_context *ldb;
1305 struct ldb_request *req;
1306 struct ldb_message *msg;
1309 ldb = ldb_module_get_ctx(ac->module);
1311 if ((ac->group_dn == NULL) || (ac->member_dn == NULL))
1312 return LDB_ERR_OPERATIONS_ERROR;
1314 msg = ldb_msg_new(ac);
1315 msg->dn = ac->group_dn;
1316 samdb_msg_add_addval(ldb, ac, msg, "member",
1317 ldb_dn_get_linearized(ac->member_dn));
1319 ret = ldb_build_mod_req(&req, ldb, ac,
1321 ac, samldb_group_add_del_member_callback,
1323 if (ret != LDB_SUCCESS)
1326 return ldb_next_request(ac->module, req);
1329 /* Removes a member with DN "member_dn" from a group with DN "group_dn" */
1330 static int samldb_group_del_member(struct samldb_ctx *ac)
1332 struct ldb_context *ldb;
1333 struct ldb_request *req;
1334 struct ldb_message *msg;
1337 ldb = ldb_module_get_ctx(ac->module);
1339 if ((ac->group_dn == NULL) || (ac->member_dn == NULL))
1340 return LDB_ERR_OPERATIONS_ERROR;
1342 msg = ldb_msg_new(ac);
1343 msg->dn = ac->group_dn;
1344 samdb_msg_add_delval(ldb, ac, msg, "member",
1345 ldb_dn_get_linearized(ac->member_dn));
1347 ret = ldb_build_mod_req(&req, ldb, ac,
1349 ac, samldb_group_add_del_member_callback,
1351 if (ret != LDB_SUCCESS)
1354 return ldb_next_request(ac->module, req);
1358 static int samldb_prim_group_change_1(struct samldb_ctx *ac)
1360 struct ldb_context *ldb;
1363 ldb = ldb_module_get_ctx(ac->module);
1365 ac->user_dn = ac->msg->dn;
1367 rid = samdb_result_uint(ac->msg, "primaryGroupID", ~0);
1368 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1369 if (ac->sid == NULL)
1370 return LDB_ERR_OPERATIONS_ERROR;
1373 ac->prim_group_rid = 0;
1375 return samldb_next_step(ac);
1378 static int samldb_prim_group_change_2(struct samldb_ctx *ac)
1380 struct ldb_context *ldb;
1382 ldb = ldb_module_get_ctx(ac->module);
1384 if (ac->res_dn != NULL)
1385 ac->new_prim_group_dn = ac->res_dn;
1387 return LDB_ERR_UNWILLING_TO_PERFORM;
1389 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1390 ac->prim_group_rid);
1391 if (ac->sid == NULL)
1392 return LDB_ERR_OPERATIONS_ERROR;
1395 return samldb_next_step(ac);
1398 static int samldb_prim_group_change_4(struct samldb_ctx *ac);
1399 static int samldb_prim_group_change_5(struct samldb_ctx *ac);
1400 static int samldb_prim_group_change_6(struct samldb_ctx *ac);
1402 static int samldb_prim_group_change_3(struct samldb_ctx *ac)
1406 if (ac->res_dn != NULL)
1407 ac->old_prim_group_dn = ac->res_dn;
1409 return LDB_ERR_UNWILLING_TO_PERFORM;
1411 /* Only update when the primary group changed */
1412 if (ldb_dn_compare(ac->old_prim_group_dn, ac->new_prim_group_dn) != 0) {
1413 ac->member_dn = ac->user_dn;
1414 /* Remove the "member" attribute of the actual (new) primary
1417 ret = samldb_add_step(ac, samldb_prim_group_change_4);
1418 if (ret != LDB_SUCCESS) return ret;
1420 ret = samldb_add_step(ac, samldb_group_del_member);
1421 if (ret != LDB_SUCCESS) return ret;
1423 /* Add a "member" attribute for the previous primary group */
1425 ret = samldb_add_step(ac, samldb_prim_group_change_5);
1426 if (ret != LDB_SUCCESS) return ret;
1428 ret = samldb_add_step(ac, samldb_group_add_member);
1429 if (ret != LDB_SUCCESS) return ret;
1432 ret = samldb_add_step(ac, samldb_prim_group_change_6);
1433 if (ret != LDB_SUCCESS) return ret;
1435 return samldb_next_step(ac);
1438 static int samldb_prim_group_change_4(struct samldb_ctx *ac)
1440 ac->group_dn = ac->new_prim_group_dn;
1442 return samldb_next_step(ac);
1445 static int samldb_prim_group_change_5(struct samldb_ctx *ac)
1447 ac->group_dn = ac->old_prim_group_dn;
1449 return samldb_next_step(ac);
1452 static int samldb_prim_group_change_6(struct samldb_ctx *ac)
1454 return ldb_next_request(ac->module, ac->req);
1457 static int samldb_prim_group_change(struct samldb_ctx *ac)
1461 /* Finds out the DN of the new primary group */
1463 ret = samldb_add_step(ac, samldb_prim_group_change_1);
1464 if (ret != LDB_SUCCESS) return ret;
1466 ret = samldb_add_step(ac, samldb_dn_from_sid);
1467 if (ret != LDB_SUCCESS) return ret;
1469 ret = samldb_add_step(ac, samldb_user_dn_to_prim_group_rid);
1470 if (ret != LDB_SUCCESS) return ret;
1472 /* Finds out the DN of the old primary group */
1474 ret = samldb_add_step(ac, samldb_prim_group_change_2);
1475 if (ret != LDB_SUCCESS) return ret;
1477 ret = samldb_add_step(ac, samldb_dn_from_sid);
1478 if (ret != LDB_SUCCESS) return ret;
1480 ret = samldb_add_step(ac, samldb_prim_group_change_3);
1481 if (ret != LDB_SUCCESS) return ret;
1483 return samldb_first_step(ac);
1487 static int samldb_member_check_1(struct samldb_ctx *ac)
1489 struct ldb_context *ldb;
1490 struct ldb_message_element *el;
1492 ldb = ldb_module_get_ctx(ac->module);
1494 el = ldb_msg_find_element(ac->msg, "member");
1496 ac->user_dn = ldb_dn_from_ldb_val(ac, ldb, &el->values[ac->cnt]);
1497 if (!ldb_dn_validate(ac->user_dn))
1498 return LDB_ERR_OPERATIONS_ERROR;
1499 ac->prim_group_rid = 0;
1501 return samldb_next_step(ac);
1504 static int samldb_member_check_2(struct samldb_ctx *ac)
1506 struct ldb_context *ldb;
1508 ldb = ldb_module_get_ctx(ac->module);
1510 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1511 ac->prim_group_rid);
1512 if (ac->sid == NULL)
1513 return LDB_ERR_OPERATIONS_ERROR;
1516 return samldb_next_step(ac);
1519 static int samldb_member_check_3(struct samldb_ctx *ac)
1521 if (ldb_dn_compare(ac->res_dn, ac->msg->dn) == 0)
1522 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1526 return samldb_next_step(ac);
1529 static int samldb_member_check_4(struct samldb_ctx *ac)
1531 return ldb_next_request(ac->module, ac->req);
1534 static int samldb_member_check(struct samldb_ctx *ac)
1536 struct ldb_message_element *el;
1539 el = ldb_msg_find_element(ac->msg, "member");
1541 for (i = 0; i < el->num_values; i++) {
1542 /* Denies to add "member"s to groups which are primary ones
1544 ret = samldb_add_step(ac, samldb_member_check_1);
1545 if (ret != LDB_SUCCESS) return ret;
1547 ret = samldb_add_step(ac, samldb_user_dn_to_prim_group_rid);
1548 if (ret != LDB_SUCCESS) return ret;
1550 ret = samldb_add_step(ac, samldb_member_check_2);
1551 if (ret != LDB_SUCCESS) return ret;
1553 ret = samldb_add_step(ac, samldb_dn_from_sid);
1554 if (ret != LDB_SUCCESS) return ret;
1556 ret = samldb_add_step(ac, samldb_member_check_3);
1557 if (ret != LDB_SUCCESS) return ret;
1560 ret = samldb_add_step(ac, samldb_member_check_4);
1561 if (ret != LDB_SUCCESS) return ret;
1563 return samldb_first_step(ac);
1567 static int samldb_prim_group_users_check_1(struct samldb_ctx *ac)
1569 ac->dn = ac->req->op.del.dn;
1572 return samldb_next_step(ac);
1575 static int samldb_prim_group_users_check_2(struct samldb_ctx *ac)
1580 if (ac->res_sid == NULL) {
1581 /* No SID - therefore ok here */
1582 return ldb_next_request(ac->module, ac->req);
1584 status = dom_sid_split_rid(ac, ac->res_sid, NULL, &rid);
1585 if (!NT_STATUS_IS_OK(status))
1586 return LDB_ERR_OPERATIONS_ERROR;
1589 /* Special object (security principal?) */
1590 return ldb_next_request(ac->module, ac->req);
1593 ac->prim_group_rid = rid;
1596 return samldb_next_step(ac);
1599 static int samldb_prim_group_users_check_3(struct samldb_ctx *ac)
1601 if (ac->users_cnt > 0)
1602 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1604 return ldb_next_request(ac->module, ac->req);
1607 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
1611 /* Finds out the SID/RID of the domain object */
1613 ret = samldb_add_step(ac, samldb_prim_group_users_check_1);
1614 if (ret != LDB_SUCCESS) return ret;
1616 ret = samldb_add_step(ac, samldb_sid_from_dn);
1617 if (ret != LDB_SUCCESS) return ret;
1619 /* Deny delete requests from groups which are primary ones */
1621 ret = samldb_add_step(ac, samldb_prim_group_users_check_2);
1622 if (ret != LDB_SUCCESS) return ret;
1624 ret = samldb_add_step(ac, samldb_prim_group_rid_to_users_cnt);
1625 if (ret != LDB_SUCCESS) return ret;
1627 ret = samldb_add_step(ac, samldb_prim_group_users_check_3);
1628 if (ret != LDB_SUCCESS) return ret;
1630 return samldb_first_step(ac);
1635 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
1637 struct ldb_context *ldb;
1638 struct samldb_ctx *ac;
1641 ldb = ldb_module_get_ctx(module);
1642 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
1644 /* do not manipulate our control entries */
1645 if (ldb_dn_is_special(req->op.add.message->dn)) {
1646 return ldb_next_request(module, req);
1649 ac = samldb_ctx_init(module, req);
1651 return LDB_ERR_OPERATIONS_ERROR;
1654 /* build the new msg */
1655 ac->msg = ldb_msg_copy(ac, ac->req->op.add.message);
1658 ldb_debug(ldb, LDB_DEBUG_FATAL,
1659 "samldb_add: ldb_msg_copy failed!\n");
1660 return LDB_ERR_OPERATIONS_ERROR;
1663 if (samdb_find_attribute(ldb, ac->msg,
1664 "objectclass", "computer") != NULL) {
1666 /* make sure the computer object also has the 'user'
1667 * objectclass so it will be handled by the next call */
1668 ret = samdb_find_or_add_value(ldb, ac->msg,
1669 "objectclass", "user");
1670 if (ret != LDB_SUCCESS) {
1676 if (samdb_find_attribute(ldb, ac->msg,
1677 "objectclass", "user") != NULL) {
1679 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1680 if (ret != LDB_SUCCESS) {
1685 return samldb_fill_object(ac, "user");
1688 if (samdb_find_attribute(ldb, ac->msg,
1689 "objectclass", "group") != NULL) {
1691 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1692 if (ret != LDB_SUCCESS) {
1697 return samldb_fill_object(ac, "group");
1700 /* perhaps a foreignSecurityPrincipal? */
1701 if (samdb_find_attribute(ldb, ac->msg,
1703 "foreignSecurityPrincipal") != NULL) {
1705 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1706 if (ret != LDB_SUCCESS) {
1711 return samldb_fill_foreignSecurityPrincipal_object(ac);
1714 if (samdb_find_attribute(ldb, ac->msg,
1715 "objectclass", "classSchema") != NULL) {
1717 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1718 if (ret != LDB_SUCCESS) {
1723 return samldb_fill_object(ac, "classSchema");
1726 if (samdb_find_attribute(ldb, ac->msg,
1727 "objectclass", "attributeSchema") != NULL) {
1729 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1730 if (ret != LDB_SUCCESS) {
1735 return samldb_fill_object(ac, "attributeSchema");
1740 /* nothing matched, go on */
1741 return ldb_next_request(module, req);
1745 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
1747 struct ldb_context *ldb;
1748 struct ldb_message *msg;
1749 struct ldb_message_element *el, *el2;
1751 uint32_t account_type;
1753 if (ldb_dn_is_special(req->op.mod.message->dn)) {
1754 /* do not manipulate our control entries */
1755 return ldb_next_request(module, req);
1758 ldb = ldb_module_get_ctx(module);
1760 if (ldb_msg_find_element(req->op.mod.message, "sAMAccountType") != NULL) {
1761 ldb_asprintf_errstring(ldb,
1762 "sAMAccountType must not be specified!");
1763 return LDB_ERR_UNWILLING_TO_PERFORM;
1766 /* TODO: do not modify original request, create a new one */
1768 el = ldb_msg_find_element(req->op.mod.message, "groupType");
1769 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1770 uint32_t group_type;
1772 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1773 req->op.mod.message);
1775 group_type = strtoul((const char *)el->values[0].data, NULL, 0);
1776 account_type = ds_gtype2atype(group_type);
1777 ret = samdb_msg_add_uint(ldb, msg, msg,
1780 if (ret != LDB_SUCCESS) {
1783 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1784 el2->flags = LDB_FLAG_MOD_REPLACE;
1787 el = ldb_msg_find_element(req->op.mod.message, "primaryGroupID");
1788 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1789 struct samldb_ctx *ac;
1791 ac = samldb_ctx_init(module, req);
1793 return LDB_ERR_OPERATIONS_ERROR;
1795 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1796 req->op.mod.message);
1798 return samldb_prim_group_change(ac);
1801 el = ldb_msg_find_element(req->op.mod.message, "userAccountControl");
1802 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1803 uint32_t user_account_control;
1805 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1806 req->op.mod.message);
1808 user_account_control = strtoul((const char *)el->values[0].data,
1810 account_type = ds_uf2atype(user_account_control);
1811 ret = samdb_msg_add_uint(ldb, msg, msg,
1814 if (ret != LDB_SUCCESS) {
1817 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1818 el2->flags = LDB_FLAG_MOD_REPLACE;
1820 if (user_account_control & UF_SERVER_TRUST_ACCOUNT) {
1821 ret = samdb_msg_add_string(ldb, msg, msg,
1822 "isCriticalSystemObject", "TRUE");
1823 if (ret != LDB_SUCCESS) {
1826 el2 = ldb_msg_find_element(msg, "isCriticalSystemObject");
1827 el2->flags = LDB_FLAG_MOD_REPLACE;
1829 /* DCs have primaryGroupID of DOMAIN_RID_DCS */
1830 if (!ldb_msg_find_element(msg, "primaryGroupID")) {
1831 ret = samdb_msg_add_uint(ldb, msg, msg,
1832 "primaryGroupID", DOMAIN_RID_DCS);
1833 if (ret != LDB_SUCCESS) {
1836 el2 = ldb_msg_find_element(msg, "primaryGroupID");
1837 el2->flags = LDB_FLAG_MOD_REPLACE;
1842 el = ldb_msg_find_element(req->op.mod.message, "member");
1843 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1844 struct samldb_ctx *ac;
1846 ac = samldb_ctx_init(module, req);
1848 return LDB_ERR_OPERATIONS_ERROR;
1850 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1851 req->op.mod.message);
1853 return samldb_member_check(ac);
1856 /* nothing matched, go on */
1857 return ldb_next_request(module, req);
1861 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
1863 struct samldb_ctx *ac;
1865 if (ldb_dn_is_special(req->op.del.dn)) {
1866 /* do not manipulate our control entries */
1867 return ldb_next_request(module, req);
1870 ac = samldb_ctx_init(module, req);
1872 return LDB_ERR_OPERATIONS_ERROR;
1874 return samldb_prim_group_users_check(ac);
1877 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
1879 struct ldb_context *ldb = ldb_module_get_ctx(module);
1880 struct dsdb_fsmo_extended_op *exop;
1883 exop = talloc_get_type(req->op.extended.data, struct dsdb_fsmo_extended_op);
1885 ldb_debug(ldb, LDB_DEBUG_FATAL, "samldb_extended_allocate_rid_pool: invalid extended data\n");
1886 return LDB_ERR_PROTOCOL_ERROR;
1889 ret = ridalloc_allocate_rid_pool_fsmo(module, exop);
1890 if (ret != LDB_SUCCESS) {
1894 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
1897 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
1899 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
1900 return samldb_extended_allocate_rid_pool(module, req);
1903 return ldb_next_request(module, req);
1907 _PUBLIC_ const struct ldb_module_ops ldb_samldb_module_ops = {
1910 .modify = samldb_modify,
1911 .del = samldb_delete,
1912 .extended = samldb_extended