4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
5 Copyright (C) Simo Sorce 2004-2008
6 Copyright (C) Matthias Dieter Wallnöfer 2009-2011
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"
42 #include "libds/common/flag_mapping.h"
45 enum samldb_add_type {
52 typedef int (*samldb_step_fn_t)(struct samldb_ctx *);
55 struct samldb_step *next;
60 struct ldb_module *module;
61 struct ldb_request *req;
63 /* used for add operations */
64 enum samldb_add_type type;
66 /* the resulting message */
67 struct ldb_message *msg;
69 /* used in "samldb_find_for_defaultObjectCategory" */
70 struct ldb_dn *dn, *res_dn;
72 /* all the async steps necessary to complete the operation */
73 struct samldb_step *steps;
74 struct samldb_step *curstep;
76 /* If someone set an ares to forward controls and response back to the caller */
77 struct ldb_reply *ares;
80 static struct samldb_ctx *samldb_ctx_init(struct ldb_module *module,
81 struct ldb_request *req)
83 struct ldb_context *ldb;
84 struct samldb_ctx *ac;
86 ldb = ldb_module_get_ctx(module);
88 ac = talloc_zero(req, struct samldb_ctx);
100 static int samldb_add_step(struct samldb_ctx *ac, samldb_step_fn_t fn)
102 struct samldb_step *step, *stepper;
104 step = talloc_zero(ac, struct samldb_step);
106 return ldb_oom(ldb_module_get_ctx(ac->module));
111 if (ac->steps == NULL) {
115 if (ac->curstep == NULL)
116 return ldb_operr(ldb_module_get_ctx(ac->module));
117 for (stepper = ac->curstep; stepper->next != NULL;
118 stepper = stepper->next);
119 stepper->next = step;
125 static int samldb_first_step(struct samldb_ctx *ac)
127 if (ac->steps == NULL) {
128 return ldb_operr(ldb_module_get_ctx(ac->module));
131 ac->curstep = ac->steps;
132 return ac->curstep->fn(ac);
135 static int samldb_next_step(struct samldb_ctx *ac)
137 if (ac->curstep->next) {
138 ac->curstep = ac->curstep->next;
139 return ac->curstep->fn(ac);
142 /* We exit the samldb module here. If someone set an "ares" to forward
143 * controls and response back to the caller, use them. */
145 return ldb_module_done(ac->req, ac->ares->controls,
146 ac->ares->response, LDB_SUCCESS);
148 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
153 /* sAMAccountName handling */
155 static int samldb_generate_sAMAccountName(struct ldb_context *ldb,
156 struct ldb_message *msg)
160 /* Format: $000000-000000000000 */
162 name = talloc_asprintf(msg, "$%.6X-%.6X%.6X",
163 (unsigned int)generate_random(),
164 (unsigned int)generate_random(),
165 (unsigned int)generate_random());
169 return ldb_msg_add_steal_string(msg, "sAMAccountName", name);
172 static int samldb_check_sAMAccountName(struct samldb_ctx *ac)
174 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
177 struct ldb_result *res;
178 const char *noattrs[] = { NULL };
180 if (ldb_msg_find_element(ac->msg, "sAMAccountName") == NULL) {
181 ret = samldb_generate_sAMAccountName(ldb, ac->msg);
182 if (ret != LDB_SUCCESS) {
187 name = ldb_msg_find_attr_as_string(ac->msg, "sAMAccountName", NULL);
189 /* The "sAMAccountName" cannot be nothing */
190 ldb_set_errstring(ldb,
191 "samldb: Empty account names aren't allowed!");
192 return LDB_ERR_CONSTRAINT_VIOLATION;
195 ret = dsdb_module_search(ac->module, ac, &res,
196 ldb_get_default_basedn(ldb), LDB_SCOPE_SUBTREE, noattrs,
197 DSDB_FLAG_NEXT_MODULE,
199 "(sAMAccountName=%s)",
200 ldb_binary_encode_string(ac, name));
201 if (ret != LDB_SUCCESS) {
204 if (res->count != 0) {
205 ldb_asprintf_errstring(ldb,
206 "samldb: Account name (sAMAccountName) '%s' already in use!",
209 return LDB_ERR_ENTRY_ALREADY_EXISTS;
213 return samldb_next_step(ac);
217 static bool samldb_msg_add_sid(struct ldb_message *msg,
219 const struct dom_sid *sid)
222 enum ndr_err_code ndr_err;
224 ndr_err = ndr_push_struct_blob(&v, msg, sid,
225 (ndr_push_flags_fn_t)ndr_push_dom_sid);
226 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
229 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
233 /* allocate a SID using our RID Set */
234 static int samldb_allocate_sid(struct samldb_ctx *ac)
238 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
241 ret = ridalloc_allocate_rid(ac->module, &rid, ac->req);
242 if (ret != LDB_SUCCESS) {
246 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
248 return ldb_module_oom(ac->module);
251 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
252 return ldb_operr(ldb);
255 return samldb_next_step(ac);
259 see if a krbtgt_number is available
261 static bool samldb_krbtgtnumber_available(struct samldb_ctx *ac,
262 uint32_t krbtgt_number)
264 TALLOC_CTX *tmp_ctx = talloc_new(ac);
265 struct ldb_result *res;
266 const char *no_attrs[] = { NULL };
269 ret = dsdb_module_search(ac->module, tmp_ctx, &res,
270 ldb_get_default_basedn(ldb_module_get_ctx(ac->module)),
271 LDB_SCOPE_SUBTREE, no_attrs,
272 DSDB_FLAG_NEXT_MODULE,
274 "(msDC-SecondaryKrbTgtNumber=%u)",
276 if (ret == LDB_SUCCESS && res->count == 0) {
277 talloc_free(tmp_ctx);
280 talloc_free(tmp_ctx);
284 /* special handling for add in RODC join */
285 static int samldb_rodc_add(struct samldb_ctx *ac)
287 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
288 uint32_t krbtgt_number, i_start, i;
291 struct ldb_val newpass_utf16;
293 /* find a unused msDC-SecondaryKrbTgtNumber */
294 i_start = generate_random() & 0xFFFF;
299 for (i=i_start; i<=0xFFFF; i++) {
300 if (samldb_krbtgtnumber_available(ac, i)) {
305 for (i=1; i<i_start; i++) {
306 if (samldb_krbtgtnumber_available(ac, i)) {
312 ldb_asprintf_errstring(ldb,
313 "%08X: Unable to find available msDS-SecondaryKrbTgtNumber",
314 W_ERROR_V(WERR_NO_SYSTEM_RESOURCES));
315 return LDB_ERR_OTHER;
318 ret = ldb_msg_add_empty(ac->msg, "msDS-SecondaryKrbTgtNumber",
319 LDB_FLAG_INTERNAL_DISABLE_VALIDATION, NULL);
320 if (ret != LDB_SUCCESS) {
321 return ldb_operr(ldb);
324 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
325 "msDS-SecondaryKrbTgtNumber", krbtgt_number);
326 if (ret != LDB_SUCCESS) {
327 return ldb_operr(ldb);
330 ret = ldb_msg_add_fmt(ac->msg, "sAMAccountName", "krbtgt_%u",
332 if (ret != LDB_SUCCESS) {
333 return ldb_operr(ldb);
336 newpass = generate_random_password(ac->msg, 128, 255);
337 if (newpass == NULL) {
338 return ldb_operr(ldb);
341 if (!convert_string_talloc(ac,
343 newpass, strlen(newpass),
344 (void *)&newpass_utf16.data,
345 &newpass_utf16.length)) {
346 ldb_asprintf_errstring(ldb,
348 "failed to generate UTF16 password from random password");
349 return LDB_ERR_OPERATIONS_ERROR;
351 ret = ldb_msg_add_steal_value(ac->msg, "clearTextPassword", &newpass_utf16);
352 if (ret != LDB_SUCCESS) {
353 return ldb_operr(ldb);
356 return samldb_next_step(ac);
359 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
361 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
362 struct ldb_result *res;
363 const char *no_attrs[] = { NULL };
368 ret = dsdb_module_search(ac->module, ac, &res,
369 ac->dn, LDB_SCOPE_BASE, no_attrs,
370 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
371 | DSDB_FLAG_NEXT_MODULE,
373 "(objectClass=classSchema)");
374 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
375 /* Don't be pricky when the DN doesn't exist if we have the */
376 /* RELAX control specified */
377 if (ldb_request_get_control(ac->req,
378 LDB_CONTROL_RELAX_OID) == NULL) {
379 ldb_set_errstring(ldb,
380 "samldb_find_defaultObjectCategory: "
381 "Invalid DN for 'defaultObjectCategory'!");
382 return LDB_ERR_CONSTRAINT_VIOLATION;
385 if ((ret != LDB_ERR_NO_SUCH_OBJECT) && (ret != LDB_SUCCESS)) {
389 if (ret == LDB_SUCCESS) {
390 /* ensure the defaultObjectCategory has a full GUID */
391 struct ldb_message *m;
392 m = ldb_msg_new(ac->msg);
397 if (ldb_msg_add_string(m, "defaultObjectCategory",
398 ldb_dn_get_extended_linearized(m, res->msgs[0]->dn, 1)) !=
402 m->elements[0].flags = LDB_FLAG_MOD_REPLACE;
404 ret = dsdb_module_modify(ac->module, m,
405 DSDB_FLAG_NEXT_MODULE,
407 if (ret != LDB_SUCCESS) {
415 return samldb_next_step(ac);
419 * msDS-IntId attributeSchema attribute handling
420 * during LDB_ADD request processing
422 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
427 int32_t system_flags;
428 struct ldb_context *ldb;
429 struct ldb_result *ldb_res;
430 struct ldb_dn *schema_dn;
432 ldb = ldb_module_get_ctx(ac->module);
433 schema_dn = ldb_get_schema_basedn(ldb);
435 /* replicated update should always go through */
436 if (ldb_request_get_control(ac->req,
437 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
441 /* msDS-IntId is handled by system and should never be
442 * passed by clients */
443 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
444 return LDB_ERR_UNWILLING_TO_PERFORM;
447 /* do not generate msDS-IntId if Relax control is passed */
448 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
452 /* check Functional Level */
453 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
457 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
458 system_flags = ldb_msg_find_attr_as_int(ac->msg, "systemFlags", 0);
459 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
463 /* Generate new value for msDs-IntId
464 * Value should be in 0x80000000..0xBFFFFFFF range */
465 msds_intid = generate_random() % 0X3FFFFFFF;
466 msds_intid += 0x80000000;
468 /* probe id values until unique one is found */
471 if (msds_intid > 0xBFFFFFFF) {
472 msds_intid = 0x80000001;
475 ret = dsdb_module_search(ac->module, ac,
477 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
478 DSDB_FLAG_NEXT_MODULE,
480 "(msDS-IntId=%d)", msds_intid);
481 if (ret != LDB_SUCCESS) {
482 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
483 __location__": Searching for msDS-IntId=%d failed - %s\n",
486 return ldb_operr(ldb);
488 id_exists = (ldb_res->count > 0);
490 talloc_free(ldb_res);
493 return samdb_msg_add_int(ldb, ac->msg, ac->msg, "msDS-IntId",
499 * samldb_add_entry (async)
502 static int samldb_add_entry_callback(struct ldb_request *req,
503 struct ldb_reply *ares)
505 struct ldb_context *ldb;
506 struct samldb_ctx *ac;
509 ac = talloc_get_type(req->context, struct samldb_ctx);
510 ldb = ldb_module_get_ctx(ac->module);
513 return ldb_module_done(ac->req, NULL, NULL,
514 LDB_ERR_OPERATIONS_ERROR);
517 if (ares->type == LDB_REPLY_REFERRAL) {
518 return ldb_module_send_referral(ac->req, ares->referral);
521 if (ares->error != LDB_SUCCESS) {
522 return ldb_module_done(ac->req, ares->controls,
523 ares->response, ares->error);
525 if (ares->type != LDB_REPLY_DONE) {
526 ldb_asprintf_errstring(ldb, "Invalid LDB reply type %d", ares->type);
527 return ldb_module_done(ac->req, NULL, NULL,
528 LDB_ERR_OPERATIONS_ERROR);
531 /* The caller may wish to get controls back from the add */
532 ac->ares = talloc_steal(ac, ares);
534 ret = samldb_next_step(ac);
535 if (ret != LDB_SUCCESS) {
536 return ldb_module_done(ac->req, NULL, NULL, ret);
541 static int samldb_add_entry(struct samldb_ctx *ac)
543 struct ldb_context *ldb;
544 struct ldb_request *req;
547 ldb = ldb_module_get_ctx(ac->module);
549 ret = ldb_build_add_req(&req, ldb, ac,
552 ac, samldb_add_entry_callback,
554 LDB_REQ_SET_LOCATION(req);
555 if (ret != LDB_SUCCESS) {
559 return ldb_next_request(ac->module, req);
563 * return true if msg carries an attributeSchema that is intended to be RODC
564 * filtered but is also a system-critical attribute.
566 static bool check_rodc_critical_attribute(struct ldb_message *msg)
568 uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
570 schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
571 searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
572 rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE
573 | SEARCH_FLAG_CONFIDENTIAL);
575 if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
576 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
584 static int samldb_fill_object(struct samldb_ctx *ac)
586 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
589 /* Add information for the different account types */
591 case SAMLDB_TYPE_USER: {
592 struct ldb_control *rodc_control = ldb_request_get_control(ac->req,
593 LDB_CONTROL_RODC_DCPROMO_OID);
594 if (rodc_control != NULL) {
595 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
596 rodc_control->critical = false;
597 ret = samldb_add_step(ac, samldb_rodc_add);
598 if (ret != LDB_SUCCESS) return ret;
601 /* check if we have a valid sAMAccountName */
602 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
603 if (ret != LDB_SUCCESS) return ret;
605 ret = samldb_add_step(ac, samldb_add_entry);
606 if (ret != LDB_SUCCESS) return ret;
610 case SAMLDB_TYPE_GROUP: {
611 /* check if we have a valid sAMAccountName */
612 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
613 if (ret != LDB_SUCCESS) return ret;
615 ret = samldb_add_step(ac, samldb_add_entry);
616 if (ret != LDB_SUCCESS) return ret;
620 case SAMLDB_TYPE_CLASS: {
621 const struct ldb_val *rdn_value, *def_obj_cat_val;
622 unsigned int v = ldb_msg_find_attr_as_uint(ac->msg, "objectClassCategory", -2);
624 ret = samdb_find_or_add_attribute(ldb, ac->msg,
626 if (ret != LDB_SUCCESS) return ret;
628 /* do not allow to mark an attributeSchema as RODC filtered if it
629 * is system-critical */
630 if (check_rodc_critical_attribute(ac->msg)) {
631 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
632 ldb_dn_get_linearized(ac->msg->dn));
633 return LDB_ERR_UNWILLING_TO_PERFORM;
636 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
637 if (rdn_value == NULL) {
638 return ldb_operr(ldb);
640 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
641 /* the RDN has prefix "CN" */
642 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
643 samdb_cn_to_lDAPDisplayName(ac->msg,
644 (const char *) rdn_value->data));
645 if (ret != LDB_SUCCESS) {
651 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
654 guid = GUID_random();
655 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
656 if (ret != LDB_SUCCESS) {
662 def_obj_cat_val = ldb_msg_find_ldb_val(ac->msg,
663 "defaultObjectCategory");
664 if (def_obj_cat_val != NULL) {
665 /* "defaultObjectCategory" has been set by the caller.
666 * Do some checks for consistency.
667 * NOTE: The real constraint check (that
668 * 'defaultObjectCategory' is the DN of the new
669 * objectclass or any parent of it) is still incomplete.
670 * For now we say that 'defaultObjectCategory' is valid
671 * if it exists and it is of objectclass "classSchema".
673 ac->dn = ldb_dn_from_ldb_val(ac, ldb, def_obj_cat_val);
674 if (ac->dn == NULL) {
675 ldb_set_errstring(ldb,
676 "Invalid DN for 'defaultObjectCategory'!");
677 return LDB_ERR_CONSTRAINT_VIOLATION;
680 /* "defaultObjectCategory" has not been set by the
681 * caller. Use the entry DN for it. */
682 ac->dn = ac->msg->dn;
684 ret = ldb_msg_add_string(ac->msg, "defaultObjectCategory",
685 ldb_dn_alloc_linearized(ac->msg, ac->dn));
686 if (ret != LDB_SUCCESS) {
692 ret = samldb_add_step(ac, samldb_add_entry);
693 if (ret != LDB_SUCCESS) return ret;
695 /* Now perform the checks for the 'defaultObjectCategory'. The
696 * lookup DN was already saved in "ac->dn" */
697 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
698 if (ret != LDB_SUCCESS) return ret;
700 /* -2 is not a valid objectClassCategory so it means the attribute wasn't present */
702 /* Windows 2003 does this*/
703 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "objectClassCategory", 0);
704 if (ret != LDB_SUCCESS) {
711 case SAMLDB_TYPE_ATTRIBUTE: {
712 const struct ldb_val *rdn_value;
713 struct ldb_message_element *el;
714 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
715 if (rdn_value == NULL) {
716 return ldb_operr(ldb);
718 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
719 /* the RDN has prefix "CN" */
720 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
721 samdb_cn_to_lDAPDisplayName(ac->msg,
722 (const char *) rdn_value->data));
723 if (ret != LDB_SUCCESS) {
729 /* do not allow to mark an attributeSchema as RODC filtered if it
730 * is system-critical */
731 if (check_rodc_critical_attribute(ac->msg)) {
732 ldb_asprintf_errstring(ldb,
733 "samldb: refusing schema add of %s - cannot combine critical attribute with RODC filtering",
734 ldb_dn_get_linearized(ac->msg->dn));
735 return LDB_ERR_UNWILLING_TO_PERFORM;
738 ret = samdb_find_or_add_attribute(ldb, ac->msg,
739 "isSingleValued", "FALSE");
740 if (ret != LDB_SUCCESS) return ret;
742 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
745 guid = GUID_random();
746 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
747 if (ret != LDB_SUCCESS) {
753 el = ldb_msg_find_element(ac->msg, "attributeSyntax");
756 * No need to scream if there isn't as we have code later on
757 * that will take care of it.
759 const struct dsdb_syntax *syntax = find_syntax_map_by_ad_oid((const char *)el->values[0].data);
761 DEBUG(9, ("Can't find dsdb_syntax object for attributeSyntax %s\n",
762 (const char *)el->values[0].data));
764 unsigned int v = ldb_msg_find_attr_as_uint(ac->msg, "oMSyntax", 0);
765 const struct ldb_val *val = ldb_msg_find_ldb_val(ac->msg, "oMObjectClass");
768 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "oMSyntax", syntax->oMSyntax);
769 if (ret != LDB_SUCCESS) {
774 struct ldb_val val2 = ldb_val_dup(ldb, &syntax->oMObjectClass);
775 if (val2.length > 0) {
776 ret = ldb_msg_add_value(ac->msg, "oMObjectClass", &val2, NULL);
777 if (ret != LDB_SUCCESS) {
785 /* handle msDS-IntID attribute */
786 ret = samldb_add_handle_msDS_IntId(ac);
787 if (ret != LDB_SUCCESS) return ret;
789 ret = samldb_add_step(ac, samldb_add_entry);
790 if (ret != LDB_SUCCESS) return ret;
795 ldb_asprintf_errstring(ldb, "Invalid entry type!");
796 return LDB_ERR_OPERATIONS_ERROR;
800 return samldb_first_step(ac);
803 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
805 struct ldb_context *ldb;
806 const struct ldb_val *rdn_value;
810 ldb = ldb_module_get_ctx(ac->module);
812 sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
814 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
815 if (rdn_value == NULL) {
816 return ldb_operr(ldb);
818 sid = dom_sid_parse_talloc(ac->msg,
819 (const char *)rdn_value->data);
821 ldb_set_errstring(ldb,
822 "samldb: No valid SID found in ForeignSecurityPrincipal CN!");
823 return LDB_ERR_CONSTRAINT_VIOLATION;
825 if (! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
826 return ldb_operr(ldb);
830 /* finally proceed with adding the entry */
831 ret = samldb_add_step(ac, samldb_add_entry);
832 if (ret != LDB_SUCCESS) return ret;
834 return samldb_first_step(ac);
837 static int samldb_schema_info_update(struct samldb_ctx *ac)
840 struct ldb_context *ldb;
841 struct dsdb_schema *schema;
843 /* replicated update should always go through */
844 if (ldb_request_get_control(ac->req,
845 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
849 /* do not update schemaInfo during provisioning */
850 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
854 ldb = ldb_module_get_ctx(ac->module);
855 schema = dsdb_get_schema(ldb, NULL);
857 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
858 "samldb_schema_info_update: no dsdb_schema loaded");
859 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
860 return ldb_operr(ldb);
863 ret = dsdb_module_schema_info_update(ac->module, schema,
864 DSDB_FLAG_NEXT_MODULE|
867 if (ret != LDB_SUCCESS) {
868 ldb_asprintf_errstring(ldb,
869 "samldb_schema_info_update: dsdb_module_schema_info_update failed with %s",
877 static int samldb_prim_group_tester(struct samldb_ctx *ac, uint32_t rid);
880 * "Objectclass" trigger (MS-SAMR 3.1.1.8.1)
882 * Has to be invoked on "add" and "modify" operations on "user", "computer" and
884 * ac->msg contains the "add"/"modify" message
885 * ac->type contains the object type (main objectclass)
887 static int samldb_objectclass_trigger(struct samldb_ctx *ac)
889 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
890 void *skip_allocate_sids = ldb_get_opaque(ldb,
891 "skip_allocate_sids");
892 struct ldb_message_element *el, *el2;
896 /* make sure that "sAMAccountType" is not specified */
897 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
899 ldb_set_errstring(ldb,
900 "samldb: sAMAccountType must not be specified!");
901 return LDB_ERR_UNWILLING_TO_PERFORM;
904 /* Step 1: objectSid assignment */
906 /* Don't allow the objectSid to be changed. But beside the RELAX
907 * control we have also to guarantee that it can always be set with
908 * SYSTEM permissions. This is needed for the "samba3sam" backend. */
909 sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
910 if ((sid != NULL) && (!dsdb_module_am_system(ac->module)) &&
911 (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
912 ldb_set_errstring(ldb,
913 "samldb: objectSid must not be specified!");
914 return LDB_ERR_UNWILLING_TO_PERFORM;
917 /* but generate a new SID when we do have an add operations */
918 if ((sid == NULL) && (ac->req->operation == LDB_ADD) && !skip_allocate_sids) {
919 ret = samldb_add_step(ac, samldb_allocate_sid);
920 if (ret != LDB_SUCCESS) return ret;
924 case SAMLDB_TYPE_USER: {
925 bool uac_generated = false;
927 /* Step 1.2: Default values */
928 ret = samdb_find_or_add_attribute(ldb, ac->msg,
929 "accountExpires", "9223372036854775807");
930 if (ret != LDB_SUCCESS) return ret;
931 ret = samdb_find_or_add_attribute(ldb, ac->msg,
932 "badPasswordTime", "0");
933 if (ret != LDB_SUCCESS) return ret;
934 ret = samdb_find_or_add_attribute(ldb, ac->msg,
936 if (ret != LDB_SUCCESS) return ret;
937 ret = samdb_find_or_add_attribute(ldb, ac->msg,
939 if (ret != LDB_SUCCESS) return ret;
940 ret = samdb_find_or_add_attribute(ldb, ac->msg,
942 if (ret != LDB_SUCCESS) return ret;
943 ret = samdb_find_or_add_attribute(ldb, ac->msg,
945 if (ret != LDB_SUCCESS) return ret;
946 ret = samdb_find_or_add_attribute(ldb, ac->msg,
948 if (ret != LDB_SUCCESS) return ret;
949 ret = samdb_find_or_add_attribute(ldb, ac->msg,
951 if (ret != LDB_SUCCESS) return ret;
952 ret = samdb_find_or_add_attribute(ldb, ac->msg,
954 if (ret != LDB_SUCCESS) return ret;
956 /* On add operations we might need to generate a
957 * "userAccountControl" (if it isn't specified). */
958 el = ldb_msg_find_element(ac->msg, "userAccountControl");
959 if ((el == NULL) && (ac->req->operation == LDB_ADD)) {
960 ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
961 "userAccountControl",
963 if (ret != LDB_SUCCESS) {
966 uac_generated = true;
969 el = ldb_msg_find_element(ac->msg, "userAccountControl");
971 uint32_t user_account_control, account_type;
973 /* Step 1.3: "userAccountControl" -> "sAMAccountType" mapping */
974 user_account_control = ldb_msg_find_attr_as_uint(ac->msg,
975 "userAccountControl",
978 /* Temporary duplicate accounts aren't allowed */
979 if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
980 return LDB_ERR_OTHER;
983 /* Workstation and (read-only) DC objects do need objectclass "computer" */
984 if ((samdb_find_attribute(ldb, ac->msg,
985 "objectclass", "computer") == NULL) &&
986 (user_account_control &
987 (UF_SERVER_TRUST_ACCOUNT | UF_WORKSTATION_TRUST_ACCOUNT))) {
988 ldb_set_errstring(ldb,
989 "samldb: Requested account type does need objectclass 'computer'!");
990 return LDB_ERR_OBJECT_CLASS_VIOLATION;
993 account_type = ds_uf2atype(user_account_control);
994 if (account_type == 0) {
995 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
996 return LDB_ERR_UNWILLING_TO_PERFORM;
998 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1001 if (ret != LDB_SUCCESS) {
1004 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
1005 el2->flags = LDB_FLAG_MOD_REPLACE;
1007 /* "isCriticalSystemObject" might be set */
1008 if (user_account_control &
1009 (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
1010 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1012 if (ret != LDB_SUCCESS) {
1015 el2 = ldb_msg_find_element(ac->msg,
1016 "isCriticalSystemObject");
1017 el2->flags = LDB_FLAG_MOD_REPLACE;
1018 } else if (user_account_control & UF_WORKSTATION_TRUST_ACCOUNT) {
1019 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1021 if (ret != LDB_SUCCESS) {
1024 el2 = ldb_msg_find_element(ac->msg,
1025 "isCriticalSystemObject");
1026 el2->flags = LDB_FLAG_MOD_REPLACE;
1029 /* Step 1.4: "userAccountControl" -> "primaryGroupID" mapping */
1030 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
1031 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
1034 * Older AD deployments don't know about the
1037 if (rid == DOMAIN_RID_READONLY_DCS) {
1038 ret = samldb_prim_group_tester(ac, rid);
1039 if (ret != LDB_SUCCESS) {
1044 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1045 "primaryGroupID", rid);
1046 if (ret != LDB_SUCCESS) {
1049 el2 = ldb_msg_find_element(ac->msg,
1051 el2->flags = LDB_FLAG_MOD_REPLACE;
1054 /* Step 1.5: Add additional flags when needed */
1055 /* Obviously this is done when the "userAccountControl"
1056 * has been generated here (tested against Windows
1058 if (uac_generated) {
1059 user_account_control |= UF_ACCOUNTDISABLE;
1060 user_account_control |= UF_PASSWD_NOTREQD;
1062 ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
1063 "userAccountControl",
1064 user_account_control);
1065 if (ret != LDB_SUCCESS) {
1073 case SAMLDB_TYPE_GROUP: {
1074 const char *tempstr;
1076 /* Step 2.2: Default values */
1077 tempstr = talloc_asprintf(ac->msg, "%d",
1078 GTYPE_SECURITY_GLOBAL_GROUP);
1079 if (tempstr == NULL) return ldb_operr(ldb);
1080 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1081 "groupType", tempstr);
1082 if (ret != LDB_SUCCESS) return ret;
1084 /* Step 2.3: "groupType" -> "sAMAccountType" */
1085 el = ldb_msg_find_element(ac->msg, "groupType");
1087 uint32_t group_type, account_type;
1089 group_type = ldb_msg_find_attr_as_uint(ac->msg,
1092 /* The creation of builtin groups requires the
1094 if (group_type == GTYPE_SECURITY_BUILTIN_LOCAL_GROUP) {
1095 if (ldb_request_get_control(ac->req,
1096 LDB_CONTROL_RELAX_OID) == NULL) {
1097 return LDB_ERR_UNWILLING_TO_PERFORM;
1101 account_type = ds_gtype2atype(group_type);
1102 if (account_type == 0) {
1103 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1104 return LDB_ERR_UNWILLING_TO_PERFORM;
1106 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1109 if (ret != LDB_SUCCESS) {
1112 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
1113 el2->flags = LDB_FLAG_MOD_REPLACE;
1119 ldb_asprintf_errstring(ldb,
1120 "Invalid entry type!");
1121 return LDB_ERR_OPERATIONS_ERROR;
1129 * "Primary group ID" trigger (MS-SAMR 3.1.1.8.2)
1131 * Has to be invoked on "add" and "modify" operations on "user" and "computer"
1133 * ac->msg contains the "add"/"modify" message
1136 static int samldb_prim_group_tester(struct samldb_ctx *ac, uint32_t rid)
1138 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1139 struct dom_sid *sid;
1140 struct ldb_result *res;
1142 const char *noattrs[] = { NULL };
1144 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1146 return ldb_operr(ldb);
1149 ret = dsdb_module_search(ac->module, ac, &res,
1150 ldb_get_default_basedn(ldb),
1152 noattrs, DSDB_FLAG_NEXT_MODULE,
1155 ldap_encode_ndr_dom_sid(ac, sid));
1156 if (ret != LDB_SUCCESS) {
1159 if (res->count != 1) {
1161 ldb_asprintf_errstring(ldb,
1162 "Failed to find primary group with RID %u!",
1164 return LDB_ERR_UNWILLING_TO_PERFORM;
1171 static int samldb_prim_group_set(struct samldb_ctx *ac)
1173 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1176 rid = ldb_msg_find_attr_as_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
1177 if (rid == (uint32_t) -1) {
1178 /* we aren't affected of any primary group set */
1181 } else if (!ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
1182 ldb_set_errstring(ldb,
1183 "The primary group isn't settable on add operations!");
1184 return LDB_ERR_UNWILLING_TO_PERFORM;
1187 return samldb_prim_group_tester(ac, rid);
1190 static int samldb_prim_group_change(struct samldb_ctx *ac)
1192 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1193 const char * attrs[] = { "primaryGroupID", "memberOf", NULL };
1194 struct ldb_result *res, *group_res;
1195 struct ldb_message_element *el;
1196 struct ldb_message *msg;
1197 uint32_t prev_rid, new_rid;
1198 struct dom_sid *prev_sid, *new_sid;
1199 struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
1201 const char *noattrs[] = { NULL };
1203 el = dsdb_get_single_valued_attr(ac->msg, "primaryGroupID",
1204 ac->req->operation);
1206 /* we are not affected */
1210 /* Fetch information from the existing object */
1212 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1213 DSDB_FLAG_NEXT_MODULE, ac->req);
1214 if (ret != LDB_SUCCESS) {
1218 /* Finds out the DN of the old primary group */
1220 prev_rid = ldb_msg_find_attr_as_uint(res->msgs[0], "primaryGroupID",
1222 if (prev_rid == (uint32_t) -1) {
1223 /* User objects do always have a mandatory "primaryGroupID"
1224 * attribute. If this doesn't exist then the object is of the
1225 * wrong type. This is the exact Windows error code */
1226 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1229 prev_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), prev_rid);
1230 if (prev_sid == NULL) {
1231 return ldb_operr(ldb);
1234 /* Finds out the DN of the new primary group
1235 * Notice: in order to parse the primary group ID correctly we create
1236 * a temporary message here. */
1238 msg = ldb_msg_new(ac->msg);
1240 return ldb_module_oom(ac->module);
1242 ret = ldb_msg_add(msg, el, 0);
1243 if (ret != LDB_SUCCESS) {
1246 new_rid = ldb_msg_find_attr_as_uint(msg, "primaryGroupID", (uint32_t) -1);
1248 if (new_rid == (uint32_t) -1) {
1249 /* we aren't affected of any primary group change */
1253 if (prev_rid == new_rid) {
1257 ret = dsdb_module_search(ac->module, ac, &group_res,
1258 ldb_get_default_basedn(ldb),
1260 noattrs, DSDB_FLAG_NEXT_MODULE,
1263 ldap_encode_ndr_dom_sid(ac, prev_sid));
1264 if (ret != LDB_SUCCESS) {
1267 if (group_res->count != 1) {
1268 return ldb_operr(ldb);
1270 prev_prim_group_dn = group_res->msgs[0]->dn;
1272 new_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), new_rid);
1273 if (new_sid == NULL) {
1274 return ldb_operr(ldb);
1277 ret = dsdb_module_search(ac->module, ac, &group_res,
1278 ldb_get_default_basedn(ldb),
1280 noattrs, DSDB_FLAG_NEXT_MODULE,
1283 ldap_encode_ndr_dom_sid(ac, new_sid));
1284 if (ret != LDB_SUCCESS) {
1287 if (group_res->count != 1) {
1288 /* Here we know if the specified new primary group candidate is
1290 return LDB_ERR_UNWILLING_TO_PERFORM;
1292 new_prim_group_dn = group_res->msgs[0]->dn;
1294 /* We need to be already a normal member of the new primary
1295 * group in order to be successful. */
1296 el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
1297 ldb_dn_get_linearized(new_prim_group_dn));
1299 return LDB_ERR_UNWILLING_TO_PERFORM;
1302 /* Remove the "member" attribute on the new primary group */
1303 msg = ldb_msg_new(ac->msg);
1305 return ldb_module_oom(ac->module);
1307 msg->dn = new_prim_group_dn;
1309 ret = samdb_msg_add_delval(ldb, msg, msg, "member",
1310 ldb_dn_get_linearized(ac->msg->dn));
1311 if (ret != LDB_SUCCESS) {
1315 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE, ac->req);
1316 if (ret != LDB_SUCCESS) {
1321 /* Add a "member" attribute for the previous primary group */
1322 msg = ldb_msg_new(ac->msg);
1324 return ldb_module_oom(ac->module);
1326 msg->dn = prev_prim_group_dn;
1328 ret = samdb_msg_add_addval(ldb, msg, msg, "member",
1329 ldb_dn_get_linearized(ac->msg->dn));
1330 if (ret != LDB_SUCCESS) {
1334 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE, ac->req);
1335 if (ret != LDB_SUCCESS) {
1343 static int samldb_prim_group_trigger(struct samldb_ctx *ac)
1347 if (ac->req->operation == LDB_ADD) {
1348 ret = samldb_prim_group_set(ac);
1350 ret = samldb_prim_group_change(ac);
1358 * This function is called on LDB modify operations. It performs some additions/
1359 * replaces on the current LDB message when "userAccountControl" changes.
1361 static int samldb_user_account_control_change(struct samldb_ctx *ac)
1363 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1364 uint32_t user_account_control, old_user_account_control, account_type;
1365 struct ldb_message_element *el;
1366 struct ldb_message *tmp_msg;
1368 struct ldb_result *res;
1369 const char *attrs[] = { "userAccountControl", "objectClass", NULL };
1371 bool is_computer = false;
1373 el = dsdb_get_single_valued_attr(ac->msg, "userAccountControl",
1374 ac->req->operation);
1376 /* we are not affected */
1380 /* Create a temporary message for fetching the "userAccountControl" */
1381 tmp_msg = ldb_msg_new(ac->msg);
1382 if (tmp_msg == NULL) {
1383 return ldb_module_oom(ac->module);
1385 ret = ldb_msg_add(tmp_msg, el, 0);
1386 if (ret != LDB_SUCCESS) {
1389 user_account_control = ldb_msg_find_attr_as_uint(tmp_msg,
1390 "userAccountControl",
1392 talloc_free(tmp_msg);
1394 /* Temporary duplicate accounts aren't allowed */
1395 if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
1396 return LDB_ERR_OTHER;
1399 /* Fetch the old "userAccountControl" and "objectClass" */
1400 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1401 DSDB_FLAG_NEXT_MODULE, ac->req);
1402 if (ret != LDB_SUCCESS) {
1405 old_user_account_control = ldb_msg_find_attr_as_uint(res->msgs[0], "userAccountControl", 0);
1406 if (old_user_account_control == 0) {
1407 return ldb_operr(ldb);
1409 el = ldb_msg_find_element(res->msgs[0], "objectClass");
1411 return ldb_operr(ldb);
1414 /* When we do not have objectclass "computer" we cannot switch to a (read-only) DC */
1415 for (i = 0; i < el->num_values; i++) {
1416 if (ldb_attr_cmp((char *)el->values[i].data, "computer") == 0) {
1422 (user_account_control & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT))) {
1423 ldb_set_errstring(ldb,
1424 "samldb: Requested account type does need objectclass 'computer'!");
1425 return LDB_ERR_UNWILLING_TO_PERFORM;
1429 * The functions "ds_uf2atype" and "ds_uf2prim_group_rid" are used as
1430 * detectors for account type changes.
1431 * So if the account type does change then we need to adjust the
1432 * "sAMAccountType", the "isCriticalSystemObject" and the
1433 * "primaryGroupID" attribute.
1435 if ((ds_uf2atype(user_account_control)
1436 == ds_uf2atype(old_user_account_control)) &&
1437 (ds_uf2prim_group_rid(user_account_control)
1438 == ds_uf2prim_group_rid(old_user_account_control))) {
1442 account_type = ds_uf2atype(user_account_control);
1443 if (account_type == 0) {
1444 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1445 return LDB_ERR_UNWILLING_TO_PERFORM;
1447 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
1449 if (ret != LDB_SUCCESS) {
1452 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
1453 el->flags = LDB_FLAG_MOD_REPLACE;
1455 /* "isCriticalSystemObject" might be set/changed */
1456 if (user_account_control
1457 & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
1458 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1460 if (ret != LDB_SUCCESS) {
1463 el = ldb_msg_find_element(ac->msg,
1464 "isCriticalSystemObject");
1465 el->flags = LDB_FLAG_MOD_REPLACE;
1466 } else if (user_account_control & UF_WORKSTATION_TRUST_ACCOUNT) {
1467 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1469 if (ret != LDB_SUCCESS) {
1472 el = ldb_msg_find_element(ac->msg,
1473 "isCriticalSystemObject");
1474 el->flags = LDB_FLAG_MOD_REPLACE;
1477 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
1478 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
1480 /* Older AD deployments don't know about the RODC group */
1481 if (rid == DOMAIN_RID_READONLY_DCS) {
1482 ret = samldb_prim_group_tester(ac, rid);
1483 if (ret != LDB_SUCCESS) {
1488 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1489 "primaryGroupID", rid);
1490 if (ret != LDB_SUCCESS) {
1493 el = ldb_msg_find_element(ac->msg,
1495 el->flags = LDB_FLAG_MOD_REPLACE;
1501 static int samldb_group_type_change(struct samldb_ctx *ac)
1503 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1504 uint32_t group_type, old_group_type, account_type;
1505 struct ldb_message_element *el;
1506 struct ldb_message *tmp_msg;
1508 struct ldb_result *res;
1509 const char *attrs[] = { "groupType", NULL };
1511 el = dsdb_get_single_valued_attr(ac->msg, "groupType",
1512 ac->req->operation);
1514 /* we are not affected */
1518 /* Create a temporary message for fetching the "groupType" */
1519 tmp_msg = ldb_msg_new(ac->msg);
1520 if (tmp_msg == NULL) {
1521 return ldb_module_oom(ac->module);
1523 ret = ldb_msg_add(tmp_msg, el, 0);
1524 if (ret != LDB_SUCCESS) {
1527 group_type = ldb_msg_find_attr_as_uint(tmp_msg, "groupType", 0);
1528 talloc_free(tmp_msg);
1530 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1531 DSDB_FLAG_NEXT_MODULE |
1532 DSDB_SEARCH_SHOW_DELETED, ac->req);
1533 if (ret != LDB_SUCCESS) {
1536 old_group_type = ldb_msg_find_attr_as_uint(res->msgs[0], "groupType", 0);
1537 if (old_group_type == 0) {
1538 return ldb_operr(ldb);
1541 /* Group type switching isn't so easy as it seems: We can only
1542 * change in this directions: global <-> universal <-> local
1543 * On each step also the group type itself
1544 * (security/distribution) is variable. */
1546 if (ldb_request_get_control(ac->req, LDB_CONTROL_PROVISION_OID) == NULL) {
1547 switch (group_type) {
1548 case GTYPE_SECURITY_GLOBAL_GROUP:
1549 case GTYPE_DISTRIBUTION_GLOBAL_GROUP:
1550 /* change to "universal" allowed */
1551 if ((old_group_type == GTYPE_SECURITY_DOMAIN_LOCAL_GROUP) ||
1552 (old_group_type == GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP)) {
1553 ldb_set_errstring(ldb,
1554 "samldb: Change from security/distribution local group forbidden!");
1555 return LDB_ERR_UNWILLING_TO_PERFORM;
1559 case GTYPE_SECURITY_UNIVERSAL_GROUP:
1560 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP:
1561 /* each change allowed */
1563 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP:
1564 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP:
1565 /* change to "universal" allowed */
1566 if ((old_group_type == GTYPE_SECURITY_GLOBAL_GROUP) ||
1567 (old_group_type == GTYPE_DISTRIBUTION_GLOBAL_GROUP)) {
1568 ldb_set_errstring(ldb,
1569 "samldb: Change from security/distribution global group forbidden!");
1570 return LDB_ERR_UNWILLING_TO_PERFORM;
1574 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP:
1576 /* we don't allow this "groupType" values */
1577 return LDB_ERR_UNWILLING_TO_PERFORM;
1582 account_type = ds_gtype2atype(group_type);
1583 if (account_type == 0) {
1584 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1585 return LDB_ERR_UNWILLING_TO_PERFORM;
1587 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
1589 if (ret != LDB_SUCCESS) {
1592 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
1593 el->flags = LDB_FLAG_MOD_REPLACE;
1598 static int samldb_sam_accountname_check(struct samldb_ctx *ac)
1600 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1601 const char *no_attrs[] = { NULL };
1602 struct ldb_result *res;
1603 const char *sam_accountname, *enc_str;
1604 struct ldb_message_element *el;
1605 struct ldb_message *tmp_msg;
1608 el = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName",
1609 ac->req->operation);
1611 /* we are not affected */
1615 /* Create a temporary message for fetching the "sAMAccountName" */
1616 tmp_msg = ldb_msg_new(ac->msg);
1617 if (tmp_msg == NULL) {
1618 return ldb_module_oom(ac->module);
1620 ret = ldb_msg_add(tmp_msg, el, 0);
1621 if (ret != LDB_SUCCESS) {
1624 sam_accountname = talloc_steal(ac,
1625 ldb_msg_find_attr_as_string(tmp_msg, "sAMAccountName", NULL));
1626 talloc_free(tmp_msg);
1628 if (sam_accountname == NULL) {
1629 /* The "sAMAccountName" cannot be nothing */
1630 ldb_set_errstring(ldb,
1631 "samldb: Empty account names aren't allowed!");
1632 return LDB_ERR_UNWILLING_TO_PERFORM;
1635 enc_str = ldb_binary_encode_string(ac, sam_accountname);
1636 if (enc_str == NULL) {
1637 return ldb_module_oom(ac->module);
1640 /* Make sure that a "sAMAccountName" is only used once */
1642 ret = dsdb_module_search(ac->module, ac, &res,
1643 ldb_get_default_basedn(ldb),
1644 LDB_SCOPE_SUBTREE, no_attrs,
1645 DSDB_FLAG_NEXT_MODULE, ac->req,
1646 "(sAMAccountName=%s)", enc_str);
1647 if (ret != LDB_SUCCESS) {
1650 if (res->count > 1) {
1651 return ldb_operr(ldb);
1652 } else if (res->count == 1) {
1653 if (ldb_dn_compare(res->msgs[0]->dn, ac->msg->dn) != 0) {
1654 ldb_asprintf_errstring(ldb,
1655 "samldb: Account name (sAMAccountName) '%s' already in use!",
1657 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1665 static int samldb_member_check(struct samldb_ctx *ac)
1667 static const char * const attrs[] = { "objectSid", "member", NULL };
1668 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1669 struct ldb_message_element *el;
1670 struct ldb_dn *member_dn;
1671 struct dom_sid *sid;
1672 struct ldb_result *res;
1673 struct dom_sid *group_sid;
1677 /* Fetch information from the existing object */
1679 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1680 DSDB_FLAG_NEXT_MODULE, ac->req, NULL);
1681 if (ret != LDB_SUCCESS) {
1684 if (res->count != 1) {
1685 return ldb_operr(ldb);
1688 group_sid = samdb_result_dom_sid(res, res->msgs[0], "objectSid");
1689 if (group_sid == NULL) {
1690 return ldb_operr(ldb);
1693 /* We've to walk over all modification entries and consider the "member"
1695 for (i = 0; i < ac->msg->num_elements; i++) {
1696 if (ldb_attr_cmp(ac->msg->elements[i].name, "member") != 0) {
1700 el = &ac->msg->elements[i];
1701 for (j = 0; j < el->num_values; j++) {
1702 struct ldb_result *group_res;
1703 const char *group_attrs[] = { "primaryGroupID" , NULL };
1704 uint32_t prim_group_rid;
1706 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
1707 /* Deletes will be handled in
1708 * repl_meta_data, and deletes not
1709 * matching a member will return
1710 * LDB_ERR_UNWILLING_TO_PERFORM
1715 member_dn = ldb_dn_from_ldb_val(ac, ldb,
1717 if (!ldb_dn_validate(member_dn)) {
1718 return ldb_operr(ldb);
1721 /* Denies to add "member"s to groups which are primary
1722 * ones for them - in this case return
1723 * ERR_ENTRY_ALREADY_EXISTS. */
1725 ret = dsdb_module_search_dn(ac->module, ac, &group_res,
1726 member_dn, group_attrs,
1727 DSDB_FLAG_NEXT_MODULE, ac->req);
1728 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1729 /* member DN doesn't exist yet */
1732 if (ret != LDB_SUCCESS) {
1735 prim_group_rid = ldb_msg_find_attr_as_uint(group_res->msgs[0], "primaryGroupID", (uint32_t)-1);
1736 if (prim_group_rid == (uint32_t) -1) {
1737 /* the member hasn't to be a user account ->
1738 * therefore no check needed in this case. */
1742 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1745 return ldb_operr(ldb);
1748 if (dom_sid_equal(group_sid, sid)) {
1749 ldb_asprintf_errstring(ldb,
1750 "samldb: member %s already set via primaryGroupID %u",
1751 ldb_dn_get_linearized(member_dn), prim_group_rid);
1752 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1762 /* SAM objects have special rules regarding the "description" attribute on
1763 * modify operations. */
1764 static int samldb_description_check(struct samldb_ctx *ac, bool *modified)
1766 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1767 const char * const attrs[] = { "objectClass", "description", NULL };
1768 struct ldb_result *res;
1772 /* Fetch information from the existing object */
1773 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1774 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED, ac->req,
1775 "(|(objectclass=user)(objectclass=group)(objectclass=samDomain)(objectclass=samServer))");
1776 if (ret != LDB_SUCCESS) {
1777 /* don't treat it specially ... let normal error codes
1778 happen from other places */
1779 ldb_reset_err_string(ldb);
1782 if (res->count == 0) {
1783 /* we didn't match the filter */
1788 /* We've to walk over all modification entries and consider the
1789 * "description" ones. */
1790 for (i = 0; i < ac->msg->num_elements; i++) {
1791 if (ldb_attr_cmp(ac->msg->elements[i].name, "description") == 0) {
1792 ac->msg->elements[i].flags |= LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK;
1802 /* This trigger adapts the "servicePrincipalName" attributes if the
1803 * "dNSHostName" and/or "sAMAccountName" attribute change(s) */
1804 static int samldb_service_principal_names_change(struct samldb_ctx *ac)
1806 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1807 struct ldb_message_element *el = NULL, *el2 = NULL;
1808 struct ldb_message *msg;
1809 const char *attrs[] = { "servicePrincipalName", NULL };
1810 struct ldb_result *res;
1811 const char *dns_hostname = NULL, *old_dns_hostname = NULL,
1812 *sam_accountname = NULL, *old_sam_accountname = NULL;
1816 el = dsdb_get_single_valued_attr(ac->msg, "dNSHostName",
1817 ac->req->operation);
1818 el2 = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName",
1819 ac->req->operation);
1820 if ((el == NULL) && (el2 == NULL)) {
1821 /* we are not affected */
1825 /* Create a temporary message for fetching the "dNSHostName" */
1827 const char *dns_attrs[] = { "dNSHostName", NULL };
1828 msg = ldb_msg_new(ac->msg);
1830 return ldb_module_oom(ac->module);
1832 ret = ldb_msg_add(msg, el, 0);
1833 if (ret != LDB_SUCCESS) {
1836 dns_hostname = talloc_steal(ac,
1837 ldb_msg_find_attr_as_string(msg, "dNSHostName", NULL));
1840 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn,
1841 dns_attrs, DSDB_FLAG_NEXT_MODULE, ac->req);
1842 if (ret == LDB_SUCCESS) {
1843 old_dns_hostname = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
1847 /* Create a temporary message for fetching the "sAMAccountName" */
1849 char *tempstr, *tempstr2;
1850 const char *acct_attrs[] = { "sAMAccountName", NULL };
1852 msg = ldb_msg_new(ac->msg);
1854 return ldb_module_oom(ac->module);
1856 ret = ldb_msg_add(msg, el2, 0);
1857 if (ret != LDB_SUCCESS) {
1860 tempstr = talloc_strdup(ac,
1861 ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL));
1864 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, acct_attrs,
1865 DSDB_FLAG_NEXT_MODULE, ac->req);
1866 if (ret == LDB_SUCCESS) {
1867 tempstr2 = talloc_strdup(ac,
1868 ldb_msg_find_attr_as_string(res->msgs[0],
1869 "sAMAccountName", NULL));
1873 /* The "sAMAccountName" needs some additional trimming: we need
1874 * to remove the trailing "$"s if they exist. */
1875 if ((tempstr != NULL) && (tempstr[0] != '\0') &&
1876 (tempstr[strlen(tempstr) - 1] == '$')) {
1877 tempstr[strlen(tempstr) - 1] = '\0';
1879 if ((tempstr2 != NULL) && (tempstr2[0] != '\0') &&
1880 (tempstr2[strlen(tempstr2) - 1] == '$')) {
1881 tempstr2[strlen(tempstr2) - 1] = '\0';
1883 sam_accountname = tempstr;
1884 old_sam_accountname = tempstr2;
1887 if (old_dns_hostname == NULL) {
1888 /* we cannot change when the old name is unknown */
1889 dns_hostname = NULL;
1891 if ((old_dns_hostname != NULL) && (dns_hostname != NULL) &&
1892 (strcasecmp(old_dns_hostname, dns_hostname) == 0)) {
1893 /* The "dNSHostName" didn't change */
1894 dns_hostname = NULL;
1897 if (old_sam_accountname == NULL) {
1898 /* we cannot change when the old name is unknown */
1899 sam_accountname = NULL;
1901 if ((old_sam_accountname != NULL) && (sam_accountname != NULL) &&
1902 (strcasecmp(old_sam_accountname, sam_accountname) == 0)) {
1903 /* The "sAMAccountName" didn't change */
1904 sam_accountname = NULL;
1907 if ((dns_hostname == NULL) && (sam_accountname == NULL)) {
1908 /* Well, there are information missing (old name(s)) or the
1909 * names didn't change. We've nothing to do and can exit here */
1913 /* Potential "servicePrincipalName" changes in the same request have to
1914 * be handled before the update (Windows behaviour). */
1915 el = ldb_msg_find_element(ac->msg, "servicePrincipalName");
1917 msg = ldb_msg_new(ac->msg);
1919 return ldb_module_oom(ac->module);
1921 msg->dn = ac->msg->dn;
1924 ret = ldb_msg_add(msg, el, el->flags);
1925 if (ret != LDB_SUCCESS) {
1929 ldb_msg_remove_element(ac->msg, el);
1931 el = ldb_msg_find_element(ac->msg,
1932 "servicePrincipalName");
1933 } while (el != NULL);
1935 ret = dsdb_module_modify(ac->module, msg,
1936 DSDB_FLAG_NEXT_MODULE, ac->req);
1937 if (ret != LDB_SUCCESS) {
1943 /* Fetch the "servicePrincipalName"s if any */
1944 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1945 DSDB_FLAG_NEXT_MODULE, ac->req, NULL);
1946 if (ret != LDB_SUCCESS) {
1949 if ((res->count != 1) || (res->msgs[0]->num_elements > 1)) {
1950 return ldb_operr(ldb);
1953 if (res->msgs[0]->num_elements == 1) {
1954 /* Yes, we do have "servicePrincipalName"s. First we update them
1955 * locally, that means we do always substitute the current
1956 * "dNSHostName" with the new one and/or "sAMAccountName"
1957 * without "$" with the new one and then we append this to the
1958 * modification request (Windows behaviour). */
1960 for (i = 0; i < res->msgs[0]->elements[0].num_values; i++) {
1961 char *old_str, *new_str, *pos;
1965 res->msgs[0]->elements[0].values[i].data;
1967 new_str = talloc_strdup(ac->msg,
1968 strtok_r(old_str, "/", &pos));
1969 if (new_str == NULL) {
1970 return ldb_module_oom(ac->module);
1973 while ((tok = strtok_r(NULL, "/", &pos)) != NULL) {
1974 if ((dns_hostname != NULL) &&
1975 (strcasecmp(tok, old_dns_hostname) == 0)) {
1978 if ((sam_accountname != NULL) &&
1979 (strcasecmp(tok, old_sam_accountname) == 0)) {
1980 tok = sam_accountname;
1983 new_str = talloc_asprintf(ac->msg, "%s/%s",
1985 if (new_str == NULL) {
1986 return ldb_module_oom(ac->module);
1990 ret = ldb_msg_add_string(ac->msg,
1991 "servicePrincipalName",
1993 if (ret != LDB_SUCCESS) {
1998 el = ldb_msg_find_element(ac->msg, "servicePrincipalName");
1999 el->flags = LDB_FLAG_MOD_REPLACE;
2009 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
2011 struct ldb_context *ldb;
2012 struct samldb_ctx *ac;
2015 ldb = ldb_module_get_ctx(module);
2016 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
2018 /* do not manipulate our control entries */
2019 if (ldb_dn_is_special(req->op.add.message->dn)) {
2020 return ldb_next_request(module, req);
2023 ac = samldb_ctx_init(module, req);
2025 return ldb_operr(ldb);
2028 /* build the new msg */
2029 ac->msg = ldb_msg_copy_shallow(ac, req->op.add.message);
2030 if (ac->msg == NULL) {
2032 ldb_debug(ldb, LDB_DEBUG_FATAL,
2033 "samldb_add: ldb_msg_copy_shallow failed!\n");
2034 return ldb_operr(ldb);
2037 if (samdb_find_attribute(ldb, ac->msg,
2038 "objectclass", "user") != NULL) {
2039 ac->type = SAMLDB_TYPE_USER;
2041 ret = samldb_prim_group_trigger(ac);
2042 if (ret != LDB_SUCCESS) {
2046 ret = samldb_objectclass_trigger(ac);
2047 if (ret != LDB_SUCCESS) {
2051 return samldb_fill_object(ac);
2054 if (samdb_find_attribute(ldb, ac->msg,
2055 "objectclass", "group") != NULL) {
2056 ac->type = SAMLDB_TYPE_GROUP;
2058 ret = samldb_objectclass_trigger(ac);
2059 if (ret != LDB_SUCCESS) {
2063 return samldb_fill_object(ac);
2066 /* perhaps a foreignSecurityPrincipal? */
2067 if (samdb_find_attribute(ldb, ac->msg,
2069 "foreignSecurityPrincipal") != NULL) {
2070 return samldb_fill_foreignSecurityPrincipal_object(ac);
2073 if (samdb_find_attribute(ldb, ac->msg,
2074 "objectclass", "classSchema") != NULL) {
2075 ret = samldb_schema_info_update(ac);
2076 if (ret != LDB_SUCCESS) {
2081 ac->type = SAMLDB_TYPE_CLASS;
2082 return samldb_fill_object(ac);
2085 if (samdb_find_attribute(ldb, ac->msg,
2086 "objectclass", "attributeSchema") != NULL) {
2087 ret = samldb_schema_info_update(ac);
2088 if (ret != LDB_SUCCESS) {
2093 ac->type = SAMLDB_TYPE_ATTRIBUTE;
2094 return samldb_fill_object(ac);
2099 /* nothing matched, go on */
2100 return ldb_next_request(module, req);
2104 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
2106 struct ldb_context *ldb;
2107 struct samldb_ctx *ac;
2108 struct ldb_message_element *el, *el2;
2109 bool modified = false;
2112 if (ldb_dn_is_special(req->op.mod.message->dn)) {
2113 /* do not manipulate our control entries */
2114 return ldb_next_request(module, req);
2117 ldb = ldb_module_get_ctx(module);
2119 /* make sure that "objectSid" is not specified */
2120 el = ldb_msg_find_element(req->op.mod.message, "objectSid");
2122 if (ldb_request_get_control(req, LDB_CONTROL_PROVISION_OID) == NULL) {
2123 ldb_set_errstring(ldb,
2124 "samldb: objectSid must not be specified!");
2125 return LDB_ERR_UNWILLING_TO_PERFORM;
2128 /* make sure that "sAMAccountType" is not specified */
2129 el = ldb_msg_find_element(req->op.mod.message, "sAMAccountType");
2131 ldb_set_errstring(ldb,
2132 "samldb: sAMAccountType must not be specified!");
2133 return LDB_ERR_UNWILLING_TO_PERFORM;
2135 /* make sure that "isCriticalSystemObject" is not specified */
2136 el = ldb_msg_find_element(req->op.mod.message, "isCriticalSystemObject");
2138 if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID) == NULL) {
2139 ldb_set_errstring(ldb,
2140 "samldb: isCriticalSystemObject must not be specified!");
2141 return LDB_ERR_UNWILLING_TO_PERFORM;
2145 /* msDS-IntId is not allowed to be modified
2146 * except when modification comes from replication */
2147 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
2148 if (!ldb_request_get_control(req,
2149 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
2150 return LDB_ERR_CONSTRAINT_VIOLATION;
2154 ac = samldb_ctx_init(module, req);
2156 return ldb_operr(ldb);
2159 /* build the new msg */
2160 ac->msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
2161 if (ac->msg == NULL) {
2163 ldb_debug(ldb, LDB_DEBUG_FATAL,
2164 "samldb_modify: ldb_msg_copy_shallow failed!\n");
2165 return ldb_operr(ldb);
2168 el = ldb_msg_find_element(ac->msg, "primaryGroupID");
2170 ret = samldb_prim_group_trigger(ac);
2171 if (ret != LDB_SUCCESS) {
2176 el = ldb_msg_find_element(ac->msg, "userAccountControl");
2179 ret = samldb_user_account_control_change(ac);
2180 if (ret != LDB_SUCCESS) {
2185 el = ldb_msg_find_element(ac->msg, "groupType");
2188 ret = samldb_group_type_change(ac);
2189 if (ret != LDB_SUCCESS) {
2194 el = ldb_msg_find_element(ac->msg, "sAMAccountName");
2196 ret = samldb_sam_accountname_check(ac);
2197 if (ret != LDB_SUCCESS) {
2202 el = ldb_msg_find_element(ac->msg, "member");
2204 ret = samldb_member_check(ac);
2205 if (ret != LDB_SUCCESS) {
2210 el = ldb_msg_find_element(ac->msg, "description");
2212 ret = samldb_description_check(ac, &modified);
2213 if (ret != LDB_SUCCESS) {
2218 el = ldb_msg_find_element(ac->msg, "dNSHostName");
2219 el2 = ldb_msg_find_element(ac->msg, "sAMAccountName");
2220 if ((el != NULL) || (el2 != NULL)) {
2222 ret = samldb_service_principal_names_change(ac);
2223 if (ret != LDB_SUCCESS) {
2229 struct ldb_request *child_req;
2231 /* Now perform the real modifications as a child request */
2232 ret = ldb_build_mod_req(&child_req, ldb, ac,
2235 req, dsdb_next_callback,
2237 LDB_REQ_SET_LOCATION(child_req);
2238 if (ret != LDB_SUCCESS) {
2242 return ldb_next_request(module, child_req);
2247 /* no change which interests us, go on */
2248 return ldb_next_request(module, req);
2253 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
2255 struct ldb_context *ldb;
2256 struct dom_sid *sid;
2260 struct ldb_result *res;
2261 const char *attrs[] = { "objectSid", "isDeleted", NULL };
2262 const char *noattrs[] = { NULL };
2264 ldb = ldb_module_get_ctx(ac->module);
2266 /* Finds out the SID/RID of the SAM object */
2267 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->req->op.del.dn,
2269 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED,
2271 if (ret != LDB_SUCCESS) {
2275 if (ldb_msg_check_string_attribute(res->msgs[0], "isDeleted", "TRUE")) {
2279 sid = samdb_result_dom_sid(ac, res->msgs[0], "objectSid");
2281 /* No SID - it might not be a SAM object - therefore ok */
2284 status = dom_sid_split_rid(ac, sid, NULL, &rid);
2285 if (!NT_STATUS_IS_OK(status)) {
2286 return ldb_operr(ldb);
2289 /* Special object (security principal?) */
2293 /* Deny delete requests from groups which are primary ones */
2294 ret = dsdb_module_search(ac->module, ac, &res,
2295 ldb_get_default_basedn(ldb),
2296 LDB_SCOPE_SUBTREE, noattrs,
2297 DSDB_FLAG_NEXT_MODULE,
2299 "(&(primaryGroupID=%u)(objectClass=user))", rid);
2300 if (ret != LDB_SUCCESS) {
2303 if (res->count > 0) {
2304 return LDB_ERR_ENTRY_ALREADY_EXISTS;
2310 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
2312 struct samldb_ctx *ac;
2315 if (ldb_dn_is_special(req->op.del.dn)) {
2316 /* do not manipulate our control entries */
2317 return ldb_next_request(module, req);
2320 ac = samldb_ctx_init(module, req);
2322 return ldb_operr(ldb_module_get_ctx(module));
2325 ret = samldb_prim_group_users_check(ac);
2326 if (ret != LDB_SUCCESS) {
2332 return ldb_next_request(module, req);
2337 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
2339 struct ldb_context *ldb = ldb_module_get_ctx(module);
2340 struct dsdb_fsmo_extended_op *exop;
2343 exop = talloc_get_type(req->op.extended.data,
2344 struct dsdb_fsmo_extended_op);
2346 ldb_set_errstring(ldb,
2347 "samldb_extended_allocate_rid_pool: invalid extended data");
2348 return LDB_ERR_PROTOCOL_ERROR;
2351 ret = ridalloc_allocate_rid_pool_fsmo(module, exop, req);
2352 if (ret != LDB_SUCCESS) {
2356 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
2359 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
2361 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
2362 return samldb_extended_allocate_rid_pool(module, req);
2365 return ldb_next_request(module, req);
2369 static const struct ldb_module_ops ldb_samldb_module_ops = {
2372 .modify = samldb_modify,
2373 .del = samldb_delete,
2374 .extended = samldb_extended
2378 int ldb_samldb_module_init(const char *version)
2380 LDB_MODULE_CHECK_VERSION(version);
2381 return ldb_register_module(&ldb_samldb_module_ops);