s4-dsdb: Implementation of access checks on a undelete operation
[Samba.git] / source4 / dsdb / samdb / ldb_modules / acl.c
blob78e646159c4eaf7eb6e46c96a18a3a3b7881dda7
1 /*
2 ldb database library
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/>.
23 * Name: ldb
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
34 #include "includes.h"
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"
43 #include "system/kerberos.h"
44 #include "auth/kerberos/kerberos.h"
46 struct extended_access_check_attribute {
47 const char *oa_name;
48 const uint32_t requires_rights;
51 struct acl_private {
52 bool acl_search;
53 const char **password_attrs;
54 void *cached_schema_ptr;
55 uint64_t cached_schema_metadata_usn;
56 uint64_t cached_schema_loaded_usn;
57 const char **confidential_attrs;
60 struct acl_context {
61 struct ldb_module *module;
62 struct ldb_request *req;
63 bool am_system;
64 bool am_administrator;
65 bool modify_search;
66 bool constructed_attrs;
67 bool allowedAttributes;
68 bool allowedAttributesEffective;
69 bool allowedChildClasses;
70 bool allowedChildClassesEffective;
71 bool sDRightsEffective;
72 bool userPassword;
73 const char * const *attrs;
74 struct dsdb_schema *schema;
77 static int acl_module_init(struct ldb_module *module)
79 struct ldb_context *ldb;
80 struct acl_private *data;
81 int ret;
82 unsigned int i, n, j;
83 TALLOC_CTX *mem_ctx;
84 static const char * const attrs[] = { "passwordAttribute", NULL };
85 static const char * const secret_attrs[] = {
86 DSDB_SECRET_ATTRIBUTES
88 struct ldb_result *res;
89 struct ldb_message *msg;
90 struct ldb_message_element *password_attributes;
92 ldb = ldb_module_get_ctx(module);
94 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
95 if (ret != LDB_SUCCESS) {
96 ldb_debug(ldb, LDB_DEBUG_ERROR,
97 "acl_module_init: Unable to register control with rootdse!\n");
98 return ldb_operr(ldb);
101 data = talloc_zero(module, struct acl_private);
102 if (data == NULL) {
103 return ldb_oom(ldb);
106 data->acl_search = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"),
107 NULL, "acl", "search", true);
108 ldb_module_set_private(module, data);
110 mem_ctx = talloc_new(module);
111 if (!mem_ctx) {
112 return ldb_oom(ldb);
115 ret = dsdb_module_search_dn(module, mem_ctx, &res,
116 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
117 attrs,
118 DSDB_FLAG_NEXT_MODULE |
119 DSDB_FLAG_AS_SYSTEM,
120 NULL);
121 if (ret != LDB_SUCCESS) {
122 goto done;
124 if (res->count == 0) {
125 goto done;
128 if (res->count > 1) {
129 talloc_free(mem_ctx);
130 return LDB_ERR_CONSTRAINT_VIOLATION;
133 msg = res->msgs[0];
135 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
136 if (!password_attributes) {
137 goto done;
139 data->password_attrs = talloc_array(data, const char *,
140 password_attributes->num_values +
141 ARRAY_SIZE(secret_attrs) + 1);
142 if (!data->password_attrs) {
143 talloc_free(mem_ctx);
144 return ldb_oom(ldb);
147 n = 0;
148 for (i=0; i < password_attributes->num_values; i++) {
149 data->password_attrs[n] = (const char *)password_attributes->values[i].data;
150 talloc_steal(data->password_attrs, password_attributes->values[i].data);
151 n++;
154 for (i=0; i < ARRAY_SIZE(secret_attrs); i++) {
155 bool found = false;
157 for (j=0; j < n; j++) {
158 if (strcasecmp(data->password_attrs[j], secret_attrs[i]) == 0) {
159 found = true;
160 break;
164 if (found) {
165 continue;
168 data->password_attrs[n] = talloc_strdup(data->password_attrs,
169 secret_attrs[i]);
170 if (data->password_attrs[n] == NULL) {
171 talloc_free(mem_ctx);
172 return ldb_oom(ldb);
174 n++;
176 data->password_attrs[n] = NULL;
178 done:
179 talloc_free(mem_ctx);
180 return ldb_next_init(module);
183 static int acl_allowedAttributes(struct ldb_module *module,
184 const struct dsdb_schema *schema,
185 struct ldb_message *sd_msg,
186 struct ldb_message *msg,
187 struct acl_context *ac)
189 struct ldb_message_element *oc_el;
190 struct ldb_context *ldb = ldb_module_get_ctx(module);
191 TALLOC_CTX *mem_ctx;
192 const char **attr_list;
193 int i, ret;
194 const struct dsdb_class *objectclass;
196 /* If we don't have a schema yet, we can't do anything... */
197 if (schema == NULL) {
198 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
199 return LDB_ERR_OPERATIONS_ERROR;
202 /* Must remove any existing attribute */
203 if (ac->allowedAttributes) {
204 ldb_msg_remove_attr(msg, "allowedAttributes");
207 mem_ctx = talloc_new(msg);
208 if (!mem_ctx) {
209 return ldb_oom(ldb);
212 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
213 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
214 if (!attr_list) {
215 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
216 talloc_free(mem_ctx);
217 return LDB_ERR_OPERATIONS_ERROR;
221 * Get the top-most structural object class for the ACL check
223 objectclass = dsdb_get_last_structural_class(ac->schema,
224 oc_el);
225 if (objectclass == NULL) {
226 ldb_asprintf_errstring(ldb, "acl_read: Failed to find a structural class for %s",
227 ldb_dn_get_linearized(sd_msg->dn));
228 talloc_free(mem_ctx);
229 return LDB_ERR_OPERATIONS_ERROR;
232 if (ac->allowedAttributes) {
233 for (i=0; attr_list && attr_list[i]; i++) {
234 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
237 if (ac->allowedAttributesEffective) {
238 struct security_descriptor *sd;
239 struct dom_sid *sid = NULL;
240 struct ldb_control *as_system = ldb_request_get_control(ac->req,
241 LDB_CONTROL_AS_SYSTEM_OID);
243 if (as_system != NULL) {
244 as_system->critical = 0;
247 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
248 if (ac->am_system || as_system) {
249 for (i=0; attr_list && attr_list[i]; i++) {
250 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
252 return LDB_SUCCESS;
255 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), mem_ctx, sd_msg, &sd);
257 if (ret != LDB_SUCCESS) {
258 return ret;
261 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
262 for (i=0; attr_list && attr_list[i]; i++) {
263 const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
264 attr_list[i]);
265 if (!attr) {
266 return ldb_operr(ldb);
268 /* remove constructed attributes */
269 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
270 || attr->systemOnly
271 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
272 continue;
274 ret = acl_check_access_on_attribute(module,
275 msg,
277 sid,
278 SEC_ADS_WRITE_PROP,
279 attr,
280 objectclass);
281 if (ret == LDB_SUCCESS) {
282 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
286 return LDB_SUCCESS;
289 static int acl_childClasses(struct ldb_module *module,
290 const struct dsdb_schema *schema,
291 struct ldb_message *sd_msg,
292 struct ldb_message *msg,
293 const char *attrName)
295 struct ldb_message_element *oc_el;
296 struct ldb_message_element *allowedClasses;
297 const struct dsdb_class *sclass;
298 unsigned int i, j;
299 int ret;
301 /* If we don't have a schema yet, we can't do anything... */
302 if (schema == NULL) {
303 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
304 return LDB_ERR_OPERATIONS_ERROR;
307 /* Must remove any existing attribute, or else confusion reins */
308 ldb_msg_remove_attr(msg, attrName);
309 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
310 if (ret != LDB_SUCCESS) {
311 return ret;
314 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
316 for (i=0; oc_el && i < oc_el->num_values; i++) {
317 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
318 if (!sclass) {
319 /* We don't know this class? what is going on? */
320 continue;
323 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
324 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
327 if (allowedClasses->num_values > 1) {
328 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
329 for (i=1 ; i < allowedClasses->num_values; i++) {
330 struct ldb_val *val1 = &allowedClasses->values[i-1];
331 struct ldb_val *val2 = &allowedClasses->values[i];
332 if (data_blob_cmp(val1, val2) == 0) {
333 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
334 allowedClasses->num_values--;
335 i--;
340 return LDB_SUCCESS;
343 static int acl_childClassesEffective(struct ldb_module *module,
344 const struct dsdb_schema *schema,
345 struct ldb_message *sd_msg,
346 struct ldb_message *msg,
347 struct acl_context *ac)
349 struct ldb_message_element *oc_el;
350 struct ldb_message_element *allowedClasses = NULL;
351 const struct dsdb_class *sclass;
352 struct security_descriptor *sd;
353 struct ldb_control *as_system = ldb_request_get_control(ac->req,
354 LDB_CONTROL_AS_SYSTEM_OID);
355 struct dom_sid *sid = NULL;
356 unsigned int i, j;
357 int ret;
359 if (as_system != NULL) {
360 as_system->critical = 0;
363 if (ac->am_system || as_system) {
364 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
367 /* If we don't have a schema yet, we can't do anything... */
368 if (schema == NULL) {
369 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
370 return LDB_ERR_OPERATIONS_ERROR;
373 /* Must remove any existing attribute, or else confusion reins */
374 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
376 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
377 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
378 if (ret != LDB_SUCCESS) {
379 return ret;
382 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
383 for (i=0; oc_el && i < oc_el->num_values; i++) {
384 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
385 if (!sclass) {
386 /* We don't know this class? what is going on? */
387 continue;
390 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
391 const struct dsdb_class *sc;
393 sc = dsdb_class_by_lDAPDisplayName(schema,
394 sclass->possibleInferiors[j]);
395 if (!sc) {
396 /* We don't know this class? what is going on? */
397 continue;
400 ret = acl_check_access_on_objectclass(module, ac,
401 sd, sid,
402 SEC_ADS_CREATE_CHILD,
403 sc);
404 if (ret == LDB_SUCCESS) {
405 ldb_msg_add_string(msg, "allowedChildClassesEffective",
406 sclass->possibleInferiors[j]);
410 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
411 if (!allowedClasses) {
412 return LDB_SUCCESS;
415 if (allowedClasses->num_values > 1) {
416 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
417 for (i=1 ; i < allowedClasses->num_values; i++) {
418 struct ldb_val *val1 = &allowedClasses->values[i-1];
419 struct ldb_val *val2 = &allowedClasses->values[i];
420 if (data_blob_cmp(val1, val2) == 0) {
421 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
422 allowedClasses->num_values--;
423 i--;
427 return LDB_SUCCESS;
430 static int acl_sDRightsEffective(struct ldb_module *module,
431 struct ldb_message *sd_msg,
432 struct ldb_message *msg,
433 struct acl_context *ac)
435 struct ldb_context *ldb = ldb_module_get_ctx(module);
436 struct ldb_message_element *rightsEffective;
437 int ret;
438 struct security_descriptor *sd;
439 struct ldb_control *as_system = ldb_request_get_control(ac->req,
440 LDB_CONTROL_AS_SYSTEM_OID);
441 struct dom_sid *sid = NULL;
442 uint32_t flags = 0;
444 if (as_system != NULL) {
445 as_system->critical = 0;
448 /* Must remove any existing attribute, or else confusion reins */
449 ldb_msg_remove_attr(msg, "sDRightsEffective");
450 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
451 if (ret != LDB_SUCCESS) {
452 return ret;
454 if (ac->am_system || as_system) {
455 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
456 } else {
457 const struct dsdb_class *objectclass;
458 const struct dsdb_attribute *attr;
460 objectclass = dsdb_get_structural_oc_from_msg(ac->schema, sd_msg);
461 if (objectclass == NULL) {
462 return ldb_operr(ldb);
465 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
466 "nTSecurityDescriptor");
467 if (attr == NULL) {
468 return ldb_operr(ldb);
471 /* Get the security descriptor from the message */
472 ret = dsdb_get_sd_from_ldb_message(ldb, msg, sd_msg, &sd);
473 if (ret != LDB_SUCCESS) {
474 return ret;
476 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
477 ret = acl_check_access_on_attribute(module,
478 msg,
480 sid,
481 SEC_STD_WRITE_OWNER,
482 attr,
483 objectclass);
484 if (ret == LDB_SUCCESS) {
485 flags |= SECINFO_OWNER | SECINFO_GROUP;
487 ret = acl_check_access_on_attribute(module,
488 msg,
490 sid,
491 SEC_STD_WRITE_DAC,
492 attr,
493 objectclass);
494 if (ret == LDB_SUCCESS) {
495 flags |= SECINFO_DACL;
497 ret = acl_check_access_on_attribute(module,
498 msg,
500 sid,
501 SEC_FLAG_SYSTEM_SECURITY,
502 attr,
503 objectclass);
504 if (ret == LDB_SUCCESS) {
505 flags |= SECINFO_SACL;
508 return samdb_msg_add_uint(ldb_module_get_ctx(module), msg, msg,
509 "sDRightsEffective", flags);
512 static int acl_validate_spn_value(TALLOC_CTX *mem_ctx,
513 struct ldb_context *ldb,
514 const char *spn_value,
515 uint32_t userAccountControl,
516 const char *samAccountName,
517 const char *dnsHostName,
518 const char *netbios_name,
519 const char *ntds_guid)
521 int ret;
522 krb5_context krb_ctx;
523 krb5_error_code kerr;
524 krb5_principal principal;
525 char *instanceName;
526 char *serviceType;
527 char *serviceName;
528 const char *forest_name = samdb_forest_name(ldb, mem_ctx);
529 const char *base_domain = samdb_default_domain_name(ldb, mem_ctx);
530 struct loadparm_context *lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
531 struct loadparm_context);
532 bool is_dc = (userAccountControl & UF_SERVER_TRUST_ACCOUNT) ||
533 (userAccountControl & UF_PARTIAL_SECRETS_ACCOUNT);
535 if (strcasecmp_m(spn_value, samAccountName) == 0) {
536 /* MacOS X sets this value, and setting an SPN of your
537 * own samAccountName is both pointless and safe */
538 return LDB_SUCCESS;
541 kerr = smb_krb5_init_context_basic(mem_ctx,
542 lp_ctx,
543 &krb_ctx);
544 if (kerr != 0) {
545 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
546 "Could not initialize kerberos context.");
549 ret = krb5_parse_name(krb_ctx, spn_value, &principal);
550 if (ret) {
551 krb5_free_context(krb_ctx);
552 return LDB_ERR_CONSTRAINT_VIOLATION;
555 if (krb5_princ_size(krb_ctx, principal) < 2) {
556 goto fail;
559 instanceName = smb_krb5_principal_get_comp_string(mem_ctx, krb_ctx,
560 principal, 1);
561 serviceType = smb_krb5_principal_get_comp_string(mem_ctx, krb_ctx,
562 principal, 0);
563 if (krb5_princ_size(krb_ctx, principal) == 3) {
564 serviceName = smb_krb5_principal_get_comp_string(mem_ctx, krb_ctx,
565 principal, 2);
566 } else {
567 serviceName = NULL;
570 if (serviceName) {
571 if (!is_dc) {
572 goto fail;
574 if (strcasecmp(serviceType, "ldap") == 0) {
575 if (strcasecmp(serviceName, netbios_name) != 0 &&
576 strcasecmp(serviceName, forest_name) != 0) {
577 goto fail;
580 } else if (strcasecmp(serviceType, "gc") == 0) {
581 if (strcasecmp(serviceName, forest_name) != 0) {
582 goto fail;
584 } else {
585 if (strcasecmp(serviceName, base_domain) != 0 &&
586 strcasecmp(serviceName, netbios_name) != 0) {
587 goto fail;
591 /* instanceName can be samAccountName without $ or dnsHostName
592 * or "ntds_guid._msdcs.forest_domain for DC objects */
593 if (strlen(instanceName) == (strlen(samAccountName) - 1)
594 && strncasecmp(instanceName, samAccountName, strlen(samAccountName) - 1) == 0) {
595 goto success;
596 } else if (dnsHostName != NULL && strcasecmp(instanceName, dnsHostName) == 0) {
597 goto success;
598 } else if (is_dc) {
599 const char *guid_str;
600 guid_str = talloc_asprintf(mem_ctx,"%s._msdcs.%s",
601 ntds_guid,
602 forest_name);
603 if (strcasecmp(instanceName, guid_str) == 0) {
604 goto success;
608 fail:
609 krb5_free_principal(krb_ctx, principal);
610 krb5_free_context(krb_ctx);
611 return LDB_ERR_CONSTRAINT_VIOLATION;
613 success:
614 krb5_free_principal(krb_ctx, principal);
615 krb5_free_context(krb_ctx);
616 return LDB_SUCCESS;
619 static int acl_check_spn(TALLOC_CTX *mem_ctx,
620 struct ldb_module *module,
621 struct ldb_request *req,
622 struct security_descriptor *sd,
623 struct dom_sid *sid,
624 const struct dsdb_attribute *attr,
625 const struct dsdb_class *objectclass)
627 int ret;
628 unsigned int i;
629 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
630 struct ldb_context *ldb = ldb_module_get_ctx(module);
631 struct ldb_result *acl_res;
632 struct ldb_result *netbios_res;
633 struct ldb_message_element *el;
634 struct ldb_dn *partitions_dn = samdb_partitions_dn(ldb, tmp_ctx);
635 uint32_t userAccountControl;
636 const char *samAccountName;
637 const char *dnsHostName;
638 const char *netbios_name;
639 struct GUID ntds;
640 char *ntds_guid = NULL;
642 static const char *acl_attrs[] = {
643 "samAccountName",
644 "dnsHostName",
645 "userAccountControl",
646 NULL
648 static const char *netbios_attrs[] = {
649 "nETBIOSName",
650 NULL
653 /* if we have wp, we can do whatever we like */
654 if (acl_check_access_on_attribute(module,
655 tmp_ctx,
657 sid,
658 SEC_ADS_WRITE_PROP,
659 attr, objectclass) == LDB_SUCCESS) {
660 talloc_free(tmp_ctx);
661 return LDB_SUCCESS;
664 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
665 GUID_DRS_VALIDATE_SPN,
666 SEC_ADS_SELF_WRITE,
667 sid);
669 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
670 dsdb_acl_debug(sd, acl_user_token(module),
671 req->op.mod.message->dn,
672 true,
673 10);
674 talloc_free(tmp_ctx);
675 return ret;
678 ret = dsdb_module_search_dn(module, tmp_ctx,
679 &acl_res, req->op.mod.message->dn,
680 acl_attrs,
681 DSDB_FLAG_NEXT_MODULE |
682 DSDB_FLAG_AS_SYSTEM |
683 DSDB_SEARCH_SHOW_RECYCLED,
684 req);
685 if (ret != LDB_SUCCESS) {
686 talloc_free(tmp_ctx);
687 return ret;
690 userAccountControl = ldb_msg_find_attr_as_uint(acl_res->msgs[0], "userAccountControl", 0);
691 dnsHostName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "dnsHostName", NULL);
692 samAccountName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "samAccountName", NULL);
694 ret = dsdb_module_search(module, tmp_ctx,
695 &netbios_res, partitions_dn,
696 LDB_SCOPE_ONELEVEL,
697 netbios_attrs,
698 DSDB_FLAG_NEXT_MODULE |
699 DSDB_FLAG_AS_SYSTEM,
700 req,
701 "(ncName=%s)",
702 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)));
704 netbios_name = ldb_msg_find_attr_as_string(netbios_res->msgs[0], "nETBIOSName", NULL);
706 el = ldb_msg_find_element(req->op.mod.message, "servicePrincipalName");
707 if (!el) {
708 talloc_free(tmp_ctx);
709 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
710 "Error finding element for servicePrincipalName.");
713 /* NTDSDSA objectGuid of object we are checking SPN for */
714 if (userAccountControl & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
715 ret = dsdb_module_find_ntdsguid_for_computer(module, tmp_ctx,
716 req->op.mod.message->dn, &ntds, req);
717 if (ret != LDB_SUCCESS) {
718 ldb_asprintf_errstring(ldb, "Failed to find NTDSDSA objectGuid for %s: %s",
719 ldb_dn_get_linearized(req->op.mod.message->dn),
720 ldb_strerror(ret));
721 talloc_free(tmp_ctx);
722 return LDB_ERR_OPERATIONS_ERROR;
724 ntds_guid = GUID_string(tmp_ctx, &ntds);
727 for (i=0; i < el->num_values; i++) {
728 ret = acl_validate_spn_value(tmp_ctx,
729 ldb,
730 (char *)el->values[i].data,
731 userAccountControl,
732 samAccountName,
733 dnsHostName,
734 netbios_name,
735 ntds_guid);
736 if (ret != LDB_SUCCESS) {
737 talloc_free(tmp_ctx);
738 return ret;
741 talloc_free(tmp_ctx);
742 return LDB_SUCCESS;
745 static int acl_add(struct ldb_module *module, struct ldb_request *req)
747 int ret;
748 struct ldb_dn *parent;
749 struct ldb_context *ldb;
750 const struct dsdb_schema *schema;
751 const struct dsdb_class *objectclass;
752 struct ldb_control *as_system;
753 struct ldb_message_element *el;
754 unsigned int instanceType = 0;
756 if (ldb_dn_is_special(req->op.add.message->dn)) {
757 return ldb_next_request(module, req);
760 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
761 if (as_system != NULL) {
762 as_system->critical = 0;
765 if (dsdb_module_am_system(module) || as_system) {
766 return ldb_next_request(module, req);
769 ldb = ldb_module_get_ctx(module);
771 parent = ldb_dn_get_parent(req, req->op.add.message->dn);
772 if (parent == NULL) {
773 return ldb_oom(ldb);
776 schema = dsdb_get_schema(ldb, req);
777 if (!schema) {
778 return ldb_operr(ldb);
781 objectclass = dsdb_get_structural_oc_from_msg(schema, req->op.add.message);
782 if (!objectclass) {
783 ldb_asprintf_errstring(ldb_module_get_ctx(module),
784 "acl: unable to find or validate structural objectClass on %s\n",
785 ldb_dn_get_linearized(req->op.add.message->dn));
786 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
789 el = ldb_msg_find_element(req->op.add.message, "instanceType");
790 if ((el != NULL) && (el->num_values != 1)) {
791 ldb_set_errstring(ldb, "acl: the 'instanceType' attribute is single-valued!");
792 return LDB_ERR_UNWILLING_TO_PERFORM;
795 instanceType = ldb_msg_find_attr_as_uint(req->op.add.message,
796 "instanceType", 0);
797 if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
798 static const char *no_attrs[] = { NULL };
799 struct ldb_result *partition_res;
800 struct ldb_dn *partitions_dn;
802 partitions_dn = samdb_partitions_dn(ldb, req);
803 if (!partitions_dn) {
804 ldb_set_errstring(ldb, "acl: CN=partitions dn could not be generated!");
805 return LDB_ERR_UNWILLING_TO_PERFORM;
808 ret = dsdb_module_search(module, req, &partition_res,
809 partitions_dn, LDB_SCOPE_ONELEVEL,
810 no_attrs,
811 DSDB_FLAG_NEXT_MODULE |
812 DSDB_FLAG_AS_SYSTEM |
813 DSDB_SEARCH_ONE_ONLY |
814 DSDB_SEARCH_SHOW_RECYCLED,
815 req,
816 "(&(nCName=%s)(objectClass=crossRef))",
817 ldb_dn_get_linearized(req->op.add.message->dn));
819 if (ret == LDB_SUCCESS) {
820 /* Check that we can write to the crossRef object MS-ADTS 3.1.1.5.2.8.2 */
821 ret = dsdb_module_check_access_on_dn(module, req, partition_res->msgs[0]->dn,
822 SEC_ADS_WRITE_PROP,
823 &objectclass->schemaIDGUID, req);
824 if (ret != LDB_SUCCESS) {
825 ldb_asprintf_errstring(ldb_module_get_ctx(module),
826 "acl: ACL check failed on crossRef object %s: %s\n",
827 ldb_dn_get_linearized(partition_res->msgs[0]->dn),
828 ldb_errstring(ldb));
829 return ret;
833 * TODO: Remaining checks, like if we are
834 * the naming master etc need to be handled
835 * in the instanceType module
837 return ldb_next_request(module, req);
840 /* Check that we can create a crossRef object MS-ADTS 3.1.1.5.2.8.2 */
841 ret = dsdb_module_check_access_on_dn(module, req, partitions_dn,
842 SEC_ADS_CREATE_CHILD,
843 &objectclass->schemaIDGUID, req);
844 if (ret == LDB_ERR_NO_SUCH_OBJECT &&
845 ldb_request_get_control(req, LDB_CONTROL_RELAX_OID))
847 /* Allow provision bootstrap */
848 ret = LDB_SUCCESS;
850 if (ret != LDB_SUCCESS) {
851 ldb_asprintf_errstring(ldb_module_get_ctx(module),
852 "acl: ACL check failed on CN=Partitions crossRef container %s: %s\n",
853 ldb_dn_get_linearized(partitions_dn), ldb_errstring(ldb));
854 return ret;
858 * TODO: Remaining checks, like if we are the naming
859 * master and adding the crossRef object need to be
860 * handled in the instanceType module
862 return ldb_next_request(module, req);
865 ret = dsdb_module_check_access_on_dn(module, req, parent,
866 SEC_ADS_CREATE_CHILD,
867 &objectclass->schemaIDGUID, req);
868 if (ret != LDB_SUCCESS) {
869 ldb_asprintf_errstring(ldb_module_get_ctx(module),
870 "acl: unable to find or validate structural objectClass on %s\n",
871 ldb_dn_get_linearized(req->op.add.message->dn));
872 return ret;
874 return ldb_next_request(module, req);
877 /* ckecks if modifications are allowed on "Member" attribute */
878 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
879 struct ldb_module *module,
880 struct ldb_request *req,
881 struct security_descriptor *sd,
882 struct dom_sid *sid,
883 const struct dsdb_attribute *attr,
884 const struct dsdb_class *objectclass)
886 int ret;
887 unsigned int i;
888 struct ldb_context *ldb = ldb_module_get_ctx(module);
889 struct ldb_dn *user_dn;
890 struct ldb_message_element *member_el;
891 /* if we have wp, we can do whatever we like */
892 if (acl_check_access_on_attribute(module,
893 mem_ctx,
895 sid,
896 SEC_ADS_WRITE_PROP,
897 attr, objectclass) == LDB_SUCCESS) {
898 return LDB_SUCCESS;
900 /* if we are adding/deleting ourselves, check for self membership */
901 ret = dsdb_find_dn_by_sid(ldb, mem_ctx,
902 &acl_user_token(module)->sids[PRIMARY_USER_SID_INDEX],
903 &user_dn);
904 if (ret != LDB_SUCCESS) {
905 return ret;
907 member_el = ldb_msg_find_element(req->op.mod.message, "member");
908 if (!member_el) {
909 return ldb_operr(ldb);
911 /* user can only remove oneself */
912 if (member_el->num_values == 0) {
913 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
915 for (i = 0; i < member_el->num_values; i++) {
916 if (strcasecmp((const char *)member_el->values[i].data,
917 ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
918 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
921 ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
922 GUID_DRS_SELF_MEMBERSHIP,
923 SEC_ADS_SELF_WRITE,
924 sid);
925 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
926 dsdb_acl_debug(sd, acl_user_token(module),
927 req->op.mod.message->dn,
928 true,
929 10);
931 return ret;
934 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
935 struct ldb_module *module,
936 struct ldb_request *req,
937 struct security_descriptor *sd,
938 struct dom_sid *sid,
939 const struct dsdb_class *objectclass,
940 bool userPassword)
942 int ret = LDB_SUCCESS;
943 unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
944 struct ldb_message_element *el;
945 struct ldb_message *msg;
946 const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
947 "unicodePwd", "dBCSPwd", NULL }, **l;
948 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
950 msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
951 if (msg == NULL) {
952 return ldb_module_oom(module);
954 for (l = passwordAttrs; *l != NULL; l++) {
955 if ((!userPassword) && (ldb_attr_cmp(*l, "userPassword") == 0)) {
956 continue;
959 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
960 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
961 ++del_attr_cnt;
963 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
964 ++add_attr_cnt;
966 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
967 ++rep_attr_cnt;
969 ldb_msg_remove_element(msg, el);
973 /* single deletes will be handled by the "password_hash" LDB module
974 * later in the stack, so we let it though here */
975 if ((del_attr_cnt > 0) && (add_attr_cnt == 0) && (rep_attr_cnt == 0)) {
976 talloc_free(tmp_ctx);
977 return LDB_SUCCESS;
980 if (ldb_request_get_control(req,
981 DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
982 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
983 * have a user password change and not a set as the message
984 * looks like. In it's value blob it contains the NT and/or LM
985 * hash of the old password specified by the user.
986 * This control is used by the SAMR and "kpasswd" password
987 * change mechanisms. */
988 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
989 GUID_DRS_USER_CHANGE_PASSWORD,
990 SEC_ADS_CONTROL_ACCESS,
991 sid);
993 else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
994 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
995 GUID_DRS_FORCE_CHANGE_PASSWORD,
996 SEC_ADS_CONTROL_ACCESS,
997 sid);
999 else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
1000 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
1001 GUID_DRS_USER_CHANGE_PASSWORD,
1002 SEC_ADS_CONTROL_ACCESS,
1003 sid);
1004 /* Very strange, but we get constraint violation in this case */
1005 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
1006 ret = LDB_ERR_CONSTRAINT_VIOLATION;
1009 if (ret != LDB_SUCCESS) {
1010 dsdb_acl_debug(sd, acl_user_token(module),
1011 req->op.mod.message->dn,
1012 true,
1013 10);
1015 talloc_free(tmp_ctx);
1016 return ret;
1020 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
1022 int ret;
1023 struct ldb_context *ldb = ldb_module_get_ctx(module);
1024 const struct dsdb_schema *schema;
1025 unsigned int i;
1026 const struct dsdb_class *objectclass;
1027 struct ldb_result *acl_res;
1028 struct security_descriptor *sd;
1029 struct dom_sid *sid = NULL;
1030 struct ldb_control *as_system;
1031 struct ldb_control *is_undelete;
1032 bool userPassword;
1033 TALLOC_CTX *tmp_ctx;
1034 const struct ldb_message *msg = req->op.mod.message;
1035 static const char *acl_attrs[] = {
1036 "nTSecurityDescriptor",
1037 "objectClass",
1038 "objectSid",
1039 NULL
1042 if (ldb_dn_is_special(msg->dn)) {
1043 return ldb_next_request(module, req);
1046 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1047 if (as_system != NULL) {
1048 as_system->critical = 0;
1051 is_undelete = ldb_request_get_control(req, DSDB_CONTROL_RESTORE_TOMBSTONE_OID);
1053 /* Don't print this debug statement if elements[0].name is going to be NULL */
1054 if (msg->num_elements > 0) {
1055 DEBUG(10, ("ldb:acl_modify: %s\n", msg->elements[0].name));
1057 if (dsdb_module_am_system(module) || as_system) {
1058 return ldb_next_request(module, req);
1061 tmp_ctx = talloc_new(req);
1062 if (tmp_ctx == NULL) {
1063 return ldb_oom(ldb);
1066 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, msg->dn,
1067 acl_attrs,
1068 DSDB_FLAG_NEXT_MODULE |
1069 DSDB_FLAG_AS_SYSTEM |
1070 DSDB_SEARCH_SHOW_RECYCLED,
1071 req);
1073 if (ret != LDB_SUCCESS) {
1074 goto fail;
1077 userPassword = dsdb_user_password_support(module, req, req);
1079 schema = dsdb_get_schema(ldb, tmp_ctx);
1080 if (!schema) {
1081 talloc_free(tmp_ctx);
1082 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1083 "acl_modify: Error obtaining schema.");
1086 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
1087 if (ret != LDB_SUCCESS) {
1088 talloc_free(tmp_ctx);
1089 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1090 "acl_modify: Error retrieving security descriptor.");
1092 /* Theoretically we pass the check if the object has no sd */
1093 if (!sd) {
1094 goto success;
1097 objectclass = dsdb_get_structural_oc_from_msg(schema, acl_res->msgs[0]);
1098 if (!objectclass) {
1099 talloc_free(tmp_ctx);
1100 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1101 "acl_modify: Error retrieving object class for GUID.");
1103 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1104 for (i=0; i < msg->num_elements; i++) {
1105 const struct ldb_message_element *el = &msg->elements[i];
1106 const struct dsdb_attribute *attr;
1109 * This basic attribute existence check with the right errorcode
1110 * is needed since this module is the first one which requests
1111 * schema attribute information.
1112 * The complete attribute checking is done in the
1113 * "objectclass_attrs" module behind this one.
1115 * NOTE: "clearTextPassword" is not defined in the schema.
1117 attr = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
1118 if (!attr && ldb_attr_cmp("clearTextPassword", el->name) != 0) {
1119 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' "
1120 "on entry '%s' was not found in the schema!",
1121 req->op.mod.message->elements[i].name,
1122 ldb_dn_get_linearized(req->op.mod.message->dn));
1123 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
1124 goto fail;
1127 if (ldb_attr_cmp("nTSecurityDescriptor", el->name) == 0) {
1128 uint32_t sd_flags = dsdb_request_sd_flags(req, NULL);
1129 uint32_t access_mask = 0;
1131 if (sd_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
1132 access_mask |= SEC_STD_WRITE_OWNER;
1134 if (sd_flags & SECINFO_DACL) {
1135 access_mask |= SEC_STD_WRITE_DAC;
1137 if (sd_flags & SECINFO_SACL) {
1138 access_mask |= SEC_FLAG_SYSTEM_SECURITY;
1141 ret = acl_check_access_on_attribute(module,
1142 tmp_ctx,
1144 sid,
1145 access_mask,
1146 attr,
1147 objectclass);
1148 if (ret != LDB_SUCCESS) {
1149 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1150 "Object %s has no write dacl access\n",
1151 ldb_dn_get_linearized(msg->dn));
1152 dsdb_acl_debug(sd,
1153 acl_user_token(module),
1154 msg->dn,
1155 true,
1156 10);
1157 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1158 goto fail;
1160 } else if (ldb_attr_cmp("member", el->name) == 0) {
1161 ret = acl_check_self_membership(tmp_ctx,
1162 module,
1163 req,
1165 sid,
1166 attr,
1167 objectclass);
1168 if (ret != LDB_SUCCESS) {
1169 goto fail;
1171 } else if (ldb_attr_cmp("dBCSPwd", el->name) == 0) {
1172 /* this one is not affected by any rights, we should let it through
1173 so that passwords_hash returns the correct error */
1174 continue;
1175 } else if (ldb_attr_cmp("unicodePwd", el->name) == 0 ||
1176 (userPassword && ldb_attr_cmp("userPassword", el->name) == 0) ||
1177 ldb_attr_cmp("clearTextPassword", el->name) == 0) {
1178 ret = acl_check_password_rights(tmp_ctx,
1179 module,
1180 req,
1182 sid,
1183 objectclass,
1184 userPassword);
1185 if (ret != LDB_SUCCESS) {
1186 goto fail;
1188 } else if (ldb_attr_cmp("servicePrincipalName", el->name) == 0) {
1189 ret = acl_check_spn(tmp_ctx,
1190 module,
1191 req,
1193 sid,
1194 attr,
1195 objectclass);
1196 if (ret != LDB_SUCCESS) {
1197 goto fail;
1199 } else if (is_undelete != NULL && (ldb_attr_cmp("isDeleted", el->name) == 0)) {
1201 * in case of undelete op permissions on
1202 * isDeleted are irrelevant and
1203 * distinguishedName is removed by the
1204 * tombstone_reanimate module
1206 continue;
1207 } else {
1208 ret = acl_check_access_on_attribute(module,
1209 tmp_ctx,
1211 sid,
1212 SEC_ADS_WRITE_PROP,
1213 attr,
1214 objectclass);
1215 if (ret != LDB_SUCCESS) {
1216 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1217 "Object %s has no write property access\n",
1218 ldb_dn_get_linearized(msg->dn));
1219 dsdb_acl_debug(sd,
1220 acl_user_token(module),
1221 msg->dn,
1222 true,
1223 10);
1224 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1225 goto fail;
1230 success:
1231 talloc_free(tmp_ctx);
1232 return ldb_next_request(module, req);
1233 fail:
1234 talloc_free(tmp_ctx);
1235 return ret;
1238 /* similar to the modify for the time being.
1239 * We need to consider the special delete tree case, though - TODO */
1240 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
1242 int ret;
1243 struct ldb_dn *parent;
1244 struct ldb_context *ldb;
1245 struct ldb_dn *nc_root;
1246 struct ldb_control *as_system;
1247 const struct dsdb_schema *schema;
1248 const struct dsdb_class *objectclass;
1249 struct security_descriptor *sd = NULL;
1250 struct dom_sid *sid = NULL;
1251 struct ldb_result *acl_res;
1252 static const char *acl_attrs[] = {
1253 "nTSecurityDescriptor",
1254 "objectClass",
1255 "objectSid",
1256 NULL
1259 if (ldb_dn_is_special(req->op.del.dn)) {
1260 return ldb_next_request(module, req);
1263 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1264 if (as_system != NULL) {
1265 as_system->critical = 0;
1268 if (dsdb_module_am_system(module) || as_system) {
1269 return ldb_next_request(module, req);
1272 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
1274 ldb = ldb_module_get_ctx(module);
1276 parent = ldb_dn_get_parent(req, req->op.del.dn);
1277 if (parent == NULL) {
1278 return ldb_oom(ldb);
1281 /* Make sure we aren't deleting a NC */
1283 ret = dsdb_find_nc_root(ldb, req, req->op.del.dn, &nc_root);
1284 if (ret != LDB_SUCCESS) {
1285 return ret;
1287 if (ldb_dn_compare(nc_root, req->op.del.dn) == 0) {
1288 talloc_free(nc_root);
1289 DEBUG(10,("acl:deleting a NC\n"));
1290 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1291 return ldb_module_done(req, NULL, NULL,
1292 LDB_ERR_UNWILLING_TO_PERFORM);
1294 talloc_free(nc_root);
1296 ret = dsdb_module_search_dn(module, req, &acl_res,
1297 req->op.del.dn, acl_attrs,
1298 DSDB_FLAG_NEXT_MODULE |
1299 DSDB_FLAG_AS_SYSTEM |
1300 DSDB_SEARCH_SHOW_RECYCLED, req);
1301 /* we sould be able to find the parent */
1302 if (ret != LDB_SUCCESS) {
1303 DEBUG(10,("acl: failed to find object %s\n",
1304 ldb_dn_get_linearized(req->op.rename.olddn)));
1305 return ret;
1308 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1309 if (ret != LDB_SUCCESS) {
1310 return ldb_operr(ldb);
1312 if (!sd) {
1313 return ldb_operr(ldb);
1316 schema = dsdb_get_schema(ldb, req);
1317 if (!schema) {
1318 return ldb_operr(ldb);
1321 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1323 objectclass = dsdb_get_structural_oc_from_msg(schema, acl_res->msgs[0]);
1324 if (!objectclass) {
1325 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1326 "acl_modify: Error retrieving object class for GUID.");
1329 if (ldb_request_get_control(req, LDB_CONTROL_TREE_DELETE_OID)) {
1330 ret = acl_check_access_on_objectclass(module, req, sd, sid,
1331 SEC_ADS_DELETE_TREE,
1332 objectclass);
1333 if (ret != LDB_SUCCESS) {
1334 return ret;
1337 return ldb_next_request(module, req);
1340 /* First check if we have delete object right */
1341 ret = acl_check_access_on_objectclass(module, req, sd, sid,
1342 SEC_STD_DELETE,
1343 objectclass);
1344 if (ret == LDB_SUCCESS) {
1345 return ldb_next_request(module, req);
1348 /* Nope, we don't have delete object. Lets check if we have delete
1349 * child on the parent */
1350 ret = dsdb_module_check_access_on_dn(module, req, parent,
1351 SEC_ADS_DELETE_CHILD,
1352 &objectclass->schemaIDGUID,
1353 req);
1354 if (ret != LDB_SUCCESS) {
1355 return ret;
1358 return ldb_next_request(module, req);
1360 static int acl_check_reanimate_tombstone(TALLOC_CTX *mem_ctx,
1361 struct ldb_module *module,
1362 struct ldb_request *req,
1363 struct ldb_dn *nc_root)
1365 int ret;
1366 struct ldb_result *acl_res;
1367 struct security_descriptor *sd = NULL;
1368 struct dom_sid *sid = NULL;
1369 static const char *acl_attrs[] = {
1370 "nTSecurityDescriptor",
1371 "objectClass",
1372 "objectSid",
1373 NULL
1376 ret = dsdb_module_search_dn(module, mem_ctx, &acl_res,
1377 nc_root, acl_attrs,
1378 DSDB_FLAG_NEXT_MODULE |
1379 DSDB_FLAG_AS_SYSTEM |
1380 DSDB_SEARCH_SHOW_RECYCLED, req);
1381 if (ret != LDB_SUCCESS) {
1382 DEBUG(10,("acl: failed to find object %s\n",
1383 ldb_dn_get_linearized(nc_root)));
1384 return ret;
1387 ret = dsdb_get_sd_from_ldb_message(mem_ctx, req, acl_res->msgs[0], &sd);
1388 sid = samdb_result_dom_sid(mem_ctx, acl_res->msgs[0], "objectSid");
1389 if (ret != LDB_SUCCESS || !sd) {
1390 return ldb_operr(ldb_module_get_ctx(module));
1392 return acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
1393 GUID_DRS_REANIMATE_TOMBSTONE,
1394 SEC_ADS_CONTROL_ACCESS, sid);
1397 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
1399 int ret;
1400 struct ldb_dn *oldparent;
1401 struct ldb_dn *newparent;
1402 const struct dsdb_schema *schema;
1403 const struct dsdb_class *objectclass;
1404 const struct dsdb_attribute *attr = NULL;
1405 struct ldb_context *ldb;
1406 struct security_descriptor *sd = NULL;
1407 struct dom_sid *sid = NULL;
1408 struct ldb_result *acl_res;
1409 struct ldb_dn *nc_root;
1410 struct ldb_control *as_system;
1411 struct ldb_control *is_undelete;
1412 TALLOC_CTX *tmp_ctx;
1413 const char *rdn_name;
1414 static const char *acl_attrs[] = {
1415 "nTSecurityDescriptor",
1416 "objectClass",
1417 "objectSid",
1418 NULL
1421 if (ldb_dn_is_special(req->op.rename.olddn)) {
1422 return ldb_next_request(module, req);
1425 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1426 if (as_system != NULL) {
1427 as_system->critical = 0;
1430 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
1431 if (dsdb_module_am_system(module) || as_system) {
1432 return ldb_next_request(module, req);
1435 ldb = ldb_module_get_ctx(module);
1437 tmp_ctx = talloc_new(req);
1438 if (tmp_ctx == NULL) {
1439 return ldb_oom(ldb);
1442 oldparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.olddn);
1443 if (oldparent == NULL) {
1444 return ldb_oom(ldb);
1446 newparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.newdn);
1447 if (newparent == NULL) {
1448 return ldb_oom(ldb);
1451 /* Make sure we aren't renaming/moving a NC */
1453 ret = dsdb_find_nc_root(ldb, req, req->op.rename.olddn, &nc_root);
1454 if (ret != LDB_SUCCESS) {
1455 return ret;
1457 if (ldb_dn_compare(nc_root, req->op.rename.olddn) == 0) {
1458 talloc_free(nc_root);
1459 DEBUG(10,("acl:renaming/moving a NC\n"));
1460 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1461 return ldb_module_done(req, NULL, NULL,
1462 LDB_ERR_UNWILLING_TO_PERFORM);
1465 /* special check for undelete operation */
1466 is_undelete = ldb_request_get_control(req, DSDB_CONTROL_RESTORE_TOMBSTONE_OID);
1467 if (is_undelete != NULL) {
1468 is_undelete->critical = 0;
1469 ret = acl_check_reanimate_tombstone(tmp_ctx, module, req, nc_root);
1470 if (ret != LDB_SUCCESS) {
1471 talloc_free(tmp_ctx);
1472 return ret;
1475 talloc_free(nc_root);
1477 /* Look for the parent */
1479 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res,
1480 req->op.rename.olddn, acl_attrs,
1481 DSDB_FLAG_NEXT_MODULE |
1482 DSDB_FLAG_AS_SYSTEM |
1483 DSDB_SEARCH_SHOW_RECYCLED, req);
1484 /* we sould be able to find the parent */
1485 if (ret != LDB_SUCCESS) {
1486 DEBUG(10,("acl: failed to find object %s\n",
1487 ldb_dn_get_linearized(req->op.rename.olddn)));
1488 talloc_free(tmp_ctx);
1489 return ret;
1492 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1493 if (ret != LDB_SUCCESS) {
1494 talloc_free(tmp_ctx);
1495 return ldb_operr(ldb);
1497 if (!sd) {
1498 talloc_free(tmp_ctx);
1499 return ldb_operr(ldb);
1502 schema = dsdb_get_schema(ldb, acl_res);
1503 if (!schema) {
1504 talloc_free(tmp_ctx);
1505 return ldb_operr(ldb);
1508 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1510 objectclass = dsdb_get_structural_oc_from_msg(schema, acl_res->msgs[0]);
1511 if (!objectclass) {
1512 talloc_free(tmp_ctx);
1513 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1514 "acl_modify: Error retrieving object class for GUID.");
1517 attr = dsdb_attribute_by_lDAPDisplayName(schema, "name");
1518 if (attr == NULL) {
1519 talloc_free(tmp_ctx);
1520 return ldb_operr(ldb);
1523 ret = acl_check_access_on_attribute(module, tmp_ctx, sd, sid,
1524 SEC_ADS_WRITE_PROP,
1525 attr, objectclass);
1526 if (ret != LDB_SUCCESS) {
1527 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1528 "Object %s has no wp on %s\n",
1529 ldb_dn_get_linearized(req->op.rename.olddn),
1530 attr->lDAPDisplayName);
1531 dsdb_acl_debug(sd,
1532 acl_user_token(module),
1533 req->op.rename.olddn,
1534 true,
1535 10);
1536 talloc_free(tmp_ctx);
1537 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1540 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1541 if (rdn_name == NULL) {
1542 talloc_free(tmp_ctx);
1543 return ldb_operr(ldb);
1546 attr = dsdb_attribute_by_lDAPDisplayName(schema, rdn_name);
1547 if (attr == NULL) {
1548 talloc_free(tmp_ctx);
1549 return ldb_operr(ldb);
1552 ret = acl_check_access_on_attribute(module, tmp_ctx, sd, sid,
1553 SEC_ADS_WRITE_PROP,
1554 attr, objectclass);
1555 if (ret != LDB_SUCCESS) {
1556 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1557 "Object %s has no wp on %s\n",
1558 ldb_dn_get_linearized(req->op.rename.olddn),
1559 attr->lDAPDisplayName);
1560 dsdb_acl_debug(sd,
1561 acl_user_token(module),
1562 req->op.rename.olddn,
1563 true,
1564 10);
1565 talloc_free(tmp_ctx);
1566 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1569 if (ldb_dn_compare(oldparent, newparent) == 0) {
1570 /* regular rename, not move, nothing more to do */
1571 talloc_free(tmp_ctx);
1572 return ldb_next_request(module, req);
1575 /* new parent should have create child */
1576 ret = dsdb_module_check_access_on_dn(module, req, newparent,
1577 SEC_ADS_CREATE_CHILD,
1578 &objectclass->schemaIDGUID, req);
1579 if (ret != LDB_SUCCESS) {
1580 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1581 "acl:access_denied renaming %s",
1582 ldb_dn_get_linearized(req->op.rename.olddn));
1583 talloc_free(tmp_ctx);
1584 return ret;
1587 /* do we have delete object on the object? */
1588 /* this access is not necessary for undelete ops */
1589 if (is_undelete == NULL) {
1590 ret = acl_check_access_on_objectclass(module, tmp_ctx, sd, sid,
1591 SEC_STD_DELETE,
1592 objectclass);
1593 if (ret == LDB_SUCCESS) {
1594 talloc_free(tmp_ctx);
1595 return ldb_next_request(module, req);
1597 /* what about delete child on the current parent */
1598 ret = dsdb_module_check_access_on_dn(module, req, oldparent,
1599 SEC_ADS_DELETE_CHILD,
1600 &objectclass->schemaIDGUID,
1601 req);
1602 if (ret != LDB_SUCCESS) {
1603 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1604 "acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn));
1605 talloc_free(tmp_ctx);
1606 return ldb_module_done(req, NULL, NULL, ret);
1609 talloc_free(tmp_ctx);
1611 return ldb_next_request(module, req);
1614 static int acl_search_update_confidential_attrs(struct acl_context *ac,
1615 struct acl_private *data)
1617 struct dsdb_attribute *a;
1618 uint32_t n = 0;
1620 if (data->acl_search) {
1622 * If acl:search is activated, the acl_read module
1623 * protects confidential attributes.
1625 return LDB_SUCCESS;
1628 if ((ac->schema == data->cached_schema_ptr) &&
1629 (ac->schema->loaded_usn == data->cached_schema_loaded_usn) &&
1630 (ac->schema->metadata_usn == data->cached_schema_metadata_usn))
1632 return LDB_SUCCESS;
1635 data->cached_schema_ptr = NULL;
1636 data->cached_schema_loaded_usn = 0;
1637 data->cached_schema_metadata_usn = 0;
1638 TALLOC_FREE(data->confidential_attrs);
1640 if (ac->schema == NULL) {
1641 return LDB_SUCCESS;
1644 for (a = ac->schema->attributes; a; a = a->next) {
1645 const char **attrs = data->confidential_attrs;
1647 if (!(a->searchFlags & SEARCH_FLAG_CONFIDENTIAL)) {
1648 continue;
1651 attrs = talloc_realloc(data, attrs, const char *, n + 2);
1652 if (attrs == NULL) {
1653 TALLOC_FREE(data->confidential_attrs);
1654 return ldb_module_oom(ac->module);
1657 attrs[n] = a->lDAPDisplayName;
1658 attrs[n+1] = NULL;
1659 n++;
1661 data->confidential_attrs = attrs;
1664 data->cached_schema_ptr = ac->schema;
1665 data->cached_schema_loaded_usn = ac->schema->loaded_usn;
1666 data->cached_schema_metadata_usn = ac->schema->metadata_usn;
1668 return LDB_SUCCESS;
1671 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1673 struct acl_context *ac;
1674 struct acl_private *data;
1675 struct ldb_result *acl_res;
1676 static const char *acl_attrs[] = {
1677 "objectClass",
1678 "nTSecurityDescriptor",
1679 "objectSid",
1680 NULL
1682 int ret;
1683 unsigned int i;
1685 ac = talloc_get_type(req->context, struct acl_context);
1686 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1687 if (!ares) {
1688 return ldb_module_done(ac->req, NULL, NULL,
1689 LDB_ERR_OPERATIONS_ERROR);
1691 if (ares->error != LDB_SUCCESS) {
1692 return ldb_module_done(ac->req, ares->controls,
1693 ares->response, ares->error);
1696 switch (ares->type) {
1697 case LDB_REPLY_ENTRY:
1698 if (ac->constructed_attrs) {
1699 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1700 acl_attrs,
1701 DSDB_FLAG_NEXT_MODULE |
1702 DSDB_FLAG_AS_SYSTEM |
1703 DSDB_SEARCH_SHOW_RECYCLED,
1704 req);
1705 if (ret != LDB_SUCCESS) {
1706 return ldb_module_done(ac->req, NULL, NULL, ret);
1710 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1711 ret = acl_allowedAttributes(ac->module, ac->schema,
1712 acl_res->msgs[0],
1713 ares->message, ac);
1714 if (ret != LDB_SUCCESS) {
1715 return ldb_module_done(ac->req, NULL, NULL, ret);
1719 if (ac->allowedChildClasses) {
1720 ret = acl_childClasses(ac->module, ac->schema,
1721 acl_res->msgs[0],
1722 ares->message,
1723 "allowedChildClasses");
1724 if (ret != LDB_SUCCESS) {
1725 return ldb_module_done(ac->req, NULL, NULL, ret);
1729 if (ac->allowedChildClassesEffective) {
1730 ret = acl_childClassesEffective(ac->module, ac->schema,
1731 acl_res->msgs[0],
1732 ares->message, ac);
1733 if (ret != LDB_SUCCESS) {
1734 return ldb_module_done(ac->req, NULL, NULL, ret);
1738 if (ac->sDRightsEffective) {
1739 ret = acl_sDRightsEffective(ac->module,
1740 acl_res->msgs[0],
1741 ares->message, ac);
1742 if (ret != LDB_SUCCESS) {
1743 return ldb_module_done(ac->req, NULL, NULL, ret);
1747 if (data == NULL) {
1748 return ldb_module_send_entry(ac->req, ares->message,
1749 ares->controls);
1752 if (ac->am_system) {
1753 return ldb_module_send_entry(ac->req, ares->message,
1754 ares->controls);
1757 if (data->password_attrs != NULL) {
1758 for (i = 0; data->password_attrs[i]; i++) {
1759 if ((!ac->userPassword) &&
1760 (ldb_attr_cmp(data->password_attrs[i],
1761 "userPassword") == 0))
1763 continue;
1766 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1770 if (ac->am_administrator) {
1771 return ldb_module_send_entry(ac->req, ares->message,
1772 ares->controls);
1775 ret = acl_search_update_confidential_attrs(ac, data);
1776 if (ret != LDB_SUCCESS) {
1777 return ret;
1780 if (data->confidential_attrs != NULL) {
1781 for (i = 0; data->confidential_attrs[i]; i++) {
1782 ldb_msg_remove_attr(ares->message,
1783 data->confidential_attrs[i]);
1787 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1789 case LDB_REPLY_REFERRAL:
1790 return ldb_module_send_referral(ac->req, ares->referral);
1792 case LDB_REPLY_DONE:
1793 return ldb_module_done(ac->req, ares->controls,
1794 ares->response, LDB_SUCCESS);
1797 return LDB_SUCCESS;
1800 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1802 struct ldb_context *ldb;
1803 struct acl_context *ac;
1804 struct ldb_parse_tree *down_tree;
1805 struct ldb_request *down_req;
1806 struct acl_private *data;
1807 int ret;
1808 unsigned int i;
1810 if (ldb_dn_is_special(req->op.search.base)) {
1811 return ldb_next_request(module, req);
1814 ldb = ldb_module_get_ctx(module);
1816 ac = talloc_zero(req, struct acl_context);
1817 if (ac == NULL) {
1818 return ldb_oom(ldb);
1820 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1822 ac->module = module;
1823 ac->req = req;
1824 ac->am_system = dsdb_module_am_system(module);
1825 ac->am_administrator = dsdb_module_am_administrator(module);
1826 ac->constructed_attrs = false;
1827 ac->modify_search = true;
1828 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1829 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1830 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1831 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1832 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1833 ac->userPassword = true;
1834 ac->schema = dsdb_get_schema(ldb, ac);
1836 ac->constructed_attrs |= ac->allowedAttributes;
1837 ac->constructed_attrs |= ac->allowedChildClasses;
1838 ac->constructed_attrs |= ac->allowedChildClassesEffective;
1839 ac->constructed_attrs |= ac->allowedAttributesEffective;
1840 ac->constructed_attrs |= ac->sDRightsEffective;
1842 if (data == NULL) {
1843 ac->modify_search = false;
1845 if (ac->am_system) {
1846 ac->modify_search = false;
1849 if (!ac->constructed_attrs && !ac->modify_search) {
1850 talloc_free(ac);
1851 return ldb_next_request(module, req);
1854 if (!ac->am_system) {
1855 ac->userPassword = dsdb_user_password_support(module, ac, req);
1858 ret = acl_search_update_confidential_attrs(ac, data);
1859 if (ret != LDB_SUCCESS) {
1860 return ret;
1863 down_tree = ldb_parse_tree_copy_shallow(ac, req->op.search.tree);
1864 if (down_tree == NULL) {
1865 return ldb_oom(ldb);
1868 if (!ac->am_system && data->password_attrs) {
1869 for (i = 0; data->password_attrs[i]; i++) {
1870 if ((!ac->userPassword) &&
1871 (ldb_attr_cmp(data->password_attrs[i],
1872 "userPassword") == 0))
1874 continue;
1877 ldb_parse_tree_attr_replace(down_tree,
1878 data->password_attrs[i],
1879 "kludgeACLredactedattribute");
1883 if (!ac->am_system && !ac->am_administrator && data->confidential_attrs) {
1884 for (i = 0; data->confidential_attrs[i]; i++) {
1885 ldb_parse_tree_attr_replace(down_tree,
1886 data->confidential_attrs[i],
1887 "kludgeACLredactedattribute");
1891 ret = ldb_build_search_req_ex(&down_req,
1892 ldb, ac,
1893 req->op.search.base,
1894 req->op.search.scope,
1895 down_tree,
1896 req->op.search.attrs,
1897 req->controls,
1898 ac, acl_search_callback,
1899 req);
1900 LDB_REQ_SET_LOCATION(down_req);
1901 if (ret != LDB_SUCCESS) {
1902 return ret;
1904 /* perform the search */
1905 return ldb_next_request(module, down_req);
1908 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1910 struct ldb_context *ldb = ldb_module_get_ctx(module);
1911 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1913 /* allow everybody to read the sequence number */
1914 if (strcmp(req->op.extended.oid,
1915 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1916 return ldb_next_request(module, req);
1919 if (dsdb_module_am_system(module) ||
1920 dsdb_module_am_administrator(module) || as_system) {
1921 return ldb_next_request(module, req);
1922 } else {
1923 ldb_asprintf_errstring(ldb,
1924 "acl_extended: "
1925 "attempted database modify not permitted. "
1926 "User %s is not SYSTEM or an administrator",
1927 acl_user_name(req, module));
1928 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1932 static const struct ldb_module_ops ldb_acl_module_ops = {
1933 .name = "acl",
1934 .search = acl_search,
1935 .add = acl_add,
1936 .modify = acl_modify,
1937 .del = acl_delete,
1938 .rename = acl_rename,
1939 .extended = acl_extended,
1940 .init_context = acl_module_init
1943 int ldb_acl_module_init(const char *version)
1945 LDB_MODULE_CHECK_VERSION(version);
1946 return ldb_register_module(&ldb_acl_module_ops);