s4-dsdb: check group membership only for non deleted objects
[Samba/gbeck.git] / source4 / dsdb / samdb / ldb_modules / samldb.c
blob6ca3fe84c697e241390da73bcebed8d504279824
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
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * Name: ldb
25 * Component: ldb samldb module
27 * Description: various internal DSDB triggers - most for SAM specific objects
29 * Author: Simo Sorce
32 #include "includes.h"
33 #include "libcli/ldap/ldap_ndr.h"
34 #include "ldb_module.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "dsdb/samdb/ldb_modules/util.h"
37 #include "dsdb/samdb/ldb_modules/ridalloc.h"
38 #include "libcli/security/security.h"
39 #include "librpc/gen_ndr/ndr_security.h"
40 #include "ldb_wrap.h"
41 #include "param/param.h"
42 #include "libds/common/flag_mapping.h"
44 struct samldb_ctx;
46 typedef int (*samldb_step_fn_t)(struct samldb_ctx *);
48 struct samldb_step {
49 struct samldb_step *next;
50 samldb_step_fn_t fn;
53 struct samldb_ctx {
54 struct ldb_module *module;
55 struct ldb_request *req;
57 /* used for add operations */
58 const char *type;
60 /* the resulting message */
61 struct ldb_message *msg;
63 /* used in "samldb_find_for_defaultObjectCategory" */
64 struct ldb_dn *dn, *res_dn;
66 /* all the async steps necessary to complete the operation */
67 struct samldb_step *steps;
68 struct samldb_step *curstep;
70 /* If someone set an ares to forward controls and response back to the caller */
71 struct ldb_reply *ares;
74 static struct samldb_ctx *samldb_ctx_init(struct ldb_module *module,
75 struct ldb_request *req)
77 struct ldb_context *ldb;
78 struct samldb_ctx *ac;
80 ldb = ldb_module_get_ctx(module);
82 ac = talloc_zero(req, struct samldb_ctx);
83 if (ac == NULL) {
84 ldb_oom(ldb);
85 return NULL;
88 ac->module = module;
89 ac->req = req;
91 return ac;
94 static int samldb_add_step(struct samldb_ctx *ac, samldb_step_fn_t fn)
96 struct samldb_step *step, *stepper;
98 step = talloc_zero(ac, struct samldb_step);
99 if (step == NULL) {
100 return ldb_oom(ldb_module_get_ctx(ac->module));
103 step->fn = fn;
105 if (ac->steps == NULL) {
106 ac->steps = step;
107 ac->curstep = step;
108 } else {
109 if (ac->curstep == NULL)
110 return ldb_operr(ldb_module_get_ctx(ac->module));
111 for (stepper = ac->curstep; stepper->next != NULL;
112 stepper = stepper->next);
113 stepper->next = step;
116 return LDB_SUCCESS;
119 static int samldb_first_step(struct samldb_ctx *ac)
121 if (ac->steps == NULL) {
122 return ldb_operr(ldb_module_get_ctx(ac->module));
125 ac->curstep = ac->steps;
126 return ac->curstep->fn(ac);
129 static int samldb_next_step(struct samldb_ctx *ac)
131 if (ac->curstep->next) {
132 ac->curstep = ac->curstep->next;
133 return ac->curstep->fn(ac);
136 /* We exit the samldb module here. If someone set an "ares" to forward
137 * controls and response back to the caller, use them. */
138 if (ac->ares) {
139 return ldb_module_done(ac->req, ac->ares->controls,
140 ac->ares->response, LDB_SUCCESS);
141 } else {
142 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
147 /* sAMAccountName handling */
149 static int samldb_generate_sAMAccountName(struct ldb_context *ldb,
150 struct ldb_message *msg)
152 char *name;
154 /* Format: $000000-000000000000 */
156 name = talloc_asprintf(msg, "$%.6X-%.6X%.6X",
157 (unsigned int)generate_random(),
158 (unsigned int)generate_random(),
159 (unsigned int)generate_random());
160 if (name == NULL) {
161 return ldb_oom(ldb);
163 return ldb_msg_add_steal_string(msg, "sAMAccountName", name);
166 static int samldb_check_sAMAccountName(struct samldb_ctx *ac)
168 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
169 const char *name;
170 int ret;
171 struct ldb_result *res;
172 const char *noattrs[] = { NULL };
174 if (ldb_msg_find_element(ac->msg, "sAMAccountName") == NULL) {
175 ret = samldb_generate_sAMAccountName(ldb, ac->msg);
176 if (ret != LDB_SUCCESS) {
177 return ret;
181 name = ldb_msg_find_attr_as_string(ac->msg, "sAMAccountName", NULL);
182 if (name == NULL) {
183 /* The "sAMAccountName" cannot be nothing */
184 ldb_set_errstring(ldb,
185 "samldb: Empty account names aren't allowed!");
186 return LDB_ERR_CONSTRAINT_VIOLATION;
189 ret = dsdb_module_search(ac->module, ac, &res,
190 NULL, LDB_SCOPE_SUBTREE, noattrs,
191 DSDB_FLAG_NEXT_MODULE,
192 ac->req,
193 "(sAMAccountName=%s)",
194 ldb_binary_encode_string(ac, name));
195 if (ret != LDB_SUCCESS) {
196 return ret;
198 if (res->count != 0) {
199 ldb_asprintf_errstring(ldb,
200 "samldb: Account name (sAMAccountName) '%s' already in use!",
201 name);
202 talloc_free(res);
203 return LDB_ERR_ENTRY_ALREADY_EXISTS;
205 talloc_free(res);
207 return samldb_next_step(ac);
211 static bool samldb_msg_add_sid(struct ldb_message *msg,
212 const char *name,
213 const struct dom_sid *sid)
215 struct ldb_val v;
216 enum ndr_err_code ndr_err;
218 ndr_err = ndr_push_struct_blob(&v, msg, sid,
219 (ndr_push_flags_fn_t)ndr_push_dom_sid);
220 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
221 return false;
223 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
227 /* allocate a SID using our RID Set */
228 static int samldb_allocate_sid(struct samldb_ctx *ac)
230 uint32_t rid;
231 struct dom_sid *sid;
232 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
233 int ret;
235 ret = ridalloc_allocate_rid(ac->module, &rid, ac->req);
236 if (ret != LDB_SUCCESS) {
237 return ret;
240 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
241 if (sid == NULL) {
242 return ldb_module_oom(ac->module);
245 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
246 return ldb_operr(ldb);
249 return samldb_next_step(ac);
253 see if a krbtgt_number is available
255 static bool samldb_krbtgtnumber_available(struct samldb_ctx *ac,
256 uint32_t krbtgt_number)
258 TALLOC_CTX *tmp_ctx = talloc_new(ac);
259 struct ldb_result *res;
260 const char *no_attrs[] = { NULL };
261 int ret;
263 ret = dsdb_module_search(ac->module, tmp_ctx, &res, NULL,
264 LDB_SCOPE_SUBTREE, no_attrs,
265 DSDB_FLAG_NEXT_MODULE,
266 ac->req,
267 "(msDC-SecondaryKrbTgtNumber=%u)",
268 krbtgt_number);
269 if (ret == LDB_SUCCESS && res->count == 0) {
270 talloc_free(tmp_ctx);
271 return true;
273 talloc_free(tmp_ctx);
274 return false;
277 /* special handling for add in RODC join */
278 static int samldb_rodc_add(struct samldb_ctx *ac)
280 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
281 uint32_t krbtgt_number, i_start, i;
282 int ret;
283 char *newpass;
284 struct ldb_val newpass_utf16;
286 /* find a unused msDC-SecondaryKrbTgtNumber */
287 i_start = generate_random() & 0xFFFF;
288 if (i_start == 0) {
289 i_start = 1;
292 for (i=i_start; i<=0xFFFF; i++) {
293 if (samldb_krbtgtnumber_available(ac, i)) {
294 krbtgt_number = i;
295 goto found;
298 for (i=1; i<i_start; i++) {
299 if (samldb_krbtgtnumber_available(ac, i)) {
300 krbtgt_number = i;
301 goto found;
305 ldb_asprintf_errstring(ldb,
306 "%08X: Unable to find available msDS-SecondaryKrbTgtNumber",
307 W_ERROR_V(WERR_NO_SYSTEM_RESOURCES));
308 return LDB_ERR_OTHER;
310 found:
311 ret = ldb_msg_add_empty(ac->msg, "msDS-SecondaryKrbTgtNumber",
312 LDB_FLAG_INTERNAL_DISABLE_VALIDATION, NULL);
313 if (ret != LDB_SUCCESS) {
314 return ldb_operr(ldb);
317 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
318 "msDS-SecondaryKrbTgtNumber", krbtgt_number);
319 if (ret != LDB_SUCCESS) {
320 return ldb_operr(ldb);
323 ret = ldb_msg_add_fmt(ac->msg, "sAMAccountName", "krbtgt_%u",
324 krbtgt_number);
325 if (ret != LDB_SUCCESS) {
326 return ldb_operr(ldb);
329 newpass = generate_random_password(ac->msg, 128, 255);
330 if (newpass == NULL) {
331 return ldb_operr(ldb);
334 if (!convert_string_talloc(ac,
335 CH_UNIX, CH_UTF16,
336 newpass, strlen(newpass),
337 (void *)&newpass_utf16.data,
338 &newpass_utf16.length)) {
339 ldb_asprintf_errstring(ldb,
340 "samldb_rodc_add: "
341 "failed to generate UTF16 password from random password");
342 return LDB_ERR_OPERATIONS_ERROR;
344 ret = ldb_msg_add_steal_value(ac->msg, "clearTextPassword", &newpass_utf16);
345 if (ret != LDB_SUCCESS) {
346 return ldb_operr(ldb);
349 return samldb_next_step(ac);
352 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
354 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
355 struct ldb_result *res;
356 const char *no_attrs[] = { NULL };
357 int ret;
359 ac->res_dn = NULL;
361 ret = dsdb_module_search(ac->module, ac, &res,
362 ac->dn, LDB_SCOPE_BASE, no_attrs,
363 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
364 | DSDB_FLAG_NEXT_MODULE,
365 ac->req,
366 "(objectClass=classSchema)");
367 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
368 /* Don't be pricky when the DN doesn't exist if we have the */
369 /* RELAX control specified */
370 if (ldb_request_get_control(ac->req,
371 LDB_CONTROL_RELAX_OID) == NULL) {
372 ldb_set_errstring(ldb,
373 "samldb_find_defaultObjectCategory: "
374 "Invalid DN for 'defaultObjectCategory'!");
375 return LDB_ERR_CONSTRAINT_VIOLATION;
378 if ((ret != LDB_ERR_NO_SUCH_OBJECT) && (ret != LDB_SUCCESS)) {
379 return ret;
382 if (ret == LDB_SUCCESS) {
383 /* ensure the defaultObjectCategory has a full GUID */
384 struct ldb_message *m;
385 m = ldb_msg_new(ac->msg);
386 if (m == NULL) {
387 return ldb_oom(ldb);
389 m->dn = ac->msg->dn;
390 if (ldb_msg_add_string(m, "defaultObjectCategory",
391 ldb_dn_get_extended_linearized(m, res->msgs[0]->dn, 1)) !=
392 LDB_SUCCESS) {
393 return ldb_oom(ldb);
395 m->elements[0].flags = LDB_FLAG_MOD_REPLACE;
397 ret = dsdb_module_modify(ac->module, m,
398 DSDB_FLAG_NEXT_MODULE,
399 ac->req);
400 if (ret != LDB_SUCCESS) {
401 return ret;
406 ac->res_dn = ac->dn;
408 return samldb_next_step(ac);
412 * msDS-IntId attributeSchema attribute handling
413 * during LDB_ADD request processing
415 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
417 int ret;
418 bool id_exists;
419 uint32_t msds_intid;
420 int32_t system_flags;
421 struct ldb_context *ldb;
422 struct ldb_result *ldb_res;
423 struct ldb_dn *schema_dn;
425 ldb = ldb_module_get_ctx(ac->module);
426 schema_dn = ldb_get_schema_basedn(ldb);
428 /* replicated update should always go through */
429 if (ldb_request_get_control(ac->req,
430 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
431 return LDB_SUCCESS;
434 /* msDS-IntId is handled by system and should never be
435 * passed by clients */
436 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
437 return LDB_ERR_UNWILLING_TO_PERFORM;
440 /* do not generate msDS-IntId if Relax control is passed */
441 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
442 return LDB_SUCCESS;
445 /* check Functional Level */
446 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
447 return LDB_SUCCESS;
450 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
451 system_flags = ldb_msg_find_attr_as_int(ac->msg, "systemFlags", 0);
452 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
453 return LDB_SUCCESS;
456 /* Generate new value for msDs-IntId
457 * Value should be in 0x80000000..0xBFFFFFFF range */
458 msds_intid = generate_random() % 0X3FFFFFFF;
459 msds_intid += 0x80000000;
461 /* probe id values until unique one is found */
462 do {
463 msds_intid++;
464 if (msds_intid > 0xBFFFFFFF) {
465 msds_intid = 0x80000001;
468 ret = dsdb_module_search(ac->module, ac,
469 &ldb_res,
470 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
471 DSDB_FLAG_NEXT_MODULE,
472 ac->req,
473 "(msDS-IntId=%d)", msds_intid);
474 if (ret != LDB_SUCCESS) {
475 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
476 __location__": Searching for msDS-IntId=%d failed - %s\n",
477 msds_intid,
478 ldb_errstring(ldb));
479 return ldb_operr(ldb);
481 id_exists = (ldb_res->count > 0);
483 talloc_free(ldb_res);
484 } while(id_exists);
486 return samdb_msg_add_int(ldb, ac->msg, ac->msg, "msDS-IntId",
487 msds_intid);
492 * samldb_add_entry (async)
495 static int samldb_add_entry_callback(struct ldb_request *req,
496 struct ldb_reply *ares)
498 struct ldb_context *ldb;
499 struct samldb_ctx *ac;
500 int ret;
502 ac = talloc_get_type(req->context, struct samldb_ctx);
503 ldb = ldb_module_get_ctx(ac->module);
505 if (!ares) {
506 return ldb_module_done(ac->req, NULL, NULL,
507 LDB_ERR_OPERATIONS_ERROR);
510 if (ares->type == LDB_REPLY_REFERRAL) {
511 return ldb_module_send_referral(ac->req, ares->referral);
514 if (ares->error != LDB_SUCCESS) {
515 return ldb_module_done(ac->req, ares->controls,
516 ares->response, ares->error);
518 if (ares->type != LDB_REPLY_DONE) {
519 ldb_set_errstring(ldb,
520 "Invalid reply type!\n");
521 return ldb_module_done(ac->req, NULL, NULL,
522 LDB_ERR_OPERATIONS_ERROR);
525 /* The caller may wish to get controls back from the add */
526 ac->ares = talloc_steal(ac, ares);
528 ret = samldb_next_step(ac);
529 if (ret != LDB_SUCCESS) {
530 return ldb_module_done(ac->req, NULL, NULL, ret);
532 return ret;
535 static int samldb_add_entry(struct samldb_ctx *ac)
537 struct ldb_context *ldb;
538 struct ldb_request *req;
539 int ret;
541 ldb = ldb_module_get_ctx(ac->module);
543 ret = ldb_build_add_req(&req, ldb, ac,
544 ac->msg,
545 ac->req->controls,
546 ac, samldb_add_entry_callback,
547 ac->req);
548 LDB_REQ_SET_LOCATION(req);
549 if (ret != LDB_SUCCESS) {
550 return ret;
553 return ldb_next_request(ac->module, req);
557 * return true if msg carries an attributeSchema that is intended to be RODC
558 * filtered but is also a system-critical attribute.
560 static bool check_rodc_critical_attribute(struct ldb_message *msg)
562 uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
564 schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
565 searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
566 rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE
567 | SEARCH_FLAG_CONFIDENTIAL);
569 if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
570 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
571 return true;
572 } else {
573 return false;
578 static int samldb_fill_object(struct samldb_ctx *ac)
580 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
581 int ret;
583 /* Add information for the different account types */
584 if (strcmp(ac->type, "user") == 0) {
585 struct ldb_control *rodc_control = ldb_request_get_control(ac->req,
586 LDB_CONTROL_RODC_DCPROMO_OID);
587 if (rodc_control != NULL) {
588 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
589 rodc_control->critical = false;
590 ret = samldb_add_step(ac, samldb_rodc_add);
591 if (ret != LDB_SUCCESS) return ret;
594 /* check if we have a valid sAMAccountName */
595 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
596 if (ret != LDB_SUCCESS) return ret;
598 ret = samldb_add_step(ac, samldb_add_entry);
599 if (ret != LDB_SUCCESS) return ret;
601 } else if (strcmp(ac->type, "group") == 0) {
602 /* check if we have a valid sAMAccountName */
603 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
604 if (ret != LDB_SUCCESS) return ret;
606 ret = samldb_add_step(ac, samldb_add_entry);
607 if (ret != LDB_SUCCESS) return ret;
609 } else if (strcmp(ac->type, "classSchema") == 0) {
610 const struct ldb_val *rdn_value, *def_obj_cat_val;
612 ret = samdb_find_or_add_attribute(ldb, ac->msg,
613 "rdnAttId", "cn");
614 if (ret != LDB_SUCCESS) return ret;
616 /* do not allow to mark an attributeSchema as RODC filtered if it
617 * is system-critical */
618 if (check_rodc_critical_attribute(ac->msg)) {
619 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
620 ldb_dn_get_linearized(ac->msg->dn));
621 return LDB_ERR_UNWILLING_TO_PERFORM;
624 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
625 if (rdn_value == NULL) {
626 return ldb_operr(ldb);
628 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
629 /* the RDN has prefix "CN" */
630 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
631 samdb_cn_to_lDAPDisplayName(ac->msg,
632 (const char *) rdn_value->data));
633 if (ret != LDB_SUCCESS) {
634 ldb_oom(ldb);
635 return ret;
639 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
640 struct GUID guid;
641 /* a new GUID */
642 guid = GUID_random();
643 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
644 if (ret != LDB_SUCCESS) {
645 ldb_oom(ldb);
646 return ret;
650 def_obj_cat_val = ldb_msg_find_ldb_val(ac->msg,
651 "defaultObjectCategory");
652 if (def_obj_cat_val != NULL) {
653 /* "defaultObjectCategory" has been set by the caller.
654 * Do some checks for consistency.
655 * NOTE: The real constraint check (that
656 * 'defaultObjectCategory' is the DN of the new
657 * objectclass or any parent of it) is still incomplete.
658 * For now we say that 'defaultObjectCategory' is valid
659 * if it exists and it is of objectclass "classSchema".
661 ac->dn = ldb_dn_from_ldb_val(ac, ldb, def_obj_cat_val);
662 if (ac->dn == NULL) {
663 ldb_set_errstring(ldb,
664 "Invalid DN for 'defaultObjectCategory'!");
665 return LDB_ERR_CONSTRAINT_VIOLATION;
667 } else {
668 /* "defaultObjectCategory" has not been set by the
669 * caller. Use the entry DN for it. */
670 ac->dn = ac->msg->dn;
672 ret = ldb_msg_add_string(ac->msg, "defaultObjectCategory",
673 ldb_dn_alloc_linearized(ac->msg, ac->dn));
674 if (ret != LDB_SUCCESS) {
675 ldb_oom(ldb);
676 return ret;
680 ret = samldb_add_step(ac, samldb_add_entry);
681 if (ret != LDB_SUCCESS) return ret;
683 /* Now perform the checks for the 'defaultObjectCategory'. The
684 * lookup DN was already saved in "ac->dn" */
685 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
686 if (ret != LDB_SUCCESS) return ret;
688 } else if (strcmp(ac->type, "attributeSchema") == 0) {
689 const struct ldb_val *rdn_value;
690 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
691 if (rdn_value == NULL) {
692 return ldb_operr(ldb);
694 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
695 /* the RDN has prefix "CN" */
696 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
697 samdb_cn_to_lDAPDisplayName(ac->msg,
698 (const char *) rdn_value->data));
699 if (ret != LDB_SUCCESS) {
700 ldb_oom(ldb);
701 return ret;
705 /* do not allow to mark an attributeSchema as RODC filtered if it
706 * is system-critical */
707 if (check_rodc_critical_attribute(ac->msg)) {
708 ldb_asprintf_errstring(ldb,
709 "samldb: refusing schema add of %s - cannot combine critical attribute with RODC filtering",
710 ldb_dn_get_linearized(ac->msg->dn));
711 return LDB_ERR_UNWILLING_TO_PERFORM;
714 ret = samdb_find_or_add_attribute(ldb, ac->msg,
715 "isSingleValued", "FALSE");
716 if (ret != LDB_SUCCESS) return ret;
718 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
719 struct GUID guid;
720 /* a new GUID */
721 guid = GUID_random();
722 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
723 if (ret != LDB_SUCCESS) {
724 ldb_oom(ldb);
725 return ret;
729 /* handle msDS-IntID attribute */
730 ret = samldb_add_handle_msDS_IntId(ac);
731 if (ret != LDB_SUCCESS) return ret;
733 ret = samldb_add_step(ac, samldb_add_entry);
734 if (ret != LDB_SUCCESS) return ret;
736 } else {
737 ldb_asprintf_errstring(ldb,
738 "Invalid entry type!");
739 return LDB_ERR_OPERATIONS_ERROR;
742 return samldb_first_step(ac);
745 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
747 struct ldb_context *ldb;
748 const struct ldb_val *rdn_value;
749 struct dom_sid *sid;
750 int ret;
752 ldb = ldb_module_get_ctx(ac->module);
754 sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
755 if (sid == NULL) {
756 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
757 if (rdn_value == NULL) {
758 return ldb_operr(ldb);
760 sid = dom_sid_parse_talloc(ac->msg,
761 (const char *)rdn_value->data);
762 if (sid == NULL) {
763 ldb_set_errstring(ldb,
764 "samldb: No valid SID found in ForeignSecurityPrincipal CN!");
765 return LDB_ERR_CONSTRAINT_VIOLATION;
767 if (! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
768 return ldb_operr(ldb);
772 /* finally proceed with adding the entry */
773 ret = samldb_add_step(ac, samldb_add_entry);
774 if (ret != LDB_SUCCESS) return ret;
776 return samldb_first_step(ac);
779 static int samldb_schema_info_update(struct samldb_ctx *ac)
781 int ret;
782 struct ldb_context *ldb;
783 struct dsdb_schema *schema;
785 /* replicated update should always go through */
786 if (ldb_request_get_control(ac->req,
787 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
788 return LDB_SUCCESS;
791 /* do not update schemaInfo during provisioning */
792 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
793 return LDB_SUCCESS;
796 ldb = ldb_module_get_ctx(ac->module);
797 schema = dsdb_get_schema(ldb, NULL);
798 if (!schema) {
799 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
800 "samldb_schema_info_update: no dsdb_schema loaded");
801 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
802 return ldb_operr(ldb);
805 ret = dsdb_module_schema_info_update(ac->module, schema,
806 DSDB_FLAG_NEXT_MODULE, ac->req);
807 if (ret != LDB_SUCCESS) {
808 ldb_asprintf_errstring(ldb,
809 "samldb_schema_info_update: dsdb_module_schema_info_update failed with %s",
810 ldb_errstring(ldb));
811 return ret;
814 return LDB_SUCCESS;
817 static int samldb_prim_group_tester(struct samldb_ctx *ac, uint32_t rid);
820 * "Objectclass" trigger (MS-SAMR 3.1.1.8.1)
822 * Has to be invoked on "add" and "modify" operations on "user", "computer" and
823 * "group" objects.
824 * ac->msg contains the "add"/"modify" message
825 * ac->type contains the object type (main objectclass)
827 static int samldb_objectclass_trigger(struct samldb_ctx *ac)
829 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
830 void *skip_allocate_sids = ldb_get_opaque(ldb,
831 "skip_allocate_sids");
832 struct ldb_message_element *el, *el2;
833 struct dom_sid *sid;
834 int ret;
836 /* make sure that "sAMAccountType" is not specified */
837 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
838 if (el != NULL) {
839 ldb_set_errstring(ldb,
840 "samldb: sAMAccountType must not be specified!");
841 return LDB_ERR_UNWILLING_TO_PERFORM;
844 /* Step 1: objectSid assignment */
846 /* Don't allow the objectSid to be changed. But beside the RELAX
847 * control we have also to guarantee that it can always be set with
848 * SYSTEM permissions. This is needed for the "samba3sam" backend. */
849 sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
850 if ((sid != NULL) && (!dsdb_module_am_system(ac->module)) &&
851 (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
852 ldb_set_errstring(ldb,
853 "samldb: objectSid must not be specified!");
854 return LDB_ERR_UNWILLING_TO_PERFORM;
857 /* but generate a new SID when we do have an add operations */
858 if ((sid == NULL) && (ac->req->operation == LDB_ADD) && !skip_allocate_sids) {
859 ret = samldb_add_step(ac, samldb_allocate_sid);
860 if (ret != LDB_SUCCESS) return ret;
863 if (strcmp(ac->type, "user") == 0) {
864 bool uac_generated = false;
866 /* Step 1.2: Default values */
867 ret = samdb_find_or_add_attribute(ldb, ac->msg,
868 "accountExpires", "9223372036854775807");
869 if (ret != LDB_SUCCESS) return ret;
870 ret = samdb_find_or_add_attribute(ldb, ac->msg,
871 "badPasswordTime", "0");
872 if (ret != LDB_SUCCESS) return ret;
873 ret = samdb_find_or_add_attribute(ldb, ac->msg,
874 "badPwdCount", "0");
875 if (ret != LDB_SUCCESS) return ret;
876 ret = samdb_find_or_add_attribute(ldb, ac->msg,
877 "codePage", "0");
878 if (ret != LDB_SUCCESS) return ret;
879 ret = samdb_find_or_add_attribute(ldb, ac->msg,
880 "countryCode", "0");
881 if (ret != LDB_SUCCESS) return ret;
882 ret = samdb_find_or_add_attribute(ldb, ac->msg,
883 "lastLogoff", "0");
884 if (ret != LDB_SUCCESS) return ret;
885 ret = samdb_find_or_add_attribute(ldb, ac->msg,
886 "lastLogon", "0");
887 if (ret != LDB_SUCCESS) return ret;
888 ret = samdb_find_or_add_attribute(ldb, ac->msg,
889 "logonCount", "0");
890 if (ret != LDB_SUCCESS) return ret;
891 ret = samdb_find_or_add_attribute(ldb, ac->msg,
892 "pwdLastSet", "0");
893 if (ret != LDB_SUCCESS) return ret;
895 /* On add operations we might need to generate a
896 * "userAccountControl" (if it isn't specified). */
897 el = ldb_msg_find_element(ac->msg, "userAccountControl");
898 if ((el == NULL) && (ac->req->operation == LDB_ADD)) {
899 ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
900 "userAccountControl",
901 UF_NORMAL_ACCOUNT);
902 if (ret != LDB_SUCCESS) {
903 return ret;
905 uac_generated = true;
908 el = ldb_msg_find_element(ac->msg, "userAccountControl");
909 if (el != NULL) {
910 uint32_t user_account_control, account_type;
912 /* Step 1.3: "userAccountControl" -> "sAMAccountType" mapping */
913 user_account_control = ldb_msg_find_attr_as_uint(ac->msg,
914 "userAccountControl",
917 /* Temporary duplicate accounts aren't allowed */
918 if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
919 return LDB_ERR_OTHER;
922 /* Workstation and (read-only) DC objects do need objectclass "computer" */
923 if ((samdb_find_attribute(ldb, ac->msg,
924 "objectclass", "computer") == NULL) &&
925 (user_account_control &
926 (UF_SERVER_TRUST_ACCOUNT | UF_WORKSTATION_TRUST_ACCOUNT))) {
927 ldb_set_errstring(ldb,
928 "samldb: Requested account type does need objectclass 'computer'!");
929 return LDB_ERR_OBJECT_CLASS_VIOLATION;
932 account_type = ds_uf2atype(user_account_control);
933 if (account_type == 0) {
934 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
935 return LDB_ERR_UNWILLING_TO_PERFORM;
937 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
938 "sAMAccountType",
939 account_type);
940 if (ret != LDB_SUCCESS) {
941 return ret;
943 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
944 el2->flags = LDB_FLAG_MOD_REPLACE;
946 /* "isCriticalSystemObject" might be set */
947 if (user_account_control &
948 (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
949 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
950 "TRUE");
951 if (ret != LDB_SUCCESS) {
952 return ret;
954 el2 = ldb_msg_find_element(ac->msg,
955 "isCriticalSystemObject");
956 el2->flags = LDB_FLAG_MOD_REPLACE;
957 } else if (user_account_control & UF_WORKSTATION_TRUST_ACCOUNT) {
958 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
959 "FALSE");
960 if (ret != LDB_SUCCESS) {
961 return ret;
963 el2 = ldb_msg_find_element(ac->msg,
964 "isCriticalSystemObject");
965 el2->flags = LDB_FLAG_MOD_REPLACE;
968 /* Step 1.4: "userAccountControl" -> "primaryGroupID" mapping */
969 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
970 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
973 * Older AD deployments don't know about the
974 * RODC group
976 if (rid == DOMAIN_RID_READONLY_DCS) {
977 ret = samldb_prim_group_tester(ac, rid);
978 if (ret != LDB_SUCCESS) {
979 return ret;
983 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
984 "primaryGroupID", rid);
985 if (ret != LDB_SUCCESS) {
986 return ret;
988 el2 = ldb_msg_find_element(ac->msg,
989 "primaryGroupID");
990 el2->flags = LDB_FLAG_MOD_REPLACE;
993 /* Step 1.5: Add additional flags when needed */
994 /* Obviously this is done when the "userAccountControl"
995 * has been generated here (tested against Windows
996 * Server) */
997 if (uac_generated) {
998 user_account_control |= UF_ACCOUNTDISABLE;
999 user_account_control |= UF_PASSWD_NOTREQD;
1001 ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
1002 "userAccountControl",
1003 user_account_control);
1004 if (ret != LDB_SUCCESS) {
1005 return ret;
1010 } else if (strcmp(ac->type, "group") == 0) {
1011 const char *tempstr;
1013 /* Step 2.2: Default values */
1014 tempstr = talloc_asprintf(ac->msg, "%d",
1015 GTYPE_SECURITY_GLOBAL_GROUP);
1016 if (tempstr == NULL) return ldb_operr(ldb);
1017 ret = samdb_find_or_add_attribute(ldb, ac->msg,
1018 "groupType", tempstr);
1019 if (ret != LDB_SUCCESS) return ret;
1021 /* Step 2.3: "groupType" -> "sAMAccountType" */
1022 el = ldb_msg_find_element(ac->msg, "groupType");
1023 if (el != NULL) {
1024 uint32_t group_type, account_type;
1026 group_type = ldb_msg_find_attr_as_uint(ac->msg,
1027 "groupType", 0);
1029 /* The creation of builtin groups requires the
1030 * RELAX control */
1031 if (group_type == GTYPE_SECURITY_BUILTIN_LOCAL_GROUP) {
1032 if (ldb_request_get_control(ac->req,
1033 LDB_CONTROL_RELAX_OID) == NULL) {
1034 return LDB_ERR_UNWILLING_TO_PERFORM;
1038 account_type = ds_gtype2atype(group_type);
1039 if (account_type == 0) {
1040 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1041 return LDB_ERR_UNWILLING_TO_PERFORM;
1043 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1044 "sAMAccountType",
1045 account_type);
1046 if (ret != LDB_SUCCESS) {
1047 return ret;
1049 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
1050 el2->flags = LDB_FLAG_MOD_REPLACE;
1054 return LDB_SUCCESS;
1058 * "Primary group ID" trigger (MS-SAMR 3.1.1.8.2)
1060 * Has to be invoked on "add" and "modify" operations on "user" and "computer"
1061 * objects.
1062 * ac->msg contains the "add"/"modify" message
1065 static int samldb_prim_group_tester(struct samldb_ctx *ac, uint32_t rid)
1067 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1068 struct dom_sid *sid;
1069 struct ldb_result *res;
1070 int ret;
1071 const char *noattrs[] = { NULL };
1073 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1074 if (sid == NULL) {
1075 return ldb_operr(ldb);
1078 ret = dsdb_module_search(ac->module, ac, &res, NULL, LDB_SCOPE_SUBTREE,
1079 noattrs, DSDB_FLAG_NEXT_MODULE,
1080 ac->req,
1081 "(objectSid=%s)",
1082 ldap_encode_ndr_dom_sid(ac, sid));
1083 if (ret != LDB_SUCCESS) {
1084 return ret;
1086 if (res->count != 1) {
1087 talloc_free(res);
1088 ldb_asprintf_errstring(ldb,
1089 "Failed to find primary group with RID %u!",
1090 rid);
1091 return LDB_ERR_UNWILLING_TO_PERFORM;
1093 talloc_free(res);
1095 return LDB_SUCCESS;
1098 static int samldb_prim_group_set(struct samldb_ctx *ac)
1100 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1101 uint32_t rid;
1103 rid = ldb_msg_find_attr_as_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
1104 if (rid == (uint32_t) -1) {
1105 /* we aren't affected of any primary group set */
1106 return LDB_SUCCESS;
1108 } else if (!ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
1109 ldb_set_errstring(ldb,
1110 "The primary group isn't settable on add operations!");
1111 return LDB_ERR_UNWILLING_TO_PERFORM;
1114 return samldb_prim_group_tester(ac, rid);
1117 static int samldb_prim_group_change(struct samldb_ctx *ac)
1119 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1120 const char * attrs[] = { "primaryGroupID", "memberOf", NULL };
1121 struct ldb_result *res, *group_res;
1122 struct ldb_message_element *el;
1123 struct ldb_message *msg;
1124 uint32_t prev_rid, new_rid;
1125 struct dom_sid *prev_sid, *new_sid;
1126 struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
1127 int ret;
1128 const char *noattrs[] = { NULL };
1130 el = dsdb_get_single_valued_attr(ac->msg, "primaryGroupID",
1131 ac->req->operation);
1132 if (el == NULL) {
1133 /* we are not affected */
1134 return LDB_SUCCESS;
1137 /* Fetch information from the existing object */
1139 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1140 DSDB_FLAG_NEXT_MODULE, ac->req);
1141 if (ret != LDB_SUCCESS) {
1142 return ret;
1145 /* Finds out the DN of the old primary group */
1147 prev_rid = ldb_msg_find_attr_as_uint(res->msgs[0], "primaryGroupID",
1148 (uint32_t) -1);
1149 if (prev_rid == (uint32_t) -1) {
1150 /* User objects do always have a mandatory "primaryGroupID"
1151 * attribute. If this doesn't exist then the object is of the
1152 * wrong type. This is the exact Windows error code */
1153 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1156 prev_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), prev_rid);
1157 if (prev_sid == NULL) {
1158 return ldb_operr(ldb);
1161 /* Finds out the DN of the new primary group
1162 * Notice: in order to parse the primary group ID correctly we create
1163 * a temporary message here. */
1165 msg = ldb_msg_new(ac->msg);
1166 if (msg == NULL) {
1167 return ldb_module_oom(ac->module);
1169 ret = ldb_msg_add(msg, el, 0);
1170 if (ret != LDB_SUCCESS) {
1171 return ret;
1173 new_rid = ldb_msg_find_attr_as_uint(msg, "primaryGroupID", (uint32_t) -1);
1174 talloc_free(msg);
1175 if (new_rid == (uint32_t) -1) {
1176 /* we aren't affected of any primary group change */
1177 return LDB_SUCCESS;
1180 if (prev_rid == new_rid) {
1181 return LDB_SUCCESS;
1184 ret = dsdb_module_search(ac->module, ac, &group_res, NULL, LDB_SCOPE_SUBTREE,
1185 noattrs, DSDB_FLAG_NEXT_MODULE,
1186 ac->req,
1187 "(objectSid=%s)",
1188 ldap_encode_ndr_dom_sid(ac, prev_sid));
1189 if (ret != LDB_SUCCESS) {
1190 return ret;
1192 if (group_res->count != 1) {
1193 return ldb_operr(ldb);
1195 prev_prim_group_dn = group_res->msgs[0]->dn;
1197 new_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), new_rid);
1198 if (new_sid == NULL) {
1199 return ldb_operr(ldb);
1202 ret = dsdb_module_search(ac->module, ac, &group_res, NULL, LDB_SCOPE_SUBTREE,
1203 noattrs, DSDB_FLAG_NEXT_MODULE,
1204 ac->req,
1205 "(objectSid=%s)",
1206 ldap_encode_ndr_dom_sid(ac, new_sid));
1207 if (ret != LDB_SUCCESS) {
1208 return ret;
1210 if (group_res->count != 1) {
1211 /* Here we know if the specified new primary group candidate is
1212 * valid or not. */
1213 return LDB_ERR_UNWILLING_TO_PERFORM;
1215 new_prim_group_dn = group_res->msgs[0]->dn;
1217 /* We need to be already a normal member of the new primary
1218 * group in order to be successful. */
1219 el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
1220 ldb_dn_get_linearized(new_prim_group_dn));
1221 if (el == NULL) {
1222 return LDB_ERR_UNWILLING_TO_PERFORM;
1225 /* Remove the "member" attribute on the new primary group */
1226 msg = ldb_msg_new(ac->msg);
1227 if (msg == NULL) {
1228 return ldb_module_oom(ac->module);
1230 msg->dn = new_prim_group_dn;
1232 ret = samdb_msg_add_delval(ldb, msg, msg, "member",
1233 ldb_dn_get_linearized(ac->msg->dn));
1234 if (ret != LDB_SUCCESS) {
1235 return ret;
1238 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE, ac->req);
1239 if (ret != LDB_SUCCESS) {
1240 return ret;
1242 talloc_free(msg);
1244 /* Add a "member" attribute for the previous primary group */
1245 msg = ldb_msg_new(ac->msg);
1246 if (msg == NULL) {
1247 return ldb_module_oom(ac->module);
1249 msg->dn = prev_prim_group_dn;
1251 ret = samdb_msg_add_addval(ldb, msg, msg, "member",
1252 ldb_dn_get_linearized(ac->msg->dn));
1253 if (ret != LDB_SUCCESS) {
1254 return ret;
1257 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE, ac->req);
1258 if (ret != LDB_SUCCESS) {
1259 return ret;
1261 talloc_free(msg);
1263 return LDB_SUCCESS;
1266 static int samldb_prim_group_trigger(struct samldb_ctx *ac)
1268 int ret;
1270 if (ac->req->operation == LDB_ADD) {
1271 ret = samldb_prim_group_set(ac);
1272 } else {
1273 ret = samldb_prim_group_change(ac);
1276 return ret;
1281 * This function is called on LDB modify operations. It performs some additions/
1282 * replaces on the current LDB message when "userAccountControl" changes.
1284 static int samldb_user_account_control_change(struct samldb_ctx *ac)
1286 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1287 uint32_t user_account_control, old_user_account_control, account_type;
1288 struct ldb_message_element *el;
1289 struct ldb_message *tmp_msg;
1290 int ret;
1291 struct ldb_result *res;
1292 const char *attrs[] = { "userAccountControl", "objectClass", NULL };
1293 unsigned int i;
1294 bool is_computer = false;
1296 el = dsdb_get_single_valued_attr(ac->msg, "userAccountControl",
1297 ac->req->operation);
1298 if (el == NULL) {
1299 /* we are not affected */
1300 return LDB_SUCCESS;
1303 /* Create a temporary message for fetching the "userAccountControl" */
1304 tmp_msg = ldb_msg_new(ac->msg);
1305 if (tmp_msg == NULL) {
1306 return ldb_module_oom(ac->module);
1308 ret = ldb_msg_add(tmp_msg, el, 0);
1309 if (ret != LDB_SUCCESS) {
1310 return ret;
1312 user_account_control = ldb_msg_find_attr_as_uint(tmp_msg,
1313 "userAccountControl",
1315 talloc_free(tmp_msg);
1317 /* Temporary duplicate accounts aren't allowed */
1318 if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
1319 return LDB_ERR_OTHER;
1322 /* Fetch the old "userAccountControl" and "objectClass" */
1323 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1324 DSDB_FLAG_NEXT_MODULE, ac->req);
1325 if (ret != LDB_SUCCESS) {
1326 return ret;
1328 old_user_account_control = ldb_msg_find_attr_as_uint(res->msgs[0], "userAccountControl", 0);
1329 if (old_user_account_control == 0) {
1330 return ldb_operr(ldb);
1332 el = ldb_msg_find_element(res->msgs[0], "objectClass");
1333 if (el == NULL) {
1334 return ldb_operr(ldb);
1337 /* When we do not have objectclass "computer" we cannot switch to a (read-only) DC */
1338 for (i = 0; i < el->num_values; i++) {
1339 if (ldb_attr_cmp((char *)el->values[i].data, "computer") == 0) {
1340 is_computer = true;
1341 break;
1344 if (!is_computer &&
1345 (user_account_control & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT))) {
1346 ldb_set_errstring(ldb,
1347 "samldb: Requested account type does need objectclass 'computer'!");
1348 return LDB_ERR_UNWILLING_TO_PERFORM;
1352 * The functions "ds_uf2atype" and "ds_uf2prim_group_rid" are used as
1353 * detectors for account type changes.
1354 * So if the account type does change then we need to adjust the
1355 * "sAMAccountType", the "isCriticalSystemObject" and the
1356 * "primaryGroupID" attribute.
1358 if ((ds_uf2atype(user_account_control)
1359 == ds_uf2atype(old_user_account_control)) &&
1360 (ds_uf2prim_group_rid(user_account_control)
1361 == ds_uf2prim_group_rid(old_user_account_control))) {
1362 return LDB_SUCCESS;
1365 account_type = ds_uf2atype(user_account_control);
1366 if (account_type == 0) {
1367 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1368 return LDB_ERR_UNWILLING_TO_PERFORM;
1370 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
1371 account_type);
1372 if (ret != LDB_SUCCESS) {
1373 return ret;
1375 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
1376 el->flags = LDB_FLAG_MOD_REPLACE;
1378 /* "isCriticalSystemObject" might be set/changed */
1379 if (user_account_control
1380 & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
1381 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1382 "TRUE");
1383 if (ret != LDB_SUCCESS) {
1384 return ret;
1386 el = ldb_msg_find_element(ac->msg,
1387 "isCriticalSystemObject");
1388 el->flags = LDB_FLAG_MOD_REPLACE;
1389 } else if (user_account_control & UF_WORKSTATION_TRUST_ACCOUNT) {
1390 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1391 "FALSE");
1392 if (ret != LDB_SUCCESS) {
1393 return ret;
1395 el = ldb_msg_find_element(ac->msg,
1396 "isCriticalSystemObject");
1397 el->flags = LDB_FLAG_MOD_REPLACE;
1400 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
1401 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
1403 /* Older AD deployments don't know about the RODC group */
1404 if (rid == DOMAIN_RID_READONLY_DCS) {
1405 ret = samldb_prim_group_tester(ac, rid);
1406 if (ret != LDB_SUCCESS) {
1407 return ret;
1411 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1412 "primaryGroupID", rid);
1413 if (ret != LDB_SUCCESS) {
1414 return ret;
1416 el = ldb_msg_find_element(ac->msg,
1417 "primaryGroupID");
1418 el->flags = LDB_FLAG_MOD_REPLACE;
1421 return LDB_SUCCESS;
1424 static int samldb_group_type_change(struct samldb_ctx *ac)
1426 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1427 uint32_t group_type, old_group_type, account_type;
1428 struct ldb_message_element *el;
1429 struct ldb_message *tmp_msg;
1430 int ret;
1431 struct ldb_result *res;
1432 const char *attrs[] = { "groupType", NULL };
1434 el = dsdb_get_single_valued_attr(ac->msg, "groupType",
1435 ac->req->operation);
1436 if (el == NULL) {
1437 /* we are not affected */
1438 return LDB_SUCCESS;
1441 /* Create a temporary message for fetching the "groupType" */
1442 tmp_msg = ldb_msg_new(ac->msg);
1443 if (tmp_msg == NULL) {
1444 return ldb_module_oom(ac->module);
1446 ret = ldb_msg_add(tmp_msg, el, 0);
1447 if (ret != LDB_SUCCESS) {
1448 return ret;
1450 group_type = ldb_msg_find_attr_as_uint(tmp_msg, "groupType", 0);
1451 talloc_free(tmp_msg);
1453 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1454 DSDB_FLAG_NEXT_MODULE, ac->req);
1455 if (ret != LDB_SUCCESS) {
1456 return ret;
1458 old_group_type = ldb_msg_find_attr_as_uint(res->msgs[0], "groupType", 0);
1459 if (old_group_type == 0) {
1460 return ldb_operr(ldb);
1463 /* Group type switching isn't so easy as it seems: We can only
1464 * change in this directions: global <-> universal <-> local
1465 * On each step also the group type itself
1466 * (security/distribution) is variable. */
1468 if (ldb_request_get_control(ac->req, LDB_CONTROL_PROVISION_OID) == NULL) {
1469 switch (group_type) {
1470 case GTYPE_SECURITY_GLOBAL_GROUP:
1471 case GTYPE_DISTRIBUTION_GLOBAL_GROUP:
1472 /* change to "universal" allowed */
1473 if ((old_group_type == GTYPE_SECURITY_DOMAIN_LOCAL_GROUP) ||
1474 (old_group_type == GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP)) {
1475 ldb_set_errstring(ldb,
1476 "samldb: Change from security/distribution local group forbidden!");
1477 return LDB_ERR_UNWILLING_TO_PERFORM;
1479 break;
1481 case GTYPE_SECURITY_UNIVERSAL_GROUP:
1482 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP:
1483 /* each change allowed */
1484 break;
1485 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP:
1486 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP:
1487 /* change to "universal" allowed */
1488 if ((old_group_type == GTYPE_SECURITY_GLOBAL_GROUP) ||
1489 (old_group_type == GTYPE_DISTRIBUTION_GLOBAL_GROUP)) {
1490 ldb_set_errstring(ldb,
1491 "samldb: Change from security/distribution global group forbidden!");
1492 return LDB_ERR_UNWILLING_TO_PERFORM;
1494 break;
1496 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP:
1497 default:
1498 /* we don't allow this "groupType" values */
1499 return LDB_ERR_UNWILLING_TO_PERFORM;
1500 break;
1504 account_type = ds_gtype2atype(group_type);
1505 if (account_type == 0) {
1506 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1507 return LDB_ERR_UNWILLING_TO_PERFORM;
1509 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
1510 account_type);
1511 if (ret != LDB_SUCCESS) {
1512 return ret;
1514 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
1515 el->flags = LDB_FLAG_MOD_REPLACE;
1517 return LDB_SUCCESS;
1520 static int samldb_sam_accountname_check(struct samldb_ctx *ac)
1522 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1523 const char *no_attrs[] = { NULL };
1524 struct ldb_result *res;
1525 const char *sam_accountname, *enc_str;
1526 struct ldb_message_element *el;
1527 struct ldb_message *tmp_msg;
1528 int ret;
1530 el = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName",
1531 ac->req->operation);
1532 if (el == NULL) {
1533 /* we are not affected */
1534 return LDB_SUCCESS;
1537 /* Create a temporary message for fetching the "sAMAccountName" */
1538 tmp_msg = ldb_msg_new(ac->msg);
1539 if (tmp_msg == NULL) {
1540 return ldb_module_oom(ac->module);
1542 ret = ldb_msg_add(tmp_msg, el, 0);
1543 if (ret != LDB_SUCCESS) {
1544 return ret;
1546 sam_accountname = talloc_steal(ac,
1547 ldb_msg_find_attr_as_string(tmp_msg, "sAMAccountName", NULL));
1548 talloc_free(tmp_msg);
1550 if (sam_accountname == NULL) {
1551 /* The "sAMAccountName" cannot be nothing */
1552 ldb_set_errstring(ldb,
1553 "samldb: Empty account names aren't allowed!");
1554 return LDB_ERR_UNWILLING_TO_PERFORM;
1557 enc_str = ldb_binary_encode_string(ac, sam_accountname);
1558 if (enc_str == NULL) {
1559 return ldb_module_oom(ac->module);
1562 /* Make sure that a "sAMAccountName" is only used once */
1564 ret = dsdb_module_search(ac->module, ac, &res, NULL, LDB_SCOPE_SUBTREE, no_attrs,
1565 DSDB_FLAG_NEXT_MODULE, ac->req,
1566 "(sAMAccountName=%s)", enc_str);
1567 if (ret != LDB_SUCCESS) {
1568 return ret;
1570 if (res->count > 1) {
1571 return ldb_operr(ldb);
1572 } else if (res->count == 1) {
1573 if (ldb_dn_compare(res->msgs[0]->dn, ac->msg->dn) != 0) {
1574 ldb_asprintf_errstring(ldb,
1575 "samldb: Account name (sAMAccountName) '%s' already in use!",
1576 sam_accountname);
1577 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1580 talloc_free(res);
1582 return LDB_SUCCESS;
1585 static int samldb_member_check(struct samldb_ctx *ac)
1587 static const char * const attrs[] = { "objectSid", "member", NULL };
1588 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1589 struct ldb_message_element *el;
1590 struct ldb_dn *member_dn;
1591 struct dom_sid *sid;
1592 struct ldb_result *res;
1593 struct dom_sid *group_sid;
1594 unsigned int i, j;
1595 int ret;
1597 /* Fetch information from the existing object */
1599 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1600 DSDB_FLAG_NEXT_MODULE, ac->req, NULL);
1601 if (ret != LDB_SUCCESS) {
1602 return ret;
1604 if (res->count != 1) {
1605 return ldb_operr(ldb);
1608 group_sid = samdb_result_dom_sid(res, res->msgs[0], "objectSid");
1609 if (group_sid == NULL) {
1610 return ldb_operr(ldb);
1613 /* We've to walk over all modification entries and consider the "member"
1614 * ones. */
1615 for (i = 0; i < ac->msg->num_elements; i++) {
1616 if (ldb_attr_cmp(ac->msg->elements[i].name, "member") != 0) {
1617 continue;
1620 el = &ac->msg->elements[i];
1621 for (j = 0; j < el->num_values; j++) {
1622 struct ldb_result *group_res;
1623 const char *group_attrs[] = { "primaryGroupID" , NULL };
1624 uint32_t prim_group_rid;
1626 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
1627 /* Deletes will be handled in
1628 * repl_meta_data, and deletes not
1629 * matching a member will return
1630 * LDB_ERR_UNWILLING_TO_PERFORM
1631 * there */
1632 continue;
1635 member_dn = ldb_dn_from_ldb_val(ac, ldb,
1636 &el->values[j]);
1637 if (!ldb_dn_validate(member_dn)) {
1638 return ldb_operr(ldb);
1641 /* Denies to add "member"s to groups which are primary
1642 * ones for them - in this case return
1643 * ERR_ENTRY_ALREADY_EXISTS. */
1645 ret = dsdb_module_search_dn(ac->module, ac, &group_res,
1646 member_dn, group_attrs,
1647 DSDB_FLAG_NEXT_MODULE, ac->req);
1648 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1649 /* member DN doesn't exist yet */
1650 continue;
1652 if (ret != LDB_SUCCESS) {
1653 return ret;
1655 prim_group_rid = ldb_msg_find_attr_as_uint(group_res->msgs[0], "primaryGroupID", (uint32_t)-1);
1656 if (prim_group_rid == (uint32_t) -1) {
1657 /* the member hasn't to be a user account ->
1658 * therefore no check needed in this case. */
1659 continue;
1662 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1663 prim_group_rid);
1664 if (sid == NULL) {
1665 return ldb_operr(ldb);
1668 if (dom_sid_equal(group_sid, sid)) {
1669 ldb_asprintf_errstring(ldb,
1670 "samldb: member %s already set via primaryGroupID %u",
1671 ldb_dn_get_linearized(member_dn), prim_group_rid);
1672 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1677 talloc_free(res);
1679 return LDB_SUCCESS;
1682 /* SAM objects have special rules regarding the "description" attribute on
1683 * modify operations. */
1684 static int samldb_description_check(struct samldb_ctx *ac, bool *modified)
1686 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1687 const char * const attrs[] = { "objectClass", "description", NULL };
1688 struct ldb_result *res;
1689 unsigned int i;
1690 int ret;
1692 /* Fetch information from the existing object */
1693 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1694 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED, ac->req,
1695 "(|(objectclass=user)(objectclass=group)(objectclass=samDomain)(objectclass=samServer))");
1696 if (ret != LDB_SUCCESS) {
1697 /* don't treat it specially ... let normal error codes
1698 happen from other places */
1699 ldb_reset_err_string(ldb);
1700 return LDB_SUCCESS;
1702 if (res->count == 0) {
1703 /* we didn't match the filter */
1704 talloc_free(res);
1705 return LDB_SUCCESS;
1708 /* We've to walk over all modification entries and consider the
1709 * "description" ones. */
1710 for (i = 0; i < ac->msg->num_elements; i++) {
1711 if (ldb_attr_cmp(ac->msg->elements[i].name, "description") == 0) {
1712 ac->msg->elements[i].flags |= LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK;
1713 *modified = true;
1717 talloc_free(res);
1719 return LDB_SUCCESS;
1722 /* This trigger adapts the "servicePrincipalName" attributes if the
1723 * "dNSHostName" and/or "sAMAccountName" attribute change(s) */
1724 static int samldb_service_principal_names_change(struct samldb_ctx *ac)
1726 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1727 struct ldb_message_element *el = NULL, *el2 = NULL;
1728 struct ldb_message *msg;
1729 const char *attrs[] = { "servicePrincipalName", NULL };
1730 struct ldb_result *res;
1731 const char *dns_hostname = NULL, *old_dns_hostname = NULL,
1732 *sam_accountname = NULL, *old_sam_accountname = NULL;
1733 unsigned int i;
1734 int ret;
1736 el = dsdb_get_single_valued_attr(ac->msg, "dNSHostName",
1737 ac->req->operation);
1738 el2 = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName",
1739 ac->req->operation);
1740 if ((el == NULL) && (el2 == NULL)) {
1741 /* we are not affected */
1742 return LDB_SUCCESS;
1745 /* Create a temporary message for fetching the "dNSHostName" */
1746 if (el != NULL) {
1747 const char *dns_attrs[] = { "dNSHostName", NULL };
1748 msg = ldb_msg_new(ac->msg);
1749 if (msg == NULL) {
1750 return ldb_module_oom(ac->module);
1752 ret = ldb_msg_add(msg, el, 0);
1753 if (ret != LDB_SUCCESS) {
1754 return ret;
1756 dns_hostname = talloc_steal(ac,
1757 ldb_msg_find_attr_as_string(msg, "dNSHostName", NULL));
1758 talloc_free(msg);
1760 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn,
1761 dns_attrs, DSDB_FLAG_NEXT_MODULE, ac->req);
1762 if (ret == LDB_SUCCESS) {
1763 old_dns_hostname = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
1767 /* Create a temporary message for fetching the "sAMAccountName" */
1768 if (el2 != NULL) {
1769 char *tempstr, *tempstr2;
1770 const char *acct_attrs[] = { "sAMAccountName", NULL };
1772 msg = ldb_msg_new(ac->msg);
1773 if (msg == NULL) {
1774 return ldb_module_oom(ac->module);
1776 ret = ldb_msg_add(msg, el2, 0);
1777 if (ret != LDB_SUCCESS) {
1778 return ret;
1780 tempstr = talloc_strdup(ac,
1781 ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL));
1782 talloc_free(msg);
1784 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, acct_attrs,
1785 DSDB_FLAG_NEXT_MODULE, ac->req);
1786 if (ret == LDB_SUCCESS) {
1787 tempstr2 = talloc_strdup(ac,
1788 ldb_msg_find_attr_as_string(res->msgs[0],
1789 "sAMAccountName", NULL));
1793 /* The "sAMAccountName" needs some additional trimming: we need
1794 * to remove the trailing "$"s if they exist. */
1795 if ((tempstr != NULL) && (tempstr[0] != '\0') &&
1796 (tempstr[strlen(tempstr) - 1] == '$')) {
1797 tempstr[strlen(tempstr) - 1] = '\0';
1799 if ((tempstr2 != NULL) && (tempstr2[0] != '\0') &&
1800 (tempstr2[strlen(tempstr2) - 1] == '$')) {
1801 tempstr2[strlen(tempstr2) - 1] = '\0';
1803 sam_accountname = tempstr;
1804 old_sam_accountname = tempstr2;
1807 if (old_dns_hostname == NULL) {
1808 /* we cannot change when the old name is unknown */
1809 dns_hostname = NULL;
1811 if ((old_dns_hostname != NULL) && (dns_hostname != NULL) &&
1812 (strcasecmp(old_dns_hostname, dns_hostname) == 0)) {
1813 /* The "dNSHostName" didn't change */
1814 dns_hostname = NULL;
1817 if (old_sam_accountname == NULL) {
1818 /* we cannot change when the old name is unknown */
1819 sam_accountname = NULL;
1821 if ((old_sam_accountname != NULL) && (sam_accountname != NULL) &&
1822 (strcasecmp(old_sam_accountname, sam_accountname) == 0)) {
1823 /* The "sAMAccountName" didn't change */
1824 sam_accountname = NULL;
1827 if ((dns_hostname == NULL) && (sam_accountname == NULL)) {
1828 /* Well, there are information missing (old name(s)) or the
1829 * names didn't change. We've nothing to do and can exit here */
1830 return LDB_SUCCESS;
1833 /* Potential "servicePrincipalName" changes in the same request have to
1834 * be handled before the update (Windows behaviour). */
1835 el = ldb_msg_find_element(ac->msg, "servicePrincipalName");
1836 if (el != NULL) {
1837 msg = ldb_msg_new(ac->msg);
1838 if (msg == NULL) {
1839 return ldb_module_oom(ac->module);
1841 msg->dn = ac->msg->dn;
1843 do {
1844 ret = ldb_msg_add(msg, el, el->flags);
1845 if (ret != LDB_SUCCESS) {
1846 return ret;
1849 ldb_msg_remove_element(ac->msg, el);
1851 el = ldb_msg_find_element(ac->msg,
1852 "servicePrincipalName");
1853 } while (el != NULL);
1855 ret = dsdb_module_modify(ac->module, msg,
1856 DSDB_FLAG_NEXT_MODULE, ac->req);
1857 if (ret != LDB_SUCCESS) {
1858 return ret;
1860 talloc_free(msg);
1863 /* Fetch the "servicePrincipalName"s if any */
1864 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1865 DSDB_FLAG_NEXT_MODULE, ac->req, NULL);
1866 if (ret != LDB_SUCCESS) {
1867 return ret;
1869 if ((res->count != 1) || (res->msgs[0]->num_elements > 1)) {
1870 return ldb_operr(ldb);
1873 if (res->msgs[0]->num_elements == 1) {
1874 /* Yes, we do have "servicePrincipalName"s. First we update them
1875 * locally, that means we do always substitute the current
1876 * "dNSHostName" with the new one and/or "sAMAccountName"
1877 * without "$" with the new one and then we append this to the
1878 * modification request (Windows behaviour). */
1880 for (i = 0; i < res->msgs[0]->elements[0].num_values; i++) {
1881 char *old_str, *new_str, *pos;
1882 const char *tok;
1884 old_str = (char *)
1885 res->msgs[0]->elements[0].values[i].data;
1887 new_str = talloc_strdup(ac->msg,
1888 strtok_r(old_str, "/", &pos));
1889 if (new_str == NULL) {
1890 return ldb_module_oom(ac->module);
1893 while ((tok = strtok_r(NULL, "/", &pos)) != NULL) {
1894 if ((dns_hostname != NULL) &&
1895 (strcasecmp(tok, old_dns_hostname) == 0)) {
1896 tok = dns_hostname;
1898 if ((sam_accountname != NULL) &&
1899 (strcasecmp(tok, old_sam_accountname) == 0)) {
1900 tok = sam_accountname;
1903 new_str = talloc_asprintf(ac->msg, "%s/%s",
1904 new_str, tok);
1905 if (new_str == NULL) {
1906 return ldb_module_oom(ac->module);
1910 ret = ldb_msg_add_string(ac->msg,
1911 "servicePrincipalName",
1912 new_str);
1913 if (ret != LDB_SUCCESS) {
1914 return ret;
1918 el = ldb_msg_find_element(ac->msg, "servicePrincipalName");
1919 el->flags = LDB_FLAG_MOD_REPLACE;
1922 talloc_free(res);
1924 return LDB_SUCCESS;
1928 /* add */
1929 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
1931 struct ldb_context *ldb;
1932 struct samldb_ctx *ac;
1933 int ret;
1935 ldb = ldb_module_get_ctx(module);
1936 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
1938 /* do not manipulate our control entries */
1939 if (ldb_dn_is_special(req->op.add.message->dn)) {
1940 return ldb_next_request(module, req);
1943 ac = samldb_ctx_init(module, req);
1944 if (ac == NULL) {
1945 return ldb_operr(ldb);
1948 /* build the new msg */
1949 ac->msg = ldb_msg_copy_shallow(ac, req->op.add.message);
1950 if (ac->msg == NULL) {
1951 talloc_free(ac);
1952 ldb_debug(ldb, LDB_DEBUG_FATAL,
1953 "samldb_add: ldb_msg_copy_shallow failed!\n");
1954 return ldb_operr(ldb);
1957 if (samdb_find_attribute(ldb, ac->msg,
1958 "objectclass", "user") != NULL) {
1959 ac->type = "user";
1961 ret = samldb_prim_group_trigger(ac);
1962 if (ret != LDB_SUCCESS) {
1963 return ret;
1966 ret = samldb_objectclass_trigger(ac);
1967 if (ret != LDB_SUCCESS) {
1968 return ret;
1971 return samldb_fill_object(ac);
1974 if (samdb_find_attribute(ldb, ac->msg,
1975 "objectclass", "group") != NULL) {
1976 ac->type = "group";
1978 ret = samldb_objectclass_trigger(ac);
1979 if (ret != LDB_SUCCESS) {
1980 return ret;
1983 return samldb_fill_object(ac);
1986 /* perhaps a foreignSecurityPrincipal? */
1987 if (samdb_find_attribute(ldb, ac->msg,
1988 "objectclass",
1989 "foreignSecurityPrincipal") != NULL) {
1990 return samldb_fill_foreignSecurityPrincipal_object(ac);
1993 if (samdb_find_attribute(ldb, ac->msg,
1994 "objectclass", "classSchema") != NULL) {
1995 ret = samldb_schema_info_update(ac);
1996 if (ret != LDB_SUCCESS) {
1997 talloc_free(ac);
1998 return ret;
2001 ac->type = "classSchema";
2002 return samldb_fill_object(ac);
2005 if (samdb_find_attribute(ldb, ac->msg,
2006 "objectclass", "attributeSchema") != NULL) {
2007 ret = samldb_schema_info_update(ac);
2008 if (ret != LDB_SUCCESS) {
2009 talloc_free(ac);
2010 return ret;
2013 ac->type = "attributeSchema";
2014 return samldb_fill_object(ac);
2017 talloc_free(ac);
2019 /* nothing matched, go on */
2020 return ldb_next_request(module, req);
2023 /* modify */
2024 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
2026 struct ldb_context *ldb;
2027 struct samldb_ctx *ac;
2028 struct ldb_message_element *el, *el2;
2029 bool modified = false;
2030 int ret;
2032 if (ldb_dn_is_special(req->op.mod.message->dn)) {
2033 /* do not manipulate our control entries */
2034 return ldb_next_request(module, req);
2037 ldb = ldb_module_get_ctx(module);
2039 /* make sure that "objectSid" is not specified */
2040 el = ldb_msg_find_element(req->op.mod.message, "objectSid");
2041 if (el != NULL) {
2042 if (ldb_request_get_control(req, LDB_CONTROL_PROVISION_OID) == NULL) {
2043 ldb_set_errstring(ldb,
2044 "samldb: objectSid must not be specified!");
2045 return LDB_ERR_UNWILLING_TO_PERFORM;
2048 /* make sure that "sAMAccountType" is not specified */
2049 el = ldb_msg_find_element(req->op.mod.message, "sAMAccountType");
2050 if (el != NULL) {
2051 ldb_set_errstring(ldb,
2052 "samldb: sAMAccountType must not be specified!");
2053 return LDB_ERR_UNWILLING_TO_PERFORM;
2055 /* make sure that "isCriticalSystemObject" is not specified */
2056 el = ldb_msg_find_element(req->op.mod.message, "isCriticalSystemObject");
2057 if (el != NULL) {
2058 if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID) == NULL) {
2059 ldb_set_errstring(ldb,
2060 "samldb: isCriticalSystemObject must not be specified!");
2061 return LDB_ERR_UNWILLING_TO_PERFORM;
2065 /* msDS-IntId is not allowed to be modified
2066 * except when modification comes from replication */
2067 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
2068 if (!ldb_request_get_control(req,
2069 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
2070 return LDB_ERR_CONSTRAINT_VIOLATION;
2074 ac = samldb_ctx_init(module, req);
2075 if (ac == NULL) {
2076 return ldb_operr(ldb);
2079 /* build the new msg */
2080 ac->msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
2081 if (ac->msg == NULL) {
2082 talloc_free(ac);
2083 ldb_debug(ldb, LDB_DEBUG_FATAL,
2084 "samldb_modify: ldb_msg_copy_shallow failed!\n");
2085 return ldb_operr(ldb);
2088 el = ldb_msg_find_element(ac->msg, "primaryGroupID");
2089 if (el != NULL) {
2090 ret = samldb_prim_group_trigger(ac);
2091 if (ret != LDB_SUCCESS) {
2092 return ret;
2096 el = ldb_msg_find_element(ac->msg, "userAccountControl");
2097 if (el != NULL) {
2098 modified = true;
2099 ret = samldb_user_account_control_change(ac);
2100 if (ret != LDB_SUCCESS) {
2101 return ret;
2105 el = ldb_msg_find_element(ac->msg, "groupType");
2106 if (el != NULL) {
2107 modified = true;
2108 ret = samldb_group_type_change(ac);
2109 if (ret != LDB_SUCCESS) {
2110 return ret;
2114 el = ldb_msg_find_element(ac->msg, "sAMAccountName");
2115 if (el != NULL) {
2116 ret = samldb_sam_accountname_check(ac);
2117 if (ret != LDB_SUCCESS) {
2118 return ret;
2122 el = ldb_msg_find_element(ac->msg, "member");
2123 if (el != NULL) {
2124 ret = samldb_member_check(ac);
2125 if (ret != LDB_SUCCESS) {
2126 return ret;
2130 el = ldb_msg_find_element(ac->msg, "description");
2131 if (el != NULL) {
2132 ret = samldb_description_check(ac, &modified);
2133 if (ret != LDB_SUCCESS) {
2134 return ret;
2138 el = ldb_msg_find_element(ac->msg, "dNSHostName");
2139 el2 = ldb_msg_find_element(ac->msg, "sAMAccountName");
2140 if ((el != NULL) || (el2 != NULL)) {
2141 modified = true;
2142 ret = samldb_service_principal_names_change(ac);
2143 if (ret != LDB_SUCCESS) {
2144 return ret;
2148 if (modified) {
2149 struct ldb_request *child_req;
2151 /* Now perform the real modifications as a child request */
2152 ret = ldb_build_mod_req(&child_req, ldb, ac,
2153 ac->msg,
2154 req->controls,
2155 req, dsdb_next_callback,
2156 req);
2157 LDB_REQ_SET_LOCATION(child_req);
2158 if (ret != LDB_SUCCESS) {
2159 return ret;
2162 return ldb_next_request(module, child_req);
2165 talloc_free(ac);
2167 /* no change which interests us, go on */
2168 return ldb_next_request(module, req);
2171 /* delete */
2173 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
2175 struct ldb_context *ldb;
2176 struct dom_sid *sid;
2177 uint32_t rid;
2178 NTSTATUS status;
2179 int ret;
2180 struct ldb_result *res;
2181 const char *attrs[] = { "objectSid", "isDeleted", NULL };
2182 const char *noattrs[] = { NULL };
2184 ldb = ldb_module_get_ctx(ac->module);
2186 /* Finds out the SID/RID of the SAM object */
2187 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->req->op.del.dn,
2188 attrs,
2189 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED,
2190 ac->req);
2191 if (ret != LDB_SUCCESS) {
2192 return ret;
2195 if (ldb_msg_check_string_attribute(res->msgs[0], "isDeleted", "TRUE")) {
2196 return LDB_SUCCESS;
2199 sid = samdb_result_dom_sid(ac, res->msgs[0], "objectSid");
2200 if (sid == NULL) {
2201 /* No SID - it might not be a SAM object - therefore ok */
2202 return LDB_SUCCESS;
2204 status = dom_sid_split_rid(ac, sid, NULL, &rid);
2205 if (!NT_STATUS_IS_OK(status)) {
2206 return ldb_operr(ldb);
2208 if (rid == 0) {
2209 /* Special object (security principal?) */
2210 return LDB_SUCCESS;
2213 /* Deny delete requests from groups which are primary ones */
2214 ret = dsdb_module_search(ac->module, ac, &res, NULL, LDB_SCOPE_SUBTREE, noattrs,
2215 DSDB_FLAG_NEXT_MODULE,
2216 ac->req,
2217 "(&(primaryGroupID=%u)(objectClass=user))", rid);
2218 if (ret != LDB_SUCCESS) {
2219 return ret;
2221 if (res->count > 0) {
2222 return LDB_ERR_ENTRY_ALREADY_EXISTS;
2225 return LDB_SUCCESS;
2228 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
2230 struct samldb_ctx *ac;
2231 int ret;
2233 if (ldb_dn_is_special(req->op.del.dn)) {
2234 /* do not manipulate our control entries */
2235 return ldb_next_request(module, req);
2238 ac = samldb_ctx_init(module, req);
2239 if (ac == NULL) {
2240 return ldb_operr(ldb_module_get_ctx(module));
2243 ret = samldb_prim_group_users_check(ac);
2244 if (ret != LDB_SUCCESS) {
2245 return ret;
2248 talloc_free(ac);
2250 return ldb_next_request(module, req);
2253 /* extended */
2255 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
2257 struct ldb_context *ldb = ldb_module_get_ctx(module);
2258 struct dsdb_fsmo_extended_op *exop;
2259 int ret;
2261 exop = talloc_get_type(req->op.extended.data,
2262 struct dsdb_fsmo_extended_op);
2263 if (!exop) {
2264 ldb_set_errstring(ldb,
2265 "samldb_extended_allocate_rid_pool: invalid extended data");
2266 return LDB_ERR_PROTOCOL_ERROR;
2269 ret = ridalloc_allocate_rid_pool_fsmo(module, exop, req);
2270 if (ret != LDB_SUCCESS) {
2271 return ret;
2274 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
2277 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
2279 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
2280 return samldb_extended_allocate_rid_pool(module, req);
2283 return ldb_next_request(module, req);
2287 static const struct ldb_module_ops ldb_samldb_module_ops = {
2288 .name = "samldb",
2289 .add = samldb_add,
2290 .modify = samldb_modify,
2291 .del = samldb_delete,
2292 .extended = samldb_extended
2296 int ldb_samldb_module_init(const char *version)
2298 LDB_MODULE_CHECK_VERSION(version);
2299 return ldb_register_module(&ldb_samldb_module_ops);