s4:dsdb/acl: only give administrators access to attributes marked as confidential...
[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         bool indirsync;
51 };
52
53 struct aclread_private {
54         bool enabled;
55 };
56
57 static void aclread_mark_inaccesslible(struct ldb_message_element *el) {
58          el->flags |= LDB_FLAG_INTERNAL_INACCESSIBLE_ATTRIBUTE;
59 }
60
61 static bool aclread_is_inaccessible(struct ldb_message_element *el) {
62         return el->flags & LDB_FLAG_INTERNAL_INACCESSIBLE_ATTRIBUTE;
63 }
64
65 static int aclread_callback(struct ldb_request *req, struct ldb_reply *ares)
66 {
67          struct ldb_context *ldb;
68          struct aclread_context *ac;
69          struct ldb_message *ret_msg;
70          struct ldb_message *msg;
71          int ret, num_of_attrs = 0;
72          unsigned int i, k = 0;
73          struct security_descriptor *sd;
74          struct dom_sid *sid = NULL;
75          TALLOC_CTX *tmp_ctx;
76          uint32_t instanceType;
77
78          ac = talloc_get_type(req->context, struct aclread_context);
79          ldb = ldb_module_get_ctx(ac->module);
80          if (!ares) {
81                  return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR );
82          }
83          if (ares->error != LDB_SUCCESS) {
84                  return ldb_module_done(ac->req, ares->controls,
85                                         ares->response, ares->error);
86          }
87          tmp_ctx = talloc_new(ac);
88          switch (ares->type) {
89          case LDB_REPLY_ENTRY:
90                  msg = ares->message;
91                  ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, msg, &sd);
92                  if (ret != LDB_SUCCESS || sd == NULL ) {
93                          DEBUG(10, ("acl_read: cannot get descriptor\n"));
94                          ret = LDB_ERR_OPERATIONS_ERROR;
95                          goto fail;
96                  }
97                  sid = samdb_result_dom_sid(tmp_ctx, msg, "objectSid");
98                  /* get the object instance type */
99                  instanceType = ldb_msg_find_attr_as_uint(msg,
100                                                          "instanceType", 0);
101                  if (!ldb_dn_is_null(msg->dn) && !(instanceType & INSTANCE_TYPE_IS_NC_HEAD))
102                  {
103                         /* the object has a parent, so we have to check for visibility */
104                         struct ldb_dn *parent_dn = ldb_dn_get_parent(tmp_ctx, msg->dn);
105                         ret = dsdb_module_check_access_on_dn(ac->module,
106                                                              tmp_ctx,
107                                                              parent_dn,
108                                                              SEC_ADS_LIST,
109                                                              NULL, req);
110                         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
111                                 talloc_free(tmp_ctx);
112                                 return LDB_SUCCESS;
113                         } else if (ret != LDB_SUCCESS) {
114                                 goto fail;
115                         }
116                  }
117                  /* for every element in the message check RP */
118                  for (i=0; i < msg->num_elements; i++) {
119                          const struct dsdb_attribute *attr;
120                          bool is_sd, is_objectsid, is_instancetype;
121                          uint32_t access_mask;
122                          attr =  dsdb_attribute_by_lDAPDisplayName(ac->schema,
123                                                                    msg->elements[i].name);
124                          if (!attr) {
125                                  DEBUG(2, ("acl_read: cannot find attribute %s in schema\n",
126                                            msg->elements[i].name));
127                                  ret = LDB_ERR_OPERATIONS_ERROR;
128                                  goto fail;
129                          }
130                          is_sd = ldb_attr_cmp("nTSecurityDescriptor",
131                                               msg->elements[i].name) == 0;
132                          is_objectsid = ldb_attr_cmp("objectSid",
133                                                      msg->elements[i].name) == 0;
134                          is_instancetype = ldb_attr_cmp("instanceType",
135                                                         msg->elements[i].name) == 0;
136                          /* these attributes were added to perform access checks and must be removed */
137                          if (is_objectsid && ac->object_sid) {
138                                  aclread_mark_inaccesslible(&msg->elements[i]);
139                                  continue;
140                          }
141                          if (is_instancetype && ac->instance_type) {
142                                  aclread_mark_inaccesslible(&msg->elements[i]);
143                                  continue;
144                          }
145                          if (is_sd && ac->sd) {
146                                  aclread_mark_inaccesslible(&msg->elements[i]);
147                                  continue;
148                          }
149                          /* nTSecurityDescriptor is a special case */
150                          if (is_sd) {
151                                  access_mask = SEC_FLAG_SYSTEM_SECURITY|SEC_STD_READ_CONTROL;
152                          } else {
153                                  access_mask = SEC_ADS_READ_PROP;
154                          }
155                          ret = acl_check_access_on_attribute(ac->module,
156                                                              tmp_ctx,
157                                                              sd,
158                                                              sid,
159                                                              access_mask,
160                                                              attr);
161
162                         /*
163                          * Dirsync control needs the replpropertymetadata attribute
164                          * so return it as it will be removed by the control
165                          * in anycase.
166                          */
167                         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
168                                 if (!ac->indirsync) {
169                                         /* do not return this entry if attribute is
170                                         part of the search filter */
171                                         if (dsdb_attr_in_parse_tree(ac->req->op.search.tree,
172                                                                 msg->elements[i].name)) {
173                                                 talloc_free(tmp_ctx);
174                                                 return LDB_SUCCESS;
175                                         }
176                                         aclread_mark_inaccesslible(&msg->elements[i]);
177                                 } else {
178                                         /*
179                                          * We are doing dirysnc answers
180                                          * and the object shouldn't be returned (normally)
181                                          * but we will return it without replPropertyMetaData
182                                          * so that the dirysync module will do what is needed
183                                          * (remove the object if it is not deleted, or return
184                                          * just the objectGUID if it's deleted).
185                                          */
186                                         if (dsdb_attr_in_parse_tree(ac->req->op.search.tree,
187                                                                 msg->elements[i].name)) {
188                                                 ldb_msg_remove_attr(msg, "replPropertyMetaData");
189                                                 break;
190                                         } else {
191                                                 aclread_mark_inaccesslible(&msg->elements[i]);
192                                         }
193                                 }
194                         } else if (ret != LDB_SUCCESS) {
195                                 goto fail;
196                         }
197                  }
198                  for (i=0; i < msg->num_elements; i++) {
199                          if (!aclread_is_inaccessible(&msg->elements[i])) {
200                                  num_of_attrs++;
201                          }
202                  }
203                  /*create a new message to return*/
204                  ret_msg = ldb_msg_new(ac->req);
205                  ret_msg->dn = msg->dn;
206                  talloc_steal(ret_msg, msg->dn);
207                  ret_msg->num_elements = num_of_attrs;
208                  if (num_of_attrs > 0) {
209                          ret_msg->elements = talloc_array(ret_msg,
210                                                           struct ldb_message_element,
211                                                           num_of_attrs);
212                          if (ret_msg->elements == NULL) {
213                                  return ldb_oom(ldb);
214                          }
215                          for (i=0; i < msg->num_elements; i++) {
216                                  bool to_remove = aclread_is_inaccessible(&msg->elements[i]);
217                                  if (!to_remove) {
218                                          ret_msg->elements[k] = msg->elements[i];
219                                          talloc_steal(ret_msg->elements, msg->elements[i].name);
220                                          talloc_steal(ret_msg->elements, msg->elements[i].values);
221                                          k++;
222                                  }
223                          }
224                  } else {
225                          ret_msg->elements = NULL;
226                  }
227                  talloc_free(tmp_ctx);
228
229                  return ldb_module_send_entry(ac->req, ret_msg, ares->controls);
230          case LDB_REPLY_REFERRAL:
231                  return ldb_module_send_referral(ac->req, ares->referral);
232          case LDB_REPLY_DONE:
233                  return ldb_module_done(ac->req, ares->controls,
234                                         ares->response, LDB_SUCCESS);
235
236          }
237          return LDB_SUCCESS;
238 fail:
239          talloc_free(tmp_ctx);
240          return ldb_module_done(ac->req, NULL, NULL, ret);
241 }
242
243
244 static int aclread_search(struct ldb_module *module, struct ldb_request *req)
245 {
246         struct ldb_context *ldb;
247         int ret;
248         struct aclread_context *ac;
249         struct ldb_request *down_req;
250         struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
251         uint32_t flags = ldb_req_get_custom_flags(req);
252         struct ldb_result *res;
253         struct aclread_private *p;
254         bool is_untrusted = ldb_req_is_untrusted(req);
255         const char * const *attrs = NULL;
256         uint32_t instanceType;
257         static const char *acl_attrs[] = {
258                  "instanceType",
259                  NULL
260         };
261
262         ldb = ldb_module_get_ctx(module);
263         p = talloc_get_type(ldb_module_get_private(module), struct aclread_private);
264
265         /* skip access checks if we are system or system control is supplied
266          * or this is not LDAP server request */
267         if (!p || !p->enabled ||
268             dsdb_module_am_system(module)
269             || as_system || !is_untrusted) {
270                 return ldb_next_request(module, req);
271         }
272         /* no checks on special dn */
273         if (ldb_dn_is_special(req->op.search.base)) {
274                 return ldb_next_request(module, req);
275         }
276
277         /* check accessibility of base */
278         if (!ldb_dn_is_null(req->op.search.base)) {
279                 ret = dsdb_module_search_dn(module, req, &res, req->op.search.base,
280                                             acl_attrs,
281                                             DSDB_FLAG_NEXT_MODULE |
282                                             DSDB_SEARCH_SHOW_DELETED, req);
283                 if (ret != LDB_SUCCESS) {
284                         return ldb_error(ldb, ret,
285                                          "acl_read: Error retrieving instanceType for base.");
286                 }
287                 instanceType = ldb_msg_find_attr_as_uint(res->msgs[0],
288                                                          "instanceType", 0);
289                 if (instanceType != 0 && !(instanceType & INSTANCE_TYPE_IS_NC_HEAD))
290                 {
291                         /* the object has a parent, so we have to check for visibility */
292                         struct ldb_dn *parent_dn = ldb_dn_get_parent(req, req->op.search.base);
293                         ret = dsdb_module_check_access_on_dn(module,
294                                                              req,
295                                                              parent_dn,
296                                                              SEC_ADS_LIST,
297                                                              NULL, req);
298                         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
299                                 return ldb_module_done(req, NULL, NULL, LDB_ERR_NO_SUCH_OBJECT);
300                         } else if (ret != LDB_SUCCESS) {
301                                 return ldb_module_done(req, NULL, NULL, ret);
302                         }
303                 }
304         }
305         ac = talloc_zero(req, struct aclread_context);
306         if (ac == NULL) {
307                 return ldb_oom(ldb);
308         }
309         ac->module = module;
310         ac->req = req;
311         ac->schema = dsdb_get_schema(ldb, req);
312         if (flags & DSDB_ACL_CHECKS_DIRSYNC_FLAG) {
313                 ac->indirsync = true;
314         } else {
315                 ac->indirsync = false;
316         }
317         if (!ac->schema) {
318                 return ldb_operr(ldb);
319         }
320         /*
321          * In theory we should also check for the SD control but control verification is
322          * expensive so we'd better had the ntsecuritydescriptor to the list of
323          * searched attribute and then remove it !
324          */
325         ac->sd = !(ldb_attr_in_list(req->op.search.attrs, "nTSecurityDescriptor"));
326         if (req->op.search.attrs && !ldb_attr_in_list(req->op.search.attrs, "*")) {
327                 if (!ldb_attr_in_list(req->op.search.attrs, "instanceType")) {
328                         ac->instance_type = true;
329                         attrs = ldb_attr_list_copy_add(ac, req->op.search.attrs, "instanceType");
330                 } else {
331                         attrs = req->op.search.attrs;
332                 }
333                 if (!ldb_attr_in_list(req->op.search.attrs, "objectSid")) {
334                         ac->object_sid = true;
335                         attrs = ldb_attr_list_copy_add(ac, attrs, "objectSid");
336                 }
337         }
338
339         if (ac->sd) {
340                 /* avoid replacing all attributes with nTSecurityDescriptor
341                  * if attribute list is empty */
342                 if (!attrs) {
343                         attrs = ldb_attr_list_copy_add(ac, req->op.search.attrs, "*");
344                 }
345                 attrs = ldb_attr_list_copy_add(ac, attrs, "nTSecurityDescriptor");
346         }
347         ac->attrs = req->op.search.attrs;
348         ret = ldb_build_search_req_ex(&down_req,
349                                       ldb, ac,
350                                       req->op.search.base,
351                                       req->op.search.scope,
352                                       req->op.search.tree,
353                                       attrs,
354                                       req->controls,
355                                       ac, aclread_callback,
356                                       req);
357
358         if (ret != LDB_SUCCESS) {
359                 return LDB_ERR_OPERATIONS_ERROR;
360         }
361
362         return ldb_next_request(module, down_req);
363 }
364
365 static int aclread_init(struct ldb_module *module)
366 {
367         struct ldb_context *ldb = ldb_module_get_ctx(module);
368         struct aclread_private *p = talloc_zero(module, struct aclread_private);
369         if (p == NULL) {
370                 return ldb_module_oom(module);
371         }
372         p->enabled = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"), NULL, "acl", "search", false);
373         ldb_module_set_private(module, p);
374         return ldb_next_init(module);
375 }
376
377 static const struct ldb_module_ops ldb_aclread_module_ops = {
378         .name              = "aclread",
379         .search            = aclread_search,
380         .init_context      = aclread_init
381 };
382
383 int ldb_aclread_module_init(const char *version)
384 {
385         LDB_MODULE_CHECK_VERSION(version);
386         return ldb_register_module(&ldb_aclread_module_ops);
387 }