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