s4: fix various warnings (not "const" related ones)
[sfrench/samba-autobuild/.git] / source4 / dsdb / samdb / ldb_modules / anr.c
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
36 /**
37  * Make a and 'and' or 'or' tree from the two supplied elements 
38  */
39 static struct ldb_parse_tree *make_parse_list(struct ldb_module *module,
40                                        TALLOC_CTX *mem_ctx, enum ldb_parse_op op, 
41                                        struct ldb_parse_tree *first_arm, struct ldb_parse_tree *second_arm)
42 {
43         struct ldb_context *ldb;
44         struct ldb_parse_tree *list;
45
46         ldb = ldb_module_get_ctx(module);
47
48         list = talloc(mem_ctx, struct ldb_parse_tree);
49         if (list == NULL){
50                 ldb_oom(ldb);
51                 return NULL;
52         }
53         list->operation = op;
54         
55         list->u.list.num_elements = 2;
56         list->u.list.elements = talloc_array(list, struct ldb_parse_tree *, 2);
57         if (!list->u.list.elements) {
58                 ldb_oom(ldb);
59                 return NULL;
60         }
61         list->u.list.elements[0] = talloc_steal(list, first_arm);
62         list->u.list.elements[1] = talloc_steal(list, second_arm);
63         return list;
64 }
65
66 /**
67  * Make an equality or prefix match tree, from the attribute, operation and matching value supplied
68  */
69 static struct ldb_parse_tree *make_match_tree(struct ldb_module *module,
70                                               TALLOC_CTX *mem_ctx,
71                                               enum ldb_parse_op op,
72                                               const char *attr,
73                                               struct ldb_val *match)
74 {
75         struct ldb_context *ldb;
76         struct ldb_parse_tree *match_tree;
77
78         ldb = ldb_module_get_ctx(module);
79
80         match_tree = talloc(mem_ctx, struct ldb_parse_tree);
81         
82         /* Depending on what type of match was selected, fill in the right part of the union */
83          
84         match_tree->operation = op;
85         switch (op) {
86         case LDB_OP_SUBSTRING:
87                 match_tree->u.substring.attr = attr;
88                 
89                 match_tree->u.substring.start_with_wildcard = 0;
90                 match_tree->u.substring.end_with_wildcard = 1;
91                 match_tree->u.substring.chunks = talloc_array(match_tree, struct ldb_val *, 2);
92                 
93                 if (match_tree->u.substring.chunks == NULL){
94                         talloc_free(match_tree);
95                         ldb_oom(ldb);
96                         return NULL;
97                 }
98                 match_tree->u.substring.chunks[0] = match;
99                 match_tree->u.substring.chunks[1] = NULL;
100                 break;
101         case LDB_OP_EQUALITY:
102                 match_tree->u.equality.attr = attr;
103                 match_tree->u.equality.value = *match;
104                 break;
105         default:
106                 talloc_free(match_tree);
107                 return NULL;
108         }
109         return match_tree;
110 }
111
112 struct anr_context {
113         bool found_anr;
114         struct ldb_module *module;
115         struct ldb_request *req;
116 };
117
118 /**
119  * Given the match for an 'ambigious name resolution' query, create a
120  * parse tree with an 'or' of all the anr attributes in the schema.  
121  */
122
123 /**
124  * Callback function to do the heavy lifting for the parse tree walker
125  */
126 static int anr_replace_value(struct anr_context *ac,
127                              TALLOC_CTX *mem_ctx,
128                              struct ldb_val *match,
129                              struct ldb_parse_tree **ntree)
130 {
131         struct ldb_parse_tree *tree = NULL;
132         struct ldb_module *module = ac->module;
133         struct ldb_parse_tree *match_tree;
134         struct dsdb_attribute *cur;
135         const struct dsdb_schema *schema;
136         struct ldb_context *ldb;
137         uint8_t *p;
138         enum ldb_parse_op op;
139
140         ldb = ldb_module_get_ctx(module);
141
142         schema = dsdb_get_schema(ldb);
143         if (!schema) {
144                 ldb_asprintf_errstring(ldb, "no schema with which to construct anr filter");
145                 return LDB_ERR_OPERATIONS_ERROR;
146         }
147
148         ac->found_anr = true;
149
150         if (match->length > 1 && match->data[0] == '=') {
151                 struct ldb_val *match2 = talloc(mem_ctx, struct ldb_val);
152                 *match2 = data_blob_const(match->data+1, match->length - 1);
153                 if (match2 == NULL){
154                         ldb_oom(ldb);
155                         return LDB_ERR_OPERATIONS_ERROR;
156                 }
157                 match = match2;
158                 op = LDB_OP_EQUALITY;
159         } else {
160                 op = LDB_OP_SUBSTRING;
161         }
162         for (cur = schema->attributes; cur; cur = cur->next) {
163                 if (!(cur->searchFlags & SEARCH_FLAG_ANR)) continue;
164                 match_tree = make_match_tree(module, mem_ctx, op, cur->lDAPDisplayName, match);
165
166                 if (tree) {
167                         /* Inject an 'or' with the current tree */
168                         tree = make_parse_list(module, mem_ctx,  LDB_OP_OR, tree, match_tree);
169                         if (tree == NULL) {
170                                 ldb_oom(ldb);
171                                 return LDB_ERR_OPERATIONS_ERROR;
172                         }
173                 } else {
174                         tree = match_tree;
175                 }
176         }
177
178         
179         /* If the search term has a space in it, 
180            split it up at the first space.  */
181         
182         p = memchr(match->data, ' ', match->length);
183
184         if (p) {
185                 struct ldb_parse_tree *first_split_filter, *second_split_filter, *split_filters, *match_tree_1, *match_tree_2;
186                 struct ldb_val *first_match = talloc(tree, struct ldb_val);
187                 struct ldb_val *second_match = talloc(tree, struct ldb_val);
188                 if (!first_match || !second_match) {
189                         ldb_oom(ldb);
190                         return LDB_ERR_OPERATIONS_ERROR;
191                 }
192                 *first_match = data_blob_const(match->data, p-match->data);
193                 *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                 match_tree_1 = make_match_tree(module, mem_ctx, op, "givenName", first_match);
198                 match_tree_2 = make_match_tree(module, mem_ctx, op, "sn", second_match);
199
200                 first_split_filter = make_parse_list(module, ac,  LDB_OP_AND, match_tree_1, match_tree_2);
201                 if (first_split_filter == NULL){
202                         ldb_oom(ldb);
203                         return LDB_ERR_OPERATIONS_ERROR;
204                 }
205                 
206                 match_tree_1 = make_match_tree(module, mem_ctx, op, "sn", first_match);
207                 match_tree_2 = make_match_tree(module, mem_ctx, op, "givenName", second_match);
208
209                 second_split_filter = make_parse_list(module, ac,  LDB_OP_AND, match_tree_1, match_tree_2);
210                 if (second_split_filter == NULL){
211                         ldb_oom(ldb);
212                         return LDB_ERR_OPERATIONS_ERROR;
213                 }
214
215                 split_filters = make_parse_list(module, mem_ctx,  LDB_OP_OR, 
216                                                 first_split_filter, second_split_filter);
217                 if (split_filters == NULL) {
218                         ldb_oom(ldb);
219                         return LDB_ERR_OPERATIONS_ERROR;
220                 }
221
222                 if (tree) {
223                         /* Inject an 'or' with the current tree */
224                         tree = make_parse_list(module, mem_ctx,  LDB_OP_OR, tree, split_filters);
225                 } else {
226                         tree = split_filters;
227                 }
228         }
229         *ntree = tree;
230         return LDB_SUCCESS;
231 }
232
233 /*
234   replace any occurances of an attribute with a new, generated attribute tree
235 */
236 static int anr_replace_subtrees(struct anr_context *ac,
237                                 struct ldb_parse_tree *tree,
238                                 const char *attr,
239                                 struct ldb_parse_tree **ntree)
240 {
241         int ret;
242         int i;
243
244         switch (tree->operation) {
245         case LDB_OP_AND:
246         case LDB_OP_OR:
247                 for (i=0;i<tree->u.list.num_elements;i++) {
248                         ret = anr_replace_subtrees(ac, tree->u.list.elements[i],
249                                                    attr, &tree->u.list.elements[i]);
250                         if (ret != LDB_SUCCESS) {
251                                 return ret;
252                         }
253                         *ntree = tree;
254                 }
255                 break;
256         case LDB_OP_NOT:
257                 ret = anr_replace_subtrees(ac, tree->u.isnot.child, attr, &tree->u.isnot.child);
258                 if (ret != LDB_SUCCESS) {
259                         return ret;
260                 }
261                 *ntree = tree;
262                 break;
263         case LDB_OP_EQUALITY:
264                 if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) {
265                         ret = anr_replace_value(ac, tree, &tree->u.equality.value, ntree);
266                         if (ret != LDB_SUCCESS) {
267                                 return ret;
268                         }
269                 }
270                 break;
271         case LDB_OP_SUBSTRING:
272                 if (ldb_attr_cmp(tree->u.substring.attr, attr) == 0) {
273                         if (tree->u.substring.start_with_wildcard == 0 &&
274                             tree->u.substring.end_with_wildcard == 1 && 
275                             tree->u.substring.chunks[0] != NULL && 
276                             tree->u.substring.chunks[1] == NULL) {
277                                 ret = anr_replace_value(ac, tree, tree->u.substring.chunks[0], ntree);
278                                 if (ret != LDB_SUCCESS) {
279                                         return ret;
280                                 }
281                         }
282                 }
283                 break;
284         default:
285                 break;
286         }
287
288         return LDB_SUCCESS;
289 }
290
291 static int anr_search_callback(struct ldb_request *req, struct ldb_reply *ares)
292 {
293         struct anr_context *ac;
294
295         ac = talloc_get_type(req->context, struct anr_context);
296
297         if (!ares) {
298                 return ldb_module_done(ac->req, NULL, NULL,
299                                         LDB_ERR_OPERATIONS_ERROR);
300         }
301         if (ares->error != LDB_SUCCESS) {
302                 return ldb_module_done(ac->req, ares->controls,
303                                         ares->response, ares->error);
304         }
305
306         switch (ares->type) {
307         case LDB_REPLY_ENTRY:
308                 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
309
310         case LDB_REPLY_REFERRAL:
311                 return ldb_module_send_referral(ac->req, ares->referral);
312
313         case LDB_REPLY_DONE:
314                 return ldb_module_done(ac->req, ares->controls,
315                                         ares->response, LDB_SUCCESS);
316
317         }
318         return LDB_SUCCESS;
319 }
320
321 /* search */
322 static int anr_search(struct ldb_module *module, struct ldb_request *req)
323 {
324         struct ldb_context *ldb;
325         struct ldb_parse_tree *anr_tree;
326         struct ldb_request *down_req;
327         struct anr_context *ac;
328         int ret;
329
330         ldb = ldb_module_get_ctx(module);
331
332         ac = talloc(req, struct anr_context);
333         if (!ac) {
334                 ldb_oom(ldb);
335                 return LDB_ERR_OPERATIONS_ERROR;
336         }
337
338         ac->module = module;
339         ac->req = req;
340         ac->found_anr = false;
341
342 #if 0
343         printf("oldanr : %s\n", ldb_filter_from_tree (0, req->op.search.tree));
344 #endif
345
346         ret = anr_replace_subtrees(ac, req->op.search.tree, "anr", &anr_tree);
347         if (ret != LDB_SUCCESS) {
348                 return LDB_ERR_OPERATIONS_ERROR;
349         }
350
351         if (!ac->found_anr) {
352                 talloc_free(ac);
353                 return ldb_next_request(module, req);
354         }
355
356         ret = ldb_build_search_req_ex(&down_req,
357                                         ldb, ac,
358                                         req->op.search.base,
359                                         req->op.search.scope,
360                                         anr_tree,
361                                         req->op.search.attrs,
362                                         req->controls,
363                                         ac, anr_search_callback,
364                                         req);
365         if (ret != LDB_SUCCESS) {
366                 return LDB_ERR_OPERATIONS_ERROR;
367         }
368         talloc_steal(down_req, anr_tree);
369
370         return ldb_next_request(module, down_req);
371 }
372
373 _PUBLIC_ const struct ldb_module_ops ldb_anr_module_ops = {
374         .name              = "anr",
375         .search = anr_search
376 };