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 /* used to find parent domain */
63 struct ldb_dn *check_dn;
64 struct ldb_dn *domain_dn;
65 struct dom_sid *domain_sid;
68 /* holds the entry SID */
71 /* holds a generic dn */
74 /* used in conjunction with "sid" in "samldb_dn_from_sid" */
75 struct ldb_dn *res_dn;
77 /* used in conjunction with "dn" in "samldb_sid_from_dn" */
78 struct dom_sid *res_sid;
80 /* used in "samldb_user_dn_to_prim_group_rid" */
81 uint32_t prim_group_rid;
83 /* used in conjunction with "prim_group_rid" in
84 * "samldb_prim_group_rid_to_users_cnt" */
85 unsigned int users_cnt;
87 /* used in "samldb_group_add_member" and "samldb_group_del_member" */
88 struct ldb_dn *group_dn;
89 struct ldb_dn *member_dn;
91 /* used in "samldb_primary_group_change" */
92 struct ldb_dn *user_dn;
93 struct ldb_dn *old_prim_group_dn, *new_prim_group_dn;
95 /* generic counter - used in "samldb_member_check" */
98 /* all the async steps necessary to complete the operation */
99 struct samldb_step *steps;
100 struct samldb_step *curstep;
102 /* If someone set an ares to forward controls and response back to the caller */
103 struct ldb_reply *ares;
106 static struct samldb_ctx *samldb_ctx_init(struct ldb_module *module,
107 struct ldb_request *req)
109 struct ldb_context *ldb;
110 struct samldb_ctx *ac;
112 ldb = ldb_module_get_ctx(module);
114 ac = talloc_zero(req, struct samldb_ctx);
126 static int samldb_add_step(struct samldb_ctx *ac, samldb_step_fn_t fn)
128 struct samldb_step *step, *stepper;
130 step = talloc_zero(ac, struct samldb_step);
132 return LDB_ERR_OPERATIONS_ERROR;
137 if (ac->steps == NULL) {
141 if (ac->curstep == NULL)
142 return LDB_ERR_OPERATIONS_ERROR;
143 for (stepper = ac->curstep; stepper->next != NULL;
144 stepper = stepper->next);
145 stepper->next = step;
151 static int samldb_first_step(struct samldb_ctx *ac)
153 if (ac->steps == NULL) {
154 return LDB_ERR_OPERATIONS_ERROR;
157 ac->curstep = ac->steps;
158 return ac->curstep->fn(ac);
161 static int samldb_next_step(struct samldb_ctx *ac)
163 if (ac->curstep->next) {
164 ac->curstep = ac->curstep->next;
165 return ac->curstep->fn(ac);
168 /* we exit the samldb module here */
169 /* If someone set an ares to forward controls and response back to the caller, use them */
171 return ldb_module_done(ac->req, ac->ares->controls,
172 ac->ares->response, LDB_SUCCESS);
174 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
179 * samldb_get_parent_domain (async)
182 static int samldb_get_parent_domain(struct samldb_ctx *ac);
184 static int samldb_get_parent_domain_callback(struct ldb_request *req,
185 struct ldb_reply *ares)
187 struct ldb_context *ldb;
188 struct samldb_ctx *ac;
192 ac = talloc_get_type(req->context, struct samldb_ctx);
193 ldb = ldb_module_get_ctx(ac->module);
196 ret = LDB_ERR_OPERATIONS_ERROR;
199 if (ares->error != LDB_SUCCESS) {
200 return ldb_module_done(ac->req, ares->controls,
201 ares->response, ares->error);
204 switch (ares->type) {
205 case LDB_REPLY_ENTRY:
207 if ((ac->domain_dn != NULL) || (ac->domain_sid != NULL)) {
209 ldb_set_errstring(ldb,
210 "Invalid number of results while searching "
211 "for domain object!");
212 ret = LDB_ERR_OPERATIONS_ERROR;
216 nextRid = ldb_msg_find_attr_as_string(ares->message,
218 if (nextRid == NULL) {
219 ldb_asprintf_errstring(ldb,
220 "While looking for domain above %s attribute nextRid not found in %s!",
221 ldb_dn_get_linearized(
222 ac->req->op.add.message->dn),
223 ldb_dn_get_linearized(ares->message->dn));
224 ret = LDB_ERR_OPERATIONS_ERROR;
228 ac->next_rid = strtol(nextRid, NULL, 0);
230 ac->domain_sid = samdb_result_dom_sid(ac, ares->message,
232 if (ac->domain_sid == NULL) {
233 ldb_set_errstring(ldb,
234 "Unable to get the parent domain SID!");
235 ret = LDB_ERR_CONSTRAINT_VIOLATION;
238 ac->domain_dn = ldb_dn_copy(ac, ares->message->dn);
244 case LDB_REPLY_REFERRAL:
252 if ((ac->domain_dn == NULL) || (ac->domain_sid == NULL)) {
253 /* not found -> retry */
254 ret = samldb_get_parent_domain(ac);
257 ret = samldb_next_step(ac);
263 if (ret != LDB_SUCCESS) {
264 return ldb_module_done(ac->req, NULL, NULL, ret);
270 /* Find a domain object in the parents of a particular DN. */
271 static int samldb_get_parent_domain(struct samldb_ctx *ac)
273 struct ldb_context *ldb;
274 static const char * const attrs[] = { "objectSid", "nextRid", NULL };
275 struct ldb_request *req;
279 ldb = ldb_module_get_ctx(ac->module);
281 if (ac->check_dn == NULL) {
282 return LDB_ERR_OPERATIONS_ERROR;
285 dn = ldb_dn_get_parent(ac, ac->check_dn);
287 ldb_set_errstring(ldb,
288 "Unable to find parent domain object!");
289 return LDB_ERR_CONSTRAINT_VIOLATION;
294 ret = ldb_build_search_req(&req, ldb, ac,
296 "(|(objectClass=domain)"
297 "(objectClass=builtinDomain))",
300 ac, samldb_get_parent_domain_callback,
303 if (ret != LDB_SUCCESS) {
307 return ldb_next_request(ac->module, req);
311 static int samldb_generate_samAccountName(struct ldb_message *msg)
315 /* Format: $000000-000000000000 */
317 name = talloc_asprintf(msg, "$%.6X-%.6X%.6X",
318 (unsigned int)generate_random(),
319 (unsigned int)generate_random(),
320 (unsigned int)generate_random());
322 return LDB_ERR_OPERATIONS_ERROR;
324 return ldb_msg_add_steal_string(msg, "samAccountName", name);
328 * samldb_check_samAccountName (async)
331 static int samldb_check_samAccountName_callback(struct ldb_request *req,
332 struct ldb_reply *ares)
334 struct samldb_ctx *ac;
337 ac = talloc_get_type(req->context, struct samldb_ctx);
339 if (ares->error != LDB_SUCCESS) {
340 return ldb_module_done(ac->req, ares->controls,
341 ares->response, ares->error);
344 switch (ares->type) {
345 case LDB_REPLY_ENTRY:
346 /* if we get an entry it means this samAccountName
348 return ldb_module_done(ac->req, NULL, NULL,
349 LDB_ERR_ENTRY_ALREADY_EXISTS);
351 case LDB_REPLY_REFERRAL:
352 /* this should not happen */
353 return ldb_module_done(ac->req, NULL, NULL,
354 LDB_ERR_OPERATIONS_ERROR);
357 /* not found, go on */
359 ret = samldb_next_step(ac);
363 if (ret != LDB_SUCCESS) {
364 return ldb_module_done(ac->req, NULL, NULL, ret);
370 static int samldb_check_samAccountName(struct samldb_ctx *ac)
372 struct ldb_context *ldb;
373 struct ldb_request *req;
378 ldb = ldb_module_get_ctx(ac->module);
380 if (ldb_msg_find_element(ac->msg, "samAccountName") == NULL) {
381 ret = samldb_generate_samAccountName(ac->msg);
382 if (ret != LDB_SUCCESS) {
387 name = ldb_msg_find_attr_as_string(ac->msg, "samAccountName", NULL);
389 return LDB_ERR_OPERATIONS_ERROR;
391 filter = talloc_asprintf(ac, "samAccountName=%s",
392 ldb_binary_encode_string(ac, name));
393 if (filter == NULL) {
394 return LDB_ERR_OPERATIONS_ERROR;
397 ret = ldb_build_search_req(&req, ldb, ac,
398 ac->domain_dn, LDB_SCOPE_SUBTREE,
401 ac, samldb_check_samAccountName_callback,
404 if (ret != LDB_SUCCESS) {
407 return ldb_next_request(ac->module, req);
411 static int samldb_check_samAccountType(struct samldb_ctx *ac)
413 struct ldb_context *ldb;
414 unsigned int account_type;
415 unsigned int group_type;
419 ldb = ldb_module_get_ctx(ac->module);
421 /* make sure sAMAccountType is not specified */
422 if (ldb_msg_find_element(ac->msg, "sAMAccountType") != NULL) {
423 ldb_asprintf_errstring(ldb,
424 "sAMAccountType must not be specified!");
425 return LDB_ERR_UNWILLING_TO_PERFORM;
428 if (strcmp("user", ac->type) == 0) {
429 uac = samdb_result_uint(ac->msg, "userAccountControl", 0);
431 ldb_asprintf_errstring(ldb,
432 "userAccountControl invalid!");
433 return LDB_ERR_UNWILLING_TO_PERFORM;
435 account_type = ds_uf2atype(uac);
436 ret = samdb_msg_add_uint(ldb,
440 if (ret != LDB_SUCCESS) {
445 if (strcmp("group", ac->type) == 0) {
447 group_type = samdb_result_uint(ac->msg, "groupType", 0);
448 if (group_type == 0) {
449 ldb_asprintf_errstring(ldb,
450 "groupType invalid!\n");
451 return LDB_ERR_UNWILLING_TO_PERFORM;
453 account_type = ds_gtype2atype(group_type);
454 ret = samdb_msg_add_uint(ldb,
458 if (ret != LDB_SUCCESS) {
464 return samldb_next_step(ac);
469 * samldb_get_sid_domain (async)
472 static int samldb_get_sid_domain_callback(struct ldb_request *req,
473 struct ldb_reply *ares)
475 struct ldb_context *ldb;
476 struct samldb_ctx *ac;
480 ac = talloc_get_type(req->context, struct samldb_ctx);
481 ldb = ldb_module_get_ctx(ac->module);
484 ret = LDB_ERR_OPERATIONS_ERROR;
487 if (ares->error != LDB_SUCCESS) {
488 return ldb_module_done(ac->req, ares->controls,
489 ares->response, ares->error);
492 switch (ares->type) {
493 case LDB_REPLY_ENTRY:
495 if (ac->next_rid != 0) {
497 ldb_set_errstring(ldb,
498 "Invalid number of results while searching "
499 "for domain object!");
500 ret = LDB_ERR_OPERATIONS_ERROR;
504 nextRid = ldb_msg_find_attr_as_string(ares->message,
506 if (nextRid == NULL) {
507 ldb_asprintf_errstring(ldb,
508 "Attribute nextRid not found in %s!",
509 ldb_dn_get_linearized(ares->message->dn));
510 ret = LDB_ERR_OPERATIONS_ERROR;
514 ac->next_rid = strtol(nextRid, NULL, 0);
516 ac->domain_dn = ldb_dn_copy(ac, ares->message->dn);
522 case LDB_REPLY_REFERRAL:
530 if (ac->next_rid == 0) {
531 ldb_asprintf_errstring(ldb,
532 "Unable to get nextRid from domain entry!");
533 ret = LDB_ERR_OPERATIONS_ERROR;
538 ret = samldb_next_step(ac);
543 if (ret != LDB_SUCCESS) {
544 return ldb_module_done(ac->req, NULL, NULL, ret);
550 /* Find a domain object in the parents of a particular DN. */
551 static int samldb_get_sid_domain(struct samldb_ctx *ac)
553 struct ldb_context *ldb;
554 static const char * const attrs[] = { "nextRid", NULL };
555 struct ldb_request *req;
559 ldb = ldb_module_get_ctx(ac->module);
561 if (ac->sid == NULL) {
562 return LDB_ERR_OPERATIONS_ERROR;
565 ac->domain_sid = dom_sid_dup(ac, ac->sid);
566 if (!ac->domain_sid) {
567 return LDB_ERR_OPERATIONS_ERROR;
569 /* get the domain component part of the provided SID */
570 ac->domain_sid->num_auths--;
572 filter = talloc_asprintf(ac,
574 "(|(objectClass=domain)"
575 "(objectClass=builtinDomain)))",
576 ldap_encode_ndr_dom_sid(ac, ac->domain_sid));
577 if (filter == NULL) {
578 return LDB_ERR_OPERATIONS_ERROR;
581 ret = ldb_build_search_req(&req, ldb, ac,
582 ldb_get_default_basedn(ldb),
586 ac, samldb_get_sid_domain_callback,
589 if (ret != LDB_SUCCESS) {
594 return ldb_next_request(ac->module, req);
598 * samldb_dn_from_sid (async)
601 static int samldb_dn_from_sid(struct samldb_ctx *ac);
603 static int samldb_dn_from_sid_callback(struct ldb_request *req,
604 struct ldb_reply *ares)
606 struct ldb_context *ldb;
607 struct samldb_ctx *ac;
610 ac = talloc_get_type(req->context, struct samldb_ctx);
611 ldb = ldb_module_get_ctx(ac->module);
614 ret = LDB_ERR_OPERATIONS_ERROR;
617 if (ares->error != LDB_SUCCESS) {
618 return ldb_module_done(ac->req, ares->controls,
619 ares->response, ares->error);
622 switch (ares->type) {
623 case LDB_REPLY_ENTRY:
625 if (ac->res_dn != NULL) {
627 ldb_set_errstring(ldb,
628 "Invalid number of results while searching "
629 "for domain objects!");
630 ret = LDB_ERR_OPERATIONS_ERROR;
633 ac->res_dn = ldb_dn_copy(ac, ares->message->dn);
639 case LDB_REPLY_REFERRAL:
648 /* found or not found, go on */
649 ret = samldb_next_step(ac);
654 if (ret != LDB_SUCCESS) {
655 return ldb_module_done(ac->req, NULL, NULL, ret);
661 /* Finds the DN "res_dn" of an object with a given SID "sid" */
662 static int samldb_dn_from_sid(struct samldb_ctx *ac)
664 struct ldb_context *ldb;
665 static const char * const attrs[] = { NULL };
666 struct ldb_request *req;
670 ldb = ldb_module_get_ctx(ac->module);
673 return LDB_ERR_OPERATIONS_ERROR;
675 filter = talloc_asprintf(ac, "(objectSid=%s)",
676 ldap_encode_ndr_dom_sid(ac, ac->sid));
678 return LDB_ERR_OPERATIONS_ERROR;
680 ret = ldb_build_search_req(&req, ldb, ac,
681 ldb_get_default_basedn(ldb),
685 ac, samldb_dn_from_sid_callback,
687 if (ret != LDB_SUCCESS)
690 return ldb_next_request(ac->module, req);
694 static int samldb_check_primaryGroupID_1(struct samldb_ctx *ac)
696 struct ldb_context *ldb;
699 ldb = ldb_module_get_ctx(ac->module);
701 rid = samdb_result_uint(ac->msg, "primaryGroupID", ~0);
702 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
704 return LDB_ERR_OPERATIONS_ERROR;
707 return samldb_next_step(ac);
710 static int samldb_check_primaryGroupID_2(struct samldb_ctx *ac)
712 if (ac->res_dn == NULL) {
713 struct ldb_context *ldb;
714 ldb = ldb_module_get_ctx(ac->module);
715 ldb_asprintf_errstring(ldb,
716 "Failed to find group sid %s!",
717 dom_sid_string(ac->sid, ac->sid));
718 return LDB_ERR_UNWILLING_TO_PERFORM;
721 return samldb_next_step(ac);
725 static bool samldb_msg_add_sid(struct ldb_message *msg,
727 const struct dom_sid *sid)
730 enum ndr_err_code ndr_err;
732 ndr_err = ndr_push_struct_blob(&v, msg, NULL, sid,
733 (ndr_push_flags_fn_t)ndr_push_dom_sid);
734 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
737 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
740 static int samldb_new_sid(struct samldb_ctx *ac)
743 if (ac->domain_sid == NULL || ac->next_rid == 0) {
744 return LDB_ERR_OPERATIONS_ERROR;
747 ac->sid = dom_sid_add_rid(ac, ac->domain_sid, ac->next_rid + 1);
748 if (ac->sid == NULL) {
749 return LDB_ERR_OPERATIONS_ERROR;
752 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", ac->sid)) {
753 return LDB_ERR_OPERATIONS_ERROR;
756 return samldb_next_step(ac);
760 * samldb_notice_sid_callback (async)
763 static int samldb_notice_sid_callback(struct ldb_request *req,
764 struct ldb_reply *ares)
766 struct ldb_context *ldb;
767 struct samldb_ctx *ac;
770 ac = talloc_get_type(req->context, struct samldb_ctx);
771 ldb = ldb_module_get_ctx(ac->module);
774 ret = LDB_ERR_OPERATIONS_ERROR;
777 if (ares->error != LDB_SUCCESS) {
778 return ldb_module_done(ac->req, ares->controls,
779 ares->response, ares->error);
781 if (ares->type != LDB_REPLY_DONE) {
782 ldb_set_errstring(ldb,
783 "Invalid reply type!");
784 ret = LDB_ERR_OPERATIONS_ERROR;
788 ret = samldb_next_step(ac);
791 if (ret != LDB_SUCCESS) {
792 return ldb_module_done(ac->req, NULL, NULL, ret);
798 /* If we are adding new users/groups, we need to update the nextRid
799 * attribute to be 'above' the new/incoming RID. Attempt to do it
801 static int samldb_notice_sid(struct samldb_ctx *ac)
803 struct ldb_context *ldb;
804 uint32_t old_id, new_id;
805 struct ldb_request *req;
806 struct ldb_message *msg;
807 struct ldb_message_element *els;
808 struct ldb_val *vals;
811 ldb = ldb_module_get_ctx(ac->module);
812 old_id = ac->next_rid;
813 new_id = ac->sid->sub_auths[ac->sid->num_auths - 1];
815 if (old_id >= new_id) {
816 /* no need to update the domain nextRid attribute */
817 return samldb_next_step(ac);
820 /* we do a delete and add as a single operation. That prevents
821 a race, in case we are not actually on a transaction db */
822 msg = ldb_msg_new(ac);
825 return LDB_ERR_OPERATIONS_ERROR;
827 els = talloc_array(msg, struct ldb_message_element, 2);
830 return LDB_ERR_OPERATIONS_ERROR;
832 vals = talloc_array(msg, struct ldb_val, 2);
835 return LDB_ERR_OPERATIONS_ERROR;
837 msg->dn = ac->domain_dn;
838 msg->num_elements = 2;
841 els[0].num_values = 1;
842 els[0].values = &vals[0];
843 els[0].flags = LDB_FLAG_MOD_DELETE;
844 els[0].name = talloc_strdup(msg, "nextRid");
847 return LDB_ERR_OPERATIONS_ERROR;
850 els[1].num_values = 1;
851 els[1].values = &vals[1];
852 els[1].flags = LDB_FLAG_MOD_ADD;
853 els[1].name = els[0].name;
855 vals[0].data = (uint8_t *)talloc_asprintf(vals, "%u", old_id);
858 return LDB_ERR_OPERATIONS_ERROR;
860 vals[0].length = strlen((char *)vals[0].data);
862 vals[1].data = (uint8_t *)talloc_asprintf(vals, "%u", new_id);
865 return LDB_ERR_OPERATIONS_ERROR;
867 vals[1].length = strlen((char *)vals[1].data);
869 ret = ldb_build_mod_req(&req, ldb, ac,
871 ac, samldb_notice_sid_callback,
873 if (ret != LDB_SUCCESS) {
877 return ldb_next_request(ac->module, req);
881 * samldb_set_defaultObjectCategory_callback (async)
884 static int samldb_set_defaultObjectCategory_callback(struct ldb_request *req,
885 struct ldb_reply *ares)
887 struct ldb_context *ldb;
888 struct samldb_ctx *ac;
891 ac = talloc_get_type(req->context, struct samldb_ctx);
892 ldb = ldb_module_get_ctx(ac->module);
895 ret = LDB_ERR_OPERATIONS_ERROR;
898 if (ares->error != LDB_SUCCESS) {
899 return ldb_module_done(ac->req, ares->controls,
900 ares->response, ares->error);
902 if (ares->type != LDB_REPLY_DONE) {
903 ldb_set_errstring(ldb,
904 "Invalid reply type!");
905 ret = LDB_ERR_OPERATIONS_ERROR;
909 ret = samldb_next_step(ac);
912 if (ret != LDB_SUCCESS) {
913 return ldb_module_done(ac->req, NULL, NULL, ret);
919 static int samldb_set_defaultObjectCategory(struct samldb_ctx *ac)
923 struct ldb_request *req;
924 struct ldb_context *ldb;
925 struct ldb_message *msg = ldb_msg_new(ac);
929 ldb_msg_add_empty(msg, "defaultObjectCategory", LDB_FLAG_MOD_REPLACE, NULL);
931 ldb_msg_add_steal_string(msg, "defaultObjectCategory", ldb_dn_alloc_linearized(msg, ac->dn));
933 ldb = ldb_module_get_ctx(ac->module);
935 ret = ldb_build_mod_req(&req, ldb, ac,
937 ac, samldb_set_defaultObjectCategory_callback,
939 if (ret != LDB_SUCCESS) {
943 return ldb_next_request(ac->module, req);
946 ret = samldb_next_step(ac);
947 if (ret != LDB_SUCCESS) {
948 return ldb_module_done(ac->req, NULL, NULL, ret);
954 * samldb_find_for_defaultObjectCategory (async)
957 static int samldb_find_for_defaultObjectCategory_callback(struct ldb_request *req,
958 struct ldb_reply *ares)
960 struct samldb_ctx *ac;
963 ac = talloc_get_type(req->context, struct samldb_ctx);
965 if (ares->error != LDB_SUCCESS) {
966 return ldb_module_done(ac->req, ares->controls,
967 ares->response, ares->error);
970 switch (ares->type) {
971 case LDB_REPLY_ENTRY:
972 ac->dn = talloc_steal(ac, ares->message->dn);
974 case LDB_REPLY_REFERRAL:
975 /* this should not happen */
976 return ldb_module_done(ac->req, NULL, NULL,
977 LDB_ERR_OPERATIONS_ERROR);
980 /* found or not found, go on */
982 ret = samldb_next_step(ac);
983 if (ret != LDB_SUCCESS) {
984 return ldb_module_done(ac->req, NULL, NULL, ret);
992 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
994 struct ldb_context *ldb;
995 struct ldb_request *req;
997 static const char *no_attrs[] = { NULL };
999 ldb = ldb_module_get_ctx(ac->module);
1003 if (ldb_msg_find_element(ac->msg, "defaultObjectCategory") == NULL) {
1004 ret = ldb_build_search_req(&req, ldb, ac,
1005 ac->msg->dn, LDB_SCOPE_BASE,
1006 "objectClass=classSchema", no_attrs,
1008 ac, samldb_find_for_defaultObjectCategory_callback,
1010 if (ret != LDB_SUCCESS) {
1013 ret = dsdb_request_add_controls(ac->module, req, DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT);
1014 if (ret != LDB_SUCCESS) {
1017 return ldb_next_request(ac->module, req);
1020 ret = samldb_next_step(ac);
1021 if (ret != LDB_SUCCESS) {
1022 return ldb_module_done(ac->req, NULL, NULL, ret);
1031 * samldb_add_entry (async)
1034 static int samldb_add_entry_callback(struct ldb_request *req,
1035 struct ldb_reply *ares)
1037 struct ldb_context *ldb;
1038 struct samldb_ctx *ac;
1041 ac = talloc_get_type(req->context, struct samldb_ctx);
1042 ldb = ldb_module_get_ctx(ac->module);
1045 return ldb_module_done(ac->req, NULL, NULL,
1046 LDB_ERR_OPERATIONS_ERROR);
1048 if (ares->error != LDB_SUCCESS) {
1049 return ldb_module_done(ac->req, ares->controls,
1050 ares->response, ares->error);
1052 if (ares->type != LDB_REPLY_DONE) {
1053 ldb_set_errstring(ldb,
1054 "Invalid reply type!\n");
1055 return ldb_module_done(ac->req, NULL, NULL,
1056 LDB_ERR_OPERATIONS_ERROR);
1059 /* The caller may wish to get controls back from the add */
1060 ac->ares = talloc_steal(ac, ares);
1062 ret = samldb_next_step(ac);
1063 if (ret != LDB_SUCCESS) {
1064 return ldb_module_done(ac->req, NULL, NULL, ret);
1069 static int samldb_add_entry(struct samldb_ctx *ac)
1071 struct ldb_context *ldb;
1072 struct ldb_request *req;
1075 ldb = ldb_module_get_ctx(ac->module);
1077 ret = ldb_build_add_req(&req, ldb, ac,
1080 ac, samldb_add_entry_callback,
1082 if (ret != LDB_SUCCESS) {
1086 return ldb_next_request(ac->module, req);
1090 static int samldb_fill_object(struct samldb_ctx *ac, const char *type)
1092 struct ldb_context *ldb;
1093 struct loadparm_context *lp_ctx;
1094 enum sid_generator sid_generator;
1097 ldb = ldb_module_get_ctx(ac->module);
1099 /* search for a parent domain objet */
1100 ac->check_dn = ac->req->op.add.message->dn;
1101 ret = samldb_add_step(ac, samldb_get_parent_domain);
1102 if (ret != LDB_SUCCESS) return ret;
1104 /* Add informations for the different account types */
1106 if (strcmp(ac->type, "user") == 0) {
1107 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1108 "userAccountControl", "546");
1109 if (ret != LDB_SUCCESS) return ret;
1110 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1111 "badPwdCount", "0");
1112 if (ret != LDB_SUCCESS) return ret;
1113 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1115 if (ret != LDB_SUCCESS) return ret;
1116 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1117 "countryCode", "0");
1118 if (ret != LDB_SUCCESS) return ret;
1119 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1120 "badPasswordTime", "0");
1121 if (ret != LDB_SUCCESS) return ret;
1122 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1124 if (ret != LDB_SUCCESS) return ret;
1125 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1127 if (ret != LDB_SUCCESS) return ret;
1128 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1130 if (ret != LDB_SUCCESS) return ret;
1131 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1132 "primaryGroupID", "513");
1133 if (ret != LDB_SUCCESS) return ret;
1134 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1135 "accountExpires", "9223372036854775807");
1136 if (ret != LDB_SUCCESS) return ret;
1137 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1139 if (ret != LDB_SUCCESS) return ret;
1140 } else if (strcmp(ac->type, "group") == 0) {
1141 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1142 "groupType", "-2147483646");
1143 if (ret != LDB_SUCCESS) return ret;
1144 } else if (strcmp(ac->type, "classSchema") == 0) {
1145 const struct ldb_val *rdn_value;
1147 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1149 if (ret != LDB_SUCCESS) return ret;
1151 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
1152 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
1153 /* the RDN has prefix "CN" */
1154 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
1155 samdb_cn_to_lDAPDisplayName(ac,
1156 (const char *) rdn_value->data));
1157 if (ret != LDB_SUCCESS) {
1163 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
1166 guid = GUID_random();
1167 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
1168 if (ret != LDB_SUCCESS) {
1174 ret = samldb_add_step(ac, samldb_add_entry);
1175 if (ret != LDB_SUCCESS) return ret;
1177 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
1178 if (ret != LDB_SUCCESS) return ret;
1180 ret = samldb_add_step(ac, samldb_set_defaultObjectCategory);
1181 if (ret != LDB_SUCCESS) return ret;
1183 return samldb_first_step(ac);
1184 } else if (strcmp(ac->type, "attributeSchema") == 0) {
1185 const struct ldb_val *rdn_value;
1186 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
1187 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
1188 /* the RDN has prefix "CN" */
1189 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
1190 samdb_cn_to_lDAPDisplayName(ac,
1191 (const char *) rdn_value->data));
1192 if (ret != LDB_SUCCESS) {
1198 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1199 "isSingleValued", "FALSE");
1200 if (ret != LDB_SUCCESS) return ret;
1202 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
1205 guid = GUID_random();
1206 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
1207 if (ret != LDB_SUCCESS) {
1213 ret = samldb_add_step(ac, samldb_add_entry);
1214 if (ret != LDB_SUCCESS) return ret;
1216 return samldb_first_step(ac);
1218 ldb_asprintf_errstring(ldb,
1219 "Invalid entry type!");
1220 return LDB_ERR_OPERATIONS_ERROR;
1223 /* check if we have a valid samAccountName */
1224 ret = samldb_add_step(ac, samldb_check_samAccountName);
1225 if (ret != LDB_SUCCESS) return ret;
1227 /* check account_type/group_type */
1228 ret = samldb_add_step(ac, samldb_check_samAccountType);
1229 if (ret != LDB_SUCCESS) return ret;
1231 /* check if we have a valid primary group ID */
1232 if (strcmp(ac->type, "user") == 0) {
1233 ret = samldb_add_step(ac, samldb_check_primaryGroupID_1);
1234 if (ret != LDB_SUCCESS) return ret;
1235 ret = samldb_add_step(ac, samldb_dn_from_sid);
1236 if (ret != LDB_SUCCESS) return ret;
1237 ret = samldb_add_step(ac, samldb_check_primaryGroupID_2);
1238 if (ret != LDB_SUCCESS) return ret;
1241 lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
1242 struct loadparm_context);
1244 sid_generator = lp_sid_generator(lp_ctx);
1245 if (sid_generator == SID_GENERATOR_INTERNAL) {
1246 /* check if we have a valid SID */
1247 ac->sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
1249 ret = samldb_add_step(ac, samldb_new_sid);
1250 if (ret != LDB_SUCCESS) return ret;
1252 ret = samldb_add_step(ac, samldb_get_sid_domain);
1253 if (ret != LDB_SUCCESS) return ret;
1256 ret = samldb_add_step(ac, samldb_notice_sid);
1257 if (ret != LDB_SUCCESS) return ret;
1260 /* finally proceed with adding the entry */
1261 ret = samldb_add_step(ac, samldb_add_entry);
1262 if (ret != LDB_SUCCESS) return ret;
1264 return samldb_first_step(ac);
1268 * samldb_foreign_notice_sid (async)
1271 static int samldb_foreign_notice_sid_callback(struct ldb_request *req,
1272 struct ldb_reply *ares)
1274 struct ldb_context *ldb;
1275 struct samldb_ctx *ac;
1276 const char *nextRid;
1280 ac = talloc_get_type(req->context, struct samldb_ctx);
1281 ldb = ldb_module_get_ctx(ac->module);
1284 ret = LDB_ERR_OPERATIONS_ERROR;
1287 if (ares->error != LDB_SUCCESS) {
1288 return ldb_module_done(ac->req, ares->controls,
1289 ares->response, ares->error);
1292 switch (ares->type) {
1293 case LDB_REPLY_ENTRY:
1295 if (ac->next_rid != 0) {
1297 ldb_set_errstring(ldb,
1298 "Invalid number of results while searching "
1299 "for domain object!");
1300 ret = LDB_ERR_OPERATIONS_ERROR;
1304 nextRid = ldb_msg_find_attr_as_string(ares->message,
1306 if (nextRid == NULL) {
1307 ldb_asprintf_errstring(ldb,
1308 "While looking for foreign SID %s attribute nextRid not found in %s",
1309 dom_sid_string(ares, ac->sid),
1310 ldb_dn_get_linearized(ares->message->dn));
1311 ret = LDB_ERR_OPERATIONS_ERROR;
1315 ac->next_rid = strtol(nextRid, NULL, 0);
1317 ac->domain_dn = ldb_dn_copy(ac, ares->message->dn);
1319 name = samdb_result_string(ares->message, "name", NULL);
1320 ldb_debug(ldb, LDB_DEBUG_TRACE,
1321 "NOTE (strange but valid): Adding foreign SID "
1322 "record with SID %s, but this domain (%s) is "
1323 "not foreign in the database\n",
1324 dom_sid_string(ares, ac->sid), name);
1330 case LDB_REPLY_REFERRAL:
1336 case LDB_REPLY_DONE:
1339 /* if this is a fake foreign SID, notice the SID */
1340 if (ac->domain_dn) {
1341 ret = samldb_notice_sid(ac);
1346 ret = samldb_next_step(ac);
1351 if (ret != LDB_SUCCESS) {
1352 return ldb_module_done(ac->req, NULL, NULL, ret);
1358 /* Find a domain object in the parents of a particular DN. */
1359 static int samldb_foreign_notice_sid(struct samldb_ctx *ac)
1361 struct ldb_context *ldb;
1362 static const char * const attrs[3] = { "nextRid", "name", NULL };
1363 struct ldb_request *req;
1368 ldb = ldb_module_get_ctx(ac->module);
1370 if (ac->sid == NULL) {
1371 return LDB_ERR_OPERATIONS_ERROR;
1374 status = dom_sid_split_rid(ac, ac->sid, &ac->domain_sid, NULL);
1375 if (!NT_STATUS_IS_OK(status)) {
1376 return LDB_ERR_OPERATIONS_ERROR;
1380 filter = talloc_asprintf(ac,
1382 "(|(objectClass=domain)"
1383 "(objectClass=builtinDomain)))",
1384 ldap_encode_ndr_dom_sid(ac, ac->domain_sid));
1385 if (filter == NULL) {
1386 return LDB_ERR_OPERATIONS_ERROR;
1389 ret = ldb_build_search_req(&req, ldb, ac,
1390 ldb_get_default_basedn(ldb),
1394 ac, samldb_foreign_notice_sid_callback,
1397 if (ret != LDB_SUCCESS) {
1401 return ldb_next_request(ac->module, req);
1405 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
1407 struct ldb_context *ldb;
1410 ldb = ldb_module_get_ctx(ac->module);
1414 ac->sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
1415 if (ac->sid == NULL) {
1416 ac->sid = dom_sid_parse_talloc(ac->msg,
1417 (const char *)ldb_dn_get_rdn_val(ac->msg->dn)->data);
1419 ldb_set_errstring(ldb,
1420 "No valid SID found in "
1421 "ForeignSecurityPrincipal CN!");
1423 return LDB_ERR_CONSTRAINT_VIOLATION;
1425 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", ac->sid)) {
1427 return LDB_ERR_OPERATIONS_ERROR;
1431 /* check if we need to notice this SID */
1432 ret = samldb_add_step(ac, samldb_foreign_notice_sid);
1433 if (ret != LDB_SUCCESS) return ret;
1435 /* finally proceed with adding the entry */
1436 ret = samldb_add_step(ac, samldb_add_entry);
1437 if (ret != LDB_SUCCESS) return ret;
1439 return samldb_first_step(ac);
1442 static int samldb_check_rdn(struct ldb_module *module, struct ldb_dn *dn)
1444 struct ldb_context *ldb;
1445 const char *rdn_name;
1447 ldb = ldb_module_get_ctx(module);
1448 rdn_name = ldb_dn_get_rdn_name(dn);
1450 if (strcasecmp(rdn_name, "cn") != 0) {
1451 ldb_asprintf_errstring(ldb,
1452 "Bad RDN (%s=) for samldb object, "
1453 "should be CN=!", rdn_name);
1454 return LDB_ERR_CONSTRAINT_VIOLATION;
1461 * samldb_sid_from_dn (async)
1464 static int samldb_sid_from_dn(struct samldb_ctx *ac);
1466 static int samldb_sid_from_dn_callback(struct ldb_request *req,
1467 struct ldb_reply *ares)
1469 struct ldb_context *ldb;
1470 struct samldb_ctx *ac;
1473 ac = talloc_get_type(req->context, struct samldb_ctx);
1474 ldb = ldb_module_get_ctx(ac->module);
1477 ret = LDB_ERR_OPERATIONS_ERROR;
1480 if (ares->error != LDB_SUCCESS) {
1481 return ldb_module_done(ac->req, ares->controls,
1482 ares->response, ares->error);
1485 switch (ares->type) {
1486 case LDB_REPLY_ENTRY:
1488 if (ac->res_sid != NULL) {
1490 ldb_set_errstring(ldb,
1491 "Invalid number of results while searching "
1492 "for domain objects!");
1493 ret = LDB_ERR_OPERATIONS_ERROR;
1496 ac->res_sid = samdb_result_dom_sid(ac, ares->message,
1503 case LDB_REPLY_REFERRAL:
1509 case LDB_REPLY_DONE:
1512 /* found or not found, go on */
1513 ret = samldb_next_step(ac);
1518 if (ret != LDB_SUCCESS) {
1519 return ldb_module_done(ac->req, NULL, NULL, ret);
1525 /* Finds the SID "res_sid" of an object with a given DN "dn" */
1526 static int samldb_sid_from_dn(struct samldb_ctx *ac)
1528 struct ldb_context *ldb;
1529 static const char * const attrs[] = { "objectSid", NULL };
1530 struct ldb_request *req;
1533 ldb = ldb_module_get_ctx(ac->module);
1536 return LDB_ERR_OPERATIONS_ERROR;
1538 ret = ldb_build_search_req(&req, ldb, ac,
1543 ac, samldb_sid_from_dn_callback,
1545 if (ret != LDB_SUCCESS)
1548 return ldb_next_request(ac->module, req);
1552 * samldb_user_dn_to_prim_group_rid (async)
1555 static int samldb_user_dn_to_prim_group_rid(struct samldb_ctx *ac);
1557 static int samldb_user_dn_to_prim_group_rid_callback(struct ldb_request *req,
1558 struct ldb_reply *ares)
1560 struct ldb_context *ldb;
1561 struct samldb_ctx *ac;
1564 ac = talloc_get_type(req->context, struct samldb_ctx);
1565 ldb = ldb_module_get_ctx(ac->module);
1568 ret = LDB_ERR_OPERATIONS_ERROR;
1571 if (ares->error != LDB_SUCCESS) {
1572 return ldb_module_done(ac->req, ares->controls,
1573 ares->response, ares->error);
1576 switch (ares->type) {
1577 case LDB_REPLY_ENTRY:
1579 if (ac->prim_group_rid != 0) {
1581 ldb_set_errstring(ldb,
1582 "Invalid number of results while searching "
1583 "for domain objects!");
1584 ret = LDB_ERR_OPERATIONS_ERROR;
1587 ac->prim_group_rid = samdb_result_uint(ares->message,
1588 "primaryGroupID", ~0);
1594 case LDB_REPLY_REFERRAL:
1600 case LDB_REPLY_DONE:
1602 if (ac->prim_group_rid == 0) {
1603 ldb_asprintf_errstring(ldb,
1604 "Unable to get the primary group RID!");
1605 ret = LDB_ERR_OPERATIONS_ERROR;
1610 ret = samldb_next_step(ac);
1615 if (ret != LDB_SUCCESS) {
1616 return ldb_module_done(ac->req, NULL, NULL, ret);
1622 /* Locates the "primaryGroupID" attribute from a certain user specified as
1623 * "user_dn". Saves the result in "prim_group_rid". */
1624 static int samldb_user_dn_to_prim_group_rid(struct samldb_ctx *ac)
1626 struct ldb_context *ldb;
1627 static const char * const attrs[] = { "primaryGroupID", NULL };
1628 struct ldb_request *req;
1631 ldb = ldb_module_get_ctx(ac->module);
1633 if (ac->user_dn == NULL)
1634 return LDB_ERR_OPERATIONS_ERROR;
1636 ret = ldb_build_search_req(&req, ldb, ac,
1641 ac, samldb_user_dn_to_prim_group_rid_callback,
1643 if (ret != LDB_SUCCESS)
1646 return ldb_next_request(ac->module, req);
1650 * samldb_prim_group_rid_to_users_cnt (async)
1653 static int samldb_prim_group_rid_to_users_cnt(struct samldb_ctx *ac);
1655 static int samldb_prim_group_rid_to_users_cnt_callback(struct ldb_request *req,
1656 struct ldb_reply *ares)
1658 struct ldb_context *ldb;
1659 struct samldb_ctx *ac;
1662 ac = talloc_get_type(req->context, struct samldb_ctx);
1663 ldb = ldb_module_get_ctx(ac->module);
1666 ret = LDB_ERR_OPERATIONS_ERROR;
1669 if (ares->error != LDB_SUCCESS) {
1670 return ldb_module_done(ac->req, ares->controls,
1671 ares->response, ares->error);
1674 switch (ares->type) {
1675 case LDB_REPLY_ENTRY:
1683 case LDB_REPLY_REFERRAL:
1689 case LDB_REPLY_DONE:
1692 /* found or not found, go on */
1693 ret = samldb_next_step(ac);
1698 if (ret != LDB_SUCCESS) {
1699 return ldb_module_done(ac->req, NULL, NULL, ret);
1705 /* Finds the amount of users which have the primary group "prim_group_rid" and
1706 * save the result in "users_cnt" */
1707 static int samldb_prim_group_rid_to_users_cnt(struct samldb_ctx *ac)
1709 struct ldb_context *ldb;
1710 static const char * const attrs[] = { NULL };
1711 struct ldb_request *req;
1715 ldb = ldb_module_get_ctx(ac->module);
1717 if ((ac->prim_group_rid == 0) || (ac->users_cnt != 0))
1718 return LDB_ERR_OPERATIONS_ERROR;
1720 filter = talloc_asprintf(ac, "(&(primaryGroupID=%u)(objectclass=user))",
1721 ac->prim_group_rid);
1723 return LDB_ERR_OPERATIONS_ERROR;
1725 ret = ldb_build_search_req(&req, ldb, ac,
1726 ldb_get_default_basedn(ldb),
1731 samldb_prim_group_rid_to_users_cnt_callback,
1733 if (ret != LDB_SUCCESS)
1736 return ldb_next_request(ac->module, req);
1740 * samldb_group_add_member (async)
1741 * samldb_group_del_member (async)
1744 static int samldb_group_add_del_member_callback(struct ldb_request *req,
1745 struct ldb_reply *ares)
1747 struct ldb_context *ldb;
1748 struct samldb_ctx *ac;
1751 ac = talloc_get_type(req->context, struct samldb_ctx);
1752 ldb = ldb_module_get_ctx(ac->module);
1755 ret = LDB_ERR_OPERATIONS_ERROR;
1758 if (ares->error != LDB_SUCCESS) {
1759 if (ares->error == LDB_ERR_NO_SUCH_ATTRIBUTE) {
1760 /* On error "NO_SUCH_ATTRIBUTE" (delete of an invalid
1761 * "member" attribute) return "UNWILLING_TO_PERFORM" */
1762 ares->error = LDB_ERR_UNWILLING_TO_PERFORM;
1764 return ldb_module_done(ac->req, ares->controls,
1765 ares->response, ares->error);
1767 if (ares->type != LDB_REPLY_DONE) {
1768 ldb_set_errstring(ldb,
1769 "Invalid reply type!");
1770 ret = LDB_ERR_OPERATIONS_ERROR;
1774 ret = samldb_next_step(ac);
1777 if (ret != LDB_SUCCESS) {
1778 return ldb_module_done(ac->req, NULL, NULL, ret);
1784 /* Adds a member with DN "member_dn" to a group with DN "group_dn" */
1785 static int samldb_group_add_member(struct samldb_ctx *ac)
1787 struct ldb_context *ldb;
1788 struct ldb_request *req;
1789 struct ldb_message *msg;
1792 ldb = ldb_module_get_ctx(ac->module);
1794 if ((ac->group_dn == NULL) || (ac->member_dn == NULL))
1795 return LDB_ERR_OPERATIONS_ERROR;
1797 msg = ldb_msg_new(ac);
1798 msg->dn = ac->group_dn;
1799 samdb_msg_add_addval(ldb, ac, msg, "member",
1800 ldb_dn_get_linearized(ac->member_dn));
1802 ret = ldb_build_mod_req(&req, ldb, ac,
1804 ac, samldb_group_add_del_member_callback,
1806 if (ret != LDB_SUCCESS)
1809 return ldb_next_request(ac->module, req);
1812 /* Removes a member with DN "member_dn" from a group with DN "group_dn" */
1813 static int samldb_group_del_member(struct samldb_ctx *ac)
1815 struct ldb_context *ldb;
1816 struct ldb_request *req;
1817 struct ldb_message *msg;
1820 ldb = ldb_module_get_ctx(ac->module);
1822 if ((ac->group_dn == NULL) || (ac->member_dn == NULL))
1823 return LDB_ERR_OPERATIONS_ERROR;
1825 msg = ldb_msg_new(ac);
1826 msg->dn = ac->group_dn;
1827 samdb_msg_add_delval(ldb, ac, msg, "member",
1828 ldb_dn_get_linearized(ac->member_dn));
1830 ret = ldb_build_mod_req(&req, ldb, ac,
1832 ac, samldb_group_add_del_member_callback,
1834 if (ret != LDB_SUCCESS)
1837 return ldb_next_request(ac->module, req);
1841 static int samldb_prim_group_change_1(struct samldb_ctx *ac)
1843 struct ldb_context *ldb;
1846 ldb = ldb_module_get_ctx(ac->module);
1848 ac->user_dn = ac->msg->dn;
1850 rid = samdb_result_uint(ac->msg, "primaryGroupID", ~0);
1851 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1852 if (ac->sid == NULL)
1853 return LDB_ERR_OPERATIONS_ERROR;
1856 ac->prim_group_rid = 0;
1858 return samldb_next_step(ac);
1861 static int samldb_prim_group_change_2(struct samldb_ctx *ac)
1863 struct ldb_context *ldb;
1865 ldb = ldb_module_get_ctx(ac->module);
1867 if (ac->res_dn != NULL)
1868 ac->new_prim_group_dn = ac->res_dn;
1870 return LDB_ERR_UNWILLING_TO_PERFORM;
1872 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1873 ac->prim_group_rid);
1874 if (ac->sid == NULL)
1875 return LDB_ERR_OPERATIONS_ERROR;
1878 return samldb_next_step(ac);
1881 static int samldb_prim_group_change_4(struct samldb_ctx *ac);
1882 static int samldb_prim_group_change_5(struct samldb_ctx *ac);
1883 static int samldb_prim_group_change_6(struct samldb_ctx *ac);
1885 static int samldb_prim_group_change_3(struct samldb_ctx *ac)
1889 if (ac->res_dn != NULL)
1890 ac->old_prim_group_dn = ac->res_dn;
1892 return LDB_ERR_UNWILLING_TO_PERFORM;
1894 /* Only update when the primary group changed */
1895 if (ldb_dn_compare(ac->old_prim_group_dn, ac->new_prim_group_dn) != 0) {
1896 ac->member_dn = ac->user_dn;
1897 /* Remove the "member" attribute of the actual (new) primary
1900 ret = samldb_add_step(ac, samldb_prim_group_change_4);
1901 if (ret != LDB_SUCCESS) return ret;
1903 ret = samldb_add_step(ac, samldb_group_del_member);
1904 if (ret != LDB_SUCCESS) return ret;
1906 /* Add a "member" attribute for the previous primary group */
1908 ret = samldb_add_step(ac, samldb_prim_group_change_5);
1909 if (ret != LDB_SUCCESS) return ret;
1911 ret = samldb_add_step(ac, samldb_group_add_member);
1912 if (ret != LDB_SUCCESS) return ret;
1915 ret = samldb_add_step(ac, samldb_prim_group_change_6);
1916 if (ret != LDB_SUCCESS) return ret;
1918 return samldb_next_step(ac);
1921 static int samldb_prim_group_change_4(struct samldb_ctx *ac)
1923 ac->group_dn = ac->new_prim_group_dn;
1925 return samldb_next_step(ac);
1928 static int samldb_prim_group_change_5(struct samldb_ctx *ac)
1930 ac->group_dn = ac->old_prim_group_dn;
1932 return samldb_next_step(ac);
1935 static int samldb_prim_group_change_6(struct samldb_ctx *ac)
1937 return ldb_next_request(ac->module, ac->req);
1940 static int samldb_prim_group_change(struct samldb_ctx *ac)
1944 /* Finds out the DN of the new primary group */
1946 ret = samldb_add_step(ac, samldb_prim_group_change_1);
1947 if (ret != LDB_SUCCESS) return ret;
1949 ret = samldb_add_step(ac, samldb_dn_from_sid);
1950 if (ret != LDB_SUCCESS) return ret;
1952 ret = samldb_add_step(ac, samldb_user_dn_to_prim_group_rid);
1953 if (ret != LDB_SUCCESS) return ret;
1955 /* Finds out the DN of the old primary group */
1957 ret = samldb_add_step(ac, samldb_prim_group_change_2);
1958 if (ret != LDB_SUCCESS) return ret;
1960 ret = samldb_add_step(ac, samldb_dn_from_sid);
1961 if (ret != LDB_SUCCESS) return ret;
1963 ret = samldb_add_step(ac, samldb_prim_group_change_3);
1964 if (ret != LDB_SUCCESS) return ret;
1966 return samldb_first_step(ac);
1970 static int samldb_member_check_1(struct samldb_ctx *ac)
1972 struct ldb_context *ldb;
1973 struct ldb_message_element *el;
1975 ldb = ldb_module_get_ctx(ac->module);
1977 el = ldb_msg_find_element(ac->msg, "member");
1979 ac->user_dn = ldb_dn_from_ldb_val(ac, ldb, &el->values[ac->cnt]);
1980 if (!ldb_dn_validate(ac->user_dn))
1981 return LDB_ERR_OPERATIONS_ERROR;
1982 ac->prim_group_rid = 0;
1984 return samldb_next_step(ac);
1987 static int samldb_member_check_2(struct samldb_ctx *ac)
1989 struct ldb_context *ldb;
1991 ldb = ldb_module_get_ctx(ac->module);
1993 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1994 ac->prim_group_rid);
1995 if (ac->sid == NULL)
1996 return LDB_ERR_OPERATIONS_ERROR;
1999 return samldb_next_step(ac);
2002 static int samldb_member_check_3(struct samldb_ctx *ac)
2004 if (ldb_dn_compare(ac->res_dn, ac->msg->dn) == 0)
2005 return LDB_ERR_ENTRY_ALREADY_EXISTS;
2009 return samldb_next_step(ac);
2012 static int samldb_member_check_4(struct samldb_ctx *ac)
2014 return ldb_next_request(ac->module, ac->req);
2017 static int samldb_member_check(struct samldb_ctx *ac)
2019 struct ldb_message_element *el;
2022 el = ldb_msg_find_element(ac->msg, "member");
2024 for (i = 0; i < el->num_values; i++) {
2025 /* Denies to add "member"s to groups which are primary ones
2027 ret = samldb_add_step(ac, samldb_member_check_1);
2028 if (ret != LDB_SUCCESS) return ret;
2030 ret = samldb_add_step(ac, samldb_user_dn_to_prim_group_rid);
2031 if (ret != LDB_SUCCESS) return ret;
2033 ret = samldb_add_step(ac, samldb_member_check_2);
2034 if (ret != LDB_SUCCESS) return ret;
2036 ret = samldb_add_step(ac, samldb_dn_from_sid);
2037 if (ret != LDB_SUCCESS) return ret;
2039 ret = samldb_add_step(ac, samldb_member_check_3);
2040 if (ret != LDB_SUCCESS) return ret;
2043 ret = samldb_add_step(ac, samldb_member_check_4);
2044 if (ret != LDB_SUCCESS) return ret;
2046 return samldb_first_step(ac);
2050 static int samldb_prim_group_users_check_1(struct samldb_ctx *ac)
2052 ac->dn = ac->req->op.del.dn;
2055 return samldb_next_step(ac);
2058 static int samldb_prim_group_users_check_2(struct samldb_ctx *ac)
2063 if (ac->res_sid == NULL) {
2064 /* No SID - therefore ok here */
2065 return ldb_next_request(ac->module, ac->req);
2067 status = dom_sid_split_rid(ac, ac->res_sid, NULL, &rid);
2068 if (!NT_STATUS_IS_OK(status))
2069 return LDB_ERR_OPERATIONS_ERROR;
2072 /* Special object (security principal?) */
2073 return ldb_next_request(ac->module, ac->req);
2076 ac->prim_group_rid = rid;
2079 return samldb_next_step(ac);
2082 static int samldb_prim_group_users_check_3(struct samldb_ctx *ac)
2084 if (ac->users_cnt > 0)
2085 return LDB_ERR_ENTRY_ALREADY_EXISTS;
2087 return ldb_next_request(ac->module, ac->req);
2090 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
2094 /* Finds out the SID/RID of the domain object */
2096 ret = samldb_add_step(ac, samldb_prim_group_users_check_1);
2097 if (ret != LDB_SUCCESS) return ret;
2099 ret = samldb_add_step(ac, samldb_sid_from_dn);
2100 if (ret != LDB_SUCCESS) return ret;
2102 /* Deny delete requests from groups which are primary ones */
2104 ret = samldb_add_step(ac, samldb_prim_group_users_check_2);
2105 if (ret != LDB_SUCCESS) return ret;
2107 ret = samldb_add_step(ac, samldb_prim_group_rid_to_users_cnt);
2108 if (ret != LDB_SUCCESS) return ret;
2110 ret = samldb_add_step(ac, samldb_prim_group_users_check_3);
2111 if (ret != LDB_SUCCESS) return ret;
2113 return samldb_first_step(ac);
2118 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
2120 struct ldb_context *ldb;
2121 struct samldb_ctx *ac;
2124 ldb = ldb_module_get_ctx(module);
2125 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
2127 /* do not manipulate our control entries */
2128 if (ldb_dn_is_special(req->op.add.message->dn)) {
2129 return ldb_next_request(module, req);
2132 ac = samldb_ctx_init(module, req);
2134 return LDB_ERR_OPERATIONS_ERROR;
2137 /* build the new msg */
2138 ac->msg = ldb_msg_copy(ac, ac->req->op.add.message);
2141 ldb_debug(ldb, LDB_DEBUG_FATAL,
2142 "samldb_add: ldb_msg_copy failed!\n");
2143 return LDB_ERR_OPERATIONS_ERROR;
2146 if (samdb_find_attribute(ldb, ac->msg,
2147 "objectclass", "computer") != NULL) {
2149 /* make sure the computer object also has the 'user'
2150 * objectclass so it will be handled by the next call */
2151 ret = samdb_find_or_add_value(ldb, ac->msg,
2152 "objectclass", "user");
2153 if (ret != LDB_SUCCESS) {
2159 if (samdb_find_attribute(ldb, ac->msg,
2160 "objectclass", "user") != NULL) {
2162 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
2163 if (ret != LDB_SUCCESS) {
2168 return samldb_fill_object(ac, "user");
2171 if (samdb_find_attribute(ldb, ac->msg,
2172 "objectclass", "group") != NULL) {
2174 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
2175 if (ret != LDB_SUCCESS) {
2180 return samldb_fill_object(ac, "group");
2183 /* perhaps a foreignSecurityPrincipal? */
2184 if (samdb_find_attribute(ldb, ac->msg,
2186 "foreignSecurityPrincipal") != NULL) {
2188 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
2189 if (ret != LDB_SUCCESS) {
2194 return samldb_fill_foreignSecurityPrincipal_object(ac);
2197 if (samdb_find_attribute(ldb, ac->msg,
2198 "objectclass", "classSchema") != NULL) {
2200 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
2201 if (ret != LDB_SUCCESS) {
2206 return samldb_fill_object(ac, "classSchema");
2209 if (samdb_find_attribute(ldb, ac->msg,
2210 "objectclass", "attributeSchema") != NULL) {
2212 ret = samldb_check_rdn(module, ac->req->op.add.message->dn);
2213 if (ret != LDB_SUCCESS) {
2218 return samldb_fill_object(ac, "attributeSchema");
2223 /* nothing matched, go on */
2224 return ldb_next_request(module, req);
2228 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
2230 struct ldb_context *ldb;
2231 struct ldb_message *msg;
2232 struct ldb_message_element *el, *el2;
2234 uint32_t account_type;
2236 if (ldb_dn_is_special(req->op.mod.message->dn)) {
2237 /* do not manipulate our control entries */
2238 return ldb_next_request(module, req);
2241 ldb = ldb_module_get_ctx(module);
2243 if (ldb_msg_find_element(req->op.mod.message, "sAMAccountType") != NULL) {
2244 ldb_asprintf_errstring(ldb,
2245 "sAMAccountType must not be specified!");
2246 return LDB_ERR_UNWILLING_TO_PERFORM;
2249 /* TODO: do not modify original request, create a new one */
2251 el = ldb_msg_find_element(req->op.mod.message, "groupType");
2252 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
2253 uint32_t group_type;
2255 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
2256 req->op.mod.message);
2258 group_type = strtoul((const char *)el->values[0].data, NULL, 0);
2259 account_type = ds_gtype2atype(group_type);
2260 ret = samdb_msg_add_uint(ldb, msg, msg,
2263 if (ret != LDB_SUCCESS) {
2266 el2 = ldb_msg_find_element(msg, "sAMAccountType");
2267 el2->flags = LDB_FLAG_MOD_REPLACE;
2270 el = ldb_msg_find_element(req->op.mod.message, "userAccountControl");
2271 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
2272 uint32_t user_account_control;
2274 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
2275 req->op.mod.message);
2277 user_account_control = strtoul((const char *)el->values[0].data,
2279 account_type = ds_uf2atype(user_account_control);
2280 ret = samdb_msg_add_uint(ldb, msg, msg,
2283 if (ret != LDB_SUCCESS) {
2286 el2 = ldb_msg_find_element(msg, "sAMAccountType");
2287 el2->flags = LDB_FLAG_MOD_REPLACE;
2289 if (user_account_control & UF_SERVER_TRUST_ACCOUNT) {
2290 ret = samdb_msg_add_string(ldb, msg, msg,
2291 "isCriticalSystemObject", "TRUE");
2292 if (ret != LDB_SUCCESS) {
2295 el2 = ldb_msg_find_element(msg, "isCriticalSystemObject");
2296 el2->flags = LDB_FLAG_MOD_REPLACE;
2300 el = ldb_msg_find_element(req->op.mod.message, "primaryGroupID");
2301 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
2302 struct samldb_ctx *ac;
2304 ac = samldb_ctx_init(module, req);
2306 return LDB_ERR_OPERATIONS_ERROR;
2308 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
2309 req->op.mod.message);
2311 return samldb_prim_group_change(ac);
2314 el = ldb_msg_find_element(req->op.mod.message, "member");
2315 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
2316 struct samldb_ctx *ac;
2318 ac = samldb_ctx_init(module, req);
2320 return LDB_ERR_OPERATIONS_ERROR;
2322 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
2323 req->op.mod.message);
2325 return samldb_member_check(ac);
2328 /* nothing matched, go on */
2329 return ldb_next_request(module, req);
2333 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
2335 struct samldb_ctx *ac;
2337 if (ldb_dn_is_special(req->op.del.dn)) {
2338 /* do not manipulate our control entries */
2339 return ldb_next_request(module, req);
2342 ac = samldb_ctx_init(module, req);
2344 return LDB_ERR_OPERATIONS_ERROR;
2346 return samldb_prim_group_users_check(ac);
2350 static int samldb_init(struct ldb_module *module)
2352 return ldb_next_init(module);
2355 _PUBLIC_ const struct ldb_module_ops ldb_samldb_module_ops = {
2357 .init_context = samldb_init,
2359 .modify = samldb_modify,
2360 .del = samldb_delete