4 Copyright (C) Simo Sorce 2006-2008
5 Copyright (C) Nadezhda Ivanova 2010
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * Component: ldb ACL Read module
26 * Description: Module that performs authorisation access checks on read requests
27 * Only DACL checks implemented at this point
29 * Author: Nadezhda Ivanova
33 #include "ldb_module.h"
34 #include "auth/auth.h"
35 #include "libcli/security/security.h"
36 #include "dsdb/samdb/samdb.h"
37 #include "librpc/gen_ndr/ndr_security.h"
38 #include "param/param.h"
39 #include "dsdb/samdb/ldb_modules/util.h"
42 struct aclread_context {
43 struct ldb_module *module;
44 struct ldb_request *req;
45 const char * const *attrs;
46 const struct dsdb_schema *schema;
48 bool added_nTSecurityDescriptor;
49 bool added_instanceType;
51 bool added_objectClass;
55 struct aclread_private {
59 static void aclread_mark_inaccesslible(struct ldb_message_element *el) {
60 el->flags |= LDB_FLAG_INTERNAL_INACCESSIBLE_ATTRIBUTE;
63 static bool aclread_is_inaccessible(struct ldb_message_element *el) {
64 return el->flags & LDB_FLAG_INTERNAL_INACCESSIBLE_ATTRIBUTE;
67 static int aclread_callback(struct ldb_request *req, struct ldb_reply *ares)
69 struct ldb_context *ldb;
70 struct aclread_context *ac;
71 struct ldb_message *ret_msg;
72 struct ldb_message *msg;
73 int ret, num_of_attrs = 0;
74 unsigned int i, k = 0;
75 struct security_descriptor *sd;
76 struct dom_sid *sid = NULL;
78 uint32_t instanceType;
79 const struct dsdb_class *objectclass;
81 ac = talloc_get_type(req->context, struct aclread_context);
82 ldb = ldb_module_get_ctx(ac->module);
84 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR );
86 if (ares->error != LDB_SUCCESS) {
87 return ldb_module_done(ac->req, ares->controls,
88 ares->response, ares->error);
90 tmp_ctx = talloc_new(ac);
94 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, msg, &sd);
95 if (ret != LDB_SUCCESS) {
96 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
97 "acl_read: cannot get descriptor of %s: %s\n",
98 ldb_dn_get_linearized(msg->dn), ldb_strerror(ret));
99 ret = LDB_ERR_OPERATIONS_ERROR;
101 } else if (sd == NULL) {
102 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
103 "acl_read: cannot get descriptor of %s (attribute not found)\n",
104 ldb_dn_get_linearized(msg->dn));
105 ret = LDB_ERR_OPERATIONS_ERROR;
109 * Get the most specific structural object class for the ACL check
111 objectclass = dsdb_get_structural_oc_from_msg(ac->schema, msg);
112 if (objectclass == NULL) {
113 ldb_asprintf_errstring(ldb, "acl_read: Failed to find a structural class for %s",
114 ldb_dn_get_linearized(msg->dn));
115 ret = LDB_ERR_OPERATIONS_ERROR;
119 sid = samdb_result_dom_sid(tmp_ctx, msg, "objectSid");
120 /* get the object instance type */
121 instanceType = ldb_msg_find_attr_as_uint(msg,
123 if (!ldb_dn_is_null(msg->dn) && !(instanceType & INSTANCE_TYPE_IS_NC_HEAD))
125 /* the object has a parent, so we have to check for visibility */
126 struct ldb_dn *parent_dn = ldb_dn_get_parent(tmp_ctx, msg->dn);
128 ret = dsdb_module_check_access_on_dn(ac->module,
133 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
134 talloc_free(tmp_ctx);
136 } else if (ret != LDB_SUCCESS) {
137 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
138 "acl_read: %s check parent %s - %s\n",
139 ldb_dn_get_linearized(msg->dn),
146 /* for every element in the message check RP */
147 for (i=0; i < msg->num_elements; i++) {
148 const struct dsdb_attribute *attr;
149 bool is_sd, is_objectsid, is_instancetype, is_objectclass;
150 uint32_t access_mask;
151 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
152 msg->elements[i].name);
154 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
155 "acl_read: %s cannot find attr[%s] in of schema\n",
156 ldb_dn_get_linearized(msg->dn),
157 msg->elements[i].name);
158 ret = LDB_ERR_OPERATIONS_ERROR;
161 is_sd = ldb_attr_cmp("nTSecurityDescriptor",
162 msg->elements[i].name) == 0;
163 is_objectsid = ldb_attr_cmp("objectSid",
164 msg->elements[i].name) == 0;
165 is_instancetype = ldb_attr_cmp("instanceType",
166 msg->elements[i].name) == 0;
167 is_objectclass = ldb_attr_cmp("objectClass",
168 msg->elements[i].name) == 0;
169 /* these attributes were added to perform access checks and must be removed */
170 if (is_objectsid && ac->added_objectSid) {
171 aclread_mark_inaccesslible(&msg->elements[i]);
174 if (is_instancetype && ac->added_instanceType) {
175 aclread_mark_inaccesslible(&msg->elements[i]);
178 if (is_objectclass && ac->added_objectClass) {
179 aclread_mark_inaccesslible(&msg->elements[i]);
182 if (is_sd && ac->added_nTSecurityDescriptor) {
183 aclread_mark_inaccesslible(&msg->elements[i]);
186 /* nTSecurityDescriptor is a special case */
190 if (ac->sd_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
191 access_mask |= SEC_STD_READ_CONTROL;
193 if (ac->sd_flags & SECINFO_DACL) {
194 access_mask |= SEC_STD_READ_CONTROL;
196 if (ac->sd_flags & SECINFO_SACL) {
197 access_mask |= SEC_FLAG_SYSTEM_SECURITY;
200 access_mask = SEC_ADS_READ_PROP;
203 if (attr->searchFlags & SEARCH_FLAG_CONFIDENTIAL) {
204 access_mask |= SEC_ADS_CONTROL_ACCESS;
207 if (access_mask == 0) {
208 aclread_mark_inaccesslible(&msg->elements[i]);
212 ret = acl_check_access_on_attribute(ac->module,
221 * Dirsync control needs the replpropertymetadata attribute
222 * so return it as it will be removed by the control
225 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
226 if (!ac->indirsync) {
228 * do not return this entry if attribute is
229 * part of the search filter
231 if (dsdb_attr_in_parse_tree(ac->req->op.search.tree,
232 msg->elements[i].name)) {
233 talloc_free(tmp_ctx);
236 aclread_mark_inaccesslible(&msg->elements[i]);
239 * We are doing dirysnc answers
240 * and the object shouldn't be returned (normally)
241 * but we will return it without replPropertyMetaData
242 * so that the dirysync module will do what is needed
243 * (remove the object if it is not deleted, or return
244 * just the objectGUID if it's deleted).
246 if (dsdb_attr_in_parse_tree(ac->req->op.search.tree,
247 msg->elements[i].name)) {
248 ldb_msg_remove_attr(msg, "replPropertyMetaData");
251 aclread_mark_inaccesslible(&msg->elements[i]);
254 } else if (ret != LDB_SUCCESS) {
255 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
256 "acl_read: %s check attr[%s] gives %s - %s\n",
257 ldb_dn_get_linearized(msg->dn),
258 msg->elements[i].name,
264 for (i=0; i < msg->num_elements; i++) {
265 if (!aclread_is_inaccessible(&msg->elements[i])) {
269 /*create a new message to return*/
270 ret_msg = ldb_msg_new(ac->req);
271 ret_msg->dn = msg->dn;
272 talloc_steal(ret_msg, msg->dn);
273 ret_msg->num_elements = num_of_attrs;
274 if (num_of_attrs > 0) {
275 ret_msg->elements = talloc_array(ret_msg,
276 struct ldb_message_element,
278 if (ret_msg->elements == NULL) {
281 for (i=0; i < msg->num_elements; i++) {
282 bool to_remove = aclread_is_inaccessible(&msg->elements[i]);
284 ret_msg->elements[k] = msg->elements[i];
285 talloc_steal(ret_msg->elements, msg->elements[i].name);
286 talloc_steal(ret_msg->elements, msg->elements[i].values);
291 * This should not be needed, but some modules
292 * may allocate values on the wrong context...
294 talloc_steal(ret_msg->elements, msg);
296 ret_msg->elements = NULL;
298 talloc_free(tmp_ctx);
300 return ldb_module_send_entry(ac->req, ret_msg, ares->controls);
301 case LDB_REPLY_REFERRAL:
302 return ldb_module_send_referral(ac->req, ares->referral);
304 return ldb_module_done(ac->req, ares->controls,
305 ares->response, LDB_SUCCESS);
310 talloc_free(tmp_ctx);
311 return ldb_module_done(ac->req, NULL, NULL, ret);
315 static int aclread_search(struct ldb_module *module, struct ldb_request *req)
317 struct ldb_context *ldb;
319 struct aclread_context *ac;
320 struct ldb_request *down_req;
321 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
322 uint32_t flags = ldb_req_get_custom_flags(req);
323 struct ldb_result *res;
324 struct aclread_private *p;
325 bool need_sd = false;
326 bool explicit_sd_flags = false;
327 bool is_untrusted = ldb_req_is_untrusted(req);
328 static const char * const _all_attrs[] = { "*", NULL };
329 bool all_attrs = false;
330 const char * const *attrs = NULL;
331 uint32_t instanceType;
332 static const char *acl_attrs[] = {
337 ldb = ldb_module_get_ctx(module);
338 p = talloc_get_type(ldb_module_get_private(module), struct aclread_private);
340 /* skip access checks if we are system or system control is supplied
341 * or this is not LDAP server request */
342 if (!p || !p->enabled ||
343 dsdb_module_am_system(module)
344 || as_system || !is_untrusted) {
345 return ldb_next_request(module, req);
347 /* no checks on special dn */
348 if (ldb_dn_is_special(req->op.search.base)) {
349 return ldb_next_request(module, req);
352 /* check accessibility of base */
353 if (!ldb_dn_is_null(req->op.search.base)) {
354 ret = dsdb_module_search_dn(module, req, &res, req->op.search.base,
356 DSDB_FLAG_NEXT_MODULE |
357 DSDB_FLAG_AS_SYSTEM |
358 DSDB_SEARCH_SHOW_RECYCLED,
360 if (ret != LDB_SUCCESS) {
361 return ldb_error(ldb, ret,
362 "acl_read: Error retrieving instanceType for base.");
364 instanceType = ldb_msg_find_attr_as_uint(res->msgs[0],
366 if (instanceType != 0 && !(instanceType & INSTANCE_TYPE_IS_NC_HEAD))
368 /* the object has a parent, so we have to check for visibility */
369 struct ldb_dn *parent_dn = ldb_dn_get_parent(req, req->op.search.base);
370 ret = dsdb_module_check_access_on_dn(module,
375 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
376 return ldb_module_done(req, NULL, NULL, LDB_ERR_NO_SUCH_OBJECT);
377 } else if (ret != LDB_SUCCESS) {
378 return ldb_module_done(req, NULL, NULL, ret);
382 ac = talloc_zero(req, struct aclread_context);
388 ac->schema = dsdb_get_schema(ldb, req);
389 if (flags & DSDB_ACL_CHECKS_DIRSYNC_FLAG) {
390 ac->indirsync = true;
392 ac->indirsync = false;
395 return ldb_operr(ldb);
398 attrs = req->op.search.attrs;
402 } else if (attrs[0] == NULL) {
405 } else if (ldb_attr_in_list(attrs, "*")) {
410 * In theory we should also check for the SD control but control verification is
411 * expensive so we'd better had the ntsecuritydescriptor to the list of
412 * searched attribute and then remove it !
414 ac->sd_flags = dsdb_request_sd_flags(ac->req, &explicit_sd_flags);
416 if (ldb_attr_in_list(attrs, "nTSecurityDescriptor")) {
418 } else if (explicit_sd_flags && all_attrs) {
425 if (!ldb_attr_in_list(attrs, "instanceType")) {
426 attrs = ldb_attr_list_copy_add(ac, attrs, "instanceType");
430 ac->added_instanceType = true;
432 if (!ldb_attr_in_list(req->op.search.attrs, "objectSid")) {
433 attrs = ldb_attr_list_copy_add(ac, attrs, "objectSid");
437 ac->added_objectSid = true;
439 if (!ldb_attr_in_list(req->op.search.attrs, "objectClass")) {
440 attrs = ldb_attr_list_copy_add(ac, attrs, "objectClass");
444 ac->added_objectClass = true;
449 attrs = ldb_attr_list_copy_add(ac, attrs, "nTSecurityDescriptor");
453 ac->added_nTSecurityDescriptor = true;
456 ac->attrs = req->op.search.attrs;
457 ret = ldb_build_search_req_ex(&down_req,
460 req->op.search.scope,
464 ac, aclread_callback,
467 if (ret != LDB_SUCCESS) {
468 return LDB_ERR_OPERATIONS_ERROR;
471 return ldb_next_request(module, down_req);
474 static int aclread_init(struct ldb_module *module)
476 struct ldb_context *ldb = ldb_module_get_ctx(module);
477 struct aclread_private *p = talloc_zero(module, struct aclread_private);
479 return ldb_module_oom(module);
481 p->enabled = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"), NULL, "acl", "search", true);
482 ldb_module_set_private(module, p);
483 return ldb_next_init(module);
486 static const struct ldb_module_ops ldb_aclread_module_ops = {
488 .search = aclread_search,
489 .init_context = aclread_init
492 int ldb_aclread_module_init(const char *version)
494 LDB_MODULE_CHECK_VERSION(version);
495 return ldb_register_module(&ldb_aclread_module_ops);