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/>.
25 #include "lib/ldb_compat.h"
27 static struct ldb_parse_tree
*ldb_parse_filter(void *mem_ctx
, const char **s
);
29 static int ldb_parse_hex2char(const char *x
)
31 if (isxdigit(x
[0]) && isxdigit(x
[1])) {
32 const char h1
= x
[0], h2
= x
[1];
35 if (h1
>= 'a') c
= h1
- (int)'a' + 10;
36 else if (h1
>= 'A') c
= h1
- (int)'A' + 10;
37 else if (h1
>= '0') c
= h1
- (int)'0';
39 if (h2
>= 'a') c
+= h2
- (int)'a' + 10;
40 else if (h2
>= 'A') c
+= h2
- (int)'A' + 10;
41 else if (h2
>= '0') c
+= h2
- (int)'0';
52 decode a RFC2254 binary string representation of a buffer.
55 static struct ldb_val
ldb_binary_decode(void *mem_ctx
, const char *str
)
59 size_t slen
= str
?strlen(str
):0;
61 ret
.data
= (uint8_t *)talloc_size(mem_ctx
, slen
+1);
63 if (ret
.data
== NULL
) return ret
;
65 for (i
=j
=0;i
<slen
;i
++) {
69 c
= ldb_parse_hex2char(&str
[i
+1]);
71 talloc_free(ret
.data
);
72 memset(&ret
, 0, sizeof(ret
));
75 ((uint8_t *)ret
.data
)[j
++] = c
;
78 ((uint8_t *)ret
.data
)[j
++] = str
[i
];
82 ((uint8_t *)ret
.data
)[j
] = 0;
87 static bool need_encode(unsigned char cval
)
89 if (cval
< 0x20 || cval
> 0x7E || strchr(" *()\\&|!\"", cval
)) {
96 encode a blob as a RFC2254 binary string, escaping any
97 non-printable or '\' characters
99 char *ldb_binary_encode(void *mem_ctx
, struct ldb_val val
)
103 size_t len
= val
.length
;
104 unsigned char *buf
= val
.data
;
106 for (i
=0;i
<val
.length
;i
++) {
107 if (need_encode(buf
[i
])) {
111 ret
= talloc_array(mem_ctx
, char, len
+1);
112 if (ret
== NULL
) return NULL
;
115 for (i
=0;i
<val
.length
;i
++) {
116 if (need_encode(buf
[i
])) {
117 snprintf(ret
+len
, 4, "\\%02X", buf
[i
]);
131 static enum ldb_parse_op
ldb_parse_filtertype(void *mem_ctx
, char **type
, char **value
, const char **s
)
133 enum ldb_parse_op filter
= 0;
134 char *name
, *val
, *k
;
138 /* retrieve attributetype name */
141 if (*p
== '@') { /* for internal attributes the first char can be @ */
145 while ((isascii(*p
) && isalnum((unsigned char)*p
)) || (*p
== '-') || (*p
== '.')) {
146 /* attribute names can only be alphanums */
150 if (*p
== ':') { /* but extended searches have : and . chars too */
152 if (p
== NULL
) { /* malformed attribute name */
159 while (isspace((unsigned char)*p
)) p
++;
161 if (!strchr("=<>~:", *p
)) {
166 name
= (char *)talloc_memdup(mem_ctx
, t
, t1
- t
+ 1);
167 if (name
== NULL
) return 0;
170 /* retrieve filtertype */
173 filter
= LDB_OP_EQUALITY
;
174 } else if (*(p
+ 1) == '=') {
177 filter
= LDB_OP_LESS
;
181 filter
= LDB_OP_GREATER
;
185 filter
= LDB_OP_APPROX
;
189 filter
= LDB_OP_EXTENDED
;
200 while (isspace((unsigned char)*p
)) p
++;
205 while (*p
&& ((*p
!= ')') || ((*p
== ')') && (*(p
- 1) == '\\')))) p
++;
207 val
= (char *)talloc_memdup(mem_ctx
, t
, p
- t
+ 1);
216 /* remove trailing spaces from value */
217 while ((k
> val
) && (isspace((unsigned char)*(k
- 1)))) k
--;
226 /* find the first matching wildcard */
227 static char *ldb_parse_find_wildcard(char *value
)
230 value
= strpbrk(value
, "\\*");
231 if (value
== NULL
) return NULL
;
233 if (value
[0] == '\\') {
234 if (value
[1] == '\0') return NULL
;
239 if (value
[0] == '*') return value
;
245 /* return a NULL terminated list of binary strings representing the value
246 chunks separated by wildcards that makes the value portion of the filter
248 static struct ldb_val
**ldb_wildcard_decode(void *mem_ctx
, const char *string
)
250 struct ldb_val
**ret
= NULL
;
251 unsigned int val
= 0;
254 wc
= talloc_strdup(mem_ctx
, string
);
255 if (wc
== NULL
) return NULL
;
259 wc
= ldb_parse_find_wildcard(str
);
269 ret
= talloc_realloc(mem_ctx
, ret
, struct ldb_val
*, val
+ 2);
270 if (ret
== NULL
) return NULL
;
272 ret
[val
] = talloc(mem_ctx
, struct ldb_val
);
273 if (ret
[val
] == NULL
) return NULL
;
275 *(ret
[val
]) = ldb_binary_decode(mem_ctx
, str
);
276 if ((ret
[val
])->data
== NULL
) return NULL
;
289 parse an extended match
297 the ':dn' part sets the dnAttributes boolean if present
298 the oid sets the rule_id string
301 static struct ldb_parse_tree
*ldb_parse_extended(struct ldb_parse_tree
*ret
,
302 char *attr
, char *value
)
306 ret
->operation
= LDB_OP_EXTENDED
;
307 ret
->u
.extended
.value
= ldb_binary_decode(ret
, value
);
308 if (ret
->u
.extended
.value
.data
== NULL
) goto failed
;
310 p1
= strchr(attr
, ':');
311 if (p1
== NULL
) goto failed
;
312 p2
= strchr(p1
+1, ':');
317 ret
->u
.extended
.attr
= attr
;
318 if (strcmp(p1
+1, "dn") == 0) {
319 ret
->u
.extended
.dnAttributes
= 1;
321 ret
->u
.extended
.rule_id
= talloc_strdup(ret
, p2
+1);
322 if (ret
->u
.extended
.rule_id
== NULL
) goto failed
;
324 ret
->u
.extended
.rule_id
= NULL
;
327 ret
->u
.extended
.dnAttributes
= 0;
328 ret
->u
.extended
.rule_id
= talloc_strdup(ret
, p1
+1);
329 if (ret
->u
.extended
.rule_id
== NULL
) goto failed
;
341 <simple> ::= <attributetype> <filtertype> <attributevalue>
343 static struct ldb_parse_tree
*ldb_parse_simple(void *mem_ctx
, const char **s
)
346 struct ldb_parse_tree
*ret
;
347 enum ldb_parse_op filtertype
;
349 ret
= talloc(mem_ctx
, struct ldb_parse_tree
);
355 filtertype
= ldb_parse_filtertype(ret
, &attr
, &value
, s
);
361 switch (filtertype
) {
364 ret
->operation
= LDB_OP_PRESENT
;
365 ret
->u
.present
.attr
= attr
;
368 case LDB_OP_EQUALITY
:
370 if (strcmp(value
, "*") == 0) {
371 ret
->operation
= LDB_OP_PRESENT
;
372 ret
->u
.present
.attr
= attr
;
376 if (ldb_parse_find_wildcard(value
) != NULL
) {
377 ret
->operation
= LDB_OP_SUBSTRING
;
378 ret
->u
.substring
.attr
= attr
;
379 ret
->u
.substring
.start_with_wildcard
= 0;
380 ret
->u
.substring
.end_with_wildcard
= 0;
381 ret
->u
.substring
.chunks
= ldb_wildcard_decode(ret
, value
);
382 if (ret
->u
.substring
.chunks
== NULL
){
387 ret
->u
.substring
.start_with_wildcard
= 1;
388 if (value
[strlen(value
) - 1] == '*')
389 ret
->u
.substring
.end_with_wildcard
= 1;
395 ret
->operation
= LDB_OP_EQUALITY
;
396 ret
->u
.equality
.attr
= attr
;
397 ret
->u
.equality
.value
= ldb_binary_decode(ret
, value
);
398 if (ret
->u
.equality
.value
.data
== NULL
) {
406 ret
->operation
= LDB_OP_GREATER
;
407 ret
->u
.comparison
.attr
= attr
;
408 ret
->u
.comparison
.value
= ldb_binary_decode(ret
, value
);
409 if (ret
->u
.comparison
.value
.data
== NULL
) {
417 ret
->operation
= LDB_OP_LESS
;
418 ret
->u
.comparison
.attr
= attr
;
419 ret
->u
.comparison
.value
= ldb_binary_decode(ret
, value
);
420 if (ret
->u
.comparison
.value
.data
== NULL
) {
428 ret
->operation
= LDB_OP_APPROX
;
429 ret
->u
.comparison
.attr
= attr
;
430 ret
->u
.comparison
.value
= ldb_binary_decode(ret
, value
);
431 if (ret
->u
.comparison
.value
.data
== NULL
) {
438 case LDB_OP_EXTENDED
:
440 ret
= ldb_parse_extended(ret
, attr
, value
);
453 <and> ::= '&' <filterlist>
454 <or> ::= '|' <filterlist>
455 <filterlist> ::= <filter> | <filter> <filterlist>
457 static struct ldb_parse_tree
*ldb_parse_filterlist(void *mem_ctx
, const char **s
)
459 struct ldb_parse_tree
*ret
, *next
;
460 enum ldb_parse_op op
;
475 while (isspace((unsigned char)*p
)) p
++;
477 ret
= talloc(mem_ctx
, struct ldb_parse_tree
);
484 ret
->u
.list
.num_elements
= 1;
485 ret
->u
.list
.elements
= talloc(ret
, struct ldb_parse_tree
*);
486 if (!ret
->u
.list
.elements
) {
492 ret
->u
.list
.elements
[0] = ldb_parse_filter(ret
->u
.list
.elements
, &p
);
493 if (!ret
->u
.list
.elements
[0]) {
498 while (isspace((unsigned char)*p
)) p
++;
500 while (*p
&& (next
= ldb_parse_filter(ret
->u
.list
.elements
, &p
))) {
501 struct ldb_parse_tree
**e
;
502 e
= talloc_realloc(ret
, ret
->u
.list
.elements
,
503 struct ldb_parse_tree
*,
504 ret
->u
.list
.num_elements
+ 1);
510 ret
->u
.list
.elements
= e
;
511 ret
->u
.list
.elements
[ret
->u
.list
.num_elements
] = next
;
512 ret
->u
.list
.num_elements
++;
513 while (isspace((unsigned char)*p
)) p
++;
522 <not> ::= '!' <filter>
524 static struct ldb_parse_tree
*ldb_parse_not(void *mem_ctx
, const char **s
)
526 struct ldb_parse_tree
*ret
;
534 ret
= talloc(mem_ctx
, struct ldb_parse_tree
);
540 ret
->operation
= LDB_OP_NOT
;
541 ret
->u
.isnot
.child
= ldb_parse_filter(ret
, &p
);
542 if (!ret
->u
.isnot
.child
) {
556 <filtercomp> ::= <and> | <or> | <not> | <simple>
558 static struct ldb_parse_tree
*ldb_parse_filtercomp(void *mem_ctx
, const char **s
)
560 struct ldb_parse_tree
*ret
;
563 while (isspace((unsigned char)*p
)) p
++;
567 ret
= ldb_parse_filterlist(mem_ctx
, &p
);
571 ret
= ldb_parse_filterlist(mem_ctx
, &p
);
575 ret
= ldb_parse_not(mem_ctx
, &p
);
583 ret
= ldb_parse_simple(mem_ctx
, &p
);
594 <filter> ::= '(' <filtercomp> ')'
596 static struct ldb_parse_tree
*ldb_parse_filter(void *mem_ctx
, const char **s
)
598 struct ldb_parse_tree
*ret
;
606 ret
= ldb_parse_filtercomp(mem_ctx
, &p
);
613 while (isspace((unsigned char)*p
)) {
625 main parser entry point. Takes a search string and returns a parse tree
627 expression ::= <simple> | <filter>
629 struct ldb_parse_tree
*ldb_parse_tree(void *mem_ctx
, const char *s
)
631 if (s
== NULL
|| *s
== 0) {
632 s
= "(|(objectClass=*)(distinguishedName=*))";
635 while (isspace((unsigned char)*s
)) s
++;
638 return ldb_parse_filter(mem_ctx
, &s
);
641 return ldb_parse_simple(mem_ctx
, &s
);