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 3 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, see <http://www.gnu.org/licenses/>.
27 * Component: ldb expression parsing
29 * Description: parse LDAP-like search expressions
31 * Author: Andrew Tridgell
36 - add RFC2254 binary string handling
37 - possibly add ~=, <= and >= handling
38 - expand the test suite
39 - add better parse error handling
43 #include "ldb_private.h"
44 #include "system/locale.h"
46 static int ldb_parse_hex2char(const char *x
)
48 if (isxdigit(x
[0]) && isxdigit(x
[1])) {
49 const char h1
= x
[0], h2
= x
[1];
52 if (h1
>= 'a') c
= h1
- (int)'a' + 10;
53 else if (h1
>= 'A') c
= h1
- (int)'A' + 10;
54 else if (h1
>= '0') c
= h1
- (int)'0';
56 if (h2
>= 'a') c
+= h2
- (int)'a' + 10;
57 else if (h2
>= 'A') c
+= h2
- (int)'A' + 10;
58 else if (h2
>= '0') c
+= h2
- (int)'0';
67 a filter is defined by:
68 <filter> ::= '(' <filtercomp> ')'
69 <filtercomp> ::= <and> | <or> | <not> | <simple>
70 <and> ::= '&' <filterlist>
71 <or> ::= '|' <filterlist>
72 <not> ::= '!' <filter>
73 <filterlist> ::= <filter> | <filter> <filterlist>
74 <simple> ::= <attributetype> <filtertype> <attributevalue>
75 <filtertype> ::= '=' | '~=' | '<=' | '>='
79 decode a RFC2254 binary string representation of a buffer.
82 struct ldb_val
ldb_binary_decode(TALLOC_CTX
*mem_ctx
, const char *str
)
86 size_t slen
= str
?strlen(str
):0;
88 ret
.data
= (uint8_t *)talloc_size(mem_ctx
, slen
+1);
90 if (ret
.data
== NULL
) return ret
;
92 for (i
=j
=0;i
<slen
;i
++) {
96 c
= ldb_parse_hex2char(&str
[i
+1]);
98 talloc_free(ret
.data
);
99 memset(&ret
, 0, sizeof(ret
));
102 ((uint8_t *)ret
.data
)[j
++] = c
;
105 ((uint8_t *)ret
.data
)[j
++] = str
[i
];
109 ((uint8_t *)ret
.data
)[j
] = 0;
114 static bool need_encode(unsigned char cval
)
116 if (cval
< 0x20 || cval
> 0x7E || strchr(" *()\\&|!\"", cval
)) {
123 encode a blob as a RFC2254 binary string, escaping any
124 non-printable or '\' characters
126 char *ldb_binary_encode(TALLOC_CTX
*mem_ctx
, struct ldb_val val
)
130 size_t len
= val
.length
;
131 unsigned char *buf
= val
.data
;
133 for (i
=0;i
<val
.length
;i
++) {
134 if (need_encode(buf
[i
])) {
138 ret
= talloc_array(mem_ctx
, char, len
+1);
139 if (ret
== NULL
) return NULL
;
142 for (i
=0;i
<val
.length
;i
++) {
143 if (need_encode(buf
[i
])) {
144 snprintf(ret
+len
, 4, "\\%02X", buf
[i
]);
157 encode a string as a RFC2254 binary string, escaping any
158 non-printable or '\' characters. This routine is suitable for use
159 in escaping user data in ldap filters.
161 char *ldb_binary_encode_string(TALLOC_CTX
*mem_ctx
, const char *string
)
164 if (string
== NULL
) {
167 val
.data
= discard_const_p(uint8_t, string
);
168 val
.length
= strlen(string
);
169 return ldb_binary_encode(mem_ctx
, val
);
172 /* find the first matching wildcard */
173 static char *ldb_parse_find_wildcard(char *value
)
176 value
= strpbrk(value
, "\\*");
177 if (value
== NULL
) return NULL
;
179 if (value
[0] == '\\') {
180 if (value
[1] == '\0') return NULL
;
185 if (value
[0] == '*') return value
;
191 /* return a NULL terminated list of binary strings representing the value
192 chunks separated by wildcards that makes the value portion of the filter
194 static struct ldb_val
**ldb_wildcard_decode(TALLOC_CTX
*mem_ctx
, const char *string
)
196 struct ldb_val
**ret
= NULL
;
197 unsigned int val
= 0;
200 wc
= talloc_strdup(mem_ctx
, string
);
201 if (wc
== NULL
) return NULL
;
205 wc
= ldb_parse_find_wildcard(str
);
215 ret
= talloc_realloc(mem_ctx
, ret
, struct ldb_val
*, val
+ 2);
216 if (ret
== NULL
) return NULL
;
218 ret
[val
] = talloc(mem_ctx
, struct ldb_val
);
219 if (ret
[val
] == NULL
) return NULL
;
221 *(ret
[val
]) = ldb_binary_decode(mem_ctx
, str
);
222 if ((ret
[val
])->data
== NULL
) return NULL
;
234 static struct ldb_parse_tree
*ldb_parse_filter(TALLOC_CTX
*mem_ctx
, const char **s
);
238 parse an extended match
246 the ':dn' part sets the dnAttributes boolean if present
247 the oid sets the rule_id string
250 static struct ldb_parse_tree
*ldb_parse_extended(struct ldb_parse_tree
*ret
,
251 char *attr
, char *value
)
255 ret
->operation
= LDB_OP_EXTENDED
;
256 ret
->u
.extended
.value
= ldb_binary_decode(ret
, value
);
257 if (ret
->u
.extended
.value
.data
== NULL
) goto failed
;
259 p1
= strchr(attr
, ':');
260 if (p1
== NULL
) goto failed
;
261 p2
= strchr(p1
+1, ':');
266 ret
->u
.extended
.attr
= attr
;
267 if (strcmp(p1
+1, "dn") == 0) {
268 ret
->u
.extended
.dnAttributes
= 1;
270 ret
->u
.extended
.rule_id
= talloc_strdup(ret
, p2
+1);
271 if (ret
->u
.extended
.rule_id
== NULL
) goto failed
;
273 ret
->u
.extended
.rule_id
= NULL
;
276 ret
->u
.extended
.dnAttributes
= 0;
277 ret
->u
.extended
.rule_id
= talloc_strdup(ret
, p1
+1);
278 if (ret
->u
.extended
.rule_id
== NULL
) goto failed
;
288 static enum ldb_parse_op
ldb_parse_filtertype(TALLOC_CTX
*mem_ctx
, char **type
, char **value
, const char **s
)
290 enum ldb_parse_op filter
= 0;
291 char *name
, *val
, *k
;
295 /* retrieve attributetype name */
298 if (*p
== '@') { /* for internal attributes the first char can be @ */
302 while ((isascii(*p
) && isalnum((unsigned char)*p
)) || (*p
== '-') || (*p
== '.')) {
303 /* attribute names can only be alphanums */
307 if (*p
== ':') { /* but extended searches have : and . chars too */
309 if (p
== NULL
) { /* malformed attribute name */
316 while (isspace((unsigned char)*p
)) p
++;
318 if (!strchr("=<>~:", *p
)) {
323 name
= (char *)talloc_memdup(mem_ctx
, t
, t1
- t
+ 1);
324 if (name
== NULL
) return 0;
327 /* retrieve filtertype */
330 filter
= LDB_OP_EQUALITY
;
331 } else if (*(p
+ 1) == '=') {
334 filter
= LDB_OP_LESS
;
338 filter
= LDB_OP_GREATER
;
342 filter
= LDB_OP_APPROX
;
346 filter
= LDB_OP_EXTENDED
;
357 while (isspace((unsigned char)*p
)) p
++;
362 while (*p
&& ((*p
!= ')') || ((*p
== ')') && (*(p
- 1) == '\\')))) p
++;
364 val
= (char *)talloc_memdup(mem_ctx
, t
, p
- t
+ 1);
373 /* remove trailing spaces from value */
374 while ((k
> val
) && (isspace((unsigned char)*(k
- 1)))) k
--;
384 <simple> ::= <attributetype> <filtertype> <attributevalue>
386 static struct ldb_parse_tree
*ldb_parse_simple(TALLOC_CTX
*mem_ctx
, const char **s
)
389 struct ldb_parse_tree
*ret
;
390 enum ldb_parse_op filtertype
;
392 ret
= talloc(mem_ctx
, struct ldb_parse_tree
);
398 filtertype
= ldb_parse_filtertype(ret
, &attr
, &value
, s
);
404 switch (filtertype
) {
407 ret
->operation
= LDB_OP_PRESENT
;
408 ret
->u
.present
.attr
= attr
;
411 case LDB_OP_EQUALITY
:
413 if (strcmp(value
, "*") == 0) {
414 ret
->operation
= LDB_OP_PRESENT
;
415 ret
->u
.present
.attr
= attr
;
419 if (ldb_parse_find_wildcard(value
) != NULL
) {
420 ret
->operation
= LDB_OP_SUBSTRING
;
421 ret
->u
.substring
.attr
= attr
;
422 ret
->u
.substring
.start_with_wildcard
= 0;
423 ret
->u
.substring
.end_with_wildcard
= 0;
424 ret
->u
.substring
.chunks
= ldb_wildcard_decode(ret
, value
);
425 if (ret
->u
.substring
.chunks
== NULL
){
430 ret
->u
.substring
.start_with_wildcard
= 1;
431 if (value
[strlen(value
) - 1] == '*')
432 ret
->u
.substring
.end_with_wildcard
= 1;
438 ret
->operation
= LDB_OP_EQUALITY
;
439 ret
->u
.equality
.attr
= attr
;
440 ret
->u
.equality
.value
= ldb_binary_decode(ret
, value
);
441 if (ret
->u
.equality
.value
.data
== NULL
) {
449 ret
->operation
= LDB_OP_GREATER
;
450 ret
->u
.comparison
.attr
= attr
;
451 ret
->u
.comparison
.value
= ldb_binary_decode(ret
, value
);
452 if (ret
->u
.comparison
.value
.data
== NULL
) {
460 ret
->operation
= LDB_OP_LESS
;
461 ret
->u
.comparison
.attr
= attr
;
462 ret
->u
.comparison
.value
= ldb_binary_decode(ret
, value
);
463 if (ret
->u
.comparison
.value
.data
== NULL
) {
471 ret
->operation
= LDB_OP_APPROX
;
472 ret
->u
.comparison
.attr
= attr
;
473 ret
->u
.comparison
.value
= ldb_binary_decode(ret
, value
);
474 if (ret
->u
.comparison
.value
.data
== NULL
) {
481 case LDB_OP_EXTENDED
:
483 ret
= ldb_parse_extended(ret
, attr
, value
);
497 <and> ::= '&' <filterlist>
498 <or> ::= '|' <filterlist>
499 <filterlist> ::= <filter> | <filter> <filterlist>
501 static struct ldb_parse_tree
*ldb_parse_filterlist(TALLOC_CTX
*mem_ctx
, const char **s
)
503 struct ldb_parse_tree
*ret
, *next
;
504 enum ldb_parse_op op
;
519 while (isspace((unsigned char)*p
)) p
++;
521 ret
= talloc(mem_ctx
, struct ldb_parse_tree
);
528 ret
->u
.list
.num_elements
= 1;
529 ret
->u
.list
.elements
= talloc(ret
, struct ldb_parse_tree
*);
530 if (!ret
->u
.list
.elements
) {
536 ret
->u
.list
.elements
[0] = ldb_parse_filter(ret
->u
.list
.elements
, &p
);
537 if (!ret
->u
.list
.elements
[0]) {
542 while (isspace((unsigned char)*p
)) p
++;
545 struct ldb_parse_tree
**e
;
550 next
= ldb_parse_filter(ret
->u
.list
.elements
, &p
);
552 /* an invalid filter element */
556 e
= talloc_realloc(ret
, ret
->u
.list
.elements
,
557 struct ldb_parse_tree
*,
558 ret
->u
.list
.num_elements
+ 1);
564 ret
->u
.list
.elements
= e
;
565 ret
->u
.list
.elements
[ret
->u
.list
.num_elements
] = next
;
566 ret
->u
.list
.num_elements
++;
567 while (isspace((unsigned char)*p
)) p
++;
577 <not> ::= '!' <filter>
579 static struct ldb_parse_tree
*ldb_parse_not(TALLOC_CTX
*mem_ctx
, const char **s
)
581 struct ldb_parse_tree
*ret
;
589 ret
= talloc(mem_ctx
, struct ldb_parse_tree
);
595 ret
->operation
= LDB_OP_NOT
;
596 ret
->u
.isnot
.child
= ldb_parse_filter(ret
, &p
);
597 if (!ret
->u
.isnot
.child
) {
609 <filtercomp> ::= <and> | <or> | <not> | <simple>
611 static struct ldb_parse_tree
*ldb_parse_filtercomp(TALLOC_CTX
*mem_ctx
, const char **s
)
613 struct ldb_parse_tree
*ret
;
616 while (isspace((unsigned char)*p
)) p
++;
620 ret
= ldb_parse_filterlist(mem_ctx
, &p
);
624 ret
= ldb_parse_filterlist(mem_ctx
, &p
);
628 ret
= ldb_parse_not(mem_ctx
, &p
);
636 ret
= ldb_parse_simple(mem_ctx
, &p
);
646 <filter> ::= '(' <filtercomp> ')'
648 static struct ldb_parse_tree
*ldb_parse_filter(TALLOC_CTX
*mem_ctx
, const char **s
)
650 struct ldb_parse_tree
*ret
;
658 ret
= ldb_parse_filtercomp(mem_ctx
, &p
);
665 while (isspace((unsigned char)*p
)) {
676 main parser entry point. Takes a search string and returns a parse tree
678 expression ::= <simple> | <filter>
680 struct ldb_parse_tree
*ldb_parse_tree(TALLOC_CTX
*mem_ctx
, const char *s
)
682 if (s
== NULL
|| *s
== 0) {
683 s
= "(|(objectClass=*)(distinguishedName=*))";
686 while (isspace((unsigned char)*s
)) s
++;
689 return ldb_parse_filter(mem_ctx
, &s
);
692 return ldb_parse_simple(mem_ctx
, &s
);
697 construct a ldap parse filter given a parse tree
699 char *ldb_filter_from_tree(TALLOC_CTX
*mem_ctx
, const struct ldb_parse_tree
*tree
)
708 switch (tree
->operation
) {
711 ret
= talloc_asprintf(mem_ctx
, "(%c", tree
->operation
==LDB_OP_AND
?'&':'|');
712 if (ret
== NULL
) return NULL
;
713 for (i
=0;i
<tree
->u
.list
.num_elements
;i
++) {
714 s
= ldb_filter_from_tree(mem_ctx
, tree
->u
.list
.elements
[i
]);
719 s2
= talloc_asprintf_append(ret
, "%s", s
);
727 s
= talloc_asprintf_append(ret
, ")");
734 s
= ldb_filter_from_tree(mem_ctx
, tree
->u
.isnot
.child
);
735 if (s
== NULL
) return NULL
;
737 ret
= talloc_asprintf(mem_ctx
, "(!%s)", s
);
740 case LDB_OP_EQUALITY
:
741 s
= ldb_binary_encode(mem_ctx
, tree
->u
.equality
.value
);
742 if (s
== NULL
) return NULL
;
743 ret
= talloc_asprintf(mem_ctx
, "(%s=%s)",
744 tree
->u
.equality
.attr
, s
);
747 case LDB_OP_SUBSTRING
:
748 ret
= talloc_asprintf(mem_ctx
, "(%s=%s", tree
->u
.substring
.attr
,
749 tree
->u
.substring
.start_with_wildcard
?"*":"");
750 if (ret
== NULL
) return NULL
;
751 for (i
= 0; tree
->u
.substring
.chunks
[i
]; i
++) {
752 s2
= ldb_binary_encode(mem_ctx
, *(tree
->u
.substring
.chunks
[i
]));
757 if (tree
->u
.substring
.chunks
[i
+1] ||
758 tree
->u
.substring
.end_with_wildcard
) {
759 s
= talloc_asprintf_append(ret
, "%s*", s2
);
761 s
= talloc_asprintf_append(ret
, "%s", s2
);
769 s
= talloc_asprintf_append(ret
, ")");
777 s
= ldb_binary_encode(mem_ctx
, tree
->u
.equality
.value
);
778 if (s
== NULL
) return NULL
;
779 ret
= talloc_asprintf(mem_ctx
, "(%s>=%s)",
780 tree
->u
.equality
.attr
, s
);
784 s
= ldb_binary_encode(mem_ctx
, tree
->u
.equality
.value
);
785 if (s
== NULL
) return NULL
;
786 ret
= talloc_asprintf(mem_ctx
, "(%s<=%s)",
787 tree
->u
.equality
.attr
, s
);
791 ret
= talloc_asprintf(mem_ctx
, "(%s=*)", tree
->u
.present
.attr
);
794 s
= ldb_binary_encode(mem_ctx
, tree
->u
.equality
.value
);
795 if (s
== NULL
) return NULL
;
796 ret
= talloc_asprintf(mem_ctx
, "(%s~=%s)",
797 tree
->u
.equality
.attr
, s
);
800 case LDB_OP_EXTENDED
:
801 s
= ldb_binary_encode(mem_ctx
, tree
->u
.extended
.value
);
802 if (s
== NULL
) return NULL
;
803 ret
= talloc_asprintf(mem_ctx
, "(%s%s%s%s:=%s)",
804 tree
->u
.extended
.attr
?tree
->u
.extended
.attr
:"",
805 tree
->u
.extended
.dnAttributes
?":dn":"",
806 tree
->u
.extended
.rule_id
?":":"",
807 tree
->u
.extended
.rule_id
?tree
->u
.extended
.rule_id
:"",
818 walk a parse tree, calling the provided callback on each node
820 int ldb_parse_tree_walk(struct ldb_parse_tree
*tree
,
821 int (*callback
)(struct ldb_parse_tree
*tree
, void *),
822 void *private_context
)
827 ret
= callback(tree
, private_context
);
828 if (ret
!= LDB_SUCCESS
) {
832 switch (tree
->operation
) {
835 for (i
=0;i
<tree
->u
.list
.num_elements
;i
++) {
836 ret
= ldb_parse_tree_walk(tree
->u
.list
.elements
[i
], callback
, private_context
);
837 if (ret
!= LDB_SUCCESS
) {
843 ret
= ldb_parse_tree_walk(tree
->u
.isnot
.child
, callback
, private_context
);
844 if (ret
!= LDB_SUCCESS
) {
848 case LDB_OP_EQUALITY
:
852 case LDB_OP_SUBSTRING
:
854 case LDB_OP_EXTENDED
:
860 struct parse_tree_attr_replace_ctx
{
866 callback for ldb_parse_tree_attr_replace()
868 static int parse_tree_attr_replace(struct ldb_parse_tree
*tree
, void *private_context
)
870 struct parse_tree_attr_replace_ctx
*ctx
= private_context
;
871 switch (tree
->operation
) {
872 case LDB_OP_EQUALITY
:
876 if (ldb_attr_cmp(tree
->u
.equality
.attr
, ctx
->attr
) == 0) {
877 tree
->u
.equality
.attr
= ctx
->replace
;
880 case LDB_OP_SUBSTRING
:
881 if (ldb_attr_cmp(tree
->u
.substring
.attr
, ctx
->attr
) == 0) {
882 tree
->u
.substring
.attr
= ctx
->replace
;
886 if (ldb_attr_cmp(tree
->u
.present
.attr
, ctx
->attr
) == 0) {
887 tree
->u
.present
.attr
= ctx
->replace
;
890 case LDB_OP_EXTENDED
:
891 if (tree
->u
.extended
.attr
&&
892 ldb_attr_cmp(tree
->u
.extended
.attr
, ctx
->attr
) == 0) {
893 tree
->u
.extended
.attr
= ctx
->replace
;
903 replace any occurrences of an attribute name in the parse tree with a
906 void ldb_parse_tree_attr_replace(struct ldb_parse_tree
*tree
,
910 struct parse_tree_attr_replace_ctx ctx
;
913 ctx
.replace
= replace
;
915 ldb_parse_tree_walk(tree
, parse_tree_attr_replace
, &ctx
);
919 shallow copy a tree - copying only the elements array so that the caller
920 can safely add new elements without changing the message
922 struct ldb_parse_tree
*ldb_parse_tree_copy_shallow(TALLOC_CTX
*mem_ctx
,
923 const struct ldb_parse_tree
*ot
)
926 struct ldb_parse_tree
*nt
;
928 nt
= talloc(mem_ctx
, struct ldb_parse_tree
);
935 switch (ot
->operation
) {
938 nt
->u
.list
.elements
= talloc_array(nt
, struct ldb_parse_tree
*,
939 ot
->u
.list
.num_elements
);
940 if (!nt
->u
.list
.elements
) {
945 for (i
=0;i
<ot
->u
.list
.num_elements
;i
++) {
946 nt
->u
.list
.elements
[i
] =
947 ldb_parse_tree_copy_shallow(nt
->u
.list
.elements
,
948 ot
->u
.list
.elements
[i
]);
949 if (!nt
->u
.list
.elements
[i
]) {
956 nt
->u
.isnot
.child
= ldb_parse_tree_copy_shallow(nt
,
958 if (!nt
->u
.isnot
.child
) {
963 case LDB_OP_EQUALITY
:
967 case LDB_OP_SUBSTRING
:
969 case LDB_OP_EXTENDED
: