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 "dsdb/samdb/ldb_modules/ridalloc.h"
38 #include "libcli/security/security.h"
39 #include "librpc/gen_ndr/ndr_security.h"
40 #include "../lib/util/util_ldb.h"
42 #include "param/param.h"
46 typedef int (*samldb_step_fn_t)(struct samldb_ctx *);
49 struct samldb_step *next;
54 struct ldb_module *module;
55 struct ldb_request *req;
57 /* used for add operations */
60 /* the resulting message */
61 struct ldb_message *msg;
63 /* used in "samldb_find_for_defaultObjectCategory" */
64 struct ldb_dn *dn, *res_dn;
66 /* all the async steps necessary to complete the operation */
67 struct samldb_step *steps;
68 struct samldb_step *curstep;
70 /* If someone set an ares to forward controls and response back to the caller */
71 struct ldb_reply *ares;
74 static struct samldb_ctx *samldb_ctx_init(struct ldb_module *module,
75 struct ldb_request *req)
77 struct ldb_context *ldb;
78 struct samldb_ctx *ac;
80 ldb = ldb_module_get_ctx(module);
82 ac = talloc_zero(req, struct samldb_ctx);
94 static int samldb_add_step(struct samldb_ctx *ac, samldb_step_fn_t fn)
96 struct samldb_step *step, *stepper;
98 step = talloc_zero(ac, struct samldb_step);
100 return ldb_oom(ldb_module_get_ctx(ac->module));
105 if (ac->steps == NULL) {
109 if (ac->curstep == NULL)
110 return ldb_operr(ldb_module_get_ctx(ac->module));
111 for (stepper = ac->curstep; stepper->next != NULL;
112 stepper = stepper->next);
113 stepper->next = step;
119 static int samldb_first_step(struct samldb_ctx *ac)
121 if (ac->steps == NULL) {
122 return ldb_operr(ldb_module_get_ctx(ac->module));
125 ac->curstep = ac->steps;
126 return ac->curstep->fn(ac);
129 static int samldb_next_step(struct samldb_ctx *ac)
131 if (ac->curstep->next) {
132 ac->curstep = ac->curstep->next;
133 return ac->curstep->fn(ac);
136 /* we exit the samldb module here */
137 /* If someone set an ares to forward controls and response back to the caller, use them */
139 return ldb_module_done(ac->req, ac->ares->controls,
140 ac->ares->response, LDB_SUCCESS);
142 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
147 /* sAMAccountName handling */
149 static int samldb_generate_sAMAccountName(struct ldb_context *ldb, struct ldb_message *msg)
153 /* Format: $000000-000000000000 */
155 name = talloc_asprintf(msg, "$%.6X-%.6X%.6X",
156 (unsigned int)generate_random(),
157 (unsigned int)generate_random(),
158 (unsigned int)generate_random());
162 return ldb_msg_add_steal_string(msg, "sAMAccountName", name);
165 static int samldb_check_sAMAccountName(struct samldb_ctx *ac)
167 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
171 if (ldb_msg_find_element(ac->msg, "sAMAccountName") == NULL) {
172 ret = samldb_generate_sAMAccountName(ldb, ac->msg);
173 if (ret != LDB_SUCCESS) {
178 name = ldb_msg_find_attr_as_string(ac->msg, "sAMAccountName", NULL);
180 return ldb_operr(ldb);
183 ret = samdb_search_count(ldb, NULL, "(sAMAccountName=%s)",
184 ldb_binary_encode_string(ac, name));
185 if ((ret < 0) || (ret > 1)) {
186 return ldb_operr(ldb);
189 return LDB_ERR_ENTRY_ALREADY_EXISTS;
192 return samldb_next_step(ac);
195 /* sAMAccountType handling */
197 static int samldb_check_sAMAccountType(struct samldb_ctx *ac)
199 struct ldb_context *ldb;
200 unsigned int account_type;
201 unsigned int group_type;
205 ldb = ldb_module_get_ctx(ac->module);
207 /* make sure sAMAccountType is not specified */
208 if (ldb_msg_find_element(ac->msg, "sAMAccountType") != NULL) {
209 ldb_asprintf_errstring(ldb,
210 "sAMAccountType must not be specified!");
211 return LDB_ERR_UNWILLING_TO_PERFORM;
214 if (strcmp("user", ac->type) == 0) {
215 uac = samdb_result_uint(ac->msg, "userAccountControl", 0);
217 ldb_asprintf_errstring(ldb,
218 "userAccountControl invalid!");
219 return LDB_ERR_UNWILLING_TO_PERFORM;
221 account_type = ds_uf2atype(uac);
222 ret = samdb_msg_add_uint(ldb,
226 if (ret != LDB_SUCCESS) {
230 } else if (strcmp("group", ac->type) == 0) {
231 group_type = samdb_result_uint(ac->msg, "groupType", 0);
232 if (group_type == 0) {
233 ldb_asprintf_errstring(ldb,
234 "groupType invalid!\n");
235 return LDB_ERR_UNWILLING_TO_PERFORM;
237 account_type = ds_gtype2atype(group_type);
238 ret = samdb_msg_add_uint(ldb,
242 if (ret != LDB_SUCCESS) {
248 return samldb_next_step(ac);
251 /* primaryGroupID handling */
253 static int samldb_check_primaryGroupID(struct samldb_ctx *ac)
255 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
256 struct ldb_dn *prim_group_dn;
261 rid = samdb_result_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
262 if (rid == (uint32_t) -1) {
263 uint32_t uac = samdb_result_uint(ac->msg, "userAccountControl",
266 rid = ds_uf2prim_group_rid(uac);
268 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
269 "primaryGroupID", rid);
270 if (ret != LDB_SUCCESS) {
273 } else if (!ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
274 ldb_set_errstring(ldb,
275 "The primary group isn't settable on add operations!");
276 return LDB_ERR_UNWILLING_TO_PERFORM;
279 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
281 return ldb_operr(ldb);
284 prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
285 ldap_encode_ndr_dom_sid(ac, sid));
286 if (prim_group_dn == NULL) {
287 ldb_asprintf_errstring(ldb,
288 "Failed to find primary group with RID %u!",
290 return LDB_ERR_UNWILLING_TO_PERFORM;
293 return samldb_next_step(ac);
297 static bool samldb_msg_add_sid(struct ldb_message *msg,
299 const struct dom_sid *sid)
302 enum ndr_err_code ndr_err;
304 ndr_err = ndr_push_struct_blob(&v, msg, sid,
305 (ndr_push_flags_fn_t)ndr_push_dom_sid);
306 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
309 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
313 /* allocate a SID using our RID Set */
314 static int samldb_allocate_sid(struct samldb_ctx *ac)
318 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
321 ret = ridalloc_allocate_rid(ac->module, &rid);
322 if (ret != LDB_SUCCESS) {
326 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
328 return ldb_module_oom(ac->module);
331 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
332 return ldb_operr(ldb);
335 return samldb_next_step(ac);
339 see if a krbtgt_number is available
341 static bool samldb_krbtgtnumber_available(struct samldb_ctx *ac, unsigned krbtgt_number)
343 TALLOC_CTX *tmp_ctx = talloc_new(ac);
344 struct ldb_result *res;
345 const char *attrs[] = { NULL };
348 ret = dsdb_module_search(ac->module, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
349 attrs, DSDB_FLAG_NEXT_MODULE,
350 "msDC-SecondaryKrbTgtNumber=%u", krbtgt_number);
351 if (ret == LDB_SUCCESS && res->count == 0) {
352 talloc_free(tmp_ctx);
355 talloc_free(tmp_ctx);
359 /* special handling for add in RODC join */
360 static int samldb_rodc_add(struct samldb_ctx *ac)
362 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
363 unsigned krbtgt_number, i_start, i;
367 /* find a unused msDC-SecondaryKrbTgtNumber */
368 i_start = generate_random() & 0xFFFF;
373 for (i=i_start; i<=0xFFFF; i++) {
374 if (samldb_krbtgtnumber_available(ac, i)) {
379 for (i=1; i<i_start; i++) {
380 if (samldb_krbtgtnumber_available(ac, i)) {
386 ldb_asprintf_errstring(ldb_module_get_ctx(ac->module),
387 "%08X: Unable to find available msDS-SecondaryKrbTgtNumber",
388 W_ERROR_V(WERR_NO_SYSTEM_RESOURCES));
389 return LDB_ERR_OTHER;
392 ret = ldb_msg_add_empty(ac->msg, "msDS-SecondaryKrbTgtNumber", LDB_FLAG_INTERNAL_DISABLE_VALIDATION, NULL);
393 if (ret != LDB_SUCCESS) {
394 return ldb_operr(ldb);
397 ret = ldb_msg_add_fmt(ac->msg, "msDS-SecondaryKrbTgtNumber", "%u", krbtgt_number);
398 if (ret != LDB_SUCCESS) {
399 return ldb_operr(ldb);
402 ret = ldb_msg_add_fmt(ac->msg, "sAMAccountName", "krbtgt_%u", krbtgt_number);
403 if (ret != LDB_SUCCESS) {
404 return ldb_operr(ldb);
407 newpass = generate_random_password(ac, 128, 255);
408 if (newpass == NULL) {
409 return ldb_operr(ldb);
412 ret = ldb_msg_add_steal_string(ac->msg, "clearTextPassword", newpass);
413 if (ret != LDB_SUCCESS) {
414 return ldb_operr(ldb);
417 return samldb_next_step(ac);
420 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
422 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
423 struct ldb_result *res;
424 const char *no_attrs[] = { NULL };
429 ret = dsdb_module_search(ac->module, ac, &res,
430 ac->dn, LDB_SCOPE_BASE, no_attrs,
431 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT | DSDB_FLAG_NEXT_MODULE,
432 "(objectClass=classSchema)");
433 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
434 /* Don't be pricky when the DN doesn't exist if we have the */
435 /* RELAX control specified */
436 if (ldb_request_get_control(ac->req,
437 LDB_CONTROL_RELAX_OID) == NULL) {
438 ldb_set_errstring(ldb,
439 "samldb_find_defaultObjectCategory: "
440 "Invalid DN for 'defaultObjectCategory'!");
441 return LDB_ERR_CONSTRAINT_VIOLATION;
444 if ((ret != LDB_ERR_NO_SUCH_OBJECT) && (ret != LDB_SUCCESS)) {
450 return samldb_next_step(ac);
454 * msDS-IntId attributeSchema attribute handling
455 * during LDB_ADD request processing
457 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
462 uint32_t system_flags;
463 struct ldb_context *ldb;
464 struct ldb_result *ldb_res;
465 struct ldb_dn *schema_dn;
467 ldb = ldb_module_get_ctx(ac->module);
468 schema_dn = ldb_get_schema_basedn(ldb);
470 /* replicated update should always go through */
471 if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
475 /* msDS-IntId is handled by system and should never be
476 * passed by clients */
477 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
478 return LDB_ERR_UNWILLING_TO_PERFORM;
481 /* do not generate msDS-IntId if Relax control is passed */
482 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
486 /* check Functional Level */
487 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
491 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
492 system_flags = ldb_msg_find_attr_as_uint(ac->msg, "systemFlags", 0);
493 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
497 /* Generate new value for msDs-IntId
498 * Value should be in 0x80000000..0xBFFFFFFF range */
499 msds_intid = generate_random() % 0X3FFFFFFF;
500 msds_intid += 0x80000000;
502 /* probe id values until unique one is found */
505 if (msds_intid > 0xBFFFFFFF) {
506 msds_intid = 0x80000001;
509 ret = dsdb_module_search(ac->module, ac,
511 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
512 DSDB_FLAG_NEXT_MODULE,
513 "(msDS-IntId=%d)", msds_intid);
514 if (ret != LDB_SUCCESS) {
515 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
516 __location__": Searching for msDS-IntId=%d failed - %s\n",
519 return ldb_operr(ldb);
521 id_exists = (ldb_res->count > 0);
523 talloc_free(ldb_res);
526 return ldb_msg_add_fmt(ac->msg, "msDS-IntId", "%d", msds_intid);
531 * samldb_add_entry (async)
534 static int samldb_add_entry_callback(struct ldb_request *req,
535 struct ldb_reply *ares)
537 struct ldb_context *ldb;
538 struct samldb_ctx *ac;
541 ac = talloc_get_type(req->context, struct samldb_ctx);
542 ldb = ldb_module_get_ctx(ac->module);
545 return ldb_module_done(ac->req, NULL, NULL,
546 LDB_ERR_OPERATIONS_ERROR);
549 if (ares->type == LDB_REPLY_REFERRAL) {
550 return ldb_module_send_referral(ac->req, ares->referral);
553 if (ares->error != LDB_SUCCESS) {
554 return ldb_module_done(ac->req, ares->controls,
555 ares->response, ares->error);
557 if (ares->type != LDB_REPLY_DONE) {
558 ldb_set_errstring(ldb,
559 "Invalid reply type!\n");
560 return ldb_module_done(ac->req, NULL, NULL,
561 LDB_ERR_OPERATIONS_ERROR);
564 /* The caller may wish to get controls back from the add */
565 ac->ares = talloc_steal(ac, ares);
567 ret = samldb_next_step(ac);
568 if (ret != LDB_SUCCESS) {
569 return ldb_module_done(ac->req, NULL, NULL, ret);
574 static int samldb_add_entry(struct samldb_ctx *ac)
576 struct ldb_context *ldb;
577 struct ldb_request *req;
580 ldb = ldb_module_get_ctx(ac->module);
582 ret = ldb_build_add_req(&req, ldb, ac,
585 ac, samldb_add_entry_callback,
587 LDB_REQ_SET_LOCATION(req);
588 if (ret != LDB_SUCCESS) {
592 return ldb_next_request(ac->module, req);
596 * return true if msg carries an attributeSchema that is intended to be RODC
597 * filtered but is also a system-critical attribute.
599 static bool check_rodc_critical_attribute(struct ldb_message *msg)
601 uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
603 schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
604 searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
605 rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE | SEARCH_FLAG_CONFIDENTIAL);
607 if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
608 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
616 static int samldb_fill_object(struct samldb_ctx *ac)
618 struct ldb_context *ldb;
619 struct loadparm_context *lp_ctx;
620 enum sid_generator sid_generator;
622 struct ldb_control *rodc_control;
625 ldb = ldb_module_get_ctx(ac->module);
627 /* Add informations for the different account types */
628 if (strcmp(ac->type, "user") == 0) {
629 ret = samdb_find_or_add_attribute(ldb, ac->msg,
630 "userAccountControl", "546");
631 if (ret != LDB_SUCCESS) return ret;
632 ret = samdb_find_or_add_attribute(ldb, ac->msg,
634 if (ret != LDB_SUCCESS) return ret;
635 ret = samdb_find_or_add_attribute(ldb, ac->msg,
637 if (ret != LDB_SUCCESS) return ret;
638 ret = samdb_find_or_add_attribute(ldb, ac->msg,
640 if (ret != LDB_SUCCESS) return ret;
641 ret = samdb_find_or_add_attribute(ldb, ac->msg,
642 "badPasswordTime", "0");
643 if (ret != LDB_SUCCESS) return ret;
644 ret = samdb_find_or_add_attribute(ldb, ac->msg,
646 if (ret != LDB_SUCCESS) return ret;
647 ret = samdb_find_or_add_attribute(ldb, ac->msg,
649 if (ret != LDB_SUCCESS) return ret;
650 ret = samdb_find_or_add_attribute(ldb, ac->msg,
652 if (ret != LDB_SUCCESS) return ret;
653 ret = samdb_find_or_add_attribute(ldb, ac->msg,
654 "accountExpires", "9223372036854775807");
655 if (ret != LDB_SUCCESS) return ret;
656 ret = samdb_find_or_add_attribute(ldb, ac->msg,
658 if (ret != LDB_SUCCESS) return ret;
659 } else if (strcmp(ac->type, "group") == 0) {
660 ret = samdb_find_or_add_attribute(ldb, ac->msg,
661 "groupType", "-2147483646");
662 if (ret != LDB_SUCCESS) return ret;
663 } else if (strcmp(ac->type, "classSchema") == 0) {
664 const struct ldb_val *rdn_value, *def_obj_cat_val;
666 ret = samdb_find_or_add_attribute(ldb, ac->msg,
668 if (ret != LDB_SUCCESS) return ret;
670 /* do not allow to mark an attributeSchema as RODC filtered if it
671 * is system-critical */
672 if (check_rodc_critical_attribute(ac->msg)) {
673 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
674 ldb_dn_get_linearized(ac->msg->dn));
675 return LDB_ERR_UNWILLING_TO_PERFORM;
679 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
680 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
681 /* the RDN has prefix "CN" */
682 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
683 samdb_cn_to_lDAPDisplayName(ac,
684 (const char *) rdn_value->data));
685 if (ret != LDB_SUCCESS) {
691 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
694 guid = GUID_random();
695 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
696 if (ret != LDB_SUCCESS) {
702 def_obj_cat_val = ldb_msg_find_ldb_val(ac->msg,
703 "defaultObjectCategory");
704 if (def_obj_cat_val != NULL) {
705 /* "defaultObjectCategory" has been set by the caller.
706 * Do some checks for consistency.
707 * NOTE: The real constraint check (that
708 * 'defaultObjectCategory' is the DN of the new
709 * objectclass or any parent of it) is still incomplete.
710 * For now we say that 'defaultObjectCategory' is valid
711 * if it exists and it is of objectclass "classSchema".
713 ac->dn = ldb_dn_from_ldb_val(ac, ldb, def_obj_cat_val);
714 if (ac->dn == NULL) {
715 ldb_set_errstring(ldb,
716 "Invalid DN for 'defaultObjectCategory'!");
717 return LDB_ERR_CONSTRAINT_VIOLATION;
720 /* "defaultObjectCategory" has not been set by the
721 * caller. Use the entry DN for it. */
722 ac->dn = ac->msg->dn;
724 ret = samdb_msg_add_string(ldb, ac->msg, ac->msg,
725 "defaultObjectCategory",
726 ldb_dn_get_linearized(ac->dn));
727 if (ret != LDB_SUCCESS) {
733 ret = samldb_add_step(ac, samldb_add_entry);
734 if (ret != LDB_SUCCESS) return ret;
736 /* Now perform the checks for the 'defaultObjectCategory'. The
737 * lookup DN was already saved in "ac->dn" */
738 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
739 if (ret != LDB_SUCCESS) return ret;
741 return samldb_first_step(ac);
742 } else if (strcmp(ac->type, "attributeSchema") == 0) {
743 const struct ldb_val *rdn_value;
744 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
745 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
746 /* the RDN has prefix "CN" */
747 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
748 samdb_cn_to_lDAPDisplayName(ac->msg,
749 (const char *) rdn_value->data));
750 if (ret != LDB_SUCCESS) {
756 /* do not allow to mark an attributeSchema as RODC filtered if it
757 * is system-critical */
758 if (check_rodc_critical_attribute(ac->msg)) {
759 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical attribute with RODC filtering",
760 ldb_dn_get_linearized(ac->msg->dn));
761 return LDB_ERR_UNWILLING_TO_PERFORM;
764 ret = samdb_find_or_add_attribute(ldb, ac->msg,
765 "isSingleValued", "FALSE");
766 if (ret != LDB_SUCCESS) return ret;
768 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
771 guid = GUID_random();
772 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
773 if (ret != LDB_SUCCESS) {
779 /* handle msDS-IntID attribute */
780 ret = samldb_add_handle_msDS_IntId(ac);
781 if (ret != LDB_SUCCESS) return ret;
783 ret = samldb_add_step(ac, samldb_add_entry);
784 if (ret != LDB_SUCCESS) return ret;
786 return samldb_first_step(ac);
788 ldb_asprintf_errstring(ldb,
789 "Invalid entry type!");
790 return LDB_ERR_OPERATIONS_ERROR;
793 rodc_control = ldb_request_get_control(ac->req, LDB_CONTROL_RODC_DCPROMO_OID);
795 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
796 rodc_control->critical = false;
797 ret = samldb_add_step(ac, samldb_rodc_add);
798 if (ret != LDB_SUCCESS) return ret;
801 /* check if we have a valid sAMAccountName */
802 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
803 if (ret != LDB_SUCCESS) return ret;
805 /* check account_type/group_type */
806 ret = samldb_add_step(ac, samldb_check_sAMAccountType);
807 if (ret != LDB_SUCCESS) return ret;
809 /* check if we have a valid primary group ID */
810 if (strcmp(ac->type, "user") == 0) {
811 ret = samldb_add_step(ac, samldb_check_primaryGroupID);
812 if (ret != LDB_SUCCESS) return ret;
815 lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
816 struct loadparm_context);
818 /* don't allow objectSid to be specified without the RELAX control */
819 sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
820 if (sid && !ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) &&
821 !dsdb_module_am_system(ac->module)) {
822 ldb_asprintf_errstring(ldb, "No SID may be specified in user/group creation for %s",
823 ldb_dn_get_linearized(ac->msg->dn));
824 return LDB_ERR_UNWILLING_TO_PERFORM;
828 sid_generator = lpcfg_sid_generator(lp_ctx);
829 if (sid_generator == SID_GENERATOR_INTERNAL) {
830 ret = samldb_add_step(ac, samldb_allocate_sid);
831 if (ret != LDB_SUCCESS) return ret;
835 /* finally proceed with adding the entry */
836 ret = samldb_add_step(ac, samldb_add_entry);
837 if (ret != LDB_SUCCESS) return ret;
839 return samldb_first_step(ac);
842 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
844 struct ldb_context *ldb;
848 ldb = ldb_module_get_ctx(ac->module);
850 sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
852 sid = dom_sid_parse_talloc(ac->msg,
853 (const char *)ldb_dn_get_rdn_val(ac->msg->dn)->data);
855 ldb_set_errstring(ldb,
856 "No valid SID found in "
857 "ForeignSecurityPrincipal CN!");
858 return LDB_ERR_CONSTRAINT_VIOLATION;
860 if (! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
861 return ldb_operr(ldb);
865 /* finally proceed with adding the entry */
866 ret = samldb_add_step(ac, samldb_add_entry);
867 if (ret != LDB_SUCCESS) return ret;
869 return samldb_first_step(ac);
872 static int samldb_schema_info_update(struct samldb_ctx *ac)
875 struct ldb_context *ldb;
876 struct dsdb_schema *schema;
878 /* replicated update should always go through */
879 if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
883 /* do not update schemaInfo during provisioning */
884 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
888 ldb = ldb_module_get_ctx(ac->module);
889 schema = dsdb_get_schema(ldb, NULL);
891 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
892 "samldb_schema_info_update: no dsdb_schema loaded");
893 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
894 return ldb_operr(ldb);
897 werr = dsdb_module_schema_info_update(ac->module, schema, DSDB_FLAG_NEXT_MODULE);
898 if (!W_ERROR_IS_OK(werr)) {
899 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
900 "samldb_schema_info_update: "
901 "dsdb_module_schema_info_update failed with %s",
903 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
904 return ldb_operr(ldb);
911 static int samldb_prim_group_change(struct samldb_ctx *ac)
913 struct ldb_context *ldb;
914 const char * attrs[] = { "primaryGroupID", "memberOf", NULL };
915 struct ldb_result *res;
916 struct ldb_message_element *el;
917 struct ldb_message *msg;
920 struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
923 ldb = ldb_module_get_ctx(ac->module);
925 /* Fetch informations from the existing object */
927 ret = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
929 if (ret != LDB_SUCCESS) {
933 /* Finds out the DN of the old primary group */
935 rid = samdb_result_uint(res->msgs[0], "primaryGroupID", (uint32_t) -1);
936 if (rid == (uint32_t) -1) {
937 /* User objects do always have a mandatory "primaryGroupID"
938 * attribute. If this doesn't exist then the object is of the
939 * wrong type. This is the exact Windows error code */
940 return LDB_ERR_OBJECT_CLASS_VIOLATION;
943 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
945 return ldb_operr(ldb);
948 prev_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
949 ldap_encode_ndr_dom_sid(ac, sid));
950 if (prev_prim_group_dn == NULL) {
951 return ldb_operr(ldb);
954 /* Finds out the DN of the new primary group */
956 rid = samdb_result_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
957 if (rid == (uint32_t) -1) {
958 /* we aren't affected of any primary group change */
962 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
964 return ldb_operr(ldb);
967 new_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
968 ldap_encode_ndr_dom_sid(ac, sid));
969 if (new_prim_group_dn == NULL) {
970 /* Here we know if the specified new primary group candidate is
972 return LDB_ERR_UNWILLING_TO_PERFORM;
975 /* Only update the "member" attributes when we really do have a change */
976 if (ldb_dn_compare(new_prim_group_dn, prev_prim_group_dn) != 0) {
977 /* We need to be already a normal member of the new primary
978 * group in order to be successful. */
979 el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
980 ldb_dn_get_linearized(new_prim_group_dn));
982 return LDB_ERR_UNWILLING_TO_PERFORM;
985 /* Remove the "member" attribute on the new primary group */
986 msg = talloc_zero(ac, struct ldb_message);
987 msg->dn = new_prim_group_dn;
989 ret = samdb_msg_add_delval(ldb, ac, msg, "member",
990 ldb_dn_get_linearized(ac->msg->dn));
991 if (ret != LDB_SUCCESS) {
995 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
996 if (ret != LDB_SUCCESS) {
1000 /* Add a "member" attribute for the previous primary group */
1001 msg = talloc_zero(ac, struct ldb_message);
1002 msg->dn = prev_prim_group_dn;
1004 ret = samdb_msg_add_addval(ldb, ac, msg, "member",
1005 ldb_dn_get_linearized(ac->msg->dn));
1006 if (ret != LDB_SUCCESS) {
1010 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1011 if (ret != LDB_SUCCESS) {
1020 static int samldb_member_check(struct samldb_ctx *ac)
1022 struct ldb_context *ldb;
1023 struct ldb_message_element *el;
1024 struct ldb_dn *member_dn, *group_dn;
1025 uint32_t prim_group_rid;
1026 struct dom_sid *sid;
1029 ldb = ldb_module_get_ctx(ac->module);
1031 el = ldb_msg_find_element(ac->msg, "member");
1033 /* we aren't affected */
1037 for (i = 0; i < el->num_values; i++) {
1038 /* Denies to add "member"s to groups which are primary ones
1040 member_dn = ldb_dn_from_ldb_val(ac, ldb, &el->values[i]);
1041 if (!ldb_dn_validate(member_dn)) {
1042 return ldb_operr(ldb);
1045 prim_group_rid = samdb_search_uint(ldb, ac, (uint32_t) -1,
1046 member_dn, "primaryGroupID",
1048 if (prim_group_rid == (uint32_t) -1) {
1049 /* the member hasn't to be a user account -> therefore
1050 * no check needed in this case. */
1054 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1057 return ldb_operr(ldb);
1060 group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
1061 ldap_encode_ndr_dom_sid(ac, sid));
1062 if (group_dn == NULL) {
1063 return ldb_operr(ldb);
1066 if (ldb_dn_compare(group_dn, ac->msg->dn) == 0) {
1067 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1076 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
1078 struct ldb_context *ldb;
1079 struct samldb_ctx *ac;
1082 ldb = ldb_module_get_ctx(module);
1083 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
1085 /* do not manipulate our control entries */
1086 if (ldb_dn_is_special(req->op.add.message->dn)) {
1087 return ldb_next_request(module, req);
1090 ac = samldb_ctx_init(module, req);
1092 return ldb_operr(ldb);
1095 /* build the new msg */
1096 ac->msg = ldb_msg_copy_shallow(ac, req->op.add.message);
1097 if (ac->msg == NULL) {
1099 ldb_debug(ldb, LDB_DEBUG_FATAL,
1100 "samldb_add: ldb_msg_copy_shallow failed!\n");
1101 return ldb_operr(ldb);
1104 if (samdb_find_attribute(ldb, ac->msg,
1105 "objectclass", "user") != NULL) {
1107 return samldb_fill_object(ac);
1110 if (samdb_find_attribute(ldb, ac->msg,
1111 "objectclass", "group") != NULL) {
1113 return samldb_fill_object(ac);
1116 /* perhaps a foreignSecurityPrincipal? */
1117 if (samdb_find_attribute(ldb, ac->msg,
1119 "foreignSecurityPrincipal") != NULL) {
1120 return samldb_fill_foreignSecurityPrincipal_object(ac);
1123 if (samdb_find_attribute(ldb, ac->msg,
1124 "objectclass", "classSchema") != NULL) {
1125 ret = samldb_schema_info_update(ac);
1126 if (ret != LDB_SUCCESS) {
1131 ac->type = "classSchema";
1132 return samldb_fill_object(ac);
1135 if (samdb_find_attribute(ldb, ac->msg,
1136 "objectclass", "attributeSchema") != NULL) {
1137 ret = samldb_schema_info_update(ac);
1138 if (ret != LDB_SUCCESS) {
1143 ac->type = "attributeSchema";
1144 return samldb_fill_object(ac);
1149 /* nothing matched, go on */
1150 return ldb_next_request(module, req);
1154 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
1156 struct ldb_context *ldb;
1157 struct samldb_ctx *ac;
1158 struct ldb_message_element *el, *el2;
1159 bool modified = false;
1161 uint32_t account_type;
1163 if (ldb_dn_is_special(req->op.mod.message->dn)) {
1164 /* do not manipulate our control entries */
1165 return ldb_next_request(module, req);
1168 ldb = ldb_module_get_ctx(module);
1170 if (ldb_msg_find_element(req->op.mod.message, "sAMAccountType") != NULL) {
1171 ldb_asprintf_errstring(ldb,
1172 "sAMAccountType must not be specified!");
1173 return LDB_ERR_UNWILLING_TO_PERFORM;
1176 /* msDS-IntId is not allowed to be modified
1177 * except when modification comes from replication */
1178 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
1179 if (!ldb_request_get_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1180 return LDB_ERR_CONSTRAINT_VIOLATION;
1184 ac = samldb_ctx_init(module, req);
1186 return ldb_operr(ldb);
1189 /* build the new msg */
1190 ac->msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
1191 if (ac->msg == NULL) {
1193 ldb_debug(ldb, LDB_DEBUG_FATAL,
1194 "samldb_modify: ldb_msg_copy_shallow failed!\n");
1195 return ldb_operr(ldb);
1198 el = ldb_msg_find_element(ac->msg, "groupType");
1199 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1200 uint32_t group_type;
1204 group_type = strtoul((const char *)el->values[0].data, NULL, 0);
1205 account_type = ds_gtype2atype(group_type);
1206 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1209 if (ret != LDB_SUCCESS) {
1212 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
1213 el2->flags = LDB_FLAG_MOD_REPLACE;
1215 el = ldb_msg_find_element(ac->msg, "groupType");
1216 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
1217 return LDB_ERR_UNWILLING_TO_PERFORM;
1220 el = ldb_msg_find_element(ac->msg, "primaryGroupID");
1221 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1224 ret = samldb_prim_group_change(ac);
1225 if (ret != LDB_SUCCESS) {
1229 el = ldb_msg_find_element(ac->msg, "primaryGroupID");
1230 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
1231 return LDB_ERR_UNWILLING_TO_PERFORM;
1234 el = ldb_msg_find_element(ac->msg, "userAccountControl");
1235 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1236 uint32_t user_account_control;
1240 user_account_control = strtoul((const char *)el->values[0].data,
1242 account_type = ds_uf2atype(user_account_control);
1243 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1246 if (ret != LDB_SUCCESS) {
1249 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
1250 el2->flags = LDB_FLAG_MOD_REPLACE;
1252 if (user_account_control & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
1253 ret = samdb_msg_add_string(ldb, ac->msg, ac->msg,
1254 "isCriticalSystemObject",
1256 if (ret != LDB_SUCCESS) {
1259 el2 = ldb_msg_find_element(ac->msg,
1260 "isCriticalSystemObject");
1261 el2->flags = LDB_FLAG_MOD_REPLACE;
1264 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
1265 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
1267 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1268 "primaryGroupID", rid);
1269 if (ret != LDB_SUCCESS) {
1272 el2 = ldb_msg_find_element(ac->msg,
1274 el2->flags = LDB_FLAG_MOD_REPLACE;
1277 el = ldb_msg_find_element(ac->msg, "userAccountControl");
1278 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
1279 return LDB_ERR_UNWILLING_TO_PERFORM;
1282 el = ldb_msg_find_element(ac->msg, "member");
1283 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1284 ret = samldb_member_check(ac);
1285 if (ret != LDB_SUCCESS) {
1291 struct ldb_request *child_req;
1293 /* Now perform the real modifications as a child request */
1294 ret = ldb_build_mod_req(&child_req, ldb, ac,
1297 req, dsdb_next_callback,
1299 LDB_REQ_SET_LOCATION(child_req);
1300 if (ret != LDB_SUCCESS) {
1304 return ldb_next_request(module, child_req);
1309 /* no change which interests us, go on */
1310 return ldb_next_request(module, req);
1315 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
1317 struct ldb_context *ldb;
1318 struct dom_sid *sid;
1323 ldb = ldb_module_get_ctx(ac->module);
1325 /* Finds out the SID/RID of the SAM object */
1326 sid = samdb_search_dom_sid(ldb, ac, ac->req->op.del.dn, "objectSid",
1329 /* No SID - it might not be a SAM object - therefore ok */
1332 status = dom_sid_split_rid(ac, sid, NULL, &rid);
1333 if (!NT_STATUS_IS_OK(status)) {
1334 return ldb_operr(ldb);
1337 /* Special object (security principal?) */
1341 /* Deny delete requests from groups which are primary ones */
1342 count = samdb_search_count(ldb, NULL,
1343 "(&(primaryGroupID=%u)(objectClass=user))",
1346 return ldb_operr(ldb);
1349 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1355 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
1357 struct samldb_ctx *ac;
1360 if (ldb_dn_is_special(req->op.del.dn)) {
1361 /* do not manipulate our control entries */
1362 return ldb_next_request(module, req);
1365 ac = samldb_ctx_init(module, req);
1367 return ldb_operr(ldb_module_get_ctx(module));
1370 ret = samldb_prim_group_users_check(ac);
1371 if (ret != LDB_SUCCESS) {
1377 return ldb_next_request(module, req);
1382 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
1384 struct ldb_context *ldb = ldb_module_get_ctx(module);
1385 struct dsdb_fsmo_extended_op *exop;
1388 exop = talloc_get_type(req->op.extended.data, struct dsdb_fsmo_extended_op);
1390 ldb_debug(ldb, LDB_DEBUG_FATAL, "samldb_extended_allocate_rid_pool: invalid extended data\n");
1391 return LDB_ERR_PROTOCOL_ERROR;
1394 ret = ridalloc_allocate_rid_pool_fsmo(module, exop);
1395 if (ret != LDB_SUCCESS) {
1399 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
1402 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
1404 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
1405 return samldb_extended_allocate_rid_pool(module, req);
1408 return ldb_next_request(module, req);
1412 _PUBLIC_ const struct ldb_module_ops ldb_samldb_module_ops = {
1415 .modify = samldb_modify,
1416 .del = samldb_delete,
1417 .extended = samldb_extended