TODO this could be wrong, but NULL also...? dsdb-acl: pass the object type to dsdb_mo...
[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         uint32_t sd_flags;
48         bool added_nTSecurityDescriptor;
49         bool added_instanceType;
50         bool added_objectSid;
51         bool added_objectClass;
52         bool indirsync;
53 };
54
55 struct aclread_private {
56         bool enabled;
57 };
58
59 static void aclread_mark_inaccesslible(struct ldb_message_element *el) {
60         el->flags |= LDB_FLAG_INTERNAL_INACCESSIBLE_ATTRIBUTE;
61 }
62
63 static bool aclread_is_inaccessible(struct ldb_message_element *el) {
64         return el->flags & LDB_FLAG_INTERNAL_INACCESSIBLE_ATTRIBUTE;
65 }
66
67 static int aclread_callback(struct ldb_request *req, struct ldb_reply *ares)
68 {
69         struct ldb_context *ldb;
70         struct aclread_context *ac;
71         struct ldb_message *ret_msg;
72         struct ldb_message *msg;
73         int ret, num_of_attrs = 0;
74         unsigned int i, k = 0;
75         struct security_descriptor *sd;
76         struct dom_sid *sid = NULL;
77         TALLOC_CTX *tmp_ctx;
78         uint32_t instanceType;
79         const struct dsdb_class *objectclass;
80
81         ac = talloc_get_type(req->context, struct aclread_context);
82         ldb = ldb_module_get_ctx(ac->module);
83         if (!ares) {
84                 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR );
85         }
86         if (ares->error != LDB_SUCCESS) {
87                 return ldb_module_done(ac->req, ares->controls,
88                                        ares->response, ares->error);
89         }
90         tmp_ctx = talloc_new(ac);
91         switch (ares->type) {
92         case LDB_REPLY_ENTRY:
93                 msg = ares->message;
94                 ret = dsdb_get_sd_from_ldb_message(ldb, tmp_ctx, msg, &sd);
95                 if (ret != LDB_SUCCESS || sd == NULL ) {
96                         ldb_debug_set(ldb, LDB_DEBUG_FATAL,
97                                       "acl_read: cannot get descriptor of %s\n",
98                                       ldb_dn_get_linearized(msg->dn));
99                         ret = LDB_ERR_OPERATIONS_ERROR;
100                         goto fail;
101                 }
102                 /*
103                  * Get the most specific structural object class for the ACL check
104                  */
105                 objectclass = dsdb_get_structural_oc_from_msg(ac->schema, msg);
106                 if (objectclass == NULL) {
107                         ldb_asprintf_errstring(ldb, "acl_read: Failed to find a structural class for %s",
108                                                ldb_dn_get_linearized(msg->dn));
109                         ret = LDB_ERR_OPERATIONS_ERROR;
110                         goto fail;
111                 }
112
113                 sid = samdb_result_dom_sid(tmp_ctx, msg, "objectSid");
114                 /* get the object instance type */
115                 instanceType = ldb_msg_find_attr_as_uint(msg,
116                                                          "instanceType", 0);
117                 if (!ldb_dn_is_null(msg->dn) && !(instanceType & INSTANCE_TYPE_IS_NC_HEAD))
118                 {
119                         /* the object has a parent, so we have to check for visibility */
120                         struct ldb_dn *parent_dn = ldb_dn_get_parent(tmp_ctx, msg->dn);
121
122                         ret = dsdb_module_check_access_on_dn(ac->module,
123                                                              tmp_ctx,
124                                                              parent_dn,
125                                                              SEC_ADS_LIST,
126                                                              &objectclass->schemaIDGUID,
127                                                              req);
128                         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
129                                 talloc_free(tmp_ctx);
130                                 return LDB_SUCCESS;
131                         } else if (ret != LDB_SUCCESS) {
132                                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
133                                               "acl_read: %s check parent %s - %s\n",
134                                               ldb_dn_get_linearized(msg->dn),
135                                               ldb_strerror(ret),
136                                               ldb_errstring(ldb));
137                                 goto fail;
138                         }
139                 }
140
141                 /* for every element in the message check RP */
142                 for (i=0; i < msg->num_elements; i++) {
143                         const struct dsdb_attribute *attr;
144                         bool is_sd, is_objectsid, is_instancetype, is_objectclass;
145                         uint32_t access_mask;
146                         attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
147                                                                  msg->elements[i].name);
148                         if (!attr) {
149                                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
150                                               "acl_read: %s cannot find attr[%s] in of schema\n",
151                                               ldb_dn_get_linearized(msg->dn),
152                                               msg->elements[i].name);
153                                 ret = LDB_ERR_OPERATIONS_ERROR;
154                                 goto fail;
155                         }
156                         is_sd = ldb_attr_cmp("nTSecurityDescriptor",
157                                               msg->elements[i].name) == 0;
158                         is_objectsid = ldb_attr_cmp("objectSid",
159                                                     msg->elements[i].name) == 0;
160                         is_instancetype = ldb_attr_cmp("instanceType",
161                                                        msg->elements[i].name) == 0;
162                         is_objectclass = ldb_attr_cmp("objectClass",
163                                                       msg->elements[i].name) == 0;
164                         /* these attributes were added to perform access checks and must be removed */
165                         if (is_objectsid && ac->added_objectSid) {
166                                 aclread_mark_inaccesslible(&msg->elements[i]);
167                                 continue;
168                         }
169                         if (is_instancetype && ac->added_instanceType) {
170                                 aclread_mark_inaccesslible(&msg->elements[i]);
171                                 continue;
172                         }
173                         if (is_objectclass && ac->added_objectClass) {
174                                 aclread_mark_inaccesslible(&msg->elements[i]);
175                                 continue;
176                         }
177                         if (is_sd && ac->added_nTSecurityDescriptor) {
178                                 aclread_mark_inaccesslible(&msg->elements[i]);
179                                 continue;
180                         }
181                         /* nTSecurityDescriptor is a special case */
182                         if (is_sd) {
183                                 access_mask = 0;
184
185                                 if (ac->sd_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
186                                         access_mask |= SEC_STD_READ_CONTROL;
187                                 }
188                                 if (ac->sd_flags & SECINFO_DACL) {
189                                         access_mask |= SEC_STD_READ_CONTROL;
190                                 }
191                                 if (ac->sd_flags & SECINFO_SACL) {
192                                         access_mask |= SEC_FLAG_SYSTEM_SECURITY;
193                                 }
194                         } else {
195                                 access_mask = SEC_ADS_READ_PROP;
196                         }
197
198                         if (attr->searchFlags & SEARCH_FLAG_CONFIDENTIAL) {
199                                 access_mask |= SEC_ADS_CONTROL_ACCESS;
200                         }
201
202                         if (access_mask == 0) {
203                                 aclread_mark_inaccesslible(&msg->elements[i]);
204                                 continue;
205                         }
206
207                         ret = acl_check_access_on_attribute(ac->module,
208                                                             tmp_ctx,
209                                                             sd,
210                                                             sid,
211                                                             access_mask,
212                                                             attr,
213                                                             objectclass);
214
215                         /*
216                          * Dirsync control needs the replpropertymetadata attribute
217                          * so return it as it will be removed by the control
218                          * in anycase.
219                          */
220                         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
221                                 if (!ac->indirsync) {
222                                         /*
223                                          * do not return this entry if attribute is
224                                          * part of the search filter
225                                          */
226                                         if (dsdb_attr_in_parse_tree(ac->req->op.search.tree,
227                                                                 msg->elements[i].name)) {
228                                                 talloc_free(tmp_ctx);
229                                                 return LDB_SUCCESS;
230                                         }
231                                         aclread_mark_inaccesslible(&msg->elements[i]);
232                                 } else {
233                                         /*
234                                          * We are doing dirysnc answers
235                                          * and the object shouldn't be returned (normally)
236                                          * but we will return it without replPropertyMetaData
237                                          * so that the dirysync module will do what is needed
238                                          * (remove the object if it is not deleted, or return
239                                          * just the objectGUID if it's deleted).
240                                          */
241                                         if (dsdb_attr_in_parse_tree(ac->req->op.search.tree,
242                                                                 msg->elements[i].name)) {
243                                                 ldb_msg_remove_attr(msg, "replPropertyMetaData");
244                                                 break;
245                                         } else {
246                                                 aclread_mark_inaccesslible(&msg->elements[i]);
247                                         }
248                                 }
249                         } else if (ret != LDB_SUCCESS) {
250                                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
251                                               "acl_read: %s check attr[%s] gives %s - %s\n",
252                                               ldb_dn_get_linearized(msg->dn),
253                                               msg->elements[i].name,
254                                               ldb_strerror(ret),
255                                               ldb_errstring(ldb));
256                                 goto fail;
257                         }
258                 }
259                 for (i=0; i < msg->num_elements; i++) {
260                         if (!aclread_is_inaccessible(&msg->elements[i])) {
261                                 num_of_attrs++;
262                         }
263                 }
264                 /*create a new message to return*/
265                 ret_msg = ldb_msg_new(ac->req);
266                 ret_msg->dn = msg->dn;
267                 talloc_steal(ret_msg, msg->dn);
268                 ret_msg->num_elements = num_of_attrs;
269                 if (num_of_attrs > 0) {
270                         ret_msg->elements = talloc_array(ret_msg,
271                                                          struct ldb_message_element,
272                                                          num_of_attrs);
273                         if (ret_msg->elements == NULL) {
274                                 return ldb_oom(ldb);
275                         }
276                         for (i=0; i < msg->num_elements; i++) {
277                                 bool to_remove = aclread_is_inaccessible(&msg->elements[i]);
278                                 if (!to_remove) {
279                                         ret_msg->elements[k] = msg->elements[i];
280                                         talloc_steal(ret_msg->elements, msg->elements[i].name);
281                                         talloc_steal(ret_msg->elements, msg->elements[i].values);
282                                         k++;
283                                 }
284                         }
285                         /*
286                          * This should not be needed, but some modules
287                          * may allocate values on the wrong context...
288                          */
289                         talloc_steal(ret_msg->elements, msg);
290                 } else {
291                         ret_msg->elements = NULL;
292                 }
293                 talloc_free(tmp_ctx);
294
295                 return ldb_module_send_entry(ac->req, ret_msg, ares->controls);
296         case LDB_REPLY_REFERRAL:
297                 return ldb_module_send_referral(ac->req, ares->referral);
298         case LDB_REPLY_DONE:
299                 return ldb_module_done(ac->req, ares->controls,
300                                         ares->response, LDB_SUCCESS);
301
302         }
303         return LDB_SUCCESS;
304 fail:
305         talloc_free(tmp_ctx);
306         return ldb_module_done(ac->req, NULL, NULL, ret);
307 }
308
309
310 static int aclread_search(struct ldb_module *module, struct ldb_request *req)
311 {
312         struct ldb_context *ldb;
313         int ret;
314         struct aclread_context *ac;
315         struct ldb_request *down_req;
316         struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
317         uint32_t flags = ldb_req_get_custom_flags(req);
318         struct ldb_result *res;
319         struct aclread_private *p;
320         bool need_sd = false;
321         bool explicit_sd_flags = false;
322         bool is_untrusted = ldb_req_is_untrusted(req);
323         static const char * const _all_attrs[] = { "*", NULL };
324         bool all_attrs = false;
325         const char * const *attrs = NULL;
326         uint32_t instanceType;
327         static const char *acl_attrs[] = {
328                 "instanceType",
329                 NULL
330         };
331
332         ldb = ldb_module_get_ctx(module);
333         p = talloc_get_type(ldb_module_get_private(module), struct aclread_private);
334
335         /* skip access checks if we are system or system control is supplied
336          * or this is not LDAP server request */
337         if (!p || !p->enabled ||
338             dsdb_module_am_system(module)
339             || as_system || !is_untrusted) {
340                 return ldb_next_request(module, req);
341         }
342         /* no checks on special dn */
343         if (ldb_dn_is_special(req->op.search.base)) {
344                 return ldb_next_request(module, req);
345         }
346
347         /* check accessibility of base */
348         if (!ldb_dn_is_null(req->op.search.base)) {
349                 ret = dsdb_module_search_dn(module, req, &res, req->op.search.base,
350                                             acl_attrs,
351                                             DSDB_FLAG_NEXT_MODULE |
352                                             DSDB_FLAG_AS_SYSTEM |
353                                             DSDB_SEARCH_SHOW_RECYCLED,
354                                             req);
355                 if (ret != LDB_SUCCESS) {
356                         return ldb_error(ldb, ret,
357                                         "acl_read: Error retrieving instanceType for base.");
358                 }
359                 instanceType = ldb_msg_find_attr_as_uint(res->msgs[0],
360                                                         "instanceType", 0);
361                 if (instanceType != 0 && !(instanceType & INSTANCE_TYPE_IS_NC_HEAD))
362                 {
363                         /* the object has a parent, so we have to check for visibility */
364                         struct ldb_dn *parent_dn = ldb_dn_get_parent(req, req->op.search.base);
365                         ret = dsdb_module_check_access_on_dn(module,
366                                                              req,
367                                                              parent_dn,
368                                                              SEC_ADS_LIST,
369                                                              NULL, req);
370                         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
371                                 return ldb_module_done(req, NULL, NULL, LDB_ERR_NO_SUCH_OBJECT);
372                         } else if (ret != LDB_SUCCESS) {
373                                 return ldb_module_done(req, NULL, NULL, ret);
374                         }
375                 }
376         }
377         ac = talloc_zero(req, struct aclread_context);
378         if (ac == NULL) {
379                 return ldb_oom(ldb);
380         }
381         ac->module = module;
382         ac->req = req;
383         ac->schema = dsdb_get_schema(ldb, req);
384         if (flags & DSDB_ACL_CHECKS_DIRSYNC_FLAG) {
385                 ac->indirsync = true;
386         } else {
387                 ac->indirsync = false;
388         }
389         if (!ac->schema) {
390                 return ldb_operr(ldb);
391         }
392
393         attrs = req->op.search.attrs;
394         if (attrs == NULL) {
395                 all_attrs = true;
396                 attrs = _all_attrs;
397         } else if (attrs[0] == NULL) {
398                 all_attrs = true;
399                 attrs = _all_attrs;
400         } else if (ldb_attr_in_list(attrs, "*")) {
401                 all_attrs = true;
402         }
403
404         /*
405          * In theory we should also check for the SD control but control verification is
406          * expensive so we'd better had the ntsecuritydescriptor to the list of
407          * searched attribute and then remove it !
408          */
409         ac->sd_flags = dsdb_request_sd_flags(ac->req, &explicit_sd_flags);
410
411         if (ldb_attr_in_list(attrs, "nTSecurityDescriptor")) {
412                 need_sd = false;
413         } else if (explicit_sd_flags && all_attrs) {
414                 need_sd = false;
415         } else {
416                 need_sd = true;
417         }
418
419         if (!all_attrs) {
420                 if (!ldb_attr_in_list(attrs, "instanceType")) {
421                         attrs = ldb_attr_list_copy_add(ac, attrs, "instanceType");
422                         if (attrs == NULL) {
423                                 return ldb_oom(ldb);
424                         }
425                         ac->added_instanceType = true;
426                 }
427                 if (!ldb_attr_in_list(req->op.search.attrs, "objectSid")) {
428                         attrs = ldb_attr_list_copy_add(ac, attrs, "objectSid");
429                         if (attrs == NULL) {
430                                 return ldb_oom(ldb);
431                         }
432                         ac->added_objectSid = true;
433                 }
434                 if (!ldb_attr_in_list(req->op.search.attrs, "objectClass")) {
435                         attrs = ldb_attr_list_copy_add(ac, attrs, "objectClass");
436                         if (attrs == NULL) {
437                                 return ldb_oom(ldb);
438                         }
439                         ac->added_objectClass = true;
440                 }
441         }
442
443         if (need_sd) {
444                 attrs = ldb_attr_list_copy_add(ac, attrs, "nTSecurityDescriptor");
445                 if (attrs == NULL) {
446                         return ldb_oom(ldb);
447                 }
448                 ac->added_nTSecurityDescriptor = true;
449         }
450
451         ac->attrs = req->op.search.attrs;
452         ret = ldb_build_search_req_ex(&down_req,
453                                       ldb, ac,
454                                       req->op.search.base,
455                                       req->op.search.scope,
456                                       req->op.search.tree,
457                                       attrs,
458                                       req->controls,
459                                       ac, aclread_callback,
460                                       req);
461
462         if (ret != LDB_SUCCESS) {
463                 return LDB_ERR_OPERATIONS_ERROR;
464         }
465
466         return ldb_next_request(module, down_req);
467 }
468
469 static int aclread_init(struct ldb_module *module)
470 {
471         struct ldb_context *ldb = ldb_module_get_ctx(module);
472         struct aclread_private *p = talloc_zero(module, struct aclread_private);
473         if (p == NULL) {
474                 return ldb_module_oom(module);
475         }
476         p->enabled = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"), NULL, "acl", "search", true);
477         ldb_module_set_private(module, p);
478         return ldb_next_init(module);
479 }
480
481 static const struct ldb_module_ops ldb_aclread_module_ops = {
482         .name              = "aclread",
483         .search            = aclread_search,
484         .init_context      = aclread_init
485 };
486
487 int ldb_aclread_module_init(const char *version)
488 {
489         LDB_MODULE_CHECK_VERSION(version);
490         return ldb_register_module(&ldb_aclread_module_ops);
491 }