4 Copyright (C) Simo Sorce 2006-2008
5 Copyright (C) Nadezhda Ivanova 2009
6 Copyright (C) Anatoliy Atanasov 2009
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 ACL module
27 * Description: Module that performs authorisation access checks based on the
28 * account's security context and the DACL of the object being polled.
29 * Only DACL checks implemented at this point
31 * Authors: Nadezhda Ivanova, Anatoliy Atanasov
35 #include "ldb_module.h"
36 #include "auth/auth.h"
37 #include "libcli/security/security.h"
38 #include "dsdb/samdb/samdb.h"
39 #include "librpc/gen_ndr/ndr_security.h"
40 #include "param/param.h"
41 #include "dsdb/samdb/ldb_modules/util.h"
42 #include "lib/util/tsort.h"
43 #include "system/kerberos.h"
44 #include "auth/kerberos/kerberos.h"
46 struct extended_access_check_attribute {
48 const uint32_t requires_rights;
53 const char **password_attrs;
54 void *cached_schema_ptr;
55 uint64_t cached_schema_metadata_usn;
56 uint64_t cached_schema_loaded_usn;
57 const char **confidential_attrs;
58 bool userPassword_support;
62 struct ldb_module *module;
63 struct ldb_request *req;
65 bool am_administrator;
67 bool constructed_attrs;
68 bool allowedAttributes;
69 bool allowedAttributesEffective;
70 bool allowedChildClasses;
71 bool allowedChildClassesEffective;
72 bool sDRightsEffective;
74 const char * const *attrs;
75 struct dsdb_schema *schema;
78 static int acl_module_init(struct ldb_module *module)
80 struct ldb_context *ldb;
81 struct acl_private *data;
85 static const char * const attrs[] = { "passwordAttribute", NULL };
86 static const char * const secret_attrs[] = {
87 DSDB_SECRET_ATTRIBUTES
89 struct ldb_result *res;
90 struct ldb_message *msg;
91 struct ldb_message_element *password_attributes;
93 ldb = ldb_module_get_ctx(module);
95 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
96 if (ret != LDB_SUCCESS) {
97 ldb_debug(ldb, LDB_DEBUG_ERROR,
98 "acl_module_init: Unable to register control with rootdse!\n");
99 return ldb_operr(ldb);
102 data = talloc_zero(module, struct acl_private);
107 data->acl_search = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"),
108 NULL, "acl", "search", true);
109 ldb_module_set_private(module, data);
111 data->userPassword_support = dsdb_user_password_support(module, module, NULL);
113 mem_ctx = talloc_new(module);
118 ret = dsdb_module_search_dn(module, mem_ctx, &res,
119 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
121 DSDB_FLAG_NEXT_MODULE |
124 if (ret != LDB_SUCCESS) {
127 if (res->count == 0) {
131 if (res->count > 1) {
132 talloc_free(mem_ctx);
133 return LDB_ERR_CONSTRAINT_VIOLATION;
138 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
139 if (!password_attributes) {
142 data->password_attrs = talloc_array(data, const char *,
143 password_attributes->num_values +
144 ARRAY_SIZE(secret_attrs) + 1);
145 if (!data->password_attrs) {
146 talloc_free(mem_ctx);
151 for (i=0; i < password_attributes->num_values; i++) {
152 data->password_attrs[n] = (const char *)password_attributes->values[i].data;
153 talloc_steal(data->password_attrs, password_attributes->values[i].data);
157 for (i=0; i < ARRAY_SIZE(secret_attrs); i++) {
160 for (j=0; j < n; j++) {
161 if (strcasecmp(data->password_attrs[j], secret_attrs[i]) == 0) {
171 data->password_attrs[n] = talloc_strdup(data->password_attrs,
173 if (data->password_attrs[n] == NULL) {
174 talloc_free(mem_ctx);
179 data->password_attrs[n] = NULL;
182 talloc_free(mem_ctx);
183 return ldb_next_init(module);
186 static int acl_allowedAttributes(struct ldb_module *module,
187 const struct dsdb_schema *schema,
188 struct ldb_message *sd_msg,
189 struct ldb_message *msg,
190 struct acl_context *ac)
192 struct ldb_message_element *oc_el;
193 struct ldb_context *ldb = ldb_module_get_ctx(module);
195 const char **attr_list;
197 const struct dsdb_class *objectclass;
199 /* If we don't have a schema yet, we can't do anything... */
200 if (schema == NULL) {
201 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
202 return LDB_ERR_OPERATIONS_ERROR;
205 /* Must remove any existing attribute */
206 if (ac->allowedAttributes) {
207 ldb_msg_remove_attr(msg, "allowedAttributes");
210 mem_ctx = talloc_new(msg);
215 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
216 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
218 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
219 talloc_free(mem_ctx);
220 return LDB_ERR_OPERATIONS_ERROR;
224 * Get the top-most structural object class for the ACL check
226 objectclass = dsdb_get_last_structural_class(ac->schema,
228 if (objectclass == NULL) {
229 ldb_asprintf_errstring(ldb, "acl_read: Failed to find a structural class for %s",
230 ldb_dn_get_linearized(sd_msg->dn));
231 talloc_free(mem_ctx);
232 return LDB_ERR_OPERATIONS_ERROR;
235 if (ac->allowedAttributes) {
236 for (i=0; attr_list && attr_list[i]; i++) {
237 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
240 if (ac->allowedAttributesEffective) {
241 struct security_descriptor *sd;
242 struct dom_sid *sid = NULL;
243 struct ldb_control *as_system = ldb_request_get_control(ac->req,
244 LDB_CONTROL_AS_SYSTEM_OID);
246 if (as_system != NULL) {
247 as_system->critical = 0;
250 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
251 if (ac->am_system || as_system) {
252 for (i=0; attr_list && attr_list[i]; i++) {
253 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
258 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), mem_ctx, sd_msg, &sd);
260 if (ret != LDB_SUCCESS) {
264 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
265 for (i=0; attr_list && attr_list[i]; i++) {
266 const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
269 return ldb_operr(ldb);
271 /* remove constructed attributes */
272 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
274 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
277 ret = acl_check_access_on_attribute(module,
284 if (ret == LDB_SUCCESS) {
285 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
292 static int acl_childClasses(struct ldb_module *module,
293 const struct dsdb_schema *schema,
294 struct ldb_message *sd_msg,
295 struct ldb_message *msg,
296 const char *attrName)
298 struct ldb_message_element *oc_el;
299 struct ldb_message_element *allowedClasses;
300 const struct dsdb_class *sclass;
304 /* If we don't have a schema yet, we can't do anything... */
305 if (schema == NULL) {
306 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
307 return LDB_ERR_OPERATIONS_ERROR;
310 /* Must remove any existing attribute, or else confusion reins */
311 ldb_msg_remove_attr(msg, attrName);
312 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
313 if (ret != LDB_SUCCESS) {
317 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
319 for (i=0; oc_el && i < oc_el->num_values; i++) {
320 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
322 /* We don't know this class? what is going on? */
326 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
327 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
330 if (allowedClasses->num_values > 1) {
331 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
332 for (i=1 ; i < allowedClasses->num_values; i++) {
333 struct ldb_val *val1 = &allowedClasses->values[i-1];
334 struct ldb_val *val2 = &allowedClasses->values[i];
335 if (data_blob_cmp(val1, val2) == 0) {
336 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
337 allowedClasses->num_values--;
346 static int acl_childClassesEffective(struct ldb_module *module,
347 const struct dsdb_schema *schema,
348 struct ldb_message *sd_msg,
349 struct ldb_message *msg,
350 struct acl_context *ac)
352 struct ldb_message_element *oc_el;
353 struct ldb_message_element *allowedClasses = NULL;
354 const struct dsdb_class *sclass;
355 struct security_descriptor *sd;
356 struct ldb_control *as_system = ldb_request_get_control(ac->req,
357 LDB_CONTROL_AS_SYSTEM_OID);
358 struct dom_sid *sid = NULL;
362 if (as_system != NULL) {
363 as_system->critical = 0;
366 if (ac->am_system || as_system) {
367 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
370 /* If we don't have a schema yet, we can't do anything... */
371 if (schema == NULL) {
372 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
373 return LDB_ERR_OPERATIONS_ERROR;
376 /* Must remove any existing attribute, or else confusion reins */
377 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
379 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
380 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
381 if (ret != LDB_SUCCESS) {
385 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
386 for (i=0; oc_el && i < oc_el->num_values; i++) {
387 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
389 /* We don't know this class? what is going on? */
393 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
394 const struct dsdb_class *sc;
396 sc = dsdb_class_by_lDAPDisplayName(schema,
397 sclass->possibleInferiors[j]);
399 /* We don't know this class? what is going on? */
403 ret = acl_check_access_on_objectclass(module, ac,
405 SEC_ADS_CREATE_CHILD,
407 if (ret == LDB_SUCCESS) {
408 ldb_msg_add_string(msg, "allowedChildClassesEffective",
409 sclass->possibleInferiors[j]);
413 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
414 if (!allowedClasses) {
418 if (allowedClasses->num_values > 1) {
419 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
420 for (i=1 ; i < allowedClasses->num_values; i++) {
421 struct ldb_val *val1 = &allowedClasses->values[i-1];
422 struct ldb_val *val2 = &allowedClasses->values[i];
423 if (data_blob_cmp(val1, val2) == 0) {
424 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
425 allowedClasses->num_values--;
433 static int acl_sDRightsEffective(struct ldb_module *module,
434 struct ldb_message *sd_msg,
435 struct ldb_message *msg,
436 struct acl_context *ac)
438 struct ldb_context *ldb = ldb_module_get_ctx(module);
439 struct ldb_message_element *rightsEffective;
441 struct security_descriptor *sd;
442 struct ldb_control *as_system = ldb_request_get_control(ac->req,
443 LDB_CONTROL_AS_SYSTEM_OID);
444 struct dom_sid *sid = NULL;
447 if (as_system != NULL) {
448 as_system->critical = 0;
451 /* Must remove any existing attribute, or else confusion reins */
452 ldb_msg_remove_attr(msg, "sDRightsEffective");
453 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
454 if (ret != LDB_SUCCESS) {
457 if (ac->am_system || as_system) {
458 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
460 const struct dsdb_class *objectclass;
461 const struct dsdb_attribute *attr;
463 objectclass = dsdb_get_structural_oc_from_msg(ac->schema, sd_msg);
464 if (objectclass == NULL) {
465 return ldb_operr(ldb);
468 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
469 "nTSecurityDescriptor");
471 return ldb_operr(ldb);
474 /* Get the security descriptor from the message */
475 ret = dsdb_get_sd_from_ldb_message(ldb, msg, sd_msg, &sd);
476 if (ret != LDB_SUCCESS) {
479 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
480 ret = acl_check_access_on_attribute(module,
487 if (ret == LDB_SUCCESS) {
488 flags |= SECINFO_OWNER | SECINFO_GROUP;
490 ret = acl_check_access_on_attribute(module,
497 if (ret == LDB_SUCCESS) {
498 flags |= SECINFO_DACL;
500 ret = acl_check_access_on_attribute(module,
504 SEC_FLAG_SYSTEM_SECURITY,
507 if (ret == LDB_SUCCESS) {
508 flags |= SECINFO_SACL;
511 return samdb_msg_add_uint(ldb_module_get_ctx(module), msg, msg,
512 "sDRightsEffective", flags);
515 static int acl_validate_spn_value(TALLOC_CTX *mem_ctx,
516 struct ldb_context *ldb,
517 const char *spn_value,
518 uint32_t userAccountControl,
519 const char *samAccountName,
520 const char *dnsHostName,
521 const char *netbios_name,
522 const char *ntds_guid)
525 krb5_context krb_ctx;
526 krb5_error_code kerr;
527 krb5_principal principal;
531 const char *forest_name = samdb_forest_name(ldb, mem_ctx);
532 const char *base_domain = samdb_default_domain_name(ldb, mem_ctx);
533 struct loadparm_context *lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
534 struct loadparm_context);
535 bool is_dc = (userAccountControl & UF_SERVER_TRUST_ACCOUNT) ||
536 (userAccountControl & UF_PARTIAL_SECRETS_ACCOUNT);
538 if (strcasecmp_m(spn_value, samAccountName) == 0) {
539 /* MacOS X sets this value, and setting an SPN of your
540 * own samAccountName is both pointless and safe */
544 kerr = smb_krb5_init_context_basic(mem_ctx,
548 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
549 "Could not initialize kerberos context.");
552 ret = krb5_parse_name(krb_ctx, spn_value, &principal);
554 krb5_free_context(krb_ctx);
555 return LDB_ERR_CONSTRAINT_VIOLATION;
558 princ_size = krb5_princ_size(krb_ctx, principal);
559 if (princ_size < 2) {
560 DBG_WARNING("princ_size=%d\n", princ_size);
564 instanceName = smb_krb5_principal_get_comp_string(mem_ctx, krb_ctx,
566 serviceType = smb_krb5_principal_get_comp_string(mem_ctx, krb_ctx,
568 if (krb5_princ_size(krb_ctx, principal) == 3) {
569 serviceName = smb_krb5_principal_get_comp_string(mem_ctx, krb_ctx,
577 DBG_WARNING("is_dc=false, serviceName=%s,"
578 "serviceType=%s\n", serviceName,
582 if (strcasecmp(serviceType, "ldap") == 0) {
583 if (strcasecmp(serviceName, netbios_name) != 0 &&
584 strcasecmp(serviceName, forest_name) != 0) {
585 DBG_WARNING("serviceName=%s\n", serviceName);
589 } else if (strcasecmp(serviceType, "gc") == 0) {
590 if (strcasecmp(serviceName, forest_name) != 0) {
591 DBG_WARNING("serviceName=%s\n", serviceName);
595 if (strcasecmp(serviceName, base_domain) != 0 &&
596 strcasecmp(serviceName, netbios_name) != 0) {
597 DBG_WARNING("serviceType=%s, "
599 serviceType, serviceName);
604 /* instanceName can be samAccountName without $ or dnsHostName
605 * or "ntds_guid._msdcs.forest_domain for DC objects */
606 if (strlen(instanceName) == (strlen(samAccountName) - 1)
607 && strncasecmp(instanceName, samAccountName,
608 strlen(samAccountName) - 1) == 0) {
611 if ((dnsHostName != NULL) &&
612 (strcasecmp(instanceName, dnsHostName) == 0)) {
616 const char *guid_str;
617 guid_str = talloc_asprintf(mem_ctx,"%s._msdcs.%s",
620 if (strcasecmp(instanceName, guid_str) == 0) {
626 krb5_free_principal(krb_ctx, principal);
627 krb5_free_context(krb_ctx);
628 ldb_debug_set(ldb, LDB_DEBUG_WARNING,
629 "acl: spn validation failed for "
630 "spn[%s] uac[0x%x] account[%s] hostname[%s] "
631 "nbname[%s] ntds[%s] forest[%s] domain[%s]\n",
632 spn_value, (unsigned)userAccountControl,
633 samAccountName, dnsHostName,
634 netbios_name, ntds_guid,
635 forest_name, base_domain);
636 return LDB_ERR_CONSTRAINT_VIOLATION;
639 krb5_free_principal(krb_ctx, principal);
640 krb5_free_context(krb_ctx);
644 static int acl_check_spn(TALLOC_CTX *mem_ctx,
645 struct ldb_module *module,
646 struct ldb_request *req,
647 struct security_descriptor *sd,
649 const struct dsdb_attribute *attr,
650 const struct dsdb_class *objectclass)
654 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
655 struct ldb_context *ldb = ldb_module_get_ctx(module);
656 struct ldb_result *acl_res;
657 struct ldb_result *netbios_res;
658 struct ldb_message_element *el;
659 struct ldb_dn *partitions_dn = samdb_partitions_dn(ldb, tmp_ctx);
660 uint32_t userAccountControl;
661 const char *samAccountName;
662 const char *dnsHostName;
663 const char *netbios_name;
665 char *ntds_guid = NULL;
667 static const char *acl_attrs[] = {
670 "userAccountControl",
673 static const char *netbios_attrs[] = {
678 /* if we have wp, we can do whatever we like */
679 if (acl_check_access_on_attribute(module,
684 attr, objectclass) == LDB_SUCCESS) {
685 talloc_free(tmp_ctx);
689 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
690 GUID_DRS_VALIDATE_SPN,
694 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
695 dsdb_acl_debug(sd, acl_user_token(module),
696 req->op.mod.message->dn,
699 talloc_free(tmp_ctx);
703 ret = dsdb_module_search_dn(module, tmp_ctx,
704 &acl_res, req->op.mod.message->dn,
706 DSDB_FLAG_NEXT_MODULE |
707 DSDB_FLAG_AS_SYSTEM |
708 DSDB_SEARCH_SHOW_RECYCLED,
710 if (ret != LDB_SUCCESS) {
711 talloc_free(tmp_ctx);
715 userAccountControl = ldb_msg_find_attr_as_uint(acl_res->msgs[0], "userAccountControl", 0);
716 dnsHostName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "dnsHostName", NULL);
717 samAccountName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "samAccountName", NULL);
719 ret = dsdb_module_search(module, tmp_ctx,
720 &netbios_res, partitions_dn,
723 DSDB_FLAG_NEXT_MODULE |
727 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)));
729 netbios_name = ldb_msg_find_attr_as_string(netbios_res->msgs[0], "nETBIOSName", NULL);
731 el = ldb_msg_find_element(req->op.mod.message, "servicePrincipalName");
733 talloc_free(tmp_ctx);
734 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
735 "Error finding element for servicePrincipalName.");
738 /* NTDSDSA objectGuid of object we are checking SPN for */
739 if (userAccountControl & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
740 ret = dsdb_module_find_ntdsguid_for_computer(module, tmp_ctx,
741 req->op.mod.message->dn, &ntds, req);
742 if (ret != LDB_SUCCESS) {
743 ldb_asprintf_errstring(ldb, "Failed to find NTDSDSA objectGuid for %s: %s",
744 ldb_dn_get_linearized(req->op.mod.message->dn),
746 talloc_free(tmp_ctx);
747 return LDB_ERR_OPERATIONS_ERROR;
749 ntds_guid = GUID_string(tmp_ctx, &ntds);
752 for (i=0; i < el->num_values; i++) {
753 ret = acl_validate_spn_value(tmp_ctx,
755 (char *)el->values[i].data,
761 if (ret != LDB_SUCCESS) {
762 talloc_free(tmp_ctx);
766 talloc_free(tmp_ctx);
770 static int acl_add(struct ldb_module *module, struct ldb_request *req)
773 struct ldb_dn *parent;
774 struct ldb_context *ldb;
775 const struct dsdb_schema *schema;
776 const struct dsdb_class *objectclass;
777 struct ldb_control *as_system;
778 struct ldb_message_element *el;
779 unsigned int instanceType = 0;
781 if (ldb_dn_is_special(req->op.add.message->dn)) {
782 return ldb_next_request(module, req);
785 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
786 if (as_system != NULL) {
787 as_system->critical = 0;
790 if (dsdb_module_am_system(module) || as_system) {
791 return ldb_next_request(module, req);
794 ldb = ldb_module_get_ctx(module);
796 parent = ldb_dn_get_parent(req, req->op.add.message->dn);
797 if (parent == NULL) {
801 schema = dsdb_get_schema(ldb, req);
803 return ldb_operr(ldb);
806 objectclass = dsdb_get_structural_oc_from_msg(schema, req->op.add.message);
808 ldb_asprintf_errstring(ldb_module_get_ctx(module),
809 "acl: unable to find or validate structural objectClass on %s\n",
810 ldb_dn_get_linearized(req->op.add.message->dn));
811 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
814 el = ldb_msg_find_element(req->op.add.message, "instanceType");
815 if ((el != NULL) && (el->num_values != 1)) {
816 ldb_set_errstring(ldb, "acl: the 'instanceType' attribute is single-valued!");
817 return LDB_ERR_UNWILLING_TO_PERFORM;
820 instanceType = ldb_msg_find_attr_as_uint(req->op.add.message,
822 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
823 static const char *no_attrs[] = { NULL };
824 struct ldb_result *partition_res;
825 struct ldb_dn *partitions_dn;
827 partitions_dn = samdb_partitions_dn(ldb, req);
828 if (!partitions_dn) {
829 ldb_set_errstring(ldb, "acl: CN=partitions dn could not be generated!");
830 return LDB_ERR_UNWILLING_TO_PERFORM;
833 ret = dsdb_module_search(module, req, &partition_res,
834 partitions_dn, LDB_SCOPE_ONELEVEL,
836 DSDB_FLAG_NEXT_MODULE |
837 DSDB_FLAG_AS_SYSTEM |
838 DSDB_SEARCH_ONE_ONLY |
839 DSDB_SEARCH_SHOW_RECYCLED,
841 "(&(nCName=%s)(objectClass=crossRef))",
842 ldb_dn_get_linearized(req->op.add.message->dn));
844 if (ret == LDB_SUCCESS) {
845 /* Check that we can write to the crossRef object MS-ADTS 3.1.1.5.2.8.2 */
846 ret = dsdb_module_check_access_on_dn(module, req, partition_res->msgs[0]->dn,
848 &objectclass->schemaIDGUID, req);
849 if (ret != LDB_SUCCESS) {
850 ldb_asprintf_errstring(ldb_module_get_ctx(module),
851 "acl: ACL check failed on crossRef object %s: %s\n",
852 ldb_dn_get_linearized(partition_res->msgs[0]->dn),
858 * TODO: Remaining checks, like if we are
859 * the naming master etc need to be handled
860 * in the instanceType module
862 return ldb_next_request(module, req);
865 /* Check that we can create a crossRef object MS-ADTS 3.1.1.5.2.8.2 */
866 ret = dsdb_module_check_access_on_dn(module, req, partitions_dn,
867 SEC_ADS_CREATE_CHILD,
868 &objectclass->schemaIDGUID, req);
869 if (ret == LDB_ERR_NO_SUCH_OBJECT &&
870 ldb_request_get_control(req, LDB_CONTROL_RELAX_OID))
872 /* Allow provision bootstrap */
875 if (ret != LDB_SUCCESS) {
876 ldb_asprintf_errstring(ldb_module_get_ctx(module),
877 "acl: ACL check failed on CN=Partitions crossRef container %s: %s\n",
878 ldb_dn_get_linearized(partitions_dn), ldb_errstring(ldb));
883 * TODO: Remaining checks, like if we are the naming
884 * master and adding the crossRef object need to be
885 * handled in the instanceType module
887 return ldb_next_request(module, req);
890 ret = dsdb_module_check_access_on_dn(module, req, parent,
891 SEC_ADS_CREATE_CHILD,
892 &objectclass->schemaIDGUID, req);
893 if (ret != LDB_SUCCESS) {
894 ldb_asprintf_errstring(ldb_module_get_ctx(module),
895 "acl: unable to get access to %s\n",
896 ldb_dn_get_linearized(req->op.add.message->dn));
899 return ldb_next_request(module, req);
902 /* ckecks if modifications are allowed on "Member" attribute */
903 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
904 struct ldb_module *module,
905 struct ldb_request *req,
906 struct security_descriptor *sd,
908 const struct dsdb_attribute *attr,
909 const struct dsdb_class *objectclass)
913 struct ldb_context *ldb = ldb_module_get_ctx(module);
914 struct ldb_dn *user_dn;
915 struct ldb_message_element *member_el;
916 /* if we have wp, we can do whatever we like */
917 if (acl_check_access_on_attribute(module,
922 attr, objectclass) == LDB_SUCCESS) {
925 /* if we are adding/deleting ourselves, check for self membership */
926 ret = dsdb_find_dn_by_sid(ldb, mem_ctx,
927 &acl_user_token(module)->sids[PRIMARY_USER_SID_INDEX],
929 if (ret != LDB_SUCCESS) {
932 member_el = ldb_msg_find_element(req->op.mod.message, "member");
934 return ldb_operr(ldb);
936 /* user can only remove oneself */
937 if (member_el->num_values == 0) {
938 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
940 for (i = 0; i < member_el->num_values; i++) {
941 if (strcasecmp((const char *)member_el->values[i].data,
942 ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
943 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
946 ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
947 GUID_DRS_SELF_MEMBERSHIP,
950 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
951 dsdb_acl_debug(sd, acl_user_token(module),
952 req->op.mod.message->dn,
959 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
960 struct ldb_module *module,
961 struct ldb_request *req,
962 struct security_descriptor *sd,
964 const struct dsdb_class *objectclass,
967 int ret = LDB_SUCCESS;
968 unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
969 struct ldb_message_element *el;
970 struct ldb_message *msg;
971 const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
972 "unicodePwd", "dBCSPwd", NULL }, **l;
973 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
975 msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
977 return ldb_module_oom(module);
979 for (l = passwordAttrs; *l != NULL; l++) {
980 if ((!userPassword) && (ldb_attr_cmp(*l, "userPassword") == 0)) {
984 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
985 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
988 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
991 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
994 ldb_msg_remove_element(msg, el);
998 /* single deletes will be handled by the "password_hash" LDB module
999 * later in the stack, so we let it though here */
1000 if ((del_attr_cnt > 0) && (add_attr_cnt == 0) && (rep_attr_cnt == 0)) {
1001 talloc_free(tmp_ctx);
1005 if (ldb_request_get_control(req,
1006 DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
1007 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
1008 * have a user password change and not a set as the message
1009 * looks like. In it's value blob it contains the NT and/or LM
1010 * hash of the old password specified by the user.
1011 * This control is used by the SAMR and "kpasswd" password
1012 * change mechanisms. */
1013 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
1014 GUID_DRS_USER_CHANGE_PASSWORD,
1015 SEC_ADS_CONTROL_ACCESS,
1018 else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
1019 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
1020 GUID_DRS_FORCE_CHANGE_PASSWORD,
1021 SEC_ADS_CONTROL_ACCESS,
1024 else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
1025 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
1026 GUID_DRS_USER_CHANGE_PASSWORD,
1027 SEC_ADS_CONTROL_ACCESS,
1029 /* Very strange, but we get constraint violation in this case */
1030 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
1031 ret = LDB_ERR_CONSTRAINT_VIOLATION;
1034 if (ret != LDB_SUCCESS) {
1035 dsdb_acl_debug(sd, acl_user_token(module),
1036 req->op.mod.message->dn,
1040 talloc_free(tmp_ctx);
1045 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
1048 struct ldb_context *ldb = ldb_module_get_ctx(module);
1049 const struct dsdb_schema *schema;
1051 const struct dsdb_class *objectclass;
1052 struct ldb_result *acl_res;
1053 struct security_descriptor *sd;
1054 struct dom_sid *sid = NULL;
1055 struct ldb_control *as_system;
1056 struct ldb_control *is_undelete;
1058 TALLOC_CTX *tmp_ctx;
1059 const struct ldb_message *msg = req->op.mod.message;
1060 static const char *acl_attrs[] = {
1061 "nTSecurityDescriptor",
1067 if (ldb_dn_is_special(msg->dn)) {
1068 return ldb_next_request(module, req);
1071 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1072 if (as_system != NULL) {
1073 as_system->critical = 0;
1076 is_undelete = ldb_request_get_control(req, DSDB_CONTROL_RESTORE_TOMBSTONE_OID);
1078 /* Don't print this debug statement if elements[0].name is going to be NULL */
1079 if (msg->num_elements > 0) {
1080 DEBUG(10, ("ldb:acl_modify: %s\n", msg->elements[0].name));
1082 if (dsdb_module_am_system(module) || as_system) {
1083 return ldb_next_request(module, req);
1086 tmp_ctx = talloc_new(req);
1087 if (tmp_ctx == NULL) {
1088 return ldb_oom(ldb);
1091 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, msg->dn,
1093 DSDB_FLAG_NEXT_MODULE |
1094 DSDB_FLAG_AS_SYSTEM |
1095 DSDB_SEARCH_SHOW_RECYCLED,
1098 if (ret != LDB_SUCCESS) {
1102 userPassword = dsdb_user_password_support(module, req, req);
1104 schema = dsdb_get_schema(ldb, tmp_ctx);
1106 talloc_free(tmp_ctx);
1107 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1108 "acl_modify: Error obtaining schema.");
1111 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
1112 if (ret != LDB_SUCCESS) {
1113 talloc_free(tmp_ctx);
1114 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1115 "acl_modify: Error retrieving security descriptor.");
1117 /* Theoretically we pass the check if the object has no sd */
1122 objectclass = dsdb_get_structural_oc_from_msg(schema, acl_res->msgs[0]);
1124 talloc_free(tmp_ctx);
1125 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1126 "acl_modify: Error retrieving object class for GUID.");
1128 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1129 for (i=0; i < msg->num_elements; i++) {
1130 const struct ldb_message_element *el = &msg->elements[i];
1131 const struct dsdb_attribute *attr;
1134 * This basic attribute existence check with the right errorcode
1135 * is needed since this module is the first one which requests
1136 * schema attribute information.
1137 * The complete attribute checking is done in the
1138 * "objectclass_attrs" module behind this one.
1140 * NOTE: "clearTextPassword" is not defined in the schema.
1142 attr = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
1143 if (!attr && ldb_attr_cmp("clearTextPassword", el->name) != 0) {
1144 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' "
1145 "on entry '%s' was not found in the schema!",
1146 req->op.mod.message->elements[i].name,
1147 ldb_dn_get_linearized(req->op.mod.message->dn));
1148 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
1152 if (ldb_attr_cmp("nTSecurityDescriptor", el->name) == 0) {
1153 uint32_t sd_flags = dsdb_request_sd_flags(req, NULL);
1154 uint32_t access_mask = 0;
1156 if (sd_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
1157 access_mask |= SEC_STD_WRITE_OWNER;
1159 if (sd_flags & SECINFO_DACL) {
1160 access_mask |= SEC_STD_WRITE_DAC;
1162 if (sd_flags & SECINFO_SACL) {
1163 access_mask |= SEC_FLAG_SYSTEM_SECURITY;
1166 ret = acl_check_access_on_attribute(module,
1173 if (ret != LDB_SUCCESS) {
1174 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1175 "Object %s has no write dacl access\n",
1176 ldb_dn_get_linearized(msg->dn));
1178 acl_user_token(module),
1182 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1185 } else if (ldb_attr_cmp("member", el->name) == 0) {
1186 ret = acl_check_self_membership(tmp_ctx,
1193 if (ret != LDB_SUCCESS) {
1196 } else if (ldb_attr_cmp("dBCSPwd", el->name) == 0) {
1197 /* this one is not affected by any rights, we should let it through
1198 so that passwords_hash returns the correct error */
1200 } else if (ldb_attr_cmp("unicodePwd", el->name) == 0 ||
1201 (userPassword && ldb_attr_cmp("userPassword", el->name) == 0) ||
1202 ldb_attr_cmp("clearTextPassword", el->name) == 0) {
1203 ret = acl_check_password_rights(tmp_ctx,
1210 if (ret != LDB_SUCCESS) {
1213 } else if (ldb_attr_cmp("servicePrincipalName", el->name) == 0) {
1214 ret = acl_check_spn(tmp_ctx,
1221 if (ret != LDB_SUCCESS) {
1224 } else if (is_undelete != NULL && (ldb_attr_cmp("isDeleted", el->name) == 0)) {
1226 * in case of undelete op permissions on
1227 * isDeleted are irrelevant and
1228 * distinguishedName is removed by the
1229 * tombstone_reanimate module
1233 ret = acl_check_access_on_attribute(module,
1240 if (ret != LDB_SUCCESS) {
1241 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1242 "Object %s has no write property access\n",
1243 ldb_dn_get_linearized(msg->dn));
1245 acl_user_token(module),
1249 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1256 talloc_free(tmp_ctx);
1257 return ldb_next_request(module, req);
1259 talloc_free(tmp_ctx);
1263 /* similar to the modify for the time being.
1264 * We need to consider the special delete tree case, though - TODO */
1265 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
1268 struct ldb_dn *parent;
1269 struct ldb_context *ldb;
1270 struct ldb_dn *nc_root;
1271 struct ldb_control *as_system;
1272 const struct dsdb_schema *schema;
1273 const struct dsdb_class *objectclass;
1274 struct security_descriptor *sd = NULL;
1275 struct dom_sid *sid = NULL;
1276 struct ldb_result *acl_res;
1277 static const char *acl_attrs[] = {
1278 "nTSecurityDescriptor",
1284 if (ldb_dn_is_special(req->op.del.dn)) {
1285 return ldb_next_request(module, req);
1288 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1289 if (as_system != NULL) {
1290 as_system->critical = 0;
1293 if (dsdb_module_am_system(module) || as_system) {
1294 return ldb_next_request(module, req);
1297 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
1299 ldb = ldb_module_get_ctx(module);
1301 parent = ldb_dn_get_parent(req, req->op.del.dn);
1302 if (parent == NULL) {
1303 return ldb_oom(ldb);
1306 /* Make sure we aren't deleting a NC */
1308 ret = dsdb_find_nc_root(ldb, req, req->op.del.dn, &nc_root);
1309 if (ret != LDB_SUCCESS) {
1312 if (ldb_dn_compare(nc_root, req->op.del.dn) == 0) {
1313 talloc_free(nc_root);
1314 DEBUG(10,("acl:deleting a NC\n"));
1315 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1316 return ldb_module_done(req, NULL, NULL,
1317 LDB_ERR_UNWILLING_TO_PERFORM);
1319 talloc_free(nc_root);
1321 ret = dsdb_module_search_dn(module, req, &acl_res,
1322 req->op.del.dn, acl_attrs,
1323 DSDB_FLAG_NEXT_MODULE |
1324 DSDB_FLAG_AS_SYSTEM |
1325 DSDB_SEARCH_SHOW_RECYCLED, req);
1326 /* we sould be able to find the parent */
1327 if (ret != LDB_SUCCESS) {
1328 DEBUG(10,("acl: failed to find object %s\n",
1329 ldb_dn_get_linearized(req->op.rename.olddn)));
1333 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1334 if (ret != LDB_SUCCESS) {
1335 return ldb_operr(ldb);
1338 return ldb_operr(ldb);
1341 schema = dsdb_get_schema(ldb, req);
1343 return ldb_operr(ldb);
1346 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1348 objectclass = dsdb_get_structural_oc_from_msg(schema, acl_res->msgs[0]);
1350 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1351 "acl_modify: Error retrieving object class for GUID.");
1354 if (ldb_request_get_control(req, LDB_CONTROL_TREE_DELETE_OID)) {
1355 ret = acl_check_access_on_objectclass(module, req, sd, sid,
1356 SEC_ADS_DELETE_TREE,
1358 if (ret != LDB_SUCCESS) {
1362 return ldb_next_request(module, req);
1365 /* First check if we have delete object right */
1366 ret = acl_check_access_on_objectclass(module, req, sd, sid,
1369 if (ret == LDB_SUCCESS) {
1370 return ldb_next_request(module, req);
1373 /* Nope, we don't have delete object. Lets check if we have delete
1374 * child on the parent */
1375 ret = dsdb_module_check_access_on_dn(module, req, parent,
1376 SEC_ADS_DELETE_CHILD,
1377 &objectclass->schemaIDGUID,
1379 if (ret != LDB_SUCCESS) {
1383 return ldb_next_request(module, req);
1385 static int acl_check_reanimate_tombstone(TALLOC_CTX *mem_ctx,
1386 struct ldb_module *module,
1387 struct ldb_request *req,
1388 struct ldb_dn *nc_root)
1391 struct ldb_result *acl_res;
1392 struct security_descriptor *sd = NULL;
1393 struct dom_sid *sid = NULL;
1394 static const char *acl_attrs[] = {
1395 "nTSecurityDescriptor",
1401 ret = dsdb_module_search_dn(module, mem_ctx, &acl_res,
1403 DSDB_FLAG_NEXT_MODULE |
1404 DSDB_FLAG_AS_SYSTEM |
1405 DSDB_SEARCH_SHOW_RECYCLED, req);
1406 if (ret != LDB_SUCCESS) {
1407 DEBUG(10,("acl: failed to find object %s\n",
1408 ldb_dn_get_linearized(nc_root)));
1412 ret = dsdb_get_sd_from_ldb_message(mem_ctx, req, acl_res->msgs[0], &sd);
1413 sid = samdb_result_dom_sid(mem_ctx, acl_res->msgs[0], "objectSid");
1414 if (ret != LDB_SUCCESS || !sd) {
1415 return ldb_operr(ldb_module_get_ctx(module));
1417 return acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
1418 GUID_DRS_REANIMATE_TOMBSTONE,
1419 SEC_ADS_CONTROL_ACCESS, sid);
1422 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
1425 struct ldb_dn *oldparent;
1426 struct ldb_dn *newparent;
1427 const struct dsdb_schema *schema;
1428 const struct dsdb_class *objectclass;
1429 const struct dsdb_attribute *attr = NULL;
1430 struct ldb_context *ldb;
1431 struct security_descriptor *sd = NULL;
1432 struct dom_sid *sid = NULL;
1433 struct ldb_result *acl_res;
1434 struct ldb_dn *nc_root;
1435 struct ldb_control *as_system;
1436 struct ldb_control *is_undelete;
1437 TALLOC_CTX *tmp_ctx;
1438 const char *rdn_name;
1439 static const char *acl_attrs[] = {
1440 "nTSecurityDescriptor",
1446 if (ldb_dn_is_special(req->op.rename.olddn)) {
1447 return ldb_next_request(module, req);
1450 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1451 if (as_system != NULL) {
1452 as_system->critical = 0;
1455 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
1456 if (dsdb_module_am_system(module) || as_system) {
1457 return ldb_next_request(module, req);
1460 ldb = ldb_module_get_ctx(module);
1462 tmp_ctx = talloc_new(req);
1463 if (tmp_ctx == NULL) {
1464 return ldb_oom(ldb);
1467 oldparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.olddn);
1468 if (oldparent == NULL) {
1469 return ldb_oom(ldb);
1471 newparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.newdn);
1472 if (newparent == NULL) {
1473 return ldb_oom(ldb);
1476 /* Make sure we aren't renaming/moving a NC */
1478 ret = dsdb_find_nc_root(ldb, req, req->op.rename.olddn, &nc_root);
1479 if (ret != LDB_SUCCESS) {
1482 if (ldb_dn_compare(nc_root, req->op.rename.olddn) == 0) {
1483 talloc_free(nc_root);
1484 DEBUG(10,("acl:renaming/moving a NC\n"));
1485 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1486 return ldb_module_done(req, NULL, NULL,
1487 LDB_ERR_UNWILLING_TO_PERFORM);
1490 /* special check for undelete operation */
1491 is_undelete = ldb_request_get_control(req, DSDB_CONTROL_RESTORE_TOMBSTONE_OID);
1492 if (is_undelete != NULL) {
1493 is_undelete->critical = 0;
1494 ret = acl_check_reanimate_tombstone(tmp_ctx, module, req, nc_root);
1495 if (ret != LDB_SUCCESS) {
1496 talloc_free(tmp_ctx);
1500 talloc_free(nc_root);
1502 /* Look for the parent */
1504 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res,
1505 req->op.rename.olddn, acl_attrs,
1506 DSDB_FLAG_NEXT_MODULE |
1507 DSDB_FLAG_AS_SYSTEM |
1508 DSDB_SEARCH_SHOW_RECYCLED, req);
1509 /* we sould be able to find the parent */
1510 if (ret != LDB_SUCCESS) {
1511 DEBUG(10,("acl: failed to find object %s\n",
1512 ldb_dn_get_linearized(req->op.rename.olddn)));
1513 talloc_free(tmp_ctx);
1517 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1518 if (ret != LDB_SUCCESS) {
1519 talloc_free(tmp_ctx);
1520 return ldb_operr(ldb);
1523 talloc_free(tmp_ctx);
1524 return ldb_operr(ldb);
1527 schema = dsdb_get_schema(ldb, acl_res);
1529 talloc_free(tmp_ctx);
1530 return ldb_operr(ldb);
1533 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1535 objectclass = dsdb_get_structural_oc_from_msg(schema, acl_res->msgs[0]);
1537 talloc_free(tmp_ctx);
1538 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1539 "acl_modify: Error retrieving object class for GUID.");
1542 attr = dsdb_attribute_by_lDAPDisplayName(schema, "name");
1544 talloc_free(tmp_ctx);
1545 return ldb_operr(ldb);
1548 ret = acl_check_access_on_attribute(module, tmp_ctx, sd, sid,
1551 if (ret != LDB_SUCCESS) {
1552 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1553 "Object %s has no wp on %s\n",
1554 ldb_dn_get_linearized(req->op.rename.olddn),
1555 attr->lDAPDisplayName);
1557 acl_user_token(module),
1558 req->op.rename.olddn,
1561 talloc_free(tmp_ctx);
1562 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1565 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1566 if (rdn_name == NULL) {
1567 talloc_free(tmp_ctx);
1568 return ldb_operr(ldb);
1571 attr = dsdb_attribute_by_lDAPDisplayName(schema, rdn_name);
1573 talloc_free(tmp_ctx);
1574 return ldb_operr(ldb);
1577 ret = acl_check_access_on_attribute(module, tmp_ctx, sd, sid,
1580 if (ret != LDB_SUCCESS) {
1581 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1582 "Object %s has no wp on %s\n",
1583 ldb_dn_get_linearized(req->op.rename.olddn),
1584 attr->lDAPDisplayName);
1586 acl_user_token(module),
1587 req->op.rename.olddn,
1590 talloc_free(tmp_ctx);
1591 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1594 if (ldb_dn_compare(oldparent, newparent) == 0) {
1595 /* regular rename, not move, nothing more to do */
1596 talloc_free(tmp_ctx);
1597 return ldb_next_request(module, req);
1600 /* new parent should have create child */
1601 ret = dsdb_module_check_access_on_dn(module, req, newparent,
1602 SEC_ADS_CREATE_CHILD,
1603 &objectclass->schemaIDGUID, req);
1604 if (ret != LDB_SUCCESS) {
1605 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1606 "acl:access_denied renaming %s",
1607 ldb_dn_get_linearized(req->op.rename.olddn));
1608 talloc_free(tmp_ctx);
1612 /* do we have delete object on the object? */
1613 /* this access is not necessary for undelete ops */
1614 if (is_undelete == NULL) {
1615 ret = acl_check_access_on_objectclass(module, tmp_ctx, sd, sid,
1618 if (ret == LDB_SUCCESS) {
1619 talloc_free(tmp_ctx);
1620 return ldb_next_request(module, req);
1622 /* what about delete child on the current parent */
1623 ret = dsdb_module_check_access_on_dn(module, req, oldparent,
1624 SEC_ADS_DELETE_CHILD,
1625 &objectclass->schemaIDGUID,
1627 if (ret != LDB_SUCCESS) {
1628 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1629 "acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn));
1630 talloc_free(tmp_ctx);
1631 return ldb_module_done(req, NULL, NULL, ret);
1634 talloc_free(tmp_ctx);
1636 return ldb_next_request(module, req);
1639 static int acl_search_update_confidential_attrs(struct acl_context *ac,
1640 struct acl_private *data)
1642 struct dsdb_attribute *a;
1645 if (data->acl_search) {
1647 * If acl:search is activated, the acl_read module
1648 * protects confidential attributes.
1653 if ((ac->schema == data->cached_schema_ptr) &&
1654 (ac->schema->loaded_usn == data->cached_schema_loaded_usn) &&
1655 (ac->schema->metadata_usn == data->cached_schema_metadata_usn))
1660 data->cached_schema_ptr = NULL;
1661 data->cached_schema_loaded_usn = 0;
1662 data->cached_schema_metadata_usn = 0;
1663 TALLOC_FREE(data->confidential_attrs);
1665 if (ac->schema == NULL) {
1669 for (a = ac->schema->attributes; a; a = a->next) {
1670 const char **attrs = data->confidential_attrs;
1672 if (!(a->searchFlags & SEARCH_FLAG_CONFIDENTIAL)) {
1676 attrs = talloc_realloc(data, attrs, const char *, n + 2);
1677 if (attrs == NULL) {
1678 TALLOC_FREE(data->confidential_attrs);
1679 return ldb_module_oom(ac->module);
1682 attrs[n] = a->lDAPDisplayName;
1686 data->confidential_attrs = attrs;
1689 data->cached_schema_ptr = ac->schema;
1690 data->cached_schema_loaded_usn = ac->schema->loaded_usn;
1691 data->cached_schema_metadata_usn = ac->schema->metadata_usn;
1696 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1698 struct acl_context *ac;
1699 struct acl_private *data;
1700 struct ldb_result *acl_res;
1701 static const char *acl_attrs[] = {
1703 "nTSecurityDescriptor",
1710 ac = talloc_get_type(req->context, struct acl_context);
1711 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1713 return ldb_module_done(ac->req, NULL, NULL,
1714 LDB_ERR_OPERATIONS_ERROR);
1716 if (ares->error != LDB_SUCCESS) {
1717 return ldb_module_done(ac->req, ares->controls,
1718 ares->response, ares->error);
1721 switch (ares->type) {
1722 case LDB_REPLY_ENTRY:
1723 if (ac->constructed_attrs) {
1724 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1726 DSDB_FLAG_NEXT_MODULE |
1727 DSDB_FLAG_AS_SYSTEM |
1728 DSDB_SEARCH_SHOW_RECYCLED,
1730 if (ret != LDB_SUCCESS) {
1731 return ldb_module_done(ac->req, NULL, NULL, ret);
1735 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1736 ret = acl_allowedAttributes(ac->module, ac->schema,
1739 if (ret != LDB_SUCCESS) {
1740 return ldb_module_done(ac->req, NULL, NULL, ret);
1744 if (ac->allowedChildClasses) {
1745 ret = acl_childClasses(ac->module, ac->schema,
1748 "allowedChildClasses");
1749 if (ret != LDB_SUCCESS) {
1750 return ldb_module_done(ac->req, NULL, NULL, ret);
1754 if (ac->allowedChildClassesEffective) {
1755 ret = acl_childClassesEffective(ac->module, ac->schema,
1758 if (ret != LDB_SUCCESS) {
1759 return ldb_module_done(ac->req, NULL, NULL, ret);
1763 if (ac->sDRightsEffective) {
1764 ret = acl_sDRightsEffective(ac->module,
1767 if (ret != LDB_SUCCESS) {
1768 return ldb_module_done(ac->req, NULL, NULL, ret);
1773 return ldb_module_send_entry(ac->req, ares->message,
1777 if (ac->am_system) {
1778 return ldb_module_send_entry(ac->req, ares->message,
1782 if (data->password_attrs != NULL) {
1783 for (i = 0; data->password_attrs[i]; i++) {
1784 if ((!ac->userPassword) &&
1785 (ldb_attr_cmp(data->password_attrs[i],
1786 "userPassword") == 0))
1791 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1795 if (ac->am_administrator) {
1796 return ldb_module_send_entry(ac->req, ares->message,
1800 ret = acl_search_update_confidential_attrs(ac, data);
1801 if (ret != LDB_SUCCESS) {
1805 if (data->confidential_attrs != NULL) {
1806 for (i = 0; data->confidential_attrs[i]; i++) {
1807 ldb_msg_remove_attr(ares->message,
1808 data->confidential_attrs[i]);
1812 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1814 case LDB_REPLY_REFERRAL:
1815 return ldb_module_send_referral(ac->req, ares->referral);
1817 case LDB_REPLY_DONE:
1818 return ldb_module_done(ac->req, ares->controls,
1819 ares->response, LDB_SUCCESS);
1825 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1827 struct ldb_context *ldb;
1828 struct acl_context *ac;
1829 struct ldb_parse_tree *down_tree;
1830 struct ldb_request *down_req;
1831 struct acl_private *data;
1835 if (ldb_dn_is_special(req->op.search.base)) {
1836 return ldb_next_request(module, req);
1839 ldb = ldb_module_get_ctx(module);
1841 ac = talloc_zero(req, struct acl_context);
1843 return ldb_oom(ldb);
1845 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1847 ac->module = module;
1849 ac->am_system = dsdb_module_am_system(module);
1850 ac->am_administrator = dsdb_module_am_administrator(module);
1851 ac->constructed_attrs = false;
1852 ac->modify_search = true;
1853 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1854 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1855 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1856 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1857 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1858 ac->userPassword = true;
1859 ac->schema = dsdb_get_schema(ldb, ac);
1861 ac->constructed_attrs |= ac->allowedAttributes;
1862 ac->constructed_attrs |= ac->allowedChildClasses;
1863 ac->constructed_attrs |= ac->allowedChildClassesEffective;
1864 ac->constructed_attrs |= ac->allowedAttributesEffective;
1865 ac->constructed_attrs |= ac->sDRightsEffective;
1868 ac->modify_search = false;
1870 if (ac->am_system) {
1871 ac->modify_search = false;
1874 if (!ac->constructed_attrs && !ac->modify_search) {
1876 return ldb_next_request(module, req);
1879 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1881 ac->userPassword = data->userPassword_support;
1884 ret = acl_search_update_confidential_attrs(ac, data);
1885 if (ret != LDB_SUCCESS) {
1889 down_tree = ldb_parse_tree_copy_shallow(ac, req->op.search.tree);
1890 if (down_tree == NULL) {
1891 return ldb_oom(ldb);
1894 if (!ac->am_system && data->password_attrs) {
1895 for (i = 0; data->password_attrs[i]; i++) {
1896 if ((!ac->userPassword) &&
1897 (ldb_attr_cmp(data->password_attrs[i],
1898 "userPassword") == 0))
1903 ldb_parse_tree_attr_replace(down_tree,
1904 data->password_attrs[i],
1905 "kludgeACLredactedattribute");
1909 if (!ac->am_system && !ac->am_administrator && data->confidential_attrs) {
1910 for (i = 0; data->confidential_attrs[i]; i++) {
1911 ldb_parse_tree_attr_replace(down_tree,
1912 data->confidential_attrs[i],
1913 "kludgeACLredactedattribute");
1917 ret = ldb_build_search_req_ex(&down_req,
1919 req->op.search.base,
1920 req->op.search.scope,
1922 req->op.search.attrs,
1924 ac, acl_search_callback,
1926 LDB_REQ_SET_LOCATION(down_req);
1927 if (ret != LDB_SUCCESS) {
1930 /* perform the search */
1931 return ldb_next_request(module, down_req);
1934 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1936 struct ldb_context *ldb = ldb_module_get_ctx(module);
1937 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1939 /* allow everybody to read the sequence number */
1940 if (strcmp(req->op.extended.oid,
1941 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1942 return ldb_next_request(module, req);
1945 if (dsdb_module_am_system(module) ||
1946 dsdb_module_am_administrator(module) || as_system) {
1947 return ldb_next_request(module, req);
1949 ldb_asprintf_errstring(ldb,
1951 "attempted database modify not permitted. "
1952 "User %s is not SYSTEM or an administrator",
1953 acl_user_name(req, module));
1954 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1958 static const struct ldb_module_ops ldb_acl_module_ops = {
1960 .search = acl_search,
1962 .modify = acl_modify,
1964 .rename = acl_rename,
1965 .extended = acl_extended,
1966 .init_context = acl_module_init
1969 int ldb_acl_module_init(const char *version)
1971 LDB_MODULE_CHECK_VERSION(version);
1972 return ldb_register_module(&ldb_acl_module_ops);