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 *attrs[] = { "passwordAttribute", NULL };
85 struct ldb_result *res;
86 struct ldb_message *msg;
87 struct ldb_message_element *password_attributes;
89 ldb = ldb_module_get_ctx(module);
91 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
92 if (ret != LDB_SUCCESS) {
93 ldb_debug(ldb, LDB_DEBUG_ERROR,
94 "acl_module_init: Unable to register control with rootdse!\n");
95 return ldb_operr(ldb);
98 data = talloc_zero(module, struct acl_private);
103 data->acl_search = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"),
104 NULL, "acl", "search", false);
105 ldb_module_set_private(module, data);
107 mem_ctx = talloc_new(module);
112 ret = dsdb_module_search_dn(module, mem_ctx, &res,
113 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
115 DSDB_FLAG_NEXT_MODULE |
118 if (ret != LDB_SUCCESS) {
121 if (res->count == 0) {
125 if (res->count > 1) {
126 talloc_free(mem_ctx);
127 return LDB_ERR_CONSTRAINT_VIOLATION;
132 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
133 if (!password_attributes) {
136 data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
137 if (!data->password_attrs) {
138 talloc_free(mem_ctx);
141 for (i=0; i < password_attributes->num_values; i++) {
142 data->password_attrs[i] = (const char *)password_attributes->values[i].data;
143 talloc_steal(data->password_attrs, password_attributes->values[i].data);
145 data->password_attrs[i] = NULL;
148 talloc_free(mem_ctx);
149 return ldb_next_init(module);
152 static int acl_allowedAttributes(struct ldb_module *module,
153 const struct dsdb_schema *schema,
154 struct ldb_message *sd_msg,
155 struct ldb_message *msg,
156 struct acl_context *ac)
158 struct ldb_message_element *oc_el;
159 struct ldb_context *ldb = ldb_module_get_ctx(module);
161 const char **attr_list;
164 /* If we don't have a schema yet, we can't do anything... */
165 if (schema == NULL) {
166 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
167 return LDB_ERR_OPERATIONS_ERROR;
170 /* Must remove any existing attribute */
171 if (ac->allowedAttributes) {
172 ldb_msg_remove_attr(msg, "allowedAttributes");
175 mem_ctx = talloc_new(msg);
180 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
181 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
183 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
184 talloc_free(mem_ctx);
185 return LDB_ERR_OPERATIONS_ERROR;
187 if (ac->allowedAttributes) {
188 for (i=0; attr_list && attr_list[i]; i++) {
189 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
192 if (ac->allowedAttributesEffective) {
193 struct security_descriptor *sd;
194 struct dom_sid *sid = NULL;
195 struct ldb_control *as_system = ldb_request_get_control(ac->req,
196 LDB_CONTROL_AS_SYSTEM_OID);
198 if (as_system != NULL) {
199 as_system->critical = 0;
202 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
203 if (ac->am_system || as_system) {
204 for (i=0; attr_list && attr_list[i]; i++) {
205 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
210 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), mem_ctx, sd_msg, &sd);
212 if (ret != LDB_SUCCESS) {
216 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
217 for (i=0; attr_list && attr_list[i]; i++) {
218 const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
221 return ldb_operr(ldb);
223 /* remove constructed attributes */
224 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
226 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
229 ret = acl_check_access_on_attribute(module,
235 if (ret == LDB_SUCCESS) {
236 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
243 static int acl_childClasses(struct ldb_module *module,
244 const struct dsdb_schema *schema,
245 struct ldb_message *sd_msg,
246 struct ldb_message *msg,
247 const char *attrName)
249 struct ldb_message_element *oc_el;
250 struct ldb_message_element *allowedClasses;
251 const struct dsdb_class *sclass;
255 /* If we don't have a schema yet, we can't do anything... */
256 if (schema == NULL) {
257 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
258 return LDB_ERR_OPERATIONS_ERROR;
261 /* Must remove any existing attribute, or else confusion reins */
262 ldb_msg_remove_attr(msg, attrName);
263 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
264 if (ret != LDB_SUCCESS) {
268 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
270 for (i=0; oc_el && i < oc_el->num_values; i++) {
271 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
273 /* We don't know this class? what is going on? */
277 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
278 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
281 if (allowedClasses->num_values > 1) {
282 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
283 for (i=1 ; i < allowedClasses->num_values; i++) {
284 struct ldb_val *val1 = &allowedClasses->values[i-1];
285 struct ldb_val *val2 = &allowedClasses->values[i];
286 if (data_blob_cmp(val1, val2) == 0) {
287 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
288 allowedClasses->num_values--;
297 static int acl_check_access_on_class(struct ldb_module *module,
298 const struct dsdb_schema *schema,
300 struct security_descriptor *sd,
301 struct security_token *token,
302 struct dom_sid *rp_sid,
303 uint32_t access_mask,
304 const char *class_name)
308 uint32_t access_granted;
309 struct object_tree *root = NULL;
310 struct object_tree *new_node = NULL;
311 const struct GUID *guid;
313 if (class_name != NULL) {
314 guid = class_schemaid_guid_by_lDAPDisplayName(schema, class_name);
316 DEBUG(10, ("acl_search: cannot find class %s\n",
320 if (!insert_in_object_tree(mem_ctx,
323 DEBUG(10, ("acl_search: cannot add to object tree guid\n"));
328 status = sec_access_check_ds(sd, token,
333 if (!NT_STATUS_IS_OK(status)) {
334 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
340 return ldb_operr(ldb_module_get_ctx(module));
343 static int acl_childClassesEffective(struct ldb_module *module,
344 const struct dsdb_schema *schema,
345 struct ldb_message *sd_msg,
346 struct ldb_message *msg,
347 struct acl_context *ac)
349 struct ldb_message_element *oc_el;
350 struct ldb_message_element *allowedClasses = NULL;
351 const struct dsdb_class *sclass;
352 struct security_descriptor *sd;
353 struct ldb_control *as_system = ldb_request_get_control(ac->req,
354 LDB_CONTROL_AS_SYSTEM_OID);
355 struct dom_sid *sid = NULL;
359 if (as_system != NULL) {
360 as_system->critical = 0;
363 if (ac->am_system || as_system) {
364 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
367 /* If we don't have a schema yet, we can't do anything... */
368 if (schema == NULL) {
369 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
370 return LDB_ERR_OPERATIONS_ERROR;
373 /* Must remove any existing attribute, or else confusion reins */
374 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
376 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
377 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
378 if (ret != LDB_SUCCESS) {
382 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
383 for (i=0; oc_el && i < oc_el->num_values; i++) {
384 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
386 /* We don't know this class? what is going on? */
390 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
391 ret = acl_check_access_on_class(module,
395 acl_user_token(module),
397 SEC_ADS_CREATE_CHILD,
398 sclass->possibleInferiors[j]);
399 if (ret == LDB_SUCCESS) {
400 ldb_msg_add_string(msg, "allowedChildClassesEffective",
401 sclass->possibleInferiors[j]);
405 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
406 if (!allowedClasses) {
410 if (allowedClasses->num_values > 1) {
411 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
412 for (i=1 ; i < allowedClasses->num_values; i++) {
413 struct ldb_val *val1 = &allowedClasses->values[i-1];
414 struct ldb_val *val2 = &allowedClasses->values[i];
415 if (data_blob_cmp(val1, val2) == 0) {
416 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
417 allowedClasses->num_values--;
425 static int acl_sDRightsEffective(struct ldb_module *module,
426 struct ldb_message *sd_msg,
427 struct ldb_message *msg,
428 struct acl_context *ac)
430 struct ldb_message_element *rightsEffective;
432 struct security_descriptor *sd;
433 struct ldb_control *as_system = ldb_request_get_control(ac->req,
434 LDB_CONTROL_AS_SYSTEM_OID);
435 struct dom_sid *sid = NULL;
438 if (as_system != NULL) {
439 as_system->critical = 0;
442 /* Must remove any existing attribute, or else confusion reins */
443 ldb_msg_remove_attr(msg, "sDRightsEffective");
444 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
445 if (ret != LDB_SUCCESS) {
448 if (ac->am_system || as_system) {
449 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
452 /* Get the security descriptor from the message */
453 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
454 if (ret != LDB_SUCCESS) {
457 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
458 ret = acl_check_access_on_attribute(module,
464 if (ret == LDB_SUCCESS) {
465 flags |= SECINFO_OWNER | SECINFO_GROUP;
467 ret = acl_check_access_on_attribute(module,
473 if (ret == LDB_SUCCESS) {
474 flags |= SECINFO_DACL;
476 ret = acl_check_access_on_attribute(module,
480 SEC_FLAG_SYSTEM_SECURITY,
482 if (ret == LDB_SUCCESS) {
483 flags |= SECINFO_SACL;
486 return samdb_msg_add_uint(ldb_module_get_ctx(module), msg, msg,
487 "sDRightsEffective", flags);
490 static int acl_validate_spn_value(TALLOC_CTX *mem_ctx,
491 struct ldb_context *ldb,
492 const char *spn_value,
493 uint32_t userAccountControl,
494 const char *samAccountName,
495 const char *dnsHostName,
496 const char *netbios_name,
497 const char *ntds_guid)
500 krb5_context krb_ctx;
501 krb5_error_code kerr;
502 krb5_principal principal;
506 const char *forest_name = samdb_forest_name(ldb, mem_ctx);
507 const char *base_domain = samdb_default_domain_name(ldb, mem_ctx);
508 struct loadparm_context *lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
509 struct loadparm_context);
510 bool is_dc = (userAccountControl & UF_SERVER_TRUST_ACCOUNT) ||
511 (userAccountControl & UF_PARTIAL_SECRETS_ACCOUNT);
513 if (strcasecmp_m(spn_value, samAccountName) == 0) {
514 /* MacOS X sets this value, and setting an SPN of your
515 * own samAccountName is both pointless and safe */
519 kerr = smb_krb5_init_context_basic(mem_ctx,
523 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
524 "Could not initialize kerberos context.");
527 ret = krb5_parse_name(krb_ctx, spn_value, &principal);
529 krb5_free_context(krb_ctx);
530 return LDB_ERR_CONSTRAINT_VIOLATION;
533 if (principal->name.name_string.len < 2) {
537 instanceName = principal->name.name_string.val[1];
538 serviceType = principal->name.name_string.val[0];
539 if (principal->name.name_string.len == 3) {
540 serviceName = principal->name.name_string.val[2];
549 if (strcasecmp(serviceType, "ldap") == 0) {
550 if (strcasecmp(serviceName, netbios_name) != 0 &&
551 strcasecmp(serviceName, forest_name) != 0) {
555 } else if (strcasecmp(serviceType, "gc") == 0) {
556 if (strcasecmp(serviceName, forest_name) != 0) {
560 if (strcasecmp(serviceName, base_domain) != 0 &&
561 strcasecmp(serviceName, netbios_name) != 0) {
566 /* instanceName can be samAccountName without $ or dnsHostName
567 * or "ntds_guid._msdcs.forest_domain for DC objects */
568 if (strlen(instanceName) == (strlen(samAccountName) - 1)
569 && strncasecmp(instanceName, samAccountName, strlen(samAccountName) - 1) == 0) {
571 } else if (dnsHostName != NULL && strcasecmp(instanceName, dnsHostName) == 0) {
574 const char *guid_str;
575 guid_str = talloc_asprintf(mem_ctx,"%s._msdcs.%s",
578 if (strcasecmp(instanceName, guid_str) == 0) {
584 krb5_free_principal(krb_ctx, principal);
585 krb5_free_context(krb_ctx);
586 return LDB_ERR_CONSTRAINT_VIOLATION;
589 krb5_free_principal(krb_ctx, principal);
590 krb5_free_context(krb_ctx);
594 static int acl_check_spn(TALLOC_CTX *mem_ctx,
595 struct ldb_module *module,
596 struct ldb_request *req,
597 struct security_descriptor *sd,
599 const struct GUID *oc_guid,
600 const struct dsdb_attribute *attr)
604 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
605 struct ldb_context *ldb = ldb_module_get_ctx(module);
606 struct ldb_result *acl_res;
607 struct ldb_result *netbios_res;
608 struct ldb_message_element *el;
609 struct ldb_dn *partitions_dn = samdb_partitions_dn(ldb, tmp_ctx);
610 uint32_t userAccountControl;
611 const char *samAccountName;
612 const char *dnsHostName;
613 const char *netbios_name;
615 char *ntds_guid = NULL;
617 static const char *acl_attrs[] = {
620 "userAccountControl",
623 static const char *netbios_attrs[] = {
628 /* if we have wp, we can do whatever we like */
629 if (acl_check_access_on_attribute(module,
634 attr) == LDB_SUCCESS) {
635 talloc_free(tmp_ctx);
639 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
640 GUID_DRS_VALIDATE_SPN,
644 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
645 dsdb_acl_debug(sd, acl_user_token(module),
646 req->op.mod.message->dn,
649 talloc_free(tmp_ctx);
653 ret = dsdb_module_search_dn(module, tmp_ctx,
654 &acl_res, req->op.mod.message->dn,
656 DSDB_FLAG_NEXT_MODULE |
657 DSDB_FLAG_AS_SYSTEM |
658 DSDB_SEARCH_SHOW_RECYCLED,
660 if (ret != LDB_SUCCESS) {
661 talloc_free(tmp_ctx);
665 userAccountControl = ldb_msg_find_attr_as_uint(acl_res->msgs[0], "userAccountControl", 0);
666 dnsHostName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "dnsHostName", NULL);
667 samAccountName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "samAccountName", NULL);
669 ret = dsdb_module_search(module, tmp_ctx,
670 &netbios_res, partitions_dn,
673 DSDB_FLAG_NEXT_MODULE |
677 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)));
679 netbios_name = ldb_msg_find_attr_as_string(netbios_res->msgs[0], "nETBIOSName", NULL);
681 el = ldb_msg_find_element(req->op.mod.message, "servicePrincipalName");
683 talloc_free(tmp_ctx);
684 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
685 "Error finding element for servicePrincipalName.");
688 /* NTDSDSA objectGuid of object we are checking SPN for */
689 if (userAccountControl & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
690 ret = dsdb_module_find_ntdsguid_for_computer(module, tmp_ctx,
691 req->op.mod.message->dn, &ntds, req);
692 if (ret != LDB_SUCCESS) {
693 ldb_asprintf_errstring(ldb, "Failed to find NTDSDSA objectGuid for %s: %s",
694 ldb_dn_get_linearized(req->op.mod.message->dn),
696 talloc_free(tmp_ctx);
697 return LDB_ERR_OPERATIONS_ERROR;
699 ntds_guid = GUID_string(tmp_ctx, &ntds);
702 for (i=0; i < el->num_values; i++) {
703 ret = acl_validate_spn_value(tmp_ctx,
705 (char *)el->values[i].data,
711 if (ret != LDB_SUCCESS) {
712 talloc_free(tmp_ctx);
716 talloc_free(tmp_ctx);
720 static int acl_add(struct ldb_module *module, struct ldb_request *req)
723 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.add.message->dn);
724 struct ldb_context *ldb;
725 const struct dsdb_schema *schema;
726 struct ldb_message_element *oc_el;
727 const struct GUID *guid;
728 struct ldb_dn *nc_root;
729 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
731 if (as_system != NULL) {
732 as_system->critical = 0;
735 if (dsdb_module_am_system(module) || as_system) {
736 return ldb_next_request(module, req);
738 if (ldb_dn_is_special(req->op.add.message->dn)) {
739 return ldb_next_request(module, req);
742 ldb = ldb_module_get_ctx(module);
744 /* Creating an NC. There is probably something we should do here,
745 * but we will establish that later */
747 ret = dsdb_find_nc_root(ldb, req, req->op.add.message->dn, &nc_root);
748 if (ret != LDB_SUCCESS) {
751 if (ldb_dn_compare(nc_root, req->op.add.message->dn) == 0) {
752 talloc_free(nc_root);
753 return ldb_next_request(module, req);
755 talloc_free(nc_root);
757 schema = dsdb_get_schema(ldb, req);
759 return ldb_operr(ldb);
762 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
763 if (!oc_el || oc_el->num_values == 0) {
764 ldb_asprintf_errstring(ldb_module_get_ctx(module),
765 "acl: unable to find objectClass on %s\n",
766 ldb_dn_get_linearized(req->op.add.message->dn));
767 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
770 guid = class_schemaid_guid_by_lDAPDisplayName(schema,
771 (char *)oc_el->values[oc_el->num_values-1].data);
772 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid, req);
773 if (ret != LDB_SUCCESS) {
776 return ldb_next_request(module, req);
779 /* ckecks if modifications are allowed on "Member" attribute */
780 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
781 struct ldb_module *module,
782 struct ldb_request *req,
783 struct security_descriptor *sd,
785 const struct GUID *oc_guid,
786 const struct dsdb_attribute *attr)
790 struct ldb_context *ldb = ldb_module_get_ctx(module);
791 struct ldb_dn *user_dn;
792 struct ldb_message_element *member_el;
793 /* if we have wp, we can do whatever we like */
794 if (acl_check_access_on_attribute(module,
799 attr) == LDB_SUCCESS) {
802 /* if we are adding/deleting ourselves, check for self membership */
803 ret = dsdb_find_dn_by_sid(ldb, mem_ctx,
804 &acl_user_token(module)->sids[PRIMARY_USER_SID_INDEX],
806 if (ret != LDB_SUCCESS) {
809 member_el = ldb_msg_find_element(req->op.mod.message, "member");
811 return ldb_operr(ldb);
813 /* user can only remove oneself */
814 if (member_el->num_values == 0) {
815 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
817 for (i = 0; i < member_el->num_values; i++) {
818 if (strcasecmp((const char *)member_el->values[i].data,
819 ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
820 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
823 ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
824 GUID_DRS_SELF_MEMBERSHIP,
827 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
828 dsdb_acl_debug(sd, acl_user_token(module),
829 req->op.mod.message->dn,
836 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
837 struct ldb_module *module,
838 struct ldb_request *req,
839 struct security_descriptor *sd,
841 const struct GUID *oc_guid,
844 int ret = LDB_SUCCESS;
845 unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
846 struct ldb_message_element *el;
847 struct ldb_message *msg;
848 const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
849 "unicodePwd", "dBCSPwd", NULL }, **l;
850 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
852 msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
854 return ldb_module_oom(module);
856 for (l = passwordAttrs; *l != NULL; l++) {
857 if ((!userPassword) && (ldb_attr_cmp(*l, "userPassword") == 0)) {
861 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
862 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
865 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
868 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
871 ldb_msg_remove_element(msg, el);
875 /* single deletes will be handled by the "password_hash" LDB module
876 * later in the stack, so we let it though here */
877 if ((del_attr_cnt > 0) && (add_attr_cnt == 0) && (rep_attr_cnt == 0)) {
878 talloc_free(tmp_ctx);
882 if (ldb_request_get_control(req,
883 DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
884 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
885 * have a user password change and not a set as the message
886 * looks like. In it's value blob it contains the NT and/or LM
887 * hash of the old password specified by the user.
888 * This control is used by the SAMR and "kpasswd" password
889 * change mechanisms. */
890 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
891 GUID_DRS_USER_CHANGE_PASSWORD,
892 SEC_ADS_CONTROL_ACCESS,
895 else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
896 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
897 GUID_DRS_FORCE_CHANGE_PASSWORD,
898 SEC_ADS_CONTROL_ACCESS,
901 else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
902 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
903 GUID_DRS_USER_CHANGE_PASSWORD,
904 SEC_ADS_CONTROL_ACCESS,
906 /* Very strange, but we get constraint violation in this case */
907 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
908 ret = LDB_ERR_CONSTRAINT_VIOLATION;
911 if (ret != LDB_SUCCESS) {
912 dsdb_acl_debug(sd, acl_user_token(module),
913 req->op.mod.message->dn,
917 talloc_free(tmp_ctx);
921 static const struct GUID *get_oc_guid_from_message(const struct dsdb_schema *schema,
922 struct ldb_message *msg)
924 struct ldb_message_element *oc_el;
925 const struct dsdb_class *object_class;
927 oc_el = ldb_msg_find_element(msg, "objectClass");
932 object_class = dsdb_get_last_structural_class(schema, oc_el);
933 if (object_class == NULL) {
937 return &object_class->schemaIDGUID;
941 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
944 struct ldb_context *ldb = ldb_module_get_ctx(module);
945 const struct dsdb_schema *schema;
947 const struct GUID *guid;
948 uint32_t access_granted;
949 struct object_tree *root = NULL;
950 struct object_tree *new_node = NULL;
952 struct ldb_result *acl_res;
953 struct security_descriptor *sd;
954 struct dom_sid *sid = NULL;
955 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
956 bool userPassword = dsdb_user_password_support(module, req, req);
957 TALLOC_CTX *tmp_ctx = talloc_new(req);
958 static const char *acl_attrs[] = {
959 "nTSecurityDescriptor",
965 if (as_system != NULL) {
966 as_system->critical = 0;
969 /* Don't print this debug statement if elements[0].name is going to be NULL */
970 if(req->op.mod.message->num_elements > 0)
972 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->elements[0].name));
974 if (dsdb_module_am_system(module) || as_system) {
975 return ldb_next_request(module, req);
977 if (ldb_dn_is_special(req->op.mod.message->dn)) {
978 return ldb_next_request(module, req);
980 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, req->op.mod.message->dn,
982 DSDB_FLAG_NEXT_MODULE |
983 DSDB_FLAG_AS_SYSTEM |
984 DSDB_SEARCH_SHOW_RECYCLED,
987 if (ret != LDB_SUCCESS) {
991 schema = dsdb_get_schema(ldb, tmp_ctx);
993 ret = LDB_ERR_OPERATIONS_ERROR;
997 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
998 if (ret != LDB_SUCCESS) {
999 talloc_free(tmp_ctx);
1000 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1001 "acl_modify: Error retrieving security descriptor.");
1003 /* Theoretically we pass the check if the object has no sd */
1008 guid = get_oc_guid_from_message(schema, acl_res->msgs[0]);
1010 talloc_free(tmp_ctx);
1011 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1012 "acl_modify: Error retrieving object class GUID.");
1014 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1015 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1016 &root, &new_node)) {
1017 talloc_free(tmp_ctx);
1018 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1019 "acl_modify: Error adding new node in object tree.");
1021 for (i=0; i < req->op.mod.message->num_elements; i++){
1022 const struct dsdb_attribute *attr;
1023 attr = dsdb_attribute_by_lDAPDisplayName(schema,
1024 req->op.mod.message->elements[i].name);
1026 if (ldb_attr_cmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
1027 uint32_t sd_flags = dsdb_request_sd_flags(req, NULL);
1028 uint32_t access_mask = 0;
1030 if (sd_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
1031 access_mask |= SEC_STD_WRITE_OWNER;
1033 if (sd_flags & SECINFO_DACL) {
1034 access_mask |= SEC_STD_WRITE_DAC;
1036 if (sd_flags & SECINFO_SACL) {
1037 access_mask |= SEC_FLAG_SYSTEM_SECURITY;
1040 status = sec_access_check_ds(sd, acl_user_token(module),
1046 if (!NT_STATUS_IS_OK(status)) {
1047 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1048 "Object %s has no write dacl access\n",
1049 ldb_dn_get_linearized(req->op.mod.message->dn));
1051 acl_user_token(module),
1052 req->op.mod.message->dn,
1055 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1059 else if (ldb_attr_cmp("member", req->op.mod.message->elements[i].name) == 0) {
1060 ret = acl_check_self_membership(tmp_ctx,
1067 if (ret != LDB_SUCCESS) {
1071 else if (ldb_attr_cmp("dBCSPwd", req->op.mod.message->elements[i].name) == 0) {
1072 /* this one is not affected by any rights, we should let it through
1073 so that passwords_hash returns the correct error */
1076 else if (ldb_attr_cmp("unicodePwd", req->op.mod.message->elements[i].name) == 0 ||
1077 (userPassword && ldb_attr_cmp("userPassword", req->op.mod.message->elements[i].name) == 0) ||
1078 ldb_attr_cmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
1079 ret = acl_check_password_rights(tmp_ctx,
1086 if (ret != LDB_SUCCESS) {
1089 } else if (ldb_attr_cmp("servicePrincipalName", req->op.mod.message->elements[i].name) == 0) {
1090 ret = acl_check_spn(tmp_ctx,
1097 if (ret != LDB_SUCCESS) {
1102 /* This basic attribute existence check with the right errorcode
1103 * is needed since this module is the first one which requests
1104 * schema attribute information.
1105 * The complete attribute checking is done in the
1106 * "objectclass_attrs" module behind this one.
1109 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' on entry '%s' was not found in the schema!",
1110 req->op.mod.message->elements[i].name,
1111 ldb_dn_get_linearized(req->op.mod.message->dn));
1112 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
1115 if (!insert_in_object_tree(tmp_ctx,
1116 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
1117 &new_node, &new_node)) {
1118 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1119 "acl_modify: cannot add to object tree securityGUID\n");
1120 ret = LDB_ERR_OPERATIONS_ERROR;
1124 if (!insert_in_object_tree(tmp_ctx,
1125 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
1126 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1127 "acl_modify: cannot add to object tree attributeGUID\n");
1128 ret = LDB_ERR_OPERATIONS_ERROR;
1134 if (root->num_of_children > 0) {
1135 status = sec_access_check_ds(sd, acl_user_token(module),
1141 if (!NT_STATUS_IS_OK(status)) {
1142 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1143 "Object %s has no write property access\n",
1144 ldb_dn_get_linearized(req->op.mod.message->dn));
1146 acl_user_token(module),
1147 req->op.mod.message->dn,
1150 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1156 talloc_free(tmp_ctx);
1157 return ldb_next_request(module, req);
1159 talloc_free(tmp_ctx);
1163 /* similar to the modify for the time being.
1164 * We need to consider the special delete tree case, though - TODO */
1165 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
1168 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.del.dn);
1169 struct ldb_context *ldb;
1170 struct ldb_dn *nc_root;
1171 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1173 if (as_system != NULL) {
1174 as_system->critical = 0;
1177 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
1178 if (dsdb_module_am_system(module) || as_system) {
1179 return ldb_next_request(module, req);
1181 if (ldb_dn_is_special(req->op.del.dn)) {
1182 return ldb_next_request(module, req);
1185 ldb = ldb_module_get_ctx(module);
1187 /* Make sure we aren't deleting a NC */
1189 ret = dsdb_find_nc_root(ldb, req, req->op.del.dn, &nc_root);
1190 if (ret != LDB_SUCCESS) {
1193 if (ldb_dn_compare(nc_root, req->op.del.dn) == 0) {
1194 talloc_free(nc_root);
1195 DEBUG(10,("acl:deleting a NC\n"));
1196 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1197 return ldb_module_done(req, NULL, NULL,
1198 LDB_ERR_UNWILLING_TO_PERFORM);
1200 talloc_free(nc_root);
1202 /* First check if we have delete object right */
1203 ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn,
1204 SEC_STD_DELETE, NULL, req);
1205 if (ret == LDB_SUCCESS) {
1206 return ldb_next_request(module, req);
1209 /* Nope, we don't have delete object. Lets check if we have delete
1210 * child on the parent */
1211 ret = dsdb_module_check_access_on_dn(module, req, parent,
1212 SEC_ADS_DELETE_CHILD, NULL, req);
1213 if (ret != LDB_SUCCESS) {
1217 return ldb_next_request(module, req);
1220 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
1223 struct ldb_dn *oldparent = ldb_dn_get_parent(req, req->op.rename.olddn);
1224 struct ldb_dn *newparent = ldb_dn_get_parent(req, req->op.rename.newdn);
1225 const struct dsdb_schema *schema;
1226 struct ldb_context *ldb;
1227 struct security_descriptor *sd = NULL;
1228 struct dom_sid *sid = NULL;
1229 struct ldb_result *acl_res;
1230 const struct GUID *guid;
1231 struct ldb_dn *nc_root;
1232 struct object_tree *root = NULL;
1233 struct object_tree *new_node = NULL;
1234 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1235 TALLOC_CTX *tmp_ctx = talloc_new(req);
1237 uint32_t access_granted;
1238 const char *rdn_name;
1239 static const char *acl_attrs[] = {
1240 "nTSecurityDescriptor",
1246 if (as_system != NULL) {
1247 as_system->critical = 0;
1250 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
1251 if (dsdb_module_am_system(module) || as_system) {
1252 return ldb_next_request(module, req);
1254 if (ldb_dn_is_special(req->op.rename.olddn)) {
1255 return ldb_next_request(module, req);
1258 ldb = ldb_module_get_ctx(module);
1260 /* Make sure we aren't renaming/moving a NC */
1262 ret = dsdb_find_nc_root(ldb, req, req->op.rename.olddn, &nc_root);
1263 if (ret != LDB_SUCCESS) {
1266 if (ldb_dn_compare(nc_root, req->op.rename.olddn) == 0) {
1267 talloc_free(nc_root);
1268 DEBUG(10,("acl:renaming/moving a NC\n"));
1269 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1270 return ldb_module_done(req, NULL, NULL,
1271 LDB_ERR_UNWILLING_TO_PERFORM);
1273 talloc_free(nc_root);
1275 /* Look for the parent */
1277 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res,
1278 req->op.rename.olddn, acl_attrs,
1279 DSDB_FLAG_NEXT_MODULE |
1280 DSDB_FLAG_AS_SYSTEM |
1281 DSDB_SEARCH_SHOW_RECYCLED, req);
1282 /* we sould be able to find the parent */
1283 if (ret != LDB_SUCCESS) {
1284 DEBUG(10,("acl: failed to find object %s\n",
1285 ldb_dn_get_linearized(req->op.rename.olddn)));
1286 talloc_free(tmp_ctx);
1290 schema = dsdb_get_schema(ldb, acl_res);
1292 talloc_free(tmp_ctx);
1293 return ldb_operr(ldb);
1296 guid = get_oc_guid_from_message(schema, acl_res->msgs[0]);
1297 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1298 &root, &new_node)) {
1299 talloc_free(tmp_ctx);
1300 return ldb_operr(ldb);
1303 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1305 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1306 &new_node, &new_node)) {
1307 talloc_free(tmp_ctx);
1308 return ldb_operr(ldb);
1311 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1312 if (rdn_name == NULL) {
1313 talloc_free(tmp_ctx);
1314 return ldb_operr(ldb);
1316 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1318 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1319 &new_node, &new_node)) {
1320 talloc_free(tmp_ctx);
1321 return ldb_operr(ldb);
1324 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1326 if (ret != LDB_SUCCESS) {
1327 talloc_free(tmp_ctx);
1328 return ldb_operr(ldb);
1330 /* Theoretically we pass the check if the object has no sd */
1332 talloc_free(tmp_ctx);
1335 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1336 status = sec_access_check_ds(sd, acl_user_token(module),
1342 if (!NT_STATUS_IS_OK(status)) {
1343 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1344 "Object %s has no wp on name\n",
1345 ldb_dn_get_linearized(req->op.rename.olddn));
1347 acl_user_token(module),
1348 req->op.rename.olddn,
1351 talloc_free(tmp_ctx);
1352 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1355 if (ldb_dn_compare(oldparent, newparent) == 0) {
1356 /* regular rename, not move, nothing more to do */
1357 talloc_free(tmp_ctx);
1358 return ldb_next_request(module, req);
1361 /* new parent should have create child */
1364 guid = get_oc_guid_from_message(schema, acl_res->msgs[0]);
1366 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1367 "acl:renamed object has no object class\n");
1368 talloc_free(tmp_ctx);
1369 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
1372 ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid, req);
1373 if (ret != LDB_SUCCESS) {
1374 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1375 "acl:access_denied renaming %s",
1376 ldb_dn_get_linearized(req->op.rename.olddn));
1377 talloc_free(tmp_ctx);
1380 /* do we have delete object on the object? */
1382 status = sec_access_check_ds(sd, acl_user_token(module),
1388 if (NT_STATUS_IS_OK(status)) {
1389 talloc_free(tmp_ctx);
1390 return ldb_next_request(module, req);
1392 /* what about delete child on the current parent */
1393 ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL, req);
1394 if (ret != LDB_SUCCESS) {
1395 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1396 "acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn));
1397 talloc_free(tmp_ctx);
1398 return ldb_module_done(req, NULL, NULL, ret);
1401 talloc_free(tmp_ctx);
1403 return ldb_next_request(module, req);
1406 static int acl_search_update_confidential_attrs(struct acl_context *ac,
1407 struct acl_private *data)
1409 struct dsdb_attribute *a;
1412 if (data->acl_search) {
1414 * if acl:search is activated we
1415 * the acl_read module protects
1416 * confidential attributes.
1421 if ((ac->schema == data->cached_schema_ptr) &&
1422 (ac->schema->loaded_usn == data->cached_schema_loaded_usn) &&
1423 (ac->schema->metadata_usn == data->cached_schema_metadata_usn))
1428 data->cached_schema_ptr = NULL;
1429 data->cached_schema_loaded_usn = 0;
1430 data->cached_schema_metadata_usn = 0;
1431 TALLOC_FREE(data->confidential_attrs);
1433 if (ac->schema == NULL) {
1437 for (a = ac->schema->attributes; a; a = a->next) {
1438 const char **attrs = data->confidential_attrs;
1440 if (!(a->searchFlags & SEARCH_FLAG_CONFIDENTIAL)) {
1444 attrs = talloc_realloc(data, attrs, const char *, n + 2);
1445 if (attrs == NULL) {
1446 TALLOC_FREE(data->confidential_attrs);
1447 return ldb_module_oom(ac->module);
1450 attrs[n] = a->lDAPDisplayName;
1454 data->confidential_attrs = attrs;
1457 data->cached_schema_ptr = ac->schema;
1458 data->cached_schema_loaded_usn = ac->schema->loaded_usn;
1459 data->cached_schema_metadata_usn = ac->schema->metadata_usn;
1464 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1466 struct acl_context *ac;
1467 struct acl_private *data;
1468 struct ldb_result *acl_res;
1469 static const char *acl_attrs[] = {
1471 "nTSecurityDescriptor",
1478 ac = talloc_get_type(req->context, struct acl_context);
1479 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1481 return ldb_module_done(ac->req, NULL, NULL,
1482 LDB_ERR_OPERATIONS_ERROR);
1484 if (ares->error != LDB_SUCCESS) {
1485 return ldb_module_done(ac->req, ares->controls,
1486 ares->response, ares->error);
1489 switch (ares->type) {
1490 case LDB_REPLY_ENTRY:
1491 if (ac->constructed_attrs) {
1492 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1494 DSDB_FLAG_NEXT_MODULE |
1495 DSDB_FLAG_AS_SYSTEM |
1496 DSDB_SEARCH_SHOW_RECYCLED,
1498 if (ret != LDB_SUCCESS) {
1499 return ldb_module_done(ac->req, NULL, NULL, ret);
1503 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1504 ret = acl_allowedAttributes(ac->module, ac->schema,
1507 if (ret != LDB_SUCCESS) {
1508 return ldb_module_done(ac->req, NULL, NULL, ret);
1512 if (ac->allowedChildClasses) {
1513 ret = acl_childClasses(ac->module, ac->schema,
1516 "allowedChildClasses");
1517 if (ret != LDB_SUCCESS) {
1518 return ldb_module_done(ac->req, NULL, NULL, ret);
1522 if (ac->allowedChildClassesEffective) {
1523 ret = acl_childClassesEffective(ac->module, ac->schema,
1526 if (ret != LDB_SUCCESS) {
1527 return ldb_module_done(ac->req, NULL, NULL, ret);
1531 if (ac->sDRightsEffective) {
1532 ret = acl_sDRightsEffective(ac->module,
1535 if (ret != LDB_SUCCESS) {
1536 return ldb_module_done(ac->req, NULL, NULL, ret);
1541 return ldb_module_send_entry(ac->req, ares->message,
1545 if (ac->am_system) {
1546 return ldb_module_send_entry(ac->req, ares->message,
1550 if (data->password_attrs != NULL) {
1551 for (i = 0; data->password_attrs[i]; i++) {
1552 if ((!ac->userPassword) &&
1553 (ldb_attr_cmp(data->password_attrs[i],
1554 "userPassword") == 0))
1559 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1563 if (ac->am_administrator) {
1564 return ldb_module_send_entry(ac->req, ares->message,
1568 ret = acl_search_update_confidential_attrs(ac, data);
1569 if (ret != LDB_SUCCESS) {
1573 if (data->confidential_attrs != NULL) {
1574 for (i = 0; data->confidential_attrs[i]; i++) {
1575 ldb_msg_remove_attr(ares->message,
1576 data->confidential_attrs[i]);
1580 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1582 case LDB_REPLY_REFERRAL:
1583 return ldb_module_send_referral(ac->req, ares->referral);
1585 case LDB_REPLY_DONE:
1586 return ldb_module_done(ac->req, ares->controls,
1587 ares->response, LDB_SUCCESS);
1593 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1595 struct ldb_context *ldb;
1596 struct acl_context *ac;
1597 struct ldb_parse_tree *down_tree;
1598 struct ldb_request *down_req;
1599 struct acl_private *data;
1603 ldb = ldb_module_get_ctx(module);
1605 ac = talloc_zero(req, struct acl_context);
1607 return ldb_oom(ldb);
1609 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1611 ac->module = module;
1613 ac->am_system = dsdb_module_am_system(module);
1614 ac->am_administrator = dsdb_module_am_administrator(module);
1615 ac->constructed_attrs = false;
1616 ac->modify_search = true;
1617 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1618 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1619 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1620 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1621 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1622 ac->userPassword = dsdb_user_password_support(module, ac, req);
1623 ac->schema = dsdb_get_schema(ldb, ac);
1625 ac->constructed_attrs |= ac->allowedAttributes;
1626 ac->constructed_attrs |= ac->allowedChildClasses;
1627 ac->constructed_attrs |= ac->allowedChildClassesEffective;
1628 ac->constructed_attrs |= ac->allowedAttributesEffective;
1629 ac->constructed_attrs |= ac->sDRightsEffective;
1632 ac->modify_search = false;
1634 if (ac->am_system) {
1635 ac->modify_search = false;
1638 if (!ac->constructed_attrs && !ac->modify_search) {
1639 return ldb_next_request(module, req);
1642 ret = acl_search_update_confidential_attrs(ac, data);
1643 if (ret != LDB_SUCCESS) {
1647 down_tree = ldb_parse_tree_copy_shallow(ac, req->op.search.tree);
1648 if (down_tree == NULL) {
1649 return ldb_oom(ldb);
1652 if (!ac->am_system && data->password_attrs) {
1653 for (i = 0; data->password_attrs[i]; i++) {
1654 if ((!ac->userPassword) &&
1655 (ldb_attr_cmp(data->password_attrs[i],
1656 "userPassword") == 0))
1661 ldb_parse_tree_attr_replace(down_tree,
1662 data->password_attrs[i],
1663 "kludgeACLredactedattribute");
1667 if (!ac->am_system && !ac->am_administrator && data->confidential_attrs) {
1668 for (i = 0; data->confidential_attrs[i]; i++) {
1669 ldb_parse_tree_attr_replace(down_tree,
1670 data->confidential_attrs[i],
1671 "kludgeACLredactedattribute");
1675 ret = ldb_build_search_req_ex(&down_req,
1677 req->op.search.base,
1678 req->op.search.scope,
1680 req->op.search.attrs,
1682 ac, acl_search_callback,
1684 LDB_REQ_SET_LOCATION(down_req);
1685 if (ret != LDB_SUCCESS) {
1688 /* perform the search */
1689 return ldb_next_request(module, down_req);
1692 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1694 struct ldb_context *ldb = ldb_module_get_ctx(module);
1695 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1697 /* allow everybody to read the sequence number */
1698 if (strcmp(req->op.extended.oid,
1699 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1700 return ldb_next_request(module, req);
1703 if (dsdb_module_am_system(module) ||
1704 dsdb_module_am_administrator(module) || as_system) {
1705 return ldb_next_request(module, req);
1707 ldb_asprintf_errstring(ldb,
1709 "attempted database modify not permitted. "
1710 "User %s is not SYSTEM or an administrator",
1711 acl_user_name(req, module));
1712 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1716 static const struct ldb_module_ops ldb_acl_module_ops = {
1718 .search = acl_search,
1720 .modify = acl_modify,
1722 .rename = acl_rename,
1723 .extended = acl_extended,
1724 .init_context = acl_module_init
1727 int ldb_acl_module_init(const char *version)
1729 LDB_MODULE_CHECK_VERSION(version);
1730 return ldb_register_module(&ldb_acl_module_ops);