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 "librpc/gen_ndr/ndr_security.h"
39 #include "dsdb/samdb/samdb.h"
40 #include "librpc/gen_ndr/ndr_security.h"
41 #include "param/param.h"
43 struct extended_access_check_attribute {
45 const uint32_t requires_rights;
50 const char **password_attrs;
54 struct ldb_module *module;
55 struct ldb_request *req;
57 bool allowedAttributes;
58 bool allowedAttributesEffective;
59 bool allowedChildClasses;
60 bool allowedChildClassesEffective;
61 bool sDRightsEffective;
62 const char * const *attrs;
65 bool is_root_base_dn(struct ldb_context *ldb, struct ldb_dn *dn_to_check)
68 struct ldb_dn *root_base_dn = ldb_get_root_basedn(ldb);
69 result = ldb_dn_compare(root_base_dn,dn_to_check);
73 static struct security_token *acl_user_token(struct ldb_module *module)
75 struct ldb_context *ldb = ldb_module_get_ctx(module);
76 struct auth_session_info *session_info
77 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
81 return session_info->security_token;
84 static int acl_module_init(struct ldb_module *module)
86 struct ldb_context *ldb;
87 struct acl_private *data;
89 TALLOC_CTX *mem_ctx = talloc_new(module);
90 static const char *attrs[] = { "passwordAttribute", NULL };
91 struct ldb_result *res;
92 struct ldb_message *msg;
93 struct ldb_message_element *password_attributes;
95 ldb = ldb_module_get_ctx(module);
97 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
98 if (ret != LDB_SUCCESS) {
99 ldb_debug(ldb, LDB_DEBUG_ERROR,
100 "acl_module_init: Unable to register control with rootdse!\n");
101 return LDB_ERR_OPERATIONS_ERROR;
104 data = talloc(module, struct acl_private);
107 return LDB_ERR_OPERATIONS_ERROR;
110 data->password_attrs = NULL;
111 data->acl_perform = lp_parm_bool(ldb_get_opaque(ldb, "loadparm"),
112 NULL, "acl", "perform", false);
113 ldb_module_set_private(module, data);
117 return LDB_ERR_OPERATIONS_ERROR;
120 ret = ldb_search(ldb, mem_ctx, &res,
121 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
122 LDB_SCOPE_BASE, attrs, NULL);
123 if (ret != LDB_SUCCESS) {
126 if (res->count == 0) {
130 if (res->count > 1) {
131 talloc_free(mem_ctx);
132 return LDB_ERR_CONSTRAINT_VIOLATION;
137 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
138 if (!password_attributes) {
141 data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
142 if (!data->password_attrs) {
143 talloc_free(mem_ctx);
145 return LDB_ERR_OPERATIONS_ERROR;
147 for (i=0; i < password_attributes->num_values; i++) {
148 data->password_attrs[i] = (const char *)password_attributes->values[i].data;
149 talloc_steal(data->password_attrs, password_attributes->values[i].data);
151 data->password_attrs[i] = NULL;
154 talloc_free(mem_ctx);
155 return ldb_next_init(module);
158 static int get_sd_from_ldb_message(TALLOC_CTX *mem_ctx,
159 struct ldb_message *acl_res,
160 struct security_descriptor **sd)
162 struct ldb_message_element *sd_element;
163 enum ndr_err_code ndr_err;
165 sd_element = ldb_msg_find_element(acl_res, "nTSecurityDescriptor");
170 *sd = talloc(mem_ctx, struct security_descriptor);
172 return LDB_ERR_OPERATIONS_ERROR;
174 ndr_err = ndr_pull_struct_blob(&sd_element->values[0], *sd, NULL, *sd,
175 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
177 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
178 return LDB_ERR_OPERATIONS_ERROR;
184 static const struct GUID *get_oc_guid_from_message(struct ldb_module *module,
185 struct ldb_message *msg)
187 struct ldb_message_element *oc_el;
188 struct ldb_context *ldb = ldb_module_get_ctx(module);
190 oc_el = ldb_msg_find_element(msg, "objectClass");
195 return class_schemaid_guid_by_lDAPDisplayName(dsdb_get_schema(ldb),
196 (char *)oc_el->values[oc_el->num_values-1].data);
199 static int get_dom_sid_from_ldb_message(TALLOC_CTX *mem_ctx,
200 struct ldb_message *acl_res,
201 struct dom_sid **sid)
203 struct ldb_message_element *sid_element;
204 enum ndr_err_code ndr_err;
206 sid_element = ldb_msg_find_element(acl_res, "objectSid");
211 *sid = talloc(mem_ctx, struct dom_sid);
213 return LDB_ERR_OPERATIONS_ERROR;
215 ndr_err = ndr_pull_struct_blob(&sid_element->values[0], *sid, NULL, *sid,
216 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
218 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
219 return LDB_ERR_OPERATIONS_ERROR;
226 static void acl_debug(struct security_descriptor *sd,
227 struct security_token *token,
233 DEBUG(level, ("Access on %s denied", ldb_dn_get_linearized(dn)));
235 DEBUG(level, ("Access on %s granted", ldb_dn_get_linearized(dn)));
238 DEBUG(level,("Security context: %s\n",
239 ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_token,"", token)));
240 DEBUG(level,("Security descriptor: %s\n",
241 ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_descriptor,"", sd)));
244 static int check_access_on_dn(struct ldb_module *module,
248 struct object_tree *tree)
251 struct ldb_context *ldb = ldb_module_get_ctx(module);
252 struct ldb_result *acl_res;
253 struct security_descriptor *sd = NULL;
254 struct dom_sid *sid = NULL;
256 uint32_t access_granted;
257 static const char *acl_attrs[] = {
258 "nTSecurityDescriptor",
263 ret = ldb_search(ldb, mem_ctx, &acl_res, dn, LDB_SCOPE_BASE, acl_attrs, NULL);
264 /* we sould be able to find the parent */
265 if (ret != LDB_SUCCESS) {
266 DEBUG(10,("acl: failed to find object %s\n", ldb_dn_get_linearized(dn)));
270 ret = get_sd_from_ldb_message(mem_ctx, acl_res->msgs[0], &sd);
271 if (ret != LDB_SUCCESS) {
272 return LDB_ERR_OPERATIONS_ERROR;
274 /* Theoretically we pass the check if the object has no sd */
278 ret = get_dom_sid_from_ldb_message(mem_ctx, acl_res->msgs[0], &sid);
279 if (ret != LDB_SUCCESS) {
280 return LDB_ERR_OPERATIONS_ERROR;
283 status = sec_access_check_ds(sd, acl_user_token(module),
288 if (!NT_STATUS_IS_OK(status)) {
290 acl_user_token(module),
294 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
299 static int acl_check_access_on_attribute(struct ldb_module *module,
301 struct security_descriptor *sd,
302 struct dom_sid *rp_sid,
304 struct dsdb_attribute *attr)
308 uint32_t access_granted;
309 struct object_tree *root = NULL;
310 struct object_tree *new_node = NULL;
311 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
312 struct security_token *token = acl_user_token(module);
314 if (!GUID_all_zero(&attr->attributeSecurityGUID)) {
315 if (!insert_in_object_tree(tmp_ctx,
316 &attr->attributeSecurityGUID, access,
318 DEBUG(10, ("acl_search: cannot add to object tree securityGUID\n"));
322 if (!insert_in_object_tree(tmp_ctx,
323 &attr->schemaIDGUID, access, &new_node, &new_node)) {
324 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
329 if (!insert_in_object_tree(tmp_ctx,
330 &attr->schemaIDGUID, access, &root, &new_node)) {
331 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
336 status = sec_access_check_ds(sd, token,
341 if (!NT_STATUS_IS_OK(status)) {
342 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
349 return LDB_ERR_OPERATIONS_ERROR;
352 static int acl_check_access_on_class(struct ldb_module *module,
354 struct security_descriptor *sd,
355 struct dom_sid *rp_sid,
357 const char *class_name)
360 struct ldb_context *ldb = ldb_module_get_ctx(module);
362 uint32_t access_granted;
363 struct object_tree *root = NULL;
364 struct object_tree *new_node = NULL;
366 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
367 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
368 struct security_token *token = acl_user_token(module);
370 guid = class_schemaid_guid_by_lDAPDisplayName(schema, class_name);
372 DEBUG(10, ("acl_search: cannot find class %s\n",
376 if (!insert_in_object_tree(tmp_ctx,
379 DEBUG(10, ("acl_search: cannot add to object tree guid\n"));
383 status = sec_access_check_ds(sd, token,
388 if (!NT_STATUS_IS_OK(status)) {
389 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
396 return LDB_ERR_OPERATIONS_ERROR;
399 static int acl_allowedAttributes(struct ldb_module *module,
400 struct ldb_message *sd_msg,
401 struct ldb_message *msg,
402 struct acl_context *ac)
404 struct ldb_message_element *oc_el;
405 struct ldb_context *ldb = ldb_module_get_ctx(module);
406 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
408 const char **attr_list;
411 /* If we don't have a schema yet, we can't do anything... */
412 if (schema == NULL) {
416 /* Must remove any existing attribute */
417 if (ac->allowedAttributes) {
418 ldb_msg_remove_attr(msg, "allowedAttributes");
421 mem_ctx = talloc_new(msg);
424 return LDB_ERR_OPERATIONS_ERROR;
427 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
428 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
430 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
431 talloc_free(mem_ctx);
432 return LDB_ERR_OPERATIONS_ERROR;
434 if (ac->allowedAttributes) {
435 for (i=0; attr_list && attr_list[i]; i++) {
436 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
439 if (ac->allowedAttributesEffective) {
440 struct security_descriptor *sd;
441 struct dom_sid *sid = NULL;
442 struct ldb_control *as_system = ldb_request_get_control(ac->req,
443 LDB_CONTROL_AS_SYSTEM_OID);
444 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
445 if (ac->am_system || as_system) {
446 for (i=0; attr_list && attr_list[i]; i++) {
447 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
452 ret = get_sd_from_ldb_message(mem_ctx, sd_msg, &sd);
454 if (ret != LDB_SUCCESS) {
457 ret = get_dom_sid_from_ldb_message(mem_ctx, sd_msg, &sid);
459 if (ret != LDB_SUCCESS) {
462 for (i=0; attr_list && attr_list[i]; i++) {
463 struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
466 return LDB_ERR_OPERATIONS_ERROR;
468 /* remove constructed attributes */
469 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
471 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
474 ret = acl_check_access_on_attribute(module,
480 if (ret == LDB_SUCCESS) {
481 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
488 static int acl_childClasses(struct ldb_module *module,
489 struct ldb_message *sd_msg,
490 struct ldb_message *msg,
491 const char *attrName)
493 struct ldb_message_element *oc_el;
494 struct ldb_message_element *allowedClasses;
495 struct ldb_context *ldb = ldb_module_get_ctx(module);
496 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
497 const struct dsdb_class *sclass;
500 /* If we don't have a schema yet, we can't do anything... */
501 if (schema == NULL) {
505 /* Must remove any existing attribute, or else confusion reins */
506 ldb_msg_remove_attr(msg, attrName);
507 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
508 if (ret != LDB_SUCCESS) {
512 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
514 for (i=0; oc_el && i < oc_el->num_values; i++) {
515 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
517 /* We don't know this class? what is going on? */
521 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
522 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
525 if (allowedClasses->num_values > 1) {
526 qsort(allowedClasses->values,
527 allowedClasses->num_values,
528 sizeof(*allowedClasses->values),
529 (comparison_fn_t)data_blob_cmp);
531 for (i=1 ; i < allowedClasses->num_values; i++) {
532 struct ldb_val *val1 = &allowedClasses->values[i-1];
533 struct ldb_val *val2 = &allowedClasses->values[i];
534 if (data_blob_cmp(val1, val2) == 0) {
535 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
536 allowedClasses->num_values--;
545 static int acl_childClassesEffective(struct ldb_module *module,
546 struct ldb_message *sd_msg,
547 struct ldb_message *msg,
548 struct acl_context *ac)
550 struct ldb_message_element *oc_el;
551 struct ldb_message_element *allowedClasses = NULL;
552 struct ldb_context *ldb = ldb_module_get_ctx(module);
553 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
554 const struct dsdb_class *sclass;
555 struct security_descriptor *sd;
556 struct ldb_control *as_system = ldb_request_get_control(ac->req,
557 LDB_CONTROL_AS_SYSTEM_OID);
558 struct dom_sid *sid = NULL;
561 if (ac->am_system || as_system) {
562 return acl_childClasses(module, sd_msg, msg, "allowedChildClassesEffective");
565 /* If we don't have a schema yet, we can't do anything... */
566 if (schema == NULL) {
570 /* Must remove any existing attribute, or else confusion reins */
571 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
573 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
574 ret = get_sd_from_ldb_message(msg, sd_msg, &sd);
575 if (ret != LDB_SUCCESS) {
578 ret = get_dom_sid_from_ldb_message(msg, sd_msg, &sid);
580 if (ret != LDB_SUCCESS) {
583 for (i=0; oc_el && i < oc_el->num_values; i++) {
584 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
586 /* We don't know this class? what is going on? */
590 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
591 ret = acl_check_access_on_class(module,
595 SEC_ADS_CREATE_CHILD,
596 sclass->possibleInferiors[j]);
597 if (ret == LDB_SUCCESS) {
598 ldb_msg_add_string(msg, "allowedChildClassesEffective",
599 sclass->possibleInferiors[j]);
603 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
604 if (!allowedClasses) {
608 if (allowedClasses->num_values > 1) {
609 qsort(allowedClasses->values,
610 allowedClasses->num_values,
611 sizeof(*allowedClasses->values),
612 (comparison_fn_t)data_blob_cmp);
613 for (i=1 ; i < allowedClasses->num_values; i++) {
614 struct ldb_val *val1 = &allowedClasses->values[i-1];
615 struct ldb_val *val2 = &allowedClasses->values[i];
616 if (data_blob_cmp(val1, val2) == 0) {
617 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
618 allowedClasses->num_values--;
626 static int acl_sDRightsEffective(struct ldb_module *module,
627 struct ldb_message *sd_msg,
628 struct ldb_message *msg,
629 struct acl_context *ac)
631 struct ldb_message_element *rightsEffective;
633 struct security_descriptor *sd;
634 struct ldb_control *as_system = ldb_request_get_control(ac->req,
635 LDB_CONTROL_AS_SYSTEM_OID);
636 struct dom_sid *sid = NULL;
639 /* Must remove any existing attribute, or else confusion reins */
640 ldb_msg_remove_attr(msg, "sDRightsEffective");
641 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
642 if (ret != LDB_SUCCESS) {
645 if (ac->am_system || as_system) {
646 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
649 /* Get the security descriptor from the message */
650 ret = get_sd_from_ldb_message(msg, sd_msg, &sd);
651 if (ret != LDB_SUCCESS) {
654 ret = get_dom_sid_from_ldb_message(msg, sd_msg, &sid);
656 if (ret != LDB_SUCCESS) {
659 ret = acl_check_access_on_attribute(module,
665 if (ret == LDB_SUCCESS) {
666 flags |= SECINFO_OWNER | SECINFO_GROUP;
668 ret = acl_check_access_on_attribute(module,
674 if (ret == LDB_SUCCESS) {
675 flags |= SECINFO_DACL;
677 ret = acl_check_access_on_attribute(module,
681 SEC_FLAG_SYSTEM_SECURITY,
683 if (ret == LDB_SUCCESS) {
684 flags |= SECINFO_SACL;
687 ldb_msg_add_fmt(msg, "sDRightsEffective", "%u", flags);
691 static int acl_add(struct ldb_module *module, struct ldb_request *req)
694 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.add.message->dn);
695 struct ldb_context *ldb;
696 struct ldb_message_element *oc_el;
697 const struct GUID *guid;
698 struct object_tree *root = NULL;
699 struct object_tree *new_node = NULL;
700 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
702 if (dsdb_module_am_system(module) || as_system) {
703 return ldb_next_request(module, req);
706 if (ldb_dn_is_special(req->op.add.message->dn)) {
707 return ldb_next_request(module, req);
709 ldb = ldb_module_get_ctx(module);
710 /* Creating an NC. There is probably something we should do here,
711 * but we will establish that later */
712 if ((ldb_dn_compare(req->op.add.message->dn, (ldb_get_schema_basedn(ldb))) == 0) ||
713 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_config_basedn(ldb))) == 0) ||
714 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_root_basedn(ldb))) == 0)) {
715 return ldb_next_request(module, req);
718 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
719 if (!oc_el || oc_el->num_values == 0) {
720 DEBUG(10,("acl:operation error %s\n", ldb_dn_get_linearized(req->op.add.message->dn)));
721 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
724 guid = class_schemaid_guid_by_lDAPDisplayName(dsdb_get_schema(ldb),
725 (char *)oc_el->values[oc_el->num_values-1].data);
727 if (!insert_in_object_tree(req, guid, SEC_ADS_CREATE_CHILD, &root, &new_node)) {
728 return LDB_ERR_OPERATIONS_ERROR;
731 ret = check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, root);
732 if (ret != LDB_SUCCESS) {
736 return ldb_next_request(module, req);
739 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
742 struct ldb_context *ldb = ldb_module_get_ctx(module);
743 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
745 bool modify_sd = false;
746 const struct GUID *guid;
747 uint32_t access_granted;
748 struct object_tree *root = NULL;
749 struct object_tree *new_node = NULL;
751 struct ldb_result *acl_res;
752 struct security_descriptor *sd;
753 struct dom_sid *sid = NULL;
754 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
755 TALLOC_CTX *tmp_ctx = talloc_new(req);
756 static const char *acl_attrs[] = {
757 "nTSecurityDescriptor",
763 /* Don't print this debug statement if elements[0].name is going to be NULL */
764 if(req->op.mod.message->num_elements > 0)
766 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->elements[0].name));
768 if (dsdb_module_am_system(module) || as_system) {
769 return ldb_next_request(module, req);
771 if (ldb_dn_is_special(req->op.mod.message->dn)) {
772 return ldb_next_request(module, req);
774 ret = ldb_search(ldb, req, &acl_res, req->op.mod.message->dn,
775 LDB_SCOPE_BASE, acl_attrs, NULL);
777 if (ret != LDB_SUCCESS) {
781 ret = get_sd_from_ldb_message(req, acl_res->msgs[0], &sd);
782 if (ret != LDB_SUCCESS) {
783 DEBUG(10, ("acl_modify: cannot get descriptor\n"));
786 /* Theoretically we pass the check if the object has no sd */
791 guid = get_oc_guid_from_message(module,acl_res->msgs[0]);
793 DEBUG(10, ("acl_modify: cannot get guid\n"));
797 ret = get_dom_sid_from_ldb_message(req, acl_res->msgs[0], &sid);
798 if (ret != LDB_SUCCESS) {
799 return LDB_ERR_OPERATIONS_ERROR;
802 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
804 DEBUG(10, ("acl_modify: cannot add to object tree\n"));
807 for (i=0; i < req->op.mod.message->num_elements; i++){
808 const struct dsdb_attribute *attr;
809 /* clearTextPassword is not in schema */
810 if (strcmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
811 attr = dsdb_attribute_by_lDAPDisplayName(schema, "unicodePwd");
813 attr = dsdb_attribute_by_lDAPDisplayName(schema,
814 req->op.mod.message->elements[i].name);
816 if (strcmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
821 DEBUG(10, ("acl_modify: cannot find attribute %s\n",
822 req->op.mod.message->elements[i].name));
825 if (!insert_in_object_tree(tmp_ctx,
826 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
827 &new_node, &new_node)) {
828 DEBUG(10, ("acl_modify: cannot add to object tree securityGUID\n"));
832 if (!insert_in_object_tree(tmp_ctx,
833 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
834 DEBUG(10, ("acl_modify: cannot add to object tree attributeGUID\n"));
840 if (root->num_of_children > 0) {
841 status = sec_access_check_ds(sd, acl_user_token(module),
847 if (!NT_STATUS_IS_OK(status)) {
848 DEBUG(10, ("Object %s nas no write property access\n",
849 ldb_dn_get_linearized(req->op.mod.message->dn)));
851 acl_user_token(module),
852 req->op.mod.message->dn,
855 talloc_free(tmp_ctx);
856 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
860 status = sec_access_check_ds(sd, acl_user_token(module),
866 if (!NT_STATUS_IS_OK(status)) {
867 DEBUG(10, ("Object %s nas no write dacl access\n",
868 ldb_dn_get_linearized(req->op.mod.message->dn)));
870 acl_user_token(module),
871 req->op.mod.message->dn,
874 talloc_free(tmp_ctx);
875 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
879 talloc_free(tmp_ctx);
880 return ldb_next_request(module, req);
882 talloc_free(tmp_ctx);
883 return LDB_ERR_OPERATIONS_ERROR;
886 /* similar to the modify for the time being.
887 * We need to concider the special delete tree case, though - TODO */
888 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
891 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.del.dn);
892 struct ldb_context *ldb;
893 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
895 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
896 if (dsdb_module_am_system(module) || as_system) {
897 return ldb_next_request(module, req);
900 if (ldb_dn_is_special(req->op.del.dn)) {
901 return ldb_next_request(module, req);
903 ldb = ldb_module_get_ctx(module);
904 /* first check if we have delete object right */
905 ret = check_access_on_dn(module, req, req->op.del.dn, SEC_STD_DELETE, NULL);
906 if (ret == LDB_SUCCESS) {
907 return ldb_next_request(module, req);
910 /* Nope, we don't have delete object. Lets check if we have delete child on the parent */
911 /* No parent, so check fails */
912 if ((ldb_dn_compare(req->op.del.dn, (ldb_get_schema_basedn(ldb))) == 0) ||
913 (ldb_dn_compare(req->op.del.dn, (ldb_get_config_basedn(ldb))) == 0) ||
914 (ldb_dn_compare(req->op.del.dn, (ldb_get_root_basedn(ldb))) == 0)) {
915 DEBUG(10,("acl:deleting an NC\n"));
916 return ldb_module_done(req, NULL, NULL, LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS);
919 ret = check_access_on_dn(module, req, parent, SEC_ADS_DELETE_CHILD, NULL);
920 if (ret != LDB_SUCCESS) {
923 return ldb_next_request(module, req);
926 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
929 struct ldb_dn *oldparent = ldb_dn_get_parent(req, req->op.rename.olddn);
930 struct ldb_dn *newparent = ldb_dn_get_parent(req, req->op.rename.newdn);
931 struct ldb_context *ldb;
932 struct security_descriptor *sd = NULL;
933 struct dom_sid *sid = NULL;
934 struct ldb_result *acl_res;
935 const struct GUID *guid;
936 struct object_tree *root = NULL;
937 struct object_tree *new_node = NULL;
938 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
939 TALLOC_CTX *tmp_ctx = talloc_new(req);
941 uint32_t access_granted;
942 static const char *acl_attrs[] = {
943 "nTSecurityDescriptor",
949 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
950 if (dsdb_module_am_system(module) || as_system) {
951 return ldb_next_request(module, req);
953 if (ldb_dn_is_special(req->op.rename.olddn)) {
954 return ldb_next_request(module, req);
956 ldb = ldb_module_get_ctx(module);
958 /* TODO search to include deleted objects */
959 ret = ldb_search(ldb, req, &acl_res, req->op.rename.olddn,
960 LDB_SCOPE_BASE, acl_attrs, NULL);
961 /* we sould be able to find the parent */
962 if (ret != LDB_SUCCESS) {
963 DEBUG(10,("acl: failed to find object %s\n",
964 ldb_dn_get_linearized(req->op.rename.olddn)));
968 guid = get_oc_guid_from_message(module,acl_res->msgs[0]);
969 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
971 return LDB_ERR_OPERATIONS_ERROR;
974 guid = attribute_schemaid_guid_by_lDAPDisplayName(dsdb_get_schema(ldb),
976 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
977 &new_node, &new_node)) {
978 return LDB_ERR_OPERATIONS_ERROR;
981 ret = get_sd_from_ldb_message(req, acl_res->msgs[0], &sd);
983 if (ret != LDB_SUCCESS) {
984 return LDB_ERR_OPERATIONS_ERROR;
986 /* Theoretically we pass the check if the object has no sd */
990 ret = get_dom_sid_from_ldb_message(req, acl_res->msgs[0], &sid);
991 if (ret != LDB_SUCCESS) {
992 return LDB_ERR_OPERATIONS_ERROR;
995 status = sec_access_check_ds(sd, acl_user_token(module),
1001 if (!NT_STATUS_IS_OK(status)) {
1002 DEBUG(10, ("Object %s nas no wp on name\n",
1003 ldb_dn_get_linearized(req->op.rename.olddn)));
1005 acl_user_token(module),
1006 req->op.rename.olddn,
1009 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1012 if (ldb_dn_compare(oldparent, newparent) == 0) {
1013 /* regular rename, not move, nothing more to do */
1014 return ldb_next_request(module, req);
1017 /* What exactly to do in this case? It would fail anyway.. */
1018 if ((ldb_dn_compare(req->op.rename.newdn, (ldb_get_schema_basedn(ldb))) == 0) ||
1019 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_config_basedn(ldb))) == 0) ||
1020 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_root_basedn(ldb))) == 0)) {
1021 DEBUG(10,("acl:moving as an NC\n"));
1022 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1024 /* new parent should have create child */
1025 talloc_free(tmp_ctx);
1026 tmp_ctx = talloc_new(req);
1029 guid = get_oc_guid_from_message(module,acl_res->msgs[0]);
1031 DEBUG(10,("acl:renamed object has no object class\n"));
1032 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
1034 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_CREATE_CHILD,
1035 &root, &new_node)) {
1036 return LDB_ERR_OPERATIONS_ERROR;
1038 ret = check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, root);
1039 if (ret != LDB_SUCCESS) {
1040 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1043 /* do we have delete object on the object? */
1045 status = sec_access_check_ds(sd, acl_user_token(module),
1051 if (NT_STATUS_IS_OK(status)) {
1052 return ldb_next_request(module, req);
1054 /* what about delete child on the current parent */
1055 ret = check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL);
1056 if (ret != LDB_SUCCESS) {
1057 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1058 return ldb_module_done(req, NULL, NULL, ret);
1060 return ldb_next_request(module, req);
1063 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1065 struct ldb_context *ldb;
1066 struct acl_context *ac;
1067 struct acl_private *data;
1068 struct ldb_result *acl_res;
1069 static const char *acl_attrs[] = {
1071 "nTSecurityDescriptor",
1077 ac = talloc_get_type(req->context, struct acl_context);
1078 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1079 ldb = ldb_module_get_ctx(ac->module);
1082 return ldb_module_done(ac->req, NULL, NULL,
1083 LDB_ERR_OPERATIONS_ERROR);
1085 if (ares->error != LDB_SUCCESS) {
1086 return ldb_module_done(ac->req, ares->controls,
1087 ares->response, ares->error);
1090 switch (ares->type) {
1091 case LDB_REPLY_ENTRY:
1092 if (ac->allowedAttributes
1093 || ac->allowedChildClasses
1094 || ac->allowedChildClassesEffective
1095 || ac->allowedAttributesEffective
1096 || ac->sDRightsEffective) {
1097 ret = ldb_search(ldb, ac, &acl_res, ares->message->dn, LDB_SCOPE_BASE, acl_attrs, NULL);
1098 if (ret != LDB_SUCCESS) {
1099 return ldb_module_done(ac->req, NULL, NULL, ret);
1101 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1102 ret = acl_allowedAttributes(ac->module, acl_res->msgs[0], ares->message, ac);
1103 if (ret != LDB_SUCCESS) {
1104 return ldb_module_done(ac->req, NULL, NULL, ret);
1107 if (ac->allowedChildClasses) {
1108 ret = acl_childClasses(ac->module, acl_res->msgs[0],
1109 ares->message, "allowedChildClasses");
1110 if (ret != LDB_SUCCESS) {
1111 return ldb_module_done(ac->req, NULL, NULL, ret);
1114 if (ac->allowedChildClassesEffective) {
1115 ret = acl_childClassesEffective(ac->module,
1116 acl_res->msgs[0], ares->message, ac);
1117 if (ret != LDB_SUCCESS) {
1118 return ldb_module_done(ac->req, NULL, NULL, ret);
1121 if (ac->sDRightsEffective) {
1122 ret = acl_sDRightsEffective(ac->module,
1123 acl_res->msgs[0], ares->message, ac);
1124 if (ret != LDB_SUCCESS) {
1125 return ldb_module_done(ac->req, NULL, NULL, ret);
1129 if (data && data->password_attrs) {
1130 if (!ac->am_system) {
1131 for (i = 0; data->password_attrs[i]; i++) {
1132 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1136 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1138 case LDB_REPLY_REFERRAL:
1139 return ldb_module_send_referral(ac->req, ares->referral);
1141 case LDB_REPLY_DONE:
1142 return ldb_module_done(ac->req, ares->controls,
1143 ares->response, LDB_SUCCESS);
1149 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1151 struct ldb_context *ldb;
1152 struct acl_context *ac;
1153 struct ldb_request *down_req;
1154 struct acl_private *data;
1157 ldb = ldb_module_get_ctx(module);
1159 ac = talloc_zero(req, struct acl_context);
1162 return LDB_ERR_OPERATIONS_ERROR;
1164 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1166 ac->module = module;
1168 ac->am_system = dsdb_module_am_system(module);
1169 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1170 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1171 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1172 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1173 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1175 /* replace any attributes in the parse tree that are private,
1176 so we don't allow a search for 'userPassword=penguin',
1177 just as we would not allow that attribute to be returned */
1178 if (ac->am_system) {
1179 /* FIXME: We should copy the tree and keep the original unmodified. */
1180 /* remove password attributes */
1181 if (data && data->password_attrs) {
1182 for (i = 0; data->password_attrs[i]; i++) {
1183 ldb_parse_tree_attr_replace(req->op.search.tree,
1184 data->password_attrs[i],
1185 "kludgeACLredactedattribute");
1189 ret = ldb_build_search_req_ex(&down_req,
1191 req->op.search.base,
1192 req->op.search.scope,
1193 req->op.search.tree,
1194 req->op.search.attrs,
1196 ac, acl_search_callback,
1198 if (ret != LDB_SUCCESS) {
1201 /* perform the search */
1202 return ldb_next_request(module, down_req);
1205 _PUBLIC_ const struct ldb_module_ops ldb_acl_module_ops = {
1207 .search = acl_search,
1209 .modify = acl_modify,
1211 .rename = acl_rename,
1212 .init_context = acl_module_init