Fix bug #9147 - winbind can't fetch user or group info from AD via LDAP
[Samba/vl.git] / lib / ldb / common / ldb_parse.c
blobf47ef4337619016451c8c19c622d7dca0e2123b8
1 /*
2 ldb database library
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
8 ** under the LGPL
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 * Name: ldb
27 * Component: ldb expression parsing
29 * Description: parse LDAP-like search expressions
31 * Author: Andrew Tridgell
35 TODO:
36 - add RFC2254 binary string handling
37 - possibly add ~=, <= and >= handling
38 - expand the test suite
39 - add better parse error handling
43 #include "ldb_private.h"
44 #include "system/locale.h"
46 static int ldb_parse_hex2char(const char *x)
48 if (isxdigit(x[0]) && isxdigit(x[1])) {
49 const char h1 = x[0], h2 = x[1];
50 int c = 0;
52 if (h1 >= 'a') c = h1 - (int)'a' + 10;
53 else if (h1 >= 'A') c = h1 - (int)'A' + 10;
54 else if (h1 >= '0') c = h1 - (int)'0';
55 c = c << 4;
56 if (h2 >= 'a') c += h2 - (int)'a' + 10;
57 else if (h2 >= 'A') c += h2 - (int)'A' + 10;
58 else if (h2 >= '0') c += h2 - (int)'0';
60 return c;
63 return -1;
67 a filter is defined by:
68 <filter> ::= '(' <filtercomp> ')'
69 <filtercomp> ::= <and> | <or> | <not> | <simple>
70 <and> ::= '&' <filterlist>
71 <or> ::= '|' <filterlist>
72 <not> ::= '!' <filter>
73 <filterlist> ::= <filter> | <filter> <filterlist>
74 <simple> ::= <attributetype> <filtertype> <attributevalue>
75 <filtertype> ::= '=' | '~=' | '<=' | '>='
79 decode a RFC2254 binary string representation of a buffer.
80 Used in LDAP filters.
82 struct ldb_val ldb_binary_decode(TALLOC_CTX *mem_ctx, const char *str)
84 size_t i, j;
85 struct ldb_val ret;
86 size_t slen = str?strlen(str):0;
88 ret.data = (uint8_t *)talloc_size(mem_ctx, slen+1);
89 ret.length = 0;
90 if (ret.data == NULL) return ret;
92 for (i=j=0;i<slen;i++) {
93 if (str[i] == '\\') {
94 int c;
96 c = ldb_parse_hex2char(&str[i+1]);
97 if (c == -1) {
98 talloc_free(ret.data);
99 memset(&ret, 0, sizeof(ret));
100 return ret;
102 ((uint8_t *)ret.data)[j++] = c;
103 i += 2;
104 } else {
105 ((uint8_t *)ret.data)[j++] = str[i];
108 ret.length = j;
109 ((uint8_t *)ret.data)[j] = 0;
111 return ret;
116 encode a blob as a RFC2254 binary string, escaping any
117 non-printable or '\' characters
119 char *ldb_binary_encode(TALLOC_CTX *mem_ctx, struct ldb_val val)
121 size_t i;
122 char *ret;
123 size_t len = val.length;
124 unsigned char *buf = val.data;
126 for (i=0;i<val.length;i++) {
127 unsigned int cval = buf[i];
128 if (cval < 0x20 || cval > 0x7E || strchr(" *()\\&|!\"", buf[i])) {
129 len += 2;
132 ret = talloc_array(mem_ctx, char, len+1);
133 if (ret == NULL) return NULL;
135 len = 0;
136 for (i=0;i<val.length;i++) {
137 unsigned int cval = buf[i];
138 if (cval < 0x20 || cval > 0x7E || strchr(" *()\\&|!\"", buf[i])) {
139 snprintf(ret+len, 4, "\\%02X", buf[i]);
140 len += 3;
141 } else {
142 ret[len++] = buf[i];
146 ret[len] = 0;
148 return ret;
152 encode a string as a RFC2254 binary string, escaping any
153 non-printable or '\' characters. This routine is suitable for use
154 in escaping user data in ldap filters.
156 char *ldb_binary_encode_string(TALLOC_CTX *mem_ctx, const char *string)
158 struct ldb_val val;
159 if (string == NULL) {
160 return NULL;
162 val.data = discard_const_p(uint8_t, string);
163 val.length = strlen(string);
164 return ldb_binary_encode(mem_ctx, val);
167 /* find the first matching wildcard */
168 static char *ldb_parse_find_wildcard(char *value)
170 while (*value) {
171 value = strpbrk(value, "\\*");
172 if (value == NULL) return NULL;
174 if (value[0] == '\\') {
175 if (value[1] == '\0') return NULL;
176 value += 2;
177 continue;
180 if (value[0] == '*') return value;
183 return NULL;
186 /* return a NULL terminated list of binary strings representing the value
187 chunks separated by wildcards that makes the value portion of the filter
189 static struct ldb_val **ldb_wildcard_decode(TALLOC_CTX *mem_ctx, const char *string)
191 struct ldb_val **ret = NULL;
192 unsigned int val = 0;
193 char *wc, *str;
195 wc = talloc_strdup(mem_ctx, string);
196 if (wc == NULL) return NULL;
198 while (wc && *wc) {
199 str = wc;
200 wc = ldb_parse_find_wildcard(str);
201 if (wc && *wc) {
202 if (wc == str) {
203 wc++;
204 continue;
206 *wc = 0;
207 wc++;
210 ret = talloc_realloc(mem_ctx, ret, struct ldb_val *, val + 2);
211 if (ret == NULL) return NULL;
213 ret[val] = talloc(mem_ctx, struct ldb_val);
214 if (ret[val] == NULL) return NULL;
216 *(ret[val]) = ldb_binary_decode(mem_ctx, str);
217 if ((ret[val])->data == NULL) return NULL;
219 val++;
222 if (ret != NULL) {
223 ret[val] = NULL;
226 return ret;
229 static struct ldb_parse_tree *ldb_parse_filter(TALLOC_CTX *mem_ctx, const char **s);
233 parse an extended match
235 possible forms:
236 (attr:oid:=value)
237 (attr:dn:oid:=value)
238 (attr:dn:=value)
239 (:dn:oid:=value)
241 the ':dn' part sets the dnAttributes boolean if present
242 the oid sets the rule_id string
245 static struct ldb_parse_tree *ldb_parse_extended(struct ldb_parse_tree *ret,
246 char *attr, char *value)
248 char *p1, *p2;
250 ret->operation = LDB_OP_EXTENDED;
251 ret->u.extended.value = ldb_binary_decode(ret, value);
252 if (ret->u.extended.value.data == NULL) goto failed;
254 p1 = strchr(attr, ':');
255 if (p1 == NULL) goto failed;
256 p2 = strchr(p1+1, ':');
258 *p1 = 0;
259 if (p2) *p2 = 0;
261 ret->u.extended.attr = attr;
262 if (strcmp(p1+1, "dn") == 0) {
263 ret->u.extended.dnAttributes = 1;
264 if (p2) {
265 ret->u.extended.rule_id = talloc_strdup(ret, p2+1);
266 if (ret->u.extended.rule_id == NULL) goto failed;
267 } else {
268 ret->u.extended.rule_id = NULL;
270 } else {
271 ret->u.extended.dnAttributes = 0;
272 ret->u.extended.rule_id = talloc_strdup(ret, p1+1);
273 if (ret->u.extended.rule_id == NULL) goto failed;
276 return ret;
278 failed:
279 talloc_free(ret);
280 return NULL;
283 static enum ldb_parse_op ldb_parse_filtertype(TALLOC_CTX *mem_ctx, char **type, char **value, const char **s)
285 enum ldb_parse_op filter = 0;
286 char *name, *val, *k;
287 const char *p = *s;
288 const char *t, *t1;
290 /* retrieve attributetype name */
291 t = p;
293 if (*p == '@') { /* for internal attributes the first char can be @ */
294 p++;
297 while ((isascii(*p) && isalnum((unsigned char)*p)) || (*p == '-') || (*p == '.')) {
298 /* attribute names can only be alphanums */
299 p++;
302 if (*p == ':') { /* but extended searches have : and . chars too */
303 p = strstr(p, ":=");
304 if (p == NULL) { /* malformed attribute name */
305 return 0;
309 t1 = p;
311 while (isspace((unsigned char)*p)) p++;
313 if (!strchr("=<>~:", *p)) {
314 return 0;
317 /* save name */
318 name = (char *)talloc_memdup(mem_ctx, t, t1 - t + 1);
319 if (name == NULL) return 0;
320 name[t1 - t] = '\0';
322 /* retrieve filtertype */
324 if (*p == '=') {
325 filter = LDB_OP_EQUALITY;
326 } else if (*(p + 1) == '=') {
327 switch (*p) {
328 case '<':
329 filter = LDB_OP_LESS;
330 p++;
331 break;
332 case '>':
333 filter = LDB_OP_GREATER;
334 p++;
335 break;
336 case '~':
337 filter = LDB_OP_APPROX;
338 p++;
339 break;
340 case ':':
341 filter = LDB_OP_EXTENDED;
342 p++;
343 break;
346 if (!filter) {
347 talloc_free(name);
348 return 0;
350 p++;
352 while (isspace((unsigned char)*p)) p++;
354 /* retrieve value */
355 t = p;
357 while (*p && ((*p != ')') || ((*p == ')') && (*(p - 1) == '\\')))) p++;
359 val = (char *)talloc_memdup(mem_ctx, t, p - t + 1);
360 if (val == NULL) {
361 talloc_free(name);
362 return 0;
364 val[p - t] = '\0';
366 k = &(val[p - t]);
368 /* remove trailing spaces from value */
369 while ((k > val) && (isspace((unsigned char)*(k - 1)))) k--;
370 *k = '\0';
372 *type = name;
373 *value = val;
374 *s = p;
375 return filter;
379 <simple> ::= <attributetype> <filtertype> <attributevalue>
381 static struct ldb_parse_tree *ldb_parse_simple(TALLOC_CTX *mem_ctx, const char **s)
383 char *attr, *value;
384 struct ldb_parse_tree *ret;
385 enum ldb_parse_op filtertype;
387 ret = talloc(mem_ctx, struct ldb_parse_tree);
388 if (!ret) {
389 errno = ENOMEM;
390 return NULL;
393 filtertype = ldb_parse_filtertype(ret, &attr, &value, s);
394 if (!filtertype) {
395 talloc_free(ret);
396 return NULL;
399 switch (filtertype) {
401 case LDB_OP_PRESENT:
402 ret->operation = LDB_OP_PRESENT;
403 ret->u.present.attr = attr;
404 break;
406 case LDB_OP_EQUALITY:
408 if (strcmp(value, "*") == 0) {
409 ret->operation = LDB_OP_PRESENT;
410 ret->u.present.attr = attr;
411 break;
414 if (ldb_parse_find_wildcard(value) != NULL) {
415 ret->operation = LDB_OP_SUBSTRING;
416 ret->u.substring.attr = attr;
417 ret->u.substring.start_with_wildcard = 0;
418 ret->u.substring.end_with_wildcard = 0;
419 ret->u.substring.chunks = ldb_wildcard_decode(ret, value);
420 if (ret->u.substring.chunks == NULL){
421 talloc_free(ret);
422 return NULL;
424 if (value[0] == '*')
425 ret->u.substring.start_with_wildcard = 1;
426 if (value[strlen(value) - 1] == '*')
427 ret->u.substring.end_with_wildcard = 1;
428 talloc_free(value);
430 break;
433 ret->operation = LDB_OP_EQUALITY;
434 ret->u.equality.attr = attr;
435 ret->u.equality.value = ldb_binary_decode(ret, value);
436 if (ret->u.equality.value.data == NULL) {
437 talloc_free(ret);
438 return NULL;
440 talloc_free(value);
441 break;
443 case LDB_OP_GREATER:
444 ret->operation = LDB_OP_GREATER;
445 ret->u.comparison.attr = attr;
446 ret->u.comparison.value = ldb_binary_decode(ret, value);
447 if (ret->u.comparison.value.data == NULL) {
448 talloc_free(ret);
449 return NULL;
451 talloc_free(value);
452 break;
454 case LDB_OP_LESS:
455 ret->operation = LDB_OP_LESS;
456 ret->u.comparison.attr = attr;
457 ret->u.comparison.value = ldb_binary_decode(ret, value);
458 if (ret->u.comparison.value.data == NULL) {
459 talloc_free(ret);
460 return NULL;
462 talloc_free(value);
463 break;
465 case LDB_OP_APPROX:
466 ret->operation = LDB_OP_APPROX;
467 ret->u.comparison.attr = attr;
468 ret->u.comparison.value = ldb_binary_decode(ret, value);
469 if (ret->u.comparison.value.data == NULL) {
470 talloc_free(ret);
471 return NULL;
473 talloc_free(value);
474 break;
476 case LDB_OP_EXTENDED:
478 ret = ldb_parse_extended(ret, attr, value);
479 break;
481 default:
482 talloc_free(ret);
483 return NULL;
486 return ret;
491 parse a filterlist
492 <and> ::= '&' <filterlist>
493 <or> ::= '|' <filterlist>
494 <filterlist> ::= <filter> | <filter> <filterlist>
496 static struct ldb_parse_tree *ldb_parse_filterlist(TALLOC_CTX *mem_ctx, const char **s)
498 struct ldb_parse_tree *ret, *next;
499 enum ldb_parse_op op;
500 const char *p = *s;
502 switch (*p) {
503 case '&':
504 op = LDB_OP_AND;
505 break;
506 case '|':
507 op = LDB_OP_OR;
508 break;
509 default:
510 return NULL;
512 p++;
514 while (isspace((unsigned char)*p)) p++;
516 ret = talloc(mem_ctx, struct ldb_parse_tree);
517 if (!ret) {
518 errno = ENOMEM;
519 return NULL;
522 ret->operation = op;
523 ret->u.list.num_elements = 1;
524 ret->u.list.elements = talloc(ret, struct ldb_parse_tree *);
525 if (!ret->u.list.elements) {
526 errno = ENOMEM;
527 talloc_free(ret);
528 return NULL;
531 ret->u.list.elements[0] = ldb_parse_filter(ret->u.list.elements, &p);
532 if (!ret->u.list.elements[0]) {
533 talloc_free(ret);
534 return NULL;
537 while (isspace((unsigned char)*p)) p++;
539 while (*p) {
540 struct ldb_parse_tree **e;
541 if (*p == ')') {
542 break;
545 next = ldb_parse_filter(ret->u.list.elements, &p);
546 if (next == NULL) {
547 /* an invalid filter element */
548 talloc_free(ret);
549 return NULL;
551 e = talloc_realloc(ret, ret->u.list.elements,
552 struct ldb_parse_tree *,
553 ret->u.list.num_elements + 1);
554 if (!e) {
555 errno = ENOMEM;
556 talloc_free(ret);
557 return NULL;
559 ret->u.list.elements = e;
560 ret->u.list.elements[ret->u.list.num_elements] = next;
561 ret->u.list.num_elements++;
562 while (isspace((unsigned char)*p)) p++;
565 *s = p;
567 return ret;
572 <not> ::= '!' <filter>
574 static struct ldb_parse_tree *ldb_parse_not(TALLOC_CTX *mem_ctx, const char **s)
576 struct ldb_parse_tree *ret;
577 const char *p = *s;
579 if (*p != '!') {
580 return NULL;
582 p++;
584 ret = talloc(mem_ctx, struct ldb_parse_tree);
585 if (!ret) {
586 errno = ENOMEM;
587 return NULL;
590 ret->operation = LDB_OP_NOT;
591 ret->u.isnot.child = ldb_parse_filter(ret, &p);
592 if (!ret->u.isnot.child) {
593 talloc_free(ret);
594 return NULL;
597 *s = p;
599 return ret;
603 parse a filtercomp
604 <filtercomp> ::= <and> | <or> | <not> | <simple>
606 static struct ldb_parse_tree *ldb_parse_filtercomp(TALLOC_CTX *mem_ctx, const char **s)
608 struct ldb_parse_tree *ret;
609 const char *p = *s;
611 while (isspace((unsigned char)*p)) p++;
613 switch (*p) {
614 case '&':
615 ret = ldb_parse_filterlist(mem_ctx, &p);
616 break;
618 case '|':
619 ret = ldb_parse_filterlist(mem_ctx, &p);
620 break;
622 case '!':
623 ret = ldb_parse_not(mem_ctx, &p);
624 break;
626 case '(':
627 case ')':
628 return NULL;
630 default:
631 ret = ldb_parse_simple(mem_ctx, &p);
635 *s = p;
636 return ret;
641 <filter> ::= '(' <filtercomp> ')'
643 static struct ldb_parse_tree *ldb_parse_filter(TALLOC_CTX *mem_ctx, const char **s)
645 struct ldb_parse_tree *ret;
646 const char *p = *s;
648 if (*p != '(') {
649 return NULL;
651 p++;
653 ret = ldb_parse_filtercomp(mem_ctx, &p);
655 if (*p != ')') {
656 return NULL;
658 p++;
660 while (isspace((unsigned char)*p)) {
661 p++;
664 *s = p;
666 return ret;
671 main parser entry point. Takes a search string and returns a parse tree
673 expression ::= <simple> | <filter>
675 struct ldb_parse_tree *ldb_parse_tree(TALLOC_CTX *mem_ctx, const char *s)
677 if (s == NULL || *s == 0) {
678 s = "(|(objectClass=*)(distinguishedName=*))";
681 while (isspace((unsigned char)*s)) s++;
683 if (*s == '(') {
684 return ldb_parse_filter(mem_ctx, &s);
687 return ldb_parse_simple(mem_ctx, &s);
692 construct a ldap parse filter given a parse tree
694 char *ldb_filter_from_tree(TALLOC_CTX *mem_ctx, const struct ldb_parse_tree *tree)
696 char *s, *s2, *ret;
697 unsigned int i;
699 if (tree == NULL) {
700 return NULL;
703 switch (tree->operation) {
704 case LDB_OP_AND:
705 case LDB_OP_OR:
706 ret = talloc_asprintf(mem_ctx, "(%c", tree->operation==LDB_OP_AND?'&':'|');
707 if (ret == NULL) return NULL;
708 for (i=0;i<tree->u.list.num_elements;i++) {
709 s = ldb_filter_from_tree(mem_ctx, tree->u.list.elements[i]);
710 if (s == NULL) {
711 talloc_free(ret);
712 return NULL;
714 s2 = talloc_asprintf_append(ret, "%s", s);
715 talloc_free(s);
716 if (s2 == NULL) {
717 talloc_free(ret);
718 return NULL;
720 ret = s2;
722 s = talloc_asprintf_append(ret, ")");
723 if (s == NULL) {
724 talloc_free(ret);
725 return NULL;
727 return s;
728 case LDB_OP_NOT:
729 s = ldb_filter_from_tree(mem_ctx, tree->u.isnot.child);
730 if (s == NULL) return NULL;
732 ret = talloc_asprintf(mem_ctx, "(!%s)", s);
733 talloc_free(s);
734 return ret;
735 case LDB_OP_EQUALITY:
736 s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
737 if (s == NULL) return NULL;
738 ret = talloc_asprintf(mem_ctx, "(%s=%s)",
739 tree->u.equality.attr, s);
740 talloc_free(s);
741 return ret;
742 case LDB_OP_SUBSTRING:
743 ret = talloc_asprintf(mem_ctx, "(%s=%s", tree->u.substring.attr,
744 tree->u.substring.start_with_wildcard?"*":"");
745 if (ret == NULL) return NULL;
746 for (i = 0; tree->u.substring.chunks[i]; i++) {
747 s2 = ldb_binary_encode(mem_ctx, *(tree->u.substring.chunks[i]));
748 if (s2 == NULL) {
749 talloc_free(ret);
750 return NULL;
752 if (tree->u.substring.chunks[i+1] ||
753 tree->u.substring.end_with_wildcard) {
754 s = talloc_asprintf_append(ret, "%s*", s2);
755 } else {
756 s = talloc_asprintf_append(ret, "%s", s2);
758 if (s == NULL) {
759 talloc_free(ret);
760 return NULL;
762 ret = s;
764 s = talloc_asprintf_append(ret, ")");
765 if (s == NULL) {
766 talloc_free(ret);
767 return NULL;
769 ret = s;
770 return ret;
771 case LDB_OP_GREATER:
772 s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
773 if (s == NULL) return NULL;
774 ret = talloc_asprintf(mem_ctx, "(%s>=%s)",
775 tree->u.equality.attr, s);
776 talloc_free(s);
777 return ret;
778 case LDB_OP_LESS:
779 s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
780 if (s == NULL) return NULL;
781 ret = talloc_asprintf(mem_ctx, "(%s<=%s)",
782 tree->u.equality.attr, s);
783 talloc_free(s);
784 return ret;
785 case LDB_OP_PRESENT:
786 ret = talloc_asprintf(mem_ctx, "(%s=*)", tree->u.present.attr);
787 return ret;
788 case LDB_OP_APPROX:
789 s = ldb_binary_encode(mem_ctx, tree->u.equality.value);
790 if (s == NULL) return NULL;
791 ret = talloc_asprintf(mem_ctx, "(%s~=%s)",
792 tree->u.equality.attr, s);
793 talloc_free(s);
794 return ret;
795 case LDB_OP_EXTENDED:
796 s = ldb_binary_encode(mem_ctx, tree->u.extended.value);
797 if (s == NULL) return NULL;
798 ret = talloc_asprintf(mem_ctx, "(%s%s%s%s:=%s)",
799 tree->u.extended.attr?tree->u.extended.attr:"",
800 tree->u.extended.dnAttributes?":dn":"",
801 tree->u.extended.rule_id?":":"",
802 tree->u.extended.rule_id?tree->u.extended.rule_id:"",
804 talloc_free(s);
805 return ret;
808 return NULL;
813 walk a parse tree, calling the provided callback on each node
815 int ldb_parse_tree_walk(struct ldb_parse_tree *tree,
816 int (*callback)(struct ldb_parse_tree *tree, void *),
817 void *private_context)
819 unsigned int i;
820 int ret;
822 ret = callback(tree, private_context);
823 if (ret != LDB_SUCCESS) {
824 return ret;
827 switch (tree->operation) {
828 case LDB_OP_AND:
829 case LDB_OP_OR:
830 for (i=0;i<tree->u.list.num_elements;i++) {
831 ret = ldb_parse_tree_walk(tree->u.list.elements[i], callback, private_context);
832 if (ret != LDB_SUCCESS) {
833 return ret;
836 break;
837 case LDB_OP_NOT:
838 ret = ldb_parse_tree_walk(tree->u.isnot.child, callback, private_context);
839 if (ret != LDB_SUCCESS) {
840 return ret;
842 break;
843 case LDB_OP_EQUALITY:
844 case LDB_OP_GREATER:
845 case LDB_OP_LESS:
846 case LDB_OP_APPROX:
847 case LDB_OP_SUBSTRING:
848 case LDB_OP_PRESENT:
849 case LDB_OP_EXTENDED:
850 break;
852 return LDB_SUCCESS;
855 struct parse_tree_attr_replace_ctx {
856 const char *attr;
857 const char *replace;
861 callback for ldb_parse_tree_attr_replace()
863 static int parse_tree_attr_replace(struct ldb_parse_tree *tree, void *private_context)
865 struct parse_tree_attr_replace_ctx *ctx = private_context;
866 switch (tree->operation) {
867 case LDB_OP_EQUALITY:
868 case LDB_OP_GREATER:
869 case LDB_OP_LESS:
870 case LDB_OP_APPROX:
871 if (ldb_attr_cmp(tree->u.equality.attr, ctx->attr) == 0) {
872 tree->u.equality.attr = ctx->replace;
874 break;
875 case LDB_OP_SUBSTRING:
876 if (ldb_attr_cmp(tree->u.substring.attr, ctx->attr) == 0) {
877 tree->u.substring.attr = ctx->replace;
879 break;
880 case LDB_OP_PRESENT:
881 if (ldb_attr_cmp(tree->u.present.attr, ctx->attr) == 0) {
882 tree->u.present.attr = ctx->replace;
884 break;
885 case LDB_OP_EXTENDED:
886 if (tree->u.extended.attr &&
887 ldb_attr_cmp(tree->u.extended.attr, ctx->attr) == 0) {
888 tree->u.extended.attr = ctx->replace;
890 break;
891 default:
892 break;
894 return LDB_SUCCESS;
898 replace any occurrences of an attribute name in the parse tree with a
899 new name
901 void ldb_parse_tree_attr_replace(struct ldb_parse_tree *tree,
902 const char *attr,
903 const char *replace)
905 struct parse_tree_attr_replace_ctx ctx;
907 ctx.attr = attr;
908 ctx.replace = replace;
910 ldb_parse_tree_walk(tree, parse_tree_attr_replace, &ctx);
914 shallow copy a tree - copying only the elements array so that the caller
915 can safely add new elements without changing the message
917 struct ldb_parse_tree *ldb_parse_tree_copy_shallow(TALLOC_CTX *mem_ctx,
918 const struct ldb_parse_tree *ot)
920 unsigned int i;
921 struct ldb_parse_tree *nt;
923 nt = talloc(mem_ctx, struct ldb_parse_tree);
924 if (!nt) {
925 return NULL;
928 *nt = *ot;
930 switch (ot->operation) {
931 case LDB_OP_AND:
932 case LDB_OP_OR:
933 nt->u.list.elements = talloc_array(nt, struct ldb_parse_tree *,
934 ot->u.list.num_elements);
935 if (!nt->u.list.elements) {
936 talloc_free(nt);
937 return NULL;
940 for (i=0;i<ot->u.list.num_elements;i++) {
941 nt->u.list.elements[i] =
942 ldb_parse_tree_copy_shallow(nt->u.list.elements,
943 ot->u.list.elements[i]);
944 if (!nt->u.list.elements[i]) {
945 talloc_free(nt);
946 return NULL;
949 break;
950 case LDB_OP_NOT:
951 nt->u.isnot.child = ldb_parse_tree_copy_shallow(nt,
952 ot->u.isnot.child);
953 if (!nt->u.isnot.child) {
954 talloc_free(nt);
955 return NULL;
957 break;
958 case LDB_OP_EQUALITY:
959 case LDB_OP_GREATER:
960 case LDB_OP_LESS:
961 case LDB_OP_APPROX:
962 case LDB_OP_SUBSTRING:
963 case LDB_OP_PRESENT:
964 case LDB_OP_EXTENDED:
965 break;
968 return nt;