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"
44 #include "system/kerberos.h"
45 #include "auth/kerberos/kerberos.h"
47 struct extended_access_check_attribute {
49 const uint32_t requires_rights;
54 const char **password_attrs;
58 struct ldb_module *module;
59 struct ldb_request *req;
61 bool allowedAttributes;
62 bool allowedAttributesEffective;
63 bool allowedChildClasses;
64 bool allowedChildClassesEffective;
65 bool sDRightsEffective;
67 const char * const *attrs;
68 struct dsdb_schema *schema;
71 static int acl_module_init(struct ldb_module *module)
73 struct ldb_context *ldb;
74 struct acl_private *data;
78 static const char *attrs[] = { "passwordAttribute", NULL };
79 struct ldb_result *res;
80 struct ldb_message *msg;
81 struct ldb_message_element *password_attributes;
83 ldb = ldb_module_get_ctx(module);
85 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
86 if (ret != LDB_SUCCESS) {
87 ldb_debug(ldb, LDB_DEBUG_ERROR,
88 "acl_module_init: Unable to register control with rootdse!\n");
89 return ldb_operr(ldb);
92 data = talloc(module, struct acl_private);
97 data->password_attrs = NULL;
98 data->acl_perform = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"),
99 NULL, "acl", "perform", false);
100 ldb_module_set_private(module, data);
102 mem_ctx = talloc_new(module);
107 ret = dsdb_module_search_dn(module, mem_ctx, &res,
108 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
110 DSDB_FLAG_NEXT_MODULE);
111 if (ret != LDB_SUCCESS) {
114 if (res->count == 0) {
118 if (res->count > 1) {
119 talloc_free(mem_ctx);
120 return LDB_ERR_CONSTRAINT_VIOLATION;
125 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
126 if (!password_attributes) {
129 data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
130 if (!data->password_attrs) {
131 talloc_free(mem_ctx);
134 for (i=0; i < password_attributes->num_values; i++) {
135 data->password_attrs[i] = (const char *)password_attributes->values[i].data;
136 talloc_steal(data->password_attrs, password_attributes->values[i].data);
138 data->password_attrs[i] = NULL;
141 talloc_free(mem_ctx);
142 return ldb_next_init(module);
145 static int acl_allowedAttributes(struct ldb_module *module,
146 const struct dsdb_schema *schema,
147 struct ldb_message *sd_msg,
148 struct ldb_message *msg,
149 struct acl_context *ac)
151 struct ldb_message_element *oc_el;
152 struct ldb_context *ldb = ldb_module_get_ctx(module);
154 const char **attr_list;
157 /* If we don't have a schema yet, we can't do anything... */
158 if (schema == NULL) {
159 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
160 return LDB_ERR_OPERATIONS_ERROR;
163 /* Must remove any existing attribute */
164 if (ac->allowedAttributes) {
165 ldb_msg_remove_attr(msg, "allowedAttributes");
168 mem_ctx = talloc_new(msg);
173 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
174 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
176 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
177 talloc_free(mem_ctx);
178 return LDB_ERR_OPERATIONS_ERROR;
180 if (ac->allowedAttributes) {
181 for (i=0; attr_list && attr_list[i]; i++) {
182 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
185 if (ac->allowedAttributesEffective) {
186 struct security_descriptor *sd;
187 struct dom_sid *sid = NULL;
188 struct ldb_control *as_system = ldb_request_get_control(ac->req,
189 LDB_CONTROL_AS_SYSTEM_OID);
191 if (as_system != NULL) {
192 as_system->critical = 0;
195 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
196 if (ac->am_system || as_system) {
197 for (i=0; attr_list && attr_list[i]; i++) {
198 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
203 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), mem_ctx, sd_msg, &sd);
205 if (ret != LDB_SUCCESS) {
209 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
210 for (i=0; attr_list && attr_list[i]; i++) {
211 const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
214 return ldb_operr(ldb);
216 /* remove constructed attributes */
217 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
219 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
222 ret = acl_check_access_on_attribute(module,
228 if (ret == LDB_SUCCESS) {
229 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
236 static int acl_childClasses(struct ldb_module *module,
237 const struct dsdb_schema *schema,
238 struct ldb_message *sd_msg,
239 struct ldb_message *msg,
240 const char *attrName)
242 struct ldb_message_element *oc_el;
243 struct ldb_message_element *allowedClasses;
244 const struct dsdb_class *sclass;
248 /* If we don't have a schema yet, we can't do anything... */
249 if (schema == NULL) {
250 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
251 return LDB_ERR_OPERATIONS_ERROR;
254 /* Must remove any existing attribute, or else confusion reins */
255 ldb_msg_remove_attr(msg, attrName);
256 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
257 if (ret != LDB_SUCCESS) {
261 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
263 for (i=0; oc_el && i < oc_el->num_values; i++) {
264 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
266 /* We don't know this class? what is going on? */
270 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
271 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
274 if (allowedClasses->num_values > 1) {
275 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
276 for (i=1 ; i < allowedClasses->num_values; i++) {
277 struct ldb_val *val1 = &allowedClasses->values[i-1];
278 struct ldb_val *val2 = &allowedClasses->values[i];
279 if (data_blob_cmp(val1, val2) == 0) {
280 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
281 allowedClasses->num_values--;
290 static int acl_childClassesEffective(struct ldb_module *module,
291 const struct dsdb_schema *schema,
292 struct ldb_message *sd_msg,
293 struct ldb_message *msg,
294 struct acl_context *ac)
296 struct ldb_message_element *oc_el;
297 struct ldb_message_element *allowedClasses = NULL;
298 const struct dsdb_class *sclass;
299 struct security_descriptor *sd;
300 struct ldb_control *as_system = ldb_request_get_control(ac->req,
301 LDB_CONTROL_AS_SYSTEM_OID);
302 struct dom_sid *sid = NULL;
306 if (as_system != NULL) {
307 as_system->critical = 0;
310 if (ac->am_system || as_system) {
311 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
314 /* If we don't have a schema yet, we can't do anything... */
315 if (schema == NULL) {
316 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
317 return LDB_ERR_OPERATIONS_ERROR;
320 /* Must remove any existing attribute, or else confusion reins */
321 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
323 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
324 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
325 if (ret != LDB_SUCCESS) {
329 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
330 for (i=0; oc_el && i < oc_el->num_values; i++) {
331 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
333 /* We don't know this class? what is going on? */
337 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
338 ret = acl_check_access_on_class(module,
343 SEC_ADS_CREATE_CHILD,
344 sclass->possibleInferiors[j]);
345 if (ret == LDB_SUCCESS) {
346 ldb_msg_add_string(msg, "allowedChildClassesEffective",
347 sclass->possibleInferiors[j]);
351 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
352 if (!allowedClasses) {
356 if (allowedClasses->num_values > 1) {
357 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
358 for (i=1 ; i < allowedClasses->num_values; i++) {
359 struct ldb_val *val1 = &allowedClasses->values[i-1];
360 struct ldb_val *val2 = &allowedClasses->values[i];
361 if (data_blob_cmp(val1, val2) == 0) {
362 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
363 allowedClasses->num_values--;
371 static int acl_sDRightsEffective(struct ldb_module *module,
372 struct ldb_message *sd_msg,
373 struct ldb_message *msg,
374 struct acl_context *ac)
376 struct ldb_message_element *rightsEffective;
378 struct security_descriptor *sd;
379 struct ldb_control *as_system = ldb_request_get_control(ac->req,
380 LDB_CONTROL_AS_SYSTEM_OID);
381 struct dom_sid *sid = NULL;
384 if (as_system != NULL) {
385 as_system->critical = 0;
388 /* Must remove any existing attribute, or else confusion reins */
389 ldb_msg_remove_attr(msg, "sDRightsEffective");
390 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
391 if (ret != LDB_SUCCESS) {
394 if (ac->am_system || as_system) {
395 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
398 /* Get the security descriptor from the message */
399 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
400 if (ret != LDB_SUCCESS) {
403 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
404 ret = acl_check_access_on_attribute(module,
410 if (ret == LDB_SUCCESS) {
411 flags |= SECINFO_OWNER | SECINFO_GROUP;
413 ret = acl_check_access_on_attribute(module,
419 if (ret == LDB_SUCCESS) {
420 flags |= SECINFO_DACL;
422 ret = acl_check_access_on_attribute(module,
426 SEC_FLAG_SYSTEM_SECURITY,
428 if (ret == LDB_SUCCESS) {
429 flags |= SECINFO_SACL;
432 return samdb_msg_add_uint(ldb_module_get_ctx(module), msg, msg,
433 "sDRightsEffective", flags);
436 static int acl_validate_spn_value(TALLOC_CTX *mem_ctx,
437 struct ldb_context *ldb,
438 const char *spn_value,
439 int userAccountControl,
440 const char *samAccountName,
441 const char *dnsHostName,
442 const char *netbios_name,
443 const char *ntds_guid)
446 krb5_context krb_ctx;
447 krb5_error_code kerr;
448 krb5_principal principal;
453 const char *guid_str;
454 const char *forest_name = samdb_forest_name(ldb, mem_ctx);
455 const char *base_domain = samdb_default_domain_name(ldb, mem_ctx);
456 struct loadparm_context *lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
457 struct loadparm_context);
458 bool is_dc = (userAccountControl & UF_SERVER_TRUST_ACCOUNT) ||
459 (userAccountControl & UF_PARTIAL_SECRETS_ACCOUNT);
461 kerr = smb_krb5_init_context_basic(mem_ctx,
465 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
466 "Could not initialize kerberos context.");
469 ret = krb5_parse_name(krb_ctx, spn_value, &principal);
471 krb5_free_context(krb_ctx);
472 return LDB_ERR_CONSTRAINT_VIOLATION;
475 instanceName = principal->name.name_string.val[1];
476 serviceType = principal->name.name_string.val[0];
477 realm = krb5_principal_get_realm(krb_ctx, principal);
478 guid_str = talloc_asprintf(mem_ctx,"%s._msdcs.%s",
481 if (principal->name.name_string.len == 3) {
482 serviceName = principal->name.name_string.val[2];
491 if (strcasecmp(serviceType, "ldap") == 0) {
492 if (strcasecmp(serviceName, netbios_name) != 0 &&
493 strcasecmp(serviceName, forest_name) != 0) {
497 } else if (strcasecmp(serviceType, "gc") == 0) {
498 if (strcasecmp(serviceName, forest_name) != 0) {
502 if (strcasecmp(serviceName, base_domain) != 0 &&
503 strcasecmp(serviceName, netbios_name) != 0) {
508 /* instanceName can be samAccountName without $ or dnsHostName
509 * or "ntds_guid._msdcs.forest_domain for DC objects */
510 if (strncasecmp(instanceName, samAccountName, strlen(samAccountName - 1)) == 0) {
512 } else if (strcasecmp(instanceName, dnsHostName) == 0) {
515 if (strcasecmp(instanceName, guid_str) == 0) {
522 krb5_free_principal(krb_ctx, principal);
523 krb5_free_context(krb_ctx);
524 return LDB_ERR_CONSTRAINT_VIOLATION;
527 krb5_free_principal(krb_ctx, principal);
528 krb5_free_context(krb_ctx);
532 static int acl_check_spn(TALLOC_CTX *mem_ctx,
533 struct ldb_module *module,
534 struct ldb_request *req,
535 struct security_descriptor *sd,
537 const struct GUID *oc_guid,
538 const struct dsdb_attribute *attr)
542 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
543 struct ldb_context *ldb = ldb_module_get_ctx(module);
544 struct ldb_result *acl_res;
545 struct ldb_result *netbios_res;
546 struct ldb_message_element *el;
547 struct ldb_dn *partitions_dn = samdb_partitions_dn(ldb, tmp_ctx);
548 int userAccountControl;
549 const char *samAccountName;
550 const char *dnsHostName;
551 const char *netbios_name;
552 const struct GUID *ntds = samdb_ntds_objectGUID(ldb);
553 const char *ntds_guid = GUID_string(tmp_ctx, ntds);
555 static const char *acl_attrs[] = {
558 "userAccountControl",
561 static const char *netbios_attrs[] = {
565 /* if we have wp, we can do whatever we like */
566 if (acl_check_access_on_attribute(module,
571 attr) == LDB_SUCCESS) {
572 talloc_free(tmp_ctx);
576 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
577 GUID_DRS_VALIDATE_SPN,
581 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
582 dsdb_acl_debug(sd, acl_user_token(module),
583 req->op.mod.message->dn,
586 talloc_free(tmp_ctx);
590 ret = dsdb_module_search_dn(module, tmp_ctx,
591 &acl_res, req->op.mod.message->dn,
593 DSDB_FLAG_NEXT_MODULE |
594 DSDB_SEARCH_SHOW_DELETED);
595 if (ret != LDB_SUCCESS) {
596 talloc_free(tmp_ctx);
600 userAccountControl = ldb_msg_find_attr_as_int(acl_res->msgs[0], "userAccountControl", 0);
601 dnsHostName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "dnsHostName", NULL);
602 samAccountName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "samAccountName", NULL);
604 ret = dsdb_module_search(module, tmp_ctx,
605 &netbios_res, partitions_dn,
608 DSDB_FLAG_NEXT_MODULE,
610 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)));
612 netbios_name = ldb_msg_find_attr_as_string(netbios_res->msgs[0], "nETBIOSName", NULL);
614 el = ldb_msg_find_element(req->op.mod.message, "servicePrincipalName");
616 talloc_free(tmp_ctx);
617 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
618 "Error finding element for servicePrincipalName.");
621 for (i=0; i < el->num_values; i++) {
622 ret = acl_validate_spn_value(tmp_ctx,
624 (char *)el->values[i].data,
630 if (ret != LDB_SUCCESS) {
631 talloc_free(tmp_ctx);
635 talloc_free(tmp_ctx);
639 static int acl_add(struct ldb_module *module, struct ldb_request *req)
642 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.add.message->dn);
643 struct ldb_context *ldb;
644 const struct dsdb_schema *schema;
645 struct ldb_message_element *oc_el;
646 const struct GUID *guid;
647 struct ldb_dn *nc_root;
648 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
650 if (as_system != NULL) {
651 as_system->critical = 0;
654 if (dsdb_module_am_system(module) || as_system) {
655 return ldb_next_request(module, req);
657 if (ldb_dn_is_special(req->op.add.message->dn)) {
658 return ldb_next_request(module, req);
661 ldb = ldb_module_get_ctx(module);
663 /* Creating an NC. There is probably something we should do here,
664 * but we will establish that later */
666 ret = dsdb_find_nc_root(ldb, req, req->op.add.message->dn, &nc_root);
667 if (ret != LDB_SUCCESS) {
670 if (ldb_dn_compare(nc_root, req->op.add.message->dn) == 0) {
671 talloc_free(nc_root);
672 return ldb_next_request(module, req);
674 talloc_free(nc_root);
676 schema = dsdb_get_schema(ldb, req);
678 return ldb_operr(ldb);
681 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
682 if (!oc_el || oc_el->num_values == 0) {
683 DEBUG(10,("acl:operation error %s\n", ldb_dn_get_linearized(req->op.add.message->dn)));
684 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
687 guid = class_schemaid_guid_by_lDAPDisplayName(schema,
688 (char *)oc_el->values[oc_el->num_values-1].data);
689 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid);
690 if (ret != LDB_SUCCESS) {
693 return ldb_next_request(module, req);
696 /* ckecks if modifications are allowed on "Member" attribute */
697 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
698 struct ldb_module *module,
699 struct ldb_request *req,
700 struct security_descriptor *sd,
702 const struct GUID *oc_guid,
703 const struct dsdb_attribute *attr)
707 struct ldb_context *ldb = ldb_module_get_ctx(module);
708 struct ldb_dn *user_dn;
709 struct ldb_message_element *member_el;
710 /* if we have wp, we can do whatever we like */
711 if (acl_check_access_on_attribute(module,
716 attr) == LDB_SUCCESS) {
719 /* if we are adding/deleting ourselves, check for self membership */
720 ret = dsdb_find_dn_by_sid(ldb, mem_ctx,
721 &acl_user_token(module)->sids[PRIMARY_USER_SID_INDEX],
723 if (ret != LDB_SUCCESS) {
726 member_el = ldb_msg_find_element(req->op.mod.message, "member");
728 return ldb_operr(ldb);
730 /* user can only remove oneself */
731 if (member_el->num_values == 0) {
732 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
734 for (i = 0; i < member_el->num_values; i++) {
735 if (strcasecmp((const char *)member_el->values[i].data,
736 ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
737 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
740 ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
741 GUID_DRS_SELF_MEMBERSHIP,
744 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
745 dsdb_acl_debug(sd, acl_user_token(module),
746 req->op.mod.message->dn,
753 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
754 struct ldb_module *module,
755 struct ldb_request *req,
756 struct security_descriptor *sd,
758 const struct GUID *oc_guid,
761 int ret = LDB_SUCCESS;
762 unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
763 struct ldb_message_element *el;
764 struct ldb_message *msg;
765 const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
766 "unicodePwd", "dBCSPwd", NULL }, **l;
767 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
769 msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
771 return ldb_module_oom(module);
773 for (l = passwordAttrs; *l != NULL; l++) {
774 if ((!userPassword) && (ldb_attr_cmp(*l, "userPassword") == 0)) {
778 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
779 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
782 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
785 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
788 ldb_msg_remove_element(msg, el);
792 /* single deletes will be handled by the "password_hash" LDB module
793 * later in the stack, so we let it though here */
794 if ((del_attr_cnt > 0) && (add_attr_cnt == 0) && (rep_attr_cnt == 0)) {
795 talloc_free(tmp_ctx);
799 if (ldb_request_get_control(req,
800 DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
801 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
802 * have a user password change and not a set as the message
803 * looks like. In it's value blob it contains the NT and/or LM
804 * hash of the old password specified by the user.
805 * This control is used by the SAMR and "kpasswd" password
806 * change mechanisms. */
807 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
808 GUID_DRS_USER_CHANGE_PASSWORD,
809 SEC_ADS_CONTROL_ACCESS,
812 else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
813 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
814 GUID_DRS_FORCE_CHANGE_PASSWORD,
815 SEC_ADS_CONTROL_ACCESS,
818 else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
819 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
820 GUID_DRS_USER_CHANGE_PASSWORD,
821 SEC_ADS_CONTROL_ACCESS,
823 /* Very strange, but we get constraint violation in this case */
824 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
825 ret = LDB_ERR_CONSTRAINT_VIOLATION;
828 if (ret != LDB_SUCCESS) {
829 dsdb_acl_debug(sd, acl_user_token(module),
830 req->op.mod.message->dn,
834 talloc_free(tmp_ctx);
838 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
841 struct ldb_context *ldb = ldb_module_get_ctx(module);
842 const struct dsdb_schema *schema;
844 const struct GUID *guid;
845 uint32_t access_granted;
846 struct object_tree *root = NULL;
847 struct object_tree *new_node = NULL;
849 struct ldb_result *acl_res;
850 struct security_descriptor *sd;
851 struct dom_sid *sid = NULL;
852 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
853 bool userPassword = dsdb_user_password_support(module, req);
854 TALLOC_CTX *tmp_ctx = talloc_new(req);
855 static const char *acl_attrs[] = {
856 "nTSecurityDescriptor",
862 if (as_system != NULL) {
863 as_system->critical = 0;
866 /* Don't print this debug statement if elements[0].name is going to be NULL */
867 if(req->op.mod.message->num_elements > 0)
869 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->elements[0].name));
871 if (dsdb_module_am_system(module) || as_system) {
872 return ldb_next_request(module, req);
874 if (ldb_dn_is_special(req->op.mod.message->dn)) {
875 return ldb_next_request(module, req);
877 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, req->op.mod.message->dn,
879 DSDB_FLAG_NEXT_MODULE);
881 if (ret != LDB_SUCCESS) {
885 schema = dsdb_get_schema(ldb, tmp_ctx);
887 ret = LDB_ERR_OPERATIONS_ERROR;
891 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
892 if (ret != LDB_SUCCESS) {
893 DEBUG(10, ("acl_modify: cannot get descriptor\n"));
896 /* Theoretically we pass the check if the object has no sd */
901 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
903 DEBUG(10, ("acl_modify: cannot get guid\n"));
906 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
907 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
909 DEBUG(10, ("acl_modify: cannot add to object tree\n"));
912 for (i=0; i < req->op.mod.message->num_elements; i++){
913 const struct dsdb_attribute *attr;
914 attr = dsdb_attribute_by_lDAPDisplayName(schema,
915 req->op.mod.message->elements[i].name);
917 if (ldb_attr_cmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
918 status = sec_access_check_ds(sd, acl_user_token(module),
924 if (!NT_STATUS_IS_OK(status)) {
925 DEBUG(10, ("Object %s has no write dacl access\n",
926 ldb_dn_get_linearized(req->op.mod.message->dn)));
928 acl_user_token(module),
929 req->op.mod.message->dn,
932 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
936 else if (ldb_attr_cmp("member", req->op.mod.message->elements[i].name) == 0) {
937 ret = acl_check_self_membership(tmp_ctx,
944 if (ret != LDB_SUCCESS) {
948 else if (ldb_attr_cmp("dBCSPwd", req->op.mod.message->elements[i].name) == 0) {
949 /* this one is not affected by any rights, we should let it through
950 so that passwords_hash returns the correct error */
953 else if (ldb_attr_cmp("unicodePwd", req->op.mod.message->elements[i].name) == 0 ||
954 (userPassword && ldb_attr_cmp("userPassword", req->op.mod.message->elements[i].name) == 0) ||
955 ldb_attr_cmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
956 ret = acl_check_password_rights(tmp_ctx,
963 if (ret != LDB_SUCCESS) {
966 } else if (ldb_attr_cmp("servicePrincipalName", req->op.mod.message->elements[i].name) == 0) {
967 ret = acl_check_spn(tmp_ctx,
974 if (ret != LDB_SUCCESS) {
979 /* This basic attribute existence check with the right errorcode
980 * is needed since this module is the first one which requests
981 * schema attribute informations.
982 * The complete attribute checking is done in the
983 * "objectclass_attrs" module behind this one.
986 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' on entry '%s' was not found in the schema!",
987 req->op.mod.message->elements[i].name,
988 ldb_dn_get_linearized(req->op.mod.message->dn));
989 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
992 if (!insert_in_object_tree(tmp_ctx,
993 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
994 &new_node, &new_node)) {
995 DEBUG(10, ("acl_modify: cannot add to object tree securityGUID\n"));
996 ret = LDB_ERR_OPERATIONS_ERROR;
1000 if (!insert_in_object_tree(tmp_ctx,
1001 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
1002 DEBUG(10, ("acl_modify: cannot add to object tree attributeGUID\n"));
1003 ret = LDB_ERR_OPERATIONS_ERROR;
1009 if (root->num_of_children > 0) {
1010 status = sec_access_check_ds(sd, acl_user_token(module),
1016 if (!NT_STATUS_IS_OK(status)) {
1017 DEBUG(10, ("Object %s has no write property access\n",
1018 ldb_dn_get_linearized(req->op.mod.message->dn)));
1020 acl_user_token(module),
1021 req->op.mod.message->dn,
1024 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1030 talloc_free(tmp_ctx);
1031 return ldb_next_request(module, req);
1033 talloc_free(tmp_ctx);
1037 /* similar to the modify for the time being.
1038 * We need to consider the special delete tree case, though - TODO */
1039 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
1042 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.del.dn);
1043 struct ldb_context *ldb;
1044 struct ldb_dn *nc_root;
1045 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1047 if (as_system != NULL) {
1048 as_system->critical = 0;
1051 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
1052 if (dsdb_module_am_system(module) || as_system) {
1053 return ldb_next_request(module, req);
1055 if (ldb_dn_is_special(req->op.del.dn)) {
1056 return ldb_next_request(module, req);
1059 ldb = ldb_module_get_ctx(module);
1061 /* Make sure we aren't deleting a NC */
1063 ret = dsdb_find_nc_root(ldb, req, req->op.del.dn, &nc_root);
1064 if (ret != LDB_SUCCESS) {
1067 if (ldb_dn_compare(nc_root, req->op.del.dn) == 0) {
1068 talloc_free(nc_root);
1069 DEBUG(10,("acl:deleting a NC\n"));
1070 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1071 return ldb_module_done(req, NULL, NULL,
1072 LDB_ERR_UNWILLING_TO_PERFORM);
1074 talloc_free(nc_root);
1076 /* First check if we have delete object right */
1077 ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn,
1078 SEC_STD_DELETE, NULL);
1079 if (ret == LDB_SUCCESS) {
1080 return ldb_next_request(module, req);
1083 /* Nope, we don't have delete object. Lets check if we have delete
1084 * child on the parent */
1085 ret = dsdb_module_check_access_on_dn(module, req, parent,
1086 SEC_ADS_DELETE_CHILD, NULL);
1087 if (ret != LDB_SUCCESS) {
1091 return ldb_next_request(module, req);
1094 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
1097 struct ldb_dn *oldparent = ldb_dn_get_parent(req, req->op.rename.olddn);
1098 struct ldb_dn *newparent = ldb_dn_get_parent(req, req->op.rename.newdn);
1099 const struct dsdb_schema *schema;
1100 struct ldb_context *ldb;
1101 struct security_descriptor *sd = NULL;
1102 struct dom_sid *sid = NULL;
1103 struct ldb_result *acl_res;
1104 const struct GUID *guid;
1105 struct ldb_dn *nc_root;
1106 struct object_tree *root = NULL;
1107 struct object_tree *new_node = NULL;
1108 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1109 TALLOC_CTX *tmp_ctx = talloc_new(req);
1111 uint32_t access_granted;
1112 const char *rdn_name;
1113 static const char *acl_attrs[] = {
1114 "nTSecurityDescriptor",
1120 if (as_system != NULL) {
1121 as_system->critical = 0;
1124 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
1125 if (dsdb_module_am_system(module) || as_system) {
1126 return ldb_next_request(module, req);
1128 if (ldb_dn_is_special(req->op.rename.olddn)) {
1129 return ldb_next_request(module, req);
1132 ldb = ldb_module_get_ctx(module);
1134 /* Make sure we aren't renaming/moving a NC */
1136 ret = dsdb_find_nc_root(ldb, req, req->op.rename.olddn, &nc_root);
1137 if (ret != LDB_SUCCESS) {
1140 if (ldb_dn_compare(nc_root, req->op.rename.olddn) == 0) {
1141 talloc_free(nc_root);
1142 DEBUG(10,("acl:renaming/moving a NC\n"));
1143 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1144 return ldb_module_done(req, NULL, NULL,
1145 LDB_ERR_UNWILLING_TO_PERFORM);
1147 talloc_free(nc_root);
1149 /* Look for the parent */
1151 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res,
1152 req->op.rename.olddn, acl_attrs,
1153 DSDB_FLAG_NEXT_MODULE |
1154 DSDB_SEARCH_SHOW_RECYCLED);
1155 /* we sould be able to find the parent */
1156 if (ret != LDB_SUCCESS) {
1157 DEBUG(10,("acl: failed to find object %s\n",
1158 ldb_dn_get_linearized(req->op.rename.olddn)));
1159 talloc_free(tmp_ctx);
1163 schema = dsdb_get_schema(ldb, acl_res);
1165 talloc_free(tmp_ctx);
1166 return ldb_operr(ldb);
1169 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
1170 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1171 &root, &new_node)) {
1172 talloc_free(tmp_ctx);
1173 return ldb_operr(ldb);
1176 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1178 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1179 &new_node, &new_node)) {
1180 talloc_free(tmp_ctx);
1181 return ldb_operr(ldb);
1184 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1185 if (rdn_name == NULL) {
1186 talloc_free(tmp_ctx);
1187 return ldb_operr(ldb);
1189 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1191 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1192 &new_node, &new_node)) {
1193 talloc_free(tmp_ctx);
1194 return ldb_operr(ldb);
1197 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1199 if (ret != LDB_SUCCESS) {
1200 talloc_free(tmp_ctx);
1201 return ldb_operr(ldb);
1203 /* Theoretically we pass the check if the object has no sd */
1205 talloc_free(tmp_ctx);
1208 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1209 status = sec_access_check_ds(sd, acl_user_token(module),
1215 if (!NT_STATUS_IS_OK(status)) {
1216 DEBUG(10, ("Object %s has no wp on name\n",
1217 ldb_dn_get_linearized(req->op.rename.olddn)));
1219 acl_user_token(module),
1220 req->op.rename.olddn,
1223 talloc_free(tmp_ctx);
1224 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1227 if (ldb_dn_compare(oldparent, newparent) == 0) {
1228 /* regular rename, not move, nothing more to do */
1229 talloc_free(tmp_ctx);
1230 return ldb_next_request(module, req);
1233 /* new parent should have create child */
1236 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
1238 DEBUG(10,("acl:renamed object has no object class\n"));
1239 talloc_free(tmp_ctx);
1240 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
1243 ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid);
1244 if (ret != LDB_SUCCESS) {
1245 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1246 talloc_free(tmp_ctx);
1249 /* do we have delete object on the object? */
1251 status = sec_access_check_ds(sd, acl_user_token(module),
1257 if (NT_STATUS_IS_OK(status)) {
1258 talloc_free(tmp_ctx);
1259 return ldb_next_request(module, req);
1261 /* what about delete child on the current parent */
1262 ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL);
1263 if (ret != LDB_SUCCESS) {
1264 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1265 talloc_free(tmp_ctx);
1266 return ldb_module_done(req, NULL, NULL, ret);
1269 talloc_free(tmp_ctx);
1271 return ldb_next_request(module, req);
1274 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1276 struct ldb_context *ldb;
1277 struct acl_context *ac;
1278 struct acl_private *data;
1279 struct ldb_result *acl_res;
1280 static const char *acl_attrs[] = {
1282 "nTSecurityDescriptor",
1289 ac = talloc_get_type(req->context, struct acl_context);
1290 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1291 ldb = ldb_module_get_ctx(ac->module);
1294 return ldb_module_done(ac->req, NULL, NULL,
1295 LDB_ERR_OPERATIONS_ERROR);
1297 if (ares->error != LDB_SUCCESS) {
1298 return ldb_module_done(ac->req, ares->controls,
1299 ares->response, ares->error);
1302 switch (ares->type) {
1303 case LDB_REPLY_ENTRY:
1304 if (ac->allowedAttributes
1305 || ac->allowedChildClasses
1306 || ac->allowedChildClassesEffective
1307 || ac->allowedAttributesEffective
1308 || ac->sDRightsEffective) {
1309 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1311 DSDB_FLAG_NEXT_MODULE);
1312 if (ret != LDB_SUCCESS) {
1313 return ldb_module_done(ac->req, NULL, NULL, ret);
1315 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1316 ret = acl_allowedAttributes(ac->module, ac->schema, acl_res->msgs[0], ares->message, ac);
1317 if (ret != LDB_SUCCESS) {
1318 return ldb_module_done(ac->req, NULL, NULL, ret);
1321 if (ac->allowedChildClasses) {
1322 ret = acl_childClasses(ac->module, ac->schema, acl_res->msgs[0],
1323 ares->message, "allowedChildClasses");
1324 if (ret != LDB_SUCCESS) {
1325 return ldb_module_done(ac->req, NULL, NULL, ret);
1328 if (ac->allowedChildClassesEffective) {
1329 ret = acl_childClassesEffective(ac->module, ac->schema,
1330 acl_res->msgs[0], ares->message, ac);
1331 if (ret != LDB_SUCCESS) {
1332 return ldb_module_done(ac->req, NULL, NULL, ret);
1335 if (ac->sDRightsEffective) {
1336 ret = acl_sDRightsEffective(ac->module,
1337 acl_res->msgs[0], ares->message, ac);
1338 if (ret != LDB_SUCCESS) {
1339 return ldb_module_done(ac->req, NULL, NULL, ret);
1343 if (data && data->password_attrs) {
1344 if (!ac->am_system) {
1345 for (i = 0; data->password_attrs[i]; i++) {
1346 if ((!ac->userPassword) &&
1347 (ldb_attr_cmp(data->password_attrs[i],
1348 "userPassword") == 0))
1351 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1355 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1357 case LDB_REPLY_REFERRAL:
1358 return ldb_module_send_referral(ac->req, ares->referral);
1360 case LDB_REPLY_DONE:
1361 return ldb_module_done(ac->req, ares->controls,
1362 ares->response, LDB_SUCCESS);
1368 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1370 struct ldb_context *ldb;
1371 struct acl_context *ac;
1372 struct ldb_request *down_req;
1373 struct acl_private *data;
1377 ldb = ldb_module_get_ctx(module);
1379 ac = talloc_zero(req, struct acl_context);
1381 return ldb_oom(ldb);
1383 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1385 ac->module = module;
1387 ac->am_system = dsdb_module_am_system(module);
1388 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1389 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1390 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1391 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1392 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1393 ac->userPassword = dsdb_user_password_support(module, ac);
1394 ac->schema = dsdb_get_schema(ldb, ac);
1396 /* replace any attributes in the parse tree that are private,
1397 so we don't allow a search for 'userPassword=penguin',
1398 just as we would not allow that attribute to be returned */
1399 if (ac->am_system) {
1400 /* FIXME: We should copy the tree and keep the original unmodified. */
1401 /* remove password attributes */
1402 if (data && data->password_attrs) {
1403 for (i = 0; data->password_attrs[i]; i++) {
1404 if ((!ac->userPassword) &&
1405 (ldb_attr_cmp(data->password_attrs[i],
1406 "userPassword") == 0))
1409 ldb_parse_tree_attr_replace(req->op.search.tree,
1410 data->password_attrs[i],
1411 "kludgeACLredactedattribute");
1415 ret = ldb_build_search_req_ex(&down_req,
1417 req->op.search.base,
1418 req->op.search.scope,
1419 req->op.search.tree,
1420 req->op.search.attrs,
1422 ac, acl_search_callback,
1424 LDB_REQ_SET_LOCATION(down_req);
1425 if (ret != LDB_SUCCESS) {
1428 /* perform the search */
1429 return ldb_next_request(module, down_req);
1432 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1434 struct ldb_context *ldb = ldb_module_get_ctx(module);
1435 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1437 /* allow everybody to read the sequence number */
1438 if (strcmp(req->op.extended.oid,
1439 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1440 return ldb_next_request(module, req);
1443 if (dsdb_module_am_system(module) ||
1444 dsdb_module_am_administrator(module) || as_system) {
1445 return ldb_next_request(module, req);
1447 ldb_asprintf_errstring(ldb,
1449 "attempted database modify not permitted. "
1450 "User %s is not SYSTEM or an administrator",
1451 acl_user_name(req, module));
1452 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1456 static const struct ldb_module_ops ldb_acl_module_ops = {
1458 .search = acl_search,
1460 .modify = acl_modify,
1462 .rename = acl_rename,
1463 .extended = acl_extended,
1464 .init_context = acl_module_init
1467 int ldb_acl_module_init(const char *version)
1469 LDB_MODULE_CHECK_VERSION(version);
1470 return ldb_register_module(&ldb_acl_module_ops);