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;
56 enum security_user_level user_type;
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 enum security_user_level what_is_user(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");
78 return security_session_user_level(session_info);
81 static struct security_token *acl_user_token(struct ldb_module *module)
83 struct ldb_context *ldb = ldb_module_get_ctx(module);
84 struct auth_session_info *session_info
85 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
89 return session_info->security_token;
92 static int acl_module_init(struct ldb_module *module)
94 struct ldb_context *ldb;
95 struct acl_private *data;
97 TALLOC_CTX *mem_ctx = talloc_new(module);
98 static const char *attrs[] = { "passwordAttribute", NULL };
99 struct ldb_result *res;
100 struct ldb_message *msg;
101 struct ldb_message_element *password_attributes;
103 ldb = ldb_module_get_ctx(module);
105 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
106 if (ret != LDB_SUCCESS) {
107 ldb_debug(ldb, LDB_DEBUG_ERROR,
108 "acl_module_init: Unable to register control with rootdse!\n");
109 return LDB_ERR_OPERATIONS_ERROR;
112 data = talloc(module, struct acl_private);
115 return LDB_ERR_OPERATIONS_ERROR;
118 data->password_attrs = NULL;
119 data->acl_perform = lp_parm_bool(ldb_get_opaque(ldb, "loadparm"),
120 NULL, "acl", "perform", false);
121 ldb_module_set_private(module, data);
125 return LDB_ERR_OPERATIONS_ERROR;
128 ret = ldb_search(ldb, mem_ctx, &res,
129 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
130 LDB_SCOPE_BASE, attrs, NULL);
131 if (ret != LDB_SUCCESS) {
134 if (res->count == 0) {
138 if (res->count > 1) {
139 talloc_free(mem_ctx);
140 return LDB_ERR_CONSTRAINT_VIOLATION;
145 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
146 if (!password_attributes) {
149 data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
150 if (!data->password_attrs) {
151 talloc_free(mem_ctx);
153 return LDB_ERR_OPERATIONS_ERROR;
155 for (i=0; i < password_attributes->num_values; i++) {
156 data->password_attrs[i] = (const char *)password_attributes->values[i].data;
157 talloc_steal(data->password_attrs, password_attributes->values[i].data);
159 data->password_attrs[i] = NULL;
162 talloc_free(mem_ctx);
163 return ldb_next_init(module);
166 static int get_sd_from_ldb_message(TALLOC_CTX *mem_ctx,
167 struct ldb_message *acl_res,
168 struct security_descriptor **sd)
170 struct ldb_message_element *sd_element;
171 enum ndr_err_code ndr_err;
173 sd_element = ldb_msg_find_element(acl_res, "nTSecurityDescriptor");
178 *sd = talloc(mem_ctx, struct security_descriptor);
180 return LDB_ERR_OPERATIONS_ERROR;
182 ndr_err = ndr_pull_struct_blob(&sd_element->values[0], *sd, NULL, *sd,
183 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
185 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
186 return LDB_ERR_OPERATIONS_ERROR;
192 static const struct GUID *get_oc_guid_from_message(struct ldb_module *module,
193 struct ldb_message *msg)
195 struct ldb_message_element *oc_el;
196 struct ldb_context *ldb = ldb_module_get_ctx(module);
198 oc_el = ldb_msg_find_element(msg, "objectClass");
203 return class_schemaid_guid_by_lDAPDisplayName(dsdb_get_schema(ldb),
204 (char *)oc_el->values[oc_el->num_values-1].data);
207 static void acl_debug(struct security_descriptor *sd,
208 struct security_token *token,
214 DEBUG(level, ("Access on %s denied", ldb_dn_get_linearized(dn)));
216 DEBUG(level, ("Access on %s granted", ldb_dn_get_linearized(dn)));
219 DEBUG(level,("Security context: %s\n",
220 ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_token,"", token)));
221 DEBUG(level,("Security descriptor: %s\n",
222 ndr_print_struct_string(0,(ndr_print_fn_t)ndr_print_security_descriptor,"", sd)));
225 static int check_access_on_dn(struct ldb_module *module,
229 struct object_tree *tree)
232 struct ldb_context *ldb = ldb_module_get_ctx(module);
233 struct ldb_result *acl_res;
234 struct security_descriptor *sd = NULL;
236 uint32_t access_granted;
237 static const char *acl_attrs[] = {
238 "nTSecurityDescriptor",
242 ret = ldb_search(ldb, mem_ctx, &acl_res, dn, LDB_SCOPE_BASE, acl_attrs, NULL);
243 /* we sould be able to find the parent */
244 if (ret != LDB_SUCCESS) {
245 DEBUG(10,("acl: failed to find object %s\n", ldb_dn_get_linearized(dn)));
249 ret = get_sd_from_ldb_message(mem_ctx, acl_res->msgs[0], &sd);
250 if (ret != LDB_SUCCESS) {
251 return LDB_ERR_OPERATIONS_ERROR;
253 /* Theoretically we pass the check if the object has no sd */
257 status = sec_access_check_ds(sd, acl_user_token(module),
261 if (!NT_STATUS_IS_OK(status)) {
263 acl_user_token(module),
267 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
272 static int acl_check_access_on_attribute(struct ldb_module *module,
274 struct security_descriptor *sd,
276 struct dsdb_attribute *attr)
279 struct ldb_context *ldb = ldb_module_get_ctx(module);
281 uint32_t access_granted;
282 struct object_tree *root = NULL;
283 struct object_tree *new_node = NULL;
284 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
285 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
286 struct security_token *token = acl_user_token(module);
288 if (!GUID_all_zero(&attr->attributeSecurityGUID)) {
289 if (!insert_in_object_tree(tmp_ctx,
290 &attr->attributeSecurityGUID, access,
292 DEBUG(10, ("acl_search: cannot add to object tree securityGUID\n"));
296 if (!insert_in_object_tree(tmp_ctx,
297 &attr->schemaIDGUID, access, &new_node, &new_node)) {
298 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
303 if (!insert_in_object_tree(tmp_ctx,
304 &attr->schemaIDGUID, access, &root, &new_node)) {
305 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
310 status = sec_access_check_ds(sd, token,
314 if (!NT_STATUS_IS_OK(status)) {
315 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
322 return LDB_ERR_OPERATIONS_ERROR;
325 static int acl_check_access_on_class(struct ldb_module *module,
327 struct security_descriptor *sd,
329 const char *class_name)
332 struct ldb_context *ldb = ldb_module_get_ctx(module);
334 uint32_t access_granted;
335 struct object_tree *root = NULL;
336 struct object_tree *new_node = NULL;
338 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
339 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
340 struct security_token *token = acl_user_token(module);
342 guid = class_schemaid_guid_by_lDAPDisplayName(schema, class_name);
344 DEBUG(10, ("acl_search: cannot find class %s\n",
348 if (!insert_in_object_tree(tmp_ctx,
351 DEBUG(10, ("acl_search: cannot add to object tree guid\n"));
355 status = sec_access_check_ds(sd, token,
359 if (!NT_STATUS_IS_OK(status)) {
360 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
367 return LDB_ERR_OPERATIONS_ERROR;
370 static int acl_allowedAttributes(struct ldb_module *module,
371 struct ldb_message *sd_msg,
372 struct ldb_message *msg,
373 struct acl_context *ac)
375 struct ldb_message_element *oc_el;
376 struct ldb_message_element *allowedAttributes;
377 struct ldb_message_element *allowedAttributesEffective;
378 struct ldb_context *ldb = ldb_module_get_ctx(module);
379 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
381 const char **attr_list;
384 /* If we don't have a schema yet, we can't do anything... */
385 if (schema == NULL) {
389 /* Must remove any existing attribute */
390 if (ac->allowedAttributes) {
391 ldb_msg_remove_attr(msg, "allowedAttributes");
394 mem_ctx = talloc_new(msg);
397 return LDB_ERR_OPERATIONS_ERROR;
400 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
401 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
403 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
404 talloc_free(mem_ctx);
405 return LDB_ERR_OPERATIONS_ERROR;
407 if (ac->allowedAttributes) {
408 for (i=0; attr_list && attr_list[i]; i++) {
409 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
412 if (ac->allowedAttributesEffective) {
413 struct security_descriptor *sd;
414 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
415 if (ac->user_type == SECURITY_SYSTEM) {
416 for (i=0; attr_list && attr_list[i]; i++) {
417 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
422 ret = get_sd_from_ldb_message(mem_ctx, sd_msg, &sd);
424 if (ret != LDB_SUCCESS) {
427 for (i=0; attr_list && attr_list[i]; i++) {
428 struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
431 return LDB_ERR_OPERATIONS_ERROR;
433 /* remove constructed attributes */
434 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED) {
437 ret = acl_check_access_on_attribute(module,
442 if (ret == LDB_SUCCESS) {
443 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
450 static int acl_childClasses(struct ldb_module *module,
451 struct ldb_message *sd_msg,
452 struct ldb_message *msg,
453 const char *attrName)
455 struct ldb_message_element *oc_el;
456 struct ldb_message_element *allowedClasses;
457 struct ldb_context *ldb = ldb_module_get_ctx(module);
458 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
459 const struct dsdb_class *sclass;
462 /* If we don't have a schema yet, we can't do anything... */
463 if (schema == NULL) {
467 /* Must remove any existing attribute, or else confusion reins */
468 ldb_msg_remove_attr(msg, attrName);
469 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
470 if (ret != LDB_SUCCESS) {
474 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
476 for (i=0; oc_el && i < oc_el->num_values; i++) {
477 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
479 /* We don't know this class? what is going on? */
483 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
484 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
487 if (allowedClasses->num_values > 1) {
488 qsort(allowedClasses->values,
489 allowedClasses->num_values,
490 sizeof(*allowedClasses->values),
491 (comparison_fn_t)data_blob_cmp);
493 for (i=1 ; i < allowedClasses->num_values; i++) {
494 struct ldb_val *val1 = &allowedClasses->values[i-1];
495 struct ldb_val *val2 = &allowedClasses->values[i];
496 if (data_blob_cmp(val1, val2) == 0) {
497 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
498 allowedClasses->num_values--;
507 static int acl_childClassesEffective(struct ldb_module *module,
508 struct ldb_message *sd_msg,
509 struct ldb_message *msg,
510 struct acl_context *ac)
512 struct ldb_message_element *oc_el;
513 struct ldb_message_element *allowedClasses = NULL;
514 struct ldb_context *ldb = ldb_module_get_ctx(module);
515 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
516 const struct dsdb_class *sclass;
517 struct security_descriptor *sd;
520 if (ac->user_type == SECURITY_SYSTEM) {
521 return acl_childClasses(module, sd_msg, msg, "allowedChildClassesEffective");
524 /* If we don't have a schema yet, we can't do anything... */
525 if (schema == NULL) {
529 /* Must remove any existing attribute, or else confusion reins */
530 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
532 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
533 ret = get_sd_from_ldb_message(msg, sd_msg, &sd);
534 if (ret != LDB_SUCCESS) {
538 for (i=0; oc_el && i < oc_el->num_values; i++) {
539 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
541 /* We don't know this class? what is going on? */
545 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
546 ret = acl_check_access_on_class(module,
549 SEC_ADS_CREATE_CHILD,
550 sclass->possibleInferiors[j]);
551 if (ret == LDB_SUCCESS) {
552 ldb_msg_add_string(msg, "allowedChildClassesEffective",
553 sclass->possibleInferiors[j]);
557 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
558 if (!allowedClasses) {
562 if (allowedClasses->num_values > 1) {
563 qsort(allowedClasses->values,
564 allowedClasses->num_values,
565 sizeof(*allowedClasses->values),
566 (comparison_fn_t)data_blob_cmp);
567 for (i=1 ; i < allowedClasses->num_values; i++) {
568 struct ldb_val *val1 = &allowedClasses->values[i-1];
569 struct ldb_val *val2 = &allowedClasses->values[i];
570 if (data_blob_cmp(val1, val2) == 0) {
571 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
572 allowedClasses->num_values--;
580 static int acl_sDRightsEffective(struct ldb_module *module,
581 struct ldb_message *sd_msg,
582 struct ldb_message *msg,
583 struct acl_context *ac)
585 struct ldb_message_element *rightsEffective;
587 struct security_descriptor *sd;
590 /* Must remove any existing attribute, or else confusion reins */
591 ldb_msg_remove_attr(msg, "sDRightsEffective");
592 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
593 if (ret != LDB_SUCCESS) {
596 if (ac->user_type == SECURITY_SYSTEM) {
597 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
600 /* Get the security descriptor from the message */
601 ret = get_sd_from_ldb_message(msg, sd_msg, &sd);
602 if (ret != LDB_SUCCESS) {
606 ret = acl_check_access_on_attribute(module,
611 if (ret == LDB_SUCCESS) {
612 flags |= SECINFO_OWNER | SECINFO_GROUP;
614 ret = acl_check_access_on_attribute(module,
619 if (ret == LDB_SUCCESS) {
620 flags |= SECINFO_DACL;
622 ret = acl_check_access_on_attribute(module,
625 SEC_FLAG_SYSTEM_SECURITY,
627 if (ret == LDB_SUCCESS) {
628 flags |= SECINFO_SACL;
631 ldb_msg_add_fmt(msg, "sDRightsEffective", "%u", flags);
635 static int acl_add(struct ldb_module *module, struct ldb_request *req)
638 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.add.message->dn);
639 struct ldb_context *ldb;
640 struct ldb_message_element *oc_el;
641 const struct GUID *guid;
642 struct object_tree *root = NULL;
643 struct object_tree *new_node = NULL;
645 if (what_is_user(module) == SECURITY_SYSTEM) {
646 return ldb_next_request(module, req);
649 if (ldb_dn_is_special(req->op.add.message->dn)) {
650 return ldb_next_request(module, req);
652 ldb = ldb_module_get_ctx(module);
653 /* Creating an NC. There is probably something we should do here,
654 * but we will establish that later */
655 if ((ldb_dn_compare(req->op.add.message->dn, (ldb_get_schema_basedn(ldb))) == 0) ||
656 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_config_basedn(ldb))) == 0) ||
657 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_root_basedn(ldb))) == 0)) {
658 return ldb_next_request(module, req);
661 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
662 if (!oc_el || oc_el->num_values == 0) {
663 DEBUG(10,("acl:operation error %s\n", ldb_dn_get_linearized(req->op.add.message->dn)));
664 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
667 guid = class_schemaid_guid_by_lDAPDisplayName(dsdb_get_schema(ldb),
668 (char *)oc_el->values[oc_el->num_values-1].data);
670 if (!insert_in_object_tree(req, guid, SEC_ADS_CREATE_CHILD, &root, &new_node)) {
671 return LDB_ERR_OPERATIONS_ERROR;
674 ret = check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, root);
675 if (ret != LDB_SUCCESS) {
679 return ldb_next_request(module, req);
682 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
685 struct ldb_context *ldb = ldb_module_get_ctx(module);
686 const struct dsdb_schema *schema = dsdb_get_schema(ldb);
688 bool modify_sd = false;
689 const struct GUID *guid;
690 uint32_t access_granted;
691 struct object_tree *root = NULL;
692 struct object_tree *new_node = NULL;
694 struct ldb_result *acl_res;
695 struct security_descriptor *sd;
696 TALLOC_CTX *tmp_ctx = talloc_new(req);
697 static const char *acl_attrs[] = {
698 "nTSecurityDescriptor",
703 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->elements[0].name));
704 if (what_is_user(module) == SECURITY_SYSTEM) {
705 return ldb_next_request(module, req);
707 if (ldb_dn_is_special(req->op.mod.message->dn)) {
708 return ldb_next_request(module, req);
710 ret = ldb_search(ldb, req, &acl_res, req->op.mod.message->dn,
711 LDB_SCOPE_BASE, acl_attrs, NULL);
713 if (ret != LDB_SUCCESS) {
717 ret = get_sd_from_ldb_message(req, acl_res->msgs[0], &sd);
718 if (ret != LDB_SUCCESS) {
719 DEBUG(10, ("acl_modify: cannot get descriptor\n"));
722 /* Theoretically we pass the check if the object has no sd */
727 guid = get_oc_guid_from_message(module,acl_res->msgs[0]);
729 DEBUG(10, ("acl_modify: cannot get guid\n"));
733 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
735 DEBUG(10, ("acl_modify: cannot add to object tree\n"));
738 for (i=0; i < req->op.mod.message->num_elements; i++){
739 const struct dsdb_attribute *attr;
740 /* clearTextPassword is not in schema */
741 if (strcmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
742 attr = dsdb_attribute_by_lDAPDisplayName(schema, "unicodePwd");
744 attr = dsdb_attribute_by_lDAPDisplayName(schema,
745 req->op.mod.message->elements[i].name);
747 if (strcmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
752 DEBUG(10, ("acl_modify: cannot find attribute %s\n",
753 req->op.mod.message->elements[i].name));
756 if (!insert_in_object_tree(tmp_ctx,
757 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
758 &new_node, &new_node)) {
759 DEBUG(10, ("acl_modify: cannot add to object tree securityGUID\n"));
763 if (!insert_in_object_tree(tmp_ctx,
764 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
765 DEBUG(10, ("acl_modify: cannot add to object tree attributeGUID\n"));
771 if (root->num_of_children > 0) {
772 status = sec_access_check_ds(sd, acl_user_token(module),
777 if (!NT_STATUS_IS_OK(status)) {
778 DEBUG(10, ("Object %s nas no write property access\n",
779 ldb_dn_get_linearized(req->op.mod.message->dn)));
781 acl_user_token(module),
782 req->op.mod.message->dn,
785 talloc_free(tmp_ctx);
786 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
790 status = sec_access_check_ds(sd, acl_user_token(module),
795 if (!NT_STATUS_IS_OK(status)) {
796 DEBUG(10, ("Object %s nas no write dacl access\n",
797 ldb_dn_get_linearized(req->op.mod.message->dn)));
799 acl_user_token(module),
800 req->op.mod.message->dn,
803 talloc_free(tmp_ctx);
804 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
808 talloc_free(tmp_ctx);
809 return ldb_next_request(module, req);
811 talloc_free(tmp_ctx);
812 return LDB_ERR_OPERATIONS_ERROR;
815 /* similar to the modify for the time being.
816 * We need to concider the special delete tree case, though - TODO */
817 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
820 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.del.dn);
821 struct ldb_context *ldb;
823 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
824 if (what_is_user(module) == SECURITY_SYSTEM) {
825 return ldb_next_request(module, req);
828 if (ldb_dn_is_special(req->op.del.dn)) {
829 return ldb_next_request(module, req);
831 ldb = ldb_module_get_ctx(module);
832 /* first check if we have delete object right */
833 ret = check_access_on_dn(module, req, req->op.del.dn, SEC_STD_DELETE, NULL);
834 if (ret == LDB_SUCCESS) {
835 return ldb_next_request(module, req);
838 /* Nope, we don't have delete object. Lets check if we have delete child on the parent */
839 /* No parent, so check fails */
840 if ((ldb_dn_compare(req->op.del.dn, (ldb_get_schema_basedn(ldb))) == 0) ||
841 (ldb_dn_compare(req->op.del.dn, (ldb_get_config_basedn(ldb))) == 0) ||
842 (ldb_dn_compare(req->op.del.dn, (ldb_get_root_basedn(ldb))) == 0)) {
843 DEBUG(10,("acl:deleting an NC\n"));
844 return ldb_module_done(req, NULL, NULL, LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS);
847 ret = check_access_on_dn(module, req, parent, SEC_ADS_DELETE_CHILD, NULL);
848 if (ret != LDB_SUCCESS) {
851 return ldb_next_request(module, req);
854 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
857 struct ldb_dn *oldparent = ldb_dn_get_parent(req, req->op.rename.olddn);
858 struct ldb_dn *newparent = ldb_dn_get_parent(req, req->op.rename.newdn);
859 struct ldb_context *ldb;
860 struct security_descriptor *sd = NULL;
861 struct ldb_result *acl_res;
862 const struct GUID *guid;
863 struct object_tree *root = NULL;
864 struct object_tree *new_node = NULL;
865 TALLOC_CTX *tmp_ctx = talloc_new(req);
867 uint32_t access_granted;
868 static const char *acl_attrs[] = {
869 "nTSecurityDescriptor",
874 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
875 if (what_is_user(module) == SECURITY_SYSTEM) {
876 return ldb_next_request(module, req);
878 if (ldb_dn_is_special(req->op.rename.olddn)) {
879 return ldb_next_request(module, req);
881 ldb = ldb_module_get_ctx(module);
883 /* TODO search to include deleted objects */
884 ret = ldb_search(ldb, req, &acl_res, req->op.rename.olddn,
885 LDB_SCOPE_BASE, acl_attrs, NULL);
886 /* we sould be able to find the parent */
887 if (ret != LDB_SUCCESS) {
888 DEBUG(10,("acl: failed to find object %s\n",
889 ldb_dn_get_linearized(req->op.rename.olddn)));
893 guid = get_oc_guid_from_message(module,acl_res->msgs[0]);
894 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
896 return LDB_ERR_OPERATIONS_ERROR;
899 guid = attribute_schemaid_guid_by_lDAPDisplayName(dsdb_get_schema(ldb),
901 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
902 &new_node, &new_node)) {
903 return LDB_ERR_OPERATIONS_ERROR;
906 ret = get_sd_from_ldb_message(req, acl_res->msgs[0], &sd);
908 if (ret != LDB_SUCCESS) {
909 return LDB_ERR_OPERATIONS_ERROR;
911 /* Theoretically we pass the check if the object has no sd */
915 status = sec_access_check_ds(sd, acl_user_token(module),
920 if (!NT_STATUS_IS_OK(status)) {
921 DEBUG(10, ("Object %s nas no wp on name\n",
922 ldb_dn_get_linearized(req->op.rename.olddn)));
924 acl_user_token(module),
925 req->op.rename.olddn,
928 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
931 if (ldb_dn_compare(oldparent, newparent) == 0) {
932 /* regular rename, not move, nothing more to do */
933 return ldb_next_request(module, req);
936 /* What exactly to do in this case? It would fail anyway.. */
937 if ((ldb_dn_compare(req->op.rename.newdn, (ldb_get_schema_basedn(ldb))) == 0) ||
938 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_config_basedn(ldb))) == 0) ||
939 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_root_basedn(ldb))) == 0)) {
940 DEBUG(10,("acl:moving as an NC\n"));
941 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
943 /* new parent should have create child */
944 talloc_free(tmp_ctx);
945 tmp_ctx = talloc_new(req);
948 guid = get_oc_guid_from_message(module,acl_res->msgs[0]);
950 DEBUG(10,("acl:renamed object has no object class\n"));
951 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
953 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_CREATE_CHILD,
955 return LDB_ERR_OPERATIONS_ERROR;
957 ret = check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, root);
958 if (ret != LDB_SUCCESS) {
959 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
962 /* do we have delete object on the object? */
964 status = sec_access_check_ds(sd, acl_user_token(module),
969 if (NT_STATUS_IS_OK(status)) {
970 return ldb_next_request(module, req);
972 /* what about delete child on the current parent */
973 ret = check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL);
974 if (ret != LDB_SUCCESS) {
975 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
976 return ldb_module_done(req, NULL, NULL, ret);
978 return ldb_next_request(module, req);
981 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
983 struct ldb_context *ldb;
984 struct acl_context *ac;
985 struct acl_private *data;
986 struct ldb_result *acl_res;
987 static const char *acl_attrs[] = {
989 "nTSecurityDescriptor",
994 ac = talloc_get_type(req->context, struct acl_context);
995 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
996 ldb = ldb_module_get_ctx(ac->module);
999 return ldb_module_done(ac->req, NULL, NULL,
1000 LDB_ERR_OPERATIONS_ERROR);
1002 if (ares->error != LDB_SUCCESS) {
1003 return ldb_module_done(ac->req, ares->controls,
1004 ares->response, ares->error);
1007 switch (ares->type) {
1008 case LDB_REPLY_ENTRY:
1009 if (ac->allowedAttributes
1010 || ac->allowedChildClasses
1011 || ac->allowedChildClassesEffective
1012 || ac->allowedAttributesEffective
1013 || ac->sDRightsEffective) {
1014 ret = ldb_search(ldb, ac, &acl_res, ares->message->dn, LDB_SCOPE_BASE, acl_attrs, NULL);
1015 if (ret != LDB_SUCCESS) {
1016 return ldb_module_done(ac->req, NULL, NULL, ret);
1018 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1019 ret = acl_allowedAttributes(ac->module, acl_res->msgs[0], ares->message, ac);
1020 if (ret != LDB_SUCCESS) {
1021 return ldb_module_done(ac->req, NULL, NULL, ret);
1024 if (ac->allowedChildClasses) {
1025 ret = acl_childClasses(ac->module, acl_res->msgs[0],
1026 ares->message, "allowedChildClasses");
1027 if (ret != LDB_SUCCESS) {
1028 return ldb_module_done(ac->req, NULL, NULL, ret);
1031 if (ac->allowedChildClassesEffective) {
1032 ret = acl_childClassesEffective(ac->module,
1033 acl_res->msgs[0], ares->message, ac);
1034 if (ret != LDB_SUCCESS) {
1035 return ldb_module_done(ac->req, NULL, NULL, ret);
1038 if (ac->sDRightsEffective) {
1039 ret = acl_sDRightsEffective(ac->module,
1040 acl_res->msgs[0], ares->message, ac);
1041 if (ret != LDB_SUCCESS) {
1042 return ldb_module_done(ac->req, NULL, NULL, ret);
1046 if (data && data->password_attrs) {
1047 if (ac->user_type != SECURITY_SYSTEM) {
1048 for (i = 0; data->password_attrs[i]; i++) {
1049 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1053 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1055 case LDB_REPLY_REFERRAL:
1056 return ldb_module_send_referral(ac->req, ares->referral);
1058 case LDB_REPLY_DONE:
1059 return ldb_module_done(ac->req, ares->controls,
1060 ares->response, LDB_SUCCESS);
1066 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1068 struct ldb_context *ldb;
1069 struct acl_context *ac;
1070 struct ldb_request *down_req;
1071 struct acl_private *data;
1074 ldb = ldb_module_get_ctx(module);
1076 ac = talloc_zero(req, struct acl_context);
1079 return LDB_ERR_OPERATIONS_ERROR;
1081 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1083 ac->module = module;
1085 ac->user_type = what_is_user(module);
1086 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1087 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1088 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1089 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1090 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1092 /* replace any attributes in the parse tree that are private,
1093 so we don't allow a search for 'userPassword=penguin',
1094 just as we would not allow that attribute to be returned */
1095 if (ac->user_type != SECURITY_SYSTEM) {
1096 /* FIXME: We should copy the tree and keep the original unmodified. */
1097 /* remove password attributes */
1098 if (data && data->password_attrs) {
1099 for (i = 0; data->password_attrs[i]; i++) {
1100 ldb_parse_tree_attr_replace(req->op.search.tree,
1101 data->password_attrs[i],
1102 "kludgeACLredactedattribute");
1106 ret = ldb_build_search_req_ex(&down_req,
1108 req->op.search.base,
1109 req->op.search.scope,
1110 req->op.search.tree,
1111 req->op.search.attrs,
1113 ac, acl_search_callback,
1115 if (ret != LDB_SUCCESS) {
1118 /* perform the search */
1119 return ldb_next_request(module, down_req);
1122 _PUBLIC_ const struct ldb_module_ops ldb_acl_module_ops = {
1124 .search = acl_search,
1126 .modify = acl_modify,
1128 .rename = acl_rename,
1129 .init_context = acl_module_init