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;
61 struct ldb_module *module;
62 struct ldb_request *req;
64 bool am_administrator;
66 bool constructed_attrs;
67 bool allowedAttributes;
68 bool allowedAttributesEffective;
69 bool allowedChildClasses;
70 bool allowedChildClassesEffective;
71 bool sDRightsEffective;
73 const char * const *attrs;
74 struct dsdb_schema *schema;
77 static int acl_module_init(struct ldb_module *module)
79 struct ldb_context *ldb;
80 struct acl_private *data;
84 static const char * const attrs[] = { "passwordAttribute", NULL };
85 static const char * const secret_attrs[] = {
86 DSDB_SECRET_ATTRIBUTES
88 struct ldb_result *res;
89 struct ldb_message *msg;
90 struct ldb_message_element *password_attributes;
92 ldb = ldb_module_get_ctx(module);
94 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
95 if (ret != LDB_SUCCESS) {
96 ldb_debug(ldb, LDB_DEBUG_ERROR,
97 "acl_module_init: Unable to register control with rootdse!\n");
98 return ldb_operr(ldb);
101 data = talloc_zero(module, struct acl_private);
106 data->acl_search = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"),
107 NULL, "acl", "search", true);
108 ldb_module_set_private(module, data);
110 mem_ctx = talloc_new(module);
115 ret = dsdb_module_search_dn(module, mem_ctx, &res,
116 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
118 DSDB_FLAG_NEXT_MODULE |
121 if (ret != LDB_SUCCESS) {
124 if (res->count == 0) {
128 if (res->count > 1) {
129 talloc_free(mem_ctx);
130 return LDB_ERR_CONSTRAINT_VIOLATION;
135 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
136 if (!password_attributes) {
139 data->password_attrs = talloc_array(data, const char *,
140 password_attributes->num_values +
141 ARRAY_SIZE(secret_attrs) + 1);
142 if (!data->password_attrs) {
143 talloc_free(mem_ctx);
148 for (i=0; i < password_attributes->num_values; i++) {
149 data->password_attrs[n] = (const char *)password_attributes->values[i].data;
150 talloc_steal(data->password_attrs, password_attributes->values[i].data);
154 for (i=0; i < ARRAY_SIZE(secret_attrs); i++) {
157 for (j=0; j < n; j++) {
158 if (strcasecmp(data->password_attrs[j], secret_attrs[i]) == 0) {
168 data->password_attrs[n] = talloc_strdup(data->password_attrs,
170 if (data->password_attrs[n] == NULL) {
171 talloc_free(mem_ctx);
176 data->password_attrs[n] = NULL;
179 talloc_free(mem_ctx);
180 return ldb_next_init(module);
183 static int acl_allowedAttributes(struct ldb_module *module,
184 const struct dsdb_schema *schema,
185 struct ldb_message *sd_msg,
186 struct ldb_message *msg,
187 struct acl_context *ac)
189 struct ldb_message_element *oc_el;
190 struct ldb_context *ldb = ldb_module_get_ctx(module);
192 const char **attr_list;
195 /* If we don't have a schema yet, we can't do anything... */
196 if (schema == NULL) {
197 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
198 return LDB_ERR_OPERATIONS_ERROR;
201 /* Must remove any existing attribute */
202 if (ac->allowedAttributes) {
203 ldb_msg_remove_attr(msg, "allowedAttributes");
206 mem_ctx = talloc_new(msg);
211 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
212 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
214 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
215 talloc_free(mem_ctx);
216 return LDB_ERR_OPERATIONS_ERROR;
218 if (ac->allowedAttributes) {
219 for (i=0; attr_list && attr_list[i]; i++) {
220 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
223 if (ac->allowedAttributesEffective) {
224 struct security_descriptor *sd;
225 struct dom_sid *sid = NULL;
226 struct ldb_control *as_system = ldb_request_get_control(ac->req,
227 LDB_CONTROL_AS_SYSTEM_OID);
229 if (as_system != NULL) {
230 as_system->critical = 0;
233 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
234 if (ac->am_system || as_system) {
235 for (i=0; attr_list && attr_list[i]; i++) {
236 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
241 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), mem_ctx, sd_msg, &sd);
243 if (ret != LDB_SUCCESS) {
247 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
248 for (i=0; attr_list && attr_list[i]; i++) {
249 const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
252 return ldb_operr(ldb);
254 /* remove constructed attributes */
255 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
257 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
260 ret = acl_check_access_on_attribute(module,
266 if (ret == LDB_SUCCESS) {
267 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
274 static int acl_childClasses(struct ldb_module *module,
275 const struct dsdb_schema *schema,
276 struct ldb_message *sd_msg,
277 struct ldb_message *msg,
278 const char *attrName)
280 struct ldb_message_element *oc_el;
281 struct ldb_message_element *allowedClasses;
282 const struct dsdb_class *sclass;
286 /* If we don't have a schema yet, we can't do anything... */
287 if (schema == NULL) {
288 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
289 return LDB_ERR_OPERATIONS_ERROR;
292 /* Must remove any existing attribute, or else confusion reins */
293 ldb_msg_remove_attr(msg, attrName);
294 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
295 if (ret != LDB_SUCCESS) {
299 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
301 for (i=0; oc_el && i < oc_el->num_values; i++) {
302 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
304 /* We don't know this class? what is going on? */
308 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
309 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
312 if (allowedClasses->num_values > 1) {
313 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
314 for (i=1 ; i < allowedClasses->num_values; i++) {
315 struct ldb_val *val1 = &allowedClasses->values[i-1];
316 struct ldb_val *val2 = &allowedClasses->values[i];
317 if (data_blob_cmp(val1, val2) == 0) {
318 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
319 allowedClasses->num_values--;
328 static int acl_check_access_on_class(struct ldb_module *module,
329 const struct dsdb_schema *schema,
331 struct security_descriptor *sd,
332 struct security_token *token,
333 struct dom_sid *rp_sid,
334 uint32_t access_mask,
335 const char *class_name)
339 uint32_t access_granted;
340 struct object_tree *root = NULL;
341 struct object_tree *new_node = NULL;
342 const struct GUID *guid;
344 if (class_name != NULL) {
345 guid = class_schemaid_guid_by_lDAPDisplayName(schema, class_name);
347 DEBUG(10, ("acl_search: cannot find class %s\n",
351 if (!insert_in_object_tree(mem_ctx,
354 DEBUG(10, ("acl_search: cannot add to object tree guid\n"));
359 status = sec_access_check_ds(sd, token,
364 if (!NT_STATUS_IS_OK(status)) {
365 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
371 return ldb_operr(ldb_module_get_ctx(module));
374 static int acl_childClassesEffective(struct ldb_module *module,
375 const struct dsdb_schema *schema,
376 struct ldb_message *sd_msg,
377 struct ldb_message *msg,
378 struct acl_context *ac)
380 struct ldb_message_element *oc_el;
381 struct ldb_message_element *allowedClasses = NULL;
382 const struct dsdb_class *sclass;
383 struct security_descriptor *sd;
384 struct ldb_control *as_system = ldb_request_get_control(ac->req,
385 LDB_CONTROL_AS_SYSTEM_OID);
386 struct dom_sid *sid = NULL;
390 if (as_system != NULL) {
391 as_system->critical = 0;
394 if (ac->am_system || as_system) {
395 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
398 /* If we don't have a schema yet, we can't do anything... */
399 if (schema == NULL) {
400 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
401 return LDB_ERR_OPERATIONS_ERROR;
404 /* Must remove any existing attribute, or else confusion reins */
405 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
407 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
408 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
409 if (ret != LDB_SUCCESS) {
413 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
414 for (i=0; oc_el && i < oc_el->num_values; i++) {
415 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
417 /* We don't know this class? what is going on? */
421 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
422 ret = acl_check_access_on_class(module,
426 acl_user_token(module),
428 SEC_ADS_CREATE_CHILD,
429 sclass->possibleInferiors[j]);
430 if (ret == LDB_SUCCESS) {
431 ldb_msg_add_string(msg, "allowedChildClassesEffective",
432 sclass->possibleInferiors[j]);
436 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
437 if (!allowedClasses) {
441 if (allowedClasses->num_values > 1) {
442 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
443 for (i=1 ; i < allowedClasses->num_values; i++) {
444 struct ldb_val *val1 = &allowedClasses->values[i-1];
445 struct ldb_val *val2 = &allowedClasses->values[i];
446 if (data_blob_cmp(val1, val2) == 0) {
447 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
448 allowedClasses->num_values--;
456 static int acl_sDRightsEffective(struct ldb_module *module,
457 struct ldb_message *sd_msg,
458 struct ldb_message *msg,
459 struct acl_context *ac)
461 struct ldb_context *ldb = ldb_module_get_ctx(module);
462 struct ldb_message_element *rightsEffective;
464 struct security_descriptor *sd;
465 struct ldb_control *as_system = ldb_request_get_control(ac->req,
466 LDB_CONTROL_AS_SYSTEM_OID);
467 struct dom_sid *sid = NULL;
470 if (as_system != NULL) {
471 as_system->critical = 0;
474 /* Must remove any existing attribute, or else confusion reins */
475 ldb_msg_remove_attr(msg, "sDRightsEffective");
476 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
477 if (ret != LDB_SUCCESS) {
480 if (ac->am_system || as_system) {
481 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
484 const struct dsdb_attribute *attr;
486 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
487 "nTSecurityDescriptor");
489 return ldb_operr(ldb);
492 /* Get the security descriptor from the message */
493 ret = dsdb_get_sd_from_ldb_message(ldb, msg, sd_msg, &sd);
494 if (ret != LDB_SUCCESS) {
497 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
498 ret = acl_check_access_on_attribute(module,
504 if (ret == LDB_SUCCESS) {
505 flags |= SECINFO_OWNER | SECINFO_GROUP;
507 ret = acl_check_access_on_attribute(module,
513 if (ret == LDB_SUCCESS) {
514 flags |= SECINFO_DACL;
516 ret = acl_check_access_on_attribute(module,
520 SEC_FLAG_SYSTEM_SECURITY,
522 if (ret == LDB_SUCCESS) {
523 flags |= SECINFO_SACL;
526 return samdb_msg_add_uint(ldb_module_get_ctx(module), msg, msg,
527 "sDRightsEffective", flags);
530 static int acl_validate_spn_value(TALLOC_CTX *mem_ctx,
531 struct ldb_context *ldb,
532 const char *spn_value,
533 uint32_t userAccountControl,
534 const char *samAccountName,
535 const char *dnsHostName,
536 const char *netbios_name,
537 const char *ntds_guid)
540 krb5_context krb_ctx;
541 krb5_error_code kerr;
542 krb5_principal principal;
546 const char *forest_name = samdb_forest_name(ldb, mem_ctx);
547 const char *base_domain = samdb_default_domain_name(ldb, mem_ctx);
548 struct loadparm_context *lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
549 struct loadparm_context);
550 bool is_dc = (userAccountControl & UF_SERVER_TRUST_ACCOUNT) ||
551 (userAccountControl & UF_PARTIAL_SECRETS_ACCOUNT);
553 if (strcasecmp_m(spn_value, samAccountName) == 0) {
554 /* MacOS X sets this value, and setting an SPN of your
555 * own samAccountName is both pointless and safe */
559 kerr = smb_krb5_init_context_basic(mem_ctx,
563 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
564 "Could not initialize kerberos context.");
567 ret = krb5_parse_name(krb_ctx, spn_value, &principal);
569 krb5_free_context(krb_ctx);
570 return LDB_ERR_CONSTRAINT_VIOLATION;
573 if (principal->name.name_string.len < 2) {
577 instanceName = principal->name.name_string.val[1];
578 serviceType = principal->name.name_string.val[0];
579 if (principal->name.name_string.len == 3) {
580 serviceName = principal->name.name_string.val[2];
589 if (strcasecmp(serviceType, "ldap") == 0) {
590 if (strcasecmp(serviceName, netbios_name) != 0 &&
591 strcasecmp(serviceName, forest_name) != 0) {
595 } else if (strcasecmp(serviceType, "gc") == 0) {
596 if (strcasecmp(serviceName, forest_name) != 0) {
600 if (strcasecmp(serviceName, base_domain) != 0 &&
601 strcasecmp(serviceName, netbios_name) != 0) {
606 /* instanceName can be samAccountName without $ or dnsHostName
607 * or "ntds_guid._msdcs.forest_domain for DC objects */
608 if (strlen(instanceName) == (strlen(samAccountName) - 1)
609 && strncasecmp(instanceName, samAccountName, strlen(samAccountName) - 1) == 0) {
611 } else if (dnsHostName != NULL && strcasecmp(instanceName, dnsHostName) == 0) {
614 const char *guid_str;
615 guid_str = talloc_asprintf(mem_ctx,"%s._msdcs.%s",
618 if (strcasecmp(instanceName, guid_str) == 0) {
624 krb5_free_principal(krb_ctx, principal);
625 krb5_free_context(krb_ctx);
626 return LDB_ERR_CONSTRAINT_VIOLATION;
629 krb5_free_principal(krb_ctx, principal);
630 krb5_free_context(krb_ctx);
634 static int acl_check_spn(TALLOC_CTX *mem_ctx,
635 struct ldb_module *module,
636 struct ldb_request *req,
637 struct security_descriptor *sd,
639 const struct GUID *oc_guid,
640 const struct dsdb_attribute *attr)
644 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
645 struct ldb_context *ldb = ldb_module_get_ctx(module);
646 struct ldb_result *acl_res;
647 struct ldb_result *netbios_res;
648 struct ldb_message_element *el;
649 struct ldb_dn *partitions_dn = samdb_partitions_dn(ldb, tmp_ctx);
650 uint32_t userAccountControl;
651 const char *samAccountName;
652 const char *dnsHostName;
653 const char *netbios_name;
655 char *ntds_guid = NULL;
657 static const char *acl_attrs[] = {
660 "userAccountControl",
663 static const char *netbios_attrs[] = {
668 /* if we have wp, we can do whatever we like */
669 if (acl_check_access_on_attribute(module,
674 attr) == LDB_SUCCESS) {
675 talloc_free(tmp_ctx);
679 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
680 GUID_DRS_VALIDATE_SPN,
684 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
685 dsdb_acl_debug(sd, acl_user_token(module),
686 req->op.mod.message->dn,
689 talloc_free(tmp_ctx);
693 ret = dsdb_module_search_dn(module, tmp_ctx,
694 &acl_res, req->op.mod.message->dn,
696 DSDB_FLAG_NEXT_MODULE |
697 DSDB_FLAG_AS_SYSTEM |
698 DSDB_SEARCH_SHOW_RECYCLED,
700 if (ret != LDB_SUCCESS) {
701 talloc_free(tmp_ctx);
705 userAccountControl = ldb_msg_find_attr_as_uint(acl_res->msgs[0], "userAccountControl", 0);
706 dnsHostName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "dnsHostName", NULL);
707 samAccountName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "samAccountName", NULL);
709 ret = dsdb_module_search(module, tmp_ctx,
710 &netbios_res, partitions_dn,
713 DSDB_FLAG_NEXT_MODULE |
717 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)));
719 netbios_name = ldb_msg_find_attr_as_string(netbios_res->msgs[0], "nETBIOSName", NULL);
721 el = ldb_msg_find_element(req->op.mod.message, "servicePrincipalName");
723 talloc_free(tmp_ctx);
724 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
725 "Error finding element for servicePrincipalName.");
728 /* NTDSDSA objectGuid of object we are checking SPN for */
729 if (userAccountControl & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
730 ret = dsdb_module_find_ntdsguid_for_computer(module, tmp_ctx,
731 req->op.mod.message->dn, &ntds, req);
732 if (ret != LDB_SUCCESS) {
733 ldb_asprintf_errstring(ldb, "Failed to find NTDSDSA objectGuid for %s: %s",
734 ldb_dn_get_linearized(req->op.mod.message->dn),
736 talloc_free(tmp_ctx);
737 return LDB_ERR_OPERATIONS_ERROR;
739 ntds_guid = GUID_string(tmp_ctx, &ntds);
742 for (i=0; i < el->num_values; i++) {
743 ret = acl_validate_spn_value(tmp_ctx,
745 (char *)el->values[i].data,
751 if (ret != LDB_SUCCESS) {
752 talloc_free(tmp_ctx);
756 talloc_free(tmp_ctx);
760 static int acl_add(struct ldb_module *module, struct ldb_request *req)
763 struct ldb_dn *parent;
764 struct ldb_context *ldb;
765 const struct dsdb_schema *schema;
766 struct ldb_message_element *oc_el;
767 const struct GUID *guid;
768 struct ldb_dn *nc_root;
769 struct ldb_control *as_system;
771 if (ldb_dn_is_special(req->op.add.message->dn)) {
772 return ldb_next_request(module, req);
775 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
776 if (as_system != NULL) {
777 as_system->critical = 0;
780 if (dsdb_module_am_system(module) || as_system) {
781 return ldb_next_request(module, req);
784 ldb = ldb_module_get_ctx(module);
786 parent = ldb_dn_get_parent(req, req->op.add.message->dn);
787 if (parent == NULL) {
791 /* Creating an NC. There is probably something we should do here,
792 * but we will establish that later */
794 ret = dsdb_find_nc_root(ldb, req, req->op.add.message->dn, &nc_root);
795 if (ret != LDB_SUCCESS) {
798 if (ldb_dn_compare(nc_root, req->op.add.message->dn) == 0) {
799 talloc_free(nc_root);
800 return ldb_next_request(module, req);
802 talloc_free(nc_root);
804 schema = dsdb_get_schema(ldb, req);
806 return ldb_operr(ldb);
809 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
810 if (!oc_el || oc_el->num_values == 0) {
811 ldb_asprintf_errstring(ldb_module_get_ctx(module),
812 "acl: unable to find objectClass on %s\n",
813 ldb_dn_get_linearized(req->op.add.message->dn));
814 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
817 guid = class_schemaid_guid_by_lDAPDisplayName(schema,
818 (char *)oc_el->values[oc_el->num_values-1].data);
819 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid, req);
820 if (ret != LDB_SUCCESS) {
823 return ldb_next_request(module, req);
826 /* ckecks if modifications are allowed on "Member" attribute */
827 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
828 struct ldb_module *module,
829 struct ldb_request *req,
830 struct security_descriptor *sd,
832 const struct GUID *oc_guid,
833 const struct dsdb_attribute *attr)
837 struct ldb_context *ldb = ldb_module_get_ctx(module);
838 struct ldb_dn *user_dn;
839 struct ldb_message_element *member_el;
840 /* if we have wp, we can do whatever we like */
841 if (acl_check_access_on_attribute(module,
846 attr) == LDB_SUCCESS) {
849 /* if we are adding/deleting ourselves, check for self membership */
850 ret = dsdb_find_dn_by_sid(ldb, mem_ctx,
851 &acl_user_token(module)->sids[PRIMARY_USER_SID_INDEX],
853 if (ret != LDB_SUCCESS) {
856 member_el = ldb_msg_find_element(req->op.mod.message, "member");
858 return ldb_operr(ldb);
860 /* user can only remove oneself */
861 if (member_el->num_values == 0) {
862 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
864 for (i = 0; i < member_el->num_values; i++) {
865 if (strcasecmp((const char *)member_el->values[i].data,
866 ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
867 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
870 ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
871 GUID_DRS_SELF_MEMBERSHIP,
874 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
875 dsdb_acl_debug(sd, acl_user_token(module),
876 req->op.mod.message->dn,
883 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
884 struct ldb_module *module,
885 struct ldb_request *req,
886 struct security_descriptor *sd,
888 const struct GUID *oc_guid,
891 int ret = LDB_SUCCESS;
892 unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
893 struct ldb_message_element *el;
894 struct ldb_message *msg;
895 const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
896 "unicodePwd", "dBCSPwd", NULL }, **l;
897 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
899 msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
901 return ldb_module_oom(module);
903 for (l = passwordAttrs; *l != NULL; l++) {
904 if ((!userPassword) && (ldb_attr_cmp(*l, "userPassword") == 0)) {
908 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
909 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
912 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
915 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
918 ldb_msg_remove_element(msg, el);
922 /* single deletes will be handled by the "password_hash" LDB module
923 * later in the stack, so we let it though here */
924 if ((del_attr_cnt > 0) && (add_attr_cnt == 0) && (rep_attr_cnt == 0)) {
925 talloc_free(tmp_ctx);
929 if (ldb_request_get_control(req,
930 DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
931 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
932 * have a user password change and not a set as the message
933 * looks like. In it's value blob it contains the NT and/or LM
934 * hash of the old password specified by the user.
935 * This control is used by the SAMR and "kpasswd" password
936 * change mechanisms. */
937 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
938 GUID_DRS_USER_CHANGE_PASSWORD,
939 SEC_ADS_CONTROL_ACCESS,
942 else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
943 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
944 GUID_DRS_FORCE_CHANGE_PASSWORD,
945 SEC_ADS_CONTROL_ACCESS,
948 else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
949 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
950 GUID_DRS_USER_CHANGE_PASSWORD,
951 SEC_ADS_CONTROL_ACCESS,
953 /* Very strange, but we get constraint violation in this case */
954 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
955 ret = LDB_ERR_CONSTRAINT_VIOLATION;
958 if (ret != LDB_SUCCESS) {
959 dsdb_acl_debug(sd, acl_user_token(module),
960 req->op.mod.message->dn,
964 talloc_free(tmp_ctx);
968 static const struct GUID *get_oc_guid_from_message(const struct dsdb_schema *schema,
969 struct ldb_message *msg)
971 struct ldb_message_element *oc_el;
972 const struct dsdb_class *object_class;
974 oc_el = ldb_msg_find_element(msg, "objectClass");
979 object_class = dsdb_get_last_structural_class(schema, oc_el);
980 if (object_class == NULL) {
984 return &object_class->schemaIDGUID;
988 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
991 struct ldb_context *ldb = ldb_module_get_ctx(module);
992 const struct dsdb_schema *schema;
994 const struct GUID *guid;
995 uint32_t access_granted;
997 struct ldb_result *acl_res;
998 struct security_descriptor *sd;
999 struct dom_sid *sid = NULL;
1000 struct ldb_control *as_system;
1002 TALLOC_CTX *tmp_ctx;
1003 const struct ldb_message *msg = req->op.mod.message;
1004 static const char *acl_attrs[] = {
1005 "nTSecurityDescriptor",
1011 if (ldb_dn_is_special(msg->dn)) {
1012 return ldb_next_request(module, req);
1015 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1016 if (as_system != NULL) {
1017 as_system->critical = 0;
1020 /* Don't print this debug statement if elements[0].name is going to be NULL */
1021 if (msg->num_elements > 0) {
1022 DEBUG(10, ("ldb:acl_modify: %s\n", msg->elements[0].name));
1024 if (dsdb_module_am_system(module) || as_system) {
1025 return ldb_next_request(module, req);
1028 tmp_ctx = talloc_new(req);
1029 if (tmp_ctx == NULL) {
1030 return ldb_oom(ldb);
1033 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, msg->dn,
1035 DSDB_FLAG_NEXT_MODULE |
1036 DSDB_FLAG_AS_SYSTEM |
1037 DSDB_SEARCH_SHOW_RECYCLED,
1040 if (ret != LDB_SUCCESS) {
1044 userPassword = dsdb_user_password_support(module, req, req);
1046 schema = dsdb_get_schema(ldb, tmp_ctx);
1048 talloc_free(tmp_ctx);
1049 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1050 "acl_modify: Error obtaining schema.");
1053 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
1054 if (ret != LDB_SUCCESS) {
1055 talloc_free(tmp_ctx);
1056 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1057 "acl_modify: Error retrieving security descriptor.");
1059 /* Theoretically we pass the check if the object has no sd */
1064 guid = get_oc_guid_from_message(schema, acl_res->msgs[0]);
1066 talloc_free(tmp_ctx);
1067 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1068 "acl_modify: Error retrieving object class GUID.");
1070 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1071 for (i=0; i < msg->num_elements; i++) {
1072 const struct ldb_message_element *el = &msg->elements[i];
1073 const struct dsdb_attribute *attr;
1076 * This basic attribute existence check with the right errorcode
1077 * is needed since this module is the first one which requests
1078 * schema attribute information.
1079 * The complete attribute checking is done in the
1080 * "objectclass_attrs" module behind this one.
1082 * NOTE: "clearTextPassword" is not defined in the schema.
1084 attr = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
1085 if (!attr && ldb_attr_cmp("clearTextPassword", el->name) != 0) {
1086 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' "
1087 "on entry '%s' was not found in the schema!",
1088 req->op.mod.message->elements[i].name,
1089 ldb_dn_get_linearized(req->op.mod.message->dn));
1090 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
1094 if (ldb_attr_cmp("nTSecurityDescriptor", el->name) == 0) {
1095 uint32_t sd_flags = dsdb_request_sd_flags(req, NULL);
1096 uint32_t access_mask = 0;
1098 if (sd_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
1099 access_mask |= SEC_STD_WRITE_OWNER;
1101 if (sd_flags & SECINFO_DACL) {
1102 access_mask |= SEC_STD_WRITE_DAC;
1104 if (sd_flags & SECINFO_SACL) {
1105 access_mask |= SEC_FLAG_SYSTEM_SECURITY;
1108 status = sec_access_check_ds(sd, acl_user_token(module),
1114 if (!NT_STATUS_IS_OK(status)) {
1115 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1116 "Object %s has no write dacl access\n",
1117 ldb_dn_get_linearized(msg->dn));
1119 acl_user_token(module),
1123 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1126 } else if (ldb_attr_cmp("member", el->name) == 0) {
1127 ret = acl_check_self_membership(tmp_ctx,
1134 if (ret != LDB_SUCCESS) {
1137 } else if (ldb_attr_cmp("dBCSPwd", el->name) == 0) {
1138 /* this one is not affected by any rights, we should let it through
1139 so that passwords_hash returns the correct error */
1141 } else if (ldb_attr_cmp("unicodePwd", el->name) == 0 ||
1142 (userPassword && ldb_attr_cmp("userPassword", el->name) == 0) ||
1143 ldb_attr_cmp("clearTextPassword", el->name) == 0) {
1144 ret = acl_check_password_rights(tmp_ctx,
1151 if (ret != LDB_SUCCESS) {
1154 } else if (ldb_attr_cmp("servicePrincipalName", el->name) == 0) {
1155 ret = acl_check_spn(tmp_ctx,
1162 if (ret != LDB_SUCCESS) {
1166 struct object_tree *root = NULL;
1167 struct object_tree *new_node = NULL;
1169 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1170 &root, &new_node)) {
1171 talloc_free(tmp_ctx);
1172 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1173 "acl_modify: Error adding new node in object tree.");
1176 if (!insert_in_object_tree(tmp_ctx,
1177 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
1178 &new_node, &new_node)) {
1179 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1180 "acl_modify: cannot add to object tree securityGUID\n");
1181 ret = LDB_ERR_OPERATIONS_ERROR;
1185 if (!insert_in_object_tree(tmp_ctx,
1186 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
1187 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1188 "acl_modify: cannot add to object tree attributeGUID\n");
1189 ret = LDB_ERR_OPERATIONS_ERROR;
1193 status = sec_access_check_ds(sd, acl_user_token(module),
1198 if (!NT_STATUS_IS_OK(status)) {
1199 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1200 "Object %s has no write property access\n",
1201 ldb_dn_get_linearized(msg->dn));
1203 acl_user_token(module),
1207 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1214 talloc_free(tmp_ctx);
1215 return ldb_next_request(module, req);
1217 talloc_free(tmp_ctx);
1221 /* similar to the modify for the time being.
1222 * We need to consider the special delete tree case, though - TODO */
1223 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
1226 struct ldb_dn *parent;
1227 struct ldb_context *ldb;
1228 struct ldb_dn *nc_root;
1229 struct ldb_control *as_system;
1231 if (ldb_dn_is_special(req->op.del.dn)) {
1232 return ldb_next_request(module, req);
1235 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1236 if (as_system != NULL) {
1237 as_system->critical = 0;
1240 if (dsdb_module_am_system(module) || as_system) {
1241 return ldb_next_request(module, req);
1244 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
1246 ldb = ldb_module_get_ctx(module);
1248 parent = ldb_dn_get_parent(req, req->op.del.dn);
1249 if (parent == NULL) {
1250 return ldb_oom(ldb);
1253 /* Make sure we aren't deleting a NC */
1255 ret = dsdb_find_nc_root(ldb, req, req->op.del.dn, &nc_root);
1256 if (ret != LDB_SUCCESS) {
1259 if (ldb_dn_compare(nc_root, req->op.del.dn) == 0) {
1260 talloc_free(nc_root);
1261 DEBUG(10,("acl:deleting a NC\n"));
1262 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1263 return ldb_module_done(req, NULL, NULL,
1264 LDB_ERR_UNWILLING_TO_PERFORM);
1266 talloc_free(nc_root);
1268 if (ldb_request_get_control(req, LDB_CONTROL_TREE_DELETE_OID)) {
1269 ret = dsdb_module_check_access_on_dn(module, req,
1271 SEC_ADS_DELETE_TREE, NULL,
1273 if (ret != LDB_SUCCESS) {
1277 return ldb_next_request(module, req);
1280 /* First check if we have delete object right */
1281 ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn,
1282 SEC_STD_DELETE, NULL, req);
1283 if (ret == LDB_SUCCESS) {
1284 return ldb_next_request(module, req);
1287 /* Nope, we don't have delete object. Lets check if we have delete
1288 * child on the parent */
1289 ret = dsdb_module_check_access_on_dn(module, req, parent,
1290 SEC_ADS_DELETE_CHILD, NULL, req);
1291 if (ret != LDB_SUCCESS) {
1295 return ldb_next_request(module, req);
1298 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
1301 struct ldb_dn *oldparent;
1302 struct ldb_dn *newparent;
1303 const struct dsdb_schema *schema;
1304 struct ldb_context *ldb;
1305 struct security_descriptor *sd = NULL;
1306 struct dom_sid *sid = NULL;
1307 struct ldb_result *acl_res;
1308 const struct GUID *guid;
1309 struct ldb_dn *nc_root;
1310 struct object_tree *root = NULL;
1311 struct object_tree *new_node = NULL;
1312 struct ldb_control *as_system;
1313 TALLOC_CTX *tmp_ctx;
1315 uint32_t access_granted;
1316 const char *rdn_name;
1317 static const char *acl_attrs[] = {
1318 "nTSecurityDescriptor",
1324 if (ldb_dn_is_special(req->op.rename.olddn)) {
1325 return ldb_next_request(module, req);
1328 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1329 if (as_system != NULL) {
1330 as_system->critical = 0;
1333 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
1334 if (dsdb_module_am_system(module) || as_system) {
1335 return ldb_next_request(module, req);
1338 ldb = ldb_module_get_ctx(module);
1340 tmp_ctx = talloc_new(req);
1341 if (tmp_ctx == NULL) {
1342 return ldb_oom(ldb);
1345 oldparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.olddn);
1346 if (oldparent == NULL) {
1347 return ldb_oom(ldb);
1349 newparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.newdn);
1350 if (newparent == NULL) {
1351 return ldb_oom(ldb);
1354 /* Make sure we aren't renaming/moving a NC */
1356 ret = dsdb_find_nc_root(ldb, req, req->op.rename.olddn, &nc_root);
1357 if (ret != LDB_SUCCESS) {
1360 if (ldb_dn_compare(nc_root, req->op.rename.olddn) == 0) {
1361 talloc_free(nc_root);
1362 DEBUG(10,("acl:renaming/moving a NC\n"));
1363 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1364 return ldb_module_done(req, NULL, NULL,
1365 LDB_ERR_UNWILLING_TO_PERFORM);
1367 talloc_free(nc_root);
1369 /* Look for the parent */
1371 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res,
1372 req->op.rename.olddn, acl_attrs,
1373 DSDB_FLAG_NEXT_MODULE |
1374 DSDB_FLAG_AS_SYSTEM |
1375 DSDB_SEARCH_SHOW_RECYCLED, req);
1376 /* we sould be able to find the parent */
1377 if (ret != LDB_SUCCESS) {
1378 DEBUG(10,("acl: failed to find object %s\n",
1379 ldb_dn_get_linearized(req->op.rename.olddn)));
1380 talloc_free(tmp_ctx);
1384 schema = dsdb_get_schema(ldb, acl_res);
1386 talloc_free(tmp_ctx);
1387 return ldb_operr(ldb);
1390 guid = get_oc_guid_from_message(schema, acl_res->msgs[0]);
1391 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1392 &root, &new_node)) {
1393 talloc_free(tmp_ctx);
1394 return ldb_operr(ldb);
1397 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1399 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1400 &new_node, &new_node)) {
1401 talloc_free(tmp_ctx);
1402 return ldb_operr(ldb);
1405 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1406 if (rdn_name == NULL) {
1407 talloc_free(tmp_ctx);
1408 return ldb_operr(ldb);
1410 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1412 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1413 &new_node, &new_node)) {
1414 talloc_free(tmp_ctx);
1415 return ldb_operr(ldb);
1418 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1420 if (ret != LDB_SUCCESS) {
1421 talloc_free(tmp_ctx);
1422 return ldb_operr(ldb);
1424 /* Theoretically we pass the check if the object has no sd */
1426 talloc_free(tmp_ctx);
1429 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1430 status = sec_access_check_ds(sd, acl_user_token(module),
1436 if (!NT_STATUS_IS_OK(status)) {
1437 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1438 "Object %s has no wp on name\n",
1439 ldb_dn_get_linearized(req->op.rename.olddn));
1441 acl_user_token(module),
1442 req->op.rename.olddn,
1445 talloc_free(tmp_ctx);
1446 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1449 if (ldb_dn_compare(oldparent, newparent) == 0) {
1450 /* regular rename, not move, nothing more to do */
1451 talloc_free(tmp_ctx);
1452 return ldb_next_request(module, req);
1455 /* new parent should have create child */
1458 guid = get_oc_guid_from_message(schema, acl_res->msgs[0]);
1460 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1461 "acl:renamed object has no object class\n");
1462 talloc_free(tmp_ctx);
1463 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
1466 ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid, req);
1467 if (ret != LDB_SUCCESS) {
1468 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1469 "acl:access_denied renaming %s",
1470 ldb_dn_get_linearized(req->op.rename.olddn));
1471 talloc_free(tmp_ctx);
1474 /* do we have delete object on the object? */
1476 status = sec_access_check_ds(sd, acl_user_token(module),
1482 if (NT_STATUS_IS_OK(status)) {
1483 talloc_free(tmp_ctx);
1484 return ldb_next_request(module, req);
1486 /* what about delete child on the current parent */
1487 ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL, req);
1488 if (ret != LDB_SUCCESS) {
1489 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1490 "acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn));
1491 talloc_free(tmp_ctx);
1492 return ldb_module_done(req, NULL, NULL, ret);
1495 talloc_free(tmp_ctx);
1497 return ldb_next_request(module, req);
1500 static int acl_search_update_confidential_attrs(struct acl_context *ac,
1501 struct acl_private *data)
1503 struct dsdb_attribute *a;
1506 if (data->acl_search) {
1508 * If acl:search is activated, the acl_read module
1509 * protects confidential attributes.
1514 if ((ac->schema == data->cached_schema_ptr) &&
1515 (ac->schema->loaded_usn == data->cached_schema_loaded_usn) &&
1516 (ac->schema->metadata_usn == data->cached_schema_metadata_usn))
1521 data->cached_schema_ptr = NULL;
1522 data->cached_schema_loaded_usn = 0;
1523 data->cached_schema_metadata_usn = 0;
1524 TALLOC_FREE(data->confidential_attrs);
1526 if (ac->schema == NULL) {
1530 for (a = ac->schema->attributes; a; a = a->next) {
1531 const char **attrs = data->confidential_attrs;
1533 if (!(a->searchFlags & SEARCH_FLAG_CONFIDENTIAL)) {
1537 attrs = talloc_realloc(data, attrs, const char *, n + 2);
1538 if (attrs == NULL) {
1539 TALLOC_FREE(data->confidential_attrs);
1540 return ldb_module_oom(ac->module);
1543 attrs[n] = a->lDAPDisplayName;
1547 data->confidential_attrs = attrs;
1550 data->cached_schema_ptr = ac->schema;
1551 data->cached_schema_loaded_usn = ac->schema->loaded_usn;
1552 data->cached_schema_metadata_usn = ac->schema->metadata_usn;
1557 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1559 struct acl_context *ac;
1560 struct acl_private *data;
1561 struct ldb_result *acl_res;
1562 static const char *acl_attrs[] = {
1564 "nTSecurityDescriptor",
1571 ac = talloc_get_type(req->context, struct acl_context);
1572 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1574 return ldb_module_done(ac->req, NULL, NULL,
1575 LDB_ERR_OPERATIONS_ERROR);
1577 if (ares->error != LDB_SUCCESS) {
1578 return ldb_module_done(ac->req, ares->controls,
1579 ares->response, ares->error);
1582 switch (ares->type) {
1583 case LDB_REPLY_ENTRY:
1584 if (ac->constructed_attrs) {
1585 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1587 DSDB_FLAG_NEXT_MODULE |
1588 DSDB_FLAG_AS_SYSTEM |
1589 DSDB_SEARCH_SHOW_RECYCLED,
1591 if (ret != LDB_SUCCESS) {
1592 return ldb_module_done(ac->req, NULL, NULL, ret);
1596 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1597 ret = acl_allowedAttributes(ac->module, ac->schema,
1600 if (ret != LDB_SUCCESS) {
1601 return ldb_module_done(ac->req, NULL, NULL, ret);
1605 if (ac->allowedChildClasses) {
1606 ret = acl_childClasses(ac->module, ac->schema,
1609 "allowedChildClasses");
1610 if (ret != LDB_SUCCESS) {
1611 return ldb_module_done(ac->req, NULL, NULL, ret);
1615 if (ac->allowedChildClassesEffective) {
1616 ret = acl_childClassesEffective(ac->module, ac->schema,
1619 if (ret != LDB_SUCCESS) {
1620 return ldb_module_done(ac->req, NULL, NULL, ret);
1624 if (ac->sDRightsEffective) {
1625 ret = acl_sDRightsEffective(ac->module,
1628 if (ret != LDB_SUCCESS) {
1629 return ldb_module_done(ac->req, NULL, NULL, ret);
1634 return ldb_module_send_entry(ac->req, ares->message,
1638 if (ac->am_system) {
1639 return ldb_module_send_entry(ac->req, ares->message,
1643 if (data->password_attrs != NULL) {
1644 for (i = 0; data->password_attrs[i]; i++) {
1645 if ((!ac->userPassword) &&
1646 (ldb_attr_cmp(data->password_attrs[i],
1647 "userPassword") == 0))
1652 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1656 if (ac->am_administrator) {
1657 return ldb_module_send_entry(ac->req, ares->message,
1661 ret = acl_search_update_confidential_attrs(ac, data);
1662 if (ret != LDB_SUCCESS) {
1666 if (data->confidential_attrs != NULL) {
1667 for (i = 0; data->confidential_attrs[i]; i++) {
1668 ldb_msg_remove_attr(ares->message,
1669 data->confidential_attrs[i]);
1673 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1675 case LDB_REPLY_REFERRAL:
1676 return ldb_module_send_referral(ac->req, ares->referral);
1678 case LDB_REPLY_DONE:
1679 return ldb_module_done(ac->req, ares->controls,
1680 ares->response, LDB_SUCCESS);
1686 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1688 struct ldb_context *ldb;
1689 struct acl_context *ac;
1690 struct ldb_parse_tree *down_tree;
1691 struct ldb_request *down_req;
1692 struct acl_private *data;
1696 if (ldb_dn_is_special(req->op.search.base)) {
1697 return ldb_next_request(module, req);
1700 ldb = ldb_module_get_ctx(module);
1702 ac = talloc_zero(req, struct acl_context);
1704 return ldb_oom(ldb);
1706 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1708 ac->module = module;
1710 ac->am_system = dsdb_module_am_system(module);
1711 ac->am_administrator = dsdb_module_am_administrator(module);
1712 ac->constructed_attrs = false;
1713 ac->modify_search = true;
1714 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1715 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1716 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1717 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1718 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1719 ac->userPassword = true;
1720 ac->schema = dsdb_get_schema(ldb, ac);
1722 ac->constructed_attrs |= ac->allowedAttributes;
1723 ac->constructed_attrs |= ac->allowedChildClasses;
1724 ac->constructed_attrs |= ac->allowedChildClassesEffective;
1725 ac->constructed_attrs |= ac->allowedAttributesEffective;
1726 ac->constructed_attrs |= ac->sDRightsEffective;
1729 ac->modify_search = false;
1731 if (ac->am_system) {
1732 ac->modify_search = false;
1735 if (!ac->constructed_attrs && !ac->modify_search) {
1737 return ldb_next_request(module, req);
1740 if (!ac->am_system) {
1741 ac->userPassword = dsdb_user_password_support(module, ac, req);
1744 ret = acl_search_update_confidential_attrs(ac, data);
1745 if (ret != LDB_SUCCESS) {
1749 down_tree = ldb_parse_tree_copy_shallow(ac, req->op.search.tree);
1750 if (down_tree == NULL) {
1751 return ldb_oom(ldb);
1754 if (!ac->am_system && data->password_attrs) {
1755 for (i = 0; data->password_attrs[i]; i++) {
1756 if ((!ac->userPassword) &&
1757 (ldb_attr_cmp(data->password_attrs[i],
1758 "userPassword") == 0))
1763 ldb_parse_tree_attr_replace(down_tree,
1764 data->password_attrs[i],
1765 "kludgeACLredactedattribute");
1769 if (!ac->am_system && !ac->am_administrator && data->confidential_attrs) {
1770 for (i = 0; data->confidential_attrs[i]; i++) {
1771 ldb_parse_tree_attr_replace(down_tree,
1772 data->confidential_attrs[i],
1773 "kludgeACLredactedattribute");
1777 ret = ldb_build_search_req_ex(&down_req,
1779 req->op.search.base,
1780 req->op.search.scope,
1782 req->op.search.attrs,
1784 ac, acl_search_callback,
1786 LDB_REQ_SET_LOCATION(down_req);
1787 if (ret != LDB_SUCCESS) {
1790 /* perform the search */
1791 return ldb_next_request(module, down_req);
1794 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1796 struct ldb_context *ldb = ldb_module_get_ctx(module);
1797 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1799 /* allow everybody to read the sequence number */
1800 if (strcmp(req->op.extended.oid,
1801 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1802 return ldb_next_request(module, req);
1805 if (dsdb_module_am_system(module) ||
1806 dsdb_module_am_administrator(module) || as_system) {
1807 return ldb_next_request(module, req);
1809 ldb_asprintf_errstring(ldb,
1811 "attempted database modify not permitted. "
1812 "User %s is not SYSTEM or an administrator",
1813 acl_user_name(req, module));
1814 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1818 static const struct ldb_module_ops ldb_acl_module_ops = {
1820 .search = acl_search,
1822 .modify = acl_modify,
1824 .rename = acl_rename,
1825 .extended = acl_extended,
1826 .init_context = acl_module_init
1829 int ldb_acl_module_init(const char *version)
1831 LDB_MODULE_CHECK_VERSION(version);
1832 return ldb_register_module(&ldb_acl_module_ops);