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

          Line data    Source code
       1             : /* 
       2             :    ldb database library
       3             : 
       4             :    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007
       5             :    Copyright (C) Simo Sorce <idra@samba.org> 2008
       6             :    Copyright (C) Andrew Tridgell  2004
       7             :     
       8             :    This program is free software; you can redistribute it and/or modify
       9             :    it under the terms of the GNU General Public License as published by
      10             :    the Free Software Foundation; either version 3 of the License, or
      11             :    (at your option) any later version.
      12             :    
      13             :    This program is distributed in the hope that it will be useful,
      14             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :    GNU General Public License for more details.
      17             :    
      18             :    You should have received a copy of the GNU General Public License
      19             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      20             : */
      21             : 
      22             : /*
      23             :  *  Name: ldb
      24             :  *
      25             :  *  Component: ldb anr module
      26             :  *
      27             :  *  Description: module to implement 'ambiguous name resolution'
      28             :  *
      29             :  *  Author: Andrew Bartlett
      30             :  */
      31             : 
      32             : #include "includes.h"
      33             : #include "ldb_module.h"
      34             : #include "dsdb/samdb/samdb.h"
      35             : #include "dsdb/samdb/ldb_modules/util.h"
      36             : 
      37             : #undef strcasecmp
      38             : 
      39             : /**
      40             :  * Make a and 'and' or 'or' tree from the two supplied elements 
      41             :  */
      42         124 : static struct ldb_parse_tree *make_parse_list(struct ldb_module *module,
      43             :                                        TALLOC_CTX *mem_ctx, enum ldb_parse_op op, 
      44             :                                        struct ldb_parse_tree *first_arm, struct ldb_parse_tree *second_arm)
      45             : {
      46             :         struct ldb_context *ldb;
      47             :         struct ldb_parse_tree *list;
      48             : 
      49         124 :         ldb = ldb_module_get_ctx(module);
      50             : 
      51         124 :         list = talloc(mem_ctx, struct ldb_parse_tree);
      52         124 :         if (list == NULL){
      53           0 :                 ldb_oom(ldb);
      54           0 :                 return NULL;
      55             :         }
      56         124 :         list->operation = op;
      57             :         
      58         124 :         list->u.list.num_elements = 2;
      59         124 :         list->u.list.elements = talloc_array(list, struct ldb_parse_tree *, 2);
      60         124 :         if (!list->u.list.elements) {
      61           0 :                 ldb_oom(ldb);
      62           0 :                 return NULL;
      63             :         }
      64         124 :         list->u.list.elements[0] = talloc_steal(list, first_arm);
      65         124 :         list->u.list.elements[1] = talloc_steal(list, second_arm);
      66         124 :         return list;
      67             : }
      68             : 
      69             : /**
      70             :  * Make an equality or prefix match tree, from the attribute, operation and matching value supplied
      71             :  */
      72         132 : static struct ldb_parse_tree *make_match_tree(struct ldb_module *module,
      73             :                                               TALLOC_CTX *mem_ctx,
      74             :                                               enum ldb_parse_op op,
      75             :                                               const char *attr,
      76             :                                               struct ldb_val *match)
      77             : {
      78             :         struct ldb_context *ldb;
      79             :         struct ldb_parse_tree *match_tree;
      80             : 
      81         132 :         ldb = ldb_module_get_ctx(module);
      82             : 
      83         132 :         match_tree = talloc(mem_ctx, struct ldb_parse_tree);
      84             :         
      85             :         /* Depending on what type of match was selected, fill in the right part of the union */
      86             :          
      87         132 :         match_tree->operation = op;
      88         132 :         switch (op) {
      89         118 :         case LDB_OP_SUBSTRING:
      90         118 :                 match_tree->u.substring.attr = attr;
      91             :                 
      92         118 :                 match_tree->u.substring.start_with_wildcard = 0;
      93         118 :                 match_tree->u.substring.end_with_wildcard = 1;
      94         118 :                 match_tree->u.substring.chunks = talloc_array(match_tree, struct ldb_val *, 2);
      95             :                 
      96         118 :                 if (match_tree->u.substring.chunks == NULL){
      97           0 :                         talloc_free(match_tree);
      98           0 :                         ldb_oom(ldb);
      99           0 :                         return NULL;
     100             :                 }
     101         118 :                 match_tree->u.substring.chunks[0] = match;
     102         118 :                 match_tree->u.substring.chunks[1] = NULL;
     103         118 :                 break;
     104          14 :         case LDB_OP_EQUALITY:
     105          14 :                 match_tree->u.equality.attr = attr;
     106          14 :                 match_tree->u.equality.value = *match;
     107          14 :                 break;
     108           0 :         default:
     109           0 :                 talloc_free(match_tree);
     110           0 :                 return NULL;
     111             :         }
     112         132 :         return match_tree;
     113             : }
     114             : 
     115             : struct anr_context {
     116             :         bool found_anr;
     117             :         struct ldb_module *module;
     118             :         struct ldb_request *req;
     119             : };
     120             : 
     121             : /**
     122             :  * Given the match for an 'ambigious name resolution' query, create a
     123             :  * parse tree with an 'or' of all the anr attributes in the schema.  
     124             :  */
     125             : 
     126             : /**
     127             :  * Callback function to do the heavy lifting for the parse tree walker
     128             :  */
     129           8 : static int anr_replace_value(struct anr_context *ac,
     130             :                              TALLOC_CTX *mem_ctx,
     131             :                              struct ldb_val *match,
     132             :                              struct ldb_parse_tree **ntree)
     133             : {
     134           8 :         struct ldb_parse_tree *tree = NULL;
     135           8 :         struct ldb_module *module = ac->module;
     136             :         struct ldb_parse_tree *match_tree;
     137             :         struct dsdb_attribute *cur;
     138             :         const struct dsdb_schema *schema;
     139             :         struct ldb_context *ldb;
     140             :         uint8_t *p;
     141             :         enum ldb_parse_op op;
     142             : 
     143           8 :         ldb = ldb_module_get_ctx(module);
     144             : 
     145           8 :         schema = dsdb_get_schema(ldb, ac);
     146           8 :         if (!schema) {
     147           0 :                 ldb_asprintf_errstring(ldb, "no schema with which to construct anr filter");
     148           0 :                 return LDB_ERR_OPERATIONS_ERROR;
     149             :         }
     150             : 
     151           8 :         ac->found_anr = true;
     152             : 
     153           9 :         if (match->length > 1 && match->data[0] == '=') {
     154           1 :                 struct ldb_val *match2 = talloc(mem_ctx, struct ldb_val);
     155           1 :                 if (match2 == NULL){
     156           0 :                         return ldb_oom(ldb);
     157             :                 }
     158           1 :                 *match2 = data_blob_const(match->data+1, match->length - 1);
     159           1 :                 match = match2;
     160           1 :                 op = LDB_OP_EQUALITY;
     161             :         } else {
     162           7 :                 op = LDB_OP_SUBSTRING;
     163             :         }
     164       11792 :         for (cur = schema->attributes; cur; cur = cur->next) {
     165       11784 :                 if (!(cur->searchFlags & SEARCH_FLAG_ANR)) continue;
     166         112 :                 match_tree = make_match_tree(module, mem_ctx, op, cur->lDAPDisplayName, match);
     167             : 
     168         112 :                 if (tree) {
     169             :                         /* Inject an 'or' with the current tree */
     170         104 :                         tree = make_parse_list(module, mem_ctx,  LDB_OP_OR, tree, match_tree);
     171         104 :                         if (tree == NULL) {
     172           0 :                                 return ldb_oom(ldb);
     173             :                         }
     174             :                 } else {
     175           8 :                         tree = match_tree;
     176             :                 }
     177             :         }
     178             : 
     179             :         
     180             :         /* If the search term has a space in it, 
     181             :            split it up at the first space.  */
     182             :         
     183           8 :         p = memchr(match->data, ' ', match->length);
     184             : 
     185           8 :         if (p) {
     186             :                 struct ldb_parse_tree *first_split_filter, *second_split_filter, *split_filters, *match_tree_1, *match_tree_2;
     187           5 :                 struct ldb_val *first_match = talloc(tree, struct ldb_val);
     188           5 :                 struct ldb_val *second_match = talloc(tree, struct ldb_val);
     189           5 :                 if (!first_match || !second_match) {
     190           0 :                         return ldb_oom(ldb);
     191             :                 }
     192           5 :                 *first_match = data_blob_const(match->data, p-match->data);
     193           5 :                 *second_match = data_blob_const(p+1, match->length - (p-match->data) - 1);
     194             :                 
     195             :                 /* Add (|(&(givenname=first)(sn=second))(&(givenname=second)(sn=first))) */
     196             : 
     197           5 :                 match_tree_1 = make_match_tree(module, mem_ctx, op, "givenName", first_match);
     198           5 :                 match_tree_2 = make_match_tree(module, mem_ctx, op, "sn", second_match);
     199             : 
     200           5 :                 first_split_filter = make_parse_list(module, ac,  LDB_OP_AND, match_tree_1, match_tree_2);
     201           5 :                 if (first_split_filter == NULL){
     202           0 :                         return ldb_oom(ldb);
     203             :                 }
     204             :                 
     205           5 :                 match_tree_1 = make_match_tree(module, mem_ctx, op, "sn", first_match);
     206           5 :                 match_tree_2 = make_match_tree(module, mem_ctx, op, "givenName", second_match);
     207             : 
     208           5 :                 second_split_filter = make_parse_list(module, ac,  LDB_OP_AND, match_tree_1, match_tree_2);
     209           5 :                 if (second_split_filter == NULL){
     210           0 :                         return ldb_oom(ldb);
     211             :                 }
     212             : 
     213           5 :                 split_filters = make_parse_list(module, mem_ctx,  LDB_OP_OR, 
     214             :                                                 first_split_filter, second_split_filter);
     215           5 :                 if (split_filters == NULL) {
     216           0 :                         return ldb_oom(ldb);
     217             :                 }
     218             : 
     219           5 :                 if (tree) {
     220             :                         /* Inject an 'or' with the current tree */
     221           5 :                         tree = make_parse_list(module, mem_ctx,  LDB_OP_OR, tree, split_filters);
     222             :                 } else {
     223           0 :                         tree = split_filters;
     224             :                 }
     225             :         }
     226           8 :         *ntree = tree;
     227           8 :         return LDB_SUCCESS;
     228             : }
     229             : 
     230             : /*
     231             :   replace any occurances of an attribute with a new, generated attribute tree
     232             : */
     233    43898530 : static int anr_replace_subtrees(struct anr_context *ac,
     234             :                                 struct ldb_parse_tree *tree,
     235             :                                 const char *attr,
     236             :                                 struct ldb_parse_tree **ntree)
     237             : {
     238             :         int ret;
     239             :         unsigned int i;
     240             : 
     241    43898530 :         switch (tree->operation) {
     242    13144624 :         case LDB_OP_AND:
     243             :         case LDB_OP_OR:
     244    41997860 :                 for (i=0;i<tree->u.list.num_elements;i++) {
     245    28853236 :                         ret = anr_replace_subtrees(ac, tree->u.list.elements[i],
     246    28853236 :                                                    attr, &tree->u.list.elements[i]);
     247    28853236 :                         if (ret != LDB_SUCCESS) {
     248           0 :                                 return ret;
     249             :                         }
     250    28853236 :                         *ntree = tree;
     251             :                 }
     252    13144624 :                 break;
     253      704755 :         case LDB_OP_NOT:
     254      704755 :                 ret = anr_replace_subtrees(ac, tree->u.isnot.child, attr, &tree->u.isnot.child);
     255      704755 :                 if (ret != LDB_SUCCESS) {
     256           0 :                         return ret;
     257             :                 }
     258      704755 :                 *ntree = tree;
     259      704755 :                 break;
     260     3466080 :         case LDB_OP_EQUALITY:
     261     3466080 :                 if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) {
     262           8 :                         ret = anr_replace_value(ac, tree, &tree->u.equality.value, ntree);
     263           8 :                         if (ret != LDB_SUCCESS) {
     264           0 :                                 return ret;
     265             :                         }
     266             :                 }
     267     3381874 :                 break;
     268        6559 :         case LDB_OP_SUBSTRING:
     269        6559 :                 if (ldb_attr_cmp(tree->u.substring.attr, attr) == 0) {
     270           0 :                         if (tree->u.substring.start_with_wildcard == 0 &&
     271           0 :                             tree->u.substring.end_with_wildcard == 1 && 
     272           0 :                             tree->u.substring.chunks[0] != NULL && 
     273           0 :                             tree->u.substring.chunks[1] == NULL) {
     274           0 :                                 ret = anr_replace_value(ac, tree, tree->u.substring.chunks[0], ntree);
     275           0 :                                 if (ret != LDB_SUCCESS) {
     276           0 :                                         return ret;
     277             :                                 }
     278             :                         }
     279             :                 }
     280        6559 :                 break;
     281    24201551 :         default:
     282    24201551 :                 break;
     283             :         }
     284             : 
     285    41421599 :         return LDB_SUCCESS;
     286             : }
     287             : 
     288          47 : static int anr_search_callback(struct ldb_request *req, struct ldb_reply *ares)
     289             : {
     290             :         struct anr_context *ac;
     291             : 
     292          47 :         ac = talloc_get_type(req->context, struct anr_context);
     293             : 
     294          47 :         if (!ares) {
     295           0 :                 return ldb_module_done(ac->req, NULL, NULL,
     296             :                                         LDB_ERR_OPERATIONS_ERROR);
     297             :         }
     298          47 :         if (ares->error != LDB_SUCCESS) {
     299           0 :                 return ldb_module_done(ac->req, ares->controls,
     300             :                                         ares->response, ares->error);
     301             :         }
     302             : 
     303          47 :         switch (ares->type) {
     304          15 :         case LDB_REPLY_ENTRY:
     305          15 :                 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
     306             : 
     307          24 :         case LDB_REPLY_REFERRAL:
     308          24 :                 return ldb_module_send_referral(ac->req, ares->referral);
     309             : 
     310           8 :         case LDB_REPLY_DONE:
     311           8 :                 return ldb_module_done(ac->req, ares->controls,
     312             :                                         ares->response, LDB_SUCCESS);
     313             : 
     314             :         }
     315           0 :         return LDB_SUCCESS;
     316             : }
     317             : 
     318             : /* search */
     319    14340539 : static int anr_search(struct ldb_module *module, struct ldb_request *req)
     320             : {
     321             :         struct ldb_context *ldb;
     322             :         struct ldb_parse_tree *anr_tree;
     323             :         struct ldb_request *down_req;
     324             :         struct anr_context *ac;
     325             :         int ret;
     326             : 
     327    14340539 :         ldb = ldb_module_get_ctx(module);
     328             : 
     329    14340539 :         ac = talloc(req, struct anr_context);
     330    14340539 :         if (!ac) {
     331           0 :                 return ldb_oom(ldb);
     332             :         }
     333             : 
     334    14340539 :         ac->module = module;
     335    14340539 :         ac->req = req;
     336    14340539 :         ac->found_anr = false;
     337             : 
     338             : #if 0
     339             :         printf("oldanr : %s\n", ldb_filter_from_tree (0, req->op.search.tree));
     340             : #endif
     341             : 
     342    14340539 :         ret = anr_replace_subtrees(ac, req->op.search.tree, "anr", &anr_tree);
     343    14340539 :         if (ret != LDB_SUCCESS) {
     344           0 :                 return ldb_operr(ldb);
     345             :         }
     346             : 
     347    14340539 :         if (!ac->found_anr) {
     348    14340531 :                 talloc_free(ac);
     349    14340531 :                 return ldb_next_request(module, req);
     350             :         }
     351             : 
     352           8 :         ret = ldb_build_search_req_ex(&down_req,
     353             :                                         ldb, ac,
     354             :                                         req->op.search.base,
     355             :                                         req->op.search.scope,
     356             :                                         anr_tree,
     357             :                                         req->op.search.attrs,
     358             :                                         req->controls,
     359             :                                         ac, anr_search_callback,
     360             :                                         req);
     361           8 :         LDB_REQ_SET_LOCATION(down_req);
     362           8 :         if (ret != LDB_SUCCESS) {
     363           0 :                 return ldb_operr(ldb);
     364             :         }
     365           8 :         talloc_steal(down_req, anr_tree);
     366             : 
     367           8 :         return ldb_next_request(module, down_req);
     368             : }
     369             : 
     370             : static const struct ldb_module_ops ldb_anr_module_ops = {
     371             :         .name              = "anr",
     372             :         .search = anr_search
     373             : };
     374             : 
     375        5536 : int ldb_anr_module_init(const char *version)
     376             : {
     377        5536 :         LDB_MODULE_CHECK_VERSION(version);
     378        5536 :         return ldb_register_module(&ldb_anr_module_ops);
     379             : }

Generated by: LCOV version 1.13