4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007
5 Copyright (C) Simo Sorce <idra@samba.org> 2008
6 Copyright (C) Andrew Tridgell 2004
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.
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.
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/>.
25 * Component: ldb anr module
27 * Description: module to implement 'ambiguous name resolution'
29 * Author: Andrew Bartlett
33 #include "ldb_module.h"
34 #include "dsdb/samdb/samdb.h"
37 * Make a and 'and' or 'or' tree from the two supplied elements
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)
43 struct ldb_context *ldb;
44 struct ldb_parse_tree *list;
46 ldb = ldb_module_get_ctx(module);
48 list = talloc(mem_ctx, struct ldb_parse_tree);
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) {
61 list->u.list.elements[0] = talloc_steal(list, first_arm);
62 list->u.list.elements[1] = talloc_steal(list, second_arm);
67 * Make an equality or prefix match tree, from the attribute, operation and matching value supplied
69 static struct ldb_parse_tree *make_match_tree(struct ldb_module *module,
70 TALLOC_CTX *mem_ctx, enum ldb_parse_op op,
71 const char *attr, const DATA_BLOB *match)
73 struct ldb_context *ldb;
74 struct ldb_parse_tree *match_tree;
76 ldb = ldb_module_get_ctx(module);
78 match_tree = talloc(mem_ctx, struct ldb_parse_tree);
80 /* Depending on what type of match was selected, fill in the right part of the union */
82 match_tree->operation = op;
84 case LDB_OP_SUBSTRING:
85 match_tree->u.substring.attr = attr;
87 match_tree->u.substring.start_with_wildcard = 0;
88 match_tree->u.substring.end_with_wildcard = 1;
89 match_tree->u.substring.chunks = talloc_array(match_tree, struct ldb_val *, 2);
91 if (match_tree->u.substring.chunks == NULL){
95 match_tree->u.substring.chunks[0] = match;
96 match_tree->u.substring.chunks[1] = NULL;
99 match_tree->u.equality.attr = attr;
100 match_tree->u.equality.value = *match;
108 struct ldb_module *module;
109 struct ldb_request *req;
113 * Given the match for an 'ambigious name resolution' query, create a
114 * parse tree with an 'or' of all the anr attributes in the schema.
118 * Callback function to do the heavy lifting for the parse tree walker
120 static int anr_replace_value(struct anr_context *ac,
122 const struct ldb_val *match,
123 struct ldb_parse_tree **ntree)
125 struct ldb_parse_tree *tree = NULL;
126 struct ldb_module *module = ac->module;
127 struct ldb_parse_tree *match_tree;
128 struct dsdb_attribute *cur;
129 const struct dsdb_schema *schema;
130 struct ldb_context *ldb;
132 enum ldb_parse_op op;
134 ldb = ldb_module_get_ctx(module);
136 schema = dsdb_get_schema(ldb);
138 ldb_asprintf_errstring(ldb, "no schema with which to construct anr filter");
139 return LDB_ERR_OPERATIONS_ERROR;
142 ac->found_anr = true;
144 if (match->length > 1 && match->data[0] == '=') {
145 DATA_BLOB *match2 = talloc(mem_ctx, DATA_BLOB);
146 *match2 = data_blob_const(match->data+1, match->length - 1);
149 return LDB_ERR_OPERATIONS_ERROR;
152 op = LDB_OP_EQUALITY;
154 op = LDB_OP_SUBSTRING;
156 for (cur = schema->attributes; cur; cur = cur->next) {
157 if (!(cur->searchFlags & SEARCH_FLAG_ANR)) continue;
158 match_tree = make_match_tree(module, mem_ctx, op, cur->lDAPDisplayName, match);
161 /* Inject an 'or' with the current tree */
162 tree = make_parse_list(module, mem_ctx, LDB_OP_OR, tree, match_tree);
165 return LDB_ERR_OPERATIONS_ERROR;
173 /* If the search term has a space in it,
174 split it up at the first space. */
176 p = memchr(match->data, ' ', match->length);
179 struct ldb_parse_tree *first_split_filter, *second_split_filter, *split_filters, *match_tree_1, *match_tree_2;
180 DATA_BLOB *first_match = talloc(tree, DATA_BLOB);
181 DATA_BLOB *second_match = talloc(tree, DATA_BLOB);
182 if (!first_match || !second_match) {
184 return LDB_ERR_OPERATIONS_ERROR;
186 *first_match = data_blob_const(match->data, p-match->data);
187 *second_match = data_blob_const(p+1, match->length - (p-match->data) - 1);
189 /* Add (|(&(givenname=first)(sn=second))(&(givenname=second)(sn=first))) */
191 match_tree_1 = make_match_tree(module, mem_ctx, op, "givenName", first_match);
192 match_tree_2 = make_match_tree(module, mem_ctx, op, "sn", second_match);
194 first_split_filter = make_parse_list(module, ac, LDB_OP_AND, match_tree_1, match_tree_2);
195 if (first_split_filter == NULL){
197 return LDB_ERR_OPERATIONS_ERROR;
200 match_tree_1 = make_match_tree(module, mem_ctx, op, "sn", first_match);
201 match_tree_2 = make_match_tree(module, mem_ctx, op, "givenName", second_match);
203 second_split_filter = make_parse_list(module, ac, LDB_OP_AND, match_tree_1, match_tree_2);
204 if (second_split_filter == NULL){
206 return LDB_ERR_OPERATIONS_ERROR;
209 split_filters = make_parse_list(module, mem_ctx, LDB_OP_OR,
210 first_split_filter, second_split_filter);
211 if (split_filters == NULL) {
213 return LDB_ERR_OPERATIONS_ERROR;
217 /* Inject an 'or' with the current tree */
218 tree = make_parse_list(module, mem_ctx, LDB_OP_OR, tree, split_filters);
220 tree = split_filters;
228 replace any occurances of an attribute with a new, generated attribute tree
230 static int anr_replace_subtrees(struct anr_context *ac,
231 struct ldb_parse_tree *tree,
233 struct ldb_parse_tree **ntree)
238 switch (tree->operation) {
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) {
251 ret = anr_replace_subtrees(ac, tree->u.isnot.child, attr, &tree->u.isnot.child);
252 if (ret != LDB_SUCCESS) {
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) {
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) {
285 static int anr_search_callback(struct ldb_request *req, struct ldb_reply *ares)
287 struct anr_context *ac;
289 ac = talloc_get_type(req->context, struct anr_context);
292 return ldb_module_done(ac->req, NULL, NULL,
293 LDB_ERR_OPERATIONS_ERROR);
295 if (ares->error != LDB_SUCCESS) {
296 return ldb_module_done(ac->req, ares->controls,
297 ares->response, ares->error);
300 switch (ares->type) {
301 case LDB_REPLY_ENTRY:
302 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
304 case LDB_REPLY_REFERRAL:
305 return ldb_module_send_referral(ac->req, ares->referral);
308 return ldb_module_done(ac->req, ares->controls,
309 ares->response, LDB_SUCCESS);
316 static int anr_search(struct ldb_module *module, struct ldb_request *req)
318 struct ldb_context *ldb;
319 struct ldb_parse_tree *anr_tree;
320 struct ldb_request *down_req;
321 struct anr_context *ac;
324 ldb = ldb_module_get_ctx(module);
326 ac = talloc(req, struct anr_context);
329 return LDB_ERR_OPERATIONS_ERROR;
334 ac->found_anr = false;
337 printf("oldanr : %s\n", ldb_filter_from_tree (0, req->op.search.tree));
340 ret = anr_replace_subtrees(ac, req->op.search.tree, "anr", &anr_tree);
341 if (ret != LDB_SUCCESS) {
342 return LDB_ERR_OPERATIONS_ERROR;
345 if (!ac->found_anr) {
347 return ldb_next_request(module, req);
350 ret = ldb_build_search_req_ex(&down_req,
353 req->op.search.scope,
355 req->op.search.attrs,
357 ac, anr_search_callback,
359 if (ret != LDB_SUCCESS) {
360 return LDB_ERR_OPERATIONS_ERROR;
362 talloc_steal(down_req, anr_tree);
364 return ldb_next_request(module, down_req);
367 _PUBLIC_ const struct ldb_module_ops ldb_anr_module_ops = {