4 Copyright (C) Andrew Tridgell 2004-2005
5 Copyright (C) Simo Sorce 2005
7 ** NOTE! The following LGPL license applies to the ldb
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * Component: ldb expression matching
31 * Description: ldb expression matching
33 * Author: Andrew Tridgell
37 #include "ldb/include/ldb.h"
38 #include "ldb/include/ldb_private.h"
42 check if the scope matches in a search result
44 static int ldb_match_scope(struct ldb_context *ldb,
53 if (base_str == NULL) {
57 base = ldb_dn_explode_casefold(ldb, base_str);
58 if (base == NULL) return 0;
60 dn = ldb_dn_explode_casefold(ldb, dn_str);
68 if (ldb_dn_compare(ldb, base, dn) == 0) {
73 case LDB_SCOPE_ONELEVEL:
74 if (dn->comp_num != base->comp_num) {
75 if (ldb_dn_compare_base(ldb, base, dn) == 0) {
81 case LDB_SCOPE_SUBTREE:
83 if (ldb_dn_compare_base(ldb, base, dn) == 0) {
96 match if node is present
98 static int ldb_match_present(struct ldb_context *ldb,
99 struct ldb_message *msg,
100 struct ldb_parse_tree *tree,
102 enum ldb_scope scope)
105 if (ldb_attr_cmp(tree->u.present.attr, "dn") == 0) {
109 if (ldb_msg_find_element(msg, tree->u.present.attr)) {
117 match a simple leaf node
119 static int ldb_match_simple(struct ldb_context *ldb,
120 struct ldb_message *msg,
121 struct ldb_parse_tree *tree,
123 enum ldb_scope scope)
126 struct ldb_message_element *el;
127 const struct ldb_attrib_handler *h;
128 struct ldb_dn *msgdn, *valuedn;
131 if (ldb_attr_cmp(tree->u.simple.attr, "dn") == 0) {
133 msgdn = ldb_dn_explode_casefold(ldb, msg->dn);
134 if (msgdn == NULL) return 0;
136 valuedn = ldb_dn_explode_casefold(ldb, tree->u.simple.value.data);
137 if (valuedn == NULL) {
142 ret = ldb_dn_compare(ldb, msgdn, valuedn);
145 talloc_free(valuedn);
147 if (ret == 0) return 1;
151 el = ldb_msg_find_element(msg, tree->u.simple.attr);
156 h = ldb_attrib_handler(ldb, el->name);
158 for (i=0;i<el->num_values;i++) {
159 if (h->comparison_fn(ldb, ldb, &tree->u.simple.value,
160 &el->values[i]) == 0) {
168 static int ldb_wildcard_compare(struct ldb_context *ldb,
169 struct ldb_parse_tree *tree,
170 const struct ldb_val value)
172 const struct ldb_attrib_handler *h;
175 struct ldb_val *chunk;
180 h = ldb_attrib_handler(ldb, tree->u.substring.attr);
182 if(h->canonicalise_fn(ldb, ldb, &value, &val) != 0)
188 if ( ! tree->u.substring.start_with_wildcard ) {
190 chunk = tree->u.substring.chunks[c];
191 if(h->canonicalise_fn(ldb, ldb, chunk, &cnk) != 0) goto failed;
193 /* FIXME: case of embedded nulls */
194 if (strncmp(val.data, cnk.data, cnk.length) != 0) goto failed;
195 val.length -= cnk.length;
196 val.data += cnk.length;
198 talloc_free(cnk.data);
202 while (tree->u.substring.chunks[c]) {
204 chunk = tree->u.substring.chunks[c];
205 if(h->canonicalise_fn(ldb, ldb, chunk, &cnk) != 0) goto failed;
207 /* FIXME: case of embedded nulls */
208 p = strstr(val.data, cnk.data);
209 if (p == NULL) goto failed;
210 if ( (! tree->u.substring.chunks[c + 1]) && (! tree->u.substring.end_with_wildcard) ) {
212 g = strstr(p + cnk.length, cnk.data);
216 val.length = val.length - (p - (char *)(val.data)) - cnk.length;
217 val.data = p + cnk.length;
219 talloc_free(cnk.data);
223 if ( (! tree->u.substring.end_with_wildcard) && (*(val.data) != 0) ) goto failed; /* last chunk have not reached end of string */
229 talloc_free(cnk.data);
234 match a simple leaf node
236 static int ldb_match_substring(struct ldb_context *ldb,
237 struct ldb_message *msg,
238 struct ldb_parse_tree *tree,
240 enum ldb_scope scope)
243 struct ldb_message_element *el;
245 el = ldb_msg_find_element(msg, tree->u.simple.attr);
250 for (i = 0; i < el->num_values; i++) {
251 if (ldb_wildcard_compare(ldb, tree, el->values[i]) == 1) {
261 bitwise-and comparator
263 static int ldb_comparator_and(struct ldb_val *v1, struct ldb_val *v2)
266 i1 = strtoull(v1->data, NULL, 0);
267 i2 = strtoull(v2->data, NULL, 0);
268 return ((i1 & i2) == i2);
272 bitwise-or comparator
274 static int ldb_comparator_or(struct ldb_val *v1, struct ldb_val *v2)
277 i1 = strtoull(v1->data, NULL, 0);
278 i2 = strtoull(v2->data, NULL, 0);
279 return ((i1 & i2) != 0);
284 extended match, handles things like bitops
286 static int ldb_match_extended(struct ldb_context *ldb,
287 struct ldb_message *msg,
288 struct ldb_parse_tree *tree,
290 enum ldb_scope scope)
295 int (*comparator)(struct ldb_val *, struct ldb_val *);
297 { LDB_OID_COMPARATOR_AND, ldb_comparator_and},
298 { LDB_OID_COMPARATOR_OR, ldb_comparator_or}
300 int (*comp)(struct ldb_val *, struct ldb_val *) = NULL;
301 struct ldb_message_element *el;
303 if (tree->u.extended.dnAttributes) {
304 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb: dnAttributes extended match not supported yet");
307 if (tree->u.extended.rule_id == NULL) {
308 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb: no-rule extended matches not supported yet");
311 if (tree->u.extended.attr == NULL) {
312 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb: no-attribute extended matches not supported yet");
316 for (i=0;i<ARRAY_SIZE(rules);i++) {
317 if (strcmp(rules[i].oid, tree->u.extended.rule_id) == 0) {
318 comp = rules[i].comparator;
323 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb: unknown extended rule_id %s\n",
324 tree->u.extended.rule_id);
328 /* find the message element */
329 el = ldb_msg_find_element(msg, tree->u.extended.attr);
334 for (i=0;i<el->num_values;i++) {
335 int ret = comp(&el->values[i], &tree->u.extended.value);
336 if (ret == -1 || ret == 1) return ret;
343 return 0 if the given parse tree matches the given message. Assumes
344 the message is in sorted order
346 return 1 if it matches, and 0 if it doesn't match
348 this is a recursive function, and does short-circuit evaluation
350 static int ldb_match_message(struct ldb_context *ldb,
351 struct ldb_message *msg,
352 struct ldb_parse_tree *tree,
354 enum ldb_scope scope)
359 switch (tree->operation) {
361 return ldb_match_simple(ldb, msg, tree, base, scope);
364 return ldb_match_present(ldb, msg, tree, base, scope);
366 case LDB_OP_SUBSTRING:
367 return ldb_match_substring(ldb, msg, tree, base, scope);
369 case LDB_OP_EXTENDED:
370 return ldb_match_extended(ldb, msg, tree, base, scope);
373 return ! ldb_match_message(ldb, msg, tree->u.isnot.child, base, scope);
376 for (i=0;i<tree->u.list.num_elements;i++) {
377 v = ldb_match_message(ldb, msg, tree->u.list.elements[i],
384 for (i=0;i<tree->u.list.num_elements;i++) {
385 v = ldb_match_message(ldb, msg, tree->u.list.elements[i],
395 int ldb_match_msg(struct ldb_context *ldb,
396 struct ldb_message *msg,
397 struct ldb_parse_tree *tree,
399 enum ldb_scope scope)
401 if ( ! ldb_match_scope(ldb, base, msg->dn, scope) ) {
405 return ldb_match_message(ldb, msg, tree, base, scope);