s4-acl: Remove unused variables from aclread module.
[metze/samba/wip.git] / source4 / dsdb / samdb / ldb_modules / acl_read.c
1 /*
2   ldb database library
3
4   Copyright (C) Simo Sorce 2006-2008
5   Copyright (C) Nadezhda Ivanova 2010
6
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.
11
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.
16
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/>.
19 */
20
21 /*
22  *  Name: ldb
23  *
24  *  Component: ldb ACL Read module
25  *
26  *  Description: Module that performs authorisation access checks on read requests
27  *               Only DACL checks implemented at this point
28  *
29  *  Author: Nadezhda Ivanova
30  */
31
32 #include "includes.h"
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"
40
41
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;
47         bool sd;
48         bool instance_type;
49         bool object_sid;
50 };
51
52 struct aclread_private {
53         bool enabled;
54 };
55
56 static int aclread_callback(struct ldb_request *req, struct ldb_reply *ares)
57 {
58          struct ldb_context *ldb;
59          struct aclread_context *ac;
60          int ret;
61          unsigned int i;
62          struct security_descriptor *sd;
63          struct dom_sid *sid = NULL;
64          TALLOC_CTX *tmp_ctx;
65          uint32_t instanceType;
66
67          ac = talloc_get_type(req->context, struct aclread_context);
68          ldb = ldb_module_get_ctx(ac->module);
69          if (!ares) {
70                  return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR );
71          }
72          if (ares->error != LDB_SUCCESS) {
73                  return ldb_module_done(ac->req, ares->controls,
74                                         ares->response, ares->error);
75          }
76          tmp_ctx = talloc_new(ac);
77          switch (ares->type) {
78          case LDB_REPLY_ENTRY:
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;
83                          goto fail;
84                  }
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,
88                                                          "instanceType", 0);
89                  if (!ldb_dn_is_null(ares->message->dn) && !(instanceType & INSTANCE_TYPE_IS_NC_HEAD))
90                  {
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,
94                                                              tmp_ctx,
95                                                              parent_dn,
96                                                              SEC_ADS_LIST,
97                                                              NULL);
98                         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
99                                 talloc_free(tmp_ctx);
100                                 return LDB_SUCCESS;
101                         } else if (ret != LDB_SUCCESS) {
102                                 goto fail;
103                         }
104                  }
105                  /* for every element in the message check RP */
106                  i = 0;
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);
111                          if (!attr) {
112                                  DEBUG(2, ("acl_read: cannot find attribute %s in schema\n",
113                                            ares->message->elements[i].name));
114                                  ret = LDB_ERR_OPERATIONS_ERROR;
115                                  goto fail;
116                          }
117                          /* nTSecurityDescriptor is a special case */
118                          if (ldb_attr_cmp("nTSecurityDescriptor",
119                                           ares->message->elements[i].name) == 0) {
120                                  if (ac->sd) {
121                                          ldb_msg_remove_attr(ares->message, ares->message->elements[i].name);
122                                          ret = LDB_SUCCESS;
123                                  }
124                                  ret = acl_check_access_on_attribute(ac->module,
125                                                                      tmp_ctx,
126                                                                      sd,
127                                                                      sid,
128                                                                      SEC_FLAG_SYSTEM_SECURITY|SEC_STD_READ_CONTROL,
129                                                                      attr);
130                          } else {
131                                  ret = acl_check_access_on_attribute(ac->module,
132                                                                      tmp_ctx,
133                                                                      sd,
134                                                                      sid,
135                                                                      SEC_ADS_READ_PROP,
136                                                                      attr);
137                          }
138                          if (ret == LDB_SUCCESS) {
139                                  i++;
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);
146                                          return LDB_SUCCESS;
147                                  }
148                                  ldb_msg_remove_attr(ares->message, ares->message->elements[i].name);
149                          } else {
150                                  goto fail;
151                          }
152                  }
153                  if (ac->instance_type) {
154                          ldb_msg_remove_attr(ares->message, "instanceType");
155                  }
156                  if (ac->object_sid) {
157                          ldb_msg_remove_attr(ares->message, "objectSid");
158                  }
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);
163          case LDB_REPLY_DONE:
164                  return ldb_module_done(ac->req, ares->controls,
165                                         ares->response, LDB_SUCCESS);
166
167          }
168          return LDB_SUCCESS;
169 fail:
170          talloc_free(tmp_ctx);
171          return ldb_module_done(ac->req, NULL, NULL, ret);
172 }
173
174
175 static int aclread_search(struct ldb_module *module, struct ldb_request *req)
176 {
177         struct ldb_context *ldb;
178         int ret;
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[] = {
188                  "instanceType",
189                  NULL
190         };
191
192         ldb = ldb_module_get_ctx(module);
193         p = talloc_get_type(ldb_module_get_private(module), struct aclread_private);
194
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);
201         }
202         /* no checks on special dn */
203         if (ldb_dn_is_special(req->op.search.base)) {
204                 return ldb_next_request(module, req);
205         }
206
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,
210                                             acl_attrs,
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.");
216                 }
217                 instanceType = ldb_msg_find_attr_as_uint(res->msgs[0],
218                                                          "instanceType", 0);
219                 if (instanceType != 0 && !(instanceType & INSTANCE_TYPE_IS_NC_HEAD))
220                 {
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,
224                                                              req,
225                                                              parent_dn,
226                                                              SEC_ADS_LIST,
227                                                              NULL);
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);
232                         }
233                 }
234         }
235         ac = talloc_zero(req, struct aclread_context);
236         if (ac == NULL) {
237                 return ldb_oom(ldb);
238         }
239         ac->module = module;
240         ac->req = req;
241         ac->schema = dsdb_get_schema(ldb, req);
242         if (!ac->schema) {
243                 return ldb_operr(ldb);
244         }
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");
250                 } else {
251                         attrs = req->op.search.attrs;
252                 }
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");
256                 }
257         }
258
259         if (ac->sd) {
260                 /* avoid replacing all attributes with nTSecurityDescriptor
261                  * if attribute list is empty */
262                 if (!attrs) {
263                         attrs = ldb_attr_list_copy_add(ac, attrs, "*");
264                 }
265                 attrs = ldb_attr_list_copy_add(ac, attrs, "nTSecurityDescriptor");
266         }
267         ac->attrs = req->op.search.attrs;
268         ret = ldb_build_search_req_ex(&down_req,
269                                       ldb, ac,
270                                       req->op.search.base,
271                                       req->op.search.scope,
272                                       req->op.search.tree,
273                                       attrs,
274                                       req->controls,
275                                       ac, aclread_callback,
276                                       req);
277
278         if (ret != LDB_SUCCESS) {
279                 return LDB_ERR_OPERATIONS_ERROR;
280         }
281
282         return ldb_next_request(module, down_req);
283 }
284
285 static int aclread_init(struct ldb_module *module)
286 {
287         struct ldb_context *ldb = ldb_module_get_ctx(module);
288         struct aclread_private *p = talloc_zero(module, struct aclread_private);
289         if (p == NULL) {
290                 return ldb_module_oom(module);
291         }
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);
295 }
296
297 static const struct ldb_module_ops ldb_aclread_module_ops = {
298         .name              = "aclread",
299         .search            = aclread_search,
300         .init_context      = aclread_init
301 };
302
303 int ldb_aclread_module_init(const char *version)
304 {
305         LDB_MODULE_CHECK_VERSION(version);
306         return ldb_register_module(&ldb_aclread_module_ops);
307 }