4 Copyright (C) Amdrew Bartlett <abartlet@samba.org> 2007
5 Copyright (C) Andrew Tridgell 2004
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.
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.
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/>.
24 * Component: ldb anr module
26 * Description: module to implement 'ambiguous name resolution'
28 * Author: Andrew Bartlett
32 #include "ldb_includes.h"
33 #include "dsdb/samdb/samdb.h"
36 * Make a and 'and' or 'or' tree from the two supplied elements
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)
42 struct ldb_parse_tree *list;
44 list = talloc(mem_ctx, struct ldb_parse_tree);
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) {
57 list->u.list.elements[0] = talloc_steal(list, first_arm);
58 list->u.list.elements[1] = talloc_steal(list, second_arm);
63 * Make an equality or prefix match tree, from the attribute, operation and matching value supplied
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)
69 struct ldb_parse_tree *match_tree;
71 match_tree = talloc(mem_ctx, struct ldb_parse_tree);
73 /* Depending on what type of match was selected, fill in the right part of the union */
75 match_tree->operation = op;
77 case LDB_OP_SUBSTRING:
78 match_tree->u.substring.attr = attr;
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);
84 if (match_tree->u.substring.chunks == NULL){
88 match_tree->u.substring.chunks[0] = match;
89 match_tree->u.substring.chunks[1] = NULL;
92 match_tree->u.equality.attr = attr;
93 match_tree->u.equality.value = *match;
101 struct ldb_module *module;
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.
109 typedef struct ldb_parse_tree *(*anr_parse_tree_callback_t)(TALLOC_CTX *mem_ctx,
110 const struct ldb_val *match,
115 * Callback function to do the heavy lifting for the for the parse tree walker
117 struct ldb_parse_tree *anr_replace_callback(TALLOC_CTX *mem_ctx,
118 const struct ldb_val *match,
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;
126 enum ldb_parse_op op;
127 struct dsdb_attribute *cur;
128 const struct dsdb_schema *schema = dsdb_get_schema(module->ldb);
130 ldb_asprintf_errstring(module->ldb, "no schema with which to construct anr filter");
134 anr_context->found_anr = true;
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);
140 ldb_oom(module->ldb);
144 op = LDB_OP_EQUALITY;
146 op = LDB_OP_SUBSTRING;
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);
153 /* Inject an 'or' with the current tree */
154 tree = make_parse_list(module, mem_ctx, LDB_OP_OR, tree, match_tree);
156 ldb_oom(module->ldb);
165 /* If the search term has a space in it,
166 split it up at the first space. */
168 p = memchr(match->data, ' ', match->length);
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);
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);
181 /* Add (|(&(givenname=first)(sn=second))(&(givenname=second)(sn=first))) */
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);
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);
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);
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);
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);
209 /* Inject an 'or' with the current tree */
210 tree = make_parse_list(module, mem_ctx, LDB_OP_OR, tree, split_filters);
212 tree = split_filters;
219 replace any occurances of an attribute with a new, generated attribute tree
221 struct ldb_parse_tree *anr_replace_subtrees(struct ldb_parse_tree *tree,
223 anr_parse_tree_callback_t callback,
227 struct ldb_parse_tree *tmp;
229 switch (tree->operation) {
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;
239 tmp = anr_replace_subtrees(tree->u.isnot.child, attr, callback, context);
240 if (tmp) tree->u.isnot.child = tmp;
242 case LDB_OP_EQUALITY:
243 if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) {
244 tmp = callback(tree, &tree->u.equality.value,
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);
265 static int anr_search(struct ldb_module *module, struct ldb_request *req)
267 struct ldb_parse_tree *anr_tree;
268 struct anr_context *context = talloc(req, struct anr_context);
270 ldb_oom(module->ldb);
271 return LDB_ERR_OPERATIONS_ERROR;
274 context->module = module;
275 context->found_anr = false;
278 printf("oldanr : %s\n", ldb_filter_from_tree (0, req->op.search.tree));
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);
284 talloc_free(context);
285 return LDB_ERR_OPERATIONS_ERROR;
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 */
292 printf("newtree: %s\n", ldb_filter_from_tree (0, anr_tree));
294 req->op.search.tree = talloc_steal(req, anr_tree);
296 if (anr_tree != req->op.search.tree) {
297 talloc_free(anr_tree);
299 talloc_free(context);
301 return ldb_next_request(module, req);
304 _PUBLIC_ const struct ldb_module_ops ldb_anr_module_ops = {