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