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;
52 struct aclread_private {
56 static int aclread_callback(struct ldb_request *req, struct ldb_reply *ares)
58 struct ldb_context *ldb;
59 struct aclread_context *ac;
62 struct security_descriptor *sd;
63 struct dom_sid *sid = NULL;
65 uint32_t instanceType;
67 ac = talloc_get_type(req->context, struct aclread_context);
68 ldb = ldb_module_get_ctx(ac->module);
70 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR );
72 if (ares->error != LDB_SUCCESS) {
73 return ldb_module_done(ac->req, ares->controls,
74 ares->response, ares->error);
76 tmp_ctx = talloc_new(ac);
79 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, ares->message, &sd);
80 if (ret != LDB_SUCCESS) {
81 DEBUG(10, ("acl_read: cannot get descriptor\n"));
82 ret = LDB_ERR_OPERATIONS_ERROR;
85 sid = samdb_result_dom_sid(tmp_ctx, ares->message, "objectSid");
86 /* get the object instance type */
87 instanceType = ldb_msg_find_attr_as_uint(ares->message,
89 if (!ldb_dn_is_null(ares->message->dn) && !(instanceType & INSTANCE_TYPE_IS_NC_HEAD))
91 /* the object has a parent, so we have to check for visibility */
92 struct ldb_dn *parent_dn = ldb_dn_get_parent(tmp_ctx, ares->message->dn);
93 ret = dsdb_module_check_access_on_dn(ac->module,
98 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
101 } else if (ret != LDB_SUCCESS) {
105 /* for every element in the message check RP */
107 while (i < ares->message->num_elements) {
108 const struct dsdb_attribute *attr;
109 attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
110 ares->message->elements[i].name);
112 DEBUG(2, ("acl_read: cannot find attribute %s in schema\n",
113 ares->message->elements[i].name));
114 ret = LDB_ERR_OPERATIONS_ERROR;
117 /* nTSecurityDescriptor is a special case */
118 if (ldb_attr_cmp("nTSecurityDescriptor",
119 ares->message->elements[i].name) == 0) {
121 ldb_msg_remove_attr(ares->message, ares->message->elements[i].name);
124 ret = acl_check_access_on_attribute(ac->module,
128 SEC_FLAG_SYSTEM_SECURITY|SEC_STD_READ_CONTROL,
131 ret = acl_check_access_on_attribute(ac->module,
138 if (ret == LDB_SUCCESS) {
140 } else if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
141 /* do not return this entry if attribute is
142 part of the search filter */
143 if (dsdb_attr_in_parse_tree(ac->req->op.search.tree,
144 ares->message->elements[i].name)) {
145 talloc_free(tmp_ctx);
148 ldb_msg_remove_attr(ares->message, ares->message->elements[i].name);
153 if (ac->instance_type) {
154 ldb_msg_remove_attr(ares->message, "instanceType");
156 if (ac->object_sid) {
157 ldb_msg_remove_attr(ares->message, "objectSid");
159 talloc_free(tmp_ctx);
160 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
161 case LDB_REPLY_REFERRAL:
162 return ldb_module_send_referral(ac->req, ares->referral);
164 return ldb_module_done(ac->req, ares->controls,
165 ares->response, LDB_SUCCESS);
170 talloc_free(tmp_ctx);
171 return ldb_module_done(ac->req, NULL, NULL, ret);
175 static int aclread_search(struct ldb_module *module, struct ldb_request *req)
177 struct ldb_context *ldb;
179 struct aclread_context *ac;
180 struct ldb_request *down_req;
181 struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
182 struct ldb_result *res;
183 struct aclread_private *p;
184 bool is_untrusted = ldb_req_is_untrusted(req);
185 const char * const *attrs = NULL;
186 uint32_t instanceType;
187 static const char *acl_attrs[] = {
192 ldb = ldb_module_get_ctx(module);
193 p = talloc_get_type(ldb_module_get_private(module), struct aclread_private);
195 /* skip access checks if we are system or system control is supplied
196 * or this is not LDAP server request */
197 if (!p || !p->enabled ||
198 dsdb_module_am_system(module)
199 || as_system || !is_untrusted) {
200 return ldb_next_request(module, req);
202 /* no checks on special dn */
203 if (ldb_dn_is_special(req->op.search.base)) {
204 return ldb_next_request(module, req);
207 /* check accessibility of base */
208 if (!ldb_dn_is_null(req->op.search.base)) {
209 ret = dsdb_module_search_dn(module, req, &res, req->op.search.base,
211 DSDB_FLAG_NEXT_MODULE |
212 DSDB_SEARCH_SHOW_DELETED);
213 if (ret != LDB_SUCCESS) {
214 return ldb_error(ldb, ret,
215 "acl_read: Error retrieving instanceType for base.");
217 instanceType = ldb_msg_find_attr_as_uint(res->msgs[0],
219 if (instanceType != 0 && !(instanceType & INSTANCE_TYPE_IS_NC_HEAD))
221 /* the object has a parent, so we have to check for visibility */
222 struct ldb_dn *parent_dn = ldb_dn_get_parent(req, req->op.search.base);
223 ret = dsdb_module_check_access_on_dn(module,
228 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
229 return ldb_module_done(req, NULL, NULL, LDB_ERR_NO_SUCH_OBJECT);
230 } else if (ret != LDB_SUCCESS) {
231 return ldb_module_done(req, NULL, NULL, ret);
235 ac = talloc_zero(req, struct aclread_context);
241 ac->schema = dsdb_get_schema(ldb, req);
243 return ldb_operr(ldb);
245 ac->sd = !(ldb_attr_in_list(req->op.search.attrs, "nTSecurityDescriptor"));
246 if (req->op.search.attrs && !ldb_attr_in_list(req->op.search.attrs, "*")) {
247 if (!ldb_attr_in_list(req->op.search.attrs, "instanceType")) {
248 ac->instance_type = true;
249 attrs = ldb_attr_list_copy_add(ac, req->op.search.attrs, "instanceType");
251 attrs = req->op.search.attrs;
253 if (!ldb_attr_in_list(req->op.search.attrs, "objectSid")) {
254 ac->object_sid = true;
255 attrs = ldb_attr_list_copy_add(ac, attrs, "objectSid");
260 /* avoid replacing all attributes with nTSecurityDescriptor
261 * if attribute list is empty */
263 attrs = ldb_attr_list_copy_add(ac, attrs, "*");
265 attrs = ldb_attr_list_copy_add(ac, attrs, "nTSecurityDescriptor");
267 ac->attrs = req->op.search.attrs;
268 ret = ldb_build_search_req_ex(&down_req,
271 req->op.search.scope,
275 ac, aclread_callback,
278 if (ret != LDB_SUCCESS) {
279 return LDB_ERR_OPERATIONS_ERROR;
282 return ldb_next_request(module, down_req);
285 static int aclread_init(struct ldb_module *module)
287 struct ldb_context *ldb = ldb_module_get_ctx(module);
288 struct aclread_private *p = talloc_zero(module, struct aclread_private);
290 return ldb_module_oom(module);
292 p->enabled = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"), NULL, "acl", "search", false);
293 ldb_module_set_private(module, p);
294 return ldb_next_init(module);
297 static const struct ldb_module_ops ldb_aclread_module_ops = {
299 .search = aclread_search,
300 .init_context = aclread_init
303 int ldb_aclread_module_init(const char *version)
305 LDB_MODULE_CHECK_VERSION(version);
306 return ldb_register_module(&ldb_aclread_module_ops);