4 Copyright (C) Simo Sorce 2006-2008
5 Copyright (C) Nadezhda Ivanova 2009
6 Copyright (C) Anatoliy Atanasov 2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * Component: ldb ACL module
27 * Description: Module that performs authorisation access checks based on the
28 * account's security context and the DACL of the object being polled.
29 * Only DACL checks implemented at this point
31 * Authors: Nadezhda Ivanova, Anatoliy Atanasov
35 #include "ldb_module.h"
36 #include "auth/auth.h"
37 #include "libcli/security/security.h"
38 #include "dsdb/samdb/samdb.h"
39 #include "librpc/gen_ndr/ndr_security.h"
40 #include "param/param.h"
41 #include "dsdb/samdb/ldb_modules/util.h"
42 #include "lib/util/tsort.h"
43 #include "system/kerberos.h"
44 #include "auth/kerberos/kerberos.h"
46 struct extended_access_check_attribute
{
48 const uint32_t requires_rights
;
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
;
61 struct ldb_module
*module
;
62 struct ldb_request
*req
;
64 bool am_administrator
;
66 bool constructed_attrs
;
67 bool allowedAttributes
;
68 bool allowedAttributesEffective
;
69 bool allowedChildClasses
;
70 bool allowedChildClassesEffective
;
71 bool sDRightsEffective
;
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
;
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
);
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
);
115 ret
= dsdb_module_search_dn(module
, mem_ctx
, &res
,
116 ldb_dn_new(mem_ctx
, ldb
, "@KLUDGEACL"),
118 DSDB_FLAG_NEXT_MODULE
|
121 if (ret
!= LDB_SUCCESS
) {
124 if (res
->count
== 0) {
128 if (res
->count
> 1) {
129 talloc_free(mem_ctx
);
130 return LDB_ERR_CONSTRAINT_VIOLATION
;
135 password_attributes
= ldb_msg_find_element(msg
, "passwordAttribute");
136 if (!password_attributes
) {
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
);
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
);
154 for (i
=0; i
< ARRAY_SIZE(secret_attrs
); i
++) {
157 for (j
=0; j
< n
; j
++) {
158 if (strcasecmp(data
->password_attrs
[j
], secret_attrs
[i
]) == 0) {
168 data
->password_attrs
[n
] = talloc_strdup(data
->password_attrs
,
170 if (data
->password_attrs
[n
] == NULL
) {
171 talloc_free(mem_ctx
);
176 data
->password_attrs
[n
] = NULL
;
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
);
192 const char **attr_list
;
194 const struct dsdb_class
*objectclass
;
196 /* If we don't have a schema yet, we can't do anything... */
197 if (schema
== NULL
) {
198 ldb_asprintf_errstring(ldb
, "cannot add allowedAttributes to %s because no schema is loaded", ldb_dn_get_linearized(msg
->dn
));
199 return LDB_ERR_OPERATIONS_ERROR
;
202 /* Must remove any existing attribute */
203 if (ac
->allowedAttributes
) {
204 ldb_msg_remove_attr(msg
, "allowedAttributes");
207 mem_ctx
= talloc_new(msg
);
212 oc_el
= ldb_msg_find_element(sd_msg
, "objectClass");
213 attr_list
= dsdb_full_attribute_list(mem_ctx
, schema
, oc_el
, DSDB_SCHEMA_ALL
);
215 ldb_asprintf_errstring(ldb
, "acl: Failed to get list of attributes");
216 talloc_free(mem_ctx
);
217 return LDB_ERR_OPERATIONS_ERROR
;
221 * Get the top-most structural object class for the ACL check
223 objectclass
= dsdb_get_last_structural_class(ac
->schema
,
225 if (objectclass
== NULL
) {
226 ldb_asprintf_errstring(ldb
, "acl_read: Failed to find a structural class for %s",
227 ldb_dn_get_linearized(sd_msg
->dn
));
228 talloc_free(mem_ctx
);
229 return LDB_ERR_OPERATIONS_ERROR
;
232 if (ac
->allowedAttributes
) {
233 for (i
=0; attr_list
&& attr_list
[i
]; i
++) {
234 ldb_msg_add_string(msg
, "allowedAttributes", attr_list
[i
]);
237 if (ac
->allowedAttributesEffective
) {
238 struct security_descriptor
*sd
;
239 struct dom_sid
*sid
= NULL
;
240 struct ldb_control
*as_system
= ldb_request_get_control(ac
->req
,
241 LDB_CONTROL_AS_SYSTEM_OID
);
243 if (as_system
!= NULL
) {
244 as_system
->critical
= 0;
247 ldb_msg_remove_attr(msg
, "allowedAttributesEffective");
248 if (ac
->am_system
|| as_system
) {
249 for (i
=0; attr_list
&& attr_list
[i
]; i
++) {
250 ldb_msg_add_string(msg
, "allowedAttributesEffective", attr_list
[i
]);
255 ret
= dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module
), mem_ctx
, sd_msg
, &sd
);
257 if (ret
!= LDB_SUCCESS
) {
261 sid
= samdb_result_dom_sid(mem_ctx
, sd_msg
, "objectSid");
262 for (i
=0; attr_list
&& attr_list
[i
]; i
++) {
263 const struct dsdb_attribute
*attr
= dsdb_attribute_by_lDAPDisplayName(schema
,
266 return ldb_operr(ldb
);
268 /* remove constructed attributes */
269 if (attr
->systemFlags
& DS_FLAG_ATTR_IS_CONSTRUCTED
271 || (attr
->linkID
!= 0 && attr
->linkID
% 2 != 0 )) {
274 ret
= acl_check_access_on_attribute(module
,
281 if (ret
== LDB_SUCCESS
) {
282 ldb_msg_add_string(msg
, "allowedAttributesEffective", attr_list
[i
]);
289 static int acl_childClasses(struct ldb_module
*module
,
290 const struct dsdb_schema
*schema
,
291 struct ldb_message
*sd_msg
,
292 struct ldb_message
*msg
,
293 const char *attrName
)
295 struct ldb_message_element
*oc_el
;
296 struct ldb_message_element
*allowedClasses
;
297 const struct dsdb_class
*sclass
;
301 /* If we don't have a schema yet, we can't do anything... */
302 if (schema
== NULL
) {
303 ldb_asprintf_errstring(ldb_module_get_ctx(module
), "cannot add childClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg
->dn
));
304 return LDB_ERR_OPERATIONS_ERROR
;
307 /* Must remove any existing attribute, or else confusion reins */
308 ldb_msg_remove_attr(msg
, attrName
);
309 ret
= ldb_msg_add_empty(msg
, attrName
, 0, &allowedClasses
);
310 if (ret
!= LDB_SUCCESS
) {
314 oc_el
= ldb_msg_find_element(sd_msg
, "objectClass");
316 for (i
=0; oc_el
&& i
< oc_el
->num_values
; i
++) {
317 sclass
= dsdb_class_by_lDAPDisplayName_ldb_val(schema
, &oc_el
->values
[i
]);
319 /* We don't know this class? what is going on? */
323 for (j
=0; sclass
->possibleInferiors
&& sclass
->possibleInferiors
[j
]; j
++) {
324 ldb_msg_add_string(msg
, attrName
, sclass
->possibleInferiors
[j
]);
327 if (allowedClasses
->num_values
> 1) {
328 TYPESAFE_QSORT(allowedClasses
->values
, allowedClasses
->num_values
, data_blob_cmp
);
329 for (i
=1 ; i
< allowedClasses
->num_values
; i
++) {
330 struct ldb_val
*val1
= &allowedClasses
->values
[i
-1];
331 struct ldb_val
*val2
= &allowedClasses
->values
[i
];
332 if (data_blob_cmp(val1
, val2
) == 0) {
333 memmove(val1
, val2
, (allowedClasses
->num_values
- i
) * sizeof(struct ldb_val
));
334 allowedClasses
->num_values
--;
343 static int acl_childClassesEffective(struct ldb_module
*module
,
344 const struct dsdb_schema
*schema
,
345 struct ldb_message
*sd_msg
,
346 struct ldb_message
*msg
,
347 struct acl_context
*ac
)
349 struct ldb_message_element
*oc_el
;
350 struct ldb_message_element
*allowedClasses
= NULL
;
351 const struct dsdb_class
*sclass
;
352 struct security_descriptor
*sd
;
353 struct ldb_control
*as_system
= ldb_request_get_control(ac
->req
,
354 LDB_CONTROL_AS_SYSTEM_OID
);
355 struct dom_sid
*sid
= NULL
;
359 if (as_system
!= NULL
) {
360 as_system
->critical
= 0;
363 if (ac
->am_system
|| as_system
) {
364 return acl_childClasses(module
, schema
, sd_msg
, msg
, "allowedChildClassesEffective");
367 /* If we don't have a schema yet, we can't do anything... */
368 if (schema
== NULL
) {
369 ldb_asprintf_errstring(ldb_module_get_ctx(module
), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg
->dn
));
370 return LDB_ERR_OPERATIONS_ERROR
;
373 /* Must remove any existing attribute, or else confusion reins */
374 ldb_msg_remove_attr(msg
, "allowedChildClassesEffective");
376 oc_el
= ldb_msg_find_element(sd_msg
, "objectClass");
377 ret
= dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module
), msg
, sd_msg
, &sd
);
378 if (ret
!= LDB_SUCCESS
) {
382 sid
= samdb_result_dom_sid(msg
, sd_msg
, "objectSid");
383 for (i
=0; oc_el
&& i
< oc_el
->num_values
; i
++) {
384 sclass
= dsdb_class_by_lDAPDisplayName_ldb_val(schema
, &oc_el
->values
[i
]);
386 /* We don't know this class? what is going on? */
390 for (j
=0; sclass
->possibleInferiors
&& sclass
->possibleInferiors
[j
]; j
++) {
391 const struct dsdb_class
*sc
;
393 sc
= dsdb_class_by_lDAPDisplayName(schema
,
394 sclass
->possibleInferiors
[j
]);
396 /* We don't know this class? what is going on? */
400 ret
= acl_check_access_on_objectclass(module
, ac
,
402 SEC_ADS_CREATE_CHILD
,
404 if (ret
== LDB_SUCCESS
) {
405 ldb_msg_add_string(msg
, "allowedChildClassesEffective",
406 sclass
->possibleInferiors
[j
]);
410 allowedClasses
= ldb_msg_find_element(msg
, "allowedChildClassesEffective");
411 if (!allowedClasses
) {
415 if (allowedClasses
->num_values
> 1) {
416 TYPESAFE_QSORT(allowedClasses
->values
, allowedClasses
->num_values
, data_blob_cmp
);
417 for (i
=1 ; i
< allowedClasses
->num_values
; i
++) {
418 struct ldb_val
*val1
= &allowedClasses
->values
[i
-1];
419 struct ldb_val
*val2
= &allowedClasses
->values
[i
];
420 if (data_blob_cmp(val1
, val2
) == 0) {
421 memmove(val1
, val2
, (allowedClasses
->num_values
- i
) * sizeof( struct ldb_val
));
422 allowedClasses
->num_values
--;
430 static int acl_sDRightsEffective(struct ldb_module
*module
,
431 struct ldb_message
*sd_msg
,
432 struct ldb_message
*msg
,
433 struct acl_context
*ac
)
435 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
436 struct ldb_message_element
*rightsEffective
;
438 struct security_descriptor
*sd
;
439 struct ldb_control
*as_system
= ldb_request_get_control(ac
->req
,
440 LDB_CONTROL_AS_SYSTEM_OID
);
441 struct dom_sid
*sid
= NULL
;
444 if (as_system
!= NULL
) {
445 as_system
->critical
= 0;
448 /* Must remove any existing attribute, or else confusion reins */
449 ldb_msg_remove_attr(msg
, "sDRightsEffective");
450 ret
= ldb_msg_add_empty(msg
, "sDRightsEffective", 0, &rightsEffective
);
451 if (ret
!= LDB_SUCCESS
) {
454 if (ac
->am_system
|| as_system
) {
455 flags
= SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_SACL
| SECINFO_DACL
;
457 const struct dsdb_class
*objectclass
;
458 const struct dsdb_attribute
*attr
;
460 objectclass
= dsdb_get_structural_oc_from_msg(ac
->schema
, sd_msg
);
461 if (objectclass
== NULL
) {
462 return ldb_operr(ldb
);
465 attr
= dsdb_attribute_by_lDAPDisplayName(ac
->schema
,
466 "nTSecurityDescriptor");
468 return ldb_operr(ldb
);
471 /* Get the security descriptor from the message */
472 ret
= dsdb_get_sd_from_ldb_message(ldb
, msg
, sd_msg
, &sd
);
473 if (ret
!= LDB_SUCCESS
) {
476 sid
= samdb_result_dom_sid(msg
, sd_msg
, "objectSid");
477 ret
= acl_check_access_on_attribute(module
,
484 if (ret
== LDB_SUCCESS
) {
485 flags
|= SECINFO_OWNER
| SECINFO_GROUP
;
487 ret
= acl_check_access_on_attribute(module
,
494 if (ret
== LDB_SUCCESS
) {
495 flags
|= SECINFO_DACL
;
497 ret
= acl_check_access_on_attribute(module
,
501 SEC_FLAG_SYSTEM_SECURITY
,
504 if (ret
== LDB_SUCCESS
) {
505 flags
|= SECINFO_SACL
;
508 return samdb_msg_add_uint(ldb_module_get_ctx(module
), msg
, msg
,
509 "sDRightsEffective", flags
);
512 static int acl_validate_spn_value(TALLOC_CTX
*mem_ctx
,
513 struct ldb_context
*ldb
,
514 const char *spn_value
,
515 uint32_t userAccountControl
,
516 const char *samAccountName
,
517 const char *dnsHostName
,
518 const char *netbios_name
,
519 const char *ntds_guid
)
522 krb5_context krb_ctx
;
523 krb5_error_code kerr
;
524 krb5_principal principal
;
528 const char *forest_name
= samdb_forest_name(ldb
, mem_ctx
);
529 const char *base_domain
= samdb_default_domain_name(ldb
, mem_ctx
);
530 struct loadparm_context
*lp_ctx
= talloc_get_type(ldb_get_opaque(ldb
, "loadparm"),
531 struct loadparm_context
);
532 bool is_dc
= (userAccountControl
& UF_SERVER_TRUST_ACCOUNT
) ||
533 (userAccountControl
& UF_PARTIAL_SECRETS_ACCOUNT
);
535 if (strcasecmp_m(spn_value
, samAccountName
) == 0) {
536 /* MacOS X sets this value, and setting an SPN of your
537 * own samAccountName is both pointless and safe */
541 kerr
= smb_krb5_init_context_basic(mem_ctx
,
545 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
546 "Could not initialize kerberos context.");
549 ret
= krb5_parse_name(krb_ctx
, spn_value
, &principal
);
551 krb5_free_context(krb_ctx
);
552 return LDB_ERR_CONSTRAINT_VIOLATION
;
555 if (principal
->name
.name_string
.len
< 2) {
559 instanceName
= principal
->name
.name_string
.val
[1];
560 serviceType
= principal
->name
.name_string
.val
[0];
561 if (principal
->name
.name_string
.len
== 3) {
562 serviceName
= principal
->name
.name_string
.val
[2];
571 if (strcasecmp(serviceType
, "ldap") == 0) {
572 if (strcasecmp(serviceName
, netbios_name
) != 0 &&
573 strcasecmp(serviceName
, forest_name
) != 0) {
577 } else if (strcasecmp(serviceType
, "gc") == 0) {
578 if (strcasecmp(serviceName
, forest_name
) != 0) {
582 if (strcasecmp(serviceName
, base_domain
) != 0 &&
583 strcasecmp(serviceName
, netbios_name
) != 0) {
588 /* instanceName can be samAccountName without $ or dnsHostName
589 * or "ntds_guid._msdcs.forest_domain for DC objects */
590 if (strlen(instanceName
) == (strlen(samAccountName
) - 1)
591 && strncasecmp(instanceName
, samAccountName
, strlen(samAccountName
) - 1) == 0) {
593 } else if (dnsHostName
!= NULL
&& strcasecmp(instanceName
, dnsHostName
) == 0) {
596 const char *guid_str
;
597 guid_str
= talloc_asprintf(mem_ctx
,"%s._msdcs.%s",
600 if (strcasecmp(instanceName
, guid_str
) == 0) {
606 krb5_free_principal(krb_ctx
, principal
);
607 krb5_free_context(krb_ctx
);
608 return LDB_ERR_CONSTRAINT_VIOLATION
;
611 krb5_free_principal(krb_ctx
, principal
);
612 krb5_free_context(krb_ctx
);
616 static int acl_check_spn(TALLOC_CTX
*mem_ctx
,
617 struct ldb_module
*module
,
618 struct ldb_request
*req
,
619 struct security_descriptor
*sd
,
621 const struct dsdb_attribute
*attr
,
622 const struct dsdb_class
*objectclass
)
626 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
627 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
628 struct ldb_result
*acl_res
;
629 struct ldb_result
*netbios_res
;
630 struct ldb_message_element
*el
;
631 struct ldb_dn
*partitions_dn
= samdb_partitions_dn(ldb
, tmp_ctx
);
632 uint32_t userAccountControl
;
633 const char *samAccountName
;
634 const char *dnsHostName
;
635 const char *netbios_name
;
637 char *ntds_guid
= NULL
;
639 static const char *acl_attrs
[] = {
642 "userAccountControl",
645 static const char *netbios_attrs
[] = {
650 /* if we have wp, we can do whatever we like */
651 if (acl_check_access_on_attribute(module
,
656 attr
, objectclass
) == LDB_SUCCESS
) {
657 talloc_free(tmp_ctx
);
661 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
662 GUID_DRS_VALIDATE_SPN
,
666 if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
667 dsdb_acl_debug(sd
, acl_user_token(module
),
668 req
->op
.mod
.message
->dn
,
671 talloc_free(tmp_ctx
);
675 ret
= dsdb_module_search_dn(module
, tmp_ctx
,
676 &acl_res
, req
->op
.mod
.message
->dn
,
678 DSDB_FLAG_NEXT_MODULE
|
679 DSDB_FLAG_AS_SYSTEM
|
680 DSDB_SEARCH_SHOW_RECYCLED
,
682 if (ret
!= LDB_SUCCESS
) {
683 talloc_free(tmp_ctx
);
687 userAccountControl
= ldb_msg_find_attr_as_uint(acl_res
->msgs
[0], "userAccountControl", 0);
688 dnsHostName
= ldb_msg_find_attr_as_string(acl_res
->msgs
[0], "dnsHostName", NULL
);
689 samAccountName
= ldb_msg_find_attr_as_string(acl_res
->msgs
[0], "samAccountName", NULL
);
691 ret
= dsdb_module_search(module
, tmp_ctx
,
692 &netbios_res
, partitions_dn
,
695 DSDB_FLAG_NEXT_MODULE
|
699 ldb_dn_get_linearized(ldb_get_default_basedn(ldb
)));
701 netbios_name
= ldb_msg_find_attr_as_string(netbios_res
->msgs
[0], "nETBIOSName", NULL
);
703 el
= ldb_msg_find_element(req
->op
.mod
.message
, "servicePrincipalName");
705 talloc_free(tmp_ctx
);
706 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
707 "Error finding element for servicePrincipalName.");
710 /* NTDSDSA objectGuid of object we are checking SPN for */
711 if (userAccountControl
& (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
)) {
712 ret
= dsdb_module_find_ntdsguid_for_computer(module
, tmp_ctx
,
713 req
->op
.mod
.message
->dn
, &ntds
, req
);
714 if (ret
!= LDB_SUCCESS
) {
715 ldb_asprintf_errstring(ldb
, "Failed to find NTDSDSA objectGuid for %s: %s",
716 ldb_dn_get_linearized(req
->op
.mod
.message
->dn
),
718 talloc_free(tmp_ctx
);
719 return LDB_ERR_OPERATIONS_ERROR
;
721 ntds_guid
= GUID_string(tmp_ctx
, &ntds
);
724 for (i
=0; i
< el
->num_values
; i
++) {
725 ret
= acl_validate_spn_value(tmp_ctx
,
727 (char *)el
->values
[i
].data
,
733 if (ret
!= LDB_SUCCESS
) {
734 talloc_free(tmp_ctx
);
738 talloc_free(tmp_ctx
);
742 static int acl_add(struct ldb_module
*module
, struct ldb_request
*req
)
745 struct ldb_dn
*parent
;
746 struct ldb_context
*ldb
;
747 const struct dsdb_schema
*schema
;
748 const struct dsdb_class
*objectclass
;
749 struct ldb_dn
*nc_root
;
750 struct ldb_control
*as_system
;
752 if (ldb_dn_is_special(req
->op
.add
.message
->dn
)) {
753 return ldb_next_request(module
, req
);
756 as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
757 if (as_system
!= NULL
) {
758 as_system
->critical
= 0;
761 if (dsdb_module_am_system(module
) || as_system
) {
762 return ldb_next_request(module
, req
);
765 ldb
= ldb_module_get_ctx(module
);
767 parent
= ldb_dn_get_parent(req
, req
->op
.add
.message
->dn
);
768 if (parent
== NULL
) {
772 /* Creating an NC. There is probably something we should do here,
773 * but we will establish that later */
775 ret
= dsdb_find_nc_root(ldb
, req
, req
->op
.add
.message
->dn
, &nc_root
);
776 if (ret
!= LDB_SUCCESS
) {
779 if (ldb_dn_compare(nc_root
, req
->op
.add
.message
->dn
) == 0) {
780 talloc_free(nc_root
);
781 return ldb_next_request(module
, req
);
783 talloc_free(nc_root
);
785 schema
= dsdb_get_schema(ldb
, req
);
787 return ldb_operr(ldb
);
790 objectclass
= dsdb_get_structural_oc_from_msg(schema
, req
->op
.add
.message
);
792 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
793 "acl: unable to find or validate structrual objectClass on %s\n",
794 ldb_dn_get_linearized(req
->op
.add
.message
->dn
));
795 return ldb_module_done(req
, NULL
, NULL
, LDB_ERR_OPERATIONS_ERROR
);
798 ret
= dsdb_module_check_access_on_dn(module
, req
, parent
,
799 SEC_ADS_CREATE_CHILD
,
800 &objectclass
->schemaIDGUID
, req
);
801 if (ret
!= LDB_SUCCESS
) {
804 return ldb_next_request(module
, req
);
807 /* ckecks if modifications are allowed on "Member" attribute */
808 static int acl_check_self_membership(TALLOC_CTX
*mem_ctx
,
809 struct ldb_module
*module
,
810 struct ldb_request
*req
,
811 struct security_descriptor
*sd
,
813 const struct dsdb_attribute
*attr
,
814 const struct dsdb_class
*objectclass
)
818 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
819 struct ldb_dn
*user_dn
;
820 struct ldb_message_element
*member_el
;
821 /* if we have wp, we can do whatever we like */
822 if (acl_check_access_on_attribute(module
,
827 attr
, objectclass
) == LDB_SUCCESS
) {
830 /* if we are adding/deleting ourselves, check for self membership */
831 ret
= dsdb_find_dn_by_sid(ldb
, mem_ctx
,
832 &acl_user_token(module
)->sids
[PRIMARY_USER_SID_INDEX
],
834 if (ret
!= LDB_SUCCESS
) {
837 member_el
= ldb_msg_find_element(req
->op
.mod
.message
, "member");
839 return ldb_operr(ldb
);
841 /* user can only remove oneself */
842 if (member_el
->num_values
== 0) {
843 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
845 for (i
= 0; i
< member_el
->num_values
; i
++) {
846 if (strcasecmp((const char *)member_el
->values
[i
].data
,
847 ldb_dn_get_extended_linearized(mem_ctx
, user_dn
, 1)) != 0) {
848 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
851 ret
= acl_check_extended_right(mem_ctx
, sd
, acl_user_token(module
),
852 GUID_DRS_SELF_MEMBERSHIP
,
855 if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
856 dsdb_acl_debug(sd
, acl_user_token(module
),
857 req
->op
.mod
.message
->dn
,
864 static int acl_check_password_rights(TALLOC_CTX
*mem_ctx
,
865 struct ldb_module
*module
,
866 struct ldb_request
*req
,
867 struct security_descriptor
*sd
,
869 const struct dsdb_class
*objectclass
,
872 int ret
= LDB_SUCCESS
;
873 unsigned int del_attr_cnt
= 0, add_attr_cnt
= 0, rep_attr_cnt
= 0;
874 struct ldb_message_element
*el
;
875 struct ldb_message
*msg
;
876 const char *passwordAttrs
[] = { "userPassword", "clearTextPassword",
877 "unicodePwd", "dBCSPwd", NULL
}, **l
;
878 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
880 msg
= ldb_msg_copy_shallow(tmp_ctx
, req
->op
.mod
.message
);
882 return ldb_module_oom(module
);
884 for (l
= passwordAttrs
; *l
!= NULL
; l
++) {
885 if ((!userPassword
) && (ldb_attr_cmp(*l
, "userPassword") == 0)) {
889 while ((el
= ldb_msg_find_element(msg
, *l
)) != NULL
) {
890 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_DELETE
) {
893 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_ADD
) {
896 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_REPLACE
) {
899 ldb_msg_remove_element(msg
, el
);
903 /* single deletes will be handled by the "password_hash" LDB module
904 * later in the stack, so we let it though here */
905 if ((del_attr_cnt
> 0) && (add_attr_cnt
== 0) && (rep_attr_cnt
== 0)) {
906 talloc_free(tmp_ctx
);
910 if (ldb_request_get_control(req
,
911 DSDB_CONTROL_PASSWORD_CHANGE_OID
) != NULL
) {
912 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
913 * have a user password change and not a set as the message
914 * looks like. In it's value blob it contains the NT and/or LM
915 * hash of the old password specified by the user.
916 * This control is used by the SAMR and "kpasswd" password
917 * change mechanisms. */
918 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
919 GUID_DRS_USER_CHANGE_PASSWORD
,
920 SEC_ADS_CONTROL_ACCESS
,
923 else if (rep_attr_cnt
> 0 || (add_attr_cnt
!= del_attr_cnt
)) {
924 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
925 GUID_DRS_FORCE_CHANGE_PASSWORD
,
926 SEC_ADS_CONTROL_ACCESS
,
929 else if (add_attr_cnt
== 1 && del_attr_cnt
== 1) {
930 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
931 GUID_DRS_USER_CHANGE_PASSWORD
,
932 SEC_ADS_CONTROL_ACCESS
,
934 /* Very strange, but we get constraint violation in this case */
935 if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
936 ret
= LDB_ERR_CONSTRAINT_VIOLATION
;
939 if (ret
!= LDB_SUCCESS
) {
940 dsdb_acl_debug(sd
, acl_user_token(module
),
941 req
->op
.mod
.message
->dn
,
945 talloc_free(tmp_ctx
);
950 static int acl_modify(struct ldb_module
*module
, struct ldb_request
*req
)
953 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
954 const struct dsdb_schema
*schema
;
956 const struct dsdb_class
*objectclass
;
957 uint32_t access_granted
;
959 struct ldb_result
*acl_res
;
960 struct security_descriptor
*sd
;
961 struct dom_sid
*sid
= NULL
;
962 struct ldb_control
*as_system
;
965 const struct ldb_message
*msg
= req
->op
.mod
.message
;
966 static const char *acl_attrs
[] = {
967 "nTSecurityDescriptor",
973 if (ldb_dn_is_special(msg
->dn
)) {
974 return ldb_next_request(module
, req
);
977 as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
978 if (as_system
!= NULL
) {
979 as_system
->critical
= 0;
982 /* Don't print this debug statement if elements[0].name is going to be NULL */
983 if (msg
->num_elements
> 0) {
984 DEBUG(10, ("ldb:acl_modify: %s\n", msg
->elements
[0].name
));
986 if (dsdb_module_am_system(module
) || as_system
) {
987 return ldb_next_request(module
, req
);
990 tmp_ctx
= talloc_new(req
);
991 if (tmp_ctx
== NULL
) {
995 ret
= dsdb_module_search_dn(module
, tmp_ctx
, &acl_res
, msg
->dn
,
997 DSDB_FLAG_NEXT_MODULE
|
998 DSDB_FLAG_AS_SYSTEM
|
999 DSDB_SEARCH_SHOW_RECYCLED
,
1002 if (ret
!= LDB_SUCCESS
) {
1006 userPassword
= dsdb_user_password_support(module
, req
, req
);
1008 schema
= dsdb_get_schema(ldb
, tmp_ctx
);
1010 talloc_free(tmp_ctx
);
1011 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1012 "acl_modify: Error obtaining schema.");
1015 ret
= dsdb_get_sd_from_ldb_message(ldb
, tmp_ctx
, acl_res
->msgs
[0], &sd
);
1016 if (ret
!= LDB_SUCCESS
) {
1017 talloc_free(tmp_ctx
);
1018 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1019 "acl_modify: Error retrieving security descriptor.");
1021 /* Theoretically we pass the check if the object has no sd */
1026 objectclass
= dsdb_get_structural_oc_from_msg(schema
, acl_res
->msgs
[0]);
1028 talloc_free(tmp_ctx
);
1029 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1030 "acl_modify: Error retrieving object class for GUID.");
1032 sid
= samdb_result_dom_sid(req
, acl_res
->msgs
[0], "objectSid");
1033 for (i
=0; i
< msg
->num_elements
; i
++) {
1034 const struct ldb_message_element
*el
= &msg
->elements
[i
];
1035 const struct dsdb_attribute
*attr
;
1038 * This basic attribute existence check with the right errorcode
1039 * is needed since this module is the first one which requests
1040 * schema attribute information.
1041 * The complete attribute checking is done in the
1042 * "objectclass_attrs" module behind this one.
1044 * NOTE: "clearTextPassword" is not defined in the schema.
1046 attr
= dsdb_attribute_by_lDAPDisplayName(schema
, el
->name
);
1047 if (!attr
&& ldb_attr_cmp("clearTextPassword", el
->name
) != 0) {
1048 ldb_asprintf_errstring(ldb
, "acl_modify: attribute '%s' "
1049 "on entry '%s' was not found in the schema!",
1050 req
->op
.mod
.message
->elements
[i
].name
,
1051 ldb_dn_get_linearized(req
->op
.mod
.message
->dn
));
1052 ret
= LDB_ERR_NO_SUCH_ATTRIBUTE
;
1056 if (ldb_attr_cmp("nTSecurityDescriptor", el
->name
) == 0) {
1057 uint32_t sd_flags
= dsdb_request_sd_flags(req
, NULL
);
1058 uint32_t access_mask
= 0;
1060 if (sd_flags
& (SECINFO_OWNER
|SECINFO_GROUP
)) {
1061 access_mask
|= SEC_STD_WRITE_OWNER
;
1063 if (sd_flags
& SECINFO_DACL
) {
1064 access_mask
|= SEC_STD_WRITE_DAC
;
1066 if (sd_flags
& SECINFO_SACL
) {
1067 access_mask
|= SEC_FLAG_SYSTEM_SECURITY
;
1070 status
= sec_access_check_ds(sd
, acl_user_token(module
),
1076 if (!NT_STATUS_IS_OK(status
)) {
1077 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1078 "Object %s has no write dacl access\n",
1079 ldb_dn_get_linearized(msg
->dn
));
1081 acl_user_token(module
),
1085 ret
= LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1088 } else if (ldb_attr_cmp("member", el
->name
) == 0) {
1089 ret
= acl_check_self_membership(tmp_ctx
,
1096 if (ret
!= LDB_SUCCESS
) {
1099 } else if (ldb_attr_cmp("dBCSPwd", el
->name
) == 0) {
1100 /* this one is not affected by any rights, we should let it through
1101 so that passwords_hash returns the correct error */
1103 } else if (ldb_attr_cmp("unicodePwd", el
->name
) == 0 ||
1104 (userPassword
&& ldb_attr_cmp("userPassword", el
->name
) == 0) ||
1105 ldb_attr_cmp("clearTextPassword", el
->name
) == 0) {
1106 ret
= acl_check_password_rights(tmp_ctx
,
1113 if (ret
!= LDB_SUCCESS
) {
1116 } else if (ldb_attr_cmp("servicePrincipalName", el
->name
) == 0) {
1117 ret
= acl_check_spn(tmp_ctx
,
1124 if (ret
!= LDB_SUCCESS
) {
1128 struct object_tree
*root
= NULL
;
1129 struct object_tree
*new_node
= NULL
;
1131 if (!insert_in_object_tree(tmp_ctx
,
1132 &objectclass
->schemaIDGUID
,
1134 &root
, &new_node
)) {
1135 talloc_free(tmp_ctx
);
1136 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1137 "acl_modify: Error adding new node in object tree.");
1140 if (!insert_in_object_tree(tmp_ctx
,
1141 &attr
->attributeSecurityGUID
, SEC_ADS_WRITE_PROP
,
1142 &new_node
, &new_node
)) {
1143 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1144 "acl_modify: cannot add to object tree securityGUID\n");
1145 ret
= LDB_ERR_OPERATIONS_ERROR
;
1149 if (!insert_in_object_tree(tmp_ctx
,
1150 &attr
->schemaIDGUID
, SEC_ADS_WRITE_PROP
, &new_node
, &new_node
)) {
1151 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1152 "acl_modify: cannot add to object tree attributeGUID\n");
1153 ret
= LDB_ERR_OPERATIONS_ERROR
;
1157 status
= sec_access_check_ds(sd
, acl_user_token(module
),
1162 if (!NT_STATUS_IS_OK(status
)) {
1163 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1164 "Object %s has no write property access\n",
1165 ldb_dn_get_linearized(msg
->dn
));
1167 acl_user_token(module
),
1171 ret
= LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1178 talloc_free(tmp_ctx
);
1179 return ldb_next_request(module
, req
);
1181 talloc_free(tmp_ctx
);
1185 /* similar to the modify for the time being.
1186 * We need to consider the special delete tree case, though - TODO */
1187 static int acl_delete(struct ldb_module
*module
, struct ldb_request
*req
)
1190 struct ldb_dn
*parent
;
1191 struct ldb_context
*ldb
;
1192 struct ldb_dn
*nc_root
;
1193 struct ldb_control
*as_system
;
1195 if (ldb_dn_is_special(req
->op
.del
.dn
)) {
1196 return ldb_next_request(module
, req
);
1199 as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
1200 if (as_system
!= NULL
) {
1201 as_system
->critical
= 0;
1204 if (dsdb_module_am_system(module
) || as_system
) {
1205 return ldb_next_request(module
, req
);
1208 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req
->op
.del
.dn
)));
1210 ldb
= ldb_module_get_ctx(module
);
1212 parent
= ldb_dn_get_parent(req
, req
->op
.del
.dn
);
1213 if (parent
== NULL
) {
1214 return ldb_oom(ldb
);
1217 /* Make sure we aren't deleting a NC */
1219 ret
= dsdb_find_nc_root(ldb
, req
, req
->op
.del
.dn
, &nc_root
);
1220 if (ret
!= LDB_SUCCESS
) {
1223 if (ldb_dn_compare(nc_root
, req
->op
.del
.dn
) == 0) {
1224 talloc_free(nc_root
);
1225 DEBUG(10,("acl:deleting a NC\n"));
1226 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1227 return ldb_module_done(req
, NULL
, NULL
,
1228 LDB_ERR_UNWILLING_TO_PERFORM
);
1230 talloc_free(nc_root
);
1232 if (ldb_request_get_control(req
, LDB_CONTROL_TREE_DELETE_OID
)) {
1233 ret
= dsdb_module_check_access_on_dn(module
, req
,
1235 SEC_ADS_DELETE_TREE
, NULL
,
1237 if (ret
!= LDB_SUCCESS
) {
1241 return ldb_next_request(module
, req
);
1244 /* First check if we have delete object right */
1245 ret
= dsdb_module_check_access_on_dn(module
, req
, req
->op
.del
.dn
,
1246 SEC_STD_DELETE
, NULL
, req
);
1247 if (ret
== LDB_SUCCESS
) {
1248 return ldb_next_request(module
, req
);
1251 /* Nope, we don't have delete object. Lets check if we have delete
1252 * child on the parent */
1253 ret
= dsdb_module_check_access_on_dn(module
, req
, parent
,
1254 SEC_ADS_DELETE_CHILD
, NULL
, req
);
1255 if (ret
!= LDB_SUCCESS
) {
1259 return ldb_next_request(module
, req
);
1262 static int acl_rename(struct ldb_module
*module
, struct ldb_request
*req
)
1265 struct ldb_dn
*oldparent
;
1266 struct ldb_dn
*newparent
;
1267 const struct dsdb_schema
*schema
;
1268 const struct dsdb_class
*objectclass
;
1269 struct ldb_context
*ldb
;
1270 struct security_descriptor
*sd
= NULL
;
1271 struct dom_sid
*sid
= NULL
;
1272 struct ldb_result
*acl_res
;
1273 const struct GUID
*guid
;
1274 struct ldb_dn
*nc_root
;
1275 struct object_tree
*root
= NULL
;
1276 struct object_tree
*new_node
= NULL
;
1277 struct ldb_control
*as_system
;
1278 TALLOC_CTX
*tmp_ctx
;
1280 uint32_t access_granted
;
1281 const char *rdn_name
;
1282 static const char *acl_attrs
[] = {
1283 "nTSecurityDescriptor",
1289 if (ldb_dn_is_special(req
->op
.rename
.olddn
)) {
1290 return ldb_next_request(module
, req
);
1293 as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
1294 if (as_system
!= NULL
) {
1295 as_system
->critical
= 0;
1298 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req
->op
.rename
.olddn
)));
1299 if (dsdb_module_am_system(module
) || as_system
) {
1300 return ldb_next_request(module
, req
);
1303 ldb
= ldb_module_get_ctx(module
);
1305 tmp_ctx
= talloc_new(req
);
1306 if (tmp_ctx
== NULL
) {
1307 return ldb_oom(ldb
);
1310 oldparent
= ldb_dn_get_parent(tmp_ctx
, req
->op
.rename
.olddn
);
1311 if (oldparent
== NULL
) {
1312 return ldb_oom(ldb
);
1314 newparent
= ldb_dn_get_parent(tmp_ctx
, req
->op
.rename
.newdn
);
1315 if (newparent
== NULL
) {
1316 return ldb_oom(ldb
);
1319 /* Make sure we aren't renaming/moving a NC */
1321 ret
= dsdb_find_nc_root(ldb
, req
, req
->op
.rename
.olddn
, &nc_root
);
1322 if (ret
!= LDB_SUCCESS
) {
1325 if (ldb_dn_compare(nc_root
, req
->op
.rename
.olddn
) == 0) {
1326 talloc_free(nc_root
);
1327 DEBUG(10,("acl:renaming/moving a NC\n"));
1328 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1329 return ldb_module_done(req
, NULL
, NULL
,
1330 LDB_ERR_UNWILLING_TO_PERFORM
);
1332 talloc_free(nc_root
);
1334 /* Look for the parent */
1336 ret
= dsdb_module_search_dn(module
, tmp_ctx
, &acl_res
,
1337 req
->op
.rename
.olddn
, acl_attrs
,
1338 DSDB_FLAG_NEXT_MODULE
|
1339 DSDB_FLAG_AS_SYSTEM
|
1340 DSDB_SEARCH_SHOW_RECYCLED
, req
);
1341 /* we sould be able to find the parent */
1342 if (ret
!= LDB_SUCCESS
) {
1343 DEBUG(10,("acl: failed to find object %s\n",
1344 ldb_dn_get_linearized(req
->op
.rename
.olddn
)));
1345 talloc_free(tmp_ctx
);
1349 schema
= dsdb_get_schema(ldb
, acl_res
);
1351 talloc_free(tmp_ctx
);
1352 return ldb_operr(ldb
);
1355 objectclass
= dsdb_get_structural_oc_from_msg(schema
, acl_res
->msgs
[0]);
1357 talloc_free(tmp_ctx
);
1358 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1359 "acl_modify: Error retrieving object class for GUID.");
1362 if (!insert_in_object_tree(tmp_ctx
, &objectclass
->schemaIDGUID
, SEC_ADS_WRITE_PROP
,
1363 &root
, &new_node
)) {
1364 talloc_free(tmp_ctx
);
1365 return ldb_operr(ldb
);
1368 guid
= attribute_schemaid_guid_by_lDAPDisplayName(schema
,
1370 if (!insert_in_object_tree(tmp_ctx
, guid
, SEC_ADS_WRITE_PROP
,
1371 &new_node
, &new_node
)) {
1372 talloc_free(tmp_ctx
);
1373 return ldb_operr(ldb
);
1376 rdn_name
= ldb_dn_get_rdn_name(req
->op
.rename
.olddn
);
1377 if (rdn_name
== NULL
) {
1378 talloc_free(tmp_ctx
);
1379 return ldb_operr(ldb
);
1381 guid
= attribute_schemaid_guid_by_lDAPDisplayName(schema
,
1383 if (!insert_in_object_tree(tmp_ctx
, guid
, SEC_ADS_WRITE_PROP
,
1384 &new_node
, &new_node
)) {
1385 talloc_free(tmp_ctx
);
1386 return ldb_operr(ldb
);
1389 ret
= dsdb_get_sd_from_ldb_message(ldb
, req
, acl_res
->msgs
[0], &sd
);
1391 if (ret
!= LDB_SUCCESS
) {
1392 talloc_free(tmp_ctx
);
1393 return ldb_operr(ldb
);
1395 /* Theoretically we pass the check if the object has no sd */
1397 talloc_free(tmp_ctx
);
1400 sid
= samdb_result_dom_sid(req
, acl_res
->msgs
[0], "objectSid");
1401 status
= sec_access_check_ds(sd
, acl_user_token(module
),
1407 if (!NT_STATUS_IS_OK(status
)) {
1408 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1409 "Object %s has no wp on name\n",
1410 ldb_dn_get_linearized(req
->op
.rename
.olddn
));
1412 acl_user_token(module
),
1413 req
->op
.rename
.olddn
,
1416 talloc_free(tmp_ctx
);
1417 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1420 if (ldb_dn_compare(oldparent
, newparent
) == 0) {
1421 /* regular rename, not move, nothing more to do */
1422 talloc_free(tmp_ctx
);
1423 return ldb_next_request(module
, req
);
1426 /* new parent should have create child */
1430 ret
= dsdb_module_check_access_on_dn(module
, req
, newparent
,
1431 SEC_ADS_CREATE_CHILD
,
1432 &objectclass
->schemaIDGUID
, req
);
1433 if (ret
!= LDB_SUCCESS
) {
1434 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1435 "acl:access_denied renaming %s",
1436 ldb_dn_get_linearized(req
->op
.rename
.olddn
));
1437 talloc_free(tmp_ctx
);
1440 /* do we have delete object on the object? */
1442 status
= sec_access_check_ds(sd
, acl_user_token(module
),
1448 if (NT_STATUS_IS_OK(status
)) {
1449 talloc_free(tmp_ctx
);
1450 return ldb_next_request(module
, req
);
1452 /* what about delete child on the current parent */
1453 ret
= dsdb_module_check_access_on_dn(module
, req
, oldparent
, SEC_ADS_DELETE_CHILD
, NULL
, req
);
1454 if (ret
!= LDB_SUCCESS
) {
1455 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1456 "acl:access_denied renaming %s", ldb_dn_get_linearized(req
->op
.rename
.olddn
));
1457 talloc_free(tmp_ctx
);
1458 return ldb_module_done(req
, NULL
, NULL
, ret
);
1461 talloc_free(tmp_ctx
);
1463 return ldb_next_request(module
, req
);
1466 static int acl_search_update_confidential_attrs(struct acl_context
*ac
,
1467 struct acl_private
*data
)
1469 struct dsdb_attribute
*a
;
1472 if (data
->acl_search
) {
1474 * If acl:search is activated, the acl_read module
1475 * protects confidential attributes.
1480 if ((ac
->schema
== data
->cached_schema_ptr
) &&
1481 (ac
->schema
->loaded_usn
== data
->cached_schema_loaded_usn
) &&
1482 (ac
->schema
->metadata_usn
== data
->cached_schema_metadata_usn
))
1487 data
->cached_schema_ptr
= NULL
;
1488 data
->cached_schema_loaded_usn
= 0;
1489 data
->cached_schema_metadata_usn
= 0;
1490 TALLOC_FREE(data
->confidential_attrs
);
1492 if (ac
->schema
== NULL
) {
1496 for (a
= ac
->schema
->attributes
; a
; a
= a
->next
) {
1497 const char **attrs
= data
->confidential_attrs
;
1499 if (!(a
->searchFlags
& SEARCH_FLAG_CONFIDENTIAL
)) {
1503 attrs
= talloc_realloc(data
, attrs
, const char *, n
+ 2);
1504 if (attrs
== NULL
) {
1505 TALLOC_FREE(data
->confidential_attrs
);
1506 return ldb_module_oom(ac
->module
);
1509 attrs
[n
] = a
->lDAPDisplayName
;
1513 data
->confidential_attrs
= attrs
;
1516 data
->cached_schema_ptr
= ac
->schema
;
1517 data
->cached_schema_loaded_usn
= ac
->schema
->loaded_usn
;
1518 data
->cached_schema_metadata_usn
= ac
->schema
->metadata_usn
;
1523 static int acl_search_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
1525 struct acl_context
*ac
;
1526 struct acl_private
*data
;
1527 struct ldb_result
*acl_res
;
1528 static const char *acl_attrs
[] = {
1530 "nTSecurityDescriptor",
1537 ac
= talloc_get_type(req
->context
, struct acl_context
);
1538 data
= talloc_get_type(ldb_module_get_private(ac
->module
), struct acl_private
);
1540 return ldb_module_done(ac
->req
, NULL
, NULL
,
1541 LDB_ERR_OPERATIONS_ERROR
);
1543 if (ares
->error
!= LDB_SUCCESS
) {
1544 return ldb_module_done(ac
->req
, ares
->controls
,
1545 ares
->response
, ares
->error
);
1548 switch (ares
->type
) {
1549 case LDB_REPLY_ENTRY
:
1550 if (ac
->constructed_attrs
) {
1551 ret
= dsdb_module_search_dn(ac
->module
, ac
, &acl_res
, ares
->message
->dn
,
1553 DSDB_FLAG_NEXT_MODULE
|
1554 DSDB_FLAG_AS_SYSTEM
|
1555 DSDB_SEARCH_SHOW_RECYCLED
,
1557 if (ret
!= LDB_SUCCESS
) {
1558 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1562 if (ac
->allowedAttributes
|| ac
->allowedAttributesEffective
) {
1563 ret
= acl_allowedAttributes(ac
->module
, ac
->schema
,
1566 if (ret
!= LDB_SUCCESS
) {
1567 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1571 if (ac
->allowedChildClasses
) {
1572 ret
= acl_childClasses(ac
->module
, ac
->schema
,
1575 "allowedChildClasses");
1576 if (ret
!= LDB_SUCCESS
) {
1577 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1581 if (ac
->allowedChildClassesEffective
) {
1582 ret
= acl_childClassesEffective(ac
->module
, ac
->schema
,
1585 if (ret
!= LDB_SUCCESS
) {
1586 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1590 if (ac
->sDRightsEffective
) {
1591 ret
= acl_sDRightsEffective(ac
->module
,
1594 if (ret
!= LDB_SUCCESS
) {
1595 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1600 return ldb_module_send_entry(ac
->req
, ares
->message
,
1604 if (ac
->am_system
) {
1605 return ldb_module_send_entry(ac
->req
, ares
->message
,
1609 if (data
->password_attrs
!= NULL
) {
1610 for (i
= 0; data
->password_attrs
[i
]; i
++) {
1611 if ((!ac
->userPassword
) &&
1612 (ldb_attr_cmp(data
->password_attrs
[i
],
1613 "userPassword") == 0))
1618 ldb_msg_remove_attr(ares
->message
, data
->password_attrs
[i
]);
1622 if (ac
->am_administrator
) {
1623 return ldb_module_send_entry(ac
->req
, ares
->message
,
1627 ret
= acl_search_update_confidential_attrs(ac
, data
);
1628 if (ret
!= LDB_SUCCESS
) {
1632 if (data
->confidential_attrs
!= NULL
) {
1633 for (i
= 0; data
->confidential_attrs
[i
]; i
++) {
1634 ldb_msg_remove_attr(ares
->message
,
1635 data
->confidential_attrs
[i
]);
1639 return ldb_module_send_entry(ac
->req
, ares
->message
, ares
->controls
);
1641 case LDB_REPLY_REFERRAL
:
1642 return ldb_module_send_referral(ac
->req
, ares
->referral
);
1644 case LDB_REPLY_DONE
:
1645 return ldb_module_done(ac
->req
, ares
->controls
,
1646 ares
->response
, LDB_SUCCESS
);
1652 static int acl_search(struct ldb_module
*module
, struct ldb_request
*req
)
1654 struct ldb_context
*ldb
;
1655 struct acl_context
*ac
;
1656 struct ldb_parse_tree
*down_tree
;
1657 struct ldb_request
*down_req
;
1658 struct acl_private
*data
;
1662 if (ldb_dn_is_special(req
->op
.search
.base
)) {
1663 return ldb_next_request(module
, req
);
1666 ldb
= ldb_module_get_ctx(module
);
1668 ac
= talloc_zero(req
, struct acl_context
);
1670 return ldb_oom(ldb
);
1672 data
= talloc_get_type(ldb_module_get_private(module
), struct acl_private
);
1674 ac
->module
= module
;
1676 ac
->am_system
= dsdb_module_am_system(module
);
1677 ac
->am_administrator
= dsdb_module_am_administrator(module
);
1678 ac
->constructed_attrs
= false;
1679 ac
->modify_search
= true;
1680 ac
->allowedAttributes
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedAttributes");
1681 ac
->allowedAttributesEffective
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedAttributesEffective");
1682 ac
->allowedChildClasses
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedChildClasses");
1683 ac
->allowedChildClassesEffective
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedChildClassesEffective");
1684 ac
->sDRightsEffective
= ldb_attr_in_list(req
->op
.search
.attrs
, "sDRightsEffective");
1685 ac
->userPassword
= true;
1686 ac
->schema
= dsdb_get_schema(ldb
, ac
);
1688 ac
->constructed_attrs
|= ac
->allowedAttributes
;
1689 ac
->constructed_attrs
|= ac
->allowedChildClasses
;
1690 ac
->constructed_attrs
|= ac
->allowedChildClassesEffective
;
1691 ac
->constructed_attrs
|= ac
->allowedAttributesEffective
;
1692 ac
->constructed_attrs
|= ac
->sDRightsEffective
;
1695 ac
->modify_search
= false;
1697 if (ac
->am_system
) {
1698 ac
->modify_search
= false;
1701 if (!ac
->constructed_attrs
&& !ac
->modify_search
) {
1703 return ldb_next_request(module
, req
);
1706 if (!ac
->am_system
) {
1707 ac
->userPassword
= dsdb_user_password_support(module
, ac
, req
);
1710 ret
= acl_search_update_confidential_attrs(ac
, data
);
1711 if (ret
!= LDB_SUCCESS
) {
1715 down_tree
= ldb_parse_tree_copy_shallow(ac
, req
->op
.search
.tree
);
1716 if (down_tree
== NULL
) {
1717 return ldb_oom(ldb
);
1720 if (!ac
->am_system
&& data
->password_attrs
) {
1721 for (i
= 0; data
->password_attrs
[i
]; i
++) {
1722 if ((!ac
->userPassword
) &&
1723 (ldb_attr_cmp(data
->password_attrs
[i
],
1724 "userPassword") == 0))
1729 ldb_parse_tree_attr_replace(down_tree
,
1730 data
->password_attrs
[i
],
1731 "kludgeACLredactedattribute");
1735 if (!ac
->am_system
&& !ac
->am_administrator
&& data
->confidential_attrs
) {
1736 for (i
= 0; data
->confidential_attrs
[i
]; i
++) {
1737 ldb_parse_tree_attr_replace(down_tree
,
1738 data
->confidential_attrs
[i
],
1739 "kludgeACLredactedattribute");
1743 ret
= ldb_build_search_req_ex(&down_req
,
1745 req
->op
.search
.base
,
1746 req
->op
.search
.scope
,
1748 req
->op
.search
.attrs
,
1750 ac
, acl_search_callback
,
1752 LDB_REQ_SET_LOCATION(down_req
);
1753 if (ret
!= LDB_SUCCESS
) {
1756 /* perform the search */
1757 return ldb_next_request(module
, down_req
);
1760 static int acl_extended(struct ldb_module
*module
, struct ldb_request
*req
)
1762 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
1763 struct ldb_control
*as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
1765 /* allow everybody to read the sequence number */
1766 if (strcmp(req
->op
.extended
.oid
,
1767 LDB_EXTENDED_SEQUENCE_NUMBER
) == 0) {
1768 return ldb_next_request(module
, req
);
1771 if (dsdb_module_am_system(module
) ||
1772 dsdb_module_am_administrator(module
) || as_system
) {
1773 return ldb_next_request(module
, req
);
1775 ldb_asprintf_errstring(ldb
,
1777 "attempted database modify not permitted. "
1778 "User %s is not SYSTEM or an administrator",
1779 acl_user_name(req
, module
));
1780 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1784 static const struct ldb_module_ops ldb_acl_module_ops
= {
1786 .search
= acl_search
,
1788 .modify
= acl_modify
,
1790 .rename
= acl_rename
,
1791 .extended
= acl_extended
,
1792 .init_context
= acl_module_init
1795 int ldb_acl_module_init(const char *version
)
1797 LDB_MODULE_CHECK_VERSION(version
);
1798 return ldb_register_module(&ldb_acl_module_ops
);