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: various internal DSDB triggers - most for SAM specific objects
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"
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 /* used in "samldb_find_for_defaultObjectCategory" */
63 struct ldb_dn *dn, *res_dn;
65 /* all the async steps necessary to complete the operation */
66 struct samldb_step *steps;
67 struct samldb_step *curstep;
69 /* If someone set an ares to forward controls and response back to the caller */
70 struct ldb_reply *ares;
73 static struct samldb_ctx *samldb_ctx_init(struct ldb_module *module,
74 struct ldb_request *req)
76 struct ldb_context *ldb;
77 struct samldb_ctx *ac;
79 ldb = ldb_module_get_ctx(module);
81 ac = talloc_zero(req, struct samldb_ctx);
93 static int samldb_add_step(struct samldb_ctx *ac, samldb_step_fn_t fn)
95 struct samldb_step *step, *stepper;
97 step = talloc_zero(ac, struct samldb_step);
99 return ldb_oom(ldb_module_get_ctx(ac->module));
104 if (ac->steps == NULL) {
108 if (ac->curstep == NULL)
109 return ldb_operr(ldb_module_get_ctx(ac->module));
110 for (stepper = ac->curstep; stepper->next != NULL;
111 stepper = stepper->next);
112 stepper->next = step;
118 static int samldb_first_step(struct samldb_ctx *ac)
120 if (ac->steps == NULL) {
121 return ldb_operr(ldb_module_get_ctx(ac->module));
124 ac->curstep = ac->steps;
125 return ac->curstep->fn(ac);
128 static int samldb_next_step(struct samldb_ctx *ac)
130 if (ac->curstep->next) {
131 ac->curstep = ac->curstep->next;
132 return ac->curstep->fn(ac);
135 /* We exit the samldb module here. If someone set an "ares" to forward
136 * controls and response back to the caller, use them. */
138 return ldb_module_done(ac->req, ac->ares->controls,
139 ac->ares->response, LDB_SUCCESS);
141 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
146 /* sAMAccountName handling */
148 static int samldb_generate_sAMAccountName(struct ldb_context *ldb,
149 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 /* The "sAMAccountName" cannot be nothing */
181 ldb_set_errstring(ldb,
182 "samldb: Empty account names aren't allowed!");
183 return LDB_ERR_CONSTRAINT_VIOLATION;
186 ret = samdb_search_count(ldb, ac, NULL, "(sAMAccountName=%s)",
187 ldb_binary_encode_string(ac, name));
188 if ((ret < 0) || (ret > 1)) {
189 return ldb_operr(ldb);
192 ldb_asprintf_errstring(ldb,
193 "samldb: Account name (sAMAccountName) '%s' already in use!",
195 return LDB_ERR_ENTRY_ALREADY_EXISTS;
198 return samldb_next_step(ac);
202 static bool samldb_msg_add_sid(struct ldb_message *msg,
204 const struct dom_sid *sid)
207 enum ndr_err_code ndr_err;
209 ndr_err = ndr_push_struct_blob(&v, msg, sid,
210 (ndr_push_flags_fn_t)ndr_push_dom_sid);
211 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
214 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
218 /* allocate a SID using our RID Set */
219 static int samldb_allocate_sid(struct samldb_ctx *ac)
223 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
226 ret = ridalloc_allocate_rid(ac->module, &rid);
227 if (ret != LDB_SUCCESS) {
231 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
233 return ldb_module_oom(ac->module);
236 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
237 return ldb_operr(ldb);
240 return samldb_next_step(ac);
244 see if a krbtgt_number is available
246 static bool samldb_krbtgtnumber_available(struct samldb_ctx *ac,
247 uint32_t krbtgt_number)
249 TALLOC_CTX *tmp_ctx = talloc_new(ac);
250 struct ldb_result *res;
251 const char *no_attrs[] = { NULL };
254 ret = dsdb_module_search(ac->module, tmp_ctx, &res, NULL,
255 LDB_SCOPE_SUBTREE, no_attrs,
256 DSDB_FLAG_NEXT_MODULE,
257 "(msDC-SecondaryKrbTgtNumber=%u)",
259 if (ret == LDB_SUCCESS && res->count == 0) {
260 talloc_free(tmp_ctx);
263 talloc_free(tmp_ctx);
267 /* special handling for add in RODC join */
268 static int samldb_rodc_add(struct samldb_ctx *ac)
270 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
271 uint32_t krbtgt_number, i_start, i;
275 /* find a unused msDC-SecondaryKrbTgtNumber */
276 i_start = generate_random() & 0xFFFF;
281 for (i=i_start; i<=0xFFFF; i++) {
282 if (samldb_krbtgtnumber_available(ac, i)) {
287 for (i=1; i<i_start; i++) {
288 if (samldb_krbtgtnumber_available(ac, i)) {
294 ldb_asprintf_errstring(ldb,
295 "%08X: Unable to find available msDS-SecondaryKrbTgtNumber",
296 W_ERROR_V(WERR_NO_SYSTEM_RESOURCES));
297 return LDB_ERR_OTHER;
300 ret = ldb_msg_add_empty(ac->msg, "msDS-SecondaryKrbTgtNumber",
301 LDB_FLAG_INTERNAL_DISABLE_VALIDATION, NULL);
302 if (ret != LDB_SUCCESS) {
303 return ldb_operr(ldb);
306 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
307 "msDS-SecondaryKrbTgtNumber", krbtgt_number);
308 if (ret != LDB_SUCCESS) {
309 return ldb_operr(ldb);
312 ret = ldb_msg_add_fmt(ac->msg, "sAMAccountName", "krbtgt_%u",
314 if (ret != LDB_SUCCESS) {
315 return ldb_operr(ldb);
318 newpass = generate_random_password(ac->msg, 128, 255);
319 if (newpass == NULL) {
320 return ldb_operr(ldb);
323 ret = ldb_msg_add_steal_string(ac->msg, "clearTextPassword", newpass);
324 if (ret != LDB_SUCCESS) {
325 return ldb_operr(ldb);
328 return samldb_next_step(ac);
331 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
333 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
334 struct ldb_result *res;
335 const char *no_attrs[] = { NULL };
340 ret = dsdb_module_search(ac->module, ac, &res,
341 ac->dn, LDB_SCOPE_BASE, no_attrs,
342 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
343 | DSDB_FLAG_NEXT_MODULE,
344 "(objectClass=classSchema)");
345 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
346 /* Don't be pricky when the DN doesn't exist if we have the */
347 /* RELAX control specified */
348 if (ldb_request_get_control(ac->req,
349 LDB_CONTROL_RELAX_OID) == NULL) {
350 ldb_set_errstring(ldb,
351 "samldb_find_defaultObjectCategory: "
352 "Invalid DN for 'defaultObjectCategory'!");
353 return LDB_ERR_CONSTRAINT_VIOLATION;
356 if ((ret != LDB_ERR_NO_SUCH_OBJECT) && (ret != LDB_SUCCESS)) {
362 return samldb_next_step(ac);
366 * msDS-IntId attributeSchema attribute handling
367 * during LDB_ADD request processing
369 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
374 int32_t system_flags;
375 struct ldb_context *ldb;
376 struct ldb_result *ldb_res;
377 struct ldb_dn *schema_dn;
379 ldb = ldb_module_get_ctx(ac->module);
380 schema_dn = ldb_get_schema_basedn(ldb);
382 /* replicated update should always go through */
383 if (ldb_request_get_control(ac->req,
384 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
388 /* msDS-IntId is handled by system and should never be
389 * passed by clients */
390 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
391 return LDB_ERR_UNWILLING_TO_PERFORM;
394 /* do not generate msDS-IntId if Relax control is passed */
395 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
399 /* check Functional Level */
400 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
404 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
405 system_flags = ldb_msg_find_attr_as_int(ac->msg, "systemFlags", 0);
406 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
410 /* Generate new value for msDs-IntId
411 * Value should be in 0x80000000..0xBFFFFFFF range */
412 msds_intid = generate_random() % 0X3FFFFFFF;
413 msds_intid += 0x80000000;
415 /* probe id values until unique one is found */
418 if (msds_intid > 0xBFFFFFFF) {
419 msds_intid = 0x80000001;
422 ret = dsdb_module_search(ac->module, ac,
424 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
425 DSDB_FLAG_NEXT_MODULE,
426 "(msDS-IntId=%d)", msds_intid);
427 if (ret != LDB_SUCCESS) {
428 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
429 __location__": Searching for msDS-IntId=%d failed - %s\n",
432 return ldb_operr(ldb);
434 id_exists = (ldb_res->count > 0);
436 talloc_free(ldb_res);
439 return samdb_msg_add_int(ldb, ac->msg, ac->msg, "msDS-IntId",
445 * samldb_add_entry (async)
448 static int samldb_add_entry_callback(struct ldb_request *req,
449 struct ldb_reply *ares)
451 struct ldb_context *ldb;
452 struct samldb_ctx *ac;
455 ac = talloc_get_type(req->context, struct samldb_ctx);
456 ldb = ldb_module_get_ctx(ac->module);
459 return ldb_module_done(ac->req, NULL, NULL,
460 LDB_ERR_OPERATIONS_ERROR);
463 if (ares->type == LDB_REPLY_REFERRAL) {
464 return ldb_module_send_referral(ac->req, ares->referral);
467 if (ares->error != LDB_SUCCESS) {
468 return ldb_module_done(ac->req, ares->controls,
469 ares->response, ares->error);
471 if (ares->type != LDB_REPLY_DONE) {
472 ldb_set_errstring(ldb,
473 "Invalid reply type!\n");
474 return ldb_module_done(ac->req, NULL, NULL,
475 LDB_ERR_OPERATIONS_ERROR);
478 /* The caller may wish to get controls back from the add */
479 ac->ares = talloc_steal(ac, ares);
481 ret = samldb_next_step(ac);
482 if (ret != LDB_SUCCESS) {
483 return ldb_module_done(ac->req, NULL, NULL, ret);
488 static int samldb_add_entry(struct samldb_ctx *ac)
490 struct ldb_context *ldb;
491 struct ldb_request *req;
494 ldb = ldb_module_get_ctx(ac->module);
496 ret = ldb_build_add_req(&req, ldb, ac,
499 ac, samldb_add_entry_callback,
501 LDB_REQ_SET_LOCATION(req);
502 if (ret != LDB_SUCCESS) {
506 return ldb_next_request(ac->module, req);
510 * return true if msg carries an attributeSchema that is intended to be RODC
511 * filtered but is also a system-critical attribute.
513 static bool check_rodc_critical_attribute(struct ldb_message *msg)
515 uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
517 schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
518 searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
519 rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE
520 | SEARCH_FLAG_CONFIDENTIAL);
522 if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
523 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
531 static int samldb_fill_object(struct samldb_ctx *ac)
533 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
536 /* Add informations for the different account types */
537 if (strcmp(ac->type, "user") == 0) {
538 struct ldb_control *rodc_control = ldb_request_get_control(ac->req,
539 LDB_CONTROL_RODC_DCPROMO_OID);
540 if (rodc_control != NULL) {
541 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
542 rodc_control->critical = false;
543 ret = samldb_add_step(ac, samldb_rodc_add);
544 if (ret != LDB_SUCCESS) return ret;
547 /* check if we have a valid sAMAccountName */
548 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
549 if (ret != LDB_SUCCESS) return ret;
551 ret = samldb_add_step(ac, samldb_add_entry);
552 if (ret != LDB_SUCCESS) return ret;
554 } else if (strcmp(ac->type, "group") == 0) {
555 /* check if we have a valid sAMAccountName */
556 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
557 if (ret != LDB_SUCCESS) return ret;
559 ret = samldb_add_step(ac, samldb_add_entry);
560 if (ret != LDB_SUCCESS) return ret;
562 } else if (strcmp(ac->type, "classSchema") == 0) {
563 const struct ldb_val *rdn_value, *def_obj_cat_val;
565 ret = samdb_find_or_add_attribute(ldb, ac->msg,
567 if (ret != LDB_SUCCESS) return ret;
569 /* do not allow to mark an attributeSchema as RODC filtered if it
570 * is system-critical */
571 if (check_rodc_critical_attribute(ac->msg)) {
572 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
573 ldb_dn_get_linearized(ac->msg->dn));
574 return LDB_ERR_UNWILLING_TO_PERFORM;
577 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
578 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
579 /* the RDN has prefix "CN" */
580 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
581 samdb_cn_to_lDAPDisplayName(ac->msg,
582 (const char *) rdn_value->data));
583 if (ret != LDB_SUCCESS) {
589 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
592 guid = GUID_random();
593 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
594 if (ret != LDB_SUCCESS) {
600 def_obj_cat_val = ldb_msg_find_ldb_val(ac->msg,
601 "defaultObjectCategory");
602 if (def_obj_cat_val != NULL) {
603 /* "defaultObjectCategory" has been set by the caller.
604 * Do some checks for consistency.
605 * NOTE: The real constraint check (that
606 * 'defaultObjectCategory' is the DN of the new
607 * objectclass or any parent of it) is still incomplete.
608 * For now we say that 'defaultObjectCategory' is valid
609 * if it exists and it is of objectclass "classSchema".
611 ac->dn = ldb_dn_from_ldb_val(ac, ldb, def_obj_cat_val);
612 if (ac->dn == NULL) {
613 ldb_set_errstring(ldb,
614 "Invalid DN for 'defaultObjectCategory'!");
615 return LDB_ERR_CONSTRAINT_VIOLATION;
618 /* "defaultObjectCategory" has not been set by the
619 * caller. Use the entry DN for it. */
620 ac->dn = ac->msg->dn;
622 ret = ldb_msg_add_string(ac->msg, "defaultObjectCategory",
623 ldb_dn_alloc_linearized(ac->msg, ac->dn));
624 if (ret != LDB_SUCCESS) {
630 ret = samldb_add_step(ac, samldb_add_entry);
631 if (ret != LDB_SUCCESS) return ret;
633 /* Now perform the checks for the 'defaultObjectCategory'. The
634 * lookup DN was already saved in "ac->dn" */
635 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
636 if (ret != LDB_SUCCESS) return ret;
638 } else if (strcmp(ac->type, "attributeSchema") == 0) {
639 const struct ldb_val *rdn_value;
640 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
641 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
642 /* the RDN has prefix "CN" */
643 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
644 samdb_cn_to_lDAPDisplayName(ac->msg,
645 (const char *) rdn_value->data));
646 if (ret != LDB_SUCCESS) {
652 /* do not allow to mark an attributeSchema as RODC filtered if it
653 * is system-critical */
654 if (check_rodc_critical_attribute(ac->msg)) {
655 ldb_asprintf_errstring(ldb,
656 "samldb: refusing schema add of %s - cannot combine critical attribute with RODC filtering",
657 ldb_dn_get_linearized(ac->msg->dn));
658 return LDB_ERR_UNWILLING_TO_PERFORM;
661 ret = samdb_find_or_add_attribute(ldb, ac->msg,
662 "isSingleValued", "FALSE");
663 if (ret != LDB_SUCCESS) return ret;
665 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
668 guid = GUID_random();
669 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
670 if (ret != LDB_SUCCESS) {
676 /* handle msDS-IntID attribute */
677 ret = samldb_add_handle_msDS_IntId(ac);
678 if (ret != LDB_SUCCESS) return ret;
680 ret = samldb_add_step(ac, samldb_add_entry);
681 if (ret != LDB_SUCCESS) return ret;
684 ldb_asprintf_errstring(ldb,
685 "Invalid entry type!");
686 return LDB_ERR_OPERATIONS_ERROR;
689 return samldb_first_step(ac);
692 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
694 struct ldb_context *ldb;
698 ldb = ldb_module_get_ctx(ac->module);
700 sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
702 sid = dom_sid_parse_talloc(ac->msg,
703 (const char *)ldb_dn_get_rdn_val(ac->msg->dn)->data);
705 ldb_set_errstring(ldb,
706 "samldb: No valid SID found in ForeignSecurityPrincipal CN!");
707 return LDB_ERR_CONSTRAINT_VIOLATION;
709 if (! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
710 return ldb_operr(ldb);
714 /* finally proceed with adding the entry */
715 ret = samldb_add_step(ac, samldb_add_entry);
716 if (ret != LDB_SUCCESS) return ret;
718 return samldb_first_step(ac);
721 static int samldb_schema_info_update(struct samldb_ctx *ac)
724 struct ldb_context *ldb;
725 struct dsdb_schema *schema;
727 /* replicated update should always go through */
728 if (ldb_request_get_control(ac->req,
729 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
733 /* do not update schemaInfo during provisioning */
734 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
738 ldb = ldb_module_get_ctx(ac->module);
739 schema = dsdb_get_schema(ldb, NULL);
741 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
742 "samldb_schema_info_update: no dsdb_schema loaded");
743 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
744 return ldb_operr(ldb);
747 ret = dsdb_module_schema_info_update(ac->module, schema,
748 DSDB_FLAG_NEXT_MODULE);
749 if (ret != LDB_SUCCESS) {
750 ldb_asprintf_errstring(ldb,
751 "samldb_schema_info_update: dsdb_module_schema_info_update failed with %s",
760 * Gets back a single-valued attribute by the rules of the SAM triggers when
761 * performing a modify operation
763 static int samldb_get_single_valued_attr(struct samldb_ctx *ac,
764 const char *attr_name,
765 struct ldb_message_element **attr)
767 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
768 struct ldb_message_element *el = NULL;
771 /* We've to walk over all modification entries and consider the
774 * 1.) Add operations aren't allowed and there is returned
775 * "ATTRIBUTE_OR_VALUE_EXISTS".
776 * 2.) Replace operations are allowed but the last one is taken
777 * 3.) Delete operations are also not allowed and there is returned
778 * "UNWILLING_TO_PERFORM".
780 * If "el" is afterwards NULL then that means we've nothing to do here.
782 for (i = 0; i < ac->msg->num_elements; i++) {
783 if (ldb_attr_cmp(ac->msg->elements[i].name, attr_name) != 0) {
787 el = &ac->msg->elements[i];
788 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
789 ldb_asprintf_errstring(ldb,
790 "samldb: attribute '%s' already exists!",
792 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
794 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
795 ldb_asprintf_errstring(ldb,
796 "samldb: attribute '%s' cannot be deleted!",
798 return LDB_ERR_UNWILLING_TO_PERFORM;
807 * "Objectclass" trigger (MS-SAMR 3.1.1.8.1)
809 * Has to be invoked on "add" and "modify" operations on "user", "computer" and
811 * ac->msg contains the "add"/"modify" message
812 * ac->type contains the object type (main objectclass)
814 static int samldb_objectclass_trigger(struct samldb_ctx *ac)
816 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
817 struct loadparm_context *lp_ctx = talloc_get_type(ldb_get_opaque(ldb,
818 "loadparm"), struct loadparm_context);
819 struct ldb_message_element *el, *el2;
820 enum sid_generator sid_generator;
825 /* make sure that "sAMAccountType" is not specified */
826 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
828 ldb_set_errstring(ldb,
829 "samldb: sAMAccountType must not be specified!");
830 return LDB_ERR_UNWILLING_TO_PERFORM;
833 /* Step 1: objectSid assignment */
835 /* Don't allow the objectSid to be changed. But beside the RELAX
836 * control we have also to guarantee that it can always be set with
837 * SYSTEM permissions. This is needed for the "samba3sam" backend. */
838 sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
839 if ((sid != NULL) && (!dsdb_module_am_system(ac->module)) &&
840 (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
841 ldb_set_errstring(ldb,
842 "samldb: objectSid must not be specified!");
843 return LDB_ERR_UNWILLING_TO_PERFORM;
846 /* but generate a new SID when we do have an add operations */
847 if ((sid == NULL) && (ac->req->operation == LDB_ADD)) {
848 sid_generator = lpcfg_sid_generator(lp_ctx);
849 if (sid_generator == SID_GENERATOR_INTERNAL) {
850 ret = samldb_add_step(ac, samldb_allocate_sid);
851 if (ret != LDB_SUCCESS) return ret;
855 if (strcmp(ac->type, "user") == 0) {
856 /* Step 1.2: Default values */
857 tempstr = talloc_asprintf(ac->msg, "%d", UF_NORMAL_ACCOUNT);
858 if (tempstr == NULL) return ldb_operr(ldb);
859 ret = samdb_find_or_add_attribute(ldb, ac->msg,
860 "userAccountControl", tempstr);
861 if (ret != LDB_SUCCESS) return ret;
862 ret = samdb_find_or_add_attribute(ldb, ac->msg,
864 if (ret != LDB_SUCCESS) return ret;
865 ret = samdb_find_or_add_attribute(ldb, ac->msg,
867 if (ret != LDB_SUCCESS) return ret;
868 ret = samdb_find_or_add_attribute(ldb, ac->msg,
870 if (ret != LDB_SUCCESS) return ret;
871 ret = samdb_find_or_add_attribute(ldb, ac->msg,
872 "badPasswordTime", "0");
873 if (ret != LDB_SUCCESS) return ret;
874 ret = samdb_find_or_add_attribute(ldb, ac->msg,
876 if (ret != LDB_SUCCESS) return ret;
877 ret = samdb_find_or_add_attribute(ldb, ac->msg,
879 if (ret != LDB_SUCCESS) return ret;
880 ret = samdb_find_or_add_attribute(ldb, ac->msg,
882 if (ret != LDB_SUCCESS) return ret;
883 ret = samdb_find_or_add_attribute(ldb, ac->msg,
884 "accountExpires", "9223372036854775807");
885 if (ret != LDB_SUCCESS) return ret;
886 ret = samdb_find_or_add_attribute(ldb, ac->msg,
888 if (ret != LDB_SUCCESS) return ret;
890 el = ldb_msg_find_element(ac->msg, "userAccountControl");
892 uint32_t user_account_control, account_type;
894 /* Step 1.3: "userAccountControl" -> "sAMAccountType" mapping */
895 user_account_control = ldb_msg_find_attr_as_uint(ac->msg,
896 "userAccountControl",
899 /* Temporary duplicate accounts aren't allowed */
900 if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
901 return LDB_ERR_OTHER;
904 account_type = ds_uf2atype(user_account_control);
905 if (account_type == 0) {
906 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
907 return LDB_ERR_UNWILLING_TO_PERFORM;
909 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
912 if (ret != LDB_SUCCESS) {
915 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
916 el2->flags = LDB_FLAG_MOD_REPLACE;
918 if (user_account_control &
919 (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
920 ret = samdb_msg_set_string(ldb, ac->msg, ac->msg,
921 "isCriticalSystemObject",
923 if (ret != LDB_SUCCESS) {
926 el2 = ldb_msg_find_element(ac->msg,
927 "isCriticalSystemObject");
928 el2->flags = LDB_FLAG_MOD_REPLACE;
931 /* Step 1.4: "userAccountControl" -> "primaryGroupID" mapping */
932 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
933 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
934 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
935 "primaryGroupID", rid);
936 if (ret != LDB_SUCCESS) {
939 el2 = ldb_msg_find_element(ac->msg,
941 el2->flags = LDB_FLAG_MOD_REPLACE;
944 /* Step 1.5: Add additional flags when needed */
945 if ((user_account_control & UF_NORMAL_ACCOUNT) &&
946 (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
947 user_account_control |= UF_ACCOUNTDISABLE;
948 user_account_control |= UF_PASSWD_NOTREQD;
950 ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
951 "userAccountControl",
952 user_account_control);
953 if (ret != LDB_SUCCESS) {
959 } else if (strcmp(ac->type, "group") == 0) {
960 /* Step 2.2: Default values */
961 tempstr = talloc_asprintf(ac->msg, "%d",
962 GTYPE_SECURITY_GLOBAL_GROUP);
963 if (tempstr == NULL) return ldb_operr(ldb);
964 ret = samdb_find_or_add_attribute(ldb, ac->msg,
965 "groupType", tempstr);
966 if (ret != LDB_SUCCESS) return ret;
968 /* Step 2.3: "groupType" -> "sAMAccountType" */
969 el = ldb_msg_find_element(ac->msg, "groupType");
971 uint32_t group_type, account_type;
973 group_type = ldb_msg_find_attr_as_uint(ac->msg,
976 /* The creation of builtin groups requires the
978 if (group_type == GTYPE_SECURITY_BUILTIN_LOCAL_GROUP) {
979 if (ldb_request_get_control(ac->req,
980 LDB_CONTROL_RELAX_OID) == NULL) {
981 return LDB_ERR_UNWILLING_TO_PERFORM;
985 account_type = ds_gtype2atype(group_type);
986 if (account_type == 0) {
987 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
988 return LDB_ERR_UNWILLING_TO_PERFORM;
990 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
993 if (ret != LDB_SUCCESS) {
996 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
997 el2->flags = LDB_FLAG_MOD_REPLACE;
1005 * "Primary group ID" trigger (MS-SAMR 3.1.1.8.2)
1007 * Has to be invoked on "add" and "modify" operations on "user" and "computer"
1009 * ac->msg contains the "add"/"modify" message
1012 static int samldb_prim_group_set(struct samldb_ctx *ac)
1014 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1015 struct ldb_dn *prim_group_dn;
1017 struct dom_sid *sid;
1019 rid = ldb_msg_find_attr_as_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
1020 if (rid == (uint32_t) -1) {
1021 /* we aren't affected of any primary group set */
1024 } else if (!ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
1025 ldb_set_errstring(ldb,
1026 "The primary group isn't settable on add operations!");
1027 return LDB_ERR_UNWILLING_TO_PERFORM;
1030 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1032 return ldb_operr(ldb);
1035 prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
1036 ldap_encode_ndr_dom_sid(ac, sid));
1037 if (prim_group_dn == NULL) {
1038 ldb_asprintf_errstring(ldb,
1039 "Failed to find primary group with RID %u!",
1041 return LDB_ERR_UNWILLING_TO_PERFORM;
1047 static int samldb_prim_group_change(struct samldb_ctx *ac)
1049 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1050 const char * attrs[] = { "primaryGroupID", "memberOf", NULL };
1051 struct ldb_result *res;
1052 struct ldb_message_element *el;
1053 struct ldb_message *msg;
1055 struct dom_sid *sid;
1056 struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
1059 ret = samldb_get_single_valued_attr(ac, "primaryGroupID", &el);
1060 if (ret != LDB_SUCCESS) {
1064 /* we are not affected */
1068 /* Fetch informations from the existing object */
1070 ret = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1072 if (ret != LDB_SUCCESS) {
1075 if (res->count != 1) {
1076 return ldb_operr(ldb);
1079 /* Finds out the DN of the old primary group */
1081 rid = ldb_msg_find_attr_as_uint(res->msgs[0], "primaryGroupID", (uint32_t) -1);
1082 if (rid == (uint32_t) -1) {
1083 /* User objects do always have a mandatory "primaryGroupID"
1084 * attribute. If this doesn't exist then the object is of the
1085 * wrong type. This is the exact Windows error code */
1086 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1089 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1091 return ldb_operr(ldb);
1094 prev_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
1095 ldap_encode_ndr_dom_sid(ac, sid));
1096 if (prev_prim_group_dn == NULL) {
1097 return ldb_operr(ldb);
1100 /* Finds out the DN of the new primary group
1101 * Notice: in order to parse the primary group ID correctly we create
1102 * a temporary message here. */
1104 msg = ldb_msg_new(ac->msg);
1106 return ldb_module_oom(ac->module);
1108 ret = ldb_msg_add(msg, el, 0);
1109 if (ret != LDB_SUCCESS) {
1112 rid = ldb_msg_find_attr_as_uint(msg, "primaryGroupID", (uint32_t) -1);
1114 if (rid == (uint32_t) -1) {
1115 /* we aren't affected of any primary group change */
1119 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1121 return ldb_operr(ldb);
1124 new_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
1125 ldap_encode_ndr_dom_sid(ac, sid));
1126 if (new_prim_group_dn == NULL) {
1127 /* Here we know if the specified new primary group candidate is
1129 return LDB_ERR_UNWILLING_TO_PERFORM;
1132 /* Only update the "member" attributes when we really do have a change */
1133 if (ldb_dn_compare(new_prim_group_dn, prev_prim_group_dn) != 0) {
1134 /* We need to be already a normal member of the new primary
1135 * group in order to be successful. */
1136 el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
1137 ldb_dn_get_linearized(new_prim_group_dn));
1139 return LDB_ERR_UNWILLING_TO_PERFORM;
1142 /* Remove the "member" attribute on the new primary group */
1143 msg = ldb_msg_new(ac->msg);
1145 return ldb_module_oom(ac->module);
1147 msg->dn = new_prim_group_dn;
1149 ret = samdb_msg_add_delval(ldb, msg, msg, "member",
1150 ldb_dn_get_linearized(ac->msg->dn));
1151 if (ret != LDB_SUCCESS) {
1155 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1156 if (ret != LDB_SUCCESS) {
1161 /* Add a "member" attribute for the previous primary group */
1162 msg = ldb_msg_new(ac->msg);
1164 return ldb_module_oom(ac->module);
1166 msg->dn = prev_prim_group_dn;
1168 ret = samdb_msg_add_addval(ldb, msg, msg, "member",
1169 ldb_dn_get_linearized(ac->msg->dn));
1170 if (ret != LDB_SUCCESS) {
1174 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1175 if (ret != LDB_SUCCESS) {
1186 static int samldb_prim_group_trigger(struct samldb_ctx *ac)
1190 if (ac->req->operation == LDB_ADD) {
1191 ret = samldb_prim_group_set(ac);
1193 ret = samldb_prim_group_change(ac);
1199 static int samldb_user_account_control_change(struct samldb_ctx *ac)
1201 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1202 uint32_t user_account_control, account_type;
1203 struct ldb_message_element *el;
1204 struct ldb_message *tmp_msg;
1207 ret = samldb_get_single_valued_attr(ac, "userAccountControl", &el);
1208 if (ret != LDB_SUCCESS) {
1212 /* we are not affected */
1216 /* Create a temporary message for fetching the "userAccountControl" */
1217 tmp_msg = ldb_msg_new(ac->msg);
1218 if (tmp_msg == NULL) {
1219 return ldb_module_oom(ac->module);
1221 ret = ldb_msg_add(tmp_msg, el, 0);
1222 if (ret != LDB_SUCCESS) {
1225 user_account_control = ldb_msg_find_attr_as_uint(tmp_msg,
1226 "userAccountControl",
1228 talloc_free(tmp_msg);
1230 /* Temporary duplicate accounts aren't allowed */
1231 if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
1232 return LDB_ERR_OTHER;
1235 account_type = ds_uf2atype(user_account_control);
1236 if (account_type == 0) {
1237 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1238 return LDB_ERR_UNWILLING_TO_PERFORM;
1240 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
1242 if (ret != LDB_SUCCESS) {
1245 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
1246 el->flags = LDB_FLAG_MOD_REPLACE;
1248 if (user_account_control
1249 & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
1250 ret = samdb_msg_add_string(ldb, ac->msg, ac->msg,
1251 "isCriticalSystemObject", "TRUE");
1252 if (ret != LDB_SUCCESS) {
1255 el = ldb_msg_find_element(ac->msg,
1256 "isCriticalSystemObject");
1257 el->flags = LDB_FLAG_MOD_REPLACE;
1260 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
1261 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
1262 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1263 "primaryGroupID", rid);
1264 if (ret != LDB_SUCCESS) {
1267 el = ldb_msg_find_element(ac->msg,
1269 el->flags = LDB_FLAG_MOD_REPLACE;
1275 static int samldb_group_type_change(struct samldb_ctx *ac)
1277 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1278 uint32_t group_type, old_group_type, account_type;
1279 struct ldb_message_element *el;
1280 struct ldb_message *tmp_msg;
1283 ret = samldb_get_single_valued_attr(ac, "groupType", &el);
1284 if (ret != LDB_SUCCESS) {
1288 /* we are not affected */
1292 /* Create a temporary message for fetching the "groupType" */
1293 tmp_msg = ldb_msg_new(ac->msg);
1294 if (tmp_msg == NULL) {
1295 return ldb_module_oom(ac->module);
1297 ret = ldb_msg_add(tmp_msg, el, 0);
1298 if (ret != LDB_SUCCESS) {
1301 group_type = ldb_msg_find_attr_as_uint(tmp_msg, "groupType", 0);
1302 talloc_free(tmp_msg);
1304 old_group_type = samdb_search_uint(ldb, ac, 0, ac->msg->dn,
1306 if (old_group_type == 0) {
1307 return ldb_operr(ldb);
1310 /* Group type switching isn't so easy as it seems: We can only
1311 * change in this directions: global <-> universal <-> local
1312 * On each step also the group type itself
1313 * (security/distribution) is variable. */
1315 switch (group_type) {
1316 case GTYPE_SECURITY_GLOBAL_GROUP:
1317 case GTYPE_DISTRIBUTION_GLOBAL_GROUP:
1318 /* change to "universal" allowed */
1319 if ((old_group_type == GTYPE_SECURITY_DOMAIN_LOCAL_GROUP) ||
1320 (old_group_type == GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP)) {
1321 return LDB_ERR_UNWILLING_TO_PERFORM;
1325 case GTYPE_SECURITY_UNIVERSAL_GROUP:
1326 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP:
1327 /* each change allowed */
1330 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP:
1331 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP:
1332 /* change to "universal" allowed */
1333 if ((old_group_type == GTYPE_SECURITY_GLOBAL_GROUP) ||
1334 (old_group_type == GTYPE_DISTRIBUTION_GLOBAL_GROUP)) {
1335 return LDB_ERR_UNWILLING_TO_PERFORM;
1339 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP:
1341 /* we don't allow this "groupType" values */
1342 return LDB_ERR_UNWILLING_TO_PERFORM;
1346 account_type = ds_gtype2atype(group_type);
1347 if (account_type == 0) {
1348 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1349 return LDB_ERR_UNWILLING_TO_PERFORM;
1351 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
1353 if (ret != LDB_SUCCESS) {
1356 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
1357 el->flags = LDB_FLAG_MOD_REPLACE;
1362 static int samldb_sam_accountname_check(struct samldb_ctx *ac)
1364 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1365 const char *no_attrs[] = { NULL };
1366 struct ldb_result *res;
1367 const char *sam_accountname, *enc_str;
1368 struct ldb_message_element *el;
1369 struct ldb_message *tmp_msg;
1372 ret = samldb_get_single_valued_attr(ac, "sAMAccountName", &el);
1373 if (ret != LDB_SUCCESS) {
1377 /* we are not affected */
1381 /* Create a temporary message for fetching the "sAMAccountName" */
1382 tmp_msg = ldb_msg_new(ac->msg);
1383 if (tmp_msg == NULL) {
1384 return ldb_module_oom(ac->module);
1386 ret = ldb_msg_add(tmp_msg, el, 0);
1387 if (ret != LDB_SUCCESS) {
1390 sam_accountname = talloc_steal(ac,
1391 ldb_msg_find_attr_as_string(tmp_msg, "sAMAccountName", NULL));
1392 talloc_free(tmp_msg);
1394 if (sam_accountname == NULL) {
1395 /* The "sAMAccountName" cannot be nothing */
1396 ldb_set_errstring(ldb,
1397 "samldb: Empty account names aren't allowed!");
1398 return LDB_ERR_UNWILLING_TO_PERFORM;
1401 enc_str = ldb_binary_encode_string(ac, sam_accountname);
1402 if (enc_str == NULL) {
1403 return ldb_module_oom(ac->module);
1406 /* Make sure that a "sAMAccountName" is only used once */
1408 ret = ldb_search(ldb, ac, &res, NULL, LDB_SCOPE_SUBTREE, no_attrs,
1409 "(sAMAccountName=%s)", enc_str);
1410 if (ret != LDB_SUCCESS) {
1413 if (res->count > 1) {
1414 return ldb_operr(ldb);
1415 } else if (res->count == 1) {
1416 if (ldb_dn_compare(res->msgs[0]->dn, ac->msg->dn) != 0) {
1417 ldb_asprintf_errstring(ldb,
1418 "samldb: Account name (sAMAccountName) '%s' already in use!",
1420 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1428 static int samldb_member_check(struct samldb_ctx *ac)
1430 static const char * const attrs[] = { "objectSid", "member", NULL };
1431 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1432 struct ldb_message_element *el;
1433 struct ldb_dn *member_dn;
1434 uint32_t prim_group_rid;
1435 struct dom_sid *sid;
1436 struct ldb_result *res;
1437 struct dom_sid *group_sid;
1442 /* Fetch informations from the existing object */
1444 ret = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1446 if (ret != LDB_SUCCESS) {
1449 if (res->count != 1) {
1450 return ldb_operr(ldb);
1453 group_sid = samdb_result_dom_sid(res, res->msgs[0], "objectSid");
1454 if (group_sid == NULL) {
1455 return ldb_operr(ldb);
1458 /* We've to walk over all modification entries and consider the "member"
1460 for (i = 0; i < ac->msg->num_elements; i++) {
1461 if (ldb_attr_cmp(ac->msg->elements[i].name, "member") != 0) {
1465 el = &ac->msg->elements[i];
1466 for (j = 0; j < el->num_values; j++) {
1467 struct ldb_message_element *mo;
1469 member_dn = ldb_dn_from_ldb_val(ac, ldb,
1471 if (!ldb_dn_validate(member_dn)) {
1472 return ldb_operr(ldb);
1475 /* The "member" attribute can be modified with the
1476 * following restrictions (beside a valid DN):
1478 * - "add" operations can only be performed when the
1479 * member still doesn't exist - if not then return
1480 * ERR_ENTRY_ALREADY_EXISTS (not
1481 * ERR_ATTRIBUTE_OR_VALUE_EXISTS!)
1482 * - "delete" operations can only be performed when the
1483 * member does exist - if not then return
1484 * ERR_UNWILLING_TO_PERFORM (not
1485 * ERR_NO_SUCH_ATTRIBUTE!)
1486 * - primary group check
1488 mo = samdb_find_attribute(ldb, res->msgs[0], "member",
1489 ldb_dn_get_linearized(member_dn));
1496 if ((cnt > 0) && (LDB_FLAG_MOD_TYPE(el->flags)
1497 == LDB_FLAG_MOD_ADD)) {
1498 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1500 if ((cnt == 0) && LDB_FLAG_MOD_TYPE(el->flags)
1501 == LDB_FLAG_MOD_DELETE) {
1502 return LDB_ERR_UNWILLING_TO_PERFORM;
1505 /* Denies to add "member"s to groups which are primary
1506 * ones for them - in this case return
1507 * ERR_ENTRY_ALREADY_EXISTS. */
1509 prim_group_rid = samdb_search_uint(ldb, ac,
1514 if (prim_group_rid == (uint32_t) -1) {
1515 /* the member hasn't to be a user account ->
1516 * therefore no check needed in this case. */
1520 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1523 return ldb_operr(ldb);
1526 if (dom_sid_equal(group_sid, sid)) {
1527 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1535 /* This trigger adapts the "servicePrincipalName" attributes if the
1536 * "dNSHostName" and/or "sAMAccountName" attribute change(s) */
1537 static int samldb_service_principal_names_change(struct samldb_ctx *ac)
1539 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1540 struct ldb_message_element *el = NULL, *el2 = NULL;
1541 struct ldb_message *msg;
1542 const char *attrs[] = { "servicePrincipalName", NULL };
1543 struct ldb_result *res;
1544 const char *dns_hostname = NULL, *old_dns_hostname = NULL,
1545 *sam_accountname = NULL, *old_sam_accountname = NULL;
1549 /* Here it's not the same logic as with "samldb_get_single_valued_attr".
1552 * - consider "add" and "replace" operations - the last value we take
1553 * - ignore "delete" operations - obviously this attribute isn't
1556 for (i = 0; i < ac->msg->num_elements; i++) {
1557 if ((ldb_attr_cmp(ac->msg->elements[i].name,
1558 "dNSHostName") == 0) &&
1559 (LDB_FLAG_MOD_TYPE(ac->msg->elements[i].flags)
1560 != LDB_FLAG_MOD_DELETE)) {
1561 el = &ac->msg->elements[i];
1563 if ((ldb_attr_cmp(ac->msg->elements[i].name,
1564 "sAMAccountName") == 0) &&
1565 (LDB_FLAG_MOD_TYPE(ac->msg->elements[i].flags)
1566 != LDB_FLAG_MOD_DELETE)) {
1567 el2 = &ac->msg->elements[i];
1570 if ((el == NULL) && (el2 == NULL)) {
1571 /* we are not affected */
1575 /* Create a temporary message for fetching the "dNSHostName" */
1577 msg = ldb_msg_new(ac->msg);
1579 return ldb_module_oom(ac->module);
1581 ret = ldb_msg_add(msg, el, 0);
1582 if (ret != LDB_SUCCESS) {
1585 dns_hostname = talloc_steal(ac,
1586 ldb_msg_find_attr_as_string(msg, "dNSHostName", NULL));
1589 old_dns_hostname = samdb_search_string(ldb, ac, ac->msg->dn,
1590 "dNSHostName", NULL);
1593 /* Create a temporary message for fetching the "sAMAccountName" */
1595 char *tempstr, *tempstr2;
1597 msg = ldb_msg_new(ac->msg);
1599 return ldb_module_oom(ac->module);
1601 ret = ldb_msg_add(msg, el2, 0);
1602 if (ret != LDB_SUCCESS) {
1605 tempstr = talloc_strdup(ac,
1606 ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL));
1609 tempstr2 = talloc_strdup(ac,
1610 samdb_search_string(ldb, ac, ac->msg->dn, "sAMAccountName", NULL));
1612 /* The "sAMAccountName" needs some additional trimming: we need
1613 * to remove the trailing "$"s if they exist. */
1614 if ((tempstr != NULL) && (tempstr[0] != '\0') &&
1615 (tempstr[strlen(tempstr) - 1] == '$')) {
1616 tempstr[strlen(tempstr) - 1] = '\0';
1618 if ((tempstr2 != NULL) && (tempstr2[0] != '\0') &&
1619 (tempstr2[strlen(tempstr2) - 1] == '$')) {
1620 tempstr2[strlen(tempstr2) - 1] = '\0';
1622 sam_accountname = tempstr;
1623 old_sam_accountname = tempstr2;
1626 if (old_dns_hostname == NULL) {
1627 /* we cannot change when the old name is unknown */
1628 dns_hostname = NULL;
1630 if ((old_dns_hostname != NULL) && (dns_hostname != NULL) &&
1631 (strcasecmp(old_dns_hostname, dns_hostname) == 0)) {
1632 /* The "dNSHostName" didn't change */
1633 dns_hostname = NULL;
1636 if (old_sam_accountname == NULL) {
1637 /* we cannot change when the old name is unknown */
1638 sam_accountname = NULL;
1640 if ((old_sam_accountname != NULL) && (sam_accountname != NULL) &&
1641 (strcasecmp(old_sam_accountname, sam_accountname) == 0)) {
1642 /* The "sAMAccountName" didn't change */
1643 sam_accountname = NULL;
1646 if ((dns_hostname == NULL) && (sam_accountname == NULL)) {
1647 /* Well, there are informations missing (old name(s)) or the
1648 * names didn't change. We've nothing to do and can exit here */
1652 /* Potential "servicePrincipalName" changes in the same request have to
1653 * be handled before the update (Windows behaviour). */
1654 el = ldb_msg_find_element(ac->msg, "servicePrincipalName");
1656 msg = ldb_msg_new(ac->msg);
1658 return ldb_module_oom(ac->module);
1660 msg->dn = ac->msg->dn;
1663 ret = ldb_msg_add(msg, el, el->flags);
1664 if (ret != LDB_SUCCESS) {
1668 ldb_msg_remove_element(ac->msg, el);
1670 el = ldb_msg_find_element(ac->msg,
1671 "servicePrincipalName");
1672 } while (el != NULL);
1674 ret = dsdb_module_modify(ac->module, msg,
1675 DSDB_FLAG_NEXT_MODULE);
1676 if (ret != LDB_SUCCESS) {
1682 /* Fetch the "servicePrincipalName"s if any */
1683 ret = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1685 if (ret != LDB_SUCCESS) {
1688 if ((res->count != 1) || (res->msgs[0]->num_elements > 1)) {
1689 return ldb_operr(ldb);
1692 if (res->msgs[0]->num_elements == 1) {
1693 /* Yes, we do have "servicePrincipalName"s. First we update them
1694 * locally, that means we do always substitute the current
1695 * "dNSHostName" with the new one and/or "sAMAccountName"
1696 * without "$" with the new one and then we append this to the
1697 * modification request (Windows behaviour). */
1699 for (i = 0; i < res->msgs[0]->elements[0].num_values; i++) {
1700 char *old_str, *new_str, *pos;
1704 res->msgs[0]->elements[0].values[i].data;
1706 new_str = talloc_strdup(ac->msg,
1707 strtok_r(old_str, "/", &pos));
1708 if (new_str == NULL) {
1709 return ldb_module_oom(ac->module);
1712 while ((tok = strtok_r(NULL, "/", &pos)) != NULL) {
1713 if ((dns_hostname != NULL) &&
1714 (strcasecmp(tok, old_dns_hostname) == 0)) {
1717 if ((sam_accountname != NULL) &&
1718 (strcasecmp(tok, old_sam_accountname) == 0)) {
1719 tok = sam_accountname;
1722 new_str = talloc_asprintf(ac->msg, "%s/%s",
1724 if (new_str == NULL) {
1725 return ldb_module_oom(ac->module);
1729 ret = ldb_msg_add_string(ac->msg,
1730 "servicePrincipalName",
1732 if (ret != LDB_SUCCESS) {
1737 el = ldb_msg_find_element(ac->msg, "servicePrincipalName");
1738 el->flags = LDB_FLAG_MOD_REPLACE;
1748 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
1750 struct ldb_context *ldb;
1751 struct samldb_ctx *ac;
1754 ldb = ldb_module_get_ctx(module);
1755 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
1757 /* do not manipulate our control entries */
1758 if (ldb_dn_is_special(req->op.add.message->dn)) {
1759 return ldb_next_request(module, req);
1762 ac = samldb_ctx_init(module, req);
1764 return ldb_operr(ldb);
1767 /* build the new msg */
1768 ac->msg = ldb_msg_copy_shallow(ac, req->op.add.message);
1769 if (ac->msg == NULL) {
1771 ldb_debug(ldb, LDB_DEBUG_FATAL,
1772 "samldb_add: ldb_msg_copy_shallow failed!\n");
1773 return ldb_operr(ldb);
1776 if (samdb_find_attribute(ldb, ac->msg,
1777 "objectclass", "user") != NULL) {
1780 ret = samldb_prim_group_trigger(ac);
1781 if (ret != LDB_SUCCESS) {
1785 ret = samldb_objectclass_trigger(ac);
1786 if (ret != LDB_SUCCESS) {
1790 return samldb_fill_object(ac);
1793 if (samdb_find_attribute(ldb, ac->msg,
1794 "objectclass", "group") != NULL) {
1797 ret = samldb_objectclass_trigger(ac);
1798 if (ret != LDB_SUCCESS) {
1802 return samldb_fill_object(ac);
1805 /* perhaps a foreignSecurityPrincipal? */
1806 if (samdb_find_attribute(ldb, ac->msg,
1808 "foreignSecurityPrincipal") != NULL) {
1809 return samldb_fill_foreignSecurityPrincipal_object(ac);
1812 if (samdb_find_attribute(ldb, ac->msg,
1813 "objectclass", "classSchema") != NULL) {
1814 ret = samldb_schema_info_update(ac);
1815 if (ret != LDB_SUCCESS) {
1820 ac->type = "classSchema";
1821 return samldb_fill_object(ac);
1824 if (samdb_find_attribute(ldb, ac->msg,
1825 "objectclass", "attributeSchema") != NULL) {
1826 ret = samldb_schema_info_update(ac);
1827 if (ret != LDB_SUCCESS) {
1832 ac->type = "attributeSchema";
1833 return samldb_fill_object(ac);
1838 /* nothing matched, go on */
1839 return ldb_next_request(module, req);
1843 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
1845 struct ldb_context *ldb;
1846 struct samldb_ctx *ac;
1847 struct ldb_message_element *el, *el2;
1848 bool modified = false;
1851 if (ldb_dn_is_special(req->op.mod.message->dn)) {
1852 /* do not manipulate our control entries */
1853 return ldb_next_request(module, req);
1856 ldb = ldb_module_get_ctx(module);
1858 /* make sure that "objectSid" is not specified */
1859 el = ldb_msg_find_element(req->op.mod.message, "objectSid");
1861 ldb_set_errstring(ldb,
1862 "samldb: objectSid must not be specified!");
1863 return LDB_ERR_UNWILLING_TO_PERFORM;
1865 /* make sure that "sAMAccountType" is not specified */
1866 el = ldb_msg_find_element(req->op.mod.message, "sAMAccountType");
1868 ldb_set_errstring(ldb,
1869 "samldb: sAMAccountType must not be specified!");
1870 return LDB_ERR_UNWILLING_TO_PERFORM;
1872 /* make sure that "isCriticalSystemObject" is not specified */
1873 el = ldb_msg_find_element(req->op.mod.message, "isCriticalSystemObject");
1875 if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID) == NULL) {
1876 ldb_set_errstring(ldb,
1877 "samldb: isCriticalSystemObject must not be specified!");
1878 return LDB_ERR_UNWILLING_TO_PERFORM;
1882 /* msDS-IntId is not allowed to be modified
1883 * except when modification comes from replication */
1884 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
1885 if (!ldb_request_get_control(req,
1886 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1887 return LDB_ERR_CONSTRAINT_VIOLATION;
1891 ac = samldb_ctx_init(module, req);
1893 return ldb_operr(ldb);
1896 /* build the new msg */
1897 ac->msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
1898 if (ac->msg == NULL) {
1900 ldb_debug(ldb, LDB_DEBUG_FATAL,
1901 "samldb_modify: ldb_msg_copy_shallow failed!\n");
1902 return ldb_operr(ldb);
1905 el = ldb_msg_find_element(ac->msg, "primaryGroupID");
1907 ret = samldb_prim_group_change(ac);
1908 if (ret != LDB_SUCCESS) {
1913 el = ldb_msg_find_element(ac->msg, "userAccountControl");
1916 ret = samldb_user_account_control_change(ac);
1917 if (ret != LDB_SUCCESS) {
1922 el = ldb_msg_find_element(ac->msg, "groupType");
1925 ret = samldb_group_type_change(ac);
1926 if (ret != LDB_SUCCESS) {
1931 el = ldb_msg_find_element(ac->msg, "sAMAccountName");
1933 ret = samldb_sam_accountname_check(ac);
1934 if (ret != LDB_SUCCESS) {
1939 el = ldb_msg_find_element(ac->msg, "member");
1941 ret = samldb_member_check(ac);
1942 if (ret != LDB_SUCCESS) {
1947 el = ldb_msg_find_element(ac->msg, "dNSHostName");
1948 el2 = ldb_msg_find_element(ac->msg, "sAMAccountName");
1949 if ((el != NULL) || (el2 != NULL)) {
1951 ret = samldb_service_principal_names_change(ac);
1952 if (ret != LDB_SUCCESS) {
1958 struct ldb_request *child_req;
1960 /* Now perform the real modifications as a child request */
1961 ret = ldb_build_mod_req(&child_req, ldb, ac,
1964 req, dsdb_next_callback,
1966 LDB_REQ_SET_LOCATION(child_req);
1967 if (ret != LDB_SUCCESS) {
1971 return ldb_next_request(module, child_req);
1976 /* no change which interests us, go on */
1977 return ldb_next_request(module, req);
1982 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
1984 struct ldb_context *ldb;
1985 struct dom_sid *sid;
1990 ldb = ldb_module_get_ctx(ac->module);
1992 /* Finds out the SID/RID of the SAM object */
1993 sid = samdb_search_dom_sid(ldb, ac, ac->req->op.del.dn, "objectSid",
1996 /* No SID - it might not be a SAM object - therefore ok */
1999 status = dom_sid_split_rid(ac, sid, NULL, &rid);
2000 if (!NT_STATUS_IS_OK(status)) {
2001 return ldb_operr(ldb);
2004 /* Special object (security principal?) */
2008 /* Deny delete requests from groups which are primary ones */
2009 count = samdb_search_count(ldb, ac, NULL,
2010 "(&(primaryGroupID=%u)(objectClass=user))",
2013 return ldb_operr(ldb);
2016 return LDB_ERR_ENTRY_ALREADY_EXISTS;
2022 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
2024 struct samldb_ctx *ac;
2027 if (ldb_dn_is_special(req->op.del.dn)) {
2028 /* do not manipulate our control entries */
2029 return ldb_next_request(module, req);
2032 ac = samldb_ctx_init(module, req);
2034 return ldb_operr(ldb_module_get_ctx(module));
2037 ret = samldb_prim_group_users_check(ac);
2038 if (ret != LDB_SUCCESS) {
2044 return ldb_next_request(module, req);
2049 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
2051 struct ldb_context *ldb = ldb_module_get_ctx(module);
2052 struct dsdb_fsmo_extended_op *exop;
2055 exop = talloc_get_type(req->op.extended.data,
2056 struct dsdb_fsmo_extended_op);
2058 ldb_set_errstring(ldb,
2059 "samldb_extended_allocate_rid_pool: invalid extended data");
2060 return LDB_ERR_PROTOCOL_ERROR;
2063 ret = ridalloc_allocate_rid_pool_fsmo(module, exop);
2064 if (ret != LDB_SUCCESS) {
2068 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
2071 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
2073 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
2074 return samldb_extended_allocate_rid_pool(module, req);
2077 return ldb_next_request(module, req);
2081 static const struct ldb_module_ops ldb_samldb_module_ops = {
2084 .modify = samldb_modify,
2085 .del = samldb_delete,
2086 .extended = samldb_extended
2090 int ldb_samldb_module_init(const char *version)
2092 LDB_MODULE_CHECK_VERSION(version);
2093 return ldb_register_module(&ldb_samldb_module_ops);