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;
64 struct dsdb_schema *schema;
67 bool is_root_base_dn(struct ldb_context *ldb, struct ldb_dn *dn_to_check)
70 struct ldb_dn *root_base_dn = ldb_get_root_basedn(ldb);
71 result = ldb_dn_compare(root_base_dn,dn_to_check);
75 static struct security_token *acl_user_token(struct ldb_module *module)
77 struct ldb_context *ldb = ldb_module_get_ctx(module);
78 struct auth_session_info *session_info
79 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
83 return session_info->security_token;
86 /* performs an access check from inside the module stack
87 * given the dn of the object to be checked, the required access
88 * guid is either the guid of the extended right, or NULL
91 int dsdb_module_check_access_on_dn(struct ldb_module *module,
95 const struct GUID *guid)
98 struct ldb_result *acl_res;
99 static const char *acl_attrs[] = {
100 "nTSecurityDescriptor",
104 struct ldb_context *ldb = ldb_module_get_ctx(module);
105 struct auth_session_info *session_info
106 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
108 return LDB_ERR_OPERATIONS_ERROR;
110 ret = dsdb_module_search_dn(module, mem_ctx, &acl_res, dn,
111 acl_attrs, DSDB_SEARCH_SHOW_DELETED);
112 if (ret != LDB_SUCCESS) {
113 DEBUG(10,("access_check: failed to find object %s\n", ldb_dn_get_linearized(dn)));
116 return dsdb_check_access_on_dn_internal(acl_res,
118 session_info->security_token,
125 static int acl_module_init(struct ldb_module *module)
127 struct ldb_context *ldb;
128 struct acl_private *data;
130 TALLOC_CTX *mem_ctx = talloc_new(module);
131 static const char *attrs[] = { "passwordAttribute", NULL };
132 struct ldb_result *res;
133 struct ldb_message *msg;
134 struct ldb_message_element *password_attributes;
136 ldb = ldb_module_get_ctx(module);
138 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
139 if (ret != LDB_SUCCESS) {
140 ldb_debug(ldb, LDB_DEBUG_ERROR,
141 "acl_module_init: Unable to register control with rootdse!\n");
142 return LDB_ERR_OPERATIONS_ERROR;
145 data = talloc(module, struct acl_private);
148 return LDB_ERR_OPERATIONS_ERROR;
151 data->password_attrs = NULL;
152 data->acl_perform = lp_parm_bool(ldb_get_opaque(ldb, "loadparm"),
153 NULL, "acl", "perform", false);
154 ldb_module_set_private(module, data);
158 return LDB_ERR_OPERATIONS_ERROR;
161 ret = dsdb_module_search_dn(module, mem_ctx, &res,
162 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
164 if (ret != LDB_SUCCESS) {
167 if (res->count == 0) {
171 if (res->count > 1) {
172 talloc_free(mem_ctx);
173 return LDB_ERR_CONSTRAINT_VIOLATION;
178 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
179 if (!password_attributes) {
182 data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
183 if (!data->password_attrs) {
184 talloc_free(mem_ctx);
186 return LDB_ERR_OPERATIONS_ERROR;
188 for (i=0; i < password_attributes->num_values; i++) {
189 data->password_attrs[i] = (const char *)password_attributes->values[i].data;
190 talloc_steal(data->password_attrs, password_attributes->values[i].data);
192 data->password_attrs[i] = NULL;
195 talloc_free(mem_ctx);
196 return ldb_next_init(module);
199 static const struct GUID *get_oc_guid_from_message(struct ldb_module *module,
200 const struct dsdb_schema *schema,
201 struct ldb_message *msg)
203 struct ldb_message_element *oc_el;
205 oc_el = ldb_msg_find_element(msg, "objectClass");
210 return class_schemaid_guid_by_lDAPDisplayName(schema,
211 (char *)oc_el->values[oc_el->num_values-1].data);
214 static int acl_check_access_on_attribute(struct ldb_module *module,
216 struct security_descriptor *sd,
217 struct dom_sid *rp_sid,
219 const struct dsdb_attribute *attr)
223 uint32_t access_granted;
224 struct object_tree *root = NULL;
225 struct object_tree *new_node = NULL;
226 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
227 struct security_token *token = acl_user_token(module);
229 if (!GUID_all_zero(&attr->attributeSecurityGUID)) {
230 if (!insert_in_object_tree(tmp_ctx,
231 &attr->attributeSecurityGUID, access,
233 DEBUG(10, ("acl_search: cannot add to object tree securityGUID\n"));
237 if (!insert_in_object_tree(tmp_ctx,
238 &attr->schemaIDGUID, access, &new_node, &new_node)) {
239 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
244 if (!insert_in_object_tree(tmp_ctx,
245 &attr->schemaIDGUID, access, &root, &new_node)) {
246 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
251 status = sec_access_check_ds(sd, token,
256 if (!NT_STATUS_IS_OK(status)) {
257 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
264 return LDB_ERR_OPERATIONS_ERROR;
267 static int acl_check_access_on_class(struct ldb_module *module,
268 const struct dsdb_schema *schema,
270 struct security_descriptor *sd,
271 struct dom_sid *rp_sid,
273 const char *class_name)
277 uint32_t access_granted;
278 struct object_tree *root = NULL;
279 struct object_tree *new_node = NULL;
280 const struct GUID *guid;
281 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
282 struct security_token *token = acl_user_token(module);
284 guid = class_schemaid_guid_by_lDAPDisplayName(schema, class_name);
286 DEBUG(10, ("acl_search: cannot find class %s\n",
290 if (!insert_in_object_tree(tmp_ctx,
293 DEBUG(10, ("acl_search: cannot add to object tree guid\n"));
297 status = sec_access_check_ds(sd, token,
302 if (!NT_STATUS_IS_OK(status)) {
303 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
310 return LDB_ERR_OPERATIONS_ERROR;
313 static int acl_allowedAttributes(struct ldb_module *module,
314 const struct dsdb_schema *schema,
315 struct ldb_message *sd_msg,
316 struct ldb_message *msg,
317 struct acl_context *ac)
319 struct ldb_message_element *oc_el;
320 struct ldb_context *ldb = ldb_module_get_ctx(module);
322 const char **attr_list;
325 /* If we don't have a schema yet, we can't do anything... */
326 if (schema == NULL) {
327 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
328 return LDB_ERR_OPERATIONS_ERROR;
331 /* Must remove any existing attribute */
332 if (ac->allowedAttributes) {
333 ldb_msg_remove_attr(msg, "allowedAttributes");
336 mem_ctx = talloc_new(msg);
339 return LDB_ERR_OPERATIONS_ERROR;
342 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
343 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
345 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
346 talloc_free(mem_ctx);
347 return LDB_ERR_OPERATIONS_ERROR;
349 if (ac->allowedAttributes) {
350 for (i=0; attr_list && attr_list[i]; i++) {
351 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
354 if (ac->allowedAttributesEffective) {
355 struct security_descriptor *sd;
356 struct dom_sid *sid = NULL;
357 struct ldb_control *as_system = ldb_request_get_control(ac->req,
358 LDB_CONTROL_AS_SYSTEM_OID);
360 if (as_system != NULL) {
361 as_system->critical = 0;
364 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
365 if (ac->am_system || as_system) {
366 for (i=0; attr_list && attr_list[i]; i++) {
367 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
372 ret = dsdb_get_sd_from_ldb_message(mem_ctx, sd_msg, &sd);
374 if (ret != LDB_SUCCESS) {
378 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
379 for (i=0; attr_list && attr_list[i]; i++) {
380 const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
383 return LDB_ERR_OPERATIONS_ERROR;
385 /* remove constructed attributes */
386 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
388 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
391 ret = acl_check_access_on_attribute(module,
397 if (ret == LDB_SUCCESS) {
398 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
405 static int acl_childClasses(struct ldb_module *module,
406 const struct dsdb_schema *schema,
407 struct ldb_message *sd_msg,
408 struct ldb_message *msg,
409 const char *attrName)
411 struct ldb_message_element *oc_el;
412 struct ldb_message_element *allowedClasses;
413 const struct dsdb_class *sclass;
417 /* If we don't have a schema yet, we can't do anything... */
418 if (schema == NULL) {
419 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
420 return LDB_ERR_OPERATIONS_ERROR;
423 /* Must remove any existing attribute, or else confusion reins */
424 ldb_msg_remove_attr(msg, attrName);
425 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
426 if (ret != LDB_SUCCESS) {
430 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
432 for (i=0; oc_el && i < oc_el->num_values; i++) {
433 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
435 /* We don't know this class? what is going on? */
439 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
440 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
443 if (allowedClasses->num_values > 1) {
444 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
445 for (i=1 ; i < allowedClasses->num_values; i++) {
446 struct ldb_val *val1 = &allowedClasses->values[i-1];
447 struct ldb_val *val2 = &allowedClasses->values[i];
448 if (data_blob_cmp(val1, val2) == 0) {
449 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
450 allowedClasses->num_values--;
459 static int acl_childClassesEffective(struct ldb_module *module,
460 const struct dsdb_schema *schema,
461 struct ldb_message *sd_msg,
462 struct ldb_message *msg,
463 struct acl_context *ac)
465 struct ldb_message_element *oc_el;
466 struct ldb_message_element *allowedClasses = NULL;
467 const struct dsdb_class *sclass;
468 struct security_descriptor *sd;
469 struct ldb_control *as_system = ldb_request_get_control(ac->req,
470 LDB_CONTROL_AS_SYSTEM_OID);
471 struct dom_sid *sid = NULL;
475 if (as_system != NULL) {
476 as_system->critical = 0;
479 if (ac->am_system || as_system) {
480 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
483 /* If we don't have a schema yet, we can't do anything... */
484 if (schema == NULL) {
485 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
486 return LDB_ERR_OPERATIONS_ERROR;
489 /* Must remove any existing attribute, or else confusion reins */
490 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
492 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
493 ret = dsdb_get_sd_from_ldb_message(msg, sd_msg, &sd);
494 if (ret != LDB_SUCCESS) {
498 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
499 for (i=0; oc_el && i < oc_el->num_values; i++) {
500 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
502 /* We don't know this class? what is going on? */
506 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
507 ret = acl_check_access_on_class(module,
512 SEC_ADS_CREATE_CHILD,
513 sclass->possibleInferiors[j]);
514 if (ret == LDB_SUCCESS) {
515 ldb_msg_add_string(msg, "allowedChildClassesEffective",
516 sclass->possibleInferiors[j]);
520 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
521 if (!allowedClasses) {
525 if (allowedClasses->num_values > 1) {
526 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
527 for (i=1 ; i < allowedClasses->num_values; i++) {
528 struct ldb_val *val1 = &allowedClasses->values[i-1];
529 struct ldb_val *val2 = &allowedClasses->values[i];
530 if (data_blob_cmp(val1, val2) == 0) {
531 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
532 allowedClasses->num_values--;
540 static int acl_sDRightsEffective(struct ldb_module *module,
541 struct ldb_message *sd_msg,
542 struct ldb_message *msg,
543 struct acl_context *ac)
545 struct ldb_message_element *rightsEffective;
547 struct security_descriptor *sd;
548 struct ldb_control *as_system = ldb_request_get_control(ac->req,
549 LDB_CONTROL_AS_SYSTEM_OID);
550 struct dom_sid *sid = NULL;
553 if (as_system != NULL) {
554 as_system->critical = 0;
557 /* Must remove any existing attribute, or else confusion reins */
558 ldb_msg_remove_attr(msg, "sDRightsEffective");
559 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
560 if (ret != LDB_SUCCESS) {
563 if (ac->am_system || as_system) {
564 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
567 /* Get the security descriptor from the message */
568 ret = dsdb_get_sd_from_ldb_message(msg, sd_msg, &sd);
569 if (ret != LDB_SUCCESS) {
572 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
573 ret = acl_check_access_on_attribute(module,
579 if (ret == LDB_SUCCESS) {
580 flags |= SECINFO_OWNER | SECINFO_GROUP;
582 ret = acl_check_access_on_attribute(module,
588 if (ret == LDB_SUCCESS) {
589 flags |= SECINFO_DACL;
591 ret = acl_check_access_on_attribute(module,
595 SEC_FLAG_SYSTEM_SECURITY,
597 if (ret == LDB_SUCCESS) {
598 flags |= SECINFO_SACL;
601 ldb_msg_add_fmt(msg, "sDRightsEffective", "%u", flags);
605 static int acl_add(struct ldb_module *module, struct ldb_request *req)
608 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.add.message->dn);
609 struct ldb_context *ldb;
610 const struct dsdb_schema *schema;
611 struct ldb_message_element *oc_el;
612 const struct GUID *guid;
613 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
615 if (as_system != NULL) {
616 as_system->critical = 0;
619 if (dsdb_module_am_system(module) || as_system) {
620 return ldb_next_request(module, req);
623 if (ldb_dn_is_special(req->op.add.message->dn)) {
624 return ldb_next_request(module, req);
626 ldb = ldb_module_get_ctx(module);
627 /* Creating an NC. There is probably something we should do here,
628 * but we will establish that later */
629 /* FIXME: this has to be made dynamic at some point */
630 if ((ldb_dn_compare(req->op.add.message->dn, (ldb_get_schema_basedn(ldb))) == 0) ||
631 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_config_basedn(ldb))) == 0) ||
632 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_default_basedn(ldb))) == 0) ||
633 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_root_basedn(ldb))) == 0)) {
634 return ldb_next_request(module, req);
637 schema = dsdb_get_schema(ldb, req);
639 return LDB_ERR_OPERATIONS_ERROR;
642 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
643 if (!oc_el || oc_el->num_values == 0) {
644 DEBUG(10,("acl:operation error %s\n", ldb_dn_get_linearized(req->op.add.message->dn)));
645 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
648 guid = class_schemaid_guid_by_lDAPDisplayName(schema,
649 (char *)oc_el->values[oc_el->num_values-1].data);
650 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid);
651 if (ret != LDB_SUCCESS) {
654 return ldb_next_request(module, req);
657 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
660 struct ldb_context *ldb = ldb_module_get_ctx(module);
661 const struct dsdb_schema *schema;
663 bool modify_sd = false;
664 const struct GUID *guid;
665 uint32_t access_granted;
666 struct object_tree *root = NULL;
667 struct object_tree *new_node = NULL;
669 struct ldb_result *acl_res;
670 struct security_descriptor *sd;
671 struct dom_sid *sid = NULL;
672 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
673 TALLOC_CTX *tmp_ctx = talloc_new(req);
674 static const char *acl_attrs[] = {
675 "nTSecurityDescriptor",
681 if (as_system != NULL) {
682 as_system->critical = 0;
685 /* Don't print this debug statement if elements[0].name is going to be NULL */
686 if(req->op.mod.message->num_elements > 0)
688 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->elements[0].name));
690 if (dsdb_module_am_system(module) || as_system) {
691 return ldb_next_request(module, req);
693 if (ldb_dn_is_special(req->op.mod.message->dn)) {
694 return ldb_next_request(module, req);
696 ret = dsdb_module_search_dn(module, req, &acl_res, req->op.mod.message->dn,
699 if (ret != LDB_SUCCESS) {
703 schema = dsdb_get_schema(ldb, acl_res);
705 talloc_free(acl_res);
706 return LDB_ERR_OPERATIONS_ERROR;
709 ret = dsdb_get_sd_from_ldb_message(req, acl_res->msgs[0], &sd);
710 if (ret != LDB_SUCCESS) {
711 DEBUG(10, ("acl_modify: cannot get descriptor\n"));
714 /* Theoretically we pass the check if the object has no sd */
719 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
721 DEBUG(10, ("acl_modify: cannot get guid\n"));
724 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
725 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
727 DEBUG(10, ("acl_modify: cannot add to object tree\n"));
730 for (i=0; i < req->op.mod.message->num_elements; i++){
731 const struct dsdb_attribute *attr;
732 /* clearTextPassword is not in schema */
733 if (ldb_attr_cmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
734 attr = dsdb_attribute_by_lDAPDisplayName(schema, "unicodePwd");
736 attr = dsdb_attribute_by_lDAPDisplayName(schema,
737 req->op.mod.message->elements[i].name);
740 /* This basic attribute existence check with the right errorcode
741 * is needed since this module is the first one which requests
742 * schema attribute informations.
743 * The complete attribute checking is done in the
744 * "objectclass_attrs" module behind this one.
747 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' on entry '%s' was not found in the schema!",
748 req->op.mod.message->elements[i].name,
749 ldb_dn_get_linearized(req->op.mod.message->dn));
750 talloc_free(tmp_ctx);
751 return LDB_ERR_NO_SUCH_ATTRIBUTE;
754 if (ldb_attr_cmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
758 if (!insert_in_object_tree(tmp_ctx,
759 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
760 &new_node, &new_node)) {
761 DEBUG(10, ("acl_modify: cannot add to object tree securityGUID\n"));
765 if (!insert_in_object_tree(tmp_ctx,
766 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
767 DEBUG(10, ("acl_modify: cannot add to object tree attributeGUID\n"));
773 if (root->num_of_children > 0) {
774 status = sec_access_check_ds(sd, acl_user_token(module),
780 if (!NT_STATUS_IS_OK(status)) {
781 DEBUG(10, ("Object %s has no write property access\n",
782 ldb_dn_get_linearized(req->op.mod.message->dn)));
784 acl_user_token(module),
785 req->op.mod.message->dn,
788 talloc_free(tmp_ctx);
789 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
793 status = sec_access_check_ds(sd, acl_user_token(module),
799 if (!NT_STATUS_IS_OK(status)) {
800 DEBUG(10, ("Object %s has no write dacl access\n",
801 ldb_dn_get_linearized(req->op.mod.message->dn)));
803 acl_user_token(module),
804 req->op.mod.message->dn,
807 talloc_free(tmp_ctx);
808 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
812 talloc_free(tmp_ctx);
813 return ldb_next_request(module, req);
815 talloc_free(tmp_ctx);
816 return LDB_ERR_OPERATIONS_ERROR;
819 /* similar to the modify for the time being.
820 * We need to concider the special delete tree case, though - TODO */
821 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
824 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.del.dn);
825 struct ldb_context *ldb;
826 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
828 if (as_system != NULL) {
829 as_system->critical = 0;
832 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
833 if (dsdb_module_am_system(module) || as_system) {
834 return ldb_next_request(module, req);
837 if (ldb_dn_is_special(req->op.del.dn)) {
838 return ldb_next_request(module, req);
840 ldb = ldb_module_get_ctx(module);
841 /* first check if we have delete object right */
842 ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn, SEC_STD_DELETE, NULL);
843 if (ret == LDB_SUCCESS) {
844 return ldb_next_request(module, req);
847 /* Nope, we don't have delete object. Lets check if we have delete child on the parent */
848 /* No parent, so check fails */
849 /* FIXME: this has to be made dynamic at some point */
850 if ((ldb_dn_compare(req->op.del.dn, (ldb_get_schema_basedn(ldb))) == 0) ||
851 (ldb_dn_compare(req->op.del.dn, (ldb_get_config_basedn(ldb))) == 0) ||
852 (ldb_dn_compare(req->op.del.dn, (ldb_get_default_basedn(ldb))) == 0) ||
853 (ldb_dn_compare(req->op.del.dn, (ldb_get_root_basedn(ldb))) == 0)) {
854 DEBUG(10,("acl:deleting an NC\n"));
855 return ldb_module_done(req, NULL, NULL, LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS);
858 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_DELETE_CHILD, NULL);
859 if (ret != LDB_SUCCESS) {
862 return ldb_next_request(module, req);
865 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
868 struct ldb_dn *oldparent = ldb_dn_get_parent(req, req->op.rename.olddn);
869 struct ldb_dn *newparent = ldb_dn_get_parent(req, req->op.rename.newdn);
870 const struct dsdb_schema *schema;
871 struct ldb_context *ldb;
872 struct security_descriptor *sd = NULL;
873 struct dom_sid *sid = NULL;
874 struct ldb_result *acl_res;
875 const struct GUID *guid;
876 struct object_tree *root = NULL;
877 struct object_tree *new_node = NULL;
878 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
879 TALLOC_CTX *tmp_ctx = talloc_new(req);
881 uint32_t access_granted;
882 const char *rdn_name;
883 static const char *acl_attrs[] = {
884 "nTSecurityDescriptor",
890 if (as_system != NULL) {
891 as_system->critical = 0;
894 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
895 if (dsdb_module_am_system(module) || as_system) {
896 return ldb_next_request(module, req);
898 if (ldb_dn_is_special(req->op.rename.olddn)) {
899 return ldb_next_request(module, req);
901 ldb = ldb_module_get_ctx(module);
903 ret = dsdb_module_search_dn(module, req, &acl_res, req->op.rename.olddn,
904 acl_attrs, DSDB_SEARCH_SHOW_DELETED);
905 /* we sould be able to find the parent */
906 if (ret != LDB_SUCCESS) {
907 DEBUG(10,("acl: failed to find object %s\n",
908 ldb_dn_get_linearized(req->op.rename.olddn)));
912 schema = dsdb_get_schema(ldb, acl_res);
914 talloc_free(acl_res);
915 return LDB_ERR_OPERATIONS_ERROR;
918 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
919 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
921 return LDB_ERR_OPERATIONS_ERROR;
924 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
926 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
927 &new_node, &new_node)) {
928 return LDB_ERR_OPERATIONS_ERROR;
931 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
932 if (rdn_name == NULL) {
933 return LDB_ERR_OPERATIONS_ERROR;
935 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
937 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
938 &new_node, &new_node)) {
939 return LDB_ERR_OPERATIONS_ERROR;
942 ret = dsdb_get_sd_from_ldb_message(req, acl_res->msgs[0], &sd);
944 if (ret != LDB_SUCCESS) {
945 return LDB_ERR_OPERATIONS_ERROR;
947 /* Theoretically we pass the check if the object has no sd */
951 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
952 status = sec_access_check_ds(sd, acl_user_token(module),
958 if (!NT_STATUS_IS_OK(status)) {
959 DEBUG(10, ("Object %s has no wp on name\n",
960 ldb_dn_get_linearized(req->op.rename.olddn)));
962 acl_user_token(module),
963 req->op.rename.olddn,
966 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
969 if (ldb_dn_compare(oldparent, newparent) == 0) {
970 /* regular rename, not move, nothing more to do */
971 return ldb_next_request(module, req);
974 /* What exactly to do in this case? It would fail anyway.. */
975 /* FIXME: this has to be made dynamic at some point */
976 if ((ldb_dn_compare(req->op.rename.newdn, (ldb_get_schema_basedn(ldb))) == 0) ||
977 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_config_basedn(ldb))) == 0) ||
978 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_default_basedn(ldb))) == 0) ||
979 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_root_basedn(ldb))) == 0)) {
980 DEBUG(10,("acl:moving as an NC\n"));
981 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
983 /* new parent should have create child */
984 talloc_free(tmp_ctx);
985 tmp_ctx = talloc_new(req);
988 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
990 DEBUG(10,("acl:renamed object has no object class\n"));
991 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
994 ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid);
995 if (ret != LDB_SUCCESS) {
996 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
999 /* do we have delete object on the object? */
1001 status = sec_access_check_ds(sd, acl_user_token(module),
1007 if (NT_STATUS_IS_OK(status)) {
1008 return ldb_next_request(module, req);
1010 /* what about delete child on the current parent */
1011 ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL);
1012 if (ret != LDB_SUCCESS) {
1013 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1014 return ldb_module_done(req, NULL, NULL, ret);
1016 return ldb_next_request(module, req);
1019 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1021 struct ldb_context *ldb;
1022 struct acl_context *ac;
1023 struct acl_private *data;
1024 struct ldb_result *acl_res;
1025 static const char *acl_attrs[] = {
1027 "nTSecurityDescriptor",
1033 ac = talloc_get_type(req->context, struct acl_context);
1034 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1035 ldb = ldb_module_get_ctx(ac->module);
1038 return ldb_module_done(ac->req, NULL, NULL,
1039 LDB_ERR_OPERATIONS_ERROR);
1041 if (ares->error != LDB_SUCCESS) {
1042 return ldb_module_done(ac->req, ares->controls,
1043 ares->response, ares->error);
1046 switch (ares->type) {
1047 case LDB_REPLY_ENTRY:
1048 if (ac->allowedAttributes
1049 || ac->allowedChildClasses
1050 || ac->allowedChildClassesEffective
1051 || ac->allowedAttributesEffective
1052 || ac->sDRightsEffective) {
1053 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1055 if (ret != LDB_SUCCESS) {
1056 return ldb_module_done(ac->req, NULL, NULL, ret);
1058 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1059 ret = acl_allowedAttributes(ac->module, ac->schema, acl_res->msgs[0], ares->message, ac);
1060 if (ret != LDB_SUCCESS) {
1061 return ldb_module_done(ac->req, NULL, NULL, ret);
1064 if (ac->allowedChildClasses) {
1065 ret = acl_childClasses(ac->module, ac->schema, acl_res->msgs[0],
1066 ares->message, "allowedChildClasses");
1067 if (ret != LDB_SUCCESS) {
1068 return ldb_module_done(ac->req, NULL, NULL, ret);
1071 if (ac->allowedChildClassesEffective) {
1072 ret = acl_childClassesEffective(ac->module, ac->schema,
1073 acl_res->msgs[0], ares->message, ac);
1074 if (ret != LDB_SUCCESS) {
1075 return ldb_module_done(ac->req, NULL, NULL, ret);
1078 if (ac->sDRightsEffective) {
1079 ret = acl_sDRightsEffective(ac->module,
1080 acl_res->msgs[0], ares->message, ac);
1081 if (ret != LDB_SUCCESS) {
1082 return ldb_module_done(ac->req, NULL, NULL, ret);
1086 if (data && data->password_attrs) {
1087 if (!ac->am_system) {
1088 for (i = 0; data->password_attrs[i]; i++) {
1089 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1093 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1095 case LDB_REPLY_REFERRAL:
1096 return ldb_module_send_referral(ac->req, ares->referral);
1098 case LDB_REPLY_DONE:
1099 return ldb_module_done(ac->req, ares->controls,
1100 ares->response, LDB_SUCCESS);
1106 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1108 struct ldb_context *ldb;
1109 struct acl_context *ac;
1110 struct ldb_request *down_req;
1111 struct acl_private *data;
1114 ldb = ldb_module_get_ctx(module);
1116 ac = talloc_zero(req, struct acl_context);
1119 return LDB_ERR_OPERATIONS_ERROR;
1121 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1123 ac->module = module;
1125 ac->am_system = dsdb_module_am_system(module);
1126 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1127 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1128 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1129 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1130 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1131 ac->schema = dsdb_get_schema(ldb, ac);
1133 /* replace any attributes in the parse tree that are private,
1134 so we don't allow a search for 'userPassword=penguin',
1135 just as we would not allow that attribute to be returned */
1136 if (ac->am_system) {
1137 /* FIXME: We should copy the tree and keep the original unmodified. */
1138 /* remove password attributes */
1139 if (data && data->password_attrs) {
1140 for (i = 0; data->password_attrs[i]; i++) {
1141 ldb_parse_tree_attr_replace(req->op.search.tree,
1142 data->password_attrs[i],
1143 "kludgeACLredactedattribute");
1147 ret = ldb_build_search_req_ex(&down_req,
1149 req->op.search.base,
1150 req->op.search.scope,
1151 req->op.search.tree,
1152 req->op.search.attrs,
1154 ac, acl_search_callback,
1156 if (ret != LDB_SUCCESS) {
1159 /* perform the search */
1160 return ldb_next_request(module, down_req);
1163 _PUBLIC_ const struct ldb_module_ops ldb_acl_module_ops = {
1165 .search = acl_search,
1167 .modify = acl_modify,
1169 .rename = acl_rename,
1170 .init_context = acl_module_init