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);
196 static bool samldb_msg_add_sid(struct ldb_message *msg,
198 const struct dom_sid *sid)
201 enum ndr_err_code ndr_err;
203 ndr_err = ndr_push_struct_blob(&v, msg, sid,
204 (ndr_push_flags_fn_t)ndr_push_dom_sid);
205 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
208 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
212 /* allocate a SID using our RID Set */
213 static int samldb_allocate_sid(struct samldb_ctx *ac)
217 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
220 ret = ridalloc_allocate_rid(ac->module, &rid);
221 if (ret != LDB_SUCCESS) {
225 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
227 return ldb_module_oom(ac->module);
230 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
231 return ldb_operr(ldb);
234 return samldb_next_step(ac);
238 see if a krbtgt_number is available
240 static bool samldb_krbtgtnumber_available(struct samldb_ctx *ac, unsigned krbtgt_number)
242 TALLOC_CTX *tmp_ctx = talloc_new(ac);
243 struct ldb_result *res;
244 const char *attrs[] = { NULL };
247 ret = dsdb_module_search(ac->module, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
248 attrs, DSDB_FLAG_NEXT_MODULE,
249 "msDC-SecondaryKrbTgtNumber=%u", krbtgt_number);
250 if (ret == LDB_SUCCESS && res->count == 0) {
251 talloc_free(tmp_ctx);
254 talloc_free(tmp_ctx);
258 /* special handling for add in RODC join */
259 static int samldb_rodc_add(struct samldb_ctx *ac)
261 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
262 unsigned krbtgt_number, i_start, i;
266 /* find a unused msDC-SecondaryKrbTgtNumber */
267 i_start = generate_random() & 0xFFFF;
272 for (i=i_start; i<=0xFFFF; i++) {
273 if (samldb_krbtgtnumber_available(ac, i)) {
278 for (i=1; i<i_start; i++) {
279 if (samldb_krbtgtnumber_available(ac, i)) {
285 ldb_asprintf_errstring(ldb_module_get_ctx(ac->module),
286 "%08X: Unable to find available msDS-SecondaryKrbTgtNumber",
287 W_ERROR_V(WERR_NO_SYSTEM_RESOURCES));
288 return LDB_ERR_OTHER;
291 ret = ldb_msg_add_empty(ac->msg, "msDS-SecondaryKrbTgtNumber", LDB_FLAG_INTERNAL_DISABLE_VALIDATION, NULL);
292 if (ret != LDB_SUCCESS) {
293 return ldb_operr(ldb);
296 ret = ldb_msg_add_fmt(ac->msg, "msDS-SecondaryKrbTgtNumber", "%u", krbtgt_number);
297 if (ret != LDB_SUCCESS) {
298 return ldb_operr(ldb);
301 ret = ldb_msg_add_fmt(ac->msg, "sAMAccountName", "krbtgt_%u", krbtgt_number);
302 if (ret != LDB_SUCCESS) {
303 return ldb_operr(ldb);
306 newpass = generate_random_password(ac, 128, 255);
307 if (newpass == NULL) {
308 return ldb_operr(ldb);
311 ret = ldb_msg_add_steal_string(ac->msg, "clearTextPassword", newpass);
312 if (ret != LDB_SUCCESS) {
313 return ldb_operr(ldb);
316 return samldb_next_step(ac);
319 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
321 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
322 struct ldb_result *res;
323 const char *no_attrs[] = { NULL };
328 ret = dsdb_module_search(ac->module, ac, &res,
329 ac->dn, LDB_SCOPE_BASE, no_attrs,
330 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT | DSDB_FLAG_NEXT_MODULE,
331 "(objectClass=classSchema)");
332 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
333 /* Don't be pricky when the DN doesn't exist if we have the */
334 /* RELAX control specified */
335 if (ldb_request_get_control(ac->req,
336 LDB_CONTROL_RELAX_OID) == NULL) {
337 ldb_set_errstring(ldb,
338 "samldb_find_defaultObjectCategory: "
339 "Invalid DN for 'defaultObjectCategory'!");
340 return LDB_ERR_CONSTRAINT_VIOLATION;
343 if ((ret != LDB_ERR_NO_SUCH_OBJECT) && (ret != LDB_SUCCESS)) {
349 return samldb_next_step(ac);
353 * msDS-IntId attributeSchema attribute handling
354 * during LDB_ADD request processing
356 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
361 uint32_t system_flags;
362 struct ldb_context *ldb;
363 struct ldb_result *ldb_res;
364 struct ldb_dn *schema_dn;
366 ldb = ldb_module_get_ctx(ac->module);
367 schema_dn = ldb_get_schema_basedn(ldb);
369 /* replicated update should always go through */
370 if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
374 /* msDS-IntId is handled by system and should never be
375 * passed by clients */
376 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
377 return LDB_ERR_UNWILLING_TO_PERFORM;
380 /* do not generate msDS-IntId if Relax control is passed */
381 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
385 /* check Functional Level */
386 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
390 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
391 system_flags = ldb_msg_find_attr_as_uint(ac->msg, "systemFlags", 0);
392 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
396 /* Generate new value for msDs-IntId
397 * Value should be in 0x80000000..0xBFFFFFFF range */
398 msds_intid = generate_random() % 0X3FFFFFFF;
399 msds_intid += 0x80000000;
401 /* probe id values until unique one is found */
404 if (msds_intid > 0xBFFFFFFF) {
405 msds_intid = 0x80000001;
408 ret = dsdb_module_search(ac->module, ac,
410 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
411 DSDB_FLAG_NEXT_MODULE,
412 "(msDS-IntId=%d)", msds_intid);
413 if (ret != LDB_SUCCESS) {
414 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
415 __location__": Searching for msDS-IntId=%d failed - %s\n",
418 return ldb_operr(ldb);
420 id_exists = (ldb_res->count > 0);
422 talloc_free(ldb_res);
425 return ldb_msg_add_fmt(ac->msg, "msDS-IntId", "%d", msds_intid);
430 * samldb_add_entry (async)
433 static int samldb_add_entry_callback(struct ldb_request *req,
434 struct ldb_reply *ares)
436 struct ldb_context *ldb;
437 struct samldb_ctx *ac;
440 ac = talloc_get_type(req->context, struct samldb_ctx);
441 ldb = ldb_module_get_ctx(ac->module);
444 return ldb_module_done(ac->req, NULL, NULL,
445 LDB_ERR_OPERATIONS_ERROR);
448 if (ares->type == LDB_REPLY_REFERRAL) {
449 return ldb_module_send_referral(ac->req, ares->referral);
452 if (ares->error != LDB_SUCCESS) {
453 return ldb_module_done(ac->req, ares->controls,
454 ares->response, ares->error);
456 if (ares->type != LDB_REPLY_DONE) {
457 ldb_set_errstring(ldb,
458 "Invalid reply type!\n");
459 return ldb_module_done(ac->req, NULL, NULL,
460 LDB_ERR_OPERATIONS_ERROR);
463 /* The caller may wish to get controls back from the add */
464 ac->ares = talloc_steal(ac, ares);
466 ret = samldb_next_step(ac);
467 if (ret != LDB_SUCCESS) {
468 return ldb_module_done(ac->req, NULL, NULL, ret);
473 static int samldb_add_entry(struct samldb_ctx *ac)
475 struct ldb_context *ldb;
476 struct ldb_request *req;
479 ldb = ldb_module_get_ctx(ac->module);
481 ret = ldb_build_add_req(&req, ldb, ac,
484 ac, samldb_add_entry_callback,
486 LDB_REQ_SET_LOCATION(req);
487 if (ret != LDB_SUCCESS) {
491 return ldb_next_request(ac->module, req);
495 * return true if msg carries an attributeSchema that is intended to be RODC
496 * filtered but is also a system-critical attribute.
498 static bool check_rodc_critical_attribute(struct ldb_message *msg)
500 uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
502 schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
503 searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
504 rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE | SEARCH_FLAG_CONFIDENTIAL);
506 if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
507 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
515 static int samldb_fill_object(struct samldb_ctx *ac)
517 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
520 /* Add informations for the different account types */
521 if (strcmp(ac->type, "user") == 0) {
522 struct ldb_control *rodc_control = ldb_request_get_control(ac->req,
523 LDB_CONTROL_RODC_DCPROMO_OID);
524 if (rodc_control != NULL) {
525 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
526 rodc_control->critical = false;
527 ret = samldb_add_step(ac, samldb_rodc_add);
528 if (ret != LDB_SUCCESS) return ret;
531 /* check if we have a valid sAMAccountName */
532 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
533 if (ret != LDB_SUCCESS) return ret;
535 ret = samldb_add_step(ac, samldb_add_entry);
536 if (ret != LDB_SUCCESS) return ret;
538 } else if (strcmp(ac->type, "group") == 0) {
539 /* check if we have a valid sAMAccountName */
540 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
541 if (ret != LDB_SUCCESS) return ret;
543 ret = samldb_add_step(ac, samldb_add_entry);
544 if (ret != LDB_SUCCESS) return ret;
546 } else if (strcmp(ac->type, "classSchema") == 0) {
547 const struct ldb_val *rdn_value, *def_obj_cat_val;
549 ret = samdb_find_or_add_attribute(ldb, ac->msg,
551 if (ret != LDB_SUCCESS) return ret;
553 /* do not allow to mark an attributeSchema as RODC filtered if it
554 * is system-critical */
555 if (check_rodc_critical_attribute(ac->msg)) {
556 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
557 ldb_dn_get_linearized(ac->msg->dn));
558 return LDB_ERR_UNWILLING_TO_PERFORM;
561 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
562 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
563 /* the RDN has prefix "CN" */
564 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
565 samdb_cn_to_lDAPDisplayName(ac,
566 (const char *) rdn_value->data));
567 if (ret != LDB_SUCCESS) {
573 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
576 guid = GUID_random();
577 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
578 if (ret != LDB_SUCCESS) {
584 def_obj_cat_val = ldb_msg_find_ldb_val(ac->msg,
585 "defaultObjectCategory");
586 if (def_obj_cat_val != NULL) {
587 /* "defaultObjectCategory" has been set by the caller.
588 * Do some checks for consistency.
589 * NOTE: The real constraint check (that
590 * 'defaultObjectCategory' is the DN of the new
591 * objectclass or any parent of it) is still incomplete.
592 * For now we say that 'defaultObjectCategory' is valid
593 * if it exists and it is of objectclass "classSchema".
595 ac->dn = ldb_dn_from_ldb_val(ac, ldb, def_obj_cat_val);
596 if (ac->dn == NULL) {
597 ldb_set_errstring(ldb,
598 "Invalid DN for 'defaultObjectCategory'!");
599 return LDB_ERR_CONSTRAINT_VIOLATION;
602 /* "defaultObjectCategory" has not been set by the
603 * caller. Use the entry DN for it. */
604 ac->dn = ac->msg->dn;
606 ret = samdb_msg_add_string(ldb, ac->msg, ac->msg,
607 "defaultObjectCategory",
608 ldb_dn_get_linearized(ac->dn));
609 if (ret != LDB_SUCCESS) {
615 ret = samldb_add_step(ac, samldb_add_entry);
616 if (ret != LDB_SUCCESS) return ret;
618 /* Now perform the checks for the 'defaultObjectCategory'. The
619 * lookup DN was already saved in "ac->dn" */
620 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
621 if (ret != LDB_SUCCESS) return ret;
623 } else if (strcmp(ac->type, "attributeSchema") == 0) {
624 const struct ldb_val *rdn_value;
625 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
626 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
627 /* the RDN has prefix "CN" */
628 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
629 samdb_cn_to_lDAPDisplayName(ac->msg,
630 (const char *) rdn_value->data));
631 if (ret != LDB_SUCCESS) {
637 /* do not allow to mark an attributeSchema as RODC filtered if it
638 * is system-critical */
639 if (check_rodc_critical_attribute(ac->msg)) {
640 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical attribute with RODC filtering",
641 ldb_dn_get_linearized(ac->msg->dn));
642 return LDB_ERR_UNWILLING_TO_PERFORM;
645 ret = samdb_find_or_add_attribute(ldb, ac->msg,
646 "isSingleValued", "FALSE");
647 if (ret != LDB_SUCCESS) return ret;
649 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
652 guid = GUID_random();
653 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
654 if (ret != LDB_SUCCESS) {
660 /* handle msDS-IntID attribute */
661 ret = samldb_add_handle_msDS_IntId(ac);
662 if (ret != LDB_SUCCESS) return ret;
664 ret = samldb_add_step(ac, samldb_add_entry);
665 if (ret != LDB_SUCCESS) return ret;
668 ldb_asprintf_errstring(ldb,
669 "Invalid entry type!");
670 return LDB_ERR_OPERATIONS_ERROR;
673 return samldb_first_step(ac);
676 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
678 struct ldb_context *ldb;
682 ldb = ldb_module_get_ctx(ac->module);
684 sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
686 sid = dom_sid_parse_talloc(ac->msg,
687 (const char *)ldb_dn_get_rdn_val(ac->msg->dn)->data);
689 ldb_set_errstring(ldb,
690 "No valid SID found in "
691 "ForeignSecurityPrincipal CN!");
692 return LDB_ERR_CONSTRAINT_VIOLATION;
694 if (! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
695 return ldb_operr(ldb);
699 /* finally proceed with adding the entry */
700 ret = samldb_add_step(ac, samldb_add_entry);
701 if (ret != LDB_SUCCESS) return ret;
703 return samldb_first_step(ac);
706 static int samldb_schema_info_update(struct samldb_ctx *ac)
709 struct ldb_context *ldb;
710 struct dsdb_schema *schema;
712 /* replicated update should always go through */
713 if (ldb_request_get_control(ac->req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
717 /* do not update schemaInfo during provisioning */
718 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
722 ldb = ldb_module_get_ctx(ac->module);
723 schema = dsdb_get_schema(ldb, NULL);
725 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
726 "samldb_schema_info_update: no dsdb_schema loaded");
727 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
728 return ldb_operr(ldb);
731 werr = dsdb_module_schema_info_update(ac->module, schema, DSDB_FLAG_NEXT_MODULE);
732 if (!W_ERROR_IS_OK(werr)) {
733 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
734 "samldb_schema_info_update: "
735 "dsdb_module_schema_info_update failed with %s",
737 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
738 return ldb_operr(ldb);
745 * "Objectclass" trigger (MS-SAMR 3.1.1.8.1)
747 * Has to be invoked on "add" and "modify" operations on "user", "computer" and
749 * ac->msg contains the "add"/"modify" message
750 * ac->type contains the object type (main objectclass)
752 static int samldb_objectclass_trigger(struct samldb_ctx *ac)
754 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
755 struct loadparm_context *lp_ctx = talloc_get_type(ldb_get_opaque(ldb,
756 "loadparm"), struct loadparm_context);
757 struct ldb_message_element *el, *el2;
758 enum sid_generator sid_generator;
762 /* make sure that "sAMAccountType" is not specified */
763 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
765 ldb_set_errstring(ldb,
766 "samldb: sAMAccountType must not be specified!");
767 return LDB_ERR_UNWILLING_TO_PERFORM;
770 /* Step 1: objectSid assignment */
772 /* Don't allow the objectSid to be changed. But beside the RELAX
773 * control we have also to guarantee that it can always be set with
774 * SYSTEM permissions. This is needed for the "samba3sam" backend. */
775 sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
776 if ((sid != NULL) && (!dsdb_module_am_system(ac->module)) &&
777 (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
778 ldb_asprintf_errstring(ldb, "No SID may be specified in user/group modifications for %s",
779 ldb_dn_get_linearized(ac->msg->dn));
780 return LDB_ERR_UNWILLING_TO_PERFORM;
783 /* but generate a new SID when we do have an add operations */
784 if ((sid == NULL) && (ac->req->operation == LDB_ADD)) {
785 sid_generator = lpcfg_sid_generator(lp_ctx);
786 if (sid_generator == SID_GENERATOR_INTERNAL) {
787 ret = samldb_add_step(ac, samldb_allocate_sid);
788 if (ret != LDB_SUCCESS) return ret;
792 if (strcmp(ac->type, "user") == 0) {
793 /* Step 1.2: Default values */
794 ret = samdb_find_or_add_attribute(ldb, ac->msg,
795 "userAccountControl",
796 talloc_asprintf(ac->msg, "%u", UF_NORMAL_ACCOUNT));
797 if (ret != LDB_SUCCESS) return ret;
798 ret = samdb_find_or_add_attribute(ldb, ac->msg,
800 if (ret != LDB_SUCCESS) return ret;
801 ret = samdb_find_or_add_attribute(ldb, ac->msg,
803 if (ret != LDB_SUCCESS) return ret;
804 ret = samdb_find_or_add_attribute(ldb, ac->msg,
806 if (ret != LDB_SUCCESS) return ret;
807 ret = samdb_find_or_add_attribute(ldb, ac->msg,
808 "badPasswordTime", "0");
809 if (ret != LDB_SUCCESS) return ret;
810 ret = samdb_find_or_add_attribute(ldb, ac->msg,
812 if (ret != LDB_SUCCESS) return ret;
813 ret = samdb_find_or_add_attribute(ldb, ac->msg,
815 if (ret != LDB_SUCCESS) return ret;
816 ret = samdb_find_or_add_attribute(ldb, ac->msg,
818 if (ret != LDB_SUCCESS) return ret;
819 ret = samdb_find_or_add_attribute(ldb, ac->msg,
820 "accountExpires", "9223372036854775807");
821 if (ret != LDB_SUCCESS) return ret;
822 ret = samdb_find_or_add_attribute(ldb, ac->msg,
824 if (ret != LDB_SUCCESS) return ret;
826 el = ldb_msg_find_element(ac->msg, "userAccountControl");
828 uint32_t user_account_control, account_type;
830 /* Step 1.3: "userAccountControl" -> "sAMAccountType" mapping */
831 user_account_control = strtoul((const char *)el->values[0].data,
833 account_type = ds_uf2atype(user_account_control);
834 if (account_type == 0) {
835 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
836 return LDB_ERR_UNWILLING_TO_PERFORM;
838 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
841 if (ret != LDB_SUCCESS) {
844 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
845 el2->flags = LDB_FLAG_MOD_REPLACE;
847 if (user_account_control &
848 (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
849 ret = samdb_msg_set_string(ldb, ac->msg, ac->msg,
850 "isCriticalSystemObject",
852 if (ret != LDB_SUCCESS) {
855 el2 = ldb_msg_find_element(ac->msg,
856 "isCriticalSystemObject");
857 el2->flags = LDB_FLAG_MOD_REPLACE;
860 /* Step 1.4: "userAccountControl" -> "primaryGroupID" mapping */
861 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
862 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
863 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
864 "primaryGroupID", rid);
865 if (ret != LDB_SUCCESS) {
868 el2 = ldb_msg_find_element(ac->msg,
870 el2->flags = LDB_FLAG_MOD_REPLACE;
873 /* Step 1.5: Add additional flags when needed */
874 if ((user_account_control & UF_NORMAL_ACCOUNT) &&
875 (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
876 user_account_control |= UF_ACCOUNTDISABLE;
877 user_account_control |= UF_PASSWD_NOTREQD;
879 ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
880 "userAccountControl",
881 user_account_control);
882 if (ret != LDB_SUCCESS) {
888 } else if (strcmp(ac->type, "group") == 0) {
889 /* Step 2.2: Default values */
890 ret = samdb_find_or_add_attribute(ldb, ac->msg,
892 talloc_asprintf(ac->msg, "%u", GTYPE_SECURITY_GLOBAL_GROUP));
893 if (ret != LDB_SUCCESS) return ret;
895 /* Step 2.3: "groupType" -> "sAMAccountType" */
896 el = ldb_msg_find_element(ac->msg, "groupType");
898 uint32_t group_type, account_type;
900 group_type = strtoul((const char *)el->values[0].data,
902 account_type = ds_gtype2atype(group_type);
903 if (account_type == 0) {
904 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
905 return LDB_ERR_UNWILLING_TO_PERFORM;
907 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
910 if (ret != LDB_SUCCESS) {
913 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
914 el2->flags = LDB_FLAG_MOD_REPLACE;
922 * "Primary group ID" trigger (MS-SAMR 3.1.1.8.2)
924 * Has to be invoked on "add" and "modify" operations on "user" and "computer"
926 * ac->msg contains the "add"/"modify" message
929 static int samldb_prim_group_set(struct samldb_ctx *ac)
931 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
932 struct ldb_dn *prim_group_dn;
936 rid = samdb_result_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
937 if (rid == (uint32_t) -1) {
938 /* we aren't affected of any primary group set */
941 } else if (!ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
942 ldb_set_errstring(ldb,
943 "The primary group isn't settable on add operations!");
944 return LDB_ERR_UNWILLING_TO_PERFORM;
947 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
949 return ldb_operr(ldb);
952 prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
953 ldap_encode_ndr_dom_sid(ac, sid));
954 if (prim_group_dn == NULL) {
955 ldb_asprintf_errstring(ldb,
956 "Failed to find primary group with RID %u!",
958 return LDB_ERR_UNWILLING_TO_PERFORM;
964 static int samldb_prim_group_change(struct samldb_ctx *ac)
966 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
967 const char * attrs[] = { "primaryGroupID", "memberOf", NULL };
968 struct ldb_result *res;
969 struct ldb_message_element *el;
970 struct ldb_message *msg;
973 struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
976 /* Fetch informations from the existing object */
978 ret = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
980 if (ret != LDB_SUCCESS) {
984 /* Finds out the DN of the old primary group */
986 rid = samdb_result_uint(res->msgs[0], "primaryGroupID", (uint32_t) -1);
987 if (rid == (uint32_t) -1) {
988 /* User objects do always have a mandatory "primaryGroupID"
989 * attribute. If this doesn't exist then the object is of the
990 * wrong type. This is the exact Windows error code */
991 return LDB_ERR_OBJECT_CLASS_VIOLATION;
994 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
996 return ldb_operr(ldb);
999 prev_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
1000 ldap_encode_ndr_dom_sid(ac, sid));
1001 if (prev_prim_group_dn == NULL) {
1002 return ldb_operr(ldb);
1005 /* Finds out the DN of the new primary group */
1007 rid = samdb_result_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
1008 if (rid == (uint32_t) -1) {
1009 /* we aren't affected of any primary group change */
1013 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1015 return ldb_operr(ldb);
1018 new_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
1019 ldap_encode_ndr_dom_sid(ac, sid));
1020 if (new_prim_group_dn == NULL) {
1021 /* Here we know if the specified new primary group candidate is
1023 return LDB_ERR_UNWILLING_TO_PERFORM;
1026 /* Only update the "member" attributes when we really do have a change */
1027 if (ldb_dn_compare(new_prim_group_dn, prev_prim_group_dn) != 0) {
1028 /* We need to be already a normal member of the new primary
1029 * group in order to be successful. */
1030 el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
1031 ldb_dn_get_linearized(new_prim_group_dn));
1033 return LDB_ERR_UNWILLING_TO_PERFORM;
1036 /* Remove the "member" attribute on the new primary group */
1037 msg = talloc_zero(ac, struct ldb_message);
1038 msg->dn = new_prim_group_dn;
1040 ret = samdb_msg_add_delval(ldb, ac, msg, "member",
1041 ldb_dn_get_linearized(ac->msg->dn));
1042 if (ret != LDB_SUCCESS) {
1046 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1047 if (ret != LDB_SUCCESS) {
1051 /* Add a "member" attribute for the previous primary group */
1052 msg = talloc_zero(ac, struct ldb_message);
1053 msg->dn = prev_prim_group_dn;
1055 ret = samdb_msg_add_addval(ldb, ac, msg, "member",
1056 ldb_dn_get_linearized(ac->msg->dn));
1057 if (ret != LDB_SUCCESS) {
1061 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1062 if (ret != LDB_SUCCESS) {
1070 static int samldb_prim_group_trigger(struct samldb_ctx *ac)
1074 if (ac->req->operation == LDB_ADD) {
1075 ret = samldb_prim_group_set(ac);
1077 ret = samldb_prim_group_change(ac);
1083 static int samldb_member_check(struct samldb_ctx *ac)
1085 struct ldb_context *ldb;
1086 struct ldb_message_element *el;
1087 struct ldb_dn *member_dn, *group_dn;
1088 uint32_t prim_group_rid;
1089 struct dom_sid *sid;
1092 ldb = ldb_module_get_ctx(ac->module);
1094 el = ldb_msg_find_element(ac->msg, "member");
1096 /* we aren't affected */
1100 for (i = 0; i < el->num_values; i++) {
1101 /* Denies to add "member"s to groups which are primary ones
1103 member_dn = ldb_dn_from_ldb_val(ac, ldb, &el->values[i]);
1104 if (!ldb_dn_validate(member_dn)) {
1105 return ldb_operr(ldb);
1108 prim_group_rid = samdb_search_uint(ldb, ac, (uint32_t) -1,
1109 member_dn, "primaryGroupID",
1111 if (prim_group_rid == (uint32_t) -1) {
1112 /* the member hasn't to be a user account -> therefore
1113 * no check needed in this case. */
1117 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1120 return ldb_operr(ldb);
1123 group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
1124 ldap_encode_ndr_dom_sid(ac, sid));
1125 if (group_dn == NULL) {
1126 return ldb_operr(ldb);
1129 if (ldb_dn_compare(group_dn, ac->msg->dn) == 0) {
1130 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1139 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
1141 struct ldb_context *ldb;
1142 struct samldb_ctx *ac;
1145 ldb = ldb_module_get_ctx(module);
1146 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
1148 /* do not manipulate our control entries */
1149 if (ldb_dn_is_special(req->op.add.message->dn)) {
1150 return ldb_next_request(module, req);
1153 ac = samldb_ctx_init(module, req);
1155 return ldb_operr(ldb);
1158 /* build the new msg */
1159 ac->msg = ldb_msg_copy_shallow(ac, req->op.add.message);
1160 if (ac->msg == NULL) {
1162 ldb_debug(ldb, LDB_DEBUG_FATAL,
1163 "samldb_add: ldb_msg_copy_shallow failed!\n");
1164 return ldb_operr(ldb);
1167 if (samdb_find_attribute(ldb, ac->msg,
1168 "objectclass", "user") != NULL) {
1171 ret = samldb_prim_group_trigger(ac);
1172 if (ret != LDB_SUCCESS) {
1176 ret = samldb_objectclass_trigger(ac);
1177 if (ret != LDB_SUCCESS) {
1181 return samldb_fill_object(ac);
1184 if (samdb_find_attribute(ldb, ac->msg,
1185 "objectclass", "group") != NULL) {
1188 ret = samldb_objectclass_trigger(ac);
1189 if (ret != LDB_SUCCESS) {
1193 return samldb_fill_object(ac);
1196 /* perhaps a foreignSecurityPrincipal? */
1197 if (samdb_find_attribute(ldb, ac->msg,
1199 "foreignSecurityPrincipal") != NULL) {
1200 return samldb_fill_foreignSecurityPrincipal_object(ac);
1203 if (samdb_find_attribute(ldb, ac->msg,
1204 "objectclass", "classSchema") != NULL) {
1205 ret = samldb_schema_info_update(ac);
1206 if (ret != LDB_SUCCESS) {
1211 ac->type = "classSchema";
1212 return samldb_fill_object(ac);
1215 if (samdb_find_attribute(ldb, ac->msg,
1216 "objectclass", "attributeSchema") != NULL) {
1217 ret = samldb_schema_info_update(ac);
1218 if (ret != LDB_SUCCESS) {
1223 ac->type = "attributeSchema";
1224 return samldb_fill_object(ac);
1229 /* nothing matched, go on */
1230 return ldb_next_request(module, req);
1234 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
1236 struct ldb_context *ldb;
1237 struct samldb_ctx *ac;
1238 struct ldb_message_element *el, *el2;
1239 bool modified = false;
1241 uint32_t account_type;
1243 if (ldb_dn_is_special(req->op.mod.message->dn)) {
1244 /* do not manipulate our control entries */
1245 return ldb_next_request(module, req);
1248 ldb = ldb_module_get_ctx(module);
1250 if (ldb_msg_find_element(req->op.mod.message, "sAMAccountType") != NULL) {
1251 ldb_asprintf_errstring(ldb,
1252 "sAMAccountType must not be specified!");
1253 return LDB_ERR_UNWILLING_TO_PERFORM;
1256 /* msDS-IntId is not allowed to be modified
1257 * except when modification comes from replication */
1258 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
1259 if (!ldb_request_get_control(req, DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1260 return LDB_ERR_CONSTRAINT_VIOLATION;
1264 ac = samldb_ctx_init(module, req);
1266 return ldb_operr(ldb);
1269 /* build the new msg */
1270 ac->msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
1271 if (ac->msg == NULL) {
1273 ldb_debug(ldb, LDB_DEBUG_FATAL,
1274 "samldb_modify: ldb_msg_copy_shallow failed!\n");
1275 return ldb_operr(ldb);
1278 el = ldb_msg_find_element(ac->msg, "groupType");
1279 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1280 uint32_t group_type;
1284 group_type = strtoul((const char *)el->values[0].data, NULL, 0);
1285 account_type = ds_gtype2atype(group_type);
1286 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1289 if (ret != LDB_SUCCESS) {
1292 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
1293 el2->flags = LDB_FLAG_MOD_REPLACE;
1295 el = ldb_msg_find_element(ac->msg, "groupType");
1296 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
1297 return LDB_ERR_UNWILLING_TO_PERFORM;
1300 el = ldb_msg_find_element(ac->msg, "primaryGroupID");
1301 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1304 ret = samldb_prim_group_change(ac);
1305 if (ret != LDB_SUCCESS) {
1309 el = ldb_msg_find_element(ac->msg, "primaryGroupID");
1310 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
1311 return LDB_ERR_UNWILLING_TO_PERFORM;
1314 el = ldb_msg_find_element(ac->msg, "userAccountControl");
1315 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1316 uint32_t user_account_control;
1320 user_account_control = strtoul((const char *)el->values[0].data,
1322 account_type = ds_uf2atype(user_account_control);
1323 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1326 if (ret != LDB_SUCCESS) {
1329 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
1330 el2->flags = LDB_FLAG_MOD_REPLACE;
1332 if (user_account_control & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
1333 ret = samdb_msg_add_string(ldb, ac->msg, ac->msg,
1334 "isCriticalSystemObject",
1336 if (ret != LDB_SUCCESS) {
1339 el2 = ldb_msg_find_element(ac->msg,
1340 "isCriticalSystemObject");
1341 el2->flags = LDB_FLAG_MOD_REPLACE;
1344 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
1345 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
1347 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1348 "primaryGroupID", rid);
1349 if (ret != LDB_SUCCESS) {
1352 el2 = ldb_msg_find_element(ac->msg,
1354 el2->flags = LDB_FLAG_MOD_REPLACE;
1357 el = ldb_msg_find_element(ac->msg, "userAccountControl");
1358 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
1359 return LDB_ERR_UNWILLING_TO_PERFORM;
1362 el = ldb_msg_find_element(ac->msg, "member");
1363 if (el && el->flags & (LDB_FLAG_MOD_ADD|LDB_FLAG_MOD_REPLACE) && el->num_values == 1) {
1364 ret = samldb_member_check(ac);
1365 if (ret != LDB_SUCCESS) {
1371 struct ldb_request *child_req;
1373 /* Now perform the real modifications as a child request */
1374 ret = ldb_build_mod_req(&child_req, ldb, ac,
1377 req, dsdb_next_callback,
1379 LDB_REQ_SET_LOCATION(child_req);
1380 if (ret != LDB_SUCCESS) {
1384 return ldb_next_request(module, child_req);
1389 /* no change which interests us, go on */
1390 return ldb_next_request(module, req);
1395 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
1397 struct ldb_context *ldb;
1398 struct dom_sid *sid;
1403 ldb = ldb_module_get_ctx(ac->module);
1405 /* Finds out the SID/RID of the SAM object */
1406 sid = samdb_search_dom_sid(ldb, ac, ac->req->op.del.dn, "objectSid",
1409 /* No SID - it might not be a SAM object - therefore ok */
1412 status = dom_sid_split_rid(ac, sid, NULL, &rid);
1413 if (!NT_STATUS_IS_OK(status)) {
1414 return ldb_operr(ldb);
1417 /* Special object (security principal?) */
1421 /* Deny delete requests from groups which are primary ones */
1422 count = samdb_search_count(ldb, NULL,
1423 "(&(primaryGroupID=%u)(objectClass=user))",
1426 return ldb_operr(ldb);
1429 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1435 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
1437 struct samldb_ctx *ac;
1440 if (ldb_dn_is_special(req->op.del.dn)) {
1441 /* do not manipulate our control entries */
1442 return ldb_next_request(module, req);
1445 ac = samldb_ctx_init(module, req);
1447 return ldb_operr(ldb_module_get_ctx(module));
1450 ret = samldb_prim_group_users_check(ac);
1451 if (ret != LDB_SUCCESS) {
1457 return ldb_next_request(module, req);
1462 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
1464 struct ldb_context *ldb = ldb_module_get_ctx(module);
1465 struct dsdb_fsmo_extended_op *exop;
1468 exop = talloc_get_type(req->op.extended.data, struct dsdb_fsmo_extended_op);
1470 ldb_debug(ldb, LDB_DEBUG_FATAL, "samldb_extended_allocate_rid_pool: invalid extended data\n");
1471 return LDB_ERR_PROTOCOL_ERROR;
1474 ret = ridalloc_allocate_rid_pool_fsmo(module, exop);
1475 if (ret != LDB_SUCCESS) {
1479 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
1482 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
1484 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
1485 return samldb_extended_allocate_rid_pool(module, req);
1488 return ldb_next_request(module, req);
1492 _PUBLIC_ const struct ldb_module_ops ldb_samldb_module_ops = {
1495 .modify = samldb_modify,
1496 .del = samldb_delete,
1497 .extended = samldb_extended