1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
/*
ldb database library
Copyright (C) Andrew Tridgell 2004
** NOTE! The following LGPL license applies to the ldb
** library. This does NOT imply that all of Samba is released
** under the LGPL
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Name: ldb
*
* Component: ldb expression parsing
*
* Description: parse LDAP-like search expressions
*
* Author: Andrew Tridgell
*/
/*
TODO:
- add RFC2254 binary string handling
- possibly add ~=, <= and >= handling
- expand the test suite
- add better parse error handling
*/
#include "includes.h"
#include "ldb/include/ldb.h"
#include "ldb/include/ldb_parse.h"
#include <ctype.h>
/*
a filter is defined by:
<filter> ::= '(' <filtercomp> ')'
<filtercomp> ::= <and> | <or> | <not> | <simple>
<and> ::= '&' <filterlist>
<or> ::= '|' <filterlist>
<not> ::= '!' <filter>
<filterlist> ::= <filter> | <filter> <filterlist>
<simple> ::= <attributetype> <filtertype> <attributevalue>
<filtertype> ::= '=' | '~=' | '<=' | '>='
*/
#define LDB_ALL_SEP "()&|=!"
/*
return next token element. Caller frees
*/
static char *ldb_parse_lex(TALLOC_CTX *ctx, const char **s, const char *sep)
{
const char *p = *s;
char *ret;
while (isspace(*p)) {
p++;
}
*s = p;
if (*p == 0) {
return NULL;
}
if (strchr(sep, *p)) {
(*s) = p+1;
ret = talloc_strndup(ctx, p, 1);
if (!ret) {
errno = ENOMEM;
}
return ret;
}
while (*p && (isalnum(*p) || !strchr(sep, *p))) {
p++;
}
if (p == *s) {
return NULL;
}
ret = talloc_strndup(ctx, *s, p - *s);
if (!ret) {
errno = ENOMEM;
}
*s = p;
return ret;
}
/*
find a matching close brace in a string
*/
static const char *match_brace(const char *s)
{
unsigned int count = 0;
while (*s && (count != 0 || *s != ')')) {
if (*s == '(') {
count++;
}
if (*s == ')') {
count--;
}
s++;
}
if (! *s) {
return NULL;
}
return s;
}
/*
decode a RFC2254 binary string representation of a buffer.
Used in LDAP filters.
*/
struct ldb_val ldb_binary_decode(TALLOC_CTX *ctx, const char *str)
{
int i, j;
struct ldb_val ret;
int slen = strlen(str);
ret.data = talloc_size(ctx, slen);
ret.length = 0;
if (ret.data == NULL) return ret;
for (i=j=0;i<slen;i++) {
if (str[i] == '\\') {
unsigned c;
if (sscanf(&str[i+1], "%02X", &c) != 1) {
talloc_free(ret.data);
memset(&ret, 0, sizeof(ret));
return ret;
}
((uint8_t *)ret.data)[j++] = c;
i += 2;
} else {
((uint8_t *)ret.data)[j++] = str[i];
}
}
ret.length = j;
return ret;
}
/*
encode a blob as a RFC2254 binary string, escaping any
non-printable or '\' characters
*/
const char *ldb_binary_encode(TALLOC_CTX *ctx, struct ldb_val val)
{
int i;
char *ret;
int len = val.length;
unsigned char *buf = val.data;
for (i=0;i<val.length;i++) {
if (!isprint(buf[i]) || strchr(" *()\\&|!", buf[i])) {
len += 2;
}
}
ret = talloc_array(ctx, char, len+1);
if (ret == NULL) return NULL;
len = 0;
for (i=0;i<val.length;i++) {
if (!isprint(buf[i]) || strchr(" *()\\&|!", buf[i])) {
snprintf(ret+len, 4, "\\%02X", buf[i]);
len += 3;
} else {
ret[len++] = buf[i];
}
}
ret[len] = 0;
return ret;
}
static struct ldb_parse_tree *ldb_parse_filter(TALLOC_CTX *ctx, const char **s);
/*
<simple> ::= <attributetype> <filtertype> <attributevalue>
*/
static struct ldb_parse_tree *ldb_parse_simple(TALLOC_CTX *ctx, const char *s)
{
char *eq, *val, *l;
struct ldb_parse_tree *ret;
ret = talloc(ctx, struct ldb_parse_tree);
if (!ret) {
errno = ENOMEM;
return NULL;
}
l = ldb_parse_lex(ret, &s, LDB_ALL_SEP);
if (!l) {
talloc_free(ret);
return NULL;
}
if (strchr("()&|=", *l)) {
talloc_free(ret);
return NULL;
}
eq = ldb_parse_lex(ret, &s, LDB_ALL_SEP);
if (!eq || strcmp(eq, "=") != 0) {
talloc_free(ret);
return NULL;
}
talloc_free(eq);
val = ldb_parse_lex(ret, &s, ")");
if (val && strchr("()&|", *val)) {
talloc_free(ret);
return NULL;
}
ret->operation = LDB_OP_SIMPLE;
ret->u.simple.attr = l;
ret->u.simple.value = ldb_binary_decode(ret, val);
return ret;
}
/*
parse a filterlist
<and> ::= '&' <filterlist>
<or> ::= '|' <filterlist>
<filterlist> ::= <filter> | <filter> <filterlist>
*/
static struct ldb_parse_tree *ldb_parse_filterlist(TALLOC_CTX *ctx,
enum ldb_parse_op op, const char *s)
{
struct ldb_parse_tree *ret, *next;
ret = talloc(ctx, struct ldb_parse_tree);
if (!ret) {
errno = ENOMEM;
return NULL;
}
ret->operation = op;
ret->u.list.num_elements = 1;
ret->u.list.elements = talloc(ret, struct ldb_parse_tree *);
if (!ret->u.list.elements) {
errno = ENOMEM;
talloc_free(ret);
return NULL;
}
ret->u.list.elements[0] = ldb_parse_filter(ret->u.list.elements, &s);
if (!ret->u.list.elements[0]) {
talloc_free(ret);
return NULL;
}
while (isspace(*s)) s++;
while (*s && (next = ldb_parse_filter(ret->u.list.elements, &s))) {
struct ldb_parse_tree **e;
e = talloc_realloc(ret, ret->u.list.elements,
struct ldb_parse_tree *,
ret->u.list.num_elements+1);
if (!e) {
errno = ENOMEM;
talloc_free(ret);
return NULL;
}
ret->u.list.elements = e;
ret->u.list.elements[ret->u.list.num_elements] = next;
ret->u.list.num_elements++;
while (isspace(*s)) s++;
}
return ret;
}
/*
<not> ::= '!' <filter>
*/
static struct ldb_parse_tree *ldb_parse_not(TALLOC_CTX *ctx, const char *s)
{
struct ldb_parse_tree *ret;
ret = talloc(ctx, struct ldb_parse_tree);
if (!ret) {
errno = ENOMEM;
return NULL;
}
ret->operation = LDB_OP_NOT;
ret->u.not.child = ldb_parse_filter(ret, &s);
if (!ret->u.not.child) {
talloc_free(ret);
return NULL;
}
return ret;
}
/*
parse a filtercomp
<filtercomp> ::= <and> | <or> | <not> | <simple>
*/
static struct ldb_parse_tree *ldb_parse_filtercomp(TALLOC_CTX *ctx, const char *s)
{
while (isspace(*s)) s++;
switch (*s) {
case '&':
return ldb_parse_filterlist(ctx, LDB_OP_AND, s+1);
case '|':
return ldb_parse_filterlist(ctx, LDB_OP_OR, s+1);
case '!':
return ldb_parse_not(ctx, s+1);
case '(':
case ')':
return NULL;
}
return ldb_parse_simple(ctx, s);
}
/*
<filter> ::= '(' <filtercomp> ')'
*/
static struct ldb_parse_tree *ldb_parse_filter(TALLOC_CTX *ctx, const char **s)
{
char *l, *s2;
const char *p, *p2;
struct ldb_parse_tree *ret;
l = ldb_parse_lex(ctx, s, LDB_ALL_SEP);
if (!l) {
return NULL;
}
if (strcmp(l, "(") != 0) {
talloc_free(l);
return NULL;
}
talloc_free(l);
p = match_brace(*s);
if (!p) {
return NULL;
}
p2 = p + 1;
s2 = talloc_strndup(ctx, *s, p - *s);
if (!s2) {
errno = ENOMEM;
return NULL;
}
ret = ldb_parse_filtercomp(ctx, s2);
talloc_free(s2);
*s = p2;
return ret;
}
/*
main parser entry point. Takes a search string and returns a parse tree
expression ::= <simple> | <filter>
*/
struct ldb_parse_tree *ldb_parse_tree(TALLOC_CTX *mem_ctx, const char *s)
{
while (isspace(*s)) s++;
if (*s == '(') {
return ldb_parse_filter(mem_ctx, &s);
}
return ldb_parse_simple(mem_ctx, s);
}
|