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;
116 encode a blob as a RFC2254 binary string, escaping any
117 non-printable or '\' characters
119 char *ldb_binary_encode(TALLOC_CTX
*mem_ctx
, struct ldb_val val
)
123 size_t len
= val
.length
;
124 unsigned char *buf
= val
.data
;
126 for (i
=0;i
<val
.length
;i
++) {
127 unsigned int cval
= buf
[i
];
128 if (cval
< 0x20 || cval
> 0x7E || strchr(" *()\\&|!\"", buf
[i
])) {
132 ret
= talloc_array(mem_ctx
, char, len
+1);
133 if (ret
== NULL
) return NULL
;
136 for (i
=0;i
<val
.length
;i
++) {
137 unsigned int cval
= buf
[i
];
138 if (cval
< 0x20 || cval
> 0x7E || strchr(" *()\\&|!\"", buf
[i
])) {
139 snprintf(ret
+len
, 4, "\\%02X", buf
[i
]);
152 encode a string as a RFC2254 binary string, escaping any
153 non-printable or '\' characters. This routine is suitable for use
154 in escaping user data in ldap filters.
156 char *ldb_binary_encode_string(TALLOC_CTX
*mem_ctx
, const char *string
)
159 if (string
== NULL
) {
162 val
.data
= discard_const_p(uint8_t, string
);
163 val
.length
= strlen(string
);
164 return ldb_binary_encode(mem_ctx
, val
);
167 /* find the first matching wildcard */
168 static char *ldb_parse_find_wildcard(char *value
)
171 value
= strpbrk(value
, "\\*");
172 if (value
== NULL
) return NULL
;
174 if (value
[0] == '\\') {
175 if (value
[1] == '\0') return NULL
;
180 if (value
[0] == '*') return value
;
186 /* return a NULL terminated list of binary strings representing the value
187 chunks separated by wildcards that makes the value portion of the filter
189 static struct ldb_val
**ldb_wildcard_decode(TALLOC_CTX
*mem_ctx
, const char *string
)
191 struct ldb_val
**ret
= NULL
;
192 unsigned int val
= 0;
195 wc
= talloc_strdup(mem_ctx
, string
);
196 if (wc
== NULL
) return NULL
;
200 wc
= ldb_parse_find_wildcard(str
);
210 ret
= talloc_realloc(mem_ctx
, ret
, struct ldb_val
*, val
+ 2);
211 if (ret
== NULL
) return NULL
;
213 ret
[val
] = talloc(mem_ctx
, struct ldb_val
);
214 if (ret
[val
] == NULL
) return NULL
;
216 *(ret
[val
]) = ldb_binary_decode(mem_ctx
, str
);
217 if ((ret
[val
])->data
== NULL
) return NULL
;
229 static struct ldb_parse_tree
*ldb_parse_filter(TALLOC_CTX
*mem_ctx
, const char **s
);
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
+ 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(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(TALLOC_CTX
*mem_ctx
, const char **s
)
498 struct ldb_parse_tree
*ret
, *next
;
499 enum ldb_parse_op op
;
514 while (isspace((unsigned char)*p
)) p
++;
516 ret
= talloc(mem_ctx
, struct ldb_parse_tree
);
523 ret
->u
.list
.num_elements
= 1;
524 ret
->u
.list
.elements
= talloc(ret
, struct ldb_parse_tree
*);
525 if (!ret
->u
.list
.elements
) {
531 ret
->u
.list
.elements
[0] = ldb_parse_filter(ret
->u
.list
.elements
, &p
);
532 if (!ret
->u
.list
.elements
[0]) {
537 while (isspace((unsigned char)*p
)) p
++;
540 struct ldb_parse_tree
**e
;
545 next
= ldb_parse_filter(ret
->u
.list
.elements
, &p
);
547 /* an invalid filter element */
551 e
= talloc_realloc(ret
, ret
->u
.list
.elements
,
552 struct ldb_parse_tree
*,
553 ret
->u
.list
.num_elements
+ 1);
559 ret
->u
.list
.elements
= e
;
560 ret
->u
.list
.elements
[ret
->u
.list
.num_elements
] = next
;
561 ret
->u
.list
.num_elements
++;
562 while (isspace((unsigned char)*p
)) p
++;
572 <not> ::= '!' <filter>
574 static struct ldb_parse_tree
*ldb_parse_not(TALLOC_CTX
*mem_ctx
, const char **s
)
576 struct ldb_parse_tree
*ret
;
584 ret
= talloc(mem_ctx
, struct ldb_parse_tree
);
590 ret
->operation
= LDB_OP_NOT
;
591 ret
->u
.isnot
.child
= ldb_parse_filter(ret
, &p
);
592 if (!ret
->u
.isnot
.child
) {
604 <filtercomp> ::= <and> | <or> | <not> | <simple>
606 static struct ldb_parse_tree
*ldb_parse_filtercomp(TALLOC_CTX
*mem_ctx
, const char **s
)
608 struct ldb_parse_tree
*ret
;
611 while (isspace((unsigned char)*p
)) p
++;
615 ret
= ldb_parse_filterlist(mem_ctx
, &p
);
619 ret
= ldb_parse_filterlist(mem_ctx
, &p
);
623 ret
= ldb_parse_not(mem_ctx
, &p
);
631 ret
= ldb_parse_simple(mem_ctx
, &p
);
641 <filter> ::= '(' <filtercomp> ')'
643 static struct ldb_parse_tree
*ldb_parse_filter(TALLOC_CTX
*mem_ctx
, const char **s
)
645 struct ldb_parse_tree
*ret
;
653 ret
= ldb_parse_filtercomp(mem_ctx
, &p
);
660 while (isspace((unsigned char)*p
)) {
671 main parser entry point. Takes a search string and returns a parse tree
673 expression ::= <simple> | <filter>
675 struct ldb_parse_tree
*ldb_parse_tree(TALLOC_CTX
*mem_ctx
, const char *s
)
677 if (s
== NULL
|| *s
== 0) {
678 s
= "(|(objectClass=*)(distinguishedName=*))";
681 while (isspace((unsigned char)*s
)) s
++;
684 return ldb_parse_filter(mem_ctx
, &s
);
687 return ldb_parse_simple(mem_ctx
, &s
);
692 construct a ldap parse filter given a parse tree
694 char *ldb_filter_from_tree(TALLOC_CTX
*mem_ctx
, const struct ldb_parse_tree
*tree
)
703 switch (tree
->operation
) {
706 ret
= talloc_asprintf(mem_ctx
, "(%c", tree
->operation
==LDB_OP_AND
?'&':'|');
707 if (ret
== NULL
) return NULL
;
708 for (i
=0;i
<tree
->u
.list
.num_elements
;i
++) {
709 s
= ldb_filter_from_tree(mem_ctx
, tree
->u
.list
.elements
[i
]);
714 s2
= talloc_asprintf_append(ret
, "%s", s
);
722 s
= talloc_asprintf_append(ret
, ")");
729 s
= ldb_filter_from_tree(mem_ctx
, tree
->u
.isnot
.child
);
730 if (s
== NULL
) return NULL
;
732 ret
= talloc_asprintf(mem_ctx
, "(!%s)", s
);
735 case LDB_OP_EQUALITY
:
736 s
= ldb_binary_encode(mem_ctx
, tree
->u
.equality
.value
);
737 if (s
== NULL
) return NULL
;
738 ret
= talloc_asprintf(mem_ctx
, "(%s=%s)",
739 tree
->u
.equality
.attr
, s
);
742 case LDB_OP_SUBSTRING
:
743 ret
= talloc_asprintf(mem_ctx
, "(%s=%s", tree
->u
.substring
.attr
,
744 tree
->u
.substring
.start_with_wildcard
?"*":"");
745 if (ret
== NULL
) return NULL
;
746 for (i
= 0; tree
->u
.substring
.chunks
[i
]; i
++) {
747 s2
= ldb_binary_encode(mem_ctx
, *(tree
->u
.substring
.chunks
[i
]));
752 if (tree
->u
.substring
.chunks
[i
+1] ||
753 tree
->u
.substring
.end_with_wildcard
) {
754 s
= talloc_asprintf_append(ret
, "%s*", s2
);
756 s
= talloc_asprintf_append(ret
, "%s", s2
);
764 s
= talloc_asprintf_append(ret
, ")");
772 s
= ldb_binary_encode(mem_ctx
, tree
->u
.equality
.value
);
773 if (s
== NULL
) return NULL
;
774 ret
= talloc_asprintf(mem_ctx
, "(%s>=%s)",
775 tree
->u
.equality
.attr
, s
);
779 s
= ldb_binary_encode(mem_ctx
, tree
->u
.equality
.value
);
780 if (s
== NULL
) return NULL
;
781 ret
= talloc_asprintf(mem_ctx
, "(%s<=%s)",
782 tree
->u
.equality
.attr
, s
);
786 ret
= talloc_asprintf(mem_ctx
, "(%s=*)", tree
->u
.present
.attr
);
789 s
= ldb_binary_encode(mem_ctx
, tree
->u
.equality
.value
);
790 if (s
== NULL
) return NULL
;
791 ret
= talloc_asprintf(mem_ctx
, "(%s~=%s)",
792 tree
->u
.equality
.attr
, s
);
795 case LDB_OP_EXTENDED
:
796 s
= ldb_binary_encode(mem_ctx
, tree
->u
.extended
.value
);
797 if (s
== NULL
) return NULL
;
798 ret
= talloc_asprintf(mem_ctx
, "(%s%s%s%s:=%s)",
799 tree
->u
.extended
.attr
?tree
->u
.extended
.attr
:"",
800 tree
->u
.extended
.dnAttributes
?":dn":"",
801 tree
->u
.extended
.rule_id
?":":"",
802 tree
->u
.extended
.rule_id
?tree
->u
.extended
.rule_id
:"",
813 walk a parse tree, calling the provided callback on each node
815 int ldb_parse_tree_walk(struct ldb_parse_tree
*tree
,
816 int (*callback
)(struct ldb_parse_tree
*tree
, void *),
817 void *private_context
)
822 ret
= callback(tree
, private_context
);
823 if (ret
!= LDB_SUCCESS
) {
827 switch (tree
->operation
) {
830 for (i
=0;i
<tree
->u
.list
.num_elements
;i
++) {
831 ret
= ldb_parse_tree_walk(tree
->u
.list
.elements
[i
], callback
, private_context
);
832 if (ret
!= LDB_SUCCESS
) {
838 ret
= ldb_parse_tree_walk(tree
->u
.isnot
.child
, callback
, private_context
);
839 if (ret
!= LDB_SUCCESS
) {
843 case LDB_OP_EQUALITY
:
847 case LDB_OP_SUBSTRING
:
849 case LDB_OP_EXTENDED
:
855 struct parse_tree_attr_replace_ctx
{
861 callback for ldb_parse_tree_attr_replace()
863 static int parse_tree_attr_replace(struct ldb_parse_tree
*tree
, void *private_context
)
865 struct parse_tree_attr_replace_ctx
*ctx
= private_context
;
866 switch (tree
->operation
) {
867 case LDB_OP_EQUALITY
:
871 if (ldb_attr_cmp(tree
->u
.equality
.attr
, ctx
->attr
) == 0) {
872 tree
->u
.equality
.attr
= ctx
->replace
;
875 case LDB_OP_SUBSTRING
:
876 if (ldb_attr_cmp(tree
->u
.substring
.attr
, ctx
->attr
) == 0) {
877 tree
->u
.substring
.attr
= ctx
->replace
;
881 if (ldb_attr_cmp(tree
->u
.present
.attr
, ctx
->attr
) == 0) {
882 tree
->u
.present
.attr
= ctx
->replace
;
885 case LDB_OP_EXTENDED
:
886 if (tree
->u
.extended
.attr
&&
887 ldb_attr_cmp(tree
->u
.extended
.attr
, ctx
->attr
) == 0) {
888 tree
->u
.extended
.attr
= ctx
->replace
;
898 replace any occurrences of an attribute name in the parse tree with a
901 void ldb_parse_tree_attr_replace(struct ldb_parse_tree
*tree
,
905 struct parse_tree_attr_replace_ctx ctx
;
908 ctx
.replace
= replace
;
910 ldb_parse_tree_walk(tree
, parse_tree_attr_replace
, &ctx
);
914 shallow copy a tree - copying only the elements array so that the caller
915 can safely add new elements without changing the message
917 struct ldb_parse_tree
*ldb_parse_tree_copy_shallow(TALLOC_CTX
*mem_ctx
,
918 const struct ldb_parse_tree
*ot
)
921 struct ldb_parse_tree
*nt
;
923 nt
= talloc(mem_ctx
, struct ldb_parse_tree
);
930 switch (ot
->operation
) {
933 nt
->u
.list
.elements
= talloc_array(nt
, struct ldb_parse_tree
*,
934 ot
->u
.list
.num_elements
);
935 if (!nt
->u
.list
.elements
) {
940 for (i
=0;i
<ot
->u
.list
.num_elements
;i
++) {
941 nt
->u
.list
.elements
[i
] =
942 ldb_parse_tree_copy_shallow(nt
->u
.list
.elements
,
943 ot
->u
.list
.elements
[i
]);
944 if (!nt
->u
.list
.elements
[i
]) {
951 nt
->u
.isnot
.child
= ldb_parse_tree_copy_shallow(nt
,
953 if (!nt
->u
.isnot
.child
) {
958 case LDB_OP_EQUALITY
:
962 case LDB_OP_SUBSTRING
:
964 case LDB_OP_EXTENDED
: