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_ERR_OPERATIONS_ERROR;
111 if (ac->steps == NULL) {
115 if (ac->curstep == NULL)
116 return LDB_ERR_OPERATIONS_ERROR;
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_ERR_OPERATIONS_ERROR;
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);
152 static int samldb_generate_samAccountName(struct ldb_message *msg)
156 /* Format: $000000-000000000000 */
158 name = talloc_asprintf(msg, "$%.6X-%.6X%.6X",
159 (unsigned int)generate_random(),
160 (unsigned int)generate_random(),
161 (unsigned int)generate_random());
163 return LDB_ERR_OPERATIONS_ERROR;
165 return ldb_msg_add_steal_string(msg, "samAccountName", name);
169 * samldb_check_samAccountName (async)
172 static int samldb_check_samAccountName_callback(struct ldb_request *req,
173 struct ldb_reply *ares)
175 struct samldb_ctx *ac;
178 ac = talloc_get_type(req->context, struct samldb_ctx);
180 if (ares->error != LDB_SUCCESS) {
181 return ldb_module_done(ac->req, ares->controls,
182 ares->response, ares->error);
185 switch (ares->type) {
186 case LDB_REPLY_ENTRY:
187 /* if we get an entry it means this samAccountName
189 return ldb_module_done(ac->req, NULL, NULL,
190 LDB_ERR_ENTRY_ALREADY_EXISTS);
192 case LDB_REPLY_REFERRAL:
199 /* not found, go on */
201 ret = samldb_next_step(ac);
205 if (ret != LDB_SUCCESS) {
206 return ldb_module_done(ac->req, NULL, NULL, ret);
212 static int samldb_check_samAccountName(struct samldb_ctx *ac)
214 struct ldb_context *ldb;
215 struct ldb_request *req;
220 ldb = ldb_module_get_ctx(ac->module);
222 if (ldb_msg_find_element(ac->msg, "samAccountName") == NULL) {
223 ret = samldb_generate_samAccountName(ac->msg);
224 if (ret != LDB_SUCCESS) {
229 name = ldb_msg_find_attr_as_string(ac->msg, "samAccountName", NULL);
231 return LDB_ERR_OPERATIONS_ERROR;
233 filter = talloc_asprintf(ac, "samAccountName=%s",
234 ldb_binary_encode_string(ac, name));
235 if (filter == NULL) {
236 return LDB_ERR_OPERATIONS_ERROR;
239 ret = ldb_build_search_req(&req, ldb, ac,
240 ldb_get_default_basedn(ldb),
244 ac, samldb_check_samAccountName_callback,
247 if (ret != LDB_SUCCESS) {
250 return ldb_next_request(ac->module, req);
254 static int samldb_check_samAccountType(struct samldb_ctx *ac)
256 struct ldb_context *ldb;
257 unsigned int account_type;
258 unsigned int group_type;
262 ldb = ldb_module_get_ctx(ac->module);
264 /* make sure sAMAccountType is not specified */
265 if (ldb_msg_find_element(ac->msg, "sAMAccountType") != NULL) {
266 ldb_asprintf_errstring(ldb,
267 "sAMAccountType must not be specified!");
268 return LDB_ERR_UNWILLING_TO_PERFORM;
271 if (strcmp("user", ac->type) == 0) {
272 uac = samdb_result_uint(ac->msg, "userAccountControl", 0);
274 ldb_asprintf_errstring(ldb,
275 "userAccountControl invalid!");
276 return LDB_ERR_UNWILLING_TO_PERFORM;
278 account_type = ds_uf2atype(uac);
279 ret = samdb_msg_add_uint(ldb,
283 if (ret != LDB_SUCCESS) {
288 if (strcmp("group", ac->type) == 0) {
290 group_type = samdb_result_uint(ac->msg, "groupType", 0);
291 if (group_type == 0) {
292 ldb_asprintf_errstring(ldb,
293 "groupType invalid!\n");
294 return LDB_ERR_UNWILLING_TO_PERFORM;
296 account_type = ds_gtype2atype(group_type);
297 ret = samdb_msg_add_uint(ldb,
301 if (ret != LDB_SUCCESS) {
307 return samldb_next_step(ac);
310 static bool samldb_msg_add_sid(struct ldb_message *msg,
312 const struct dom_sid *sid)
315 enum ndr_err_code ndr_err;
317 ndr_err = ndr_push_struct_blob(&v, msg, sid,
318 (ndr_push_flags_fn_t)ndr_push_dom_sid);
319 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
322 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
326 /* allocate a SID using our RID Set */
327 static int samldb_allocate_sid(struct samldb_ctx *ac)
331 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
333 ret = ridalloc_allocate_rid(ac->module, &rid);
334 if (ret != LDB_SUCCESS) {
338 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
339 if (ac->sid == NULL) {
340 ldb_module_oom(ac->module);
341 return LDB_ERR_OPERATIONS_ERROR;
344 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", ac->sid)) {
345 return LDB_ERR_OPERATIONS_ERROR;
348 return samldb_next_step(ac);
352 * samldb_dn_from_sid (async)
355 static int samldb_dn_from_sid(struct samldb_ctx *ac);
357 static int samldb_dn_from_sid_callback(struct ldb_request *req,
358 struct ldb_reply *ares)
360 struct ldb_context *ldb;
361 struct samldb_ctx *ac;
364 ac = talloc_get_type(req->context, struct samldb_ctx);
365 ldb = ldb_module_get_ctx(ac->module);
368 ret = LDB_ERR_OPERATIONS_ERROR;
371 if (ares->error != LDB_SUCCESS) {
372 return ldb_module_done(ac->req, ares->controls,
373 ares->response, ares->error);
376 switch (ares->type) {
377 case LDB_REPLY_ENTRY:
379 if (ac->res_dn != NULL) {
381 ldb_set_errstring(ldb,
382 "Invalid number of results while searching "
383 "for domain objects!");
384 ret = LDB_ERR_OPERATIONS_ERROR;
387 ac->res_dn = ldb_dn_copy(ac, ares->message->dn);
393 case LDB_REPLY_REFERRAL:
402 /* found or not found, go on */
403 ret = samldb_next_step(ac);
408 if (ret != LDB_SUCCESS) {
409 return ldb_module_done(ac->req, NULL, NULL, ret);
415 /* Finds the DN "res_dn" of an object with a given SID "sid" */
416 static int samldb_dn_from_sid(struct samldb_ctx *ac)
418 struct ldb_context *ldb;
419 static const char * const attrs[] = { NULL };
420 struct ldb_request *req;
424 ldb = ldb_module_get_ctx(ac->module);
427 return LDB_ERR_OPERATIONS_ERROR;
429 filter = talloc_asprintf(ac, "(objectSid=%s)",
430 ldap_encode_ndr_dom_sid(ac, ac->sid));
432 return LDB_ERR_OPERATIONS_ERROR;
434 ret = ldb_build_search_req(&req, ldb, ac,
435 ldb_get_default_basedn(ldb),
439 ac, samldb_dn_from_sid_callback,
441 if (ret != LDB_SUCCESS)
444 return ldb_next_request(ac->module, req);
448 static int samldb_check_primaryGroupID_1(struct samldb_ctx *ac)
450 struct ldb_context *ldb;
453 ldb = ldb_module_get_ctx(ac->module);
455 rid = samdb_result_uint(ac->msg, "primaryGroupID", ~0);
456 ac->sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
458 return LDB_ERR_OPERATIONS_ERROR;
461 return samldb_next_step(ac);
464 static int samldb_check_primaryGroupID_2(struct samldb_ctx *ac)
466 if (ac->res_dn == NULL) {
467 struct ldb_context *ldb;
468 ldb = ldb_module_get_ctx(ac->module);
469 ldb_asprintf_errstring(ldb,
470 "Failed to find group sid %s!",
471 dom_sid_string(ac->sid, ac->sid));
472 return LDB_ERR_UNWILLING_TO_PERFORM;
475 return samldb_next_step(ac);
480 * samldb_find_for_defaultObjectCategory (async)
483 static int samldb_find_for_defaultObjectCategory_callback(struct ldb_request *req,
484 struct ldb_reply *ares)
486 struct ldb_context *ldb;
487 struct samldb_ctx *ac;
490 ac = talloc_get_type(req->context, struct samldb_ctx);
491 ldb = ldb_module_get_ctx(ac->module);
494 ret = LDB_ERR_OPERATIONS_ERROR;
497 if (ares->error != LDB_SUCCESS) {
498 if (ares->error == LDB_ERR_NO_SUCH_OBJECT) {
499 if (ldb_request_get_control(ac->req,
500 LDB_CONTROL_RELAX_OID) != NULL) {
501 /* Don't be pricky when the DN doesn't exist */
502 /* if we have the RELAX control specified */
503 ac->res_dn = req->op.search.base;
504 return samldb_next_step(ac);
506 ldb_set_errstring(ldb,
507 "samldb_find_defaultObjectCategory: "
508 "Invalid DN for 'defaultObjectCategory'!");
509 ares->error = LDB_ERR_CONSTRAINT_VIOLATION;
513 return ldb_module_done(ac->req, ares->controls,
514 ares->response, ares->error);
517 switch (ares->type) {
518 case LDB_REPLY_ENTRY:
519 ac->res_dn = talloc_steal(ac, ares->message->dn);
524 case LDB_REPLY_REFERRAL:
533 if (ac->res_dn != NULL) {
534 /* when found go on */
535 ret = samldb_next_step(ac);
537 ret = LDB_ERR_OPERATIONS_ERROR;
543 if (ret != LDB_SUCCESS) {
544 return ldb_module_done(ac->req, NULL, NULL, ret);
550 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
552 struct ldb_context *ldb;
553 struct ldb_request *req;
554 static const char *no_attrs[] = { NULL };
557 ldb = ldb_module_get_ctx(ac->module);
561 ret = ldb_build_search_req(&req, ldb, ac,
562 ac->dn, LDB_SCOPE_BASE,
563 "(objectClass=classSchema)", no_attrs,
565 samldb_find_for_defaultObjectCategory_callback,
567 if (ret != LDB_SUCCESS) {
571 ret = dsdb_request_add_controls(req,
572 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT);
573 if (ret != LDB_SUCCESS) {
577 return ldb_next_request(ac->module, req);
581 * msDS-IntId attributeSchema attribute handling
582 * during LDB_ADD request processing
584 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
589 uint32_t system_flags;
590 struct ldb_context *ldb;
591 struct ldb_result *ldb_res;
592 struct ldb_dn *schema_dn;
594 ldb = ldb_module_get_ctx(ac->module);
595 schema_dn = ldb_get_schema_basedn(ldb);
597 /* replicated update should always go through */
598 if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
602 /* msDS-IntId is handled by system and should never be
603 * passed by clients */
604 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
605 return LDB_ERR_UNWILLING_TO_PERFORM;
608 /* do not generate msDS-IntId if Relax control is passed */
609 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
613 /* check Functional Level */
614 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
618 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
619 system_flags = ldb_msg_find_attr_as_uint(ac->msg, "systemFlags", 0);
620 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
624 /* Generate new value for msDs-IntId
625 * Value should be in 0x80000000..0xBFFFFFFF range */
626 msds_intid = generate_random() % 0X3FFFFFFF;
627 msds_intid += 0x80000000;
629 /* probe id values until unique one is found */
632 if (msds_intid > 0xBFFFFFFF) {
633 msds_intid = 0x80000001;
636 ret = dsdb_module_search(ac->module, ac,
638 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
639 DSDB_FLAG_NEXT_MODULE,
640 "(msDS-IntId=%d)", msds_intid);
641 if (ret != LDB_SUCCESS) {
642 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
643 __location__": Searching for msDS-IntId=%d failed - %s\n",
646 return LDB_ERR_OPERATIONS_ERROR;
648 id_exists = (ldb_res->count > 0);
650 talloc_free(ldb_res);
653 return ldb_msg_add_fmt(ac->msg, "msDS-IntId", "%d", msds_intid);
658 * samldb_add_entry (async)
661 static int samldb_add_entry_callback(struct ldb_request *req,
662 struct ldb_reply *ares)
664 struct ldb_context *ldb;
665 struct samldb_ctx *ac;
668 ac = talloc_get_type(req->context, struct samldb_ctx);
669 ldb = ldb_module_get_ctx(ac->module);
672 return ldb_module_done(ac->req, NULL, NULL,
673 LDB_ERR_OPERATIONS_ERROR);
676 if (ares->type == LDB_REPLY_REFERRAL) {
677 return ldb_module_send_referral(ac->req, ares->referral);
680 if (ares->error != LDB_SUCCESS) {
681 return ldb_module_done(ac->req, ares->controls,
682 ares->response, ares->error);
684 if (ares->type != LDB_REPLY_DONE) {
685 ldb_set_errstring(ldb,
686 "Invalid reply type!\n");
687 return ldb_module_done(ac->req, NULL, NULL,
688 LDB_ERR_OPERATIONS_ERROR);
691 /* The caller may wish to get controls back from the add */
692 ac->ares = talloc_steal(ac, ares);
694 ret = samldb_next_step(ac);
695 if (ret != LDB_SUCCESS) {
696 return ldb_module_done(ac->req, NULL, NULL, ret);
701 static int samldb_add_entry(struct samldb_ctx *ac)
703 struct ldb_context *ldb;
704 struct ldb_request *req;
707 ldb = ldb_module_get_ctx(ac->module);
709 ret = ldb_build_add_req(&req, ldb, ac,
712 ac, samldb_add_entry_callback,
714 if (ret != LDB_SUCCESS) {
718 return ldb_next_request(ac->module, req);
722 * return true if msg carries an attributeSchema that is intended to be RODC
723 * filtered but is also a system-critical attribute.
725 static bool check_rodc_critical_attribute(struct ldb_message *msg)
727 uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
729 schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
730 searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
731 rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE | SEARCH_FLAG_CONFIDENTIAL);
733 if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
734 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
742 static int samldb_fill_object(struct samldb_ctx *ac, const char *type)
744 struct ldb_context *ldb;
745 struct loadparm_context *lp_ctx;
746 enum sid_generator sid_generator;
749 ldb = ldb_module_get_ctx(ac->module);
751 /* Add informations for the different account types */
753 if (strcmp(ac->type, "user") == 0) {
754 ret = samdb_find_or_add_attribute(ldb, ac->msg,
755 "userAccountControl", "546");
756 if (ret != LDB_SUCCESS) return ret;
757 ret = samdb_find_or_add_attribute(ldb, ac->msg,
759 if (ret != LDB_SUCCESS) return ret;
760 ret = samdb_find_or_add_attribute(ldb, ac->msg,
762 if (ret != LDB_SUCCESS) return ret;
763 ret = samdb_find_or_add_attribute(ldb, ac->msg,
765 if (ret != LDB_SUCCESS) return ret;
766 ret = samdb_find_or_add_attribute(ldb, ac->msg,
767 "badPasswordTime", "0");
768 if (ret != LDB_SUCCESS) return ret;
769 ret = samdb_find_or_add_attribute(ldb, ac->msg,
771 if (ret != LDB_SUCCESS) return ret;
772 ret = samdb_find_or_add_attribute(ldb, ac->msg,
774 if (ret != LDB_SUCCESS) return ret;
775 ret = samdb_find_or_add_attribute(ldb, ac->msg,
777 if (ret != LDB_SUCCESS) return ret;
778 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
779 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
780 "primaryGroupID", DOMAIN_RID_USERS);
781 if (ret != LDB_SUCCESS) return ret;
783 ret = samdb_find_or_add_attribute(ldb, ac->msg,
784 "accountExpires", "9223372036854775807");
785 if (ret != LDB_SUCCESS) return ret;
786 ret = samdb_find_or_add_attribute(ldb, ac->msg,
788 if (ret != LDB_SUCCESS) return ret;
789 } else if (strcmp(ac->type, "group") == 0) {
790 ret = samdb_find_or_add_attribute(ldb, ac->msg,
791 "groupType", "-2147483646");
792 if (ret != LDB_SUCCESS) return ret;
793 } else if (strcmp(ac->type, "classSchema") == 0) {
794 const struct ldb_val *rdn_value, *def_obj_cat_val;
796 ret = samdb_find_or_add_attribute(ldb, ac->msg,
798 if (ret != LDB_SUCCESS) return ret;
800 /* do not allow to mark an attributeSchema as RODC filtered if it
801 * is system-critical */
802 if (check_rodc_critical_attribute(ac->msg)) {
803 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
804 ldb_dn_get_linearized(ac->msg->dn));
805 return LDB_ERR_UNWILLING_TO_PERFORM;
809 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
810 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
811 /* the RDN has prefix "CN" */
812 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
813 samdb_cn_to_lDAPDisplayName(ac,
814 (const char *) rdn_value->data));
815 if (ret != LDB_SUCCESS) {
821 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
824 guid = GUID_random();
825 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
826 if (ret != LDB_SUCCESS) {
832 def_obj_cat_val = ldb_msg_find_ldb_val(ac->msg,
833 "defaultObjectCategory");
834 if (def_obj_cat_val != NULL) {
835 /* "defaultObjectCategory" has been set by the caller.
836 * Do some checks for consistency.
837 * NOTE: The real constraint check (that
838 * 'defaultObjectCategory' is the DN of the new
839 * objectclass or any parent of it) is still incomplete.
840 * For now we say that 'defaultObjectCategory' is valid
841 * if it exists and it is of objectclass "classSchema".
843 ac->dn = ldb_dn_from_ldb_val(ac, ldb, def_obj_cat_val);
844 if (ac->dn == NULL) {
845 ldb_set_errstring(ldb,
846 "Invalid DN for 'defaultObjectCategory'!");
847 return LDB_ERR_CONSTRAINT_VIOLATION;
850 /* "defaultObjectCategory" has not been set by the
851 * caller. Use the entry DN for it. */
852 ac->dn = ac->msg->dn;
854 ret = samdb_msg_add_string(ldb, ac, ac->msg,
855 "defaultObjectCategory",
856 ldb_dn_get_linearized(ac->dn));
857 if (ret != LDB_SUCCESS) {
863 ret = samldb_add_step(ac, samldb_add_entry);
864 if (ret != LDB_SUCCESS) return ret;
866 /* Now perform the checks for the 'defaultObjectCategory'. The
867 * lookup DN was already saved in "ac->dn" */
868 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
869 if (ret != LDB_SUCCESS) return ret;
871 return samldb_first_step(ac);
872 } else if (strcmp(ac->type, "attributeSchema") == 0) {
873 const struct ldb_val *rdn_value;
874 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
875 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
876 /* the RDN has prefix "CN" */
877 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
878 samdb_cn_to_lDAPDisplayName(ac,
879 (const char *) rdn_value->data));
880 if (ret != LDB_SUCCESS) {
886 /* do not allow to mark an attributeSchema as RODC filtered if it
887 * is system-critical */
888 if (check_rodc_critical_attribute(ac->msg)) {
889 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical attribute with RODC filtering",
890 ldb_dn_get_linearized(ac->msg->dn));
891 return LDB_ERR_UNWILLING_TO_PERFORM;
894 ret = samdb_find_or_add_attribute(ldb, ac->msg,
895 "isSingleValued", "FALSE");
896 if (ret != LDB_SUCCESS) return ret;
898 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
901 guid = GUID_random();
902 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
903 if (ret != LDB_SUCCESS) {
909 /* handle msDS-IntID attribute */
910 ret = samldb_add_handle_msDS_IntId(ac);
911 if (ret != LDB_SUCCESS) return ret;
913 ret = samldb_add_step(ac, samldb_add_entry);
914 if (ret != LDB_SUCCESS) return ret;
916 return samldb_first_step(ac);
918 ldb_asprintf_errstring(ldb,
919 "Invalid entry type!");
920 return LDB_ERR_OPERATIONS_ERROR;
923 /* check if we have a valid samAccountName */
924 ret = samldb_add_step(ac, samldb_check_samAccountName);
925 if (ret != LDB_SUCCESS) return ret;
927 /* check account_type/group_type */
928 ret = samldb_add_step(ac, samldb_check_samAccountType);
929 if (ret != LDB_SUCCESS) return ret;
931 /* check if we have a valid primary group ID */
932 if (strcmp(ac->type, "user") == 0) {
933 ret = samldb_add_step(ac, samldb_check_primaryGroupID_1);
934 if (ret != LDB_SUCCESS) return ret;
935 ret = samldb_add_step(ac, samldb_dn_from_sid);
936 if (ret != LDB_SUCCESS) return ret;
937 ret = samldb_add_step(ac, samldb_check_primaryGroupID_2);
938 if (ret != LDB_SUCCESS) return ret;
941 lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
942 struct loadparm_context);
944 /* don't allow objectSID to be specified without the RELAX control */
945 ac->sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
946 if (ac->sid && !ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) &&
947 !dsdb_module_am_system(ac->module)) {
948 ldb_asprintf_errstring(ldb, "No SID may be specified in user/group creation for %s",
949 ldb_dn_get_linearized(ac->msg->dn));
950 return LDB_ERR_UNWILLING_TO_PERFORM;
954 sid_generator = lp_sid_generator(lp_ctx);
955 if (sid_generator == SID_GENERATOR_INTERNAL) {
956 ret = samldb_add_step(ac, samldb_allocate_sid);
957 if (ret != LDB_SUCCESS) return ret;
961 /* finally proceed with adding the entry */
962 ret = samldb_add_step(ac, samldb_add_entry);
963 if (ret != LDB_SUCCESS) return ret;
965 return samldb_first_step(ac);
968 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
970 struct ldb_context *ldb;
973 ldb = ldb_module_get_ctx(ac->module);
975 ac->sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
976 if (ac->sid == NULL) {
977 ac->sid = dom_sid_parse_talloc(ac->msg,
978 (const char *)ldb_dn_get_rdn_val(ac->msg->dn)->data);
980 ldb_set_errstring(ldb,
981 "No valid SID found in "
982 "ForeignSecurityPrincipal CN!");
984 return LDB_ERR_CONSTRAINT_VIOLATION;
986 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", ac->sid)) {
988 return LDB_ERR_OPERATIONS_ERROR;
992 /* finally proceed with adding the entry */
993 ret = samldb_add_step(ac, samldb_add_entry);
994 if (ret != LDB_SUCCESS) return ret;
996 return samldb_first_step(ac);
999 static int samldb_schema_info_update(struct samldb_ctx *ac)
1002 struct ldb_context *ldb;
1003 struct dsdb_schema *schema;
1005 /* replicated update should always go through */
1006 if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1010 /* do not update schemaInfo during provisioning */
1011 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
1015 ldb = ldb_module_get_ctx(ac->module);
1016 schema = dsdb_get_schema(ldb, NULL);
1018 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
1019 "samldb_schema_info_update: no dsdb_schema loaded");
1020 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
1021 return LDB_ERR_OPERATIONS_ERROR;
1024 werr = dsdb_module_schema_info_update(ac->module, schema, DSDB_FLAG_NEXT_MODULE);
1025 if (!W_ERROR_IS_OK(werr)) {
1026 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
1027 "samldb_schema_info_update: "
1028 "dsdb_module_schema_info_update failed with %s",
1030 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
1031 return LDB_ERR_OPERATIONS_ERROR;
1038 static int samldb_prim_group_change(struct samldb_ctx *ac)
1040 struct ldb_context *ldb;
1041 const char * attrs[] = { "primaryGroupID", "memberOf", NULL };
1042 struct ldb_result *res;
1043 struct ldb_message_element *el;
1044 struct ldb_message *msg;
1046 struct dom_sid *sid;
1047 struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
1050 ldb = ldb_module_get_ctx(ac->module);
1052 /* Fetch informations from the existing object */
1054 ret = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1056 if (ret != LDB_SUCCESS) {
1060 /* Finds out the DN of the old primary group */
1062 rid = samdb_result_uint(res->msgs[0], "primaryGroupID", (uint32_t) -1);
1063 if (rid == (uint32_t) -1) {
1064 /* User objects do always have a mandatory "primaryGroupID"
1065 * attribute. If this doesn't exist then the object is of the
1066 * wrong type. This is the exact Windows error code */
1067 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1070 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1072 return LDB_ERR_OPERATIONS_ERROR;
1075 prev_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSID=%s)",
1076 dom_sid_string(ac, sid));
1077 if (prev_prim_group_dn == NULL) {
1078 return LDB_ERR_OPERATIONS_ERROR;
1081 /* Finds out the DN of the new primary group */
1083 rid = samdb_result_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
1084 if (rid == (uint32_t) -1) {
1085 /* we aren't affected of any primary group change */
1089 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1091 return LDB_ERR_OPERATIONS_ERROR;
1094 new_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSID=%s)",
1095 dom_sid_string(ac, sid));
1096 if (new_prim_group_dn == NULL) {
1097 /* Here we know if the specified new primary group candidate is
1099 return LDB_ERR_UNWILLING_TO_PERFORM;
1102 /* Only update the "member" attributes when we really do have a change */
1103 if (ldb_dn_compare(new_prim_group_dn, prev_prim_group_dn) != 0) {
1104 /* We need to be already a normal member of the new primary
1105 * group in order to be successful. */
1106 el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
1107 ldb_dn_get_linearized(new_prim_group_dn));
1109 return LDB_ERR_UNWILLING_TO_PERFORM;
1112 /* Remove the "member" attribute on the new primary group */
1113 msg = talloc_zero(ac, struct ldb_message);
1114 msg->dn = new_prim_group_dn;
1116 ret = samdb_msg_add_delval(ldb, ac, msg, "member",
1117 ldb_dn_get_linearized(ac->msg->dn));
1118 if (ret != LDB_SUCCESS) {
1122 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1123 if (ret != LDB_SUCCESS) {
1127 /* Add a "member" attribute for the previous primary group */
1128 msg = talloc_zero(ac, struct ldb_message);
1129 msg->dn = prev_prim_group_dn;
1131 ret = samdb_msg_add_addval(ldb, ac, msg, "member",
1132 ldb_dn_get_linearized(ac->msg->dn));
1133 if (ret != LDB_SUCCESS) {
1137 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1138 if (ret != LDB_SUCCESS) {
1147 static int samldb_member_check(struct samldb_ctx *ac)
1149 struct ldb_context *ldb;
1150 struct ldb_message_element *el;
1151 struct ldb_dn *member_dn, *group_dn;
1152 uint32_t prim_group_rid;
1153 struct dom_sid *sid;
1156 ldb = ldb_module_get_ctx(ac->module);
1158 el = ldb_msg_find_element(ac->msg, "member");
1160 /* we aren't affected */
1164 for (i = 0; i < el->num_values; i++) {
1165 /* Denies to add "member"s to groups which are primary ones
1167 member_dn = ldb_dn_from_ldb_val(ac, ldb, &el->values[i]);
1168 if (!ldb_dn_validate(member_dn)) {
1169 return LDB_ERR_OPERATIONS_ERROR;
1172 prim_group_rid = samdb_search_uint(ldb, ac, (uint32_t) -1,
1173 member_dn, "primaryGroupID",
1175 if (prim_group_rid == (uint32_t) -1) {
1176 /* the member hasn't to be a user account -> therefore
1177 * no check needed in this case. */
1181 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1184 return LDB_ERR_OPERATIONS_ERROR;
1187 group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSID=%s)",
1188 dom_sid_string(ac, sid));
1189 if (group_dn == NULL) {
1190 return LDB_ERR_OPERATIONS_ERROR;
1193 if (ldb_dn_compare(group_dn, ac->msg->dn) == 0) {
1194 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1202 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
1204 struct ldb_context *ldb;
1205 struct dom_sid *sid;
1210 ldb = ldb_module_get_ctx(ac->module);
1212 /* Finds out the SID/RID of the SAM object */
1213 sid = samdb_search_dom_sid(ldb, ac, ac->req->op.del.dn, "objectSID",
1216 /* No SID - it might not be a SAM object - therefore ok */
1219 status = dom_sid_split_rid(ac, sid, NULL, &rid);
1220 if (!NT_STATUS_IS_OK(status)) {
1221 return LDB_ERR_OPERATIONS_ERROR;
1224 /* Special object (security principal?) */
1228 /* Deny delete requests from groups which are primary ones */
1229 count = samdb_search_count(ldb, NULL,
1230 "(&(primaryGroupID=%u)(objectClass=user))",
1233 return LDB_ERR_OPERATIONS_ERROR;
1236 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1244 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
1246 struct ldb_context *ldb;
1247 struct samldb_ctx *ac;
1250 ldb = ldb_module_get_ctx(module);
1251 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
1253 /* do not manipulate our control entries */
1254 if (ldb_dn_is_special(req->op.add.message->dn)) {
1255 return ldb_next_request(module, req);
1258 ac = samldb_ctx_init(module, req);
1260 return LDB_ERR_OPERATIONS_ERROR;
1263 /* build the new msg */
1264 ac->msg = ldb_msg_copy(ac, ac->req->op.add.message);
1267 ldb_debug(ldb, LDB_DEBUG_FATAL,
1268 "samldb_add: ldb_msg_copy failed!\n");
1269 return LDB_ERR_OPERATIONS_ERROR;
1272 if (samdb_find_attribute(ldb, ac->msg,
1273 "objectclass", "user") != NULL) {
1274 return samldb_fill_object(ac, "user");
1277 if (samdb_find_attribute(ldb, ac->msg,
1278 "objectclass", "group") != NULL) {
1279 return samldb_fill_object(ac, "group");
1282 /* perhaps a foreignSecurityPrincipal? */
1283 if (samdb_find_attribute(ldb, ac->msg,
1285 "foreignSecurityPrincipal") != NULL) {
1286 return samldb_fill_foreignSecurityPrincipal_object(ac);
1289 if (samdb_find_attribute(ldb, ac->msg,
1290 "objectclass", "classSchema") != NULL) {
1291 ret = samldb_schema_info_update(ac);
1292 if (ret != LDB_SUCCESS) {
1297 return samldb_fill_object(ac, "classSchema");
1300 if (samdb_find_attribute(ldb, ac->msg,
1301 "objectclass", "attributeSchema") != NULL) {
1302 ret = samldb_schema_info_update(ac);
1303 if (ret != LDB_SUCCESS) {
1308 return samldb_fill_object(ac, "attributeSchema");
1313 /* nothing matched, go on */
1314 return ldb_next_request(module, req);
1318 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
1320 struct ldb_context *ldb;
1321 struct samldb_ctx *ac;
1322 struct ldb_message *msg;
1323 struct ldb_message_element *el, *el2;
1325 uint32_t account_type;
1327 if (ldb_dn_is_special(req->op.mod.message->dn)) {
1328 /* do not manipulate our control entries */
1329 return ldb_next_request(module, req);
1332 ldb = ldb_module_get_ctx(module);
1334 if (ldb_msg_find_element(req->op.mod.message, "sAMAccountType") != NULL) {
1335 ldb_asprintf_errstring(ldb,
1336 "sAMAccountType must not be specified!");
1337 return LDB_ERR_UNWILLING_TO_PERFORM;
1340 /* msDS-IntId is not allowed to be modified
1341 * except when modification comes from replication */
1342 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
1343 if (!ldb_request_get_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1344 return LDB_ERR_CONSTRAINT_VIOLATION;
1348 ac = samldb_ctx_init(module, req);
1350 return LDB_ERR_OPERATIONS_ERROR;
1353 /* TODO: do not modify original request, create a new one */
1355 el = ldb_msg_find_element(req->op.mod.message, "groupType");
1356 if (el && (el->flags == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1357 uint32_t group_type;
1359 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1360 req->op.mod.message);
1362 group_type = strtoul((const char *)el->values[0].data, NULL, 0);
1363 account_type = ds_gtype2atype(group_type);
1364 ret = samdb_msg_add_uint(ldb, msg, msg,
1367 if (ret != LDB_SUCCESS) {
1370 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1371 el2->flags = LDB_FLAG_MOD_REPLACE;
1373 if (el && (el->flags == LDB_FLAG_MOD_DELETE)) {
1374 return LDB_ERR_UNWILLING_TO_PERFORM;
1377 el = ldb_msg_find_element(req->op.mod.message, "primaryGroupID");
1378 if (el && (el->flags == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1379 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1380 req->op.mod.message);
1382 ret = samldb_prim_group_change(ac);
1383 if (ret != LDB_SUCCESS) {
1387 if (el && (el->flags == LDB_FLAG_MOD_DELETE)) {
1388 return LDB_ERR_UNWILLING_TO_PERFORM;
1391 el = ldb_msg_find_element(req->op.mod.message, "userAccountControl");
1392 if (el && (el->flags == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1393 uint32_t user_account_control;
1395 req->op.mod.message = msg = ldb_msg_copy_shallow(req,
1396 req->op.mod.message);
1398 user_account_control = strtoul((const char *)el->values[0].data,
1400 account_type = ds_uf2atype(user_account_control);
1401 ret = samdb_msg_add_uint(ldb, msg, msg,
1404 if (ret != LDB_SUCCESS) {
1407 el2 = ldb_msg_find_element(msg, "sAMAccountType");
1408 el2->flags = LDB_FLAG_MOD_REPLACE;
1410 if (user_account_control & UF_SERVER_TRUST_ACCOUNT) {
1411 ret = samdb_msg_add_string(ldb, msg, msg,
1412 "isCriticalSystemObject", "TRUE");
1413 if (ret != LDB_SUCCESS) {
1416 el2 = ldb_msg_find_element(msg, "isCriticalSystemObject");
1417 el2->flags = LDB_FLAG_MOD_REPLACE;
1419 /* DCs have primaryGroupID of DOMAIN_RID_DCS */
1420 if (!ldb_msg_find_element(msg, "primaryGroupID")) {
1421 ret = samdb_msg_add_uint(ldb, msg, msg,
1422 "primaryGroupID", DOMAIN_RID_DCS);
1423 if (ret != LDB_SUCCESS) {
1426 el2 = ldb_msg_find_element(msg, "primaryGroupID");
1427 el2->flags = LDB_FLAG_MOD_REPLACE;
1431 if (el && (el->flags == LDB_FLAG_MOD_DELETE)) {
1432 return LDB_ERR_UNWILLING_TO_PERFORM;
1435 el = ldb_msg_find_element(req->op.mod.message, "member");
1436 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1437 req->op.mod.message = ac->msg = ldb_msg_copy_shallow(req,
1438 req->op.mod.message);
1440 ret = samldb_member_check(ac);
1441 if (ret != LDB_SUCCESS) {
1446 return ldb_next_request(module, req);
1450 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
1452 struct samldb_ctx *ac;
1455 if (ldb_dn_is_special(req->op.del.dn)) {
1456 /* do not manipulate our control entries */
1457 return ldb_next_request(module, req);
1460 ac = samldb_ctx_init(module, req);
1462 return LDB_ERR_OPERATIONS_ERROR;
1464 ret = samldb_prim_group_users_check(ac);
1465 if (ret != LDB_SUCCESS) {
1469 return ldb_next_request(module, req);
1472 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
1474 struct ldb_context *ldb = ldb_module_get_ctx(module);
1475 struct dsdb_fsmo_extended_op *exop;
1478 exop = talloc_get_type(req->op.extended.data, struct dsdb_fsmo_extended_op);
1480 ldb_debug(ldb, LDB_DEBUG_FATAL, "samldb_extended_allocate_rid_pool: invalid extended data\n");
1481 return LDB_ERR_PROTOCOL_ERROR;
1484 ret = ridalloc_allocate_rid_pool_fsmo(module, exop);
1485 if (ret != LDB_SUCCESS) {
1489 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
1492 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
1494 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
1495 return samldb_extended_allocate_rid_pool(module, req);
1498 return ldb_next_request(module, req);
1502 _PUBLIC_ const struct ldb_module_ops ldb_samldb_module_ops = {
1505 .modify = samldb_modify,
1506 .del = samldb_delete,
1507 .extended = samldb_extended