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"
50 a filter is defined by:
51 <filter> ::= '(' <filtercomp> ')'
52 <filtercomp> ::= <and> | <or> | <not> | <simple>
53 <and> ::= '&' <filterlist>
54 <or> ::= '|' <filterlist>
55 <not> ::= '!' <filter>
56 <filterlist> ::= <filter> | <filter> <filterlist>
57 <simple> ::= <attributetype> <filtertype> <attributevalue>
58 <filtertype> ::= '=' | '~=' | '<=' | '>='
61 #define LDB_ALL_SEP "()&|=!"
64 return next token element. Caller frees
66 static char *ldb_parse_lex(void *ctx, const char **s, const char *sep)
80 if (strchr(sep, *p)) {
82 ret = talloc_strndup(ctx, p, 1);
89 while (*p && (isalnum(*p) || !strchr(sep, *p))) {
97 ret = talloc_strndup(ctx, *s, p - *s);
108 find a matching close brace in a string
110 static const char *match_brace(const char *s)
112 unsigned int count = 0;
113 while (*s && (count != 0 || *s != ')')) {
129 decode a RFC2254 binary string representation of a buffer.
130 Used in LDAP filters.
132 struct ldb_val ldb_binary_decode(void *mem_ctx, const char *str)
136 int slen = str?strlen(str):0;
138 ret.data = talloc_size(mem_ctx, slen+1);
140 if (ret.data == NULL) return ret;
142 for (i=j=0;i<slen;i++) {
143 if (str[i] == '\\') {
145 if (sscanf(&str[i+1], "%02X", &c) != 1) {
146 talloc_free(ret.data);
147 memset(&ret, 0, sizeof(ret));
150 ((uint8_t *)ret.data)[j++] = c;
153 ((uint8_t *)ret.data)[j++] = str[i];
157 ((uint8_t *)ret.data)[j] = 0;
164 encode a blob as a RFC2254 binary string, escaping any
165 non-printable or '\' characters
167 char *ldb_binary_encode(void *mem_ctx, struct ldb_val val)
171 int len = val.length;
172 unsigned char *buf = val.data;
174 for (i=0;i<val.length;i++) {
175 if (!isprint(buf[i]) || strchr(" *()\\&|!", buf[i])) {
179 ret = talloc_array(mem_ctx, char, len+1);
180 if (ret == NULL) return NULL;
183 for (i=0;i<val.length;i++) {
184 if (!isprint(buf[i]) || strchr(" *()\\&|!", buf[i])) {
185 snprintf(ret+len, 4, "\\%02X", buf[i]);
197 static struct ldb_parse_tree *ldb_parse_filter(void *mem_ctx, const char **s);
201 parse an extended match
209 the ':dn' part sets the dnAttributes boolean if present
210 the oid sets the rule_id string
213 static struct ldb_parse_tree *ldb_parse_extended(struct ldb_parse_tree *ret,
214 char *attr, char *value)
217 ret->operation = LDB_OP_EXTENDED;
218 ret->u.extended.value = ldb_binary_decode(ret, value);
219 p1 = strchr(attr, ':');
220 if (p1 == NULL) goto failed;
221 p2 = strchr(p1+1, ':');
222 if (p2 == NULL) goto failed;
223 p3 = strchr(p2+1, ':');
229 ret->u.extended.attr = talloc_strdup(ret, attr);
230 if (ret->u.extended.attr == NULL) goto failed;
231 if (strcmp(p1+1, "dn") == 0) {
232 ret->u.extended.dnAttributes = 1;
234 ret->u.extended.rule_id = talloc_strdup(ret, p2+1);
235 if (ret->u.extended.rule_id == NULL) goto failed;
237 ret->u.extended.rule_id = NULL;
240 ret->u.extended.dnAttributes = 0;
241 ret->u.extended.rule_id = talloc_strdup(ret, p1+1);
242 if (ret->u.extended.rule_id == NULL) goto failed;
244 ret->u.extended.value = ldb_binary_decode(ret, value);
255 <simple> ::= <attributetype> <filtertype> <attributevalue>
257 static struct ldb_parse_tree *ldb_parse_simple(void *mem_ctx, const char *s)
260 struct ldb_parse_tree *ret;
262 ret = talloc(mem_ctx, struct ldb_parse_tree);
268 l = ldb_parse_lex(ret, &s, LDB_ALL_SEP);
274 if (strchr("()&|=", *l)) {
279 eq = ldb_parse_lex(ret, &s, LDB_ALL_SEP);
280 if (!eq || strcmp(eq, "=") != 0) {
286 val = ldb_parse_lex(ret, &s, ")");
287 if (val && strchr("()&|", *val)) {
292 if (l[strlen(l)-1] == ':') {
293 /* its an extended match */
294 return ldb_parse_extended(ret, l, val);
297 ret->operation = LDB_OP_SIMPLE;
298 ret->u.simple.attr = l;
299 ret->u.simple.value = ldb_binary_decode(ret, val);
307 <and> ::= '&' <filterlist>
308 <or> ::= '|' <filterlist>
309 <filterlist> ::= <filter> | <filter> <filterlist>
311 static struct ldb_parse_tree *ldb_parse_filterlist(void *mem_ctx,
312 enum ldb_parse_op op, const char *s)
314 struct ldb_parse_tree *ret, *next;
316 ret = talloc(mem_ctx, struct ldb_parse_tree);
323 ret->u.list.num_elements = 1;
324 ret->u.list.elements = talloc(ret, struct ldb_parse_tree *);
325 if (!ret->u.list.elements) {
331 ret->u.list.elements[0] = ldb_parse_filter(ret->u.list.elements, &s);
332 if (!ret->u.list.elements[0]) {
337 while (isspace(*s)) s++;
339 while (*s && (next = ldb_parse_filter(ret->u.list.elements, &s))) {
340 struct ldb_parse_tree **e;
341 e = talloc_realloc(ret, ret->u.list.elements,
342 struct ldb_parse_tree *,
343 ret->u.list.num_elements+1);
349 ret->u.list.elements = e;
350 ret->u.list.elements[ret->u.list.num_elements] = next;
351 ret->u.list.num_elements++;
352 while (isspace(*s)) s++;
360 <not> ::= '!' <filter>
362 static struct ldb_parse_tree *ldb_parse_not(void *mem_ctx, const char *s)
364 struct ldb_parse_tree *ret;
366 ret = talloc(mem_ctx, struct ldb_parse_tree);
372 ret->operation = LDB_OP_NOT;
373 ret->u.not.child = ldb_parse_filter(ret, &s);
374 if (!ret->u.not.child) {
384 <filtercomp> ::= <and> | <or> | <not> | <simple>
386 static struct ldb_parse_tree *ldb_parse_filtercomp(void *mem_ctx, const char *s)
388 while (isspace(*s)) s++;
392 return ldb_parse_filterlist(mem_ctx, LDB_OP_AND, s+1);
395 return ldb_parse_filterlist(mem_ctx, LDB_OP_OR, s+1);
398 return ldb_parse_not(mem_ctx, s+1);
405 return ldb_parse_simple(mem_ctx, s);
410 <filter> ::= '(' <filtercomp> ')'
412 static struct ldb_parse_tree *ldb_parse_filter(void *mem_ctx, const char **s)
416 struct ldb_parse_tree *ret;
418 l = ldb_parse_lex(mem_ctx, s, LDB_ALL_SEP);
423 if (strcmp(l, "(") != 0) {
435 s2 = talloc_strndup(mem_ctx, *s, p - *s);
441 ret = ldb_parse_filtercomp(mem_ctx, s2);
451 main parser entry point. Takes a search string and returns a parse tree
453 expression ::= <simple> | <filter>
455 struct ldb_parse_tree *ldb_parse_tree(void *mem_ctx, const char *s)
457 while (isspace(*s)) s++;
460 return ldb_parse_filter(mem_ctx, &s);
463 return ldb_parse_simple(mem_ctx, s);
468 construct a ldap parse filter given a parse tree
470 char *ldb_filter_from_tree(void *mem_ctx, struct ldb_parse_tree *tree)
475 switch (tree->operation) {
477 s = ldb_binary_encode(mem_ctx, tree->u.simple.value);
478 if (s == NULL) return NULL;
479 ret = talloc_asprintf(mem_ctx, "(%s=%s)",
480 tree->u.simple.attr, s);
483 case LDB_OP_EXTENDED:
484 s = ldb_binary_encode(mem_ctx, tree->u.extended.value);
485 if (s == NULL) return NULL;
486 ret = talloc_asprintf(mem_ctx, "(%s%s%s%s=%s)",
487 tree->u.extended.attr?tree->u.extended.attr:"",
488 tree->u.extended.dnAttributes?":dn":"",
489 tree->u.extended.rule_id?":":"",
490 tree->u.extended.rule_id?tree->u.extended.rule_id:"",
496 ret = talloc_asprintf(mem_ctx, "(%c", (char)tree->operation);
497 if (ret == NULL) return NULL;
498 for (i=0;i<tree->u.list.num_elements;i++) {
499 s = ldb_filter_from_tree(mem_ctx, tree->u.list.elements[i]);
504 s2 = talloc_asprintf_append(ret, "%s", s);
512 s = talloc_asprintf_append(ret, ")");
519 s = ldb_filter_from_tree(mem_ctx, tree->u.not.child);
520 if (s == NULL) return NULL;
522 ret = talloc_asprintf(mem_ctx, "(!%s)", s);