4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
5 Copyright (C) Simo Sorce 2004-2008
6 Copyright (C) Matthias Dieter Wallnöfer 2009-2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * Component: ldb samldb module
27 * Description: add embedded user/group creation functionality
33 #include "libcli/ldap/ldap_ndr.h"
34 #include "ldb_module.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "dsdb/samdb/ldb_modules/util.h"
37 #include "libcli/security/security.h"
38 #include "librpc/gen_ndr/ndr_security.h"
39 #include "../lib/util/util_ldb.h"
41 #include "param/param.h"
45 typedef int (*samldb_step_fn_t)(struct samldb_ctx *);
48 struct samldb_step *next;
53 struct ldb_module *module;
54 struct ldb_request *req;
56 /* used for add operations */
59 /* the resulting message */
60 struct ldb_message *msg;
62 /* holds the entry SID */
65 /* holds a generic dn */
68 /* used in conjunction with "sid" in "samldb_dn_from_sid" and
69 * "samldb_find_for_defaultObjectCategory" */
70 struct ldb_dn *res_dn;
72 /* all the async steps necessary to complete the operation */
73 struct samldb_step *steps;
74 struct samldb_step *curstep;
76 /* If someone set an ares to forward controls and response back to the caller */
77 struct ldb_reply *ares;
80 static struct samldb_ctx *samldb_ctx_init(struct ldb_module *module,
81 struct ldb_request *req)
83 struct ldb_context *ldb;
84 struct samldb_ctx *ac;
86 ldb = ldb_module_get_ctx(module);
88 ac = talloc_zero(req, struct samldb_ctx);
100 static int samldb_add_step(struct samldb_ctx *ac, samldb_step_fn_t fn)
102 struct samldb_step *step, *stepper;
104 step = talloc_zero(ac, struct samldb_step);
106 return ldb_oom(ldb_module_get_ctx(ac->module));
111 if (ac->steps == NULL) {
115 if (ac->curstep == NULL)
116 return ldb_operr(ldb_module_get_ctx(ac->module));
117 for (stepper = ac->curstep; stepper->next != NULL;
118 stepper = stepper->next);
119 stepper->next = step;
125 static int samldb_first_step(struct samldb_ctx *ac)
127 if (ac->steps == NULL) {
128 return ldb_operr(ldb_module_get_ctx(ac->module));
131 ac->curstep = ac->steps;
132 return ac->curstep->fn(ac);
135 static int samldb_next_step(struct samldb_ctx *ac)
137 if (ac->curstep->next) {
138 ac->curstep = ac->curstep->next;
139 return ac->curstep->fn(ac);
142 /* we exit the samldb module here */
143 /* If someone set an ares to forward controls and response back to the caller, use them */
145 return ldb_module_done(ac->req, ac->ares->controls,
146 ac->ares->response, LDB_SUCCESS);
148 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
153 /* sAMAccountName handling */
155 static int samldb_generate_sAMAccountName(struct ldb_context *ldb, struct ldb_message *msg)
159 /* Format: $000000-000000000000 */
161 name = talloc_asprintf(msg, "$%.6X-%.6X%.6X",
162 (unsigned int)generate_random(),
163 (unsigned int)generate_random(),
164 (unsigned int)generate_random());
168 return ldb_msg_add_steal_string(msg, "sAMAccountName", name);
171 static int samldb_check_sAMAccountName(struct samldb_ctx *ac)
173 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
177 if (ldb_msg_find_element(ac->msg, "sAMAccountName") == NULL) {
178 ret = samldb_generate_sAMAccountName(ldb, ac->msg);
179 if (ret != LDB_SUCCESS) {
184 name = ldb_msg_find_attr_as_string(ac->msg, "sAMAccountName", NULL);
186 return ldb_operr(ldb);
189 ret = samdb_search_count(ldb, NULL, "(sAMAccountName=%s)",
190 ldb_binary_encode_string(ac, name));
191 if ((ret < 0) || (ret > 1)) {
192 return ldb_operr(ldb);
195 return LDB_ERR_ENTRY_ALREADY_EXISTS;
198 return samldb_next_step(ac);
202 static int samldb_check_samAccountType(struct samldb_ctx *ac)
204 struct ldb_context *ldb;
205 unsigned int account_type;
206 unsigned int group_type;
210 ldb = ldb_module_get_ctx(ac->module);
212 /* make sure sAMAccountType is not specified */
213 if (ldb_msg_find_element(ac->msg, "sAMAccountType") != NULL) {
214 ldb_asprintf_errstring(ldb,
215 "sAMAccountType must not be specified!");
216 return LDB_ERR_UNWILLING_TO_PERFORM;
219 if (strcmp("user", ac->type) == 0) {
220 uac = samdb_result_uint(ac->msg, "userAccountControl", 0);
222 ldb_asprintf_errstring(ldb,
223 "userAccountControl invalid!");
224 return LDB_ERR_UNWILLING_TO_PERFORM;
226 account_type = ds_uf2atype(uac);
227 ret = samdb_msg_add_uint(ldb,
231 if (ret != LDB_SUCCESS) {
236 if (strcmp("group", ac->type) == 0) {
238 group_type = samdb_result_uint(ac->msg, "groupType", 0);
239 if (group_type == 0) {
240 ldb_asprintf_errstring(ldb,
241 "groupType invalid!\n");
242 return LDB_ERR_UNWILLING_TO_PERFORM;
244 account_type = ds_gtype2atype(group_type);
245 ret = samdb_msg_add_uint(ldb,
249 if (ret != LDB_SUCCESS) {
255 return samldb_next_step(ac);
258 static bool samldb_msg_add_sid(struct ldb_message *msg,
260 const struct dom_sid *sid)
263 enum ndr_err_code ndr_err;
265 ndr_err = ndr_push_struct_blob(&v, msg, sid,
266 (ndr_push_flags_fn_t)ndr_push_dom_sid);
267 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
270 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
274 /* allocate a SID using our RID Set */
275 static int samldb_allocate_sid(struct samldb_ctx *ac)
279 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
281 ret = ridalloc_allocate_rid(ac->module, &rid);
282 if (ret != LDB_SUCCESS) {
286 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
287 if (ac->sid == NULL) {
288 return ldb_module_oom(ac->module);
291 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", ac->sid)) {
292 return ldb_operr(ldb);
295 return samldb_next_step(ac);
299 see if a krbtgt_number is available
301 static bool samldb_krbtgtnumber_available(struct samldb_ctx *ac, unsigned krbtgt_number)
303 TALLOC_CTX *tmp_ctx = talloc_new(ac);
304 struct ldb_result *res;
305 const char *attrs[] = { NULL };
308 ret = dsdb_module_search(ac->module, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
309 attrs, DSDB_FLAG_NEXT_MODULE,
310 "msDC-SecondaryKrbTgtNumber=%u", krbtgt_number);
311 if (ret == LDB_SUCCESS && res->count == 0) {
312 talloc_free(tmp_ctx);
315 talloc_free(tmp_ctx);
319 /* special handling for add in RODC join */
320 static int samldb_rodc_add(struct samldb_ctx *ac)
322 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
323 unsigned krbtgt_number, i_start, i;
326 /* find a unused msDC-SecondaryKrbTgtNumber */
327 i_start = generate_random() & 0xFFFF;
332 for (i=i_start; i<=0xFFFF; i++) {
333 if (samldb_krbtgtnumber_available(ac, i)) {
338 for (i=1; i<i_start; i++) {
339 if (samldb_krbtgtnumber_available(ac, i)) {
345 ldb_asprintf_errstring(ldb_module_get_ctx(ac->module),
346 "%08X: Unable to find available msDS-SecondaryKrbTgtNumber",
347 W_ERROR_V(WERR_NO_SYSTEM_RESOURCES));
348 return LDB_ERR_OTHER;
351 ret = ldb_msg_add_empty(ac->msg, "msDS-SecondaryKrbTgtNumber", LDB_FLAG_INTERNAL_DISABLE_VALIDATION, NULL);
352 if (ret != LDB_SUCCESS) {
353 return ldb_operr(ldb);
356 ret = ldb_msg_add_fmt(ac->msg, "msDS-SecondaryKrbTgtNumber", "%u", krbtgt_number);
357 if (ret != LDB_SUCCESS) {
358 return ldb_operr(ldb);
361 ret = ldb_msg_add_fmt(ac->msg, "samAccountName", "krbtgt_%u", krbtgt_number);
362 if (ret != LDB_SUCCESS) {
363 return ldb_operr(ldb);
366 return samldb_next_step(ac);
370 * samldb_dn_from_sid (async)
373 static int samldb_dn_from_sid(struct samldb_ctx *ac);
375 static int samldb_dn_from_sid_callback(struct ldb_request *req,
376 struct ldb_reply *ares)
378 struct ldb_context *ldb;
379 struct samldb_ctx *ac;
382 ac = talloc_get_type(req->context, struct samldb_ctx);
383 ldb = ldb_module_get_ctx(ac->module);
386 ret = LDB_ERR_OPERATIONS_ERROR;
389 if (ares->error != LDB_SUCCESS) {
390 return ldb_module_done(ac->req, ares->controls,
391 ares->response, ares->error);
394 switch (ares->type) {
395 case LDB_REPLY_ENTRY:
397 if (ac->res_dn != NULL) {
399 ldb_set_errstring(ldb,
400 "Invalid number of results while searching "
401 "for domain objects!");
402 ret = LDB_ERR_OPERATIONS_ERROR;
405 ac->res_dn = ldb_dn_copy(ac, ares->message->dn);
411 case LDB_REPLY_REFERRAL:
420 /* found or not found, go on */
421 ret = samldb_next_step(ac);
426 if (ret != LDB_SUCCESS) {
427 return ldb_module_done(ac->req, NULL, NULL, ret);
433 /* Finds the DN "res_dn" of an object with a given SID "sid" */
434 static int samldb_dn_from_sid(struct samldb_ctx *ac)
436 struct ldb_context *ldb;
437 static const char * const attrs[] = { NULL };
438 struct ldb_request *req;
442 ldb = ldb_module_get_ctx(ac->module);
445 return ldb_operr(ldb);
447 filter = talloc_asprintf(ac, "(objectSid=%s)",
448 ldap_encode_ndr_dom_sid(ac, ac->sid));
452 ret = ldb_build_search_req(&req, ldb, ac,
453 ldb_get_default_basedn(ldb),
457 ac, samldb_dn_from_sid_callback,
459 if (ret != LDB_SUCCESS)
462 return ldb_next_request(ac->module, req);
466 static int samldb_check_primaryGroupID_1(struct samldb_ctx *ac)
468 struct ldb_context *ldb;
471 ldb = ldb_module_get_ctx(ac->module);
473 rid = samdb_result_uint(ac->msg, "primaryGroupID", ~0);
474 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
476 return ldb_operr(ldb);
479 return samldb_next_step(ac);
482 static int samldb_check_primaryGroupID_2(struct samldb_ctx *ac)
484 if (ac->res_dn == NULL) {
485 struct ldb_context *ldb;
486 ldb = ldb_module_get_ctx(ac->module);
487 ldb_asprintf_errstring(ldb,
488 "Failed to find group sid %s!",
489 dom_sid_string(ac->sid, ac->sid));
490 return LDB_ERR_UNWILLING_TO_PERFORM;
493 return samldb_next_step(ac);
498 * samldb_find_for_defaultObjectCategory (async)
501 static int samldb_find_for_defaultObjectCategory_callback(struct ldb_request *req,
502 struct ldb_reply *ares)
504 struct ldb_context *ldb;
505 struct samldb_ctx *ac;
508 ac = talloc_get_type(req->context, struct samldb_ctx);
509 ldb = ldb_module_get_ctx(ac->module);
512 ret = LDB_ERR_OPERATIONS_ERROR;
515 if (ares->error != LDB_SUCCESS) {
516 if (ares->error == LDB_ERR_NO_SUCH_OBJECT) {
517 if (ldb_request_get_control(ac->req,
518 LDB_CONTROL_RELAX_OID) != NULL) {
519 /* Don't be pricky when the DN doesn't exist */
520 /* if we have the RELAX control specified */
521 ac->res_dn = req->op.search.base;
522 return samldb_next_step(ac);
524 ldb_set_errstring(ldb,
525 "samldb_find_defaultObjectCategory: "
526 "Invalid DN for 'defaultObjectCategory'!");
527 ares->error = LDB_ERR_CONSTRAINT_VIOLATION;
531 return ldb_module_done(ac->req, ares->controls,
532 ares->response, ares->error);
535 switch (ares->type) {
536 case LDB_REPLY_ENTRY:
537 ac->res_dn = talloc_steal(ac, ares->message->dn);
542 case LDB_REPLY_REFERRAL:
551 if (ac->res_dn != NULL) {
552 /* when found go on */
553 ret = samldb_next_step(ac);
555 ret = LDB_ERR_OPERATIONS_ERROR;
561 if (ret != LDB_SUCCESS) {
562 return ldb_module_done(ac->req, NULL, NULL, ret);
568 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
570 struct ldb_context *ldb;
571 struct ldb_request *req;
572 static const char *no_attrs[] = { NULL };
575 ldb = ldb_module_get_ctx(ac->module);
579 ret = ldb_build_search_req(&req, ldb, ac,
580 ac->dn, LDB_SCOPE_BASE,
581 "(objectClass=classSchema)", no_attrs,
583 samldb_find_for_defaultObjectCategory_callback,
585 if (ret != LDB_SUCCESS) {
589 ret = dsdb_request_add_controls(req,
590 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT);
591 if (ret != LDB_SUCCESS) {
595 return ldb_next_request(ac->module, req);
599 * msDS-IntId attributeSchema attribute handling
600 * during LDB_ADD request processing
602 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
607 uint32_t system_flags;
608 struct ldb_context *ldb;
609 struct ldb_result *ldb_res;
610 struct ldb_dn *schema_dn;
612 ldb = ldb_module_get_ctx(ac->module);
613 schema_dn = ldb_get_schema_basedn(ldb);
615 /* replicated update should always go through */
616 if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
620 /* msDS-IntId is handled by system and should never be
621 * passed by clients */
622 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
623 return LDB_ERR_UNWILLING_TO_PERFORM;
626 /* do not generate msDS-IntId if Relax control is passed */
627 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
631 /* check Functional Level */
632 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
636 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
637 system_flags = ldb_msg_find_attr_as_uint(ac->msg, "systemFlags", 0);
638 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
642 /* Generate new value for msDs-IntId
643 * Value should be in 0x80000000..0xBFFFFFFF range */
644 msds_intid = generate_random() % 0X3FFFFFFF;
645 msds_intid += 0x80000000;
647 /* probe id values until unique one is found */
650 if (msds_intid > 0xBFFFFFFF) {
651 msds_intid = 0x80000001;
654 ret = dsdb_module_search(ac->module, ac,
656 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
657 DSDB_FLAG_NEXT_MODULE,
658 "(msDS-IntId=%d)", msds_intid);
659 if (ret != LDB_SUCCESS) {
660 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
661 __location__": Searching for msDS-IntId=%d failed - %s\n",
664 return ldb_operr(ldb);
666 id_exists = (ldb_res->count > 0);
668 talloc_free(ldb_res);
671 return ldb_msg_add_fmt(ac->msg, "msDS-IntId", "%d", msds_intid);
676 * samldb_add_entry (async)
679 static int samldb_add_entry_callback(struct ldb_request *req,
680 struct ldb_reply *ares)
682 struct ldb_context *ldb;
683 struct samldb_ctx *ac;
686 ac = talloc_get_type(req->context, struct samldb_ctx);
687 ldb = ldb_module_get_ctx(ac->module);
690 return ldb_module_done(ac->req, NULL, NULL,
691 LDB_ERR_OPERATIONS_ERROR);
694 if (ares->type == LDB_REPLY_REFERRAL) {
695 return ldb_module_send_referral(ac->req, ares->referral);
698 if (ares->error != LDB_SUCCESS) {
699 return ldb_module_done(ac->req, ares->controls,
700 ares->response, ares->error);
702 if (ares->type != LDB_REPLY_DONE) {
703 ldb_set_errstring(ldb,
704 "Invalid reply type!\n");
705 return ldb_module_done(ac->req, NULL, NULL,
706 LDB_ERR_OPERATIONS_ERROR);
709 /* The caller may wish to get controls back from the add */
710 ac->ares = talloc_steal(ac, ares);
712 ret = samldb_next_step(ac);
713 if (ret != LDB_SUCCESS) {
714 return ldb_module_done(ac->req, NULL, NULL, ret);
719 static int samldb_add_entry(struct samldb_ctx *ac)
721 struct ldb_context *ldb;
722 struct ldb_request *req;
725 ldb = ldb_module_get_ctx(ac->module);
727 ret = ldb_build_add_req(&req, ldb, ac,
730 ac, samldb_add_entry_callback,
732 if (ret != LDB_SUCCESS) {
736 return ldb_next_request(ac->module, req);
740 * return true if msg carries an attributeSchema that is intended to be RODC
741 * filtered but is also a system-critical attribute.
743 static bool check_rodc_critical_attribute(struct ldb_message *msg)
745 uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
747 schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
748 searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
749 rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE | SEARCH_FLAG_CONFIDENTIAL);
751 if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
752 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
760 static int samldb_fill_object(struct samldb_ctx *ac, const char *type)
762 struct ldb_context *ldb;
763 struct loadparm_context *lp_ctx;
764 enum sid_generator sid_generator;
766 struct ldb_control *rodc_control;
768 ldb = ldb_module_get_ctx(ac->module);
770 /* Add informations for the different account types */
772 if (strcmp(ac->type, "user") == 0) {
773 ret = samdb_find_or_add_attribute(ldb, ac->msg,
774 "userAccountControl", "546");
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,
784 if (ret != LDB_SUCCESS) return ret;
785 ret = samdb_find_or_add_attribute(ldb, ac->msg,
786 "badPasswordTime", "0");
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 ret = samdb_find_or_add_attribute(ldb, ac->msg,
796 if (ret != LDB_SUCCESS) return ret;
797 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
798 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
799 "primaryGroupID", DOMAIN_RID_USERS);
800 if (ret != LDB_SUCCESS) return ret;
802 ret = samdb_find_or_add_attribute(ldb, ac->msg,
803 "accountExpires", "9223372036854775807");
804 if (ret != LDB_SUCCESS) return ret;
805 ret = samdb_find_or_add_attribute(ldb, ac->msg,
807 if (ret != LDB_SUCCESS) return ret;
808 } else if (strcmp(ac->type, "group") == 0) {
809 ret = samdb_find_or_add_attribute(ldb, ac->msg,
810 "groupType", "-2147483646");
811 if (ret != LDB_SUCCESS) return ret;
812 } else if (strcmp(ac->type, "classSchema") == 0) {
813 const struct ldb_val *rdn_value, *def_obj_cat_val;
815 ret = samdb_find_or_add_attribute(ldb, ac->msg,
817 if (ret != LDB_SUCCESS) return ret;
819 /* do not allow to mark an attributeSchema as RODC filtered if it
820 * is system-critical */
821 if (check_rodc_critical_attribute(ac->msg)) {
822 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
823 ldb_dn_get_linearized(ac->msg->dn));
824 return LDB_ERR_UNWILLING_TO_PERFORM;
828 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
829 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
830 /* the RDN has prefix "CN" */
831 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
832 samdb_cn_to_lDAPDisplayName(ac,
833 (const char *) rdn_value->data));
834 if (ret != LDB_SUCCESS) {
840 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
843 guid = GUID_random();
844 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
845 if (ret != LDB_SUCCESS) {
851 def_obj_cat_val = ldb_msg_find_ldb_val(ac->msg,
852 "defaultObjectCategory");
853 if (def_obj_cat_val != NULL) {
854 /* "defaultObjectCategory" has been set by the caller.
855 * Do some checks for consistency.
856 * NOTE: The real constraint check (that
857 * 'defaultObjectCategory' is the DN of the new
858 * objectclass or any parent of it) is still incomplete.
859 * For now we say that 'defaultObjectCategory' is valid
860 * if it exists and it is of objectclass "classSchema".
862 ac->dn = ldb_dn_from_ldb_val(ac, ldb, def_obj_cat_val);
863 if (ac->dn == NULL) {
864 ldb_set_errstring(ldb,
865 "Invalid DN for 'defaultObjectCategory'!");
866 return LDB_ERR_CONSTRAINT_VIOLATION;
869 /* "defaultObjectCategory" has not been set by the
870 * caller. Use the entry DN for it. */
871 ac->dn = ac->msg->dn;
873 ret = samdb_msg_add_string(ldb, ac, ac->msg,
874 "defaultObjectCategory",
875 ldb_dn_get_linearized(ac->dn));
876 if (ret != LDB_SUCCESS) {
882 ret = samldb_add_step(ac, samldb_add_entry);
883 if (ret != LDB_SUCCESS) return ret;
885 /* Now perform the checks for the 'defaultObjectCategory'. The
886 * lookup DN was already saved in "ac->dn" */
887 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
888 if (ret != LDB_SUCCESS) return ret;
890 return samldb_first_step(ac);
891 } else if (strcmp(ac->type, "attributeSchema") == 0) {
892 const struct ldb_val *rdn_value;
893 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
894 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
895 /* the RDN has prefix "CN" */
896 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
897 samdb_cn_to_lDAPDisplayName(ac,
898 (const char *) rdn_value->data));
899 if (ret != LDB_SUCCESS) {
905 /* do not allow to mark an attributeSchema as RODC filtered if it
906 * is system-critical */
907 if (check_rodc_critical_attribute(ac->msg)) {
908 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical attribute with RODC filtering",
909 ldb_dn_get_linearized(ac->msg->dn));
910 return LDB_ERR_UNWILLING_TO_PERFORM;
913 ret = samdb_find_or_add_attribute(ldb, ac->msg,
914 "isSingleValued", "FALSE");
915 if (ret != LDB_SUCCESS) return ret;
917 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
920 guid = GUID_random();
921 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
922 if (ret != LDB_SUCCESS) {
928 /* handle msDS-IntID attribute */
929 ret = samldb_add_handle_msDS_IntId(ac);
930 if (ret != LDB_SUCCESS) return ret;
932 ret = samldb_add_step(ac, samldb_add_entry);
933 if (ret != LDB_SUCCESS) return ret;
935 return samldb_first_step(ac);
937 ldb_asprintf_errstring(ldb,
938 "Invalid entry type!");
939 return LDB_ERR_OPERATIONS_ERROR;
942 rodc_control = ldb_request_get_control(ac->req, LDB_CONTROL_RODC_DCPROMO_OID);
944 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
945 rodc_control->critical = false;
946 ret = samldb_add_step(ac, samldb_rodc_add);
947 if (ret != LDB_SUCCESS) return ret;
950 /* check if we have a valid sAMAccountName */
951 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
952 if (ret != LDB_SUCCESS) return ret;
954 /* check account_type/group_type */
955 ret = samldb_add_step(ac, samldb_check_samAccountType);
956 if (ret != LDB_SUCCESS) return ret;
958 /* check if we have a valid primary group ID */
959 if (strcmp(ac->type, "user") == 0) {
960 ret = samldb_add_step(ac, samldb_check_primaryGroupID_1);
961 if (ret != LDB_SUCCESS) return ret;
962 ret = samldb_add_step(ac, samldb_dn_from_sid);
963 if (ret != LDB_SUCCESS) return ret;
964 ret = samldb_add_step(ac, samldb_check_primaryGroupID_2);
965 if (ret != LDB_SUCCESS) return ret;
968 lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
969 struct loadparm_context);
971 /* don't allow objectSID to be specified without the RELAX control */
972 ac->sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
973 if (ac->sid && !ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) &&
974 !dsdb_module_am_system(ac->module)) {
975 ldb_asprintf_errstring(ldb, "No SID may be specified in user/group creation for %s",
976 ldb_dn_get_linearized(ac->msg->dn));
977 return LDB_ERR_UNWILLING_TO_PERFORM;
981 sid_generator = lpcfg_sid_generator(lp_ctx);
982 if (sid_generator == SID_GENERATOR_INTERNAL) {
983 ret = samldb_add_step(ac, samldb_allocate_sid);
984 if (ret != LDB_SUCCESS) return ret;
988 /* finally proceed with adding the entry */
989 ret = samldb_add_step(ac, samldb_add_entry);
990 if (ret != LDB_SUCCESS) return ret;
992 return samldb_first_step(ac);
995 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
997 struct ldb_context *ldb;
1000 ldb = ldb_module_get_ctx(ac->module);
1002 ac->sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
1003 if (ac->sid == NULL) {
1004 ac->sid = dom_sid_parse_talloc(ac->msg,
1005 (const char *)ldb_dn_get_rdn_val(ac->msg->dn)->data);
1007 ldb_set_errstring(ldb,
1008 "No valid SID found in "
1009 "ForeignSecurityPrincipal CN!");
1011 return LDB_ERR_CONSTRAINT_VIOLATION;
1013 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", ac->sid)) {
1015 return ldb_operr(ldb);
1019 /* finally proceed with adding the entry */
1020 ret = samldb_add_step(ac, samldb_add_entry);
1021 if (ret != LDB_SUCCESS) return ret;
1023 return samldb_first_step(ac);
1026 static int samldb_schema_info_update(struct samldb_ctx *ac)
1029 struct ldb_context *ldb;
1030 struct dsdb_schema *schema;
1032 /* replicated update should always go through */
1033 if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1037 /* do not update schemaInfo during provisioning */
1038 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
1042 ldb = ldb_module_get_ctx(ac->module);
1043 schema = dsdb_get_schema(ldb, NULL);
1045 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
1046 "samldb_schema_info_update: no dsdb_schema loaded");
1047 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
1048 return ldb_operr(ldb);
1051 werr = dsdb_module_schema_info_update(ac->module, schema, DSDB_FLAG_NEXT_MODULE);
1052 if (!W_ERROR_IS_OK(werr)) {
1053 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
1054 "samldb_schema_info_update: "
1055 "dsdb_module_schema_info_update failed with %s",
1057 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
1058 return ldb_operr(ldb);
1065 static int samldb_prim_group_change(struct samldb_ctx *ac)
1067 struct ldb_context *ldb;
1068 const char * attrs[] = { "primaryGroupID", "memberOf", NULL };
1069 struct ldb_result *res;
1070 struct ldb_message_element *el;
1071 struct ldb_message *msg;
1073 struct dom_sid *sid;
1074 struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
1077 ldb = ldb_module_get_ctx(ac->module);
1079 /* Fetch informations from the existing object */
1081 ret = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1083 if (ret != LDB_SUCCESS) {
1087 /* Finds out the DN of the old primary group */
1089 rid = samdb_result_uint(res->msgs[0], "primaryGroupID", (uint32_t) -1);
1090 if (rid == (uint32_t) -1) {
1091 /* User objects do always have a mandatory "primaryGroupID"
1092 * attribute. If this doesn't exist then the object is of the
1093 * wrong type. This is the exact Windows error code */
1094 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1097 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1099 return ldb_operr(ldb);
1102 prev_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSID=%s)",
1103 dom_sid_string(ac, sid));
1104 if (prev_prim_group_dn == NULL) {
1105 return ldb_operr(ldb);
1108 /* Finds out the DN of the new primary group */
1110 rid = samdb_result_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
1111 if (rid == (uint32_t) -1) {
1112 /* we aren't affected of any primary group change */
1116 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1118 return ldb_operr(ldb);
1121 new_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSID=%s)",
1122 dom_sid_string(ac, sid));
1123 if (new_prim_group_dn == NULL) {
1124 /* Here we know if the specified new primary group candidate is
1126 return LDB_ERR_UNWILLING_TO_PERFORM;
1129 /* Only update the "member" attributes when we really do have a change */
1130 if (ldb_dn_compare(new_prim_group_dn, prev_prim_group_dn) != 0) {
1131 /* We need to be already a normal member of the new primary
1132 * group in order to be successful. */
1133 el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
1134 ldb_dn_get_linearized(new_prim_group_dn));
1136 return LDB_ERR_UNWILLING_TO_PERFORM;
1139 /* Remove the "member" attribute on the new primary group */
1140 msg = talloc_zero(ac, struct ldb_message);
1141 msg->dn = new_prim_group_dn;
1143 ret = samdb_msg_add_delval(ldb, ac, msg, "member",
1144 ldb_dn_get_linearized(ac->msg->dn));
1145 if (ret != LDB_SUCCESS) {
1149 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1150 if (ret != LDB_SUCCESS) {
1154 /* Add a "member" attribute for the previous primary group */
1155 msg = talloc_zero(ac, struct ldb_message);
1156 msg->dn = prev_prim_group_dn;
1158 ret = samdb_msg_add_addval(ldb, ac, msg, "member",
1159 ldb_dn_get_linearized(ac->msg->dn));
1160 if (ret != LDB_SUCCESS) {
1164 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1165 if (ret != LDB_SUCCESS) {
1174 static int samldb_member_check(struct samldb_ctx *ac)
1176 struct ldb_context *ldb;
1177 struct ldb_message_element *el;
1178 struct ldb_dn *member_dn, *group_dn;
1179 uint32_t prim_group_rid;
1180 struct dom_sid *sid;
1183 ldb = ldb_module_get_ctx(ac->module);
1185 el = ldb_msg_find_element(ac->msg, "member");
1187 /* we aren't affected */
1191 for (i = 0; i < el->num_values; i++) {
1192 /* Denies to add "member"s to groups which are primary ones
1194 member_dn = ldb_dn_from_ldb_val(ac, ldb, &el->values[i]);
1195 if (!ldb_dn_validate(member_dn)) {
1196 return ldb_operr(ldb);
1199 prim_group_rid = samdb_search_uint(ldb, ac, (uint32_t) -1,
1200 member_dn, "primaryGroupID",
1202 if (prim_group_rid == (uint32_t) -1) {
1203 /* the member hasn't to be a user account -> therefore
1204 * no check needed in this case. */
1208 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1211 return ldb_operr(ldb);
1214 group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSID=%s)",
1215 dom_sid_string(ac, sid));
1216 if (group_dn == NULL) {
1217 return ldb_operr(ldb);
1220 if (ldb_dn_compare(group_dn, ac->msg->dn) == 0) {
1221 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1229 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
1231 struct ldb_context *ldb;
1232 struct dom_sid *sid;
1237 ldb = ldb_module_get_ctx(ac->module);
1239 /* Finds out the SID/RID of the SAM object */
1240 sid = samdb_search_dom_sid(ldb, ac, ac->req->op.del.dn, "objectSID",
1243 /* No SID - it might not be a SAM object - therefore ok */
1246 status = dom_sid_split_rid(ac, sid, NULL, &rid);
1247 if (!NT_STATUS_IS_OK(status)) {
1248 return ldb_operr(ldb);
1251 /* Special object (security principal?) */
1255 /* Deny delete requests from groups which are primary ones */
1256 count = samdb_search_count(ldb, NULL,
1257 "(&(primaryGroupID=%u)(objectClass=user))",
1260 return ldb_operr(ldb);
1263 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1271 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
1273 struct ldb_context *ldb;
1274 struct samldb_ctx *ac;
1277 ldb = ldb_module_get_ctx(module);
1278 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
1280 /* do not manipulate our control entries */
1281 if (ldb_dn_is_special(req->op.add.message->dn)) {
1282 return ldb_next_request(module, req);
1285 ac = samldb_ctx_init(module, req);
1287 return ldb_operr(ldb);
1290 /* build the new msg */
1291 ac->msg = ldb_msg_copy(ac, ac->req->op.add.message);
1294 ldb_debug(ldb, LDB_DEBUG_FATAL,
1295 "samldb_add: ldb_msg_copy failed!\n");
1296 return ldb_operr(ldb);
1299 if (samdb_find_attribute(ldb, ac->msg,
1300 "objectclass", "user") != NULL) {
1301 return samldb_fill_object(ac, "user");
1304 if (samdb_find_attribute(ldb, ac->msg,
1305 "objectclass", "group") != NULL) {
1306 return samldb_fill_object(ac, "group");
1309 /* perhaps a foreignSecurityPrincipal? */
1310 if (samdb_find_attribute(ldb, ac->msg,
1312 "foreignSecurityPrincipal") != NULL) {
1313 return samldb_fill_foreignSecurityPrincipal_object(ac);
1316 if (samdb_find_attribute(ldb, ac->msg,
1317 "objectclass", "classSchema") != NULL) {
1318 ret = samldb_schema_info_update(ac);
1319 if (ret != LDB_SUCCESS) {
1324 return samldb_fill_object(ac, "classSchema");
1327 if (samdb_find_attribute(ldb, ac->msg,
1328 "objectclass", "attributeSchema") != NULL) {
1329 ret = samldb_schema_info_update(ac);
1330 if (ret != LDB_SUCCESS) {
1335 return samldb_fill_object(ac, "attributeSchema");
1340 /* nothing matched, go on */
1341 return ldb_next_request(module, req);
1345 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
1347 struct ldb_context *ldb;
1348 struct samldb_ctx *ac;
1349 struct ldb_message *msg;
1350 struct ldb_message_element *el, *el2;
1352 uint32_t account_type;
1354 if (ldb_dn_is_special(req->op.mod.message->dn)) {
1355 /* do not manipulate our control entries */
1356 return ldb_next_request(module, req);
1359 ldb = ldb_module_get_ctx(module);
1361 if (ldb_msg_find_element(req->op.mod.message, "sAMAccountType") != NULL) {
1362 ldb_asprintf_errstring(ldb,
1363 "sAMAccountType must not be specified!");
1364 return LDB_ERR_UNWILLING_TO_PERFORM;
1367 /* msDS-IntId is not allowed to be modified
1368 * except when modification comes from replication */
1369 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
1370 if (!ldb_request_get_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1371 return LDB_ERR_CONSTRAINT_VIOLATION;
1375 ac = samldb_ctx_init(module, req);
1377 return ldb_operr(ldb);
1380 /* TODO: do not modify original request, create a new one */
1382 el = ldb_msg_find_element(req->op.mod.message, "groupType");
1383 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1384 uint32_t group_type;
1386 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1387 req->op.mod.message);
1389 group_type = strtoul((const char *)el->values[0].data, NULL, 0);
1390 account_type = ds_gtype2atype(group_type);
1391 ret = samdb_msg_add_uint(ldb, msg, msg,
1394 if (ret != LDB_SUCCESS) {
1397 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1398 el2->flags = LDB_FLAG_MOD_REPLACE;
1400 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
1401 return LDB_ERR_UNWILLING_TO_PERFORM;
1404 el = ldb_msg_find_element(req->op.mod.message, "primaryGroupID");
1405 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1406 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1407 req->op.mod.message);
1409 ret = samldb_prim_group_change(ac);
1410 if (ret != LDB_SUCCESS) {
1414 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
1415 return LDB_ERR_UNWILLING_TO_PERFORM;
1418 el = ldb_msg_find_element(req->op.mod.message, "userAccountControl");
1419 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1420 uint32_t user_account_control;
1422 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1423 req->op.mod.message);
1425 user_account_control = strtoul((const char *)el->values[0].data,
1427 account_type = ds_uf2atype(user_account_control);
1428 ret = samdb_msg_add_uint(ldb, msg, msg,
1431 if (ret != LDB_SUCCESS) {
1434 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1435 el2->flags = LDB_FLAG_MOD_REPLACE;
1437 if (user_account_control & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
1438 ret = samdb_msg_add_string(ldb, msg, msg,
1439 "isCriticalSystemObject", "TRUE");
1440 if (ret != LDB_SUCCESS) {
1443 el2 = ldb_msg_find_element(msg, "isCriticalSystemObject");
1444 el2->flags = LDB_FLAG_MOD_REPLACE;
1446 /* DCs have primaryGroupID of DOMAIN_RID_DCS */
1447 if (!ldb_msg_find_element(msg, "primaryGroupID")) {
1449 if (user_account_control & UF_SERVER_TRUST_ACCOUNT) {
1450 rid = DOMAIN_RID_DCS;
1453 rid = DOMAIN_RID_READONLY_DCS;
1455 ret = samdb_msg_add_uint(ldb, msg, msg,
1456 "primaryGroupID", rid);
1457 if (ret != LDB_SUCCESS) {
1460 el2 = ldb_msg_find_element(msg, "primaryGroupID");
1461 el2->flags = LDB_FLAG_MOD_REPLACE;
1465 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
1466 return LDB_ERR_UNWILLING_TO_PERFORM;
1469 el = ldb_msg_find_element(req->op.mod.message, "member");
1470 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1471 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1472 req->op.mod.message);
1474 ret = samldb_member_check(ac);
1475 if (ret != LDB_SUCCESS) {
1480 return ldb_next_request(module, req);
1484 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
1486 struct samldb_ctx *ac;
1489 if (ldb_dn_is_special(req->op.del.dn)) {
1490 /* do not manipulate our control entries */
1491 return ldb_next_request(module, req);
1494 ac = samldb_ctx_init(module, req);
1496 return ldb_operr(ldb_module_get_ctx(module));
1498 ret = samldb_prim_group_users_check(ac);
1499 if (ret != LDB_SUCCESS) {
1503 return ldb_next_request(module, req);
1506 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
1508 struct ldb_context *ldb = ldb_module_get_ctx(module);
1509 struct dsdb_fsmo_extended_op *exop;
1512 exop = talloc_get_type(req->op.extended.data, struct dsdb_fsmo_extended_op);
1514 ldb_debug(ldb, LDB_DEBUG_FATAL, "samldb_extended_allocate_rid_pool: invalid extended data\n");
1515 return LDB_ERR_PROTOCOL_ERROR;
1518 ret = ridalloc_allocate_rid_pool_fsmo(module, exop);
1519 if (ret != LDB_SUCCESS) {
1523 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
1526 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
1528 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
1529 return samldb_extended_allocate_rid_pool(module, req);
1532 return ldb_next_request(module, req);
1536 _PUBLIC_ const struct ldb_module_ops ldb_samldb_module_ops = {
1539 .modify = samldb_modify,
1540 .del = samldb_delete,
1541 .extended = samldb_extended