s4-dsdb/samdb: Don't relax contraint checking during rename for Deleted objects
[Samba.git] / source4 / dsdb / samdb / ldb_modules / samldb.c
blob39b6bdf501a44d0d5b2172c5d6995737769d92e5
1 /*
2 SAM ldb module
4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2014
5 Copyright (C) Simo Sorce 2004-2008
6 Copyright (C) Matthias Dieter Wallnöfer 2009-2011
7 Copyright (C) Matthieu Patou 2012
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * Name: ldb
26 * Component: ldb samldb module
28 * Description: various internal DSDB triggers - most for SAM specific objects
30 * Author: Simo Sorce
33 #include "includes.h"
34 #include "libcli/ldap/ldap_ndr.h"
35 #include "ldb_module.h"
36 #include "auth/auth.h"
37 #include "dsdb/samdb/samdb.h"
38 #include "dsdb/samdb/ldb_modules/util.h"
39 #include "dsdb/samdb/ldb_modules/ridalloc.h"
40 #include "libcli/security/security.h"
41 #include "librpc/gen_ndr/ndr_security.h"
42 #include "ldb_wrap.h"
43 #include "param/param.h"
44 #include "libds/common/flag_mapping.h"
46 struct samldb_ctx;
47 enum samldb_add_type {
48 SAMLDB_TYPE_USER,
49 SAMLDB_TYPE_GROUP,
50 SAMLDB_TYPE_CLASS,
51 SAMLDB_TYPE_ATTRIBUTE
54 typedef int (*samldb_step_fn_t)(struct samldb_ctx *);
56 struct samldb_step {
57 struct samldb_step *next;
58 samldb_step_fn_t fn;
61 struct samldb_ctx {
62 struct ldb_module *module;
63 struct ldb_request *req;
65 /* used for add operations */
66 enum samldb_add_type type;
68 /* the resulting message */
69 struct ldb_message *msg;
71 /* used in "samldb_find_for_defaultObjectCategory" */
72 struct ldb_dn *dn, *res_dn;
74 /* all the async steps necessary to complete the operation */
75 struct samldb_step *steps;
76 struct samldb_step *curstep;
78 /* If someone set an ares to forward controls and response back to the caller */
79 struct ldb_reply *ares;
82 static struct samldb_ctx *samldb_ctx_init(struct ldb_module *module,
83 struct ldb_request *req)
85 struct ldb_context *ldb;
86 struct samldb_ctx *ac;
88 ldb = ldb_module_get_ctx(module);
90 ac = talloc_zero(req, struct samldb_ctx);
91 if (ac == NULL) {
92 ldb_oom(ldb);
93 return NULL;
96 ac->module = module;
97 ac->req = req;
99 return ac;
102 static int samldb_add_step(struct samldb_ctx *ac, samldb_step_fn_t fn)
104 struct samldb_step *step, *stepper;
106 step = talloc_zero(ac, struct samldb_step);
107 if (step == NULL) {
108 return ldb_oom(ldb_module_get_ctx(ac->module));
111 step->fn = fn;
113 if (ac->steps == NULL) {
114 ac->steps = step;
115 ac->curstep = step;
116 } else {
117 if (ac->curstep == NULL)
118 return ldb_operr(ldb_module_get_ctx(ac->module));
119 for (stepper = ac->curstep; stepper->next != NULL;
120 stepper = stepper->next);
121 stepper->next = step;
124 return LDB_SUCCESS;
127 static int samldb_first_step(struct samldb_ctx *ac)
129 if (ac->steps == NULL) {
130 return ldb_operr(ldb_module_get_ctx(ac->module));
133 ac->curstep = ac->steps;
134 return ac->curstep->fn(ac);
137 static int samldb_next_step(struct samldb_ctx *ac)
139 if (ac->curstep->next) {
140 ac->curstep = ac->curstep->next;
141 return ac->curstep->fn(ac);
144 /* We exit the samldb module here. If someone set an "ares" to forward
145 * controls and response back to the caller, use them. */
146 if (ac->ares) {
147 return ldb_module_done(ac->req, ac->ares->controls,
148 ac->ares->response, LDB_SUCCESS);
149 } else {
150 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
155 /* sAMAccountName handling */
157 static int samldb_generate_sAMAccountName(struct ldb_context *ldb,
158 struct ldb_message *msg)
160 char *name;
162 /* Format: $000000-000000000000 */
164 name = talloc_asprintf(msg, "$%.6X-%.6X%.6X",
165 (unsigned int)generate_random(),
166 (unsigned int)generate_random(),
167 (unsigned int)generate_random());
168 if (name == NULL) {
169 return ldb_oom(ldb);
171 return ldb_msg_add_steal_string(msg, "sAMAccountName", name);
174 static int samldb_check_sAMAccountName(struct samldb_ctx *ac)
176 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
177 const char *name;
178 int ret;
179 struct ldb_result *res;
180 const char * const noattrs[] = { NULL };
182 if (ldb_msg_find_element(ac->msg, "sAMAccountName") == NULL) {
183 ret = samldb_generate_sAMAccountName(ldb, ac->msg);
184 if (ret != LDB_SUCCESS) {
185 return ret;
189 name = ldb_msg_find_attr_as_string(ac->msg, "sAMAccountName", NULL);
190 if (name == NULL) {
191 /* The "sAMAccountName" cannot be nothing */
192 ldb_set_errstring(ldb,
193 "samldb: Empty account names aren't allowed!");
194 return LDB_ERR_CONSTRAINT_VIOLATION;
197 ret = dsdb_module_search(ac->module, ac, &res,
198 ldb_get_default_basedn(ldb), LDB_SCOPE_SUBTREE, noattrs,
199 DSDB_FLAG_NEXT_MODULE,
200 ac->req,
201 "(sAMAccountName=%s)",
202 ldb_binary_encode_string(ac, name));
203 if (ret != LDB_SUCCESS) {
204 return ret;
206 if (res->count != 0) {
207 ldb_asprintf_errstring(ldb,
208 "samldb: Account name (sAMAccountName) '%s' already in use!",
209 name);
210 talloc_free(res);
211 return LDB_ERR_ENTRY_ALREADY_EXISTS;
213 talloc_free(res);
215 return samldb_next_step(ac);
219 static bool samldb_msg_add_sid(struct ldb_message *msg,
220 const char *name,
221 const struct dom_sid *sid)
223 struct ldb_val v;
224 enum ndr_err_code ndr_err;
226 ndr_err = ndr_push_struct_blob(&v, msg, sid,
227 (ndr_push_flags_fn_t)ndr_push_dom_sid);
228 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
229 return false;
231 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
235 /* allocate a SID using our RID Set */
236 static int samldb_allocate_sid(struct samldb_ctx *ac)
238 uint32_t rid;
239 struct dom_sid *sid;
240 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
241 int ret;
243 ret = ridalloc_allocate_rid(ac->module, &rid, ac->req);
244 if (ret != LDB_SUCCESS) {
245 return ret;
248 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
249 if (sid == NULL) {
250 return ldb_module_oom(ac->module);
253 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
254 return ldb_operr(ldb);
257 return samldb_next_step(ac);
261 see if a krbtgt_number is available
263 static bool samldb_krbtgtnumber_available(struct samldb_ctx *ac,
264 uint32_t krbtgt_number)
266 TALLOC_CTX *tmp_ctx = talloc_new(ac);
267 struct ldb_result *res;
268 const char * const no_attrs[] = { NULL };
269 int ret;
271 ret = dsdb_module_search(ac->module, tmp_ctx, &res,
272 ldb_get_default_basedn(ldb_module_get_ctx(ac->module)),
273 LDB_SCOPE_SUBTREE, no_attrs,
274 DSDB_FLAG_NEXT_MODULE,
275 ac->req,
276 "(msDC-SecondaryKrbTgtNumber=%u)",
277 krbtgt_number);
278 if (ret == LDB_SUCCESS && res->count == 0) {
279 talloc_free(tmp_ctx);
280 return true;
282 talloc_free(tmp_ctx);
283 return false;
286 /* special handling for add in RODC join */
287 static int samldb_rodc_add(struct samldb_ctx *ac)
289 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
290 uint32_t krbtgt_number, i_start, i;
291 int ret;
292 char *newpass;
293 struct ldb_val newpass_utf16;
295 /* find a unused msDC-SecondaryKrbTgtNumber */
296 i_start = generate_random() & 0xFFFF;
297 if (i_start == 0) {
298 i_start = 1;
301 for (i=i_start; i<=0xFFFF; i++) {
302 if (samldb_krbtgtnumber_available(ac, i)) {
303 krbtgt_number = i;
304 goto found;
307 for (i=1; i<i_start; i++) {
308 if (samldb_krbtgtnumber_available(ac, i)) {
309 krbtgt_number = i;
310 goto found;
314 ldb_asprintf_errstring(ldb,
315 "%08X: Unable to find available msDS-SecondaryKrbTgtNumber",
316 W_ERROR_V(WERR_NO_SYSTEM_RESOURCES));
317 return LDB_ERR_OTHER;
319 found:
320 ret = ldb_msg_add_empty(ac->msg, "msDS-SecondaryKrbTgtNumber",
321 LDB_FLAG_INTERNAL_DISABLE_VALIDATION, NULL);
322 if (ret != LDB_SUCCESS) {
323 return ldb_operr(ldb);
326 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
327 "msDS-SecondaryKrbTgtNumber", krbtgt_number);
328 if (ret != LDB_SUCCESS) {
329 return ldb_operr(ldb);
332 ret = ldb_msg_add_fmt(ac->msg, "sAMAccountName", "krbtgt_%u",
333 krbtgt_number);
334 if (ret != LDB_SUCCESS) {
335 return ldb_operr(ldb);
338 newpass = generate_random_password(ac->msg, 128, 255);
339 if (newpass == NULL) {
340 return ldb_operr(ldb);
343 if (!convert_string_talloc(ac,
344 CH_UNIX, CH_UTF16,
345 newpass, strlen(newpass),
346 (void *)&newpass_utf16.data,
347 &newpass_utf16.length)) {
348 ldb_asprintf_errstring(ldb,
349 "samldb_rodc_add: "
350 "failed to generate UTF16 password from random password");
351 return LDB_ERR_OPERATIONS_ERROR;
353 ret = ldb_msg_add_steal_value(ac->msg, "clearTextPassword", &newpass_utf16);
354 if (ret != LDB_SUCCESS) {
355 return ldb_operr(ldb);
358 return samldb_next_step(ac);
361 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
363 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
364 struct ldb_result *res;
365 const char * const no_attrs[] = { NULL };
366 int ret;
368 ac->res_dn = NULL;
370 ret = dsdb_module_search(ac->module, ac, &res,
371 ac->dn, LDB_SCOPE_BASE, no_attrs,
372 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
373 | DSDB_FLAG_NEXT_MODULE,
374 ac->req,
375 "(objectClass=classSchema)");
376 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
377 /* Don't be pricky when the DN doesn't exist if we have the */
378 /* RELAX control specified */
379 if (ldb_request_get_control(ac->req,
380 LDB_CONTROL_RELAX_OID) == NULL) {
381 ldb_set_errstring(ldb,
382 "samldb_find_defaultObjectCategory: "
383 "Invalid DN for 'defaultObjectCategory'!");
384 return LDB_ERR_CONSTRAINT_VIOLATION;
387 if ((ret != LDB_ERR_NO_SUCH_OBJECT) && (ret != LDB_SUCCESS)) {
388 return ret;
391 if (ret == LDB_SUCCESS) {
392 /* ensure the defaultObjectCategory has a full GUID */
393 struct ldb_message *m;
394 m = ldb_msg_new(ac->msg);
395 if (m == NULL) {
396 return ldb_oom(ldb);
398 m->dn = ac->msg->dn;
399 if (ldb_msg_add_string(m, "defaultObjectCategory",
400 ldb_dn_get_extended_linearized(m, res->msgs[0]->dn, 1)) !=
401 LDB_SUCCESS) {
402 return ldb_oom(ldb);
404 m->elements[0].flags = LDB_FLAG_MOD_REPLACE;
406 ret = dsdb_module_modify(ac->module, m,
407 DSDB_FLAG_NEXT_MODULE,
408 ac->req);
409 if (ret != LDB_SUCCESS) {
410 return ret;
415 ac->res_dn = ac->dn;
417 return samldb_next_step(ac);
421 * msDS-IntId attributeSchema attribute handling
422 * during LDB_ADD request processing
424 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
426 int ret;
427 bool id_exists;
428 uint32_t msds_intid;
429 int32_t system_flags;
430 struct ldb_context *ldb;
431 struct ldb_result *ldb_res;
432 struct ldb_dn *schema_dn;
433 struct samldb_msds_intid_persistant *msds_intid_struct;
434 struct dsdb_schema *schema;
436 ldb = ldb_module_get_ctx(ac->module);
437 schema_dn = ldb_get_schema_basedn(ldb);
439 /* replicated update should always go through */
440 if (ldb_request_get_control(ac->req,
441 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
442 return LDB_SUCCESS;
445 /* msDS-IntId is handled by system and should never be
446 * passed by clients */
447 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
448 return LDB_ERR_UNWILLING_TO_PERFORM;
451 /* do not generate msDS-IntId if Relax control is passed */
452 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
453 return LDB_SUCCESS;
456 /* check Functional Level */
457 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
458 return LDB_SUCCESS;
461 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
462 system_flags = ldb_msg_find_attr_as_int(ac->msg, "systemFlags", 0);
463 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
464 return LDB_SUCCESS;
466 schema = dsdb_get_schema(ldb, NULL);
467 if (!schema) {
468 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
469 "samldb_schema_info_update: no dsdb_schema loaded");
470 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
471 return ldb_operr(ldb);
474 msds_intid_struct = (struct samldb_msds_intid_persistant*) ldb_get_opaque(ldb, SAMLDB_MSDS_INTID_OPAQUE);
475 if (!msds_intid_struct) {
476 msds_intid_struct = talloc(ldb, struct samldb_msds_intid_persistant);
477 /* Generate new value for msDs-IntId
478 * Value should be in 0x80000000..0xBFFFFFFF range */
479 msds_intid = generate_random() % 0X3FFFFFFF;
480 msds_intid += 0x80000000;
481 msds_intid_struct->msds_intid = msds_intid;
482 msds_intid_struct->usn = schema->loaded_usn;
483 DEBUG(2, ("No samldb_msds_intid_persistant struct, allocating a new one\n"));
484 } else {
485 msds_intid = msds_intid_struct->msds_intid;
488 /* probe id values until unique one is found */
489 do {
490 uint64_t current_usn;
491 msds_intid++;
492 if (msds_intid > 0xBFFFFFFF) {
493 msds_intid = 0x80000001;
496 * Alternative strategy to a costly (even indexed search) to the
497 * database.
498 * We search in the schema if we have already this intid (using dsdb_attribute_by_attributeID_id because
499 * in the range 0x80000000 0xBFFFFFFFF, attributeID is a DSDB_ATTID_TYPE_INTID).
500 * If so generate another random value.
501 * If not check if the highest USN in the database for the schema partition is the
502 * one that we know.
503 * If so it means that's only this ldb context that is touching the schema in the database.
504 * If not it means that's someone else has modified the database while we are doing our changes too
505 * (this case should be very bery rare) in order to be sure do the search in the database.
507 if (dsdb_attribute_by_attributeID_id(schema, msds_intid)) {
508 msds_intid = generate_random() % 0X3FFFFFFF;
509 msds_intid += 0x80000000;
510 continue;
513 ret = dsdb_module_load_partition_usn(ac->module, schema_dn,
514 &current_usn, NULL, NULL);
515 if (ret != LDB_SUCCESS) {
516 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
517 __location__": Searching for schema USN failed: %s\n",
518 ldb_errstring(ldb));
519 return ldb_operr(ldb);
522 /* current_usn can be lesser than msds_intid_struct-> if there is
523 * uncommited changes.
525 if (current_usn > msds_intid_struct->usn) {
526 /* oups something has changed, someone/something
527 * else is modifying or has modified the schema
528 * we'd better check this intid is the database directly
531 DEBUG(2, ("Schema has changed, searching the database for the unicity of %d\n",
532 msds_intid));
534 ret = dsdb_module_search(ac->module, ac,
535 &ldb_res,
536 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
537 DSDB_FLAG_NEXT_MODULE,
538 ac->req,
539 "(msDS-IntId=%d)", msds_intid);
540 if (ret != LDB_SUCCESS) {
541 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
542 __location__": Searching for msDS-IntId=%d failed - %s\n",
543 msds_intid,
544 ldb_errstring(ldb));
545 return ldb_operr(ldb);
547 id_exists = (ldb_res->count > 0);
548 talloc_free(ldb_res);
549 } else {
550 id_exists = 0;
553 } while(id_exists);
554 msds_intid_struct->msds_intid = msds_intid;
555 ldb_set_opaque(ldb, SAMLDB_MSDS_INTID_OPAQUE, msds_intid_struct);
557 return samdb_msg_add_int(ldb, ac->msg, ac->msg, "msDS-IntId",
558 msds_intid);
563 * samldb_add_entry (async)
566 static int samldb_add_entry_callback(struct ldb_request *req,
567 struct ldb_reply *ares)
569 struct ldb_context *ldb;
570 struct samldb_ctx *ac;
571 int ret;
573 ac = talloc_get_type(req->context, struct samldb_ctx);
574 ldb = ldb_module_get_ctx(ac->module);
576 if (!ares) {
577 return ldb_module_done(ac->req, NULL, NULL,
578 LDB_ERR_OPERATIONS_ERROR);
581 if (ares->type == LDB_REPLY_REFERRAL) {
582 return ldb_module_send_referral(ac->req, ares->referral);
585 if (ares->error != LDB_SUCCESS) {
586 return ldb_module_done(ac->req, ares->controls,
587 ares->response, ares->error);
589 if (ares->type != LDB_REPLY_DONE) {
590 ldb_asprintf_errstring(ldb, "Invalid LDB reply type %d", ares->type);
591 return ldb_module_done(ac->req, NULL, NULL,
592 LDB_ERR_OPERATIONS_ERROR);
595 /* The caller may wish to get controls back from the add */
596 ac->ares = talloc_steal(ac, ares);
598 ret = samldb_next_step(ac);
599 if (ret != LDB_SUCCESS) {
600 return ldb_module_done(ac->req, NULL, NULL, ret);
602 return ret;
605 static int samldb_add_entry(struct samldb_ctx *ac)
607 struct ldb_context *ldb;
608 struct ldb_request *req;
609 int ret;
611 ldb = ldb_module_get_ctx(ac->module);
613 ret = ldb_build_add_req(&req, ldb, ac,
614 ac->msg,
615 ac->req->controls,
616 ac, samldb_add_entry_callback,
617 ac->req);
618 LDB_REQ_SET_LOCATION(req);
619 if (ret != LDB_SUCCESS) {
620 return ret;
623 return ldb_next_request(ac->module, req);
627 * return true if msg carries an attributeSchema that is intended to be RODC
628 * filtered but is also a system-critical attribute.
630 static bool check_rodc_critical_attribute(struct ldb_message *msg)
632 uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
634 schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
635 searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
636 rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE
637 | SEARCH_FLAG_CONFIDENTIAL);
639 if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
640 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
641 return true;
642 } else {
643 return false;
648 static int samldb_fill_object(struct samldb_ctx *ac)
650 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
651 int ret;
653 /* Add information for the different account types */
654 switch(ac->type) {
655 case SAMLDB_TYPE_USER: {
656 struct ldb_control *rodc_control = ldb_request_get_control(ac->req,
657 LDB_CONTROL_RODC_DCPROMO_OID);
658 if (rodc_control != NULL) {
659 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
660 rodc_control->critical = false;
661 ret = samldb_add_step(ac, samldb_rodc_add);
662 if (ret != LDB_SUCCESS) return ret;
665 /* check if we have a valid sAMAccountName */
666 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
667 if (ret != LDB_SUCCESS) return ret;
669 ret = samldb_add_step(ac, samldb_add_entry);
670 if (ret != LDB_SUCCESS) return ret;
671 break;
674 case SAMLDB_TYPE_GROUP: {
675 /* check if we have a valid sAMAccountName */
676 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
677 if (ret != LDB_SUCCESS) return ret;
679 ret = samldb_add_step(ac, samldb_add_entry);
680 if (ret != LDB_SUCCESS) return ret;
681 break;
684 case SAMLDB_TYPE_CLASS: {
685 const struct ldb_val *rdn_value, *def_obj_cat_val;
686 unsigned int v = ldb_msg_find_attr_as_uint(ac->msg, "objectClassCategory", -2);
688 /* As discussed with Microsoft through dochelp in April 2012 this is the behavior of windows*/
689 if (!ldb_msg_find_element(ac->msg, "subClassOf")) {
690 ret = ldb_msg_add_string(ac->msg, "subClassOf", "top");
691 if (ret != LDB_SUCCESS) return ret;
694 ret = samdb_find_or_add_attribute(ldb, ac->msg,
695 "rdnAttId", "cn");
696 if (ret != LDB_SUCCESS) return ret;
698 /* do not allow to mark an attributeSchema as RODC filtered if it
699 * is system-critical */
700 if (check_rodc_critical_attribute(ac->msg)) {
701 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
702 ldb_dn_get_linearized(ac->msg->dn));
703 return LDB_ERR_UNWILLING_TO_PERFORM;
706 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
707 if (rdn_value == NULL) {
708 return ldb_operr(ldb);
710 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
711 /* the RDN has prefix "CN" */
712 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
713 samdb_cn_to_lDAPDisplayName(ac->msg,
714 (const char *) rdn_value->data));
715 if (ret != LDB_SUCCESS) {
716 ldb_oom(ldb);
717 return ret;
721 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
722 struct GUID guid;
723 /* a new GUID */
724 guid = GUID_random();
725 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
726 if (ret != LDB_SUCCESS) {
727 ldb_oom(ldb);
728 return ret;
732 def_obj_cat_val = ldb_msg_find_ldb_val(ac->msg,
733 "defaultObjectCategory");
734 if (def_obj_cat_val != NULL) {
735 /* "defaultObjectCategory" has been set by the caller.
736 * Do some checks for consistency.
737 * NOTE: The real constraint check (that
738 * 'defaultObjectCategory' is the DN of the new
739 * objectclass or any parent of it) is still incomplete.
740 * For now we say that 'defaultObjectCategory' is valid
741 * if it exists and it is of objectclass "classSchema".
743 ac->dn = ldb_dn_from_ldb_val(ac, ldb, def_obj_cat_val);
744 if (ac->dn == NULL) {
745 ldb_set_errstring(ldb,
746 "Invalid DN for 'defaultObjectCategory'!");
747 return LDB_ERR_CONSTRAINT_VIOLATION;
749 } else {
750 /* "defaultObjectCategory" has not been set by the
751 * caller. Use the entry DN for it. */
752 ac->dn = ac->msg->dn;
754 ret = ldb_msg_add_string(ac->msg, "defaultObjectCategory",
755 ldb_dn_alloc_linearized(ac->msg, ac->dn));
756 if (ret != LDB_SUCCESS) {
757 ldb_oom(ldb);
758 return ret;
762 ret = samldb_add_step(ac, samldb_add_entry);
763 if (ret != LDB_SUCCESS) return ret;
765 /* Now perform the checks for the 'defaultObjectCategory'. The
766 * lookup DN was already saved in "ac->dn" */
767 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
768 if (ret != LDB_SUCCESS) return ret;
770 /* -2 is not a valid objectClassCategory so it means the attribute wasn't present */
771 if (v == -2) {
772 /* Windows 2003 does this*/
773 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "objectClassCategory", 0);
774 if (ret != LDB_SUCCESS) {
775 return ret;
778 break;
781 case SAMLDB_TYPE_ATTRIBUTE: {
782 const struct ldb_val *rdn_value;
783 struct ldb_message_element *el;
784 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
785 if (rdn_value == NULL) {
786 return ldb_operr(ldb);
788 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
789 /* the RDN has prefix "CN" */
790 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
791 samdb_cn_to_lDAPDisplayName(ac->msg,
792 (const char *) rdn_value->data));
793 if (ret != LDB_SUCCESS) {
794 ldb_oom(ldb);
795 return ret;
799 /* do not allow to mark an attributeSchema as RODC filtered if it
800 * is system-critical */
801 if (check_rodc_critical_attribute(ac->msg)) {
802 ldb_asprintf_errstring(ldb,
803 "samldb: refusing schema add of %s - cannot combine critical attribute with RODC filtering",
804 ldb_dn_get_linearized(ac->msg->dn));
805 return LDB_ERR_UNWILLING_TO_PERFORM;
808 ret = samdb_find_or_add_attribute(ldb, ac->msg,
809 "isSingleValued", "FALSE");
810 if (ret != LDB_SUCCESS) return ret;
812 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
813 struct GUID guid;
814 /* a new GUID */
815 guid = GUID_random();
816 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
817 if (ret != LDB_SUCCESS) {
818 ldb_oom(ldb);
819 return ret;
823 el = ldb_msg_find_element(ac->msg, "attributeSyntax");
824 if (el) {
826 * No need to scream if there isn't as we have code later on
827 * that will take care of it.
829 const struct dsdb_syntax *syntax = find_syntax_map_by_ad_oid((const char *)el->values[0].data);
830 if (!syntax) {
831 DEBUG(9, ("Can't find dsdb_syntax object for attributeSyntax %s\n",
832 (const char *)el->values[0].data));
833 } else {
834 unsigned int v = ldb_msg_find_attr_as_uint(ac->msg, "oMSyntax", 0);
835 const struct ldb_val *val = ldb_msg_find_ldb_val(ac->msg, "oMObjectClass");
837 if (v == 0) {
838 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "oMSyntax", syntax->oMSyntax);
839 if (ret != LDB_SUCCESS) {
840 return ret;
843 if (!val) {
844 struct ldb_val val2 = ldb_val_dup(ldb, &syntax->oMObjectClass);
845 if (val2.length > 0) {
846 ret = ldb_msg_add_value(ac->msg, "oMObjectClass", &val2, NULL);
847 if (ret != LDB_SUCCESS) {
848 return ret;
855 /* handle msDS-IntID attribute */
856 ret = samldb_add_handle_msDS_IntId(ac);
857 if (ret != LDB_SUCCESS) return ret;
859 ret = samldb_add_step(ac, samldb_add_entry);
860 if (ret != LDB_SUCCESS) return ret;
861 break;
864 default:
865 ldb_asprintf_errstring(ldb, "Invalid entry type!");
866 return LDB_ERR_OPERATIONS_ERROR;
867 break;
870 return samldb_first_step(ac);
873 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
875 struct ldb_context *ldb;
876 const struct ldb_val *rdn_value;
877 struct dom_sid *sid;
878 int ret;
880 ldb = ldb_module_get_ctx(ac->module);
882 sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
883 if (sid == NULL) {
884 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
885 if (rdn_value == NULL) {
886 return ldb_operr(ldb);
888 sid = dom_sid_parse_talloc(ac->msg,
889 (const char *)rdn_value->data);
890 if (sid == NULL) {
891 ldb_set_errstring(ldb,
892 "samldb: No valid SID found in ForeignSecurityPrincipal CN!");
893 return LDB_ERR_CONSTRAINT_VIOLATION;
895 if (! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
896 return ldb_operr(ldb);
900 /* finally proceed with adding the entry */
901 ret = samldb_add_step(ac, samldb_add_entry);
902 if (ret != LDB_SUCCESS) return ret;
904 return samldb_first_step(ac);
907 static int samldb_schema_info_update(struct samldb_ctx *ac)
909 int ret;
910 struct ldb_context *ldb;
911 struct dsdb_schema *schema;
913 /* replicated update should always go through */
914 if (ldb_request_get_control(ac->req,
915 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
916 return LDB_SUCCESS;
919 /* do not update schemaInfo during provisioning */
920 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
921 return LDB_SUCCESS;
924 ldb = ldb_module_get_ctx(ac->module);
925 schema = dsdb_get_schema(ldb, NULL);
926 if (!schema) {
927 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
928 "samldb_schema_info_update: no dsdb_schema loaded");
929 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
930 return ldb_operr(ldb);
933 ret = dsdb_module_schema_info_update(ac->module, schema,
934 DSDB_FLAG_NEXT_MODULE|
935 DSDB_FLAG_AS_SYSTEM,
936 ac->req);
937 if (ret != LDB_SUCCESS) {
938 ldb_asprintf_errstring(ldb,
939 "samldb_schema_info_update: dsdb_module_schema_info_update failed with %s",
940 ldb_errstring(ldb));
941 return ret;
944 return LDB_SUCCESS;
947 static int samldb_prim_group_tester(struct samldb_ctx *ac, uint32_t rid);
948 static int samldb_check_user_account_control_rules(struct samldb_ctx *ac,
949 struct dom_sid *sid,
950 uint32_t user_account_control,
951 uint32_t user_account_control_old);
954 * "Objectclass" trigger (MS-SAMR 3.1.1.8.1)
956 * Has to be invoked on "add" and "modify" operations on "user", "computer" and
957 * "group" objects.
958 * ac->msg contains the "add"/"modify" message
959 * ac->type contains the object type (main objectclass)
961 static int samldb_objectclass_trigger(struct samldb_ctx *ac)
963 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
964 void *skip_allocate_sids = ldb_get_opaque(ldb,
965 "skip_allocate_sids");
966 struct ldb_message_element *el, *el2;
967 struct dom_sid *sid;
968 int ret;
970 /* make sure that "sAMAccountType" is not specified */
971 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
972 if (el != NULL) {
973 ldb_set_errstring(ldb,
974 "samldb: sAMAccountType must not be specified!");
975 return LDB_ERR_UNWILLING_TO_PERFORM;
978 /* Step 1: objectSid assignment */
980 /* Don't allow the objectSid to be changed. But beside the RELAX
981 * control we have also to guarantee that it can always be set with
982 * SYSTEM permissions. This is needed for the "samba3sam" backend. */
983 sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
984 if ((sid != NULL) && (!dsdb_module_am_system(ac->module)) &&
985 (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
986 ldb_set_errstring(ldb,
987 "samldb: objectSid must not be specified!");
988 return LDB_ERR_UNWILLING_TO_PERFORM;
991 /* but generate a new SID when we do have an add operations */
992 if ((sid == NULL) && (ac->req->operation == LDB_ADD) && !skip_allocate_sids) {
993 ret = samldb_add_step(ac, samldb_allocate_sid);
994 if (ret != LDB_SUCCESS) return ret;
997 switch(ac->type) {
998 case SAMLDB_TYPE_USER: {
999 bool uac_generated = false, uac_add_flags = false;
1001 /* Step 1.2: Default values */
1002 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1003 "accountExpires", "9223372036854775807");
1004 if (ret != LDB_SUCCESS) return ret;
1005 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1006 "badPasswordTime", "0");
1007 if (ret != LDB_SUCCESS) return ret;
1008 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1009 "badPwdCount", "0");
1010 if (ret != LDB_SUCCESS) return ret;
1011 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1012 "codePage", "0");
1013 if (ret != LDB_SUCCESS) return ret;
1014 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1015 "countryCode", "0");
1016 if (ret != LDB_SUCCESS) return ret;
1017 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1018 "lastLogoff", "0");
1019 if (ret != LDB_SUCCESS) return ret;
1020 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1021 "lastLogon", "0");
1022 if (ret != LDB_SUCCESS) return ret;
1023 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1024 "logonCount", "0");
1025 if (ret != LDB_SUCCESS) return ret;
1026 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1027 "pwdLastSet", "0");
1028 if (ret != LDB_SUCCESS) return ret;
1030 /* On add operations we might need to generate a
1031 * "userAccountControl" (if it isn't specified). */
1032 el = ldb_msg_find_element(ac->msg, "userAccountControl");
1033 if ((el == NULL) && (ac->req->operation == LDB_ADD)) {
1034 ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
1035 "userAccountControl",
1036 UF_NORMAL_ACCOUNT);
1037 if (ret != LDB_SUCCESS) {
1038 return ret;
1040 uac_generated = true;
1041 uac_add_flags = true;
1044 el = ldb_msg_find_element(ac->msg, "userAccountControl");
1045 if (el != NULL) {
1046 uint32_t user_account_control, account_type;
1047 /* Step 1.3: "userAccountControl" -> "sAMAccountType" mapping */
1048 user_account_control = ldb_msg_find_attr_as_uint(ac->msg,
1049 "userAccountControl",
1052 * "userAccountControl" = 0 or missing one of
1053 * the types means "UF_NORMAL_ACCOUNT". See
1054 * MS-SAMR 3.1.1.8.10 point 8
1056 if ((user_account_control & UF_ACCOUNT_TYPE_MASK) == 0) {
1057 user_account_control = UF_NORMAL_ACCOUNT | user_account_control;
1058 uac_generated = true;
1062 * As per MS-SAMR 3.1.1.8.10 these flags have not to be set
1064 if ((user_account_control & UF_LOCKOUT) != 0) {
1065 user_account_control &= ~UF_LOCKOUT;
1066 uac_generated = true;
1068 if ((user_account_control & UF_PASSWORD_EXPIRED) != 0) {
1069 user_account_control &= ~UF_PASSWORD_EXPIRED;
1070 uac_generated = true;
1073 ret = samldb_check_user_account_control_rules(ac, NULL,
1074 user_account_control, 0);
1075 if (ret != LDB_SUCCESS) {
1076 return ret;
1079 /* Workstation and (read-only) DC objects do need objectclass "computer" */
1080 if ((samdb_find_attribute(ldb, ac->msg,
1081 "objectclass", "computer") == NULL) &&
1082 (user_account_control &
1083 (UF_SERVER_TRUST_ACCOUNT | UF_WORKSTATION_TRUST_ACCOUNT))) {
1084 ldb_set_errstring(ldb,
1085 "samldb: Requested account type does need objectclass 'computer'!");
1086 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1089 account_type = ds_uf2atype(user_account_control);
1090 if (account_type == 0) {
1091 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1092 return LDB_ERR_UNWILLING_TO_PERFORM;
1094 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1095 "sAMAccountType",
1096 account_type);
1097 if (ret != LDB_SUCCESS) {
1098 return ret;
1100 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
1101 el2->flags = LDB_FLAG_MOD_REPLACE;
1103 /* "isCriticalSystemObject" might be set */
1104 if (user_account_control &
1105 (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
1106 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1107 "TRUE");
1108 if (ret != LDB_SUCCESS) {
1109 return ret;
1111 el2 = ldb_msg_find_element(ac->msg,
1112 "isCriticalSystemObject");
1113 el2->flags = LDB_FLAG_MOD_REPLACE;
1114 } else if (user_account_control & UF_WORKSTATION_TRUST_ACCOUNT) {
1115 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1116 "FALSE");
1117 if (ret != LDB_SUCCESS) {
1118 return ret;
1120 el2 = ldb_msg_find_element(ac->msg,
1121 "isCriticalSystemObject");
1122 el2->flags = LDB_FLAG_MOD_REPLACE;
1125 /* Step 1.4: "userAccountControl" -> "primaryGroupID" mapping */
1126 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
1127 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
1130 * Older AD deployments don't know about the
1131 * RODC group
1133 if (rid == DOMAIN_RID_READONLY_DCS) {
1134 ret = samldb_prim_group_tester(ac, rid);
1135 if (ret != LDB_SUCCESS) {
1136 return ret;
1140 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1141 "primaryGroupID", rid);
1142 if (ret != LDB_SUCCESS) {
1143 return ret;
1145 el2 = ldb_msg_find_element(ac->msg,
1146 "primaryGroupID");
1147 el2->flags = LDB_FLAG_MOD_REPLACE;
1150 /* Step 1.5: Add additional flags when needed */
1151 /* Obviously this is done when the "userAccountControl"
1152 * has been generated here (tested against Windows
1153 * Server) */
1154 if (uac_generated) {
1155 if (uac_add_flags) {
1156 user_account_control |= UF_ACCOUNTDISABLE;
1157 user_account_control |= UF_PASSWD_NOTREQD;
1160 ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
1161 "userAccountControl",
1162 user_account_control);
1163 if (ret != LDB_SUCCESS) {
1164 return ret;
1169 break;
1172 case SAMLDB_TYPE_GROUP: {
1173 const char *tempstr;
1175 /* Step 2.2: Default values */
1176 tempstr = talloc_asprintf(ac->msg, "%d",
1177 GTYPE_SECURITY_GLOBAL_GROUP);
1178 if (tempstr == NULL) return ldb_operr(ldb);
1179 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1180 "groupType", tempstr);
1181 if (ret != LDB_SUCCESS) return ret;
1183 /* Step 2.3: "groupType" -> "sAMAccountType" */
1184 el = ldb_msg_find_element(ac->msg, "groupType");
1185 if (el != NULL) {
1186 uint32_t group_type, account_type;
1188 group_type = ldb_msg_find_attr_as_uint(ac->msg,
1189 "groupType", 0);
1191 /* The creation of builtin groups requires the
1192 * RELAX control */
1193 if (group_type == GTYPE_SECURITY_BUILTIN_LOCAL_GROUP) {
1194 if (ldb_request_get_control(ac->req,
1195 LDB_CONTROL_RELAX_OID) == NULL) {
1196 return LDB_ERR_UNWILLING_TO_PERFORM;
1200 account_type = ds_gtype2atype(group_type);
1201 if (account_type == 0) {
1202 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1203 return LDB_ERR_UNWILLING_TO_PERFORM;
1205 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1206 "sAMAccountType",
1207 account_type);
1208 if (ret != LDB_SUCCESS) {
1209 return ret;
1211 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
1212 el2->flags = LDB_FLAG_MOD_REPLACE;
1214 break;
1217 default:
1218 ldb_asprintf_errstring(ldb,
1219 "Invalid entry type!");
1220 return LDB_ERR_OPERATIONS_ERROR;
1221 break;
1224 return LDB_SUCCESS;
1228 * "Primary group ID" trigger (MS-SAMR 3.1.1.8.2)
1230 * Has to be invoked on "add" and "modify" operations on "user" and "computer"
1231 * objects.
1232 * ac->msg contains the "add"/"modify" message
1235 static int samldb_prim_group_tester(struct samldb_ctx *ac, uint32_t rid)
1237 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1238 struct dom_sid *sid;
1239 struct ldb_result *res;
1240 int ret;
1241 const char * const noattrs[] = { NULL };
1243 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1244 if (sid == NULL) {
1245 return ldb_operr(ldb);
1248 ret = dsdb_module_search(ac->module, ac, &res,
1249 ldb_get_default_basedn(ldb),
1250 LDB_SCOPE_SUBTREE,
1251 noattrs, DSDB_FLAG_NEXT_MODULE,
1252 ac->req,
1253 "(objectSid=%s)",
1254 ldap_encode_ndr_dom_sid(ac, sid));
1255 if (ret != LDB_SUCCESS) {
1256 return ret;
1258 if (res->count != 1) {
1259 talloc_free(res);
1260 ldb_asprintf_errstring(ldb,
1261 "Failed to find primary group with RID %u!",
1262 rid);
1263 return LDB_ERR_UNWILLING_TO_PERFORM;
1265 talloc_free(res);
1267 return LDB_SUCCESS;
1270 static int samldb_prim_group_set(struct samldb_ctx *ac)
1272 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1273 uint32_t rid;
1275 rid = ldb_msg_find_attr_as_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
1276 if (rid == (uint32_t) -1) {
1277 /* we aren't affected of any primary group set */
1278 return LDB_SUCCESS;
1280 } else if (!ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
1281 ldb_set_errstring(ldb,
1282 "The primary group isn't settable on add operations!");
1283 return LDB_ERR_UNWILLING_TO_PERFORM;
1286 return samldb_prim_group_tester(ac, rid);
1289 static int samldb_prim_group_change(struct samldb_ctx *ac)
1291 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1292 const char * const attrs[] = {
1293 "primaryGroupID",
1294 "memberOf",
1295 "userAccountControl",
1296 NULL };
1297 struct ldb_result *res, *group_res;
1298 struct ldb_message_element *el;
1299 struct ldb_message *msg;
1300 uint32_t prev_rid, new_rid, uac;
1301 struct dom_sid *prev_sid, *new_sid;
1302 struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
1303 int ret;
1304 const char * const noattrs[] = { NULL };
1306 el = dsdb_get_single_valued_attr(ac->msg, "primaryGroupID",
1307 ac->req->operation);
1308 if (el == NULL) {
1309 /* we are not affected */
1310 return LDB_SUCCESS;
1313 /* Fetch information from the existing object */
1315 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1316 DSDB_FLAG_NEXT_MODULE, ac->req);
1317 if (ret != LDB_SUCCESS) {
1318 return ret;
1321 uac = ldb_msg_find_attr_as_uint(res->msgs[0], "userAccountControl", 0);
1323 /* Finds out the DN of the old primary group */
1325 prev_rid = ldb_msg_find_attr_as_uint(res->msgs[0], "primaryGroupID",
1326 (uint32_t) -1);
1327 if (prev_rid == (uint32_t) -1) {
1328 /* User objects do always have a mandatory "primaryGroupID"
1329 * attribute. If this doesn't exist then the object is of the
1330 * wrong type. This is the exact Windows error code */
1331 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1334 prev_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), prev_rid);
1335 if (prev_sid == NULL) {
1336 return ldb_operr(ldb);
1339 /* Finds out the DN of the new primary group
1340 * Notice: in order to parse the primary group ID correctly we create
1341 * a temporary message here. */
1343 msg = ldb_msg_new(ac->msg);
1344 if (msg == NULL) {
1345 return ldb_module_oom(ac->module);
1347 ret = ldb_msg_add(msg, el, 0);
1348 if (ret != LDB_SUCCESS) {
1349 return ret;
1351 new_rid = ldb_msg_find_attr_as_uint(msg, "primaryGroupID", (uint32_t) -1);
1352 talloc_free(msg);
1353 if (new_rid == (uint32_t) -1) {
1354 /* we aren't affected of any primary group change */
1355 return LDB_SUCCESS;
1358 if (prev_rid == new_rid) {
1359 return LDB_SUCCESS;
1362 if ((uac & UF_SERVER_TRUST_ACCOUNT) && new_rid != DOMAIN_RID_DCS) {
1363 ldb_asprintf_errstring(ldb,
1364 "%08X: samldb: UF_SERVER_TRUST_ACCOUNT requires "
1365 "primaryGroupID=%u!",
1366 W_ERROR_V(WERR_DS_CANT_MOD_PRIMARYGROUPID),
1367 DOMAIN_RID_DCS);
1368 return LDB_ERR_UNWILLING_TO_PERFORM;
1371 if ((uac & UF_PARTIAL_SECRETS_ACCOUNT) && new_rid != DOMAIN_RID_READONLY_DCS) {
1372 ldb_asprintf_errstring(ldb,
1373 "%08X: samldb: UF_PARTIAL_SECRETS_ACCOUNT requires "
1374 "primaryGroupID=%u!",
1375 W_ERROR_V(WERR_DS_CANT_MOD_PRIMARYGROUPID),
1376 DOMAIN_RID_READONLY_DCS);
1377 return LDB_ERR_UNWILLING_TO_PERFORM;
1380 ret = dsdb_module_search(ac->module, ac, &group_res,
1381 ldb_get_default_basedn(ldb),
1382 LDB_SCOPE_SUBTREE,
1383 noattrs, DSDB_FLAG_NEXT_MODULE,
1384 ac->req,
1385 "(objectSid=%s)",
1386 ldap_encode_ndr_dom_sid(ac, prev_sid));
1387 if (ret != LDB_SUCCESS) {
1388 return ret;
1390 if (group_res->count != 1) {
1391 return ldb_operr(ldb);
1393 prev_prim_group_dn = group_res->msgs[0]->dn;
1395 new_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), new_rid);
1396 if (new_sid == NULL) {
1397 return ldb_operr(ldb);
1400 ret = dsdb_module_search(ac->module, ac, &group_res,
1401 ldb_get_default_basedn(ldb),
1402 LDB_SCOPE_SUBTREE,
1403 noattrs, DSDB_FLAG_NEXT_MODULE,
1404 ac->req,
1405 "(objectSid=%s)",
1406 ldap_encode_ndr_dom_sid(ac, new_sid));
1407 if (ret != LDB_SUCCESS) {
1408 return ret;
1410 if (group_res->count != 1) {
1411 /* Here we know if the specified new primary group candidate is
1412 * valid or not. */
1413 return LDB_ERR_UNWILLING_TO_PERFORM;
1415 new_prim_group_dn = group_res->msgs[0]->dn;
1417 /* We need to be already a normal member of the new primary
1418 * group in order to be successful. */
1419 el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
1420 ldb_dn_get_linearized(new_prim_group_dn));
1421 if (el == NULL) {
1422 return LDB_ERR_UNWILLING_TO_PERFORM;
1425 /* Remove the "member" attribute on the new primary group */
1426 msg = ldb_msg_new(ac->msg);
1427 if (msg == NULL) {
1428 return ldb_module_oom(ac->module);
1430 msg->dn = new_prim_group_dn;
1432 ret = samdb_msg_add_delval(ldb, msg, msg, "member",
1433 ldb_dn_get_linearized(ac->msg->dn));
1434 if (ret != LDB_SUCCESS) {
1435 return ret;
1438 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE, ac->req);
1439 if (ret != LDB_SUCCESS) {
1440 return ret;
1442 talloc_free(msg);
1444 /* Add a "member" attribute for the previous primary group */
1445 msg = ldb_msg_new(ac->msg);
1446 if (msg == NULL) {
1447 return ldb_module_oom(ac->module);
1449 msg->dn = prev_prim_group_dn;
1451 ret = samdb_msg_add_addval(ldb, msg, msg, "member",
1452 ldb_dn_get_linearized(ac->msg->dn));
1453 if (ret != LDB_SUCCESS) {
1454 return ret;
1457 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE, ac->req);
1458 if (ret != LDB_SUCCESS) {
1459 return ret;
1461 talloc_free(msg);
1463 return LDB_SUCCESS;
1466 static int samldb_prim_group_trigger(struct samldb_ctx *ac)
1468 int ret;
1470 if (ac->req->operation == LDB_ADD) {
1471 ret = samldb_prim_group_set(ac);
1472 } else {
1473 ret = samldb_prim_group_change(ac);
1476 return ret;
1479 static int samldb_check_user_account_control_invariants(struct samldb_ctx *ac,
1480 uint32_t user_account_control)
1482 int i, ret = 0;
1483 bool need_check = false;
1484 const struct uac_to_guid {
1485 uint32_t uac;
1486 bool never;
1487 uint32_t needs;
1488 uint32_t not_with;
1489 const char *error_string;
1490 } map[] = {
1492 .uac = UF_TEMP_DUPLICATE_ACCOUNT,
1493 .never = true,
1494 .error_string = "Updating the UF_TEMP_DUPLICATE_ACCOUNT flag is never allowed"
1497 .uac = UF_PARTIAL_SECRETS_ACCOUNT,
1498 .needs = UF_WORKSTATION_TRUST_ACCOUNT,
1499 .error_string = "Setting UF_PARTIAL_SECRETS_ACCOUNT only permitted with UF_WORKSTATION_TRUST_ACCOUNT"
1502 .uac = UF_TRUSTED_FOR_DELEGATION,
1503 .not_with = UF_PARTIAL_SECRETS_ACCOUNT,
1504 .error_string = "Setting UF_TRUSTED_FOR_DELEGATION not allowed with UF_PARTIAL_SECRETS_ACCOUNT"
1507 .uac = UF_NORMAL_ACCOUNT,
1508 .not_with = UF_ACCOUNT_TYPE_MASK & ~UF_NORMAL_ACCOUNT,
1509 .error_string = "Setting more than one account type not permitted"
1512 .uac = UF_WORKSTATION_TRUST_ACCOUNT,
1513 .not_with = UF_ACCOUNT_TYPE_MASK & ~UF_WORKSTATION_TRUST_ACCOUNT,
1514 .error_string = "Setting more than one account type not permitted"
1517 .uac = UF_INTERDOMAIN_TRUST_ACCOUNT,
1518 .not_with = UF_ACCOUNT_TYPE_MASK & ~UF_INTERDOMAIN_TRUST_ACCOUNT,
1519 .error_string = "Setting more than one account type not permitted"
1522 .uac = UF_SERVER_TRUST_ACCOUNT,
1523 .not_with = UF_ACCOUNT_TYPE_MASK & ~UF_SERVER_TRUST_ACCOUNT,
1524 .error_string = "Setting more than one account type not permitted"
1527 .uac = UF_TRUSTED_FOR_DELEGATION,
1528 .not_with = UF_PARTIAL_SECRETS_ACCOUNT,
1529 .error_string = "Setting UF_TRUSTED_FOR_DELEGATION not allowed with UF_PARTIAL_SECRETS_ACCOUNT"
1533 for (i = 0; i < ARRAY_SIZE(map); i++) {
1534 if (user_account_control & map[i].uac) {
1535 need_check = true;
1536 break;
1539 if (need_check == false) {
1540 return LDB_SUCCESS;
1543 for (i = 0; i < ARRAY_SIZE(map); i++) {
1544 uint32_t this_uac = user_account_control & map[i].uac;
1545 if (this_uac != 0) {
1546 if (map[i].never) {
1547 ret = LDB_ERR_OTHER;
1548 break;
1549 } else if (map[i].needs != 0) {
1550 if ((map[i].needs & user_account_control) == 0) {
1551 ret = LDB_ERR_OTHER;
1552 break;
1554 } else if (map[i].not_with != 0) {
1555 if ((map[i].not_with & user_account_control) != 0) {
1556 ret = LDB_ERR_OTHER;
1557 break;
1562 if (ret != LDB_SUCCESS) {
1563 switch (ac->req->operation) {
1564 case LDB_ADD:
1565 ldb_asprintf_errstring(ldb_module_get_ctx(ac->module),
1566 "Failed to add %s: %s",
1567 ldb_dn_get_linearized(ac->msg->dn),
1568 map[i].error_string);
1569 break;
1570 case LDB_MODIFY:
1571 ldb_asprintf_errstring(ldb_module_get_ctx(ac->module),
1572 "Failed to modify %s: %s",
1573 ldb_dn_get_linearized(ac->msg->dn),
1574 map[i].error_string);
1575 break;
1576 default:
1577 return ldb_module_operr(ac->module);
1580 return ret;
1584 * Validate that the restriction in point 5 of MS-SAMR 3.1.1.8.10 userAccountControl is honoured
1587 static int samldb_check_user_account_control_acl(struct samldb_ctx *ac,
1588 struct dom_sid *sid,
1589 uint32_t user_account_control,
1590 uint32_t user_account_control_old)
1592 int i, ret = 0;
1593 bool need_acl_check = false;
1594 struct ldb_result *res;
1595 const char * const sd_attrs[] = {"ntSecurityDescriptor", NULL};
1596 struct security_token *user_token;
1597 struct security_descriptor *domain_sd;
1598 struct ldb_dn *domain_dn = ldb_get_default_basedn(ldb_module_get_ctx(ac->module));
1599 const struct uac_to_guid {
1600 uint32_t uac;
1601 const char *oid;
1602 const char *guid;
1603 enum sec_privilege privilege;
1604 bool delete_is_privileged;
1605 const char *error_string;
1606 } map[] = {
1608 .uac = UF_PASSWD_NOTREQD,
1609 .guid = GUID_DRS_UPDATE_PASSWORD_NOT_REQUIRED_BIT,
1610 .error_string = "Adding the UF_PASSWD_NOTREQD bit in userAccountControl requires the Update-Password-Not-Required-Bit right that was not given on the Domain object"
1613 .uac = UF_DONT_EXPIRE_PASSWD,
1614 .guid = GUID_DRS_UNEXPIRE_PASSWORD,
1615 .error_string = "Adding the UF_DONT_EXPIRE_PASSWD bit in userAccountControl requires the Unexpire-Password right that was not given on the Domain object"
1618 .uac = UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED,
1619 .guid = GUID_DRS_ENABLE_PER_USER_REVERSIBLY_ENCRYPTED_PASSWORD,
1620 .error_string = "Adding the UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED bit in userAccountControl requires the Enable-Per-User-Reversibly-Encrypted-Password right that was not given on the Domain object"
1623 .uac = UF_SERVER_TRUST_ACCOUNT,
1624 .guid = GUID_DRS_DS_INSTALL_REPLICA,
1625 .error_string = "Adding the UF_SERVER_TRUST_ACCOUNT bit in userAccountControl requires the DS-Install-Replica right that was not given on the Domain object"
1628 .uac = UF_PARTIAL_SECRETS_ACCOUNT,
1629 .guid = GUID_DRS_DS_INSTALL_REPLICA,
1630 .error_string = "Adding the UF_PARTIAL_SECRETS_ACCOUNT bit in userAccountControl requires the DS-Install-Replica right that was not given on the Domain object"
1633 .uac = UF_INTERDOMAIN_TRUST_ACCOUNT,
1634 .oid = DSDB_CONTROL_PERMIT_INTERDOMAIN_TRUST_UAC_OID,
1635 .error_string = "Updating the UF_INTERDOMAIN_TRUST_ACCOUNT bit in userAccountControl is not permitted over LDAP. This bit is restricted to the LSA CreateTrustedDomain interface",
1636 .delete_is_privileged = true
1639 .uac = UF_TRUSTED_FOR_DELEGATION,
1640 .privilege = SEC_PRIV_ENABLE_DELEGATION,
1641 .delete_is_privileged = true,
1642 .error_string = "Updating the UF_TRUSTED_FOR_DELEGATION bit in userAccountControl is not permitted without the SeEnableDelegationPrivilege"
1645 .uac = UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION,
1646 .privilege = SEC_PRIV_ENABLE_DELEGATION,
1647 .delete_is_privileged = true,
1648 .error_string = "Updating the UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION bit in userAccountControl is not permitted without the SeEnableDelegationPrivilege"
1653 if (dsdb_module_am_system(ac->module)) {
1654 return LDB_SUCCESS;
1657 for (i = 0; i < ARRAY_SIZE(map); i++) {
1658 if (user_account_control & map[i].uac) {
1659 need_acl_check = true;
1660 break;
1663 if (need_acl_check == false) {
1664 return LDB_SUCCESS;
1667 user_token = acl_user_token(ac->module);
1668 if (user_token == NULL) {
1669 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1672 ret = dsdb_module_search_dn(ac->module, ac, &res,
1673 domain_dn,
1674 sd_attrs,
1675 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED,
1676 ac->req);
1677 if (ret != LDB_SUCCESS) {
1678 return ret;
1680 if (res->count != 1) {
1681 return ldb_module_operr(ac->module);
1684 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(ac->module),
1685 ac, res->msgs[0], &domain_sd);
1687 if (ret != LDB_SUCCESS) {
1688 return ret;
1691 for (i = 0; i < ARRAY_SIZE(map); i++) {
1692 uint32_t this_uac_new = user_account_control & map[i].uac;
1693 uint32_t this_uac_old = user_account_control_old & map[i].uac;
1694 if (this_uac_new != this_uac_old) {
1695 if (this_uac_old != 0) {
1696 if (map[i].delete_is_privileged == false) {
1697 continue;
1700 if (map[i].oid) {
1701 struct ldb_control *control = ldb_request_get_control(ac->req, map[i].oid);
1702 if (control == NULL) {
1703 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1705 } else if (map[i].privilege != SEC_PRIV_INVALID) {
1706 bool have_priv = security_token_has_privilege(user_token,
1707 map[i].privilege);
1708 if (have_priv == false) {
1709 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1711 } else {
1712 ret = acl_check_extended_right(ac, domain_sd,
1713 user_token,
1714 map[i].guid,
1715 SEC_ADS_CONTROL_ACCESS,
1716 sid);
1718 if (ret != LDB_SUCCESS) {
1719 break;
1723 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
1724 switch (ac->req->operation) {
1725 case LDB_ADD:
1726 ldb_asprintf_errstring(ldb_module_get_ctx(ac->module),
1727 "Failed to add %s: %s",
1728 ldb_dn_get_linearized(ac->msg->dn),
1729 map[i].error_string);
1730 break;
1731 case LDB_MODIFY:
1732 ldb_asprintf_errstring(ldb_module_get_ctx(ac->module),
1733 "Failed to modify %s: %s",
1734 ldb_dn_get_linearized(ac->msg->dn),
1735 map[i].error_string);
1736 break;
1737 default:
1738 return ldb_module_operr(ac->module);
1740 if (map[i].guid) {
1741 dsdb_acl_debug(domain_sd, acl_user_token(ac->module),
1742 domain_dn,
1743 true,
1744 10);
1747 return ret;
1750 static int samldb_check_user_account_control_rules(struct samldb_ctx *ac,
1751 struct dom_sid *sid,
1752 uint32_t user_account_control,
1753 uint32_t user_account_control_old)
1755 int ret;
1756 ret = samldb_check_user_account_control_invariants(ac, user_account_control);
1757 if (ret != LDB_SUCCESS) {
1758 return ret;
1760 ret = samldb_check_user_account_control_acl(ac, sid, user_account_control, user_account_control_old);
1761 if (ret != LDB_SUCCESS) {
1762 return ret;
1764 return ret;
1769 * This function is called on LDB modify operations. It performs some additions/
1770 * replaces on the current LDB message when "userAccountControl" changes.
1772 static int samldb_user_account_control_change(struct samldb_ctx *ac)
1774 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1775 uint32_t old_uac;
1776 uint32_t new_uac;
1777 uint32_t raw_uac;
1778 uint32_t old_ufa;
1779 uint32_t new_ufa;
1780 uint32_t old_uac_computed;
1781 uint32_t clear_uac;
1782 uint32_t old_atype;
1783 uint32_t new_atype;
1784 uint32_t old_pgrid;
1785 uint32_t new_pgrid;
1786 NTTIME old_lockoutTime;
1787 struct ldb_message_element *el;
1788 struct ldb_val *val;
1789 struct ldb_val computer_val;
1790 struct ldb_message *tmp_msg;
1791 struct dom_sid *sid;
1792 int ret;
1793 struct ldb_result *res;
1794 const char * const attrs[] = {
1795 "objectClass",
1796 "isCriticalSystemObject",
1797 "userAccountControl",
1798 "msDS-User-Account-Control-Computed",
1799 "lockoutTime",
1800 "objectSid",
1801 NULL
1803 bool is_computer = false;
1804 bool old_is_critical = false;
1805 bool new_is_critical = false;
1807 el = dsdb_get_single_valued_attr(ac->msg, "userAccountControl",
1808 ac->req->operation);
1809 if (el == NULL || el->num_values == 0) {
1810 ldb_asprintf_errstring(ldb,
1811 "%08X: samldb: 'userAccountControl' can't be deleted!",
1812 W_ERROR_V(WERR_DS_ILLEGAL_MOD_OPERATION));
1813 return LDB_ERR_UNWILLING_TO_PERFORM;
1816 /* Create a temporary message for fetching the "userAccountControl" */
1817 tmp_msg = ldb_msg_new(ac->msg);
1818 if (tmp_msg == NULL) {
1819 return ldb_module_oom(ac->module);
1821 ret = ldb_msg_add(tmp_msg, el, 0);
1822 if (ret != LDB_SUCCESS) {
1823 return ret;
1825 raw_uac = ldb_msg_find_attr_as_uint(tmp_msg,
1826 "userAccountControl",
1828 talloc_free(tmp_msg);
1830 * UF_LOCKOUT, UF_PASSWD_CANT_CHANGE and UF_PASSWORD_EXPIRED
1831 * are only generated and not stored. We ignore them almost
1832 * completely, along with unknown bits and UF_SCRIPT.
1834 * The only exception is ACB_AUTOLOCK, which features in
1835 * clear_acb when the bit is cleared in this modify operation.
1837 * MS-SAMR 2.2.1.13 UF_FLAG Codes states that some bits are
1838 * ignored by clients and servers
1840 new_uac = raw_uac & UF_SETTABLE_BITS;
1842 /* Fetch the old "userAccountControl" and "objectClass" */
1843 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1844 DSDB_FLAG_NEXT_MODULE, ac->req);
1845 if (ret != LDB_SUCCESS) {
1846 return ret;
1848 old_uac = ldb_msg_find_attr_as_uint(res->msgs[0], "userAccountControl", 0);
1849 if (old_uac == 0) {
1850 return ldb_operr(ldb);
1852 old_uac_computed = ldb_msg_find_attr_as_uint(res->msgs[0],
1853 "msDS-User-Account-Control-Computed", 0);
1854 old_lockoutTime = ldb_msg_find_attr_as_int64(res->msgs[0],
1855 "lockoutTime", 0);
1856 old_is_critical = ldb_msg_find_attr_as_bool(res->msgs[0],
1857 "isCriticalSystemObject", 0);
1858 /* When we do not have objectclass "computer" we cannot switch to a (read-only) DC */
1859 el = ldb_msg_find_element(res->msgs[0], "objectClass");
1860 if (el == NULL) {
1861 return ldb_operr(ldb);
1863 computer_val = data_blob_string_const("computer");
1864 val = ldb_msg_find_val(el, &computer_val);
1865 if (val != NULL) {
1866 is_computer = true;
1869 old_ufa = old_uac & UF_ACCOUNT_TYPE_MASK;
1870 old_atype = ds_uf2atype(old_ufa);
1871 old_pgrid = ds_uf2prim_group_rid(old_uac);
1873 new_ufa = new_uac & UF_ACCOUNT_TYPE_MASK;
1874 if (new_ufa == 0) {
1876 * "userAccountControl" = 0 or missing one of the
1877 * types means "UF_NORMAL_ACCOUNT". See MS-SAMR
1878 * 3.1.1.8.10 point 8
1880 new_ufa = UF_NORMAL_ACCOUNT;
1881 new_uac |= new_ufa;
1883 sid = samdb_result_dom_sid(res, res->msgs[0], "objectSid");
1884 if (sid == NULL) {
1885 return ldb_module_operr(ac->module);
1888 ret = samldb_check_user_account_control_rules(ac, sid, new_uac, old_uac);
1889 if (ret != LDB_SUCCESS) {
1890 return ret;
1893 new_atype = ds_uf2atype(new_ufa);
1894 new_pgrid = ds_uf2prim_group_rid(new_uac);
1896 clear_uac = (old_uac | old_uac_computed) & ~raw_uac;
1898 switch (new_ufa) {
1899 case UF_NORMAL_ACCOUNT:
1900 new_is_critical = old_is_critical;
1901 break;
1903 case UF_INTERDOMAIN_TRUST_ACCOUNT:
1904 new_is_critical = true;
1905 break;
1907 case UF_WORKSTATION_TRUST_ACCOUNT:
1908 new_is_critical = false;
1909 if (new_uac & UF_PARTIAL_SECRETS_ACCOUNT) {
1910 if (!is_computer) {
1911 ldb_asprintf_errstring(ldb,
1912 "%08X: samldb: UF_PARTIAL_SECRETS_ACCOUNT "
1913 "requires objectclass 'computer'!",
1914 W_ERROR_V(WERR_DS_MACHINE_ACCOUNT_CREATED_PRENT4));
1915 return LDB_ERR_UNWILLING_TO_PERFORM;
1917 new_is_critical = true;
1919 break;
1921 case UF_SERVER_TRUST_ACCOUNT:
1922 if (!is_computer) {
1923 ldb_asprintf_errstring(ldb,
1924 "%08X: samldb: UF_SERVER_TRUST_ACCOUNT "
1925 "requires objectclass 'computer'!",
1926 W_ERROR_V(WERR_DS_MACHINE_ACCOUNT_CREATED_PRENT4));
1927 return LDB_ERR_UNWILLING_TO_PERFORM;
1929 new_is_critical = true;
1930 break;
1932 default:
1933 ldb_asprintf_errstring(ldb,
1934 "%08X: samldb: invalid userAccountControl[0x%08X]",
1935 W_ERROR_V(WERR_INVALID_PARAMETER), raw_uac);
1936 return LDB_ERR_OTHER;
1939 if (old_atype != new_atype) {
1940 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1941 "sAMAccountType", new_atype);
1942 if (ret != LDB_SUCCESS) {
1943 return ret;
1945 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
1946 el->flags = LDB_FLAG_MOD_REPLACE;
1949 /* As per MS-SAMR 3.1.1.8.10 these flags have not to be set */
1950 if ((clear_uac & UF_LOCKOUT) && (old_lockoutTime != 0)) {
1951 /* "pwdLastSet" reset as password expiration has been forced */
1952 ldb_msg_remove_attr(ac->msg, "lockoutTime");
1953 ret = samdb_msg_add_uint64(ldb, ac->msg, ac->msg, "lockoutTime",
1954 (NTTIME)0);
1955 if (ret != LDB_SUCCESS) {
1956 return ret;
1958 el = ldb_msg_find_element(ac->msg, "lockoutTime");
1959 el->flags = LDB_FLAG_MOD_REPLACE;
1962 /* "isCriticalSystemObject" might be set/changed */
1963 if (old_is_critical != new_is_critical) {
1964 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1965 new_is_critical ? "TRUE": "FALSE");
1966 if (ret != LDB_SUCCESS) {
1967 return ret;
1969 el = ldb_msg_find_element(ac->msg,
1970 "isCriticalSystemObject");
1971 el->flags = LDB_FLAG_MOD_REPLACE;
1974 if (!ldb_msg_find_element(ac->msg, "primaryGroupID") &&
1975 (old_pgrid != new_pgrid)) {
1976 /* Older AD deployments don't know about the RODC group */
1977 if (new_pgrid == DOMAIN_RID_READONLY_DCS) {
1978 ret = samldb_prim_group_tester(ac, new_pgrid);
1979 if (ret != LDB_SUCCESS) {
1980 return ret;
1984 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1985 "primaryGroupID", new_pgrid);
1986 if (ret != LDB_SUCCESS) {
1987 return ret;
1989 el = ldb_msg_find_element(ac->msg,
1990 "primaryGroupID");
1991 el->flags = LDB_FLAG_MOD_REPLACE;
1994 /* Propagate eventual "userAccountControl" attribute changes */
1995 if (old_uac != new_uac) {
1996 char *tempstr = talloc_asprintf(ac->msg, "%d",
1997 new_uac);
1998 if (tempstr == NULL) {
1999 return ldb_module_oom(ac->module);
2002 /* Overwrite "userAccountControl" correctly */
2003 el = dsdb_get_single_valued_attr(ac->msg, "userAccountControl",
2004 ac->req->operation);
2005 el->values[0].data = (uint8_t *) tempstr;
2006 el->values[0].length = strlen(tempstr);
2007 } else {
2008 ldb_msg_remove_attr(ac->msg, "userAccountControl");
2011 return LDB_SUCCESS;
2014 static int samldb_lockout_time(struct samldb_ctx *ac)
2016 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
2017 NTTIME lockoutTime;
2018 struct ldb_message_element *el;
2019 struct ldb_message *tmp_msg;
2020 int ret;
2022 el = dsdb_get_single_valued_attr(ac->msg, "lockoutTime",
2023 ac->req->operation);
2024 if (el == NULL || el->num_values == 0) {
2025 ldb_asprintf_errstring(ldb,
2026 "%08X: samldb: 'lockoutTime' can't be deleted!",
2027 W_ERROR_V(WERR_DS_ILLEGAL_MOD_OPERATION));
2028 return LDB_ERR_UNWILLING_TO_PERFORM;
2031 /* Create a temporary message for fetching the "lockoutTime" */
2032 tmp_msg = ldb_msg_new(ac->msg);
2033 if (tmp_msg == NULL) {
2034 return ldb_module_oom(ac->module);
2036 ret = ldb_msg_add(tmp_msg, el, 0);
2037 if (ret != LDB_SUCCESS) {
2038 return ret;
2040 lockoutTime = ldb_msg_find_attr_as_int64(tmp_msg,
2041 "lockoutTime",
2043 talloc_free(tmp_msg);
2045 if (lockoutTime != 0) {
2046 return LDB_SUCCESS;
2049 /* lockoutTime == 0 resets badPwdCount */
2050 ldb_msg_remove_attr(ac->msg, "badPwdCount");
2051 ret = samdb_msg_add_int(ldb, ac->msg, ac->msg,
2052 "badPwdCount", 0);
2053 if (ret != LDB_SUCCESS) {
2054 return ret;
2056 el = ldb_msg_find_element(ac->msg, "badPwdCount");
2057 el->flags = LDB_FLAG_MOD_REPLACE;
2059 return LDB_SUCCESS;
2062 static int samldb_group_type_change(struct samldb_ctx *ac)
2064 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
2065 uint32_t group_type, old_group_type, account_type;
2066 struct ldb_message_element *el;
2067 struct ldb_message *tmp_msg;
2068 int ret;
2069 struct ldb_result *res;
2070 const char * const attrs[] = { "groupType", NULL };
2072 el = dsdb_get_single_valued_attr(ac->msg, "groupType",
2073 ac->req->operation);
2074 if (el == NULL) {
2075 /* we are not affected */
2076 return LDB_SUCCESS;
2079 /* Create a temporary message for fetching the "groupType" */
2080 tmp_msg = ldb_msg_new(ac->msg);
2081 if (tmp_msg == NULL) {
2082 return ldb_module_oom(ac->module);
2084 ret = ldb_msg_add(tmp_msg, el, 0);
2085 if (ret != LDB_SUCCESS) {
2086 return ret;
2088 group_type = ldb_msg_find_attr_as_uint(tmp_msg, "groupType", 0);
2089 talloc_free(tmp_msg);
2091 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
2092 DSDB_FLAG_NEXT_MODULE |
2093 DSDB_SEARCH_SHOW_DELETED, ac->req);
2094 if (ret != LDB_SUCCESS) {
2095 return ret;
2097 old_group_type = ldb_msg_find_attr_as_uint(res->msgs[0], "groupType", 0);
2098 if (old_group_type == 0) {
2099 return ldb_operr(ldb);
2102 /* Group type switching isn't so easy as it seems: We can only
2103 * change in this directions: global <-> universal <-> local
2104 * On each step also the group type itself
2105 * (security/distribution) is variable. */
2107 if (ldb_request_get_control(ac->req, LDB_CONTROL_PROVISION_OID) == NULL) {
2108 switch (group_type) {
2109 case GTYPE_SECURITY_GLOBAL_GROUP:
2110 case GTYPE_DISTRIBUTION_GLOBAL_GROUP:
2111 /* change to "universal" allowed */
2112 if ((old_group_type == GTYPE_SECURITY_DOMAIN_LOCAL_GROUP) ||
2113 (old_group_type == GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP)) {
2114 ldb_set_errstring(ldb,
2115 "samldb: Change from security/distribution local group forbidden!");
2116 return LDB_ERR_UNWILLING_TO_PERFORM;
2118 break;
2120 case GTYPE_SECURITY_UNIVERSAL_GROUP:
2121 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP:
2122 /* each change allowed */
2123 break;
2124 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP:
2125 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP:
2126 /* change to "universal" allowed */
2127 if ((old_group_type == GTYPE_SECURITY_GLOBAL_GROUP) ||
2128 (old_group_type == GTYPE_DISTRIBUTION_GLOBAL_GROUP)) {
2129 ldb_set_errstring(ldb,
2130 "samldb: Change from security/distribution global group forbidden!");
2131 return LDB_ERR_UNWILLING_TO_PERFORM;
2133 break;
2135 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP:
2136 default:
2137 /* we don't allow this "groupType" values */
2138 return LDB_ERR_UNWILLING_TO_PERFORM;
2139 break;
2143 account_type = ds_gtype2atype(group_type);
2144 if (account_type == 0) {
2145 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
2146 return LDB_ERR_UNWILLING_TO_PERFORM;
2148 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
2149 account_type);
2150 if (ret != LDB_SUCCESS) {
2151 return ret;
2153 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
2154 el->flags = LDB_FLAG_MOD_REPLACE;
2156 return LDB_SUCCESS;
2159 static int samldb_sam_accountname_check(struct samldb_ctx *ac)
2161 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
2162 const char * const no_attrs[] = { NULL };
2163 struct ldb_result *res;
2164 const char *sam_accountname, *enc_str;
2165 struct ldb_message_element *el;
2166 struct ldb_message *tmp_msg;
2167 int ret;
2169 el = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName",
2170 ac->req->operation);
2171 if (el == NULL) {
2172 /* we are not affected */
2173 return LDB_SUCCESS;
2176 /* Create a temporary message for fetching the "sAMAccountName" */
2177 tmp_msg = ldb_msg_new(ac->msg);
2178 if (tmp_msg == NULL) {
2179 return ldb_module_oom(ac->module);
2181 ret = ldb_msg_add(tmp_msg, el, 0);
2182 if (ret != LDB_SUCCESS) {
2183 return ret;
2186 /* We must not steal the original string, it belongs to the caller! */
2187 sam_accountname = talloc_strdup(ac,
2188 ldb_msg_find_attr_as_string(tmp_msg, "sAMAccountName", NULL));
2189 talloc_free(tmp_msg);
2191 if (sam_accountname == NULL) {
2192 /* The "sAMAccountName" cannot be nothing */
2193 ldb_set_errstring(ldb,
2194 "samldb: Empty account names aren't allowed!");
2195 return LDB_ERR_UNWILLING_TO_PERFORM;
2198 enc_str = ldb_binary_encode_string(ac, sam_accountname);
2199 if (enc_str == NULL) {
2200 return ldb_module_oom(ac->module);
2203 /* Make sure that a "sAMAccountName" is only used once */
2205 ret = dsdb_module_search(ac->module, ac, &res,
2206 ldb_get_default_basedn(ldb),
2207 LDB_SCOPE_SUBTREE, no_attrs,
2208 DSDB_FLAG_NEXT_MODULE, ac->req,
2209 "(sAMAccountName=%s)", enc_str);
2210 if (ret != LDB_SUCCESS) {
2211 return ret;
2213 if (res->count > 1) {
2214 return ldb_operr(ldb);
2215 } else if (res->count == 1) {
2216 if (ldb_dn_compare(res->msgs[0]->dn, ac->msg->dn) != 0) {
2217 ldb_asprintf_errstring(ldb,
2218 "samldb: Account name (sAMAccountName) '%s' already in use!",
2219 sam_accountname);
2220 return LDB_ERR_ENTRY_ALREADY_EXISTS;
2223 talloc_free(res);
2225 return LDB_SUCCESS;
2228 static int samldb_member_check(struct samldb_ctx *ac)
2230 const char * const attrs[] = { "objectSid", NULL };
2231 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
2232 struct ldb_message_element *el;
2233 struct ldb_dn *member_dn;
2234 struct dom_sid *sid;
2235 struct ldb_result *res;
2236 struct dom_sid *group_sid;
2237 unsigned int i, j;
2238 int ret;
2240 /* Fetch information from the existing object */
2242 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
2243 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED, ac->req, NULL);
2244 if (ret != LDB_SUCCESS) {
2245 return ret;
2247 if (res->count != 1) {
2248 return ldb_operr(ldb);
2251 group_sid = samdb_result_dom_sid(res, res->msgs[0], "objectSid");
2252 if (group_sid == NULL) {
2253 return ldb_operr(ldb);
2256 /* We've to walk over all modification entries and consider the "member"
2257 * ones. */
2258 for (i = 0; i < ac->msg->num_elements; i++) {
2259 if (ldb_attr_cmp(ac->msg->elements[i].name, "member") != 0) {
2260 continue;
2263 el = &ac->msg->elements[i];
2264 for (j = 0; j < el->num_values; j++) {
2265 struct ldb_result *group_res;
2266 const char *group_attrs[] = { "primaryGroupID" , NULL };
2267 uint32_t prim_group_rid;
2269 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
2270 /* Deletes will be handled in
2271 * repl_meta_data, and deletes not
2272 * matching a member will return
2273 * LDB_ERR_UNWILLING_TO_PERFORM
2274 * there */
2275 continue;
2278 member_dn = ldb_dn_from_ldb_val(ac, ldb,
2279 &el->values[j]);
2280 if (!ldb_dn_validate(member_dn)) {
2281 return ldb_operr(ldb);
2284 /* Denies to add "member"s to groups which are primary
2285 * ones for them - in this case return
2286 * ERR_ENTRY_ALREADY_EXISTS. */
2288 ret = dsdb_module_search_dn(ac->module, ac, &group_res,
2289 member_dn, group_attrs,
2290 DSDB_FLAG_NEXT_MODULE, ac->req);
2291 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
2292 /* member DN doesn't exist yet */
2293 continue;
2295 if (ret != LDB_SUCCESS) {
2296 return ret;
2298 prim_group_rid = ldb_msg_find_attr_as_uint(group_res->msgs[0], "primaryGroupID", (uint32_t)-1);
2299 if (prim_group_rid == (uint32_t) -1) {
2300 /* the member hasn't to be a user account ->
2301 * therefore no check needed in this case. */
2302 continue;
2305 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
2306 prim_group_rid);
2307 if (sid == NULL) {
2308 return ldb_operr(ldb);
2311 if (dom_sid_equal(group_sid, sid)) {
2312 ldb_asprintf_errstring(ldb,
2313 "samldb: member %s already set via primaryGroupID %u",
2314 ldb_dn_get_linearized(member_dn), prim_group_rid);
2315 return LDB_ERR_ENTRY_ALREADY_EXISTS;
2320 talloc_free(res);
2322 return LDB_SUCCESS;
2325 /* SAM objects have special rules regarding the "description" attribute on
2326 * modify operations. */
2327 static int samldb_description_check(struct samldb_ctx *ac, bool *modified)
2329 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
2330 const char * const attrs[] = { "objectClass", "description", NULL };
2331 struct ldb_result *res;
2332 unsigned int i;
2333 int ret;
2335 /* Fetch information from the existing object */
2336 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
2337 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED, ac->req,
2338 "(|(objectclass=user)(objectclass=group)(objectclass=samDomain)(objectclass=samServer))");
2339 if (ret != LDB_SUCCESS) {
2340 /* don't treat it specially ... let normal error codes
2341 happen from other places */
2342 ldb_reset_err_string(ldb);
2343 return LDB_SUCCESS;
2345 if (res->count == 0) {
2346 /* we didn't match the filter */
2347 talloc_free(res);
2348 return LDB_SUCCESS;
2351 /* We've to walk over all modification entries and consider the
2352 * "description" ones. */
2353 for (i = 0; i < ac->msg->num_elements; i++) {
2354 if (ldb_attr_cmp(ac->msg->elements[i].name, "description") == 0) {
2355 ac->msg->elements[i].flags |= LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK;
2356 *modified = true;
2360 talloc_free(res);
2362 return LDB_SUCCESS;
2365 /* This trigger adapts the "servicePrincipalName" attributes if the
2366 * "dNSHostName" and/or "sAMAccountName" attribute change(s) */
2367 static int samldb_service_principal_names_change(struct samldb_ctx *ac)
2369 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
2370 struct ldb_message_element *el = NULL, *el2 = NULL;
2371 struct ldb_message *msg;
2372 const char * const attrs[] = { "servicePrincipalName", NULL };
2373 struct ldb_result *res;
2374 const char *dns_hostname = NULL, *old_dns_hostname = NULL,
2375 *sam_accountname = NULL, *old_sam_accountname = NULL;
2376 unsigned int i, j;
2377 int ret;
2379 el = dsdb_get_single_valued_attr(ac->msg, "dNSHostName",
2380 ac->req->operation);
2381 el2 = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName",
2382 ac->req->operation);
2383 if ((el == NULL) && (el2 == NULL)) {
2384 /* we are not affected */
2385 return LDB_SUCCESS;
2388 /* Create a temporary message for fetching the "dNSHostName" */
2389 if (el != NULL) {
2390 const char *dns_attrs[] = { "dNSHostName", NULL };
2391 msg = ldb_msg_new(ac->msg);
2392 if (msg == NULL) {
2393 return ldb_module_oom(ac->module);
2395 ret = ldb_msg_add(msg, el, 0);
2396 if (ret != LDB_SUCCESS) {
2397 return ret;
2399 dns_hostname = talloc_strdup(ac,
2400 ldb_msg_find_attr_as_string(msg, "dNSHostName", NULL));
2401 if (dns_hostname == NULL) {
2402 return ldb_module_oom(ac->module);
2405 talloc_free(msg);
2407 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn,
2408 dns_attrs, DSDB_FLAG_NEXT_MODULE, ac->req);
2409 if (ret == LDB_SUCCESS) {
2410 old_dns_hostname = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
2414 /* Create a temporary message for fetching the "sAMAccountName" */
2415 if (el2 != NULL) {
2416 char *tempstr, *tempstr2 = NULL;
2417 const char *acct_attrs[] = { "sAMAccountName", NULL };
2419 msg = ldb_msg_new(ac->msg);
2420 if (msg == NULL) {
2421 return ldb_module_oom(ac->module);
2423 ret = ldb_msg_add(msg, el2, 0);
2424 if (ret != LDB_SUCCESS) {
2425 return ret;
2427 tempstr = talloc_strdup(ac,
2428 ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL));
2429 talloc_free(msg);
2431 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, acct_attrs,
2432 DSDB_FLAG_NEXT_MODULE, ac->req);
2433 if (ret == LDB_SUCCESS) {
2434 tempstr2 = talloc_strdup(ac,
2435 ldb_msg_find_attr_as_string(res->msgs[0],
2436 "sAMAccountName", NULL));
2440 /* The "sAMAccountName" needs some additional trimming: we need
2441 * to remove the trailing "$"s if they exist. */
2442 if ((tempstr != NULL) && (tempstr[0] != '\0') &&
2443 (tempstr[strlen(tempstr) - 1] == '$')) {
2444 tempstr[strlen(tempstr) - 1] = '\0';
2446 if ((tempstr2 != NULL) && (tempstr2[0] != '\0') &&
2447 (tempstr2[strlen(tempstr2) - 1] == '$')) {
2448 tempstr2[strlen(tempstr2) - 1] = '\0';
2450 sam_accountname = tempstr;
2451 old_sam_accountname = tempstr2;
2454 if (old_dns_hostname == NULL) {
2455 /* we cannot change when the old name is unknown */
2456 dns_hostname = NULL;
2458 if ((old_dns_hostname != NULL) && (dns_hostname != NULL) &&
2459 (strcasecmp_m(old_dns_hostname, dns_hostname) == 0)) {
2460 /* The "dNSHostName" didn't change */
2461 dns_hostname = NULL;
2464 if (old_sam_accountname == NULL) {
2465 /* we cannot change when the old name is unknown */
2466 sam_accountname = NULL;
2468 if ((old_sam_accountname != NULL) && (sam_accountname != NULL) &&
2469 (strcasecmp_m(old_sam_accountname, sam_accountname) == 0)) {
2470 /* The "sAMAccountName" didn't change */
2471 sam_accountname = NULL;
2474 if ((dns_hostname == NULL) && (sam_accountname == NULL)) {
2475 /* Well, there are information missing (old name(s)) or the
2476 * names didn't change. We've nothing to do and can exit here */
2477 return LDB_SUCCESS;
2480 /* Potential "servicePrincipalName" changes in the same request have to
2481 * be handled before the update (Windows behaviour). */
2482 el = ldb_msg_find_element(ac->msg, "servicePrincipalName");
2483 if (el != NULL) {
2484 msg = ldb_msg_new(ac->msg);
2485 if (msg == NULL) {
2486 return ldb_module_oom(ac->module);
2488 msg->dn = ac->msg->dn;
2490 do {
2491 ret = ldb_msg_add(msg, el, el->flags);
2492 if (ret != LDB_SUCCESS) {
2493 return ret;
2496 ldb_msg_remove_element(ac->msg, el);
2498 el = ldb_msg_find_element(ac->msg,
2499 "servicePrincipalName");
2500 } while (el != NULL);
2502 ret = dsdb_module_modify(ac->module, msg,
2503 DSDB_FLAG_NEXT_MODULE, ac->req);
2504 if (ret != LDB_SUCCESS) {
2505 return ret;
2507 talloc_free(msg);
2510 /* Fetch the "servicePrincipalName"s if any */
2511 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
2512 DSDB_FLAG_NEXT_MODULE, ac->req, NULL);
2513 if (ret != LDB_SUCCESS) {
2514 return ret;
2516 if ((res->count != 1) || (res->msgs[0]->num_elements > 1)) {
2517 return ldb_operr(ldb);
2520 if (res->msgs[0]->num_elements == 1) {
2522 * Yes, we do have "servicePrincipalName"s. First we update them
2523 * locally, that means we do always substitute the current
2524 * "dNSHostName" with the new one and/or "sAMAccountName"
2525 * without "$" with the new one and then we append the
2526 * modified "servicePrincipalName"s as a message element
2527 * replace to the modification request (Windows behaviour). We
2528 * need also to make sure that the values remain case-
2529 * insensitively unique.
2532 ret = ldb_msg_add_empty(ac->msg, "servicePrincipalName",
2533 LDB_FLAG_MOD_REPLACE, &el);
2534 if (ret != LDB_SUCCESS) {
2535 return ret;
2538 for (i = 0; i < res->msgs[0]->elements[0].num_values; i++) {
2539 char *old_str, *new_str, *pos;
2540 const char *tok;
2541 struct ldb_val *vals;
2542 bool found = false;
2544 old_str = (char *)
2545 res->msgs[0]->elements[0].values[i].data;
2547 new_str = talloc_strdup(ac->msg,
2548 strtok_r(old_str, "/", &pos));
2549 if (new_str == NULL) {
2550 return ldb_module_oom(ac->module);
2553 while ((tok = strtok_r(NULL, "/", &pos)) != NULL) {
2554 if ((dns_hostname != NULL) &&
2555 (strcasecmp_m(tok, old_dns_hostname) == 0)) {
2556 tok = dns_hostname;
2558 if ((sam_accountname != NULL) &&
2559 (strcasecmp_m(tok, old_sam_accountname) == 0)) {
2560 tok = sam_accountname;
2563 new_str = talloc_asprintf(ac->msg, "%s/%s",
2564 new_str, tok);
2565 if (new_str == NULL) {
2566 return ldb_module_oom(ac->module);
2570 /* Uniqueness check */
2571 for (j = 0; (!found) && (j < el->num_values); j++) {
2572 if (strcasecmp_m((char *)el->values[j].data,
2573 new_str) == 0) {
2574 found = true;
2577 if (found) {
2578 continue;
2582 * append the new "servicePrincipalName" -
2583 * code derived from ldb_msg_add_value().
2585 * Open coded to make it clear that we must
2586 * append to the MOD_REPLACE el created above.
2588 vals = talloc_realloc(ac->msg, el->values,
2589 struct ldb_val,
2590 el->num_values + 1);
2591 if (vals == NULL) {
2592 return ldb_module_oom(ac->module);
2594 el->values = vals;
2595 el->values[el->num_values] = data_blob_string_const(new_str);
2596 ++(el->num_values);
2600 talloc_free(res);
2602 return LDB_SUCCESS;
2605 /* This checks the "fSMORoleOwner" attributes */
2606 static int samldb_fsmo_role_owner_check(struct samldb_ctx *ac)
2608 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
2609 const char * const no_attrs[] = { NULL };
2610 struct ldb_message_element *el;
2611 struct ldb_message *tmp_msg;
2612 struct ldb_dn *res_dn;
2613 struct ldb_result *res;
2614 int ret;
2616 el = dsdb_get_single_valued_attr(ac->msg, "fSMORoleOwner",
2617 ac->req->operation);
2618 if (el == NULL) {
2619 /* we are not affected */
2620 return LDB_SUCCESS;
2623 /* Create a temporary message for fetching the "fSMORoleOwner" */
2624 tmp_msg = ldb_msg_new(ac->msg);
2625 if (tmp_msg == NULL) {
2626 return ldb_module_oom(ac->module);
2628 ret = ldb_msg_add(tmp_msg, el, 0);
2629 if (ret != LDB_SUCCESS) {
2630 return ret;
2632 res_dn = ldb_msg_find_attr_as_dn(ldb, ac, tmp_msg, "fSMORoleOwner");
2633 talloc_free(tmp_msg);
2635 if (res_dn == NULL) {
2636 ldb_set_errstring(ldb,
2637 "samldb: 'fSMORoleOwner' attributes have to reference 'nTDSDSA' entries!");
2638 if (ac->req->operation == LDB_ADD) {
2639 return LDB_ERR_CONSTRAINT_VIOLATION;
2640 } else {
2641 return LDB_ERR_UNWILLING_TO_PERFORM;
2645 /* Fetched DN has to reference a "nTDSDSA" entry */
2646 ret = dsdb_module_search(ac->module, ac, &res, res_dn, LDB_SCOPE_BASE,
2647 no_attrs,
2648 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED,
2649 ac->req, "(objectClass=nTDSDSA)");
2650 if (ret != LDB_SUCCESS) {
2651 return ret;
2653 if (res->count != 1) {
2654 ldb_set_errstring(ldb,
2655 "samldb: 'fSMORoleOwner' attributes have to reference 'nTDSDSA' entries!");
2656 return LDB_ERR_UNWILLING_TO_PERFORM;
2659 talloc_free(res);
2661 return LDB_SUCCESS;
2665 /* add */
2666 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
2668 struct ldb_context *ldb;
2669 struct samldb_ctx *ac;
2670 struct ldb_message_element *el;
2671 int ret;
2673 ldb = ldb_module_get_ctx(module);
2674 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
2676 /* do not manipulate our control entries */
2677 if (ldb_dn_is_special(req->op.add.message->dn)) {
2678 return ldb_next_request(module, req);
2681 el = ldb_msg_find_element(req->op.add.message, "userParameters");
2682 if (el != NULL && ldb_req_is_untrusted(req)) {
2683 const char *reason = "samldb_add: "
2684 "setting userParameters is not supported over LDAP, "
2685 "see https://bugzilla.samba.org/show_bug.cgi?id=8077";
2686 ldb_debug(ldb, LDB_DEBUG_WARNING, "%s", reason);
2687 return ldb_error(ldb, LDB_ERR_CONSTRAINT_VIOLATION, reason);
2690 ac = samldb_ctx_init(module, req);
2691 if (ac == NULL) {
2692 return ldb_operr(ldb);
2695 /* build the new msg */
2696 ac->msg = ldb_msg_copy_shallow(ac, req->op.add.message);
2697 if (ac->msg == NULL) {
2698 talloc_free(ac);
2699 ldb_debug(ldb, LDB_DEBUG_FATAL,
2700 "samldb_add: ldb_msg_copy_shallow failed!\n");
2701 return ldb_operr(ldb);
2704 el = ldb_msg_find_element(ac->msg, "fSMORoleOwner");
2705 if (el != NULL) {
2706 ret = samldb_fsmo_role_owner_check(ac);
2707 if (ret != LDB_SUCCESS) {
2708 return ret;
2712 if (samdb_find_attribute(ldb, ac->msg,
2713 "objectclass", "user") != NULL) {
2714 ac->type = SAMLDB_TYPE_USER;
2716 ret = samldb_prim_group_trigger(ac);
2717 if (ret != LDB_SUCCESS) {
2718 return ret;
2721 ret = samldb_objectclass_trigger(ac);
2722 if (ret != LDB_SUCCESS) {
2723 return ret;
2726 return samldb_fill_object(ac);
2729 if (samdb_find_attribute(ldb, ac->msg,
2730 "objectclass", "group") != NULL) {
2731 ac->type = SAMLDB_TYPE_GROUP;
2733 ret = samldb_objectclass_trigger(ac);
2734 if (ret != LDB_SUCCESS) {
2735 return ret;
2738 return samldb_fill_object(ac);
2741 /* perhaps a foreignSecurityPrincipal? */
2742 if (samdb_find_attribute(ldb, ac->msg,
2743 "objectclass",
2744 "foreignSecurityPrincipal") != NULL) {
2745 return samldb_fill_foreignSecurityPrincipal_object(ac);
2748 if (samdb_find_attribute(ldb, ac->msg,
2749 "objectclass", "classSchema") != NULL) {
2750 ret = samldb_schema_info_update(ac);
2751 if (ret != LDB_SUCCESS) {
2752 talloc_free(ac);
2753 return ret;
2756 ac->type = SAMLDB_TYPE_CLASS;
2757 return samldb_fill_object(ac);
2760 if (samdb_find_attribute(ldb, ac->msg,
2761 "objectclass", "attributeSchema") != NULL) {
2762 ret = samldb_schema_info_update(ac);
2763 if (ret != LDB_SUCCESS) {
2764 talloc_free(ac);
2765 return ret;
2768 ac->type = SAMLDB_TYPE_ATTRIBUTE;
2769 return samldb_fill_object(ac);
2772 talloc_free(ac);
2774 /* nothing matched, go on */
2775 return ldb_next_request(module, req);
2778 /* modify */
2779 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
2781 struct ldb_context *ldb;
2782 struct samldb_ctx *ac;
2783 struct ldb_message_element *el, *el2;
2784 struct ldb_control *is_undelete;
2785 bool modified = false;
2786 int ret;
2788 if (ldb_dn_is_special(req->op.mod.message->dn)) {
2789 /* do not manipulate our control entries */
2790 return ldb_next_request(module, req);
2793 ldb = ldb_module_get_ctx(module);
2796 * we are going to need some special handling if in Undelete call.
2797 * Since tombstone_reanimate module will restore certain attributes,
2798 * we need to relax checks for: sAMAccountType, primaryGroupID
2800 is_undelete = ldb_request_get_control(req, DSDB_CONTROL_RESTORE_TOMBSTONE_OID);
2802 /* make sure that "objectSid" is not specified */
2803 el = ldb_msg_find_element(req->op.mod.message, "objectSid");
2804 if (el != NULL) {
2805 if (ldb_request_get_control(req, LDB_CONTROL_PROVISION_OID) == NULL) {
2806 ldb_set_errstring(ldb,
2807 "samldb: objectSid must not be specified!");
2808 return LDB_ERR_UNWILLING_TO_PERFORM;
2811 if (is_undelete == NULL) {
2812 /* make sure that "sAMAccountType" is not specified */
2813 el = ldb_msg_find_element(req->op.mod.message, "sAMAccountType");
2814 if (el != NULL) {
2815 ldb_set_errstring(ldb,
2816 "samldb: sAMAccountType must not be specified!");
2817 return LDB_ERR_UNWILLING_TO_PERFORM;
2820 /* make sure that "isCriticalSystemObject" is not specified */
2821 el = ldb_msg_find_element(req->op.mod.message, "isCriticalSystemObject");
2822 if (el != NULL) {
2823 if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID) == NULL) {
2824 ldb_set_errstring(ldb,
2825 "samldb: isCriticalSystemObject must not be specified!");
2826 return LDB_ERR_UNWILLING_TO_PERFORM;
2830 /* msDS-IntId is not allowed to be modified
2831 * except when modification comes from replication */
2832 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
2833 if (!ldb_request_get_control(req,
2834 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
2835 return LDB_ERR_CONSTRAINT_VIOLATION;
2839 el = ldb_msg_find_element(req->op.mod.message, "userParameters");
2840 if (el != NULL && ldb_req_is_untrusted(req)) {
2841 const char *reason = "samldb: "
2842 "setting userParameters is not supported over LDAP, "
2843 "see https://bugzilla.samba.org/show_bug.cgi?id=8077";
2844 ldb_debug(ldb, LDB_DEBUG_WARNING, "%s", reason);
2845 return ldb_error(ldb, LDB_ERR_CONSTRAINT_VIOLATION, reason);
2848 ac = samldb_ctx_init(module, req);
2849 if (ac == NULL) {
2850 return ldb_operr(ldb);
2853 /* build the new msg */
2854 ac->msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
2855 if (ac->msg == NULL) {
2856 talloc_free(ac);
2857 ldb_debug(ldb, LDB_DEBUG_FATAL,
2858 "samldb_modify: ldb_msg_copy_shallow failed!\n");
2859 return ldb_operr(ldb);
2862 if (is_undelete == NULL) {
2863 el = ldb_msg_find_element(ac->msg, "primaryGroupID");
2864 if (el != NULL) {
2865 ret = samldb_prim_group_trigger(ac);
2866 if (ret != LDB_SUCCESS) {
2867 return ret;
2872 el = ldb_msg_find_element(ac->msg, "userAccountControl");
2873 if (el != NULL) {
2874 modified = true;
2875 ret = samldb_user_account_control_change(ac);
2876 if (ret != LDB_SUCCESS) {
2877 return ret;
2881 el = ldb_msg_find_element(ac->msg, "lockoutTime");
2882 if (el != NULL) {
2883 modified = true;
2884 ret = samldb_lockout_time(ac);
2885 if (ret != LDB_SUCCESS) {
2886 return ret;
2890 el = ldb_msg_find_element(ac->msg, "groupType");
2891 if (el != NULL) {
2892 modified = true;
2893 ret = samldb_group_type_change(ac);
2894 if (ret != LDB_SUCCESS) {
2895 return ret;
2899 el = ldb_msg_find_element(ac->msg, "sAMAccountName");
2900 if (el != NULL) {
2901 ret = samldb_sam_accountname_check(ac);
2902 if (ret != LDB_SUCCESS) {
2903 return ret;
2907 el = ldb_msg_find_element(ac->msg, "member");
2908 if (el != NULL) {
2909 ret = samldb_member_check(ac);
2910 if (ret != LDB_SUCCESS) {
2911 return ret;
2915 el = ldb_msg_find_element(ac->msg, "description");
2916 if (el != NULL) {
2917 ret = samldb_description_check(ac, &modified);
2918 if (ret != LDB_SUCCESS) {
2919 return ret;
2923 el = ldb_msg_find_element(ac->msg, "dNSHostName");
2924 el2 = ldb_msg_find_element(ac->msg, "sAMAccountName");
2925 if ((el != NULL) || (el2 != NULL)) {
2926 modified = true;
2927 ret = samldb_service_principal_names_change(ac);
2928 if (ret != LDB_SUCCESS) {
2929 return ret;
2933 el = ldb_msg_find_element(ac->msg, "fSMORoleOwner");
2934 if (el != NULL) {
2935 ret = samldb_fsmo_role_owner_check(ac);
2936 if (ret != LDB_SUCCESS) {
2937 return ret;
2941 if (modified) {
2942 struct ldb_request *child_req;
2944 /* Now perform the real modifications as a child request */
2945 ret = ldb_build_mod_req(&child_req, ldb, ac,
2946 ac->msg,
2947 req->controls,
2948 req, dsdb_next_callback,
2949 req);
2950 LDB_REQ_SET_LOCATION(child_req);
2951 if (ret != LDB_SUCCESS) {
2952 return ret;
2955 return ldb_next_request(module, child_req);
2958 talloc_free(ac);
2960 /* no change which interests us, go on */
2961 return ldb_next_request(module, req);
2964 /* delete */
2966 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
2968 struct ldb_context *ldb;
2969 struct dom_sid *sid;
2970 uint32_t rid;
2971 NTSTATUS status;
2972 int ret;
2973 struct ldb_result *res;
2974 const char * const attrs[] = { "objectSid", "isDeleted", NULL };
2975 const char * const noattrs[] = { NULL };
2977 ldb = ldb_module_get_ctx(ac->module);
2979 /* Finds out the SID/RID of the SAM object */
2980 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->req->op.del.dn,
2981 attrs,
2982 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED,
2983 ac->req);
2984 if (ret != LDB_SUCCESS) {
2985 return ret;
2988 if (ldb_msg_check_string_attribute(res->msgs[0], "isDeleted", "TRUE")) {
2989 return LDB_SUCCESS;
2992 sid = samdb_result_dom_sid(ac, res->msgs[0], "objectSid");
2993 if (sid == NULL) {
2994 /* No SID - it might not be a SAM object - therefore ok */
2995 return LDB_SUCCESS;
2997 status = dom_sid_split_rid(ac, sid, NULL, &rid);
2998 if (!NT_STATUS_IS_OK(status)) {
2999 return ldb_operr(ldb);
3001 if (rid == 0) {
3002 /* Special object (security principal?) */
3003 return LDB_SUCCESS;
3005 /* do not allow deletion of well-known sids */
3006 if (rid < DSDB_SAMDB_MINIMUM_ALLOWED_RID &&
3007 (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
3008 return LDB_ERR_OTHER;
3011 /* Deny delete requests from groups which are primary ones */
3012 ret = dsdb_module_search(ac->module, ac, &res,
3013 ldb_get_default_basedn(ldb),
3014 LDB_SCOPE_SUBTREE, noattrs,
3015 DSDB_FLAG_NEXT_MODULE,
3016 ac->req,
3017 "(&(primaryGroupID=%u)(objectClass=user))", rid);
3018 if (ret != LDB_SUCCESS) {
3019 return ret;
3021 if (res->count > 0) {
3022 return LDB_ERR_ENTRY_ALREADY_EXISTS;
3025 return LDB_SUCCESS;
3028 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
3030 struct samldb_ctx *ac;
3031 int ret;
3033 if (ldb_dn_is_special(req->op.del.dn)) {
3034 /* do not manipulate our control entries */
3035 return ldb_next_request(module, req);
3038 ac = samldb_ctx_init(module, req);
3039 if (ac == NULL) {
3040 return ldb_operr(ldb_module_get_ctx(module));
3043 ret = samldb_prim_group_users_check(ac);
3044 if (ret != LDB_SUCCESS) {
3045 return ret;
3048 talloc_free(ac);
3050 return ldb_next_request(module, req);
3053 /* rename */
3055 static int check_rename_constraints(struct ldb_message *msg,
3056 struct samldb_ctx *ac,
3057 struct ldb_dn *olddn, struct ldb_dn *newdn)
3059 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
3060 struct ldb_dn *dn1, *dn2, *nc_root;
3061 int32_t systemFlags;
3062 bool move_op = false;
3063 bool rename_op = false;
3064 int ret;
3066 /* Skip the checks if old and new DN are the same, or if we have the
3067 * relax control specified or if the returned objects is already
3068 * deleted and needs only to be moved for consistency. */
3070 if (ldb_dn_compare(olddn, newdn) == 0) {
3071 return LDB_SUCCESS;
3073 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) != NULL) {
3074 return LDB_SUCCESS;
3077 /* Objects under CN=System */
3079 dn1 = ldb_dn_copy(ac, ldb_get_default_basedn(ldb));
3080 if (dn1 == NULL) return ldb_oom(ldb);
3082 if ( ! ldb_dn_add_child_fmt(dn1, "CN=System")) {
3083 talloc_free(dn1);
3084 return LDB_ERR_OPERATIONS_ERROR;
3087 if ((ldb_dn_compare_base(dn1, olddn) == 0) &&
3088 (ldb_dn_compare_base(dn1, newdn) != 0)) {
3089 talloc_free(dn1);
3090 ldb_asprintf_errstring(ldb,
3091 "subtree_rename: Cannot move/rename %s. Objects under CN=System have to stay under it!",
3092 ldb_dn_get_linearized(olddn));
3093 return LDB_ERR_OTHER;
3096 talloc_free(dn1);
3098 /* LSA objects */
3100 if ((samdb_find_attribute(ldb, msg, "objectClass", "secret") != NULL) ||
3101 (samdb_find_attribute(ldb, msg, "objectClass", "trustedDomain") != NULL)) {
3102 ldb_asprintf_errstring(ldb,
3103 "subtree_rename: Cannot move/rename %s. It's an LSA-specific object!",
3104 ldb_dn_get_linearized(olddn));
3105 return LDB_ERR_UNWILLING_TO_PERFORM;
3108 /* systemFlags */
3110 dn1 = ldb_dn_get_parent(ac, olddn);
3111 if (dn1 == NULL) return ldb_oom(ldb);
3112 dn2 = ldb_dn_get_parent(ac, newdn);
3113 if (dn2 == NULL) return ldb_oom(ldb);
3115 if (ldb_dn_compare(dn1, dn2) == 0) {
3116 rename_op = true;
3117 } else {
3118 move_op = true;
3121 talloc_free(dn1);
3122 talloc_free(dn2);
3124 systemFlags = ldb_msg_find_attr_as_int(msg, "systemFlags", 0);
3126 /* Fetch name context */
3128 ret = dsdb_find_nc_root(ldb, ac, olddn, &nc_root);
3129 if (ret != LDB_SUCCESS) {
3130 return ret;
3133 if (ldb_dn_compare(nc_root, ldb_get_schema_basedn(ldb)) == 0) {
3134 if (move_op) {
3135 ldb_asprintf_errstring(ldb,
3136 "subtree_rename: Cannot move %s within schema partition",
3137 ldb_dn_get_linearized(olddn));
3138 return LDB_ERR_UNWILLING_TO_PERFORM;
3140 if (rename_op &&
3141 (systemFlags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) != 0) {
3142 ldb_asprintf_errstring(ldb,
3143 "subtree_rename: Cannot rename %s within schema partition",
3144 ldb_dn_get_linearized(olddn));
3145 return LDB_ERR_UNWILLING_TO_PERFORM;
3147 } else if (ldb_dn_compare(nc_root, ldb_get_config_basedn(ldb)) == 0) {
3148 if (move_op &&
3149 (systemFlags & SYSTEM_FLAG_CONFIG_ALLOW_MOVE) == 0) {
3150 /* Here we have to do more: control the
3151 * "ALLOW_LIMITED_MOVE" flag. This means that the
3152 * grand-grand-parents of two objects have to be equal
3153 * in order to perform the move (this is used for
3154 * moving "server" objects in the "sites" container). */
3155 bool limited_move =
3156 systemFlags & SYSTEM_FLAG_CONFIG_ALLOW_LIMITED_MOVE;
3158 if (limited_move) {
3159 dn1 = ldb_dn_copy(ac, olddn);
3160 if (dn1 == NULL) return ldb_oom(ldb);
3161 dn2 = ldb_dn_copy(ac, newdn);
3162 if (dn2 == NULL) return ldb_oom(ldb);
3164 limited_move &= ldb_dn_remove_child_components(dn1, 3);
3165 limited_move &= ldb_dn_remove_child_components(dn2, 3);
3166 limited_move &= ldb_dn_compare(dn1, dn2) == 0;
3168 talloc_free(dn1);
3169 talloc_free(dn2);
3172 if (!limited_move) {
3173 ldb_asprintf_errstring(ldb,
3174 "subtree_rename: Cannot move %s to %s in config partition",
3175 ldb_dn_get_linearized(olddn), ldb_dn_get_linearized(newdn));
3176 return LDB_ERR_UNWILLING_TO_PERFORM;
3179 if (rename_op &&
3180 (systemFlags & SYSTEM_FLAG_CONFIG_ALLOW_RENAME) == 0) {
3181 ldb_asprintf_errstring(ldb,
3182 "subtree_rename: Cannot rename %s to %s within config partition",
3183 ldb_dn_get_linearized(olddn), ldb_dn_get_linearized(newdn));
3184 return LDB_ERR_UNWILLING_TO_PERFORM;
3186 } else if (ldb_dn_compare(nc_root, ldb_get_default_basedn(ldb)) == 0) {
3187 if (move_op &&
3188 (systemFlags & SYSTEM_FLAG_DOMAIN_DISALLOW_MOVE) != 0) {
3189 ldb_asprintf_errstring(ldb,
3190 "subtree_rename: Cannot move %s to %s - DISALLOW_MOVE set",
3191 ldb_dn_get_linearized(olddn), ldb_dn_get_linearized(newdn));
3192 return LDB_ERR_UNWILLING_TO_PERFORM;
3194 if (rename_op &&
3195 (systemFlags & SYSTEM_FLAG_DOMAIN_DISALLOW_RENAME) != 0) {
3196 ldb_asprintf_errstring(ldb,
3197 "subtree_rename: Cannot rename %s to %s - DISALLOW_RENAME set",
3198 ldb_dn_get_linearized(olddn), ldb_dn_get_linearized(newdn));
3199 return LDB_ERR_UNWILLING_TO_PERFORM;
3203 talloc_free(nc_root);
3205 return LDB_SUCCESS;
3209 static int samldb_rename_search_base_callback(struct ldb_request *req,
3210 struct ldb_reply *ares)
3212 struct samldb_ctx *ac;
3213 int ret;
3215 ac = talloc_get_type(req->context, struct samldb_ctx);
3217 if (!ares) {
3218 return ldb_module_done(ac->req, NULL, NULL,
3219 LDB_ERR_OPERATIONS_ERROR);
3221 if (ares->error != LDB_SUCCESS) {
3222 return ldb_module_done(ac->req, ares->controls,
3223 ares->response, ares->error);
3226 switch (ares->type) {
3227 case LDB_REPLY_ENTRY:
3229 * This is the root entry of the originating move
3230 * respectively rename request. It has been already
3231 * stored in the list using "subtree_rename_search()".
3232 * Only this one is subject to constraint checking.
3234 ret = check_rename_constraints(ares->message, ac,
3235 ac->req->op.rename.olddn,
3236 ac->req->op.rename.newdn);
3237 if (ret != LDB_SUCCESS) {
3238 return ldb_module_done(ac->req, NULL, NULL,
3239 ret);
3241 break;
3243 case LDB_REPLY_REFERRAL:
3244 /* ignore */
3245 break;
3247 case LDB_REPLY_DONE:
3250 * Great, no problem with the rename, so go ahead as
3251 * if we never were here
3253 ret = ldb_next_request(ac->module, ac->req);
3254 talloc_free(ares);
3255 return ret;
3258 talloc_free(ares);
3259 return LDB_SUCCESS;
3263 /* rename */
3264 static int samldb_rename(struct ldb_module *module, struct ldb_request *req)
3266 struct ldb_context *ldb;
3267 static const char * const attrs[] = { "objectClass", "systemFlags",
3268 "isDeleted", NULL };
3269 struct ldb_request *search_req;
3270 struct samldb_ctx *ac;
3271 int ret;
3273 if (ldb_dn_is_special(req->op.rename.olddn)) { /* do not manipulate our control entries */
3274 return ldb_next_request(module, req);
3277 ldb = ldb_module_get_ctx(module);
3279 ac = samldb_ctx_init(module, req);
3280 if (!ac) {
3281 return ldb_oom(ldb);
3284 ret = ldb_build_search_req(&search_req, ldb, ac,
3285 req->op.rename.olddn,
3286 LDB_SCOPE_BASE,
3287 "(objectClass=*)",
3288 attrs,
3289 NULL,
3291 samldb_rename_search_base_callback,
3292 req);
3293 LDB_REQ_SET_LOCATION(search_req);
3294 if (ret != LDB_SUCCESS) {
3295 return ret;
3298 ret = ldb_request_add_control(search_req, LDB_CONTROL_SHOW_RECYCLED_OID,
3299 true, NULL);
3300 if (ret != LDB_SUCCESS) {
3301 return ret;
3304 return ldb_next_request(ac->module, search_req);
3307 /* extended */
3309 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
3311 struct ldb_context *ldb = ldb_module_get_ctx(module);
3312 struct dsdb_fsmo_extended_op *exop;
3313 int ret;
3315 exop = talloc_get_type(req->op.extended.data,
3316 struct dsdb_fsmo_extended_op);
3317 if (!exop) {
3318 ldb_set_errstring(ldb,
3319 "samldb_extended_allocate_rid_pool: invalid extended data");
3320 return LDB_ERR_PROTOCOL_ERROR;
3323 ret = ridalloc_allocate_rid_pool_fsmo(module, exop, req);
3324 if (ret != LDB_SUCCESS) {
3325 return ret;
3328 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
3331 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
3333 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
3334 return samldb_extended_allocate_rid_pool(module, req);
3337 return ldb_next_request(module, req);
3341 static const struct ldb_module_ops ldb_samldb_module_ops = {
3342 .name = "samldb",
3343 .add = samldb_add,
3344 .modify = samldb_modify,
3345 .del = samldb_delete,
3346 .rename = samldb_rename,
3347 .extended = samldb_extended
3351 int ldb_samldb_module_init(const char *version)
3353 LDB_MODULE_CHECK_VERSION(version);
3354 return ldb_register_module(&ldb_samldb_module_ops);