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:
219 /* not found, go on */
221 ret = samldb_next_step(ac);
225 if (ret != LDB_SUCCESS) {
226 return ldb_module_done(ac->req, NULL, NULL, ret);
232 static int samldb_check_samAccountName(struct samldb_ctx *ac)
234 struct ldb_context *ldb;
235 struct ldb_request *req;
240 ldb = ldb_module_get_ctx(ac->module);
242 if (ldb_msg_find_element(ac->msg, "samAccountName") == NULL) {
243 ret = samldb_generate_samAccountName(ac->msg);
244 if (ret != LDB_SUCCESS) {
249 name = ldb_msg_find_attr_as_string(ac->msg, "samAccountName", NULL);
251 return LDB_ERR_OPERATIONS_ERROR;
253 filter = talloc_asprintf(ac, "samAccountName=%s",
254 ldb_binary_encode_string(ac, name));
255 if (filter == NULL) {
256 return LDB_ERR_OPERATIONS_ERROR;
259 ret = ldb_build_search_req(&req, ldb, ac,
260 samdb_base_dn(ldb), LDB_SCOPE_SUBTREE,
263 ac, samldb_check_samAccountName_callback,
266 if (ret != LDB_SUCCESS) {
269 return ldb_next_request(ac->module, req);
273 static int samldb_check_samAccountType(struct samldb_ctx *ac)
275 struct ldb_context *ldb;
276 unsigned int account_type;
277 unsigned int group_type;
281 ldb = ldb_module_get_ctx(ac->module);
283 /* make sure sAMAccountType is not specified */
284 if (ldb_msg_find_element(ac->msg, "sAMAccountType") != NULL) {
285 ldb_asprintf_errstring(ldb,
286 "sAMAccountType must not be specified!");
287 return LDB_ERR_UNWILLING_TO_PERFORM;
290 if (strcmp("user", ac->type) == 0) {
291 uac = samdb_result_uint(ac->msg, "userAccountControl", 0);
293 ldb_asprintf_errstring(ldb,
294 "userAccountControl invalid!");
295 return LDB_ERR_UNWILLING_TO_PERFORM;
297 account_type = ds_uf2atype(uac);
298 ret = samdb_msg_add_uint(ldb,
302 if (ret != LDB_SUCCESS) {
307 if (strcmp("group", ac->type) == 0) {
309 group_type = samdb_result_uint(ac->msg, "groupType", 0);
310 if (group_type == 0) {
311 ldb_asprintf_errstring(ldb,
312 "groupType invalid!\n");
313 return LDB_ERR_UNWILLING_TO_PERFORM;
315 account_type = ds_gtype2atype(group_type);
316 ret = samdb_msg_add_uint(ldb,
320 if (ret != LDB_SUCCESS) {
326 return samldb_next_step(ac);
329 static bool samldb_msg_add_sid(struct ldb_message *msg,
331 const struct dom_sid *sid)
334 enum ndr_err_code ndr_err;
336 ndr_err = ndr_push_struct_blob(&v, msg, NULL, sid,
337 (ndr_push_flags_fn_t)ndr_push_dom_sid);
338 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
341 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
345 /* allocate a SID using our RID Set */
346 static int samldb_allocate_sid(struct samldb_ctx *ac)
350 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
352 ret = ridalloc_allocate_rid(ac->module, &rid);
353 if (ret != LDB_SUCCESS) {
357 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
358 if (ac->sid == NULL) {
359 ldb_module_oom(ac->module);
360 return LDB_ERR_OPERATIONS_ERROR;
363 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", ac->sid)) {
364 return LDB_ERR_OPERATIONS_ERROR;
367 return samldb_next_step(ac);
371 * samldb_dn_from_sid (async)
374 static int samldb_dn_from_sid(struct samldb_ctx *ac);
376 static int samldb_dn_from_sid_callback(struct ldb_request *req,
377 struct ldb_reply *ares)
379 struct ldb_context *ldb;
380 struct samldb_ctx *ac;
383 ac = talloc_get_type(req->context, struct samldb_ctx);
384 ldb = ldb_module_get_ctx(ac->module);
387 ret = LDB_ERR_OPERATIONS_ERROR;
390 if (ares->error != LDB_SUCCESS) {
391 return ldb_module_done(ac->req, ares->controls,
392 ares->response, ares->error);
395 switch (ares->type) {
396 case LDB_REPLY_ENTRY:
398 if (ac->res_dn != NULL) {
400 ldb_set_errstring(ldb,
401 "Invalid number of results while searching "
402 "for domain objects!");
403 ret = LDB_ERR_OPERATIONS_ERROR;
406 ac->res_dn = ldb_dn_copy(ac, ares->message->dn);
412 case LDB_REPLY_REFERRAL:
421 /* found or not found, go on */
422 ret = samldb_next_step(ac);
427 if (ret != LDB_SUCCESS) {
428 return ldb_module_done(ac->req, NULL, NULL, ret);
434 /* Finds the DN "res_dn" of an object with a given SID "sid" */
435 static int samldb_dn_from_sid(struct samldb_ctx *ac)
437 struct ldb_context *ldb;
438 static const char * const attrs[] = { NULL };
439 struct ldb_request *req;
443 ldb = ldb_module_get_ctx(ac->module);
446 return LDB_ERR_OPERATIONS_ERROR;
448 filter = talloc_asprintf(ac, "(objectSid=%s)",
449 ldap_encode_ndr_dom_sid(ac, ac->sid));
451 return LDB_ERR_OPERATIONS_ERROR;
453 ret = ldb_build_search_req(&req, ldb, ac,
454 ldb_get_default_basedn(ldb),
458 ac, samldb_dn_from_sid_callback,
460 if (ret != LDB_SUCCESS)
463 return ldb_next_request(ac->module, req);
467 static int samldb_check_primaryGroupID_1(struct samldb_ctx *ac)
469 struct ldb_context *ldb;
472 ldb = ldb_module_get_ctx(ac->module);
474 rid = samdb_result_uint(ac->msg, "primaryGroupID", ~0);
475 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
477 return LDB_ERR_OPERATIONS_ERROR;
480 return samldb_next_step(ac);
483 static int samldb_check_primaryGroupID_2(struct samldb_ctx *ac)
485 if (ac->res_dn == NULL) {
486 struct ldb_context *ldb;
487 ldb = ldb_module_get_ctx(ac->module);
488 ldb_asprintf_errstring(ldb,
489 "Failed to find group sid %s!",
490 dom_sid_string(ac->sid, ac->sid));
491 return LDB_ERR_UNWILLING_TO_PERFORM;
494 return samldb_next_step(ac);
499 * samldb_set_defaultObjectCategory_callback (async)
502 static int samldb_set_defaultObjectCategory_callback(struct ldb_request *req,
503 struct ldb_reply *ares)
505 struct ldb_context *ldb;
506 struct samldb_ctx *ac;
509 ac = talloc_get_type(req->context, struct samldb_ctx);
510 ldb = ldb_module_get_ctx(ac->module);
513 ret = LDB_ERR_OPERATIONS_ERROR;
516 if (ares->error != LDB_SUCCESS) {
517 return ldb_module_done(ac->req, ares->controls,
518 ares->response, ares->error);
520 if (ares->type != LDB_REPLY_DONE) {
521 ldb_set_errstring(ldb,
522 "Invalid reply type!");
523 ret = LDB_ERR_OPERATIONS_ERROR;
527 ret = samldb_next_step(ac);
530 if (ret != LDB_SUCCESS) {
531 return ldb_module_done(ac->req, NULL, NULL, ret);
537 static int samldb_set_defaultObjectCategory(struct samldb_ctx *ac)
539 struct ldb_context *ldb;
540 struct ldb_message *msg;
541 struct ldb_request *req;
544 ldb = ldb_module_get_ctx(ac->module);
546 /* (Re)set the default object category to have it set to the DN in the
548 msg = ldb_msg_new(ac);
549 msg->dn = ac->msg->dn;
550 ldb_msg_add_empty(msg, "defaultObjectCategory",
551 LDB_FLAG_MOD_REPLACE, NULL);
552 ldb_msg_add_steal_string(msg, "defaultObjectCategory",
553 ldb_dn_alloc_linearized(msg, ac->dn));
555 ret = ldb_build_mod_req(&req, ldb, ac,
558 samldb_set_defaultObjectCategory_callback,
560 if (ret != LDB_SUCCESS) {
565 return ldb_next_request(ac->module, req);
569 * samldb_find_for_defaultObjectCategory (async)
572 static int samldb_find_for_defaultObjectCategory_callback(struct ldb_request *req,
573 struct ldb_reply *ares)
575 struct ldb_context *ldb;
576 struct samldb_ctx *ac;
579 ac = talloc_get_type(req->context, struct samldb_ctx);
580 ldb = ldb_module_get_ctx(ac->module);
583 ret = LDB_ERR_OPERATIONS_ERROR;
586 if (ares->error != LDB_SUCCESS) {
587 if (ares->error == LDB_ERR_NO_SUCH_OBJECT) {
588 if (ldb_request_get_control(ac->req,
589 LDB_CONTROL_RELAX_OID) != NULL) {
590 /* Don't be pricky when the DN doesn't exist */
591 /* if we have the RELAX control specified */
592 ac->dn = req->op.search.base;
593 return samldb_next_step(ac);
595 ldb_set_errstring(ldb,
596 "samldb_find_defaultObjectCategory: "
597 "Invalid DN for 'defaultObjectCategory'!");
598 ares->error = LDB_ERR_CONSTRAINT_VIOLATION;
602 return ldb_module_done(ac->req, ares->controls,
603 ares->response, ares->error);
606 switch (ares->type) {
607 case LDB_REPLY_ENTRY:
608 ac->dn = talloc_steal(ac, ares->message->dn);
613 case LDB_REPLY_REFERRAL:
622 if (ac->dn != NULL) {
623 /* when found go on */
624 ret = samldb_next_step(ac);
626 ret = LDB_ERR_OPERATIONS_ERROR;
632 if (ret != LDB_SUCCESS) {
633 return ldb_module_done(ac->req, NULL, NULL, ret);
639 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
641 struct ldb_context *ldb;
642 struct ldb_request *req;
643 static const char *no_attrs[] = { NULL };
645 const struct ldb_val *val;
646 struct ldb_dn *def_obj_cat_dn;
648 ldb = ldb_module_get_ctx(ac->module);
652 val = ldb_msg_find_ldb_val(ac->msg, "defaultObjectCategory");
654 /* "defaultObjectCategory" has been set by the caller. Do some
655 * checks for consistency.
656 * NOTE: The real constraint check (that 'defaultObjectCategory'
657 * is the DN of the new objectclass or any parent of it) is
659 * For now we say that 'defaultObjectCategory' is valid if it
660 * exists and it is of objectclass "classSchema". */
661 def_obj_cat_dn = ldb_dn_from_ldb_val(ac, ldb, val);
662 if (def_obj_cat_dn == NULL) {
663 ldb_set_errstring(ldb,
664 "samldb_find_defaultObjectCategory: Invalid DN "
665 "for 'defaultObjectCategory'!");
666 return LDB_ERR_CONSTRAINT_VIOLATION;
669 /* "defaultObjectCategory" has not been set by the caller. Use
670 * the entry DN for it. */
671 def_obj_cat_dn = ac->msg->dn;
674 ret = ldb_build_search_req(&req, ldb, ac,
675 def_obj_cat_dn, LDB_SCOPE_BASE,
676 "objectClass=classSchema", no_attrs,
678 ac, samldb_find_for_defaultObjectCategory_callback,
680 if (ret != LDB_SUCCESS) {
684 ret = dsdb_request_add_controls(req,
685 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT);
686 if (ret != LDB_SUCCESS) {
690 return ldb_next_request(ac->module, req);
694 * msDS-IntId attributeSchema attribute handling
695 * during LDB_ADD request processing
697 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
702 uint32_t system_flags;
703 struct ldb_context *ldb;
704 struct ldb_result *ldb_res;
705 struct ldb_dn *schema_dn;
707 ldb = ldb_module_get_ctx(ac->module);
708 schema_dn = ldb_get_schema_basedn(ldb);
710 /* replicated update should always go through */
711 if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
715 /* msDS-IntId is handled by system and should never be
716 * passed by clients */
717 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
718 return LDB_ERR_UNWILLING_TO_PERFORM;
721 /* do not generate msDS-IntId if Relax control is passed */
722 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
726 /* check Functional Level */
727 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
731 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
732 system_flags = ldb_msg_find_attr_as_uint(ac->msg, "systemFlags", 0);
733 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
737 /* Generate new value for msDs-IntId
738 * Value should be in 0x80000000..0xBFFFFFFF range */
739 msds_intid = generate_random() % 0X3FFFFFFF;
740 msds_intid += 0x80000000;
742 /* probe id values until unique one is found */
745 if (msds_intid > 0xBFFFFFFF) {
746 msds_intid = 0x80000001;
749 ret = dsdb_module_search(ac->module, ac,
751 schema_dn, LDB_SCOPE_ONELEVEL, NULL, 0,
752 "(msDS-IntId=%d)", msds_intid);
753 if (ret != LDB_SUCCESS) {
754 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
755 __location__": Searching for msDS-IntId=%d failed - %s\n",
758 return LDB_ERR_OPERATIONS_ERROR;
760 id_exists = (ldb_res->count > 0);
762 talloc_free(ldb_res);
765 return ldb_msg_add_fmt(ac->msg, "msDS-IntId", "%d", msds_intid);
770 * samldb_add_entry (async)
773 static int samldb_add_entry_callback(struct ldb_request *req,
774 struct ldb_reply *ares)
776 struct ldb_context *ldb;
777 struct samldb_ctx *ac;
780 ac = talloc_get_type(req->context, struct samldb_ctx);
781 ldb = ldb_module_get_ctx(ac->module);
784 return ldb_module_done(ac->req, NULL, NULL,
785 LDB_ERR_OPERATIONS_ERROR);
787 if (ares->error != LDB_SUCCESS) {
788 return ldb_module_done(ac->req, ares->controls,
789 ares->response, ares->error);
791 if (ares->type != LDB_REPLY_DONE) {
792 ldb_set_errstring(ldb,
793 "Invalid reply type!\n");
794 return ldb_module_done(ac->req, NULL, NULL,
795 LDB_ERR_OPERATIONS_ERROR);
798 /* The caller may wish to get controls back from the add */
799 ac->ares = talloc_steal(ac, ares);
801 ret = samldb_next_step(ac);
802 if (ret != LDB_SUCCESS) {
803 return ldb_module_done(ac->req, NULL, NULL, ret);
808 static int samldb_add_entry(struct samldb_ctx *ac)
810 struct ldb_context *ldb;
811 struct ldb_request *req;
814 ldb = ldb_module_get_ctx(ac->module);
816 ret = ldb_build_add_req(&req, ldb, ac,
819 ac, samldb_add_entry_callback,
821 if (ret != LDB_SUCCESS) {
825 return ldb_next_request(ac->module, req);
829 static int samldb_fill_object(struct samldb_ctx *ac, const char *type)
831 struct ldb_context *ldb;
832 struct loadparm_context *lp_ctx;
833 enum sid_generator sid_generator;
836 ldb = ldb_module_get_ctx(ac->module);
838 /* Add informations for the different account types */
840 if (strcmp(ac->type, "user") == 0) {
841 ret = samdb_find_or_add_attribute(ldb, ac->msg,
842 "userAccountControl", "546");
843 if (ret != LDB_SUCCESS) return ret;
844 ret = samdb_find_or_add_attribute(ldb, ac->msg,
846 if (ret != LDB_SUCCESS) return ret;
847 ret = samdb_find_or_add_attribute(ldb, ac->msg,
849 if (ret != LDB_SUCCESS) return ret;
850 ret = samdb_find_or_add_attribute(ldb, ac->msg,
852 if (ret != LDB_SUCCESS) return ret;
853 ret = samdb_find_or_add_attribute(ldb, ac->msg,
854 "badPasswordTime", "0");
855 if (ret != LDB_SUCCESS) return ret;
856 ret = samdb_find_or_add_attribute(ldb, ac->msg,
858 if (ret != LDB_SUCCESS) return ret;
859 ret = samdb_find_or_add_attribute(ldb, ac->msg,
861 if (ret != LDB_SUCCESS) return ret;
862 ret = samdb_find_or_add_attribute(ldb, ac->msg,
864 if (ret != LDB_SUCCESS) return ret;
865 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
866 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
867 "primaryGroupID", DOMAIN_RID_USERS);
868 if (ret != LDB_SUCCESS) return ret;
870 ret = samdb_find_or_add_attribute(ldb, ac->msg,
871 "accountExpires", "9223372036854775807");
872 if (ret != LDB_SUCCESS) return ret;
873 ret = samdb_find_or_add_attribute(ldb, ac->msg,
875 if (ret != LDB_SUCCESS) return ret;
876 } else if (strcmp(ac->type, "group") == 0) {
877 ret = samdb_find_or_add_attribute(ldb, ac->msg,
878 "groupType", "-2147483646");
879 if (ret != LDB_SUCCESS) return ret;
880 } else if (strcmp(ac->type, "classSchema") == 0) {
881 const struct ldb_val *rdn_value;
883 ret = samdb_find_or_add_attribute(ldb, ac->msg,
885 if (ret != LDB_SUCCESS) return ret;
887 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
888 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
889 /* the RDN has prefix "CN" */
890 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
891 samdb_cn_to_lDAPDisplayName(ac,
892 (const char *) rdn_value->data));
893 if (ret != LDB_SUCCESS) {
899 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
902 guid = GUID_random();
903 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
904 if (ret != LDB_SUCCESS) {
910 ret = samldb_add_step(ac, samldb_add_entry);
911 if (ret != LDB_SUCCESS) return ret;
913 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
914 if (ret != LDB_SUCCESS) return ret;
916 ret = samldb_add_step(ac, samldb_set_defaultObjectCategory);
917 if (ret != LDB_SUCCESS) return ret;
919 return samldb_first_step(ac);
920 } else if (strcmp(ac->type, "attributeSchema") == 0) {
921 const struct ldb_val *rdn_value;
922 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
923 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
924 /* the RDN has prefix "CN" */
925 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
926 samdb_cn_to_lDAPDisplayName(ac,
927 (const char *) rdn_value->data));
928 if (ret != LDB_SUCCESS) {
934 ret = samdb_find_or_add_attribute(ldb, ac->msg,
935 "isSingleValued", "FALSE");
936 if (ret != LDB_SUCCESS) return ret;
938 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
941 guid = GUID_random();
942 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
943 if (ret != LDB_SUCCESS) {
949 /* handle msDS-IntID attribute */
950 ret = samldb_add_handle_msDS_IntId(ac);
951 if (ret != LDB_SUCCESS) return ret;
953 ret = samldb_add_step(ac, samldb_add_entry);
954 if (ret != LDB_SUCCESS) return ret;
956 return samldb_first_step(ac);
958 ldb_asprintf_errstring(ldb,
959 "Invalid entry type!");
960 return LDB_ERR_OPERATIONS_ERROR;
963 /* check if we have a valid samAccountName */
964 ret = samldb_add_step(ac, samldb_check_samAccountName);
965 if (ret != LDB_SUCCESS) return ret;
967 /* check account_type/group_type */
968 ret = samldb_add_step(ac, samldb_check_samAccountType);
969 if (ret != LDB_SUCCESS) return ret;
971 /* check if we have a valid primary group ID */
972 if (strcmp(ac->type, "user") == 0) {
973 ret = samldb_add_step(ac, samldb_check_primaryGroupID_1);
974 if (ret != LDB_SUCCESS) return ret;
975 ret = samldb_add_step(ac, samldb_dn_from_sid);
976 if (ret != LDB_SUCCESS) return ret;
977 ret = samldb_add_step(ac, samldb_check_primaryGroupID_2);
978 if (ret != LDB_SUCCESS) return ret;
981 lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
982 struct loadparm_context);
984 /* don't allow objectSID to be specified without the RELAX control */
985 ac->sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
986 if (ac->sid && !ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) &&
987 !dsdb_module_am_system(ac->module)) {
988 ldb_asprintf_errstring(ldb, "No SID may be specified in user/group creation for %s",
989 ldb_dn_get_linearized(ac->msg->dn));
990 return LDB_ERR_UNWILLING_TO_PERFORM;
994 sid_generator = lp_sid_generator(lp_ctx);
995 if (sid_generator == SID_GENERATOR_INTERNAL) {
996 ret = samldb_add_step(ac, samldb_allocate_sid);
997 if (ret != LDB_SUCCESS) return ret;
1001 /* finally proceed with adding the entry */
1002 ret = samldb_add_step(ac, samldb_add_entry);
1003 if (ret != LDB_SUCCESS) return ret;
1005 return samldb_first_step(ac);
1008 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
1010 struct ldb_context *ldb;
1013 ldb = ldb_module_get_ctx(ac->module);
1015 ac->sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
1016 if (ac->sid == NULL) {
1017 ac->sid = dom_sid_parse_talloc(ac->msg,
1018 (const char *)ldb_dn_get_rdn_val(ac->msg->dn)->data);
1020 ldb_set_errstring(ldb,
1021 "No valid SID found in "
1022 "ForeignSecurityPrincipal CN!");
1024 return LDB_ERR_CONSTRAINT_VIOLATION;
1026 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", ac->sid)) {
1028 return LDB_ERR_OPERATIONS_ERROR;
1032 /* finally proceed with adding the entry */
1033 ret = samldb_add_step(ac, samldb_add_entry);
1034 if (ret != LDB_SUCCESS) return ret;
1036 return samldb_first_step(ac);
1039 static int samldb_check_rdn(struct ldb_module *module, struct ldb_dn *dn)
1041 struct ldb_context *ldb;
1042 const char *rdn_name;
1044 ldb = ldb_module_get_ctx(module);
1045 rdn_name = ldb_dn_get_rdn_name(dn);
1047 if (strcasecmp(rdn_name, "cn") != 0) {
1048 ldb_asprintf_errstring(ldb,
1049 "Bad RDN (%s=) for samldb object, "
1050 "should be CN=!", rdn_name);
1051 return LDB_ERR_CONSTRAINT_VIOLATION;
1057 static int samldb_schema_info_update(struct samldb_ctx *ac)
1060 struct ldb_context *ldb;
1061 struct dsdb_schema *schema;
1063 /* replicated update should always go through */
1064 if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1068 /* do not update schemaInfo during provisioning */
1069 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
1073 ldb = ldb_module_get_ctx(ac->module);
1074 schema = dsdb_get_schema(ldb, NULL);
1076 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
1077 "samldb_schema_info_update: no dsdb_schema loaded");
1078 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
1079 return LDB_ERR_OPERATIONS_ERROR;
1082 werr = dsdb_module_schema_info_update(ac->module, schema, 0);
1083 if (!W_ERROR_IS_OK(werr)) {
1084 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
1085 "samldb_schema_info_update: "
1086 "dsdb_module_schema_info_update failed with %s",
1088 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
1089 return LDB_ERR_OPERATIONS_ERROR;
1096 * samldb_sid_from_dn (async)
1099 static int samldb_sid_from_dn(struct samldb_ctx *ac);
1101 static int samldb_sid_from_dn_callback(struct ldb_request *req,
1102 struct ldb_reply *ares)
1104 struct ldb_context *ldb;
1105 struct samldb_ctx *ac;
1108 ac = talloc_get_type(req->context, struct samldb_ctx);
1109 ldb = ldb_module_get_ctx(ac->module);
1112 ret = LDB_ERR_OPERATIONS_ERROR;
1115 if (ares->error != LDB_SUCCESS) {
1116 return ldb_module_done(ac->req, ares->controls,
1117 ares->response, ares->error);
1120 switch (ares->type) {
1121 case LDB_REPLY_ENTRY:
1123 if (ac->res_sid != NULL) {
1125 ldb_set_errstring(ldb,
1126 "Invalid number of results while searching "
1127 "for domain objects!");
1128 ret = LDB_ERR_OPERATIONS_ERROR;
1131 ac->res_sid = samdb_result_dom_sid(ac, ares->message,
1138 case LDB_REPLY_REFERRAL:
1144 case LDB_REPLY_DONE:
1147 /* found or not found, go on */
1148 ret = samldb_next_step(ac);
1153 if (ret != LDB_SUCCESS) {
1154 return ldb_module_done(ac->req, NULL, NULL, ret);
1160 /* Finds the SID "res_sid" of an object with a given DN "dn" */
1161 static int samldb_sid_from_dn(struct samldb_ctx *ac)
1163 struct ldb_context *ldb;
1164 static const char * const attrs[] = { "objectSid", NULL };
1165 struct ldb_request *req;
1168 ldb = ldb_module_get_ctx(ac->module);
1171 return LDB_ERR_OPERATIONS_ERROR;
1173 ret = ldb_build_search_req(&req, ldb, ac,
1178 ac, samldb_sid_from_dn_callback,
1180 if (ret != LDB_SUCCESS)
1183 return ldb_next_request(ac->module, req);
1187 * samldb_user_dn_to_prim_group_rid (async)
1190 static int samldb_user_dn_to_prim_group_rid(struct samldb_ctx *ac);
1192 static int samldb_user_dn_to_prim_group_rid_callback(struct ldb_request *req,
1193 struct ldb_reply *ares)
1195 struct ldb_context *ldb;
1196 struct samldb_ctx *ac;
1199 ac = talloc_get_type(req->context, struct samldb_ctx);
1200 ldb = ldb_module_get_ctx(ac->module);
1203 ret = LDB_ERR_OPERATIONS_ERROR;
1206 if (ares->error != LDB_SUCCESS) {
1207 return ldb_module_done(ac->req, ares->controls,
1208 ares->response, ares->error);
1211 switch (ares->type) {
1212 case LDB_REPLY_ENTRY:
1214 if (ac->prim_group_rid != 0) {
1216 ldb_set_errstring(ldb,
1217 "Invalid number of results while searching "
1218 "for domain objects!");
1219 ret = LDB_ERR_OPERATIONS_ERROR;
1222 ac->prim_group_rid = samdb_result_uint(ares->message,
1223 "primaryGroupID", ~0);
1229 case LDB_REPLY_REFERRAL:
1235 case LDB_REPLY_DONE:
1237 if (ac->prim_group_rid == 0) {
1238 ldb_asprintf_errstring(ldb,
1239 "Unable to get the primary group RID!");
1240 ret = LDB_ERR_OPERATIONS_ERROR;
1245 ret = samldb_next_step(ac);
1250 if (ret != LDB_SUCCESS) {
1251 return ldb_module_done(ac->req, NULL, NULL, ret);
1257 /* Locates the "primaryGroupID" attribute from a certain user specified as
1258 * "user_dn". Saves the result in "prim_group_rid". */
1259 static int samldb_user_dn_to_prim_group_rid(struct samldb_ctx *ac)
1261 struct ldb_context *ldb;
1262 static const char * const attrs[] = { "primaryGroupID", NULL };
1263 struct ldb_request *req;
1266 ldb = ldb_module_get_ctx(ac->module);
1268 if (ac->user_dn == NULL)
1269 return LDB_ERR_OPERATIONS_ERROR;
1271 ret = ldb_build_search_req(&req, ldb, ac,
1276 ac, samldb_user_dn_to_prim_group_rid_callback,
1278 if (ret != LDB_SUCCESS)
1281 return ldb_next_request(ac->module, req);
1285 * samldb_prim_group_rid_to_users_cnt (async)
1288 static int samldb_prim_group_rid_to_users_cnt(struct samldb_ctx *ac);
1290 static int samldb_prim_group_rid_to_users_cnt_callback(struct ldb_request *req,
1291 struct ldb_reply *ares)
1293 struct ldb_context *ldb;
1294 struct samldb_ctx *ac;
1297 ac = talloc_get_type(req->context, struct samldb_ctx);
1298 ldb = ldb_module_get_ctx(ac->module);
1301 ret = LDB_ERR_OPERATIONS_ERROR;
1304 if (ares->error != LDB_SUCCESS) {
1305 return ldb_module_done(ac->req, ares->controls,
1306 ares->response, ares->error);
1309 switch (ares->type) {
1310 case LDB_REPLY_ENTRY:
1318 case LDB_REPLY_REFERRAL:
1324 case LDB_REPLY_DONE:
1327 /* found or not found, go on */
1328 ret = samldb_next_step(ac);
1333 if (ret != LDB_SUCCESS) {
1334 return ldb_module_done(ac->req, NULL, NULL, ret);
1340 /* Finds the amount of users which have the primary group "prim_group_rid" and
1341 * save the result in "users_cnt" */
1342 static int samldb_prim_group_rid_to_users_cnt(struct samldb_ctx *ac)
1344 struct ldb_context *ldb;
1345 static const char * const attrs[] = { NULL };
1346 struct ldb_request *req;
1350 ldb = ldb_module_get_ctx(ac->module);
1352 if ((ac->prim_group_rid == 0) || (ac->users_cnt != 0))
1353 return LDB_ERR_OPERATIONS_ERROR;
1355 filter = talloc_asprintf(ac, "(&(primaryGroupID=%u)(objectclass=user))",
1356 ac->prim_group_rid);
1358 return LDB_ERR_OPERATIONS_ERROR;
1360 ret = ldb_build_search_req(&req, ldb, ac,
1361 ldb_get_default_basedn(ldb),
1366 samldb_prim_group_rid_to_users_cnt_callback,
1368 if (ret != LDB_SUCCESS)
1371 return ldb_next_request(ac->module, req);
1375 * samldb_group_add_member (async)
1376 * samldb_group_del_member (async)
1379 static int samldb_group_add_del_member_callback(struct ldb_request *req,
1380 struct ldb_reply *ares)
1382 struct ldb_context *ldb;
1383 struct samldb_ctx *ac;
1386 ac = talloc_get_type(req->context, struct samldb_ctx);
1387 ldb = ldb_module_get_ctx(ac->module);
1390 ret = LDB_ERR_OPERATIONS_ERROR;
1393 if (ares->error != LDB_SUCCESS) {
1394 if (ares->error == LDB_ERR_NO_SUCH_ATTRIBUTE) {
1395 /* On error "NO_SUCH_ATTRIBUTE" (delete of an invalid
1396 * "member" attribute) return "UNWILLING_TO_PERFORM" */
1397 ares->error = LDB_ERR_UNWILLING_TO_PERFORM;
1399 return ldb_module_done(ac->req, ares->controls,
1400 ares->response, ares->error);
1402 if (ares->type != LDB_REPLY_DONE) {
1403 ldb_set_errstring(ldb,
1404 "Invalid reply type!");
1405 ret = LDB_ERR_OPERATIONS_ERROR;
1409 ret = samldb_next_step(ac);
1412 if (ret != LDB_SUCCESS) {
1413 return ldb_module_done(ac->req, NULL, NULL, ret);
1419 /* Adds a member with DN "member_dn" to a group with DN "group_dn" */
1420 static int samldb_group_add_member(struct samldb_ctx *ac)
1422 struct ldb_context *ldb;
1423 struct ldb_request *req;
1424 struct ldb_message *msg;
1427 ldb = ldb_module_get_ctx(ac->module);
1429 if ((ac->group_dn == NULL) || (ac->member_dn == NULL))
1430 return LDB_ERR_OPERATIONS_ERROR;
1432 msg = ldb_msg_new(ac);
1433 msg->dn = ac->group_dn;
1434 samdb_msg_add_addval(ldb, ac, msg, "member",
1435 ldb_dn_get_linearized(ac->member_dn));
1437 ret = ldb_build_mod_req(&req, ldb, ac,
1439 ac, samldb_group_add_del_member_callback,
1441 if (ret != LDB_SUCCESS)
1444 return ldb_next_request(ac->module, req);
1447 /* Removes a member with DN "member_dn" from a group with DN "group_dn" */
1448 static int samldb_group_del_member(struct samldb_ctx *ac)
1450 struct ldb_context *ldb;
1451 struct ldb_request *req;
1452 struct ldb_message *msg;
1455 ldb = ldb_module_get_ctx(ac->module);
1457 if ((ac->group_dn == NULL) || (ac->member_dn == NULL))
1458 return LDB_ERR_OPERATIONS_ERROR;
1460 msg = ldb_msg_new(ac);
1461 msg->dn = ac->group_dn;
1462 samdb_msg_add_delval(ldb, ac, msg, "member",
1463 ldb_dn_get_linearized(ac->member_dn));
1465 ret = ldb_build_mod_req(&req, ldb, ac,
1467 ac, samldb_group_add_del_member_callback,
1469 if (ret != LDB_SUCCESS)
1472 return ldb_next_request(ac->module, req);
1476 static int samldb_prim_group_change_1(struct samldb_ctx *ac)
1478 struct ldb_context *ldb;
1481 ldb = ldb_module_get_ctx(ac->module);
1483 ac->user_dn = ac->msg->dn;
1485 rid = samdb_result_uint(ac->msg, "primaryGroupID", ~0);
1486 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1487 if (ac->sid == NULL)
1488 return LDB_ERR_OPERATIONS_ERROR;
1491 ac->prim_group_rid = 0;
1493 return samldb_next_step(ac);
1496 static int samldb_prim_group_change_2(struct samldb_ctx *ac)
1498 struct ldb_context *ldb;
1500 ldb = ldb_module_get_ctx(ac->module);
1502 if (ac->res_dn != NULL)
1503 ac->new_prim_group_dn = ac->res_dn;
1505 return LDB_ERR_UNWILLING_TO_PERFORM;
1507 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1508 ac->prim_group_rid);
1509 if (ac->sid == NULL)
1510 return LDB_ERR_OPERATIONS_ERROR;
1513 return samldb_next_step(ac);
1516 static int samldb_prim_group_change_4(struct samldb_ctx *ac);
1517 static int samldb_prim_group_change_5(struct samldb_ctx *ac);
1518 static int samldb_prim_group_change_6(struct samldb_ctx *ac);
1520 static int samldb_prim_group_change_3(struct samldb_ctx *ac)
1524 if (ac->res_dn != NULL)
1525 ac->old_prim_group_dn = ac->res_dn;
1527 return LDB_ERR_UNWILLING_TO_PERFORM;
1529 /* Only update when the primary group changed */
1530 if (ldb_dn_compare(ac->old_prim_group_dn, ac->new_prim_group_dn) != 0) {
1531 ac->member_dn = ac->user_dn;
1532 /* Remove the "member" attribute of the actual (new) primary
1535 ret = samldb_add_step(ac, samldb_prim_group_change_4);
1536 if (ret != LDB_SUCCESS) return ret;
1538 ret = samldb_add_step(ac, samldb_group_del_member);
1539 if (ret != LDB_SUCCESS) return ret;
1541 /* Add a "member" attribute for the previous primary group */
1543 ret = samldb_add_step(ac, samldb_prim_group_change_5);
1544 if (ret != LDB_SUCCESS) return ret;
1546 ret = samldb_add_step(ac, samldb_group_add_member);
1547 if (ret != LDB_SUCCESS) return ret;
1550 ret = samldb_add_step(ac, samldb_prim_group_change_6);
1551 if (ret != LDB_SUCCESS) return ret;
1553 return samldb_next_step(ac);
1556 static int samldb_prim_group_change_4(struct samldb_ctx *ac)
1558 ac->group_dn = ac->new_prim_group_dn;
1560 return samldb_next_step(ac);
1563 static int samldb_prim_group_change_5(struct samldb_ctx *ac)
1565 ac->group_dn = ac->old_prim_group_dn;
1567 return samldb_next_step(ac);
1570 static int samldb_prim_group_change_6(struct samldb_ctx *ac)
1572 return ldb_next_request(ac->module, ac->req);
1575 static int samldb_prim_group_change(struct samldb_ctx *ac)
1579 /* Finds out the DN of the new primary group */
1581 ret = samldb_add_step(ac, samldb_prim_group_change_1);
1582 if (ret != LDB_SUCCESS) return ret;
1584 ret = samldb_add_step(ac, samldb_dn_from_sid);
1585 if (ret != LDB_SUCCESS) return ret;
1587 ret = samldb_add_step(ac, samldb_user_dn_to_prim_group_rid);
1588 if (ret != LDB_SUCCESS) return ret;
1590 /* Finds out the DN of the old primary group */
1592 ret = samldb_add_step(ac, samldb_prim_group_change_2);
1593 if (ret != LDB_SUCCESS) return ret;
1595 ret = samldb_add_step(ac, samldb_dn_from_sid);
1596 if (ret != LDB_SUCCESS) return ret;
1598 ret = samldb_add_step(ac, samldb_prim_group_change_3);
1599 if (ret != LDB_SUCCESS) return ret;
1601 return samldb_first_step(ac);
1605 static int samldb_member_check_1(struct samldb_ctx *ac)
1607 struct ldb_context *ldb;
1608 struct ldb_message_element *el;
1610 ldb = ldb_module_get_ctx(ac->module);
1612 el = ldb_msg_find_element(ac->msg, "member");
1614 ac->user_dn = ldb_dn_from_ldb_val(ac, ldb, &el->values[ac->cnt]);
1615 if (!ldb_dn_validate(ac->user_dn))
1616 return LDB_ERR_OPERATIONS_ERROR;
1617 ac->prim_group_rid = 0;
1619 return samldb_next_step(ac);
1622 static int samldb_member_check_2(struct samldb_ctx *ac)
1624 struct ldb_context *ldb;
1626 ldb = ldb_module_get_ctx(ac->module);
1628 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1629 ac->prim_group_rid);
1630 if (ac->sid == NULL)
1631 return LDB_ERR_OPERATIONS_ERROR;
1634 return samldb_next_step(ac);
1637 static int samldb_member_check_3(struct samldb_ctx *ac)
1639 if (ldb_dn_compare(ac->res_dn, ac->msg->dn) == 0)
1640 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1644 return samldb_next_step(ac);
1647 static int samldb_member_check_4(struct samldb_ctx *ac)
1649 return ldb_next_request(ac->module, ac->req);
1652 static int samldb_member_check(struct samldb_ctx *ac)
1654 struct ldb_message_element *el;
1657 el = ldb_msg_find_element(ac->msg, "member");
1659 for (i = 0; i < el->num_values; i++) {
1660 /* Denies to add "member"s to groups which are primary ones
1662 ret = samldb_add_step(ac, samldb_member_check_1);
1663 if (ret != LDB_SUCCESS) return ret;
1665 ret = samldb_add_step(ac, samldb_user_dn_to_prim_group_rid);
1666 if (ret != LDB_SUCCESS) return ret;
1668 ret = samldb_add_step(ac, samldb_member_check_2);
1669 if (ret != LDB_SUCCESS) return ret;
1671 ret = samldb_add_step(ac, samldb_dn_from_sid);
1672 if (ret != LDB_SUCCESS) return ret;
1674 ret = samldb_add_step(ac, samldb_member_check_3);
1675 if (ret != LDB_SUCCESS) return ret;
1678 ret = samldb_add_step(ac, samldb_member_check_4);
1679 if (ret != LDB_SUCCESS) return ret;
1681 return samldb_first_step(ac);
1685 static int samldb_prim_group_users_check_1(struct samldb_ctx *ac)
1687 ac->dn = ac->req->op.del.dn;
1690 return samldb_next_step(ac);
1693 static int samldb_prim_group_users_check_2(struct samldb_ctx *ac)
1698 if (ac->res_sid == NULL) {
1699 /* No SID - therefore ok here */
1700 return ldb_next_request(ac->module, ac->req);
1702 status = dom_sid_split_rid(ac, ac->res_sid, NULL, &rid);
1703 if (!NT_STATUS_IS_OK(status))
1704 return LDB_ERR_OPERATIONS_ERROR;
1707 /* Special object (security principal?) */
1708 return ldb_next_request(ac->module, ac->req);
1711 ac->prim_group_rid = rid;
1714 return samldb_next_step(ac);
1717 static int samldb_prim_group_users_check_3(struct samldb_ctx *ac)
1719 if (ac->users_cnt > 0)
1720 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1722 return ldb_next_request(ac->module, ac->req);
1725 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
1729 /* Finds out the SID/RID of the domain object */
1731 ret = samldb_add_step(ac, samldb_prim_group_users_check_1);
1732 if (ret != LDB_SUCCESS) return ret;
1734 ret = samldb_add_step(ac, samldb_sid_from_dn);
1735 if (ret != LDB_SUCCESS) return ret;
1737 /* Deny delete requests from groups which are primary ones */
1739 ret = samldb_add_step(ac, samldb_prim_group_users_check_2);
1740 if (ret != LDB_SUCCESS) return ret;
1742 ret = samldb_add_step(ac, samldb_prim_group_rid_to_users_cnt);
1743 if (ret != LDB_SUCCESS) return ret;
1745 ret = samldb_add_step(ac, samldb_prim_group_users_check_3);
1746 if (ret != LDB_SUCCESS) return ret;
1748 return samldb_first_step(ac);
1753 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
1755 struct ldb_context *ldb;
1756 struct samldb_ctx *ac;
1759 ldb = ldb_module_get_ctx(module);
1760 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
1762 /* do not manipulate our control entries */
1763 if (ldb_dn_is_special(req->op.add.message->dn)) {
1764 return ldb_next_request(module, req);
1767 ac = samldb_ctx_init(module, req);
1769 return LDB_ERR_OPERATIONS_ERROR;
1772 /* build the new msg */
1773 ac->msg = ldb_msg_copy(ac, ac->req->op.add.message);
1776 ldb_debug(ldb, LDB_DEBUG_FATAL,
1777 "samldb_add: ldb_msg_copy failed!\n");
1778 return LDB_ERR_OPERATIONS_ERROR;
1781 if (samdb_find_attribute(ldb, ac->msg,
1782 "objectclass", "computer") != NULL) {
1784 /* make sure the computer object also has the 'user'
1785 * objectclass so it will be handled by the next call */
1786 ret = samdb_find_or_add_value(ldb, ac->msg,
1787 "objectclass", "user");
1788 if (ret != LDB_SUCCESS) {
1794 if (samdb_find_attribute(ldb, ac->msg,
1795 "objectclass", "user") != NULL) {
1797 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1798 if (ret != LDB_SUCCESS) {
1803 return samldb_fill_object(ac, "user");
1806 if (samdb_find_attribute(ldb, ac->msg,
1807 "objectclass", "group") != NULL) {
1809 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1810 if (ret != LDB_SUCCESS) {
1815 return samldb_fill_object(ac, "group");
1818 /* perhaps a foreignSecurityPrincipal? */
1819 if (samdb_find_attribute(ldb, ac->msg,
1821 "foreignSecurityPrincipal") != NULL) {
1823 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1824 if (ret != LDB_SUCCESS) {
1829 return samldb_fill_foreignSecurityPrincipal_object(ac);
1832 if (samdb_find_attribute(ldb, ac->msg,
1833 "objectclass", "classSchema") != NULL) {
1835 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1836 if (ret != LDB_SUCCESS) {
1841 ret = samldb_schema_info_update(ac);
1842 if (ret != LDB_SUCCESS) {
1847 return samldb_fill_object(ac, "classSchema");
1850 if (samdb_find_attribute(ldb, ac->msg,
1851 "objectclass", "attributeSchema") != NULL) {
1853 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
1854 if (ret != LDB_SUCCESS) {
1859 ret = samldb_schema_info_update(ac);
1860 if (ret != LDB_SUCCESS) {
1865 return samldb_fill_object(ac, "attributeSchema");
1870 /* nothing matched, go on */
1871 return ldb_next_request(module, req);
1875 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
1877 struct ldb_context *ldb;
1878 struct ldb_message *msg;
1879 struct ldb_message_element *el, *el2;
1881 uint32_t account_type;
1883 if (ldb_dn_is_special(req->op.mod.message->dn)) {
1884 /* do not manipulate our control entries */
1885 return ldb_next_request(module, req);
1888 ldb = ldb_module_get_ctx(module);
1890 if (ldb_msg_find_element(req->op.mod.message, "sAMAccountType") != NULL) {
1891 ldb_asprintf_errstring(ldb,
1892 "sAMAccountType must not be specified!");
1893 return LDB_ERR_UNWILLING_TO_PERFORM;
1896 /* msDS-IntId is not allowed to be modified
1897 * except when modification comes from replication */
1898 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
1899 if (!ldb_request_get_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1900 return LDB_ERR_CONSTRAINT_VIOLATION;
1904 /* TODO: do not modify original request, create a new one */
1906 el = ldb_msg_find_element(req->op.mod.message, "groupType");
1907 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1908 uint32_t group_type;
1910 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1911 req->op.mod.message);
1913 group_type = strtoul((const char *)el->values[0].data, NULL, 0);
1914 account_type = ds_gtype2atype(group_type);
1915 ret = samdb_msg_add_uint(ldb, msg, msg,
1918 if (ret != LDB_SUCCESS) {
1921 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1922 el2->flags = LDB_FLAG_MOD_REPLACE;
1925 el = ldb_msg_find_element(req->op.mod.message, "primaryGroupID");
1926 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1927 struct samldb_ctx *ac;
1929 ac = samldb_ctx_init(module, req);
1931 return LDB_ERR_OPERATIONS_ERROR;
1933 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1934 req->op.mod.message);
1936 return samldb_prim_group_change(ac);
1939 el = ldb_msg_find_element(req->op.mod.message, "userAccountControl");
1940 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1941 uint32_t user_account_control;
1943 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1944 req->op.mod.message);
1946 user_account_control = strtoul((const char *)el->values[0].data,
1948 account_type = ds_uf2atype(user_account_control);
1949 ret = samdb_msg_add_uint(ldb, msg, msg,
1952 if (ret != LDB_SUCCESS) {
1955 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1956 el2->flags = LDB_FLAG_MOD_REPLACE;
1958 if (user_account_control & UF_SERVER_TRUST_ACCOUNT) {
1959 ret = samdb_msg_add_string(ldb, msg, msg,
1960 "isCriticalSystemObject", "TRUE");
1961 if (ret != LDB_SUCCESS) {
1964 el2 = ldb_msg_find_element(msg, "isCriticalSystemObject");
1965 el2->flags = LDB_FLAG_MOD_REPLACE;
1967 /* DCs have primaryGroupID of DOMAIN_RID_DCS */
1968 if (!ldb_msg_find_element(msg, "primaryGroupID")) {
1969 ret = samdb_msg_add_uint(ldb, msg, msg,
1970 "primaryGroupID", DOMAIN_RID_DCS);
1971 if (ret != LDB_SUCCESS) {
1974 el2 = ldb_msg_find_element(msg, "primaryGroupID");
1975 el2->flags = LDB_FLAG_MOD_REPLACE;
1980 el = ldb_msg_find_element(req->op.mod.message, "member");
1981 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1982 struct samldb_ctx *ac;
1984 ac = samldb_ctx_init(module, req);
1986 return LDB_ERR_OPERATIONS_ERROR;
1988 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1989 req->op.mod.message);
1991 return samldb_member_check(ac);
1994 /* nothing matched, go on */
1995 return ldb_next_request(module, req);
1999 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
2001 struct samldb_ctx *ac;
2003 if (ldb_dn_is_special(req->op.del.dn)) {
2004 /* do not manipulate our control entries */
2005 return ldb_next_request(module, req);
2008 ac = samldb_ctx_init(module, req);
2010 return LDB_ERR_OPERATIONS_ERROR;
2012 return samldb_prim_group_users_check(ac);
2015 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
2017 struct ldb_context *ldb = ldb_module_get_ctx(module);
2018 struct dsdb_fsmo_extended_op *exop;
2021 exop = talloc_get_type(req->op.extended.data, struct dsdb_fsmo_extended_op);
2023 ldb_debug(ldb, LDB_DEBUG_FATAL, "samldb_extended_allocate_rid_pool: invalid extended data\n");
2024 return LDB_ERR_PROTOCOL_ERROR;
2027 ret = ridalloc_allocate_rid_pool_fsmo(module, exop);
2028 if (ret != LDB_SUCCESS) {
2032 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
2035 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
2037 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
2038 return samldb_extended_allocate_rid_pool(module, req);
2041 return ldb_next_request(module, req);
2045 _PUBLIC_ const struct ldb_module_ops ldb_samldb_module_ops = {
2048 .modify = samldb_modify,
2049 .del = samldb_delete,
2050 .extended = samldb_extended