dsdb-acl: Use dsdb_get_structural_oc_from_msg() in acl_rename()
[Samba.git] / source4 / dsdb / samdb / ldb_modules / acl.c
blob629e0c8b8cadd12958443dfc33cad77500e92840
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;
195 /* If we don't have a schema yet, we can't do anything... */
196 if (schema == NULL) {
197 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
198 return LDB_ERR_OPERATIONS_ERROR;
201 /* Must remove any existing attribute */
202 if (ac->allowedAttributes) {
203 ldb_msg_remove_attr(msg, "allowedAttributes");
206 mem_ctx = talloc_new(msg);
207 if (!mem_ctx) {
208 return ldb_oom(ldb);
211 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
212 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
213 if (!attr_list) {
214 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
215 talloc_free(mem_ctx);
216 return LDB_ERR_OPERATIONS_ERROR;
218 if (ac->allowedAttributes) {
219 for (i=0; attr_list && attr_list[i]; i++) {
220 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
223 if (ac->allowedAttributesEffective) {
224 struct security_descriptor *sd;
225 struct dom_sid *sid = NULL;
226 struct ldb_control *as_system = ldb_request_get_control(ac->req,
227 LDB_CONTROL_AS_SYSTEM_OID);
229 if (as_system != NULL) {
230 as_system->critical = 0;
233 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
234 if (ac->am_system || as_system) {
235 for (i=0; attr_list && attr_list[i]; i++) {
236 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
238 return LDB_SUCCESS;
241 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), mem_ctx, sd_msg, &sd);
243 if (ret != LDB_SUCCESS) {
244 return ret;
247 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
248 for (i=0; attr_list && attr_list[i]; i++) {
249 const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
250 attr_list[i]);
251 if (!attr) {
252 return ldb_operr(ldb);
254 /* remove constructed attributes */
255 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
256 || attr->systemOnly
257 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
258 continue;
260 ret = acl_check_access_on_attribute(module,
261 msg,
263 sid,
264 SEC_ADS_WRITE_PROP,
265 attr);
266 if (ret == LDB_SUCCESS) {
267 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
271 return LDB_SUCCESS;
274 static int acl_childClasses(struct ldb_module *module,
275 const struct dsdb_schema *schema,
276 struct ldb_message *sd_msg,
277 struct ldb_message *msg,
278 const char *attrName)
280 struct ldb_message_element *oc_el;
281 struct ldb_message_element *allowedClasses;
282 const struct dsdb_class *sclass;
283 unsigned int i, j;
284 int ret;
286 /* If we don't have a schema yet, we can't do anything... */
287 if (schema == NULL) {
288 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
289 return LDB_ERR_OPERATIONS_ERROR;
292 /* Must remove any existing attribute, or else confusion reins */
293 ldb_msg_remove_attr(msg, attrName);
294 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
295 if (ret != LDB_SUCCESS) {
296 return ret;
299 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
301 for (i=0; oc_el && i < oc_el->num_values; i++) {
302 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
303 if (!sclass) {
304 /* We don't know this class? what is going on? */
305 continue;
308 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
309 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
312 if (allowedClasses->num_values > 1) {
313 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
314 for (i=1 ; i < allowedClasses->num_values; i++) {
315 struct ldb_val *val1 = &allowedClasses->values[i-1];
316 struct ldb_val *val2 = &allowedClasses->values[i];
317 if (data_blob_cmp(val1, val2) == 0) {
318 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
319 allowedClasses->num_values--;
320 i--;
325 return LDB_SUCCESS;
328 static int acl_check_access_on_class(struct ldb_module *module,
329 const struct dsdb_schema *schema,
330 TALLOC_CTX *mem_ctx,
331 struct security_descriptor *sd,
332 struct security_token *token,
333 struct dom_sid *rp_sid,
334 uint32_t access_mask,
335 const char *class_name)
337 int ret;
338 NTSTATUS status;
339 uint32_t access_granted;
340 struct object_tree *root = NULL;
341 struct object_tree *new_node = NULL;
342 const struct GUID *guid;
344 if (class_name != NULL) {
345 guid = class_schemaid_guid_by_lDAPDisplayName(schema, class_name);
346 if (!guid) {
347 DEBUG(10, ("acl_search: cannot find class %s\n",
348 class_name));
349 goto fail;
351 if (!insert_in_object_tree(mem_ctx,
352 guid, access_mask,
353 &root, &new_node)) {
354 DEBUG(10, ("acl_search: cannot add to object tree guid\n"));
355 goto fail;
359 status = sec_access_check_ds(sd, token,
360 access_mask,
361 &access_granted,
362 root,
363 rp_sid);
364 if (!NT_STATUS_IS_OK(status)) {
365 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
366 } else {
367 ret = LDB_SUCCESS;
369 return ret;
370 fail:
371 return ldb_operr(ldb_module_get_ctx(module));
374 static int acl_childClassesEffective(struct ldb_module *module,
375 const struct dsdb_schema *schema,
376 struct ldb_message *sd_msg,
377 struct ldb_message *msg,
378 struct acl_context *ac)
380 struct ldb_message_element *oc_el;
381 struct ldb_message_element *allowedClasses = NULL;
382 const struct dsdb_class *sclass;
383 struct security_descriptor *sd;
384 struct ldb_control *as_system = ldb_request_get_control(ac->req,
385 LDB_CONTROL_AS_SYSTEM_OID);
386 struct dom_sid *sid = NULL;
387 unsigned int i, j;
388 int ret;
390 if (as_system != NULL) {
391 as_system->critical = 0;
394 if (ac->am_system || as_system) {
395 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
398 /* If we don't have a schema yet, we can't do anything... */
399 if (schema == NULL) {
400 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
401 return LDB_ERR_OPERATIONS_ERROR;
404 /* Must remove any existing attribute, or else confusion reins */
405 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
407 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
408 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
409 if (ret != LDB_SUCCESS) {
410 return ret;
413 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
414 for (i=0; oc_el && i < oc_el->num_values; i++) {
415 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
416 if (!sclass) {
417 /* We don't know this class? what is going on? */
418 continue;
421 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
422 ret = acl_check_access_on_class(module,
423 schema,
424 msg,
426 acl_user_token(module),
427 sid,
428 SEC_ADS_CREATE_CHILD,
429 sclass->possibleInferiors[j]);
430 if (ret == LDB_SUCCESS) {
431 ldb_msg_add_string(msg, "allowedChildClassesEffective",
432 sclass->possibleInferiors[j]);
436 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
437 if (!allowedClasses) {
438 return LDB_SUCCESS;
441 if (allowedClasses->num_values > 1) {
442 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
443 for (i=1 ; i < allowedClasses->num_values; i++) {
444 struct ldb_val *val1 = &allowedClasses->values[i-1];
445 struct ldb_val *val2 = &allowedClasses->values[i];
446 if (data_blob_cmp(val1, val2) == 0) {
447 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
448 allowedClasses->num_values--;
449 i--;
453 return LDB_SUCCESS;
456 static int acl_sDRightsEffective(struct ldb_module *module,
457 struct ldb_message *sd_msg,
458 struct ldb_message *msg,
459 struct acl_context *ac)
461 struct ldb_context *ldb = ldb_module_get_ctx(module);
462 struct ldb_message_element *rightsEffective;
463 int ret;
464 struct security_descriptor *sd;
465 struct ldb_control *as_system = ldb_request_get_control(ac->req,
466 LDB_CONTROL_AS_SYSTEM_OID);
467 struct dom_sid *sid = NULL;
468 uint32_t flags = 0;
470 if (as_system != NULL) {
471 as_system->critical = 0;
474 /* Must remove any existing attribute, or else confusion reins */
475 ldb_msg_remove_attr(msg, "sDRightsEffective");
476 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
477 if (ret != LDB_SUCCESS) {
478 return ret;
480 if (ac->am_system || as_system) {
481 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
483 else {
484 const struct dsdb_attribute *attr;
486 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
487 "nTSecurityDescriptor");
488 if (attr == NULL) {
489 return ldb_operr(ldb);
492 /* Get the security descriptor from the message */
493 ret = dsdb_get_sd_from_ldb_message(ldb, msg, sd_msg, &sd);
494 if (ret != LDB_SUCCESS) {
495 return ret;
497 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
498 ret = acl_check_access_on_attribute(module,
499 msg,
501 sid,
502 SEC_STD_WRITE_OWNER,
503 attr);
504 if (ret == LDB_SUCCESS) {
505 flags |= SECINFO_OWNER | SECINFO_GROUP;
507 ret = acl_check_access_on_attribute(module,
508 msg,
510 sid,
511 SEC_STD_WRITE_DAC,
512 attr);
513 if (ret == LDB_SUCCESS) {
514 flags |= SECINFO_DACL;
516 ret = acl_check_access_on_attribute(module,
517 msg,
519 sid,
520 SEC_FLAG_SYSTEM_SECURITY,
521 attr);
522 if (ret == LDB_SUCCESS) {
523 flags |= SECINFO_SACL;
526 return samdb_msg_add_uint(ldb_module_get_ctx(module), msg, msg,
527 "sDRightsEffective", flags);
530 static int acl_validate_spn_value(TALLOC_CTX *mem_ctx,
531 struct ldb_context *ldb,
532 const char *spn_value,
533 uint32_t userAccountControl,
534 const char *samAccountName,
535 const char *dnsHostName,
536 const char *netbios_name,
537 const char *ntds_guid)
539 int ret;
540 krb5_context krb_ctx;
541 krb5_error_code kerr;
542 krb5_principal principal;
543 char *instanceName;
544 char *serviceType;
545 char *serviceName;
546 const char *forest_name = samdb_forest_name(ldb, mem_ctx);
547 const char *base_domain = samdb_default_domain_name(ldb, mem_ctx);
548 struct loadparm_context *lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
549 struct loadparm_context);
550 bool is_dc = (userAccountControl & UF_SERVER_TRUST_ACCOUNT) ||
551 (userAccountControl & UF_PARTIAL_SECRETS_ACCOUNT);
553 if (strcasecmp_m(spn_value, samAccountName) == 0) {
554 /* MacOS X sets this value, and setting an SPN of your
555 * own samAccountName is both pointless and safe */
556 return LDB_SUCCESS;
559 kerr = smb_krb5_init_context_basic(mem_ctx,
560 lp_ctx,
561 &krb_ctx);
562 if (kerr != 0) {
563 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
564 "Could not initialize kerberos context.");
567 ret = krb5_parse_name(krb_ctx, spn_value, &principal);
568 if (ret) {
569 krb5_free_context(krb_ctx);
570 return LDB_ERR_CONSTRAINT_VIOLATION;
573 if (principal->name.name_string.len < 2) {
574 goto fail;
577 instanceName = principal->name.name_string.val[1];
578 serviceType = principal->name.name_string.val[0];
579 if (principal->name.name_string.len == 3) {
580 serviceName = principal->name.name_string.val[2];
581 } else {
582 serviceName = NULL;
585 if (serviceName) {
586 if (!is_dc) {
587 goto fail;
589 if (strcasecmp(serviceType, "ldap") == 0) {
590 if (strcasecmp(serviceName, netbios_name) != 0 &&
591 strcasecmp(serviceName, forest_name) != 0) {
592 goto fail;
595 } else if (strcasecmp(serviceType, "gc") == 0) {
596 if (strcasecmp(serviceName, forest_name) != 0) {
597 goto fail;
599 } else {
600 if (strcasecmp(serviceName, base_domain) != 0 &&
601 strcasecmp(serviceName, netbios_name) != 0) {
602 goto fail;
606 /* instanceName can be samAccountName without $ or dnsHostName
607 * or "ntds_guid._msdcs.forest_domain for DC objects */
608 if (strlen(instanceName) == (strlen(samAccountName) - 1)
609 && strncasecmp(instanceName, samAccountName, strlen(samAccountName) - 1) == 0) {
610 goto success;
611 } else if (dnsHostName != NULL && strcasecmp(instanceName, dnsHostName) == 0) {
612 goto success;
613 } else if (is_dc) {
614 const char *guid_str;
615 guid_str = talloc_asprintf(mem_ctx,"%s._msdcs.%s",
616 ntds_guid,
617 forest_name);
618 if (strcasecmp(instanceName, guid_str) == 0) {
619 goto success;
623 fail:
624 krb5_free_principal(krb_ctx, principal);
625 krb5_free_context(krb_ctx);
626 return LDB_ERR_CONSTRAINT_VIOLATION;
628 success:
629 krb5_free_principal(krb_ctx, principal);
630 krb5_free_context(krb_ctx);
631 return LDB_SUCCESS;
634 static int acl_check_spn(TALLOC_CTX *mem_ctx,
635 struct ldb_module *module,
636 struct ldb_request *req,
637 struct security_descriptor *sd,
638 struct dom_sid *sid,
639 const struct GUID *oc_guid,
640 const struct dsdb_attribute *attr)
642 int ret;
643 unsigned int i;
644 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
645 struct ldb_context *ldb = ldb_module_get_ctx(module);
646 struct ldb_result *acl_res;
647 struct ldb_result *netbios_res;
648 struct ldb_message_element *el;
649 struct ldb_dn *partitions_dn = samdb_partitions_dn(ldb, tmp_ctx);
650 uint32_t userAccountControl;
651 const char *samAccountName;
652 const char *dnsHostName;
653 const char *netbios_name;
654 struct GUID ntds;
655 char *ntds_guid = NULL;
657 static const char *acl_attrs[] = {
658 "samAccountName",
659 "dnsHostName",
660 "userAccountControl",
661 NULL
663 static const char *netbios_attrs[] = {
664 "nETBIOSName",
665 NULL
668 /* if we have wp, we can do whatever we like */
669 if (acl_check_access_on_attribute(module,
670 tmp_ctx,
672 sid,
673 SEC_ADS_WRITE_PROP,
674 attr) == LDB_SUCCESS) {
675 talloc_free(tmp_ctx);
676 return LDB_SUCCESS;
679 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
680 GUID_DRS_VALIDATE_SPN,
681 SEC_ADS_SELF_WRITE,
682 sid);
684 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
685 dsdb_acl_debug(sd, acl_user_token(module),
686 req->op.mod.message->dn,
687 true,
688 10);
689 talloc_free(tmp_ctx);
690 return ret;
693 ret = dsdb_module_search_dn(module, tmp_ctx,
694 &acl_res, req->op.mod.message->dn,
695 acl_attrs,
696 DSDB_FLAG_NEXT_MODULE |
697 DSDB_FLAG_AS_SYSTEM |
698 DSDB_SEARCH_SHOW_RECYCLED,
699 req);
700 if (ret != LDB_SUCCESS) {
701 talloc_free(tmp_ctx);
702 return ret;
705 userAccountControl = ldb_msg_find_attr_as_uint(acl_res->msgs[0], "userAccountControl", 0);
706 dnsHostName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "dnsHostName", NULL);
707 samAccountName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "samAccountName", NULL);
709 ret = dsdb_module_search(module, tmp_ctx,
710 &netbios_res, partitions_dn,
711 LDB_SCOPE_ONELEVEL,
712 netbios_attrs,
713 DSDB_FLAG_NEXT_MODULE |
714 DSDB_FLAG_AS_SYSTEM,
715 req,
716 "(ncName=%s)",
717 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)));
719 netbios_name = ldb_msg_find_attr_as_string(netbios_res->msgs[0], "nETBIOSName", NULL);
721 el = ldb_msg_find_element(req->op.mod.message, "servicePrincipalName");
722 if (!el) {
723 talloc_free(tmp_ctx);
724 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
725 "Error finding element for servicePrincipalName.");
728 /* NTDSDSA objectGuid of object we are checking SPN for */
729 if (userAccountControl & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
730 ret = dsdb_module_find_ntdsguid_for_computer(module, tmp_ctx,
731 req->op.mod.message->dn, &ntds, req);
732 if (ret != LDB_SUCCESS) {
733 ldb_asprintf_errstring(ldb, "Failed to find NTDSDSA objectGuid for %s: %s",
734 ldb_dn_get_linearized(req->op.mod.message->dn),
735 ldb_strerror(ret));
736 talloc_free(tmp_ctx);
737 return LDB_ERR_OPERATIONS_ERROR;
739 ntds_guid = GUID_string(tmp_ctx, &ntds);
742 for (i=0; i < el->num_values; i++) {
743 ret = acl_validate_spn_value(tmp_ctx,
744 ldb,
745 (char *)el->values[i].data,
746 userAccountControl,
747 samAccountName,
748 dnsHostName,
749 netbios_name,
750 ntds_guid);
751 if (ret != LDB_SUCCESS) {
752 talloc_free(tmp_ctx);
753 return ret;
756 talloc_free(tmp_ctx);
757 return LDB_SUCCESS;
760 static int acl_add(struct ldb_module *module, struct ldb_request *req)
762 int ret;
763 struct ldb_dn *parent;
764 struct ldb_context *ldb;
765 const struct dsdb_schema *schema;
766 struct ldb_message_element *oc_el;
767 const struct GUID *guid;
768 struct ldb_dn *nc_root;
769 struct ldb_control *as_system;
771 if (ldb_dn_is_special(req->op.add.message->dn)) {
772 return ldb_next_request(module, req);
775 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
776 if (as_system != NULL) {
777 as_system->critical = 0;
780 if (dsdb_module_am_system(module) || as_system) {
781 return ldb_next_request(module, req);
784 ldb = ldb_module_get_ctx(module);
786 parent = ldb_dn_get_parent(req, req->op.add.message->dn);
787 if (parent == NULL) {
788 return ldb_oom(ldb);
791 /* Creating an NC. There is probably something we should do here,
792 * but we will establish that later */
794 ret = dsdb_find_nc_root(ldb, req, req->op.add.message->dn, &nc_root);
795 if (ret != LDB_SUCCESS) {
796 return ret;
798 if (ldb_dn_compare(nc_root, req->op.add.message->dn) == 0) {
799 talloc_free(nc_root);
800 return ldb_next_request(module, req);
802 talloc_free(nc_root);
804 schema = dsdb_get_schema(ldb, req);
805 if (!schema) {
806 return ldb_operr(ldb);
809 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
810 if (!oc_el || oc_el->num_values == 0) {
811 ldb_asprintf_errstring(ldb_module_get_ctx(module),
812 "acl: unable to find objectClass on %s\n",
813 ldb_dn_get_linearized(req->op.add.message->dn));
814 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
817 guid = class_schemaid_guid_by_lDAPDisplayName(schema,
818 (char *)oc_el->values[oc_el->num_values-1].data);
819 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid, req);
820 if (ret != LDB_SUCCESS) {
821 return ret;
823 return ldb_next_request(module, req);
826 /* ckecks if modifications are allowed on "Member" attribute */
827 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
828 struct ldb_module *module,
829 struct ldb_request *req,
830 struct security_descriptor *sd,
831 struct dom_sid *sid,
832 const struct GUID *oc_guid,
833 const struct dsdb_attribute *attr)
835 int ret;
836 unsigned int i;
837 struct ldb_context *ldb = ldb_module_get_ctx(module);
838 struct ldb_dn *user_dn;
839 struct ldb_message_element *member_el;
840 /* if we have wp, we can do whatever we like */
841 if (acl_check_access_on_attribute(module,
842 mem_ctx,
844 sid,
845 SEC_ADS_WRITE_PROP,
846 attr) == LDB_SUCCESS) {
847 return LDB_SUCCESS;
849 /* if we are adding/deleting ourselves, check for self membership */
850 ret = dsdb_find_dn_by_sid(ldb, mem_ctx,
851 &acl_user_token(module)->sids[PRIMARY_USER_SID_INDEX],
852 &user_dn);
853 if (ret != LDB_SUCCESS) {
854 return ret;
856 member_el = ldb_msg_find_element(req->op.mod.message, "member");
857 if (!member_el) {
858 return ldb_operr(ldb);
860 /* user can only remove oneself */
861 if (member_el->num_values == 0) {
862 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
864 for (i = 0; i < member_el->num_values; i++) {
865 if (strcasecmp((const char *)member_el->values[i].data,
866 ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
867 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
870 ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
871 GUID_DRS_SELF_MEMBERSHIP,
872 SEC_ADS_SELF_WRITE,
873 sid);
874 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
875 dsdb_acl_debug(sd, acl_user_token(module),
876 req->op.mod.message->dn,
877 true,
878 10);
880 return ret;
883 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
884 struct ldb_module *module,
885 struct ldb_request *req,
886 struct security_descriptor *sd,
887 struct dom_sid *sid,
888 const struct GUID *oc_guid,
889 bool userPassword)
891 int ret = LDB_SUCCESS;
892 unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
893 struct ldb_message_element *el;
894 struct ldb_message *msg;
895 const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
896 "unicodePwd", "dBCSPwd", NULL }, **l;
897 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
899 msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
900 if (msg == NULL) {
901 return ldb_module_oom(module);
903 for (l = passwordAttrs; *l != NULL; l++) {
904 if ((!userPassword) && (ldb_attr_cmp(*l, "userPassword") == 0)) {
905 continue;
908 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
909 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
910 ++del_attr_cnt;
912 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
913 ++add_attr_cnt;
915 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
916 ++rep_attr_cnt;
918 ldb_msg_remove_element(msg, el);
922 /* single deletes will be handled by the "password_hash" LDB module
923 * later in the stack, so we let it though here */
924 if ((del_attr_cnt > 0) && (add_attr_cnt == 0) && (rep_attr_cnt == 0)) {
925 talloc_free(tmp_ctx);
926 return LDB_SUCCESS;
929 if (ldb_request_get_control(req,
930 DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
931 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
932 * have a user password change and not a set as the message
933 * looks like. In it's value blob it contains the NT and/or LM
934 * hash of the old password specified by the user.
935 * This control is used by the SAMR and "kpasswd" password
936 * change mechanisms. */
937 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
938 GUID_DRS_USER_CHANGE_PASSWORD,
939 SEC_ADS_CONTROL_ACCESS,
940 sid);
942 else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
943 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
944 GUID_DRS_FORCE_CHANGE_PASSWORD,
945 SEC_ADS_CONTROL_ACCESS,
946 sid);
948 else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
949 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
950 GUID_DRS_USER_CHANGE_PASSWORD,
951 SEC_ADS_CONTROL_ACCESS,
952 sid);
953 /* Very strange, but we get constraint violation in this case */
954 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
955 ret = LDB_ERR_CONSTRAINT_VIOLATION;
958 if (ret != LDB_SUCCESS) {
959 dsdb_acl_debug(sd, acl_user_token(module),
960 req->op.mod.message->dn,
961 true,
962 10);
964 talloc_free(tmp_ctx);
965 return ret;
968 static const struct GUID *get_oc_guid_from_message(const struct dsdb_schema *schema,
969 struct ldb_message *msg)
971 struct ldb_message_element *oc_el;
972 const struct dsdb_class *object_class;
974 oc_el = ldb_msg_find_element(msg, "objectClass");
975 if (!oc_el) {
976 return NULL;
979 object_class = dsdb_get_last_structural_class(schema, oc_el);
980 if (object_class == NULL) {
981 return NULL;
984 return &object_class->schemaIDGUID;
988 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
990 int ret;
991 struct ldb_context *ldb = ldb_module_get_ctx(module);
992 const struct dsdb_schema *schema;
993 unsigned int i;
994 const struct dsdb_class *objectclass;
995 uint32_t access_granted;
996 NTSTATUS status;
997 struct ldb_result *acl_res;
998 struct security_descriptor *sd;
999 struct dom_sid *sid = NULL;
1000 struct ldb_control *as_system;
1001 bool userPassword;
1002 TALLOC_CTX *tmp_ctx;
1003 const struct ldb_message *msg = req->op.mod.message;
1004 static const char *acl_attrs[] = {
1005 "nTSecurityDescriptor",
1006 "objectClass",
1007 "objectSid",
1008 NULL
1011 if (ldb_dn_is_special(msg->dn)) {
1012 return ldb_next_request(module, req);
1015 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1016 if (as_system != NULL) {
1017 as_system->critical = 0;
1020 /* Don't print this debug statement if elements[0].name is going to be NULL */
1021 if (msg->num_elements > 0) {
1022 DEBUG(10, ("ldb:acl_modify: %s\n", msg->elements[0].name));
1024 if (dsdb_module_am_system(module) || as_system) {
1025 return ldb_next_request(module, req);
1028 tmp_ctx = talloc_new(req);
1029 if (tmp_ctx == NULL) {
1030 return ldb_oom(ldb);
1033 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, msg->dn,
1034 acl_attrs,
1035 DSDB_FLAG_NEXT_MODULE |
1036 DSDB_FLAG_AS_SYSTEM |
1037 DSDB_SEARCH_SHOW_RECYCLED,
1038 req);
1040 if (ret != LDB_SUCCESS) {
1041 goto fail;
1044 userPassword = dsdb_user_password_support(module, req, req);
1046 schema = dsdb_get_schema(ldb, tmp_ctx);
1047 if (!schema) {
1048 talloc_free(tmp_ctx);
1049 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1050 "acl_modify: Error obtaining schema.");
1053 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
1054 if (ret != LDB_SUCCESS) {
1055 talloc_free(tmp_ctx);
1056 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1057 "acl_modify: Error retrieving security descriptor.");
1059 /* Theoretically we pass the check if the object has no sd */
1060 if (!sd) {
1061 goto success;
1064 objectclass = dsdb_get_structural_oc_from_msg(schema, acl_res->msgs[0]);
1065 if (!objectclass) {
1066 talloc_free(tmp_ctx);
1067 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1068 "acl_modify: Error retrieving object class for GUID.");
1070 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1071 for (i=0; i < msg->num_elements; i++) {
1072 const struct ldb_message_element *el = &msg->elements[i];
1073 const struct dsdb_attribute *attr;
1076 * This basic attribute existence check with the right errorcode
1077 * is needed since this module is the first one which requests
1078 * schema attribute information.
1079 * The complete attribute checking is done in the
1080 * "objectclass_attrs" module behind this one.
1082 * NOTE: "clearTextPassword" is not defined in the schema.
1084 attr = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
1085 if (!attr && ldb_attr_cmp("clearTextPassword", el->name) != 0) {
1086 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' "
1087 "on entry '%s' was not found in the schema!",
1088 req->op.mod.message->elements[i].name,
1089 ldb_dn_get_linearized(req->op.mod.message->dn));
1090 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
1091 goto fail;
1094 if (ldb_attr_cmp("nTSecurityDescriptor", el->name) == 0) {
1095 uint32_t sd_flags = dsdb_request_sd_flags(req, NULL);
1096 uint32_t access_mask = 0;
1098 if (sd_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
1099 access_mask |= SEC_STD_WRITE_OWNER;
1101 if (sd_flags & SECINFO_DACL) {
1102 access_mask |= SEC_STD_WRITE_DAC;
1104 if (sd_flags & SECINFO_SACL) {
1105 access_mask |= SEC_FLAG_SYSTEM_SECURITY;
1108 status = sec_access_check_ds(sd, acl_user_token(module),
1109 access_mask,
1110 &access_granted,
1111 NULL,
1112 sid);
1114 if (!NT_STATUS_IS_OK(status)) {
1115 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1116 "Object %s has no write dacl access\n",
1117 ldb_dn_get_linearized(msg->dn));
1118 dsdb_acl_debug(sd,
1119 acl_user_token(module),
1120 msg->dn,
1121 true,
1122 10);
1123 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1124 goto fail;
1126 } else if (ldb_attr_cmp("member", el->name) == 0) {
1127 ret = acl_check_self_membership(tmp_ctx,
1128 module,
1129 req,
1131 sid,
1132 &objectclass->schemaIDGUID,
1133 attr);
1134 if (ret != LDB_SUCCESS) {
1135 goto fail;
1137 } else if (ldb_attr_cmp("dBCSPwd", el->name) == 0) {
1138 /* this one is not affected by any rights, we should let it through
1139 so that passwords_hash returns the correct error */
1140 continue;
1141 } else if (ldb_attr_cmp("unicodePwd", el->name) == 0 ||
1142 (userPassword && ldb_attr_cmp("userPassword", el->name) == 0) ||
1143 ldb_attr_cmp("clearTextPassword", el->name) == 0) {
1144 ret = acl_check_password_rights(tmp_ctx,
1145 module,
1146 req,
1148 sid,
1149 &objectclass->schemaIDGUID,
1150 userPassword);
1151 if (ret != LDB_SUCCESS) {
1152 goto fail;
1154 } else if (ldb_attr_cmp("servicePrincipalName", el->name) == 0) {
1155 ret = acl_check_spn(tmp_ctx,
1156 module,
1157 req,
1159 sid,
1160 &objectclass->schemaIDGUID,
1161 attr);
1162 if (ret != LDB_SUCCESS) {
1163 goto fail;
1165 } else {
1166 struct object_tree *root = NULL;
1167 struct object_tree *new_node = NULL;
1169 if (!insert_in_object_tree(tmp_ctx,
1170 &objectclass->schemaIDGUID,
1171 SEC_ADS_WRITE_PROP,
1172 &root, &new_node)) {
1173 talloc_free(tmp_ctx);
1174 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1175 "acl_modify: Error adding new node in object tree.");
1178 if (!insert_in_object_tree(tmp_ctx,
1179 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
1180 &new_node, &new_node)) {
1181 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1182 "acl_modify: cannot add to object tree securityGUID\n");
1183 ret = LDB_ERR_OPERATIONS_ERROR;
1184 goto fail;
1187 if (!insert_in_object_tree(tmp_ctx,
1188 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
1189 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1190 "acl_modify: cannot add to object tree attributeGUID\n");
1191 ret = LDB_ERR_OPERATIONS_ERROR;
1192 goto fail;
1195 status = sec_access_check_ds(sd, acl_user_token(module),
1196 SEC_ADS_WRITE_PROP,
1197 &access_granted,
1198 root,
1199 sid);
1200 if (!NT_STATUS_IS_OK(status)) {
1201 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1202 "Object %s has no write property access\n",
1203 ldb_dn_get_linearized(msg->dn));
1204 dsdb_acl_debug(sd,
1205 acl_user_token(module),
1206 msg->dn,
1207 true,
1208 10);
1209 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1210 goto fail;
1215 success:
1216 talloc_free(tmp_ctx);
1217 return ldb_next_request(module, req);
1218 fail:
1219 talloc_free(tmp_ctx);
1220 return ret;
1223 /* similar to the modify for the time being.
1224 * We need to consider the special delete tree case, though - TODO */
1225 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
1227 int ret;
1228 struct ldb_dn *parent;
1229 struct ldb_context *ldb;
1230 struct ldb_dn *nc_root;
1231 struct ldb_control *as_system;
1233 if (ldb_dn_is_special(req->op.del.dn)) {
1234 return ldb_next_request(module, req);
1237 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1238 if (as_system != NULL) {
1239 as_system->critical = 0;
1242 if (dsdb_module_am_system(module) || as_system) {
1243 return ldb_next_request(module, req);
1246 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
1248 ldb = ldb_module_get_ctx(module);
1250 parent = ldb_dn_get_parent(req, req->op.del.dn);
1251 if (parent == NULL) {
1252 return ldb_oom(ldb);
1255 /* Make sure we aren't deleting a NC */
1257 ret = dsdb_find_nc_root(ldb, req, req->op.del.dn, &nc_root);
1258 if (ret != LDB_SUCCESS) {
1259 return ret;
1261 if (ldb_dn_compare(nc_root, req->op.del.dn) == 0) {
1262 talloc_free(nc_root);
1263 DEBUG(10,("acl:deleting a NC\n"));
1264 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1265 return ldb_module_done(req, NULL, NULL,
1266 LDB_ERR_UNWILLING_TO_PERFORM);
1268 talloc_free(nc_root);
1270 if (ldb_request_get_control(req, LDB_CONTROL_TREE_DELETE_OID)) {
1271 ret = dsdb_module_check_access_on_dn(module, req,
1272 req->op.del.dn,
1273 SEC_ADS_DELETE_TREE, NULL,
1274 req);
1275 if (ret != LDB_SUCCESS) {
1276 return ret;
1279 return ldb_next_request(module, req);
1282 /* First check if we have delete object right */
1283 ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn,
1284 SEC_STD_DELETE, NULL, req);
1285 if (ret == LDB_SUCCESS) {
1286 return ldb_next_request(module, req);
1289 /* Nope, we don't have delete object. Lets check if we have delete
1290 * child on the parent */
1291 ret = dsdb_module_check_access_on_dn(module, req, parent,
1292 SEC_ADS_DELETE_CHILD, NULL, req);
1293 if (ret != LDB_SUCCESS) {
1294 return ret;
1297 return ldb_next_request(module, req);
1300 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
1302 int ret;
1303 struct ldb_dn *oldparent;
1304 struct ldb_dn *newparent;
1305 const struct dsdb_schema *schema;
1306 const struct dsdb_class *objectclass;
1307 struct ldb_context *ldb;
1308 struct security_descriptor *sd = NULL;
1309 struct dom_sid *sid = NULL;
1310 struct ldb_result *acl_res;
1311 const struct GUID *guid;
1312 struct ldb_dn *nc_root;
1313 struct object_tree *root = NULL;
1314 struct object_tree *new_node = NULL;
1315 struct ldb_control *as_system;
1316 TALLOC_CTX *tmp_ctx;
1317 NTSTATUS status;
1318 uint32_t access_granted;
1319 const char *rdn_name;
1320 static const char *acl_attrs[] = {
1321 "nTSecurityDescriptor",
1322 "objectClass",
1323 "objectSid",
1324 NULL
1327 if (ldb_dn_is_special(req->op.rename.olddn)) {
1328 return ldb_next_request(module, req);
1331 as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1332 if (as_system != NULL) {
1333 as_system->critical = 0;
1336 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
1337 if (dsdb_module_am_system(module) || as_system) {
1338 return ldb_next_request(module, req);
1341 ldb = ldb_module_get_ctx(module);
1343 tmp_ctx = talloc_new(req);
1344 if (tmp_ctx == NULL) {
1345 return ldb_oom(ldb);
1348 oldparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.olddn);
1349 if (oldparent == NULL) {
1350 return ldb_oom(ldb);
1352 newparent = ldb_dn_get_parent(tmp_ctx, req->op.rename.newdn);
1353 if (newparent == NULL) {
1354 return ldb_oom(ldb);
1357 /* Make sure we aren't renaming/moving a NC */
1359 ret = dsdb_find_nc_root(ldb, req, req->op.rename.olddn, &nc_root);
1360 if (ret != LDB_SUCCESS) {
1361 return ret;
1363 if (ldb_dn_compare(nc_root, req->op.rename.olddn) == 0) {
1364 talloc_free(nc_root);
1365 DEBUG(10,("acl:renaming/moving a NC\n"));
1366 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1367 return ldb_module_done(req, NULL, NULL,
1368 LDB_ERR_UNWILLING_TO_PERFORM);
1370 talloc_free(nc_root);
1372 /* Look for the parent */
1374 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res,
1375 req->op.rename.olddn, acl_attrs,
1376 DSDB_FLAG_NEXT_MODULE |
1377 DSDB_FLAG_AS_SYSTEM |
1378 DSDB_SEARCH_SHOW_RECYCLED, req);
1379 /* we sould be able to find the parent */
1380 if (ret != LDB_SUCCESS) {
1381 DEBUG(10,("acl: failed to find object %s\n",
1382 ldb_dn_get_linearized(req->op.rename.olddn)));
1383 talloc_free(tmp_ctx);
1384 return ret;
1387 schema = dsdb_get_schema(ldb, acl_res);
1388 if (!schema) {
1389 talloc_free(tmp_ctx);
1390 return ldb_operr(ldb);
1393 objectclass = dsdb_get_structural_oc_from_msg(schema, acl_res->msgs[0]);
1394 if (!objectclass) {
1395 talloc_free(tmp_ctx);
1396 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
1397 "acl_modify: Error retrieving object class for GUID.");
1400 if (!insert_in_object_tree(tmp_ctx, &objectclass->schemaIDGUID, SEC_ADS_WRITE_PROP,
1401 &root, &new_node)) {
1402 talloc_free(tmp_ctx);
1403 return ldb_operr(ldb);
1406 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1407 "name");
1408 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1409 &new_node, &new_node)) {
1410 talloc_free(tmp_ctx);
1411 return ldb_operr(ldb);
1414 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1415 if (rdn_name == NULL) {
1416 talloc_free(tmp_ctx);
1417 return ldb_operr(ldb);
1419 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1420 rdn_name);
1421 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1422 &new_node, &new_node)) {
1423 talloc_free(tmp_ctx);
1424 return ldb_operr(ldb);
1427 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1429 if (ret != LDB_SUCCESS) {
1430 talloc_free(tmp_ctx);
1431 return ldb_operr(ldb);
1433 /* Theoretically we pass the check if the object has no sd */
1434 if (!sd) {
1435 talloc_free(tmp_ctx);
1436 return LDB_SUCCESS;
1438 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1439 status = sec_access_check_ds(sd, acl_user_token(module),
1440 SEC_ADS_WRITE_PROP,
1441 &access_granted,
1442 root,
1443 sid);
1445 if (!NT_STATUS_IS_OK(status)) {
1446 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1447 "Object %s has no wp on name\n",
1448 ldb_dn_get_linearized(req->op.rename.olddn));
1449 dsdb_acl_debug(sd,
1450 acl_user_token(module),
1451 req->op.rename.olddn,
1452 true,
1453 10);
1454 talloc_free(tmp_ctx);
1455 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1458 if (ldb_dn_compare(oldparent, newparent) == 0) {
1459 /* regular rename, not move, nothing more to do */
1460 talloc_free(tmp_ctx);
1461 return ldb_next_request(module, req);
1464 /* new parent should have create child */
1465 root = NULL;
1466 new_node = NULL;
1468 ret = dsdb_module_check_access_on_dn(module, req, newparent,
1469 SEC_ADS_CREATE_CHILD,
1470 &objectclass->schemaIDGUID, req);
1471 if (ret != LDB_SUCCESS) {
1472 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1473 "acl:access_denied renaming %s",
1474 ldb_dn_get_linearized(req->op.rename.olddn));
1475 talloc_free(tmp_ctx);
1476 return ret;
1478 /* do we have delete object on the object? */
1480 status = sec_access_check_ds(sd, acl_user_token(module),
1481 SEC_STD_DELETE,
1482 &access_granted,
1483 NULL,
1484 sid);
1486 if (NT_STATUS_IS_OK(status)) {
1487 talloc_free(tmp_ctx);
1488 return ldb_next_request(module, req);
1490 /* what about delete child on the current parent */
1491 ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL, req);
1492 if (ret != LDB_SUCCESS) {
1493 ldb_asprintf_errstring(ldb_module_get_ctx(module),
1494 "acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn));
1495 talloc_free(tmp_ctx);
1496 return ldb_module_done(req, NULL, NULL, ret);
1499 talloc_free(tmp_ctx);
1501 return ldb_next_request(module, req);
1504 static int acl_search_update_confidential_attrs(struct acl_context *ac,
1505 struct acl_private *data)
1507 struct dsdb_attribute *a;
1508 uint32_t n = 0;
1510 if (data->acl_search) {
1512 * If acl:search is activated, the acl_read module
1513 * protects confidential attributes.
1515 return LDB_SUCCESS;
1518 if ((ac->schema == data->cached_schema_ptr) &&
1519 (ac->schema->loaded_usn == data->cached_schema_loaded_usn) &&
1520 (ac->schema->metadata_usn == data->cached_schema_metadata_usn))
1522 return LDB_SUCCESS;
1525 data->cached_schema_ptr = NULL;
1526 data->cached_schema_loaded_usn = 0;
1527 data->cached_schema_metadata_usn = 0;
1528 TALLOC_FREE(data->confidential_attrs);
1530 if (ac->schema == NULL) {
1531 return LDB_SUCCESS;
1534 for (a = ac->schema->attributes; a; a = a->next) {
1535 const char **attrs = data->confidential_attrs;
1537 if (!(a->searchFlags & SEARCH_FLAG_CONFIDENTIAL)) {
1538 continue;
1541 attrs = talloc_realloc(data, attrs, const char *, n + 2);
1542 if (attrs == NULL) {
1543 TALLOC_FREE(data->confidential_attrs);
1544 return ldb_module_oom(ac->module);
1547 attrs[n] = a->lDAPDisplayName;
1548 attrs[n+1] = NULL;
1549 n++;
1551 data->confidential_attrs = attrs;
1554 data->cached_schema_ptr = ac->schema;
1555 data->cached_schema_loaded_usn = ac->schema->loaded_usn;
1556 data->cached_schema_metadata_usn = ac->schema->metadata_usn;
1558 return LDB_SUCCESS;
1561 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1563 struct acl_context *ac;
1564 struct acl_private *data;
1565 struct ldb_result *acl_res;
1566 static const char *acl_attrs[] = {
1567 "objectClass",
1568 "nTSecurityDescriptor",
1569 "objectSid",
1570 NULL
1572 int ret;
1573 unsigned int i;
1575 ac = talloc_get_type(req->context, struct acl_context);
1576 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1577 if (!ares) {
1578 return ldb_module_done(ac->req, NULL, NULL,
1579 LDB_ERR_OPERATIONS_ERROR);
1581 if (ares->error != LDB_SUCCESS) {
1582 return ldb_module_done(ac->req, ares->controls,
1583 ares->response, ares->error);
1586 switch (ares->type) {
1587 case LDB_REPLY_ENTRY:
1588 if (ac->constructed_attrs) {
1589 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1590 acl_attrs,
1591 DSDB_FLAG_NEXT_MODULE |
1592 DSDB_FLAG_AS_SYSTEM |
1593 DSDB_SEARCH_SHOW_RECYCLED,
1594 req);
1595 if (ret != LDB_SUCCESS) {
1596 return ldb_module_done(ac->req, NULL, NULL, ret);
1600 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1601 ret = acl_allowedAttributes(ac->module, ac->schema,
1602 acl_res->msgs[0],
1603 ares->message, ac);
1604 if (ret != LDB_SUCCESS) {
1605 return ldb_module_done(ac->req, NULL, NULL, ret);
1609 if (ac->allowedChildClasses) {
1610 ret = acl_childClasses(ac->module, ac->schema,
1611 acl_res->msgs[0],
1612 ares->message,
1613 "allowedChildClasses");
1614 if (ret != LDB_SUCCESS) {
1615 return ldb_module_done(ac->req, NULL, NULL, ret);
1619 if (ac->allowedChildClassesEffective) {
1620 ret = acl_childClassesEffective(ac->module, ac->schema,
1621 acl_res->msgs[0],
1622 ares->message, ac);
1623 if (ret != LDB_SUCCESS) {
1624 return ldb_module_done(ac->req, NULL, NULL, ret);
1628 if (ac->sDRightsEffective) {
1629 ret = acl_sDRightsEffective(ac->module,
1630 acl_res->msgs[0],
1631 ares->message, ac);
1632 if (ret != LDB_SUCCESS) {
1633 return ldb_module_done(ac->req, NULL, NULL, ret);
1637 if (data == NULL) {
1638 return ldb_module_send_entry(ac->req, ares->message,
1639 ares->controls);
1642 if (ac->am_system) {
1643 return ldb_module_send_entry(ac->req, ares->message,
1644 ares->controls);
1647 if (data->password_attrs != NULL) {
1648 for (i = 0; data->password_attrs[i]; i++) {
1649 if ((!ac->userPassword) &&
1650 (ldb_attr_cmp(data->password_attrs[i],
1651 "userPassword") == 0))
1653 continue;
1656 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1660 if (ac->am_administrator) {
1661 return ldb_module_send_entry(ac->req, ares->message,
1662 ares->controls);
1665 ret = acl_search_update_confidential_attrs(ac, data);
1666 if (ret != LDB_SUCCESS) {
1667 return ret;
1670 if (data->confidential_attrs != NULL) {
1671 for (i = 0; data->confidential_attrs[i]; i++) {
1672 ldb_msg_remove_attr(ares->message,
1673 data->confidential_attrs[i]);
1677 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1679 case LDB_REPLY_REFERRAL:
1680 return ldb_module_send_referral(ac->req, ares->referral);
1682 case LDB_REPLY_DONE:
1683 return ldb_module_done(ac->req, ares->controls,
1684 ares->response, LDB_SUCCESS);
1687 return LDB_SUCCESS;
1690 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1692 struct ldb_context *ldb;
1693 struct acl_context *ac;
1694 struct ldb_parse_tree *down_tree;
1695 struct ldb_request *down_req;
1696 struct acl_private *data;
1697 int ret;
1698 unsigned int i;
1700 if (ldb_dn_is_special(req->op.search.base)) {
1701 return ldb_next_request(module, req);
1704 ldb = ldb_module_get_ctx(module);
1706 ac = talloc_zero(req, struct acl_context);
1707 if (ac == NULL) {
1708 return ldb_oom(ldb);
1710 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1712 ac->module = module;
1713 ac->req = req;
1714 ac->am_system = dsdb_module_am_system(module);
1715 ac->am_administrator = dsdb_module_am_administrator(module);
1716 ac->constructed_attrs = false;
1717 ac->modify_search = true;
1718 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1719 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1720 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1721 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1722 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1723 ac->userPassword = true;
1724 ac->schema = dsdb_get_schema(ldb, ac);
1726 ac->constructed_attrs |= ac->allowedAttributes;
1727 ac->constructed_attrs |= ac->allowedChildClasses;
1728 ac->constructed_attrs |= ac->allowedChildClassesEffective;
1729 ac->constructed_attrs |= ac->allowedAttributesEffective;
1730 ac->constructed_attrs |= ac->sDRightsEffective;
1732 if (data == NULL) {
1733 ac->modify_search = false;
1735 if (ac->am_system) {
1736 ac->modify_search = false;
1739 if (!ac->constructed_attrs && !ac->modify_search) {
1740 talloc_free(ac);
1741 return ldb_next_request(module, req);
1744 if (!ac->am_system) {
1745 ac->userPassword = dsdb_user_password_support(module, ac, req);
1748 ret = acl_search_update_confidential_attrs(ac, data);
1749 if (ret != LDB_SUCCESS) {
1750 return ret;
1753 down_tree = ldb_parse_tree_copy_shallow(ac, req->op.search.tree);
1754 if (down_tree == NULL) {
1755 return ldb_oom(ldb);
1758 if (!ac->am_system && data->password_attrs) {
1759 for (i = 0; data->password_attrs[i]; i++) {
1760 if ((!ac->userPassword) &&
1761 (ldb_attr_cmp(data->password_attrs[i],
1762 "userPassword") == 0))
1764 continue;
1767 ldb_parse_tree_attr_replace(down_tree,
1768 data->password_attrs[i],
1769 "kludgeACLredactedattribute");
1773 if (!ac->am_system && !ac->am_administrator && data->confidential_attrs) {
1774 for (i = 0; data->confidential_attrs[i]; i++) {
1775 ldb_parse_tree_attr_replace(down_tree,
1776 data->confidential_attrs[i],
1777 "kludgeACLredactedattribute");
1781 ret = ldb_build_search_req_ex(&down_req,
1782 ldb, ac,
1783 req->op.search.base,
1784 req->op.search.scope,
1785 down_tree,
1786 req->op.search.attrs,
1787 req->controls,
1788 ac, acl_search_callback,
1789 req);
1790 LDB_REQ_SET_LOCATION(down_req);
1791 if (ret != LDB_SUCCESS) {
1792 return ret;
1794 /* perform the search */
1795 return ldb_next_request(module, down_req);
1798 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1800 struct ldb_context *ldb = ldb_module_get_ctx(module);
1801 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1803 /* allow everybody to read the sequence number */
1804 if (strcmp(req->op.extended.oid,
1805 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1806 return ldb_next_request(module, req);
1809 if (dsdb_module_am_system(module) ||
1810 dsdb_module_am_administrator(module) || as_system) {
1811 return ldb_next_request(module, req);
1812 } else {
1813 ldb_asprintf_errstring(ldb,
1814 "acl_extended: "
1815 "attempted database modify not permitted. "
1816 "User %s is not SYSTEM or an administrator",
1817 acl_user_name(req, module));
1818 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1822 static const struct ldb_module_ops ldb_acl_module_ops = {
1823 .name = "acl",
1824 .search = acl_search,
1825 .add = acl_add,
1826 .modify = acl_modify,
1827 .del = acl_delete,
1828 .rename = acl_rename,
1829 .extended = acl_extended,
1830 .init_context = acl_module_init
1833 int ldb_acl_module_init(const char *version)
1835 LDB_MODULE_CHECK_VERSION(version);
1836 return ldb_register_module(&ldb_acl_module_ops);