LCOV - code coverage report
Current view: top level - source4/dsdb/samdb/ldb_modules - acl_read.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 330 384 85.9 %
Date: 2021-09-23 10:06:22 Functions: 17 17 100.0 %

          Line data    Source code
       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             : #undef strcasecmp
      42             : 
      43             : struct aclread_context {
      44             :         struct ldb_module *module;
      45             :         struct ldb_request *req;
      46             :         const char * const *attrs;
      47             :         const struct dsdb_schema *schema;
      48             :         uint32_t sd_flags;
      49             :         bool added_nTSecurityDescriptor;
      50             :         bool added_instanceType;
      51             :         bool added_objectSid;
      52             :         bool added_objectClass;
      53             :         bool indirsync;
      54             : 
      55             :         bool do_list_object_initialized;
      56             :         bool do_list_object;
      57             :         bool base_invisible;
      58             :         uint64_t num_entries;
      59             : 
      60             :         /* cache on the last parent we checked in this search */
      61             :         struct ldb_dn *last_parent_dn;
      62             :         int last_parent_check_ret;
      63             : };
      64             : 
      65             : struct aclread_private {
      66             :         bool enabled;
      67             : 
      68             :         /* cache of the last SD we read during any search */
      69             :         struct security_descriptor *sd_cached;
      70             :         struct ldb_val sd_cached_blob;
      71             : };
      72             : 
      73     4550301 : static void aclread_mark_inaccesslible(struct ldb_message_element *el) {
      74     4551077 :         el->flags |= LDB_FLAG_INTERNAL_INACCESSIBLE_ATTRIBUTE;
      75     4550301 : }
      76             : 
      77    14918768 : static bool aclread_is_inaccessible(struct ldb_message_element *el) {
      78    14920322 :         return el->flags & LDB_FLAG_INTERNAL_INACCESSIBLE_ATTRIBUTE;
      79             : }
      80             : 
      81             : /*
      82             :  * the object has a parent, so we have to check for visibility
      83             :  *
      84             :  * This helper function uses a per-search cache to avoid checking the
      85             :  * parent object for each of many possible children.  This is likely
      86             :  * to help on SCOPE_ONE searches and on typical tree structures for
      87             :  * SCOPE_SUBTREE, where an OU has many users as children.
      88             :  *
      89             :  * We rely for safety on the DB being locked for reads during the full
      90             :  * search.
      91             :  */
      92     1875233 : static int aclread_check_parent(struct aclread_context *ac,
      93             :                                 struct ldb_message *msg,
      94             :                                 struct ldb_request *req)
      95             : {
      96             :         int ret;
      97     1875233 :         struct ldb_dn *parent_dn = NULL;
      98             : 
      99             :         /* We may have a cached result from earlier in this search */
     100     1875233 :         if (ac->last_parent_dn != NULL) {
     101             :                 /*
     102             :                  * We try the no-allocation ldb_dn_compare_base()
     103             :                  * first however it will not tell parents and
     104             :                  * grand-parents apart
     105             :                  */
     106     1271055 :                 int cmp_base = ldb_dn_compare_base(ac->last_parent_dn,
     107             :                                                    msg->dn);
     108     1271055 :                 if (cmp_base == 0) {
     109             :                         /* Now check if it is a direct parent */
     110     1193782 :                         parent_dn = ldb_dn_get_parent(ac, msg->dn);
     111     1193782 :                         if (parent_dn == NULL) {
     112           0 :                                 return ldb_oom(ldb_module_get_ctx(ac->module));
     113             :                         }
     114     1193782 :                         if (ldb_dn_compare(ac->last_parent_dn,
     115             :                                            parent_dn) == 0) {
     116     1125421 :                                 TALLOC_FREE(parent_dn);
     117             : 
     118             :                                 /*
     119             :                                  * If we checked the same parent last
     120             :                                  * time, then return the cached
     121             :                                  * result.
     122             :                                  *
     123             :                                  * The cache is valid as long as the
     124             :                                  * search as the DB is read locked and
     125             :                                  * the session_info (connected user)
     126             :                                  * is constant.
     127             :                                  */
     128     1125421 :                                 return ac->last_parent_check_ret;
     129             :                         }
     130             :                 }
     131             :         }
     132             : 
     133             :         {
     134      749812 :                 TALLOC_CTX *frame = NULL;
     135      749812 :                 frame = talloc_stackframe();
     136             : 
     137             :                 /*
     138             :                  * This may have been set in the block above, don't
     139             :                  * re-parse
     140             :                  */
     141      749812 :                 if (parent_dn == NULL) {
     142      681451 :                         parent_dn = ldb_dn_get_parent(ac, msg->dn);
     143      681451 :                         if (parent_dn == NULL) {
     144           0 :                                 TALLOC_FREE(frame);
     145           0 :                                 return ldb_oom(ldb_module_get_ctx(ac->module));
     146             :                         }
     147             :                 }
     148      749812 :                 ret = dsdb_module_check_access_on_dn(ac->module,
     149             :                                                      frame,
     150             :                                                      parent_dn,
     151             :                                                      SEC_ADS_LIST,
     152             :                                                      NULL, req);
     153      749812 :                 talloc_unlink(ac, ac->last_parent_dn);
     154      749812 :                 ac->last_parent_dn = parent_dn;
     155      749812 :                 ac->last_parent_check_ret = ret;
     156             : 
     157      749812 :                 TALLOC_FREE(frame);
     158             :         }
     159      749582 :         return ret;
     160             : }
     161             : 
     162     1940810 : static int aclread_check_object_visible(struct aclread_context *ac,
     163             :                                         struct ldb_message *msg,
     164             :                                         struct ldb_request *req)
     165             : {
     166             :         uint32_t instanceType;
     167             :         int ret;
     168             : 
     169             :         /* get the object instance type */
     170     1940810 :         instanceType = ldb_msg_find_attr_as_uint(msg,
     171             :                                                  "instanceType", 0);
     172     1940810 :         if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
     173             :                 /*
     174             :                  * NC_HEAD objects are always visible
     175             :                  */
     176       65561 :                 return LDB_SUCCESS;
     177             :         }
     178             : 
     179     1875233 :         ret = aclread_check_parent(ac, msg, req);
     180     1875233 :         if (ret == LDB_SUCCESS) {
     181             :                 /*
     182             :                  * SEC_ADS_LIST (List Children) alone
     183             :                  * on the parent is enough to make the
     184             :                  * object visible.
     185             :                  */
     186     1834913 :                 return LDB_SUCCESS;
     187             :         }
     188       39872 :         if (ret != LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
     189           0 :                 return ret;
     190             :         }
     191             : 
     192       39872 :         if (!ac->do_list_object_initialized) {
     193             :                 /*
     194             :                  * We only call dsdb_do_list_object() once
     195             :                  * and only when needed in order to
     196             :                  * check the dSHeuristics for fDoListObject.
     197             :                  */
     198       16308 :                 ac->do_list_object = dsdb_do_list_object(ac->module, ac, req);
     199       16308 :                 ac->do_list_object_initialized = true;
     200             :         }
     201             : 
     202       39872 :         if (ac->do_list_object) {
     203       12672 :                 TALLOC_CTX *frame = talloc_stackframe();
     204       12672 :                 struct ldb_dn *parent_dn = NULL;
     205             : 
     206             :                 /*
     207             :                  * Here we're in "List Object" mode (fDoListObject=true).
     208             :                  *
     209             :                  * If SEC_ADS_LIST (List Children) is not
     210             :                  * granted on the parent, we need to check if
     211             :                  * SEC_ADS_LIST_OBJECT (List Object) is granted
     212             :                  * on the parent and also on the object itself.
     213             :                  *
     214             :                  * We could optimize this similar to aclread_check_parent(),
     215             :                  * but that would require quite a bit of restructuring,
     216             :                  * so that we cache the granted access bits instead
     217             :                  * of just the result for 'SEC_ADS_LIST (List Children)'.
     218             :                  *
     219             :                  * But as this is the uncommon case and
     220             :                  * 'SEC_ADS_LIST (List Children)' is most likely granted
     221             :                  * on most of the objects, we'll just implement what
     222             :                  * we have to.
     223             :                  */
     224             : 
     225       12672 :                 parent_dn = ldb_dn_get_parent(frame, msg->dn);
     226       12672 :                 if (parent_dn == NULL) {
     227           0 :                         TALLOC_FREE(frame);
     228           0 :                         return ldb_oom(ldb_module_get_ctx(ac->module));
     229             :                 }
     230       12672 :                 ret = dsdb_module_check_access_on_dn(ac->module,
     231             :                                                      frame,
     232             :                                                      parent_dn,
     233             :                                                      SEC_ADS_LIST_OBJECT,
     234             :                                                      NULL, req);
     235       12672 :                 if (ret != LDB_SUCCESS) {
     236        6048 :                         TALLOC_FREE(frame);
     237        6048 :                         return ret;
     238             :                 }
     239        6624 :                 ret = dsdb_module_check_access_on_dn(ac->module,
     240             :                                                      frame,
     241             :                                                      msg->dn,
     242             :                                                      SEC_ADS_LIST_OBJECT,
     243             :                                                      NULL, req);
     244        6624 :                 if (ret != LDB_SUCCESS) {
     245        1872 :                         TALLOC_FREE(frame);
     246        1872 :                         return ret;
     247             :                 }
     248             : 
     249        4752 :                 TALLOC_FREE(frame);
     250        4752 :                 return LDB_SUCCESS;
     251             :         }
     252             : 
     253       27200 :         return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
     254             : }
     255             : 
     256             : /*
     257             :  * The sd returned from this function is valid until the next call on
     258             :  * this module context
     259             :  *
     260             :  * This helper function uses a cache on the module private data to
     261             :  * speed up repeated use of the same SD.
     262             :  */
     263             : 
     264     1320983 : static int aclread_get_sd_from_ldb_message(struct aclread_context *ac,
     265             :                                            struct ldb_message *acl_res,
     266             :                                            struct security_descriptor **sd)
     267             : {
     268             :         struct ldb_message_element *sd_element;
     269     1320983 :         struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
     270     1210613 :         struct aclread_private *private_data
     271     1320983 :                 = talloc_get_type(ldb_module_get_private(ac->module),
     272             :                                   struct aclread_private);
     273             :         enum ndr_err_code ndr_err;
     274             : 
     275     1320983 :         sd_element = ldb_msg_find_element(acl_res, "nTSecurityDescriptor");
     276     1320983 :         if (sd_element == NULL) {
     277           0 :                 return ldb_error(ldb, LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS,
     278             :                                  "nTSecurityDescriptor is missing");
     279             :         }
     280             : 
     281     1320983 :         if (sd_element->num_values != 1) {
     282           0 :                 return ldb_operr(ldb);
     283             :         }
     284             : 
     285             :         /*
     286             :          * The time spent in ndr_pull_security_descriptor() is quite
     287             :          * expensive, so we check if this is the same binary blob as last
     288             :          * time, and if so return the memory tree from that previous parse.
     289             :          */
     290             : 
     291     2520533 :         if (private_data->sd_cached != NULL &&
     292     2610966 :             private_data->sd_cached_blob.data != NULL &&
     293     1305483 :             ldb_val_equal_exact(&sd_element->values[0],
     294     1305483 :                                 &private_data->sd_cached_blob)) {
     295     1126567 :                 *sd = private_data->sd_cached;
     296     1126486 :                 return LDB_SUCCESS;
     297             :         }
     298             : 
     299      194416 :         *sd = talloc(private_data, struct security_descriptor);
     300      194416 :         if(!*sd) {
     301           0 :                 return ldb_oom(ldb);
     302             :         }
     303      194416 :         ndr_err = ndr_pull_struct_blob(&sd_element->values[0], *sd, *sd,
     304             :                              (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
     305             : 
     306      194416 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     307           0 :                 TALLOC_FREE(*sd);
     308           0 :                 return ldb_operr(ldb);
     309             :         }
     310             : 
     311      194416 :         talloc_unlink(private_data, private_data->sd_cached_blob.data);
     312      194416 :         if (ac->added_nTSecurityDescriptor) {
     313      192274 :                 private_data->sd_cached_blob = sd_element->values[0];
     314      192274 :                 talloc_steal(private_data, sd_element->values[0].data);
     315             :         } else {
     316        2142 :                 private_data->sd_cached_blob = ldb_val_dup(private_data,
     317        2142 :                                                            &sd_element->values[0]);
     318        2142 :                 if (private_data->sd_cached_blob.data == NULL) {
     319           0 :                         TALLOC_FREE(*sd);
     320           0 :                         return ldb_operr(ldb);
     321             :                 }
     322             :         }
     323             : 
     324      194416 :         talloc_unlink(private_data, private_data->sd_cached);
     325      194416 :         private_data->sd_cached = *sd;
     326             : 
     327      194265 :         return LDB_SUCCESS;
     328             : }
     329             : 
     330             : /*
     331             :  * Returns the access mask required to read a given attribute
     332             :  */
     333     4532107 : static uint32_t get_attr_access_mask(const struct dsdb_attribute *attr,
     334             :                                      uint32_t sd_flags)
     335             : {
     336             : 
     337     4532107 :         uint32_t access_mask = 0;
     338             :         bool is_sd;
     339             : 
     340             :         /* nTSecurityDescriptor is a special case */
     341     4532107 :         is_sd = (ldb_attr_cmp("nTSecurityDescriptor",
     342             :                               attr->lDAPDisplayName) == 0);
     343             : 
     344     4532107 :         if (is_sd) {
     345       15982 :                 if (sd_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
     346       15812 :                         access_mask |= SEC_STD_READ_CONTROL;
     347             :                 }
     348       15982 :                 if (sd_flags & SECINFO_DACL) {
     349       15955 :                         access_mask |= SEC_STD_READ_CONTROL;
     350             :                 }
     351       15982 :                 if (sd_flags & SECINFO_SACL) {
     352       15563 :                         access_mask |= SEC_FLAG_SYSTEM_SECURITY;
     353             :                 }
     354             :         } else {
     355     4515894 :                 access_mask = SEC_ADS_READ_PROP;
     356             :         }
     357             : 
     358     4532107 :         if (attr->searchFlags & SEARCH_FLAG_CONFIDENTIAL) {
     359        5591 :                 access_mask |= SEC_ADS_CONTROL_ACCESS;
     360             :         }
     361             : 
     362     4532107 :         return access_mask;
     363             : }
     364             : 
     365             : /* helper struct for traversing the attributes in the search-tree */
     366             : struct parse_tree_aclread_ctx {
     367             :         struct aclread_context *ac;
     368             :         TALLOC_CTX *mem_ctx;
     369             :         struct dom_sid *sid;
     370             :         struct ldb_dn *dn;
     371             :         struct security_descriptor *sd;
     372             :         const struct dsdb_class *objectclass;
     373             :         bool suppress_result;
     374             : };
     375             : 
     376             : /*
     377             :  * Checks that the user has sufficient access rights to view an attribute
     378             :  */
     379     1162762 : static int check_attr_access_rights(TALLOC_CTX *mem_ctx, const char *attr_name,
     380             :                                     struct aclread_context *ac,
     381             :                                     struct security_descriptor *sd,
     382             :                                     const struct dsdb_class *objectclass,
     383             :                                     struct dom_sid *sid, struct ldb_dn *dn)
     384             : {
     385             :         int ret;
     386     1162762 :         const struct dsdb_attribute *attr = NULL;
     387             :         uint32_t access_mask;
     388     1162762 :         struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
     389             : 
     390     1162762 :         attr = dsdb_attribute_by_lDAPDisplayName(ac->schema, attr_name);
     391     1162762 :         if (!attr) {
     392           1 :                 ldb_debug_set(ldb,
     393             :                               LDB_DEBUG_TRACE,
     394             :                               "acl_read: %s cannot find attr[%s] in schema,"
     395             :                               "ignoring\n",
     396             :                               ldb_dn_get_linearized(dn), attr_name);
     397           1 :                 return LDB_SUCCESS;
     398             :         }
     399             : 
     400     1162761 :         access_mask = get_attr_access_mask(attr, ac->sd_flags);
     401             : 
     402             :         /* the access-mask should be non-zero. Skip attribute otherwise */
     403     1162761 :         if (access_mask == 0) {
     404           0 :                 DBG_ERR("Could not determine access mask for attribute %s\n",
     405             :                         attr_name);
     406           0 :                 return LDB_SUCCESS;
     407             :         }
     408             : 
     409     1162761 :         ret = acl_check_access_on_attribute(ac->module, mem_ctx, sd, sid,
     410             :                                             access_mask, attr, objectclass);
     411             : 
     412     1162761 :         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
     413        2266 :                 return ret;
     414             :         }
     415             : 
     416     1160495 :         if (ret != LDB_SUCCESS) {
     417           0 :                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
     418             :                               "acl_read: %s check attr[%s] gives %s - %s\n",
     419             :                               ldb_dn_get_linearized(dn), attr_name,
     420             :                               ldb_strerror(ret), ldb_errstring(ldb));
     421           0 :                 return ret;
     422             :         }
     423             : 
     424     1160481 :         return LDB_SUCCESS;
     425             : }
     426             : 
     427             : /*
     428             :  * Returns the attribute name for this particular level of a search operation
     429             :  * parse-tree.
     430             :  */
     431     3740167 : static const char * parse_tree_get_attr(struct ldb_parse_tree *tree)
     432             : {
     433     3740835 :         const char *attr = NULL;
     434             : 
     435     3740835 :         switch (tree->operation) {
     436      278452 :         case LDB_OP_EQUALITY:
     437             :         case LDB_OP_GREATER:
     438             :         case LDB_OP_LESS:
     439             :         case LDB_OP_APPROX:
     440      278452 :                 attr = tree->u.equality.attr;
     441      278440 :                 break;
     442      857444 :         case LDB_OP_SUBSTRING:
     443      857444 :                 attr = tree->u.substring.attr;
     444      857444 :                 break;
     445     1372886 :         case LDB_OP_PRESENT:
     446     1372886 :                 attr = tree->u.present.attr;
     447     1372448 :                 break;
     448       16036 :         case LDB_OP_EXTENDED:
     449       16036 :                 attr = tree->u.extended.attr;
     450       16036 :                 break;
     451             : 
     452             :         /* we'll check LDB_OP_AND/_OR/_NOT children later on in the walk */
     453     1215799 :         default:
     454     1215799 :                 break;
     455             :         }
     456     3740167 :         return attr;
     457             : }
     458             : 
     459             : /*
     460             :  * Checks a single attribute in the search parse-tree to make sure the user has
     461             :  * sufficient rights to view it.
     462             :  */
     463     3741921 : static int parse_tree_check_attr_access(struct ldb_parse_tree *tree,
     464             :                                         void *private_context)
     465             : {
     466     3741921 :         struct parse_tree_aclread_ctx *ctx = NULL;
     467     3741921 :         const char *attr_name = NULL;
     468             :         int ret;
     469             :         static const char * const attrs_always_present[] = {
     470             :                 "objectClass",
     471             :                 "distinguishedName",
     472             :                 "name",
     473             :                 "objectGUID",
     474             :                 NULL
     475             :         };
     476             : 
     477     3741921 :         ctx = (struct parse_tree_aclread_ctx *)private_context;
     478             : 
     479             :         /*
     480             :          * we can skip any further checking if we already know that this object
     481             :          * shouldn't be visible in this user's search
     482             :          */
     483     3741921 :         if (ctx->suppress_result) {
     484        1086 :                 return LDB_SUCCESS;
     485             :         }
     486             : 
     487             :         /* skip this level of the search-tree if it has no attribute to check */
     488     3741285 :         attr_name = parse_tree_get_attr(tree);
     489     3740617 :         if (attr_name == NULL) {
     490     1215799 :                 return LDB_SUCCESS;
     491             :         }
     492             : 
     493             :         /*
     494             :          * If the search filter is checking for an attribute's presence, and the
     495             :          * attribute is always present, we can skip access rights checks. Every
     496             :          * object has these attributes, and so there's no security reason to
     497             :          * hide their presence.
     498             :          * Note: the acl.py tests (e.g. test_search1()) rely on this exception.
     499             :          * I.e. even if we lack Read Property (RP) rights for a child object, it
     500             :          * should still appear as a visible object in 'objectClass=*' searches,
     501             :          * so long as we have List Contents (LC) rights for the object.
     502             :          */
     503     3897704 :         if (tree->operation == LDB_OP_PRESENT &&
     504     1372886 :             is_attr_in_list(attrs_always_present, attr_name)) {
     505     1361620 :                 return LDB_SUCCESS;
     506             :         }
     507             : 
     508     1162762 :         ret = check_attr_access_rights(ctx->mem_ctx, attr_name, ctx->ac,
     509             :                                        ctx->sd, ctx->objectclass, ctx->sid,
     510             :                                        ctx->dn);
     511             : 
     512             :         /*
     513             :          * if the user does not have the rights to view this attribute, then we
     514             :          * should not return the object as a search result, i.e. act as if the
     515             :          * object doesn't exist (for this particular user, at least)
     516             :          */
     517     1162762 :         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
     518        2266 :                 ctx->suppress_result = true;
     519        2266 :                 return LDB_SUCCESS;
     520             :         }
     521             : 
     522     1160482 :         return ret;
     523             : }
     524             : 
     525             : /*
     526             :  * Traverse the search-tree to check that the user has sufficient access rights
     527             :  * to view all the attributes.
     528             :  */
     529     1293322 : static int check_search_ops_access(struct aclread_context *ac,
     530             :                                    TALLOC_CTX *mem_ctx,
     531             :                                    struct security_descriptor *sd,
     532             :                                    const struct dsdb_class *objectclass,
     533             :                                    struct dom_sid *sid, struct ldb_dn *dn,
     534             :                                    bool *suppress_result)
     535             : {
     536             :         int ret;
     537     1293554 :         struct parse_tree_aclread_ctx ctx = { 0 };
     538     1293554 :         struct ldb_parse_tree *tree = ac->req->op.search.tree;
     539             : 
     540     1293554 :         ctx.ac = ac;
     541     1293554 :         ctx.mem_ctx = mem_ctx;
     542     1293322 :         ctx.suppress_result = false;
     543     1293554 :         ctx.sid = sid;
     544     1293554 :         ctx.dn = dn;
     545     1293554 :         ctx.sd = sd;
     546     1293554 :         ctx.objectclass = objectclass;
     547             : 
     548             :         /* walk the search tree, checking each attribute as we go */
     549     1293554 :         ret = ldb_parse_tree_walk(tree, parse_tree_check_attr_access, &ctx);
     550             : 
     551             :         /* return whether this search result should be hidden to this user */
     552     1293554 :         *suppress_result = ctx.suppress_result;
     553     1293322 :         return ret;
     554             : }
     555             : 
     556     2028738 : static int aclread_callback(struct ldb_request *req, struct ldb_reply *ares)
     557             : {
     558             :         struct ldb_context *ldb;
     559             :         struct aclread_context *ac;
     560             :         struct ldb_message *ret_msg;
     561             :         struct ldb_message *msg;
     562             :         int ret;
     563     2028738 :         size_t num_of_attrs = 0;
     564     2028738 :         unsigned int i, k = 0;
     565     2028738 :         struct security_descriptor *sd = NULL;
     566     2028738 :         struct dom_sid *sid = NULL;
     567             :         TALLOC_CTX *tmp_ctx;
     568             :         const struct dsdb_class *objectclass;
     569     2028738 :         bool suppress_result = false;
     570             : 
     571     2028738 :         ac = talloc_get_type(req->context, struct aclread_context);
     572     2028738 :         ldb = ldb_module_get_ctx(ac->module);
     573     2028738 :         if (!ares) {
     574           0 :                 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR );
     575             :         }
     576     2028738 :         if (ares->error != LDB_SUCCESS) {
     577           3 :                 return ldb_module_done(ac->req, ares->controls,
     578             :                                        ares->response, ares->error);
     579             :         }
     580     2028735 :         tmp_ctx = talloc_new(ac);
     581     2028735 :         switch (ares->type) {
     582     1320983 :         case LDB_REPLY_ENTRY:
     583     1320983 :                 msg = ares->message;
     584     1320983 :                 ret = aclread_get_sd_from_ldb_message(ac, msg, &sd);
     585     1320983 :                 if (ret != LDB_SUCCESS) {
     586           0 :                         ldb_debug_set(ldb, LDB_DEBUG_FATAL,
     587             :                                       "acl_read: cannot get descriptor of %s: %s\n",
     588             :                                       ldb_dn_get_linearized(msg->dn), ldb_strerror(ret));
     589           0 :                         ret = LDB_ERR_OPERATIONS_ERROR;
     590           0 :                         goto fail;
     591     1320983 :                 } else if (sd == NULL) {
     592           0 :                         ldb_debug_set(ldb, LDB_DEBUG_FATAL,
     593             :                                       "acl_read: cannot get descriptor of %s (attribute not found)\n",
     594             :                                       ldb_dn_get_linearized(msg->dn));
     595           0 :                         ret = LDB_ERR_OPERATIONS_ERROR;
     596           0 :                         goto fail;
     597             :                 }
     598             :                 /*
     599             :                  * Get the most specific structural object class for the ACL check
     600             :                  */
     601     1320983 :                 objectclass = dsdb_get_structural_oc_from_msg(ac->schema, msg);
     602     1320983 :                 if (objectclass == NULL) {
     603           0 :                         ldb_asprintf_errstring(ldb, "acl_read: Failed to find a structural class for %s",
     604             :                                                ldb_dn_get_linearized(msg->dn));
     605           0 :                         ret = LDB_ERR_OPERATIONS_ERROR;
     606           0 :                         goto fail;
     607             :                 }
     608             : 
     609     1320983 :                 sid = samdb_result_dom_sid(tmp_ctx, msg, "objectSid");
     610     1320983 :                 if (!ldb_dn_is_null(msg->dn)) {
     611             :                         /*
     612             :                          * this is a real object, so we have
     613             :                          * to check for visibility
     614             :                          */
     615     1320983 :                         ret = aclread_check_object_visible(ac, msg, req);
     616     1320983 :                         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
     617       26471 :                                 talloc_free(tmp_ctx);
     618       26471 :                                 return LDB_SUCCESS;
     619     1294512 :                         } else if (ret != LDB_SUCCESS) {
     620           0 :                                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
     621             :                                               "acl_read: %s check parent %s - %s\n",
     622             :                                               ldb_dn_get_linearized(msg->dn),
     623             :                                               ldb_strerror(ret),
     624             :                                               ldb_errstring(ldb));
     625           0 :                                 goto fail;
     626             :                         }
     627             :                 }
     628             : 
     629             :                 /* for every element in the message check RP */
     630     9181676 :                 for (i=0; i < msg->num_elements; i++) {
     631             :                         const struct dsdb_attribute *attr;
     632             :                         bool is_sd, is_objectsid, is_instancetype, is_objectclass;
     633             :                         uint32_t access_mask;
     634     7888459 :                         attr = dsdb_attribute_by_lDAPDisplayName(ac->schema,
     635     7888459 :                                                                  msg->elements[i].name);
     636     7888459 :                         if (!attr) {
     637           0 :                                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
     638             :                                               "acl_read: %s cannot find attr[%s] in of schema\n",
     639             :                                               ldb_dn_get_linearized(msg->dn),
     640           0 :                                               msg->elements[i].name);
     641           0 :                                 ret = LDB_ERR_OPERATIONS_ERROR;
     642           0 :                                 goto fail;
     643             :                         }
     644     7888459 :                         is_sd = ldb_attr_cmp("nTSecurityDescriptor",
     645             :                                               msg->elements[i].name) == 0;
     646     7888459 :                         is_objectsid = ldb_attr_cmp("objectSid",
     647             :                                                     msg->elements[i].name) == 0;
     648     7888459 :                         is_instancetype = ldb_attr_cmp("instanceType",
     649             :                                                        msg->elements[i].name) == 0;
     650     7888459 :                         is_objectclass = ldb_attr_cmp("objectClass",
     651             :                                                       msg->elements[i].name) == 0;
     652             :                         /* these attributes were added to perform access checks and must be removed */
     653     7888459 :                         if (is_objectsid && ac->added_objectSid) {
     654      825995 :                                 aclread_mark_inaccesslible(&msg->elements[i]);
     655      825995 :                                 continue;
     656             :                         }
     657     7062464 :                         if (is_instancetype && ac->added_instanceType) {
     658     1207743 :                                 aclread_mark_inaccesslible(&msg->elements[i]);
     659     1207743 :                                 continue;
     660             :                         }
     661     5854721 :                         if (is_objectclass && ac->added_objectClass) {
     662     1207047 :                                 aclread_mark_inaccesslible(&msg->elements[i]);
     663     1207047 :                                 continue;
     664             :                         }
     665     4647674 :                         if (is_sd && ac->added_nTSecurityDescriptor) {
     666     1278328 :                                 aclread_mark_inaccesslible(&msg->elements[i]);
     667     1278328 :                                 continue;
     668             :                         }
     669             : 
     670     3369346 :                         access_mask = get_attr_access_mask(attr, ac->sd_flags);
     671             : 
     672     3369346 :                         if (access_mask == 0) {
     673           0 :                                 aclread_mark_inaccesslible(&msg->elements[i]);
     674           0 :                                 continue;
     675             :                         }
     676             : 
     677     3369346 :                         ret = acl_check_access_on_attribute(ac->module,
     678             :                                                             tmp_ctx,
     679             :                                                             sd,
     680             :                                                             sid,
     681             :                                                             access_mask,
     682             :                                                             attr,
     683             :                                                             objectclass);
     684             : 
     685             :                         /*
     686             :                          * Dirsync control needs the replpropertymetadata attribute
     687             :                          * so return it as it will be removed by the control
     688             :                          * in anycase.
     689             :                          */
     690     3369346 :                         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
     691             :                                 bool in_search_filter;
     692             : 
     693             :                                 /* check if attr is part of the search filter */
     694       33027 :                                 in_search_filter = dsdb_attr_in_parse_tree(ac->req->op.search.tree,
     695       33027 :                                                                 msg->elements[i].name);
     696             : 
     697       33027 :                                 if (in_search_filter) {
     698             : 
     699             :                                         /*
     700             :                                          * We are doing dirysnc answers
     701             :                                          * and the object shouldn't be returned (normally)
     702             :                                          * but we will return it without replPropertyMetaData
     703             :                                          * so that the dirysync module will do what is needed
     704             :                                          * (remove the object if it is not deleted, or return
     705             :                                          * just the objectGUID if it's deleted).
     706             :                                          */
     707        1063 :                                         if (ac->indirsync) {
     708         105 :                                                 ldb_msg_remove_attr(msg, "replPropertyMetaData");
     709         105 :                                                 break;
     710             :                                         } else {
     711             : 
     712             :                                                 /* do not return this entry */
     713         958 :                                                 talloc_free(tmp_ctx);
     714         958 :                                                 return LDB_SUCCESS;
     715             :                                         }
     716             :                                 } else {
     717       31964 :                                         aclread_mark_inaccesslible(&msg->elements[i]);
     718             :                                 }
     719     3336319 :                         } else if (ret != LDB_SUCCESS) {
     720           0 :                                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
     721             :                                               "acl_read: %s check attr[%s] gives %s - %s\n",
     722             :                                               ldb_dn_get_linearized(msg->dn),
     723           0 :                                               msg->elements[i].name,
     724             :                                               ldb_strerror(ret),
     725             :                                               ldb_errstring(ldb));
     726           0 :                                 goto fail;
     727             :                         }
     728             :                 }
     729             : 
     730             :                 /*
     731             :                  * check access rights for the search attributes, as well as the
     732             :                  * attribute values actually being returned
     733             :                  */
     734     1293786 :                 ret = check_search_ops_access(ac, tmp_ctx, sd, objectclass, sid,
     735             :                                               msg->dn, &suppress_result);
     736     1293554 :                 if (ret != LDB_SUCCESS) {
     737           0 :                         ldb_debug_set(ldb, LDB_DEBUG_FATAL,
     738             :                                       "acl_read: %s check search ops %s - %s\n",
     739             :                                       ldb_dn_get_linearized(msg->dn),
     740             :                                       ldb_strerror(ret), ldb_errstring(ldb));
     741           0 :                         goto fail;
     742             :                 }
     743             : 
     744     1293554 :                 if (suppress_result) {
     745             : 
     746             :                         /*
     747             :                          * As per the above logic, we strip replPropertyMetaData
     748             :                          * out of the msg so that the dirysync module will do
     749             :                          * what is needed (return just the objectGUID if it's,
     750             :                          * deleted, or remove the object if it is not).
     751             :                          */
     752        2266 :                         if (ac->indirsync) {
     753         220 :                                 ldb_msg_remove_attr(msg, "replPropertyMetaData");
     754             :                         } else {
     755        2046 :                                 talloc_free(tmp_ctx);
     756        2046 :                                 return LDB_SUCCESS;
     757             :                         }
     758             :                 }
     759             : 
     760     9137638 :                 for (i=0; i < msg->num_elements; i++) {
     761     7847355 :                         if (!aclread_is_inaccessible(&msg->elements[i])) {
     762     3309453 :                                 num_of_attrs++;
     763             :                         }
     764             :                 }
     765             :                 /*create a new message to return*/
     766     1291508 :                 ret_msg = ldb_msg_new(ac->req);
     767     1291508 :                 ret_msg->dn = msg->dn;
     768     1291508 :                 talloc_steal(ret_msg, msg->dn);
     769     1291508 :                 ret_msg->num_elements = num_of_attrs;
     770     1291508 :                 if (num_of_attrs > 0) {
     771     1052658 :                         ret_msg->elements = talloc_array(ret_msg,
     772             :                                                          struct ldb_message_element,
     773             :                                                          num_of_attrs);
     774     1052658 :                         if (ret_msg->elements == NULL) {
     775           0 :                                 return ldb_oom(ldb);
     776             :                         }
     777     8126530 :                         for (i=0; i < msg->num_elements; i++) {
     778     7074521 :                                 bool to_remove = aclread_is_inaccessible(&msg->elements[i]);
     779     7073960 :                                 if (!to_remove) {
     780     3309453 :                                         ret_msg->elements[k] = msg->elements[i];
     781     3309453 :                                         talloc_steal(ret_msg->elements, msg->elements[i].name);
     782     3309453 :                                         talloc_steal(ret_msg->elements, msg->elements[i].values);
     783     3309453 :                                         k++;
     784             :                                 }
     785             :                         }
     786             :                         /*
     787             :                          * This should not be needed, but some modules
     788             :                          * may allocate values on the wrong context...
     789             :                          */
     790     1052658 :                         talloc_steal(ret_msg->elements, msg);
     791             :                 } else {
     792      238850 :                         ret_msg->elements = NULL;
     793             :                 }
     794     1291508 :                 talloc_free(tmp_ctx);
     795             : 
     796     1291508 :                 ac->num_entries++;
     797     1291508 :                 return ldb_module_send_entry(ac->req, ret_msg, ares->controls);
     798       90566 :         case LDB_REPLY_REFERRAL:
     799       90566 :                 return ldb_module_send_referral(ac->req, ares->referral);
     800      617186 :         case LDB_REPLY_DONE:
     801      617186 :                 if (ac->base_invisible && ac->num_entries == 0) {
     802             :                         /*
     803             :                          * If the base is invisible and we didn't
     804             :                          * returned any object, we need to return
     805             :                          * NO_SUCH_OBJECT.
     806             :                          */
     807        3258 :                         return ldb_module_done(ac->req,
     808             :                                                NULL, NULL,
     809             :                                                LDB_ERR_NO_SUCH_OBJECT);
     810             :                 }
     811      613928 :                 return ldb_module_done(ac->req, ares->controls,
     812             :                                         ares->response, LDB_SUCCESS);
     813             : 
     814             :         }
     815           0 :         return LDB_SUCCESS;
     816           0 : fail:
     817           0 :         talloc_free(tmp_ctx);
     818           0 :         return ldb_module_done(ac->req, NULL, NULL, ret);
     819             : }
     820             : 
     821             : 
     822    22593928 : static int aclread_search(struct ldb_module *module, struct ldb_request *req)
     823             : {
     824             :         struct ldb_context *ldb;
     825             :         int ret;
     826             :         struct aclread_context *ac;
     827             :         struct ldb_request *down_req;
     828    22593928 :         struct ldb_control *as_system = ldb_request_get_control(req, LDB_CONTROL_AS_SYSTEM_OID);
     829    22593928 :         uint32_t flags = ldb_req_get_custom_flags(req);
     830             :         struct ldb_result *res;
     831             :         struct aclread_private *p;
     832    22593928 :         bool need_sd = false;
     833    22593928 :         bool explicit_sd_flags = false;
     834    22593928 :         bool is_untrusted = ldb_req_is_untrusted(req);
     835             :         static const char * const _all_attrs[] = { "*", NULL };
     836    22593928 :         bool all_attrs = false;
     837    22593928 :         const char * const *attrs = NULL;
     838             :         static const char *acl_attrs[] = {
     839             :                 "instanceType",
     840             :                 NULL
     841             :         };
     842             : 
     843    22593928 :         ldb = ldb_module_get_ctx(module);
     844    22593928 :         p = talloc_get_type(ldb_module_get_private(module), struct aclread_private);
     845             : 
     846             :         /* skip access checks if we are system or system control is supplied
     847             :          * or this is not LDAP server request */
     848    44922950 :         if (!p || !p->enabled ||
     849    22329022 :             dsdb_module_am_system(module)
     850     7781106 :             || as_system || !is_untrusted) {
     851    21930684 :                 return ldb_next_request(module, req);
     852             :         }
     853             :         /* no checks on special dn */
     854      663244 :         if (ldb_dn_is_special(req->op.search.base)) {
     855       42924 :                 return ldb_next_request(module, req);
     856             :         }
     857             : 
     858      620320 :         ac = talloc_zero(req, struct aclread_context);
     859      620320 :         if (ac == NULL) {
     860           0 :                 return ldb_oom(ldb);
     861             :         }
     862      620320 :         ac->module = module;
     863      620320 :         ac->req = req;
     864      620320 :         ac->schema = dsdb_get_schema(ldb, req);
     865      620320 :         if (flags & DSDB_ACL_CHECKS_DIRSYNC_FLAG) {
     866        1505 :                 ac->indirsync = true;
     867             :         } else {
     868      618815 :                 ac->indirsync = false;
     869             :         }
     870      620320 :         if (!ac->schema) {
     871           0 :                 return ldb_operr(ldb);
     872             :         }
     873             : 
     874      620320 :         attrs = req->op.search.attrs;
     875      620320 :         if (attrs == NULL) {
     876       50500 :                 all_attrs = true;
     877       50500 :                 attrs = _all_attrs;
     878      569818 :         } else if (ldb_attr_in_list(attrs, "*")) {
     879       16508 :                 all_attrs = true;
     880             :         }
     881             : 
     882             :         /*
     883             :          * In theory we should also check for the SD control but control verification is
     884             :          * expensive so we'd better had the ntsecuritydescriptor to the list of
     885             :          * searched attribute and then remove it !
     886             :          */
     887      620320 :         ac->sd_flags = dsdb_request_sd_flags(ac->req, &explicit_sd_flags);
     888             : 
     889      620320 :         if (ldb_attr_in_list(attrs, "nTSecurityDescriptor")) {
     890       15866 :                 need_sd = false;
     891      604454 :         } else if (explicit_sd_flags && all_attrs) {
     892         216 :                 need_sd = false;
     893             :         } else {
     894      604238 :                 need_sd = true;
     895             :         }
     896             : 
     897      620320 :         if (!all_attrs) {
     898      553310 :                 if (!ldb_attr_in_list(attrs, "instanceType")) {
     899      542008 :                         attrs = ldb_attr_list_copy_add(ac, attrs, "instanceType");
     900      542008 :                         if (attrs == NULL) {
     901           0 :                                 return ldb_oom(ldb);
     902             :                         }
     903      542008 :                         ac->added_instanceType = true;
     904             :                 }
     905      553310 :                 if (!ldb_attr_in_list(req->op.search.attrs, "objectSid")) {
     906      540029 :                         attrs = ldb_attr_list_copy_add(ac, attrs, "objectSid");
     907      540029 :                         if (attrs == NULL) {
     908           0 :                                 return ldb_oom(ldb);
     909             :                         }
     910      540029 :                         ac->added_objectSid = true;
     911             :                 }
     912      553310 :                 if (!ldb_attr_in_list(req->op.search.attrs, "objectClass")) {
     913      541441 :                         attrs = ldb_attr_list_copy_add(ac, attrs, "objectClass");
     914      541441 :                         if (attrs == NULL) {
     915           0 :                                 return ldb_oom(ldb);
     916             :                         }
     917      541441 :                         ac->added_objectClass = true;
     918             :                 }
     919             :         }
     920             : 
     921      620320 :         if (need_sd) {
     922      604238 :                 attrs = ldb_attr_list_copy_add(ac, attrs, "nTSecurityDescriptor");
     923      604238 :                 if (attrs == NULL) {
     924           0 :                         return ldb_oom(ldb);
     925             :                 }
     926      604238 :                 ac->added_nTSecurityDescriptor = true;
     927             :         }
     928             : 
     929      620320 :         ac->attrs = req->op.search.attrs;
     930             : 
     931             :         /* check accessibility of base */
     932      620320 :         if (!ldb_dn_is_null(req->op.search.base)) {
     933      620059 :                 ret = dsdb_module_search_dn(module, req, &res, req->op.search.base,
     934             :                                             acl_attrs,
     935             :                                             DSDB_FLAG_NEXT_MODULE |
     936             :                                             DSDB_FLAG_AS_SYSTEM |
     937             :                                             DSDB_SEARCH_SHOW_RECYCLED,
     938             :                                             req);
     939      620059 :                 if (ret != LDB_SUCCESS) {
     940         232 :                         return ldb_error(ldb, ret,
     941             :                                         "acl_read: Error retrieving instanceType for base.");
     942             :                 }
     943      619827 :                 ret = aclread_check_object_visible(ac, res->msgs[0], req);
     944      619827 :                 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
     945        8649 :                         if (req->op.search.scope == LDB_SCOPE_BASE) {
     946        2889 :                                 return ldb_module_done(req, NULL, NULL,
     947             :                                                        LDB_ERR_NO_SUCH_OBJECT);
     948             :                         }
     949             :                         /*
     950             :                          * Defer LDB_ERR_NO_SUCH_OBJECT,
     951             :                          * we may return sub objects
     952             :                          */
     953        5760 :                         ac->base_invisible = true;
     954      611178 :                 } else if (ret != LDB_SUCCESS) {
     955           0 :                         return ldb_module_done(req, NULL, NULL, ret);
     956             :                 }
     957             :         }
     958             : 
     959      617199 :         ret = ldb_build_search_req_ex(&down_req,
     960             :                                       ldb, ac,
     961             :                                       req->op.search.base,
     962             :                                       req->op.search.scope,
     963             :                                       req->op.search.tree,
     964             :                                       attrs,
     965             :                                       req->controls,
     966             :                                       ac, aclread_callback,
     967             :                                       req);
     968             : 
     969      617199 :         if (ret != LDB_SUCCESS) {
     970           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     971             :         }
     972             : 
     973      617199 :         return ldb_next_request(module, down_req);
     974             : }
     975             : 
     976      132453 : static int aclread_init(struct ldb_module *module)
     977             : {
     978      132453 :         struct ldb_context *ldb = ldb_module_get_ctx(module);
     979      132453 :         struct aclread_private *p = talloc_zero(module, struct aclread_private);
     980      132453 :         if (p == NULL) {
     981           0 :                 return ldb_module_oom(module);
     982             :         }
     983      132453 :         p->enabled = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"), NULL, "acl", "search", true);
     984      132453 :         ldb_module_set_private(module, p);
     985      132453 :         return ldb_next_init(module);
     986             : }
     987             : 
     988             : static const struct ldb_module_ops ldb_aclread_module_ops = {
     989             :         .name              = "aclread",
     990             :         .search            = aclread_search,
     991             :         .init_context      = aclread_init
     992             : };
     993             : 
     994        5536 : int ldb_aclread_module_init(const char *version)
     995             : {
     996        5536 :         LDB_MODULE_CHECK_VERSION(version);
     997        5536 :         return ldb_register_module(&ldb_aclread_module_ops);
     998             : }

Generated by: LCOV version 1.13