Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into 4-0-local
[sfrench/samba-autobuild/.git] / source4 / dsdb / samdb / ldb_modules / anr.c
1 /* 
2    ldb database library
3
4    Copyright (C) Amdrew Bartlett <abartlet@samba.org> 2007
5    Copyright (C) Andrew Tridgell  2004
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 anr module
25  *
26  *  Description: module to implement 'ambiguous name resolution'
27  *
28  *  Author: Andrew Bartlett
29  */
30
31 #include "includes.h"
32 #include "ldb_includes.h"
33 #include "dsdb/samdb/samdb.h"
34
35 /**
36  * Make a and 'and' or 'or' tree from the two supplied elements 
37  */
38 struct ldb_parse_tree *make_parse_list(struct ldb_module *module,
39                                        TALLOC_CTX *mem_ctx, enum ldb_parse_op op, 
40                                        struct ldb_parse_tree *first_arm, struct ldb_parse_tree *second_arm)
41 {
42         struct ldb_parse_tree *list;
43
44         list = talloc(mem_ctx, struct ldb_parse_tree);
45         if (list == NULL){
46                 ldb_oom(module->ldb);
47                 return NULL;
48         }
49         list->operation = op;
50         
51         list->u.list.num_elements = 2;
52         list->u.list.elements = talloc_array(list, struct ldb_parse_tree *, 2);
53         if (!list->u.list.elements) {
54                 ldb_oom(module->ldb);
55                 return NULL;
56         }
57         list->u.list.elements[0] = talloc_steal(list, first_arm);
58         list->u.list.elements[1] = talloc_steal(list, second_arm);
59         return list;
60 }
61
62 /**
63  * Make an equality or prefix match tree, from the attribute, operation and matching value supplied
64  */
65 struct ldb_parse_tree *make_match_tree(struct ldb_module *module,
66                                        TALLOC_CTX *mem_ctx, enum ldb_parse_op op, 
67                                        const char *attr, const DATA_BLOB *match)
68 {
69         struct ldb_parse_tree *match_tree;
70
71         match_tree = talloc(mem_ctx, struct ldb_parse_tree);
72         
73         /* Depending on what type of match was selected, fill in the right part of the union */
74          
75         match_tree->operation = op;
76         switch (op) {
77         case LDB_OP_SUBSTRING:
78                 match_tree->u.substring.attr = attr;
79                 
80                 match_tree->u.substring.start_with_wildcard = 0;
81                 match_tree->u.substring.end_with_wildcard = 1;
82                 match_tree->u.substring.chunks = talloc_array(match_tree, struct ldb_val *, 2);
83                 
84                 if (match_tree->u.substring.chunks == NULL){
85                         ldb_oom(module->ldb);
86                         return NULL;
87                 }
88                 match_tree->u.substring.chunks[0] = match;
89                 match_tree->u.substring.chunks[1] = NULL;
90                 break;
91         case LDB_OP_EQUALITY:
92                 match_tree->u.equality.attr = attr;
93                 match_tree->u.equality.value = *match;
94                 break;
95         }
96         return match_tree;
97 }
98
99 struct anr_context {
100         bool found_anr;
101         struct ldb_module *module;
102 };
103
104 /**
105  * Given the match for an 'ambigious name resolution' query, create a
106  * parse tree with an 'or' of all the anr attributes in the schema.  
107  */
108
109 typedef struct ldb_parse_tree *(*anr_parse_tree_callback_t)(TALLOC_CTX *mem_ctx,
110                                                            const struct ldb_val *match,
111                                                            void *context);
112
113
114 /**
115  * Callback function to do the heavy lifting for the for the parse tree walker 
116  */
117 struct ldb_parse_tree *anr_replace_callback(TALLOC_CTX *mem_ctx,
118                                             const struct ldb_val *match,
119                                             void *context)
120 {
121         struct ldb_parse_tree *tree = NULL;
122         struct anr_context *anr_context = talloc_get_type(context, struct anr_context);
123         struct ldb_module *module = anr_context->module;
124         struct ldb_parse_tree *match_tree;
125         uint8_t *p;
126         enum ldb_parse_op op;
127         struct dsdb_attribute *cur;
128         const struct dsdb_schema *schema = dsdb_get_schema(module->ldb);
129         if (!schema) {
130                 ldb_asprintf_errstring(module->ldb, "no schema with which to construct anr filter");
131                 return NULL;
132         }
133
134         anr_context->found_anr = true;
135
136         if (match->length > 1 && match->data[0] == '=') {
137                 DATA_BLOB *match2 = talloc(tree, DATA_BLOB);
138                 *match2 = data_blob_const(match->data+1, match->length - 1);
139                 if (match2 == NULL){
140                         ldb_oom(module->ldb);
141                         return NULL;
142                 }
143                 match = match2;
144                 op = LDB_OP_EQUALITY;
145         } else {
146                 op = LDB_OP_SUBSTRING;
147         }
148         for (cur = schema->attributes; cur; cur = cur->next) {
149                 if (!(cur->searchFlags & SEARCH_FLAG_ANR)) continue;
150                 match_tree = make_match_tree(module, mem_ctx, op, cur->lDAPDisplayName, match);
151
152                 if (tree) {
153                         /* Inject an 'or' with the current tree */
154                         tree = make_parse_list(module, mem_ctx,  LDB_OP_OR, tree, match_tree);
155                         if (tree == NULL) {
156                                 ldb_oom(module->ldb);
157                                 return NULL;
158                         }
159                 } else {
160                         tree = match_tree;
161                 }
162         }
163
164         
165         /* If the search term has a space in it, 
166            split it up at the first space.  */
167         
168         p = memchr(match->data, ' ', match->length);
169
170         if (p) {
171                 struct ldb_parse_tree *first_split_filter, *second_split_filter, *split_filters, *match_tree_1, *match_tree_2;
172                 DATA_BLOB *first_match = talloc(tree, DATA_BLOB);
173                 DATA_BLOB *second_match = talloc(tree, DATA_BLOB);
174                 if (!first_match || !second_match) {
175                         ldb_oom(module->ldb);
176                         return NULL;
177                 }
178                 *first_match = data_blob_const(match->data, p-match->data);
179                 *second_match = data_blob_const(p+1, match->length - (p-match->data) - 1);
180                 
181                 /* Add (|(&(givenname=first)(sn=second))(&(givenname=second)(sn=first))) */
182
183                 match_tree_1 = make_match_tree(module, mem_ctx, op, "givenName", first_match);
184                 match_tree_2 = make_match_tree(module, mem_ctx, op, "sn", second_match);
185
186                 first_split_filter = make_parse_list(module, context,  LDB_OP_AND, match_tree_1, match_tree_2);
187                 if (first_split_filter == NULL){
188                         ldb_oom(module->ldb);
189                         return NULL;
190                 }
191                 
192                 match_tree_1 = make_match_tree(module, mem_ctx, op, "sn", first_match);
193                 match_tree_2 = make_match_tree(module, mem_ctx, op, "givenName", second_match);
194
195                 second_split_filter = make_parse_list(module, context,  LDB_OP_AND, match_tree_1, match_tree_2);
196                 if (second_split_filter == NULL){
197                         ldb_oom(module->ldb);
198                         return NULL;
199                 }
200
201                 split_filters = make_parse_list(module, mem_ctx,  LDB_OP_OR, 
202                                                 first_split_filter, second_split_filter);
203                 if (split_filters == NULL) {
204                         ldb_oom(module->ldb);
205                         return NULL;
206                 }
207
208                 if (tree) {
209                         /* Inject an 'or' with the current tree */
210                         tree = make_parse_list(module, mem_ctx,  LDB_OP_OR, tree, split_filters);
211                 } else {
212                         tree = split_filters;
213                 }
214         }
215         return tree;
216 }
217
218 /*
219   replace any occurances of an attribute with a new, generated attribute tree
220 */
221 struct ldb_parse_tree *anr_replace_subtrees(struct ldb_parse_tree *tree, 
222                                             const char *attr, 
223                                             anr_parse_tree_callback_t callback,
224                                             void *context)
225 {
226         int i;
227         struct ldb_parse_tree *tmp;
228
229         switch (tree->operation) {
230         case LDB_OP_AND:
231         case LDB_OP_OR:
232                 for (i=0;i<tree->u.list.num_elements;i++) {
233                         tmp = anr_replace_subtrees(tree->u.list.elements[i],
234                                                    attr, callback, context);
235                         if (tmp) tree->u.list.elements[i] = tmp;
236                 }
237                 break;
238         case LDB_OP_NOT:
239                 tmp = anr_replace_subtrees(tree->u.isnot.child, attr, callback, context);
240                 if (tmp) tree->u.isnot.child = tmp;
241                 break;
242         case LDB_OP_EQUALITY:
243                 if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) {
244                         tmp = callback(tree, &tree->u.equality.value, 
245                                         context);
246                         if (tmp) tree = tmp;
247                 }
248                 break;
249         case LDB_OP_SUBSTRING:
250                 if (ldb_attr_cmp(tree->u.substring.attr, attr) == 0) {
251                         if (tree->u.substring.start_with_wildcard == 0 &&
252                             tree->u.substring.end_with_wildcard == 1 && 
253                             tree->u.substring.chunks[0] != NULL && 
254                             tree->u.substring.chunks[1] == NULL) {
255                                 tmp = callback(tree, tree->u.substring.chunks[0], context);
256                                 if (tmp) tree = tmp;
257                         }
258                 }
259                 break;
260         }
261         return tree;
262 }
263
264 /* search */
265 static int anr_search(struct ldb_module *module, struct ldb_request *req)
266 {
267         struct ldb_parse_tree *anr_tree;
268         struct anr_context *context = talloc(req, struct anr_context);
269         if (!context) {
270                 ldb_oom(module->ldb);
271                 return LDB_ERR_OPERATIONS_ERROR;
272         }
273
274         context->module = module;
275         context->found_anr = false;
276
277 #if 0
278         printf("oldanr : %s\n", ldb_filter_from_tree (0, req->op.search.tree));
279 #endif
280
281         /* Yes, this is a problem with req->op.search.tree being const... */
282         anr_tree = anr_replace_subtrees(req->op.search.tree, "anr", anr_replace_callback, context);
283         if (!anr_tree) {
284                 talloc_free(context);
285                 return LDB_ERR_OPERATIONS_ERROR;
286         }
287
288         if (context->found_anr) {
289                 /* The above function modifies the tree if it finds "anr", so no
290                  * point just setting this on the down_req */
291 #if 0
292                 printf("newtree: %s\n", ldb_filter_from_tree (0, anr_tree));
293 #endif
294                 req->op.search.tree = talloc_steal(req, anr_tree);
295         } else {
296                 if (anr_tree != req->op.search.tree) {
297                         talloc_free(anr_tree);
298                 }
299                 talloc_free(context);
300         }
301         return ldb_next_request(module, req);
302 }
303
304 _PUBLIC_ const struct ldb_module_ops ldb_anr_module_ops = {
305         .name              = "anr",
306         .search = anr_search
307 };