dsdb-schema: do not reload more often than schema_reload_interval
[Samba/gbeck.git] / source4 / dsdb / samdb / ldb_modules / samldb.c
blob7b98dd62c1fe2d6c319b58590a3a1f573d2081ec
1 /*
2 SAM ldb module
4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
5 Copyright (C) Simo Sorce 2004-2008
6 Copyright (C) Matthias Dieter Wallnöfer 2009-2011
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 "dsdb/samdb/samdb.h"
37 #include "dsdb/samdb/ldb_modules/util.h"
38 #include "dsdb/samdb/ldb_modules/ridalloc.h"
39 #include "libcli/security/security.h"
40 #include "librpc/gen_ndr/ndr_security.h"
41 #include "ldb_wrap.h"
42 #include "param/param.h"
43 #include "libds/common/flag_mapping.h"
45 struct samldb_ctx;
46 enum samldb_add_type {
47 SAMLDB_TYPE_USER,
48 SAMLDB_TYPE_GROUP,
49 SAMLDB_TYPE_CLASS,
50 SAMLDB_TYPE_ATTRIBUTE
53 typedef int (*samldb_step_fn_t)(struct samldb_ctx *);
55 struct samldb_step {
56 struct samldb_step *next;
57 samldb_step_fn_t fn;
60 struct samldb_ctx {
61 struct ldb_module *module;
62 struct ldb_request *req;
64 /* used for add operations */
65 enum samldb_add_type type;
67 /* the resulting message */
68 struct ldb_message *msg;
70 /* used in "samldb_find_for_defaultObjectCategory" */
71 struct ldb_dn *dn, *res_dn;
73 /* all the async steps necessary to complete the operation */
74 struct samldb_step *steps;
75 struct samldb_step *curstep;
77 /* If someone set an ares to forward controls and response back to the caller */
78 struct ldb_reply *ares;
81 static struct samldb_ctx *samldb_ctx_init(struct ldb_module *module,
82 struct ldb_request *req)
84 struct ldb_context *ldb;
85 struct samldb_ctx *ac;
87 ldb = ldb_module_get_ctx(module);
89 ac = talloc_zero(req, struct samldb_ctx);
90 if (ac == NULL) {
91 ldb_oom(ldb);
92 return NULL;
95 ac->module = module;
96 ac->req = req;
98 return ac;
101 static int samldb_add_step(struct samldb_ctx *ac, samldb_step_fn_t fn)
103 struct samldb_step *step, *stepper;
105 step = talloc_zero(ac, struct samldb_step);
106 if (step == NULL) {
107 return ldb_oom(ldb_module_get_ctx(ac->module));
110 step->fn = fn;
112 if (ac->steps == NULL) {
113 ac->steps = step;
114 ac->curstep = step;
115 } else {
116 if (ac->curstep == NULL)
117 return ldb_operr(ldb_module_get_ctx(ac->module));
118 for (stepper = ac->curstep; stepper->next != NULL;
119 stepper = stepper->next);
120 stepper->next = step;
123 return LDB_SUCCESS;
126 static int samldb_first_step(struct samldb_ctx *ac)
128 if (ac->steps == NULL) {
129 return ldb_operr(ldb_module_get_ctx(ac->module));
132 ac->curstep = ac->steps;
133 return ac->curstep->fn(ac);
136 static int samldb_next_step(struct samldb_ctx *ac)
138 if (ac->curstep->next) {
139 ac->curstep = ac->curstep->next;
140 return ac->curstep->fn(ac);
143 /* We exit the samldb module here. If someone set an "ares" to forward
144 * controls and response back to the caller, use them. */
145 if (ac->ares) {
146 return ldb_module_done(ac->req, ac->ares->controls,
147 ac->ares->response, LDB_SUCCESS);
148 } else {
149 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
154 /* sAMAccountName handling */
156 static int samldb_generate_sAMAccountName(struct ldb_context *ldb,
157 struct ldb_message *msg)
159 char *name;
161 /* Format: $000000-000000000000 */
163 name = talloc_asprintf(msg, "$%.6X-%.6X%.6X",
164 (unsigned int)generate_random(),
165 (unsigned int)generate_random(),
166 (unsigned int)generate_random());
167 if (name == NULL) {
168 return ldb_oom(ldb);
170 return ldb_msg_add_steal_string(msg, "sAMAccountName", name);
173 static int samldb_check_sAMAccountName(struct samldb_ctx *ac)
175 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
176 const char *name;
177 int ret;
178 struct ldb_result *res;
179 const char * const noattrs[] = { NULL };
181 if (ldb_msg_find_element(ac->msg, "sAMAccountName") == NULL) {
182 ret = samldb_generate_sAMAccountName(ldb, ac->msg);
183 if (ret != LDB_SUCCESS) {
184 return ret;
188 name = ldb_msg_find_attr_as_string(ac->msg, "sAMAccountName", NULL);
189 if (name == NULL) {
190 /* The "sAMAccountName" cannot be nothing */
191 ldb_set_errstring(ldb,
192 "samldb: Empty account names aren't allowed!");
193 return LDB_ERR_CONSTRAINT_VIOLATION;
196 ret = dsdb_module_search(ac->module, ac, &res,
197 ldb_get_default_basedn(ldb), LDB_SCOPE_SUBTREE, noattrs,
198 DSDB_FLAG_NEXT_MODULE,
199 ac->req,
200 "(sAMAccountName=%s)",
201 ldb_binary_encode_string(ac, name));
202 if (ret != LDB_SUCCESS) {
203 return ret;
205 if (res->count != 0) {
206 ldb_asprintf_errstring(ldb,
207 "samldb: Account name (sAMAccountName) '%s' already in use!",
208 name);
209 talloc_free(res);
210 return LDB_ERR_ENTRY_ALREADY_EXISTS;
212 talloc_free(res);
214 return samldb_next_step(ac);
218 static bool samldb_msg_add_sid(struct ldb_message *msg,
219 const char *name,
220 const struct dom_sid *sid)
222 struct ldb_val v;
223 enum ndr_err_code ndr_err;
225 ndr_err = ndr_push_struct_blob(&v, msg, sid,
226 (ndr_push_flags_fn_t)ndr_push_dom_sid);
227 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
228 return false;
230 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
234 /* allocate a SID using our RID Set */
235 static int samldb_allocate_sid(struct samldb_ctx *ac)
237 uint32_t rid;
238 struct dom_sid *sid;
239 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
240 int ret;
242 ret = ridalloc_allocate_rid(ac->module, &rid, ac->req);
243 if (ret != LDB_SUCCESS) {
244 return ret;
247 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
248 if (sid == NULL) {
249 return ldb_module_oom(ac->module);
252 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
253 return ldb_operr(ldb);
256 return samldb_next_step(ac);
260 see if a krbtgt_number is available
262 static bool samldb_krbtgtnumber_available(struct samldb_ctx *ac,
263 uint32_t krbtgt_number)
265 TALLOC_CTX *tmp_ctx = talloc_new(ac);
266 struct ldb_result *res;
267 const char * const no_attrs[] = { NULL };
268 int ret;
270 ret = dsdb_module_search(ac->module, tmp_ctx, &res,
271 ldb_get_default_basedn(ldb_module_get_ctx(ac->module)),
272 LDB_SCOPE_SUBTREE, no_attrs,
273 DSDB_FLAG_NEXT_MODULE,
274 ac->req,
275 "(msDC-SecondaryKrbTgtNumber=%u)",
276 krbtgt_number);
277 if (ret == LDB_SUCCESS && res->count == 0) {
278 talloc_free(tmp_ctx);
279 return true;
281 talloc_free(tmp_ctx);
282 return false;
285 /* special handling for add in RODC join */
286 static int samldb_rodc_add(struct samldb_ctx *ac)
288 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
289 uint32_t krbtgt_number, i_start, i;
290 int ret;
291 char *newpass;
292 struct ldb_val newpass_utf16;
294 /* find a unused msDC-SecondaryKrbTgtNumber */
295 i_start = generate_random() & 0xFFFF;
296 if (i_start == 0) {
297 i_start = 1;
300 for (i=i_start; i<=0xFFFF; i++) {
301 if (samldb_krbtgtnumber_available(ac, i)) {
302 krbtgt_number = i;
303 goto found;
306 for (i=1; i<i_start; i++) {
307 if (samldb_krbtgtnumber_available(ac, i)) {
308 krbtgt_number = i;
309 goto found;
313 ldb_asprintf_errstring(ldb,
314 "%08X: Unable to find available msDS-SecondaryKrbTgtNumber",
315 W_ERROR_V(WERR_NO_SYSTEM_RESOURCES));
316 return LDB_ERR_OTHER;
318 found:
319 ret = ldb_msg_add_empty(ac->msg, "msDS-SecondaryKrbTgtNumber",
320 LDB_FLAG_INTERNAL_DISABLE_VALIDATION, NULL);
321 if (ret != LDB_SUCCESS) {
322 return ldb_operr(ldb);
325 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
326 "msDS-SecondaryKrbTgtNumber", krbtgt_number);
327 if (ret != LDB_SUCCESS) {
328 return ldb_operr(ldb);
331 ret = ldb_msg_add_fmt(ac->msg, "sAMAccountName", "krbtgt_%u",
332 krbtgt_number);
333 if (ret != LDB_SUCCESS) {
334 return ldb_operr(ldb);
337 newpass = generate_random_password(ac->msg, 128, 255);
338 if (newpass == NULL) {
339 return ldb_operr(ldb);
342 if (!convert_string_talloc(ac,
343 CH_UNIX, CH_UTF16,
344 newpass, strlen(newpass),
345 (void *)&newpass_utf16.data,
346 &newpass_utf16.length)) {
347 ldb_asprintf_errstring(ldb,
348 "samldb_rodc_add: "
349 "failed to generate UTF16 password from random password");
350 return LDB_ERR_OPERATIONS_ERROR;
352 ret = ldb_msg_add_steal_value(ac->msg, "clearTextPassword", &newpass_utf16);
353 if (ret != LDB_SUCCESS) {
354 return ldb_operr(ldb);
357 return samldb_next_step(ac);
360 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
362 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
363 struct ldb_result *res;
364 const char * const no_attrs[] = { NULL };
365 int ret;
367 ac->res_dn = NULL;
369 ret = dsdb_module_search(ac->module, ac, &res,
370 ac->dn, LDB_SCOPE_BASE, no_attrs,
371 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
372 | DSDB_FLAG_NEXT_MODULE,
373 ac->req,
374 "(objectClass=classSchema)");
375 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
376 /* Don't be pricky when the DN doesn't exist if we have the */
377 /* RELAX control specified */
378 if (ldb_request_get_control(ac->req,
379 LDB_CONTROL_RELAX_OID) == NULL) {
380 ldb_set_errstring(ldb,
381 "samldb_find_defaultObjectCategory: "
382 "Invalid DN for 'defaultObjectCategory'!");
383 return LDB_ERR_CONSTRAINT_VIOLATION;
386 if ((ret != LDB_ERR_NO_SUCH_OBJECT) && (ret != LDB_SUCCESS)) {
387 return ret;
390 if (ret == LDB_SUCCESS) {
391 /* ensure the defaultObjectCategory has a full GUID */
392 struct ldb_message *m;
393 m = ldb_msg_new(ac->msg);
394 if (m == NULL) {
395 return ldb_oom(ldb);
397 m->dn = ac->msg->dn;
398 if (ldb_msg_add_string(m, "defaultObjectCategory",
399 ldb_dn_get_extended_linearized(m, res->msgs[0]->dn, 1)) !=
400 LDB_SUCCESS) {
401 return ldb_oom(ldb);
403 m->elements[0].flags = LDB_FLAG_MOD_REPLACE;
405 ret = dsdb_module_modify(ac->module, m,
406 DSDB_FLAG_NEXT_MODULE,
407 ac->req);
408 if (ret != LDB_SUCCESS) {
409 return ret;
414 ac->res_dn = ac->dn;
416 return samldb_next_step(ac);
420 * msDS-IntId attributeSchema attribute handling
421 * during LDB_ADD request processing
423 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
425 int ret;
426 bool id_exists;
427 uint32_t msds_intid;
428 int32_t system_flags;
429 struct ldb_context *ldb;
430 struct ldb_result *ldb_res;
431 struct ldb_dn *schema_dn;
432 struct samldb_msds_intid_persistant *msds_intid_struct;
433 struct dsdb_schema *schema;
435 ldb = ldb_module_get_ctx(ac->module);
436 schema_dn = ldb_get_schema_basedn(ldb);
438 /* replicated update should always go through */
439 if (ldb_request_get_control(ac->req,
440 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
441 return LDB_SUCCESS;
444 /* msDS-IntId is handled by system and should never be
445 * passed by clients */
446 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
447 return LDB_ERR_UNWILLING_TO_PERFORM;
450 /* do not generate msDS-IntId if Relax control is passed */
451 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
452 return LDB_SUCCESS;
455 /* check Functional Level */
456 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
457 return LDB_SUCCESS;
460 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
461 system_flags = ldb_msg_find_attr_as_int(ac->msg, "systemFlags", 0);
462 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
463 return LDB_SUCCESS;
466 /* Generate new value for msDs-IntId
467 * Value should be in 0x80000000..0xBFFFFFFF range */
468 msds_intid = generate_random() % 0X3FFFFFFF;
469 msds_intid += 0x80000000;
471 /* probe id values until unique one is found */
472 do {
473 msds_intid++;
474 if (msds_intid > 0xBFFFFFFF) {
475 msds_intid = 0x80000001;
478 ret = dsdb_module_search(ac->module, ac,
479 &ldb_res,
480 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
481 DSDB_FLAG_NEXT_MODULE,
482 ac->req,
483 "(msDS-IntId=%d)", msds_intid);
484 if (ret != LDB_SUCCESS) {
485 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
486 __location__": Searching for msDS-IntId=%d failed - %s\n",
487 msds_intid,
488 ldb_errstring(ldb));
489 return ldb_operr(ldb);
491 id_exists = (ldb_res->count > 0);
493 talloc_free(ldb_res);
494 } while(id_exists);
496 return samdb_msg_add_int(ldb, ac->msg, ac->msg, "msDS-IntId",
497 msds_intid);
502 * samldb_add_entry (async)
505 static int samldb_add_entry_callback(struct ldb_request *req,
506 struct ldb_reply *ares)
508 struct ldb_context *ldb;
509 struct samldb_ctx *ac;
510 int ret;
512 ac = talloc_get_type(req->context, struct samldb_ctx);
513 ldb = ldb_module_get_ctx(ac->module);
515 if (!ares) {
516 return ldb_module_done(ac->req, NULL, NULL,
517 LDB_ERR_OPERATIONS_ERROR);
520 if (ares->type == LDB_REPLY_REFERRAL) {
521 return ldb_module_send_referral(ac->req, ares->referral);
524 if (ares->error != LDB_SUCCESS) {
525 return ldb_module_done(ac->req, ares->controls,
526 ares->response, ares->error);
528 if (ares->type != LDB_REPLY_DONE) {
529 ldb_asprintf_errstring(ldb, "Invalid LDB reply type %d", ares->type);
530 return ldb_module_done(ac->req, NULL, NULL,
531 LDB_ERR_OPERATIONS_ERROR);
534 /* The caller may wish to get controls back from the add */
535 ac->ares = talloc_steal(ac, ares);
537 ret = samldb_next_step(ac);
538 if (ret != LDB_SUCCESS) {
539 return ldb_module_done(ac->req, NULL, NULL, ret);
541 return ret;
544 static int samldb_add_entry(struct samldb_ctx *ac)
546 struct ldb_context *ldb;
547 struct ldb_request *req;
548 int ret;
550 ldb = ldb_module_get_ctx(ac->module);
552 ret = ldb_build_add_req(&req, ldb, ac,
553 ac->msg,
554 ac->req->controls,
555 ac, samldb_add_entry_callback,
556 ac->req);
557 LDB_REQ_SET_LOCATION(req);
558 if (ret != LDB_SUCCESS) {
559 return ret;
562 return ldb_next_request(ac->module, req);
566 * return true if msg carries an attributeSchema that is intended to be RODC
567 * filtered but is also a system-critical attribute.
569 static bool check_rodc_critical_attribute(struct ldb_message *msg)
571 uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
573 schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
574 searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
575 rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE
576 | SEARCH_FLAG_CONFIDENTIAL);
578 if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
579 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
580 return true;
581 } else {
582 return false;
587 static int samldb_fill_object(struct samldb_ctx *ac)
589 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
590 int ret;
592 /* Add information for the different account types */
593 switch(ac->type) {
594 case SAMLDB_TYPE_USER: {
595 struct ldb_control *rodc_control = ldb_request_get_control(ac->req,
596 LDB_CONTROL_RODC_DCPROMO_OID);
597 if (rodc_control != NULL) {
598 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
599 rodc_control->critical = false;
600 ret = samldb_add_step(ac, samldb_rodc_add);
601 if (ret != LDB_SUCCESS) return ret;
604 /* check if we have a valid sAMAccountName */
605 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
606 if (ret != LDB_SUCCESS) return ret;
608 ret = samldb_add_step(ac, samldb_add_entry);
609 if (ret != LDB_SUCCESS) return ret;
610 break;
613 case SAMLDB_TYPE_GROUP: {
614 /* check if we have a valid sAMAccountName */
615 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
616 if (ret != LDB_SUCCESS) return ret;
618 ret = samldb_add_step(ac, samldb_add_entry);
619 if (ret != LDB_SUCCESS) return ret;
620 break;
623 case SAMLDB_TYPE_CLASS: {
624 const struct ldb_val *rdn_value, *def_obj_cat_val;
625 unsigned int v = ldb_msg_find_attr_as_uint(ac->msg, "objectClassCategory", -2);
627 /* As discussed with Microsoft through dochelp in April 2012 this is the behavior of windows*/
628 if (!ldb_msg_find_element(ac->msg, "subClassOf")) {
629 ret = ldb_msg_add_string(ac->msg, "subClassOf", "top");
630 if (ret != LDB_SUCCESS) return ret;
633 ret = samdb_find_or_add_attribute(ldb, ac->msg,
634 "rdnAttId", "cn");
635 if (ret != LDB_SUCCESS) return ret;
637 /* do not allow to mark an attributeSchema as RODC filtered if it
638 * is system-critical */
639 if (check_rodc_critical_attribute(ac->msg)) {
640 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
641 ldb_dn_get_linearized(ac->msg->dn));
642 return LDB_ERR_UNWILLING_TO_PERFORM;
645 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
646 if (rdn_value == NULL) {
647 return ldb_operr(ldb);
649 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
650 /* the RDN has prefix "CN" */
651 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
652 samdb_cn_to_lDAPDisplayName(ac->msg,
653 (const char *) rdn_value->data));
654 if (ret != LDB_SUCCESS) {
655 ldb_oom(ldb);
656 return ret;
660 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
661 struct GUID guid;
662 /* a new GUID */
663 guid = GUID_random();
664 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
665 if (ret != LDB_SUCCESS) {
666 ldb_oom(ldb);
667 return ret;
671 def_obj_cat_val = ldb_msg_find_ldb_val(ac->msg,
672 "defaultObjectCategory");
673 if (def_obj_cat_val != NULL) {
674 /* "defaultObjectCategory" has been set by the caller.
675 * Do some checks for consistency.
676 * NOTE: The real constraint check (that
677 * 'defaultObjectCategory' is the DN of the new
678 * objectclass or any parent of it) is still incomplete.
679 * For now we say that 'defaultObjectCategory' is valid
680 * if it exists and it is of objectclass "classSchema".
682 ac->dn = ldb_dn_from_ldb_val(ac, ldb, def_obj_cat_val);
683 if (ac->dn == NULL) {
684 ldb_set_errstring(ldb,
685 "Invalid DN for 'defaultObjectCategory'!");
686 return LDB_ERR_CONSTRAINT_VIOLATION;
688 } else {
689 /* "defaultObjectCategory" has not been set by the
690 * caller. Use the entry DN for it. */
691 ac->dn = ac->msg->dn;
693 ret = ldb_msg_add_string(ac->msg, "defaultObjectCategory",
694 ldb_dn_alloc_linearized(ac->msg, ac->dn));
695 if (ret != LDB_SUCCESS) {
696 ldb_oom(ldb);
697 return ret;
701 ret = samldb_add_step(ac, samldb_add_entry);
702 if (ret != LDB_SUCCESS) return ret;
704 /* Now perform the checks for the 'defaultObjectCategory'. The
705 * lookup DN was already saved in "ac->dn" */
706 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
707 if (ret != LDB_SUCCESS) return ret;
709 /* -2 is not a valid objectClassCategory so it means the attribute wasn't present */
710 if (v == -2) {
711 /* Windows 2003 does this*/
712 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "objectClassCategory", 0);
713 if (ret != LDB_SUCCESS) {
714 return ret;
717 break;
720 case SAMLDB_TYPE_ATTRIBUTE: {
721 const struct ldb_val *rdn_value;
722 struct ldb_message_element *el;
723 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
724 if (rdn_value == NULL) {
725 return ldb_operr(ldb);
727 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
728 /* the RDN has prefix "CN" */
729 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
730 samdb_cn_to_lDAPDisplayName(ac->msg,
731 (const char *) rdn_value->data));
732 if (ret != LDB_SUCCESS) {
733 ldb_oom(ldb);
734 return ret;
738 /* do not allow to mark an attributeSchema as RODC filtered if it
739 * is system-critical */
740 if (check_rodc_critical_attribute(ac->msg)) {
741 ldb_asprintf_errstring(ldb,
742 "samldb: refusing schema add of %s - cannot combine critical attribute with RODC filtering",
743 ldb_dn_get_linearized(ac->msg->dn));
744 return LDB_ERR_UNWILLING_TO_PERFORM;
747 ret = samdb_find_or_add_attribute(ldb, ac->msg,
748 "isSingleValued", "FALSE");
749 if (ret != LDB_SUCCESS) return ret;
751 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
752 struct GUID guid;
753 /* a new GUID */
754 guid = GUID_random();
755 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
756 if (ret != LDB_SUCCESS) {
757 ldb_oom(ldb);
758 return ret;
762 el = ldb_msg_find_element(ac->msg, "attributeSyntax");
763 if (el) {
765 * No need to scream if there isn't as we have code later on
766 * that will take care of it.
768 const struct dsdb_syntax *syntax = find_syntax_map_by_ad_oid((const char *)el->values[0].data);
769 if (!syntax) {
770 DEBUG(9, ("Can't find dsdb_syntax object for attributeSyntax %s\n",
771 (const char *)el->values[0].data));
772 } else {
773 unsigned int v = ldb_msg_find_attr_as_uint(ac->msg, "oMSyntax", 0);
774 const struct ldb_val *val = ldb_msg_find_ldb_val(ac->msg, "oMObjectClass");
776 if (v == 0) {
777 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "oMSyntax", syntax->oMSyntax);
778 if (ret != LDB_SUCCESS) {
779 return ret;
782 if (!val) {
783 struct ldb_val val2 = ldb_val_dup(ldb, &syntax->oMObjectClass);
784 if (val2.length > 0) {
785 ret = ldb_msg_add_value(ac->msg, "oMObjectClass", &val2, NULL);
786 if (ret != LDB_SUCCESS) {
787 return ret;
794 /* handle msDS-IntID attribute */
795 ret = samldb_add_handle_msDS_IntId(ac);
796 if (ret != LDB_SUCCESS) return ret;
798 ret = samldb_add_step(ac, samldb_add_entry);
799 if (ret != LDB_SUCCESS) return ret;
800 break;
803 default:
804 ldb_asprintf_errstring(ldb, "Invalid entry type!");
805 return LDB_ERR_OPERATIONS_ERROR;
806 break;
809 return samldb_first_step(ac);
812 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
814 struct ldb_context *ldb;
815 const struct ldb_val *rdn_value;
816 struct dom_sid *sid;
817 int ret;
819 ldb = ldb_module_get_ctx(ac->module);
821 sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
822 if (sid == NULL) {
823 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
824 if (rdn_value == NULL) {
825 return ldb_operr(ldb);
827 sid = dom_sid_parse_talloc(ac->msg,
828 (const char *)rdn_value->data);
829 if (sid == NULL) {
830 ldb_set_errstring(ldb,
831 "samldb: No valid SID found in ForeignSecurityPrincipal CN!");
832 return LDB_ERR_CONSTRAINT_VIOLATION;
834 if (! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
835 return ldb_operr(ldb);
839 /* finally proceed with adding the entry */
840 ret = samldb_add_step(ac, samldb_add_entry);
841 if (ret != LDB_SUCCESS) return ret;
843 return samldb_first_step(ac);
846 static int samldb_schema_info_update(struct samldb_ctx *ac)
848 int ret;
849 struct ldb_context *ldb;
850 struct dsdb_schema *schema;
852 /* replicated update should always go through */
853 if (ldb_request_get_control(ac->req,
854 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
855 return LDB_SUCCESS;
858 /* do not update schemaInfo during provisioning */
859 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
860 return LDB_SUCCESS;
863 ldb = ldb_module_get_ctx(ac->module);
864 schema = dsdb_get_schema(ldb, NULL);
865 if (!schema) {
866 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
867 "samldb_schema_info_update: no dsdb_schema loaded");
868 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
869 return ldb_operr(ldb);
872 ret = dsdb_module_schema_info_update(ac->module, schema,
873 DSDB_FLAG_NEXT_MODULE|
874 DSDB_FLAG_AS_SYSTEM,
875 ac->req);
876 if (ret != LDB_SUCCESS) {
877 ldb_asprintf_errstring(ldb,
878 "samldb_schema_info_update: dsdb_module_schema_info_update failed with %s",
879 ldb_errstring(ldb));
880 return ret;
883 return LDB_SUCCESS;
886 static int samldb_prim_group_tester(struct samldb_ctx *ac, uint32_t rid);
889 * "Objectclass" trigger (MS-SAMR 3.1.1.8.1)
891 * Has to be invoked on "add" and "modify" operations on "user", "computer" and
892 * "group" objects.
893 * ac->msg contains the "add"/"modify" message
894 * ac->type contains the object type (main objectclass)
896 static int samldb_objectclass_trigger(struct samldb_ctx *ac)
898 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
899 void *skip_allocate_sids = ldb_get_opaque(ldb,
900 "skip_allocate_sids");
901 struct ldb_message_element *el, *el2;
902 struct dom_sid *sid;
903 int ret;
905 /* make sure that "sAMAccountType" is not specified */
906 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
907 if (el != NULL) {
908 ldb_set_errstring(ldb,
909 "samldb: sAMAccountType must not be specified!");
910 return LDB_ERR_UNWILLING_TO_PERFORM;
913 /* Step 1: objectSid assignment */
915 /* Don't allow the objectSid to be changed. But beside the RELAX
916 * control we have also to guarantee that it can always be set with
917 * SYSTEM permissions. This is needed for the "samba3sam" backend. */
918 sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
919 if ((sid != NULL) && (!dsdb_module_am_system(ac->module)) &&
920 (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
921 ldb_set_errstring(ldb,
922 "samldb: objectSid must not be specified!");
923 return LDB_ERR_UNWILLING_TO_PERFORM;
926 /* but generate a new SID when we do have an add operations */
927 if ((sid == NULL) && (ac->req->operation == LDB_ADD) && !skip_allocate_sids) {
928 ret = samldb_add_step(ac, samldb_allocate_sid);
929 if (ret != LDB_SUCCESS) return ret;
932 switch(ac->type) {
933 case SAMLDB_TYPE_USER: {
934 bool uac_generated = false;
936 /* Step 1.2: Default values */
937 ret = samdb_find_or_add_attribute(ldb, ac->msg,
938 "accountExpires", "9223372036854775807");
939 if (ret != LDB_SUCCESS) return ret;
940 ret = samdb_find_or_add_attribute(ldb, ac->msg,
941 "badPasswordTime", "0");
942 if (ret != LDB_SUCCESS) return ret;
943 ret = samdb_find_or_add_attribute(ldb, ac->msg,
944 "badPwdCount", "0");
945 if (ret != LDB_SUCCESS) return ret;
946 ret = samdb_find_or_add_attribute(ldb, ac->msg,
947 "codePage", "0");
948 if (ret != LDB_SUCCESS) return ret;
949 ret = samdb_find_or_add_attribute(ldb, ac->msg,
950 "countryCode", "0");
951 if (ret != LDB_SUCCESS) return ret;
952 ret = samdb_find_or_add_attribute(ldb, ac->msg,
953 "lastLogoff", "0");
954 if (ret != LDB_SUCCESS) return ret;
955 ret = samdb_find_or_add_attribute(ldb, ac->msg,
956 "lastLogon", "0");
957 if (ret != LDB_SUCCESS) return ret;
958 ret = samdb_find_or_add_attribute(ldb, ac->msg,
959 "logonCount", "0");
960 if (ret != LDB_SUCCESS) return ret;
961 ret = samdb_find_or_add_attribute(ldb, ac->msg,
962 "pwdLastSet", "0");
963 if (ret != LDB_SUCCESS) return ret;
965 /* On add operations we might need to generate a
966 * "userAccountControl" (if it isn't specified). */
967 el = ldb_msg_find_element(ac->msg, "userAccountControl");
968 if ((el == NULL) && (ac->req->operation == LDB_ADD)) {
969 ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
970 "userAccountControl",
971 UF_NORMAL_ACCOUNT);
972 if (ret != LDB_SUCCESS) {
973 return ret;
975 uac_generated = true;
978 el = ldb_msg_find_element(ac->msg, "userAccountControl");
979 if (el != NULL) {
980 uint32_t user_account_control, account_type;
982 /* Step 1.3: "userAccountControl" -> "sAMAccountType" mapping */
983 user_account_control = ldb_msg_find_attr_as_uint(ac->msg,
984 "userAccountControl",
987 /* Temporary duplicate accounts aren't allowed */
988 if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
989 return LDB_ERR_OTHER;
992 /* Workstation and (read-only) DC objects do need objectclass "computer" */
993 if ((samdb_find_attribute(ldb, ac->msg,
994 "objectclass", "computer") == NULL) &&
995 (user_account_control &
996 (UF_SERVER_TRUST_ACCOUNT | UF_WORKSTATION_TRUST_ACCOUNT))) {
997 ldb_set_errstring(ldb,
998 "samldb: Requested account type does need objectclass 'computer'!");
999 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1002 account_type = ds_uf2atype(user_account_control);
1003 if (account_type == 0) {
1004 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1005 return LDB_ERR_UNWILLING_TO_PERFORM;
1007 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1008 "sAMAccountType",
1009 account_type);
1010 if (ret != LDB_SUCCESS) {
1011 return ret;
1013 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
1014 el2->flags = LDB_FLAG_MOD_REPLACE;
1016 /* "isCriticalSystemObject" might be set */
1017 if (user_account_control &
1018 (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
1019 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1020 "TRUE");
1021 if (ret != LDB_SUCCESS) {
1022 return ret;
1024 el2 = ldb_msg_find_element(ac->msg,
1025 "isCriticalSystemObject");
1026 el2->flags = LDB_FLAG_MOD_REPLACE;
1027 } else if (user_account_control & UF_WORKSTATION_TRUST_ACCOUNT) {
1028 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1029 "FALSE");
1030 if (ret != LDB_SUCCESS) {
1031 return ret;
1033 el2 = ldb_msg_find_element(ac->msg,
1034 "isCriticalSystemObject");
1035 el2->flags = LDB_FLAG_MOD_REPLACE;
1038 /* Step 1.4: "userAccountControl" -> "primaryGroupID" mapping */
1039 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
1040 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
1043 * Older AD deployments don't know about the
1044 * RODC group
1046 if (rid == DOMAIN_RID_READONLY_DCS) {
1047 ret = samldb_prim_group_tester(ac, rid);
1048 if (ret != LDB_SUCCESS) {
1049 return ret;
1053 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1054 "primaryGroupID", rid);
1055 if (ret != LDB_SUCCESS) {
1056 return ret;
1058 el2 = ldb_msg_find_element(ac->msg,
1059 "primaryGroupID");
1060 el2->flags = LDB_FLAG_MOD_REPLACE;
1063 /* Step 1.5: Add additional flags when needed */
1064 /* Obviously this is done when the "userAccountControl"
1065 * has been generated here (tested against Windows
1066 * Server) */
1067 if (uac_generated) {
1068 user_account_control |= UF_ACCOUNTDISABLE;
1069 user_account_control |= UF_PASSWD_NOTREQD;
1071 ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
1072 "userAccountControl",
1073 user_account_control);
1074 if (ret != LDB_SUCCESS) {
1075 return ret;
1079 break;
1082 case SAMLDB_TYPE_GROUP: {
1083 const char *tempstr;
1085 /* Step 2.2: Default values */
1086 tempstr = talloc_asprintf(ac->msg, "%d",
1087 GTYPE_SECURITY_GLOBAL_GROUP);
1088 if (tempstr == NULL) return ldb_operr(ldb);
1089 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1090 "groupType", tempstr);
1091 if (ret != LDB_SUCCESS) return ret;
1093 /* Step 2.3: "groupType" -> "sAMAccountType" */
1094 el = ldb_msg_find_element(ac->msg, "groupType");
1095 if (el != NULL) {
1096 uint32_t group_type, account_type;
1098 group_type = ldb_msg_find_attr_as_uint(ac->msg,
1099 "groupType", 0);
1101 /* The creation of builtin groups requires the
1102 * RELAX control */
1103 if (group_type == GTYPE_SECURITY_BUILTIN_LOCAL_GROUP) {
1104 if (ldb_request_get_control(ac->req,
1105 LDB_CONTROL_RELAX_OID) == NULL) {
1106 return LDB_ERR_UNWILLING_TO_PERFORM;
1110 account_type = ds_gtype2atype(group_type);
1111 if (account_type == 0) {
1112 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1113 return LDB_ERR_UNWILLING_TO_PERFORM;
1115 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1116 "sAMAccountType",
1117 account_type);
1118 if (ret != LDB_SUCCESS) {
1119 return ret;
1121 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
1122 el2->flags = LDB_FLAG_MOD_REPLACE;
1124 break;
1127 default:
1128 ldb_asprintf_errstring(ldb,
1129 "Invalid entry type!");
1130 return LDB_ERR_OPERATIONS_ERROR;
1131 break;
1134 return LDB_SUCCESS;
1138 * "Primary group ID" trigger (MS-SAMR 3.1.1.8.2)
1140 * Has to be invoked on "add" and "modify" operations on "user" and "computer"
1141 * objects.
1142 * ac->msg contains the "add"/"modify" message
1145 static int samldb_prim_group_tester(struct samldb_ctx *ac, uint32_t rid)
1147 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1148 struct dom_sid *sid;
1149 struct ldb_result *res;
1150 int ret;
1151 const char * const noattrs[] = { NULL };
1153 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1154 if (sid == NULL) {
1155 return ldb_operr(ldb);
1158 ret = dsdb_module_search(ac->module, ac, &res,
1159 ldb_get_default_basedn(ldb),
1160 LDB_SCOPE_SUBTREE,
1161 noattrs, DSDB_FLAG_NEXT_MODULE,
1162 ac->req,
1163 "(objectSid=%s)",
1164 ldap_encode_ndr_dom_sid(ac, sid));
1165 if (ret != LDB_SUCCESS) {
1166 return ret;
1168 if (res->count != 1) {
1169 talloc_free(res);
1170 ldb_asprintf_errstring(ldb,
1171 "Failed to find primary group with RID %u!",
1172 rid);
1173 return LDB_ERR_UNWILLING_TO_PERFORM;
1175 talloc_free(res);
1177 return LDB_SUCCESS;
1180 static int samldb_prim_group_set(struct samldb_ctx *ac)
1182 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1183 uint32_t rid;
1185 rid = ldb_msg_find_attr_as_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
1186 if (rid == (uint32_t) -1) {
1187 /* we aren't affected of any primary group set */
1188 return LDB_SUCCESS;
1190 } else if (!ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
1191 ldb_set_errstring(ldb,
1192 "The primary group isn't settable on add operations!");
1193 return LDB_ERR_UNWILLING_TO_PERFORM;
1196 return samldb_prim_group_tester(ac, rid);
1199 static int samldb_prim_group_change(struct samldb_ctx *ac)
1201 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1202 const char * const attrs[] = { "primaryGroupID", "memberOf", NULL };
1203 struct ldb_result *res, *group_res;
1204 struct ldb_message_element *el;
1205 struct ldb_message *msg;
1206 uint32_t prev_rid, new_rid;
1207 struct dom_sid *prev_sid, *new_sid;
1208 struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
1209 int ret;
1210 const char * const noattrs[] = { NULL };
1212 el = dsdb_get_single_valued_attr(ac->msg, "primaryGroupID",
1213 ac->req->operation);
1214 if (el == NULL) {
1215 /* we are not affected */
1216 return LDB_SUCCESS;
1219 /* Fetch information from the existing object */
1221 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1222 DSDB_FLAG_NEXT_MODULE, ac->req);
1223 if (ret != LDB_SUCCESS) {
1224 return ret;
1227 /* Finds out the DN of the old primary group */
1229 prev_rid = ldb_msg_find_attr_as_uint(res->msgs[0], "primaryGroupID",
1230 (uint32_t) -1);
1231 if (prev_rid == (uint32_t) -1) {
1232 /* User objects do always have a mandatory "primaryGroupID"
1233 * attribute. If this doesn't exist then the object is of the
1234 * wrong type. This is the exact Windows error code */
1235 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1238 prev_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), prev_rid);
1239 if (prev_sid == NULL) {
1240 return ldb_operr(ldb);
1243 /* Finds out the DN of the new primary group
1244 * Notice: in order to parse the primary group ID correctly we create
1245 * a temporary message here. */
1247 msg = ldb_msg_new(ac->msg);
1248 if (msg == NULL) {
1249 return ldb_module_oom(ac->module);
1251 ret = ldb_msg_add(msg, el, 0);
1252 if (ret != LDB_SUCCESS) {
1253 return ret;
1255 new_rid = ldb_msg_find_attr_as_uint(msg, "primaryGroupID", (uint32_t) -1);
1256 talloc_free(msg);
1257 if (new_rid == (uint32_t) -1) {
1258 /* we aren't affected of any primary group change */
1259 return LDB_SUCCESS;
1262 if (prev_rid == new_rid) {
1263 return LDB_SUCCESS;
1266 ret = dsdb_module_search(ac->module, ac, &group_res,
1267 ldb_get_default_basedn(ldb),
1268 LDB_SCOPE_SUBTREE,
1269 noattrs, DSDB_FLAG_NEXT_MODULE,
1270 ac->req,
1271 "(objectSid=%s)",
1272 ldap_encode_ndr_dom_sid(ac, prev_sid));
1273 if (ret != LDB_SUCCESS) {
1274 return ret;
1276 if (group_res->count != 1) {
1277 return ldb_operr(ldb);
1279 prev_prim_group_dn = group_res->msgs[0]->dn;
1281 new_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), new_rid);
1282 if (new_sid == NULL) {
1283 return ldb_operr(ldb);
1286 ret = dsdb_module_search(ac->module, ac, &group_res,
1287 ldb_get_default_basedn(ldb),
1288 LDB_SCOPE_SUBTREE,
1289 noattrs, DSDB_FLAG_NEXT_MODULE,
1290 ac->req,
1291 "(objectSid=%s)",
1292 ldap_encode_ndr_dom_sid(ac, new_sid));
1293 if (ret != LDB_SUCCESS) {
1294 return ret;
1296 if (group_res->count != 1) {
1297 /* Here we know if the specified new primary group candidate is
1298 * valid or not. */
1299 return LDB_ERR_UNWILLING_TO_PERFORM;
1301 new_prim_group_dn = group_res->msgs[0]->dn;
1303 /* We need to be already a normal member of the new primary
1304 * group in order to be successful. */
1305 el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
1306 ldb_dn_get_linearized(new_prim_group_dn));
1307 if (el == NULL) {
1308 return LDB_ERR_UNWILLING_TO_PERFORM;
1311 /* Remove the "member" attribute on the new primary group */
1312 msg = ldb_msg_new(ac->msg);
1313 if (msg == NULL) {
1314 return ldb_module_oom(ac->module);
1316 msg->dn = new_prim_group_dn;
1318 ret = samdb_msg_add_delval(ldb, msg, msg, "member",
1319 ldb_dn_get_linearized(ac->msg->dn));
1320 if (ret != LDB_SUCCESS) {
1321 return ret;
1324 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE, ac->req);
1325 if (ret != LDB_SUCCESS) {
1326 return ret;
1328 talloc_free(msg);
1330 /* Add a "member" attribute for the previous primary group */
1331 msg = ldb_msg_new(ac->msg);
1332 if (msg == NULL) {
1333 return ldb_module_oom(ac->module);
1335 msg->dn = prev_prim_group_dn;
1337 ret = samdb_msg_add_addval(ldb, msg, msg, "member",
1338 ldb_dn_get_linearized(ac->msg->dn));
1339 if (ret != LDB_SUCCESS) {
1340 return ret;
1343 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE, ac->req);
1344 if (ret != LDB_SUCCESS) {
1345 return ret;
1347 talloc_free(msg);
1349 return LDB_SUCCESS;
1352 static int samldb_prim_group_trigger(struct samldb_ctx *ac)
1354 int ret;
1356 if (ac->req->operation == LDB_ADD) {
1357 ret = samldb_prim_group_set(ac);
1358 } else {
1359 ret = samldb_prim_group_change(ac);
1362 return ret;
1367 * This function is called on LDB modify operations. It performs some additions/
1368 * replaces on the current LDB message when "userAccountControl" changes.
1370 static int samldb_user_account_control_change(struct samldb_ctx *ac)
1372 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1373 uint32_t user_account_control, old_user_account_control, account_type;
1374 struct ldb_message_element *el;
1375 struct ldb_message *tmp_msg;
1376 int ret;
1377 struct ldb_result *res;
1378 const char * const attrs[] = { "userAccountControl", "objectClass", NULL };
1379 unsigned int i;
1380 bool is_computer = false;
1382 el = dsdb_get_single_valued_attr(ac->msg, "userAccountControl",
1383 ac->req->operation);
1384 if (el == NULL) {
1385 /* we are not affected */
1386 return LDB_SUCCESS;
1389 /* Create a temporary message for fetching the "userAccountControl" */
1390 tmp_msg = ldb_msg_new(ac->msg);
1391 if (tmp_msg == NULL) {
1392 return ldb_module_oom(ac->module);
1394 ret = ldb_msg_add(tmp_msg, el, 0);
1395 if (ret != LDB_SUCCESS) {
1396 return ret;
1398 user_account_control = ldb_msg_find_attr_as_uint(tmp_msg,
1399 "userAccountControl",
1401 talloc_free(tmp_msg);
1403 /* Temporary duplicate accounts aren't allowed */
1404 if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
1405 return LDB_ERR_OTHER;
1408 /* Fetch the old "userAccountControl" and "objectClass" */
1409 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1410 DSDB_FLAG_NEXT_MODULE, ac->req);
1411 if (ret != LDB_SUCCESS) {
1412 return ret;
1414 old_user_account_control = ldb_msg_find_attr_as_uint(res->msgs[0], "userAccountControl", 0);
1415 if (old_user_account_control == 0) {
1416 return ldb_operr(ldb);
1418 el = ldb_msg_find_element(res->msgs[0], "objectClass");
1419 if (el == NULL) {
1420 return ldb_operr(ldb);
1423 /* When we do not have objectclass "computer" we cannot switch to a (read-only) DC */
1424 for (i = 0; i < el->num_values; i++) {
1425 if (ldb_attr_cmp((char *)el->values[i].data, "computer") == 0) {
1426 is_computer = true;
1427 break;
1430 if (!is_computer &&
1431 (user_account_control & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT))) {
1432 ldb_set_errstring(ldb,
1433 "samldb: Requested account type does need objectclass 'computer'!");
1434 return LDB_ERR_UNWILLING_TO_PERFORM;
1438 * The functions "ds_uf2atype" and "ds_uf2prim_group_rid" are used as
1439 * detectors for account type changes.
1440 * So if the account type does change then we need to adjust the
1441 * "sAMAccountType", the "isCriticalSystemObject" and the
1442 * "primaryGroupID" attribute.
1444 if ((ds_uf2atype(user_account_control)
1445 == ds_uf2atype(old_user_account_control)) &&
1446 (ds_uf2prim_group_rid(user_account_control)
1447 == ds_uf2prim_group_rid(old_user_account_control))) {
1448 return LDB_SUCCESS;
1451 account_type = ds_uf2atype(user_account_control);
1452 if (account_type == 0) {
1453 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1454 return LDB_ERR_UNWILLING_TO_PERFORM;
1456 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
1457 account_type);
1458 if (ret != LDB_SUCCESS) {
1459 return ret;
1461 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
1462 el->flags = LDB_FLAG_MOD_REPLACE;
1464 /* "isCriticalSystemObject" might be set/changed */
1465 if (user_account_control
1466 & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
1467 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1468 "TRUE");
1469 if (ret != LDB_SUCCESS) {
1470 return ret;
1472 el = ldb_msg_find_element(ac->msg,
1473 "isCriticalSystemObject");
1474 el->flags = LDB_FLAG_MOD_REPLACE;
1475 } else if (user_account_control & UF_WORKSTATION_TRUST_ACCOUNT) {
1476 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1477 "FALSE");
1478 if (ret != LDB_SUCCESS) {
1479 return ret;
1481 el = ldb_msg_find_element(ac->msg,
1482 "isCriticalSystemObject");
1483 el->flags = LDB_FLAG_MOD_REPLACE;
1486 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
1487 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
1489 /* Older AD deployments don't know about the RODC group */
1490 if (rid == DOMAIN_RID_READONLY_DCS) {
1491 ret = samldb_prim_group_tester(ac, rid);
1492 if (ret != LDB_SUCCESS) {
1493 return ret;
1497 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1498 "primaryGroupID", rid);
1499 if (ret != LDB_SUCCESS) {
1500 return ret;
1502 el = ldb_msg_find_element(ac->msg,
1503 "primaryGroupID");
1504 el->flags = LDB_FLAG_MOD_REPLACE;
1507 return LDB_SUCCESS;
1510 static int samldb_group_type_change(struct samldb_ctx *ac)
1512 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1513 uint32_t group_type, old_group_type, account_type;
1514 struct ldb_message_element *el;
1515 struct ldb_message *tmp_msg;
1516 int ret;
1517 struct ldb_result *res;
1518 const char * const attrs[] = { "groupType", NULL };
1520 el = dsdb_get_single_valued_attr(ac->msg, "groupType",
1521 ac->req->operation);
1522 if (el == NULL) {
1523 /* we are not affected */
1524 return LDB_SUCCESS;
1527 /* Create a temporary message for fetching the "groupType" */
1528 tmp_msg = ldb_msg_new(ac->msg);
1529 if (tmp_msg == NULL) {
1530 return ldb_module_oom(ac->module);
1532 ret = ldb_msg_add(tmp_msg, el, 0);
1533 if (ret != LDB_SUCCESS) {
1534 return ret;
1536 group_type = ldb_msg_find_attr_as_uint(tmp_msg, "groupType", 0);
1537 talloc_free(tmp_msg);
1539 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1540 DSDB_FLAG_NEXT_MODULE |
1541 DSDB_SEARCH_SHOW_DELETED, ac->req);
1542 if (ret != LDB_SUCCESS) {
1543 return ret;
1545 old_group_type = ldb_msg_find_attr_as_uint(res->msgs[0], "groupType", 0);
1546 if (old_group_type == 0) {
1547 return ldb_operr(ldb);
1550 /* Group type switching isn't so easy as it seems: We can only
1551 * change in this directions: global <-> universal <-> local
1552 * On each step also the group type itself
1553 * (security/distribution) is variable. */
1555 if (ldb_request_get_control(ac->req, LDB_CONTROL_PROVISION_OID) == NULL) {
1556 switch (group_type) {
1557 case GTYPE_SECURITY_GLOBAL_GROUP:
1558 case GTYPE_DISTRIBUTION_GLOBAL_GROUP:
1559 /* change to "universal" allowed */
1560 if ((old_group_type == GTYPE_SECURITY_DOMAIN_LOCAL_GROUP) ||
1561 (old_group_type == GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP)) {
1562 ldb_set_errstring(ldb,
1563 "samldb: Change from security/distribution local group forbidden!");
1564 return LDB_ERR_UNWILLING_TO_PERFORM;
1566 break;
1568 case GTYPE_SECURITY_UNIVERSAL_GROUP:
1569 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP:
1570 /* each change allowed */
1571 break;
1572 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP:
1573 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP:
1574 /* change to "universal" allowed */
1575 if ((old_group_type == GTYPE_SECURITY_GLOBAL_GROUP) ||
1576 (old_group_type == GTYPE_DISTRIBUTION_GLOBAL_GROUP)) {
1577 ldb_set_errstring(ldb,
1578 "samldb: Change from security/distribution global group forbidden!");
1579 return LDB_ERR_UNWILLING_TO_PERFORM;
1581 break;
1583 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP:
1584 default:
1585 /* we don't allow this "groupType" values */
1586 return LDB_ERR_UNWILLING_TO_PERFORM;
1587 break;
1591 account_type = ds_gtype2atype(group_type);
1592 if (account_type == 0) {
1593 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1594 return LDB_ERR_UNWILLING_TO_PERFORM;
1596 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
1597 account_type);
1598 if (ret != LDB_SUCCESS) {
1599 return ret;
1601 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
1602 el->flags = LDB_FLAG_MOD_REPLACE;
1604 return LDB_SUCCESS;
1607 static int samldb_sam_accountname_check(struct samldb_ctx *ac)
1609 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1610 const char * const no_attrs[] = { NULL };
1611 struct ldb_result *res;
1612 const char *sam_accountname, *enc_str;
1613 struct ldb_message_element *el;
1614 struct ldb_message *tmp_msg;
1615 int ret;
1617 el = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName",
1618 ac->req->operation);
1619 if (el == NULL) {
1620 /* we are not affected */
1621 return LDB_SUCCESS;
1624 /* Create a temporary message for fetching the "sAMAccountName" */
1625 tmp_msg = ldb_msg_new(ac->msg);
1626 if (tmp_msg == NULL) {
1627 return ldb_module_oom(ac->module);
1629 ret = ldb_msg_add(tmp_msg, el, 0);
1630 if (ret != LDB_SUCCESS) {
1631 return ret;
1633 sam_accountname = talloc_steal(ac,
1634 ldb_msg_find_attr_as_string(tmp_msg, "sAMAccountName", NULL));
1635 talloc_free(tmp_msg);
1637 if (sam_accountname == NULL) {
1638 /* The "sAMAccountName" cannot be nothing */
1639 ldb_set_errstring(ldb,
1640 "samldb: Empty account names aren't allowed!");
1641 return LDB_ERR_UNWILLING_TO_PERFORM;
1644 enc_str = ldb_binary_encode_string(ac, sam_accountname);
1645 if (enc_str == NULL) {
1646 return ldb_module_oom(ac->module);
1649 /* Make sure that a "sAMAccountName" is only used once */
1651 ret = dsdb_module_search(ac->module, ac, &res,
1652 ldb_get_default_basedn(ldb),
1653 LDB_SCOPE_SUBTREE, no_attrs,
1654 DSDB_FLAG_NEXT_MODULE, ac->req,
1655 "(sAMAccountName=%s)", enc_str);
1656 if (ret != LDB_SUCCESS) {
1657 return ret;
1659 if (res->count > 1) {
1660 return ldb_operr(ldb);
1661 } else if (res->count == 1) {
1662 if (ldb_dn_compare(res->msgs[0]->dn, ac->msg->dn) != 0) {
1663 ldb_asprintf_errstring(ldb,
1664 "samldb: Account name (sAMAccountName) '%s' already in use!",
1665 sam_accountname);
1666 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1669 talloc_free(res);
1671 return LDB_SUCCESS;
1674 static int samldb_member_check(struct samldb_ctx *ac)
1676 const char * const attrs[] = { "objectSid", "member", NULL };
1677 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1678 struct ldb_message_element *el;
1679 struct ldb_dn *member_dn;
1680 struct dom_sid *sid;
1681 struct ldb_result *res;
1682 struct dom_sid *group_sid;
1683 unsigned int i, j;
1684 int ret;
1686 /* Fetch information from the existing object */
1688 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1689 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED, ac->req, NULL);
1690 if (ret != LDB_SUCCESS) {
1691 return ret;
1693 if (res->count != 1) {
1694 return ldb_operr(ldb);
1697 group_sid = samdb_result_dom_sid(res, res->msgs[0], "objectSid");
1698 if (group_sid == NULL) {
1699 return ldb_operr(ldb);
1702 /* We've to walk over all modification entries and consider the "member"
1703 * ones. */
1704 for (i = 0; i < ac->msg->num_elements; i++) {
1705 if (ldb_attr_cmp(ac->msg->elements[i].name, "member") != 0) {
1706 continue;
1709 el = &ac->msg->elements[i];
1710 for (j = 0; j < el->num_values; j++) {
1711 struct ldb_result *group_res;
1712 const char *group_attrs[] = { "primaryGroupID" , NULL };
1713 uint32_t prim_group_rid;
1715 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
1716 /* Deletes will be handled in
1717 * repl_meta_data, and deletes not
1718 * matching a member will return
1719 * LDB_ERR_UNWILLING_TO_PERFORM
1720 * there */
1721 continue;
1724 member_dn = ldb_dn_from_ldb_val(ac, ldb,
1725 &el->values[j]);
1726 if (!ldb_dn_validate(member_dn)) {
1727 return ldb_operr(ldb);
1730 /* Denies to add "member"s to groups which are primary
1731 * ones for them - in this case return
1732 * ERR_ENTRY_ALREADY_EXISTS. */
1734 ret = dsdb_module_search_dn(ac->module, ac, &group_res,
1735 member_dn, group_attrs,
1736 DSDB_FLAG_NEXT_MODULE, ac->req);
1737 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1738 /* member DN doesn't exist yet */
1739 continue;
1741 if (ret != LDB_SUCCESS) {
1742 return ret;
1744 prim_group_rid = ldb_msg_find_attr_as_uint(group_res->msgs[0], "primaryGroupID", (uint32_t)-1);
1745 if (prim_group_rid == (uint32_t) -1) {
1746 /* the member hasn't to be a user account ->
1747 * therefore no check needed in this case. */
1748 continue;
1751 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1752 prim_group_rid);
1753 if (sid == NULL) {
1754 return ldb_operr(ldb);
1757 if (dom_sid_equal(group_sid, sid)) {
1758 ldb_asprintf_errstring(ldb,
1759 "samldb: member %s already set via primaryGroupID %u",
1760 ldb_dn_get_linearized(member_dn), prim_group_rid);
1761 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1766 talloc_free(res);
1768 return LDB_SUCCESS;
1771 /* SAM objects have special rules regarding the "description" attribute on
1772 * modify operations. */
1773 static int samldb_description_check(struct samldb_ctx *ac, bool *modified)
1775 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1776 const char * const attrs[] = { "objectClass", "description", NULL };
1777 struct ldb_result *res;
1778 unsigned int i;
1779 int ret;
1781 /* Fetch information from the existing object */
1782 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1783 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED, ac->req,
1784 "(|(objectclass=user)(objectclass=group)(objectclass=samDomain)(objectclass=samServer))");
1785 if (ret != LDB_SUCCESS) {
1786 /* don't treat it specially ... let normal error codes
1787 happen from other places */
1788 ldb_reset_err_string(ldb);
1789 return LDB_SUCCESS;
1791 if (res->count == 0) {
1792 /* we didn't match the filter */
1793 talloc_free(res);
1794 return LDB_SUCCESS;
1797 /* We've to walk over all modification entries and consider the
1798 * "description" ones. */
1799 for (i = 0; i < ac->msg->num_elements; i++) {
1800 if (ldb_attr_cmp(ac->msg->elements[i].name, "description") == 0) {
1801 ac->msg->elements[i].flags |= LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK;
1802 *modified = true;
1806 talloc_free(res);
1808 return LDB_SUCCESS;
1811 /* This trigger adapts the "servicePrincipalName" attributes if the
1812 * "dNSHostName" and/or "sAMAccountName" attribute change(s) */
1813 static int samldb_service_principal_names_change(struct samldb_ctx *ac)
1815 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1816 struct ldb_message_element *el = NULL, *el2 = NULL;
1817 struct ldb_message *msg;
1818 const char * const attrs[] = { "servicePrincipalName", NULL };
1819 struct ldb_result *res;
1820 const char *dns_hostname = NULL, *old_dns_hostname = NULL,
1821 *sam_accountname = NULL, *old_sam_accountname = NULL;
1822 unsigned int i, j;
1823 int ret;
1825 el = dsdb_get_single_valued_attr(ac->msg, "dNSHostName",
1826 ac->req->operation);
1827 el2 = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName",
1828 ac->req->operation);
1829 if ((el == NULL) && (el2 == NULL)) {
1830 /* we are not affected */
1831 return LDB_SUCCESS;
1834 /* Create a temporary message for fetching the "dNSHostName" */
1835 if (el != NULL) {
1836 const char *dns_attrs[] = { "dNSHostName", NULL };
1837 msg = ldb_msg_new(ac->msg);
1838 if (msg == NULL) {
1839 return ldb_module_oom(ac->module);
1841 ret = ldb_msg_add(msg, el, 0);
1842 if (ret != LDB_SUCCESS) {
1843 return ret;
1845 dns_hostname = talloc_steal(ac,
1846 ldb_msg_find_attr_as_string(msg, "dNSHostName", NULL));
1847 talloc_free(msg);
1849 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn,
1850 dns_attrs, DSDB_FLAG_NEXT_MODULE, ac->req);
1851 if (ret == LDB_SUCCESS) {
1852 old_dns_hostname = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
1856 /* Create a temporary message for fetching the "sAMAccountName" */
1857 if (el2 != NULL) {
1858 char *tempstr, *tempstr2;
1859 const char *acct_attrs[] = { "sAMAccountName", NULL };
1861 msg = ldb_msg_new(ac->msg);
1862 if (msg == NULL) {
1863 return ldb_module_oom(ac->module);
1865 ret = ldb_msg_add(msg, el2, 0);
1866 if (ret != LDB_SUCCESS) {
1867 return ret;
1869 tempstr = talloc_strdup(ac,
1870 ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL));
1871 talloc_free(msg);
1873 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, acct_attrs,
1874 DSDB_FLAG_NEXT_MODULE, ac->req);
1875 if (ret == LDB_SUCCESS) {
1876 tempstr2 = talloc_strdup(ac,
1877 ldb_msg_find_attr_as_string(res->msgs[0],
1878 "sAMAccountName", NULL));
1882 /* The "sAMAccountName" needs some additional trimming: we need
1883 * to remove the trailing "$"s if they exist. */
1884 if ((tempstr != NULL) && (tempstr[0] != '\0') &&
1885 (tempstr[strlen(tempstr) - 1] == '$')) {
1886 tempstr[strlen(tempstr) - 1] = '\0';
1888 if ((tempstr2 != NULL) && (tempstr2[0] != '\0') &&
1889 (tempstr2[strlen(tempstr2) - 1] == '$')) {
1890 tempstr2[strlen(tempstr2) - 1] = '\0';
1892 sam_accountname = tempstr;
1893 old_sam_accountname = tempstr2;
1896 if (old_dns_hostname == NULL) {
1897 /* we cannot change when the old name is unknown */
1898 dns_hostname = NULL;
1900 if ((old_dns_hostname != NULL) && (dns_hostname != NULL) &&
1901 (strcasecmp_m(old_dns_hostname, dns_hostname) == 0)) {
1902 /* The "dNSHostName" didn't change */
1903 dns_hostname = NULL;
1906 if (old_sam_accountname == NULL) {
1907 /* we cannot change when the old name is unknown */
1908 sam_accountname = NULL;
1910 if ((old_sam_accountname != NULL) && (sam_accountname != NULL) &&
1911 (strcasecmp_m(old_sam_accountname, sam_accountname) == 0)) {
1912 /* The "sAMAccountName" didn't change */
1913 sam_accountname = NULL;
1916 if ((dns_hostname == NULL) && (sam_accountname == NULL)) {
1917 /* Well, there are information missing (old name(s)) or the
1918 * names didn't change. We've nothing to do and can exit here */
1919 return LDB_SUCCESS;
1922 /* Potential "servicePrincipalName" changes in the same request have to
1923 * be handled before the update (Windows behaviour). */
1924 el = ldb_msg_find_element(ac->msg, "servicePrincipalName");
1925 if (el != NULL) {
1926 msg = ldb_msg_new(ac->msg);
1927 if (msg == NULL) {
1928 return ldb_module_oom(ac->module);
1930 msg->dn = ac->msg->dn;
1932 do {
1933 ret = ldb_msg_add(msg, el, el->flags);
1934 if (ret != LDB_SUCCESS) {
1935 return ret;
1938 ldb_msg_remove_element(ac->msg, el);
1940 el = ldb_msg_find_element(ac->msg,
1941 "servicePrincipalName");
1942 } while (el != NULL);
1944 ret = dsdb_module_modify(ac->module, msg,
1945 DSDB_FLAG_NEXT_MODULE, ac->req);
1946 if (ret != LDB_SUCCESS) {
1947 return ret;
1949 talloc_free(msg);
1952 /* Fetch the "servicePrincipalName"s if any */
1953 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1954 DSDB_FLAG_NEXT_MODULE, ac->req, NULL);
1955 if (ret != LDB_SUCCESS) {
1956 return ret;
1958 if ((res->count != 1) || (res->msgs[0]->num_elements > 1)) {
1959 return ldb_operr(ldb);
1962 if (res->msgs[0]->num_elements == 1) {
1964 * Yes, we do have "servicePrincipalName"s. First we update them
1965 * locally, that means we do always substitute the current
1966 * "dNSHostName" with the new one and/or "sAMAccountName"
1967 * without "$" with the new one and then we append the
1968 * modified "servicePrincipalName"s as a message element
1969 * replace to the modification request (Windows behaviour). We
1970 * need also to make sure that the values remain case-
1971 * insensitively unique.
1974 ret = ldb_msg_add_empty(ac->msg, "servicePrincipalName",
1975 LDB_FLAG_MOD_REPLACE, &el);
1976 if (ret != LDB_SUCCESS) {
1977 return ret;
1980 for (i = 0; i < res->msgs[0]->elements[0].num_values; i++) {
1981 char *old_str, *new_str, *pos;
1982 const char *tok;
1983 struct ldb_val *vals;
1984 bool found = false;
1986 old_str = (char *)
1987 res->msgs[0]->elements[0].values[i].data;
1989 new_str = talloc_strdup(ac->msg,
1990 strtok_r(old_str, "/", &pos));
1991 if (new_str == NULL) {
1992 return ldb_module_oom(ac->module);
1995 while ((tok = strtok_r(NULL, "/", &pos)) != NULL) {
1996 if ((dns_hostname != NULL) &&
1997 (strcasecmp_m(tok, old_dns_hostname) == 0)) {
1998 tok = dns_hostname;
2000 if ((sam_accountname != NULL) &&
2001 (strcasecmp_m(tok, old_sam_accountname) == 0)) {
2002 tok = sam_accountname;
2005 new_str = talloc_asprintf(ac->msg, "%s/%s",
2006 new_str, tok);
2007 if (new_str == NULL) {
2008 return ldb_module_oom(ac->module);
2012 /* Uniqueness check */
2013 for (j = 0; (!found) && (j < el->num_values); j++) {
2014 if (strcasecmp_m((char *)el->values[j].data,
2015 new_str) == 0) {
2016 found = true;
2019 if (found) {
2020 continue;
2024 * append the new "servicePrincipalName" -
2025 * code derived from ldb_msg_add_value().
2027 * Open coded to make it clear that we must
2028 * append to the MOD_REPLACE el created above.
2030 vals = talloc_realloc(ac->msg, el->values,
2031 struct ldb_val,
2032 el->num_values + 1);
2033 if (vals == NULL) {
2034 return ldb_module_oom(ac->module);
2036 el->values = vals;
2037 el->values[el->num_values] = data_blob_string_const(new_str);
2038 ++(el->num_values);
2042 talloc_free(res);
2044 return LDB_SUCCESS;
2047 /* This checks the "fSMORoleOwner" attributes */
2048 static int samldb_fsmo_role_owner_check(struct samldb_ctx *ac)
2050 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
2051 const char * const no_attrs[] = { NULL };
2052 struct ldb_message_element *el;
2053 struct ldb_message *tmp_msg;
2054 struct ldb_dn *res_dn;
2055 struct ldb_result *res;
2056 int ret;
2058 el = dsdb_get_single_valued_attr(ac->msg, "fSMORoleOwner",
2059 ac->req->operation);
2060 if (el == NULL) {
2061 /* we are not affected */
2062 return LDB_SUCCESS;
2065 /* Create a temporary message for fetching the "fSMORoleOwner" */
2066 tmp_msg = ldb_msg_new(ac->msg);
2067 if (tmp_msg == NULL) {
2068 return ldb_module_oom(ac->module);
2070 ret = ldb_msg_add(tmp_msg, el, 0);
2071 if (ret != LDB_SUCCESS) {
2072 return ret;
2074 res_dn = ldb_msg_find_attr_as_dn(ldb, ac, tmp_msg, "fSMORoleOwner");
2075 talloc_free(tmp_msg);
2077 if (res_dn == NULL) {
2078 ldb_set_errstring(ldb,
2079 "samldb: 'fSMORoleOwner' attributes have to reference 'nTDSDSA' entries!");
2080 if (ac->req->operation == LDB_ADD) {
2081 return LDB_ERR_CONSTRAINT_VIOLATION;
2082 } else {
2083 return LDB_ERR_UNWILLING_TO_PERFORM;
2087 /* Fetched DN has to reference a "nTDSDSA" entry */
2088 ret = dsdb_module_search(ac->module, ac, &res, res_dn, LDB_SCOPE_BASE,
2089 no_attrs,
2090 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED,
2091 ac->req, "(objectClass=nTDSDSA)");
2092 if (ret != LDB_SUCCESS) {
2093 return ret;
2095 if (res->count != 1) {
2096 ldb_set_errstring(ldb,
2097 "samldb: 'fSMORoleOwner' attributes have to reference 'nTDSDSA' entries!");
2098 return LDB_ERR_UNWILLING_TO_PERFORM;
2101 talloc_free(res);
2103 return LDB_SUCCESS;
2107 /* add */
2108 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
2110 struct ldb_context *ldb;
2111 struct samldb_ctx *ac;
2112 struct ldb_message_element *el;
2113 int ret;
2115 ldb = ldb_module_get_ctx(module);
2116 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
2118 /* do not manipulate our control entries */
2119 if (ldb_dn_is_special(req->op.add.message->dn)) {
2120 return ldb_next_request(module, req);
2123 ac = samldb_ctx_init(module, req);
2124 if (ac == NULL) {
2125 return ldb_operr(ldb);
2128 /* build the new msg */
2129 ac->msg = ldb_msg_copy_shallow(ac, req->op.add.message);
2130 if (ac->msg == NULL) {
2131 talloc_free(ac);
2132 ldb_debug(ldb, LDB_DEBUG_FATAL,
2133 "samldb_add: ldb_msg_copy_shallow failed!\n");
2134 return ldb_operr(ldb);
2137 el = ldb_msg_find_element(ac->msg, "fSMORoleOwner");
2138 if (el != NULL) {
2139 ret = samldb_fsmo_role_owner_check(ac);
2140 if (ret != LDB_SUCCESS) {
2141 return ret;
2145 if (samdb_find_attribute(ldb, ac->msg,
2146 "objectclass", "user") != NULL) {
2147 ac->type = SAMLDB_TYPE_USER;
2149 ret = samldb_prim_group_trigger(ac);
2150 if (ret != LDB_SUCCESS) {
2151 return ret;
2154 ret = samldb_objectclass_trigger(ac);
2155 if (ret != LDB_SUCCESS) {
2156 return ret;
2159 return samldb_fill_object(ac);
2162 if (samdb_find_attribute(ldb, ac->msg,
2163 "objectclass", "group") != NULL) {
2164 ac->type = SAMLDB_TYPE_GROUP;
2166 ret = samldb_objectclass_trigger(ac);
2167 if (ret != LDB_SUCCESS) {
2168 return ret;
2171 return samldb_fill_object(ac);
2174 /* perhaps a foreignSecurityPrincipal? */
2175 if (samdb_find_attribute(ldb, ac->msg,
2176 "objectclass",
2177 "foreignSecurityPrincipal") != NULL) {
2178 return samldb_fill_foreignSecurityPrincipal_object(ac);
2181 if (samdb_find_attribute(ldb, ac->msg,
2182 "objectclass", "classSchema") != NULL) {
2183 ret = samldb_schema_info_update(ac);
2184 if (ret != LDB_SUCCESS) {
2185 talloc_free(ac);
2186 return ret;
2189 ac->type = SAMLDB_TYPE_CLASS;
2190 return samldb_fill_object(ac);
2193 if (samdb_find_attribute(ldb, ac->msg,
2194 "objectclass", "attributeSchema") != NULL) {
2195 ret = samldb_schema_info_update(ac);
2196 if (ret != LDB_SUCCESS) {
2197 talloc_free(ac);
2198 return ret;
2201 ac->type = SAMLDB_TYPE_ATTRIBUTE;
2202 return samldb_fill_object(ac);
2205 talloc_free(ac);
2207 /* nothing matched, go on */
2208 return ldb_next_request(module, req);
2211 /* modify */
2212 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
2214 struct ldb_context *ldb;
2215 struct samldb_ctx *ac;
2216 struct ldb_message_element *el, *el2;
2217 bool modified = false;
2218 int ret;
2220 if (ldb_dn_is_special(req->op.mod.message->dn)) {
2221 /* do not manipulate our control entries */
2222 return ldb_next_request(module, req);
2225 ldb = ldb_module_get_ctx(module);
2227 /* make sure that "objectSid" is not specified */
2228 el = ldb_msg_find_element(req->op.mod.message, "objectSid");
2229 if (el != NULL) {
2230 if (ldb_request_get_control(req, LDB_CONTROL_PROVISION_OID) == NULL) {
2231 ldb_set_errstring(ldb,
2232 "samldb: objectSid must not be specified!");
2233 return LDB_ERR_UNWILLING_TO_PERFORM;
2236 /* make sure that "sAMAccountType" is not specified */
2237 el = ldb_msg_find_element(req->op.mod.message, "sAMAccountType");
2238 if (el != NULL) {
2239 ldb_set_errstring(ldb,
2240 "samldb: sAMAccountType must not be specified!");
2241 return LDB_ERR_UNWILLING_TO_PERFORM;
2243 /* make sure that "isCriticalSystemObject" is not specified */
2244 el = ldb_msg_find_element(req->op.mod.message, "isCriticalSystemObject");
2245 if (el != NULL) {
2246 if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID) == NULL) {
2247 ldb_set_errstring(ldb,
2248 "samldb: isCriticalSystemObject must not be specified!");
2249 return LDB_ERR_UNWILLING_TO_PERFORM;
2253 /* msDS-IntId is not allowed to be modified
2254 * except when modification comes from replication */
2255 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
2256 if (!ldb_request_get_control(req,
2257 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
2258 return LDB_ERR_CONSTRAINT_VIOLATION;
2262 ac = samldb_ctx_init(module, req);
2263 if (ac == NULL) {
2264 return ldb_operr(ldb);
2267 /* build the new msg */
2268 ac->msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
2269 if (ac->msg == NULL) {
2270 talloc_free(ac);
2271 ldb_debug(ldb, LDB_DEBUG_FATAL,
2272 "samldb_modify: ldb_msg_copy_shallow failed!\n");
2273 return ldb_operr(ldb);
2276 el = ldb_msg_find_element(ac->msg, "primaryGroupID");
2277 if (el != NULL) {
2278 ret = samldb_prim_group_trigger(ac);
2279 if (ret != LDB_SUCCESS) {
2280 return ret;
2284 el = ldb_msg_find_element(ac->msg, "userAccountControl");
2285 if (el != NULL) {
2286 modified = true;
2287 ret = samldb_user_account_control_change(ac);
2288 if (ret != LDB_SUCCESS) {
2289 return ret;
2293 el = ldb_msg_find_element(ac->msg, "groupType");
2294 if (el != NULL) {
2295 modified = true;
2296 ret = samldb_group_type_change(ac);
2297 if (ret != LDB_SUCCESS) {
2298 return ret;
2302 el = ldb_msg_find_element(ac->msg, "sAMAccountName");
2303 if (el != NULL) {
2304 ret = samldb_sam_accountname_check(ac);
2305 if (ret != LDB_SUCCESS) {
2306 return ret;
2310 el = ldb_msg_find_element(ac->msg, "member");
2311 if (el != NULL) {
2312 ret = samldb_member_check(ac);
2313 if (ret != LDB_SUCCESS) {
2314 return ret;
2318 el = ldb_msg_find_element(ac->msg, "description");
2319 if (el != NULL) {
2320 ret = samldb_description_check(ac, &modified);
2321 if (ret != LDB_SUCCESS) {
2322 return ret;
2326 el = ldb_msg_find_element(ac->msg, "dNSHostName");
2327 el2 = ldb_msg_find_element(ac->msg, "sAMAccountName");
2328 if ((el != NULL) || (el2 != NULL)) {
2329 modified = true;
2330 ret = samldb_service_principal_names_change(ac);
2331 if (ret != LDB_SUCCESS) {
2332 return ret;
2336 el = ldb_msg_find_element(ac->msg, "fSMORoleOwner");
2337 if (el != NULL) {
2338 ret = samldb_fsmo_role_owner_check(ac);
2339 if (ret != LDB_SUCCESS) {
2340 return ret;
2344 if (modified) {
2345 struct ldb_request *child_req;
2347 /* Now perform the real modifications as a child request */
2348 ret = ldb_build_mod_req(&child_req, ldb, ac,
2349 ac->msg,
2350 req->controls,
2351 req, dsdb_next_callback,
2352 req);
2353 LDB_REQ_SET_LOCATION(child_req);
2354 if (ret != LDB_SUCCESS) {
2355 return ret;
2358 return ldb_next_request(module, child_req);
2361 talloc_free(ac);
2363 /* no change which interests us, go on */
2364 return ldb_next_request(module, req);
2367 /* delete */
2369 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
2371 struct ldb_context *ldb;
2372 struct dom_sid *sid;
2373 uint32_t rid;
2374 NTSTATUS status;
2375 int ret;
2376 struct ldb_result *res;
2377 const char * const attrs[] = { "objectSid", "isDeleted", NULL };
2378 const char * const noattrs[] = { NULL };
2380 ldb = ldb_module_get_ctx(ac->module);
2382 /* Finds out the SID/RID of the SAM object */
2383 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->req->op.del.dn,
2384 attrs,
2385 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED,
2386 ac->req);
2387 if (ret != LDB_SUCCESS) {
2388 return ret;
2391 if (ldb_msg_check_string_attribute(res->msgs[0], "isDeleted", "TRUE")) {
2392 return LDB_SUCCESS;
2395 sid = samdb_result_dom_sid(ac, res->msgs[0], "objectSid");
2396 if (sid == NULL) {
2397 /* No SID - it might not be a SAM object - therefore ok */
2398 return LDB_SUCCESS;
2400 status = dom_sid_split_rid(ac, sid, NULL, &rid);
2401 if (!NT_STATUS_IS_OK(status)) {
2402 return ldb_operr(ldb);
2404 if (rid == 0) {
2405 /* Special object (security principal?) */
2406 return LDB_SUCCESS;
2409 /* Deny delete requests from groups which are primary ones */
2410 ret = dsdb_module_search(ac->module, ac, &res,
2411 ldb_get_default_basedn(ldb),
2412 LDB_SCOPE_SUBTREE, noattrs,
2413 DSDB_FLAG_NEXT_MODULE,
2414 ac->req,
2415 "(&(primaryGroupID=%u)(objectClass=user))", rid);
2416 if (ret != LDB_SUCCESS) {
2417 return ret;
2419 if (res->count > 0) {
2420 return LDB_ERR_ENTRY_ALREADY_EXISTS;
2423 return LDB_SUCCESS;
2426 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
2428 struct samldb_ctx *ac;
2429 int ret;
2431 if (ldb_dn_is_special(req->op.del.dn)) {
2432 /* do not manipulate our control entries */
2433 return ldb_next_request(module, req);
2436 ac = samldb_ctx_init(module, req);
2437 if (ac == NULL) {
2438 return ldb_operr(ldb_module_get_ctx(module));
2441 ret = samldb_prim_group_users_check(ac);
2442 if (ret != LDB_SUCCESS) {
2443 return ret;
2446 talloc_free(ac);
2448 return ldb_next_request(module, req);
2451 /* extended */
2453 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
2455 struct ldb_context *ldb = ldb_module_get_ctx(module);
2456 struct dsdb_fsmo_extended_op *exop;
2457 int ret;
2459 exop = talloc_get_type(req->op.extended.data,
2460 struct dsdb_fsmo_extended_op);
2461 if (!exop) {
2462 ldb_set_errstring(ldb,
2463 "samldb_extended_allocate_rid_pool: invalid extended data");
2464 return LDB_ERR_PROTOCOL_ERROR;
2467 ret = ridalloc_allocate_rid_pool_fsmo(module, exop, req);
2468 if (ret != LDB_SUCCESS) {
2469 return ret;
2472 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
2475 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
2477 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
2478 return samldb_extended_allocate_rid_pool(module, req);
2481 return ldb_next_request(module, req);
2485 static const struct ldb_module_ops ldb_samldb_module_ops = {
2486 .name = "samldb",
2487 .add = samldb_add,
2488 .modify = samldb_modify,
2489 .del = samldb_delete,
2490 .extended = samldb_extended
2494 int ldb_samldb_module_init(const char *version)
2496 LDB_MODULE_CHECK_VERSION(version);
2497 return ldb_register_module(&ldb_samldb_module_ops);