4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
5 Copyright (C) Simo Sorce 2004-2008
6 Copyright (C) Matthias Dieter Wallnöfer 2009-2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * Component: ldb samldb module
27 * Description: various internal DSDB triggers - most for SAM specific objects
33 #include "libcli/ldap/ldap_ndr.h"
34 #include "ldb_module.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "dsdb/samdb/ldb_modules/util.h"
37 #include "dsdb/samdb/ldb_modules/ridalloc.h"
38 #include "libcli/security/security.h"
39 #include "librpc/gen_ndr/ndr_security.h"
41 #include "param/param.h"
45 typedef int (*samldb_step_fn_t)(struct samldb_ctx *);
48 struct samldb_step *next;
53 struct ldb_module *module;
54 struct ldb_request *req;
56 /* used for add operations */
59 /* the resulting message */
60 struct ldb_message *msg;
62 /* used in "samldb_find_for_defaultObjectCategory" */
63 struct ldb_dn *dn, *res_dn;
65 /* all the async steps necessary to complete the operation */
66 struct samldb_step *steps;
67 struct samldb_step *curstep;
69 /* If someone set an ares to forward controls and response back to the caller */
70 struct ldb_reply *ares;
73 static struct samldb_ctx *samldb_ctx_init(struct ldb_module *module,
74 struct ldb_request *req)
76 struct ldb_context *ldb;
77 struct samldb_ctx *ac;
79 ldb = ldb_module_get_ctx(module);
81 ac = talloc_zero(req, struct samldb_ctx);
93 static int samldb_add_step(struct samldb_ctx *ac, samldb_step_fn_t fn)
95 struct samldb_step *step, *stepper;
97 step = talloc_zero(ac, struct samldb_step);
99 return ldb_oom(ldb_module_get_ctx(ac->module));
104 if (ac->steps == NULL) {
108 if (ac->curstep == NULL)
109 return ldb_operr(ldb_module_get_ctx(ac->module));
110 for (stepper = ac->curstep; stepper->next != NULL;
111 stepper = stepper->next);
112 stepper->next = step;
118 static int samldb_first_step(struct samldb_ctx *ac)
120 if (ac->steps == NULL) {
121 return ldb_operr(ldb_module_get_ctx(ac->module));
124 ac->curstep = ac->steps;
125 return ac->curstep->fn(ac);
128 static int samldb_next_step(struct samldb_ctx *ac)
130 if (ac->curstep->next) {
131 ac->curstep = ac->curstep->next;
132 return ac->curstep->fn(ac);
135 /* We exit the samldb module here. If someone set an "ares" to forward
136 * controls and response back to the caller, use them. */
138 return ldb_module_done(ac->req, ac->ares->controls,
139 ac->ares->response, LDB_SUCCESS);
141 return ldb_module_done(ac->req, NULL, NULL, LDB_SUCCESS);
146 /* sAMAccountName handling */
148 static int samldb_generate_sAMAccountName(struct ldb_context *ldb,
149 struct ldb_message *msg)
153 /* Format: $000000-000000000000 */
155 name = talloc_asprintf(msg, "$%.6X-%.6X%.6X",
156 (unsigned int)generate_random(),
157 (unsigned int)generate_random(),
158 (unsigned int)generate_random());
162 return ldb_msg_add_steal_string(msg, "sAMAccountName", name);
165 static int samldb_check_sAMAccountName(struct samldb_ctx *ac)
167 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
171 if (ldb_msg_find_element(ac->msg, "sAMAccountName") == NULL) {
172 ret = samldb_generate_sAMAccountName(ldb, ac->msg);
173 if (ret != LDB_SUCCESS) {
178 name = ldb_msg_find_attr_as_string(ac->msg, "sAMAccountName", NULL);
180 return ldb_operr(ldb);
183 ret = samdb_search_count(ldb, ac, NULL, "(sAMAccountName=%s)",
184 ldb_binary_encode_string(ac, name));
185 if ((ret < 0) || (ret > 1)) {
186 return ldb_operr(ldb);
189 return LDB_ERR_ENTRY_ALREADY_EXISTS;
192 return samldb_next_step(ac);
196 static bool samldb_msg_add_sid(struct ldb_message *msg,
198 const struct dom_sid *sid)
201 enum ndr_err_code ndr_err;
203 ndr_err = ndr_push_struct_blob(&v, msg, sid,
204 (ndr_push_flags_fn_t)ndr_push_dom_sid);
205 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
208 return (ldb_msg_add_value(msg, name, &v, NULL) == 0);
212 /* allocate a SID using our RID Set */
213 static int samldb_allocate_sid(struct samldb_ctx *ac)
217 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
220 ret = ridalloc_allocate_rid(ac->module, &rid);
221 if (ret != LDB_SUCCESS) {
225 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
227 return ldb_module_oom(ac->module);
230 if ( ! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
231 return ldb_operr(ldb);
234 return samldb_next_step(ac);
238 see if a krbtgt_number is available
240 static bool samldb_krbtgtnumber_available(struct samldb_ctx *ac,
241 uint32_t krbtgt_number)
243 TALLOC_CTX *tmp_ctx = talloc_new(ac);
244 struct ldb_result *res;
245 const char *no_attrs[] = { NULL };
248 ret = dsdb_module_search(ac->module, tmp_ctx, &res, NULL,
249 LDB_SCOPE_SUBTREE, no_attrs,
250 DSDB_FLAG_NEXT_MODULE,
251 "(msDC-SecondaryKrbTgtNumber=%u)",
253 if (ret == LDB_SUCCESS && res->count == 0) {
254 talloc_free(tmp_ctx);
257 talloc_free(tmp_ctx);
261 /* special handling for add in RODC join */
262 static int samldb_rodc_add(struct samldb_ctx *ac)
264 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
265 uint32_t krbtgt_number, i_start, i;
269 /* find a unused msDC-SecondaryKrbTgtNumber */
270 i_start = generate_random() & 0xFFFF;
275 for (i=i_start; i<=0xFFFF; i++) {
276 if (samldb_krbtgtnumber_available(ac, i)) {
281 for (i=1; i<i_start; i++) {
282 if (samldb_krbtgtnumber_available(ac, i)) {
288 ldb_asprintf_errstring(ldb,
289 "%08X: Unable to find available msDS-SecondaryKrbTgtNumber",
290 W_ERROR_V(WERR_NO_SYSTEM_RESOURCES));
291 return LDB_ERR_OTHER;
294 ret = ldb_msg_add_empty(ac->msg, "msDS-SecondaryKrbTgtNumber",
295 LDB_FLAG_INTERNAL_DISABLE_VALIDATION, NULL);
296 if (ret != LDB_SUCCESS) {
297 return ldb_operr(ldb);
300 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
301 "msDS-SecondaryKrbTgtNumber", krbtgt_number);
302 if (ret != LDB_SUCCESS) {
303 return ldb_operr(ldb);
306 ret = ldb_msg_add_fmt(ac->msg, "sAMAccountName", "krbtgt_%u",
308 if (ret != LDB_SUCCESS) {
309 return ldb_operr(ldb);
312 newpass = generate_random_password(ac->msg, 128, 255);
313 if (newpass == NULL) {
314 return ldb_operr(ldb);
317 ret = ldb_msg_add_steal_string(ac->msg, "clearTextPassword", newpass);
318 if (ret != LDB_SUCCESS) {
319 return ldb_operr(ldb);
322 return samldb_next_step(ac);
325 static int samldb_find_for_defaultObjectCategory(struct samldb_ctx *ac)
327 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
328 struct ldb_result *res;
329 const char *no_attrs[] = { NULL };
334 ret = dsdb_module_search(ac->module, ac, &res,
335 ac->dn, LDB_SCOPE_BASE, no_attrs,
336 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT
337 | DSDB_FLAG_NEXT_MODULE,
338 "(objectClass=classSchema)");
339 if (ret == LDB_ERR_NO_SUCH_OBJECT) {
340 /* Don't be pricky when the DN doesn't exist if we have the */
341 /* RELAX control specified */
342 if (ldb_request_get_control(ac->req,
343 LDB_CONTROL_RELAX_OID) == NULL) {
344 ldb_set_errstring(ldb,
345 "samldb_find_defaultObjectCategory: "
346 "Invalid DN for 'defaultObjectCategory'!");
347 return LDB_ERR_CONSTRAINT_VIOLATION;
350 if ((ret != LDB_ERR_NO_SUCH_OBJECT) && (ret != LDB_SUCCESS)) {
356 return samldb_next_step(ac);
360 * msDS-IntId attributeSchema attribute handling
361 * during LDB_ADD request processing
363 static int samldb_add_handle_msDS_IntId(struct samldb_ctx *ac)
368 int32_t system_flags;
369 struct ldb_context *ldb;
370 struct ldb_result *ldb_res;
371 struct ldb_dn *schema_dn;
373 ldb = ldb_module_get_ctx(ac->module);
374 schema_dn = ldb_get_schema_basedn(ldb);
376 /* replicated update should always go through */
377 if (ldb_request_get_control(ac->req,
378 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
382 /* msDS-IntId is handled by system and should never be
383 * passed by clients */
384 if (ldb_msg_find_element(ac->msg, "msDS-IntId")) {
385 return LDB_ERR_UNWILLING_TO_PERFORM;
388 /* do not generate msDS-IntId if Relax control is passed */
389 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
393 /* check Functional Level */
394 if (dsdb_functional_level(ldb) < DS_DOMAIN_FUNCTION_2003) {
398 /* check systemFlags for SCHEMA_BASE_OBJECT flag */
399 system_flags = ldb_msg_find_attr_as_int(ac->msg, "systemFlags", 0);
400 if (system_flags & SYSTEM_FLAG_SCHEMA_BASE_OBJECT) {
404 /* Generate new value for msDs-IntId
405 * Value should be in 0x80000000..0xBFFFFFFF range */
406 msds_intid = generate_random() % 0X3FFFFFFF;
407 msds_intid += 0x80000000;
409 /* probe id values until unique one is found */
412 if (msds_intid > 0xBFFFFFFF) {
413 msds_intid = 0x80000001;
416 ret = dsdb_module_search(ac->module, ac,
418 schema_dn, LDB_SCOPE_ONELEVEL, NULL,
419 DSDB_FLAG_NEXT_MODULE,
420 "(msDS-IntId=%d)", msds_intid);
421 if (ret != LDB_SUCCESS) {
422 ldb_debug_set(ldb, LDB_DEBUG_ERROR,
423 __location__": Searching for msDS-IntId=%d failed - %s\n",
426 return ldb_operr(ldb);
428 id_exists = (ldb_res->count > 0);
430 talloc_free(ldb_res);
433 return samdb_msg_add_int(ldb, ac->msg, ac->msg, "msDS-IntId",
439 * samldb_add_entry (async)
442 static int samldb_add_entry_callback(struct ldb_request *req,
443 struct ldb_reply *ares)
445 struct ldb_context *ldb;
446 struct samldb_ctx *ac;
449 ac = talloc_get_type(req->context, struct samldb_ctx);
450 ldb = ldb_module_get_ctx(ac->module);
453 return ldb_module_done(ac->req, NULL, NULL,
454 LDB_ERR_OPERATIONS_ERROR);
457 if (ares->type == LDB_REPLY_REFERRAL) {
458 return ldb_module_send_referral(ac->req, ares->referral);
461 if (ares->error != LDB_SUCCESS) {
462 return ldb_module_done(ac->req, ares->controls,
463 ares->response, ares->error);
465 if (ares->type != LDB_REPLY_DONE) {
466 ldb_set_errstring(ldb,
467 "Invalid reply type!\n");
468 return ldb_module_done(ac->req, NULL, NULL,
469 LDB_ERR_OPERATIONS_ERROR);
472 /* The caller may wish to get controls back from the add */
473 ac->ares = talloc_steal(ac, ares);
475 ret = samldb_next_step(ac);
476 if (ret != LDB_SUCCESS) {
477 return ldb_module_done(ac->req, NULL, NULL, ret);
482 static int samldb_add_entry(struct samldb_ctx *ac)
484 struct ldb_context *ldb;
485 struct ldb_request *req;
488 ldb = ldb_module_get_ctx(ac->module);
490 ret = ldb_build_add_req(&req, ldb, ac,
493 ac, samldb_add_entry_callback,
495 LDB_REQ_SET_LOCATION(req);
496 if (ret != LDB_SUCCESS) {
500 return ldb_next_request(ac->module, req);
504 * return true if msg carries an attributeSchema that is intended to be RODC
505 * filtered but is also a system-critical attribute.
507 static bool check_rodc_critical_attribute(struct ldb_message *msg)
509 uint32_t schemaFlagsEx, searchFlags, rodc_filtered_flags;
511 schemaFlagsEx = ldb_msg_find_attr_as_uint(msg, "schemaFlagsEx", 0);
512 searchFlags = ldb_msg_find_attr_as_uint(msg, "searchFlags", 0);
513 rodc_filtered_flags = (SEARCH_FLAG_RODC_ATTRIBUTE
514 | SEARCH_FLAG_CONFIDENTIAL);
516 if ((schemaFlagsEx & SCHEMA_FLAG_ATTR_IS_CRITICAL) &&
517 ((searchFlags & rodc_filtered_flags) == rodc_filtered_flags)) {
525 static int samldb_fill_object(struct samldb_ctx *ac)
527 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
530 /* Add informations for the different account types */
531 if (strcmp(ac->type, "user") == 0) {
532 struct ldb_control *rodc_control = ldb_request_get_control(ac->req,
533 LDB_CONTROL_RODC_DCPROMO_OID);
534 if (rodc_control != NULL) {
535 /* see [MS-ADTS] 3.1.1.3.4.1.23 LDAP_SERVER_RODC_DCPROMO_OID */
536 rodc_control->critical = false;
537 ret = samldb_add_step(ac, samldb_rodc_add);
538 if (ret != LDB_SUCCESS) return ret;
541 /* check if we have a valid sAMAccountName */
542 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
543 if (ret != LDB_SUCCESS) return ret;
545 ret = samldb_add_step(ac, samldb_add_entry);
546 if (ret != LDB_SUCCESS) return ret;
548 } else if (strcmp(ac->type, "group") == 0) {
549 /* check if we have a valid sAMAccountName */
550 ret = samldb_add_step(ac, samldb_check_sAMAccountName);
551 if (ret != LDB_SUCCESS) return ret;
553 ret = samldb_add_step(ac, samldb_add_entry);
554 if (ret != LDB_SUCCESS) return ret;
556 } else if (strcmp(ac->type, "classSchema") == 0) {
557 const struct ldb_val *rdn_value, *def_obj_cat_val;
559 ret = samdb_find_or_add_attribute(ldb, ac->msg,
561 if (ret != LDB_SUCCESS) return ret;
563 /* do not allow to mark an attributeSchema as RODC filtered if it
564 * is system-critical */
565 if (check_rodc_critical_attribute(ac->msg)) {
566 ldb_asprintf_errstring(ldb, "Refusing schema add of %s - cannot combine critical class with RODC filtering",
567 ldb_dn_get_linearized(ac->msg->dn));
568 return LDB_ERR_UNWILLING_TO_PERFORM;
571 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
572 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
573 /* the RDN has prefix "CN" */
574 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
575 samdb_cn_to_lDAPDisplayName(ac->msg,
576 (const char *) rdn_value->data));
577 if (ret != LDB_SUCCESS) {
583 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
586 guid = GUID_random();
587 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
588 if (ret != LDB_SUCCESS) {
594 def_obj_cat_val = ldb_msg_find_ldb_val(ac->msg,
595 "defaultObjectCategory");
596 if (def_obj_cat_val != NULL) {
597 /* "defaultObjectCategory" has been set by the caller.
598 * Do some checks for consistency.
599 * NOTE: The real constraint check (that
600 * 'defaultObjectCategory' is the DN of the new
601 * objectclass or any parent of it) is still incomplete.
602 * For now we say that 'defaultObjectCategory' is valid
603 * if it exists and it is of objectclass "classSchema".
605 ac->dn = ldb_dn_from_ldb_val(ac, ldb, def_obj_cat_val);
606 if (ac->dn == NULL) {
607 ldb_set_errstring(ldb,
608 "Invalid DN for 'defaultObjectCategory'!");
609 return LDB_ERR_CONSTRAINT_VIOLATION;
612 /* "defaultObjectCategory" has not been set by the
613 * caller. Use the entry DN for it. */
614 ac->dn = ac->msg->dn;
616 ret = ldb_msg_add_string(ac->msg, "defaultObjectCategory",
617 ldb_dn_alloc_linearized(ac->msg, ac->dn));
618 if (ret != LDB_SUCCESS) {
624 ret = samldb_add_step(ac, samldb_add_entry);
625 if (ret != LDB_SUCCESS) return ret;
627 /* Now perform the checks for the 'defaultObjectCategory'. The
628 * lookup DN was already saved in "ac->dn" */
629 ret = samldb_add_step(ac, samldb_find_for_defaultObjectCategory);
630 if (ret != LDB_SUCCESS) return ret;
632 } else if (strcmp(ac->type, "attributeSchema") == 0) {
633 const struct ldb_val *rdn_value;
634 rdn_value = ldb_dn_get_rdn_val(ac->msg->dn);
635 if (!ldb_msg_find_element(ac->msg, "lDAPDisplayName")) {
636 /* the RDN has prefix "CN" */
637 ret = ldb_msg_add_string(ac->msg, "lDAPDisplayName",
638 samdb_cn_to_lDAPDisplayName(ac->msg,
639 (const char *) rdn_value->data));
640 if (ret != LDB_SUCCESS) {
646 /* do not allow to mark an attributeSchema as RODC filtered if it
647 * is system-critical */
648 if (check_rodc_critical_attribute(ac->msg)) {
649 ldb_asprintf_errstring(ldb,
650 "samldb: refusing schema add of %s - cannot combine critical attribute with RODC filtering",
651 ldb_dn_get_linearized(ac->msg->dn));
652 return LDB_ERR_UNWILLING_TO_PERFORM;
655 ret = samdb_find_or_add_attribute(ldb, ac->msg,
656 "isSingleValued", "FALSE");
657 if (ret != LDB_SUCCESS) return ret;
659 if (!ldb_msg_find_element(ac->msg, "schemaIDGUID")) {
662 guid = GUID_random();
663 ret = dsdb_msg_add_guid(ac->msg, &guid, "schemaIDGUID");
664 if (ret != LDB_SUCCESS) {
670 /* handle msDS-IntID attribute */
671 ret = samldb_add_handle_msDS_IntId(ac);
672 if (ret != LDB_SUCCESS) return ret;
674 ret = samldb_add_step(ac, samldb_add_entry);
675 if (ret != LDB_SUCCESS) return ret;
678 ldb_asprintf_errstring(ldb,
679 "Invalid entry type!");
680 return LDB_ERR_OPERATIONS_ERROR;
683 return samldb_first_step(ac);
686 static int samldb_fill_foreignSecurityPrincipal_object(struct samldb_ctx *ac)
688 struct ldb_context *ldb;
692 ldb = ldb_module_get_ctx(ac->module);
694 sid = samdb_result_dom_sid(ac->msg, ac->msg, "objectSid");
696 sid = dom_sid_parse_talloc(ac->msg,
697 (const char *)ldb_dn_get_rdn_val(ac->msg->dn)->data);
699 ldb_set_errstring(ldb,
700 "samldb: No valid SID found in ForeignSecurityPrincipal CN!");
701 return LDB_ERR_CONSTRAINT_VIOLATION;
703 if (! samldb_msg_add_sid(ac->msg, "objectSid", sid)) {
704 return ldb_operr(ldb);
708 /* finally proceed with adding the entry */
709 ret = samldb_add_step(ac, samldb_add_entry);
710 if (ret != LDB_SUCCESS) return ret;
712 return samldb_first_step(ac);
715 static int samldb_schema_info_update(struct samldb_ctx *ac)
718 struct ldb_context *ldb;
719 struct dsdb_schema *schema;
721 /* replicated update should always go through */
722 if (ldb_request_get_control(ac->req,
723 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
727 /* do not update schemaInfo during provisioning */
728 if (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
732 ldb = ldb_module_get_ctx(ac->module);
733 schema = dsdb_get_schema(ldb, NULL);
735 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
736 "samldb_schema_info_update: no dsdb_schema loaded");
737 DEBUG(0,(__location__ ": %s\n", ldb_errstring(ldb)));
738 return ldb_operr(ldb);
741 ret = dsdb_module_schema_info_update(ac->module, schema,
742 DSDB_FLAG_NEXT_MODULE);
743 if (ret != LDB_SUCCESS) {
744 ldb_asprintf_errstring(ldb,
745 "samldb_schema_info_update: dsdb_module_schema_info_update failed with %s",
754 * Gets back a single-valued attribute by the rules of the SAM triggers when
755 * performing a modify operation
757 static int samldb_get_single_valued_attr(struct samldb_ctx *ac,
758 const char *attr_name,
759 struct ldb_message_element **attr)
761 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
762 struct ldb_message_element *el = NULL;
765 /* We've to walk over all modification entries and consider the
768 * 1.) Add operations aren't allowed and there is returned
769 * "ATTRIBUTE_OR_VALUE_EXISTS".
770 * 2.) Replace operations are allowed but the last one is taken
771 * 3.) Delete operations are also not allowed and there is returned
772 * "UNWILLING_TO_PERFORM".
774 * If "el" is afterwards NULL then that means we've nothing to do here.
776 for (i = 0; i < ac->msg->num_elements; i++) {
777 if (ldb_attr_cmp(ac->msg->elements[i].name, attr_name) != 0) {
781 el = &ac->msg->elements[i];
782 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
783 ldb_asprintf_errstring(ldb,
784 "samldb: attribute '%s' already exists!",
786 return LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS;
788 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
789 ldb_asprintf_errstring(ldb,
790 "samldb: attribute '%s' cannot be deleted!",
792 return LDB_ERR_UNWILLING_TO_PERFORM;
801 * "Objectclass" trigger (MS-SAMR 3.1.1.8.1)
803 * Has to be invoked on "add" and "modify" operations on "user", "computer" and
805 * ac->msg contains the "add"/"modify" message
806 * ac->type contains the object type (main objectclass)
808 static int samldb_objectclass_trigger(struct samldb_ctx *ac)
810 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
811 struct loadparm_context *lp_ctx = talloc_get_type(ldb_get_opaque(ldb,
812 "loadparm"), struct loadparm_context);
813 struct ldb_message_element *el, *el2;
814 enum sid_generator sid_generator;
819 /* make sure that "sAMAccountType" is not specified */
820 el = ldb_msg_find_element(ac->msg, "sAMAccountType");
822 ldb_set_errstring(ldb,
823 "samldb: sAMAccountType must not be specified!");
824 return LDB_ERR_UNWILLING_TO_PERFORM;
827 /* Step 1: objectSid assignment */
829 /* Don't allow the objectSid to be changed. But beside the RELAX
830 * control we have also to guarantee that it can always be set with
831 * SYSTEM permissions. This is needed for the "samba3sam" backend. */
832 sid = samdb_result_dom_sid(ac, ac->msg, "objectSid");
833 if ((sid != NULL) && (!dsdb_module_am_system(ac->module)) &&
834 (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
835 ldb_asprintf_errstring(ldb,
836 "samldb: no SID may be specified in user/group modifications for %s",
837 ldb_dn_get_linearized(ac->msg->dn));
838 return LDB_ERR_UNWILLING_TO_PERFORM;
841 /* but generate a new SID when we do have an add operations */
842 if ((sid == NULL) && (ac->req->operation == LDB_ADD)) {
843 sid_generator = lpcfg_sid_generator(lp_ctx);
844 if (sid_generator == SID_GENERATOR_INTERNAL) {
845 ret = samldb_add_step(ac, samldb_allocate_sid);
846 if (ret != LDB_SUCCESS) return ret;
850 if (strcmp(ac->type, "user") == 0) {
851 /* Step 1.2: Default values */
852 tempstr = talloc_asprintf(ac->msg, "%d", UF_NORMAL_ACCOUNT);
853 if (tempstr == NULL) return ldb_operr(ldb);
854 ret = samdb_find_or_add_attribute(ldb, ac->msg,
855 "userAccountControl", tempstr);
856 if (ret != LDB_SUCCESS) return ret;
857 ret = samdb_find_or_add_attribute(ldb, ac->msg,
859 if (ret != LDB_SUCCESS) return ret;
860 ret = samdb_find_or_add_attribute(ldb, ac->msg,
862 if (ret != LDB_SUCCESS) return ret;
863 ret = samdb_find_or_add_attribute(ldb, ac->msg,
865 if (ret != LDB_SUCCESS) return ret;
866 ret = samdb_find_or_add_attribute(ldb, ac->msg,
867 "badPasswordTime", "0");
868 if (ret != LDB_SUCCESS) return ret;
869 ret = samdb_find_or_add_attribute(ldb, ac->msg,
871 if (ret != LDB_SUCCESS) return ret;
872 ret = samdb_find_or_add_attribute(ldb, ac->msg,
874 if (ret != LDB_SUCCESS) return ret;
875 ret = samdb_find_or_add_attribute(ldb, ac->msg,
877 if (ret != LDB_SUCCESS) return ret;
878 ret = samdb_find_or_add_attribute(ldb, ac->msg,
879 "accountExpires", "9223372036854775807");
880 if (ret != LDB_SUCCESS) return ret;
881 ret = samdb_find_or_add_attribute(ldb, ac->msg,
883 if (ret != LDB_SUCCESS) return ret;
885 el = ldb_msg_find_element(ac->msg, "userAccountControl");
887 uint32_t user_account_control, account_type;
889 /* Step 1.3: "userAccountControl" -> "sAMAccountType" mapping */
890 user_account_control = ldb_msg_find_attr_as_uint(ac->msg,
891 "userAccountControl",
894 /* Temporary duplicate accounts aren't allowed */
895 if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
896 return LDB_ERR_OTHER;
899 account_type = ds_uf2atype(user_account_control);
900 if (account_type == 0) {
901 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
902 return LDB_ERR_UNWILLING_TO_PERFORM;
904 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
907 if (ret != LDB_SUCCESS) {
910 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
911 el2->flags = LDB_FLAG_MOD_REPLACE;
913 if (user_account_control &
914 (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
915 ret = samdb_msg_set_string(ldb, ac->msg, ac->msg,
916 "isCriticalSystemObject",
918 if (ret != LDB_SUCCESS) {
921 el2 = ldb_msg_find_element(ac->msg,
922 "isCriticalSystemObject");
923 el2->flags = LDB_FLAG_MOD_REPLACE;
926 /* Step 1.4: "userAccountControl" -> "primaryGroupID" mapping */
927 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
928 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
929 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
930 "primaryGroupID", rid);
931 if (ret != LDB_SUCCESS) {
934 el2 = ldb_msg_find_element(ac->msg,
936 el2->flags = LDB_FLAG_MOD_REPLACE;
939 /* Step 1.5: Add additional flags when needed */
940 if ((user_account_control & UF_NORMAL_ACCOUNT) &&
941 (ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID) == NULL)) {
942 user_account_control |= UF_ACCOUNTDISABLE;
943 user_account_control |= UF_PASSWD_NOTREQD;
945 ret = samdb_msg_set_uint(ldb, ac->msg, ac->msg,
946 "userAccountControl",
947 user_account_control);
948 if (ret != LDB_SUCCESS) {
954 } else if (strcmp(ac->type, "group") == 0) {
955 /* Step 2.2: Default values */
956 tempstr = talloc_asprintf(ac->msg, "%d",
957 GTYPE_SECURITY_GLOBAL_GROUP);
958 if (tempstr == NULL) return ldb_operr(ldb);
959 ret = samdb_find_or_add_attribute(ldb, ac->msg,
960 "groupType", tempstr);
961 if (ret != LDB_SUCCESS) return ret;
963 /* Step 2.3: "groupType" -> "sAMAccountType" */
964 el = ldb_msg_find_element(ac->msg, "groupType");
966 uint32_t group_type, account_type;
968 group_type = ldb_msg_find_attr_as_uint(ac->msg,
971 /* The creation of builtin groups requires the
973 if (group_type == GTYPE_SECURITY_BUILTIN_LOCAL_GROUP) {
974 if (ldb_request_get_control(ac->req,
975 LDB_CONTROL_RELAX_OID) == NULL) {
976 return LDB_ERR_UNWILLING_TO_PERFORM;
980 account_type = ds_gtype2atype(group_type);
981 if (account_type == 0) {
982 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
983 return LDB_ERR_UNWILLING_TO_PERFORM;
985 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
988 if (ret != LDB_SUCCESS) {
991 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
992 el2->flags = LDB_FLAG_MOD_REPLACE;
1000 * "Primary group ID" trigger (MS-SAMR 3.1.1.8.2)
1002 * Has to be invoked on "add" and "modify" operations on "user" and "computer"
1004 * ac->msg contains the "add"/"modify" message
1007 static int samldb_prim_group_set(struct samldb_ctx *ac)
1009 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1010 struct ldb_dn *prim_group_dn;
1012 struct dom_sid *sid;
1014 rid = ldb_msg_find_attr_as_uint(ac->msg, "primaryGroupID", (uint32_t) -1);
1015 if (rid == (uint32_t) -1) {
1016 /* we aren't affected of any primary group set */
1019 } else if (!ldb_request_get_control(ac->req, LDB_CONTROL_RELAX_OID)) {
1020 ldb_set_errstring(ldb,
1021 "The primary group isn't settable on add operations!");
1022 return LDB_ERR_UNWILLING_TO_PERFORM;
1025 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1027 return ldb_operr(ldb);
1030 prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
1031 ldap_encode_ndr_dom_sid(ac, sid));
1032 if (prim_group_dn == NULL) {
1033 ldb_asprintf_errstring(ldb,
1034 "Failed to find primary group with RID %u!",
1036 return LDB_ERR_UNWILLING_TO_PERFORM;
1042 static int samldb_prim_group_change(struct samldb_ctx *ac)
1044 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1045 const char * attrs[] = { "primaryGroupID", "memberOf", NULL };
1046 struct ldb_result *res;
1047 struct ldb_message_element *el;
1048 struct ldb_message *msg;
1050 struct dom_sid *sid;
1051 struct ldb_dn *prev_prim_group_dn, *new_prim_group_dn;
1054 ret = samldb_get_single_valued_attr(ac, "primaryGroupID", &el);
1055 if (ret != LDB_SUCCESS) {
1059 /* we are not affected */
1063 /* Fetch informations from the existing object */
1065 ret = ldb_search(ldb, ac, &res, ac->msg->dn, LDB_SCOPE_BASE, attrs,
1067 if (ret != LDB_SUCCESS) {
1071 /* Finds out the DN of the old primary group */
1073 rid = ldb_msg_find_attr_as_uint(res->msgs[0], "primaryGroupID", (uint32_t) -1);
1074 if (rid == (uint32_t) -1) {
1075 /* User objects do always have a mandatory "primaryGroupID"
1076 * attribute. If this doesn't exist then the object is of the
1077 * wrong type. This is the exact Windows error code */
1078 return LDB_ERR_OBJECT_CLASS_VIOLATION;
1081 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1083 return ldb_operr(ldb);
1086 prev_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
1087 ldap_encode_ndr_dom_sid(ac, sid));
1088 if (prev_prim_group_dn == NULL) {
1089 return ldb_operr(ldb);
1092 /* Finds out the DN of the new primary group
1093 * Notice: in order to parse the primary group ID correctly we create
1094 * a temporary message here. */
1096 msg = ldb_msg_new(ac->msg);
1098 return ldb_module_oom(ac->module);
1100 ret = ldb_msg_add(msg, el, 0);
1101 if (ret != LDB_SUCCESS) {
1104 rid = ldb_msg_find_attr_as_uint(msg, "primaryGroupID", (uint32_t) -1);
1106 if (rid == (uint32_t) -1) {
1107 /* we aren't affected of any primary group change */
1111 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb), rid);
1113 return ldb_operr(ldb);
1116 new_prim_group_dn = samdb_search_dn(ldb, ac, NULL, "(objectSid=%s)",
1117 ldap_encode_ndr_dom_sid(ac, sid));
1118 if (new_prim_group_dn == NULL) {
1119 /* Here we know if the specified new primary group candidate is
1121 return LDB_ERR_UNWILLING_TO_PERFORM;
1124 /* Only update the "member" attributes when we really do have a change */
1125 if (ldb_dn_compare(new_prim_group_dn, prev_prim_group_dn) != 0) {
1126 /* We need to be already a normal member of the new primary
1127 * group in order to be successful. */
1128 el = samdb_find_attribute(ldb, res->msgs[0], "memberOf",
1129 ldb_dn_get_linearized(new_prim_group_dn));
1131 return LDB_ERR_UNWILLING_TO_PERFORM;
1134 /* Remove the "member" attribute on the new primary group */
1135 msg = ldb_msg_new(ac->msg);
1137 return ldb_module_oom(ac->module);
1139 msg->dn = new_prim_group_dn;
1141 ret = samdb_msg_add_delval(ldb, msg, msg, "member",
1142 ldb_dn_get_linearized(ac->msg->dn));
1143 if (ret != LDB_SUCCESS) {
1147 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1148 if (ret != LDB_SUCCESS) {
1153 /* Add a "member" attribute for the previous primary group */
1154 msg = ldb_msg_new(ac->msg);
1156 return ldb_module_oom(ac->module);
1158 msg->dn = prev_prim_group_dn;
1160 ret = samdb_msg_add_addval(ldb, msg, msg, "member",
1161 ldb_dn_get_linearized(ac->msg->dn));
1162 if (ret != LDB_SUCCESS) {
1166 ret = dsdb_module_modify(ac->module, msg, DSDB_FLAG_NEXT_MODULE);
1167 if (ret != LDB_SUCCESS) {
1178 static int samldb_prim_group_trigger(struct samldb_ctx *ac)
1182 if (ac->req->operation == LDB_ADD) {
1183 ret = samldb_prim_group_set(ac);
1185 ret = samldb_prim_group_change(ac);
1191 static int samldb_member_check(struct samldb_ctx *ac)
1193 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
1194 struct ldb_message_element *el;
1195 struct ldb_dn *member_dn, *group_dn;
1196 uint32_t prim_group_rid;
1197 struct dom_sid *sid;
1201 /* We've to walk over all modification entries and consider the "member"
1203 for (i = 0; i < ac->msg->num_elements; i++) {
1204 if (ldb_attr_cmp(ac->msg->elements[i].name, "member") != 0) {
1208 el = &ac->msg->elements[i];
1209 for (j = 0; j < el->num_values; j++) {
1210 member_dn = ldb_dn_from_ldb_val(ac, ldb,
1212 if (!ldb_dn_validate(member_dn)) {
1213 return ldb_operr(ldb);
1216 /* The "member" attribute can be modified with the
1217 * following restrictions (beside a valid DN):
1219 * - "add" operations can only be performed when the
1220 * member still doesn't exist - if not then return
1221 * ERR_ENTRY_ALREADY_EXISTS (not
1222 * ERR_ATTRIBUTE_OR_VALUE_EXISTS!)
1223 * - "delete" operations can only be performed when the
1224 * member does exist - if not then return
1225 * ERR_UNWILLING_TO_PERFORM (not
1226 * ERR_NO_SUCH_ATTRIBUTE!)
1227 * - primary group check
1229 cnt = samdb_search_count(ldb, ac, ac->msg->dn,
1231 ldb_dn_get_linearized(member_dn));
1233 return ldb_operr(ldb);
1235 if ((cnt > 0) && (LDB_FLAG_MOD_TYPE(el->flags)
1236 == LDB_FLAG_MOD_ADD)) {
1237 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1239 if ((cnt == 0) && LDB_FLAG_MOD_TYPE(el->flags)
1240 == LDB_FLAG_MOD_DELETE) {
1241 return LDB_ERR_UNWILLING_TO_PERFORM;
1244 /* Denies to add "member"s to groups which are primary
1245 * ones for them - in this case return
1246 * ERR_ENTRY_ALREADY_EXISTS. */
1248 prim_group_rid = samdb_search_uint(ldb, ac,
1253 if (prim_group_rid == (uint32_t) -1) {
1254 /* the member hasn't to be a user account ->
1255 * therefore no check needed in this case. */
1259 sid = dom_sid_add_rid(ac, samdb_domain_sid(ldb),
1262 return ldb_operr(ldb);
1265 group_dn = samdb_search_dn(ldb, ac, NULL,
1267 ldap_encode_ndr_dom_sid(ac, sid));
1268 if (group_dn == NULL) {
1269 return ldb_operr(ldb);
1272 if (ldb_dn_compare(group_dn, ac->msg->dn) == 0) {
1273 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1283 static int samldb_add(struct ldb_module *module, struct ldb_request *req)
1285 struct ldb_context *ldb;
1286 struct samldb_ctx *ac;
1289 ldb = ldb_module_get_ctx(module);
1290 ldb_debug(ldb, LDB_DEBUG_TRACE, "samldb_add\n");
1292 /* do not manipulate our control entries */
1293 if (ldb_dn_is_special(req->op.add.message->dn)) {
1294 return ldb_next_request(module, req);
1297 ac = samldb_ctx_init(module, req);
1299 return ldb_operr(ldb);
1302 /* build the new msg */
1303 ac->msg = ldb_msg_copy_shallow(ac, req->op.add.message);
1304 if (ac->msg == NULL) {
1306 ldb_debug(ldb, LDB_DEBUG_FATAL,
1307 "samldb_add: ldb_msg_copy_shallow failed!\n");
1308 return ldb_operr(ldb);
1311 if (samdb_find_attribute(ldb, ac->msg,
1312 "objectclass", "user") != NULL) {
1315 ret = samldb_prim_group_trigger(ac);
1316 if (ret != LDB_SUCCESS) {
1320 ret = samldb_objectclass_trigger(ac);
1321 if (ret != LDB_SUCCESS) {
1325 return samldb_fill_object(ac);
1328 if (samdb_find_attribute(ldb, ac->msg,
1329 "objectclass", "group") != NULL) {
1332 ret = samldb_objectclass_trigger(ac);
1333 if (ret != LDB_SUCCESS) {
1337 return samldb_fill_object(ac);
1340 /* perhaps a foreignSecurityPrincipal? */
1341 if (samdb_find_attribute(ldb, ac->msg,
1343 "foreignSecurityPrincipal") != NULL) {
1344 return samldb_fill_foreignSecurityPrincipal_object(ac);
1347 if (samdb_find_attribute(ldb, ac->msg,
1348 "objectclass", "classSchema") != NULL) {
1349 ret = samldb_schema_info_update(ac);
1350 if (ret != LDB_SUCCESS) {
1355 ac->type = "classSchema";
1356 return samldb_fill_object(ac);
1359 if (samdb_find_attribute(ldb, ac->msg,
1360 "objectclass", "attributeSchema") != NULL) {
1361 ret = samldb_schema_info_update(ac);
1362 if (ret != LDB_SUCCESS) {
1367 ac->type = "attributeSchema";
1368 return samldb_fill_object(ac);
1373 /* nothing matched, go on */
1374 return ldb_next_request(module, req);
1378 static int samldb_modify(struct ldb_module *module, struct ldb_request *req)
1380 struct ldb_context *ldb;
1381 struct samldb_ctx *ac;
1382 struct ldb_message_element *el, *el2;
1383 bool modified = false;
1385 uint32_t account_type;
1387 if (ldb_dn_is_special(req->op.mod.message->dn)) {
1388 /* do not manipulate our control entries */
1389 return ldb_next_request(module, req);
1392 ldb = ldb_module_get_ctx(module);
1394 /* make sure that "sAMAccountType" is not specified */
1395 el = ldb_msg_find_element(req->op.mod.message, "sAMAccountType");
1397 ldb_set_errstring(ldb,
1398 "samldb: sAMAccountType must not be specified!");
1399 return LDB_ERR_UNWILLING_TO_PERFORM;
1401 /* make sure that "isCriticalSystemObject" is not specified */
1402 el = ldb_msg_find_element(req->op.mod.message, "isCriticalSystemObject");
1404 if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID) == NULL) {
1405 ldb_set_errstring(ldb,
1406 "samldb: isCriticalSystemObject must not be specified!");
1407 return LDB_ERR_UNWILLING_TO_PERFORM;
1411 /* msDS-IntId is not allowed to be modified
1412 * except when modification comes from replication */
1413 if (ldb_msg_find_element(req->op.mod.message, "msDS-IntId")) {
1414 if (!ldb_request_get_control(req,
1415 DSDB_CONTROL_REPLICATED_UPDATE_OID)) {
1416 return LDB_ERR_CONSTRAINT_VIOLATION;
1420 ac = samldb_ctx_init(module, req);
1422 return ldb_operr(ldb);
1425 /* build the new msg */
1426 ac->msg = ldb_msg_copy_shallow(ac, req->op.mod.message);
1427 if (ac->msg == NULL) {
1429 ldb_debug(ldb, LDB_DEBUG_FATAL,
1430 "samldb_modify: ldb_msg_copy_shallow failed!\n");
1431 return ldb_operr(ldb);
1434 el = ldb_msg_find_element(ac->msg, "groupType");
1435 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE)
1436 && el->num_values == 1) {
1437 uint32_t group_type, old_group_type;
1441 group_type = ldb_msg_find_attr_as_uint(ac->msg, "groupType", 0);
1442 old_group_type = samdb_search_uint(ldb, ac, 0, ac->msg->dn,
1444 if (old_group_type == 0) {
1445 return ldb_operr(ldb);
1448 /* Group type switching isn't so easy as it seems: We can only
1449 * change in this directions: global <-> universal <-> local
1450 * On each step also the group type itself
1451 * (security/distribution) is variable. */
1453 switch (group_type) {
1454 case GTYPE_SECURITY_GLOBAL_GROUP:
1455 case GTYPE_DISTRIBUTION_GLOBAL_GROUP:
1456 /* change to "universal" allowed */
1457 if ((old_group_type == GTYPE_SECURITY_DOMAIN_LOCAL_GROUP) ||
1458 (old_group_type == GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP)) {
1459 return LDB_ERR_UNWILLING_TO_PERFORM;
1463 case GTYPE_SECURITY_UNIVERSAL_GROUP:
1464 case GTYPE_DISTRIBUTION_UNIVERSAL_GROUP:
1465 /* each change allowed */
1468 case GTYPE_SECURITY_DOMAIN_LOCAL_GROUP:
1469 case GTYPE_DISTRIBUTION_DOMAIN_LOCAL_GROUP:
1470 /* change to "universal" allowed */
1471 if ((old_group_type == GTYPE_SECURITY_GLOBAL_GROUP) ||
1472 (old_group_type == GTYPE_DISTRIBUTION_GLOBAL_GROUP)) {
1473 return LDB_ERR_UNWILLING_TO_PERFORM;
1477 case GTYPE_SECURITY_BUILTIN_LOCAL_GROUP:
1479 /* we don't allow this "groupType" values */
1480 return LDB_ERR_UNWILLING_TO_PERFORM;
1484 account_type = ds_gtype2atype(group_type);
1485 if (account_type == 0) {
1486 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1487 return LDB_ERR_UNWILLING_TO_PERFORM;
1489 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1492 if (ret != LDB_SUCCESS) {
1495 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
1496 el2->flags = LDB_FLAG_MOD_REPLACE;
1498 el = ldb_msg_find_element(ac->msg, "groupType");
1499 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
1500 return LDB_ERR_UNWILLING_TO_PERFORM;
1503 el = ldb_msg_find_element(ac->msg, "primaryGroupID");
1505 ret = samldb_prim_group_change(ac);
1506 if (ret != LDB_SUCCESS) {
1511 el = ldb_msg_find_element(ac->msg, "userAccountControl");
1512 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE)
1513 && el->num_values == 1) {
1514 uint32_t user_account_control;
1518 user_account_control = ldb_msg_find_attr_as_uint(ac->msg,
1519 "userAccountControl",
1522 /* Temporary duplicate accounts aren't allowed */
1523 if ((user_account_control & UF_TEMP_DUPLICATE_ACCOUNT) != 0) {
1524 return LDB_ERR_OTHER;
1527 account_type = ds_uf2atype(user_account_control);
1528 if (account_type == 0) {
1529 ldb_set_errstring(ldb, "samldb: Unrecognized account type!");
1530 return LDB_ERR_UNWILLING_TO_PERFORM;
1532 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1535 if (ret != LDB_SUCCESS) {
1538 el2 = ldb_msg_find_element(ac->msg, "sAMAccountType");
1539 el2->flags = LDB_FLAG_MOD_REPLACE;
1541 if (user_account_control & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
1542 ret = samdb_msg_add_string(ldb, ac->msg, ac->msg,
1543 "isCriticalSystemObject",
1545 if (ret != LDB_SUCCESS) {
1548 el2 = ldb_msg_find_element(ac->msg,
1549 "isCriticalSystemObject");
1550 el2->flags = LDB_FLAG_MOD_REPLACE;
1553 if (!ldb_msg_find_element(ac->msg, "primaryGroupID")) {
1554 uint32_t rid = ds_uf2prim_group_rid(user_account_control);
1556 ret = samdb_msg_add_uint(ldb, ac->msg, ac->msg,
1557 "primaryGroupID", rid);
1558 if (ret != LDB_SUCCESS) {
1561 el2 = ldb_msg_find_element(ac->msg,
1563 el2->flags = LDB_FLAG_MOD_REPLACE;
1566 el = ldb_msg_find_element(ac->msg, "userAccountControl");
1567 if (el && (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE)) {
1568 return LDB_ERR_UNWILLING_TO_PERFORM;
1571 el = ldb_msg_find_element(ac->msg, "member");
1573 ret = samldb_member_check(ac);
1574 if (ret != LDB_SUCCESS) {
1580 struct ldb_request *child_req;
1582 /* Now perform the real modifications as a child request */
1583 ret = ldb_build_mod_req(&child_req, ldb, ac,
1586 req, dsdb_next_callback,
1588 LDB_REQ_SET_LOCATION(child_req);
1589 if (ret != LDB_SUCCESS) {
1593 return ldb_next_request(module, child_req);
1598 /* no change which interests us, go on */
1599 return ldb_next_request(module, req);
1604 static int samldb_prim_group_users_check(struct samldb_ctx *ac)
1606 struct ldb_context *ldb;
1607 struct dom_sid *sid;
1612 ldb = ldb_module_get_ctx(ac->module);
1614 /* Finds out the SID/RID of the SAM object */
1615 sid = samdb_search_dom_sid(ldb, ac, ac->req->op.del.dn, "objectSid",
1618 /* No SID - it might not be a SAM object - therefore ok */
1621 status = dom_sid_split_rid(ac, sid, NULL, &rid);
1622 if (!NT_STATUS_IS_OK(status)) {
1623 return ldb_operr(ldb);
1626 /* Special object (security principal?) */
1630 /* Deny delete requests from groups which are primary ones */
1631 count = samdb_search_count(ldb, ac, NULL,
1632 "(&(primaryGroupID=%u)(objectClass=user))",
1635 return ldb_operr(ldb);
1638 return LDB_ERR_ENTRY_ALREADY_EXISTS;
1644 static int samldb_delete(struct ldb_module *module, struct ldb_request *req)
1646 struct samldb_ctx *ac;
1649 if (ldb_dn_is_special(req->op.del.dn)) {
1650 /* do not manipulate our control entries */
1651 return ldb_next_request(module, req);
1654 ac = samldb_ctx_init(module, req);
1656 return ldb_operr(ldb_module_get_ctx(module));
1659 ret = samldb_prim_group_users_check(ac);
1660 if (ret != LDB_SUCCESS) {
1666 return ldb_next_request(module, req);
1671 static int samldb_extended_allocate_rid_pool(struct ldb_module *module, struct ldb_request *req)
1673 struct ldb_context *ldb = ldb_module_get_ctx(module);
1674 struct dsdb_fsmo_extended_op *exop;
1677 exop = talloc_get_type(req->op.extended.data,
1678 struct dsdb_fsmo_extended_op);
1680 ldb_set_errstring(ldb,
1681 "samldb_extended_allocate_rid_pool: invalid extended data");
1682 return LDB_ERR_PROTOCOL_ERROR;
1685 ret = ridalloc_allocate_rid_pool_fsmo(module, exop);
1686 if (ret != LDB_SUCCESS) {
1690 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
1693 static int samldb_extended(struct ldb_module *module, struct ldb_request *req)
1695 if (strcmp(req->op.extended.oid, DSDB_EXTENDED_ALLOCATE_RID_POOL) == 0) {
1696 return samldb_extended_allocate_rid_pool(module, req);
1699 return ldb_next_request(module, req);
1703 _PUBLIC_ const struct ldb_module_ops ldb_samldb_module_ops = {
1706 .modify = samldb_modify,
1707 .del = samldb_delete,
1708 .extended = samldb_extended