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/includes.h"
46 #include "system/locale.h"
49 a filter is defined by:
50 <filter> ::= '(' <filtercomp> ')'
51 <filtercomp> ::= <and> | <or> | <not> | <simple>
52 <and> ::= '&' <filterlist>
53 <or> ::= '|' <filterlist>
54 <not> ::= '!' <filter>
55 <filterlist> ::= <filter> | <filter> <filterlist>
56 <simple> ::= <attributetype> <filtertype> <attributevalue>
57 <filtertype> ::= '=' | '~=' | '<=' | '>='
61 decode a RFC2254 binary string representation of a buffer.
64 struct ldb_val ldb_binary_decode(void *mem_ctx, const char *str)
68 int slen = str?strlen(str):0;
70 ret.data = talloc_size(mem_ctx, slen+1);
72 if (ret.data == NULL) return ret;
74 for (i=j=0;i<slen;i++) {
77 if (sscanf(&str[i+1], "%02X", &c) != 1) {
78 talloc_free(ret.data);
79 memset(&ret, 0, sizeof(ret));
82 ((uint8_t *)ret.data)[j++] = c;
85 ((uint8_t *)ret.data)[j++] = str[i];
89 ((uint8_t *)ret.data)[j] = 0;
96 encode a blob as a RFC2254 binary string, escaping any
97 non-printable or '\' characters
99 char *ldb_binary_encode(void *mem_ctx, struct ldb_val val)
103 int len = val.length;
104 unsigned char *buf = val.data;
106 for (i=0;i<val.length;i++) {
107 if (!isprint(buf[i]) || strchr(" *()\\&|!\"", buf[i])) {
111 ret = talloc_array(mem_ctx, char, len+1);
112 if (ret == NULL) return NULL;
115 for (i=0;i<val.length;i++) {
116 if (!isprint(buf[i]) || strchr(" *()\\&|!\"", buf[i])) {
117 snprintf(ret+len, 4, "\\%02X", buf[i]);
130 encode a string as a RFC2254 binary string, escaping any
131 non-printable or '\' characters. This routine is suitable for use
132 in escaping user data in ldap filters.
134 char *ldb_binary_encode_string(void *mem_ctx, const char *string)
137 val.data = discard_const(string);
138 val.length = strlen(string);
139 return ldb_binary_encode(mem_ctx, val);
142 /* find the first matching wildcard */
143 static char *ldb_parse_find_wildcard(char *value)
146 value = strpbrk(value, "\\*");
147 if (value == NULL) return NULL;
149 if (value[0] == '\\') {
150 if (value[1] == '\0') return NULL;
155 if (value[0] == '*') return value;
161 /* return a NULL terminated list of binary strings representing the value
162 chunks separated by wildcards that makes the value portion of the filter
164 static struct ldb_val **ldb_wildcard_decode(void *mem_ctx, const char *string)
166 struct ldb_val **ret = NULL;
170 wc = talloc_strdup(mem_ctx, string);
171 if (wc == NULL) return NULL;
175 wc = ldb_parse_find_wildcard(str);
185 ret = talloc_realloc(mem_ctx, ret, struct ldb_val *, val + 2);
186 if (ret == NULL) return NULL;
188 ret[val] = talloc(mem_ctx, struct ldb_val);
189 if (ret[val] == NULL) return NULL;
191 *(ret[val]) = ldb_binary_decode(mem_ctx, str);
192 if ((ret[val])->data == NULL) return NULL;
204 static struct ldb_parse_tree *ldb_parse_filter(void *mem_ctx, const char **s);
208 parse an extended match
216 the ':dn' part sets the dnAttributes boolean if present
217 the oid sets the rule_id string
220 static struct ldb_parse_tree *ldb_parse_extended(struct ldb_parse_tree *ret,
221 char *attr, char *value)
225 ret->operation = LDB_OP_EXTENDED;
226 ret->u.extended.value = ldb_binary_decode(ret, value);
227 if (ret->u.extended.value.data == NULL) goto failed;
229 p1 = strchr(attr, ':');
230 if (p1 == NULL) goto failed;
231 p2 = strchr(p1+1, ':');
236 ret->u.extended.attr = attr;
237 if (strcmp(p1+1, "dn") == 0) {
238 ret->u.extended.dnAttributes = 1;
240 ret->u.extended.rule_id = talloc_strdup(ret, p2+1);
241 if (ret->u.extended.rule_id == NULL) goto failed;
243 ret->u.extended.rule_id = NULL;
246 ret->u.extended.dnAttributes = 0;
247 ret->u.extended.rule_id = talloc_strdup(ret, p1+1);
248 if (ret->u.extended.rule_id == NULL) goto failed;
258 static enum ldb_parse_op ldb_parse_filtertype(void *mem_ctx, char **type, char **value, const char **s)
260 enum ldb_parse_op filter = 0;
261 char *name, *val, *k;
265 /* retrieve attributetype name */
268 while ((isascii(*p) && isalnum((unsigned char)*p)) || (*p == '-')) { /* attribute names can only be alphanums */
272 if (*p == ':') { /* but extended searches have : and . chars too */
274 if (p == NULL) { /* malformed attribute name */
281 while (isspace((unsigned char)*p)) p++;
283 if (!strchr("=<>~:", *p)) {
288 name = talloc_memdup(mem_ctx, t, t1 - t + 1);
289 if (name == NULL) return 0;
292 /* retrieve filtertype */
295 filter = LDB_OP_EQUALITY;
296 } else if (*(p + 1) == '=') {
299 filter = LDB_OP_LESS;
303 filter = LDB_OP_GREATER;
307 filter = LDB_OP_APPROX;
311 filter = LDB_OP_EXTENDED;
322 while (isspace((unsigned char)*p)) p++;
327 while (*p && ((*p != ')') || ((*p == ')') && (*(p - 1) == '\\')))) p++;
329 val = talloc_memdup(mem_ctx, t, p - t + 1);
338 /* remove trailing spaces from value */
339 while ((k > val) && (isspace((unsigned char)*(k - 1)))) k--;
349 <simple> ::= <attributetype> <filtertype> <attributevalue>
351 static struct ldb_parse_tree *ldb_parse_simple(void *mem_ctx, const char **s)
354 struct ldb_parse_tree *ret;
355 enum ldb_parse_op filtertype;
357 ret = talloc(mem_ctx, struct ldb_parse_tree);
363 filtertype = ldb_parse_filtertype(ret, &attr, &value, s);
369 switch (filtertype) {
372 ret->operation = LDB_OP_PRESENT;
373 ret->u.present.attr = attr;
376 case LDB_OP_EQUALITY:
378 if (strcmp(value, "*") == 0) {
379 ret->operation = LDB_OP_PRESENT;
380 ret->u.present.attr = attr;
384 if (ldb_parse_find_wildcard(value) != NULL) {
385 ret->operation = LDB_OP_SUBSTRING;
386 ret->u.substring.attr = attr;
387 ret->u.substring.start_with_wildcard = 0;
388 ret->u.substring.end_with_wildcard = 0;
389 ret->u.substring.chunks = ldb_wildcard_decode(ret, value);
390 if (ret->u.substring.chunks == NULL){
395 ret->u.substring.start_with_wildcard = 1;
396 if (value[strlen(value) - 1] == '*')
397 ret->u.substring.end_with_wildcard = 1;
403 ret->operation = LDB_OP_EQUALITY;
404 ret->u.equality.attr = attr;
405 ret->u.equality.value = ldb_binary_decode(ret, value);
406 if (ret->u.equality.value.data == NULL) {
414 ret->operation = LDB_OP_GREATER;
415 ret->u.comparison.attr = attr;
416 ret->u.comparison.value = ldb_binary_decode(ret, value);
417 if (ret->u.comparison.value.data == NULL) {
425 ret->operation = LDB_OP_LESS;
426 ret->u.comparison.attr = attr;
427 ret->u.comparison.value = ldb_binary_decode(ret, value);
428 if (ret->u.comparison.value.data == NULL) {
436 ret->operation = LDB_OP_APPROX;
437 ret->u.comparison.attr = attr;
438 ret->u.comparison.value = ldb_binary_decode(ret, value);
439 if (ret->u.comparison.value.data == NULL) {
446 case LDB_OP_EXTENDED:
448 ret = ldb_parse_extended(ret, attr, value);
462 <and> ::= '&' <filterlist>
463 <or> ::= '|' <filterlist>
464 <filterlist> ::= <filter> | <filter> <filterlist>
466 static struct ldb_parse_tree *ldb_parse_filterlist(void *mem_ctx, const char **s)
468 struct ldb_parse_tree *ret, *next;
469 enum ldb_parse_op op;
484 while (isspace((unsigned char)*p)) p++;
486 ret = talloc(mem_ctx, struct ldb_parse_tree);
493 ret->u.list.num_elements = 1;
494 ret->u.list.elements = talloc(ret, struct ldb_parse_tree *);
495 if (!ret->u.list.elements) {
501 ret->u.list.elements[0] = ldb_parse_filter(ret->u.list.elements, &p);
502 if (!ret->u.list.elements[0]) {
507 while (isspace((unsigned char)*p)) p++;
509 while (*p && (next = ldb_parse_filter(ret->u.list.elements, &p))) {
510 struct ldb_parse_tree **e;
511 e = talloc_realloc(ret, ret->u.list.elements,
512 struct ldb_parse_tree *,
513 ret->u.list.num_elements + 1);
519 ret->u.list.elements = e;
520 ret->u.list.elements[ret->u.list.num_elements] = next;
521 ret->u.list.num_elements++;
522 while (isspace((unsigned char)*p)) p++;
532 <not> ::= '!' <filter>
534 static struct ldb_parse_tree *ldb_parse_not(void *mem_ctx, const char **s)
536 struct ldb_parse_tree *ret;
544 ret = talloc(mem_ctx, struct ldb_parse_tree);
550 ret->operation = LDB_OP_NOT;
551 ret->u.isnot.child = ldb_parse_filter(ret, &p);
552 if (!ret->u.isnot.child) {
564 <filtercomp> ::= <and> | <or> | <not> | <simple>
566 static struct ldb_parse_tree *ldb_parse_filtercomp(void *mem_ctx, const char **s)
568 struct ldb_parse_tree *ret;
571 while (isspace((unsigned char)*p)) p++;
575 ret = ldb_parse_filterlist(mem_ctx, &p);
579 ret = ldb_parse_filterlist(mem_ctx, &p);
583 ret = ldb_parse_not(mem_ctx, &p);
591 ret = ldb_parse_simple(mem_ctx, &p);
601 <filter> ::= '(' <filtercomp> ')'
603 static struct ldb_parse_tree *ldb_parse_filter(void *mem_ctx, const char **s)
605 struct ldb_parse_tree *ret;
613 ret = ldb_parse_filtercomp(mem_ctx, &p);
620 while (isspace((unsigned char)*p)) {
631 main parser entry point. Takes a search string and returns a parse tree
633 expression ::= <simple> | <filter>
635 struct ldb_parse_tree *ldb_parse_tree(void *mem_ctx, const char *s)
637 if (s == NULL || *s == 0) {
638 s = "(|(objectClass=*)(distinguishedName=*))";
641 while (isspace((unsigned char)*s)) s++;
644 return ldb_parse_filter(mem_ctx, &s);
647 return ldb_parse_simple(mem_ctx, &s);
652 construct a ldap parse filter given a parse tree
654 char *ldb_filter_from_tree(void *mem_ctx, struct ldb_parse_tree *tree)
663 switch (tree->operation) {
666 ret = talloc_asprintf(mem_ctx, "(%c", tree->operation==LDB_OP_AND?'&':'|');
667 if (ret == NULL) return NULL;
668 for (i=0;i<tree->u.list.num_elements;i++) {
669 s = ldb_filter_from_tree(mem_ctx, tree->u.list.elements[i]);
674 s2 = talloc_asprintf_append(ret, "%s", s);
682 s = talloc_asprintf_append(ret, ")");
689 s = ldb_filter_from_tree(mem_ctx, tree->u.isnot.child);
690 if (s == NULL) return NULL;
692 ret = talloc_asprintf(mem_ctx, "(!%s)", s);
695 case LDB_OP_EQUALITY:
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);
702 case LDB_OP_SUBSTRING:
703 ret = talloc_asprintf(mem_ctx, "(%s=%s", tree->u.substring.attr,
704 tree->u.substring.start_with_wildcard?"*":"");
705 if (ret == NULL) return NULL;
706 for (i = 0; tree->u.substring.chunks[i]; i++) {
707 s2 = ldb_binary_encode(mem_ctx, *(tree->u.substring.chunks[i]));
712 if (tree->u.substring.chunks[i+1] ||
713 tree->u.substring.end_with_wildcard) {
714 s = talloc_asprintf_append(ret, "%s*", s2);
716 s = talloc_asprintf_append(ret, "%s", s2);
724 s = talloc_asprintf_append(ret, ")");
732 s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
733 if (s == NULL) return NULL;
734 ret = talloc_asprintf(mem_ctx, "(%s>=%s)",
735 tree->u.equality.attr, s);
739 s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
740 if (s == NULL) return NULL;
741 ret = talloc_asprintf(mem_ctx, "(%s<=%s)",
742 tree->u.equality.attr, s);
746 ret = talloc_asprintf(mem_ctx, "(%s=*)", tree->u.present.attr);
749 s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
750 if (s == NULL) return NULL;
751 ret = talloc_asprintf(mem_ctx, "(%s~=%s)",
752 tree->u.equality.attr, s);
755 case LDB_OP_EXTENDED:
756 s = ldb_binary_encode(mem_ctx, tree->u.extended.value);
757 if (s == NULL) return NULL;
758 ret = talloc_asprintf(mem_ctx, "(%s%s%s%s:=%s)",
759 tree->u.extended.attr?tree->u.extended.attr:"",
760 tree->u.extended.dnAttributes?":dn":"",
761 tree->u.extended.rule_id?":":"",
762 tree->u.extended.rule_id?tree->u.extended.rule_id:"",
773 replace any occurances of an attribute name in the parse tree with a
776 void ldb_parse_tree_attr_replace(struct ldb_parse_tree *tree,
781 switch (tree->operation) {
784 for (i=0;i<tree->u.list.num_elements;i++) {
785 ldb_parse_tree_attr_replace(tree->u.list.elements[i],
790 ldb_parse_tree_attr_replace(tree->u.isnot.child, attr, replace);
792 case LDB_OP_EQUALITY:
796 if (ldb_attr_cmp(tree->u.equality.attr, attr) == 0) {
797 tree->u.equality.attr = replace;
800 case LDB_OP_SUBSTRING:
801 if (ldb_attr_cmp(tree->u.substring.attr, attr) == 0) {
802 tree->u.substring.attr = replace;
806 if (ldb_attr_cmp(tree->u.present.attr, attr) == 0) {
807 tree->u.present.attr = replace;
810 case LDB_OP_EXTENDED:
811 if (tree->u.extended.attr &&
812 ldb_attr_cmp(tree->u.extended.attr, attr) == 0) {
813 tree->u.extended.attr = replace;