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_private.h"
47 #include "ldb/include/ldb_parse.h"
52 a filter is defined by:
53 <filter> ::= '(' <filtercomp> ')'
54 <filtercomp> ::= <and> | <or> | <not> | <simple>
55 <and> ::= '&' <filterlist>
56 <or> ::= '|' <filterlist>
57 <not> ::= '!' <filter>
58 <filterlist> ::= <filter> | <filter> <filterlist>
59 <simple> ::= <attributetype> <filtertype> <attributevalue>
60 <filtertype> ::= '=' | '~=' | '<=' | '>='
63 #define LDB_ALL_SEP "()&|=!"
66 return next token element. Caller frees
68 static char *ldb_parse_lex(struct ldb_context *ldb, const char **s, const char *sep)
82 if (strchr(sep, *p)) {
84 ret = ldb_strndup(ldb, p, 1);
91 while (*p && (isalnum(*p) || !strchr(sep, *p))) {
99 ret = ldb_strndup(ldb, *s, p - *s);
110 find a matching close brace in a string
112 static const char *match_brace(const char *s)
114 unsigned int count = 0;
115 while (*s && (count != 0 || *s != ')')) {
131 static struct ldb_parse_tree *ldb_parse_filter(struct ldb_context *ldb, const char **s);
134 <simple> ::= <attributetype> <filtertype> <attributevalue>
136 static struct ldb_parse_tree *ldb_parse_simple(struct ldb_context *ldb, const char *s)
139 struct ldb_parse_tree *ret;
141 l = ldb_parse_lex(ldb, &s, LDB_ALL_SEP);
146 if (strchr("()&|=", *l)) {
151 eq = ldb_parse_lex(ldb, &s, LDB_ALL_SEP);
152 if (!eq || strcmp(eq, "=") != 0) {
154 if (eq) ldb_free(ldb, eq);
159 val = ldb_parse_lex(ldb, &s, ")");
160 if (val && strchr("()&|", *val)) {
162 if (val) ldb_free(ldb, val);
166 ret = ldb_malloc_p(ldb, struct ldb_parse_tree);
172 ret->operation = LDB_OP_SIMPLE;
173 ret->u.simple.attr = l;
174 ret->u.simple.value.data = val;
175 ret->u.simple.value.length = val?strlen(val):0;
183 <and> ::= '&' <filterlist>
184 <or> ::= '|' <filterlist>
185 <filterlist> ::= <filter> | <filter> <filterlist>
187 static struct ldb_parse_tree *ldb_parse_filterlist(struct ldb_context *ldb,
188 enum ldb_parse_op op, const char *s)
190 struct ldb_parse_tree *ret, *next;
192 ret = ldb_malloc_p(ldb, struct ldb_parse_tree);
199 ret->u.list.num_elements = 1;
200 ret->u.list.elements = ldb_malloc_p(ldb, struct ldb_parse_tree *);
201 if (!ret->u.list.elements) {
207 ret->u.list.elements[0] = ldb_parse_filter(ldb, &s);
208 if (!ret->u.list.elements[0]) {
209 ldb_free(ldb, ret->u.list.elements);
214 while (isspace(*s)) s++;
216 while (*s && (next = ldb_parse_filter(ldb, &s))) {
217 struct ldb_parse_tree **e;
218 e = ldb_realloc_p(ldb, ret->u.list.elements,
219 struct ldb_parse_tree *,
220 ret->u.list.num_elements+1);
223 ldb_parse_tree_free(ldb, next);
224 ldb_parse_tree_free(ldb, ret);
227 ret->u.list.elements = e;
228 ret->u.list.elements[ret->u.list.num_elements] = next;
229 ret->u.list.num_elements++;
230 while (isspace(*s)) s++;
238 <not> ::= '!' <filter>
240 static struct ldb_parse_tree *ldb_parse_not(struct ldb_context *ldb, const char *s)
242 struct ldb_parse_tree *ret;
244 ret = ldb_malloc_p(ldb, struct ldb_parse_tree);
250 ret->operation = LDB_OP_NOT;
251 ret->u.not.child = ldb_parse_filter(ldb, &s);
252 if (!ret->u.not.child) {
262 <filtercomp> ::= <and> | <or> | <not> | <simple>
264 static struct ldb_parse_tree *ldb_parse_filtercomp(struct ldb_context *ldb,
267 while (isspace(*s)) s++;
271 return ldb_parse_filterlist(ldb, LDB_OP_AND, s+1);
274 return ldb_parse_filterlist(ldb, LDB_OP_OR, s+1);
277 return ldb_parse_not(ldb, s+1);
284 return ldb_parse_simple(ldb, s);
289 <filter> ::= '(' <filtercomp> ')'
291 static struct ldb_parse_tree *ldb_parse_filter(struct ldb_context *ldb, const char **s)
295 struct ldb_parse_tree *ret;
297 l = ldb_parse_lex(ldb, s, LDB_ALL_SEP);
302 if (strcmp(l, "(") != 0) {
314 s2 = ldb_strndup(ldb, *s, p - *s);
320 ret = ldb_parse_filtercomp(ldb, s2);
330 main parser entry point. Takes a search string and returns a parse tree
332 expression ::= <simple> | <filter>
334 struct ldb_parse_tree *ldb_parse_tree(struct ldb_context *ldb, const char *s)
336 while (isspace(*s)) s++;
339 return ldb_parse_filter(ldb, &s);
342 return ldb_parse_simple(ldb, s);
346 free a parse tree returned from ldb_parse_tree()
348 void ldb_parse_tree_free(struct ldb_context *ldb, struct ldb_parse_tree *tree)
352 switch (tree->operation) {
354 ldb_free(ldb, tree->u.simple.attr);
355 if (tree->u.simple.value.data) ldb_free(ldb, tree->u.simple.value.data);
360 for (i=0;i<tree->u.list.num_elements;i++) {
361 ldb_parse_tree_free(ldb, tree->u.list.elements[i]);
363 if (tree->u.list.elements) ldb_free(ldb, tree->u.list.elements);
367 ldb_parse_tree_free(ldb, tree->u.not.child);