68f007ebd97dce53da0e8eb3bca9327d89b22133
[metze/samba/wip.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, ac);
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                         return ldb_oom(ldb);
155                 }
156                 match = match2;
157                 op = LDB_OP_EQUALITY;
158         } else {
159                 op = LDB_OP_SUBSTRING;
160         }
161         for (cur = schema->attributes; cur; cur = cur->next) {
162                 if (!(cur->searchFlags & SEARCH_FLAG_ANR)) continue;
163                 match_tree = make_match_tree(module, mem_ctx, op, cur->lDAPDisplayName, match);
164
165                 if (tree) {
166                         /* Inject an 'or' with the current tree */
167                         tree = make_parse_list(module, mem_ctx,  LDB_OP_OR, tree, match_tree);
168                         if (tree == NULL) {
169                                 return ldb_oom(ldb);
170                         }
171                 } else {
172                         tree = match_tree;
173                 }
174         }
175
176         
177         /* If the search term has a space in it, 
178            split it up at the first space.  */
179         
180         p = memchr(match->data, ' ', match->length);
181
182         if (p) {
183                 struct ldb_parse_tree *first_split_filter, *second_split_filter, *split_filters, *match_tree_1, *match_tree_2;
184                 struct ldb_val *first_match = talloc(tree, struct ldb_val);
185                 struct ldb_val *second_match = talloc(tree, struct ldb_val);
186                 if (!first_match || !second_match) {
187                         return ldb_oom(ldb);
188                 }
189                 *first_match = data_blob_const(match->data, p-match->data);
190                 *second_match = data_blob_const(p+1, match->length - (p-match->data) - 1);
191                 
192                 /* Add (|(&(givenname=first)(sn=second))(&(givenname=second)(sn=first))) */
193
194                 match_tree_1 = make_match_tree(module, mem_ctx, op, "givenName", first_match);
195                 match_tree_2 = make_match_tree(module, mem_ctx, op, "sn", second_match);
196
197                 first_split_filter = make_parse_list(module, ac,  LDB_OP_AND, match_tree_1, match_tree_2);
198                 if (first_split_filter == NULL){
199                         return ldb_oom(ldb);
200                 }
201                 
202                 match_tree_1 = make_match_tree(module, mem_ctx, op, "sn", first_match);
203                 match_tree_2 = make_match_tree(module, mem_ctx, op, "givenName", second_match);
204
205                 second_split_filter = make_parse_list(module, ac,  LDB_OP_AND, match_tree_1, match_tree_2);
206                 if (second_split_filter == NULL){
207                         return ldb_oom(ldb);
208                 }
209
210                 split_filters = make_parse_list(module, mem_ctx,  LDB_OP_OR, 
211                                                 first_split_filter, second_split_filter);
212                 if (split_filters == NULL) {
213                         return ldb_oom(ldb);
214                 }
215
216                 if (tree) {
217                         /* Inject an 'or' with the current tree */
218                         tree = make_parse_list(module, mem_ctx,  LDB_OP_OR, tree, split_filters);
219                 } else {
220                         tree = split_filters;
221                 }
222         }
223         *ntree = tree;
224         return LDB_SUCCESS;
225 }
226
227 /*
228   replace any occurances of an attribute with a new, generated attribute tree
229 */
230 static int anr_replace_subtrees(struct anr_context *ac,
231                                 struct ldb_parse_tree *tree,
232                                 const char *attr,
233                                 struct ldb_parse_tree **ntree)
234 {
235         int ret;
236         unsigned int i;
237
238         switch (tree->operation) {
239         case LDB_OP_AND:
240         case LDB_OP_OR:
241                 for (i=0;i<tree->u.list.num_elements;i++) {
242                         ret = anr_replace_subtrees(ac, tree->u.list.elements[i],
243                                                    attr, &tree->u.list.elements[i]);
244                         if (ret != LDB_SUCCESS) {
245                                 return ret;
246                         }
247                         *ntree = tree;
248                 }
249                 break;
250         case LDB_OP_NOT:
251                 ret = anr_replace_subtrees(ac, tree->u.isnot.child, attr, &tree->u.isnot.child);
252                 if (ret != LDB_SUCCESS) {
253                         return ret;
254                 }
255                 *ntree = tree;
256                 break;
257         case LDB_OP_EQUALITY:
258                 if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) {
259                         ret = anr_replace_value(ac, tree, &tree->u.equality.value, ntree);
260                         if (ret != LDB_SUCCESS) {
261                                 return ret;
262                         }
263                 }
264                 break;
265         case LDB_OP_SUBSTRING:
266                 if (ldb_attr_cmp(tree->u.substring.attr, attr) == 0) {
267                         if (tree->u.substring.start_with_wildcard == 0 &&
268                             tree->u.substring.end_with_wildcard == 1 && 
269                             tree->u.substring.chunks[0] != NULL && 
270                             tree->u.substring.chunks[1] == NULL) {
271                                 ret = anr_replace_value(ac, tree, tree->u.substring.chunks[0], ntree);
272                                 if (ret != LDB_SUCCESS) {
273                                         return ret;
274                                 }
275                         }
276                 }
277                 break;
278         default:
279                 break;
280         }
281
282         return LDB_SUCCESS;
283 }
284
285 static int anr_search_callback(struct ldb_request *req, struct ldb_reply *ares)
286 {
287         struct anr_context *ac;
288
289         ac = talloc_get_type(req->context, struct anr_context);
290
291         if (!ares) {
292                 return ldb_module_done(ac->req, NULL, NULL,
293                                         LDB_ERR_OPERATIONS_ERROR);
294         }
295         if (ares->error != LDB_SUCCESS) {
296                 return ldb_module_done(ac->req, ares->controls,
297                                         ares->response, ares->error);
298         }
299
300         switch (ares->type) {
301         case LDB_REPLY_ENTRY:
302                 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
303
304         case LDB_REPLY_REFERRAL:
305                 return ldb_module_send_referral(ac->req, ares->referral);
306
307         case LDB_REPLY_DONE:
308                 return ldb_module_done(ac->req, ares->controls,
309                                         ares->response, LDB_SUCCESS);
310
311         }
312         return LDB_SUCCESS;
313 }
314
315 /* search */
316 static int anr_search(struct ldb_module *module, struct ldb_request *req)
317 {
318         struct ldb_context *ldb;
319         struct ldb_parse_tree *anr_tree;
320         struct ldb_request *down_req;
321         struct anr_context *ac;
322         int ret;
323
324         ldb = ldb_module_get_ctx(module);
325
326         ac = talloc(req, struct anr_context);
327         if (!ac) {
328                 return ldb_oom(ldb);
329         }
330
331         ac->module = module;
332         ac->req = req;
333         ac->found_anr = false;
334
335 #if 0
336         printf("oldanr : %s\n", ldb_filter_from_tree (0, req->op.search.tree));
337 #endif
338
339         ret = anr_replace_subtrees(ac, req->op.search.tree, "anr", &anr_tree);
340         if (ret != LDB_SUCCESS) {
341                 return ldb_operr(ldb);
342         }
343
344         if (!ac->found_anr) {
345                 talloc_free(ac);
346                 return ldb_next_request(module, req);
347         }
348
349         ret = ldb_build_search_req_ex(&down_req,
350                                         ldb, ac,
351                                         req->op.search.base,
352                                         req->op.search.scope,
353                                         anr_tree,
354                                         req->op.search.attrs,
355                                         req->controls,
356                                         ac, anr_search_callback,
357                                         req);
358         if (ret != LDB_SUCCESS) {
359                 return ldb_operr(ldb);
360         }
361         talloc_steal(down_req, anr_tree);
362
363         return ldb_next_request(module, down_req);
364 }
365
366 _PUBLIC_ const struct ldb_module_ops ldb_anr_module_ops = {
367         .name              = "anr",
368         .search = anr_search
369 };