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