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