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 "dsdb/samdb/ldb_modules/schema.h"
43 #include "lib/util/tsort.h"
45 struct extended_access_check_attribute {
47 const uint32_t requires_rights;
52 const char **password_attrs;
56 struct ldb_module *module;
57 struct ldb_request *req;
59 bool allowedAttributes;
60 bool allowedAttributesEffective;
61 bool allowedChildClasses;
62 bool allowedChildClassesEffective;
63 bool sDRightsEffective;
64 const char * const *attrs;
65 struct dsdb_schema *schema;
68 static int acl_module_init(struct ldb_module *module)
70 struct ldb_context *ldb;
71 struct acl_private *data;
74 static const char *attrs[] = { "passwordAttribute", NULL };
75 struct ldb_result *res;
76 struct ldb_message *msg;
77 struct ldb_message_element *password_attributes;
79 ldb = ldb_module_get_ctx(module);
81 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
82 if (ret != LDB_SUCCESS) {
83 ldb_debug(ldb, LDB_DEBUG_ERROR,
84 "acl_module_init: Unable to register control with rootdse!\n");
85 return ldb_operr(ldb);
88 data = talloc(module, struct acl_private);
93 data->password_attrs = NULL;
94 data->acl_perform = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"),
95 NULL, "acl", "perform", false);
96 ldb_module_set_private(module, data);
98 mem_ctx = talloc_new(module);
103 ret = dsdb_module_search_dn(module, mem_ctx, &res,
104 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
106 DSDB_FLAG_NEXT_MODULE);
107 if (ret != LDB_SUCCESS) {
110 if (res->count == 0) {
114 if (res->count > 1) {
115 talloc_free(mem_ctx);
116 return LDB_ERR_CONSTRAINT_VIOLATION;
121 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
122 if (!password_attributes) {
125 data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
126 if (!data->password_attrs) {
127 talloc_free(mem_ctx);
130 for (i=0; i < password_attributes->num_values; i++) {
131 data->password_attrs[i] = (const char *)password_attributes->values[i].data;
132 talloc_steal(data->password_attrs, password_attributes->values[i].data);
134 data->password_attrs[i] = NULL;
137 talloc_free(mem_ctx);
138 return ldb_next_init(module);
141 static int acl_allowedAttributes(struct ldb_module *module,
142 const struct dsdb_schema *schema,
143 struct ldb_message *sd_msg,
144 struct ldb_message *msg,
145 struct acl_context *ac)
147 struct ldb_message_element *oc_el;
148 struct ldb_context *ldb = ldb_module_get_ctx(module);
150 const char **attr_list;
153 /* If we don't have a schema yet, we can't do anything... */
154 if (schema == NULL) {
155 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
156 return LDB_ERR_OPERATIONS_ERROR;
159 /* Must remove any existing attribute */
160 if (ac->allowedAttributes) {
161 ldb_msg_remove_attr(msg, "allowedAttributes");
164 mem_ctx = talloc_new(msg);
169 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
170 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
172 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
173 talloc_free(mem_ctx);
174 return LDB_ERR_OPERATIONS_ERROR;
176 if (ac->allowedAttributes) {
177 for (i=0; attr_list && attr_list[i]; i++) {
178 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
181 if (ac->allowedAttributesEffective) {
182 struct security_descriptor *sd;
183 struct dom_sid *sid = NULL;
184 struct ldb_control *as_system = ldb_request_get_control(ac->req,
185 LDB_CONTROL_AS_SYSTEM_OID);
187 if (as_system != NULL) {
188 as_system->critical = 0;
191 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
192 if (ac->am_system || as_system) {
193 for (i=0; attr_list && attr_list[i]; i++) {
194 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
199 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), mem_ctx, sd_msg, &sd);
201 if (ret != LDB_SUCCESS) {
205 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
206 for (i=0; attr_list && attr_list[i]; i++) {
207 const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
210 return ldb_operr(ldb);
212 /* remove constructed attributes */
213 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
215 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
218 ret = acl_check_access_on_attribute(module,
224 if (ret == LDB_SUCCESS) {
225 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
232 static int acl_childClasses(struct ldb_module *module,
233 const struct dsdb_schema *schema,
234 struct ldb_message *sd_msg,
235 struct ldb_message *msg,
236 const char *attrName)
238 struct ldb_message_element *oc_el;
239 struct ldb_message_element *allowedClasses;
240 const struct dsdb_class *sclass;
244 /* If we don't have a schema yet, we can't do anything... */
245 if (schema == NULL) {
246 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
247 return LDB_ERR_OPERATIONS_ERROR;
250 /* Must remove any existing attribute, or else confusion reins */
251 ldb_msg_remove_attr(msg, attrName);
252 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
253 if (ret != LDB_SUCCESS) {
257 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
259 for (i=0; oc_el && i < oc_el->num_values; i++) {
260 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
262 /* We don't know this class? what is going on? */
266 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
267 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
270 if (allowedClasses->num_values > 1) {
271 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
272 for (i=1 ; i < allowedClasses->num_values; i++) {
273 struct ldb_val *val1 = &allowedClasses->values[i-1];
274 struct ldb_val *val2 = &allowedClasses->values[i];
275 if (data_blob_cmp(val1, val2) == 0) {
276 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
277 allowedClasses->num_values--;
286 static int acl_childClassesEffective(struct ldb_module *module,
287 const struct dsdb_schema *schema,
288 struct ldb_message *sd_msg,
289 struct ldb_message *msg,
290 struct acl_context *ac)
292 struct ldb_message_element *oc_el;
293 struct ldb_message_element *allowedClasses = NULL;
294 const struct dsdb_class *sclass;
295 struct security_descriptor *sd;
296 struct ldb_control *as_system = ldb_request_get_control(ac->req,
297 LDB_CONTROL_AS_SYSTEM_OID);
298 struct dom_sid *sid = NULL;
302 if (as_system != NULL) {
303 as_system->critical = 0;
306 if (ac->am_system || as_system) {
307 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
310 /* If we don't have a schema yet, we can't do anything... */
311 if (schema == NULL) {
312 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
313 return LDB_ERR_OPERATIONS_ERROR;
316 /* Must remove any existing attribute, or else confusion reins */
317 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
319 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
320 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
321 if (ret != LDB_SUCCESS) {
325 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
326 for (i=0; oc_el && i < oc_el->num_values; i++) {
327 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
329 /* We don't know this class? what is going on? */
333 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
334 ret = acl_check_access_on_class(module,
339 SEC_ADS_CREATE_CHILD,
340 sclass->possibleInferiors[j]);
341 if (ret == LDB_SUCCESS) {
342 ldb_msg_add_string(msg, "allowedChildClassesEffective",
343 sclass->possibleInferiors[j]);
347 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
348 if (!allowedClasses) {
352 if (allowedClasses->num_values > 1) {
353 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
354 for (i=1 ; i < allowedClasses->num_values; i++) {
355 struct ldb_val *val1 = &allowedClasses->values[i-1];
356 struct ldb_val *val2 = &allowedClasses->values[i];
357 if (data_blob_cmp(val1, val2) == 0) {
358 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
359 allowedClasses->num_values--;
367 static int acl_sDRightsEffective(struct ldb_module *module,
368 struct ldb_message *sd_msg,
369 struct ldb_message *msg,
370 struct acl_context *ac)
372 struct ldb_message_element *rightsEffective;
374 struct security_descriptor *sd;
375 struct ldb_control *as_system = ldb_request_get_control(ac->req,
376 LDB_CONTROL_AS_SYSTEM_OID);
377 struct dom_sid *sid = NULL;
380 if (as_system != NULL) {
381 as_system->critical = 0;
384 /* Must remove any existing attribute, or else confusion reins */
385 ldb_msg_remove_attr(msg, "sDRightsEffective");
386 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
387 if (ret != LDB_SUCCESS) {
390 if (ac->am_system || as_system) {
391 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
394 /* Get the security descriptor from the message */
395 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
396 if (ret != LDB_SUCCESS) {
399 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
400 ret = acl_check_access_on_attribute(module,
406 if (ret == LDB_SUCCESS) {
407 flags |= SECINFO_OWNER | SECINFO_GROUP;
409 ret = acl_check_access_on_attribute(module,
415 if (ret == LDB_SUCCESS) {
416 flags |= SECINFO_DACL;
418 ret = acl_check_access_on_attribute(module,
422 SEC_FLAG_SYSTEM_SECURITY,
424 if (ret == LDB_SUCCESS) {
425 flags |= SECINFO_SACL;
428 return samdb_msg_add_uint(ldb_module_get_ctx(module), msg, msg,
429 "sDRightsEffective", flags);
432 static int acl_add(struct ldb_module *module, struct ldb_request *req)
435 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.add.message->dn);
436 struct ldb_context *ldb;
437 const struct dsdb_schema *schema;
438 struct ldb_message_element *oc_el;
439 const struct GUID *guid;
440 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
442 if (as_system != NULL) {
443 as_system->critical = 0;
446 if (dsdb_module_am_system(module) || as_system) {
447 return ldb_next_request(module, req);
450 if (ldb_dn_is_special(req->op.add.message->dn)) {
451 return ldb_next_request(module, req);
453 ldb = ldb_module_get_ctx(module);
454 /* Creating an NC. There is probably something we should do here,
455 * but we will establish that later */
456 /* FIXME: this has to be made dynamic at some point */
457 if ((ldb_dn_compare(req->op.add.message->dn, (ldb_get_schema_basedn(ldb))) == 0) ||
458 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_config_basedn(ldb))) == 0) ||
459 (ldb_dn_compare(req->op.add.message->dn, (ldb_get_default_basedn(ldb))) == 0)) {
460 return ldb_next_request(module, req);
463 schema = dsdb_get_schema(ldb, req);
465 return ldb_operr(ldb);
468 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
469 if (!oc_el || oc_el->num_values == 0) {
470 DEBUG(10,("acl:operation error %s\n", ldb_dn_get_linearized(req->op.add.message->dn)));
471 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
474 guid = class_schemaid_guid_by_lDAPDisplayName(schema,
475 (char *)oc_el->values[oc_el->num_values-1].data);
476 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid);
477 if (ret != LDB_SUCCESS) {
480 return ldb_next_request(module, req);
483 /* ckecks if modifications are allowed on "Member" attribute */
484 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
485 struct ldb_module *module,
486 struct ldb_request *req,
487 struct security_descriptor *sd,
489 const struct GUID *oc_guid,
490 const struct dsdb_attribute *attr)
494 struct ldb_context *ldb = ldb_module_get_ctx(module);
495 struct ldb_dn *user_dn;
496 struct ldb_message_element *member_el;
497 /* if we have wp, we can do whatever we like */
498 if (acl_check_access_on_attribute(module,
503 attr) == LDB_SUCCESS) {
506 /* if we are adding/deleting ourselves, check for self membership */
507 ret = dsdb_find_dn_by_sid(ldb, mem_ctx,
508 &acl_user_token(module)->sids[PRIMARY_USER_SID_INDEX],
510 if (ret != LDB_SUCCESS) {
513 member_el = ldb_msg_find_element(req->op.mod.message, "member");
515 return ldb_operr(ldb);
517 /* user can only remove oneself */
518 if (member_el->num_values == 0) {
519 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
521 for (i = 0; i < member_el->num_values; i++) {
522 if (strcasecmp((const char *)member_el->values[i].data,
523 ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
524 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
527 ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
528 GUID_DRS_SELF_MEMBERSHIP,
531 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
532 dsdb_acl_debug(sd, acl_user_token(module),
533 req->op.mod.message->dn,
540 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
541 struct ldb_module *module,
542 struct ldb_request *req,
543 struct security_descriptor *sd,
545 const struct GUID *oc_guid)
547 int ret = LDB_SUCCESS;
548 unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
549 struct ldb_message_element *el;
550 struct ldb_message *msg;
551 const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
552 "unicodePwd", "dBCSPwd", NULL }, **l;
553 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
555 msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
557 return ldb_module_oom(module);
559 for (l = passwordAttrs; *l != NULL; l++) {
560 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
561 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
564 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
567 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
570 ldb_msg_remove_element(msg, el);
574 /* single deletes will be handled by the "password_hash" LDB module
575 * later in the stack, so we let it though here */
576 if ((del_attr_cnt > 0) && (add_attr_cnt == 0) && (rep_attr_cnt == 0)) {
577 talloc_free(tmp_ctx);
581 if (ldb_request_get_control(req,
582 DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
583 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
584 * have a user password change and not a set as the message
585 * looks like. In it's value blob it contains the NT and/or LM
586 * hash of the old password specified by the user.
587 * This control is used by the SAMR and "kpasswd" password
588 * change mechanisms. */
589 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
590 GUID_DRS_USER_CHANGE_PASSWORD,
591 SEC_ADS_CONTROL_ACCESS,
594 else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
595 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
596 GUID_DRS_FORCE_CHANGE_PASSWORD,
597 SEC_ADS_CONTROL_ACCESS,
600 else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
601 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
602 GUID_DRS_USER_CHANGE_PASSWORD,
603 SEC_ADS_CONTROL_ACCESS,
605 /* Very strange, but we get constraint violation in this case */
606 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
607 ret = LDB_ERR_CONSTRAINT_VIOLATION;
610 if (ret != LDB_SUCCESS) {
611 dsdb_acl_debug(sd, acl_user_token(module),
612 req->op.mod.message->dn,
616 talloc_free(tmp_ctx);
620 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
623 struct ldb_context *ldb = ldb_module_get_ctx(module);
624 const struct dsdb_schema *schema;
626 const struct GUID *guid;
627 uint32_t access_granted;
628 struct object_tree *root = NULL;
629 struct object_tree *new_node = NULL;
631 struct ldb_result *acl_res;
632 struct security_descriptor *sd;
633 struct dom_sid *sid = NULL;
634 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
635 TALLOC_CTX *tmp_ctx = talloc_new(req);
636 static const char *acl_attrs[] = {
637 "nTSecurityDescriptor",
643 if (as_system != NULL) {
644 as_system->critical = 0;
647 /* Don't print this debug statement if elements[0].name is going to be NULL */
648 if(req->op.mod.message->num_elements > 0)
650 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->elements[0].name));
652 if (dsdb_module_am_system(module) || as_system) {
653 return ldb_next_request(module, req);
655 if (ldb_dn_is_special(req->op.mod.message->dn)) {
656 return ldb_next_request(module, req);
658 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, req->op.mod.message->dn,
660 DSDB_FLAG_NEXT_MODULE);
662 if (ret != LDB_SUCCESS) {
666 schema = dsdb_get_schema(ldb, tmp_ctx);
668 ret = LDB_ERR_OPERATIONS_ERROR;
672 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
673 if (ret != LDB_SUCCESS) {
674 DEBUG(10, ("acl_modify: cannot get descriptor\n"));
677 /* Theoretically we pass the check if the object has no sd */
682 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
684 DEBUG(10, ("acl_modify: cannot get guid\n"));
687 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
688 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
690 DEBUG(10, ("acl_modify: cannot add to object tree\n"));
693 for (i=0; i < req->op.mod.message->num_elements; i++){
694 const struct dsdb_attribute *attr;
695 attr = dsdb_attribute_by_lDAPDisplayName(schema,
696 req->op.mod.message->elements[i].name);
698 if (ldb_attr_cmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
699 status = sec_access_check_ds(sd, acl_user_token(module),
705 if (!NT_STATUS_IS_OK(status)) {
706 DEBUG(10, ("Object %s has no write dacl access\n",
707 ldb_dn_get_linearized(req->op.mod.message->dn)));
709 acl_user_token(module),
710 req->op.mod.message->dn,
713 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
717 else if (ldb_attr_cmp("member", req->op.mod.message->elements[i].name) == 0) {
718 ret = acl_check_self_membership(tmp_ctx,
725 if (ret != LDB_SUCCESS) {
729 else if (ldb_attr_cmp("dBCSPwd", req->op.mod.message->elements[i].name) == 0) {
730 /* this one is not affected by any rights, we should let it through
731 so that passwords_hash returns the correct error */
734 else if (ldb_attr_cmp("unicodePwd", req->op.mod.message->elements[i].name) == 0 ||
735 ldb_attr_cmp("userPassword", req->op.mod.message->elements[i].name) == 0 ||
736 ldb_attr_cmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
737 ret = acl_check_password_rights(tmp_ctx,
743 if (ret != LDB_SUCCESS) {
748 /* This basic attribute existence check with the right errorcode
749 * is needed since this module is the first one which requests
750 * schema attribute informations.
751 * The complete attribute checking is done in the
752 * "objectclass_attrs" module behind this one.
755 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' on entry '%s' was not found in the schema!",
756 req->op.mod.message->elements[i].name,
757 ldb_dn_get_linearized(req->op.mod.message->dn));
758 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
761 if (!insert_in_object_tree(tmp_ctx,
762 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
763 &new_node, &new_node)) {
764 DEBUG(10, ("acl_modify: cannot add to object tree securityGUID\n"));
765 ret = LDB_ERR_OPERATIONS_ERROR;
769 if (!insert_in_object_tree(tmp_ctx,
770 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
771 DEBUG(10, ("acl_modify: cannot add to object tree attributeGUID\n"));
772 ret = LDB_ERR_OPERATIONS_ERROR;
778 if (root->num_of_children > 0) {
779 status = sec_access_check_ds(sd, acl_user_token(module),
785 if (!NT_STATUS_IS_OK(status)) {
786 DEBUG(10, ("Object %s has no write property access\n",
787 ldb_dn_get_linearized(req->op.mod.message->dn)));
789 acl_user_token(module),
790 req->op.mod.message->dn,
793 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
799 talloc_free(tmp_ctx);
800 return ldb_next_request(module, req);
802 talloc_free(tmp_ctx);
806 /* similar to the modify for the time being.
807 * We need to concider the special delete tree case, though - TODO */
808 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
811 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.del.dn);
812 struct ldb_context *ldb;
813 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
815 if (as_system != NULL) {
816 as_system->critical = 0;
819 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
820 if (dsdb_module_am_system(module) || as_system) {
821 return ldb_next_request(module, req);
824 if (ldb_dn_is_special(req->op.del.dn)) {
825 return ldb_next_request(module, req);
827 ldb = ldb_module_get_ctx(module);
828 /* first check if we have delete object right */
829 ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn, SEC_STD_DELETE, NULL);
830 if (ret == LDB_SUCCESS) {
831 return ldb_next_request(module, req);
834 /* Nope, we don't have delete object. Lets check if we have delete child on the parent */
835 /* No parent, so check fails */
836 /* FIXME: this has to be made dynamic at some point */
837 if ((ldb_dn_compare(req->op.del.dn, (ldb_get_schema_basedn(ldb))) == 0) ||
838 (ldb_dn_compare(req->op.del.dn, (ldb_get_config_basedn(ldb))) == 0) ||
839 (ldb_dn_compare(req->op.del.dn, (ldb_get_default_basedn(ldb))) == 0)) {
840 DEBUG(10,("acl:deleting an NC\n"));
841 return ldb_module_done(req, NULL, NULL, LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS);
844 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_DELETE_CHILD, NULL);
845 if (ret != LDB_SUCCESS) {
848 return ldb_next_request(module, req);
851 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
854 struct ldb_dn *oldparent = ldb_dn_get_parent(req, req->op.rename.olddn);
855 struct ldb_dn *newparent = ldb_dn_get_parent(req, req->op.rename.newdn);
856 const struct dsdb_schema *schema;
857 struct ldb_context *ldb;
858 struct security_descriptor *sd = NULL;
859 struct dom_sid *sid = NULL;
860 struct ldb_result *acl_res;
861 const struct GUID *guid;
862 struct object_tree *root = NULL;
863 struct object_tree *new_node = NULL;
864 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
865 TALLOC_CTX *tmp_ctx = talloc_new(req);
867 uint32_t access_granted;
868 const char *rdn_name;
869 static const char *acl_attrs[] = {
870 "nTSecurityDescriptor",
876 if (as_system != NULL) {
877 as_system->critical = 0;
880 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
881 if (dsdb_module_am_system(module) || as_system) {
882 return ldb_next_request(module, req);
884 if (ldb_dn_is_special(req->op.rename.olddn)) {
885 return ldb_next_request(module, req);
887 ldb = ldb_module_get_ctx(module);
889 ret = dsdb_module_search_dn(module, req, &acl_res, req->op.rename.olddn,
891 DSDB_FLAG_NEXT_MODULE |
892 DSDB_SEARCH_SHOW_RECYCLED);
893 /* we sould be able to find the parent */
894 if (ret != LDB_SUCCESS) {
895 DEBUG(10,("acl: failed to find object %s\n",
896 ldb_dn_get_linearized(req->op.rename.olddn)));
900 schema = dsdb_get_schema(ldb, acl_res);
902 talloc_free(acl_res);
903 return ldb_operr(ldb);
906 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
907 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
909 return ldb_operr(ldb);
912 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
914 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
915 &new_node, &new_node)) {
916 return ldb_operr(ldb);
919 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
920 if (rdn_name == NULL) {
921 return ldb_operr(ldb);
923 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
925 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
926 &new_node, &new_node)) {
927 return ldb_operr(ldb);
930 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
932 if (ret != LDB_SUCCESS) {
933 return ldb_operr(ldb);
935 /* Theoretically we pass the check if the object has no sd */
939 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
940 status = sec_access_check_ds(sd, acl_user_token(module),
946 if (!NT_STATUS_IS_OK(status)) {
947 DEBUG(10, ("Object %s has no wp on name\n",
948 ldb_dn_get_linearized(req->op.rename.olddn)));
950 acl_user_token(module),
951 req->op.rename.olddn,
954 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
957 if (ldb_dn_compare(oldparent, newparent) == 0) {
958 /* regular rename, not move, nothing more to do */
959 return ldb_next_request(module, req);
962 /* What exactly to do in this case? It would fail anyway.. */
963 /* FIXME: this has to be made dynamic at some point */
964 if ((ldb_dn_compare(req->op.rename.newdn, (ldb_get_schema_basedn(ldb))) == 0) ||
965 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_config_basedn(ldb))) == 0) ||
966 (ldb_dn_compare(req->op.rename.newdn, (ldb_get_default_basedn(ldb))) == 0)) {
967 DEBUG(10,("acl:moving as an NC\n"));
968 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
970 /* new parent should have create child */
971 talloc_free(tmp_ctx);
972 tmp_ctx = talloc_new(req);
975 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
977 DEBUG(10,("acl:renamed object has no object class\n"));
978 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
981 ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid);
982 if (ret != LDB_SUCCESS) {
983 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
986 /* do we have delete object on the object? */
988 status = sec_access_check_ds(sd, acl_user_token(module),
994 if (NT_STATUS_IS_OK(status)) {
995 return ldb_next_request(module, req);
997 /* what about delete child on the current parent */
998 ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL);
999 if (ret != LDB_SUCCESS) {
1000 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1001 return ldb_module_done(req, NULL, NULL, ret);
1003 return ldb_next_request(module, req);
1006 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1008 struct ldb_context *ldb;
1009 struct acl_context *ac;
1010 struct acl_private *data;
1011 struct ldb_result *acl_res;
1012 static const char *acl_attrs[] = {
1014 "nTSecurityDescriptor",
1020 ac = talloc_get_type(req->context, struct acl_context);
1021 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1022 ldb = ldb_module_get_ctx(ac->module);
1025 return ldb_module_done(ac->req, NULL, NULL,
1026 LDB_ERR_OPERATIONS_ERROR);
1028 if (ares->error != LDB_SUCCESS) {
1029 return ldb_module_done(ac->req, ares->controls,
1030 ares->response, ares->error);
1033 switch (ares->type) {
1034 case LDB_REPLY_ENTRY:
1035 if (ac->allowedAttributes
1036 || ac->allowedChildClasses
1037 || ac->allowedChildClassesEffective
1038 || ac->allowedAttributesEffective
1039 || ac->sDRightsEffective) {
1040 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1042 DSDB_FLAG_NEXT_MODULE);
1043 if (ret != LDB_SUCCESS) {
1044 return ldb_module_done(ac->req, NULL, NULL, ret);
1046 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1047 ret = acl_allowedAttributes(ac->module, ac->schema, acl_res->msgs[0], ares->message, ac);
1048 if (ret != LDB_SUCCESS) {
1049 return ldb_module_done(ac->req, NULL, NULL, ret);
1052 if (ac->allowedChildClasses) {
1053 ret = acl_childClasses(ac->module, ac->schema, acl_res->msgs[0],
1054 ares->message, "allowedChildClasses");
1055 if (ret != LDB_SUCCESS) {
1056 return ldb_module_done(ac->req, NULL, NULL, ret);
1059 if (ac->allowedChildClassesEffective) {
1060 ret = acl_childClassesEffective(ac->module, ac->schema,
1061 acl_res->msgs[0], ares->message, ac);
1062 if (ret != LDB_SUCCESS) {
1063 return ldb_module_done(ac->req, NULL, NULL, ret);
1066 if (ac->sDRightsEffective) {
1067 ret = acl_sDRightsEffective(ac->module,
1068 acl_res->msgs[0], ares->message, ac);
1069 if (ret != LDB_SUCCESS) {
1070 return ldb_module_done(ac->req, NULL, NULL, ret);
1074 if (data && data->password_attrs) {
1075 if (!ac->am_system) {
1076 for (i = 0; data->password_attrs[i]; i++) {
1077 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1081 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1083 case LDB_REPLY_REFERRAL:
1084 return ldb_module_send_referral(ac->req, ares->referral);
1086 case LDB_REPLY_DONE:
1087 return ldb_module_done(ac->req, ares->controls,
1088 ares->response, LDB_SUCCESS);
1094 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1096 struct ldb_context *ldb;
1097 struct acl_context *ac;
1098 struct ldb_request *down_req;
1099 struct acl_private *data;
1102 ldb = ldb_module_get_ctx(module);
1104 ac = talloc_zero(req, struct acl_context);
1106 return ldb_oom(ldb);
1108 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1110 ac->module = module;
1112 ac->am_system = dsdb_module_am_system(module);
1113 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1114 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1115 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1116 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1117 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1118 ac->schema = dsdb_get_schema(ldb, ac);
1120 /* replace any attributes in the parse tree that are private,
1121 so we don't allow a search for 'userPassword=penguin',
1122 just as we would not allow that attribute to be returned */
1123 if (ac->am_system) {
1124 /* FIXME: We should copy the tree and keep the original unmodified. */
1125 /* remove password attributes */
1126 if (data && data->password_attrs) {
1127 for (i = 0; data->password_attrs[i]; i++) {
1128 ldb_parse_tree_attr_replace(req->op.search.tree,
1129 data->password_attrs[i],
1130 "kludgeACLredactedattribute");
1134 ret = ldb_build_search_req_ex(&down_req,
1136 req->op.search.base,
1137 req->op.search.scope,
1138 req->op.search.tree,
1139 req->op.search.attrs,
1141 ac, acl_search_callback,
1143 LDB_REQ_SET_LOCATION(down_req);
1144 if (ret != LDB_SUCCESS) {
1147 /* perform the search */
1148 return ldb_next_request(module, down_req);
1151 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1153 struct ldb_context *ldb = ldb_module_get_ctx(module);
1154 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1156 /* allow everybody to read the sequence number */
1157 if (strcmp(req->op.extended.oid,
1158 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1159 return ldb_next_request(module, req);
1162 if (dsdb_module_am_system(module) ||
1163 dsdb_module_am_administrator(module) || as_system) {
1164 return ldb_next_request(module, req);
1166 ldb_asprintf_errstring(ldb,
1168 "attempted database modify not permitted. "
1169 "User %s is not SYSTEM or an administrator",
1170 acl_user_name(req, module));
1171 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1175 static const struct ldb_module_ops ldb_acl_module_ops = {
1177 .search = acl_search,
1179 .modify = acl_modify,
1181 .rename = acl_rename,
1182 .extended = acl_extended,
1183 .init_context = acl_module_init
1186 int ldb_acl_module_init(const char *version)
1188 LDB_MODULE_CHECK_VERSION(version);
1189 return ldb_register_module(&ldb_acl_module_ops);