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_check_access_on_class(struct ldb_module
*module
,
344 const struct dsdb_schema
*schema
,
346 struct security_descriptor
*sd
,
347 struct security_token
*token
,
348 struct dom_sid
*rp_sid
,
349 uint32_t access_mask
,
350 const char *class_name
)
354 uint32_t access_granted
;
355 struct object_tree
*root
= NULL
;
356 struct object_tree
*new_node
= NULL
;
357 const struct GUID
*guid
;
359 if (class_name
!= NULL
) {
360 guid
= class_schemaid_guid_by_lDAPDisplayName(schema
, class_name
);
362 DEBUG(10, ("acl_search: cannot find class %s\n",
366 if (!insert_in_object_tree(mem_ctx
,
369 DEBUG(10, ("acl_search: cannot add to object tree guid\n"));
374 status
= sec_access_check_ds(sd
, token
,
379 if (!NT_STATUS_IS_OK(status
)) {
380 ret
= LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
386 return ldb_operr(ldb_module_get_ctx(module
));
389 static int acl_childClassesEffective(struct ldb_module
*module
,
390 const struct dsdb_schema
*schema
,
391 struct ldb_message
*sd_msg
,
392 struct ldb_message
*msg
,
393 struct acl_context
*ac
)
395 struct ldb_message_element
*oc_el
;
396 struct ldb_message_element
*allowedClasses
= NULL
;
397 const struct dsdb_class
*sclass
;
398 struct security_descriptor
*sd
;
399 struct ldb_control
*as_system
= ldb_request_get_control(ac
->req
,
400 LDB_CONTROL_AS_SYSTEM_OID
);
401 struct dom_sid
*sid
= NULL
;
405 if (as_system
!= NULL
) {
406 as_system
->critical
= 0;
409 if (ac
->am_system
|| as_system
) {
410 return acl_childClasses(module
, schema
, sd_msg
, msg
, "allowedChildClassesEffective");
413 /* If we don't have a schema yet, we can't do anything... */
414 if (schema
== NULL
) {
415 ldb_asprintf_errstring(ldb_module_get_ctx(module
), "cannot add allowedChildClassesEffective to %s because no schema is loaded", ldb_dn_get_linearized(msg
->dn
));
416 return LDB_ERR_OPERATIONS_ERROR
;
419 /* Must remove any existing attribute, or else confusion reins */
420 ldb_msg_remove_attr(msg
, "allowedChildClassesEffective");
422 oc_el
= ldb_msg_find_element(sd_msg
, "objectClass");
423 ret
= dsdb_get_sd_from_ldb_message(ldb_module_get_ctx(module
), msg
, sd_msg
, &sd
);
424 if (ret
!= LDB_SUCCESS
) {
428 sid
= samdb_result_dom_sid(msg
, sd_msg
, "objectSid");
429 for (i
=0; oc_el
&& i
< oc_el
->num_values
; i
++) {
430 sclass
= dsdb_class_by_lDAPDisplayName_ldb_val(schema
, &oc_el
->values
[i
]);
432 /* We don't know this class? what is going on? */
436 for (j
=0; sclass
->possibleInferiors
&& sclass
->possibleInferiors
[j
]; j
++) {
437 const struct dsdb_class
*sc
;
439 sc
= dsdb_class_by_lDAPDisplayName(schema
,
440 sclass
->possibleInferiors
[j
]);
442 /* We don't know this class? what is going on? */
446 ret
= acl_check_access_on_objectclass(module
, ac
,
448 SEC_ADS_CREATE_CHILD
,
450 if (ret
== LDB_SUCCESS
) {
451 ldb_msg_add_string(msg
, "allowedChildClassesEffective",
452 sclass
->possibleInferiors
[j
]);
456 allowedClasses
= ldb_msg_find_element(msg
, "allowedChildClassesEffective");
457 if (!allowedClasses
) {
461 if (allowedClasses
->num_values
> 1) {
462 TYPESAFE_QSORT(allowedClasses
->values
, allowedClasses
->num_values
, data_blob_cmp
);
463 for (i
=1 ; i
< allowedClasses
->num_values
; i
++) {
464 struct ldb_val
*val1
= &allowedClasses
->values
[i
-1];
465 struct ldb_val
*val2
= &allowedClasses
->values
[i
];
466 if (data_blob_cmp(val1
, val2
) == 0) {
467 memmove(val1
, val2
, (allowedClasses
->num_values
- i
) * sizeof( struct ldb_val
));
468 allowedClasses
->num_values
--;
476 static int acl_sDRightsEffective(struct ldb_module
*module
,
477 struct ldb_message
*sd_msg
,
478 struct ldb_message
*msg
,
479 struct acl_context
*ac
)
481 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
482 struct ldb_message_element
*rightsEffective
;
484 struct security_descriptor
*sd
;
485 struct ldb_control
*as_system
= ldb_request_get_control(ac
->req
,
486 LDB_CONTROL_AS_SYSTEM_OID
);
487 struct dom_sid
*sid
= NULL
;
490 if (as_system
!= NULL
) {
491 as_system
->critical
= 0;
494 /* Must remove any existing attribute, or else confusion reins */
495 ldb_msg_remove_attr(msg
, "sDRightsEffective");
496 ret
= ldb_msg_add_empty(msg
, "sDRightsEffective", 0, &rightsEffective
);
497 if (ret
!= LDB_SUCCESS
) {
500 if (ac
->am_system
|| as_system
) {
501 flags
= SECINFO_OWNER
| SECINFO_GROUP
| SECINFO_SACL
| SECINFO_DACL
;
503 const struct dsdb_class
*objectclass
;
504 const struct dsdb_attribute
*attr
;
506 objectclass
= dsdb_get_structural_oc_from_msg(ac
->schema
, sd_msg
);
507 if (objectclass
== NULL
) {
508 return ldb_operr(ldb
);
511 attr
= dsdb_attribute_by_lDAPDisplayName(ac
->schema
,
512 "nTSecurityDescriptor");
514 return ldb_operr(ldb
);
517 /* Get the security descriptor from the message */
518 ret
= dsdb_get_sd_from_ldb_message(ldb
, msg
, sd_msg
, &sd
);
519 if (ret
!= LDB_SUCCESS
) {
522 sid
= samdb_result_dom_sid(msg
, sd_msg
, "objectSid");
523 ret
= acl_check_access_on_attribute(module
,
530 if (ret
== LDB_SUCCESS
) {
531 flags
|= SECINFO_OWNER
| SECINFO_GROUP
;
533 ret
= acl_check_access_on_attribute(module
,
540 if (ret
== LDB_SUCCESS
) {
541 flags
|= SECINFO_DACL
;
543 ret
= acl_check_access_on_attribute(module
,
547 SEC_FLAG_SYSTEM_SECURITY
,
550 if (ret
== LDB_SUCCESS
) {
551 flags
|= SECINFO_SACL
;
554 return samdb_msg_add_uint(ldb_module_get_ctx(module
), msg
, msg
,
555 "sDRightsEffective", flags
);
558 static int acl_validate_spn_value(TALLOC_CTX
*mem_ctx
,
559 struct ldb_context
*ldb
,
560 const char *spn_value
,
561 uint32_t userAccountControl
,
562 const char *samAccountName
,
563 const char *dnsHostName
,
564 const char *netbios_name
,
565 const char *ntds_guid
)
568 krb5_context krb_ctx
;
569 krb5_error_code kerr
;
570 krb5_principal principal
;
574 const char *forest_name
= samdb_forest_name(ldb
, mem_ctx
);
575 const char *base_domain
= samdb_default_domain_name(ldb
, mem_ctx
);
576 struct loadparm_context
*lp_ctx
= talloc_get_type(ldb_get_opaque(ldb
, "loadparm"),
577 struct loadparm_context
);
578 bool is_dc
= (userAccountControl
& UF_SERVER_TRUST_ACCOUNT
) ||
579 (userAccountControl
& UF_PARTIAL_SECRETS_ACCOUNT
);
581 if (strcasecmp_m(spn_value
, samAccountName
) == 0) {
582 /* MacOS X sets this value, and setting an SPN of your
583 * own samAccountName is both pointless and safe */
587 kerr
= smb_krb5_init_context_basic(mem_ctx
,
591 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
592 "Could not initialize kerberos context.");
595 ret
= krb5_parse_name(krb_ctx
, spn_value
, &principal
);
597 krb5_free_context(krb_ctx
);
598 return LDB_ERR_CONSTRAINT_VIOLATION
;
601 if (principal
->name
.name_string
.len
< 2) {
605 instanceName
= principal
->name
.name_string
.val
[1];
606 serviceType
= principal
->name
.name_string
.val
[0];
607 if (principal
->name
.name_string
.len
== 3) {
608 serviceName
= principal
->name
.name_string
.val
[2];
617 if (strcasecmp(serviceType
, "ldap") == 0) {
618 if (strcasecmp(serviceName
, netbios_name
) != 0 &&
619 strcasecmp(serviceName
, forest_name
) != 0) {
623 } else if (strcasecmp(serviceType
, "gc") == 0) {
624 if (strcasecmp(serviceName
, forest_name
) != 0) {
628 if (strcasecmp(serviceName
, base_domain
) != 0 &&
629 strcasecmp(serviceName
, netbios_name
) != 0) {
634 /* instanceName can be samAccountName without $ or dnsHostName
635 * or "ntds_guid._msdcs.forest_domain for DC objects */
636 if (strlen(instanceName
) == (strlen(samAccountName
) - 1)
637 && strncasecmp(instanceName
, samAccountName
, strlen(samAccountName
) - 1) == 0) {
639 } else if (dnsHostName
!= NULL
&& strcasecmp(instanceName
, dnsHostName
) == 0) {
642 const char *guid_str
;
643 guid_str
= talloc_asprintf(mem_ctx
,"%s._msdcs.%s",
646 if (strcasecmp(instanceName
, guid_str
) == 0) {
652 krb5_free_principal(krb_ctx
, principal
);
653 krb5_free_context(krb_ctx
);
654 return LDB_ERR_CONSTRAINT_VIOLATION
;
657 krb5_free_principal(krb_ctx
, principal
);
658 krb5_free_context(krb_ctx
);
662 static int acl_check_spn(TALLOC_CTX
*mem_ctx
,
663 struct ldb_module
*module
,
664 struct ldb_request
*req
,
665 struct security_descriptor
*sd
,
667 const struct dsdb_attribute
*attr
,
668 const struct dsdb_class
*objectclass
)
672 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
673 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
674 struct ldb_result
*acl_res
;
675 struct ldb_result
*netbios_res
;
676 struct ldb_message_element
*el
;
677 struct ldb_dn
*partitions_dn
= samdb_partitions_dn(ldb
, tmp_ctx
);
678 uint32_t userAccountControl
;
679 const char *samAccountName
;
680 const char *dnsHostName
;
681 const char *netbios_name
;
683 char *ntds_guid
= NULL
;
685 static const char *acl_attrs
[] = {
688 "userAccountControl",
691 static const char *netbios_attrs
[] = {
696 /* if we have wp, we can do whatever we like */
697 if (acl_check_access_on_attribute(module
,
702 attr
, objectclass
) == LDB_SUCCESS
) {
703 talloc_free(tmp_ctx
);
707 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
708 GUID_DRS_VALIDATE_SPN
,
712 if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
713 dsdb_acl_debug(sd
, acl_user_token(module
),
714 req
->op
.mod
.message
->dn
,
717 talloc_free(tmp_ctx
);
721 ret
= dsdb_module_search_dn(module
, tmp_ctx
,
722 &acl_res
, req
->op
.mod
.message
->dn
,
724 DSDB_FLAG_NEXT_MODULE
|
725 DSDB_FLAG_AS_SYSTEM
|
726 DSDB_SEARCH_SHOW_RECYCLED
,
728 if (ret
!= LDB_SUCCESS
) {
729 talloc_free(tmp_ctx
);
733 userAccountControl
= ldb_msg_find_attr_as_uint(acl_res
->msgs
[0], "userAccountControl", 0);
734 dnsHostName
= ldb_msg_find_attr_as_string(acl_res
->msgs
[0], "dnsHostName", NULL
);
735 samAccountName
= ldb_msg_find_attr_as_string(acl_res
->msgs
[0], "samAccountName", NULL
);
737 ret
= dsdb_module_search(module
, tmp_ctx
,
738 &netbios_res
, partitions_dn
,
741 DSDB_FLAG_NEXT_MODULE
|
745 ldb_dn_get_linearized(ldb_get_default_basedn(ldb
)));
747 netbios_name
= ldb_msg_find_attr_as_string(netbios_res
->msgs
[0], "nETBIOSName", NULL
);
749 el
= ldb_msg_find_element(req
->op
.mod
.message
, "servicePrincipalName");
751 talloc_free(tmp_ctx
);
752 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
753 "Error finding element for servicePrincipalName.");
756 /* NTDSDSA objectGuid of object we are checking SPN for */
757 if (userAccountControl
& (UF_SERVER_TRUST_ACCOUNT
| UF_PARTIAL_SECRETS_ACCOUNT
)) {
758 ret
= dsdb_module_find_ntdsguid_for_computer(module
, tmp_ctx
,
759 req
->op
.mod
.message
->dn
, &ntds
, req
);
760 if (ret
!= LDB_SUCCESS
) {
761 ldb_asprintf_errstring(ldb
, "Failed to find NTDSDSA objectGuid for %s: %s",
762 ldb_dn_get_linearized(req
->op
.mod
.message
->dn
),
764 talloc_free(tmp_ctx
);
765 return LDB_ERR_OPERATIONS_ERROR
;
767 ntds_guid
= GUID_string(tmp_ctx
, &ntds
);
770 for (i
=0; i
< el
->num_values
; i
++) {
771 ret
= acl_validate_spn_value(tmp_ctx
,
773 (char *)el
->values
[i
].data
,
779 if (ret
!= LDB_SUCCESS
) {
780 talloc_free(tmp_ctx
);
784 talloc_free(tmp_ctx
);
788 static int acl_add(struct ldb_module
*module
, struct ldb_request
*req
)
791 struct ldb_dn
*parent
;
792 struct ldb_context
*ldb
;
793 const struct dsdb_schema
*schema
;
794 const struct dsdb_class
*objectclass
;
795 struct ldb_dn
*nc_root
;
796 struct ldb_control
*as_system
;
798 if (ldb_dn_is_special(req
->op
.add
.message
->dn
)) {
799 return ldb_next_request(module
, req
);
802 as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
803 if (as_system
!= NULL
) {
804 as_system
->critical
= 0;
807 if (dsdb_module_am_system(module
) || as_system
) {
808 return ldb_next_request(module
, req
);
811 ldb
= ldb_module_get_ctx(module
);
813 parent
= ldb_dn_get_parent(req
, req
->op
.add
.message
->dn
);
814 if (parent
== NULL
) {
818 /* Creating an NC. There is probably something we should do here,
819 * but we will establish that later */
821 ret
= dsdb_find_nc_root(ldb
, req
, req
->op
.add
.message
->dn
, &nc_root
);
822 if (ret
!= LDB_SUCCESS
) {
825 if (ldb_dn_compare(nc_root
, req
->op
.add
.message
->dn
) == 0) {
826 talloc_free(nc_root
);
827 return ldb_next_request(module
, req
);
829 talloc_free(nc_root
);
831 schema
= dsdb_get_schema(ldb
, req
);
833 return ldb_operr(ldb
);
836 objectclass
= dsdb_get_structural_oc_from_msg(schema
, req
->op
.add
.message
);
838 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
839 "acl: unable to find or validate structrual objectClass on %s\n",
840 ldb_dn_get_linearized(req
->op
.add
.message
->dn
));
841 return ldb_module_done(req
, NULL
, NULL
, LDB_ERR_OPERATIONS_ERROR
);
844 ret
= dsdb_module_check_access_on_dn(module
, req
, parent
,
845 SEC_ADS_CREATE_CHILD
,
846 &objectclass
->schemaIDGUID
, req
);
847 if (ret
!= LDB_SUCCESS
) {
850 return ldb_next_request(module
, req
);
853 /* ckecks if modifications are allowed on "Member" attribute */
854 static int acl_check_self_membership(TALLOC_CTX
*mem_ctx
,
855 struct ldb_module
*module
,
856 struct ldb_request
*req
,
857 struct security_descriptor
*sd
,
859 const struct dsdb_attribute
*attr
,
860 const struct dsdb_class
*objectclass
)
864 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
865 struct ldb_dn
*user_dn
;
866 struct ldb_message_element
*member_el
;
867 /* if we have wp, we can do whatever we like */
868 if (acl_check_access_on_attribute(module
,
873 attr
, objectclass
) == LDB_SUCCESS
) {
876 /* if we are adding/deleting ourselves, check for self membership */
877 ret
= dsdb_find_dn_by_sid(ldb
, mem_ctx
,
878 &acl_user_token(module
)->sids
[PRIMARY_USER_SID_INDEX
],
880 if (ret
!= LDB_SUCCESS
) {
883 member_el
= ldb_msg_find_element(req
->op
.mod
.message
, "member");
885 return ldb_operr(ldb
);
887 /* user can only remove oneself */
888 if (member_el
->num_values
== 0) {
889 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
891 for (i
= 0; i
< member_el
->num_values
; i
++) {
892 if (strcasecmp((const char *)member_el
->values
[i
].data
,
893 ldb_dn_get_extended_linearized(mem_ctx
, user_dn
, 1)) != 0) {
894 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
897 ret
= acl_check_extended_right(mem_ctx
, sd
, acl_user_token(module
),
898 GUID_DRS_SELF_MEMBERSHIP
,
901 if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
902 dsdb_acl_debug(sd
, acl_user_token(module
),
903 req
->op
.mod
.message
->dn
,
910 static int acl_check_password_rights(TALLOC_CTX
*mem_ctx
,
911 struct ldb_module
*module
,
912 struct ldb_request
*req
,
913 struct security_descriptor
*sd
,
915 const struct dsdb_class
*objectclass
,
918 int ret
= LDB_SUCCESS
;
919 unsigned int del_attr_cnt
= 0, add_attr_cnt
= 0, rep_attr_cnt
= 0;
920 struct ldb_message_element
*el
;
921 struct ldb_message
*msg
;
922 const char *passwordAttrs
[] = { "userPassword", "clearTextPassword",
923 "unicodePwd", "dBCSPwd", NULL
}, **l
;
924 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
926 msg
= ldb_msg_copy_shallow(tmp_ctx
, req
->op
.mod
.message
);
928 return ldb_module_oom(module
);
930 for (l
= passwordAttrs
; *l
!= NULL
; l
++) {
931 if ((!userPassword
) && (ldb_attr_cmp(*l
, "userPassword") == 0)) {
935 while ((el
= ldb_msg_find_element(msg
, *l
)) != NULL
) {
936 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_DELETE
) {
939 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_ADD
) {
942 if (LDB_FLAG_MOD_TYPE(el
->flags
) == LDB_FLAG_MOD_REPLACE
) {
945 ldb_msg_remove_element(msg
, el
);
949 /* single deletes will be handled by the "password_hash" LDB module
950 * later in the stack, so we let it though here */
951 if ((del_attr_cnt
> 0) && (add_attr_cnt
== 0) && (rep_attr_cnt
== 0)) {
952 talloc_free(tmp_ctx
);
956 if (ldb_request_get_control(req
,
957 DSDB_CONTROL_PASSWORD_CHANGE_OID
) != NULL
) {
958 /* The "DSDB_CONTROL_PASSWORD_CHANGE_OID" control means that we
959 * have a user password change and not a set as the message
960 * looks like. In it's value blob it contains the NT and/or LM
961 * hash of the old password specified by the user.
962 * This control is used by the SAMR and "kpasswd" password
963 * change mechanisms. */
964 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
965 GUID_DRS_USER_CHANGE_PASSWORD
,
966 SEC_ADS_CONTROL_ACCESS
,
969 else if (rep_attr_cnt
> 0 || (add_attr_cnt
!= del_attr_cnt
)) {
970 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
971 GUID_DRS_FORCE_CHANGE_PASSWORD
,
972 SEC_ADS_CONTROL_ACCESS
,
975 else if (add_attr_cnt
== 1 && del_attr_cnt
== 1) {
976 ret
= acl_check_extended_right(tmp_ctx
, sd
, acl_user_token(module
),
977 GUID_DRS_USER_CHANGE_PASSWORD
,
978 SEC_ADS_CONTROL_ACCESS
,
980 /* Very strange, but we get constraint violation in this case */
981 if (ret
== LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
) {
982 ret
= LDB_ERR_CONSTRAINT_VIOLATION
;
985 if (ret
!= LDB_SUCCESS
) {
986 dsdb_acl_debug(sd
, acl_user_token(module
),
987 req
->op
.mod
.message
->dn
,
991 talloc_free(tmp_ctx
);
996 static int acl_modify(struct ldb_module
*module
, struct ldb_request
*req
)
999 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
1000 const struct dsdb_schema
*schema
;
1002 const struct dsdb_class
*objectclass
;
1003 uint32_t access_granted
;
1005 struct ldb_result
*acl_res
;
1006 struct security_descriptor
*sd
;
1007 struct dom_sid
*sid
= NULL
;
1008 struct ldb_control
*as_system
;
1010 TALLOC_CTX
*tmp_ctx
;
1011 const struct ldb_message
*msg
= req
->op
.mod
.message
;
1012 static const char *acl_attrs
[] = {
1013 "nTSecurityDescriptor",
1019 if (ldb_dn_is_special(msg
->dn
)) {
1020 return ldb_next_request(module
, req
);
1023 as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
1024 if (as_system
!= NULL
) {
1025 as_system
->critical
= 0;
1028 /* Don't print this debug statement if elements[0].name is going to be NULL */
1029 if (msg
->num_elements
> 0) {
1030 DEBUG(10, ("ldb:acl_modify: %s\n", msg
->elements
[0].name
));
1032 if (dsdb_module_am_system(module
) || as_system
) {
1033 return ldb_next_request(module
, req
);
1036 tmp_ctx
= talloc_new(req
);
1037 if (tmp_ctx
== NULL
) {
1038 return ldb_oom(ldb
);
1041 ret
= dsdb_module_search_dn(module
, tmp_ctx
, &acl_res
, msg
->dn
,
1043 DSDB_FLAG_NEXT_MODULE
|
1044 DSDB_FLAG_AS_SYSTEM
|
1045 DSDB_SEARCH_SHOW_RECYCLED
,
1048 if (ret
!= LDB_SUCCESS
) {
1052 userPassword
= dsdb_user_password_support(module
, req
, req
);
1054 schema
= dsdb_get_schema(ldb
, tmp_ctx
);
1056 talloc_free(tmp_ctx
);
1057 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1058 "acl_modify: Error obtaining schema.");
1061 ret
= dsdb_get_sd_from_ldb_message(ldb
, tmp_ctx
, acl_res
->msgs
[0], &sd
);
1062 if (ret
!= LDB_SUCCESS
) {
1063 talloc_free(tmp_ctx
);
1064 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1065 "acl_modify: Error retrieving security descriptor.");
1067 /* Theoretically we pass the check if the object has no sd */
1072 objectclass
= dsdb_get_structural_oc_from_msg(schema
, acl_res
->msgs
[0]);
1074 talloc_free(tmp_ctx
);
1075 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1076 "acl_modify: Error retrieving object class for GUID.");
1078 sid
= samdb_result_dom_sid(req
, acl_res
->msgs
[0], "objectSid");
1079 for (i
=0; i
< msg
->num_elements
; i
++) {
1080 const struct ldb_message_element
*el
= &msg
->elements
[i
];
1081 const struct dsdb_attribute
*attr
;
1084 * This basic attribute existence check with the right errorcode
1085 * is needed since this module is the first one which requests
1086 * schema attribute information.
1087 * The complete attribute checking is done in the
1088 * "objectclass_attrs" module behind this one.
1090 * NOTE: "clearTextPassword" is not defined in the schema.
1092 attr
= dsdb_attribute_by_lDAPDisplayName(schema
, el
->name
);
1093 if (!attr
&& ldb_attr_cmp("clearTextPassword", el
->name
) != 0) {
1094 ldb_asprintf_errstring(ldb
, "acl_modify: attribute '%s' "
1095 "on entry '%s' was not found in the schema!",
1096 req
->op
.mod
.message
->elements
[i
].name
,
1097 ldb_dn_get_linearized(req
->op
.mod
.message
->dn
));
1098 ret
= LDB_ERR_NO_SUCH_ATTRIBUTE
;
1102 if (ldb_attr_cmp("nTSecurityDescriptor", el
->name
) == 0) {
1103 uint32_t sd_flags
= dsdb_request_sd_flags(req
, NULL
);
1104 uint32_t access_mask
= 0;
1106 if (sd_flags
& (SECINFO_OWNER
|SECINFO_GROUP
)) {
1107 access_mask
|= SEC_STD_WRITE_OWNER
;
1109 if (sd_flags
& SECINFO_DACL
) {
1110 access_mask
|= SEC_STD_WRITE_DAC
;
1112 if (sd_flags
& SECINFO_SACL
) {
1113 access_mask
|= SEC_FLAG_SYSTEM_SECURITY
;
1116 status
= sec_access_check_ds(sd
, acl_user_token(module
),
1122 if (!NT_STATUS_IS_OK(status
)) {
1123 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1124 "Object %s has no write dacl access\n",
1125 ldb_dn_get_linearized(msg
->dn
));
1127 acl_user_token(module
),
1131 ret
= LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1134 } else if (ldb_attr_cmp("member", el
->name
) == 0) {
1135 ret
= acl_check_self_membership(tmp_ctx
,
1142 if (ret
!= LDB_SUCCESS
) {
1145 } else if (ldb_attr_cmp("dBCSPwd", el
->name
) == 0) {
1146 /* this one is not affected by any rights, we should let it through
1147 so that passwords_hash returns the correct error */
1149 } else if (ldb_attr_cmp("unicodePwd", el
->name
) == 0 ||
1150 (userPassword
&& ldb_attr_cmp("userPassword", el
->name
) == 0) ||
1151 ldb_attr_cmp("clearTextPassword", el
->name
) == 0) {
1152 ret
= acl_check_password_rights(tmp_ctx
,
1159 if (ret
!= LDB_SUCCESS
) {
1162 } else if (ldb_attr_cmp("servicePrincipalName", el
->name
) == 0) {
1163 ret
= acl_check_spn(tmp_ctx
,
1170 if (ret
!= LDB_SUCCESS
) {
1174 struct object_tree
*root
= NULL
;
1175 struct object_tree
*new_node
= NULL
;
1177 if (!insert_in_object_tree(tmp_ctx
,
1178 &objectclass
->schemaIDGUID
,
1180 &root
, &new_node
)) {
1181 talloc_free(tmp_ctx
);
1182 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1183 "acl_modify: Error adding new node in object tree.");
1186 if (!insert_in_object_tree(tmp_ctx
,
1187 &attr
->attributeSecurityGUID
, SEC_ADS_WRITE_PROP
,
1188 &new_node
, &new_node
)) {
1189 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1190 "acl_modify: cannot add to object tree securityGUID\n");
1191 ret
= LDB_ERR_OPERATIONS_ERROR
;
1195 if (!insert_in_object_tree(tmp_ctx
,
1196 &attr
->schemaIDGUID
, SEC_ADS_WRITE_PROP
, &new_node
, &new_node
)) {
1197 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1198 "acl_modify: cannot add to object tree attributeGUID\n");
1199 ret
= LDB_ERR_OPERATIONS_ERROR
;
1203 status
= sec_access_check_ds(sd
, acl_user_token(module
),
1208 if (!NT_STATUS_IS_OK(status
)) {
1209 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1210 "Object %s has no write property access\n",
1211 ldb_dn_get_linearized(msg
->dn
));
1213 acl_user_token(module
),
1217 ret
= LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1224 talloc_free(tmp_ctx
);
1225 return ldb_next_request(module
, req
);
1227 talloc_free(tmp_ctx
);
1231 /* similar to the modify for the time being.
1232 * We need to consider the special delete tree case, though - TODO */
1233 static int acl_delete(struct ldb_module
*module
, struct ldb_request
*req
)
1236 struct ldb_dn
*parent
;
1237 struct ldb_context
*ldb
;
1238 struct ldb_dn
*nc_root
;
1239 struct ldb_control
*as_system
;
1241 if (ldb_dn_is_special(req
->op
.del
.dn
)) {
1242 return ldb_next_request(module
, req
);
1245 as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
1246 if (as_system
!= NULL
) {
1247 as_system
->critical
= 0;
1250 if (dsdb_module_am_system(module
) || as_system
) {
1251 return ldb_next_request(module
, req
);
1254 DEBUG(10, ("ldb:acl_delete: %s\n", ldb_dn_get_linearized(req
->op
.del
.dn
)));
1256 ldb
= ldb_module_get_ctx(module
);
1258 parent
= ldb_dn_get_parent(req
, req
->op
.del
.dn
);
1259 if (parent
== NULL
) {
1260 return ldb_oom(ldb
);
1263 /* Make sure we aren't deleting a NC */
1265 ret
= dsdb_find_nc_root(ldb
, req
, req
->op
.del
.dn
, &nc_root
);
1266 if (ret
!= LDB_SUCCESS
) {
1269 if (ldb_dn_compare(nc_root
, req
->op
.del
.dn
) == 0) {
1270 talloc_free(nc_root
);
1271 DEBUG(10,("acl:deleting a NC\n"));
1272 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1273 return ldb_module_done(req
, NULL
, NULL
,
1274 LDB_ERR_UNWILLING_TO_PERFORM
);
1276 talloc_free(nc_root
);
1278 if (ldb_request_get_control(req
, LDB_CONTROL_TREE_DELETE_OID
)) {
1279 ret
= dsdb_module_check_access_on_dn(module
, req
,
1281 SEC_ADS_DELETE_TREE
, NULL
,
1283 if (ret
!= LDB_SUCCESS
) {
1287 return ldb_next_request(module
, req
);
1290 /* First check if we have delete object right */
1291 ret
= dsdb_module_check_access_on_dn(module
, req
, req
->op
.del
.dn
,
1292 SEC_STD_DELETE
, NULL
, req
);
1293 if (ret
== LDB_SUCCESS
) {
1294 return ldb_next_request(module
, req
);
1297 /* Nope, we don't have delete object. Lets check if we have delete
1298 * child on the parent */
1299 ret
= dsdb_module_check_access_on_dn(module
, req
, parent
,
1300 SEC_ADS_DELETE_CHILD
, NULL
, req
);
1301 if (ret
!= LDB_SUCCESS
) {
1305 return ldb_next_request(module
, req
);
1308 static int acl_rename(struct ldb_module
*module
, struct ldb_request
*req
)
1311 struct ldb_dn
*oldparent
;
1312 struct ldb_dn
*newparent
;
1313 const struct dsdb_schema
*schema
;
1314 const struct dsdb_class
*objectclass
;
1315 struct ldb_context
*ldb
;
1316 struct security_descriptor
*sd
= NULL
;
1317 struct dom_sid
*sid
= NULL
;
1318 struct ldb_result
*acl_res
;
1319 const struct GUID
*guid
;
1320 struct ldb_dn
*nc_root
;
1321 struct object_tree
*root
= NULL
;
1322 struct object_tree
*new_node
= NULL
;
1323 struct ldb_control
*as_system
;
1324 TALLOC_CTX
*tmp_ctx
;
1326 uint32_t access_granted
;
1327 const char *rdn_name
;
1328 static const char *acl_attrs
[] = {
1329 "nTSecurityDescriptor",
1335 if (ldb_dn_is_special(req
->op
.rename
.olddn
)) {
1336 return ldb_next_request(module
, req
);
1339 as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
1340 if (as_system
!= NULL
) {
1341 as_system
->critical
= 0;
1344 DEBUG(10, ("ldb:acl_rename: %s\n", ldb_dn_get_linearized(req
->op
.rename
.olddn
)));
1345 if (dsdb_module_am_system(module
) || as_system
) {
1346 return ldb_next_request(module
, req
);
1349 ldb
= ldb_module_get_ctx(module
);
1351 tmp_ctx
= talloc_new(req
);
1352 if (tmp_ctx
== NULL
) {
1353 return ldb_oom(ldb
);
1356 oldparent
= ldb_dn_get_parent(tmp_ctx
, req
->op
.rename
.olddn
);
1357 if (oldparent
== NULL
) {
1358 return ldb_oom(ldb
);
1360 newparent
= ldb_dn_get_parent(tmp_ctx
, req
->op
.rename
.newdn
);
1361 if (newparent
== NULL
) {
1362 return ldb_oom(ldb
);
1365 /* Make sure we aren't renaming/moving a NC */
1367 ret
= dsdb_find_nc_root(ldb
, req
, req
->op
.rename
.olddn
, &nc_root
);
1368 if (ret
!= LDB_SUCCESS
) {
1371 if (ldb_dn_compare(nc_root
, req
->op
.rename
.olddn
) == 0) {
1372 talloc_free(nc_root
);
1373 DEBUG(10,("acl:renaming/moving a NC\n"));
1374 /* Windows returns "ERR_UNWILLING_TO_PERFORM */
1375 return ldb_module_done(req
, NULL
, NULL
,
1376 LDB_ERR_UNWILLING_TO_PERFORM
);
1378 talloc_free(nc_root
);
1380 /* Look for the parent */
1382 ret
= dsdb_module_search_dn(module
, tmp_ctx
, &acl_res
,
1383 req
->op
.rename
.olddn
, acl_attrs
,
1384 DSDB_FLAG_NEXT_MODULE
|
1385 DSDB_FLAG_AS_SYSTEM
|
1386 DSDB_SEARCH_SHOW_RECYCLED
, req
);
1387 /* we sould be able to find the parent */
1388 if (ret
!= LDB_SUCCESS
) {
1389 DEBUG(10,("acl: failed to find object %s\n",
1390 ldb_dn_get_linearized(req
->op
.rename
.olddn
)));
1391 talloc_free(tmp_ctx
);
1395 schema
= dsdb_get_schema(ldb
, acl_res
);
1397 talloc_free(tmp_ctx
);
1398 return ldb_operr(ldb
);
1401 objectclass
= dsdb_get_structural_oc_from_msg(schema
, acl_res
->msgs
[0]);
1403 talloc_free(tmp_ctx
);
1404 return ldb_error(ldb
, LDB_ERR_OPERATIONS_ERROR
,
1405 "acl_modify: Error retrieving object class for GUID.");
1408 if (!insert_in_object_tree(tmp_ctx
, &objectclass
->schemaIDGUID
, SEC_ADS_WRITE_PROP
,
1409 &root
, &new_node
)) {
1410 talloc_free(tmp_ctx
);
1411 return ldb_operr(ldb
);
1414 guid
= attribute_schemaid_guid_by_lDAPDisplayName(schema
,
1416 if (!insert_in_object_tree(tmp_ctx
, guid
, SEC_ADS_WRITE_PROP
,
1417 &new_node
, &new_node
)) {
1418 talloc_free(tmp_ctx
);
1419 return ldb_operr(ldb
);
1422 rdn_name
= ldb_dn_get_rdn_name(req
->op
.rename
.olddn
);
1423 if (rdn_name
== NULL
) {
1424 talloc_free(tmp_ctx
);
1425 return ldb_operr(ldb
);
1427 guid
= attribute_schemaid_guid_by_lDAPDisplayName(schema
,
1429 if (!insert_in_object_tree(tmp_ctx
, guid
, SEC_ADS_WRITE_PROP
,
1430 &new_node
, &new_node
)) {
1431 talloc_free(tmp_ctx
);
1432 return ldb_operr(ldb
);
1435 ret
= dsdb_get_sd_from_ldb_message(ldb
, req
, acl_res
->msgs
[0], &sd
);
1437 if (ret
!= LDB_SUCCESS
) {
1438 talloc_free(tmp_ctx
);
1439 return ldb_operr(ldb
);
1441 /* Theoretically we pass the check if the object has no sd */
1443 talloc_free(tmp_ctx
);
1446 sid
= samdb_result_dom_sid(req
, acl_res
->msgs
[0], "objectSid");
1447 status
= sec_access_check_ds(sd
, acl_user_token(module
),
1453 if (!NT_STATUS_IS_OK(status
)) {
1454 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1455 "Object %s has no wp on name\n",
1456 ldb_dn_get_linearized(req
->op
.rename
.olddn
));
1458 acl_user_token(module
),
1459 req
->op
.rename
.olddn
,
1462 talloc_free(tmp_ctx
);
1463 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1466 if (ldb_dn_compare(oldparent
, newparent
) == 0) {
1467 /* regular rename, not move, nothing more to do */
1468 talloc_free(tmp_ctx
);
1469 return ldb_next_request(module
, req
);
1472 /* new parent should have create child */
1476 ret
= dsdb_module_check_access_on_dn(module
, req
, newparent
,
1477 SEC_ADS_CREATE_CHILD
,
1478 &objectclass
->schemaIDGUID
, req
);
1479 if (ret
!= LDB_SUCCESS
) {
1480 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1481 "acl:access_denied renaming %s",
1482 ldb_dn_get_linearized(req
->op
.rename
.olddn
));
1483 talloc_free(tmp_ctx
);
1486 /* do we have delete object on the object? */
1488 status
= sec_access_check_ds(sd
, acl_user_token(module
),
1494 if (NT_STATUS_IS_OK(status
)) {
1495 talloc_free(tmp_ctx
);
1496 return ldb_next_request(module
, req
);
1498 /* what about delete child on the current parent */
1499 ret
= dsdb_module_check_access_on_dn(module
, req
, oldparent
, SEC_ADS_DELETE_CHILD
, NULL
, req
);
1500 if (ret
!= LDB_SUCCESS
) {
1501 ldb_asprintf_errstring(ldb_module_get_ctx(module
),
1502 "acl:access_denied renaming %s", ldb_dn_get_linearized(req
->op
.rename
.olddn
));
1503 talloc_free(tmp_ctx
);
1504 return ldb_module_done(req
, NULL
, NULL
, ret
);
1507 talloc_free(tmp_ctx
);
1509 return ldb_next_request(module
, req
);
1512 static int acl_search_update_confidential_attrs(struct acl_context
*ac
,
1513 struct acl_private
*data
)
1515 struct dsdb_attribute
*a
;
1518 if (data
->acl_search
) {
1520 * If acl:search is activated, the acl_read module
1521 * protects confidential attributes.
1526 if ((ac
->schema
== data
->cached_schema_ptr
) &&
1527 (ac
->schema
->loaded_usn
== data
->cached_schema_loaded_usn
) &&
1528 (ac
->schema
->metadata_usn
== data
->cached_schema_metadata_usn
))
1533 data
->cached_schema_ptr
= NULL
;
1534 data
->cached_schema_loaded_usn
= 0;
1535 data
->cached_schema_metadata_usn
= 0;
1536 TALLOC_FREE(data
->confidential_attrs
);
1538 if (ac
->schema
== NULL
) {
1542 for (a
= ac
->schema
->attributes
; a
; a
= a
->next
) {
1543 const char **attrs
= data
->confidential_attrs
;
1545 if (!(a
->searchFlags
& SEARCH_FLAG_CONFIDENTIAL
)) {
1549 attrs
= talloc_realloc(data
, attrs
, const char *, n
+ 2);
1550 if (attrs
== NULL
) {
1551 TALLOC_FREE(data
->confidential_attrs
);
1552 return ldb_module_oom(ac
->module
);
1555 attrs
[n
] = a
->lDAPDisplayName
;
1559 data
->confidential_attrs
= attrs
;
1562 data
->cached_schema_ptr
= ac
->schema
;
1563 data
->cached_schema_loaded_usn
= ac
->schema
->loaded_usn
;
1564 data
->cached_schema_metadata_usn
= ac
->schema
->metadata_usn
;
1569 static int acl_search_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
1571 struct acl_context
*ac
;
1572 struct acl_private
*data
;
1573 struct ldb_result
*acl_res
;
1574 static const char *acl_attrs
[] = {
1576 "nTSecurityDescriptor",
1583 ac
= talloc_get_type(req
->context
, struct acl_context
);
1584 data
= talloc_get_type(ldb_module_get_private(ac
->module
), struct acl_private
);
1586 return ldb_module_done(ac
->req
, NULL
, NULL
,
1587 LDB_ERR_OPERATIONS_ERROR
);
1589 if (ares
->error
!= LDB_SUCCESS
) {
1590 return ldb_module_done(ac
->req
, ares
->controls
,
1591 ares
->response
, ares
->error
);
1594 switch (ares
->type
) {
1595 case LDB_REPLY_ENTRY
:
1596 if (ac
->constructed_attrs
) {
1597 ret
= dsdb_module_search_dn(ac
->module
, ac
, &acl_res
, ares
->message
->dn
,
1599 DSDB_FLAG_NEXT_MODULE
|
1600 DSDB_FLAG_AS_SYSTEM
|
1601 DSDB_SEARCH_SHOW_RECYCLED
,
1603 if (ret
!= LDB_SUCCESS
) {
1604 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1608 if (ac
->allowedAttributes
|| ac
->allowedAttributesEffective
) {
1609 ret
= acl_allowedAttributes(ac
->module
, ac
->schema
,
1612 if (ret
!= LDB_SUCCESS
) {
1613 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1617 if (ac
->allowedChildClasses
) {
1618 ret
= acl_childClasses(ac
->module
, ac
->schema
,
1621 "allowedChildClasses");
1622 if (ret
!= LDB_SUCCESS
) {
1623 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1627 if (ac
->allowedChildClassesEffective
) {
1628 ret
= acl_childClassesEffective(ac
->module
, ac
->schema
,
1631 if (ret
!= LDB_SUCCESS
) {
1632 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1636 if (ac
->sDRightsEffective
) {
1637 ret
= acl_sDRightsEffective(ac
->module
,
1640 if (ret
!= LDB_SUCCESS
) {
1641 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
1646 return ldb_module_send_entry(ac
->req
, ares
->message
,
1650 if (ac
->am_system
) {
1651 return ldb_module_send_entry(ac
->req
, ares
->message
,
1655 if (data
->password_attrs
!= NULL
) {
1656 for (i
= 0; data
->password_attrs
[i
]; i
++) {
1657 if ((!ac
->userPassword
) &&
1658 (ldb_attr_cmp(data
->password_attrs
[i
],
1659 "userPassword") == 0))
1664 ldb_msg_remove_attr(ares
->message
, data
->password_attrs
[i
]);
1668 if (ac
->am_administrator
) {
1669 return ldb_module_send_entry(ac
->req
, ares
->message
,
1673 ret
= acl_search_update_confidential_attrs(ac
, data
);
1674 if (ret
!= LDB_SUCCESS
) {
1678 if (data
->confidential_attrs
!= NULL
) {
1679 for (i
= 0; data
->confidential_attrs
[i
]; i
++) {
1680 ldb_msg_remove_attr(ares
->message
,
1681 data
->confidential_attrs
[i
]);
1685 return ldb_module_send_entry(ac
->req
, ares
->message
, ares
->controls
);
1687 case LDB_REPLY_REFERRAL
:
1688 return ldb_module_send_referral(ac
->req
, ares
->referral
);
1690 case LDB_REPLY_DONE
:
1691 return ldb_module_done(ac
->req
, ares
->controls
,
1692 ares
->response
, LDB_SUCCESS
);
1698 static int acl_search(struct ldb_module
*module
, struct ldb_request
*req
)
1700 struct ldb_context
*ldb
;
1701 struct acl_context
*ac
;
1702 struct ldb_parse_tree
*down_tree
;
1703 struct ldb_request
*down_req
;
1704 struct acl_private
*data
;
1708 if (ldb_dn_is_special(req
->op
.search
.base
)) {
1709 return ldb_next_request(module
, req
);
1712 ldb
= ldb_module_get_ctx(module
);
1714 ac
= talloc_zero(req
, struct acl_context
);
1716 return ldb_oom(ldb
);
1718 data
= talloc_get_type(ldb_module_get_private(module
), struct acl_private
);
1720 ac
->module
= module
;
1722 ac
->am_system
= dsdb_module_am_system(module
);
1723 ac
->am_administrator
= dsdb_module_am_administrator(module
);
1724 ac
->constructed_attrs
= false;
1725 ac
->modify_search
= true;
1726 ac
->allowedAttributes
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedAttributes");
1727 ac
->allowedAttributesEffective
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedAttributesEffective");
1728 ac
->allowedChildClasses
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedChildClasses");
1729 ac
->allowedChildClassesEffective
= ldb_attr_in_list(req
->op
.search
.attrs
, "allowedChildClassesEffective");
1730 ac
->sDRightsEffective
= ldb_attr_in_list(req
->op
.search
.attrs
, "sDRightsEffective");
1731 ac
->userPassword
= true;
1732 ac
->schema
= dsdb_get_schema(ldb
, ac
);
1734 ac
->constructed_attrs
|= ac
->allowedAttributes
;
1735 ac
->constructed_attrs
|= ac
->allowedChildClasses
;
1736 ac
->constructed_attrs
|= ac
->allowedChildClassesEffective
;
1737 ac
->constructed_attrs
|= ac
->allowedAttributesEffective
;
1738 ac
->constructed_attrs
|= ac
->sDRightsEffective
;
1741 ac
->modify_search
= false;
1743 if (ac
->am_system
) {
1744 ac
->modify_search
= false;
1747 if (!ac
->constructed_attrs
&& !ac
->modify_search
) {
1749 return ldb_next_request(module
, req
);
1752 if (!ac
->am_system
) {
1753 ac
->userPassword
= dsdb_user_password_support(module
, ac
, req
);
1756 ret
= acl_search_update_confidential_attrs(ac
, data
);
1757 if (ret
!= LDB_SUCCESS
) {
1761 down_tree
= ldb_parse_tree_copy_shallow(ac
, req
->op
.search
.tree
);
1762 if (down_tree
== NULL
) {
1763 return ldb_oom(ldb
);
1766 if (!ac
->am_system
&& data
->password_attrs
) {
1767 for (i
= 0; data
->password_attrs
[i
]; i
++) {
1768 if ((!ac
->userPassword
) &&
1769 (ldb_attr_cmp(data
->password_attrs
[i
],
1770 "userPassword") == 0))
1775 ldb_parse_tree_attr_replace(down_tree
,
1776 data
->password_attrs
[i
],
1777 "kludgeACLredactedattribute");
1781 if (!ac
->am_system
&& !ac
->am_administrator
&& data
->confidential_attrs
) {
1782 for (i
= 0; data
->confidential_attrs
[i
]; i
++) {
1783 ldb_parse_tree_attr_replace(down_tree
,
1784 data
->confidential_attrs
[i
],
1785 "kludgeACLredactedattribute");
1789 ret
= ldb_build_search_req_ex(&down_req
,
1791 req
->op
.search
.base
,
1792 req
->op
.search
.scope
,
1794 req
->op
.search
.attrs
,
1796 ac
, acl_search_callback
,
1798 LDB_REQ_SET_LOCATION(down_req
);
1799 if (ret
!= LDB_SUCCESS
) {
1802 /* perform the search */
1803 return ldb_next_request(module
, down_req
);
1806 static int acl_extended(struct ldb_module
*module
, struct ldb_request
*req
)
1808 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
1809 struct ldb_control
*as_system
= ldb_request_get_control(req
, LDB_CONTROL_AS_SYSTEM_OID
);
1811 /* allow everybody to read the sequence number */
1812 if (strcmp(req
->op
.extended
.oid
,
1813 LDB_EXTENDED_SEQUENCE_NUMBER
) == 0) {
1814 return ldb_next_request(module
, req
);
1817 if (dsdb_module_am_system(module
) ||
1818 dsdb_module_am_administrator(module
) || as_system
) {
1819 return ldb_next_request(module
, req
);
1821 ldb_asprintf_errstring(ldb
,
1823 "attempted database modify not permitted. "
1824 "User %s is not SYSTEM or an administrator",
1825 acl_user_name(req
, module
));
1826 return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
;
1830 static const struct ldb_module_ops ldb_acl_module_ops
= {
1832 .search
= acl_search
,
1834 .modify
= acl_modify
,
1836 .rename
= acl_rename
,
1837 .extended
= acl_extended
,
1838 .init_context
= acl_module_init
1841 int ldb_acl_module_init(const char *version
)
1843 LDB_MODULE_CHECK_VERSION(version
);
1844 return ldb_register_module(&ldb_acl_module_ops
);