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"
47 * Maximum depth of the filter parse tree, the value chosen is small enough to
48 * avoid triggering ASAN stack overflow checks. But large enough to be useful.
50 * On Windows clients the maximum number of levels of recursion allowed is 100.
51 * In the LDAP server, Windows restricts clients to 512 nested
54 #define LDB_MAX_PARSE_TREE_DEPTH 128
57 a filter is defined by:
58 <filter> ::= '(' <filtercomp> ')'
59 <filtercomp> ::= <and> | <or> | <not> | <simple>
60 <and> ::= '&' <filterlist>
61 <or> ::= '|' <filterlist>
62 <not> ::= '!' <filter>
63 <filterlist> ::= <filter> | <filter> <filterlist>
64 <simple> ::= <attributetype> <filtertype> <attributevalue>
65 <filtertype> ::= '=' | '~=' | '<=' | '>='
69 decode a RFC2254 binary string representation of a buffer.
72 struct ldb_val
ldb_binary_decode(TALLOC_CTX
*mem_ctx
, const char *str
)
76 size_t slen
= str
?strlen(str
):0;
78 ret
.data
= (uint8_t *)talloc_size(mem_ctx
, slen
+1);
80 if (ret
.data
== NULL
) return ret
;
82 for (i
=j
=0;i
<slen
;i
++) {
87 ok
= hex_byte(&str
[i
+1], &c
);
89 talloc_free(ret
.data
);
90 memset(&ret
, 0, sizeof(ret
));
93 ((uint8_t *)ret
.data
)[j
++] = c
;
96 ((uint8_t *)ret
.data
)[j
++] = str
[i
];
100 ((uint8_t *)ret
.data
)[j
] = 0;
105 static bool need_encode(unsigned char cval
)
107 if (cval
< 0x20 || cval
> 0x7E || strchr(" *()\\&|!\"", cval
)) {
114 encode a blob as a RFC2254 binary string, escaping any
115 non-printable or '\' characters
117 char *ldb_binary_encode(TALLOC_CTX
*mem_ctx
, struct ldb_val val
)
121 size_t len
= val
.length
;
122 unsigned char *buf
= val
.data
;
124 for (i
=0;i
<val
.length
;i
++) {
125 if (need_encode(buf
[i
])) {
129 ret
= talloc_array(mem_ctx
, char, len
+1);
130 if (ret
== NULL
) return NULL
;
133 for (i
=0;i
<val
.length
;i
++) {
134 if (need_encode(buf
[i
])) {
135 snprintf(ret
+len
, 4, "\\%02X", buf
[i
]);
148 encode a string as a RFC2254 binary string, escaping any
149 non-printable or '\' characters. This routine is suitable for use
150 in escaping user data in ldap filters.
152 char *ldb_binary_encode_string(TALLOC_CTX
*mem_ctx
, const char *string
)
155 if (string
== NULL
) {
158 val
.data
= discard_const_p(uint8_t, string
);
159 val
.length
= strlen(string
);
160 return ldb_binary_encode(mem_ctx
, val
);
163 /* find the first matching wildcard */
164 static char *ldb_parse_find_wildcard(char *value
)
167 value
= strpbrk(value
, "\\*");
168 if (value
== NULL
) return NULL
;
170 if (value
[0] == '\\') {
171 if (value
[1] == '\0') return NULL
;
176 if (value
[0] == '*') return value
;
182 /* return a NULL terminated list of binary strings representing the value
183 chunks separated by wildcards that makes the value portion of the filter
185 static struct ldb_val
**ldb_wildcard_decode(TALLOC_CTX
*mem_ctx
, const char *string
)
187 struct ldb_val
**ret
= NULL
;
188 unsigned int val
= 0;
191 wc
= talloc_strdup(mem_ctx
, string
);
192 if (wc
== NULL
) return NULL
;
196 wc
= ldb_parse_find_wildcard(str
);
206 ret
= talloc_realloc(mem_ctx
, ret
, struct ldb_val
*, val
+ 2);
207 if (ret
== NULL
) return NULL
;
209 ret
[val
] = talloc(mem_ctx
, struct ldb_val
);
210 if (ret
[val
] == NULL
) return NULL
;
212 *(ret
[val
]) = ldb_binary_decode(mem_ctx
, str
);
213 if ((ret
[val
])->data
== NULL
) return NULL
;
225 static struct ldb_parse_tree
*ldb_parse_filter(
233 parse an extended match
241 the ':dn' part sets the dnAttributes boolean if present
242 the oid sets the rule_id string
245 static struct ldb_parse_tree
*ldb_parse_extended(struct ldb_parse_tree
*ret
,
246 char *attr
, char *value
)
250 ret
->operation
= LDB_OP_EXTENDED
;
251 ret
->u
.extended
.value
= ldb_binary_decode(ret
, value
);
252 if (ret
->u
.extended
.value
.data
== NULL
) goto failed
;
254 p1
= strchr(attr
, ':');
255 if (p1
== NULL
) goto failed
;
256 p2
= strchr(p1
+1, ':');
261 ret
->u
.extended
.attr
= attr
;
262 if (strcmp(p1
+1, "dn") == 0) {
263 ret
->u
.extended
.dnAttributes
= 1;
265 ret
->u
.extended
.rule_id
= talloc_strdup(ret
, p2
+1);
266 if (ret
->u
.extended
.rule_id
== NULL
) goto failed
;
268 ret
->u
.extended
.rule_id
= NULL
;
271 ret
->u
.extended
.dnAttributes
= 0;
272 ret
->u
.extended
.rule_id
= talloc_strdup(ret
, p1
+1);
273 if (ret
->u
.extended
.rule_id
== NULL
) goto failed
;
283 static enum ldb_parse_op
ldb_parse_filtertype(TALLOC_CTX
*mem_ctx
, char **type
, char **value
, const char **s
)
285 enum ldb_parse_op filter
= 0;
286 char *name
, *val
, *k
;
290 /* retrieve attributetype name */
293 if (*p
== '@') { /* for internal attributes the first char can be @ */
297 while ((isascii(*p
) && isalnum((unsigned char)*p
)) || (*p
== '-') || (*p
== '.')) {
298 /* attribute names can only be alphanums */
302 if (*p
== ':') { /* but extended searches have : and . chars too */
304 if (p
== NULL
) { /* malformed attribute name */
311 while (isspace((unsigned char)*p
)) p
++;
313 if (!strchr("=<>~:", *p
)) {
318 name
= (char *)talloc_memdup(mem_ctx
, t
, t1
- t
+ 1);
319 if (name
== NULL
) return 0;
322 /* retrieve filtertype */
325 filter
= LDB_OP_EQUALITY
;
326 } else if (*p
!= '\0' && *(p
+ 1) == '=') {
329 filter
= LDB_OP_LESS
;
333 filter
= LDB_OP_GREATER
;
337 filter
= LDB_OP_APPROX
;
341 filter
= LDB_OP_EXTENDED
;
352 while (isspace((unsigned char)*p
)) p
++;
357 while (*p
&& ((*p
!= ')') || ((*p
== ')') && (*(p
- 1) == '\\')))) p
++;
359 val
= (char *)talloc_memdup(mem_ctx
, t
, p
- t
+ 1);
368 /* remove trailing spaces from value */
369 while ((k
> val
) && (isspace((unsigned char)*(k
- 1)))) k
--;
379 <simple> ::= <attributetype> <filtertype> <attributevalue>
381 static struct ldb_parse_tree
*ldb_parse_simple(TALLOC_CTX
*mem_ctx
, const char **s
)
384 struct ldb_parse_tree
*ret
;
385 enum ldb_parse_op filtertype
;
387 ret
= talloc_zero(mem_ctx
, struct ldb_parse_tree
);
393 filtertype
= ldb_parse_filtertype(ret
, &attr
, &value
, s
);
399 switch (filtertype
) {
402 ret
->operation
= LDB_OP_PRESENT
;
403 ret
->u
.present
.attr
= attr
;
406 case LDB_OP_EQUALITY
:
408 if (strcmp(value
, "*") == 0) {
409 ret
->operation
= LDB_OP_PRESENT
;
410 ret
->u
.present
.attr
= attr
;
414 if (ldb_parse_find_wildcard(value
) != NULL
) {
415 ret
->operation
= LDB_OP_SUBSTRING
;
416 ret
->u
.substring
.attr
= attr
;
417 ret
->u
.substring
.start_with_wildcard
= 0;
418 ret
->u
.substring
.end_with_wildcard
= 0;
419 ret
->u
.substring
.chunks
= ldb_wildcard_decode(ret
, value
);
420 if (ret
->u
.substring
.chunks
== NULL
){
425 ret
->u
.substring
.start_with_wildcard
= 1;
426 if (value
[strlen(value
) - 1] == '*')
427 ret
->u
.substring
.end_with_wildcard
= 1;
433 ret
->operation
= LDB_OP_EQUALITY
;
434 ret
->u
.equality
.attr
= attr
;
435 ret
->u
.equality
.value
= ldb_binary_decode(ret
, value
);
436 if (ret
->u
.equality
.value
.data
== NULL
) {
444 ret
->operation
= LDB_OP_GREATER
;
445 ret
->u
.comparison
.attr
= attr
;
446 ret
->u
.comparison
.value
= ldb_binary_decode(ret
, value
);
447 if (ret
->u
.comparison
.value
.data
== NULL
) {
455 ret
->operation
= LDB_OP_LESS
;
456 ret
->u
.comparison
.attr
= attr
;
457 ret
->u
.comparison
.value
= ldb_binary_decode(ret
, value
);
458 if (ret
->u
.comparison
.value
.data
== NULL
) {
466 ret
->operation
= LDB_OP_APPROX
;
467 ret
->u
.comparison
.attr
= attr
;
468 ret
->u
.comparison
.value
= ldb_binary_decode(ret
, value
);
469 if (ret
->u
.comparison
.value
.data
== NULL
) {
476 case LDB_OP_EXTENDED
:
478 ret
= ldb_parse_extended(ret
, attr
, value
);
492 <and> ::= '&' <filterlist>
493 <or> ::= '|' <filterlist>
494 <filterlist> ::= <filter> | <filter> <filterlist>
496 static struct ldb_parse_tree
*ldb_parse_filterlist(
502 struct ldb_parse_tree
*ret
, *next
;
503 enum ldb_parse_op op
;
518 while (isspace((unsigned char)*p
)) p
++;
520 ret
= talloc(mem_ctx
, struct ldb_parse_tree
);
527 ret
->u
.list
.num_elements
= 1;
528 ret
->u
.list
.elements
= talloc(ret
, struct ldb_parse_tree
*);
529 if (!ret
->u
.list
.elements
) {
535 ret
->u
.list
.elements
[0] =
536 ldb_parse_filter(ret
->u
.list
.elements
, &p
, depth
, max_depth
);
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(
551 ret
->u
.list
.elements
, &p
, depth
, max_depth
);
553 /* an invalid filter element */
557 e
= talloc_realloc(ret
, ret
->u
.list
.elements
,
558 struct ldb_parse_tree
*,
559 ret
->u
.list
.num_elements
+ 1);
565 ret
->u
.list
.elements
= e
;
566 ret
->u
.list
.elements
[ret
->u
.list
.num_elements
] = next
;
567 ret
->u
.list
.num_elements
++;
568 while (isspace((unsigned char)*p
)) p
++;
578 <not> ::= '!' <filter>
580 static struct ldb_parse_tree
*ldb_parse_not(
586 struct ldb_parse_tree
*ret
;
594 ret
= talloc(mem_ctx
, struct ldb_parse_tree
);
600 ret
->operation
= LDB_OP_NOT
;
601 ret
->u
.isnot
.child
= ldb_parse_filter(ret
, &p
, depth
, max_depth
);
602 if (!ret
->u
.isnot
.child
) {
614 <filtercomp> ::= <and> | <or> | <not> | <simple>
616 static struct ldb_parse_tree
*ldb_parse_filtercomp(
622 struct ldb_parse_tree
*ret
;
625 while (isspace((unsigned char)*p
)) p
++;
629 ret
= ldb_parse_filterlist(mem_ctx
, &p
, depth
, max_depth
);
633 ret
= ldb_parse_filterlist(mem_ctx
, &p
, depth
, max_depth
);
637 ret
= ldb_parse_not(mem_ctx
, &p
, depth
, max_depth
);
645 ret
= ldb_parse_simple(mem_ctx
, &p
);
654 <filter> ::= '(' <filtercomp> ')'
656 static struct ldb_parse_tree
*ldb_parse_filter(
662 struct ldb_parse_tree
*ret
;
666 * Check the depth of the parse tree, and reject the input if
667 * max_depth exceeded. This avoids stack overflow
670 if (depth
> max_depth
) {
680 ret
= ldb_parse_filtercomp(mem_ctx
, &p
, depth
, max_depth
);
687 while (isspace((unsigned char)*p
)) {
698 main parser entry point. Takes a search string and returns a parse tree
700 expression ::= <simple> | <filter>
702 struct ldb_parse_tree
*ldb_parse_tree(TALLOC_CTX
*mem_ctx
, const char *s
)
706 while (s
&& isspace((unsigned char)*s
)) s
++;
708 if (s
== NULL
|| *s
== 0) {
709 s
= "(|(objectClass=*)(distinguishedName=*))";
713 return ldb_parse_filter(
714 mem_ctx
, &s
, depth
, LDB_MAX_PARSE_TREE_DEPTH
);
717 return ldb_parse_simple(mem_ctx
, &s
);
722 construct a ldap parse filter given a parse tree
724 char *ldb_filter_from_tree(TALLOC_CTX
*mem_ctx
, const struct ldb_parse_tree
*tree
)
733 switch (tree
->operation
) {
736 ret
= talloc_asprintf(mem_ctx
, "(%c", tree
->operation
==LDB_OP_AND
?'&':'|');
737 if (ret
== NULL
) return NULL
;
738 for (i
=0;i
<tree
->u
.list
.num_elements
;i
++) {
739 s
= ldb_filter_from_tree(mem_ctx
, tree
->u
.list
.elements
[i
]);
744 s2
= talloc_asprintf_append(ret
, "%s", s
);
752 s
= talloc_asprintf_append(ret
, ")");
759 s
= ldb_filter_from_tree(mem_ctx
, tree
->u
.isnot
.child
);
760 if (s
== NULL
) return NULL
;
762 ret
= talloc_asprintf(mem_ctx
, "(!%s)", s
);
765 case LDB_OP_EQUALITY
:
766 s
= ldb_binary_encode(mem_ctx
, tree
->u
.equality
.value
);
767 if (s
== NULL
) return NULL
;
768 ret
= talloc_asprintf(mem_ctx
, "(%s=%s)",
769 tree
->u
.equality
.attr
, s
);
772 case LDB_OP_SUBSTRING
:
773 ret
= talloc_asprintf(mem_ctx
, "(%s=%s", tree
->u
.substring
.attr
,
774 tree
->u
.substring
.start_with_wildcard
?"*":"");
775 if (ret
== NULL
) return NULL
;
776 for (i
= 0; tree
->u
.substring
.chunks
&& tree
->u
.substring
.chunks
[i
]; i
++) {
777 s2
= ldb_binary_encode(mem_ctx
, *(tree
->u
.substring
.chunks
[i
]));
782 if (tree
->u
.substring
.chunks
[i
+1] ||
783 tree
->u
.substring
.end_with_wildcard
) {
784 s
= talloc_asprintf_append(ret
, "%s*", s2
);
786 s
= talloc_asprintf_append(ret
, "%s", s2
);
794 s
= talloc_asprintf_append(ret
, ")");
802 s
= ldb_binary_encode(mem_ctx
, tree
->u
.comparison
.value
);
803 if (s
== NULL
) return NULL
;
804 ret
= talloc_asprintf(mem_ctx
, "(%s>=%s)",
805 tree
->u
.comparison
.attr
, s
);
809 s
= ldb_binary_encode(mem_ctx
, tree
->u
.comparison
.value
);
810 if (s
== NULL
) return NULL
;
811 ret
= talloc_asprintf(mem_ctx
, "(%s<=%s)",
812 tree
->u
.comparison
.attr
, s
);
816 ret
= talloc_asprintf(mem_ctx
, "(%s=*)", tree
->u
.present
.attr
);
819 s
= ldb_binary_encode(mem_ctx
, tree
->u
.comparison
.value
);
820 if (s
== NULL
) return NULL
;
821 ret
= talloc_asprintf(mem_ctx
, "(%s~=%s)",
822 tree
->u
.comparison
.attr
, s
);
825 case LDB_OP_EXTENDED
:
826 s
= ldb_binary_encode(mem_ctx
, tree
->u
.extended
.value
);
827 if (s
== NULL
) return NULL
;
828 ret
= talloc_asprintf(mem_ctx
, "(%s%s%s%s:=%s)",
829 tree
->u
.extended
.attr
?tree
->u
.extended
.attr
:"",
830 tree
->u
.extended
.dnAttributes
?":dn":"",
831 tree
->u
.extended
.rule_id
?":":"",
832 tree
->u
.extended
.rule_id
?tree
->u
.extended
.rule_id
:"",
843 walk a parse tree, calling the provided callback on each node
845 int ldb_parse_tree_walk(struct ldb_parse_tree
*tree
,
846 int (*callback
)(struct ldb_parse_tree
*tree
, void *),
847 void *private_context
)
852 ret
= callback(tree
, private_context
);
853 if (ret
!= LDB_SUCCESS
) {
857 switch (tree
->operation
) {
860 for (i
=0;i
<tree
->u
.list
.num_elements
;i
++) {
861 ret
= ldb_parse_tree_walk(tree
->u
.list
.elements
[i
], callback
, private_context
);
862 if (ret
!= LDB_SUCCESS
) {
868 ret
= ldb_parse_tree_walk(tree
->u
.isnot
.child
, callback
, private_context
);
869 if (ret
!= LDB_SUCCESS
) {
873 case LDB_OP_EQUALITY
:
877 case LDB_OP_SUBSTRING
:
879 case LDB_OP_EXTENDED
:
885 struct parse_tree_attr_replace_ctx
{
891 callback for ldb_parse_tree_attr_replace()
893 static int parse_tree_attr_replace(struct ldb_parse_tree
*tree
, void *private_context
)
895 struct parse_tree_attr_replace_ctx
*ctx
= private_context
;
896 switch (tree
->operation
) {
897 case LDB_OP_EQUALITY
:
898 if (ldb_attr_cmp(tree
->u
.equality
.attr
, ctx
->attr
) == 0) {
899 tree
->u
.equality
.attr
= ctx
->replace
;
905 if (ldb_attr_cmp(tree
->u
.comparison
.attr
, ctx
->attr
) == 0) {
906 tree
->u
.comparison
.attr
= ctx
->replace
;
909 case LDB_OP_SUBSTRING
:
910 if (ldb_attr_cmp(tree
->u
.substring
.attr
, ctx
->attr
) == 0) {
911 tree
->u
.substring
.attr
= ctx
->replace
;
915 if (ldb_attr_cmp(tree
->u
.present
.attr
, ctx
->attr
) == 0) {
916 tree
->u
.present
.attr
= ctx
->replace
;
919 case LDB_OP_EXTENDED
:
920 if (tree
->u
.extended
.attr
&&
921 ldb_attr_cmp(tree
->u
.extended
.attr
, ctx
->attr
) == 0) {
922 tree
->u
.extended
.attr
= ctx
->replace
;
932 replace any occurrences of an attribute name in the parse tree with a
935 void ldb_parse_tree_attr_replace(struct ldb_parse_tree
*tree
,
939 struct parse_tree_attr_replace_ctx ctx
;
942 ctx
.replace
= replace
;
944 ldb_parse_tree_walk(tree
, parse_tree_attr_replace
, &ctx
);
948 shallow copy a tree - copying only the elements array so that the caller
949 can safely add new elements without changing the message
951 struct ldb_parse_tree
*ldb_parse_tree_copy_shallow(TALLOC_CTX
*mem_ctx
,
952 const struct ldb_parse_tree
*ot
)
955 struct ldb_parse_tree
*nt
;
957 nt
= talloc(mem_ctx
, struct ldb_parse_tree
);
964 switch (ot
->operation
) {
967 nt
->u
.list
.elements
= talloc_array(nt
, struct ldb_parse_tree
*,
968 ot
->u
.list
.num_elements
);
969 if (!nt
->u
.list
.elements
) {
974 for (i
=0;i
<ot
->u
.list
.num_elements
;i
++) {
975 nt
->u
.list
.elements
[i
] =
976 ldb_parse_tree_copy_shallow(nt
->u
.list
.elements
,
977 ot
->u
.list
.elements
[i
]);
978 if (!nt
->u
.list
.elements
[i
]) {
985 nt
->u
.isnot
.child
= ldb_parse_tree_copy_shallow(nt
,
987 if (!nt
->u
.isnot
.child
) {
992 case LDB_OP_EQUALITY
:
996 case LDB_OP_SUBSTRING
:
998 case LDB_OP_EXTENDED
:
1005 /* Get the attribute (if any) associated with the top node of a parse tree. */
1006 const char *ldb_parse_tree_get_attr(const struct ldb_parse_tree
*tree
)
1008 switch (tree
->operation
) {
1013 case LDB_OP_EQUALITY
:
1014 return tree
->u
.equality
.attr
;
1015 case LDB_OP_SUBSTRING
:
1016 return tree
->u
.substring
.attr
;
1017 case LDB_OP_GREATER
:
1020 return tree
->u
.comparison
.attr
;
1021 case LDB_OP_PRESENT
:
1022 return tree
->u
.present
.attr
;
1023 case LDB_OP_EXTENDED
:
1024 return tree
->u
.extended
.attr
;