81f9bf62842245f74b4d684cf2ecb125198baa6c
[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 };
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         struct aclread_context *ac;
195         struct ldb_request *down_req;
196         struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
197         struct ldb_result *res;
198         struct ldb_message_element *parent;
199         struct aclread_private *p;
200         bool is_untrusted = ldb_req_is_untrusted(req);
201         static const char *acl_attrs[] = {
202                  "parentGUID",
203                  NULL
204         };
205
206         ldb = ldb_module_get_ctx(module);
207         p = talloc_get_type(ldb_module_get_private(module), struct aclread_private);
208
209         /* skip access checks if we are system or system control is supplied
210          * or this is not LDAP server request */
211         if (!p || !p->enabled ||
212             dsdb_module_am_system(module)
213             || as_system || !is_untrusted) {
214                 return ldb_next_request(module, req);
215         }
216         /* no checks on special dn */
217         if (ldb_dn_is_special(req->op.search.base)) {
218                 return ldb_next_request(module, req);
219         }
220
221         /* check accessibility of base */
222         if (!ldb_dn_is_null(req->op.search.base)) {
223                 ret = dsdb_module_search_dn(module, req, &res, req->op.search.base,
224                                             acl_attrs,
225                                             DSDB_FLAG_NEXT_MODULE |
226                                             DSDB_SEARCH_SHOW_DELETED);
227                 if (ret != LDB_SUCCESS) {
228                         return ldb_error(ldb, ret,
229                                          "acl_read: Error retrieving SD for base.");
230                 }
231
232                 parent = ldb_msg_find_element(res->msgs[0], "parentGUID");
233                 if (parent) {
234                         /* the object has a parent, so we have to check for visibility */
235                         struct GUID parent_guid = samdb_result_guid(res->msgs[0], "parentGUID");
236                         ret = dsdb_module_check_access_on_guid(module,
237                                                                req,
238                                                                &parent_guid,
239                                                                SEC_ADS_LIST,
240                                                                NULL);
241                         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
242                                 return ldb_module_done(req, NULL, NULL, LDB_ERR_NO_SUCH_OBJECT);
243                         } else if (ret != LDB_SUCCESS) {
244                                 return ldb_module_done(req, NULL, NULL, ret);
245                         }
246                 }
247         }
248         ac = talloc_zero(req, struct aclread_context);
249         if (ac == NULL) {
250                 return ldb_oom(ldb);
251         }
252         ac->module = module;
253         ac->req = req;
254         ac->schema = dsdb_get_schema(ldb, req);
255         if (!ac->schema) {
256                 return ldb_operr(ldb);
257         }
258
259         ret = ldb_build_search_req_ex(&down_req,
260                                       ldb, ac,
261                                       req->op.search.base,
262                                       req->op.search.scope,
263                                       req->op.search.tree,
264                                       req->op.search.attrs,
265                                       req->controls,
266                                       ac, aclread_callback,
267                                       req);
268
269         if (ret != LDB_SUCCESS) {
270                 return LDB_ERR_OPERATIONS_ERROR;
271         }
272
273         return ldb_next_request(module, down_req);
274 }
275
276 static int aclread_init(struct ldb_module *module)
277 {
278         struct ldb_context *ldb = ldb_module_get_ctx(module);
279         struct aclread_private *p = talloc_zero(module, struct aclread_private);
280         if (p == NULL) {
281                 return ldb_module_oom(module);
282         }
283         p->enabled = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"), NULL, "acl", "search", false);
284         ldb_module_set_private(module, p);
285         return ldb_next_init(module);
286 }
287
288 static const struct ldb_module_ops ldb_aclread_module_ops = {
289         .name              = "aclread",
290         .search            = aclread_search,
291         .init_context      = aclread_init
292 };
293
294 int ldb_aclread_module_init(const char *version)
295 {
296         LDB_MODULE_CHECK_VERSION(version);
297         return ldb_register_module(&ldb_aclread_module_ops);
298 }