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_parse.h"
49 a filter is defined by:
50 <filter> ::= '(' <filtercomp> ')'
51 <filtercomp> ::= <and> | <or> | <not> | <simple>
52 <and> ::= '&' <filterlist>
53 <or> ::= '|' <filterlist>
54 <not> ::= '!' <filter>
55 <filterlist> ::= <filter> | <filter> <filterlist>
56 <simple> ::= <attributetype> <filtertype> <attributevalue>
57 <filtertype> ::= '=' | '~=' | '<=' | '>='
60 #define LDB_ALL_SEP "()&|=!"
63 return next token element. Caller frees
65 static char *ldb_parse_lex(struct ldb_context *ldb, const char **s, const char *sep)
79 if (strchr(sep, *p)) {
81 ret = ldb_strndup(ldb, p, 1);
88 while (*p && (isalnum(*p) || !strchr(sep, *p))) {
96 ret = ldb_strndup(ldb, *s, p - *s);
107 find a matching close brace in a string
109 static const char *match_brace(const char *s)
111 unsigned int count = 0;
112 while (*s && (count != 0 || *s != ')')) {
128 static struct ldb_parse_tree *ldb_parse_filter(struct ldb_context *ldb, const char **s);
131 <simple> ::= <attributetype> <filtertype> <attributevalue>
133 static struct ldb_parse_tree *ldb_parse_simple(struct ldb_context *ldb, const char *s)
136 struct ldb_parse_tree *ret;
138 l = ldb_parse_lex(ldb, &s, LDB_ALL_SEP);
143 if (strchr("()&|=", *l)) {
148 eq = ldb_parse_lex(ldb, &s, LDB_ALL_SEP);
149 if (!eq || strcmp(eq, "=") != 0) {
151 if (eq) ldb_free(ldb, eq);
156 val = ldb_parse_lex(ldb, &s, ")");
157 if (val && strchr("()&|", *val)) {
159 if (val) ldb_free(ldb, val);
163 ret = ldb_malloc_p(ldb, struct ldb_parse_tree);
169 ret->operation = LDB_OP_SIMPLE;
170 ret->u.simple.attr = l;
171 ret->u.simple.value.data = val;
172 ret->u.simple.value.length = val?strlen(val):0;
180 <and> ::= '&' <filterlist>
181 <or> ::= '|' <filterlist>
182 <filterlist> ::= <filter> | <filter> <filterlist>
184 static struct ldb_parse_tree *ldb_parse_filterlist(struct ldb_context *ldb,
185 enum ldb_parse_op op, const char *s)
187 struct ldb_parse_tree *ret, *next;
189 ret = ldb_malloc_p(ldb, struct ldb_parse_tree);
196 ret->u.list.num_elements = 1;
197 ret->u.list.elements = ldb_malloc_p(ldb, struct ldb_parse_tree *);
198 if (!ret->u.list.elements) {
204 ret->u.list.elements[0] = ldb_parse_filter(ldb, &s);
205 if (!ret->u.list.elements[0]) {
206 ldb_free(ldb, ret->u.list.elements);
211 while (isspace(*s)) s++;
213 while (*s && (next = ldb_parse_filter(ldb, &s))) {
214 struct ldb_parse_tree **e;
215 e = ldb_realloc_p(ldb, ret->u.list.elements,
216 struct ldb_parse_tree *,
217 ret->u.list.num_elements+1);
220 ldb_parse_tree_free(ldb, next);
221 ldb_parse_tree_free(ldb, ret);
224 ret->u.list.elements = e;
225 ret->u.list.elements[ret->u.list.num_elements] = next;
226 ret->u.list.num_elements++;
227 while (isspace(*s)) s++;
235 <not> ::= '!' <filter>
237 static struct ldb_parse_tree *ldb_parse_not(struct ldb_context *ldb, const char *s)
239 struct ldb_parse_tree *ret;
241 ret = ldb_malloc_p(ldb, struct ldb_parse_tree);
247 ret->operation = LDB_OP_NOT;
248 ret->u.not.child = ldb_parse_filter(ldb, &s);
249 if (!ret->u.not.child) {
259 <filtercomp> ::= <and> | <or> | <not> | <simple>
261 static struct ldb_parse_tree *ldb_parse_filtercomp(struct ldb_context *ldb,
264 while (isspace(*s)) s++;
268 return ldb_parse_filterlist(ldb, LDB_OP_AND, s+1);
271 return ldb_parse_filterlist(ldb, LDB_OP_OR, s+1);
274 return ldb_parse_not(ldb, s+1);
281 return ldb_parse_simple(ldb, s);
286 <filter> ::= '(' <filtercomp> ')'
288 static struct ldb_parse_tree *ldb_parse_filter(struct ldb_context *ldb, const char **s)
292 struct ldb_parse_tree *ret;
294 l = ldb_parse_lex(ldb, s, LDB_ALL_SEP);
299 if (strcmp(l, "(") != 0) {
311 s2 = ldb_strndup(ldb, *s, p - *s);
317 ret = ldb_parse_filtercomp(ldb, s2);
327 main parser entry point. Takes a search string and returns a parse tree
329 expression ::= <simple> | <filter>
331 struct ldb_parse_tree *ldb_parse_tree(struct ldb_context *ldb, const char *s)
333 while (isspace(*s)) s++;
336 return ldb_parse_filter(ldb, &s);
339 return ldb_parse_simple(ldb, s);
343 free a parse tree returned from ldb_parse_tree()
345 void ldb_parse_tree_free(struct ldb_context *ldb, struct ldb_parse_tree *tree)
349 switch (tree->operation) {
351 ldb_free(ldb, tree->u.simple.attr);
352 if (tree->u.simple.value.data) ldb_free(ldb, tree->u.simple.value.data);
357 for (i=0;i<tree->u.list.num_elements;i++) {
358 ldb_parse_tree_free(ldb, tree->u.list.elements[i]);
360 if (tree->u.list.elements) ldb_free(ldb, tree->u.list.elements);
364 ldb_parse_tree_free(ldb, tree->u.not.child);