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"
44 struct extended_access_check_attribute {
46 const uint32_t requires_rights;
51 const char **password_attrs;
55 struct ldb_module *module;
56 struct ldb_request *req;
58 bool allowedAttributes;
59 bool allowedAttributesEffective;
60 bool allowedChildClasses;
61 bool allowedChildClassesEffective;
62 bool sDRightsEffective;
63 const char * const *attrs;
66 bool is_root_base_dn(struct ldb_context *ldb, struct ldb_dn *dn_to_check)
69 struct ldb_dn *root_base_dn = ldb_get_root_basedn(ldb);
70 result = ldb_dn_compare(root_base_dn,dn_to_check);
74 static struct security_token *acl_user_token(struct ldb_module *module)
76 struct ldb_context *ldb = ldb_module_get_ctx(module);
77 struct auth_session_info *session_info
78 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
82 return session_info->security_token;
85 static int acl_module_init(struct ldb_module *module)
87 struct ldb_context *ldb;
88 struct acl_private *data;
90 TALLOC_CTX *mem_ctx = talloc_new(module);
91 static const char *attrs[] = { "passwordAttribute", NULL };
92 struct ldb_result *res;
93 struct ldb_message *msg;
94 struct ldb_message_element *password_attributes;
96 ldb = ldb_module_get_ctx(module);
98 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
99 if (ret != LDB_SUCCESS) {
100 ldb_debug(ldb, LDB_DEBUG_ERROR,
101 "acl_module_init: Unable to register control with rootdse!\n");
102 return LDB_ERR_OPERATIONS_ERROR;
105 data = talloc(module, struct acl_private);
108 return LDB_ERR_OPERATIONS_ERROR;
111 data->password_attrs = NULL;
112 data->acl_perform = lp_parm_bool(ldb_get_opaque(ldb, "loadparm"),
113 NULL, "acl", "perform", false);
114 ldb_module_set_private(module, data);
118 return LDB_ERR_OPERATIONS_ERROR;
121 ret = ldb_search(ldb, mem_ctx, &res,
122 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
123 LDB_SCOPE_BASE, attrs, NULL);
124 if (ret != LDB_SUCCESS) {
127 if (res->count == 0) {
131 if (res->count > 1) {
132 talloc_free(mem_ctx);
133 return LDB_ERR_CONSTRAINT_VIOLATION;
138 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
139 if (!password_attributes) {
142 data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
143 if (!data->password_attrs) {
144 talloc_free(mem_ctx);
146 return LDB_ERR_OPERATIONS_ERROR;
148 for (i=0; i < password_attributes->num_values; i++) {
149 data->password_attrs[i] = (const char *)password_attributes->values[i].data;
150 talloc_steal(data->password_attrs, password_attributes->values[i].data);
152 data->password_attrs[i] = NULL;
155 talloc_free(mem_ctx);
156 return ldb_next_init(module);
159 static int get_sd_from_ldb_message(TALLOC_CTX *mem_ctx,
160 struct ldb_message *acl_res,
161 struct security_descriptor **sd)
163 struct ldb_message_element *sd_element;
164 enum ndr_err_code ndr_err;
166 sd_element = ldb_msg_find_element(acl_res, "nTSecurityDescriptor");
171 *sd = talloc(mem_ctx, struct security_descriptor);
173 return LDB_ERR_OPERATIONS_ERROR;
175 ndr_err = ndr_pull_struct_blob(&sd_element->values[0], *sd, NULL, *sd,
176 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
178 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
179 return LDB_ERR_OPERATIONS_ERROR;
185 static const struct GUID *get_oc_guid_from_message(struct ldb_module *module,
186 struct ldb_message *msg)
188 struct ldb_message_element *oc_el;
189 struct ldb_context *ldb = ldb_module_get_ctx(module);
191 oc_el = ldb_msg_find_element(msg, "objectClass");
196 return class_schemaid_guid_by_lDAPDisplayName(dsdb_get_schema(ldb),
197 (char *)oc_el->values[oc_el->num_values-1].data);
200 static int get_dom_sid_from_ldb_message(TALLOC_CTX *mem_ctx,
201 struct ldb_message *acl_res,
202 struct dom_sid **sid)
204 struct ldb_message_element *sid_element;
205 enum ndr_err_code ndr_err;
207 sid_element = ldb_msg_find_element(acl_res, "objectSid");
212 *sid = talloc(mem_ctx, struct dom_sid);
214 return LDB_ERR_OPERATIONS_ERROR;
216 ndr_err = ndr_pull_struct_blob(&sid_element->values[0], *sid, NULL, *sid,
217 (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
219 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
220 return LDB_ERR_OPERATIONS_ERROR;
227 static void acl_debug(struct security_descriptor *sd,
228 struct security_token *token,
234 DEBUG(level, ("Access on %s denied", ldb_dn_get_linearized(dn)));
236 DEBUG(level, ("Access on %s granted", ldb_dn_get_linearized(dn)));
239 DEBUG(level,("Security context: %s\n",
240 ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_token,"", token)));
241 DEBUG(level,("Security descriptor: %s\n",
242 ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_descriptor,"", sd)));
245 static int check_access_on_dn(struct ldb_module *module,
249 struct object_tree *tree)
252 struct ldb_context *ldb = ldb_module_get_ctx(module);
253 struct ldb_result *acl_res;
254 struct security_descriptor *sd = NULL;
255 struct dom_sid *sid = NULL;
257 uint32_t access_granted;
258 static const char *acl_attrs[] = {
259 "nTSecurityDescriptor",
264 ret = ldb_search(ldb, mem_ctx, &acl_res, dn, LDB_SCOPE_BASE, acl_attrs, NULL);
265 /* we sould be able to find the parent */
266 if (ret != LDB_SUCCESS) {
267 DEBUG(10,("acl: failed to find object %s\n", ldb_dn_get_linearized(dn)));
271 ret = get_sd_from_ldb_message(mem_ctx, acl_res->msgs[0], &sd);
272 if (ret != LDB_SUCCESS) {
273 return LDB_ERR_OPERATIONS_ERROR;
275 /* Theoretically we pass the check if the object has no sd */
279 ret = get_dom_sid_from_ldb_message(mem_ctx, acl_res->msgs[0], &sid);
280 if (ret != LDB_SUCCESS) {
281 return LDB_ERR_OPERATIONS_ERROR;
284 status = sec_access_check_ds(sd, acl_user_token(module),
289 if (!NT_STATUS_IS_OK(status)) {
291 acl_user_token(module),
295 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
300 static int acl_check_access_on_attribute(struct ldb_module *module,
302 struct security_descriptor *sd,
303 struct dom_sid *rp_sid,
305 const struct dsdb_attribute *attr)
309 uint32_t access_granted;
310 struct object_tree *root = NULL;
311 struct object_tree *new_node = NULL;
312 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
313 struct security_token *token = acl_user_token(module);
315 if (!GUID_all_zero(&attr->attributeSecurityGUID)) {
316 if (!insert_in_object_tree(tmp_ctx,
317 &attr->attributeSecurityGUID, access,
319 DEBUG(10, ("acl_search: cannot add to object tree securityGUID\n"));
323 if (!insert_in_object_tree(tmp_ctx,
324 &attr->schemaIDGUID, access, &new_node, &new_node)) {
325 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
330 if (!insert_in_object_tree(tmp_ctx,
331 &attr->schemaIDGUID, access, &root, &new_node)) {
332 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
337 status = sec_access_check_ds(sd, token,
342 if (!NT_STATUS_IS_OK(status)) {
343 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
350 return LDB_ERR_OPERATIONS_ERROR;
353 static int acl_check_access_on_class(struct ldb_module *module,
355 struct security_descriptor *sd,
356 struct dom_sid *rp_sid,
358 const char *class_name)
361 struct ldb_context *ldb = ldb_module_get_ctx(module);
363 uint32_t access_granted;
364 struct object_tree *root = NULL;
365 struct object_tree *new_node = NULL;
366 const struct GUID *guid;
367 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
368 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
369 struct security_token *token = acl_user_token(module);
371 guid = class_schemaid_guid_by_lDAPDisplayName(schema, class_name);
373 DEBUG(10, ("acl_search: cannot find class %s\n",
377 if (!insert_in_object_tree(tmp_ctx,
380 DEBUG(10, ("acl_search: cannot add to object tree guid\n"));
384 status = sec_access_check_ds(sd, token,
389 if (!NT_STATUS_IS_OK(status)) {
390 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
397 return LDB_ERR_OPERATIONS_ERROR;
400 static int acl_allowedAttributes(struct ldb_module *module,
401 struct ldb_message *sd_msg,
402 struct ldb_message *msg,
403 struct acl_context *ac)
405 struct ldb_message_element *oc_el;
406 struct ldb_context *ldb = ldb_module_get_ctx(module);
407 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
409 const char **attr_list;
412 /* If we don't have a schema yet, we can't do anything... */
413 if (schema == NULL) {
417 /* Must remove any existing attribute */
418 if (ac->allowedAttributes) {
419 ldb_msg_remove_attr(msg, "allowedAttributes");
422 mem_ctx = talloc_new(msg);
425 return LDB_ERR_OPERATIONS_ERROR;
428 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
429 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
431 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
432 talloc_free(mem_ctx);
433 return LDB_ERR_OPERATIONS_ERROR;
435 if (ac->allowedAttributes) {
436 for (i=0; attr_list && attr_list[i]; i++) {
437 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
440 if (ac->allowedAttributesEffective) {
441 struct security_descriptor *sd;
442 struct dom_sid *sid = NULL;
443 struct ldb_control *as_system = ldb_request_get_control(ac->req,
444 LDB_CONTROL_AS_SYSTEM_OID);
446 if (as_system != NULL) {
447 as_system->critical = 0;
450 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
451 if (ac->am_system || as_system) {
452 for (i=0; attr_list && attr_list[i]; i++) {
453 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
458 ret = get_sd_from_ldb_message(mem_ctx, sd_msg, &sd);
460 if (ret != LDB_SUCCESS) {
463 ret = get_dom_sid_from_ldb_message(mem_ctx, sd_msg, &sid);
465 if (ret != LDB_SUCCESS) {
468 for (i=0; attr_list && attr_list[i]; i++) {
469 const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
472 return LDB_ERR_OPERATIONS_ERROR;
474 /* remove constructed attributes */
475 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
477 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
480 ret = acl_check_access_on_attribute(module,
486 if (ret == LDB_SUCCESS) {
487 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
494 static int acl_childClasses(struct ldb_module *module,
495 struct ldb_message *sd_msg,
496 struct ldb_message *msg,
497 const char *attrName)
499 struct ldb_message_element *oc_el;
500 struct ldb_message_element *allowedClasses;
501 struct ldb_context *ldb = ldb_module_get_ctx(module);
502 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
503 const struct dsdb_class *sclass;
506 /* If we don't have a schema yet, we can't do anything... */
507 if (schema == NULL) {
511 /* Must remove any existing attribute, or else confusion reins */
512 ldb_msg_remove_attr(msg, attrName);
513 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
514 if (ret != LDB_SUCCESS) {
518 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
520 for (i=0; oc_el && i < oc_el->num_values; i++) {
521 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
523 /* We don't know this class? what is going on? */
527 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
528 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
531 if (allowedClasses->num_values > 1) {
532 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
533 for (i=1 ; i < allowedClasses->num_values; i++) {
534 struct ldb_val *val1 = &allowedClasses->values[i-1];
535 struct ldb_val *val2 = &allowedClasses->values[i];
536 if (data_blob_cmp(val1, val2) == 0) {
537 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
538 allowedClasses->num_values--;
547 static int acl_childClassesEffective(struct ldb_module *module,
548 struct ldb_message *sd_msg,
549 struct ldb_message *msg,
550 struct acl_context *ac)
552 struct ldb_message_element *oc_el;
553 struct ldb_message_element *allowedClasses = NULL;
554 struct ldb_context *ldb = ldb_module_get_ctx(module);
555 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
556 const struct dsdb_class *sclass;
557 struct security_descriptor *sd;
558 struct ldb_control *as_system = ldb_request_get_control(ac->req,
559 LDB_CONTROL_AS_SYSTEM_OID);
560 struct dom_sid *sid = NULL;
563 if (as_system != NULL) {
564 as_system->critical = 0;
567 if (ac->am_system || as_system) {
568 return acl_childClasses(module, sd_msg, msg, "allowedChildClassesEffective");
571 /* If we don't have a schema yet, we can't do anything... */
572 if (schema == NULL) {
576 /* Must remove any existing attribute, or else confusion reins */
577 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
579 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
580 ret = get_sd_from_ldb_message(msg, sd_msg, &sd);
581 if (ret != LDB_SUCCESS) {
584 ret = get_dom_sid_from_ldb_message(msg, sd_msg, &sid);
586 if (ret != LDB_SUCCESS) {
589 for (i=0; oc_el && i < oc_el->num_values; i++) {
590 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
592 /* We don't know this class? what is going on? */
596 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
597 ret = acl_check_access_on_class(module,
601 SEC_ADS_CREATE_CHILD,
602 sclass->possibleInferiors[j]);
603 if (ret == LDB_SUCCESS) {
604 ldb_msg_add_string(msg, "allowedChildClassesEffective",
605 sclass->possibleInferiors[j]);
609 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
610 if (!allowedClasses) {
614 if (allowedClasses->num_values > 1) {
615 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
616 for (i=1 ; i < allowedClasses->num_values; i++) {
617 struct ldb_val *val1 = &allowedClasses->values[i-1];
618 struct ldb_val *val2 = &allowedClasses->values[i];
619 if (data_blob_cmp(val1, val2) == 0) {
620 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
621 allowedClasses->num_values--;
629 static int acl_sDRightsEffective(struct ldb_module *module,
630 struct ldb_message *sd_msg,
631 struct ldb_message *msg,
632 struct acl_context *ac)
634 struct ldb_message_element *rightsEffective;
636 struct security_descriptor *sd;
637 struct ldb_control *as_system = ldb_request_get_control(ac->req,
638 LDB_CONTROL_AS_SYSTEM_OID);
639 struct dom_sid *sid = NULL;
642 if (as_system != NULL) {
643 as_system->critical = 0;
646 /* Must remove any existing attribute, or else confusion reins */
647 ldb_msg_remove_attr(msg, "sDRightsEffective");
648 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
649 if (ret != LDB_SUCCESS) {
652 if (ac->am_system || as_system) {
653 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
656 /* Get the security descriptor from the message */
657 ret = get_sd_from_ldb_message(msg, sd_msg, &sd);
658 if (ret != LDB_SUCCESS) {
661 ret = get_dom_sid_from_ldb_message(msg, sd_msg, &sid);
663 if (ret != LDB_SUCCESS) {
666 ret = acl_check_access_on_attribute(module,
672 if (ret == LDB_SUCCESS) {
673 flags |= SECINFO_OWNER | SECINFO_GROUP;
675 ret = acl_check_access_on_attribute(module,
681 if (ret == LDB_SUCCESS) {
682 flags |= SECINFO_DACL;
684 ret = acl_check_access_on_attribute(module,
688 SEC_FLAG_SYSTEM_SECURITY,
690 if (ret == LDB_SUCCESS) {
691 flags |= SECINFO_SACL;
694 ldb_msg_add_fmt(msg, "sDRightsEffective", "%u", flags);
698 static int acl_add(struct ldb_module *module, struct ldb_request *req)
701 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.add.message->dn);
702 struct ldb_context *ldb;
703 struct ldb_message_element *oc_el;
704 const struct GUID *guid;
705 struct object_tree *root = NULL;
706 struct object_tree *new_node = NULL;
707 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
709 if (as_system != NULL) {
710 as_system->critical = 0;
713 if (dsdb_module_am_system(module) || as_system) {
714 return ldb_next_request(module, req);
717 if (ldb_dn_is_special(req->op.add.message->dn)) {
718 return ldb_next_request(module, req);
720 ldb = ldb_module_get_ctx(module);
721 /* Creating an NC. There is probably something we should do here,
722 * but we will establish that later */
723 if ((ldb_dn_compare(req->op.add.message->dn, (ldb_get_schema_basedn(ldb))) == 0) ||
724 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_config_basedn(ldb))) == 0) ||
725 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_root_basedn(ldb))) == 0)) {
726 return ldb_next_request(module, req);
729 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
730 if (!oc_el || oc_el->num_values == 0) {
731 DEBUG(10,("acl:operation error %s\n", ldb_dn_get_linearized(req->op.add.message->dn)));
732 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
735 guid = class_schemaid_guid_by_lDAPDisplayName(dsdb_get_schema(ldb),
736 (char *)oc_el->values[oc_el->num_values-1].data);
738 if (!insert_in_object_tree(req, guid, SEC_ADS_CREATE_CHILD, &root, &new_node)) {
739 return LDB_ERR_OPERATIONS_ERROR;
742 ret = check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, root);
743 if (ret != LDB_SUCCESS) {
747 return ldb_next_request(module, req);
750 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
753 struct ldb_context *ldb = ldb_module_get_ctx(module);
754 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
756 bool modify_sd = false;
757 const struct GUID *guid;
758 uint32_t access_granted;
759 struct object_tree *root = NULL;
760 struct object_tree *new_node = NULL;
762 struct ldb_result *acl_res;
763 struct security_descriptor *sd;
764 struct dom_sid *sid = NULL;
765 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
766 TALLOC_CTX *tmp_ctx = talloc_new(req);
767 static const char *acl_attrs[] = {
768 "nTSecurityDescriptor",
774 if (as_system != NULL) {
775 as_system->critical = 0;
778 /* Don't print this debug statement if elements[0].name is going to be NULL */
779 if(req->op.mod.message->num_elements > 0)
781 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->elements[0].name));
783 if (dsdb_module_am_system(module) || as_system) {
784 return ldb_next_request(module, req);
786 if (ldb_dn_is_special(req->op.mod.message->dn)) {
787 return ldb_next_request(module, req);
789 ret = ldb_search(ldb, req, &acl_res, req->op.mod.message->dn,
790 LDB_SCOPE_BASE, acl_attrs, NULL);
792 if (ret != LDB_SUCCESS) {
796 ret = get_sd_from_ldb_message(req, acl_res->msgs[0], &sd);
797 if (ret != LDB_SUCCESS) {
798 DEBUG(10, ("acl_modify: cannot get descriptor\n"));
801 /* Theoretically we pass the check if the object has no sd */
806 guid = get_oc_guid_from_message(module,acl_res->msgs[0]);
808 DEBUG(10, ("acl_modify: cannot get guid\n"));
812 ret = get_dom_sid_from_ldb_message(req, acl_res->msgs[0], &sid);
813 if (ret != LDB_SUCCESS) {
814 return LDB_ERR_OPERATIONS_ERROR;
817 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
819 DEBUG(10, ("acl_modify: cannot add to object tree\n"));
822 for (i=0; i < req->op.mod.message->num_elements; i++){
823 const struct dsdb_attribute *attr;
824 /* clearTextPassword is not in schema */
825 if (strcmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
826 attr = dsdb_attribute_by_lDAPDisplayName(schema, "unicodePwd");
828 attr = dsdb_attribute_by_lDAPDisplayName(schema,
829 req->op.mod.message->elements[i].name);
831 if (strcmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
836 DEBUG(10, ("acl_modify: cannot find attribute %s\n",
837 req->op.mod.message->elements[i].name));
840 if (!insert_in_object_tree(tmp_ctx,
841 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
842 &new_node, &new_node)) {
843 DEBUG(10, ("acl_modify: cannot add to object tree securityGUID\n"));
847 if (!insert_in_object_tree(tmp_ctx,
848 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
849 DEBUG(10, ("acl_modify: cannot add to object tree attributeGUID\n"));
855 if (root->num_of_children > 0) {
856 status = sec_access_check_ds(sd, acl_user_token(module),
862 if (!NT_STATUS_IS_OK(status)) {
863 DEBUG(10, ("Object %s nas no write property access\n",
864 ldb_dn_get_linearized(req->op.mod.message->dn)));
866 acl_user_token(module),
867 req->op.mod.message->dn,
870 talloc_free(tmp_ctx);
871 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
875 status = sec_access_check_ds(sd, acl_user_token(module),
881 if (!NT_STATUS_IS_OK(status)) {
882 DEBUG(10, ("Object %s nas no write dacl access\n",
883 ldb_dn_get_linearized(req->op.mod.message->dn)));
885 acl_user_token(module),
886 req->op.mod.message->dn,
889 talloc_free(tmp_ctx);
890 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
894 talloc_free(tmp_ctx);
895 return ldb_next_request(module, req);
897 talloc_free(tmp_ctx);
898 return LDB_ERR_OPERATIONS_ERROR;
901 /* similar to the modify for the time being.
902 * We need to concider the special delete tree case, though - TODO */
903 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
906 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.del.dn);
907 struct ldb_context *ldb;
908 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
910 if (as_system != NULL) {
911 as_system->critical = 0;
914 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
915 if (dsdb_module_am_system(module) || as_system) {
916 return ldb_next_request(module, req);
919 if (ldb_dn_is_special(req->op.del.dn)) {
920 return ldb_next_request(module, req);
922 ldb = ldb_module_get_ctx(module);
923 /* first check if we have delete object right */
924 ret = check_access_on_dn(module, req, req->op.del.dn, SEC_STD_DELETE, NULL);
925 if (ret == LDB_SUCCESS) {
926 return ldb_next_request(module, req);
929 /* Nope, we don't have delete object. Lets check if we have delete child on the parent */
930 /* No parent, so check fails */
931 if ((ldb_dn_compare(req->op.del.dn, (ldb_get_schema_basedn(ldb))) == 0) ||
932 (ldb_dn_compare(req->op.del.dn, (ldb_get_config_basedn(ldb))) == 0) ||
933 (ldb_dn_compare(req->op.del.dn, (ldb_get_root_basedn(ldb))) == 0)) {
934 DEBUG(10,("acl:deleting an NC\n"));
935 return ldb_module_done(req, NULL, NULL, LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS);
938 ret = check_access_on_dn(module, req, parent, SEC_ADS_DELETE_CHILD, NULL);
939 if (ret != LDB_SUCCESS) {
942 return ldb_next_request(module, req);
945 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
948 struct ldb_dn *oldparent = ldb_dn_get_parent(req, req->op.rename.olddn);
949 struct ldb_dn *newparent = ldb_dn_get_parent(req, req->op.rename.newdn);
950 struct ldb_context *ldb;
951 struct security_descriptor *sd = NULL;
952 struct dom_sid *sid = NULL;
953 struct ldb_result *acl_res;
954 const struct GUID *guid;
955 struct object_tree *root = NULL;
956 struct object_tree *new_node = NULL;
957 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
958 TALLOC_CTX *tmp_ctx = talloc_new(req);
960 uint32_t access_granted;
961 const char *rdn_name;
962 static const char *acl_attrs[] = {
963 "nTSecurityDescriptor",
969 if (as_system != NULL) {
970 as_system->critical = 0;
973 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
974 if (dsdb_module_am_system(module) || as_system) {
975 return ldb_next_request(module, req);
977 if (ldb_dn_is_special(req->op.rename.olddn)) {
978 return ldb_next_request(module, req);
980 ldb = ldb_module_get_ctx(module);
982 /* TODO search to include deleted objects */
983 ret = ldb_search(ldb, req, &acl_res, req->op.rename.olddn,
984 LDB_SCOPE_BASE, acl_attrs, NULL);
985 /* we sould be able to find the parent */
986 if (ret != LDB_SUCCESS) {
987 DEBUG(10,("acl: failed to find object %s\n",
988 ldb_dn_get_linearized(req->op.rename.olddn)));
992 guid = get_oc_guid_from_message(module,acl_res->msgs[0]);
993 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
995 return LDB_ERR_OPERATIONS_ERROR;
998 guid = attribute_schemaid_guid_by_lDAPDisplayName(dsdb_get_schema(ldb),
1000 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1001 &new_node, &new_node)) {
1002 return LDB_ERR_OPERATIONS_ERROR;
1005 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1006 if (rdn_name == NULL) {
1007 return LDB_ERR_OPERATIONS_ERROR;
1009 guid = attribute_schemaid_guid_by_lDAPDisplayName(dsdb_get_schema(ldb),
1011 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1012 &new_node, &new_node)) {
1013 return LDB_ERR_OPERATIONS_ERROR;
1016 ret = get_sd_from_ldb_message(req, acl_res->msgs[0], &sd);
1018 if (ret != LDB_SUCCESS) {
1019 return LDB_ERR_OPERATIONS_ERROR;
1021 /* Theoretically we pass the check if the object has no sd */
1025 ret = get_dom_sid_from_ldb_message(req, acl_res->msgs[0], &sid);
1026 if (ret != LDB_SUCCESS) {
1027 return LDB_ERR_OPERATIONS_ERROR;
1030 status = sec_access_check_ds(sd, acl_user_token(module),
1036 if (!NT_STATUS_IS_OK(status)) {
1037 DEBUG(10, ("Object %s nas no wp on name\n",
1038 ldb_dn_get_linearized(req->op.rename.olddn)));
1040 acl_user_token(module),
1041 req->op.rename.olddn,
1044 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1047 if (ldb_dn_compare(oldparent, newparent) == 0) {
1048 /* regular rename, not move, nothing more to do */
1049 return ldb_next_request(module, req);
1052 /* What exactly to do in this case? It would fail anyway.. */
1053 if ((ldb_dn_compare(req->op.rename.newdn, (ldb_get_schema_basedn(ldb))) == 0) ||
1054 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_config_basedn(ldb))) == 0) ||
1055 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_root_basedn(ldb))) == 0)) {
1056 DEBUG(10,("acl:moving as an NC\n"));
1057 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1059 /* new parent should have create child */
1060 talloc_free(tmp_ctx);
1061 tmp_ctx = talloc_new(req);
1064 guid = get_oc_guid_from_message(module,acl_res->msgs[0]);
1066 DEBUG(10,("acl:renamed object has no object class\n"));
1067 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
1069 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_CREATE_CHILD,
1070 &root, &new_node)) {
1071 return LDB_ERR_OPERATIONS_ERROR;
1073 ret = check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, root);
1074 if (ret != LDB_SUCCESS) {
1075 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1078 /* do we have delete object on the object? */
1080 status = sec_access_check_ds(sd, acl_user_token(module),
1086 if (NT_STATUS_IS_OK(status)) {
1087 return ldb_next_request(module, req);
1089 /* what about delete child on the current parent */
1090 ret = check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL);
1091 if (ret != LDB_SUCCESS) {
1092 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1093 return ldb_module_done(req, NULL, NULL, ret);
1095 return ldb_next_request(module, req);
1098 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1100 struct ldb_context *ldb;
1101 struct acl_context *ac;
1102 struct acl_private *data;
1103 struct ldb_result *acl_res;
1104 static const char *acl_attrs[] = {
1106 "nTSecurityDescriptor",
1112 ac = talloc_get_type(req->context, struct acl_context);
1113 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1114 ldb = ldb_module_get_ctx(ac->module);
1117 return ldb_module_done(ac->req, NULL, NULL,
1118 LDB_ERR_OPERATIONS_ERROR);
1120 if (ares->error != LDB_SUCCESS) {
1121 return ldb_module_done(ac->req, ares->controls,
1122 ares->response, ares->error);
1125 switch (ares->type) {
1126 case LDB_REPLY_ENTRY:
1127 if (ac->allowedAttributes
1128 || ac->allowedChildClasses
1129 || ac->allowedChildClassesEffective
1130 || ac->allowedAttributesEffective
1131 || ac->sDRightsEffective) {
1132 ret = ldb_search(ldb, ac, &acl_res, ares->message->dn, LDB_SCOPE_BASE, acl_attrs, NULL);
1133 if (ret != LDB_SUCCESS) {
1134 return ldb_module_done(ac->req, NULL, NULL, ret);
1136 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1137 ret = acl_allowedAttributes(ac->module, acl_res->msgs[0], ares->message, ac);
1138 if (ret != LDB_SUCCESS) {
1139 return ldb_module_done(ac->req, NULL, NULL, ret);
1142 if (ac->allowedChildClasses) {
1143 ret = acl_childClasses(ac->module, acl_res->msgs[0],
1144 ares->message, "allowedChildClasses");
1145 if (ret != LDB_SUCCESS) {
1146 return ldb_module_done(ac->req, NULL, NULL, ret);
1149 if (ac->allowedChildClassesEffective) {
1150 ret = acl_childClassesEffective(ac->module,
1151 acl_res->msgs[0], ares->message, ac);
1152 if (ret != LDB_SUCCESS) {
1153 return ldb_module_done(ac->req, NULL, NULL, ret);
1156 if (ac->sDRightsEffective) {
1157 ret = acl_sDRightsEffective(ac->module,
1158 acl_res->msgs[0], ares->message, ac);
1159 if (ret != LDB_SUCCESS) {
1160 return ldb_module_done(ac->req, NULL, NULL, ret);
1164 if (data && data->password_attrs) {
1165 if (!ac->am_system) {
1166 for (i = 0; data->password_attrs[i]; i++) {
1167 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1171 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1173 case LDB_REPLY_REFERRAL:
1174 return ldb_module_send_referral(ac->req, ares->referral);
1176 case LDB_REPLY_DONE:
1177 return ldb_module_done(ac->req, ares->controls,
1178 ares->response, LDB_SUCCESS);
1184 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1186 struct ldb_context *ldb;
1187 struct acl_context *ac;
1188 struct ldb_request *down_req;
1189 struct acl_private *data;
1192 ldb = ldb_module_get_ctx(module);
1194 ac = talloc_zero(req, struct acl_context);
1197 return LDB_ERR_OPERATIONS_ERROR;
1199 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1201 ac->module = module;
1203 ac->am_system = dsdb_module_am_system(module);
1204 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1205 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1206 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1207 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1208 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1210 /* replace any attributes in the parse tree that are private,
1211 so we don't allow a search for 'userPassword=penguin',
1212 just as we would not allow that attribute to be returned */
1213 if (ac->am_system) {
1214 /* FIXME: We should copy the tree and keep the original unmodified. */
1215 /* remove password attributes */
1216 if (data && data->password_attrs) {
1217 for (i = 0; data->password_attrs[i]; i++) {
1218 ldb_parse_tree_attr_replace(req->op.search.tree,
1219 data->password_attrs[i],
1220 "kludgeACLredactedattribute");
1224 ret = ldb_build_search_req_ex(&down_req,
1226 req->op.search.base,
1227 req->op.search.scope,
1228 req->op.search.tree,
1229 req->op.search.attrs,
1231 ac, acl_search_callback,
1233 if (ret != LDB_SUCCESS) {
1236 /* perform the search */
1237 return ldb_next_request(module, down_req);
1240 _PUBLIC_ const struct ldb_module_ops ldb_acl_module_ops = {
1242 .search = acl_search,
1244 .modify = acl_modify,
1246 .rename = acl_rename,
1247 .init_context = acl_module_init