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 static const char *acl_attrs[] = {
1004 "nTSecurityDescriptor",
1010 if (ldb_dn_is_special(req->op.mod.message->dn)) {
1011 return ldb_next_request(module, req);
1014 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1015 if (as_system != NULL) {
1016 as_system->critical = 0;
1019 /* Don't print this debug statement if elements[0].name is going to be NULL */
1020 if(req->op.mod.message->num_elements > 0)
1022 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->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, req->op.mod.message->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 < req->op.mod.message->num_elements; i++){
1072 const struct dsdb_attribute *attr;
1073 attr = dsdb_attribute_by_lDAPDisplayName(schema,
1074 req->op.mod.message->elements[i].name);
1076 if (ldb_attr_cmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
1077 uint32_t sd_flags = dsdb_request_sd_flags(req, NULL);
1078 uint32_t access_mask = 0;
1080 if (sd_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
1081 access_mask |= SEC_STD_WRITE_OWNER;
1083 if (sd_flags & SECINFO_DACL) {
1084 access_mask |= SEC_STD_WRITE_DAC;
1086 if (sd_flags & SECINFO_SACL) {
1087 access_mask |= SEC_FLAG_SYSTEM_SECURITY;
1090 status = sec_access_check_ds(sd, acl_user_token(module),
1096 if (!NT_STATUS_IS_OK(status)) {
1097 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1098 "Object %s has no write dacl access\n",
1099 ldb_dn_get_linearized(req->op.mod.message->dn));
1101 acl_user_token(module),
1102 req->op.mod.message->dn,
1105 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1109 else if (ldb_attr_cmp("member", req->op.mod.message->elements[i].name) == 0) {
1110 ret = acl_check_self_membership(tmp_ctx,
1117 if (ret != LDB_SUCCESS) {
1121 else if (ldb_attr_cmp("dBCSPwd", req->op.mod.message->elements[i].name) == 0) {
1122 /* this one is not affected by any rights, we should let it through
1123 so that passwords_hash returns the correct error */
1126 else if (ldb_attr_cmp("unicodePwd", req->op.mod.message->elements[i].name) == 0 ||
1127 (userPassword && ldb_attr_cmp("userPassword", req->op.mod.message->elements[i].name) == 0) ||
1128 ldb_attr_cmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
1129 ret = acl_check_password_rights(tmp_ctx,
1136 if (ret != LDB_SUCCESS) {
1139 } else if (ldb_attr_cmp("servicePrincipalName", req->op.mod.message->elements[i].name) == 0) {
1140 ret = acl_check_spn(tmp_ctx,
1147 if (ret != LDB_SUCCESS) {
1151 struct object_tree *root = NULL;
1152 struct object_tree *new_node = NULL;
1154 /* This basic attribute existence check with the right errorcode
1155 * is needed since this module is the first one which requests
1156 * schema attribute information.
1157 * The complete attribute checking is done in the
1158 * "objectclass_attrs" module behind this one.
1161 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' on entry '%s' was not found in the schema!",
1162 req->op.mod.message->elements[i].name,
1163 ldb_dn_get_linearized(req->op.mod.message->dn));
1164 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
1168 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1169 &root, &new_node)) {
1170 talloc_free(tmp_ctx);
1171 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1172 "acl_modify: Error adding new node in object tree.");
1175 if (!insert_in_object_tree(tmp_ctx,
1176 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
1177 &new_node, &new_node)) {
1178 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1179 "acl_modify: cannot add to object tree securityGUID\n");
1180 ret = LDB_ERR_OPERATIONS_ERROR;
1184 if (!insert_in_object_tree(tmp_ctx,
1185 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
1186 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1187 "acl_modify: cannot add to object tree attributeGUID\n");
1188 ret = LDB_ERR_OPERATIONS_ERROR;
1192 status = sec_access_check_ds(sd, acl_user_token(module),
1197 if (!NT_STATUS_IS_OK(status)) {
1198 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1199 "Object %s has no write property access\n",
1200 ldb_dn_get_linearized(req->op.mod.message->dn));
1202 acl_user_token(module),
1203 req->op.mod.message->dn,
1206 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1213 talloc_free(tmp_ctx);
1214 return ldb_next_request(module, req);
1216 talloc_free(tmp_ctx);
1220 /* similar to the modify for the time being.
1221 * We need to consider the special delete tree case, though - TODO */
1222 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
1225 struct ldb_dn *parent;
1226 struct ldb_context *ldb;
1227 struct ldb_dn *nc_root;
1228 struct ldb_control *as_system;
1230 if (ldb_dn_is_special(req->op.del.dn)) {
1231 return ldb_next_request(module, req);
1234 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1235 if (as_system != NULL) {
1236 as_system->critical = 0;
1239 if (dsdb_module_am_system(module) || as_system) {
1240 return ldb_next_request(module, req);
1243 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
1245 ldb = ldb_module_get_ctx(module);
1247 parent = ldb_dn_get_parent(req, req->op.del.dn);
1248 if (parent == NULL) {
1249 return ldb_oom(ldb);
1252 /* Make sure we aren't deleting a NC */
1254 ret = dsdb_find_nc_root(ldb, req, req->op.del.dn, &nc_root);
1255 if (ret != LDB_SUCCESS) {
1258 if (ldb_dn_compare(nc_root, req->op.del.dn) == 0) {
1259 talloc_free(nc_root);
1260 DEBUG(10,("acl:deleting a NC\n"));
1261 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1262 return ldb_module_done(req, NULL, NULL,
1263 LDB_ERR_UNWILLING_TO_PERFORM);
1265 talloc_free(nc_root);
1267 if (ldb_request_get_control(req, LDB_CONTROL_TREE_DELETE_OID)) {
1268 ret = dsdb_module_check_access_on_dn(module, req,
1270 SEC_ADS_DELETE_TREE, NULL,
1272 if (ret != LDB_SUCCESS) {
1276 return ldb_next_request(module, req);
1279 /* First check if we have delete object right */
1280 ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn,
1281 SEC_STD_DELETE, NULL, req);
1282 if (ret == LDB_SUCCESS) {
1283 return ldb_next_request(module, req);
1286 /* Nope, we don't have delete object. Lets check if we have delete
1287 * child on the parent */
1288 ret = dsdb_module_check_access_on_dn(module, req, parent,
1289 SEC_ADS_DELETE_CHILD, NULL, req);
1290 if (ret != LDB_SUCCESS) {
1294 return ldb_next_request(module, req);
1297 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
1300 struct ldb_dn *oldparent;
1301 struct ldb_dn *newparent;
1302 const struct dsdb_schema *schema;
1303 struct ldb_context *ldb;
1304 struct security_descriptor *sd = NULL;
1305 struct dom_sid *sid = NULL;
1306 struct ldb_result *acl_res;
1307 const struct GUID *guid;
1308 struct ldb_dn *nc_root;
1309 struct object_tree *root = NULL;
1310 struct object_tree *new_node = NULL;
1311 struct ldb_control *as_system;
1312 TALLOC_CTX *tmp_ctx;
1314 uint32_t access_granted;
1315 const char *rdn_name;
1316 static const char *acl_attrs[] = {
1317 "nTSecurityDescriptor",
1323 if (ldb_dn_is_special(req->op.rename.olddn)) {
1324 return ldb_next_request(module, req);
1327 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1328 if (as_system != NULL) {
1329 as_system->critical = 0;
1332 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
1333 if (dsdb_module_am_system(module) || as_system) {
1334 return ldb_next_request(module, req);
1337 ldb = ldb_module_get_ctx(module);
1339 tmp_ctx = talloc_new(req);
1340 if (tmp_ctx == NULL) {
1341 return ldb_oom(ldb);
1344 oldparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.olddn);
1345 if (oldparent == NULL) {
1346 return ldb_oom(ldb);
1348 newparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.newdn);
1349 if (newparent == NULL) {
1350 return ldb_oom(ldb);
1353 /* Make sure we aren't renaming/moving a NC */
1355 ret = dsdb_find_nc_root(ldb, req, req->op.rename.olddn, &nc_root);
1356 if (ret != LDB_SUCCESS) {
1359 if (ldb_dn_compare(nc_root, req->op.rename.olddn) == 0) {
1360 talloc_free(nc_root);
1361 DEBUG(10,("acl:renaming/moving a NC\n"));
1362 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1363 return ldb_module_done(req, NULL, NULL,
1364 LDB_ERR_UNWILLING_TO_PERFORM);
1366 talloc_free(nc_root);
1368 /* Look for the parent */
1370 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res,
1371 req->op.rename.olddn, acl_attrs,
1372 DSDB_FLAG_NEXT_MODULE |
1373 DSDB_FLAG_AS_SYSTEM |
1374 DSDB_SEARCH_SHOW_RECYCLED, req);
1375 /* we sould be able to find the parent */
1376 if (ret != LDB_SUCCESS) {
1377 DEBUG(10,("acl: failed to find object %s\n",
1378 ldb_dn_get_linearized(req->op.rename.olddn)));
1379 talloc_free(tmp_ctx);
1383 schema = dsdb_get_schema(ldb, acl_res);
1385 talloc_free(tmp_ctx);
1386 return ldb_operr(ldb);
1389 guid = get_oc_guid_from_message(schema, acl_res->msgs[0]);
1390 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1391 &root, &new_node)) {
1392 talloc_free(tmp_ctx);
1393 return ldb_operr(ldb);
1396 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1398 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1399 &new_node, &new_node)) {
1400 talloc_free(tmp_ctx);
1401 return ldb_operr(ldb);
1404 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1405 if (rdn_name == NULL) {
1406 talloc_free(tmp_ctx);
1407 return ldb_operr(ldb);
1409 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1411 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1412 &new_node, &new_node)) {
1413 talloc_free(tmp_ctx);
1414 return ldb_operr(ldb);
1417 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1419 if (ret != LDB_SUCCESS) {
1420 talloc_free(tmp_ctx);
1421 return ldb_operr(ldb);
1423 /* Theoretically we pass the check if the object has no sd */
1425 talloc_free(tmp_ctx);
1428 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1429 status = sec_access_check_ds(sd, acl_user_token(module),
1435 if (!NT_STATUS_IS_OK(status)) {
1436 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1437 "Object %s has no wp on name\n",
1438 ldb_dn_get_linearized(req->op.rename.olddn));
1440 acl_user_token(module),
1441 req->op.rename.olddn,
1444 talloc_free(tmp_ctx);
1445 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1448 if (ldb_dn_compare(oldparent, newparent) == 0) {
1449 /* regular rename, not move, nothing more to do */
1450 talloc_free(tmp_ctx);
1451 return ldb_next_request(module, req);
1454 /* new parent should have create child */
1457 guid = get_oc_guid_from_message(schema, acl_res->msgs[0]);
1459 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1460 "acl:renamed object has no object class\n");
1461 talloc_free(tmp_ctx);
1462 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
1465 ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid, req);
1466 if (ret != LDB_SUCCESS) {
1467 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1468 "acl:access_denied renaming %s",
1469 ldb_dn_get_linearized(req->op.rename.olddn));
1470 talloc_free(tmp_ctx);
1473 /* do we have delete object on the object? */
1475 status = sec_access_check_ds(sd, acl_user_token(module),
1481 if (NT_STATUS_IS_OK(status)) {
1482 talloc_free(tmp_ctx);
1483 return ldb_next_request(module, req);
1485 /* what about delete child on the current parent */
1486 ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL, req);
1487 if (ret != LDB_SUCCESS) {
1488 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1489 "acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn));
1490 talloc_free(tmp_ctx);
1491 return ldb_module_done(req, NULL, NULL, ret);
1494 talloc_free(tmp_ctx);
1496 return ldb_next_request(module, req);
1499 static int acl_search_update_confidential_attrs(struct acl_context *ac,
1500 struct acl_private *data)
1502 struct dsdb_attribute *a;
1505 if (data->acl_search) {
1507 * If acl:search is activated, the acl_read module
1508 * protects confidential attributes.
1513 if ((ac->schema == data->cached_schema_ptr) &&
1514 (ac->schema->loaded_usn == data->cached_schema_loaded_usn) &&
1515 (ac->schema->metadata_usn == data->cached_schema_metadata_usn))
1520 data->cached_schema_ptr = NULL;
1521 data->cached_schema_loaded_usn = 0;
1522 data->cached_schema_metadata_usn = 0;
1523 TALLOC_FREE(data->confidential_attrs);
1525 if (ac->schema == NULL) {
1529 for (a = ac->schema->attributes; a; a = a->next) {
1530 const char **attrs = data->confidential_attrs;
1532 if (!(a->searchFlags & SEARCH_FLAG_CONFIDENTIAL)) {
1536 attrs = talloc_realloc(data, attrs, const char *, n + 2);
1537 if (attrs == NULL) {
1538 TALLOC_FREE(data->confidential_attrs);
1539 return ldb_module_oom(ac->module);
1542 attrs[n] = a->lDAPDisplayName;
1546 data->confidential_attrs = attrs;
1549 data->cached_schema_ptr = ac->schema;
1550 data->cached_schema_loaded_usn = ac->schema->loaded_usn;
1551 data->cached_schema_metadata_usn = ac->schema->metadata_usn;
1556 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1558 struct acl_context *ac;
1559 struct acl_private *data;
1560 struct ldb_result *acl_res;
1561 static const char *acl_attrs[] = {
1563 "nTSecurityDescriptor",
1570 ac = talloc_get_type(req->context, struct acl_context);
1571 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1573 return ldb_module_done(ac->req, NULL, NULL,
1574 LDB_ERR_OPERATIONS_ERROR);
1576 if (ares->error != LDB_SUCCESS) {
1577 return ldb_module_done(ac->req, ares->controls,
1578 ares->response, ares->error);
1581 switch (ares->type) {
1582 case LDB_REPLY_ENTRY:
1583 if (ac->constructed_attrs) {
1584 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1586 DSDB_FLAG_NEXT_MODULE |
1587 DSDB_FLAG_AS_SYSTEM |
1588 DSDB_SEARCH_SHOW_RECYCLED,
1590 if (ret != LDB_SUCCESS) {
1591 return ldb_module_done(ac->req, NULL, NULL, ret);
1595 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1596 ret = acl_allowedAttributes(ac->module, ac->schema,
1599 if (ret != LDB_SUCCESS) {
1600 return ldb_module_done(ac->req, NULL, NULL, ret);
1604 if (ac->allowedChildClasses) {
1605 ret = acl_childClasses(ac->module, ac->schema,
1608 "allowedChildClasses");
1609 if (ret != LDB_SUCCESS) {
1610 return ldb_module_done(ac->req, NULL, NULL, ret);
1614 if (ac->allowedChildClassesEffective) {
1615 ret = acl_childClassesEffective(ac->module, ac->schema,
1618 if (ret != LDB_SUCCESS) {
1619 return ldb_module_done(ac->req, NULL, NULL, ret);
1623 if (ac->sDRightsEffective) {
1624 ret = acl_sDRightsEffective(ac->module,
1627 if (ret != LDB_SUCCESS) {
1628 return ldb_module_done(ac->req, NULL, NULL, ret);
1633 return ldb_module_send_entry(ac->req, ares->message,
1637 if (ac->am_system) {
1638 return ldb_module_send_entry(ac->req, ares->message,
1642 if (data->password_attrs != NULL) {
1643 for (i = 0; data->password_attrs[i]; i++) {
1644 if ((!ac->userPassword) &&
1645 (ldb_attr_cmp(data->password_attrs[i],
1646 "userPassword") == 0))
1651 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1655 if (ac->am_administrator) {
1656 return ldb_module_send_entry(ac->req, ares->message,
1660 ret = acl_search_update_confidential_attrs(ac, data);
1661 if (ret != LDB_SUCCESS) {
1665 if (data->confidential_attrs != NULL) {
1666 for (i = 0; data->confidential_attrs[i]; i++) {
1667 ldb_msg_remove_attr(ares->message,
1668 data->confidential_attrs[i]);
1672 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1674 case LDB_REPLY_REFERRAL:
1675 return ldb_module_send_referral(ac->req, ares->referral);
1677 case LDB_REPLY_DONE:
1678 return ldb_module_done(ac->req, ares->controls,
1679 ares->response, LDB_SUCCESS);
1685 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1687 struct ldb_context *ldb;
1688 struct acl_context *ac;
1689 struct ldb_parse_tree *down_tree;
1690 struct ldb_request *down_req;
1691 struct acl_private *data;
1695 if (ldb_dn_is_special(req->op.search.base)) {
1696 return ldb_next_request(module, req);
1699 ldb = ldb_module_get_ctx(module);
1701 ac = talloc_zero(req, struct acl_context);
1703 return ldb_oom(ldb);
1705 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1707 ac->module = module;
1709 ac->am_system = dsdb_module_am_system(module);
1710 ac->am_administrator = dsdb_module_am_administrator(module);
1711 ac->constructed_attrs = false;
1712 ac->modify_search = true;
1713 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1714 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1715 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1716 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1717 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1718 ac->userPassword = true;
1719 ac->schema = dsdb_get_schema(ldb, ac);
1721 ac->constructed_attrs |= ac->allowedAttributes;
1722 ac->constructed_attrs |= ac->allowedChildClasses;
1723 ac->constructed_attrs |= ac->allowedChildClassesEffective;
1724 ac->constructed_attrs |= ac->allowedAttributesEffective;
1725 ac->constructed_attrs |= ac->sDRightsEffective;
1728 ac->modify_search = false;
1730 if (ac->am_system) {
1731 ac->modify_search = false;
1734 if (!ac->constructed_attrs && !ac->modify_search) {
1736 return ldb_next_request(module, req);
1739 if (!ac->am_system) {
1740 ac->userPassword = dsdb_user_password_support(module, ac, req);
1743 ret = acl_search_update_confidential_attrs(ac, data);
1744 if (ret != LDB_SUCCESS) {
1748 down_tree = ldb_parse_tree_copy_shallow(ac, req->op.search.tree);
1749 if (down_tree == NULL) {
1750 return ldb_oom(ldb);
1753 if (!ac->am_system && data->password_attrs) {
1754 for (i = 0; data->password_attrs[i]; i++) {
1755 if ((!ac->userPassword) &&
1756 (ldb_attr_cmp(data->password_attrs[i],
1757 "userPassword") == 0))
1762 ldb_parse_tree_attr_replace(down_tree,
1763 data->password_attrs[i],
1764 "kludgeACLredactedattribute");
1768 if (!ac->am_system && !ac->am_administrator && data->confidential_attrs) {
1769 for (i = 0; data->confidential_attrs[i]; i++) {
1770 ldb_parse_tree_attr_replace(down_tree,
1771 data->confidential_attrs[i],
1772 "kludgeACLredactedattribute");
1776 ret = ldb_build_search_req_ex(&down_req,
1778 req->op.search.base,
1779 req->op.search.scope,
1781 req->op.search.attrs,
1783 ac, acl_search_callback,
1785 LDB_REQ_SET_LOCATION(down_req);
1786 if (ret != LDB_SUCCESS) {
1789 /* perform the search */
1790 return ldb_next_request(module, down_req);
1793 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1795 struct ldb_context *ldb = ldb_module_get_ctx(module);
1796 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1798 /* allow everybody to read the sequence number */
1799 if (strcmp(req->op.extended.oid,
1800 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1801 return ldb_next_request(module, req);
1804 if (dsdb_module_am_system(module) ||
1805 dsdb_module_am_administrator(module) || as_system) {
1806 return ldb_next_request(module, req);
1808 ldb_asprintf_errstring(ldb,
1810 "attempted database modify not permitted. "
1811 "User %s is not SYSTEM or an administrator",
1812 acl_user_name(req, module));
1813 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1817 static const struct ldb_module_ops ldb_acl_module_ops = {
1819 .search = acl_search,
1821 .modify = acl_modify,
1823 .rename = acl_rename,
1824 .extended = acl_extended,
1825 .init_context = acl_module_init
1828 int ldb_acl_module_init(const char *version)
1830 LDB_MODULE_CHECK_VERSION(version);
1831 return ldb_register_module(&ldb_acl_module_ops);