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) {
358 ret->operation = LDB_OP_PRESENT;
359 ret->u.present.attr = attr;
362 case LDB_OP_EQUALITY:
364 if (strcmp(value, "*") == 0) {
365 ret->operation = LDB_OP_PRESENT;
366 ret->u.present.attr = attr;
370 if (ldb_parse_find_wildcard(value) != NULL) {
371 ret->operation = LDB_OP_SUBSTRING;
372 ret->u.substring.attr = attr;
373 ret->u.substring.start_with_wildcard = 0;
374 ret->u.substring.end_with_wildcard = 0;
375 ret->u.substring.chunks = ldb_wildcard_decode(ret, value);
376 if (ret->u.substring.chunks == NULL){
381 ret->u.substring.start_with_wildcard = 1;
382 if (value[strlen(value) - 1] == '*')
383 ret->u.substring.end_with_wildcard = 1;
389 ret->operation = LDB_OP_EQUALITY;
390 ret->u.equality.attr = attr;
391 ret->u.equality.value = ldb_binary_decode(ret, value);
392 if (ret->u.equality.value.data == NULL) {
400 ret->operation = LDB_OP_GREATER;
401 ret->u.comparison.attr = attr;
402 ret->u.comparison.value = ldb_binary_decode(ret, value);
403 if (ret->u.comparison.value.data == NULL) {
411 ret->operation = LDB_OP_LESS;
412 ret->u.comparison.attr = attr;
413 ret->u.comparison.value = ldb_binary_decode(ret, value);
414 if (ret->u.comparison.value.data == NULL) {
422 ret->operation = LDB_OP_APPROX;
423 ret->u.comparison.attr = attr;
424 ret->u.comparison.value = ldb_binary_decode(ret, value);
425 if (ret->u.comparison.value.data == NULL) {
432 case LDB_OP_EXTENDED:
434 ret = ldb_parse_extended(ret, attr, value);
448 <and> ::= '&' <filterlist>
449 <or> ::= '|' <filterlist>
450 <filterlist> ::= <filter> | <filter> <filterlist>
452 static struct ldb_parse_tree *ldb_parse_filterlist(void *mem_ctx, const char **s)
454 struct ldb_parse_tree *ret, *next;
455 enum ldb_parse_op op;
470 while (isspace((unsigned char)*p)) p++;
472 ret = talloc(mem_ctx, struct ldb_parse_tree);
479 ret->u.list.num_elements = 1;
480 ret->u.list.elements = talloc(ret, struct ldb_parse_tree *);
481 if (!ret->u.list.elements) {
487 ret->u.list.elements[0] = ldb_parse_filter(ret->u.list.elements, &p);
488 if (!ret->u.list.elements[0]) {
493 while (isspace((unsigned char)*p)) p++;
495 while (*p && (next = ldb_parse_filter(ret->u.list.elements, &p))) {
496 struct ldb_parse_tree **e;
497 e = talloc_realloc(ret, ret->u.list.elements,
498 struct ldb_parse_tree *,
499 ret->u.list.num_elements + 1);
505 ret->u.list.elements = e;
506 ret->u.list.elements[ret->u.list.num_elements] = next;
507 ret->u.list.num_elements++;
508 while (isspace((unsigned char)*p)) p++;
518 <not> ::= '!' <filter>
520 static struct ldb_parse_tree *ldb_parse_not(void *mem_ctx, const char **s)
522 struct ldb_parse_tree *ret;
530 ret = talloc(mem_ctx, struct ldb_parse_tree);
536 ret->operation = LDB_OP_NOT;
537 ret->u.isnot.child = ldb_parse_filter(ret, &p);
538 if (!ret->u.isnot.child) {
550 <filtercomp> ::= <and> | <or> | <not> | <simple>
552 static struct ldb_parse_tree *ldb_parse_filtercomp(void *mem_ctx, const char **s)
554 struct ldb_parse_tree *ret;
557 while (isspace((unsigned char)*p)) p++;
561 ret = ldb_parse_filterlist(mem_ctx, &p);
565 ret = ldb_parse_filterlist(mem_ctx, &p);
569 ret = ldb_parse_not(mem_ctx, &p);
577 ret = ldb_parse_simple(mem_ctx, &p);
587 <filter> ::= '(' <filtercomp> ')'
589 static struct ldb_parse_tree *ldb_parse_filter(void *mem_ctx, const char **s)
591 struct ldb_parse_tree *ret;
599 ret = ldb_parse_filtercomp(mem_ctx, &p);
606 while (isspace((unsigned char)*p)) {
617 main parser entry point. Takes a search string and returns a parse tree
619 expression ::= <simple> | <filter>
621 struct ldb_parse_tree *ldb_parse_tree(void *mem_ctx, const char *s)
623 if (s == NULL || *s == 0) {
624 s = "(|(objectClass=*)(distinguishedName=*))";
627 while (isspace((unsigned char)*s)) s++;
630 return ldb_parse_filter(mem_ctx, &s);
633 return ldb_parse_simple(mem_ctx, &s);
638 construct a ldap parse filter given a parse tree
640 char *ldb_filter_from_tree(void *mem_ctx, struct ldb_parse_tree *tree)
649 switch (tree->operation) {
652 ret = talloc_asprintf(mem_ctx, "(%c", tree->operation==LDB_OP_AND?'&':'|');
653 if (ret == NULL) return NULL;
654 for (i=0;i<tree->u.list.num_elements;i++) {
655 s = ldb_filter_from_tree(mem_ctx, tree->u.list.elements[i]);
660 s2 = talloc_asprintf_append(ret, "%s", s);
668 s = talloc_asprintf_append(ret, ")");
675 s = ldb_filter_from_tree(mem_ctx, tree->u.isnot.child);
676 if (s == NULL) return NULL;
678 ret = talloc_asprintf(mem_ctx, "(!%s)", s);
681 case LDB_OP_EQUALITY:
682 s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
683 if (s == NULL) return NULL;
684 ret = talloc_asprintf(mem_ctx, "(%s=%s)",
685 tree->u.equality.attr, s);
688 case LDB_OP_SUBSTRING:
689 ret = talloc_asprintf(mem_ctx, "(%s=%s", tree->u.substring.attr,
690 tree->u.substring.start_with_wildcard?"*":"");
691 if (ret == NULL) return NULL;
692 for (i = 0; tree->u.substring.chunks[i]; i++) {
693 s2 = ldb_binary_encode(mem_ctx, *(tree->u.substring.chunks[i]));
698 if (tree->u.substring.chunks[i+1] ||
699 tree->u.substring.end_with_wildcard) {
700 s = talloc_asprintf_append(ret, "%s*", s2);
702 s = talloc_asprintf_append(ret, "%s", s2);
710 s = talloc_asprintf_append(ret, ")");
718 s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
719 if (s == NULL) return NULL;
720 ret = talloc_asprintf(mem_ctx, "(%s>=%s)",
721 tree->u.equality.attr, s);
725 s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
726 if (s == NULL) return NULL;
727 ret = talloc_asprintf(mem_ctx, "(%s<=%s)",
728 tree->u.equality.attr, s);
732 ret = talloc_asprintf(mem_ctx, "(%s=*)", tree->u.present.attr);
735 s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
736 if (s == NULL) return NULL;
737 ret = talloc_asprintf(mem_ctx, "(%s~=%s)",
738 tree->u.equality.attr, s);
741 case LDB_OP_EXTENDED:
742 s = ldb_binary_encode(mem_ctx, tree->u.extended.value);
743 if (s == NULL) return NULL;
744 ret = talloc_asprintf(mem_ctx, "(%s%s%s%s:=%s)",
745 tree->u.extended.attr?tree->u.extended.attr:"",
746 tree->u.extended.dnAttributes?":dn":"",
747 tree->u.extended.rule_id?":":"",
748 tree->u.extended.rule_id?tree->u.extended.rule_id:"",
759 replace any occurances of an attribute name in the parse tree with a
762 void ldb_parse_tree_attr_replace(struct ldb_parse_tree *tree,
767 switch (tree->operation) {
770 for (i=0;i<tree->u.list.num_elements;i++) {
771 ldb_parse_tree_attr_replace(tree->u.list.elements[i],
776 ldb_parse_tree_attr_replace(tree->u.isnot.child, attr, replace);
778 case LDB_OP_EQUALITY:
782 if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) {
783 tree->u.equality.attr = replace;
786 case LDB_OP_SUBSTRING:
787 if (ldb_attr_cmp(tree->u.substring.attr, attr) == 0) {
788 tree->u.substring.attr = replace;
792 if (ldb_attr_cmp(tree->u.present.attr, attr) == 0) {
793 tree->u.present.attr = replace;
796 case LDB_OP_EXTENDED:
797 if (tree->u.extended.attr &&
798 ldb_attr_cmp(tree->u.extended.attr, attr) == 0) {
799 tree->u.extended.attr = replace;