4 Copyright (C) Andrew Tridgell 2004
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: ldb expression parsing
30 * Description: parse LDAP-like search expressions
32 * Author: Andrew Tridgell
37 - add RFC2254 binary string handling
38 - possibly add ~=, <= and >= handling
39 - expand the test suite
40 - add better parse error handling
45 #include "ldb/include/ldb.h"
50 a filter is defined by:
51 <filter> ::= '(' <filtercomp> ')'
52 <filtercomp> ::= <and> | <or> | <not> | <simple>
53 <and> ::= '&' <filterlist>
54 <or> ::= '|' <filterlist>
55 <not> ::= '!' <filter>
56 <filterlist> ::= <filter> | <filter> <filterlist>
57 <simple> ::= <attributetype> <filtertype> <attributevalue>
58 <filtertype> ::= '=' | '~=' | '<=' | '>='
62 decode a RFC2254 binary string representation of a buffer.
65 struct ldb_val ldb_binary_decode(void *mem_ctx, const char *str)
69 int slen = str?strlen(str):0;
71 ret.data = talloc_size(mem_ctx, slen+1);
73 if (ret.data == NULL) return ret;
75 for (i=j=0;i<slen;i++) {
78 if (sscanf(&str[i+1], "%02X", &c) != 1) {
79 talloc_free(ret.data);
80 memset(&ret, 0, sizeof(ret));
83 ((uint8_t *)ret.data)[j++] = c;
86 ((uint8_t *)ret.data)[j++] = str[i];
90 ((uint8_t *)ret.data)[j] = 0;
97 encode a blob as a RFC2254 binary string, escaping any
98 non-printable or '\' characters
100 char *ldb_binary_encode(void *mem_ctx, struct ldb_val val)
104 int len = val.length;
105 unsigned char *buf = val.data;
107 for (i=0;i<val.length;i++) {
108 if (!isprint(buf[i]) || strchr(" *()\\&|!\"", buf[i])) {
112 ret = talloc_array(mem_ctx, char, len+1);
113 if (ret == NULL) return NULL;
116 for (i=0;i<val.length;i++) {
117 if (!isprint(buf[i]) || strchr(" *()\\&|!\"", buf[i])) {
118 snprintf(ret+len, 4, "\\%02X", buf[i]);
130 /* find the first matching wildcard */
131 static char *ldb_parse_find_wildcard(char *value)
134 value = strpbrk(value, "\\*");
135 if (value == NULL) return NULL;
137 if (value[0] == '\\') {
138 if (value[1] == '\0') return NULL;
143 if (value[0] == '*') return value;
149 /* return a NULL terminated list of binary strings representing the value
150 chunks separated by wildcards that makes the value portion of the filter
152 static struct ldb_val **ldb_wildcard_decode(void *mem_ctx, const char *string)
154 struct ldb_val **ret = NULL;
158 wc = talloc_strdup(mem_ctx, string);
159 if (wc == NULL) return NULL;
163 wc = ldb_parse_find_wildcard(str);
173 ret = talloc_realloc(mem_ctx, ret, struct ldb_val *, val + 2);
174 if (ret == NULL) return NULL;
176 ret[val] = talloc(mem_ctx, struct ldb_val);
177 if (ret[val] == NULL) return NULL;
179 *(ret[val]) = ldb_binary_decode(mem_ctx, str);
180 if ((ret[val])->data == NULL) return NULL;
190 static struct ldb_parse_tree *ldb_parse_filter(void *mem_ctx, const char **s);
194 parse an extended match
202 the ':dn' part sets the dnAttributes boolean if present
203 the oid sets the rule_id string
206 static struct ldb_parse_tree *ldb_parse_extended(struct ldb_parse_tree *ret,
207 char *attr, char *value)
211 ret->operation = LDB_OP_EXTENDED;
212 ret->u.extended.value = ldb_binary_decode(ret, value);
213 if (ret->u.extended.value.data == NULL) goto failed;
215 p1 = strchr(attr, ':');
216 if (p1 == NULL) goto failed;
217 p2 = strchr(p1+1, ':');
222 ret->u.extended.attr = attr;
223 if (strcmp(p1+1, "dn") == 0) {
224 ret->u.extended.dnAttributes = 1;
226 ret->u.extended.rule_id = talloc_strdup(ret, p2+1);
227 if (ret->u.extended.rule_id == NULL) goto failed;
229 ret->u.extended.rule_id = NULL;
232 ret->u.extended.dnAttributes = 0;
233 ret->u.extended.rule_id = talloc_strdup(ret, p1+1);
234 if (ret->u.extended.rule_id == NULL) goto failed;
244 static enum ldb_parse_op ldb_parse_filtertype(void *mem_ctx, char **type, char **value, const char **s)
246 enum ldb_parse_op filter = 0;
247 char *name, *val, *k;
251 /* retrieve attributetype name */
254 while ((isascii(*p) && isalnum((unsigned char)*p)) || (*p == '-')) { /* attribute names can only be alphanums */
258 if (*p == ':') { /* but extended searches have : and . chars too */
260 if (p == NULL) { /* malformed attribute name */
267 while (isspace((unsigned char)*p)) p++;
269 if (!strchr("=<>~:", *p)) {
274 name = talloc_memdup(mem_ctx, t, t1 - t + 1);
275 if (name == NULL) return 0;
278 /* retrieve filtertype */
281 filter = LDB_OP_EQUALITY;
282 } else if (*(p + 1) == '=') {
285 filter = LDB_OP_LESS;
289 filter = LDB_OP_GREATER;
293 filter = LDB_OP_APPROX;
297 filter = LDB_OP_EXTENDED;
308 while (isspace((unsigned char)*p)) p++;
313 while (*p && ((*p != ')') || ((*p == ')') && (*(p - 1) == '\\')))) p++;
315 val = talloc_memdup(mem_ctx, t, p - t + 1);
324 /* remove trailing spaces from value */
325 while ((k > val) && (isspace((unsigned char)*(k - 1)))) k--;
335 <simple> ::= <attributetype> <filtertype> <attributevalue>
337 static struct ldb_parse_tree *ldb_parse_simple(void *mem_ctx, const char **s)
340 struct ldb_parse_tree *ret;
341 enum ldb_parse_op filtertype;
343 ret = talloc(mem_ctx, struct ldb_parse_tree);
349 filtertype = ldb_parse_filtertype(ret, &attr, &value, s);
355 switch (filtertype) {
357 case LDB_OP_EQUALITY:
359 if (strcmp(value, "*") == 0) {
360 ret->operation = LDB_OP_PRESENT;
361 ret->u.present.attr = attr;
365 if (ldb_parse_find_wildcard(value) != NULL) {
366 ret->operation = LDB_OP_SUBSTRING;
367 ret->u.substring.attr = attr;
368 ret->u.substring.start_with_wildcard = 0;
369 ret->u.substring.end_with_wildcard = 0;
370 ret->u.substring.chunks = ldb_wildcard_decode(ret, value);
371 if (ret->u.substring.chunks == NULL){
376 ret->u.substring.start_with_wildcard = 1;
377 if (value[strlen(value) - 1] == '*')
378 ret->u.substring.end_with_wildcard = 1;
384 ret->operation = LDB_OP_EQUALITY;
385 ret->u.equality.attr = attr;
386 ret->u.equality.value = ldb_binary_decode(ret, value);
387 if (ret->u.equality.value.data == NULL) {
395 ret->operation = LDB_OP_GREATER;
396 ret->u.comparison.attr = attr;
397 ret->u.comparison.value = ldb_binary_decode(ret, value);
398 if (ret->u.comparison.value.data == NULL) {
406 ret->operation = LDB_OP_LESS;
407 ret->u.comparison.attr = attr;
408 ret->u.comparison.value = ldb_binary_decode(ret, value);
409 if (ret->u.comparison.value.data == NULL) {
417 ret->operation = LDB_OP_APPROX;
418 ret->u.comparison.attr = attr;
419 ret->u.comparison.value = ldb_binary_decode(ret, value);
420 if (ret->u.comparison.value.data == NULL) {
427 case LDB_OP_EXTENDED:
429 ret = ldb_parse_extended(ret, attr, value);
443 <and> ::= '&' <filterlist>
444 <or> ::= '|' <filterlist>
445 <filterlist> ::= <filter> | <filter> <filterlist>
447 static struct ldb_parse_tree *ldb_parse_filterlist(void *mem_ctx, const char **s)
449 struct ldb_parse_tree *ret, *next;
450 enum ldb_parse_op op;
465 while (isspace((unsigned char)*p)) p++;
467 ret = talloc(mem_ctx, struct ldb_parse_tree);
474 ret->u.list.num_elements = 1;
475 ret->u.list.elements = talloc(ret, struct ldb_parse_tree *);
476 if (!ret->u.list.elements) {
482 ret->u.list.elements[0] = ldb_parse_filter(ret->u.list.elements, &p);
483 if (!ret->u.list.elements[0]) {
488 while (isspace((unsigned char)*p)) p++;
490 while (*p && (next = ldb_parse_filter(ret->u.list.elements, &p))) {
491 struct ldb_parse_tree **e;
492 e = talloc_realloc(ret, ret->u.list.elements,
493 struct ldb_parse_tree *,
494 ret->u.list.num_elements + 1);
500 ret->u.list.elements = e;
501 ret->u.list.elements[ret->u.list.num_elements] = next;
502 ret->u.list.num_elements++;
503 while (isspace((unsigned char)*p)) p++;
513 <not> ::= '!' <filter>
515 static struct ldb_parse_tree *ldb_parse_not(void *mem_ctx, const char **s)
517 struct ldb_parse_tree *ret;
525 ret = talloc(mem_ctx, struct ldb_parse_tree);
531 ret->operation = LDB_OP_NOT;
532 ret->u.isnot.child = ldb_parse_filter(ret, &p);
533 if (!ret->u.isnot.child) {
545 <filtercomp> ::= <and> | <or> | <not> | <simple>
547 static struct ldb_parse_tree *ldb_parse_filtercomp(void *mem_ctx, const char **s)
549 struct ldb_parse_tree *ret;
552 while (isspace((unsigned char)*p)) p++;
556 ret = ldb_parse_filterlist(mem_ctx, &p);
560 ret = ldb_parse_filterlist(mem_ctx, &p);
564 ret = ldb_parse_not(mem_ctx, &p);
572 ret = ldb_parse_simple(mem_ctx, &p);
582 <filter> ::= '(' <filtercomp> ')'
584 static struct ldb_parse_tree *ldb_parse_filter(void *mem_ctx, const char **s)
586 struct ldb_parse_tree *ret;
594 ret = ldb_parse_filtercomp(mem_ctx, &p);
601 while (isspace((unsigned char)*p)) {
612 main parser entry point. Takes a search string and returns a parse tree
614 expression ::= <simple> | <filter>
616 struct ldb_parse_tree *ldb_parse_tree(void *mem_ctx, const char *s)
618 while (isspace((unsigned char)*s)) s++;
621 return ldb_parse_filter(mem_ctx, &s);
624 return ldb_parse_simple(mem_ctx, &s);
629 construct a ldap parse filter given a parse tree
631 char *ldb_filter_from_tree(void *mem_ctx, struct ldb_parse_tree *tree)
636 switch (tree->operation) {
639 ret = talloc_asprintf(mem_ctx, "(%c", (char)tree->operation);
640 if (ret == NULL) return NULL;
641 for (i=0;i<tree->u.list.num_elements;i++) {
642 s = ldb_filter_from_tree(mem_ctx, tree->u.list.elements[i]);
647 s2 = talloc_asprintf_append(ret, "%s", s);
655 s = talloc_asprintf_append(ret, ")");
662 s = ldb_filter_from_tree(mem_ctx, tree->u.isnot.child);
663 if (s == NULL) return NULL;
665 ret = talloc_asprintf(mem_ctx, "(!%s)", s);
668 case LDB_OP_EQUALITY:
669 s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
670 if (s == NULL) return NULL;
671 ret = talloc_asprintf(mem_ctx, "(%s=%s)",
672 tree->u.equality.attr, s);
675 case LDB_OP_SUBSTRING:
676 ret = talloc_strdup(mem_ctx, (tree->u.substring.start_with_wildcard)?"*":"");
677 if (ret == NULL) return NULL;
678 for (i = 0; tree->u.substring.chunks[i]; i++) {
679 s2 = ldb_binary_encode(mem_ctx, *(tree->u.substring.chunks[i]));
684 s = talloc_asprintf_append(ret, "%s*", s2);
691 if ( ! tree->u.substring.end_with_wildcard ) {
692 ret[strlen(ret) - 1] = '\0'; /* remove last wildcard */
696 s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
697 if (s == NULL) return NULL;
698 ret = talloc_asprintf(mem_ctx, "(%s>=%s)",
699 tree->u.equality.attr, s);
703 s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
704 if (s == NULL) return NULL;
705 ret = talloc_asprintf(mem_ctx, "(%s<=%s)",
706 tree->u.equality.attr, s);
710 ret = talloc_strdup(mem_ctx, "*");
711 if (ret == NULL) return NULL;
714 s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
715 if (s == NULL) return NULL;
716 ret = talloc_asprintf(mem_ctx, "(%s~=%s)",
717 tree->u.equality.attr, s);
720 case LDB_OP_EXTENDED:
721 s = ldb_binary_encode(mem_ctx, tree->u.extended.value);
722 if (s == NULL) return NULL;
723 ret = talloc_asprintf(mem_ctx, "(%s%s%s%s:=%s)",
724 tree->u.extended.attr?tree->u.extended.attr:"",
725 tree->u.extended.dnAttributes?":dn":"",
726 tree->u.extended.rule_id?":":"",
727 tree->u.extended.rule_id?tree->u.extended.rule_id:"",