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 static struct security_token *acl_user_token(struct ldb_module *module)
69 struct ldb_context *ldb = ldb_module_get_ctx(module);
70 struct auth_session_info *session_info
71 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
75 return session_info->security_token;
78 /* performs an access check from inside the module stack
79 * given the dn of the object to be checked, the required access
80 * guid is either the guid of the extended right, or NULL
83 int dsdb_module_check_access_on_dn(struct ldb_module *module,
87 const struct GUID *guid)
90 struct ldb_result *acl_res;
91 static const char *acl_attrs[] = {
92 "nTSecurityDescriptor",
96 struct ldb_context *ldb = ldb_module_get_ctx(module);
97 struct auth_session_info *session_info
98 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
100 return ldb_operr(ldb);
102 ret = dsdb_module_search_dn(module, mem_ctx, &acl_res, dn,
104 DSDB_FLAG_NEXT_MODULE |
105 DSDB_SEARCH_SHOW_DELETED);
106 if (ret != LDB_SUCCESS) {
107 DEBUG(10,("access_check: failed to find object %s\n", ldb_dn_get_linearized(dn)));
110 return dsdb_check_access_on_dn_internal(ldb, acl_res,
112 session_info->security_token,
119 static int acl_module_init(struct ldb_module *module)
121 struct ldb_context *ldb;
122 struct acl_private *data;
125 static const char *attrs[] = { "passwordAttribute", NULL };
126 struct ldb_result *res;
127 struct ldb_message *msg;
128 struct ldb_message_element *password_attributes;
130 ldb = ldb_module_get_ctx(module);
132 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
133 if (ret != LDB_SUCCESS) {
134 ldb_debug(ldb, LDB_DEBUG_ERROR,
135 "acl_module_init: Unable to register control with rootdse!\n");
136 return ldb_operr(ldb);
139 data = talloc(module, struct acl_private);
144 data->password_attrs = NULL;
145 data->acl_perform = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"),
146 NULL, "acl", "perform", false);
147 ldb_module_set_private(module, data);
149 mem_ctx = talloc_new(module);
154 ret = dsdb_module_search_dn(module, mem_ctx, &res,
155 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
157 DSDB_FLAG_NEXT_MODULE);
158 if (ret != LDB_SUCCESS) {
161 if (res->count == 0) {
165 if (res->count > 1) {
166 talloc_free(mem_ctx);
167 return LDB_ERR_CONSTRAINT_VIOLATION;
172 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
173 if (!password_attributes) {
176 data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
177 if (!data->password_attrs) {
178 talloc_free(mem_ctx);
181 for (i=0; i < password_attributes->num_values; i++) {
182 data->password_attrs[i] = (const char *)password_attributes->values[i].data;
183 talloc_steal(data->password_attrs, password_attributes->values[i].data);
185 data->password_attrs[i] = NULL;
188 talloc_free(mem_ctx);
189 return ldb_next_init(module);
192 static const struct GUID *get_oc_guid_from_message(struct ldb_module *module,
193 const struct dsdb_schema *schema,
194 struct ldb_message *msg)
196 struct ldb_message_element *oc_el;
198 oc_el = ldb_msg_find_element(msg, "objectClass");
203 return class_schemaid_guid_by_lDAPDisplayName(schema,
204 (char *)oc_el->values[oc_el->num_values-1].data);
207 static int acl_check_access_on_attribute(struct ldb_module *module,
209 struct security_descriptor *sd,
210 struct dom_sid *rp_sid,
212 const struct dsdb_attribute *attr)
216 uint32_t access_granted;
217 struct object_tree *root = NULL;
218 struct object_tree *new_node = NULL;
219 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
220 struct security_token *token = acl_user_token(module);
222 if (!GUID_all_zero(&attr->attributeSecurityGUID)) {
223 if (!insert_in_object_tree(tmp_ctx,
224 &attr->attributeSecurityGUID, access,
226 DEBUG(10, ("acl_search: cannot add to object tree securityGUID\n"));
230 if (!insert_in_object_tree(tmp_ctx,
231 &attr->schemaIDGUID, access, &new_node, &new_node)) {
232 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
237 if (!insert_in_object_tree(tmp_ctx,
238 &attr->schemaIDGUID, access, &root, &new_node)) {
239 DEBUG(10, ("acl_search: cannot add to object tree attributeGUID\n"));
244 status = sec_access_check_ds(sd, token,
249 if (!NT_STATUS_IS_OK(status)) {
250 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
255 talloc_free(tmp_ctx);
258 talloc_free(tmp_ctx);
259 return ldb_operr(ldb_module_get_ctx(module));
262 static int acl_check_access_on_class(struct ldb_module *module,
263 const struct dsdb_schema *schema,
265 struct security_descriptor *sd,
266 struct dom_sid *rp_sid,
268 const char *class_name)
272 uint32_t access_granted;
273 struct object_tree *root = NULL;
274 struct object_tree *new_node = NULL;
275 const struct GUID *guid;
276 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
277 struct security_token *token = acl_user_token(module);
279 guid = class_schemaid_guid_by_lDAPDisplayName(schema, class_name);
281 DEBUG(10, ("acl_search: cannot find class %s\n",
285 if (!insert_in_object_tree(tmp_ctx,
288 DEBUG(10, ("acl_search: cannot add to object tree guid\n"));
292 status = sec_access_check_ds(sd, token,
297 if (!NT_STATUS_IS_OK(status)) {
298 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
305 return ldb_operr(ldb_module_get_ctx(module));
308 static int acl_allowedAttributes(struct ldb_module *module,
309 const struct dsdb_schema *schema,
310 struct ldb_message *sd_msg,
311 struct ldb_message *msg,
312 struct acl_context *ac)
314 struct ldb_message_element *oc_el;
315 struct ldb_context *ldb = ldb_module_get_ctx(module);
317 const char **attr_list;
320 /* If we don't have a schema yet, we can't do anything... */
321 if (schema == NULL) {
322 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
323 return LDB_ERR_OPERATIONS_ERROR;
326 /* Must remove any existing attribute */
327 if (ac->allowedAttributes) {
328 ldb_msg_remove_attr(msg, "allowedAttributes");
331 mem_ctx = talloc_new(msg);
336 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
337 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
339 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
340 talloc_free(mem_ctx);
341 return LDB_ERR_OPERATIONS_ERROR;
343 if (ac->allowedAttributes) {
344 for (i=0; attr_list && attr_list[i]; i++) {
345 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
348 if (ac->allowedAttributesEffective) {
349 struct security_descriptor *sd;
350 struct dom_sid *sid = NULL;
351 struct ldb_control *as_system = ldb_request_get_control(ac->req,
352 LDB_CONTROL_AS_SYSTEM_OID);
354 if (as_system != NULL) {
355 as_system->critical = 0;
358 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
359 if (ac->am_system || as_system) {
360 for (i=0; attr_list && attr_list[i]; i++) {
361 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
366 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), mem_ctx, sd_msg, &sd);
368 if (ret != LDB_SUCCESS) {
372 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
373 for (i=0; attr_list && attr_list[i]; i++) {
374 const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
377 return ldb_operr(ldb);
379 /* remove constructed attributes */
380 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
382 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
385 ret = acl_check_access_on_attribute(module,
391 if (ret == LDB_SUCCESS) {
392 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
399 static int acl_childClasses(struct ldb_module *module,
400 const struct dsdb_schema *schema,
401 struct ldb_message *sd_msg,
402 struct ldb_message *msg,
403 const char *attrName)
405 struct ldb_message_element *oc_el;
406 struct ldb_message_element *allowedClasses;
407 const struct dsdb_class *sclass;
411 /* If we don't have a schema yet, we can't do anything... */
412 if (schema == NULL) {
413 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
414 return LDB_ERR_OPERATIONS_ERROR;
417 /* Must remove any existing attribute, or else confusion reins */
418 ldb_msg_remove_attr(msg, attrName);
419 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
420 if (ret != LDB_SUCCESS) {
424 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
426 for (i=0; oc_el && i < oc_el->num_values; i++) {
427 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
429 /* We don't know this class? what is going on? */
433 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
434 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
437 if (allowedClasses->num_values > 1) {
438 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
439 for (i=1 ; i < allowedClasses->num_values; i++) {
440 struct ldb_val *val1 = &allowedClasses->values[i-1];
441 struct ldb_val *val2 = &allowedClasses->values[i];
442 if (data_blob_cmp(val1, val2) == 0) {
443 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
444 allowedClasses->num_values--;
453 static int acl_childClassesEffective(struct ldb_module *module,
454 const struct dsdb_schema *schema,
455 struct ldb_message *sd_msg,
456 struct ldb_message *msg,
457 struct acl_context *ac)
459 struct ldb_message_element *oc_el;
460 struct ldb_message_element *allowedClasses = NULL;
461 const struct dsdb_class *sclass;
462 struct security_descriptor *sd;
463 struct ldb_control *as_system = ldb_request_get_control(ac->req,
464 LDB_CONTROL_AS_SYSTEM_OID);
465 struct dom_sid *sid = NULL;
469 if (as_system != NULL) {
470 as_system->critical = 0;
473 if (ac->am_system || as_system) {
474 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
477 /* If we don't have a schema yet, we can't do anything... */
478 if (schema == NULL) {
479 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
480 return LDB_ERR_OPERATIONS_ERROR;
483 /* Must remove any existing attribute, or else confusion reins */
484 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
486 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
487 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
488 if (ret != LDB_SUCCESS) {
492 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
493 for (i=0; oc_el && i < oc_el->num_values; i++) {
494 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
496 /* We don't know this class? what is going on? */
500 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
501 ret = acl_check_access_on_class(module,
506 SEC_ADS_CREATE_CHILD,
507 sclass->possibleInferiors[j]);
508 if (ret == LDB_SUCCESS) {
509 ldb_msg_add_string(msg, "allowedChildClassesEffective",
510 sclass->possibleInferiors[j]);
514 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
515 if (!allowedClasses) {
519 if (allowedClasses->num_values > 1) {
520 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
521 for (i=1 ; i < allowedClasses->num_values; i++) {
522 struct ldb_val *val1 = &allowedClasses->values[i-1];
523 struct ldb_val *val2 = &allowedClasses->values[i];
524 if (data_blob_cmp(val1, val2) == 0) {
525 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
526 allowedClasses->num_values--;
534 static int acl_sDRightsEffective(struct ldb_module *module,
535 struct ldb_message *sd_msg,
536 struct ldb_message *msg,
537 struct acl_context *ac)
539 struct ldb_message_element *rightsEffective;
541 struct security_descriptor *sd;
542 struct ldb_control *as_system = ldb_request_get_control(ac->req,
543 LDB_CONTROL_AS_SYSTEM_OID);
544 struct dom_sid *sid = NULL;
547 if (as_system != NULL) {
548 as_system->critical = 0;
551 /* Must remove any existing attribute, or else confusion reins */
552 ldb_msg_remove_attr(msg, "sDRightsEffective");
553 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
554 if (ret != LDB_SUCCESS) {
557 if (ac->am_system || as_system) {
558 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
561 /* Get the security descriptor from the message */
562 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
563 if (ret != LDB_SUCCESS) {
566 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
567 ret = acl_check_access_on_attribute(module,
573 if (ret == LDB_SUCCESS) {
574 flags |= SECINFO_OWNER | SECINFO_GROUP;
576 ret = acl_check_access_on_attribute(module,
582 if (ret == LDB_SUCCESS) {
583 flags |= SECINFO_DACL;
585 ret = acl_check_access_on_attribute(module,
589 SEC_FLAG_SYSTEM_SECURITY,
591 if (ret == LDB_SUCCESS) {
592 flags |= SECINFO_SACL;
595 ldb_msg_add_fmt(msg, "sDRightsEffective", "%u", flags);
599 static int acl_add(struct ldb_module *module, struct ldb_request *req)
602 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.add.message->dn);
603 struct ldb_context *ldb;
604 const struct dsdb_schema *schema;
605 struct ldb_message_element *oc_el;
606 const struct GUID *guid;
607 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
609 if (as_system != NULL) {
610 as_system->critical = 0;
613 if (dsdb_module_am_system(module) || as_system) {
614 return ldb_next_request(module, req);
617 if (ldb_dn_is_special(req->op.add.message->dn)) {
618 return ldb_next_request(module, req);
620 ldb = ldb_module_get_ctx(module);
621 /* Creating an NC. There is probably something we should do here,
622 * but we will establish that later */
623 /* FIXME: this has to be made dynamic at some point */
624 if ((ldb_dn_compare(req->op.add.message->dn, (ldb_get_schema_basedn(ldb))) == 0) ||
625 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_config_basedn(ldb))) == 0) ||
626 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_default_basedn(ldb))) == 0)) {
627 return ldb_next_request(module, req);
630 schema = dsdb_get_schema(ldb, req);
632 return ldb_operr(ldb);
635 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
636 if (!oc_el || oc_el->num_values == 0) {
637 DEBUG(10,("acl:operation error %s\n", ldb_dn_get_linearized(req->op.add.message->dn)));
638 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
641 guid = class_schemaid_guid_by_lDAPDisplayName(schema,
642 (char *)oc_el->values[oc_el->num_values-1].data);
643 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid);
644 if (ret != LDB_SUCCESS) {
647 return ldb_next_request(module, req);
650 /* checks for validated writes */
651 static int acl_check_extended_right(TALLOC_CTX *mem_ctx,
652 struct security_descriptor *sd,
653 struct security_token *token,
654 const char *ext_right,
660 uint32_t access_granted;
661 struct object_tree *root = NULL;
662 struct object_tree *new_node = NULL;
663 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
665 GUID_from_string(ext_right, &right);
667 if (!insert_in_object_tree(tmp_ctx, &right, right_type,
669 DEBUG(10, ("acl_ext_right: cannot add to object tree\n"));
670 talloc_free(tmp_ctx);
671 return LDB_ERR_OPERATIONS_ERROR;
673 status = sec_access_check_ds(sd, token,
679 if (!NT_STATUS_IS_OK(status)) {
680 talloc_free(tmp_ctx);
681 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
683 talloc_free(tmp_ctx);
688 /* ckecks if modifications are allowed on "Member" attribute */
689 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
690 struct ldb_module *module,
691 struct ldb_request *req,
692 struct security_descriptor *sd,
694 const struct GUID *oc_guid,
695 const struct dsdb_attribute *attr)
699 struct ldb_context *ldb = ldb_module_get_ctx(module);
700 struct ldb_dn *user_dn;
701 struct ldb_message_element *member_el;
702 /* if we have wp, we can do whatever we like */
703 if (acl_check_access_on_attribute(module,
708 attr) == LDB_SUCCESS) {
711 /* if we are adding/deleting ourselves, check for self membership */
712 ret = dsdb_find_dn_by_sid(ldb, mem_ctx, acl_user_token(module)->user_sid, &user_dn);
713 if (ret != LDB_SUCCESS) {
716 member_el = ldb_msg_find_element(req->op.mod.message, "member");
718 return ldb_operr(ldb);
720 /* user can only remove oneself */
721 if (member_el->num_values == 0) {
722 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
724 for (i = 0; i < member_el->num_values; i++) {
725 if (strcasecmp((const char *)member_el->values[i].data,
726 ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
727 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
730 ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
731 GUID_DRS_SELF_MEMBERSHIP,
734 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
735 dsdb_acl_debug(sd, acl_user_token(module),
736 req->op.mod.message->dn,
743 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
744 struct ldb_module *module,
745 struct ldb_request *req,
746 struct security_descriptor *sd,
748 const struct GUID *oc_guid)
750 int ret = LDB_SUCCESS;
751 unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
752 struct ldb_message_element *el;
753 struct ldb_message *msg;
754 const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
755 "unicodePwd", "dBCSPwd", NULL }, **l;
756 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
758 msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
760 return ldb_module_oom(module);
762 for (l = passwordAttrs; *l != NULL; l++) {
763 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
764 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
767 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
770 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
773 ldb_msg_remove_element(msg, el);
776 /* a single delete will be handled by password hash
777 later in the stack, so we let it though here */
778 if (del_attr_cnt > 0 && add_attr_cnt == 0) {
779 talloc_free(tmp_ctx);
783 if (ldb_request_get_control(req,
784 DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
785 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
786 * have a user password change and not a set as the message
787 * looks like. In it's value blob it contains the NT and/or LM
788 * hash of the old password specified by the user.
789 * This control is used by the SAMR and "kpasswd" password
790 * change mechanisms. */
791 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
792 GUID_DRS_USER_CHANGE_PASSWORD,
793 SEC_ADS_CONTROL_ACCESS,
796 else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
797 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
798 GUID_DRS_FORCE_CHANGE_PASSWORD,
799 SEC_ADS_CONTROL_ACCESS,
802 else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
803 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
804 GUID_DRS_USER_CHANGE_PASSWORD,
805 SEC_ADS_CONTROL_ACCESS,
807 /* Very strange, but we get constraint violation in this case */
808 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
809 ret = LDB_ERR_CONSTRAINT_VIOLATION;
812 if (ret != LDB_SUCCESS) {
813 dsdb_acl_debug(sd, acl_user_token(module),
814 req->op.mod.message->dn,
818 talloc_free(tmp_ctx);
822 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
825 struct ldb_context *ldb = ldb_module_get_ctx(module);
826 const struct dsdb_schema *schema;
828 const struct GUID *guid;
829 uint32_t access_granted;
830 struct object_tree *root = NULL;
831 struct object_tree *new_node = NULL;
833 struct ldb_result *acl_res;
834 struct security_descriptor *sd;
835 struct dom_sid *sid = NULL;
836 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
837 TALLOC_CTX *tmp_ctx = talloc_new(req);
838 static const char *acl_attrs[] = {
839 "nTSecurityDescriptor",
845 if (as_system != NULL) {
846 as_system->critical = 0;
849 /* Don't print this debug statement if elements[0].name is going to be NULL */
850 if(req->op.mod.message->num_elements > 0)
852 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->elements[0].name));
854 if (dsdb_module_am_system(module) || as_system) {
855 return ldb_next_request(module, req);
857 if (ldb_dn_is_special(req->op.mod.message->dn)) {
858 return ldb_next_request(module, req);
860 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, req->op.mod.message->dn,
862 DSDB_FLAG_NEXT_MODULE);
864 if (ret != LDB_SUCCESS) {
868 schema = dsdb_get_schema(ldb, tmp_ctx);
870 ret = LDB_ERR_OPERATIONS_ERROR;
874 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
875 if (ret != LDB_SUCCESS) {
876 DEBUG(10, ("acl_modify: cannot get descriptor\n"));
879 /* Theoretically we pass the check if the object has no sd */
884 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
886 DEBUG(10, ("acl_modify: cannot get guid\n"));
889 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
890 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
892 DEBUG(10, ("acl_modify: cannot add to object tree\n"));
895 for (i=0; i < req->op.mod.message->num_elements; i++){
896 const struct dsdb_attribute *attr;
897 attr = dsdb_attribute_by_lDAPDisplayName(schema,
898 req->op.mod.message->elements[i].name);
900 if (ldb_attr_cmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
901 status = sec_access_check_ds(sd, acl_user_token(module),
907 if (!NT_STATUS_IS_OK(status)) {
908 DEBUG(10, ("Object %s has no write dacl access\n",
909 ldb_dn_get_linearized(req->op.mod.message->dn)));
911 acl_user_token(module),
912 req->op.mod.message->dn,
915 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
919 else if (ldb_attr_cmp("member", req->op.mod.message->elements[i].name) == 0) {
920 ret = acl_check_self_membership(tmp_ctx,
927 if (ret != LDB_SUCCESS) {
931 else if (ldb_attr_cmp("dBCSPwd", req->op.mod.message->elements[i].name) == 0) {
932 /* this one is not affected by any rights, we should let it through
933 so that passwords_hash returns the correct error */
936 else if (ldb_attr_cmp("unicodePwd", req->op.mod.message->elements[i].name) == 0 ||
937 ldb_attr_cmp("userPassword", req->op.mod.message->elements[i].name) == 0 ||
938 ldb_attr_cmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
939 ret = acl_check_password_rights(tmp_ctx,
945 if (ret != LDB_SUCCESS) {
950 /* This basic attribute existence check with the right errorcode
951 * is needed since this module is the first one which requests
952 * schema attribute informations.
953 * The complete attribute checking is done in the
954 * "objectclass_attrs" module behind this one.
957 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' on entry '%s' was not found in the schema!",
958 req->op.mod.message->elements[i].name,
959 ldb_dn_get_linearized(req->op.mod.message->dn));
960 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
963 if (!insert_in_object_tree(tmp_ctx,
964 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
965 &new_node, &new_node)) {
966 DEBUG(10, ("acl_modify: cannot add to object tree securityGUID\n"));
967 ret = LDB_ERR_OPERATIONS_ERROR;
971 if (!insert_in_object_tree(tmp_ctx,
972 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
973 DEBUG(10, ("acl_modify: cannot add to object tree attributeGUID\n"));
974 ret = LDB_ERR_OPERATIONS_ERROR;
980 if (root->num_of_children > 0) {
981 status = sec_access_check_ds(sd, acl_user_token(module),
987 if (!NT_STATUS_IS_OK(status)) {
988 DEBUG(10, ("Object %s has no write property access\n",
989 ldb_dn_get_linearized(req->op.mod.message->dn)));
991 acl_user_token(module),
992 req->op.mod.message->dn,
995 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1001 talloc_free(tmp_ctx);
1002 return ldb_next_request(module, req);
1004 talloc_free(tmp_ctx);
1008 /* similar to the modify for the time being.
1009 * We need to concider the special delete tree case, though - TODO */
1010 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
1013 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.del.dn);
1014 struct ldb_context *ldb;
1015 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1017 if (as_system != NULL) {
1018 as_system->critical = 0;
1021 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
1022 if (dsdb_module_am_system(module) || as_system) {
1023 return ldb_next_request(module, req);
1026 if (ldb_dn_is_special(req->op.del.dn)) {
1027 return ldb_next_request(module, req);
1029 ldb = ldb_module_get_ctx(module);
1030 /* first check if we have delete object right */
1031 ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn, SEC_STD_DELETE, NULL);
1032 if (ret == LDB_SUCCESS) {
1033 return ldb_next_request(module, req);
1036 /* Nope, we don't have delete object. Lets check if we have delete child on the parent */
1037 /* No parent, so check fails */
1038 /* FIXME: this has to be made dynamic at some point */
1039 if ((ldb_dn_compare(req->op.del.dn, (ldb_get_schema_basedn(ldb))) == 0) ||
1040 (ldb_dn_compare(req->op.del.dn, (ldb_get_config_basedn(ldb))) == 0) ||
1041 (ldb_dn_compare(req->op.del.dn, (ldb_get_default_basedn(ldb))) == 0)) {
1042 DEBUG(10,("acl:deleting an NC\n"));
1043 return ldb_module_done(req, NULL, NULL, LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS);
1046 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_DELETE_CHILD, NULL);
1047 if (ret != LDB_SUCCESS) {
1050 return ldb_next_request(module, req);
1053 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
1056 struct ldb_dn *oldparent = ldb_dn_get_parent(req, req->op.rename.olddn);
1057 struct ldb_dn *newparent = ldb_dn_get_parent(req, req->op.rename.newdn);
1058 const struct dsdb_schema *schema;
1059 struct ldb_context *ldb;
1060 struct security_descriptor *sd = NULL;
1061 struct dom_sid *sid = NULL;
1062 struct ldb_result *acl_res;
1063 const struct GUID *guid;
1064 struct object_tree *root = NULL;
1065 struct object_tree *new_node = NULL;
1066 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1067 TALLOC_CTX *tmp_ctx = talloc_new(req);
1069 uint32_t access_granted;
1070 const char *rdn_name;
1071 static const char *acl_attrs[] = {
1072 "nTSecurityDescriptor",
1078 if (as_system != NULL) {
1079 as_system->critical = 0;
1082 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
1083 if (dsdb_module_am_system(module) || as_system) {
1084 return ldb_next_request(module, req);
1086 if (ldb_dn_is_special(req->op.rename.olddn)) {
1087 return ldb_next_request(module, req);
1089 ldb = ldb_module_get_ctx(module);
1091 ret = dsdb_module_search_dn(module, req, &acl_res, req->op.rename.olddn,
1093 DSDB_FLAG_NEXT_MODULE |
1094 DSDB_SEARCH_SHOW_DELETED);
1095 /* we sould be able to find the parent */
1096 if (ret != LDB_SUCCESS) {
1097 DEBUG(10,("acl: failed to find object %s\n",
1098 ldb_dn_get_linearized(req->op.rename.olddn)));
1102 schema = dsdb_get_schema(ldb, acl_res);
1104 talloc_free(acl_res);
1105 return ldb_operr(ldb);
1108 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
1109 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1110 &root, &new_node)) {
1111 return ldb_operr(ldb);
1114 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1116 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1117 &new_node, &new_node)) {
1118 return ldb_operr(ldb);
1121 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1122 if (rdn_name == NULL) {
1123 return ldb_operr(ldb);
1125 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1127 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1128 &new_node, &new_node)) {
1129 return ldb_operr(ldb);
1132 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1134 if (ret != LDB_SUCCESS) {
1135 return ldb_operr(ldb);
1137 /* Theoretically we pass the check if the object has no sd */
1141 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1142 status = sec_access_check_ds(sd, acl_user_token(module),
1148 if (!NT_STATUS_IS_OK(status)) {
1149 DEBUG(10, ("Object %s has no wp on name\n",
1150 ldb_dn_get_linearized(req->op.rename.olddn)));
1152 acl_user_token(module),
1153 req->op.rename.olddn,
1156 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1159 if (ldb_dn_compare(oldparent, newparent) == 0) {
1160 /* regular rename, not move, nothing more to do */
1161 return ldb_next_request(module, req);
1164 /* What exactly to do in this case? It would fail anyway.. */
1165 /* FIXME: this has to be made dynamic at some point */
1166 if ((ldb_dn_compare(req->op.rename.newdn, (ldb_get_schema_basedn(ldb))) == 0) ||
1167 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_config_basedn(ldb))) == 0) ||
1168 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_default_basedn(ldb))) == 0)) {
1169 DEBUG(10,("acl:moving as an NC\n"));
1170 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1172 /* new parent should have create child */
1173 talloc_free(tmp_ctx);
1174 tmp_ctx = talloc_new(req);
1177 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
1179 DEBUG(10,("acl:renamed object has no object class\n"));
1180 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
1183 ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid);
1184 if (ret != LDB_SUCCESS) {
1185 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1188 /* do we have delete object on the object? */
1190 status = sec_access_check_ds(sd, acl_user_token(module),
1196 if (NT_STATUS_IS_OK(status)) {
1197 return ldb_next_request(module, req);
1199 /* what about delete child on the current parent */
1200 ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL);
1201 if (ret != LDB_SUCCESS) {
1202 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1203 return ldb_module_done(req, NULL, NULL, ret);
1205 return ldb_next_request(module, req);
1208 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1210 struct ldb_context *ldb;
1211 struct acl_context *ac;
1212 struct acl_private *data;
1213 struct ldb_result *acl_res;
1214 static const char *acl_attrs[] = {
1216 "nTSecurityDescriptor",
1222 ac = talloc_get_type(req->context, struct acl_context);
1223 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1224 ldb = ldb_module_get_ctx(ac->module);
1227 return ldb_module_done(ac->req, NULL, NULL,
1228 LDB_ERR_OPERATIONS_ERROR);
1230 if (ares->error != LDB_SUCCESS) {
1231 return ldb_module_done(ac->req, ares->controls,
1232 ares->response, ares->error);
1235 switch (ares->type) {
1236 case LDB_REPLY_ENTRY:
1237 if (ac->allowedAttributes
1238 || ac->allowedChildClasses
1239 || ac->allowedChildClassesEffective
1240 || ac->allowedAttributesEffective
1241 || ac->sDRightsEffective) {
1242 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1244 DSDB_FLAG_NEXT_MODULE);
1245 if (ret != LDB_SUCCESS) {
1246 return ldb_module_done(ac->req, NULL, NULL, ret);
1248 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1249 ret = acl_allowedAttributes(ac->module, ac->schema, acl_res->msgs[0], ares->message, ac);
1250 if (ret != LDB_SUCCESS) {
1251 return ldb_module_done(ac->req, NULL, NULL, ret);
1254 if (ac->allowedChildClasses) {
1255 ret = acl_childClasses(ac->module, ac->schema, acl_res->msgs[0],
1256 ares->message, "allowedChildClasses");
1257 if (ret != LDB_SUCCESS) {
1258 return ldb_module_done(ac->req, NULL, NULL, ret);
1261 if (ac->allowedChildClassesEffective) {
1262 ret = acl_childClassesEffective(ac->module, ac->schema,
1263 acl_res->msgs[0], ares->message, ac);
1264 if (ret != LDB_SUCCESS) {
1265 return ldb_module_done(ac->req, NULL, NULL, ret);
1268 if (ac->sDRightsEffective) {
1269 ret = acl_sDRightsEffective(ac->module,
1270 acl_res->msgs[0], ares->message, ac);
1271 if (ret != LDB_SUCCESS) {
1272 return ldb_module_done(ac->req, NULL, NULL, ret);
1276 if (data && data->password_attrs) {
1277 if (!ac->am_system) {
1278 for (i = 0; data->password_attrs[i]; i++) {
1279 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1283 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1285 case LDB_REPLY_REFERRAL:
1286 return ldb_module_send_referral(ac->req, ares->referral);
1288 case LDB_REPLY_DONE:
1289 return ldb_module_done(ac->req, ares->controls,
1290 ares->response, LDB_SUCCESS);
1296 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1298 struct ldb_context *ldb;
1299 struct acl_context *ac;
1300 struct ldb_request *down_req;
1301 struct acl_private *data;
1304 ldb = ldb_module_get_ctx(module);
1306 ac = talloc_zero(req, struct acl_context);
1308 return ldb_oom(ldb);
1310 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1312 ac->module = module;
1314 ac->am_system = dsdb_module_am_system(module);
1315 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1316 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1317 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1318 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1319 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1320 ac->schema = dsdb_get_schema(ldb, ac);
1322 /* replace any attributes in the parse tree that are private,
1323 so we don't allow a search for 'userPassword=penguin',
1324 just as we would not allow that attribute to be returned */
1325 if (ac->am_system) {
1326 /* FIXME: We should copy the tree and keep the original unmodified. */
1327 /* remove password attributes */
1328 if (data && data->password_attrs) {
1329 for (i = 0; data->password_attrs[i]; i++) {
1330 ldb_parse_tree_attr_replace(req->op.search.tree,
1331 data->password_attrs[i],
1332 "kludgeACLredactedattribute");
1336 ret = ldb_build_search_req_ex(&down_req,
1338 req->op.search.base,
1339 req->op.search.scope,
1340 req->op.search.tree,
1341 req->op.search.attrs,
1343 ac, acl_search_callback,
1345 if (ret != LDB_SUCCESS) {
1348 /* perform the search */
1349 return ldb_next_request(module, down_req);
1352 static const char *acl_user_name(TALLOC_CTX *mem_ctx, struct ldb_module *module)
1354 struct ldb_context *ldb = ldb_module_get_ctx(module);
1355 struct auth_session_info *session_info
1356 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
1357 if (!session_info) {
1358 return "UNKNOWN (NULL)";
1361 return talloc_asprintf(mem_ctx, "%s\\%s",
1362 session_info->server_info->domain_name,
1363 session_info->server_info->account_name);
1366 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1368 struct ldb_context *ldb = ldb_module_get_ctx(module);
1369 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1371 /* allow everybody to read the sequence number */
1372 if (strcmp(req->op.extended.oid,
1373 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1374 return ldb_next_request(module, req);
1377 if (dsdb_module_am_system(module) ||
1378 dsdb_module_am_administrator(module) || as_system) {
1379 return ldb_next_request(module, req);
1381 ldb_asprintf_errstring(ldb,
1383 "attempted database modify not permitted. "
1384 "User %s is not SYSTEM or an administrator",
1385 acl_user_name(req, module));
1386 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1390 _PUBLIC_ const struct ldb_module_ops ldb_acl_module_ops = {
1392 .search = acl_search,
1394 .modify = acl_modify,
1396 .rename = acl_rename,
1397 .extended = acl_extended,
1398 .init_context = acl_module_init