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