4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
5 Copyright (C) Simo Sorce 2004-2008
6 Copyright (C) Matthias Dieter Wallnöfer 2009-2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * Component: ldb samldb module
27 * Description: add embedded user/group creation functionality
33 #include "libcli/ldap/ldap_ndr.h"
34 #include "ldb_module.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "dsdb/samdb/ldb_modules/util.h"
37 #include "libcli/security/security.h"
38 #include "librpc/gen_ndr/ndr_security.h"
39 #include "../lib/util/util_ldb.h"
41 #include "param/param.h"
45 typedef int (*samldb_step_fn_t)(struct samldb_ctx *);
48 struct samldb_step *next;
53 struct ldb_module *module;
54 struct ldb_request *req;
56 /* used for add operations */
59 /* the resulting message */
60 struct ldb_message *msg;
62 /* holds a generic dn */
65 /* used in "samldb_find_for_defaultObjectCategory" */
66 struct ldb_dn *res_dn;
68 /* all the async steps necessary to complete the operation */
69 struct samldb_step *steps;
70 struct samldb_step *curstep;
72 /* If someone set an ares to forward controls and response back to the caller */
73 struct ldb_reply *ares;
76 static struct samldb_ctx *samldb_ctx_init(struct ldb_module *module,
77 struct ldb_request *req)
79 struct ldb_context *ldb;
80 struct samldb_ctx *ac;
82 ldb = ldb_module_get_ctx(module);
84 ac = talloc_zero(req, struct samldb_ctx);
96 static int samldb_add_step(struct samldb_ctx *ac, samldb_step_fn_t fn)
98 struct samldb_step *step, *stepper;
100 step = talloc_zero(ac, struct samldb_step);
102 return ldb_oom(ldb_module_get_ctx(ac->module));
107 if (ac->steps == NULL) {
111 if (ac->curstep == NULL)
112 return ldb_operr(ldb_module_get_ctx(ac->module));
113 for (stepper = ac->curstep; stepper->next != NULL;
114 stepper = stepper->next);
115 stepper->next = step;
121 static int samldb_first_step(struct samldb_ctx *ac)
123 if (ac->steps == NULL) {
124 return ldb_operr(ldb_module_get_ctx(ac->module));
127 ac->curstep = ac->steps;
128 return ac->curstep->fn(ac);
131 static int samldb_next_step(struct samldb_ctx *ac)
133 if (ac->curstep->next) {
134 ac->curstep = ac->curstep->next;
135 return ac->curstep->fn(ac);
138 /* we exit the samldb module here */
139 /* If someone set an ares to forward controls and response back to the caller, use them */
141 return ldb_module_done(ac->req, ac->ares->controls,
142 ac->ares->response, LDB_SUCCESS);
144 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
149 /* sAMAccountName handling */
151 static int samldb_generate_sAMAccountName(struct ldb_context *ldb, struct ldb_message *msg)
155 /* Format: $000000-000000000000 */
157 name = talloc_asprintf(msg, "$%.6X-%.6X%.6X",
158 (unsigned int)generate_random(),
159 (unsigned int)generate_random(),
160 (unsigned int)generate_random());
164 return ldb_msg_add_steal_string(msg, "sAMAccountName", name);
167 static int samldb_check_sAMAccountName(struct samldb_ctx *ac)
169 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
173 if (ldb_msg_find_element(ac->msg, "sAMAccountName") == NULL) {
174 ret = samldb_generate_sAMAccountName(ldb, ac->msg);
175 if (ret != LDB_SUCCESS) {
180 name = ldb_msg_find_attr_as_string(ac->msg, "sAMAccountName", NULL);
182 return ldb_operr(ldb);
185 ret = samdb_search_count(ldb, NULL, "(sAMAccountName=%s)",
186 ldb_binary_encode_string(ac, name));
187 if ((ret < 0) || (ret > 1)) {
188 return ldb_operr(ldb);
191 return LDB_ERR_ENTRY_ALREADY_EXISTS;
194 return samldb_next_step(ac);
197 /* sAMAccountType handling */
199 static int samldb_check_sAMAccountType(struct samldb_ctx *ac)
201 struct ldb_context *ldb;
202 unsigned int account_type;
203 unsigned int group_type;
207 ldb = ldb_module_get_ctx(ac->module);
209 /* make sure sAMAccountType is not specified */
210 if (ldb_msg_find_element(ac->msg, "sAMAccountType") != NULL) {
211 ldb_asprintf_errstring(ldb,
212 "sAMAccountType must not be specified!");
213 return LDB_ERR_UNWILLING_TO_PERFORM;
216 if (strcmp("user", ac->type) == 0) {
217 uac = samdb_result_uint(ac->msg, "userAccountControl", 0);
219 ldb_asprintf_errstring(ldb,
220 "userAccountControl invalid!");
221 return LDB_ERR_UNWILLING_TO_PERFORM;
223 account_type = ds_uf2atype(uac);
224 ret = samdb_msg_add_uint(ldb,
228 if (ret != LDB_SUCCESS) {
232 } else if (strcmp("group", ac->type) == 0) {
233 group_type = samdb_result_uint(ac->msg, "groupType", 0);
234 if (group_type == 0) {
235 ldb_asprintf_errstring(ldb,
236 "groupType invalid!\n");
237 return LDB_ERR_UNWILLING_TO_PERFORM;
239 account_type = ds_gtype2atype(group_type);
240 ret = samdb_msg_add_uint(ldb,
244 if (ret != LDB_SUCCESS) {
250 return samldb_next_step(ac);
253 static bool samldb_msg_add_sid(struct ldb_message *msg,
255 const struct dom_sid *sid)
258 enum ndr_err_code ndr_err;
260 ndr_err = ndr_push_struct_blob(&v, msg, sid,
261 (ndr_push_flags_fn_t)ndr_push_dom_sid);
262 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
265 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
269 /* allocate a SID using our RID Set */
270 static int samldb_allocate_sid(struct samldb_ctx *ac)
274 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
277 ret = ridalloc_allocate_rid(ac->module, &rid);
278 if (ret != LDB_SUCCESS) {
282 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
284 return ldb_module_oom(ac->module);
287 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
288 return ldb_operr(ldb);
291 return samldb_next_step(ac);
295 see if a krbtgt_number is available
297 static bool samldb_krbtgtnumber_available(struct samldb_ctx *ac, unsigned krbtgt_number)
299 TALLOC_CTX *tmp_ctx = talloc_new(ac);
300 struct ldb_result *res;
301 const char *attrs[] = { NULL };
304 ret = dsdb_module_search(ac->module, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
305 attrs, DSDB_FLAG_NEXT_MODULE,
306 "msDC-SecondaryKrbTgtNumber=%u", krbtgt_number);
307 if (ret == LDB_SUCCESS && res->count == 0) {
308 talloc_free(tmp_ctx);
311 talloc_free(tmp_ctx);
315 /* special handling for add in RODC join */
316 static int samldb_rodc_add(struct samldb_ctx *ac)
318 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
319 unsigned krbtgt_number, i_start, i;
322 /* find a unused msDC-SecondaryKrbTgtNumber */
323 i_start = generate_random() & 0xFFFF;
328 for (i=i_start; i<=0xFFFF; i++) {
329 if (samldb_krbtgtnumber_available(ac, i)) {
334 for (i=1; i<i_start; i++) {
335 if (samldb_krbtgtnumber_available(ac, i)) {
341 ldb_asprintf_errstring(ldb_module_get_ctx(ac->module),
342 "%08X: Unable to find available msDS-SecondaryKrbTgtNumber",
343 W_ERROR_V(WERR_NO_SYSTEM_RESOURCES));
344 return LDB_ERR_OTHER;
347 ret = ldb_msg_add_empty(ac->msg, "msDS-SecondaryKrbTgtNumber", LDB_FLAG_INTERNAL_DISABLE_VALIDATION, NULL);
348 if (ret != LDB_SUCCESS) {
349 return ldb_operr(ldb);
352 ret = ldb_msg_add_fmt(ac->msg, "msDS-SecondaryKrbTgtNumber", "%u", krbtgt_number);
353 if (ret != LDB_SUCCESS) {
354 return ldb_operr(ldb);
357 ret = ldb_msg_add_fmt(ac->msg, "sAMAccountName", "krbtgt_%u", krbtgt_number);
358 if (ret != LDB_SUCCESS) {
359 return ldb_operr(ldb);
362 return samldb_next_step(ac);
366 * samldb_dn_from_sid (async)
369 static int samldb_dn_from_sid(struct samldb_ctx *ac);
371 static int samldb_dn_from_sid_callback(struct ldb_request *req,
372 struct ldb_reply *ares)
374 struct ldb_context *ldb;
375 struct samldb_ctx *ac;
378 ac = talloc_get_type(req->context, struct samldb_ctx);
379 ldb = ldb_module_get_ctx(ac->module);
382 ret = LDB_ERR_OPERATIONS_ERROR;
385 if (ares->error != LDB_SUCCESS) {
386 return ldb_module_done(ac->req, ares->controls,
387 ares->response, ares->error);
390 switch (ares->type) {
391 case LDB_REPLY_ENTRY:
393 if (ac->res_dn != NULL) {
395 ldb_set_errstring(ldb,
396 "Invalid number of results while searching "
397 "for domain objects!");
398 ret = LDB_ERR_OPERATIONS_ERROR;
401 ac->res_dn = ldb_dn_copy(ac, ares->message->dn);
407 case LDB_REPLY_REFERRAL:
416 /* found or not found, go on */
417 ret = samldb_next_step(ac);
422 if (ret != LDB_SUCCESS) {
423 return ldb_module_done(ac->req, NULL, NULL, ret);
429 /* Finds the DN "res_dn" of an object with a given SID "sid" */
430 static int samldb_dn_from_sid(struct samldb_ctx *ac)
432 struct ldb_context *ldb;
433 static const char * const attrs[] = { NULL };
434 struct ldb_request *req;
438 ldb = ldb_module_get_ctx(ac->module);
441 return ldb_operr(ldb);
443 filter = talloc_asprintf(ac, "(objectSid=%s)",
444 ldap_encode_ndr_dom_sid(ac, ac->sid));
448 ret = ldb_build_search_req(&req, ldb, ac,
449 ldb_get_default_basedn(ldb),
453 ac, samldb_dn_from_sid_callback,
455 if (ret != LDB_SUCCESS)
458 return ldb_next_request(ac->module, req);
462 static int samldb_check_primaryGroupID_1(struct samldb_ctx *ac)
464 struct ldb_context *ldb;
467 ldb = ldb_module_get_ctx(ac->module);
469 rid = samdb_result_uint(ac->msg, "primaryGroupID", ~0);
470 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
472 return ldb_operr(ldb);
475 return samldb_next_step(ac);
478 static int samldb_check_primaryGroupID_2(struct samldb_ctx *ac)
480 if (ac->res_dn == NULL) {
481 struct ldb_context *ldb;
482 ldb = ldb_module_get_ctx(ac->module);
483 ldb_asprintf_errstring(ldb,
484 "Failed to find group sid %s!",
485 dom_sid_string(ac->sid, ac->sid));
486 return LDB_ERR_UNWILLING_TO_PERFORM;
489 return samldb_next_step(ac);
494 * samldb_find_for_defaultObjectCategory (async)
497 static int samldb_find_for_defaultObjectCategory_callback(struct ldb_request *req,
498 struct ldb_reply *ares)
500 struct ldb_context *ldb;
501 struct samldb_ctx *ac;
504 ac = talloc_get_type(req->context, struct samldb_ctx);
505 ldb = ldb_module_get_ctx(ac->module);
508 ret = LDB_ERR_OPERATIONS_ERROR;
511 if (ares->error != LDB_SUCCESS) {
512 if (ares->error == LDB_ERR_NO_SUCH_OBJECT) {
513 if (ldb_request_get_control(ac->req,
514 LDB_CONTROL_RELAX_OID) != NULL) {
515 /* Don't be pricky when the DN doesn't exist */
516 /* if we have the RELAX control specified */
517 ac->res_dn = req->op.search.base;
518 return samldb_next_step(ac);
520 ldb_set_errstring(ldb,
521 "samldb_find_defaultObjectCategory: "
522 "Invalid DN for 'defaultObjectCategory'!");
523 ares->error = LDB_ERR_CONSTRAINT_VIOLATION;
527 return ldb_module_done(ac->req, ares->controls,
528 ares->response, ares->error);
531 switch (ares->type) {
532 case LDB_REPLY_ENTRY:
533 ac->res_dn = talloc_steal(ac, ares->message->dn);
538 case LDB_REPLY_REFERRAL:
547 if (ac->res_dn != NULL) {
548 /* when found go on */
549 ret = samldb_next_step(ac);
551 ret = LDB_ERR_OPERATIONS_ERROR;
557 if (ret != LDB_SUCCESS) {
558 return ldb_module_done(ac->req, NULL, NULL, ret);
564 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
566 struct ldb_context *ldb;
567 struct ldb_request *req;
568 static const char *no_attrs[] = { NULL };
571 ldb = ldb_module_get_ctx(ac->module);
575 ret = ldb_build_search_req(&req, ldb, ac,
576 ac->dn, LDB_SCOPE_BASE,
577 "(objectClass=classSchema)", no_attrs,
579 samldb_find_for_defaultObjectCategory_callback,
581 if (ret != LDB_SUCCESS) {
585 ret = dsdb_request_add_controls(req,
586 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT);
587 if (ret != LDB_SUCCESS) {
591 return ldb_next_request(ac->module, req);
595 * msDS-IntId attributeSchema attribute handling
596 * during LDB_ADD request processing
598 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
603 uint32_t system_flags;
604 struct ldb_context *ldb;
605 struct ldb_result *ldb_res;
606 struct ldb_dn *schema_dn;
608 ldb = ldb_module_get_ctx(ac->module);
609 schema_dn = ldb_get_schema_basedn(ldb);
611 /* replicated update should always go through */
612 if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
616 /* msDS-IntId is handled by system and should never be
617 * passed by clients */
618 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
619 return LDB_ERR_UNWILLING_TO_PERFORM;
622 /* do not generate msDS-IntId if Relax control is passed */
623 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
627 /* check Functional Level */
628 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
632 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
633 system_flags = ldb_msg_find_attr_as_uint(ac->msg, "systemFlags", 0);
634 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
638 /* Generate new value for msDs-IntId
639 * Value should be in 0x80000000..0xBFFFFFFF range */
640 msds_intid = generate_random() % 0X3FFFFFFF;
641 msds_intid += 0x80000000;
643 /* probe id values until unique one is found */
646 if (msds_intid > 0xBFFFFFFF) {
647 msds_intid = 0x80000001;
650 ret = dsdb_module_search(ac->module, ac,
652 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
653 DSDB_FLAG_NEXT_MODULE,
654 "(msDS-IntId=%d)", msds_intid);
655 if (ret != LDB_SUCCESS) {
656 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
657 __location__": Searching for msDS-IntId=%d failed - %s\n",
660 return ldb_operr(ldb);
662 id_exists = (ldb_res->count > 0);
664 talloc_free(ldb_res);
667 return ldb_msg_add_fmt(ac->msg, "msDS-IntId", "%d", msds_intid);
672 * samldb_add_entry (async)
675 static int samldb_add_entry_callback(struct ldb_request *req,
676 struct ldb_reply *ares)
678 struct ldb_context *ldb;
679 struct samldb_ctx *ac;
682 ac = talloc_get_type(req->context, struct samldb_ctx);
683 ldb = ldb_module_get_ctx(ac->module);
686 return ldb_module_done(ac->req, NULL, NULL,
687 LDB_ERR_OPERATIONS_ERROR);
690 if (ares->type == LDB_REPLY_REFERRAL) {
691 return ldb_module_send_referral(ac->req, ares->referral);
694 if (ares->error != LDB_SUCCESS) {
695 return ldb_module_done(ac->req, ares->controls,
696 ares->response, ares->error);
698 if (ares->type != LDB_REPLY_DONE) {
699 ldb_set_errstring(ldb,
700 "Invalid reply type!\n");
701 return ldb_module_done(ac->req, NULL, NULL,
702 LDB_ERR_OPERATIONS_ERROR);
705 /* The caller may wish to get controls back from the add */
706 ac->ares = talloc_steal(ac, ares);
708 ret = samldb_next_step(ac);
709 if (ret != LDB_SUCCESS) {
710 return ldb_module_done(ac->req, NULL, NULL, ret);
715 static int samldb_add_entry(struct samldb_ctx *ac)
717 struct ldb_context *ldb;
718 struct ldb_request *req;
721 ldb = ldb_module_get_ctx(ac->module);
723 ret = ldb_build_add_req(&req, ldb, ac,
726 ac, samldb_add_entry_callback,
728 if (ret != LDB_SUCCESS) {
732 return ldb_next_request(ac->module, req);
736 * return true if msg carries an attributeSchema that is intended to be RODC
737 * filtered but is also a system-critical attribute.
739 static bool check_rodc_critical_attribute(struct ldb_message *msg)
741 uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
743 schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
744 searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
745 rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE | SEARCH_FLAG_CONFIDENTIAL);
747 if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
748 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
756 static int samldb_fill_object(struct samldb_ctx *ac, const char *type)
758 struct ldb_context *ldb;
759 struct loadparm_context *lp_ctx;
760 enum sid_generator sid_generator;
762 struct ldb_control *rodc_control;
765 ldb = ldb_module_get_ctx(ac->module);
767 /* Add informations for the different account types */
769 if (strcmp(ac->type, "user") == 0) {
770 ret = samdb_find_or_add_attribute(ldb, ac->msg,
771 "userAccountControl", "546");
772 if (ret != LDB_SUCCESS) return ret;
773 ret = samdb_find_or_add_attribute(ldb, ac->msg,
775 if (ret != LDB_SUCCESS) return ret;
776 ret = samdb_find_or_add_attribute(ldb, ac->msg,
778 if (ret != LDB_SUCCESS) return ret;
779 ret = samdb_find_or_add_attribute(ldb, ac->msg,
781 if (ret != LDB_SUCCESS) return ret;
782 ret = samdb_find_or_add_attribute(ldb, ac->msg,
783 "badPasswordTime", "0");
784 if (ret != LDB_SUCCESS) return ret;
785 ret = samdb_find_or_add_attribute(ldb, ac->msg,
787 if (ret != LDB_SUCCESS) return ret;
788 ret = samdb_find_or_add_attribute(ldb, ac->msg,
790 if (ret != LDB_SUCCESS) return ret;
791 ret = samdb_find_or_add_attribute(ldb, ac->msg,
793 if (ret != LDB_SUCCESS) return ret;
794 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
795 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
796 "primaryGroupID", DOMAIN_RID_USERS);
797 if (ret != LDB_SUCCESS) return ret;
799 ret = samdb_find_or_add_attribute(ldb, ac->msg,
800 "accountExpires", "9223372036854775807");
801 if (ret != LDB_SUCCESS) return ret;
802 ret = samdb_find_or_add_attribute(ldb, ac->msg,
804 if (ret != LDB_SUCCESS) return ret;
805 } else if (strcmp(ac->type, "group") == 0) {
806 ret = samdb_find_or_add_attribute(ldb, ac->msg,
807 "groupType", "-2147483646");
808 if (ret != LDB_SUCCESS) return ret;
809 } else if (strcmp(ac->type, "classSchema") == 0) {
810 const struct ldb_val *rdn_value, *def_obj_cat_val;
812 ret = samdb_find_or_add_attribute(ldb, ac->msg,
814 if (ret != LDB_SUCCESS) return ret;
816 /* do not allow to mark an attributeSchema as RODC filtered if it
817 * is system-critical */
818 if (check_rodc_critical_attribute(ac->msg)) {
819 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
820 ldb_dn_get_linearized(ac->msg->dn));
821 return LDB_ERR_UNWILLING_TO_PERFORM;
825 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
826 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
827 /* the RDN has prefix "CN" */
828 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
829 samdb_cn_to_lDAPDisplayName(ac,
830 (const char *) rdn_value->data));
831 if (ret != LDB_SUCCESS) {
837 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
840 guid = GUID_random();
841 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
842 if (ret != LDB_SUCCESS) {
848 def_obj_cat_val = ldb_msg_find_ldb_val(ac->msg,
849 "defaultObjectCategory");
850 if (def_obj_cat_val != NULL) {
851 /* "defaultObjectCategory" has been set by the caller.
852 * Do some checks for consistency.
853 * NOTE: The real constraint check (that
854 * 'defaultObjectCategory' is the DN of the new
855 * objectclass or any parent of it) is still incomplete.
856 * For now we say that 'defaultObjectCategory' is valid
857 * if it exists and it is of objectclass "classSchema".
859 ac->dn = ldb_dn_from_ldb_val(ac, ldb, def_obj_cat_val);
860 if (ac->dn == NULL) {
861 ldb_set_errstring(ldb,
862 "Invalid DN for 'defaultObjectCategory'!");
863 return LDB_ERR_CONSTRAINT_VIOLATION;
866 /* "defaultObjectCategory" has not been set by the
867 * caller. Use the entry DN for it. */
868 ac->dn = ac->msg->dn;
870 ret = samdb_msg_add_string(ldb, ac, ac->msg,
871 "defaultObjectCategory",
872 ldb_dn_get_linearized(ac->dn));
873 if (ret != LDB_SUCCESS) {
879 ret = samldb_add_step(ac, samldb_add_entry);
880 if (ret != LDB_SUCCESS) return ret;
882 /* Now perform the checks for the 'defaultObjectCategory'. The
883 * lookup DN was already saved in "ac->dn" */
884 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
885 if (ret != LDB_SUCCESS) return ret;
887 return samldb_first_step(ac);
888 } else if (strcmp(ac->type, "attributeSchema") == 0) {
889 const struct ldb_val *rdn_value;
890 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
891 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
892 /* the RDN has prefix "CN" */
893 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
894 samdb_cn_to_lDAPDisplayName(ac,
895 (const char *) rdn_value->data));
896 if (ret != LDB_SUCCESS) {
902 /* do not allow to mark an attributeSchema as RODC filtered if it
903 * is system-critical */
904 if (check_rodc_critical_attribute(ac->msg)) {
905 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical attribute with RODC filtering",
906 ldb_dn_get_linearized(ac->msg->dn));
907 return LDB_ERR_UNWILLING_TO_PERFORM;
910 ret = samdb_find_or_add_attribute(ldb, ac->msg,
911 "isSingleValued", "FALSE");
912 if (ret != LDB_SUCCESS) return ret;
914 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
917 guid = GUID_random();
918 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
919 if (ret != LDB_SUCCESS) {
925 /* handle msDS-IntID attribute */
926 ret = samldb_add_handle_msDS_IntId(ac);
927 if (ret != LDB_SUCCESS) return ret;
929 ret = samldb_add_step(ac, samldb_add_entry);
930 if (ret != LDB_SUCCESS) return ret;
932 return samldb_first_step(ac);
934 ldb_asprintf_errstring(ldb,
935 "Invalid entry type!");
936 return LDB_ERR_OPERATIONS_ERROR;
939 rodc_control = ldb_request_get_control(ac->req, LDB_CONTROL_RODC_DCPROMO_OID);
941 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
942 rodc_control->critical = false;
943 ret = samldb_add_step(ac, samldb_rodc_add);
944 if (ret != LDB_SUCCESS) return ret;
947 /* check if we have a valid sAMAccountName */
948 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
949 if (ret != LDB_SUCCESS) return ret;
951 /* check account_type/group_type */
952 ret = samldb_add_step(ac, samldb_check_sAMAccountType);
953 if (ret != LDB_SUCCESS) return ret;
955 /* check if we have a valid primary group ID */
956 if (strcmp(ac->type, "user") == 0) {
957 ret = samldb_add_step(ac, samldb_check_primaryGroupID_1);
958 if (ret != LDB_SUCCESS) return ret;
959 ret = samldb_add_step(ac, samldb_dn_from_sid);
960 if (ret != LDB_SUCCESS) return ret;
961 ret = samldb_add_step(ac, samldb_check_primaryGroupID_2);
962 if (ret != LDB_SUCCESS) return ret;
965 lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
966 struct loadparm_context);
968 /* don't allow objectSID to be specified without the RELAX control */
969 sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
970 if (sid && !ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) &&
971 !dsdb_module_am_system(ac->module)) {
972 ldb_asprintf_errstring(ldb, "No SID may be specified in user/group creation for %s",
973 ldb_dn_get_linearized(ac->msg->dn));
974 return LDB_ERR_UNWILLING_TO_PERFORM;
978 sid_generator = lpcfg_sid_generator(lp_ctx);
979 if (sid_generator == SID_GENERATOR_INTERNAL) {
980 ret = samldb_add_step(ac, samldb_allocate_sid);
981 if (ret != LDB_SUCCESS) return ret;
985 /* finally proceed with adding the entry */
986 ret = samldb_add_step(ac, samldb_add_entry);
987 if (ret != LDB_SUCCESS) return ret;
989 return samldb_first_step(ac);
992 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
994 struct ldb_context *ldb;
998 ldb = ldb_module_get_ctx(ac->module);
1000 sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
1002 sid = dom_sid_parse_talloc(ac->msg,
1003 (const char *)ldb_dn_get_rdn_val(ac->msg->dn)->data);
1005 ldb_set_errstring(ldb,
1006 "No valid SID found in "
1007 "ForeignSecurityPrincipal CN!");
1008 return LDB_ERR_CONSTRAINT_VIOLATION;
1010 if (! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
1011 return ldb_operr(ldb);
1015 /* finally proceed with adding the entry */
1016 ret = samldb_add_step(ac, samldb_add_entry);
1017 if (ret != LDB_SUCCESS) return ret;
1019 return samldb_first_step(ac);
1022 static int samldb_schema_info_update(struct samldb_ctx *ac)
1025 struct ldb_context *ldb;
1026 struct dsdb_schema *schema;
1028 /* replicated update should always go through */
1029 if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1033 /* do not update schemaInfo during provisioning */
1034 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
1038 ldb = ldb_module_get_ctx(ac->module);
1039 schema = dsdb_get_schema(ldb, NULL);
1041 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
1042 "samldb_schema_info_update: no dsdb_schema loaded");
1043 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
1044 return ldb_operr(ldb);
1047 werr = dsdb_module_schema_info_update(ac->module, schema, DSDB_FLAG_NEXT_MODULE);
1048 if (!W_ERROR_IS_OK(werr)) {
1049 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
1050 "samldb_schema_info_update: "
1051 "dsdb_module_schema_info_update failed with %s",
1053 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
1054 return ldb_operr(ldb);
1061 static int samldb_prim_group_change(struct samldb_ctx *ac)
1063 struct ldb_context *ldb;
1064 const char * attrs[] = { "primaryGroupID", "memberOf", NULL };
1065 struct ldb_result *res;
1066 struct ldb_message_element *el;
1067 struct ldb_message *msg;
1069 struct dom_sid *sid;
1070 struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
1073 ldb = ldb_module_get_ctx(ac->module);
1075 /* Fetch informations from the existing object */
1077 ret = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1079 if (ret != LDB_SUCCESS) {
1083 /* Finds out the DN of the old primary group */
1085 rid = samdb_result_uint(res->msgs[0], "primaryGroupID", (uint32_t) -1);
1086 if (rid == (uint32_t) -1) {
1087 /* User objects do always have a mandatory "primaryGroupID"
1088 * attribute. If this doesn't exist then the object is of the
1089 * wrong type. This is the exact Windows error code */
1090 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1093 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1095 return ldb_operr(ldb);
1098 prev_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSID=%s)",
1099 dom_sid_string(ac, sid));
1100 if (prev_prim_group_dn == NULL) {
1101 return ldb_operr(ldb);
1104 /* Finds out the DN of the new primary group */
1106 rid = samdb_result_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
1107 if (rid == (uint32_t) -1) {
1108 /* we aren't affected of any primary group change */
1112 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1114 return ldb_operr(ldb);
1117 new_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSID=%s)",
1118 dom_sid_string(ac, sid));
1119 if (new_prim_group_dn == NULL) {
1120 /* Here we know if the specified new primary group candidate is
1122 return LDB_ERR_UNWILLING_TO_PERFORM;
1125 /* Only update the "member" attributes when we really do have a change */
1126 if (ldb_dn_compare(new_prim_group_dn, prev_prim_group_dn) != 0) {
1127 /* We need to be already a normal member of the new primary
1128 * group in order to be successful. */
1129 el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
1130 ldb_dn_get_linearized(new_prim_group_dn));
1132 return LDB_ERR_UNWILLING_TO_PERFORM;
1135 /* Remove the "member" attribute on the new primary group */
1136 msg = talloc_zero(ac, struct ldb_message);
1137 msg->dn = new_prim_group_dn;
1139 ret = samdb_msg_add_delval(ldb, ac, msg, "member",
1140 ldb_dn_get_linearized(ac->msg->dn));
1141 if (ret != LDB_SUCCESS) {
1145 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1146 if (ret != LDB_SUCCESS) {
1150 /* Add a "member" attribute for the previous primary group */
1151 msg = talloc_zero(ac, struct ldb_message);
1152 msg->dn = prev_prim_group_dn;
1154 ret = samdb_msg_add_addval(ldb, ac, msg, "member",
1155 ldb_dn_get_linearized(ac->msg->dn));
1156 if (ret != LDB_SUCCESS) {
1160 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1161 if (ret != LDB_SUCCESS) {
1170 static int samldb_member_check(struct samldb_ctx *ac)
1172 struct ldb_context *ldb;
1173 struct ldb_message_element *el;
1174 struct ldb_dn *member_dn, *group_dn;
1175 uint32_t prim_group_rid;
1176 struct dom_sid *sid;
1179 ldb = ldb_module_get_ctx(ac->module);
1181 el = ldb_msg_find_element(ac->msg, "member");
1183 /* we aren't affected */
1187 for (i = 0; i < el->num_values; i++) {
1188 /* Denies to add "member"s to groups which are primary ones
1190 member_dn = ldb_dn_from_ldb_val(ac, ldb, &el->values[i]);
1191 if (!ldb_dn_validate(member_dn)) {
1192 return ldb_operr(ldb);
1195 prim_group_rid = samdb_search_uint(ldb, ac, (uint32_t) -1,
1196 member_dn, "primaryGroupID",
1198 if (prim_group_rid == (uint32_t) -1) {
1199 /* the member hasn't to be a user account -> therefore
1200 * no check needed in this case. */
1204 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1207 return ldb_operr(ldb);
1210 group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSID=%s)",
1211 dom_sid_string(ac, sid));
1212 if (group_dn == NULL) {
1213 return ldb_operr(ldb);
1216 if (ldb_dn_compare(group_dn, ac->msg->dn) == 0) {
1217 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1225 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
1227 struct ldb_context *ldb;
1228 struct dom_sid *sid;
1233 ldb = ldb_module_get_ctx(ac->module);
1235 /* Finds out the SID/RID of the SAM object */
1236 sid = samdb_search_dom_sid(ldb, ac, ac->req->op.del.dn, "objectSID",
1239 /* No SID - it might not be a SAM object - therefore ok */
1242 status = dom_sid_split_rid(ac, sid, NULL, &rid);
1243 if (!NT_STATUS_IS_OK(status)) {
1244 return ldb_operr(ldb);
1247 /* Special object (security principal?) */
1251 /* Deny delete requests from groups which are primary ones */
1252 count = samdb_search_count(ldb, NULL,
1253 "(&(primaryGroupID=%u)(objectClass=user))",
1256 return ldb_operr(ldb);
1259 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1267 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
1269 struct ldb_context *ldb;
1270 struct samldb_ctx *ac;
1273 ldb = ldb_module_get_ctx(module);
1274 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
1276 /* do not manipulate our control entries */
1277 if (ldb_dn_is_special(req->op.add.message->dn)) {
1278 return ldb_next_request(module, req);
1281 ac = samldb_ctx_init(module, req);
1283 return ldb_operr(ldb);
1286 /* build the new msg */
1287 ac->msg = ldb_msg_copy(ac, ac->req->op.add.message);
1290 ldb_debug(ldb, LDB_DEBUG_FATAL,
1291 "samldb_add: ldb_msg_copy failed!\n");
1292 return ldb_operr(ldb);
1295 if (samdb_find_attribute(ldb, ac->msg,
1296 "objectclass", "user") != NULL) {
1297 return samldb_fill_object(ac, "user");
1300 if (samdb_find_attribute(ldb, ac->msg,
1301 "objectclass", "group") != NULL) {
1302 return samldb_fill_object(ac, "group");
1305 /* perhaps a foreignSecurityPrincipal? */
1306 if (samdb_find_attribute(ldb, ac->msg,
1308 "foreignSecurityPrincipal") != NULL) {
1309 return samldb_fill_foreignSecurityPrincipal_object(ac);
1312 if (samdb_find_attribute(ldb, ac->msg,
1313 "objectclass", "classSchema") != NULL) {
1314 ret = samldb_schema_info_update(ac);
1315 if (ret != LDB_SUCCESS) {
1320 return samldb_fill_object(ac, "classSchema");
1323 if (samdb_find_attribute(ldb, ac->msg,
1324 "objectclass", "attributeSchema") != NULL) {
1325 ret = samldb_schema_info_update(ac);
1326 if (ret != LDB_SUCCESS) {
1331 return samldb_fill_object(ac, "attributeSchema");
1336 /* nothing matched, go on */
1337 return ldb_next_request(module, req);
1341 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
1343 struct ldb_context *ldb;
1344 struct samldb_ctx *ac;
1345 struct ldb_message *msg;
1346 struct ldb_message_element *el, *el2;
1348 uint32_t account_type;
1350 if (ldb_dn_is_special(req->op.mod.message->dn)) {
1351 /* do not manipulate our control entries */
1352 return ldb_next_request(module, req);
1355 ldb = ldb_module_get_ctx(module);
1357 if (ldb_msg_find_element(req->op.mod.message, "sAMAccountType") != NULL) {
1358 ldb_asprintf_errstring(ldb,
1359 "sAMAccountType must not be specified!");
1360 return LDB_ERR_UNWILLING_TO_PERFORM;
1363 /* msDS-IntId is not allowed to be modified
1364 * except when modification comes from replication */
1365 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
1366 if (!ldb_request_get_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1367 return LDB_ERR_CONSTRAINT_VIOLATION;
1371 ac = samldb_ctx_init(module, req);
1373 return ldb_operr(ldb);
1376 /* TODO: do not modify original request, create a new one */
1378 el = ldb_msg_find_element(req->op.mod.message, "groupType");
1379 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1380 uint32_t group_type;
1382 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1383 req->op.mod.message);
1385 group_type = strtoul((const char *)el->values[0].data, NULL, 0);
1386 account_type = ds_gtype2atype(group_type);
1387 ret = samdb_msg_add_uint(ldb, msg, msg,
1390 if (ret != LDB_SUCCESS) {
1393 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1394 el2->flags = LDB_FLAG_MOD_REPLACE;
1396 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
1397 return LDB_ERR_UNWILLING_TO_PERFORM;
1400 el = ldb_msg_find_element(req->op.mod.message, "primaryGroupID");
1401 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1402 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1403 req->op.mod.message);
1405 ret = samldb_prim_group_change(ac);
1406 if (ret != LDB_SUCCESS) {
1410 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
1411 return LDB_ERR_UNWILLING_TO_PERFORM;
1414 el = ldb_msg_find_element(req->op.mod.message, "userAccountControl");
1415 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1416 uint32_t user_account_control;
1418 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1419 req->op.mod.message);
1421 user_account_control = strtoul((const char *)el->values[0].data,
1423 account_type = ds_uf2atype(user_account_control);
1424 ret = samdb_msg_add_uint(ldb, msg, msg,
1427 if (ret != LDB_SUCCESS) {
1430 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1431 el2->flags = LDB_FLAG_MOD_REPLACE;
1433 if (user_account_control & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
1434 ret = samdb_msg_add_string(ldb, msg, msg,
1435 "isCriticalSystemObject", "TRUE");
1436 if (ret != LDB_SUCCESS) {
1439 el2 = ldb_msg_find_element(msg, "isCriticalSystemObject");
1440 el2->flags = LDB_FLAG_MOD_REPLACE;
1442 /* DCs have primaryGroupID of DOMAIN_RID_DCS */
1443 if (!ldb_msg_find_element(msg, "primaryGroupID")) {
1445 if (user_account_control & UF_SERVER_TRUST_ACCOUNT) {
1446 rid = DOMAIN_RID_DCS;
1449 rid = DOMAIN_RID_READONLY_DCS;
1451 ret = samdb_msg_add_uint(ldb, msg, msg,
1452 "primaryGroupID", rid);
1453 if (ret != LDB_SUCCESS) {
1456 el2 = ldb_msg_find_element(msg, "primaryGroupID");
1457 el2->flags = LDB_FLAG_MOD_REPLACE;
1461 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
1462 return LDB_ERR_UNWILLING_TO_PERFORM;
1465 el = ldb_msg_find_element(req->op.mod.message, "member");
1466 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1467 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1468 req->op.mod.message);
1470 ret = samldb_member_check(ac);
1471 if (ret != LDB_SUCCESS) {
1476 return ldb_next_request(module, req);
1480 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
1482 struct samldb_ctx *ac;
1485 if (ldb_dn_is_special(req->op.del.dn)) {
1486 /* do not manipulate our control entries */
1487 return ldb_next_request(module, req);
1490 ac = samldb_ctx_init(module, req);
1492 return ldb_operr(ldb_module_get_ctx(module));
1494 ret = samldb_prim_group_users_check(ac);
1495 if (ret != LDB_SUCCESS) {
1499 return ldb_next_request(module, req);
1502 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
1504 struct ldb_context *ldb = ldb_module_get_ctx(module);
1505 struct dsdb_fsmo_extended_op *exop;
1508 exop = talloc_get_type(req->op.extended.data, struct dsdb_fsmo_extended_op);
1510 ldb_debug(ldb, LDB_DEBUG_FATAL, "samldb_extended_allocate_rid_pool: invalid extended data\n");
1511 return LDB_ERR_PROTOCOL_ERROR;
1514 ret = ridalloc_allocate_rid_pool_fsmo(module, exop);
1515 if (ret != LDB_SUCCESS) {
1519 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
1522 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
1524 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
1525 return samldb_extended_allocate_rid_pool(module, req);
1528 return ldb_next_request(module, req);
1532 _PUBLIC_ const struct ldb_module_ops ldb_samldb_module_ops = {
1535 .modify = samldb_modify,
1536 .del = samldb_delete,
1537 .extended = samldb_extended