s4-dsdb guard principalName parse for invalid inputs
[Samba/gebeck_regimport.git] / source4 / dsdb / samdb / ldb_modules / acl.c
blob49152d418a2cae543575c484230a20e2b2353556
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 "dsdb/samdb/ldb_modules/schema.h"
43 #include "lib/util/tsort.h"
44 #include "system/kerberos.h"
45 #include "auth/kerberos/kerberos.h"
47 struct extended_access_check_attribute {
48 const char *oa_name;
49 const uint32_t requires_rights;
52 struct acl_private {
53 bool acl_perform;
54 const char **password_attrs;
57 struct acl_context {
58 struct ldb_module *module;
59 struct ldb_request *req;
60 bool am_system;
61 bool allowedAttributes;
62 bool allowedAttributesEffective;
63 bool allowedChildClasses;
64 bool allowedChildClassesEffective;
65 bool sDRightsEffective;
66 bool userPassword;
67 const char * const *attrs;
68 struct dsdb_schema *schema;
71 static int acl_module_init(struct ldb_module *module)
73 struct ldb_context *ldb;
74 struct acl_private *data;
75 int ret;
76 unsigned int i;
77 TALLOC_CTX *mem_ctx;
78 static const char *attrs[] = { "passwordAttribute", NULL };
79 struct ldb_result *res;
80 struct ldb_message *msg;
81 struct ldb_message_element *password_attributes;
83 ldb = ldb_module_get_ctx(module);
85 ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
86 if (ret != LDB_SUCCESS) {
87 ldb_debug(ldb, LDB_DEBUG_ERROR,
88 "acl_module_init: Unable to register control with rootdse!\n");
89 return ldb_operr(ldb);
92 data = talloc(module, struct acl_private);
93 if (data == NULL) {
94 return ldb_oom(ldb);
97 data->password_attrs = NULL;
98 data->acl_perform = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"),
99 NULL, "acl", "perform", false);
100 ldb_module_set_private(module, data);
102 mem_ctx = talloc_new(module);
103 if (!mem_ctx) {
104 return ldb_oom(ldb);
107 ret = dsdb_module_search_dn(module, mem_ctx, &res,
108 ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
109 attrs,
110 DSDB_FLAG_NEXT_MODULE, NULL);
111 if (ret != LDB_SUCCESS) {
112 goto done;
114 if (res->count == 0) {
115 goto done;
118 if (res->count > 1) {
119 talloc_free(mem_ctx);
120 return LDB_ERR_CONSTRAINT_VIOLATION;
123 msg = res->msgs[0];
125 password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
126 if (!password_attributes) {
127 goto done;
129 data->password_attrs = talloc_array(data, const char *, password_attributes->num_values + 1);
130 if (!data->password_attrs) {
131 talloc_free(mem_ctx);
132 return ldb_oom(ldb);
134 for (i=0; i < password_attributes->num_values; i++) {
135 data->password_attrs[i] = (const char *)password_attributes->values[i].data;
136 talloc_steal(data->password_attrs, password_attributes->values[i].data);
138 data->password_attrs[i] = NULL;
140 done:
141 talloc_free(mem_ctx);
142 return ldb_next_init(module);
145 static int acl_allowedAttributes(struct ldb_module *module,
146 const struct dsdb_schema *schema,
147 struct ldb_message *sd_msg,
148 struct ldb_message *msg,
149 struct acl_context *ac)
151 struct ldb_message_element *oc_el;
152 struct ldb_context *ldb = ldb_module_get_ctx(module);
153 TALLOC_CTX *mem_ctx;
154 const char **attr_list;
155 int i, ret;
157 /* If we don't have a schema yet, we can't do anything... */
158 if (schema == NULL) {
159 ldb_asprintf_errstring(ldb, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
160 return LDB_ERR_OPERATIONS_ERROR;
163 /* Must remove any existing attribute */
164 if (ac->allowedAttributes) {
165 ldb_msg_remove_attr(msg, "allowedAttributes");
168 mem_ctx = talloc_new(msg);
169 if (!mem_ctx) {
170 return ldb_oom(ldb);
173 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
174 attr_list = dsdb_full_attribute_list(mem_ctx, schema, oc_el, DSDB_SCHEMA_ALL);
175 if (!attr_list) {
176 ldb_asprintf_errstring(ldb, "acl: Failed to get list of attributes");
177 talloc_free(mem_ctx);
178 return LDB_ERR_OPERATIONS_ERROR;
180 if (ac->allowedAttributes) {
181 for (i=0; attr_list && attr_list[i]; i++) {
182 ldb_msg_add_string(msg, "allowedAttributes", attr_list[i]);
185 if (ac->allowedAttributesEffective) {
186 struct security_descriptor *sd;
187 struct dom_sid *sid = NULL;
188 struct ldb_control *as_system = ldb_request_get_control(ac->req,
189 LDB_CONTROL_AS_SYSTEM_OID);
191 if (as_system != NULL) {
192 as_system->critical = 0;
195 ldb_msg_remove_attr(msg, "allowedAttributesEffective");
196 if (ac->am_system || as_system) {
197 for (i=0; attr_list && attr_list[i]; i++) {
198 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
200 return LDB_SUCCESS;
203 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), mem_ctx, sd_msg, &sd);
205 if (ret != LDB_SUCCESS) {
206 return ret;
209 sid = samdb_result_dom_sid(mem_ctx, sd_msg, "objectSid");
210 for (i=0; attr_list && attr_list[i]; i++) {
211 const struct dsdb_attribute *attr = dsdb_attribute_by_lDAPDisplayName(schema,
212 attr_list[i]);
213 if (!attr) {
214 return ldb_operr(ldb);
216 /* remove constructed attributes */
217 if (attr->systemFlags & DS_FLAG_ATTR_IS_CONSTRUCTED
218 || attr->systemOnly
219 || (attr->linkID != 0 && attr->linkID % 2 != 0 )) {
220 continue;
222 ret = acl_check_access_on_attribute(module,
223 msg,
225 sid,
226 SEC_ADS_WRITE_PROP,
227 attr);
228 if (ret == LDB_SUCCESS) {
229 ldb_msg_add_string(msg, "allowedAttributesEffective", attr_list[i]);
233 return LDB_SUCCESS;
236 static int acl_childClasses(struct ldb_module *module,
237 const struct dsdb_schema *schema,
238 struct ldb_message *sd_msg,
239 struct ldb_message *msg,
240 const char *attrName)
242 struct ldb_message_element *oc_el;
243 struct ldb_message_element *allowedClasses;
244 const struct dsdb_class *sclass;
245 unsigned int i, j;
246 int ret;
248 /* If we don't have a schema yet, we can't do anything... */
249 if (schema == NULL) {
250 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
251 return LDB_ERR_OPERATIONS_ERROR;
254 /* Must remove any existing attribute, or else confusion reins */
255 ldb_msg_remove_attr(msg, attrName);
256 ret = ldb_msg_add_empty(msg, attrName, 0, &allowedClasses);
257 if (ret != LDB_SUCCESS) {
258 return ret;
261 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
263 for (i=0; oc_el && i < oc_el->num_values; i++) {
264 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
265 if (!sclass) {
266 /* We don't know this class? what is going on? */
267 continue;
270 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
271 ldb_msg_add_string(msg, attrName, sclass->possibleInferiors[j]);
274 if (allowedClasses->num_values > 1) {
275 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
276 for (i=1 ; i < allowedClasses->num_values; i++) {
277 struct ldb_val *val1 = &allowedClasses->values[i-1];
278 struct ldb_val *val2 = &allowedClasses->values[i];
279 if (data_blob_cmp(val1, val2) == 0) {
280 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof(struct ldb_val));
281 allowedClasses->num_values--;
282 i--;
287 return LDB_SUCCESS;
290 static int acl_childClassesEffective(struct ldb_module *module,
291 const struct dsdb_schema *schema,
292 struct ldb_message *sd_msg,
293 struct ldb_message *msg,
294 struct acl_context *ac)
296 struct ldb_message_element *oc_el;
297 struct ldb_message_element *allowedClasses = NULL;
298 const struct dsdb_class *sclass;
299 struct security_descriptor *sd;
300 struct ldb_control *as_system = ldb_request_get_control(ac->req,
301 LDB_CONTROL_AS_SYSTEM_OID);
302 struct dom_sid *sid = NULL;
303 unsigned int i, j;
304 int ret;
306 if (as_system != NULL) {
307 as_system->critical = 0;
310 if (ac->am_system || as_system) {
311 return acl_childClasses(module, schema, sd_msg, msg, "allowedChildClassesEffective");
314 /* If we don't have a schema yet, we can't do anything... */
315 if (schema == NULL) {
316 ldb_asprintf_errstring(ldb_module_get_ctx(module), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg->dn));
317 return LDB_ERR_OPERATIONS_ERROR;
320 /* Must remove any existing attribute, or else confusion reins */
321 ldb_msg_remove_attr(msg, "allowedChildClassesEffective");
323 oc_el = ldb_msg_find_element(sd_msg, "objectClass");
324 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
325 if (ret != LDB_SUCCESS) {
326 return ret;
329 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
330 for (i=0; oc_el && i < oc_el->num_values; i++) {
331 sclass = dsdb_class_by_lDAPDisplayName_ldb_val(schema, &oc_el->values[i]);
332 if (!sclass) {
333 /* We don't know this class? what is going on? */
334 continue;
337 for (j=0; sclass->possibleInferiors && sclass->possibleInferiors[j]; j++) {
338 ret = acl_check_access_on_class(module,
339 schema,
340 msg,
342 sid,
343 SEC_ADS_CREATE_CHILD,
344 sclass->possibleInferiors[j]);
345 if (ret == LDB_SUCCESS) {
346 ldb_msg_add_string(msg, "allowedChildClassesEffective",
347 sclass->possibleInferiors[j]);
351 allowedClasses = ldb_msg_find_element(msg, "allowedChildClassesEffective");
352 if (!allowedClasses) {
353 return LDB_SUCCESS;
356 if (allowedClasses->num_values > 1) {
357 TYPESAFE_QSORT(allowedClasses->values, allowedClasses->num_values, data_blob_cmp);
358 for (i=1 ; i < allowedClasses->num_values; i++) {
359 struct ldb_val *val1 = &allowedClasses->values[i-1];
360 struct ldb_val *val2 = &allowedClasses->values[i];
361 if (data_blob_cmp(val1, val2) == 0) {
362 memmove(val1, val2, (allowedClasses->num_values - i) * sizeof( struct ldb_val));
363 allowedClasses->num_values--;
364 i--;
368 return LDB_SUCCESS;
371 static int acl_sDRightsEffective(struct ldb_module *module,
372 struct ldb_message *sd_msg,
373 struct ldb_message *msg,
374 struct acl_context *ac)
376 struct ldb_message_element *rightsEffective;
377 int ret;
378 struct security_descriptor *sd;
379 struct ldb_control *as_system = ldb_request_get_control(ac->req,
380 LDB_CONTROL_AS_SYSTEM_OID);
381 struct dom_sid *sid = NULL;
382 uint32_t flags = 0;
384 if (as_system != NULL) {
385 as_system->critical = 0;
388 /* Must remove any existing attribute, or else confusion reins */
389 ldb_msg_remove_attr(msg, "sDRightsEffective");
390 ret = ldb_msg_add_empty(msg, "sDRightsEffective", 0, &rightsEffective);
391 if (ret != LDB_SUCCESS) {
392 return ret;
394 if (ac->am_system || as_system) {
395 flags = SECINFO_OWNER | SECINFO_GROUP | SECINFO_SACL | SECINFO_DACL;
397 else {
398 /* Get the security descriptor from the message */
399 ret = dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module), msg, sd_msg, &sd);
400 if (ret != LDB_SUCCESS) {
401 return ret;
403 sid = samdb_result_dom_sid(msg, sd_msg, "objectSid");
404 ret = acl_check_access_on_attribute(module,
405 msg,
407 sid,
408 SEC_STD_WRITE_OWNER,
409 NULL);
410 if (ret == LDB_SUCCESS) {
411 flags |= SECINFO_OWNER | SECINFO_GROUP;
413 ret = acl_check_access_on_attribute(module,
414 msg,
416 sid,
417 SEC_STD_WRITE_DAC,
418 NULL);
419 if (ret == LDB_SUCCESS) {
420 flags |= SECINFO_DACL;
422 ret = acl_check_access_on_attribute(module,
423 msg,
425 sid,
426 SEC_FLAG_SYSTEM_SECURITY,
427 NULL);
428 if (ret == LDB_SUCCESS) {
429 flags |= SECINFO_SACL;
432 return samdb_msg_add_uint(ldb_module_get_ctx(module), msg, msg,
433 "sDRightsEffective", flags);
436 static int acl_validate_spn_value(TALLOC_CTX *mem_ctx,
437 struct ldb_context *ldb,
438 const char *spn_value,
439 uint32_t userAccountControl,
440 const char *samAccountName,
441 const char *dnsHostName,
442 const char *netbios_name,
443 const char *ntds_guid)
445 int ret;
446 krb5_context krb_ctx;
447 krb5_error_code kerr;
448 krb5_principal principal;
449 char *instanceName;
450 char *serviceType;
451 char *serviceName;
452 const char *realm;
453 const char *forest_name = samdb_forest_name(ldb, mem_ctx);
454 const char *base_domain = samdb_default_domain_name(ldb, mem_ctx);
455 struct loadparm_context *lp_ctx = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
456 struct loadparm_context);
457 bool is_dc = (userAccountControl & UF_SERVER_TRUST_ACCOUNT) ||
458 (userAccountControl & UF_PARTIAL_SECRETS_ACCOUNT);
460 if (strcasecmp_m(spn_value, samAccountName) == 0) {
461 /* MacOS X sets this value, and setting an SPN of your
462 * own samAccountName is both pointless and safe */
463 return LDB_SUCCESS;
466 kerr = smb_krb5_init_context_basic(mem_ctx,
467 lp_ctx,
468 &krb_ctx);
469 if (kerr != 0) {
470 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
471 "Could not initialize kerberos context.");
474 ret = krb5_parse_name(krb_ctx, spn_value, &principal);
475 if (ret) {
476 krb5_free_context(krb_ctx);
477 return LDB_ERR_CONSTRAINT_VIOLATION;
480 if (principal->name.name_string.len < 2) {
481 goto fail;
484 instanceName = principal->name.name_string.val[1];
485 serviceType = principal->name.name_string.val[0];
486 realm = krb5_principal_get_realm(krb_ctx, principal);
487 if (principal->name.name_string.len == 3) {
488 serviceName = principal->name.name_string.val[2];
489 } else {
490 serviceName = NULL;
493 if (serviceName) {
494 if (!is_dc) {
495 goto fail;
497 if (strcasecmp(serviceType, "ldap") == 0) {
498 if (strcasecmp(serviceName, netbios_name) != 0 &&
499 strcasecmp(serviceName, forest_name) != 0) {
500 goto fail;
503 } else if (strcasecmp(serviceType, "gc") == 0) {
504 if (strcasecmp(serviceName, forest_name) != 0) {
505 goto fail;
507 } else {
508 if (strcasecmp(serviceName, base_domain) != 0 &&
509 strcasecmp(serviceName, netbios_name) != 0) {
510 goto fail;
514 /* instanceName can be samAccountName without $ or dnsHostName
515 * or "ntds_guid._msdcs.forest_domain for DC objects */
516 if (strlen(instanceName) == (strlen(samAccountName) - 1)
517 && strncasecmp(instanceName, samAccountName, strlen(samAccountName) - 1) == 0) {
518 goto success;
519 } else if (strcasecmp(instanceName, dnsHostName) == 0) {
520 goto success;
521 } else if (is_dc) {
522 const char *guid_str;
523 guid_str = talloc_asprintf(mem_ctx,"%s._msdcs.%s",
524 ntds_guid,
525 forest_name);
526 if (strcasecmp(instanceName, guid_str) == 0) {
527 goto success;
531 fail:
532 krb5_free_principal(krb_ctx, principal);
533 krb5_free_context(krb_ctx);
534 return LDB_ERR_CONSTRAINT_VIOLATION;
536 success:
537 krb5_free_principal(krb_ctx, principal);
538 krb5_free_context(krb_ctx);
539 return LDB_SUCCESS;
542 static int acl_check_spn(TALLOC_CTX *mem_ctx,
543 struct ldb_module *module,
544 struct ldb_request *req,
545 struct security_descriptor *sd,
546 struct dom_sid *sid,
547 const struct GUID *oc_guid,
548 const struct dsdb_attribute *attr)
550 int ret;
551 unsigned int i;
552 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
553 struct ldb_context *ldb = ldb_module_get_ctx(module);
554 struct ldb_result *acl_res;
555 struct ldb_result *netbios_res;
556 struct ldb_message_element *el;
557 struct ldb_dn *partitions_dn = samdb_partitions_dn(ldb, tmp_ctx);
558 uint32_t userAccountControl;
559 const char *samAccountName;
560 const char *dnsHostName;
561 const char *netbios_name;
562 struct GUID ntds;
563 char *ntds_guid = NULL;
565 static const char *acl_attrs[] = {
566 "samAccountName",
567 "dnsHostName",
568 "userAccountControl",
569 NULL
571 static const char *netbios_attrs[] = {
572 "nETBIOSName",
573 NULL
576 /* if we have wp, we can do whatever we like */
577 if (acl_check_access_on_attribute(module,
578 tmp_ctx,
580 sid,
581 SEC_ADS_WRITE_PROP,
582 attr) == LDB_SUCCESS) {
583 talloc_free(tmp_ctx);
584 return LDB_SUCCESS;
587 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
588 GUID_DRS_VALIDATE_SPN,
589 SEC_ADS_SELF_WRITE,
590 sid);
592 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
593 dsdb_acl_debug(sd, acl_user_token(module),
594 req->op.mod.message->dn,
595 true,
596 10);
597 talloc_free(tmp_ctx);
598 return ret;
601 ret = dsdb_module_search_dn(module, tmp_ctx,
602 &acl_res, req->op.mod.message->dn,
603 acl_attrs,
604 DSDB_FLAG_NEXT_MODULE |
605 DSDB_SEARCH_SHOW_DELETED, req);
606 if (ret != LDB_SUCCESS) {
607 talloc_free(tmp_ctx);
608 return ret;
611 userAccountControl = ldb_msg_find_attr_as_uint(acl_res->msgs[0], "userAccountControl", 0);
612 dnsHostName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "dnsHostName", NULL);
613 samAccountName = ldb_msg_find_attr_as_string(acl_res->msgs[0], "samAccountName", NULL);
615 ret = dsdb_module_search(module, tmp_ctx,
616 &netbios_res, partitions_dn,
617 LDB_SCOPE_ONELEVEL,
618 netbios_attrs,
619 DSDB_FLAG_NEXT_MODULE,
620 req,
621 "(ncName=%s)",
622 ldb_dn_get_linearized(ldb_get_default_basedn(ldb)));
624 netbios_name = ldb_msg_find_attr_as_string(netbios_res->msgs[0], "nETBIOSName", NULL);
626 el = ldb_msg_find_element(req->op.mod.message, "servicePrincipalName");
627 if (!el) {
628 talloc_free(tmp_ctx);
629 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
630 "Error finding element for servicePrincipalName.");
633 /* NTDSDSA objectGuid of object we are checking SPN for */
634 if (userAccountControl & (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT)) {
635 ret = dsdb_module_find_ntdsguid_for_computer(module, tmp_ctx,
636 req->op.mod.message->dn, &ntds, req);
637 if (ret != LDB_SUCCESS) {
638 ldb_asprintf_errstring(ldb, "Failed to find NTDSDSA objectGuid for %s: %s",
639 ldb_dn_get_linearized(req->op.mod.message->dn),
640 ldb_strerror(ret));
641 talloc_free(tmp_ctx);
642 return LDB_ERR_OPERATIONS_ERROR;
644 ntds_guid = GUID_string(tmp_ctx, &ntds);
647 for (i=0; i < el->num_values; i++) {
648 ret = acl_validate_spn_value(tmp_ctx,
649 ldb,
650 (char *)el->values[i].data,
651 userAccountControl,
652 samAccountName,
653 dnsHostName,
654 netbios_name,
655 ntds_guid);
656 if (ret != LDB_SUCCESS) {
657 talloc_free(tmp_ctx);
658 return ret;
661 talloc_free(tmp_ctx);
662 return LDB_SUCCESS;
665 static int acl_add(struct ldb_module *module, struct ldb_request *req)
667 int ret;
668 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.add.message->dn);
669 struct ldb_context *ldb;
670 const struct dsdb_schema *schema;
671 struct ldb_message_element *oc_el;
672 const struct GUID *guid;
673 struct ldb_dn *nc_root;
674 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
676 if (as_system != NULL) {
677 as_system->critical = 0;
680 if (dsdb_module_am_system(module) || as_system) {
681 return ldb_next_request(module, req);
683 if (ldb_dn_is_special(req->op.add.message->dn)) {
684 return ldb_next_request(module, req);
687 ldb = ldb_module_get_ctx(module);
689 /* Creating an NC. There is probably something we should do here,
690 * but we will establish that later */
692 ret = dsdb_find_nc_root(ldb, req, req->op.add.message->dn, &nc_root);
693 if (ret != LDB_SUCCESS) {
694 return ret;
696 if (ldb_dn_compare(nc_root, req->op.add.message->dn) == 0) {
697 talloc_free(nc_root);
698 return ldb_next_request(module, req);
700 talloc_free(nc_root);
702 schema = dsdb_get_schema(ldb, req);
703 if (!schema) {
704 return ldb_operr(ldb);
707 oc_el = ldb_msg_find_element(req->op.add.message, "objectClass");
708 if (!oc_el || oc_el->num_values == 0) {
709 DEBUG(10,("acl:operation error %s\n", ldb_dn_get_linearized(req->op.add.message->dn)));
710 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
713 guid = class_schemaid_guid_by_lDAPDisplayName(schema,
714 (char *)oc_el->values[oc_el->num_values-1].data);
715 ret = dsdb_module_check_access_on_dn(module, req, parent, SEC_ADS_CREATE_CHILD, guid, req);
716 if (ret != LDB_SUCCESS) {
717 return ret;
719 return ldb_next_request(module, req);
722 /* ckecks if modifications are allowed on "Member" attribute */
723 static int acl_check_self_membership(TALLOC_CTX *mem_ctx,
724 struct ldb_module *module,
725 struct ldb_request *req,
726 struct security_descriptor *sd,
727 struct dom_sid *sid,
728 const struct GUID *oc_guid,
729 const struct dsdb_attribute *attr)
731 int ret;
732 unsigned int i;
733 struct ldb_context *ldb = ldb_module_get_ctx(module);
734 struct ldb_dn *user_dn;
735 struct ldb_message_element *member_el;
736 /* if we have wp, we can do whatever we like */
737 if (acl_check_access_on_attribute(module,
738 mem_ctx,
740 sid,
741 SEC_ADS_WRITE_PROP,
742 attr) == LDB_SUCCESS) {
743 return LDB_SUCCESS;
745 /* if we are adding/deleting ourselves, check for self membership */
746 ret = dsdb_find_dn_by_sid(ldb, mem_ctx,
747 &acl_user_token(module)->sids[PRIMARY_USER_SID_INDEX],
748 &user_dn);
749 if (ret != LDB_SUCCESS) {
750 return ret;
752 member_el = ldb_msg_find_element(req->op.mod.message, "member");
753 if (!member_el) {
754 return ldb_operr(ldb);
756 /* user can only remove oneself */
757 if (member_el->num_values == 0) {
758 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
760 for (i = 0; i < member_el->num_values; i++) {
761 if (strcasecmp((const char *)member_el->values[i].data,
762 ldb_dn_get_extended_linearized(mem_ctx, user_dn, 1)) != 0) {
763 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
766 ret = acl_check_extended_right(mem_ctx, sd, acl_user_token(module),
767 GUID_DRS_SELF_MEMBERSHIP,
768 SEC_ADS_SELF_WRITE,
769 sid);
770 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
771 dsdb_acl_debug(sd, acl_user_token(module),
772 req->op.mod.message->dn,
773 true,
774 10);
776 return ret;
779 static int acl_check_password_rights(TALLOC_CTX *mem_ctx,
780 struct ldb_module *module,
781 struct ldb_request *req,
782 struct security_descriptor *sd,
783 struct dom_sid *sid,
784 const struct GUID *oc_guid,
785 bool userPassword)
787 int ret = LDB_SUCCESS;
788 unsigned int del_attr_cnt = 0, add_attr_cnt = 0, rep_attr_cnt = 0;
789 struct ldb_message_element *el;
790 struct ldb_message *msg;
791 const char *passwordAttrs[] = { "userPassword", "clearTextPassword",
792 "unicodePwd", "dBCSPwd", NULL }, **l;
793 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
795 msg = ldb_msg_copy_shallow(tmp_ctx, req->op.mod.message);
796 if (msg == NULL) {
797 return ldb_module_oom(module);
799 for (l = passwordAttrs; *l != NULL; l++) {
800 if ((!userPassword) && (ldb_attr_cmp(*l, "userPassword") == 0)) {
801 continue;
804 while ((el = ldb_msg_find_element(msg, *l)) != NULL) {
805 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_DELETE) {
806 ++del_attr_cnt;
808 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_ADD) {
809 ++add_attr_cnt;
811 if (LDB_FLAG_MOD_TYPE(el->flags) == LDB_FLAG_MOD_REPLACE) {
812 ++rep_attr_cnt;
814 ldb_msg_remove_element(msg, el);
818 /* single deletes will be handled by the "password_hash" LDB module
819 * later in the stack, so we let it though here */
820 if ((del_attr_cnt > 0) && (add_attr_cnt == 0) && (rep_attr_cnt == 0)) {
821 talloc_free(tmp_ctx);
822 return LDB_SUCCESS;
825 if (ldb_request_get_control(req,
826 DSDB_CONTROL_PASSWORD_CHANGE_OID) != NULL) {
827 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
828 * have a user password change and not a set as the message
829 * looks like. In it's value blob it contains the NT and/or LM
830 * hash of the old password specified by the user.
831 * This control is used by the SAMR and "kpasswd" password
832 * change mechanisms. */
833 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
834 GUID_DRS_USER_CHANGE_PASSWORD,
835 SEC_ADS_CONTROL_ACCESS,
836 sid);
838 else if (rep_attr_cnt > 0 || (add_attr_cnt != del_attr_cnt)) {
839 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
840 GUID_DRS_FORCE_CHANGE_PASSWORD,
841 SEC_ADS_CONTROL_ACCESS,
842 sid);
844 else if (add_attr_cnt == 1 && del_attr_cnt == 1) {
845 ret = acl_check_extended_right(tmp_ctx, sd, acl_user_token(module),
846 GUID_DRS_USER_CHANGE_PASSWORD,
847 SEC_ADS_CONTROL_ACCESS,
848 sid);
849 /* Very strange, but we get constraint violation in this case */
850 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
851 ret = LDB_ERR_CONSTRAINT_VIOLATION;
854 if (ret != LDB_SUCCESS) {
855 dsdb_acl_debug(sd, acl_user_token(module),
856 req->op.mod.message->dn,
857 true,
858 10);
860 talloc_free(tmp_ctx);
861 return ret;
864 static int acl_modify(struct ldb_module *module, struct ldb_request *req)
866 int ret;
867 struct ldb_context *ldb = ldb_module_get_ctx(module);
868 const struct dsdb_schema *schema;
869 unsigned int i;
870 const struct GUID *guid;
871 uint32_t access_granted;
872 struct object_tree *root = NULL;
873 struct object_tree *new_node = NULL;
874 NTSTATUS status;
875 struct ldb_result *acl_res;
876 struct security_descriptor *sd;
877 struct dom_sid *sid = NULL;
878 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
879 bool userPassword = dsdb_user_password_support(module, req, req);
880 TALLOC_CTX *tmp_ctx = talloc_new(req);
881 static const char *acl_attrs[] = {
882 "nTSecurityDescriptor",
883 "objectClass",
884 "objectSid",
885 NULL
888 if (as_system != NULL) {
889 as_system->critical = 0;
892 /* Don't print this debug statement if elements[0].name is going to be NULL */
893 if(req->op.mod.message->num_elements > 0)
895 DEBUG(10, ("ldb:acl_modify: %s\n", req->op.mod.message->elements[0].name));
897 if (dsdb_module_am_system(module) || as_system) {
898 return ldb_next_request(module, req);
900 if (ldb_dn_is_special(req->op.mod.message->dn)) {
901 return ldb_next_request(module, req);
903 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res, req->op.mod.message->dn,
904 acl_attrs,
905 DSDB_FLAG_NEXT_MODULE, req);
907 if (ret != LDB_SUCCESS) {
908 goto fail;
911 schema = dsdb_get_schema(ldb, tmp_ctx);
912 if (!schema) {
913 ret = LDB_ERR_OPERATIONS_ERROR;
914 goto fail;
917 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
918 if (ret != LDB_SUCCESS) {
919 talloc_free(tmp_ctx);
920 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
921 "acl_modify: Error retrieving security descriptor.");
923 /* Theoretically we pass the check if the object has no sd */
924 if (!sd) {
925 goto success;
928 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
929 if (!guid) {
930 talloc_free(tmp_ctx);
931 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
932 "acl_modify: Error retrieving object class GUID.");
934 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
935 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
936 &root, &new_node)) {
937 talloc_free(tmp_ctx);
938 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
939 "acl_modify: Error adding new node in object tree.");
941 for (i=0; i < req->op.mod.message->num_elements; i++){
942 const struct dsdb_attribute *attr;
943 attr = dsdb_attribute_by_lDAPDisplayName(schema,
944 req->op.mod.message->elements[i].name);
946 if (ldb_attr_cmp("nTSecurityDescriptor", req->op.mod.message->elements[i].name) == 0) {
947 status = sec_access_check_ds(sd, acl_user_token(module),
948 SEC_STD_WRITE_DAC,
949 &access_granted,
950 NULL,
951 sid);
953 if (!NT_STATUS_IS_OK(status)) {
954 DEBUG(10, ("Object %s has no write dacl access\n",
955 ldb_dn_get_linearized(req->op.mod.message->dn)));
956 dsdb_acl_debug(sd,
957 acl_user_token(module),
958 req->op.mod.message->dn,
959 true,
960 10);
961 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
962 goto fail;
965 else if (ldb_attr_cmp("member", req->op.mod.message->elements[i].name) == 0) {
966 ret = acl_check_self_membership(tmp_ctx,
967 module,
968 req,
970 sid,
971 guid,
972 attr);
973 if (ret != LDB_SUCCESS) {
974 goto fail;
977 else if (ldb_attr_cmp("dBCSPwd", req->op.mod.message->elements[i].name) == 0) {
978 /* this one is not affected by any rights, we should let it through
979 so that passwords_hash returns the correct error */
980 continue;
982 else if (ldb_attr_cmp("unicodePwd", req->op.mod.message->elements[i].name) == 0 ||
983 (userPassword && ldb_attr_cmp("userPassword", req->op.mod.message->elements[i].name) == 0) ||
984 ldb_attr_cmp("clearTextPassword", req->op.mod.message->elements[i].name) == 0) {
985 ret = acl_check_password_rights(tmp_ctx,
986 module,
987 req,
989 sid,
990 guid,
991 userPassword);
992 if (ret != LDB_SUCCESS) {
993 goto fail;
995 } else if (ldb_attr_cmp("servicePrincipalName", req->op.mod.message->elements[i].name) == 0) {
996 ret = acl_check_spn(tmp_ctx,
997 module,
998 req,
1000 sid,
1001 guid,
1002 attr);
1003 if (ret != LDB_SUCCESS) {
1004 goto fail;
1006 } else {
1008 /* This basic attribute existence check with the right errorcode
1009 * is needed since this module is the first one which requests
1010 * schema attribute information.
1011 * The complete attribute checking is done in the
1012 * "objectclass_attrs" module behind this one.
1014 if (!attr) {
1015 ldb_asprintf_errstring(ldb, "acl_modify: attribute '%s' on entry '%s' was not found in the schema!",
1016 req->op.mod.message->elements[i].name,
1017 ldb_dn_get_linearized(req->op.mod.message->dn));
1018 ret = LDB_ERR_NO_SUCH_ATTRIBUTE;
1019 goto fail;
1021 if (!insert_in_object_tree(tmp_ctx,
1022 &attr->attributeSecurityGUID, SEC_ADS_WRITE_PROP,
1023 &new_node, &new_node)) {
1024 DEBUG(10, ("acl_modify: cannot add to object tree securityGUID\n"));
1025 ret = LDB_ERR_OPERATIONS_ERROR;
1026 goto fail;
1029 if (!insert_in_object_tree(tmp_ctx,
1030 &attr->schemaIDGUID, SEC_ADS_WRITE_PROP, &new_node, &new_node)) {
1031 DEBUG(10, ("acl_modify: cannot add to object tree attributeGUID\n"));
1032 ret = LDB_ERR_OPERATIONS_ERROR;
1033 goto fail;
1038 if (root->num_of_children > 0) {
1039 status = sec_access_check_ds(sd, acl_user_token(module),
1040 SEC_ADS_WRITE_PROP,
1041 &access_granted,
1042 root,
1043 sid);
1045 if (!NT_STATUS_IS_OK(status)) {
1046 DEBUG(10, ("Object %s has no write property access\n",
1047 ldb_dn_get_linearized(req->op.mod.message->dn)));
1048 dsdb_acl_debug(sd,
1049 acl_user_token(module),
1050 req->op.mod.message->dn,
1051 true,
1052 10);
1053 ret = LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1054 goto fail;
1058 success:
1059 talloc_free(tmp_ctx);
1060 return ldb_next_request(module, req);
1061 fail:
1062 talloc_free(tmp_ctx);
1063 return ret;
1066 /* similar to the modify for the time being.
1067 * We need to consider the special delete tree case, though - TODO */
1068 static int acl_delete(struct ldb_module *module, struct ldb_request *req)
1070 int ret;
1071 struct ldb_dn *parent = ldb_dn_get_parent(req, req->op.del.dn);
1072 struct ldb_context *ldb;
1073 struct ldb_dn *nc_root;
1074 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1076 if (as_system != NULL) {
1077 as_system->critical = 0;
1080 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req->op.del.dn)));
1081 if (dsdb_module_am_system(module) || as_system) {
1082 return ldb_next_request(module, req);
1084 if (ldb_dn_is_special(req->op.del.dn)) {
1085 return ldb_next_request(module, req);
1088 ldb = ldb_module_get_ctx(module);
1090 /* Make sure we aren't deleting a NC */
1092 ret = dsdb_find_nc_root(ldb, req, req->op.del.dn, &nc_root);
1093 if (ret != LDB_SUCCESS) {
1094 return ret;
1096 if (ldb_dn_compare(nc_root, req->op.del.dn) == 0) {
1097 talloc_free(nc_root);
1098 DEBUG(10,("acl:deleting a NC\n"));
1099 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1100 return ldb_module_done(req, NULL, NULL,
1101 LDB_ERR_UNWILLING_TO_PERFORM);
1103 talloc_free(nc_root);
1105 /* First check if we have delete object right */
1106 ret = dsdb_module_check_access_on_dn(module, req, req->op.del.dn,
1107 SEC_STD_DELETE, NULL, req);
1108 if (ret == LDB_SUCCESS) {
1109 return ldb_next_request(module, req);
1112 /* Nope, we don't have delete object. Lets check if we have delete
1113 * child on the parent */
1114 ret = dsdb_module_check_access_on_dn(module, req, parent,
1115 SEC_ADS_DELETE_CHILD, NULL, req);
1116 if (ret != LDB_SUCCESS) {
1117 return ret;
1120 return ldb_next_request(module, req);
1123 static int acl_rename(struct ldb_module *module, struct ldb_request *req)
1125 int ret;
1126 struct ldb_dn *oldparent = ldb_dn_get_parent(req, req->op.rename.olddn);
1127 struct ldb_dn *newparent = ldb_dn_get_parent(req, req->op.rename.newdn);
1128 const struct dsdb_schema *schema;
1129 struct ldb_context *ldb;
1130 struct security_descriptor *sd = NULL;
1131 struct dom_sid *sid = NULL;
1132 struct ldb_result *acl_res;
1133 const struct GUID *guid;
1134 struct ldb_dn *nc_root;
1135 struct object_tree *root = NULL;
1136 struct object_tree *new_node = NULL;
1137 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1138 TALLOC_CTX *tmp_ctx = talloc_new(req);
1139 NTSTATUS status;
1140 uint32_t access_granted;
1141 const char *rdn_name;
1142 static const char *acl_attrs[] = {
1143 "nTSecurityDescriptor",
1144 "objectClass",
1145 "objectSid",
1146 NULL
1149 if (as_system != NULL) {
1150 as_system->critical = 0;
1153 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req->op.rename.olddn)));
1154 if (dsdb_module_am_system(module) || as_system) {
1155 return ldb_next_request(module, req);
1157 if (ldb_dn_is_special(req->op.rename.olddn)) {
1158 return ldb_next_request(module, req);
1161 ldb = ldb_module_get_ctx(module);
1163 /* Make sure we aren't renaming/moving a NC */
1165 ret = dsdb_find_nc_root(ldb, req, req->op.rename.olddn, &nc_root);
1166 if (ret != LDB_SUCCESS) {
1167 return ret;
1169 if (ldb_dn_compare(nc_root, req->op.rename.olddn) == 0) {
1170 talloc_free(nc_root);
1171 DEBUG(10,("acl:renaming/moving a NC\n"));
1172 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1173 return ldb_module_done(req, NULL, NULL,
1174 LDB_ERR_UNWILLING_TO_PERFORM);
1176 talloc_free(nc_root);
1178 /* Look for the parent */
1180 ret = dsdb_module_search_dn(module, tmp_ctx, &acl_res,
1181 req->op.rename.olddn, acl_attrs,
1182 DSDB_FLAG_NEXT_MODULE |
1183 DSDB_SEARCH_SHOW_RECYCLED, req);
1184 /* we sould be able to find the parent */
1185 if (ret != LDB_SUCCESS) {
1186 DEBUG(10,("acl: failed to find object %s\n",
1187 ldb_dn_get_linearized(req->op.rename.olddn)));
1188 talloc_free(tmp_ctx);
1189 return ret;
1192 schema = dsdb_get_schema(ldb, acl_res);
1193 if (!schema) {
1194 talloc_free(tmp_ctx);
1195 return ldb_operr(ldb);
1198 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
1199 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1200 &root, &new_node)) {
1201 talloc_free(tmp_ctx);
1202 return ldb_operr(ldb);
1205 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1206 "name");
1207 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1208 &new_node, &new_node)) {
1209 talloc_free(tmp_ctx);
1210 return ldb_operr(ldb);
1213 rdn_name = ldb_dn_get_rdn_name(req->op.rename.olddn);
1214 if (rdn_name == NULL) {
1215 talloc_free(tmp_ctx);
1216 return ldb_operr(ldb);
1218 guid = attribute_schemaid_guid_by_lDAPDisplayName(schema,
1219 rdn_name);
1220 if (!insert_in_object_tree(tmp_ctx, guid, SEC_ADS_WRITE_PROP,
1221 &new_node, &new_node)) {
1222 talloc_free(tmp_ctx);
1223 return ldb_operr(ldb);
1226 ret = dsdb_get_sd_from_ldb_message(ldb, req, acl_res->msgs[0], &sd);
1228 if (ret != LDB_SUCCESS) {
1229 talloc_free(tmp_ctx);
1230 return ldb_operr(ldb);
1232 /* Theoretically we pass the check if the object has no sd */
1233 if (!sd) {
1234 talloc_free(tmp_ctx);
1235 return LDB_SUCCESS;
1237 sid = samdb_result_dom_sid(req, acl_res->msgs[0], "objectSid");
1238 status = sec_access_check_ds(sd, acl_user_token(module),
1239 SEC_ADS_WRITE_PROP,
1240 &access_granted,
1241 root,
1242 sid);
1244 if (!NT_STATUS_IS_OK(status)) {
1245 DEBUG(10, ("Object %s has no wp on name\n",
1246 ldb_dn_get_linearized(req->op.rename.olddn)));
1247 dsdb_acl_debug(sd,
1248 acl_user_token(module),
1249 req->op.rename.olddn,
1250 true,
1251 10);
1252 talloc_free(tmp_ctx);
1253 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1256 if (ldb_dn_compare(oldparent, newparent) == 0) {
1257 /* regular rename, not move, nothing more to do */
1258 talloc_free(tmp_ctx);
1259 return ldb_next_request(module, req);
1262 /* new parent should have create child */
1263 root = NULL;
1264 new_node = NULL;
1265 guid = get_oc_guid_from_message(module, schema, acl_res->msgs[0]);
1266 if (!guid) {
1267 DEBUG(10,("acl:renamed object has no object class\n"));
1268 talloc_free(tmp_ctx);
1269 return ldb_module_done(req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR);
1272 ret = dsdb_module_check_access_on_dn(module, req, newparent, SEC_ADS_CREATE_CHILD, guid, req);
1273 if (ret != LDB_SUCCESS) {
1274 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1275 talloc_free(tmp_ctx);
1276 return ret;
1278 /* do we have delete object on the object? */
1280 status = sec_access_check_ds(sd, acl_user_token(module),
1281 SEC_STD_DELETE,
1282 &access_granted,
1283 NULL,
1284 sid);
1286 if (NT_STATUS_IS_OK(status)) {
1287 talloc_free(tmp_ctx);
1288 return ldb_next_request(module, req);
1290 /* what about delete child on the current parent */
1291 ret = dsdb_module_check_access_on_dn(module, req, oldparent, SEC_ADS_DELETE_CHILD, NULL, req);
1292 if (ret != LDB_SUCCESS) {
1293 DEBUG(10,("acl:access_denied renaming %s", ldb_dn_get_linearized(req->op.rename.olddn)));
1294 talloc_free(tmp_ctx);
1295 return ldb_module_done(req, NULL, NULL, ret);
1298 talloc_free(tmp_ctx);
1300 return ldb_next_request(module, req);
1303 static int acl_search_callback(struct ldb_request *req, struct ldb_reply *ares)
1305 struct ldb_context *ldb;
1306 struct acl_context *ac;
1307 struct acl_private *data;
1308 struct ldb_result *acl_res;
1309 static const char *acl_attrs[] = {
1310 "objectClass",
1311 "nTSecurityDescriptor",
1312 "objectSid",
1313 NULL
1315 int ret;
1316 unsigned int i;
1318 ac = talloc_get_type(req->context, struct acl_context);
1319 data = talloc_get_type(ldb_module_get_private(ac->module), struct acl_private);
1320 ldb = ldb_module_get_ctx(ac->module);
1322 if (!ares) {
1323 return ldb_module_done(ac->req, NULL, NULL,
1324 LDB_ERR_OPERATIONS_ERROR);
1326 if (ares->error != LDB_SUCCESS) {
1327 return ldb_module_done(ac->req, ares->controls,
1328 ares->response, ares->error);
1331 switch (ares->type) {
1332 case LDB_REPLY_ENTRY:
1333 if (ac->allowedAttributes
1334 || ac->allowedChildClasses
1335 || ac->allowedChildClassesEffective
1336 || ac->allowedAttributesEffective
1337 || ac->sDRightsEffective) {
1338 ret = dsdb_module_search_dn(ac->module, ac, &acl_res, ares->message->dn,
1339 acl_attrs,
1340 DSDB_FLAG_NEXT_MODULE, req);
1341 if (ret != LDB_SUCCESS) {
1342 return ldb_module_done(ac->req, NULL, NULL, ret);
1344 if (ac->allowedAttributes || ac->allowedAttributesEffective) {
1345 ret = acl_allowedAttributes(ac->module, ac->schema, acl_res->msgs[0], ares->message, ac);
1346 if (ret != LDB_SUCCESS) {
1347 return ldb_module_done(ac->req, NULL, NULL, ret);
1350 if (ac->allowedChildClasses) {
1351 ret = acl_childClasses(ac->module, ac->schema, acl_res->msgs[0],
1352 ares->message, "allowedChildClasses");
1353 if (ret != LDB_SUCCESS) {
1354 return ldb_module_done(ac->req, NULL, NULL, ret);
1357 if (ac->allowedChildClassesEffective) {
1358 ret = acl_childClassesEffective(ac->module, ac->schema,
1359 acl_res->msgs[0], ares->message, ac);
1360 if (ret != LDB_SUCCESS) {
1361 return ldb_module_done(ac->req, NULL, NULL, ret);
1364 if (ac->sDRightsEffective) {
1365 ret = acl_sDRightsEffective(ac->module,
1366 acl_res->msgs[0], ares->message, ac);
1367 if (ret != LDB_SUCCESS) {
1368 return ldb_module_done(ac->req, NULL, NULL, ret);
1372 if (data && data->password_attrs) {
1373 if (!ac->am_system) {
1374 for (i = 0; data->password_attrs[i]; i++) {
1375 if ((!ac->userPassword) &&
1376 (ldb_attr_cmp(data->password_attrs[i],
1377 "userPassword") == 0))
1378 continue;
1380 ldb_msg_remove_attr(ares->message, data->password_attrs[i]);
1384 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
1386 case LDB_REPLY_REFERRAL:
1387 return ldb_module_send_referral(ac->req, ares->referral);
1389 case LDB_REPLY_DONE:
1390 return ldb_module_done(ac->req, ares->controls,
1391 ares->response, LDB_SUCCESS);
1394 return LDB_SUCCESS;
1397 static int acl_search(struct ldb_module *module, struct ldb_request *req)
1399 struct ldb_context *ldb;
1400 struct acl_context *ac;
1401 struct ldb_request *down_req;
1402 struct acl_private *data;
1403 int ret;
1404 unsigned int i;
1406 ldb = ldb_module_get_ctx(module);
1408 ac = talloc_zero(req, struct acl_context);
1409 if (ac == NULL) {
1410 return ldb_oom(ldb);
1412 data = talloc_get_type(ldb_module_get_private(module), struct acl_private);
1414 ac->module = module;
1415 ac->req = req;
1416 ac->am_system = dsdb_module_am_system(module);
1417 ac->allowedAttributes = ldb_attr_in_list(req->op.search.attrs, "allowedAttributes");
1418 ac->allowedAttributesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedAttributesEffective");
1419 ac->allowedChildClasses = ldb_attr_in_list(req->op.search.attrs, "allowedChildClasses");
1420 ac->allowedChildClassesEffective = ldb_attr_in_list(req->op.search.attrs, "allowedChildClassesEffective");
1421 ac->sDRightsEffective = ldb_attr_in_list(req->op.search.attrs, "sDRightsEffective");
1422 ac->userPassword = dsdb_user_password_support(module, ac, req);
1423 ac->schema = dsdb_get_schema(ldb, ac);
1425 /* replace any attributes in the parse tree that are private,
1426 so we don't allow a search for 'userPassword=penguin',
1427 just as we would not allow that attribute to be returned */
1428 if (ac->am_system) {
1429 /* FIXME: We should copy the tree and keep the original unmodified. */
1430 /* remove password attributes */
1431 if (data && data->password_attrs) {
1432 for (i = 0; data->password_attrs[i]; i++) {
1433 if ((!ac->userPassword) &&
1434 (ldb_attr_cmp(data->password_attrs[i],
1435 "userPassword") == 0))
1436 continue;
1438 ldb_parse_tree_attr_replace(req->op.search.tree,
1439 data->password_attrs[i],
1440 "kludgeACLredactedattribute");
1444 ret = ldb_build_search_req_ex(&down_req,
1445 ldb, ac,
1446 req->op.search.base,
1447 req->op.search.scope,
1448 req->op.search.tree,
1449 req->op.search.attrs,
1450 req->controls,
1451 ac, acl_search_callback,
1452 req);
1453 LDB_REQ_SET_LOCATION(down_req);
1454 if (ret != LDB_SUCCESS) {
1455 return ret;
1457 /* perform the search */
1458 return ldb_next_request(module, down_req);
1461 static int acl_extended(struct ldb_module *module, struct ldb_request *req)
1463 struct ldb_context *ldb = ldb_module_get_ctx(module);
1464 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
1466 /* allow everybody to read the sequence number */
1467 if (strcmp(req->op.extended.oid,
1468 LDB_EXTENDED_SEQUENCE_NUMBER) == 0) {
1469 return ldb_next_request(module, req);
1472 if (dsdb_module_am_system(module) ||
1473 dsdb_module_am_administrator(module) || as_system) {
1474 return ldb_next_request(module, req);
1475 } else {
1476 ldb_asprintf_errstring(ldb,
1477 "acl_extended: "
1478 "attempted database modify not permitted. "
1479 "User %s is not SYSTEM or an administrator",
1480 acl_user_name(req, module));
1481 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
1485 static const struct ldb_module_ops ldb_acl_module_ops = {
1486 .name = "acl",
1487 .search = acl_search,
1488 .add = acl_add,
1489 .modify = acl_modify,
1490 .del = acl_delete,
1491 .rename = acl_rename,
1492 .extended = acl_extended,
1493 .init_context = acl_module_init
1496 int ldb_acl_module_init(const char *version)
1498 LDB_MODULE_CHECK_VERSION(version);
1499 return ldb_register_module(&ldb_acl_module_ops);