s4:samldb LDB modules - only objectClass "computer" is allowed to embed all types...
[Samba/bjacke.git] / source4 / dsdb / samdb / ldb_modules / samldb.c
blobfa0c4f631739cf8eb532438e59f8c40bf7c06480
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 ac->res_dn = ac->dn;
384 return samldb_next_step(ac);
388 * msDS-IntId attributeSchema attribute handling
389 * during LDB_ADD request processing
391 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
393 int ret;
394 bool id_exists;
395 uint32_t msds_intid;
396 int32_t system_flags;
397 struct ldb_context *ldb;
398 struct ldb_result *ldb_res;
399 struct ldb_dn *schema_dn;
401 ldb = ldb_module_get_ctx(ac->module);
402 schema_dn = ldb_get_schema_basedn(ldb);
404 /* replicated update should always go through */
405 if (ldb_request_get_control(ac->req,
406 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
407 return LDB_SUCCESS;
410 /* msDS-IntId is handled by system and should never be
411 * passed by clients */
412 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
413 return LDB_ERR_UNWILLING_TO_PERFORM;
416 /* do not generate msDS-IntId if Relax control is passed */
417 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
418 return LDB_SUCCESS;
421 /* check Functional Level */
422 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
423 return LDB_SUCCESS;
426 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
427 system_flags = ldb_msg_find_attr_as_int(ac->msg, "systemFlags", 0);
428 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
429 return LDB_SUCCESS;
432 /* Generate new value for msDs-IntId
433 * Value should be in 0x80000000..0xBFFFFFFF range */
434 msds_intid = generate_random() % 0X3FFFFFFF;
435 msds_intid += 0x80000000;
437 /* probe id values until unique one is found */
438 do {
439 msds_intid++;
440 if (msds_intid > 0xBFFFFFFF) {
441 msds_intid = 0x80000001;
444 ret = dsdb_module_search(ac->module, ac,
445 &ldb_res,
446 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
447 DSDB_FLAG_NEXT_MODULE,
448 ac->req,
449 "(msDS-IntId=%d)", msds_intid);
450 if (ret != LDB_SUCCESS) {
451 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
452 __location__": Searching for msDS-IntId=%d failed - %s\n",
453 msds_intid,
454 ldb_errstring(ldb));
455 return ldb_operr(ldb);
457 id_exists = (ldb_res->count > 0);
459 talloc_free(ldb_res);
460 } while(id_exists);
462 return samdb_msg_add_int(ldb, ac->msg, ac->msg, "msDS-IntId",
463 msds_intid);
468 * samldb_add_entry (async)
471 static int samldb_add_entry_callback(struct ldb_request *req,
472 struct ldb_reply *ares)
474 struct ldb_context *ldb;
475 struct samldb_ctx *ac;
476 int ret;
478 ac = talloc_get_type(req->context, struct samldb_ctx);
479 ldb = ldb_module_get_ctx(ac->module);
481 if (!ares) {
482 return ldb_module_done(ac->req, NULL, NULL,
483 LDB_ERR_OPERATIONS_ERROR);
486 if (ares->type == LDB_REPLY_REFERRAL) {
487 return ldb_module_send_referral(ac->req, ares->referral);
490 if (ares->error != LDB_SUCCESS) {
491 return ldb_module_done(ac->req, ares->controls,
492 ares->response, ares->error);
494 if (ares->type != LDB_REPLY_DONE) {
495 ldb_set_errstring(ldb,
496 "Invalid reply type!\n");
497 return ldb_module_done(ac->req, NULL, NULL,
498 LDB_ERR_OPERATIONS_ERROR);
501 /* The caller may wish to get controls back from the add */
502 ac->ares = talloc_steal(ac, ares);
504 ret = samldb_next_step(ac);
505 if (ret != LDB_SUCCESS) {
506 return ldb_module_done(ac->req, NULL, NULL, ret);
508 return ret;
511 static int samldb_add_entry(struct samldb_ctx *ac)
513 struct ldb_context *ldb;
514 struct ldb_request *req;
515 int ret;
517 ldb = ldb_module_get_ctx(ac->module);
519 ret = ldb_build_add_req(&req, ldb, ac,
520 ac->msg,
521 ac->req->controls,
522 ac, samldb_add_entry_callback,
523 ac->req);
524 LDB_REQ_SET_LOCATION(req);
525 if (ret != LDB_SUCCESS) {
526 return ret;
529 return ldb_next_request(ac->module, req);
533 * return true if msg carries an attributeSchema that is intended to be RODC
534 * filtered but is also a system-critical attribute.
536 static bool check_rodc_critical_attribute(struct ldb_message *msg)
538 uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
540 schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
541 searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
542 rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE
543 | SEARCH_FLAG_CONFIDENTIAL);
545 if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
546 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
547 return true;
548 } else {
549 return false;
554 static int samldb_fill_object(struct samldb_ctx *ac)
556 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
557 int ret;
559 /* Add information for the different account types */
560 if (strcmp(ac->type, "user") == 0) {
561 struct ldb_control *rodc_control = ldb_request_get_control(ac->req,
562 LDB_CONTROL_RODC_DCPROMO_OID);
563 if (rodc_control != NULL) {
564 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
565 rodc_control->critical = false;
566 ret = samldb_add_step(ac, samldb_rodc_add);
567 if (ret != LDB_SUCCESS) return ret;
570 /* check if we have a valid sAMAccountName */
571 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
572 if (ret != LDB_SUCCESS) return ret;
574 ret = samldb_add_step(ac, samldb_add_entry);
575 if (ret != LDB_SUCCESS) return ret;
577 } else if (strcmp(ac->type, "group") == 0) {
578 /* check if we have a valid sAMAccountName */
579 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
580 if (ret != LDB_SUCCESS) return ret;
582 ret = samldb_add_step(ac, samldb_add_entry);
583 if (ret != LDB_SUCCESS) return ret;
585 } else if (strcmp(ac->type, "classSchema") == 0) {
586 const struct ldb_val *rdn_value, *def_obj_cat_val;
588 ret = samdb_find_or_add_attribute(ldb, ac->msg,
589 "rdnAttId", "cn");
590 if (ret != LDB_SUCCESS) return ret;
592 /* do not allow to mark an attributeSchema as RODC filtered if it
593 * is system-critical */
594 if (check_rodc_critical_attribute(ac->msg)) {
595 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
596 ldb_dn_get_linearized(ac->msg->dn));
597 return LDB_ERR_UNWILLING_TO_PERFORM;
600 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
601 if (rdn_value == NULL) {
602 return ldb_operr(ldb);
604 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
605 /* the RDN has prefix "CN" */
606 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
607 samdb_cn_to_lDAPDisplayName(ac->msg,
608 (const char *) rdn_value->data));
609 if (ret != LDB_SUCCESS) {
610 ldb_oom(ldb);
611 return ret;
615 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
616 struct GUID guid;
617 /* a new GUID */
618 guid = GUID_random();
619 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
620 if (ret != LDB_SUCCESS) {
621 ldb_oom(ldb);
622 return ret;
626 def_obj_cat_val = ldb_msg_find_ldb_val(ac->msg,
627 "defaultObjectCategory");
628 if (def_obj_cat_val != NULL) {
629 /* "defaultObjectCategory" has been set by the caller.
630 * Do some checks for consistency.
631 * NOTE: The real constraint check (that
632 * 'defaultObjectCategory' is the DN of the new
633 * objectclass or any parent of it) is still incomplete.
634 * For now we say that 'defaultObjectCategory' is valid
635 * if it exists and it is of objectclass "classSchema".
637 ac->dn = ldb_dn_from_ldb_val(ac, ldb, def_obj_cat_val);
638 if (ac->dn == NULL) {
639 ldb_set_errstring(ldb,
640 "Invalid DN for 'defaultObjectCategory'!");
641 return LDB_ERR_CONSTRAINT_VIOLATION;
643 } else {
644 /* "defaultObjectCategory" has not been set by the
645 * caller. Use the entry DN for it. */
646 ac->dn = ac->msg->dn;
648 ret = ldb_msg_add_string(ac->msg, "defaultObjectCategory",
649 ldb_dn_alloc_linearized(ac->msg, ac->dn));
650 if (ret != LDB_SUCCESS) {
651 ldb_oom(ldb);
652 return ret;
656 ret = samldb_add_step(ac, samldb_add_entry);
657 if (ret != LDB_SUCCESS) return ret;
659 /* Now perform the checks for the 'defaultObjectCategory'. The
660 * lookup DN was already saved in "ac->dn" */
661 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
662 if (ret != LDB_SUCCESS) return ret;
664 } else if (strcmp(ac->type, "attributeSchema") == 0) {
665 const struct ldb_val *rdn_value;
666 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
667 if (rdn_value == NULL) {
668 return ldb_operr(ldb);
670 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
671 /* the RDN has prefix "CN" */
672 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
673 samdb_cn_to_lDAPDisplayName(ac->msg,
674 (const char *) rdn_value->data));
675 if (ret != LDB_SUCCESS) {
676 ldb_oom(ldb);
677 return ret;
681 /* do not allow to mark an attributeSchema as RODC filtered if it
682 * is system-critical */
683 if (check_rodc_critical_attribute(ac->msg)) {
684 ldb_asprintf_errstring(ldb,
685 "samldb: refusing schema add of %s - cannot combine critical attribute with RODC filtering",
686 ldb_dn_get_linearized(ac->msg->dn));
687 return LDB_ERR_UNWILLING_TO_PERFORM;
690 ret = samdb_find_or_add_attribute(ldb, ac->msg,
691 "isSingleValued", "FALSE");
692 if (ret != LDB_SUCCESS) return ret;
694 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
695 struct GUID guid;
696 /* a new GUID */
697 guid = GUID_random();
698 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
699 if (ret != LDB_SUCCESS) {
700 ldb_oom(ldb);
701 return ret;
705 /* handle msDS-IntID attribute */
706 ret = samldb_add_handle_msDS_IntId(ac);
707 if (ret != LDB_SUCCESS) return ret;
709 ret = samldb_add_step(ac, samldb_add_entry);
710 if (ret != LDB_SUCCESS) return ret;
712 } else {
713 ldb_asprintf_errstring(ldb,
714 "Invalid entry type!");
715 return LDB_ERR_OPERATIONS_ERROR;
718 return samldb_first_step(ac);
721 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
723 struct ldb_context *ldb;
724 const struct ldb_val *rdn_value;
725 struct dom_sid *sid;
726 int ret;
728 ldb = ldb_module_get_ctx(ac->module);
730 sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
731 if (sid == NULL) {
732 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
733 if (rdn_value == NULL) {
734 return ldb_operr(ldb);
736 sid = dom_sid_parse_talloc(ac->msg,
737 (const char *)rdn_value->data);
738 if (sid == NULL) {
739 ldb_set_errstring(ldb,
740 "samldb: No valid SID found in ForeignSecurityPrincipal CN!");
741 return LDB_ERR_CONSTRAINT_VIOLATION;
743 if (! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
744 return ldb_operr(ldb);
748 /* finally proceed with adding the entry */
749 ret = samldb_add_step(ac, samldb_add_entry);
750 if (ret != LDB_SUCCESS) return ret;
752 return samldb_first_step(ac);
755 static int samldb_schema_info_update(struct samldb_ctx *ac)
757 int ret;
758 struct ldb_context *ldb;
759 struct dsdb_schema *schema;
761 /* replicated update should always go through */
762 if (ldb_request_get_control(ac->req,
763 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
764 return LDB_SUCCESS;
767 /* do not update schemaInfo during provisioning */
768 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
769 return LDB_SUCCESS;
772 ldb = ldb_module_get_ctx(ac->module);
773 schema = dsdb_get_schema(ldb, NULL);
774 if (!schema) {
775 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
776 "samldb_schema_info_update: no dsdb_schema loaded");
777 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
778 return ldb_operr(ldb);
781 ret = dsdb_module_schema_info_update(ac->module, schema,
782 DSDB_FLAG_NEXT_MODULE, ac->req);
783 if (ret != LDB_SUCCESS) {
784 ldb_asprintf_errstring(ldb,
785 "samldb_schema_info_update: dsdb_module_schema_info_update failed with %s",
786 ldb_errstring(ldb));
787 return ret;
790 return LDB_SUCCESS;
794 * "Objectclass" trigger (MS-SAMR 3.1.1.8.1)
796 * Has to be invoked on "add" and "modify" operations on "user", "computer" and
797 * "group" objects.
798 * ac->msg contains the "add"/"modify" message
799 * ac->type contains the object type (main objectclass)
801 static int samldb_objectclass_trigger(struct samldb_ctx *ac)
803 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
804 struct loadparm_context *lp_ctx = talloc_get_type(ldb_get_opaque(ldb,
805 "loadparm"), struct loadparm_context);
806 struct ldb_message_element *el, *el2;
807 enum sid_generator sid_generator;
808 struct dom_sid *sid;
809 int ret;
811 /* make sure that "sAMAccountType" is not specified */
812 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
813 if (el != NULL) {
814 ldb_set_errstring(ldb,
815 "samldb: sAMAccountType must not be specified!");
816 return LDB_ERR_UNWILLING_TO_PERFORM;
819 /* Step 1: objectSid assignment */
821 /* Don't allow the objectSid to be changed. But beside the RELAX
822 * control we have also to guarantee that it can always be set with
823 * SYSTEM permissions. This is needed for the "samba3sam" backend. */
824 sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
825 if ((sid != NULL) && (!dsdb_module_am_system(ac->module)) &&
826 (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
827 ldb_set_errstring(ldb,
828 "samldb: objectSid must not be specified!");
829 return LDB_ERR_UNWILLING_TO_PERFORM;
832 /* but generate a new SID when we do have an add operations */
833 if ((sid == NULL) && (ac->req->operation == LDB_ADD)) {
834 sid_generator = lpcfg_sid_generator(lp_ctx);
835 if (sid_generator == SID_GENERATOR_INTERNAL) {
836 ret = samldb_add_step(ac, samldb_allocate_sid);
837 if (ret != LDB_SUCCESS) return ret;
841 if (strcmp(ac->type, "user") == 0) {
842 bool uac_generated = false;
844 /* Step 1.2: Default values */
845 ret = samdb_find_or_add_attribute(ldb, ac->msg,
846 "accountExpires", "9223372036854775807");
847 if (ret != LDB_SUCCESS) return ret;
848 ret = samdb_find_or_add_attribute(ldb, ac->msg,
849 "badPasswordTime", "0");
850 if (ret != LDB_SUCCESS) return ret;
851 ret = samdb_find_or_add_attribute(ldb, ac->msg,
852 "badPwdCount", "0");
853 if (ret != LDB_SUCCESS) return ret;
854 ret = samdb_find_or_add_attribute(ldb, ac->msg,
855 "codePage", "0");
856 if (ret != LDB_SUCCESS) return ret;
857 ret = samdb_find_or_add_attribute(ldb, ac->msg,
858 "countryCode", "0");
859 if (ret != LDB_SUCCESS) return ret;
860 ret = samdb_find_or_add_attribute(ldb, ac->msg,
861 "lastLogoff", "0");
862 if (ret != LDB_SUCCESS) return ret;
863 ret = samdb_find_or_add_attribute(ldb, ac->msg,
864 "lastLogon", "0");
865 if (ret != LDB_SUCCESS) return ret;
866 ret = samdb_find_or_add_attribute(ldb, ac->msg,
867 "logonCount", "0");
868 if (ret != LDB_SUCCESS) return ret;
869 ret = samdb_find_or_add_attribute(ldb, ac->msg,
870 "pwdLastSet", "0");
871 if (ret != LDB_SUCCESS) return ret;
873 /* On add operations we might need to generate a
874 * "userAccountControl" (if it isn't specified). */
875 el = ldb_msg_find_element(ac->msg, "userAccountControl");
876 if ((el == NULL) && (ac->req->operation == LDB_ADD)) {
877 ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
878 "userAccountControl",
879 UF_NORMAL_ACCOUNT);
880 if (ret != LDB_SUCCESS) {
881 return ret;
883 uac_generated = true;
886 el = ldb_msg_find_element(ac->msg, "userAccountControl");
887 if (el != NULL) {
888 uint32_t user_account_control, account_type;
890 /* Step 1.3: "userAccountControl" -> "sAMAccountType" mapping */
891 user_account_control = ldb_msg_find_attr_as_uint(ac->msg,
892 "userAccountControl",
895 /* Temporary duplicate accounts aren't allowed */
896 if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
897 return LDB_ERR_OTHER;
900 /* Workstation and (read-only) DC objects do need objectclass "computer" */
901 if ((samdb_find_attribute(ldb, ac->msg,
902 "objectclass", "computer") == NULL) &&
903 (user_account_control &
904 (UF_SERVER_TRUST_ACCOUNT | UF_WORKSTATION_TRUST_ACCOUNT))) {
905 ldb_set_errstring(ldb,
906 "samldb: Requested account type does need objectclass 'computer'!");
907 return LDB_ERR_OBJECT_CLASS_VIOLATION;
910 account_type = ds_uf2atype(user_account_control);
911 if (account_type == 0) {
912 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
913 return LDB_ERR_UNWILLING_TO_PERFORM;
915 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
916 "sAMAccountType",
917 account_type);
918 if (ret != LDB_SUCCESS) {
919 return ret;
921 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
922 el2->flags = LDB_FLAG_MOD_REPLACE;
924 /* "isCriticalSystemObject" might be set */
925 if (user_account_control &
926 (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
927 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
928 "TRUE");
929 if (ret != LDB_SUCCESS) {
930 return ret;
932 el2 = ldb_msg_find_element(ac->msg,
933 "isCriticalSystemObject");
934 el2->flags = LDB_FLAG_MOD_REPLACE;
935 } else if (user_account_control & UF_WORKSTATION_TRUST_ACCOUNT) {
936 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
937 "FALSE");
938 if (ret != LDB_SUCCESS) {
939 return ret;
941 el2 = ldb_msg_find_element(ac->msg,
942 "isCriticalSystemObject");
943 el2->flags = LDB_FLAG_MOD_REPLACE;
946 /* Step 1.4: "userAccountControl" -> "primaryGroupID" mapping */
947 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
948 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
949 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
950 "primaryGroupID", rid);
951 if (ret != LDB_SUCCESS) {
952 return ret;
954 el2 = ldb_msg_find_element(ac->msg,
955 "primaryGroupID");
956 el2->flags = LDB_FLAG_MOD_REPLACE;
959 /* Step 1.5: Add additional flags when needed */
960 /* Obviously this is done when the "userAccountControl"
961 * has been generated here (tested against Windows
962 * Server) */
963 if (uac_generated) {
964 user_account_control |= UF_ACCOUNTDISABLE;
965 user_account_control |= UF_PASSWD_NOTREQD;
967 ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
968 "userAccountControl",
969 user_account_control);
970 if (ret != LDB_SUCCESS) {
971 return ret;
976 } else if (strcmp(ac->type, "group") == 0) {
977 const char *tempstr;
979 /* Step 2.2: Default values */
980 tempstr = talloc_asprintf(ac->msg, "%d",
981 GTYPE_SECURITY_GLOBAL_GROUP);
982 if (tempstr == NULL) return ldb_operr(ldb);
983 ret = samdb_find_or_add_attribute(ldb, ac->msg,
984 "groupType", tempstr);
985 if (ret != LDB_SUCCESS) return ret;
987 /* Step 2.3: "groupType" -> "sAMAccountType" */
988 el = ldb_msg_find_element(ac->msg, "groupType");
989 if (el != NULL) {
990 uint32_t group_type, account_type;
992 group_type = ldb_msg_find_attr_as_uint(ac->msg,
993 "groupType", 0);
995 /* The creation of builtin groups requires the
996 * RELAX control */
997 if (group_type == GTYPE_SECURITY_BUILTIN_LOCAL_GROUP) {
998 if (ldb_request_get_control(ac->req,
999 LDB_CONTROL_RELAX_OID) == NULL) {
1000 return LDB_ERR_UNWILLING_TO_PERFORM;
1004 account_type = ds_gtype2atype(group_type);
1005 if (account_type == 0) {
1006 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1007 return LDB_ERR_UNWILLING_TO_PERFORM;
1009 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1010 "sAMAccountType",
1011 account_type);
1012 if (ret != LDB_SUCCESS) {
1013 return ret;
1015 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
1016 el2->flags = LDB_FLAG_MOD_REPLACE;
1020 return LDB_SUCCESS;
1024 * "Primary group ID" trigger (MS-SAMR 3.1.1.8.2)
1026 * Has to be invoked on "add" and "modify" operations on "user" and "computer"
1027 * objects.
1028 * ac->msg contains the "add"/"modify" message
1031 static int samldb_prim_group_set(struct samldb_ctx *ac)
1033 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1034 uint32_t rid;
1035 struct dom_sid *sid;
1036 struct ldb_result *res;
1037 int ret;
1038 const char *noattrs[] = { NULL };
1040 rid = ldb_msg_find_attr_as_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
1041 if (rid == (uint32_t) -1) {
1042 /* we aren't affected of any primary group set */
1043 return LDB_SUCCESS;
1045 } else if (!ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
1046 ldb_set_errstring(ldb,
1047 "The primary group isn't settable on add operations!");
1048 return LDB_ERR_UNWILLING_TO_PERFORM;
1051 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1052 if (sid == NULL) {
1053 return ldb_operr(ldb);
1056 ret = dsdb_module_search(ac->module, ac, &res, NULL, LDB_SCOPE_SUBTREE,
1057 noattrs, DSDB_FLAG_NEXT_MODULE,
1058 ac->req,
1059 "(objectSid=%s)",
1060 ldap_encode_ndr_dom_sid(ac, sid));
1061 if (ret != LDB_SUCCESS) {
1062 return ret;
1064 if (res->count != 1) {
1065 talloc_free(res);
1066 ldb_asprintf_errstring(ldb,
1067 "Failed to find primary group with RID %u!",
1068 rid);
1069 return LDB_ERR_UNWILLING_TO_PERFORM;
1071 talloc_free(res);
1073 return LDB_SUCCESS;
1076 static int samldb_prim_group_change(struct samldb_ctx *ac)
1078 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1079 const char * attrs[] = { "primaryGroupID", "memberOf", NULL };
1080 struct ldb_result *res, *group_res;
1081 struct ldb_message_element *el;
1082 struct ldb_message *msg;
1083 uint32_t prev_rid, new_rid;
1084 struct dom_sid *prev_sid, *new_sid;
1085 struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
1086 int ret;
1087 const char *noattrs[] = { NULL };
1089 el = dsdb_get_single_valued_attr(ac->msg, "primaryGroupID",
1090 ac->req->operation);
1091 if (el == NULL) {
1092 /* we are not affected */
1093 return LDB_SUCCESS;
1096 /* Fetch information from the existing object */
1098 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1099 DSDB_FLAG_NEXT_MODULE, ac->req, NULL);
1100 if (ret != LDB_SUCCESS) {
1101 return ret;
1103 if (res->count != 1) {
1104 return ldb_operr(ldb);
1107 /* Finds out the DN of the old primary group */
1109 prev_rid = ldb_msg_find_attr_as_uint(res->msgs[0], "primaryGroupID",
1110 (uint32_t) -1);
1111 if (prev_rid == (uint32_t) -1) {
1112 /* User objects do always have a mandatory "primaryGroupID"
1113 * attribute. If this doesn't exist then the object is of the
1114 * wrong type. This is the exact Windows error code */
1115 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1118 prev_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), prev_rid);
1119 if (prev_sid == NULL) {
1120 return ldb_operr(ldb);
1123 /* Finds out the DN of the new primary group
1124 * Notice: in order to parse the primary group ID correctly we create
1125 * a temporary message here. */
1127 msg = ldb_msg_new(ac->msg);
1128 if (msg == NULL) {
1129 return ldb_module_oom(ac->module);
1131 ret = ldb_msg_add(msg, el, 0);
1132 if (ret != LDB_SUCCESS) {
1133 return ret;
1135 new_rid = ldb_msg_find_attr_as_uint(msg, "primaryGroupID", (uint32_t) -1);
1136 talloc_free(msg);
1137 if (new_rid == (uint32_t) -1) {
1138 /* we aren't affected of any primary group change */
1139 return LDB_SUCCESS;
1142 if (prev_rid == new_rid) {
1143 return LDB_SUCCESS;
1146 ret = dsdb_module_search(ac->module, ac, &group_res, NULL, LDB_SCOPE_SUBTREE,
1147 noattrs, DSDB_FLAG_NEXT_MODULE,
1148 ac->req,
1149 "(objectSid=%s)",
1150 ldap_encode_ndr_dom_sid(ac, prev_sid));
1151 if (ret != LDB_SUCCESS) {
1152 return ret;
1154 if (group_res->count != 1) {
1155 return ldb_operr(ldb);
1157 prev_prim_group_dn = group_res->msgs[0]->dn;
1159 new_sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), new_rid);
1160 if (new_sid == NULL) {
1161 return ldb_operr(ldb);
1164 ret = dsdb_module_search(ac->module, ac, &group_res, NULL, LDB_SCOPE_SUBTREE,
1165 noattrs, DSDB_FLAG_NEXT_MODULE,
1166 ac->req,
1167 "(objectSid=%s)",
1168 ldap_encode_ndr_dom_sid(ac, new_sid));
1169 if (ret != LDB_SUCCESS) {
1170 return ret;
1172 if (group_res->count != 1) {
1173 /* Here we know if the specified new primary group candidate is
1174 * valid or not. */
1175 return LDB_ERR_UNWILLING_TO_PERFORM;
1177 new_prim_group_dn = group_res->msgs[0]->dn;
1179 /* We need to be already a normal member of the new primary
1180 * group in order to be successful. */
1181 el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
1182 ldb_dn_get_linearized(new_prim_group_dn));
1183 if (el == NULL) {
1184 return LDB_ERR_UNWILLING_TO_PERFORM;
1187 /* Remove the "member" attribute on the new primary group */
1188 msg = ldb_msg_new(ac->msg);
1189 if (msg == NULL) {
1190 return ldb_module_oom(ac->module);
1192 msg->dn = new_prim_group_dn;
1194 ret = samdb_msg_add_delval(ldb, msg, msg, "member",
1195 ldb_dn_get_linearized(ac->msg->dn));
1196 if (ret != LDB_SUCCESS) {
1197 return ret;
1200 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE, ac->req);
1201 if (ret != LDB_SUCCESS) {
1202 return ret;
1204 talloc_free(msg);
1206 /* Add a "member" attribute for the previous primary group */
1207 msg = ldb_msg_new(ac->msg);
1208 if (msg == NULL) {
1209 return ldb_module_oom(ac->module);
1211 msg->dn = prev_prim_group_dn;
1213 ret = samdb_msg_add_addval(ldb, msg, msg, "member",
1214 ldb_dn_get_linearized(ac->msg->dn));
1215 if (ret != LDB_SUCCESS) {
1216 return ret;
1219 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE, ac->req);
1220 if (ret != LDB_SUCCESS) {
1221 return ret;
1223 talloc_free(msg);
1225 return LDB_SUCCESS;
1228 static int samldb_prim_group_trigger(struct samldb_ctx *ac)
1230 int ret;
1232 if (ac->req->operation == LDB_ADD) {
1233 ret = samldb_prim_group_set(ac);
1234 } else {
1235 ret = samldb_prim_group_change(ac);
1238 return ret;
1243 * This function is called on LDB modify operations. It performs some additions/
1244 * replaces on the current LDB message when "userAccountControl" changes.
1246 static int samldb_user_account_control_change(struct samldb_ctx *ac)
1248 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1249 uint32_t user_account_control, old_user_account_control, account_type;
1250 struct ldb_message_element *el;
1251 struct ldb_message *tmp_msg;
1252 int ret;
1253 struct ldb_result *res;
1254 const char *attrs[] = { "userAccountControl", "objectClass", NULL };
1255 unsigned int i;
1256 bool is_computer = false;
1258 el = dsdb_get_single_valued_attr(ac->msg, "userAccountControl",
1259 ac->req->operation);
1260 if (el == NULL) {
1261 /* we are not affected */
1262 return LDB_SUCCESS;
1265 /* Create a temporary message for fetching the "userAccountControl" */
1266 tmp_msg = ldb_msg_new(ac->msg);
1267 if (tmp_msg == NULL) {
1268 return ldb_module_oom(ac->module);
1270 ret = ldb_msg_add(tmp_msg, el, 0);
1271 if (ret != LDB_SUCCESS) {
1272 return ret;
1274 user_account_control = ldb_msg_find_attr_as_uint(tmp_msg,
1275 "userAccountControl",
1277 talloc_free(tmp_msg);
1279 /* Temporary duplicate accounts aren't allowed */
1280 if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
1281 return LDB_ERR_OTHER;
1284 /* Fetch the old "userAccountControl" and "objectClass" */
1285 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1286 DSDB_FLAG_NEXT_MODULE, ac->req);
1287 if (ret != LDB_SUCCESS) {
1288 return ret;
1290 old_user_account_control = ldb_msg_find_attr_as_uint(res->msgs[0], "userAccountControl", 0);
1291 if (old_user_account_control == 0) {
1292 return ldb_operr(ldb);
1294 el = ldb_msg_find_element(res->msgs[0], "objectClass");
1295 if (el == NULL) {
1296 return ldb_operr(ldb);
1299 /* When we do not have objectclass "computer" we cannot switch to a (read-only) DC */
1300 for (i = 0; i < el->num_values; i++) {
1301 if (ldb_attr_cmp((char *)el->values[i].data, "computer") == 0) {
1302 is_computer = true;
1303 break;
1306 if (!is_computer &&
1307 (user_account_control & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT))) {
1308 ldb_set_errstring(ldb,
1309 "samldb: Requested account type does need objectclass 'computer'!");
1310 return LDB_ERR_UNWILLING_TO_PERFORM;
1314 * The functions "ds_uf2atype" and "ds_uf2prim_group_rid" are used as
1315 * detectors for account type changes.
1316 * So if the account type does change then we need to adjust the
1317 * "sAMAccountType", the "isCriticalSystemObject" and the
1318 * "primaryGroupID" attribute.
1320 if ((ds_uf2atype(user_account_control)
1321 == ds_uf2atype(old_user_account_control)) &&
1322 (ds_uf2prim_group_rid(user_account_control)
1323 == ds_uf2prim_group_rid(old_user_account_control))) {
1324 return LDB_SUCCESS;
1327 account_type = ds_uf2atype(user_account_control);
1328 if (account_type == 0) {
1329 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1330 return LDB_ERR_UNWILLING_TO_PERFORM;
1332 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
1333 account_type);
1334 if (ret != LDB_SUCCESS) {
1335 return ret;
1337 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
1338 el->flags = LDB_FLAG_MOD_REPLACE;
1340 /* "isCriticalSystemObject" might be set/changed */
1341 if (user_account_control
1342 & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
1343 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1344 "TRUE");
1345 if (ret != LDB_SUCCESS) {
1346 return ret;
1348 el = ldb_msg_find_element(ac->msg,
1349 "isCriticalSystemObject");
1350 el->flags = LDB_FLAG_MOD_REPLACE;
1351 } else if (user_account_control & UF_WORKSTATION_TRUST_ACCOUNT) {
1352 ret = ldb_msg_add_string(ac->msg, "isCriticalSystemObject",
1353 "FALSE");
1354 if (ret != LDB_SUCCESS) {
1355 return ret;
1357 el = ldb_msg_find_element(ac->msg,
1358 "isCriticalSystemObject");
1359 el->flags = LDB_FLAG_MOD_REPLACE;
1362 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
1363 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
1364 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1365 "primaryGroupID", rid);
1366 if (ret != LDB_SUCCESS) {
1367 return ret;
1369 el = ldb_msg_find_element(ac->msg,
1370 "primaryGroupID");
1371 el->flags = LDB_FLAG_MOD_REPLACE;
1374 return LDB_SUCCESS;
1377 static int samldb_group_type_change(struct samldb_ctx *ac)
1379 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1380 uint32_t group_type, old_group_type, account_type;
1381 struct ldb_message_element *el;
1382 struct ldb_message *tmp_msg;
1383 int ret;
1384 struct ldb_result *res;
1385 const char *attrs[] = { "groupType", NULL };
1387 el = dsdb_get_single_valued_attr(ac->msg, "groupType",
1388 ac->req->operation);
1389 if (el == NULL) {
1390 /* we are not affected */
1391 return LDB_SUCCESS;
1394 /* Create a temporary message for fetching the "groupType" */
1395 tmp_msg = ldb_msg_new(ac->msg);
1396 if (tmp_msg == NULL) {
1397 return ldb_module_oom(ac->module);
1399 ret = ldb_msg_add(tmp_msg, el, 0);
1400 if (ret != LDB_SUCCESS) {
1401 return ret;
1403 group_type = ldb_msg_find_attr_as_uint(tmp_msg, "groupType", 0);
1404 talloc_free(tmp_msg);
1406 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, attrs,
1407 DSDB_FLAG_NEXT_MODULE, ac->req);
1408 if (ret != LDB_SUCCESS) {
1409 return ret;
1411 old_group_type = ldb_msg_find_attr_as_uint(res->msgs[0], "groupType", 0);
1412 if (old_group_type == 0) {
1413 return ldb_operr(ldb);
1416 /* Group type switching isn't so easy as it seems: We can only
1417 * change in this directions: global <-> universal <-> local
1418 * On each step also the group type itself
1419 * (security/distribution) is variable. */
1421 if (ldb_request_get_control(ac->req, LDB_CONTROL_PROVISION_OID) == NULL) {
1422 switch (group_type) {
1423 case GTYPE_SECURITY_GLOBAL_GROUP:
1424 case GTYPE_DISTRIBUTION_GLOBAL_GROUP:
1425 /* change to "universal" allowed */
1426 if ((old_group_type == GTYPE_SECURITY_DOMAIN_LOCAL_GROUP) ||
1427 (old_group_type == GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP)) {
1428 ldb_set_errstring(ldb,
1429 "samldb: Change from security/distribution local group forbidden!");
1430 return LDB_ERR_UNWILLING_TO_PERFORM;
1432 break;
1434 case GTYPE_SECURITY_UNIVERSAL_GROUP:
1435 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP:
1436 /* each change allowed */
1437 break;
1438 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP:
1439 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP:
1440 /* change to "universal" allowed */
1441 if ((old_group_type == GTYPE_SECURITY_GLOBAL_GROUP) ||
1442 (old_group_type == GTYPE_DISTRIBUTION_GLOBAL_GROUP)) {
1443 ldb_set_errstring(ldb,
1444 "samldb: Change from security/distribution global group forbidden!");
1445 return LDB_ERR_UNWILLING_TO_PERFORM;
1447 break;
1449 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP:
1450 default:
1451 /* we don't allow this "groupType" values */
1452 return LDB_ERR_UNWILLING_TO_PERFORM;
1453 break;
1457 account_type = ds_gtype2atype(group_type);
1458 if (account_type == 0) {
1459 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1460 return LDB_ERR_UNWILLING_TO_PERFORM;
1462 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg, "sAMAccountType",
1463 account_type);
1464 if (ret != LDB_SUCCESS) {
1465 return ret;
1467 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
1468 el->flags = LDB_FLAG_MOD_REPLACE;
1470 return LDB_SUCCESS;
1473 static int samldb_sam_accountname_check(struct samldb_ctx *ac)
1475 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1476 const char *no_attrs[] = { NULL };
1477 struct ldb_result *res;
1478 const char *sam_accountname, *enc_str;
1479 struct ldb_message_element *el;
1480 struct ldb_message *tmp_msg;
1481 int ret;
1483 el = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName",
1484 ac->req->operation);
1485 if (el == NULL) {
1486 /* we are not affected */
1487 return LDB_SUCCESS;
1490 /* Create a temporary message for fetching the "sAMAccountName" */
1491 tmp_msg = ldb_msg_new(ac->msg);
1492 if (tmp_msg == NULL) {
1493 return ldb_module_oom(ac->module);
1495 ret = ldb_msg_add(tmp_msg, el, 0);
1496 if (ret != LDB_SUCCESS) {
1497 return ret;
1499 sam_accountname = talloc_steal(ac,
1500 ldb_msg_find_attr_as_string(tmp_msg, "sAMAccountName", NULL));
1501 talloc_free(tmp_msg);
1503 if (sam_accountname == NULL) {
1504 /* The "sAMAccountName" cannot be nothing */
1505 ldb_set_errstring(ldb,
1506 "samldb: Empty account names aren't allowed!");
1507 return LDB_ERR_UNWILLING_TO_PERFORM;
1510 enc_str = ldb_binary_encode_string(ac, sam_accountname);
1511 if (enc_str == NULL) {
1512 return ldb_module_oom(ac->module);
1515 /* Make sure that a "sAMAccountName" is only used once */
1517 ret = dsdb_module_search(ac->module, ac, &res, NULL, LDB_SCOPE_SUBTREE, no_attrs,
1518 DSDB_FLAG_NEXT_MODULE, ac->req,
1519 "(sAMAccountName=%s)", enc_str);
1520 if (ret != LDB_SUCCESS) {
1521 return ret;
1523 if (res->count > 1) {
1524 return ldb_operr(ldb);
1525 } else if (res->count == 1) {
1526 if (ldb_dn_compare(res->msgs[0]->dn, ac->msg->dn) != 0) {
1527 ldb_asprintf_errstring(ldb,
1528 "samldb: Account name (sAMAccountName) '%s' already in use!",
1529 sam_accountname);
1530 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1533 talloc_free(res);
1535 return LDB_SUCCESS;
1538 static int samldb_member_check(struct samldb_ctx *ac)
1540 static const char * const attrs[] = { "objectSid", "member", NULL };
1541 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1542 struct ldb_message_element *el;
1543 struct ldb_dn *member_dn;
1544 struct dom_sid *sid;
1545 struct ldb_result *res;
1546 struct dom_sid *group_sid;
1547 unsigned int i, j;
1548 int cnt;
1549 int ret;
1551 /* Fetch information from the existing object */
1553 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1554 DSDB_FLAG_NEXT_MODULE, ac->req, NULL);
1555 if (ret != LDB_SUCCESS) {
1556 return ret;
1558 if (res->count != 1) {
1559 return ldb_operr(ldb);
1562 group_sid = samdb_result_dom_sid(res, res->msgs[0], "objectSid");
1563 if (group_sid == NULL) {
1564 return ldb_operr(ldb);
1567 /* We've to walk over all modification entries and consider the "member"
1568 * ones. */
1569 for (i = 0; i < ac->msg->num_elements; i++) {
1570 if (ldb_attr_cmp(ac->msg->elements[i].name, "member") != 0) {
1571 continue;
1574 el = &ac->msg->elements[i];
1575 for (j = 0; j < el->num_values; j++) {
1576 struct ldb_message_element *mo;
1577 struct ldb_result *group_res;
1578 const char *group_attrs[] = { "primaryGroupID" , NULL };
1579 uint32_t prim_group_rid;
1581 member_dn = ldb_dn_from_ldb_val(ac, ldb,
1582 &el->values[j]);
1583 if (!ldb_dn_validate(member_dn)) {
1584 return ldb_operr(ldb);
1587 /* The "member" attribute can be modified with the
1588 * following restrictions (beside a valid DN):
1590 * - "add" operations can only be performed when the
1591 * member still doesn't exist - if not then return
1592 * ERR_ENTRY_ALREADY_EXISTS (not
1593 * ERR_ATTRIBUTE_OR_VALUE_EXISTS!)
1594 * - "delete" operations can only be performed when the
1595 * member does exist - if not then return
1596 * ERR_UNWILLING_TO_PERFORM (not
1597 * ERR_NO_SUCH_ATTRIBUTE!)
1598 * - primary group check
1600 mo = samdb_find_attribute(ldb, res->msgs[0], "member",
1601 ldb_dn_get_linearized(member_dn));
1602 if (mo == NULL) {
1603 cnt = 0;
1604 } else {
1605 cnt = 1;
1608 if ((cnt > 0) && (LDB_FLAG_MOD_TYPE(el->flags)
1609 == LDB_FLAG_MOD_ADD)) {
1610 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1612 if ((cnt == 0) && LDB_FLAG_MOD_TYPE(el->flags)
1613 == LDB_FLAG_MOD_DELETE) {
1614 return LDB_ERR_UNWILLING_TO_PERFORM;
1617 /* Denies to add "member"s to groups which are primary
1618 * ones for them - in this case return
1619 * ERR_ENTRY_ALREADY_EXISTS. */
1621 ret = dsdb_module_search_dn(ac->module, ac, &group_res,
1622 member_dn, group_attrs,
1623 DSDB_FLAG_NEXT_MODULE, ac->req);
1624 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
1625 /* member DN doesn't exist yet */
1626 continue;
1628 if (ret != LDB_SUCCESS) {
1629 return ret;
1631 prim_group_rid = ldb_msg_find_attr_as_uint(group_res->msgs[0], "primaryGroupID", (uint32_t)-1);
1632 if (prim_group_rid == (uint32_t) -1) {
1633 /* the member hasn't to be a user account ->
1634 * therefore no check needed in this case. */
1635 continue;
1638 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1639 prim_group_rid);
1640 if (sid == NULL) {
1641 return ldb_operr(ldb);
1644 if (dom_sid_equal(group_sid, sid)) {
1645 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1650 talloc_free(res);
1652 return LDB_SUCCESS;
1655 /* SAM objects have special rules regarding the "description" attribute on
1656 * modify operations. */
1657 static int samldb_description_check(struct samldb_ctx *ac, bool *modified)
1659 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1660 const char * const attrs[] = { "objectClass", "description", NULL };
1661 struct ldb_result *res;
1662 unsigned int i;
1663 int ret;
1665 /* Fetch information from the existing object */
1666 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1667 DSDB_FLAG_NEXT_MODULE | DSDB_SEARCH_SHOW_DELETED, ac->req,
1668 "(|(objectclass=user)(objectclass=group)(objectclass=samDomain)(objectclass=samServer))");
1669 if (ret != LDB_SUCCESS) {
1670 /* don't treat it specially ... let normal error codes
1671 happen from other places */
1672 ldb_reset_err_string(ldb);
1673 return LDB_SUCCESS;
1675 if (res->count == 0) {
1676 /* we didn't match the filter */
1677 talloc_free(res);
1678 return LDB_SUCCESS;
1681 /* We've to walk over all modification entries and consider the
1682 * "description" ones. */
1683 for (i = 0; i < ac->msg->num_elements; i++) {
1684 if (ldb_attr_cmp(ac->msg->elements[i].name, "description") == 0) {
1685 ac->msg->elements[i].flags |= LDB_FLAG_INTERNAL_FORCE_SINGLE_VALUE_CHECK;
1686 *modified = true;
1690 talloc_free(res);
1692 return LDB_SUCCESS;
1695 /* This trigger adapts the "servicePrincipalName" attributes if the
1696 * "dNSHostName" and/or "sAMAccountName" attribute change(s) */
1697 static int samldb_service_principal_names_change(struct samldb_ctx *ac)
1699 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1700 struct ldb_message_element *el = NULL, *el2 = NULL;
1701 struct ldb_message *msg;
1702 const char *attrs[] = { "servicePrincipalName", NULL };
1703 struct ldb_result *res;
1704 const char *dns_hostname = NULL, *old_dns_hostname = NULL,
1705 *sam_accountname = NULL, *old_sam_accountname = NULL;
1706 unsigned int i;
1707 int ret;
1709 el = dsdb_get_single_valued_attr(ac->msg, "dNSHostName",
1710 ac->req->operation);
1711 el2 = dsdb_get_single_valued_attr(ac->msg, "sAMAccountName",
1712 ac->req->operation);
1713 if ((el == NULL) && (el2 == NULL)) {
1714 /* we are not affected */
1715 return LDB_SUCCESS;
1718 /* Create a temporary message for fetching the "dNSHostName" */
1719 if (el != NULL) {
1720 const char *dns_attrs[] = { "dNSHostName", NULL };
1721 msg = ldb_msg_new(ac->msg);
1722 if (msg == NULL) {
1723 return ldb_module_oom(ac->module);
1725 ret = ldb_msg_add(msg, el, 0);
1726 if (ret != LDB_SUCCESS) {
1727 return ret;
1729 dns_hostname = talloc_steal(ac,
1730 ldb_msg_find_attr_as_string(msg, "dNSHostName", NULL));
1731 talloc_free(msg);
1733 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn,
1734 dns_attrs, DSDB_FLAG_NEXT_MODULE, ac->req);
1735 if (ret == LDB_SUCCESS) {
1736 old_dns_hostname = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
1740 /* Create a temporary message for fetching the "sAMAccountName" */
1741 if (el2 != NULL) {
1742 char *tempstr, *tempstr2;
1743 const char *acct_attrs[] = { "sAMAccountName", NULL };
1745 msg = ldb_msg_new(ac->msg);
1746 if (msg == NULL) {
1747 return ldb_module_oom(ac->module);
1749 ret = ldb_msg_add(msg, el2, 0);
1750 if (ret != LDB_SUCCESS) {
1751 return ret;
1753 tempstr = talloc_strdup(ac,
1754 ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL));
1755 talloc_free(msg);
1757 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->msg->dn, acct_attrs,
1758 DSDB_FLAG_NEXT_MODULE, ac->req);
1759 if (ret == LDB_SUCCESS) {
1760 tempstr2 = talloc_strdup(ac,
1761 ldb_msg_find_attr_as_string(res->msgs[0],
1762 "sAMAccountName", NULL));
1766 /* The "sAMAccountName" needs some additional trimming: we need
1767 * to remove the trailing "$"s if they exist. */
1768 if ((tempstr != NULL) && (tempstr[0] != '\0') &&
1769 (tempstr[strlen(tempstr) - 1] == '$')) {
1770 tempstr[strlen(tempstr) - 1] = '\0';
1772 if ((tempstr2 != NULL) && (tempstr2[0] != '\0') &&
1773 (tempstr2[strlen(tempstr2) - 1] == '$')) {
1774 tempstr2[strlen(tempstr2) - 1] = '\0';
1776 sam_accountname = tempstr;
1777 old_sam_accountname = tempstr2;
1780 if (old_dns_hostname == NULL) {
1781 /* we cannot change when the old name is unknown */
1782 dns_hostname = NULL;
1784 if ((old_dns_hostname != NULL) && (dns_hostname != NULL) &&
1785 (strcasecmp(old_dns_hostname, dns_hostname) == 0)) {
1786 /* The "dNSHostName" didn't change */
1787 dns_hostname = NULL;
1790 if (old_sam_accountname == NULL) {
1791 /* we cannot change when the old name is unknown */
1792 sam_accountname = NULL;
1794 if ((old_sam_accountname != NULL) && (sam_accountname != NULL) &&
1795 (strcasecmp(old_sam_accountname, sam_accountname) == 0)) {
1796 /* The "sAMAccountName" didn't change */
1797 sam_accountname = NULL;
1800 if ((dns_hostname == NULL) && (sam_accountname == NULL)) {
1801 /* Well, there are information missing (old name(s)) or the
1802 * names didn't change. We've nothing to do and can exit here */
1803 return LDB_SUCCESS;
1806 /* Potential "servicePrincipalName" changes in the same request have to
1807 * be handled before the update (Windows behaviour). */
1808 el = ldb_msg_find_element(ac->msg, "servicePrincipalName");
1809 if (el != NULL) {
1810 msg = ldb_msg_new(ac->msg);
1811 if (msg == NULL) {
1812 return ldb_module_oom(ac->module);
1814 msg->dn = ac->msg->dn;
1816 do {
1817 ret = ldb_msg_add(msg, el, el->flags);
1818 if (ret != LDB_SUCCESS) {
1819 return ret;
1822 ldb_msg_remove_element(ac->msg, el);
1824 el = ldb_msg_find_element(ac->msg,
1825 "servicePrincipalName");
1826 } while (el != NULL);
1828 ret = dsdb_module_modify(ac->module, msg,
1829 DSDB_FLAG_NEXT_MODULE, ac->req);
1830 if (ret != LDB_SUCCESS) {
1831 return ret;
1833 talloc_free(msg);
1836 /* Fetch the "servicePrincipalName"s if any */
1837 ret = dsdb_module_search(ac->module, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1838 DSDB_FLAG_NEXT_MODULE, ac->req, NULL);
1839 if (ret != LDB_SUCCESS) {
1840 return ret;
1842 if ((res->count != 1) || (res->msgs[0]->num_elements > 1)) {
1843 return ldb_operr(ldb);
1846 if (res->msgs[0]->num_elements == 1) {
1847 /* Yes, we do have "servicePrincipalName"s. First we update them
1848 * locally, that means we do always substitute the current
1849 * "dNSHostName" with the new one and/or "sAMAccountName"
1850 * without "$" with the new one and then we append this to the
1851 * modification request (Windows behaviour). */
1853 for (i = 0; i < res->msgs[0]->elements[0].num_values; i++) {
1854 char *old_str, *new_str, *pos;
1855 const char *tok;
1857 old_str = (char *)
1858 res->msgs[0]->elements[0].values[i].data;
1860 new_str = talloc_strdup(ac->msg,
1861 strtok_r(old_str, "/", &pos));
1862 if (new_str == NULL) {
1863 return ldb_module_oom(ac->module);
1866 while ((tok = strtok_r(NULL, "/", &pos)) != NULL) {
1867 if ((dns_hostname != NULL) &&
1868 (strcasecmp(tok, old_dns_hostname) == 0)) {
1869 tok = dns_hostname;
1871 if ((sam_accountname != NULL) &&
1872 (strcasecmp(tok, old_sam_accountname) == 0)) {
1873 tok = sam_accountname;
1876 new_str = talloc_asprintf(ac->msg, "%s/%s",
1877 new_str, tok);
1878 if (new_str == NULL) {
1879 return ldb_module_oom(ac->module);
1883 ret = ldb_msg_add_string(ac->msg,
1884 "servicePrincipalName",
1885 new_str);
1886 if (ret != LDB_SUCCESS) {
1887 return ret;
1891 el = ldb_msg_find_element(ac->msg, "servicePrincipalName");
1892 el->flags = LDB_FLAG_MOD_REPLACE;
1895 talloc_free(res);
1897 return LDB_SUCCESS;
1901 /* add */
1902 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
1904 struct ldb_context *ldb;
1905 struct samldb_ctx *ac;
1906 int ret;
1908 ldb = ldb_module_get_ctx(module);
1909 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
1911 /* do not manipulate our control entries */
1912 if (ldb_dn_is_special(req->op.add.message->dn)) {
1913 return ldb_next_request(module, req);
1916 ac = samldb_ctx_init(module, req);
1917 if (ac == NULL) {
1918 return ldb_operr(ldb);
1921 /* build the new msg */
1922 ac->msg = ldb_msg_copy_shallow(ac, req->op.add.message);
1923 if (ac->msg == NULL) {
1924 talloc_free(ac);
1925 ldb_debug(ldb, LDB_DEBUG_FATAL,
1926 "samldb_add: ldb_msg_copy_shallow failed!\n");
1927 return ldb_operr(ldb);
1930 if (samdb_find_attribute(ldb, ac->msg,
1931 "objectclass", "user") != NULL) {
1932 ac->type = "user";
1934 ret = samldb_prim_group_trigger(ac);
1935 if (ret != LDB_SUCCESS) {
1936 return ret;
1939 ret = samldb_objectclass_trigger(ac);
1940 if (ret != LDB_SUCCESS) {
1941 return ret;
1944 return samldb_fill_object(ac);
1947 if (samdb_find_attribute(ldb, ac->msg,
1948 "objectclass", "group") != NULL) {
1949 ac->type = "group";
1951 ret = samldb_objectclass_trigger(ac);
1952 if (ret != LDB_SUCCESS) {
1953 return ret;
1956 return samldb_fill_object(ac);
1959 /* perhaps a foreignSecurityPrincipal? */
1960 if (samdb_find_attribute(ldb, ac->msg,
1961 "objectclass",
1962 "foreignSecurityPrincipal") != NULL) {
1963 return samldb_fill_foreignSecurityPrincipal_object(ac);
1966 if (samdb_find_attribute(ldb, ac->msg,
1967 "objectclass", "classSchema") != NULL) {
1968 ret = samldb_schema_info_update(ac);
1969 if (ret != LDB_SUCCESS) {
1970 talloc_free(ac);
1971 return ret;
1974 ac->type = "classSchema";
1975 return samldb_fill_object(ac);
1978 if (samdb_find_attribute(ldb, ac->msg,
1979 "objectclass", "attributeSchema") != NULL) {
1980 ret = samldb_schema_info_update(ac);
1981 if (ret != LDB_SUCCESS) {
1982 talloc_free(ac);
1983 return ret;
1986 ac->type = "attributeSchema";
1987 return samldb_fill_object(ac);
1990 talloc_free(ac);
1992 /* nothing matched, go on */
1993 return ldb_next_request(module, req);
1996 /* modify */
1997 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
1999 struct ldb_context *ldb;
2000 struct samldb_ctx *ac;
2001 struct ldb_message_element *el, *el2;
2002 bool modified = false;
2003 int ret;
2005 if (ldb_dn_is_special(req->op.mod.message->dn)) {
2006 /* do not manipulate our control entries */
2007 return ldb_next_request(module, req);
2010 ldb = ldb_module_get_ctx(module);
2012 /* make sure that "objectSid" is not specified */
2013 el = ldb_msg_find_element(req->op.mod.message, "objectSid");
2014 if (el != NULL) {
2015 ldb_set_errstring(ldb,
2016 "samldb: objectSid must not be specified!");
2017 return LDB_ERR_UNWILLING_TO_PERFORM;
2019 /* make sure that "sAMAccountType" is not specified */
2020 el = ldb_msg_find_element(req->op.mod.message, "sAMAccountType");
2021 if (el != NULL) {
2022 ldb_set_errstring(ldb,
2023 "samldb: sAMAccountType must not be specified!");
2024 return LDB_ERR_UNWILLING_TO_PERFORM;
2026 /* make sure that "isCriticalSystemObject" is not specified */
2027 el = ldb_msg_find_element(req->op.mod.message, "isCriticalSystemObject");
2028 if (el != NULL) {
2029 if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID) == NULL) {
2030 ldb_set_errstring(ldb,
2031 "samldb: isCriticalSystemObject must not be specified!");
2032 return LDB_ERR_UNWILLING_TO_PERFORM;
2036 /* msDS-IntId is not allowed to be modified
2037 * except when modification comes from replication */
2038 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
2039 if (!ldb_request_get_control(req,
2040 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
2041 return LDB_ERR_CONSTRAINT_VIOLATION;
2045 ac = samldb_ctx_init(module, req);
2046 if (ac == NULL) {
2047 return ldb_operr(ldb);
2050 /* build the new msg */
2051 ac->msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
2052 if (ac->msg == NULL) {
2053 talloc_free(ac);
2054 ldb_debug(ldb, LDB_DEBUG_FATAL,
2055 "samldb_modify: ldb_msg_copy_shallow failed!\n");
2056 return ldb_operr(ldb);
2059 el = ldb_msg_find_element(ac->msg, "primaryGroupID");
2060 if (el != NULL) {
2061 ret = samldb_prim_group_change(ac);
2062 if (ret != LDB_SUCCESS) {
2063 return ret;
2067 el = ldb_msg_find_element(ac->msg, "userAccountControl");
2068 if (el != NULL) {
2069 modified = true;
2070 ret = samldb_user_account_control_change(ac);
2071 if (ret != LDB_SUCCESS) {
2072 return ret;
2076 el = ldb_msg_find_element(ac->msg, "groupType");
2077 if (el != NULL) {
2078 modified = true;
2079 ret = samldb_group_type_change(ac);
2080 if (ret != LDB_SUCCESS) {
2081 return ret;
2085 el = ldb_msg_find_element(ac->msg, "sAMAccountName");
2086 if (el != NULL) {
2087 ret = samldb_sam_accountname_check(ac);
2088 if (ret != LDB_SUCCESS) {
2089 return ret;
2093 el = ldb_msg_find_element(ac->msg, "member");
2094 if (el != NULL) {
2095 ret = samldb_member_check(ac);
2096 if (ret != LDB_SUCCESS) {
2097 return ret;
2101 el = ldb_msg_find_element(ac->msg, "description");
2102 if (el != NULL) {
2103 ret = samldb_description_check(ac, &modified);
2104 if (ret != LDB_SUCCESS) {
2105 return ret;
2109 el = ldb_msg_find_element(ac->msg, "dNSHostName");
2110 el2 = ldb_msg_find_element(ac->msg, "sAMAccountName");
2111 if ((el != NULL) || (el2 != NULL)) {
2112 modified = true;
2113 ret = samldb_service_principal_names_change(ac);
2114 if (ret != LDB_SUCCESS) {
2115 return ret;
2119 if (modified) {
2120 struct ldb_request *child_req;
2122 /* Now perform the real modifications as a child request */
2123 ret = ldb_build_mod_req(&child_req, ldb, ac,
2124 ac->msg,
2125 req->controls,
2126 req, dsdb_next_callback,
2127 req);
2128 LDB_REQ_SET_LOCATION(child_req);
2129 if (ret != LDB_SUCCESS) {
2130 return ret;
2133 return ldb_next_request(module, child_req);
2136 talloc_free(ac);
2138 /* no change which interests us, go on */
2139 return ldb_next_request(module, req);
2142 /* delete */
2144 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
2146 struct ldb_context *ldb;
2147 struct dom_sid *sid;
2148 uint32_t rid;
2149 NTSTATUS status;
2150 int ret;
2151 struct ldb_result *res;
2152 const char *attrs[] = { "objectSid", NULL };
2153 const char *noattrs[] = { NULL };
2155 ldb = ldb_module_get_ctx(ac->module);
2157 /* Finds out the SID/RID of the SAM object */
2158 ret = dsdb_module_search_dn(ac->module, ac, &res, ac->req->op.del.dn, attrs, DSDB_FLAG_NEXT_MODULE, ac->req);
2159 if (ret != LDB_SUCCESS) {
2160 return ret;
2163 sid = samdb_result_dom_sid(ac, res->msgs[0], "objectSid");
2164 if (sid == NULL) {
2165 /* No SID - it might not be a SAM object - therefore ok */
2166 return LDB_SUCCESS;
2168 status = dom_sid_split_rid(ac, sid, NULL, &rid);
2169 if (!NT_STATUS_IS_OK(status)) {
2170 return ldb_operr(ldb);
2172 if (rid == 0) {
2173 /* Special object (security principal?) */
2174 return LDB_SUCCESS;
2177 /* Deny delete requests from groups which are primary ones */
2178 ret = dsdb_module_search(ac->module, ac, &res, NULL, LDB_SCOPE_SUBTREE, noattrs,
2179 DSDB_FLAG_NEXT_MODULE,
2180 ac->req,
2181 "(&(primaryGroupID=%u)(objectClass=user))", rid);
2182 if (ret != LDB_SUCCESS) {
2183 return ret;
2185 if (res->count > 0) {
2186 return LDB_ERR_ENTRY_ALREADY_EXISTS;
2189 return LDB_SUCCESS;
2192 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
2194 struct samldb_ctx *ac;
2195 int ret;
2197 if (ldb_dn_is_special(req->op.del.dn)) {
2198 /* do not manipulate our control entries */
2199 return ldb_next_request(module, req);
2202 ac = samldb_ctx_init(module, req);
2203 if (ac == NULL) {
2204 return ldb_operr(ldb_module_get_ctx(module));
2207 ret = samldb_prim_group_users_check(ac);
2208 if (ret != LDB_SUCCESS) {
2209 return ret;
2212 talloc_free(ac);
2214 return ldb_next_request(module, req);
2217 /* extended */
2219 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
2221 struct ldb_context *ldb = ldb_module_get_ctx(module);
2222 struct dsdb_fsmo_extended_op *exop;
2223 int ret;
2225 exop = talloc_get_type(req->op.extended.data,
2226 struct dsdb_fsmo_extended_op);
2227 if (!exop) {
2228 ldb_set_errstring(ldb,
2229 "samldb_extended_allocate_rid_pool: invalid extended data");
2230 return LDB_ERR_PROTOCOL_ERROR;
2233 ret = ridalloc_allocate_rid_pool_fsmo(module, exop, req);
2234 if (ret != LDB_SUCCESS) {
2235 return ret;
2238 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
2241 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
2243 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
2244 return samldb_extended_allocate_rid_pool(module, req);
2247 return ldb_next_request(module, req);
2251 static const struct ldb_module_ops ldb_samldb_module_ops = {
2252 .name = "samldb",
2253 .add = samldb_add,
2254 .modify = samldb_modify,
2255 .del = samldb_delete,
2256 .extended = samldb_extended
2260 int ldb_samldb_module_init(const char *version)
2262 LDB_MODULE_CHECK_VERSION(version);
2263 return ldb_register_module(&ldb_samldb_module_ops);