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"
46 #include "ldb/include/ldb_parse.h"
51 a filter is defined by:
52 <filter> ::= '(' <filtercomp> ')'
53 <filtercomp> ::= <and> | <or> | <not> | <simple>
54 <and> ::= '&' <filterlist>
55 <or> ::= '|' <filterlist>
56 <not> ::= '!' <filter>
57 <filterlist> ::= <filter> | <filter> <filterlist>
58 <simple> ::= <attributetype> <filtertype> <attributevalue>
59 <filtertype> ::= '=' | '~=' | '<=' | '>='
62 #define LDB_ALL_SEP "()&|=!"
65 return next token element. Caller frees
67 static char *ldb_parse_lex(TALLOC_CTX *ctx, const char **s, const char *sep)
81 if (strchr(sep, *p)) {
83 ret = talloc_strndup(ctx, p, 1);
90 while (*p && (isalnum(*p) || !strchr(sep, *p))) {
98 ret = talloc_strndup(ctx, *s, p - *s);
109 find a matching close brace in a string
111 static const char *match_brace(const char *s)
113 unsigned int count = 0;
114 while (*s && (count != 0 || *s != ')')) {
130 static struct ldb_parse_tree *ldb_parse_filter(TALLOC_CTX *ctx, const char **s);
133 <simple> ::= <attributetype> <filtertype> <attributevalue>
135 static struct ldb_parse_tree *ldb_parse_simple(TALLOC_CTX *ctx, const char *s)
138 struct ldb_parse_tree *ret;
140 ret = talloc(ctx, struct ldb_parse_tree);
146 l = ldb_parse_lex(ret, &s, LDB_ALL_SEP);
152 if (strchr("()&|=", *l)) {
157 eq = ldb_parse_lex(ret, &s, LDB_ALL_SEP);
158 if (!eq || strcmp(eq, "=") != 0) {
164 val = ldb_parse_lex(ret, &s, ")");
165 if (val && strchr("()&|", *val)) {
170 ret->operation = LDB_OP_SIMPLE;
171 ret->u.simple.attr = l;
172 ret->u.simple.value.data = val?val:discard_const_p(char, "");
173 ret->u.simple.value.length = val?strlen(val):0;
181 <and> ::= '&' <filterlist>
182 <or> ::= '|' <filterlist>
183 <filterlist> ::= <filter> | <filter> <filterlist>
185 static struct ldb_parse_tree *ldb_parse_filterlist(TALLOC_CTX *ctx,
186 enum ldb_parse_op op, const char *s)
188 struct ldb_parse_tree *ret, *next;
190 ret = talloc(ctx, struct ldb_parse_tree);
197 ret->u.list.num_elements = 1;
198 ret->u.list.elements = talloc(ret, struct ldb_parse_tree *);
199 if (!ret->u.list.elements) {
205 ret->u.list.elements[0] = ldb_parse_filter(ret->u.list.elements, &s);
206 if (!ret->u.list.elements[0]) {
211 while (isspace(*s)) s++;
213 while (*s && (next = ldb_parse_filter(ret->u.list.elements, &s))) {
214 struct ldb_parse_tree **e;
215 e = talloc_realloc(ret, ret->u.list.elements,
216 struct ldb_parse_tree *,
217 ret->u.list.num_elements+1);
223 ret->u.list.elements = e;
224 ret->u.list.elements[ret->u.list.num_elements] = next;
225 ret->u.list.num_elements++;
226 while (isspace(*s)) s++;
234 <not> ::= '!' <filter>
236 static struct ldb_parse_tree *ldb_parse_not(TALLOC_CTX *ctx, const char *s)
238 struct ldb_parse_tree *ret;
240 ret = talloc(ctx, struct ldb_parse_tree);
246 ret->operation = LDB_OP_NOT;
247 ret->u.not.child = ldb_parse_filter(ret, &s);
248 if (!ret->u.not.child) {
258 <filtercomp> ::= <and> | <or> | <not> | <simple>
260 static struct ldb_parse_tree *ldb_parse_filtercomp(TALLOC_CTX *ctx, const char *s)
262 while (isspace(*s)) s++;
266 return ldb_parse_filterlist(ctx, LDB_OP_AND, s+1);
269 return ldb_parse_filterlist(ctx, LDB_OP_OR, s+1);
272 return ldb_parse_not(ctx, s+1);
279 return ldb_parse_simple(ctx, s);
284 <filter> ::= '(' <filtercomp> ')'
286 static struct ldb_parse_tree *ldb_parse_filter(TALLOC_CTX *ctx, const char **s)
290 struct ldb_parse_tree *ret;
292 l = ldb_parse_lex(ctx, s, LDB_ALL_SEP);
297 if (strcmp(l, "(") != 0) {
309 s2 = talloc_strndup(ctx, *s, p - *s);
315 ret = ldb_parse_filtercomp(ctx, s2);
325 main parser entry point. Takes a search string and returns a parse tree
327 expression ::= <simple> | <filter>
329 struct ldb_parse_tree *ldb_parse_tree(TALLOC_CTX *mem_ctx, const char *s)
331 while (isspace(*s)) s++;
334 return ldb_parse_filter(mem_ctx, &s);
337 return ldb_parse_simple(mem_ctx, s);