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_perform = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"),
104 NULL, "acl", "perform", 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_DELETED, req);
659 if (ret != LDB_SUCCESS) {
660 talloc_free(tmp_ctx);
664 userAccountControl = ldb_msg_find_attr_as_uint(acl_res->msgs[0], "userAccountControl", 0);
665 dnsHostName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "dnsHostName", NULL);
666 samAccountName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "samAccountName", NULL);
668 ret = dsdb_module_search(module, tmp_ctx,
669 &netbios_res, partitions_dn,
672 DSDB_FLAG_NEXT_MODULE |
676 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)));
678 netbios_name = ldb_msg_find_attr_as_string(netbios_res->msgs[0], "nETBIOSName", NULL);
680 el = ldb_msg_find_element(req->op.mod.message, "servicePrincipalName");
682 talloc_free(tmp_ctx);
683 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
684 "Error finding element for servicePrincipalName.");
687 /* NTDSDSA objectGuid of object we are checking SPN for */
688 if (userAccountControl & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
689 ret = dsdb_module_find_ntdsguid_for_computer(module, tmp_ctx,
690 req->op.mod.message->dn, &ntds, req);
691 if (ret != LDB_SUCCESS) {
692 ldb_asprintf_errstring(ldb, "Failed to find NTDSDSA objectGuid for %s: %s",
693 ldb_dn_get_linearized(req->op.mod.message->dn),
695 talloc_free(tmp_ctx);
696 return LDB_ERR_OPERATIONS_ERROR;
698 ntds_guid = GUID_string(tmp_ctx, &ntds);
701 for (i=0; i < el->num_values; i++) {
702 ret = acl_validate_spn_value(tmp_ctx,
704 (char *)el->values[i].data,
710 if (ret != LDB_SUCCESS) {
711 talloc_free(tmp_ctx);
715 talloc_free(tmp_ctx);
719 static int acl_add(struct ldb_module *module, struct ldb_request *req)
722 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.add.message->dn);
723 struct ldb_context *ldb;
724 const struct dsdb_schema *schema;
725 struct ldb_message_element *oc_el;
726 const struct GUID *guid;
727 struct ldb_dn *nc_root;
728 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
730 if (as_system != NULL) {
731 as_system->critical = 0;
734 if (dsdb_module_am_system(module) || as_system) {
735 return ldb_next_request(module, req);
737 if (ldb_dn_is_special(req->op.add.message->dn)) {
738 return ldb_next_request(module, req);
741 ldb = ldb_module_get_ctx(module);
743 /* Creating an NC. There is probably something we should do here,
744 * but we will establish that later */
746 ret = dsdb_find_nc_root(ldb, req, req->op.add.message->dn, &nc_root);
747 if (ret != LDB_SUCCESS) {
750 if (ldb_dn_compare(nc_root, req->op.add.message->dn) == 0) {
751 talloc_free(nc_root);
752 return ldb_next_request(module, req);
754 talloc_free(nc_root);
756 schema = dsdb_get_schema(ldb, req);
758 return ldb_operr(ldb);
761 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
762 if (!oc_el || oc_el->num_values == 0) {
763 ldb_asprintf_errstring(ldb_module_get_ctx(module),
764 "acl: unable to find objectClass on %s\n",
765 ldb_dn_get_linearized(req->op.add.message->dn));
766 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
769 guid = class_schemaid_guid_by_lDAPDisplayName(schema,
770 (char *)oc_el->values[oc_el->num_values-1].data);
771 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid, req);
772 if (ret != LDB_SUCCESS) {
775 return ldb_next_request(module, req);
778 /* ckecks if modifications are allowed on "Member" attribute */
779 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
780 struct ldb_module *module,
781 struct ldb_request *req,
782 struct security_descriptor *sd,
784 const struct GUID *oc_guid,
785 const struct dsdb_attribute *attr)
789 struct ldb_context *ldb = ldb_module_get_ctx(module);
790 struct ldb_dn *user_dn;
791 struct ldb_message_element *member_el;
792 /* if we have wp, we can do whatever we like */
793 if (acl_check_access_on_attribute(module,
798 attr) == LDB_SUCCESS) {
801 /* if we are adding/deleting ourselves, check for self membership */
802 ret = dsdb_find_dn_by_sid(ldb, mem_ctx,
803 &acl_user_token(module)->sids[PRIMARY_USER_SID_INDEX],
805 if (ret != LDB_SUCCESS) {
808 member_el = ldb_msg_find_element(req->op.mod.message, "member");
810 return ldb_operr(ldb);
812 /* user can only remove oneself */
813 if (member_el->num_values == 0) {
814 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
816 for (i = 0; i < member_el->num_values; i++) {
817 if (strcasecmp((const char *)member_el->values[i].data,
818 ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
819 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
822 ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
823 GUID_DRS_SELF_MEMBERSHIP,
826 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
827 dsdb_acl_debug(sd, acl_user_token(module),
828 req->op.mod.message->dn,
835 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
836 struct ldb_module *module,
837 struct ldb_request *req,
838 struct security_descriptor *sd,
840 const struct GUID *oc_guid,
843 int ret = LDB_SUCCESS;
844 unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
845 struct ldb_message_element *el;
846 struct ldb_message *msg;
847 const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
848 "unicodePwd", "dBCSPwd", NULL }, **l;
849 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
851 msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
853 return ldb_module_oom(module);
855 for (l = passwordAttrs; *l != NULL; l++) {
856 if ((!userPassword) && (ldb_attr_cmp(*l, "userPassword") == 0)) {
860 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
861 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
864 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
867 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
870 ldb_msg_remove_element(msg, el);
874 /* single deletes will be handled by the "password_hash" LDB module
875 * later in the stack, so we let it though here */
876 if ((del_attr_cnt > 0) && (add_attr_cnt == 0) && (rep_attr_cnt == 0)) {
877 talloc_free(tmp_ctx);
881 if (ldb_request_get_control(req,
882 DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
883 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
884 * have a user password change and not a set as the message
885 * looks like. In it's value blob it contains the NT and/or LM
886 * hash of the old password specified by the user.
887 * This control is used by the SAMR and "kpasswd" password
888 * change mechanisms. */
889 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
890 GUID_DRS_USER_CHANGE_PASSWORD,
891 SEC_ADS_CONTROL_ACCESS,
894 else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
895 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
896 GUID_DRS_FORCE_CHANGE_PASSWORD,
897 SEC_ADS_CONTROL_ACCESS,
900 else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
901 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
902 GUID_DRS_USER_CHANGE_PASSWORD,
903 SEC_ADS_CONTROL_ACCESS,
905 /* Very strange, but we get constraint violation in this case */
906 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
907 ret = LDB_ERR_CONSTRAINT_VIOLATION;
910 if (ret != LDB_SUCCESS) {
911 dsdb_acl_debug(sd, acl_user_token(module),
912 req->op.mod.message->dn,
916 talloc_free(tmp_ctx);
920 static const struct GUID *get_oc_guid_from_message(const struct dsdb_schema *schema,
921 struct ldb_message *msg)
923 struct ldb_message_element *oc_el;
924 const struct dsdb_class *object_class;
926 oc_el = ldb_msg_find_element(msg, "objectClass");
931 object_class = dsdb_get_last_structural_class(schema, oc_el);
932 if (object_class == NULL) {
936 return &object_class->schemaIDGUID;
940 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
943 struct ldb_context *ldb = ldb_module_get_ctx(module);
944 const struct dsdb_schema *schema;
946 const struct GUID *guid;
947 uint32_t access_granted;
948 struct object_tree *root = NULL;
949 struct object_tree *new_node = NULL;
951 struct ldb_result *acl_res;
952 struct security_descriptor *sd;
953 struct dom_sid *sid = NULL;
954 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
955 bool userPassword = dsdb_user_password_support(module, req, req);
956 TALLOC_CTX *tmp_ctx = talloc_new(req);
957 static const char *acl_attrs[] = {
958 "nTSecurityDescriptor",
964 if (as_system != NULL) {
965 as_system->critical = 0;
968 /* Don't print this debug statement if elements[0].name is going to be NULL */
969 if(req->op.mod.message->num_elements > 0)
971 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->elements[0].name));
973 if (dsdb_module_am_system(module) || as_system) {
974 return ldb_next_request(module, req);
976 if (ldb_dn_is_special(req->op.mod.message->dn)) {
977 return ldb_next_request(module, req);
979 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, req->op.mod.message->dn,
981 DSDB_FLAG_NEXT_MODULE |
982 DSDB_FLAG_AS_SYSTEM |
983 DSDB_SEARCH_SHOW_DELETED,
986 if (ret != LDB_SUCCESS) {
990 schema = dsdb_get_schema(ldb, tmp_ctx);
992 ret = LDB_ERR_OPERATIONS_ERROR;
996 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
997 if (ret != LDB_SUCCESS) {
998 talloc_free(tmp_ctx);
999 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1000 "acl_modify: Error retrieving security descriptor.");
1002 /* Theoretically we pass the check if the object has no sd */
1007 guid = get_oc_guid_from_message(schema, acl_res->msgs[0]);
1009 talloc_free(tmp_ctx);
1010 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1011 "acl_modify: Error retrieving object class GUID.");
1013 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1014 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1015 &root, &new_node)) {
1016 talloc_free(tmp_ctx);
1017 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1018 "acl_modify: Error adding new node in object tree.");
1020 for (i=0; i < req->op.mod.message->num_elements; i++){
1021 const struct dsdb_attribute *attr;
1022 attr = dsdb_attribute_by_lDAPDisplayName(schema,
1023 req->op.mod.message->elements[i].name);
1025 if (ldb_attr_cmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
1026 status = sec_access_check_ds(sd, acl_user_token(module),
1032 if (!NT_STATUS_IS_OK(status)) {
1033 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1034 "Object %s has no write dacl access\n",
1035 ldb_dn_get_linearized(req->op.mod.message->dn));
1037 acl_user_token(module),
1038 req->op.mod.message->dn,
1041 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1045 else if (ldb_attr_cmp("member", req->op.mod.message->elements[i].name) == 0) {
1046 ret = acl_check_self_membership(tmp_ctx,
1053 if (ret != LDB_SUCCESS) {
1057 else if (ldb_attr_cmp("dBCSPwd", req->op.mod.message->elements[i].name) == 0) {
1058 /* this one is not affected by any rights, we should let it through
1059 so that passwords_hash returns the correct error */
1062 else if (ldb_attr_cmp("unicodePwd", req->op.mod.message->elements[i].name) == 0 ||
1063 (userPassword && ldb_attr_cmp("userPassword", req->op.mod.message->elements[i].name) == 0) ||
1064 ldb_attr_cmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
1065 ret = acl_check_password_rights(tmp_ctx,
1072 if (ret != LDB_SUCCESS) {
1075 } else if (ldb_attr_cmp("servicePrincipalName", req->op.mod.message->elements[i].name) == 0) {
1076 ret = acl_check_spn(tmp_ctx,
1083 if (ret != LDB_SUCCESS) {
1088 /* This basic attribute existence check with the right errorcode
1089 * is needed since this module is the first one which requests
1090 * schema attribute information.
1091 * The complete attribute checking is done in the
1092 * "objectclass_attrs" module behind this one.
1095 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' on entry '%s' was not found in the schema!",
1096 req->op.mod.message->elements[i].name,
1097 ldb_dn_get_linearized(req->op.mod.message->dn));
1098 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
1101 if (!insert_in_object_tree(tmp_ctx,
1102 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
1103 &new_node, &new_node)) {
1104 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1105 "acl_modify: cannot add to object tree securityGUID\n");
1106 ret = LDB_ERR_OPERATIONS_ERROR;
1110 if (!insert_in_object_tree(tmp_ctx,
1111 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
1112 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1113 "acl_modify: cannot add to object tree attributeGUID\n");
1114 ret = LDB_ERR_OPERATIONS_ERROR;
1120 if (root->num_of_children > 0) {
1121 status = sec_access_check_ds(sd, acl_user_token(module),
1127 if (!NT_STATUS_IS_OK(status)) {
1128 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1129 "Object %s has no write property access\n",
1130 ldb_dn_get_linearized(req->op.mod.message->dn));
1132 acl_user_token(module),
1133 req->op.mod.message->dn,
1136 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1142 talloc_free(tmp_ctx);
1143 return ldb_next_request(module, req);
1145 talloc_free(tmp_ctx);
1149 /* similar to the modify for the time being.
1150 * We need to consider the special delete tree case, though - TODO */
1151 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
1154 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.del.dn);
1155 struct ldb_context *ldb;
1156 struct ldb_dn *nc_root;
1157 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1159 if (as_system != NULL) {
1160 as_system->critical = 0;
1163 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
1164 if (dsdb_module_am_system(module) || as_system) {
1165 return ldb_next_request(module, req);
1167 if (ldb_dn_is_special(req->op.del.dn)) {
1168 return ldb_next_request(module, req);
1171 ldb = ldb_module_get_ctx(module);
1173 /* Make sure we aren't deleting a NC */
1175 ret = dsdb_find_nc_root(ldb, req, req->op.del.dn, &nc_root);
1176 if (ret != LDB_SUCCESS) {
1179 if (ldb_dn_compare(nc_root, req->op.del.dn) == 0) {
1180 talloc_free(nc_root);
1181 DEBUG(10,("acl:deleting a NC\n"));
1182 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1183 return ldb_module_done(req, NULL, NULL,
1184 LDB_ERR_UNWILLING_TO_PERFORM);
1186 talloc_free(nc_root);
1188 /* First check if we have delete object right */
1189 ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn,
1190 SEC_STD_DELETE, NULL, req);
1191 if (ret == LDB_SUCCESS) {
1192 return ldb_next_request(module, req);
1195 /* Nope, we don't have delete object. Lets check if we have delete
1196 * child on the parent */
1197 ret = dsdb_module_check_access_on_dn(module, req, parent,
1198 SEC_ADS_DELETE_CHILD, NULL, req);
1199 if (ret != LDB_SUCCESS) {
1203 return ldb_next_request(module, req);
1206 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
1209 struct ldb_dn *oldparent = ldb_dn_get_parent(req, req->op.rename.olddn);
1210 struct ldb_dn *newparent = ldb_dn_get_parent(req, req->op.rename.newdn);
1211 const struct dsdb_schema *schema;
1212 struct ldb_context *ldb;
1213 struct security_descriptor *sd = NULL;
1214 struct dom_sid *sid = NULL;
1215 struct ldb_result *acl_res;
1216 const struct GUID *guid;
1217 struct ldb_dn *nc_root;
1218 struct object_tree *root = NULL;
1219 struct object_tree *new_node = NULL;
1220 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1221 TALLOC_CTX *tmp_ctx = talloc_new(req);
1223 uint32_t access_granted;
1224 const char *rdn_name;
1225 static const char *acl_attrs[] = {
1226 "nTSecurityDescriptor",
1232 if (as_system != NULL) {
1233 as_system->critical = 0;
1236 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
1237 if (dsdb_module_am_system(module) || as_system) {
1238 return ldb_next_request(module, req);
1240 if (ldb_dn_is_special(req->op.rename.olddn)) {
1241 return ldb_next_request(module, req);
1244 ldb = ldb_module_get_ctx(module);
1246 /* Make sure we aren't renaming/moving a NC */
1248 ret = dsdb_find_nc_root(ldb, req, req->op.rename.olddn, &nc_root);
1249 if (ret != LDB_SUCCESS) {
1252 if (ldb_dn_compare(nc_root, req->op.rename.olddn) == 0) {
1253 talloc_free(nc_root);
1254 DEBUG(10,("acl:renaming/moving a NC\n"));
1255 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1256 return ldb_module_done(req, NULL, NULL,
1257 LDB_ERR_UNWILLING_TO_PERFORM);
1259 talloc_free(nc_root);
1261 /* Look for the parent */
1263 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res,
1264 req->op.rename.olddn, acl_attrs,
1265 DSDB_FLAG_NEXT_MODULE |
1266 DSDB_FLAG_AS_SYSTEM |
1267 DSDB_SEARCH_SHOW_RECYCLED, req);
1268 /* we sould be able to find the parent */
1269 if (ret != LDB_SUCCESS) {
1270 DEBUG(10,("acl: failed to find object %s\n",
1271 ldb_dn_get_linearized(req->op.rename.olddn)));
1272 talloc_free(tmp_ctx);
1276 schema = dsdb_get_schema(ldb, acl_res);
1278 talloc_free(tmp_ctx);
1279 return ldb_operr(ldb);
1282 guid = get_oc_guid_from_message(schema, acl_res->msgs[0]);
1283 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1284 &root, &new_node)) {
1285 talloc_free(tmp_ctx);
1286 return ldb_operr(ldb);
1289 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1291 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1292 &new_node, &new_node)) {
1293 talloc_free(tmp_ctx);
1294 return ldb_operr(ldb);
1297 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1298 if (rdn_name == NULL) {
1299 talloc_free(tmp_ctx);
1300 return ldb_operr(ldb);
1302 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1304 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1305 &new_node, &new_node)) {
1306 talloc_free(tmp_ctx);
1307 return ldb_operr(ldb);
1310 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1312 if (ret != LDB_SUCCESS) {
1313 talloc_free(tmp_ctx);
1314 return ldb_operr(ldb);
1316 /* Theoretically we pass the check if the object has no sd */
1318 talloc_free(tmp_ctx);
1321 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1322 status = sec_access_check_ds(sd, acl_user_token(module),
1328 if (!NT_STATUS_IS_OK(status)) {
1329 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1330 "Object %s has no wp on name\n",
1331 ldb_dn_get_linearized(req->op.rename.olddn));
1333 acl_user_token(module),
1334 req->op.rename.olddn,
1337 talloc_free(tmp_ctx);
1338 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1341 if (ldb_dn_compare(oldparent, newparent) == 0) {
1342 /* regular rename, not move, nothing more to do */
1343 talloc_free(tmp_ctx);
1344 return ldb_next_request(module, req);
1347 /* new parent should have create child */
1350 guid = get_oc_guid_from_message(schema, acl_res->msgs[0]);
1352 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1353 "acl:renamed object has no object class\n");
1354 talloc_free(tmp_ctx);
1355 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
1358 ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid, req);
1359 if (ret != LDB_SUCCESS) {
1360 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1361 "acl:access_denied renaming %s",
1362 ldb_dn_get_linearized(req->op.rename.olddn));
1363 talloc_free(tmp_ctx);
1366 /* do we have delete object on the object? */
1368 status = sec_access_check_ds(sd, acl_user_token(module),
1374 if (NT_STATUS_IS_OK(status)) {
1375 talloc_free(tmp_ctx);
1376 return ldb_next_request(module, req);
1378 /* what about delete child on the current parent */
1379 ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL, req);
1380 if (ret != LDB_SUCCESS) {
1381 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1382 "acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn));
1383 talloc_free(tmp_ctx);
1384 return ldb_module_done(req, NULL, NULL, ret);
1387 talloc_free(tmp_ctx);
1389 return ldb_next_request(module, req);
1392 static int acl_search_update_confidential_attrs(struct acl_context *ac,
1393 struct acl_private *data)
1395 struct dsdb_attribute *a;
1398 if ((ac->schema == data->cached_schema_ptr) &&
1399 (ac->schema->loaded_usn == data->cached_schema_loaded_usn) &&
1400 (ac->schema->metadata_usn == data->cached_schema_metadata_usn))
1405 data->cached_schema_ptr = NULL;
1406 data->cached_schema_loaded_usn = 0;
1407 data->cached_schema_metadata_usn = 0;
1408 TALLOC_FREE(data->confidential_attrs);
1410 if (ac->schema == NULL) {
1414 for (a = ac->schema->attributes; a; a = a->next) {
1415 const char **attrs = data->confidential_attrs;
1417 if (!(a->searchFlags & SEARCH_FLAG_CONFIDENTIAL)) {
1421 attrs = talloc_realloc(data, attrs, const char *, n + 2);
1422 if (attrs == NULL) {
1423 TALLOC_FREE(data->confidential_attrs);
1424 return ldb_module_oom(ac->module);
1427 attrs[n] = a->lDAPDisplayName;
1431 data->confidential_attrs = attrs;
1434 data->cached_schema_ptr = ac->schema;
1435 data->cached_schema_loaded_usn = ac->schema->loaded_usn;
1436 data->cached_schema_metadata_usn = ac->schema->metadata_usn;
1441 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1443 struct acl_context *ac;
1444 struct acl_private *data;
1445 struct ldb_result *acl_res;
1446 static const char *acl_attrs[] = {
1448 "nTSecurityDescriptor",
1455 ac = talloc_get_type(req->context, struct acl_context);
1456 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1458 return ldb_module_done(ac->req, NULL, NULL,
1459 LDB_ERR_OPERATIONS_ERROR);
1461 if (ares->error != LDB_SUCCESS) {
1462 return ldb_module_done(ac->req, ares->controls,
1463 ares->response, ares->error);
1466 switch (ares->type) {
1467 case LDB_REPLY_ENTRY:
1468 if (ac->constructed_attrs) {
1469 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1471 DSDB_FLAG_NEXT_MODULE |
1472 DSDB_FLAG_AS_SYSTEM |
1473 DSDB_SEARCH_SHOW_DELETED, req);
1474 if (ret != LDB_SUCCESS) {
1475 return ldb_module_done(ac->req, NULL, NULL, ret);
1479 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1480 ret = acl_allowedAttributes(ac->module, ac->schema,
1483 if (ret != LDB_SUCCESS) {
1484 return ldb_module_done(ac->req, NULL, NULL, ret);
1488 if (ac->allowedChildClasses) {
1489 ret = acl_childClasses(ac->module, ac->schema,
1492 "allowedChildClasses");
1493 if (ret != LDB_SUCCESS) {
1494 return ldb_module_done(ac->req, NULL, NULL, ret);
1498 if (ac->allowedChildClassesEffective) {
1499 ret = acl_childClassesEffective(ac->module, ac->schema,
1502 if (ret != LDB_SUCCESS) {
1503 return ldb_module_done(ac->req, NULL, NULL, ret);
1507 if (ac->sDRightsEffective) {
1508 ret = acl_sDRightsEffective(ac->module,
1511 if (ret != LDB_SUCCESS) {
1512 return ldb_module_done(ac->req, NULL, NULL, ret);
1517 return ldb_module_send_entry(ac->req, ares->message,
1521 if (ac->am_system) {
1522 return ldb_module_send_entry(ac->req, ares->message,
1526 if (data->password_attrs != NULL) {
1527 for (i = 0; data->password_attrs[i]; i++) {
1528 if ((!ac->userPassword) &&
1529 (ldb_attr_cmp(data->password_attrs[i],
1530 "userPassword") == 0))
1535 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1539 if (ac->am_administrator) {
1540 return ldb_module_send_entry(ac->req, ares->message,
1544 ret = acl_search_update_confidential_attrs(ac, data);
1545 if (ret != LDB_SUCCESS) {
1549 if (data->confidential_attrs != NULL) {
1550 for (i = 0; data->confidential_attrs[i]; i++) {
1551 ldb_msg_remove_attr(ares->message,
1552 data->confidential_attrs[i]);
1556 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1558 case LDB_REPLY_REFERRAL:
1559 return ldb_module_send_referral(ac->req, ares->referral);
1561 case LDB_REPLY_DONE:
1562 return ldb_module_done(ac->req, ares->controls,
1563 ares->response, LDB_SUCCESS);
1569 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1571 struct ldb_context *ldb;
1572 struct acl_context *ac;
1573 struct ldb_parse_tree *down_tree;
1574 struct ldb_request *down_req;
1575 struct acl_private *data;
1579 ldb = ldb_module_get_ctx(module);
1581 ac = talloc_zero(req, struct acl_context);
1583 return ldb_oom(ldb);
1585 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1587 ac->module = module;
1589 ac->am_system = dsdb_module_am_system(module);
1590 ac->am_administrator = dsdb_module_am_administrator(module);
1591 ac->constructed_attrs = false;
1592 ac->modify_search = true;
1593 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1594 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1595 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1596 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1597 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1598 ac->userPassword = dsdb_user_password_support(module, ac, req);
1599 ac->schema = dsdb_get_schema(ldb, ac);
1601 ac->constructed_attrs |= ac->allowedAttributes;
1602 ac->constructed_attrs |= ac->allowedChildClasses;
1603 ac->constructed_attrs |= ac->allowedChildClassesEffective;
1604 ac->constructed_attrs |= ac->allowedAttributesEffective;
1605 ac->constructed_attrs |= ac->sDRightsEffective;
1608 ac->modify_search = false;
1610 if (ac->am_system) {
1611 ac->modify_search = false;
1614 if (!ac->constructed_attrs && !ac->modify_search) {
1615 return ldb_next_request(module, req);
1618 ret = acl_search_update_confidential_attrs(ac, data);
1619 if (ret != LDB_SUCCESS) {
1623 down_tree = ldb_parse_tree_copy_shallow(ac, req->op.search.tree);
1624 if (down_tree == NULL) {
1625 return ldb_oom(ldb);
1628 if (!ac->am_system && data->password_attrs) {
1629 for (i = 0; data->password_attrs[i]; i++) {
1630 if ((!ac->userPassword) &&
1631 (ldb_attr_cmp(data->password_attrs[i],
1632 "userPassword") == 0))
1637 ldb_parse_tree_attr_replace(down_tree,
1638 data->password_attrs[i],
1639 "kludgeACLredactedattribute");
1643 if (!ac->am_system && !ac->am_administrator && data->confidential_attrs) {
1644 for (i = 0; data->confidential_attrs[i]; i++) {
1645 ldb_parse_tree_attr_replace(down_tree,
1646 data->confidential_attrs[i],
1647 "kludgeACLredactedattribute");
1651 ret = ldb_build_search_req_ex(&down_req,
1653 req->op.search.base,
1654 req->op.search.scope,
1656 req->op.search.attrs,
1658 ac, acl_search_callback,
1660 LDB_REQ_SET_LOCATION(down_req);
1661 if (ret != LDB_SUCCESS) {
1664 /* perform the search */
1665 return ldb_next_request(module, down_req);
1668 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1670 struct ldb_context *ldb = ldb_module_get_ctx(module);
1671 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1673 /* allow everybody to read the sequence number */
1674 if (strcmp(req->op.extended.oid,
1675 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1676 return ldb_next_request(module, req);
1679 if (dsdb_module_am_system(module) ||
1680 dsdb_module_am_administrator(module) || as_system) {
1681 return ldb_next_request(module, req);
1683 ldb_asprintf_errstring(ldb,
1685 "attempted database modify not permitted. "
1686 "User %s is not SYSTEM or an administrator",
1687 acl_user_name(req, module));
1688 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1692 static const struct ldb_module_ops ldb_acl_module_ops = {
1694 .search = acl_search,
1696 .modify = acl_modify,
1698 .rename = acl_rename,
1699 .extended = acl_extended,
1700 .init_context = acl_module_init
1703 int ldb_acl_module_init(const char *version)
1705 LDB_MODULE_CHECK_VERSION(version);
1706 return ldb_register_module(&ldb_acl_module_ops);