r10895: allow 'dn=string' searches to work again. Windows doesn't allow these,
[nivanova/samba-autobuild/.git] / source4 / lib / ldb / common / ldb_match.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004-2005
5    Copyright (C) Simo Sorce            2005
6
7      ** NOTE! The following LGPL license applies to the ldb
8      ** library. This does NOT imply that all of Samba is released
9      ** under the LGPL
10    
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.
15
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.
20
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
24 */
25
26 /*
27  *  Name: ldb
28  *
29  *  Component: ldb expression matching
30  *
31  *  Description: ldb expression matching 
32  *
33  *  Author: Andrew Tridgell
34  */
35
36 #include "includes.h"
37 #include "ldb/include/ldb.h"
38 #include "ldb/include/ldb_private.h"
39
40
41 /*
42   check if the scope matches in a search result
43 */
44 static int ldb_match_scope(struct ldb_context *ldb,
45                            const struct ldb_dn *base,
46                            const struct ldb_dn *dn,
47                            enum ldb_scope scope)
48 {
49         int ret = 0;
50
51         if (base == NULL || dn == NULL) {
52                 return 1;
53         }
54
55         switch (scope) {
56         case LDB_SCOPE_BASE:
57                 if (ldb_dn_compare(ldb, base, dn) == 0) {
58                         ret = 1;
59                 }
60                 break;
61
62         case LDB_SCOPE_ONELEVEL:
63                 if (dn->comp_num == (base->comp_num + 1)) {
64                         if (ldb_dn_compare_base(ldb, base, dn) == 0) {
65                                 ret = 1;
66                         }
67                 }
68                 break;
69                 
70         case LDB_SCOPE_SUBTREE:
71         default:
72                 if (ldb_dn_compare_base(ldb, base, dn) == 0) {
73                         ret = 1;
74                 }
75                 break;
76         }
77
78         return ret;
79 }
80
81
82 /*
83   match if node is present
84 */
85 static int ldb_match_present(struct ldb_context *ldb, 
86                             struct ldb_message *msg,
87                             struct ldb_parse_tree *tree,
88                             enum ldb_scope scope)
89 {
90
91         if (ldb_attr_cmp(tree->u.present.attr, "distinguishedName") == 0) {
92                 return 1;
93         }
94
95         if (ldb_msg_find_element(msg, tree->u.present.attr)) {
96                 return 1;
97         }
98
99         return 0;
100 }
101
102 static int ldb_match_comparison(struct ldb_context *ldb, 
103                                 struct ldb_message *msg,
104                                 struct ldb_parse_tree *tree,
105                                 enum ldb_scope scope,
106                                 enum ldb_parse_op comp_op)
107 {
108         unsigned int i;
109         struct ldb_message_element *el;
110         const struct ldb_attrib_handler *h;
111         int ret;
112
113         /* FIXME: APPROX comparison not handled yet */
114         if (comp_op == LDB_OP_APPROX) return 0;
115
116         el = ldb_msg_find_element(msg, tree->u.comparison.attr);
117         if (el == NULL) {
118                 return 0;
119         }
120
121         h = ldb_attrib_handler(ldb, el->name);
122
123         for (i = 0; i < el->num_values; i++) {
124                 ret = h->comparison_fn(ldb, ldb, &el->values[i], &tree->u.comparison.value);
125
126                 if (ret == 0) {
127                         return 1;
128                 }
129                 if (ret > 0 && comp_op == LDB_OP_GREATER) {
130                         return 1;
131                 }
132                 if (ret < 0 && comp_op == LDB_OP_LESS) {
133                         return 1;
134                 }
135         }
136
137         return 0;
138 }
139
140 /*
141   match a simple leaf node
142 */
143 static int ldb_match_equality(struct ldb_context *ldb, 
144                               struct ldb_message *msg,
145                               struct ldb_parse_tree *tree,
146                               enum ldb_scope scope)
147 {
148         unsigned int i;
149         struct ldb_message_element *el;
150         const struct ldb_attrib_handler *h;
151         struct ldb_dn *valuedn;
152         int ret;
153
154         if (ldb_attr_cmp(tree->u.equality.attr, "dn") == 0 ||
155             ldb_attr_cmp(tree->u.equality.attr, "distinguishedName") == 0) {
156                 valuedn = ldb_dn_explode_casefold(ldb, tree->u.equality.value.data);
157                 if (valuedn == NULL) {
158                         return 0;
159                 }
160
161                 ret = ldb_dn_compare(ldb, msg->dn, valuedn);
162
163                 talloc_free(valuedn);
164
165                 if (ret == 0) return 1;
166                 return 0;
167         }
168
169         /* TODO: handle the "*" case derived from an extended search
170            operation without the attibute type defined */
171         el = ldb_msg_find_element(msg, tree->u.equality.attr);
172         if (el == NULL) {
173                 return 0;
174         }
175
176         h = ldb_attrib_handler(ldb, el->name);
177
178         for (i=0;i<el->num_values;i++) {
179                 if (h->comparison_fn(ldb, ldb, &tree->u.equality.value, 
180                                      &el->values[i]) == 0) {
181                         return 1;
182                 }
183         }
184
185         return 0;
186 }
187
188 static int ldb_wildcard_compare(struct ldb_context *ldb,
189                                 struct ldb_parse_tree *tree,
190                                 const struct ldb_val value)
191 {
192         const struct ldb_attrib_handler *h;
193         struct ldb_val val;
194         struct ldb_val cnk;
195         struct ldb_val *chunk;
196         char *p, *g;
197         char *save_p = NULL;
198         int c = 0;
199
200         h = ldb_attrib_handler(ldb, tree->u.substring.attr);
201
202         if(h->canonicalise_fn(ldb, ldb, &value, &val) != 0)
203                 return -1;
204
205         save_p = val.data;
206         cnk.data = NULL;
207
208         if ( ! tree->u.substring.start_with_wildcard ) {
209
210                 chunk = tree->u.substring.chunks[c];
211                 if(h->canonicalise_fn(ldb, ldb, chunk, &cnk) != 0) goto failed;
212
213                 /* FIXME: case of embedded nulls */
214                 if (strncmp(val.data, cnk.data, cnk.length) != 0) goto failed;
215                 val.length -= cnk.length;
216                 val.data += cnk.length;
217                 c++;
218                 talloc_free(cnk.data);
219                 cnk.data = NULL;
220         }
221
222         while (tree->u.substring.chunks[c]) {
223
224                 chunk = tree->u.substring.chunks[c];
225                 if(h->canonicalise_fn(ldb, ldb, chunk, &cnk) != 0) goto failed;
226
227                 /* FIXME: case of embedded nulls */
228                 p = strstr(val.data, cnk.data);
229                 if (p == NULL) goto failed;
230                 if ( (! tree->u.substring.chunks[c + 1]) && (! tree->u.substring.end_with_wildcard) ) {
231                         do { /* greedy */
232                                 g = strstr(p + cnk.length, cnk.data);
233                                 if (g) p = g;
234                         } while(g);
235                 }
236                 val.length = val.length - (p - (char *)(val.data)) - cnk.length;
237                 val.data = p + cnk.length;
238                 c++;
239                 talloc_free(cnk.data);
240                 cnk.data = NULL;
241         }
242
243         if ( (! tree->u.substring.end_with_wildcard) && (*(val.data) != 0) ) goto failed; /* last chunk have not reached end of string */
244         talloc_free(save_p);
245         return 1;
246
247 failed:
248         talloc_free(save_p);
249         talloc_free(cnk.data);
250         return 0;
251 }
252
253 /*
254   match a simple leaf node
255 */
256 static int ldb_match_substring(struct ldb_context *ldb, 
257                                struct ldb_message *msg,
258                                struct ldb_parse_tree *tree,
259                                enum ldb_scope scope)
260 {
261         unsigned int i;
262         struct ldb_message_element *el;
263
264         el = ldb_msg_find_element(msg, tree->u.substring.attr);
265         if (el == NULL) {
266                 return 0;
267         }
268
269         for (i = 0; i < el->num_values; i++) {
270                 if (ldb_wildcard_compare(ldb, tree, el->values[i]) == 1) {
271                         return 1;
272                 }
273         }
274
275         return 0;
276 }
277
278
279 /*
280   bitwise-and comparator
281 */
282 static int ldb_comparator_and(struct ldb_val *v1, struct ldb_val *v2)
283 {
284         uint64_t i1, i2;
285         i1 = strtoull(v1->data, NULL, 0);
286         i2 = strtoull(v2->data, NULL, 0);
287         return ((i1 & i2) == i2);
288 }
289
290 /*
291   bitwise-or comparator
292 */
293 static int ldb_comparator_or(struct ldb_val *v1, struct ldb_val *v2)
294 {
295         uint64_t i1, i2;
296         i1 = strtoull(v1->data, NULL, 0);
297         i2 = strtoull(v2->data, NULL, 0);
298         return ((i1 & i2) != 0);
299 }
300
301
302 /*
303   extended match, handles things like bitops
304 */
305 static int ldb_match_extended(struct ldb_context *ldb, 
306                               struct ldb_message *msg,
307                               struct ldb_parse_tree *tree,
308                               enum ldb_scope scope)
309 {
310         int i;
311         const struct {
312                 const char *oid;
313                 int (*comparator)(struct ldb_val *, struct ldb_val *);
314         } rules[] = {
315                 { LDB_OID_COMPARATOR_AND, ldb_comparator_and},
316                 { LDB_OID_COMPARATOR_OR, ldb_comparator_or}
317         };
318         int (*comp)(struct ldb_val *, struct ldb_val *) = NULL;
319         struct ldb_message_element *el;
320
321         if (tree->u.extended.dnAttributes) {
322                 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb: dnAttributes extended match not supported yet");
323                 return -1;
324         }
325         if (tree->u.extended.rule_id == NULL) {
326                 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb: no-rule extended matches not supported yet");
327                 return -1;
328         }
329         if (tree->u.extended.attr == NULL) {
330                 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb: no-attribute extended matches not supported yet");
331                 return -1;
332         }
333
334         for (i=0;i<ARRAY_SIZE(rules);i++) {
335                 if (strcmp(rules[i].oid, tree->u.extended.rule_id) == 0) {
336                         comp = rules[i].comparator;
337                         break;
338                 }
339         }
340         if (comp == NULL) {
341                 ldb_debug(ldb, LDB_DEBUG_ERROR, "ldb: unknown extended rule_id %s\n",
342                           tree->u.extended.rule_id);
343                 return -1;
344         }
345
346         /* find the message element */
347         el = ldb_msg_find_element(msg, tree->u.extended.attr);
348         if (el == NULL) {
349                 return 0;
350         }
351
352         for (i=0;i<el->num_values;i++) {
353                 int ret = comp(&el->values[i], &tree->u.extended.value);
354                 if (ret == -1 || ret == 1) return ret;
355         }
356
357         return 0;
358 }
359
360 /*
361   return 0 if the given parse tree matches the given message. Assumes
362   the message is in sorted order
363
364   return 1 if it matches, and 0 if it doesn't match
365
366   this is a recursive function, and does short-circuit evaluation
367  */
368 static int ldb_match_message(struct ldb_context *ldb, 
369                              struct ldb_message *msg,
370                              struct ldb_parse_tree *tree,
371                              enum ldb_scope scope)
372 {
373         unsigned int i;
374         int v;
375
376         switch (tree->operation) {
377         case LDB_OP_AND:
378                 for (i=0;i<tree->u.list.num_elements;i++) {
379                         v = ldb_match_message(ldb, msg, tree->u.list.elements[i], scope);
380                         if (!v) return 0;
381                 }
382                 return 1;
383
384         case LDB_OP_OR:
385                 for (i=0;i<tree->u.list.num_elements;i++) {
386                         v = ldb_match_message(ldb, msg, tree->u.list.elements[i], scope);
387                         if (v) return 1;
388                 }
389                 return 0;
390
391         case LDB_OP_NOT:
392                 return ! ldb_match_message(ldb, msg, tree->u.isnot.child, scope);
393
394         case LDB_OP_EQUALITY:
395                 return ldb_match_equality(ldb, msg, tree, scope);
396
397         case LDB_OP_SUBSTRING:
398                 return ldb_match_substring(ldb, msg, tree, scope);
399
400         case LDB_OP_GREATER:
401                 return ldb_match_comparison(ldb, msg, tree, scope, LDB_OP_GREATER);
402
403         case LDB_OP_LESS:
404                 return ldb_match_comparison(ldb, msg, tree, scope, LDB_OP_LESS);
405
406         case LDB_OP_PRESENT:
407                 return ldb_match_present(ldb, msg, tree, scope);
408
409         case LDB_OP_APPROX:
410                 return ldb_match_comparison(ldb, msg, tree, scope, LDB_OP_APPROX);
411
412         case LDB_OP_EXTENDED:
413                 return ldb_match_extended(ldb, msg, tree, scope);
414
415         }
416
417         return 0;
418 }
419
420 int ldb_match_msg(struct ldb_context *ldb,
421                   struct ldb_message *msg,
422                   struct ldb_parse_tree *tree,
423                   const struct ldb_dn *base,
424                   enum ldb_scope scope)
425 {
426         if ( ! ldb_match_scope(ldb, base, msg->dn, scope) ) {
427                 return 0;
428         }
429
430         return ldb_match_message(ldb, msg, tree, scope);
431 }