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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
|
/*
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 3 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, see <http://www.gnu.org/licenses/>.
*/
/*
* 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 "ldb_private.h"
#include "system/locale.h"
static int ldb_parse_hex2char(const char *x)
{
if (isxdigit(x[0]) && isxdigit(x[1])) {
const char h1 = x[0], h2 = x[1];
int c = 0;
if (h1 >= 'a') c = h1 - (int)'a' + 10;
else if (h1 >= 'A') c = h1 - (int)'A' + 10;
else if (h1 >= '0') c = h1 - (int)'0';
c = c << 4;
if (h2 >= 'a') c += h2 - (int)'a' + 10;
else if (h2 >= 'A') c += h2 - (int)'A' + 10;
else if (h2 >= '0') c += h2 - (int)'0';
return c;
}
return -1;
}
/*
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> ::= '=' | '~=' | '<=' | '>='
*/
/*
decode a RFC2254 binary string representation of a buffer.
Used in LDAP filters.
*/
struct ldb_val ldb_binary_decode(TALLOC_CTX *mem_ctx, const char *str)
{
size_t i, j;
struct ldb_val ret;
size_t slen = str?strlen(str):0;
ret.data = (uint8_t *)talloc_size(mem_ctx, slen+1);
ret.length = 0;
if (ret.data == NULL) return ret;
for (i=j=0;i<slen;i++) {
if (str[i] == '\\') {
int c;
c = ldb_parse_hex2char(&str[i+1]);
if (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;
((uint8_t *)ret.data)[j] = 0;
return ret;
}
static bool need_encode(unsigned char cval)
{
if (cval < 0x20 || cval > 0x7E || strchr(" *()\\&|!\"", cval)) {
return true;
}
return false;
}
/*
encode a blob as a RFC2254 binary string, escaping any
non-printable or '\' characters
*/
char *ldb_binary_encode(TALLOC_CTX *mem_ctx, struct ldb_val val)
{
size_t i;
char *ret;
size_t len = val.length;
unsigned char *buf = val.data;
for (i=0;i<val.length;i++) {
if (need_encode(buf[i])) {
len += 2;
}
}
ret = talloc_array(mem_ctx, char, len+1);
if (ret == NULL) return NULL;
len = 0;
for (i=0;i<val.length;i++) {
if (need_encode(buf[i])) {
snprintf(ret+len, 4, "\\%02X", buf[i]);
len += 3;
} else {
ret[len++] = buf[i];
}
}
ret[len] = 0;
return ret;
}
/*
encode a string as a RFC2254 binary string, escaping any
non-printable or '\' characters. This routine is suitable for use
in escaping user data in ldap filters.
*/
char *ldb_binary_encode_string(TALLOC_CTX *mem_ctx, const char *string)
{
struct ldb_val val;
if (string == NULL) {
return NULL;
}
val.data = discard_const_p(uint8_t, string);
val.length = strlen(string);
return ldb_binary_encode(mem_ctx, val);
}
/* find the first matching wildcard */
static char *ldb_parse_find_wildcard(char *value)
{
while (*value) {
value = strpbrk(value, "\\*");
if (value == NULL) return NULL;
if (value[0] == '\\') {
if (value[1] == '\0') return NULL;
value += 2;
continue;
}
if (value[0] == '*') return value;
}
return NULL;
}
/* return a NULL terminated list of binary strings representing the value
chunks separated by wildcards that makes the value portion of the filter
*/
static struct ldb_val **ldb_wildcard_decode(TALLOC_CTX *mem_ctx, const char *string)
{
struct ldb_val **ret = NULL;
unsigned int val = 0;
char *wc, *str;
wc = talloc_strdup(mem_ctx, string);
if (wc == NULL) return NULL;
while (wc && *wc) {
str = wc;
wc = ldb_parse_find_wildcard(str);
if (wc && *wc) {
if (wc == str) {
wc++;
continue;
}
*wc = 0;
wc++;
}
ret = talloc_realloc(mem_ctx, ret, struct ldb_val *, val + 2);
if (ret == NULL) return NULL;
ret[val] = talloc(mem_ctx, struct ldb_val);
if (ret[val] == NULL) return NULL;
*(ret[val]) = ldb_binary_decode(mem_ctx, str);
if ((ret[val])->data == NULL) return NULL;
val++;
}
if (ret != NULL) {
ret[val] = NULL;
}
return ret;
}
static struct ldb_parse_tree *ldb_parse_filter(TALLOC_CTX *mem_ctx, const char **s);
/*
parse an extended match
possible forms:
(attr:oid:=value)
(attr:dn:oid:=value)
(attr:dn:=value)
(:dn:oid:=value)
the ':dn' part sets the dnAttributes boolean if present
the oid sets the rule_id string
*/
static struct ldb_parse_tree *ldb_parse_extended(struct ldb_parse_tree *ret,
char *attr, char *value)
{
char *p1, *p2;
ret->operation = LDB_OP_EXTENDED;
ret->u.extended.value = ldb_binary_decode(ret, value);
if (ret->u.extended.value.data == NULL) goto failed;
p1 = strchr(attr, ':');
if (p1 == NULL) goto failed;
p2 = strchr(p1+1, ':');
*p1 = 0;
if (p2) *p2 = 0;
ret->u.extended.attr = attr;
if (strcmp(p1+1, "dn") == 0) {
ret->u.extended.dnAttributes = 1;
if (p2) {
ret->u.extended.rule_id = talloc_strdup(ret, p2+1);
if (ret->u.extended.rule_id == NULL) goto failed;
} else {
ret->u.extended.rule_id = NULL;
}
} else {
ret->u.extended.dnAttributes = 0;
ret->u.extended.rule_id = talloc_strdup(ret, p1+1);
if (ret->u.extended.rule_id == NULL) goto failed;
}
return ret;
failed:
talloc_free(ret);
return NULL;
}
static enum ldb_parse_op ldb_parse_filtertype(TALLOC_CTX *mem_ctx, char **type, char **value, const char **s)
{
enum ldb_parse_op filter = 0;
char *name, *val, *k;
const char *p = *s;
const char *t, *t1;
/* retrieve attributetype name */
t = p;
if (*p == '@') { /* for internal attributes the first char can be @ */
p++;
}
while ((isascii(*p) && isalnum((unsigned char)*p)) || (*p == '-') || (*p == '.')) {
/* attribute names can only be alphanums */
p++;
}
if (*p == ':') { /* but extended searches have : and . chars too */
p = strstr(p, ":=");
if (p == NULL) { /* malformed attribute name */
return 0;
}
}
t1 = p;
while (isspace((unsigned char)*p)) p++;
if (!strchr("=<>~:", *p)) {
return 0;
}
/* save name */
name = (char *)talloc_memdup(mem_ctx, t, t1 - t + 1);
if (name == NULL) return 0;
name[t1 - t] = '\0';
/* retrieve filtertype */
if (*p == '=') {
filter = LDB_OP_EQUALITY;
} else if (*(p + 1) == '=') {
switch (*p) {
case '<':
filter = LDB_OP_LESS;
p++;
break;
case '>':
filter = LDB_OP_GREATER;
p++;
break;
case '~':
filter = LDB_OP_APPROX;
p++;
break;
case ':':
filter = LDB_OP_EXTENDED;
p++;
break;
}
}
if (!filter) {
talloc_free(name);
return 0;
}
p++;
while (isspace((unsigned char)*p)) p++;
/* retrieve value */
t = p;
while (*p && ((*p != ')') || ((*p == ')') && (*(p - 1) == '\\')))) p++;
val = (char *)talloc_memdup(mem_ctx, t, p - t + 1);
if (val == NULL) {
talloc_free(name);
return 0;
}
val[p - t] = '\0';
k = &(val[p - t]);
/* remove trailing spaces from value */
while ((k > val) && (isspace((unsigned char)*(k - 1)))) k--;
*k = '\0';
*type = name;
*value = val;
*s = p;
return filter;
}
/*
<simple> ::= <attributetype> <filtertype> <attributevalue>
*/
static struct ldb_parse_tree *ldb_parse_simple(TALLOC_CTX *mem_ctx, const char **s)
{
char *attr, *value;
struct ldb_parse_tree *ret;
enum ldb_parse_op filtertype;
ret = talloc(mem_ctx, struct ldb_parse_tree);
if (!ret) {
errno = ENOMEM;
return NULL;
}
filtertype = ldb_parse_filtertype(ret, &attr, &value, s);
if (!filtertype) {
talloc_free(ret);
return NULL;
}
switch (filtertype) {
case LDB_OP_PRESENT:
ret->operation = LDB_OP_PRESENT;
ret->u.present.attr = attr;
break;
case LDB_OP_EQUALITY:
if (strcmp(value, "*") == 0) {
ret->operation = LDB_OP_PRESENT;
ret->u.present.attr = attr;
break;
}
if (ldb_parse_find_wildcard(value) != NULL) {
ret->operation = LDB_OP_SUBSTRING;
ret->u.substring.attr = attr;
ret->u.substring.start_with_wildcard = 0;
ret->u.substring.end_with_wildcard = 0;
ret->u.substring.chunks = ldb_wildcard_decode(ret, value);
if (ret->u.substring.chunks == NULL){
talloc_free(ret);
return NULL;
}
if (value[0] == '*')
ret->u.substring.start_with_wildcard = 1;
if (value[strlen(value) - 1] == '*')
ret->u.substring.end_with_wildcard = 1;
talloc_free(value);
break;
}
ret->operation = LDB_OP_EQUALITY;
ret->u.equality.attr = attr;
ret->u.equality.value = ldb_binary_decode(ret, value);
if (ret->u.equality.value.data == NULL) {
talloc_free(ret);
return NULL;
}
talloc_free(value);
break;
case LDB_OP_GREATER:
ret->operation = LDB_OP_GREATER;
ret->u.comparison.attr = attr;
ret->u.comparison.value = ldb_binary_decode(ret, value);
if (ret->u.comparison.value.data == NULL) {
talloc_free(ret);
return NULL;
}
talloc_free(value);
break;
case LDB_OP_LESS:
ret->operation = LDB_OP_LESS;
ret->u.comparison.attr = attr;
ret->u.comparison.value = ldb_binary_decode(ret, value);
if (ret->u.comparison.value.data == NULL) {
talloc_free(ret);
return NULL;
}
talloc_free(value);
break;
case LDB_OP_APPROX:
ret->operation = LDB_OP_APPROX;
ret->u.comparison.attr = attr;
ret->u.comparison.value = ldb_binary_decode(ret, value);
if (ret->u.comparison.value.data == NULL) {
talloc_free(ret);
return NULL;
}
talloc_free(value);
break;
case LDB_OP_EXTENDED:
ret = ldb_parse_extended(ret, attr, value);
break;
default:
talloc_free(ret);
return NULL;
}
return ret;
}
/*
parse a filterlist
<and> ::= '&' <filterlist>
<or> ::= '|' <filterlist>
<filterlist> ::= <filter> | <filter> <filterlist>
*/
static struct ldb_parse_tree *ldb_parse_filterlist(TALLOC_CTX *mem_ctx, const char **s)
{
struct ldb_parse_tree *ret, *next;
enum ldb_parse_op op;
const char *p = *s;
switch (*p) {
case '&':
op = LDB_OP_AND;
break;
case '|':
op = LDB_OP_OR;
break;
default:
return NULL;
}
p++;
while (isspace((unsigned char)*p)) p++;
ret = talloc(mem_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, &p);
if (!ret->u.list.elements[0]) {
talloc_free(ret);
return NULL;
}
while (isspace((unsigned char)*p)) p++;
while (*p) {
struct ldb_parse_tree **e;
if (*p == ')') {
break;
}
next = ldb_parse_filter(ret->u.list.elements, &p);
if (next == NULL) {
/* an invalid filter element */
talloc_free(ret);
return NULL;
}
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((unsigned char)*p)) p++;
}
*s = p;
return ret;
}
/*
<not> ::= '!' <filter>
*/
static struct ldb_parse_tree *ldb_parse_not(TALLOC_CTX *mem_ctx, const char **s)
{
struct ldb_parse_tree *ret;
const char *p = *s;
if (*p != '!') {
return NULL;
}
p++;
ret = talloc(mem_ctx, struct ldb_parse_tree);
if (!ret) {
errno = ENOMEM;
return NULL;
}
ret->operation = LDB_OP_NOT;
ret->u.isnot.child = ldb_parse_filter(ret, &p);
if (!ret->u.isnot.child) {
talloc_free(ret);
return NULL;
}
*s = p;
return ret;
}
/*
parse a filtercomp
<filtercomp> ::= <and> | <or> | <not> | <simple>
*/
static struct ldb_parse_tree *ldb_parse_filtercomp(TALLOC_CTX *mem_ctx, const char **s)
{
struct ldb_parse_tree *ret;
const char *p = *s;
while (isspace((unsigned char)*p)) p++;
switch (*p) {
case '&':
ret = ldb_parse_filterlist(mem_ctx, &p);
break;
case '|':
ret = ldb_parse_filterlist(mem_ctx, &p);
break;
case '!':
ret = ldb_parse_not(mem_ctx, &p);
break;
case '(':
case ')':
return NULL;
default:
ret = ldb_parse_simple(mem_ctx, &p);
}
*s = p;
return ret;
}
/*
<filter> ::= '(' <filtercomp> ')'
*/
static struct ldb_parse_tree *ldb_parse_filter(TALLOC_CTX *mem_ctx, const char **s)
{
struct ldb_parse_tree *ret;
const char *p = *s;
if (*p != '(') {
return NULL;
}
p++;
ret = ldb_parse_filtercomp(mem_ctx, &p);
if (*p != ')') {
return NULL;
}
p++;
while (isspace((unsigned char)*p)) {
p++;
}
*s = p;
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)
{
if (s == NULL || *s == 0) {
s = "(|(objectClass=*)(distinguishedName=*))";
}
while (isspace((unsigned char)*s)) s++;
if (*s == '(') {
return ldb_parse_filter(mem_ctx, &s);
}
return ldb_parse_simple(mem_ctx, &s);
}
/*
construct a ldap parse filter given a parse tree
*/
char *ldb_filter_from_tree(TALLOC_CTX *mem_ctx, const struct ldb_parse_tree *tree)
{
char *s, *s2, *ret;
unsigned int i;
if (tree == NULL) {
return NULL;
}
switch (tree->operation) {
case LDB_OP_AND:
case LDB_OP_OR:
ret = talloc_asprintf(mem_ctx, "(%c", tree->operation==LDB_OP_AND?'&':'|');
if (ret == NULL) return NULL;
for (i=0;i<tree->u.list.num_elements;i++) {
s = ldb_filter_from_tree(mem_ctx, tree->u.list.elements[i]);
if (s == NULL) {
talloc_free(ret);
return NULL;
}
s2 = talloc_asprintf_append(ret, "%s", s);
talloc_free(s);
if (s2 == NULL) {
talloc_free(ret);
return NULL;
}
ret = s2;
}
s = talloc_asprintf_append(ret, ")");
if (s == NULL) {
talloc_free(ret);
return NULL;
}
return s;
case LDB_OP_NOT:
s = ldb_filter_from_tree(mem_ctx, tree->u.isnot.child);
if (s == NULL) return NULL;
ret = talloc_asprintf(mem_ctx, "(!%s)", s);
talloc_free(s);
return ret;
case LDB_OP_EQUALITY:
s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
if (s == NULL) return NULL;
ret = talloc_asprintf(mem_ctx, "(%s=%s)",
tree->u.equality.attr, s);
talloc_free(s);
return ret;
case LDB_OP_SUBSTRING:
ret = talloc_asprintf(mem_ctx, "(%s=%s", tree->u.substring.attr,
tree->u.substring.start_with_wildcard?"*":"");
if (ret == NULL) return NULL;
for (i = 0; tree->u.substring.chunks && tree->u.substring.chunks[i]; i++) {
s2 = ldb_binary_encode(mem_ctx, *(tree->u.substring.chunks[i]));
if (s2 == NULL) {
talloc_free(ret);
return NULL;
}
if (tree->u.substring.chunks[i+1] ||
tree->u.substring.end_with_wildcard) {
s = talloc_asprintf_append(ret, "%s*", s2);
} else {
s = talloc_asprintf_append(ret, "%s", s2);
}
if (s == NULL) {
talloc_free(ret);
return NULL;
}
ret = s;
}
s = talloc_asprintf_append(ret, ")");
if (s == NULL) {
talloc_free(ret);
return NULL;
}
ret = s;
return ret;
case LDB_OP_GREATER:
s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
if (s == NULL) return NULL;
ret = talloc_asprintf(mem_ctx, "(%s>=%s)",
tree->u.equality.attr, s);
talloc_free(s);
return ret;
case LDB_OP_LESS:
s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
if (s == NULL) return NULL;
ret = talloc_asprintf(mem_ctx, "(%s<=%s)",
tree->u.equality.attr, s);
talloc_free(s);
return ret;
case LDB_OP_PRESENT:
ret = talloc_asprintf(mem_ctx, "(%s=*)", tree->u.present.attr);
return ret;
case LDB_OP_APPROX:
s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
if (s == NULL) return NULL;
ret = talloc_asprintf(mem_ctx, "(%s~=%s)",
tree->u.equality.attr, s);
talloc_free(s);
return ret;
case LDB_OP_EXTENDED:
s = ldb_binary_encode(mem_ctx, tree->u.extended.value);
if (s == NULL) return NULL;
ret = talloc_asprintf(mem_ctx, "(%s%s%s%s:=%s)",
tree->u.extended.attr?tree->u.extended.attr:"",
tree->u.extended.dnAttributes?":dn":"",
tree->u.extended.rule_id?":":"",
tree->u.extended.rule_id?tree->u.extended.rule_id:"",
s);
talloc_free(s);
return ret;
}
return NULL;
}
/*
walk a parse tree, calling the provided callback on each node
*/
int ldb_parse_tree_walk(struct ldb_parse_tree *tree,
int (*callback)(struct ldb_parse_tree *tree, void *),
void *private_context)
{
unsigned int i;
int ret;
ret = callback(tree, private_context);
if (ret != LDB_SUCCESS) {
return ret;
}
switch (tree->operation) {
case LDB_OP_AND:
case LDB_OP_OR:
for (i=0;i<tree->u.list.num_elements;i++) {
ret = ldb_parse_tree_walk(tree->u.list.elements[i], callback, private_context);
if (ret != LDB_SUCCESS) {
return ret;
}
}
break;
case LDB_OP_NOT:
ret = ldb_parse_tree_walk(tree->u.isnot.child, callback, private_context);
if (ret != LDB_SUCCESS) {
return ret;
}
break;
case LDB_OP_EQUALITY:
case LDB_OP_GREATER:
case LDB_OP_LESS:
case LDB_OP_APPROX:
case LDB_OP_SUBSTRING:
case LDB_OP_PRESENT:
case LDB_OP_EXTENDED:
break;
}
return LDB_SUCCESS;
}
struct parse_tree_attr_replace_ctx {
const char *attr;
const char *replace;
};
/*
callback for ldb_parse_tree_attr_replace()
*/
static int parse_tree_attr_replace(struct ldb_parse_tree *tree, void *private_context)
{
struct parse_tree_attr_replace_ctx *ctx = private_context;
switch (tree->operation) {
case LDB_OP_EQUALITY:
case LDB_OP_GREATER:
case LDB_OP_LESS:
case LDB_OP_APPROX:
if (ldb_attr_cmp(tree->u.equality.attr, ctx->attr) == 0) {
tree->u.equality.attr = ctx->replace;
}
break;
case LDB_OP_SUBSTRING:
if (ldb_attr_cmp(tree->u.substring.attr, ctx->attr) == 0) {
tree->u.substring.attr = ctx->replace;
}
break;
case LDB_OP_PRESENT:
if (ldb_attr_cmp(tree->u.present.attr, ctx->attr) == 0) {
tree->u.present.attr = ctx->replace;
}
break;
case LDB_OP_EXTENDED:
if (tree->u.extended.attr &&
ldb_attr_cmp(tree->u.extended.attr, ctx->attr) == 0) {
tree->u.extended.attr = ctx->replace;
}
break;
default:
break;
}
return LDB_SUCCESS;
}
/*
replace any occurrences of an attribute name in the parse tree with a
new name
*/
void ldb_parse_tree_attr_replace(struct ldb_parse_tree *tree,
const char *attr,
const char *replace)
{
struct parse_tree_attr_replace_ctx ctx;
ctx.attr = attr;
ctx.replace = replace;
ldb_parse_tree_walk(tree, parse_tree_attr_replace, &ctx);
}
/*
shallow copy a tree - copying only the elements array so that the caller
can safely add new elements without changing the message
*/
struct ldb_parse_tree *ldb_parse_tree_copy_shallow(TALLOC_CTX *mem_ctx,
const struct ldb_parse_tree *ot)
{
unsigned int i;
struct ldb_parse_tree *nt;
nt = talloc(mem_ctx, struct ldb_parse_tree);
if (!nt) {
return NULL;
}
*nt = *ot;
switch (ot->operation) {
case LDB_OP_AND:
case LDB_OP_OR:
nt->u.list.elements = talloc_array(nt, struct ldb_parse_tree *,
ot->u.list.num_elements);
if (!nt->u.list.elements) {
talloc_free(nt);
return NULL;
}
for (i=0;i<ot->u.list.num_elements;i++) {
nt->u.list.elements[i] =
ldb_parse_tree_copy_shallow(nt->u.list.elements,
ot->u.list.elements[i]);
if (!nt->u.list.elements[i]) {
talloc_free(nt);
return NULL;
}
}
break;
case LDB_OP_NOT:
nt->u.isnot.child = ldb_parse_tree_copy_shallow(nt,
ot->u.isnot.child);
if (!nt->u.isnot.child) {
talloc_free(nt);
return NULL;
}
break;
case LDB_OP_EQUALITY:
case LDB_OP_GREATER:
case LDB_OP_LESS:
case LDB_OP_APPROX:
case LDB_OP_SUBSTRING:
case LDB_OP_PRESENT:
case LDB_OP_EXTENDED:
break;
}
return nt;
}
|