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 /* primaryGroupID handling */
255 static int samldb_check_primaryGroupID(struct samldb_ctx *ac)
257 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
258 struct ldb_dn *prim_group_dn;
263 rid = samdb_result_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
264 if (rid == (uint32_t) -1) {
265 rid = DOMAIN_RID_USERS;
266 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
267 "primaryGroupID", rid);
268 if (ret != LDB_SUCCESS) {
273 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
275 return ldb_operr(ldb);
278 prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSID=%s)",
279 dom_sid_string(ac, sid));
280 if (prim_group_dn == NULL) {
281 ldb_asprintf_errstring(ldb,
282 "Failed to find primary group with RID %u!",
284 return LDB_ERR_UNWILLING_TO_PERFORM;
287 return samldb_next_step(ac);
291 static bool samldb_msg_add_sid(struct ldb_message *msg,
293 const struct dom_sid *sid)
296 enum ndr_err_code ndr_err;
298 ndr_err = ndr_push_struct_blob(&v, msg, sid,
299 (ndr_push_flags_fn_t)ndr_push_dom_sid);
300 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
303 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
307 /* allocate a SID using our RID Set */
308 static int samldb_allocate_sid(struct samldb_ctx *ac)
312 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
315 ret = ridalloc_allocate_rid(ac->module, &rid);
316 if (ret != LDB_SUCCESS) {
320 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
322 return ldb_module_oom(ac->module);
325 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
326 return ldb_operr(ldb);
329 return samldb_next_step(ac);
333 see if a krbtgt_number is available
335 static bool samldb_krbtgtnumber_available(struct samldb_ctx *ac, unsigned krbtgt_number)
337 TALLOC_CTX *tmp_ctx = talloc_new(ac);
338 struct ldb_result *res;
339 const char *attrs[] = { NULL };
342 ret = dsdb_module_search(ac->module, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
343 attrs, DSDB_FLAG_NEXT_MODULE,
344 "msDC-SecondaryKrbTgtNumber=%u", krbtgt_number);
345 if (ret == LDB_SUCCESS && res->count == 0) {
346 talloc_free(tmp_ctx);
349 talloc_free(tmp_ctx);
353 /* special handling for add in RODC join */
354 static int samldb_rodc_add(struct samldb_ctx *ac)
356 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
357 unsigned krbtgt_number, i_start, i;
360 /* find a unused msDC-SecondaryKrbTgtNumber */
361 i_start = generate_random() & 0xFFFF;
366 for (i=i_start; i<=0xFFFF; i++) {
367 if (samldb_krbtgtnumber_available(ac, i)) {
372 for (i=1; i<i_start; i++) {
373 if (samldb_krbtgtnumber_available(ac, i)) {
379 ldb_asprintf_errstring(ldb_module_get_ctx(ac->module),
380 "%08X: Unable to find available msDS-SecondaryKrbTgtNumber",
381 W_ERROR_V(WERR_NO_SYSTEM_RESOURCES));
382 return LDB_ERR_OTHER;
385 ret = ldb_msg_add_empty(ac->msg, "msDS-SecondaryKrbTgtNumber", LDB_FLAG_INTERNAL_DISABLE_VALIDATION, NULL);
386 if (ret != LDB_SUCCESS) {
387 return ldb_operr(ldb);
390 ret = ldb_msg_add_fmt(ac->msg, "msDS-SecondaryKrbTgtNumber", "%u", krbtgt_number);
391 if (ret != LDB_SUCCESS) {
392 return ldb_operr(ldb);
395 ret = ldb_msg_add_fmt(ac->msg, "sAMAccountName", "krbtgt_%u", krbtgt_number);
396 if (ret != LDB_SUCCESS) {
397 return ldb_operr(ldb);
400 return samldb_next_step(ac);
405 * samldb_find_for_defaultObjectCategory (async)
408 static int samldb_find_for_defaultObjectCategory_callback(struct ldb_request *req,
409 struct ldb_reply *ares)
411 struct ldb_context *ldb;
412 struct samldb_ctx *ac;
415 ac = talloc_get_type(req->context, struct samldb_ctx);
416 ldb = ldb_module_get_ctx(ac->module);
419 ret = LDB_ERR_OPERATIONS_ERROR;
422 if (ares->error != LDB_SUCCESS) {
423 if (ares->error == LDB_ERR_NO_SUCH_OBJECT) {
424 if (ldb_request_get_control(ac->req,
425 LDB_CONTROL_RELAX_OID) != NULL) {
426 /* Don't be pricky when the DN doesn't exist */
427 /* if we have the RELAX control specified */
428 ac->res_dn = req->op.search.base;
429 return samldb_next_step(ac);
431 ldb_set_errstring(ldb,
432 "samldb_find_defaultObjectCategory: "
433 "Invalid DN for 'defaultObjectCategory'!");
434 ares->error = LDB_ERR_CONSTRAINT_VIOLATION;
438 return ldb_module_done(ac->req, ares->controls,
439 ares->response, ares->error);
442 switch (ares->type) {
443 case LDB_REPLY_ENTRY:
444 ac->res_dn = talloc_steal(ac, ares->message->dn);
449 case LDB_REPLY_REFERRAL:
458 if (ac->res_dn != NULL) {
459 /* when found go on */
460 ret = samldb_next_step(ac);
462 ret = LDB_ERR_OPERATIONS_ERROR;
468 if (ret != LDB_SUCCESS) {
469 return ldb_module_done(ac->req, NULL, NULL, ret);
475 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
477 struct ldb_context *ldb;
478 struct ldb_request *req;
479 static const char *no_attrs[] = { NULL };
482 ldb = ldb_module_get_ctx(ac->module);
486 ret = ldb_build_search_req(&req, ldb, ac,
487 ac->dn, LDB_SCOPE_BASE,
488 "(objectClass=classSchema)", no_attrs,
490 samldb_find_for_defaultObjectCategory_callback,
492 if (ret != LDB_SUCCESS) {
496 ret = dsdb_request_add_controls(req,
497 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT);
498 if (ret != LDB_SUCCESS) {
502 return ldb_next_request(ac->module, req);
506 * msDS-IntId attributeSchema attribute handling
507 * during LDB_ADD request processing
509 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
514 uint32_t system_flags;
515 struct ldb_context *ldb;
516 struct ldb_result *ldb_res;
517 struct ldb_dn *schema_dn;
519 ldb = ldb_module_get_ctx(ac->module);
520 schema_dn = ldb_get_schema_basedn(ldb);
522 /* replicated update should always go through */
523 if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
527 /* msDS-IntId is handled by system and should never be
528 * passed by clients */
529 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
530 return LDB_ERR_UNWILLING_TO_PERFORM;
533 /* do not generate msDS-IntId if Relax control is passed */
534 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
538 /* check Functional Level */
539 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
543 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
544 system_flags = ldb_msg_find_attr_as_uint(ac->msg, "systemFlags", 0);
545 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
549 /* Generate new value for msDs-IntId
550 * Value should be in 0x80000000..0xBFFFFFFF range */
551 msds_intid = generate_random() % 0X3FFFFFFF;
552 msds_intid += 0x80000000;
554 /* probe id values until unique one is found */
557 if (msds_intid > 0xBFFFFFFF) {
558 msds_intid = 0x80000001;
561 ret = dsdb_module_search(ac->module, ac,
563 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
564 DSDB_FLAG_NEXT_MODULE,
565 "(msDS-IntId=%d)", msds_intid);
566 if (ret != LDB_SUCCESS) {
567 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
568 __location__": Searching for msDS-IntId=%d failed - %s\n",
571 return ldb_operr(ldb);
573 id_exists = (ldb_res->count > 0);
575 talloc_free(ldb_res);
578 return ldb_msg_add_fmt(ac->msg, "msDS-IntId", "%d", msds_intid);
583 * samldb_add_entry (async)
586 static int samldb_add_entry_callback(struct ldb_request *req,
587 struct ldb_reply *ares)
589 struct ldb_context *ldb;
590 struct samldb_ctx *ac;
593 ac = talloc_get_type(req->context, struct samldb_ctx);
594 ldb = ldb_module_get_ctx(ac->module);
597 return ldb_module_done(ac->req, NULL, NULL,
598 LDB_ERR_OPERATIONS_ERROR);
601 if (ares->type == LDB_REPLY_REFERRAL) {
602 return ldb_module_send_referral(ac->req, ares->referral);
605 if (ares->error != LDB_SUCCESS) {
606 return ldb_module_done(ac->req, ares->controls,
607 ares->response, ares->error);
609 if (ares->type != LDB_REPLY_DONE) {
610 ldb_set_errstring(ldb,
611 "Invalid reply type!\n");
612 return ldb_module_done(ac->req, NULL, NULL,
613 LDB_ERR_OPERATIONS_ERROR);
616 /* The caller may wish to get controls back from the add */
617 ac->ares = talloc_steal(ac, ares);
619 ret = samldb_next_step(ac);
620 if (ret != LDB_SUCCESS) {
621 return ldb_module_done(ac->req, NULL, NULL, ret);
626 static int samldb_add_entry(struct samldb_ctx *ac)
628 struct ldb_context *ldb;
629 struct ldb_request *req;
632 ldb = ldb_module_get_ctx(ac->module);
634 ret = ldb_build_add_req(&req, ldb, ac,
637 ac, samldb_add_entry_callback,
639 if (ret != LDB_SUCCESS) {
643 return ldb_next_request(ac->module, req);
647 * return true if msg carries an attributeSchema that is intended to be RODC
648 * filtered but is also a system-critical attribute.
650 static bool check_rodc_critical_attribute(struct ldb_message *msg)
652 uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
654 schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
655 searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
656 rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE | SEARCH_FLAG_CONFIDENTIAL);
658 if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
659 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
667 static int samldb_fill_object(struct samldb_ctx *ac, const char *type)
669 struct ldb_context *ldb;
670 struct loadparm_context *lp_ctx;
671 enum sid_generator sid_generator;
673 struct ldb_control *rodc_control;
676 ldb = ldb_module_get_ctx(ac->module);
678 /* Add informations for the different account types */
680 if (strcmp(ac->type, "user") == 0) {
681 ret = samdb_find_or_add_attribute(ldb, ac->msg,
682 "userAccountControl", "546");
683 if (ret != LDB_SUCCESS) return ret;
684 ret = samdb_find_or_add_attribute(ldb, ac->msg,
686 if (ret != LDB_SUCCESS) return ret;
687 ret = samdb_find_or_add_attribute(ldb, ac->msg,
689 if (ret != LDB_SUCCESS) return ret;
690 ret = samdb_find_or_add_attribute(ldb, ac->msg,
692 if (ret != LDB_SUCCESS) return ret;
693 ret = samdb_find_or_add_attribute(ldb, ac->msg,
694 "badPasswordTime", "0");
695 if (ret != LDB_SUCCESS) return ret;
696 ret = samdb_find_or_add_attribute(ldb, ac->msg,
698 if (ret != LDB_SUCCESS) return ret;
699 ret = samdb_find_or_add_attribute(ldb, ac->msg,
701 if (ret != LDB_SUCCESS) return ret;
702 ret = samdb_find_or_add_attribute(ldb, ac->msg,
704 if (ret != LDB_SUCCESS) return ret;
705 ret = samdb_find_or_add_attribute(ldb, ac->msg,
706 "accountExpires", "9223372036854775807");
707 if (ret != LDB_SUCCESS) return ret;
708 ret = samdb_find_or_add_attribute(ldb, ac->msg,
710 if (ret != LDB_SUCCESS) return ret;
711 } else if (strcmp(ac->type, "group") == 0) {
712 ret = samdb_find_or_add_attribute(ldb, ac->msg,
713 "groupType", "-2147483646");
714 if (ret != LDB_SUCCESS) return ret;
715 } else if (strcmp(ac->type, "classSchema") == 0) {
716 const struct ldb_val *rdn_value, *def_obj_cat_val;
718 ret = samdb_find_or_add_attribute(ldb, ac->msg,
720 if (ret != LDB_SUCCESS) return ret;
722 /* do not allow to mark an attributeSchema as RODC filtered if it
723 * is system-critical */
724 if (check_rodc_critical_attribute(ac->msg)) {
725 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
726 ldb_dn_get_linearized(ac->msg->dn));
727 return LDB_ERR_UNWILLING_TO_PERFORM;
731 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
732 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
733 /* the RDN has prefix "CN" */
734 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
735 samdb_cn_to_lDAPDisplayName(ac,
736 (const char *) rdn_value->data));
737 if (ret != LDB_SUCCESS) {
743 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
746 guid = GUID_random();
747 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
748 if (ret != LDB_SUCCESS) {
754 def_obj_cat_val = ldb_msg_find_ldb_val(ac->msg,
755 "defaultObjectCategory");
756 if (def_obj_cat_val != NULL) {
757 /* "defaultObjectCategory" has been set by the caller.
758 * Do some checks for consistency.
759 * NOTE: The real constraint check (that
760 * 'defaultObjectCategory' is the DN of the new
761 * objectclass or any parent of it) is still incomplete.
762 * For now we say that 'defaultObjectCategory' is valid
763 * if it exists and it is of objectclass "classSchema".
765 ac->dn = ldb_dn_from_ldb_val(ac, ldb, def_obj_cat_val);
766 if (ac->dn == NULL) {
767 ldb_set_errstring(ldb,
768 "Invalid DN for 'defaultObjectCategory'!");
769 return LDB_ERR_CONSTRAINT_VIOLATION;
772 /* "defaultObjectCategory" has not been set by the
773 * caller. Use the entry DN for it. */
774 ac->dn = ac->msg->dn;
776 ret = samdb_msg_add_string(ldb, ac, ac->msg,
777 "defaultObjectCategory",
778 ldb_dn_get_linearized(ac->dn));
779 if (ret != LDB_SUCCESS) {
785 ret = samldb_add_step(ac, samldb_add_entry);
786 if (ret != LDB_SUCCESS) return ret;
788 /* Now perform the checks for the 'defaultObjectCategory'. The
789 * lookup DN was already saved in "ac->dn" */
790 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
791 if (ret != LDB_SUCCESS) return ret;
793 return samldb_first_step(ac);
794 } else if (strcmp(ac->type, "attributeSchema") == 0) {
795 const struct ldb_val *rdn_value;
796 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
797 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
798 /* the RDN has prefix "CN" */
799 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
800 samdb_cn_to_lDAPDisplayName(ac,
801 (const char *) rdn_value->data));
802 if (ret != LDB_SUCCESS) {
808 /* do not allow to mark an attributeSchema as RODC filtered if it
809 * is system-critical */
810 if (check_rodc_critical_attribute(ac->msg)) {
811 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical attribute with RODC filtering",
812 ldb_dn_get_linearized(ac->msg->dn));
813 return LDB_ERR_UNWILLING_TO_PERFORM;
816 ret = samdb_find_or_add_attribute(ldb, ac->msg,
817 "isSingleValued", "FALSE");
818 if (ret != LDB_SUCCESS) return ret;
820 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
823 guid = GUID_random();
824 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
825 if (ret != LDB_SUCCESS) {
831 /* handle msDS-IntID attribute */
832 ret = samldb_add_handle_msDS_IntId(ac);
833 if (ret != LDB_SUCCESS) return ret;
835 ret = samldb_add_step(ac, samldb_add_entry);
836 if (ret != LDB_SUCCESS) return ret;
838 return samldb_first_step(ac);
840 ldb_asprintf_errstring(ldb,
841 "Invalid entry type!");
842 return LDB_ERR_OPERATIONS_ERROR;
845 rodc_control = ldb_request_get_control(ac->req, LDB_CONTROL_RODC_DCPROMO_OID);
847 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
848 rodc_control->critical = false;
849 ret = samldb_add_step(ac, samldb_rodc_add);
850 if (ret != LDB_SUCCESS) return ret;
853 /* check if we have a valid sAMAccountName */
854 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
855 if (ret != LDB_SUCCESS) return ret;
857 /* check account_type/group_type */
858 ret = samldb_add_step(ac, samldb_check_sAMAccountType);
859 if (ret != LDB_SUCCESS) return ret;
861 /* check if we have a valid primary group ID */
862 if (strcmp(ac->type, "user") == 0) {
863 ret = samldb_add_step(ac, samldb_check_primaryGroupID);
864 if (ret != LDB_SUCCESS) return ret;
867 lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
868 struct loadparm_context);
870 /* don't allow objectSID to be specified without the RELAX control */
871 sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
872 if (sid && !ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) &&
873 !dsdb_module_am_system(ac->module)) {
874 ldb_asprintf_errstring(ldb, "No SID may be specified in user/group creation for %s",
875 ldb_dn_get_linearized(ac->msg->dn));
876 return LDB_ERR_UNWILLING_TO_PERFORM;
880 sid_generator = lpcfg_sid_generator(lp_ctx);
881 if (sid_generator == SID_GENERATOR_INTERNAL) {
882 ret = samldb_add_step(ac, samldb_allocate_sid);
883 if (ret != LDB_SUCCESS) return ret;
887 /* finally proceed with adding the entry */
888 ret = samldb_add_step(ac, samldb_add_entry);
889 if (ret != LDB_SUCCESS) return ret;
891 return samldb_first_step(ac);
894 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
896 struct ldb_context *ldb;
900 ldb = ldb_module_get_ctx(ac->module);
902 sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
904 sid = dom_sid_parse_talloc(ac->msg,
905 (const char *)ldb_dn_get_rdn_val(ac->msg->dn)->data);
907 ldb_set_errstring(ldb,
908 "No valid SID found in "
909 "ForeignSecurityPrincipal CN!");
910 return LDB_ERR_CONSTRAINT_VIOLATION;
912 if (! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
913 return ldb_operr(ldb);
917 /* finally proceed with adding the entry */
918 ret = samldb_add_step(ac, samldb_add_entry);
919 if (ret != LDB_SUCCESS) return ret;
921 return samldb_first_step(ac);
924 static int samldb_schema_info_update(struct samldb_ctx *ac)
927 struct ldb_context *ldb;
928 struct dsdb_schema *schema;
930 /* replicated update should always go through */
931 if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
935 /* do not update schemaInfo during provisioning */
936 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
940 ldb = ldb_module_get_ctx(ac->module);
941 schema = dsdb_get_schema(ldb, NULL);
943 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
944 "samldb_schema_info_update: no dsdb_schema loaded");
945 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
946 return ldb_operr(ldb);
949 werr = dsdb_module_schema_info_update(ac->module, schema, DSDB_FLAG_NEXT_MODULE);
950 if (!W_ERROR_IS_OK(werr)) {
951 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
952 "samldb_schema_info_update: "
953 "dsdb_module_schema_info_update failed with %s",
955 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
956 return ldb_operr(ldb);
963 static int samldb_prim_group_change(struct samldb_ctx *ac)
965 struct ldb_context *ldb;
966 const char * attrs[] = { "primaryGroupID", "memberOf", NULL };
967 struct ldb_result *res;
968 struct ldb_message_element *el;
969 struct ldb_message *msg;
972 struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
975 ldb = ldb_module_get_ctx(ac->module);
977 /* Fetch informations from the existing object */
979 ret = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
981 if (ret != LDB_SUCCESS) {
985 /* Finds out the DN of the old primary group */
987 rid = samdb_result_uint(res->msgs[0], "primaryGroupID", (uint32_t) -1);
988 if (rid == (uint32_t) -1) {
989 /* User objects do always have a mandatory "primaryGroupID"
990 * attribute. If this doesn't exist then the object is of the
991 * wrong type. This is the exact Windows error code */
992 return LDB_ERR_OBJECT_CLASS_VIOLATION;
995 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
997 return ldb_operr(ldb);
1000 prev_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSID=%s)",
1001 dom_sid_string(ac, sid));
1002 if (prev_prim_group_dn == NULL) {
1003 return ldb_operr(ldb);
1006 /* Finds out the DN of the new primary group */
1008 rid = samdb_result_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
1009 if (rid == (uint32_t) -1) {
1010 /* we aren't affected of any primary group change */
1014 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1016 return ldb_operr(ldb);
1019 new_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSID=%s)",
1020 dom_sid_string(ac, sid));
1021 if (new_prim_group_dn == NULL) {
1022 /* Here we know if the specified new primary group candidate is
1024 return LDB_ERR_UNWILLING_TO_PERFORM;
1027 /* Only update the "member" attributes when we really do have a change */
1028 if (ldb_dn_compare(new_prim_group_dn, prev_prim_group_dn) != 0) {
1029 /* We need to be already a normal member of the new primary
1030 * group in order to be successful. */
1031 el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
1032 ldb_dn_get_linearized(new_prim_group_dn));
1034 return LDB_ERR_UNWILLING_TO_PERFORM;
1037 /* Remove the "member" attribute on the new primary group */
1038 msg = talloc_zero(ac, struct ldb_message);
1039 msg->dn = new_prim_group_dn;
1041 ret = samdb_msg_add_delval(ldb, ac, msg, "member",
1042 ldb_dn_get_linearized(ac->msg->dn));
1043 if (ret != LDB_SUCCESS) {
1047 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1048 if (ret != LDB_SUCCESS) {
1052 /* Add a "member" attribute for the previous primary group */
1053 msg = talloc_zero(ac, struct ldb_message);
1054 msg->dn = prev_prim_group_dn;
1056 ret = samdb_msg_add_addval(ldb, ac, msg, "member",
1057 ldb_dn_get_linearized(ac->msg->dn));
1058 if (ret != LDB_SUCCESS) {
1062 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1063 if (ret != LDB_SUCCESS) {
1072 static int samldb_member_check(struct samldb_ctx *ac)
1074 struct ldb_context *ldb;
1075 struct ldb_message_element *el;
1076 struct ldb_dn *member_dn, *group_dn;
1077 uint32_t prim_group_rid;
1078 struct dom_sid *sid;
1081 ldb = ldb_module_get_ctx(ac->module);
1083 el = ldb_msg_find_element(ac->msg, "member");
1085 /* we aren't affected */
1089 for (i = 0; i < el->num_values; i++) {
1090 /* Denies to add "member"s to groups which are primary ones
1092 member_dn = ldb_dn_from_ldb_val(ac, ldb, &el->values[i]);
1093 if (!ldb_dn_validate(member_dn)) {
1094 return ldb_operr(ldb);
1097 prim_group_rid = samdb_search_uint(ldb, ac, (uint32_t) -1,
1098 member_dn, "primaryGroupID",
1100 if (prim_group_rid == (uint32_t) -1) {
1101 /* the member hasn't to be a user account -> therefore
1102 * no check needed in this case. */
1106 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1109 return ldb_operr(ldb);
1112 group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSID=%s)",
1113 dom_sid_string(ac, sid));
1114 if (group_dn == NULL) {
1115 return ldb_operr(ldb);
1118 if (ldb_dn_compare(group_dn, ac->msg->dn) == 0) {
1119 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1127 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
1129 struct ldb_context *ldb;
1130 struct dom_sid *sid;
1135 ldb = ldb_module_get_ctx(ac->module);
1137 /* Finds out the SID/RID of the SAM object */
1138 sid = samdb_search_dom_sid(ldb, ac, ac->req->op.del.dn, "objectSID",
1141 /* No SID - it might not be a SAM object - therefore ok */
1144 status = dom_sid_split_rid(ac, sid, NULL, &rid);
1145 if (!NT_STATUS_IS_OK(status)) {
1146 return ldb_operr(ldb);
1149 /* Special object (security principal?) */
1153 /* Deny delete requests from groups which are primary ones */
1154 count = samdb_search_count(ldb, NULL,
1155 "(&(primaryGroupID=%u)(objectClass=user))",
1158 return ldb_operr(ldb);
1161 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1169 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
1171 struct ldb_context *ldb;
1172 struct samldb_ctx *ac;
1175 ldb = ldb_module_get_ctx(module);
1176 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
1178 /* do not manipulate our control entries */
1179 if (ldb_dn_is_special(req->op.add.message->dn)) {
1180 return ldb_next_request(module, req);
1183 ac = samldb_ctx_init(module, req);
1185 return ldb_operr(ldb);
1188 /* build the new msg */
1189 ac->msg = ldb_msg_copy(ac, ac->req->op.add.message);
1192 ldb_debug(ldb, LDB_DEBUG_FATAL,
1193 "samldb_add: ldb_msg_copy failed!\n");
1194 return ldb_operr(ldb);
1197 if (samdb_find_attribute(ldb, ac->msg,
1198 "objectclass", "user") != NULL) {
1199 return samldb_fill_object(ac, "user");
1202 if (samdb_find_attribute(ldb, ac->msg,
1203 "objectclass", "group") != NULL) {
1204 return samldb_fill_object(ac, "group");
1207 /* perhaps a foreignSecurityPrincipal? */
1208 if (samdb_find_attribute(ldb, ac->msg,
1210 "foreignSecurityPrincipal") != NULL) {
1211 return samldb_fill_foreignSecurityPrincipal_object(ac);
1214 if (samdb_find_attribute(ldb, ac->msg,
1215 "objectclass", "classSchema") != NULL) {
1216 ret = samldb_schema_info_update(ac);
1217 if (ret != LDB_SUCCESS) {
1222 return samldb_fill_object(ac, "classSchema");
1225 if (samdb_find_attribute(ldb, ac->msg,
1226 "objectclass", "attributeSchema") != NULL) {
1227 ret = samldb_schema_info_update(ac);
1228 if (ret != LDB_SUCCESS) {
1233 return samldb_fill_object(ac, "attributeSchema");
1238 /* nothing matched, go on */
1239 return ldb_next_request(module, req);
1243 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
1245 struct ldb_context *ldb;
1246 struct samldb_ctx *ac;
1247 struct ldb_message *msg;
1248 struct ldb_message_element *el, *el2;
1250 uint32_t account_type;
1252 if (ldb_dn_is_special(req->op.mod.message->dn)) {
1253 /* do not manipulate our control entries */
1254 return ldb_next_request(module, req);
1257 ldb = ldb_module_get_ctx(module);
1259 if (ldb_msg_find_element(req->op.mod.message, "sAMAccountType") != NULL) {
1260 ldb_asprintf_errstring(ldb,
1261 "sAMAccountType must not be specified!");
1262 return LDB_ERR_UNWILLING_TO_PERFORM;
1265 /* msDS-IntId is not allowed to be modified
1266 * except when modification comes from replication */
1267 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
1268 if (!ldb_request_get_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1269 return LDB_ERR_CONSTRAINT_VIOLATION;
1273 ac = samldb_ctx_init(module, req);
1275 return ldb_operr(ldb);
1278 /* TODO: do not modify original request, create a new one */
1280 el = ldb_msg_find_element(req->op.mod.message, "groupType");
1281 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1282 uint32_t group_type;
1284 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1285 req->op.mod.message);
1287 group_type = strtoul((const char *)el->values[0].data, NULL, 0);
1288 account_type = ds_gtype2atype(group_type);
1289 ret = samdb_msg_add_uint(ldb, msg, msg,
1292 if (ret != LDB_SUCCESS) {
1295 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1296 el2->flags = LDB_FLAG_MOD_REPLACE;
1298 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
1299 return LDB_ERR_UNWILLING_TO_PERFORM;
1302 el = ldb_msg_find_element(req->op.mod.message, "primaryGroupID");
1303 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1304 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1305 req->op.mod.message);
1307 ret = samldb_prim_group_change(ac);
1308 if (ret != LDB_SUCCESS) {
1312 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
1313 return LDB_ERR_UNWILLING_TO_PERFORM;
1316 el = ldb_msg_find_element(req->op.mod.message, "userAccountControl");
1317 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1318 uint32_t user_account_control;
1320 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1321 req->op.mod.message);
1323 user_account_control = strtoul((const char *)el->values[0].data,
1325 account_type = ds_uf2atype(user_account_control);
1326 ret = samdb_msg_add_uint(ldb, msg, msg,
1329 if (ret != LDB_SUCCESS) {
1332 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1333 el2->flags = LDB_FLAG_MOD_REPLACE;
1335 if (user_account_control & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
1336 ret = samdb_msg_add_string(ldb, msg, msg,
1337 "isCriticalSystemObject", "TRUE");
1338 if (ret != LDB_SUCCESS) {
1341 el2 = ldb_msg_find_element(msg, "isCriticalSystemObject");
1342 el2->flags = LDB_FLAG_MOD_REPLACE;
1344 /* DCs have primaryGroupID of DOMAIN_RID_DCS */
1345 if (!ldb_msg_find_element(msg, "primaryGroupID")) {
1347 if (user_account_control & UF_SERVER_TRUST_ACCOUNT) {
1348 rid = DOMAIN_RID_DCS;
1351 rid = DOMAIN_RID_READONLY_DCS;
1353 ret = samdb_msg_add_uint(ldb, msg, msg,
1354 "primaryGroupID", rid);
1355 if (ret != LDB_SUCCESS) {
1358 el2 = ldb_msg_find_element(msg, "primaryGroupID");
1359 el2->flags = LDB_FLAG_MOD_REPLACE;
1363 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
1364 return LDB_ERR_UNWILLING_TO_PERFORM;
1367 el = ldb_msg_find_element(req->op.mod.message, "member");
1368 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1369 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1370 req->op.mod.message);
1372 ret = samldb_member_check(ac);
1373 if (ret != LDB_SUCCESS) {
1378 return ldb_next_request(module, req);
1382 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
1384 struct samldb_ctx *ac;
1387 if (ldb_dn_is_special(req->op.del.dn)) {
1388 /* do not manipulate our control entries */
1389 return ldb_next_request(module, req);
1392 ac = samldb_ctx_init(module, req);
1394 return ldb_operr(ldb_module_get_ctx(module));
1396 ret = samldb_prim_group_users_check(ac);
1397 if (ret != LDB_SUCCESS) {
1401 return ldb_next_request(module, req);
1404 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
1406 struct ldb_context *ldb = ldb_module_get_ctx(module);
1407 struct dsdb_fsmo_extended_op *exop;
1410 exop = talloc_get_type(req->op.extended.data, struct dsdb_fsmo_extended_op);
1412 ldb_debug(ldb, LDB_DEBUG_FATAL, "samldb_extended_allocate_rid_pool: invalid extended data\n");
1413 return LDB_ERR_PROTOCOL_ERROR;
1416 ret = ridalloc_allocate_rid_pool_fsmo(module, exop);
1417 if (ret != LDB_SUCCESS) {
1421 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
1424 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
1426 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
1427 return samldb_extended_allocate_rid_pool(module, req);
1430 return ldb_next_request(module, req);
1434 _PUBLIC_ const struct ldb_module_ops ldb_samldb_module_ops = {
1437 .modify = samldb_modify,
1438 .del = samldb_delete,
1439 .extended = samldb_extended