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 master 2f515e9b Lines: 450 528 85.2 %
Date: 2024-04-21 15:09:00 Functions: 22 22 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             : #include "lib/util/binsearch.h"
      41             : 
      42             : #undef strcasecmp
      43             : 
      44             : struct ldb_attr_vec {
      45             :         const char** attrs;
      46             :         size_t len;
      47             :         size_t capacity;
      48             : };
      49             : 
      50             : struct aclread_context {
      51             :         struct ldb_module *module;
      52             :         struct ldb_request *req;
      53             :         const struct dsdb_schema *schema;
      54             :         uint32_t sd_flags;
      55             :         bool added_nTSecurityDescriptor;
      56             :         bool added_instanceType;
      57             :         bool added_objectSid;
      58             :         bool added_objectClass;
      59             : 
      60             :         bool do_list_object_initialized;
      61             :         bool do_list_object;
      62             :         bool base_invisible;
      63             :         uint64_t num_entries;
      64             : 
      65             :         /* cache on the last parent we checked in this search */
      66             :         struct ldb_dn *last_parent_dn;
      67             :         int last_parent_check_ret;
      68             : 
      69             :         bool am_administrator;
      70             : 
      71             :         bool got_tree_attrs;
      72             :         struct ldb_attr_vec tree_attrs;
      73             : };
      74             : 
      75             : struct aclread_private {
      76             :         bool enabled;
      77             : 
      78             :         /* cache of the last SD we read during any search */
      79             :         struct security_descriptor *sd_cached;
      80             :         struct ldb_val sd_cached_blob;
      81             :         const char **password_attrs;
      82             :         size_t num_password_attrs;
      83             : };
      84             : 
      85             : struct access_check_context {
      86             :         struct security_descriptor *sd;
      87             :         struct dom_sid sid_buf;
      88             :         const struct dom_sid *sid;
      89             :         const struct dsdb_class *objectclass;
      90             : };
      91             : 
      92     3131819 : static void acl_element_mark_access_checked(struct ldb_message_element *el)
      93             : {
      94     3131819 :         el->flags |= LDB_FLAG_INTERNAL_ACCESS_CHECKED;
      95     3131819 : }
      96             : 
      97     5325805 : static bool acl_element_is_access_checked(const struct ldb_message_element *el)
      98             : {
      99     5325805 :         return (el->flags & LDB_FLAG_INTERNAL_ACCESS_CHECKED) != 0;
     100             : }
     101             : 
     102    81604935 : static bool attr_in_vec(const struct ldb_attr_vec *vec, const char *attr)
     103             : {
     104    81604935 :         const char **found = NULL;
     105             : 
     106    81604935 :         if (vec == NULL) {
     107           0 :                 return false;
     108             :         }
     109             : 
     110   150817102 :         BINARY_ARRAY_SEARCH_V(vec->attrs,
     111             :                               vec->len,
     112             :                               attr,
     113             :                               ldb_attr_cmp,
     114             :                               found);
     115    81604935 :         return found != NULL;
     116             : }
     117             : 
     118      216164 : static int acl_attr_cmp_fn(const char *a, const char **b)
     119             : {
     120      216164 :         return ldb_attr_cmp(a, *b);
     121             : }
     122             : 
     123      290140 : static int attr_vec_add_unique(TALLOC_CTX *mem_ctx,
     124             :                                struct ldb_attr_vec *vec,
     125             :                                const char *attr)
     126             : {
     127      290140 :         const char **exact = NULL;
     128      290140 :         const char **next = NULL;
     129      290140 :         size_t next_idx = 0;
     130             : 
     131      506304 :         BINARY_ARRAY_SEARCH_GTE(vec->attrs,
     132             :                                 vec->len,
     133             :                                 attr,
     134             :                                 acl_attr_cmp_fn,
     135             :                                 exact,
     136             :                                 next);
     137      290126 :         if (exact != NULL) {
     138       70159 :                 return LDB_SUCCESS;
     139             :         }
     140             : 
     141      219981 :         if (vec->len == SIZE_MAX) {
     142           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     143             :         }
     144             : 
     145      219981 :         if (next != NULL) {
     146       66556 :                 next_idx = next - vec->attrs;
     147             :         }
     148             : 
     149      219981 :         if (vec->len >= vec->capacity) {
     150      149764 :                 const char **attrs = NULL;
     151             : 
     152      149764 :                 if (vec->capacity == 0) {
     153      137911 :                         vec->capacity = 4;
     154             :                 } else {
     155       11853 :                         if (vec->capacity > SIZE_MAX / 2) {
     156           0 :                                 return LDB_ERR_OPERATIONS_ERROR;
     157             :                         }
     158       11853 :                         vec->capacity *= 2;
     159             :                 }
     160             : 
     161      149764 :                 attrs = talloc_realloc(mem_ctx, vec->attrs, const char *, vec->capacity);
     162      149764 :                 if (attrs == NULL) {
     163           0 :                         return LDB_ERR_OPERATIONS_ERROR;
     164             :                 }
     165             : 
     166      149764 :                 vec->attrs = attrs;
     167             :         }
     168      219981 :         SMB_ASSERT(vec->len < vec->capacity);
     169             : 
     170      219981 :         if (next == NULL) {
     171      153425 :                 vec->attrs[vec->len++] = attr;
     172             :         } else {
     173       66556 :                 size_t count = (vec->len - next_idx) * sizeof (vec->attrs[0]);
     174       66556 :                 memmove(&vec->attrs[next_idx + 1],
     175       66556 :                         &vec->attrs[next_idx],
     176             :                         count);
     177             : 
     178       66556 :                 vec->attrs[next_idx] = attr;
     179       66556 :                 ++vec->len;
     180             :         }
     181             : 
     182      219967 :         return LDB_SUCCESS;
     183             : }
     184             : 
     185     1308421 : static bool ldb_attr_always_present(const char *attr)
     186             : {
     187         510 :         static const char * const attrs_always_present[] = {
     188             :                 "objectClass",
     189             :                 "distinguishedName",
     190             :                 "name",
     191             :                 "objectGUID",
     192             :                 NULL
     193             :         };
     194             : 
     195     1308421 :         return ldb_attr_in_list(attrs_always_present, attr);
     196             : }
     197             : 
     198      978586 : static bool ldb_attr_always_visible(const char *attr)
     199             : {
     200         282 :         static const char * const attrs_always_visible[] = {
     201             :                 "isDeleted",
     202             :                 "isRecycled",
     203             :                 NULL
     204             :         };
     205             : 
     206      978586 :         return ldb_attr_in_list(attrs_always_visible, attr);
     207             : }
     208             : 
     209             : /* Collect a list of attributes required to match a given parse tree. */
     210     4524454 : static int ldb_parse_tree_collect_acl_attrs(const struct ldb_module *module,
     211             :                                             TALLOC_CTX *mem_ctx,
     212             :                                             struct ldb_attr_vec *attrs,
     213             :                                             const struct ldb_parse_tree *tree)
     214             : {
     215     4524722 :         const char *attr = NULL;
     216        1580 :         unsigned int i;
     217        1580 :         int ret;
     218             : 
     219     4524722 :         if (tree == NULL) {
     220           0 :                 return 0;
     221             :         }
     222             : 
     223     4524722 :         switch (tree->operation) {
     224     1459221 :         case LDB_OP_OR:
     225             :         case LDB_OP_AND:                /* attributes stored in list of subtrees */
     226     4444810 :                 for (i = 0; i < tree->u.list.num_elements; i++) {
     227     2986111 :                         ret = ldb_parse_tree_collect_acl_attrs(module, mem_ctx,
     228     2985067 :                                                                attrs, tree->u.list.elements[i]);
     229     2985067 :                         if (ret) {
     230           0 :                                 return ret;
     231             :                         }
     232             :                 }
     233     1459221 :                 return 0;
     234             : 
     235      753471 :         case LDB_OP_NOT:                /* attributes stored in single subtree */
     236      753471 :                 return ldb_parse_tree_collect_acl_attrs(module, mem_ctx, attrs, tree->u.isnot.child);
     237             : 
     238     1308421 :         case LDB_OP_PRESENT:
     239             :                 /*
     240             :                  * If the search filter is checking for an attribute's presence,
     241             :                  * and the attribute is always present, we can skip access
     242             :                  * rights checks. Every object has these attributes, and so
     243             :                  * there's no security reason to hide their presence.
     244             :                  * Note: the acl.py tests (e.g. test_search1()) rely on this
     245             :                  * exception.  I.e. even if we lack Read Property (RP) rights
     246             :                  * for a child object, it should still appear as a visible
     247             :                  * object in 'objectClass=*' searches, so long as we have List
     248             :                  * Contents (LC) rights for the object.
     249             :                  */
     250     1308421 :                 if (ldb_attr_always_present(tree->u.present.attr)) {
     251             :                         /* No need to check this attribute. */
     252     1292907 :                         return 0;
     253             :                 }
     254             : 
     255       15006 :                 if (ldb_attr_always_visible(tree->u.present.attr)) {
     256             :                         /* No need to check this attribute. */
     257        1308 :                         return 0;
     258             :                 }
     259             : 
     260       13696 :                 break;
     261             : 
     262      963580 :         case LDB_OP_EQUALITY:
     263      963580 :                 if (ldb_attr_always_visible(tree->u.equality.attr)) {
     264             :                         /* No need to check this attribute. */
     265      726377 :                         return 0;
     266             :                 }
     267             : 
     268      236923 :                 break;
     269             : 
     270       39507 :         default:                        /* single attribute in tree */
     271       39507 :                 break;
     272             :         }
     273             : 
     274      290140 :         attr = ldb_parse_tree_get_attr(tree);
     275      290140 :         return attr_vec_add_unique(mem_ctx, attrs, attr);
     276             : }
     277             : 
     278             : /*
     279             :  * the object has a parent, so we have to check for visibility
     280             :  *
     281             :  * This helper function uses a per-search cache to avoid checking the
     282             :  * parent object for each of many possible children.  This is likely
     283             :  * to help on SCOPE_ONE searches and on typical tree structures for
     284             :  * SCOPE_SUBTREE, where an OU has many users as children.
     285             :  *
     286             :  * We rely for safety on the DB being locked for reads during the full
     287             :  * search.
     288             :  */
     289     2214599 : static int aclread_check_parent(struct aclread_context *ac,
     290             :                                 struct ldb_message *msg,
     291             :                                 struct ldb_request *req)
     292             : {
     293         520 :         int ret;
     294     2214599 :         struct ldb_dn *parent_dn = NULL;
     295             : 
     296             :         /* We may have a cached result from earlier in this search */
     297     2214599 :         if (ac->last_parent_dn != NULL) {
     298             :                 /*
     299             :                  * We try the no-allocation ldb_dn_compare_base()
     300             :                  * first however it will not tell parents and
     301             :                  * grand-parents apart
     302             :                  */
     303     1441375 :                 int cmp_base = ldb_dn_compare_base(ac->last_parent_dn,
     304             :                                                    msg->dn);
     305     1441375 :                 if (cmp_base == 0) {
     306             :                         /* Now check if it is a direct parent */
     307     1362333 :                         parent_dn = ldb_dn_get_parent(ac, msg->dn);
     308     1362333 :                         if (parent_dn == NULL) {
     309           0 :                                 return ldb_oom(ldb_module_get_ctx(ac->module));
     310             :                         }
     311     1362333 :                         if (ldb_dn_compare(ac->last_parent_dn,
     312             :                                            parent_dn) == 0) {
     313     1291138 :                                 TALLOC_FREE(parent_dn);
     314             : 
     315             :                                 /*
     316             :                                  * If we checked the same parent last
     317             :                                  * time, then return the cached
     318             :                                  * result.
     319             :                                  *
     320             :                                  * The cache is valid as long as the
     321             :                                  * search as the DB is read locked and
     322             :                                  * the session_info (connected user)
     323             :                                  * is constant.
     324             :                                  */
     325     1291138 :                                 return ac->last_parent_check_ret;
     326             :                         }
     327             :                 }
     328             :         }
     329             : 
     330             :         {
     331      923461 :                 TALLOC_CTX *frame = NULL;
     332      923461 :                 frame = talloc_stackframe();
     333             : 
     334             :                 /*
     335             :                  * This may have been set in the block above, don't
     336             :                  * re-parse
     337             :                  */
     338      923461 :                 if (parent_dn == NULL) {
     339      852266 :                         parent_dn = ldb_dn_get_parent(ac, msg->dn);
     340      852266 :                         if (parent_dn == NULL) {
     341           0 :                                 TALLOC_FREE(frame);
     342           0 :                                 return ldb_oom(ldb_module_get_ctx(ac->module));
     343             :                         }
     344             :                 }
     345      923461 :                 ret = dsdb_module_check_access_on_dn(ac->module,
     346             :                                                      frame,
     347             :                                                      parent_dn,
     348             :                                                      SEC_ADS_LIST,
     349             :                                                      NULL, req);
     350      923461 :                 talloc_unlink(ac, ac->last_parent_dn);
     351      923461 :                 ac->last_parent_dn = parent_dn;
     352      923461 :                 ac->last_parent_check_ret = ret;
     353             : 
     354      923461 :                 TALLOC_FREE(frame);
     355             :         }
     356      923195 :         return ret;
     357             : }
     358             : 
     359     2308946 : static int aclread_check_object_visible(struct aclread_context *ac,
     360             :                                         struct ldb_message *msg,
     361             :                                         struct ldb_request *req)
     362             : {
     363         536 :         uint32_t instanceType;
     364         536 :         int ret;
     365             : 
     366             :         /* get the object instance type */
     367     2308946 :         instanceType = ldb_msg_find_attr_as_uint(msg,
     368             :                                                  "instanceType", 0);
     369     2308946 :         if (instanceType & INSTANCE_TYPE_IS_NC_HEAD) {
     370             :                 /*
     371             :                  * NC_HEAD objects are always visible
     372             :                  */
     373       94331 :                 return LDB_SUCCESS;
     374             :         }
     375             : 
     376     2214599 :         ret = aclread_check_parent(ac, msg, req);
     377     2214599 :         if (ret == LDB_SUCCESS) {
     378             :                 /*
     379             :                  * SEC_ADS_LIST (List Children) alone
     380             :                  * on the parent is enough to make the
     381             :                  * object visible.
     382             :                  */
     383     2173029 :                 return LDB_SUCCESS;
     384             :         }
     385       41050 :         if (ret != LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
     386           0 :                 return ret;
     387             :         }
     388             : 
     389       41050 :         if (!ac->do_list_object_initialized) {
     390             :                 /*
     391             :                  * We only call dsdb_do_list_object() once
     392             :                  * and only when needed in order to
     393             :                  * check the dSHeuristics for fDoListObject.
     394             :                  */
     395       16280 :                 ac->do_list_object = dsdb_do_list_object(ac->module, ac, req);
     396       16280 :                 ac->do_list_object_initialized = true;
     397             :         }
     398             : 
     399       41050 :         if (ac->do_list_object) {
     400       12672 :                 TALLOC_CTX *frame = talloc_stackframe();
     401       12672 :                 struct ldb_dn *parent_dn = NULL;
     402             : 
     403             :                 /*
     404             :                  * Here we're in "List Object" mode (fDoListObject=true).
     405             :                  *
     406             :                  * If SEC_ADS_LIST (List Children) is not
     407             :                  * granted on the parent, we need to check if
     408             :                  * SEC_ADS_LIST_OBJECT (List Object) is granted
     409             :                  * on the parent and also on the object itself.
     410             :                  *
     411             :                  * We could optimize this similar to aclread_check_parent(),
     412             :                  * but that would require quite a bit of restructuring,
     413             :                  * so that we cache the granted access bits instead
     414             :                  * of just the result for 'SEC_ADS_LIST (List Children)'.
     415             :                  *
     416             :                  * But as this is the uncommon case and
     417             :                  * 'SEC_ADS_LIST (List Children)' is most likely granted
     418             :                  * on most of the objects, we'll just implement what
     419             :                  * we have to.
     420             :                  */
     421             : 
     422       12672 :                 parent_dn = ldb_dn_get_parent(frame, msg->dn);
     423       12672 :                 if (parent_dn == NULL) {
     424           0 :                         TALLOC_FREE(frame);
     425           0 :                         return ldb_oom(ldb_module_get_ctx(ac->module));
     426             :                 }
     427       12672 :                 ret = dsdb_module_check_access_on_dn(ac->module,
     428             :                                                      frame,
     429             :                                                      parent_dn,
     430             :                                                      SEC_ADS_LIST_OBJECT,
     431             :                                                      NULL, req);
     432       12672 :                 if (ret != LDB_SUCCESS) {
     433        6048 :                         TALLOC_FREE(frame);
     434        6048 :                         return ret;
     435             :                 }
     436        6624 :                 ret = dsdb_module_check_access_on_dn(ac->module,
     437             :                                                      frame,
     438             :                                                      msg->dn,
     439             :                                                      SEC_ADS_LIST_OBJECT,
     440             :                                                      NULL, req);
     441        6624 :                 if (ret != LDB_SUCCESS) {
     442        1872 :                         TALLOC_FREE(frame);
     443        1872 :                         return ret;
     444             :                 }
     445             : 
     446        4752 :                 TALLOC_FREE(frame);
     447        4752 :                 return LDB_SUCCESS;
     448             :         }
     449             : 
     450       28378 :         return LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS;
     451             : }
     452             : 
     453             : /*
     454             :  * The sd returned from this function is valid until the next call on
     455             :  * this module context
     456             :  *
     457             :  * This helper function uses a cache on the module private data to
     458             :  * speed up repeated use of the same SD.
     459             :  */
     460             : 
     461     3709502 : static int aclread_get_sd_from_ldb_message(struct aclread_context *ac,
     462             :                                            const struct ldb_message *acl_res,
     463             :                                            struct security_descriptor **sd)
     464             : {
     465         172 :         struct ldb_message_element *sd_element;
     466     3709502 :         struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
     467         172 :         struct aclread_private *private_data
     468     3709502 :                 = talloc_get_type_abort(ldb_module_get_private(ac->module),
     469             :                                   struct aclread_private);
     470         172 :         enum ndr_err_code ndr_err;
     471             : 
     472     3709502 :         sd_element = ldb_msg_find_element(acl_res, "nTSecurityDescriptor");
     473     3709502 :         if (sd_element == NULL) {
     474           0 :                 return ldb_error(ldb, LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS,
     475             :                                  "nTSecurityDescriptor is missing");
     476             :         }
     477             : 
     478     3709502 :         if (sd_element->num_values != 1) {
     479           0 :                 return ldb_operr(ldb);
     480             :         }
     481             : 
     482             :         /*
     483             :          * The time spent in ndr_pull_security_descriptor() is quite
     484             :          * expensive, so we check if this is the same binary blob as last
     485             :          * time, and if so return the memory tree from that previous parse.
     486             :          */
     487             : 
     488     3709502 :         if (private_data->sd_cached != NULL &&
     489     7375220 :             private_data->sd_cached_blob.data != NULL &&
     490     3687610 :             ldb_val_equal_exact(&sd_element->values[0],
     491     3687610 :                                 &private_data->sd_cached_blob)) {
     492     2514609 :                 *sd = private_data->sd_cached;
     493     2514609 :                 return LDB_SUCCESS;
     494             :         }
     495             : 
     496     1194893 :         *sd = talloc(private_data, struct security_descriptor);
     497     1194893 :         if(!*sd) {
     498           0 :                 return ldb_oom(ldb);
     499             :         }
     500     1194893 :         ndr_err = ndr_pull_struct_blob(&sd_element->values[0], *sd, *sd,
     501             :                              (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
     502             : 
     503     1194893 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
     504           0 :                 TALLOC_FREE(*sd);
     505           0 :                 return ldb_operr(ldb);
     506             :         }
     507             : 
     508     1194893 :         talloc_unlink(private_data, private_data->sd_cached_blob.data);
     509     1194893 :         private_data->sd_cached_blob = ldb_val_dup(private_data,
     510     1194893 :                                                    &sd_element->values[0]);
     511     1194893 :         if (private_data->sd_cached_blob.data == NULL) {
     512           0 :                 TALLOC_FREE(*sd);
     513           0 :                 return ldb_operr(ldb);
     514             :         }
     515             : 
     516     1194893 :         talloc_unlink(private_data, private_data->sd_cached);
     517     1194893 :         private_data->sd_cached = *sd;
     518             : 
     519     1194893 :         return LDB_SUCCESS;
     520             : }
     521             : 
     522             : /* Check whether the attribute is a password attribute. */
     523     7097974 : static bool attr_is_secret(const char *attr, const struct aclread_private *private_data)
     524             : {
     525     7097974 :         const char **found = NULL;
     526             : 
     527     7097974 :         if (private_data->password_attrs == NULL) {
     528           0 :                 return false;
     529             :         }
     530             : 
     531    38120427 :         BINARY_ARRAY_SEARCH_V(private_data->password_attrs,
     532             :                               private_data->num_password_attrs,
     533             :                               attr,
     534             :                               ldb_attr_cmp,
     535             :                               found);
     536     7097974 :         return found != NULL;
     537             : }
     538             : 
     539             : /*
     540             :  * Returns the access mask required to read a given attribute
     541             :  */
     542     7097289 : static uint32_t get_attr_access_mask(const struct dsdb_attribute *attr,
     543             :                                      uint32_t sd_flags)
     544             : {
     545             : 
     546     7097289 :         uint32_t access_mask = 0;
     547         307 :         bool is_sd;
     548             : 
     549             :         /* nTSecurityDescriptor is a special case */
     550     7097289 :         is_sd = (ldb_attr_cmp("nTSecurityDescriptor",
     551             :                               attr->lDAPDisplayName) == 0);
     552             : 
     553     7097289 :         if (is_sd) {
     554       24353 :                 if (sd_flags & (SECINFO_OWNER|SECINFO_GROUP)) {
     555       23247 :                         access_mask |= SEC_STD_READ_CONTROL;
     556             :                 }
     557       24353 :                 if (sd_flags & SECINFO_DACL) {
     558       24326 :                         access_mask |= SEC_STD_READ_CONTROL;
     559             :                 }
     560       24353 :                 if (sd_flags & SECINFO_SACL) {
     561       22682 :                         access_mask |= SEC_FLAG_SYSTEM_SECURITY;
     562             :                 }
     563             :         } else {
     564     7072629 :                 access_mask = SEC_ADS_READ_PROP;
     565             :         }
     566             : 
     567     7097289 :         if (attr->searchFlags & SEARCH_FLAG_CONFIDENTIAL) {
     568        6824 :                 access_mask |= SEC_ADS_CONTROL_ACCESS;
     569             :         }
     570             : 
     571     7097289 :         return access_mask;
     572             : }
     573             : 
     574             : /*
     575             :  * Checks that the user has sufficient access rights to view an attribute, else
     576             :  * marks it as inaccessible.
     577             :  */
     578     7097974 : static int acl_redact_attr(TALLOC_CTX *mem_ctx,
     579             :                            struct ldb_message_element *el,
     580             :                            struct aclread_context *ac,
     581             :                            const struct aclread_private *private_data,
     582             :                            const struct ldb_message *msg,
     583             :                            const struct dsdb_schema *schema,
     584             :                            const struct security_descriptor *sd,
     585             :                            const struct dom_sid *sid,
     586             :                            const struct dsdb_class *objectclass)
     587             : {
     588         307 :         int ret;
     589     7097974 :         const struct dsdb_attribute *attr = NULL;
     590         307 :         uint32_t access_mask;
     591     7097974 :         struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
     592             : 
     593     7097974 :         if (attr_is_secret(el->name, private_data)) {
     594         685 :                 ldb_msg_element_mark_inaccessible(el);
     595         685 :                 return LDB_SUCCESS;
     596             :         }
     597             : 
     598             :         /* Look up the attribute in the schema. */
     599     7097289 :         attr = dsdb_attribute_by_lDAPDisplayName(schema, el->name);
     600     7097289 :         if (!attr) {
     601           0 :                 ldb_debug_set(ldb,
     602             :                               LDB_DEBUG_FATAL,
     603             :                               "acl_read: %s cannot find attr[%s] in schema\n",
     604           0 :                               ldb_dn_get_linearized(msg->dn), el->name);
     605           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     606             :         }
     607             : 
     608     7097289 :         access_mask = get_attr_access_mask(attr, ac->sd_flags);
     609     7097289 :         if (access_mask == 0) {
     610           0 :                 DBG_ERR("Could not determine access mask for attribute %s\n",
     611             :                         el->name);
     612           0 :                 ldb_msg_element_mark_inaccessible(el);
     613           0 :                 return LDB_SUCCESS;
     614             :         }
     615             : 
     616             :         /* We must check whether the user has rights to view the attribute. */
     617             : 
     618     7097289 :         ret = acl_check_access_on_attribute_implicit_owner(ac->module, mem_ctx, sd, sid,
     619             :                                                            access_mask, attr, objectclass,
     620             :                                                            IMPLICIT_OWNER_READ_CONTROL_RIGHTS);
     621     7097289 :         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
     622       30076 :                 ldb_msg_element_mark_inaccessible(el);
     623     7067213 :         } else if (ret != LDB_SUCCESS) {
     624           0 :                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
     625             :                               "acl_read: %s check attr[%s] gives %s - %s\n",
     626           0 :                               ldb_dn_get_linearized(msg->dn), el->name,
     627             :                               ldb_strerror(ret), ldb_errstring(ldb));
     628           0 :                 return ret;
     629             :         }
     630             : 
     631     7096982 :         return LDB_SUCCESS;
     632             : }
     633             : 
     634     3709502 : static int setup_access_check_context(struct aclread_context *ac,
     635             :                                       const struct ldb_message *msg,
     636             :                                       struct access_check_context *ctx)
     637             : {
     638         172 :         int ret;
     639             : 
     640             :         /*
     641             :          * Fetch the schema so we can check which attributes are
     642             :          * considered confidential.
     643             :          */
     644     3709502 :         if (ac->schema == NULL) {
     645      598083 :                 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
     646             : 
     647             :                 /* Cache the schema for later use. */
     648      598083 :                 ac->schema = dsdb_get_schema(ldb, ac);
     649             : 
     650      598083 :                 if (ac->schema == NULL) {
     651           0 :                         return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
     652             :                                          "aclread_callback: Error obtaining schema.");
     653             :                 }
     654             :         }
     655             : 
     656             :         /* Fetch the object's security descriptor. */
     657     3709502 :         ret = aclread_get_sd_from_ldb_message(ac, msg, &ctx->sd);
     658     3709502 :         if (ret != LDB_SUCCESS) {
     659           0 :                 ldb_debug_set(ldb_module_get_ctx(ac->module), LDB_DEBUG_FATAL,
     660             :                               "acl_read: cannot get descriptor of %s: %s\n",
     661           0 :                               ldb_dn_get_linearized(msg->dn), ldb_strerror(ret));
     662           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     663     3709502 :         } else if (ctx->sd == NULL) {
     664           0 :                 ldb_debug_set(ldb_module_get_ctx(ac->module), LDB_DEBUG_FATAL,
     665             :                               "acl_read: cannot get descriptor of %s (attribute not found)\n",
     666           0 :                               ldb_dn_get_linearized(msg->dn));
     667           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     668             :         }
     669             :         /*
     670             :          * Get the most specific structural object class for the ACL check
     671             :          */
     672     3709502 :         ctx->objectclass = dsdb_get_structural_oc_from_msg(ac->schema, msg);
     673     3709502 :         if (ctx->objectclass == NULL) {
     674           0 :                 ldb_asprintf_errstring(ldb_module_get_ctx(ac->module),
     675             :                                        "acl_read: Failed to find a structural class for %s",
     676           0 :                                        ldb_dn_get_linearized(msg->dn));
     677           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     678             :         }
     679             : 
     680             :         /* Fetch the object's SID. */
     681     3709502 :         ret = samdb_result_dom_sid_buf(msg, "objectSid", &ctx->sid_buf);
     682     3709502 :         if (ret == LDB_SUCCESS) {
     683     3100932 :                 ctx->sid = &ctx->sid_buf;
     684      608570 :         } else if (ret == LDB_ERR_NO_SUCH_ATTRIBUTE) {
     685             :                 /* This is expected. */
     686      608570 :                 ctx->sid = NULL;
     687             :         } else {
     688           0 :                 ldb_asprintf_errstring(ldb_module_get_ctx(ac->module),
     689             :                                        "acl_read: Failed to parse objectSid as dom_sid for %s",
     690           0 :                                        ldb_dn_get_linearized(msg->dn));
     691           0 :                 return ret;
     692             :         }
     693             : 
     694     3709330 :         return LDB_SUCCESS;
     695             : }
     696             : 
     697             : /*
     698             :  * Whether this attribute was added to perform access checks and must be
     699             :  * removed.
     700             :  */
     701    10366663 : static bool should_remove_attr(const char *attr, const struct aclread_context *ac)
     702             : {
     703    10366663 :         if (ac->added_nTSecurityDescriptor &&
     704    10086295 :             ldb_attr_cmp("nTSecurityDescriptor", attr) == 0)
     705             :         {
     706     1459311 :                 return true;
     707             :         }
     708             : 
     709     8907084 :         if (ac->added_objectSid &&
     710     6118848 :             ldb_attr_cmp("objectSid", attr) == 0)
     711             :         {
     712      854238 :                 return true;
     713             :         }
     714             : 
     715     8052762 :         if (ac->added_instanceType &&
     716     5514342 :             ldb_attr_cmp("instanceType", attr) == 0)
     717             :         {
     718     1363732 :                 return true;
     719             :         }
     720             : 
     721     6688764 :         if (ac->added_objectClass &&
     722     4140310 :             ldb_attr_cmp("objectClass", attr) == 0)
     723             :         {
     724     1362959 :                 return true;
     725             :         }
     726             : 
     727     5325496 :         return false;
     728             : }
     729             : 
     730     2442996 : static int aclread_callback(struct ldb_request *req, struct ldb_reply *ares)
     731             : {
     732         572 :         struct aclread_context *ac;
     733     2442996 :         struct aclread_private *private_data = NULL;
     734         572 :         struct ldb_message *msg;
     735         572 :         int ret;
     736         572 :         unsigned int i;
     737         572 :         struct access_check_context acl_ctx;
     738             : 
     739     2442996 :         ac = talloc_get_type_abort(req->context, struct aclread_context);
     740     2442996 :         if (!ares) {
     741           0 :                 return ldb_module_done(ac->req, NULL, NULL, LDB_ERR_OPERATIONS_ERROR );
     742             :         }
     743     2442996 :         if (ares->error != LDB_SUCCESS) {
     744           8 :                 return ldb_module_done(ac->req, ares->controls,
     745             :                                        ares->response, ares->error);
     746             :         }
     747     2442988 :         switch (ares->type) {
     748     1511581 :         case LDB_REPLY_ENTRY:
     749     1511581 :                 msg = ares->message;
     750             : 
     751     1511581 :                 if (!ldb_dn_is_null(msg->dn)) {
     752             :                         /*
     753             :                          * this is a real object, so we have
     754             :                          * to check for visibility
     755             :                          */
     756     1511581 :                         ret = aclread_check_object_visible(ac, msg, req);
     757     1511581 :                         if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
     758       27649 :                                 return LDB_SUCCESS;
     759     1483932 :                         } else if (ret != LDB_SUCCESS) {
     760           0 :                                 struct ldb_context *ldb = ldb_module_get_ctx(ac->module);
     761           0 :                                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
     762             :                                               "acl_read: %s check parent %s - %s\n",
     763             :                                               ldb_dn_get_linearized(msg->dn),
     764             :                                               ldb_strerror(ret),
     765             :                                               ldb_errstring(ldb));
     766           0 :                                 return ldb_module_done(ac->req, NULL, NULL, ret);
     767             :                         }
     768             :                 }
     769             : 
     770             :                 /* for every element in the message check RP */
     771     5338250 :                 for (i = 0; i < msg->num_elements; ++i) {
     772     5052547 :                         struct ldb_message_element *el = &msg->elements[i];
     773             : 
     774             :                         /* Remove attributes added to perform access checks. */
     775     5052547 :                         if (should_remove_attr(el->name, ac)) {
     776     3809024 :                                 ldb_msg_element_mark_inaccessible(el);
     777     3809024 :                                 continue;
     778             :                         }
     779             : 
     780     1243523 :                         if (acl_element_is_access_checked(el)) {
     781             :                                 /* We will have already checked this attribute. */
     782       45294 :                                 continue;
     783             :                         }
     784             : 
     785             :                         /*
     786             :                          * We need to fetch the security descriptor to check
     787             :                          * this attribute.
     788             :                          */
     789     1198143 :                         break;
     790             :                 }
     791             : 
     792     1483932 :                 if (i == msg->num_elements) {
     793             :                         /* All elements have been checked. */
     794      285703 :                         goto reply_entry_done;
     795             :                 }
     796             : 
     797     1198229 :                 ret = setup_access_check_context(ac, msg, &acl_ctx);
     798     1198229 :                 if (ret != LDB_SUCCESS) {
     799           0 :                         return ret;
     800             :                 }
     801             : 
     802     1198229 :                 private_data = talloc_get_type_abort(ldb_module_get_private(ac->module),
     803             :                                                      struct aclread_private);
     804             : 
     805     6512431 :                 for (/* begin where we left off */; i < msg->num_elements; ++i) {
     806     5314116 :                         struct ldb_message_element *el = &msg->elements[i];
     807             : 
     808             :                         /* Remove attributes added to perform access checks. */
     809     5314116 :                         if (should_remove_attr(el->name, ac)) {
     810     1231834 :                                 ldb_msg_element_mark_inaccessible(el);
     811     1231834 :                                 continue;
     812             :                         }
     813             : 
     814     4082282 :                         if (acl_element_is_access_checked(el)) {
     815             :                                 /* We will have already checked this attribute. */
     816      116127 :                                 continue;
     817             :                         }
     818             : 
     819             :                         /*
     820             :                          * We need to check whether the attribute is secret,
     821             :                          * confidential, or access-controlled.
     822             :                          */
     823     3966376 :                         ret = acl_redact_attr(ac,
     824             :                                               el,
     825             :                                               ac,
     826             :                                               private_data,
     827             :                                               msg,
     828             :                                               ac->schema,
     829     3966155 :                                               acl_ctx.sd,
     830             :                                               acl_ctx.sid,
     831             :                                               acl_ctx.objectclass);
     832     3966155 :                         if (ret != LDB_SUCCESS) {
     833           0 :                                 return ldb_module_done(ac->req, NULL, NULL, ret);
     834             :                         }
     835             :                 }
     836             : 
     837     1198229 :         reply_entry_done:
     838     1483932 :                 ldb_msg_remove_inaccessible(msg);
     839             : 
     840     1483932 :                 ac->num_entries++;
     841     1483932 :                 return ldb_module_send_entry(ac->req, msg, ares->controls);
     842      136683 :         case LDB_REPLY_REFERRAL:
     843      136683 :                 return ldb_module_send_referral(ac->req, ares->referral);
     844      794724 :         case LDB_REPLY_DONE:
     845      794724 :                 if (ac->base_invisible && ac->num_entries == 0) {
     846             :                         /*
     847             :                          * If the base is invisible and we didn't
     848             :                          * returned any object, we need to return
     849             :                          * NO_SUCH_OBJECT.
     850             :                          */
     851        3258 :                         return ldb_module_done(ac->req,
     852             :                                                NULL, NULL,
     853             :                                                LDB_ERR_NO_SUCH_OBJECT);
     854             :                 }
     855      791466 :                 return ldb_module_done(ac->req, ares->controls,
     856             :                                         ares->response, LDB_SUCCESS);
     857             : 
     858             :         }
     859           0 :         return LDB_SUCCESS;
     860             : }
     861             : 
     862             : 
     863    34375840 : static int aclread_search(struct ldb_module *module, struct ldb_request *req)
     864             : {
     865     1950411 :         struct ldb_context *ldb;
     866     1950411 :         int ret;
     867     1950411 :         struct aclread_context *ac;
     868     1950411 :         struct ldb_request *down_req;
     869     1950411 :         bool am_system;
     870     1950411 :         struct ldb_result *res;
     871     1950411 :         struct aclread_private *p;
     872    34375840 :         bool need_sd = false;
     873    34375840 :         bool explicit_sd_flags = false;
     874    34375840 :         bool is_untrusted = ldb_req_is_untrusted(req);
     875     1950411 :         static const char * const _all_attrs[] = { "*", NULL };
     876    34375840 :         bool all_attrs = false;
     877    34375840 :         const char * const *attrs = NULL;
     878     1950411 :         static const char *acl_attrs[] = {
     879             :                 "instanceType",
     880             :                 NULL
     881             :         };
     882             : 
     883    34375840 :         ldb = ldb_module_get_ctx(module);
     884    34375840 :         p = talloc_get_type(ldb_module_get_private(module), struct aclread_private);
     885             : 
     886    34375840 :         am_system = dsdb_have_system_access(module,
     887             :                                             req,
     888             :                                             SYSTEM_CONTROL_KEEP_CRITICAL);
     889             : 
     890             :         /* skip access checks if we are system or system control is supplied
     891             :          * or this is not LDAP server request */
     892    34375840 :         if (!p || !p->enabled ||
     893     7498435 :             am_system ||
     894     7498435 :             !is_untrusted) {
     895    33516170 :                 return ldb_next_request(module, req);
     896             :         }
     897             :         /* no checks on special dn */
     898      859670 :         if (ldb_dn_is_special(req->op.search.base)) {
     899       61775 :                 return ldb_next_request(module, req);
     900             :         }
     901             : 
     902      797895 :         ac = talloc_zero(req, struct aclread_context);
     903      797895 :         if (ac == NULL) {
     904           0 :                 return ldb_oom(ldb);
     905             :         }
     906      797895 :         ac->module = module;
     907      797895 :         ac->req = req;
     908             : 
     909      797895 :         attrs = req->op.search.attrs;
     910      797895 :         if (attrs == NULL) {
     911       83920 :                 all_attrs = true;
     912       83920 :                 attrs = _all_attrs;
     913      713973 :         } else if (ldb_attr_in_list(attrs, "*")) {
     914       17504 :                 all_attrs = true;
     915             :         }
     916             : 
     917             :         /*
     918             :          * In theory we should also check for the SD control but control verification is
     919             :          * expensive so we'd better had the ntsecuritydescriptor to the list of
     920             :          * searched attribute and then remove it !
     921             :          */
     922      797895 :         ac->sd_flags = dsdb_request_sd_flags(ac->req, &explicit_sd_flags);
     923             : 
     924      797895 :         if (ldb_attr_in_list(attrs, "nTSecurityDescriptor")) {
     925       21990 :                 need_sd = false;
     926      775905 :         } else if (explicit_sd_flags && all_attrs) {
     927         306 :                 need_sd = false;
     928             :         } else {
     929      775599 :                 need_sd = true;
     930             :         }
     931             : 
     932      797895 :         if (!all_attrs) {
     933      696469 :                 if (!ldb_attr_in_list(attrs, "instanceType")) {
     934      685767 :                         attrs = ldb_attr_list_copy_add(ac, attrs, "instanceType");
     935      685767 :                         if (attrs == NULL) {
     936           0 :                                 return ldb_oom(ldb);
     937             :                         }
     938      685767 :                         ac->added_instanceType = true;
     939             :                 }
     940      696469 :                 if (!ldb_attr_in_list(req->op.search.attrs, "objectSid")) {
     941      675987 :                         attrs = ldb_attr_list_copy_add(ac, attrs, "objectSid");
     942      675987 :                         if (attrs == NULL) {
     943           0 :                                 return ldb_oom(ldb);
     944             :                         }
     945      675987 :                         ac->added_objectSid = true;
     946             :                 }
     947      696469 :                 if (!ldb_attr_in_list(req->op.search.attrs, "objectClass")) {
     948      684976 :                         attrs = ldb_attr_list_copy_add(ac, attrs, "objectClass");
     949      684976 :                         if (attrs == NULL) {
     950           0 :                                 return ldb_oom(ldb);
     951             :                         }
     952      684976 :                         ac->added_objectClass = true;
     953             :                 }
     954             :         }
     955             : 
     956      797895 :         if (need_sd) {
     957      775599 :                 attrs = ldb_attr_list_copy_add(ac, attrs, "nTSecurityDescriptor");
     958      775599 :                 if (attrs == NULL) {
     959           0 :                         return ldb_oom(ldb);
     960             :                 }
     961      775599 :                 ac->added_nTSecurityDescriptor = true;
     962             :         }
     963             : 
     964      797895 :         ac->am_administrator = dsdb_module_am_administrator(module);
     965             : 
     966             :         /* check accessibility of base */
     967      797895 :         if (!ldb_dn_is_null(req->op.search.base)) {
     968      797629 :                 ret = dsdb_module_search_dn(module, req, &res, req->op.search.base,
     969             :                                             acl_attrs,
     970             :                                             DSDB_FLAG_NEXT_MODULE |
     971             :                                             DSDB_FLAG_AS_SYSTEM |
     972             :                                             DSDB_SEARCH_SHOW_RECYCLED,
     973             :                                             req);
     974      797629 :                 if (ret != LDB_SUCCESS) {
     975         264 :                         return ldb_error(ldb, ret,
     976             :                                         "acl_read: Error retrieving instanceType for base.");
     977             :                 }
     978      797365 :                 ret = aclread_check_object_visible(ac, res->msgs[0], req);
     979      797365 :                 if (ret == LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS) {
     980        8649 :                         if (req->op.search.scope == LDB_SCOPE_BASE) {
     981        2889 :                                 return ldb_module_done(req, NULL, NULL,
     982             :                                                        LDB_ERR_NO_SUCH_OBJECT);
     983             :                         }
     984             :                         /*
     985             :                          * Defer LDB_ERR_NO_SUCH_OBJECT,
     986             :                          * we may return sub objects
     987             :                          */
     988        5760 :                         ac->base_invisible = true;
     989      788716 :                 } else if (ret != LDB_SUCCESS) {
     990           0 :                         return ldb_module_done(req, NULL, NULL, ret);
     991             :                 }
     992             :         }
     993             : 
     994      794742 :         ret = ldb_build_search_req_ex(&down_req,
     995             :                                       ldb, ac,
     996             :                                       req->op.search.base,
     997             :                                       req->op.search.scope,
     998             :                                       req->op.search.tree,
     999             :                                       attrs,
    1000             :                                       req->controls,
    1001             :                                       ac, aclread_callback,
    1002             :                                       req);
    1003             : 
    1004      794742 :         if (ret != LDB_SUCCESS) {
    1005           0 :                 return LDB_ERR_OPERATIONS_ERROR;
    1006             :         }
    1007             : 
    1008             :         /*
    1009             :          * We provide 'ac' as the control value, which is then used by the
    1010             :          * callback to avoid double-work.
    1011             :          */
    1012      794742 :         ret = ldb_request_add_control(down_req, DSDB_CONTROL_ACL_READ_OID, false, ac);
    1013      794742 :         if (ret != LDB_SUCCESS) {
    1014           0 :                         return ldb_error(ldb, ret,
    1015             :                                         "acl_read: Error adding acl_read control.");
    1016             :         }
    1017             : 
    1018      794742 :         return ldb_next_request(module, down_req);
    1019             : }
    1020             : 
    1021             : /*
    1022             :  * Here we mark inaccessible attributes known to be looked for in the
    1023             :  * filter. This only redacts attributes found in the search expression. If any
    1024             :  * extended attribute match rules examine different attributes without their own
    1025             :  * access control checks, a security bypass is possible.
    1026             :  */
    1027   120090038 : static int acl_redact_msg_for_filter(struct ldb_module *module, struct ldb_request *req, struct ldb_message *msg)
    1028             : {
    1029   120090038 :         struct ldb_context *ldb = ldb_module_get_ctx(module);
    1030   120090038 :         const struct aclread_private *private_data = NULL;
    1031   120090038 :         struct ldb_control *control = NULL;
    1032   120090038 :         struct aclread_context *ac = NULL;
    1033     2778756 :         struct access_check_context acl_ctx;
    1034     2778756 :         int ret;
    1035     2778756 :         unsigned i;
    1036             : 
    1037             :         /*
    1038             :          * The private data contains a list of attributes which are to be
    1039             :          * considered secret.
    1040             :          */
    1041   120090038 :         private_data = talloc_get_type(ldb_module_get_private(module), struct aclread_private);
    1042   120090038 :         if (private_data == NULL) {
    1043           0 :                 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR,
    1044             :                                  "aclread_private data is missing");
    1045             :         }
    1046   120090038 :         if (!private_data->enabled) {
    1047           0 :                 return LDB_SUCCESS;
    1048             :         }
    1049             : 
    1050   120090038 :         control = ldb_request_get_control(req, DSDB_CONTROL_ACL_READ_OID);
    1051   120090038 :         if (control == NULL) {
    1052             :                 /*
    1053             :                  * We've bypassed the acl_read module for this request, and
    1054             :                  * should skip redaction in this case.
    1055             :                  */
    1056   113207714 :                 return LDB_SUCCESS;
    1057             :         }
    1058             : 
    1059     4103908 :         ac = talloc_get_type_abort(control->data, struct aclread_context);
    1060             : 
    1061     4103908 :         if (!ac->got_tree_attrs) {
    1062      786184 :                 ret = ldb_parse_tree_collect_acl_attrs(module, ac, &ac->tree_attrs, req->op.search.tree);
    1063      786184 :                 if (ret != LDB_SUCCESS) {
    1064           0 :                         return ret;
    1065             :                 }
    1066      786184 :                 ac->got_tree_attrs = true;
    1067             :         }
    1068             : 
    1069    59529187 :         for (i = 0; i < msg->num_elements; ++i) {
    1070    57936552 :                 struct ldb_message_element *el = &msg->elements[i];
    1071             : 
    1072             :                 /* Is the attribute mentioned in the search expression? */
    1073    57936552 :                 if (attr_in_vec(&ac->tree_attrs, el->name)) {
    1074             :                         /*
    1075             :                          * We need to fetch the security descriptor to check
    1076             :                          * this element.
    1077             :                          */
    1078     2511187 :                         break;
    1079             :                 }
    1080             : 
    1081             :                 /*
    1082             :                  * This attribute is not in the search filter, so we can leave
    1083             :                  * handling it till aclread_callback(), by which time we know
    1084             :                  * this object is a match. This saves work checking ACLs if the
    1085             :                  * search is unindexed and most objects don't match the filter.
    1086             :                  */
    1087             :         }
    1088             : 
    1089     4103908 :         if (i == msg->num_elements) {
    1090             :                 /* All elements have been checked. */
    1091     1592381 :                 return LDB_SUCCESS;
    1092             :         }
    1093             : 
    1094     2511273 :         ret = setup_access_check_context(ac, msg, &acl_ctx);
    1095     2511273 :         if (ret != LDB_SUCCESS) {
    1096           0 :                 return ret;
    1097             :         }
    1098             : 
    1099             :         /* For every element in the message and the parse tree, check RP. */
    1100             : 
    1101    26179656 :         for (/* begin where we left off */; i < msg->num_elements; ++i) {
    1102    23668383 :                 struct ldb_message_element *el = &msg->elements[i];
    1103             : 
    1104             :                 /* Is the attribute mentioned in the search expression? */
    1105    23668383 :                 if (!attr_in_vec(&ac->tree_attrs, el->name)) {
    1106             :                         /*
    1107             :                          * If not, leave it for later and check the next
    1108             :                          * attribute.
    1109             :                          */
    1110    20536564 :                         continue;
    1111             :                 }
    1112             : 
    1113             :                 /*
    1114             :                  * We need to check whether the attribute is secret,
    1115             :                  * confidential, or access-controlled.
    1116             :                  */
    1117     3131905 :                 ret = acl_redact_attr(ac,
    1118             :                                       el,
    1119             :                                       ac,
    1120             :                                       private_data,
    1121             :                                       msg,
    1122             :                                       ac->schema,
    1123     3131819 :                                       acl_ctx.sd,
    1124             :                                       acl_ctx.sid,
    1125             :                                       acl_ctx.objectclass);
    1126     3131819 :                 if (ret != LDB_SUCCESS) {
    1127           0 :                         return ret;
    1128             :                 }
    1129             : 
    1130     3132843 :                 acl_element_mark_access_checked(el);
    1131             :         }
    1132             : 
    1133     2511187 :         return LDB_SUCCESS;
    1134             : }
    1135             : 
    1136    12012264 : static int ldb_attr_cmp_fn(const void *_a, const void *_b)
    1137             : {
    1138    12012264 :         const char * const *a = _a;
    1139    12012264 :         const char * const *b = _b;
    1140             : 
    1141    12012264 :         return ldb_attr_cmp(*a, *b);
    1142             : }
    1143             : 
    1144      182004 : static int aclread_init(struct ldb_module *module)
    1145             : {
    1146      182004 :         struct ldb_context *ldb = ldb_module_get_ctx(module);
    1147        6016 :         unsigned int i, n, j;
    1148      182004 :         TALLOC_CTX *mem_ctx = NULL;
    1149        6016 :         int ret;
    1150        6016 :         bool userPassword_support;
    1151        6016 :         static const char * const attrs[] = { "passwordAttribute", NULL };
    1152        6016 :         static const char * const secret_attrs[] = {
    1153             :                 DSDB_SECRET_ATTRIBUTES
    1154             :         };
    1155        6016 :         struct ldb_result *res;
    1156        6016 :         struct ldb_message *msg;
    1157        6016 :         struct ldb_message_element *password_attributes;
    1158      182004 :         struct aclread_private *p = talloc_zero(module, struct aclread_private);
    1159      182004 :         if (p == NULL) {
    1160           0 :                 return ldb_module_oom(module);
    1161             :         }
    1162      182004 :         p->enabled = lpcfg_parm_bool(ldb_get_opaque(ldb, "loadparm"), NULL, "acl", "search", true);
    1163             : 
    1164      182004 :         ret = ldb_mod_register_control(module, LDB_CONTROL_SD_FLAGS_OID);
    1165      182004 :         if (ret != LDB_SUCCESS) {
    1166           0 :                 ldb_debug(ldb, LDB_DEBUG_ERROR,
    1167             :                           "acl_module_init: Unable to register sd_flags control with rootdse!\n");
    1168           0 :                 return ldb_operr(ldb);
    1169             :         }
    1170             : 
    1171      182004 :         ldb_module_set_private(module, p);
    1172             : 
    1173      182004 :         mem_ctx = talloc_new(module);
    1174      182004 :         if (!mem_ctx) {
    1175           0 :                 return ldb_oom(ldb);
    1176             :         }
    1177             : 
    1178      182004 :         ret = dsdb_module_search_dn(module, mem_ctx, &res,
    1179             :                                     ldb_dn_new(mem_ctx, ldb, "@KLUDGEACL"),
    1180             :                                     attrs,
    1181             :                                     DSDB_FLAG_NEXT_MODULE |
    1182             :                                     DSDB_FLAG_AS_SYSTEM,
    1183             :                                     NULL);
    1184      182004 :         if (ret != LDB_SUCCESS) {
    1185           0 :                 goto done;
    1186             :         }
    1187      182004 :         if (res->count == 0) {
    1188           0 :                 goto done;
    1189             :         }
    1190             : 
    1191      182004 :         if (res->count > 1) {
    1192           0 :                 talloc_free(mem_ctx);
    1193           0 :                 return LDB_ERR_CONSTRAINT_VIOLATION;
    1194             :         }
    1195             : 
    1196      182004 :         msg = res->msgs[0];
    1197             : 
    1198      182004 :         password_attributes = ldb_msg_find_element(msg, "passwordAttribute");
    1199      182004 :         if (!password_attributes) {
    1200           0 :                 goto done;
    1201             :         }
    1202      182004 :         p->password_attrs = talloc_array(p, const char *,
    1203             :                         password_attributes->num_values +
    1204             :                         ARRAY_SIZE(secret_attrs));
    1205      182004 :         if (!p->password_attrs) {
    1206           0 :                 talloc_free(mem_ctx);
    1207           0 :                 return ldb_oom(ldb);
    1208             :         }
    1209             : 
    1210      175988 :         n = 0;
    1211     3822018 :         for (i=0; i < password_attributes->num_values; i++) {
    1212     3640014 :                 p->password_attrs[n] = (const char *)password_attributes->values[i].data;
    1213     3640014 :                 talloc_steal(p->password_attrs, password_attributes->values[i].data);
    1214     3640014 :                 n++;
    1215             :         }
    1216             : 
    1217     2730060 :         for (i=0; i < ARRAY_SIZE(secret_attrs); i++) {
    1218     3564760 :                 bool found = false;
    1219             : 
    1220    33306732 :                 for (j=0; j < n; j++) {
    1221    33306666 :                         if (strcasecmp(p->password_attrs[j], secret_attrs[i]) == 0) {
    1222     2463782 :                                 found = true;
    1223     2463782 :                                 break;
    1224             :                         }
    1225             :                 }
    1226             : 
    1227     2548056 :                 if (found) {
    1228     2547990 :                         continue;
    1229             :                 }
    1230             : 
    1231         132 :                 p->password_attrs[n] = talloc_strdup(p->password_attrs,
    1232          66 :                                                      secret_attrs[i]);
    1233          66 :                 if (p->password_attrs[n] == NULL) {
    1234           0 :                         talloc_free(mem_ctx);
    1235           0 :                         return ldb_oom(ldb);
    1236             :                 }
    1237          66 :                 n++;
    1238             :         }
    1239      182004 :         p->num_password_attrs = n;
    1240             : 
    1241             :         /* Sort the password attributes so we can use binary search. */
    1242      182004 :         TYPESAFE_QSORT(p->password_attrs, p->num_password_attrs, ldb_attr_cmp_fn);
    1243             : 
    1244      182004 :         ret = ldb_register_redact_callback(ldb, acl_redact_msg_for_filter, module);
    1245      182004 :         if (ret != LDB_SUCCESS) {
    1246           0 :                 return ret;
    1247             :         }
    1248             : 
    1249      182004 : done:
    1250      182004 :         talloc_free(mem_ctx);
    1251      182004 :         ret = ldb_next_init(module);
    1252             : 
    1253      182004 :         if (ret != LDB_SUCCESS) {
    1254           0 :                 return ret;
    1255             :         }
    1256             : 
    1257      182004 :         if (p->password_attrs != NULL) {
    1258             :                 /*
    1259             :                  * Check this after the modules have be initialised so we can
    1260             :                  * actually read the backend DB.
    1261             :                  */
    1262      182004 :                 userPassword_support = dsdb_user_password_support(module,
    1263             :                                                                   module,
    1264             :                                                                   NULL);
    1265      182004 :                 if (!userPassword_support) {
    1266      175460 :                         const char **found = NULL;
    1267             : 
    1268             :                         /*
    1269             :                          * Remove the userPassword attribute, as it is not
    1270             :                          * considered secret.
    1271             :                          */
    1272      877300 :                         BINARY_ARRAY_SEARCH_V(p->password_attrs,
    1273             :                                               p->num_password_attrs,
    1274             :                                               "userPassword",
    1275             :                                               ldb_attr_cmp,
    1276             :                                               found);
    1277      175460 :                         if (found != NULL) {
    1278      175460 :                                 size_t found_idx = found - p->password_attrs;
    1279             : 
    1280             :                                 /* Shift following elements backwards by one. */
    1281      175460 :                                 for (i = found_idx; i < p->num_password_attrs - 1; ++i) {
    1282           0 :                                         p->password_attrs[i] = p->password_attrs[i + 1];
    1283             :                                 }
    1284      175460 :                                 --p->num_password_attrs;
    1285             :                         }
    1286             :                 }
    1287             :         }
    1288      175988 :         return ret;
    1289             : }
    1290             : 
    1291             : static const struct ldb_module_ops ldb_aclread_module_ops = {
    1292             :         .name              = "aclread",
    1293             :         .search            = aclread_search,
    1294             :         .init_context      = aclread_init
    1295             : };
    1296             : 
    1297        6040 : int ldb_aclread_module_init(const char *version)
    1298             : {
    1299        6040 :         LDB_MODULE_CHECK_VERSION(version);
    1300        6040 :         return ldb_register_module(&ldb_aclread_module_ops);
    1301             : }

Generated by: LCOV version 1.14