s4-dsdb Remove mem_ctx argument from dsdb_module_find_dsheuristics().
[mat/samba.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 };
48
49 struct aclread_private {
50         bool enabled;
51 };
52
53 static int aclread_callback(struct ldb_request *req, struct ldb_reply *ares)
54 {
55          struct ldb_context *ldb;
56          struct aclread_context *ac;
57          struct ldb_result *acl_res;
58          struct ldb_message_element *parent;
59          static const char *acl_attrs[] = {
60                  "nTSecurityDescriptor",
61                  "objectSid",
62                  "parentGUID",
63                  NULL
64          };
65          int ret;
66          unsigned int i;
67          struct security_descriptor *sd;
68          struct dom_sid *sid = NULL;
69          TALLOC_CTX *tmp_ctx;
70          ac = talloc_get_type(req->context, struct aclread_context);
71          ldb = ldb_module_get_ctx(ac->module);
72          if (!ares) {
73                  return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR );
74          }
75          if (ares->error != LDB_SUCCESS) {
76                  return ldb_module_done(ac->req, ares->controls,
77                                         ares->response, ares->error);
78          }
79          tmp_ctx = talloc_new(ac);
80          switch (ares->type) {
81          case LDB_REPLY_ENTRY:
82                  ret = dsdb_module_search_dn(ac->module, tmp_ctx, &acl_res, ares->message->dn,
83                                              acl_attrs,
84                                              DSDB_FLAG_NEXT_MODULE |
85                                              DSDB_SEARCH_SHOW_DELETED);
86                  if (ret != LDB_SUCCESS) {
87                          goto fail;
88                  }
89                  ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, acl_res->msgs[0], &sd);
90                  if (ret != LDB_SUCCESS) {
91                          DEBUG(10, ("acl_read: cannot get descriptor\n"));
92                          ret = LDB_ERR_OPERATIONS_ERROR;
93                          goto fail;
94                  }
95                  sid = samdb_result_dom_sid(tmp_ctx, acl_res->msgs[0], "objectSid");
96                  /* get the parent guid */
97                  parent = ldb_msg_find_element(acl_res->msgs[0], "parentGUID");
98                  if (parent) {
99                          /* the object has a parent, so we have to check for visibility */
100                          struct GUID parent_guid = samdb_result_guid(acl_res->msgs[0], "parentGUID");
101                          ret = dsdb_module_check_access_on_guid(ac->module,
102                                                                 tmp_ctx,
103                                                                 &parent_guid,
104                                                                 SEC_ADS_LIST,
105                                                                 NULL);
106                          if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
107                                  talloc_free(tmp_ctx);
108                                  return LDB_SUCCESS;
109                          } else if (ret != LDB_SUCCESS) {
110                                  goto fail;
111                          }
112                  }
113                  /* for every element in the message check RP */
114                  i = 0;
115                  while (i < ares->message->num_elements) {
116                          char *p, *new_attr;
117                          const struct dsdb_attribute *attr;
118                          p = strchr(ares->message->elements[i].name, ';');
119                          if (!p) {
120                                  attr =  dsdb_attribute_by_lDAPDisplayName(ac->schema,
121                                                                            ares->message->elements[i].name);
122                          } else {
123                                  new_attr = talloc_strndup(tmp_ctx,
124                                                            ares->message->elements[i].name,
125                                                            (size_t)(p -ares->message->elements[i].name));
126                                  if (!new_attr) {
127                                          ldb_oom(ldb);
128                                          ret = LDB_ERR_OPERATIONS_ERROR;
129                                          goto fail;
130                                  }
131                                  attr =  dsdb_attribute_by_lDAPDisplayName(ac->schema,
132                                                                            new_attr);
133                                  talloc_free(new_attr);
134                          }
135
136                          if (!attr) {
137                                  DEBUG(2, ("acl_read: cannot find attribute %s in schema\n",
138                                            ares->message->elements[i].name));
139                                  ret = LDB_ERR_OPERATIONS_ERROR;
140                                  goto fail;
141                          }
142                          /* nTSecurityDescriptor is a special case */
143                          if (ldb_attr_cmp("nTSecurityDescriptor",
144                                           ares->message->elements[i].name) == 0) {
145                                  ret = acl_check_access_on_attribute(ac->module,
146                                                                      tmp_ctx,
147                                                                      sd,
148                                                                      sid,
149                                                                      SEC_FLAG_SYSTEM_SECURITY|SEC_STD_READ_CONTROL,
150                                                                      attr);
151                          } else {
152                                  ret = acl_check_access_on_attribute(ac->module,
153                                                                      tmp_ctx,
154                                                                      sd,
155                                                                      sid,
156                                                                      SEC_ADS_READ_PROP,
157                                                                      attr);
158                          }
159                          if (ret == LDB_SUCCESS) {
160                                  i++;
161                          } else if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
162                                  /* do not return this entry if attribute is
163                                             part of the search filter */
164                                  if (dsdb_attr_in_parse_tree(ac->req->op.search.tree,
165                                                              ares->message->elements[i].name)) {
166                                          talloc_free(tmp_ctx);
167                                          return LDB_SUCCESS;
168                                  }
169                                  ldb_msg_remove_attr(ares->message, ares->message->elements[i].name);
170                          } else {
171                                  goto fail;
172                          }
173                  }
174                  talloc_free(tmp_ctx);
175                  return ldb_module_send_entry(ac->req, ares->message, ares->controls);
176          case LDB_REPLY_REFERRAL:
177                  return ldb_module_send_referral(ac->req, ares->referral);
178          case LDB_REPLY_DONE:
179                  return ldb_module_done(ac->req, ares->controls,
180                                         ares->response, LDB_SUCCESS);
181
182          }
183          return LDB_SUCCESS;
184 fail:
185          talloc_free(tmp_ctx);
186          return ldb_module_done(ac->req, NULL, NULL, ret);
187 }
188
189
190 static int aclread_search(struct ldb_module *module, struct ldb_request *req)
191 {
192         struct ldb_context *ldb;
193         int ret;
194         bool block_anonymous;
195         struct aclread_context *ac;
196         struct ldb_request *down_req;
197         struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
198         struct auth_session_info *session_info;
199         struct ldb_result *res;
200         struct ldb_message_element *parent;
201         struct aclread_private *p;
202         bool is_untrusted = ldb_req_is_untrusted(req);
203         static const char *acl_attrs[] = {
204                  "parentGUID",
205                  NULL
206         };
207
208         ldb = ldb_module_get_ctx(module);
209         p = talloc_get_type(ldb_module_get_private(module), struct aclread_private);
210
211         /* skip access checks if we are system or system control is supplied
212          * or this is not LDAP server request */
213         if (!p || !p->enabled ||
214             dsdb_module_am_system(module)
215             || as_system || !is_untrusted) {
216                 return ldb_next_request(module, req);
217         }
218         /* no checks on special dn */
219         if (ldb_dn_is_special(req->op.search.base)) {
220                 return ldb_next_request(module, req);
221         }
222         /* allow all access to rootDSE */
223         if (req->op.search.scope == LDB_SCOPE_BASE && ldb_dn_is_null(req->op.search.base)) {
224                 return ldb_next_request(module, req);
225         }
226
227         session_info = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
228         if (session_info && security_token_is_anonymous(session_info->security_token)) {
229                 block_anonymous = dsdb_block_anonymous_ops(module);
230                 if (block_anonymous) {
231                         return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
232                                          "This request is not allowed to an anonymous connection.");
233                 }
234         }
235
236         /* check accessibility of base */
237         if (!ldb_dn_is_null(req->op.search.base)) {
238                 ret = dsdb_module_search_dn(module, req, &res, req->op.search.base,
239                                             acl_attrs,
240                                             DSDB_FLAG_NEXT_MODULE |
241                                             DSDB_SEARCH_SHOW_DELETED);
242                 if (ret != LDB_SUCCESS) {
243                         return ldb_error(ldb, ret,
244                                          "acl_read: Error retrieving SD for base.");
245                 }
246
247                 parent = ldb_msg_find_element(res->msgs[0], "parentGUID");
248                 if (parent) {
249                         /* the object has a parent, so we have to check for visibility */
250                         struct GUID parent_guid = samdb_result_guid(res->msgs[0], "parentGUID");
251                         ret = dsdb_module_check_access_on_guid(module,
252                                                                req,
253                                                                &parent_guid,
254                                                                SEC_ADS_LIST,
255                                                                NULL);
256                         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
257                                 return ldb_module_done(req, NULL, NULL, LDB_ERR_NO_SUCH_OBJECT);
258                         } else if (ret != LDB_SUCCESS) {
259                                 return ldb_module_done(req, NULL, NULL, ret);
260                         }
261                 }
262         }
263         ac = talloc_zero(req, struct aclread_context);
264         if (ac == NULL) {
265                 return ldb_oom(ldb);
266         }
267         ac->module = module;
268         ac->req = req;
269         ac->schema = dsdb_get_schema(ldb, req);
270         if (!ac->schema) {
271                 return ldb_operr(ldb);
272         }
273
274         ret = ldb_build_search_req_ex(&down_req,
275                                       ldb, ac,
276                                       req->op.search.base,
277                                       req->op.search.scope,
278                                       req->op.search.tree,
279                                       req->op.search.attrs,
280                                       req->controls,
281                                       ac, aclread_callback,
282                                       req);
283
284         if (ret != LDB_SUCCESS) {
285                 return LDB_ERR_OPERATIONS_ERROR;
286         }
287
288         return ldb_next_request(module, down_req);
289 }
290
291 static int aclread_init(struct ldb_module *module)
292 {
293         struct ldb_context *ldb = ldb_module_get_ctx(module);
294         struct aclread_private *p = talloc_zero(module, struct aclread_private);
295         if (p == NULL) {
296                 return ldb_module_oom(module);
297         }
298         p->enabled = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"), NULL, "acl", "search", false);
299         ldb_module_set_private(module, p);
300         return ldb_next_init(module);
301 }
302
303 static const struct ldb_module_ops ldb_aclread_module_ops = {
304         .name              = "aclread",
305         .search            = aclread_search,
306         .init_context      = aclread_init
307 };
308
309 int ldb_aclread_module_init(const char *version)
310 {
311         LDB_MODULE_CHECK_VERSION(version);
312         return ldb_register_module(&ldb_aclread_module_ops);
313 }