r19531: Make struct ldb_dn opaque and local to ldb_dn.c
[Samba/aatanasov.git] / source4 / lib / ldb / common / ldb_dn.c
blob550b80c26a0b110ccd041b3d47b85c0fff3d83c6
1 /*
2 ldb database library
4 Copyright (C) Simo Sorce 2005
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 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
26 * Name: ldb
28 * Component: ldb dn explode and utility functions
30 * Description: - explode a dn into it's own basic elements
31 * and put them in a structure
32 * - manipulate ldb_dn structures
34 * Author: Simo Sorce
37 #include "includes.h"
38 #include "ldb/include/includes.h"
40 #define LDB_DN_NULL_FAILED(x) if (!(x)) goto failed
42 #define LDB_SPECIAL "@SPECIAL"
44 /**
45 internal ldb exploded dn structures
47 struct ldb_dn_component {
48 char *name;
49 struct ldb_val value;
52 struct ldb_dn {
53 int comp_num;
54 struct ldb_dn_component *components;
57 int ldb_dn_is_special(const struct ldb_dn *dn)
59 if (dn == NULL || dn->comp_num != 1) return 0;
61 return ! strcmp(dn->components[0].name, LDB_SPECIAL);
64 int ldb_dn_check_special(const struct ldb_dn *dn, const char *check)
66 if (dn == NULL || dn->comp_num != 1) return 0;
68 return ! strcmp((char *)dn->components[0].value.data, check);
71 char *ldb_dn_escape_value(void *mem_ctx, struct ldb_val value)
73 const char *p, *s, *src;
74 char *d, *dst;
75 int len;
77 if (!value.length)
78 return NULL;
80 p = s = src = (const char *)value.data;
81 len = value.length;
83 /* allocate destination string, it will be at most 3 times the source */
84 dst = d = talloc_array(mem_ctx, char, len * 3 + 1);
85 LDB_DN_NULL_FAILED(dst);
87 while (p - src < len) {
89 p += strcspn(p, ",=\n+<>#;\\\"");
91 if (p - src == len) /* found no escapable chars */
92 break;
94 memcpy(d, s, p - s); /* copy the part of the string before the stop */
95 d += (p - s); /* move to current position */
97 if (*p) { /* it is a normal escapable character */
98 *d++ = '\\';
99 *d++ = *p++;
100 } else { /* we have a zero byte in the string */
101 strncpy(d, "\00", 3); /* escape the zero */
102 d = d + 3;
103 p++; /* skip the zero */
105 s = p; /* move forward */
108 /* copy the last part (with zero) and return */
109 memcpy(d, s, &src[len] - s + 1);
111 return dst;
113 failed:
114 talloc_free(dst);
115 return NULL;
118 static struct ldb_val ldb_dn_unescape_value(void *mem_ctx, const char *src)
120 struct ldb_val value;
121 unsigned x;
122 char *p, *dst = NULL, *end;
124 memset(&value, 0, sizeof(value));
126 LDB_DN_NULL_FAILED(src);
128 dst = p = (char *)talloc_memdup(mem_ctx, src, strlen(src) + 1);
129 LDB_DN_NULL_FAILED(dst);
131 end = &dst[strlen(dst)];
133 while (*p) {
134 p += strcspn(p, ",=\n+<>#;\\\"");
136 if (*p == '\\') {
137 if (strchr(",=\n+<>#;\\\"", p[1])) {
138 memmove(p, p + 1, end - (p + 1) + 1);
139 end--;
140 p++;
141 continue;
144 if (sscanf(p + 1, "%02x", &x) == 1) {
145 *p = (unsigned char)x;
146 memmove(p + 1, p + 3, end - (p + 3) + 1);
147 end -= 2;
148 p++;
149 continue;
153 /* a string with not escaped specials is invalid (tested) */
154 if (*p != '\0') {
155 goto failed;
159 value.length = end - dst;
160 value.data = (uint8_t *)dst;
161 return value;
163 failed:
164 talloc_free(dst);
165 return value;
168 /* check if the string contains quotes
169 * skips leading and trailing spaces
170 * - returns 0 if no quotes found
171 * - returns 1 if quotes are found and put their position
172 * in *quote_start and *quote_end parameters
173 * - return -1 if there are open quotes
176 static int get_quotes_position(const char *source, int *quote_start, int *quote_end)
178 const char *p;
180 if (source == NULL || quote_start == NULL || quote_end == NULL) return -1;
182 p = source;
184 /* check if there are quotes surrounding the value */
185 p += strspn(p, " \n"); /* skip white spaces */
187 if (*p == '\"') {
188 *quote_start = p - source;
190 p++;
191 while (*p != '\"') {
192 p = strchr(p, '\"');
193 LDB_DN_NULL_FAILED(p);
195 if (*(p - 1) == '\\')
196 p++;
199 *quote_end = p - source;
200 return 1;
203 return 0;
205 failed:
206 return -1;
209 static char *seek_to_separator(char *string, const char *separators)
211 char *p, *q;
212 int ret, qs, qe, escaped;
214 if (string == NULL || separators == NULL) return NULL;
216 p = strchr(string, '=');
217 LDB_DN_NULL_FAILED(p);
219 p++;
221 /* check if there are quotes surrounding the value */
223 ret = get_quotes_position(p, &qs, &qe);
224 if (ret == -1)
225 return NULL;
227 if (ret == 1) { /* quotes found */
229 p += qe; /* positioning after quotes */
230 p += strspn(p, " \n"); /* skip white spaces after the quote */
232 if (strcspn(p, separators) != 0) /* if there are characters between quotes */
233 return NULL; /* and separators, the dn is invalid */
235 return p; /* return on the separator */
238 /* no quotes found seek to separators */
239 q = p;
240 do {
241 escaped = 0;
242 ret = strcspn(q, separators);
244 if (q[ret - 1] == '\\') {
245 escaped = 1;
246 q = q + ret + 1;
248 } while (escaped);
250 if (ret == 0 && p == q) /* no separators ?! bail out */
251 return NULL;
253 return q + ret;
255 failed:
256 return NULL;
259 static char *ldb_dn_trim_string(char *string, const char *edge)
261 char *s, *p;
263 /* seek out edge from start of string */
264 s = string + strspn(string, edge);
266 /* backwards skip from end of string */
267 p = &s[strlen(s) - 1];
268 while (p > s && strchr(edge, *p)) {
269 *p = '\0';
270 p--;
273 return s;
276 /* we choosed to not support multpile valued components */
277 static struct ldb_dn_component ldb_dn_explode_component(void *mem_ctx, char *raw_component)
279 struct ldb_dn_component dc;
280 char *p;
281 int ret, qs, qe;
283 memset(&dc, 0, sizeof(dc));
285 if (raw_component == NULL) {
286 return dc;
289 /* find attribute type/value separator */
290 p = strchr(raw_component, '=');
291 LDB_DN_NULL_FAILED(p);
293 *p++ = '\0'; /* terminate name and point to value */
295 /* copy and trim name in the component */
296 dc.name = talloc_strdup(mem_ctx, ldb_dn_trim_string(raw_component, " \n"));
297 if (!dc.name)
298 return dc;
300 if (! ldb_valid_attr_name(dc.name)) {
301 goto failed;
304 ret = get_quotes_position(p, &qs, &qe);
306 switch (ret) {
307 case 0: /* no quotes trim the string */
308 p = ldb_dn_trim_string(p, " \n");
309 dc.value = ldb_dn_unescape_value(mem_ctx, p);
310 break;
312 case 1: /* quotes found get the unquoted string */
313 p[qe] = '\0';
314 p = p + qs + 1;
315 dc.value.length = strlen(p);
316 dc.value.data = (uint8_t *)talloc_memdup(mem_ctx, p,
317 dc.value.length + 1);
318 break;
320 default: /* mismatched quotes ot other error, bail out */
321 goto failed;
324 if (dc.value.length == 0) {
325 goto failed;
328 return dc;
330 failed:
331 talloc_free(dc.name);
332 dc.name = NULL;
333 return dc;
336 struct ldb_dn *ldb_dn_new(void *mem_ctx)
338 struct ldb_dn *edn;
340 edn = talloc(mem_ctx, struct ldb_dn);
341 LDB_DN_NULL_FAILED(edn);
343 /* Initially there are no components */
344 edn->comp_num = 0;
345 edn->components = NULL;
347 return edn;
349 failed:
350 return NULL;
354 explode a DN string into a ldb_dn structure
356 struct ldb_dn *ldb_dn_explode(void *mem_ctx, const char *dn)
358 struct ldb_dn *edn; /* the exploded dn */
359 char *pdn, *p;
361 if (dn == NULL) return NULL;
363 /* Allocate a structure to hold the exploded DN */
364 edn = ldb_dn_new(mem_ctx);
365 if (edn == NULL) {
366 return NULL;
369 pdn = NULL;
371 /* Empty DNs */
372 if (dn[0] == '\0') {
373 return edn;
376 /* Special DNs case */
377 if (dn[0] == '@') {
378 edn->comp_num = 1;
379 edn->components = talloc(edn, struct ldb_dn_component);
380 if (edn->components == NULL) goto failed;
381 edn->components[0].name = talloc_strdup(edn->components, LDB_SPECIAL);
382 if (edn->components[0].name == NULL) goto failed;
383 edn->components[0].value.data = (uint8_t *)talloc_strdup(edn->components, dn);
384 if (edn->components[0].value.data== NULL) goto failed;
385 edn->components[0].value.length = strlen(dn);
386 return edn;
389 pdn = p = talloc_strdup(edn, dn);
390 LDB_DN_NULL_FAILED(pdn);
392 /* get the components */
393 do {
394 char *t;
396 /* terminate the current component and return pointer to the next one */
397 t = seek_to_separator(p, ",;");
398 LDB_DN_NULL_FAILED(t);
400 if (*t) { /* here there is a separator */
401 *t = '\0'; /*terminate */
402 t++; /* a separtor means another component follows */
405 /* allocate space to hold the dn component */
406 edn->components = talloc_realloc(edn, edn->components,
407 struct ldb_dn_component,
408 edn->comp_num + 1);
409 if (edn->components == NULL)
410 goto failed;
412 /* store the exploded component in the main structure */
413 edn->components[edn->comp_num] = ldb_dn_explode_component(edn, p);
414 LDB_DN_NULL_FAILED(edn->components[edn->comp_num].name);
416 edn->comp_num++;
418 /* jump to the next component if any */
419 p = t;
421 } while(*p);
423 talloc_free(pdn);
424 return edn;
426 failed:
427 talloc_free(pdn);
428 talloc_free(edn);
429 return NULL;
432 struct ldb_dn *ldb_dn_explode_or_special(void *mem_ctx, const char *dn)
434 struct ldb_dn *edn; /* the exploded dn */
436 if (dn == NULL) return NULL;
438 if (strncasecmp(dn, "<GUID=", 6) == 0) {
439 /* this is special DN returned when the
440 * exploded_dn control is used
443 /* Allocate a structure to hold the exploded DN */
444 edn = ldb_dn_new(mem_ctx);
446 edn->comp_num = 1;
447 edn->components = talloc(edn, struct ldb_dn_component);
448 if (edn->components == NULL) goto failed;
449 edn->components[0].name = talloc_strdup(edn->components, LDB_SPECIAL);
450 if (edn->components[0].name == NULL) goto failed;
451 edn->components[0].value.data = (uint8_t *)talloc_strdup(edn->components, dn);
452 if (edn->components[0].value.data== NULL) goto failed;
453 edn->components[0].value.length = strlen(dn);
454 return edn;
458 return ldb_dn_explode(mem_ctx, dn);
460 failed:
461 talloc_free(edn);
462 return NULL;
465 char *ldb_dn_linearize(void *mem_ctx, const struct ldb_dn *edn)
467 char *dn, *value;
468 int i;
470 if (edn == NULL) return NULL;
472 /* Special DNs */
473 if (ldb_dn_is_special(edn)) {
474 dn = talloc_strdup(mem_ctx, (char *)edn->components[0].value.data);
475 return dn;
478 dn = talloc_strdup(mem_ctx, "");
479 LDB_DN_NULL_FAILED(dn);
481 for (i = 0; i < edn->comp_num; i++) {
482 value = ldb_dn_escape_value(dn, edn->components[i].value);
483 LDB_DN_NULL_FAILED(value);
485 if (i == 0) {
486 dn = talloc_asprintf_append(dn, "%s=%s", edn->components[i].name, value);
487 } else {
488 dn = talloc_asprintf_append(dn, ",%s=%s", edn->components[i].name, value);
490 LDB_DN_NULL_FAILED(dn);
492 talloc_free(value);
495 return dn;
497 failed:
498 talloc_free(dn);
499 return NULL;
502 /* Determine if dn is below base, in the ldap tree. Used for
503 * evaluating a subtree search.
504 * 0 if they match, otherwise non-zero
507 int ldb_dn_compare_base(struct ldb_context *ldb,
508 const struct ldb_dn *base,
509 const struct ldb_dn *dn)
511 int ret;
512 int n0, n1;
514 if (base == NULL || base->comp_num == 0) return 0;
515 if (dn == NULL || dn->comp_num == 0) return -1;
517 /* if the base has more componts than the dn, then they differ */
518 if (base->comp_num > dn->comp_num) {
519 return (dn->comp_num - base->comp_num);
522 n0 = base->comp_num - 1;
523 n1 = dn->comp_num - 1;
524 while (n0 >= 0 && n1 >= 0) {
525 const struct ldb_attrib_handler *h;
527 /* compare names (attribute names are guaranteed to be ASCII only) */
528 ret = ldb_attr_cmp(base->components[n0].name,
529 dn->components[n1].name);
530 if (ret) {
531 return ret;
534 /* names match, compare values */
535 h = ldb_attrib_handler(ldb, base->components[n0].name);
536 ret = h->comparison_fn(ldb, ldb, &(base->components[n0].value),
537 &(dn->components[n1].value));
538 if (ret) {
539 return ret;
541 n1--;
542 n0--;
545 return 0;
548 /* compare DNs using casefolding compare functions.
550 If they match, then return 0
553 int ldb_dn_compare(struct ldb_context *ldb,
554 const struct ldb_dn *edn0,
555 const struct ldb_dn *edn1)
557 if (edn0 == NULL || edn1 == NULL) return edn1 - edn0;
559 if (edn0->comp_num != edn1->comp_num)
560 return (edn1->comp_num - edn0->comp_num);
562 return ldb_dn_compare_base(ldb, edn0, edn1);
565 int ldb_dn_cmp(struct ldb_context *ldb, const char *dn0, const char *dn1)
567 struct ldb_dn *edn0;
568 struct ldb_dn *edn1;
569 int ret;
571 if (dn0 == NULL || dn1 == NULL) return dn1 - dn0;
573 edn0 = ldb_dn_explode_casefold(ldb, ldb, dn0);
574 if (edn0 == NULL) return 1;
576 edn1 = ldb_dn_explode_casefold(ldb, ldb, dn1);
577 if (edn1 == NULL) {
578 talloc_free(edn0);
579 return -1;
582 ret = ldb_dn_compare(ldb, edn0, edn1);
584 talloc_free(edn0);
585 talloc_free(edn1);
587 return ret;
591 casefold a dn. We need to casefold the attribute names, and canonicalize
592 attribute values of case insensitive attributes.
594 struct ldb_dn *ldb_dn_casefold(struct ldb_context *ldb, void *mem_ctx, const struct ldb_dn *edn)
596 struct ldb_dn *cedn;
597 int i, ret;
599 if (edn == NULL) return NULL;
601 cedn = ldb_dn_new(mem_ctx);
602 if (!cedn) {
603 return NULL;
606 cedn->comp_num = edn->comp_num;
607 cedn->components = talloc_array(cedn, struct ldb_dn_component, edn->comp_num);
608 if (!cedn->components) {
609 talloc_free(cedn);
610 return NULL;
613 for (i = 0; i < edn->comp_num; i++) {
614 struct ldb_dn_component dc;
615 const struct ldb_attrib_handler *h;
617 memset(&dc, 0, sizeof(dc));
618 dc.name = ldb_attr_casefold(cedn->components, edn->components[i].name);
619 if (!dc.name) {
620 talloc_free(cedn);
621 return NULL;
624 h = ldb_attrib_handler(ldb, dc.name);
625 ret = h->canonicalise_fn(ldb, cedn->components,
626 &(edn->components[i].value),
627 &(dc.value));
628 if (ret != 0) {
629 talloc_free(cedn);
630 return NULL;
633 cedn->components[i] = dc;
636 return cedn;
639 struct ldb_dn *ldb_dn_explode_casefold(struct ldb_context *ldb, void *mem_ctx, const char *dn)
641 struct ldb_dn *edn, *cdn;
643 if (dn == NULL) return NULL;
645 edn = ldb_dn_explode(ldb, dn);
646 if (edn == NULL) return NULL;
648 cdn = ldb_dn_casefold(ldb, mem_ctx, edn);
650 talloc_free(edn);
651 return cdn;
654 char *ldb_dn_linearize_casefold(struct ldb_context *ldb, void *mem_ctx, const struct ldb_dn *edn)
656 struct ldb_dn *cdn;
657 char *dn;
659 if (edn == NULL) return NULL;
661 /* Special DNs */
662 if (ldb_dn_is_special(edn)) {
663 dn = talloc_strdup(mem_ctx, (char *)edn->components[0].value.data);
664 return dn;
667 cdn = ldb_dn_casefold(ldb, mem_ctx, edn);
668 if (cdn == NULL) return NULL;
670 dn = ldb_dn_linearize(ldb, cdn);
671 if (dn == NULL) {
672 talloc_free(cdn);
673 return NULL;
676 talloc_free(cdn);
677 return dn;
680 static struct ldb_dn_component ldb_dn_copy_component(void *mem_ctx, struct ldb_dn_component *src)
682 struct ldb_dn_component dst;
684 memset(&dst, 0, sizeof(dst));
686 if (src == NULL) {
687 return dst;
690 dst.value = ldb_val_dup(mem_ctx, &(src->value));
691 if (dst.value.data == NULL) {
692 return dst;
695 dst.name = talloc_strdup(mem_ctx, src->name);
696 if (dst.name == NULL) {
697 talloc_free(dst.value.data);
698 dst.value.data = NULL;
701 return dst;
704 /* Copy a DN but replace the old with the new base DN. */
705 struct ldb_dn *ldb_dn_copy_rebase(void *mem_ctx, const struct ldb_dn *old, const struct ldb_dn *old_base, const struct ldb_dn *new_base)
707 struct ldb_dn *new;
708 int i, offset;
710 /* Perhaps we don't need to rebase at all? */
711 if (!old_base || !new_base) {
712 return ldb_dn_copy(mem_ctx, old);
715 offset = old->comp_num - old_base->comp_num;
716 new = ldb_dn_copy_partial(mem_ctx, new_base, offset + new_base->comp_num);
717 for (i = 0; i < offset; i++) {
718 new->components[i] = ldb_dn_copy_component(new->components, &(old->components[i]));
721 return new;
724 /* copy specified number of elements of a dn into a new one
725 element are copied from top level up to the unique rdn
726 num_el may be greater than dn->comp_num (see ldb_dn_make_child)
728 struct ldb_dn *ldb_dn_copy_partial(void *mem_ctx, const struct ldb_dn *dn, int num_el)
730 struct ldb_dn *newdn;
731 int i, n, e;
733 if (dn == NULL) return NULL;
734 if (num_el <= 0) return NULL;
736 newdn = ldb_dn_new(mem_ctx);
737 LDB_DN_NULL_FAILED(newdn);
739 newdn->comp_num = num_el;
740 n = newdn->comp_num - 1;
741 newdn->components = talloc_array(newdn, struct ldb_dn_component, newdn->comp_num);
742 if (newdn->components == NULL) goto failed;
744 if (dn->comp_num == 0) return newdn;
745 e = dn->comp_num - 1;
747 for (i = 0; i < newdn->comp_num; i++) {
748 newdn->components[n - i] = ldb_dn_copy_component(newdn->components,
749 &(dn->components[e - i]));
750 if ((e - i) == 0) {
751 return newdn;
755 return newdn;
757 failed:
758 talloc_free(newdn);
759 return NULL;
762 struct ldb_dn *ldb_dn_copy(void *mem_ctx, const struct ldb_dn *dn)
764 if (dn == NULL) return NULL;
765 return ldb_dn_copy_partial(mem_ctx, dn, dn->comp_num);
768 struct ldb_dn *ldb_dn_get_parent(void *mem_ctx, const struct ldb_dn *dn)
770 if (dn == NULL) return NULL;
771 return ldb_dn_copy_partial(mem_ctx, dn, dn->comp_num - 1);
774 struct ldb_dn_component *ldb_dn_build_component(void *mem_ctx, const char *attr,
775 const char *val)
777 struct ldb_dn_component *dc;
779 if (attr == NULL || val == NULL) return NULL;
781 dc = talloc(mem_ctx, struct ldb_dn_component);
782 if (dc == NULL) return NULL;
784 dc->name = talloc_strdup(dc, attr);
785 if (dc->name == NULL) {
786 talloc_free(dc);
787 return NULL;
790 dc->value.data = (uint8_t *)talloc_strdup(dc, val);
791 if (dc->value.data == NULL) {
792 talloc_free(dc);
793 return NULL;
796 dc->value.length = strlen(val);
798 return dc;
801 struct ldb_dn *ldb_dn_build_child(void *mem_ctx, const char *attr,
802 const char * value,
803 const struct ldb_dn *base)
805 struct ldb_dn *newdn;
806 if (! ldb_valid_attr_name(attr)) return NULL;
807 if (value == NULL || value == '\0') return NULL;
809 if (base != NULL) {
810 newdn = ldb_dn_copy_partial(mem_ctx, base, base->comp_num + 1);
811 LDB_DN_NULL_FAILED(newdn);
812 } else {
813 newdn = ldb_dn_new(mem_ctx);
814 LDB_DN_NULL_FAILED(newdn);
816 newdn->comp_num = 1;
817 newdn->components = talloc_array(newdn, struct ldb_dn_component, newdn->comp_num);
820 newdn->components[0].name = talloc_strdup(newdn->components, attr);
821 LDB_DN_NULL_FAILED(newdn->components[0].name);
823 newdn->components[0].value.data = (uint8_t *)talloc_strdup(newdn->components, value);
824 LDB_DN_NULL_FAILED(newdn->components[0].value.data);
825 newdn->components[0].value.length = strlen((char *)newdn->components[0].value.data);
827 return newdn;
829 failed:
830 talloc_free(newdn);
831 return NULL;
835 struct ldb_dn *ldb_dn_compose(void *mem_ctx, const struct ldb_dn *dn1, const struct ldb_dn *dn2)
837 int i;
838 struct ldb_dn *newdn;
840 if (dn2 == NULL && dn1 == NULL) {
841 return NULL;
844 if (dn2 == NULL) {
845 newdn = ldb_dn_new(mem_ctx);
846 LDB_DN_NULL_FAILED(newdn);
848 newdn->comp_num = dn1->comp_num;
849 newdn->components = talloc_array(newdn, struct ldb_dn_component, newdn->comp_num);
850 } else {
851 int comp_num = dn2->comp_num;
852 if (dn1 != NULL) comp_num += dn1->comp_num;
853 newdn = ldb_dn_copy_partial(mem_ctx, dn2, comp_num);
854 LDB_DN_NULL_FAILED(newdn);
857 if (dn1 == NULL) {
858 return newdn;
861 for (i = 0; i < dn1->comp_num; i++) {
862 newdn->components[i] = ldb_dn_copy_component(newdn->components,
863 &(dn1->components[i]));
864 if (newdn->components[i].value.data == NULL) {
865 goto failed;
869 return newdn;
871 failed:
872 talloc_free(newdn);
873 return NULL;
876 struct ldb_dn *ldb_dn_string_compose(void *mem_ctx, const struct ldb_dn *base, const char *child_fmt, ...)
878 struct ldb_dn *dn, *dn1;
879 char *child_str;
880 va_list ap;
882 if (child_fmt == NULL) return NULL;
884 va_start(ap, child_fmt);
885 child_str = talloc_vasprintf(mem_ctx, child_fmt, ap);
886 va_end(ap);
888 if (child_str == NULL) return NULL;
890 dn1 = ldb_dn_explode(mem_ctx, child_str);
891 dn = ldb_dn_compose(mem_ctx, dn1, base);
893 talloc_free(child_str);
894 talloc_free(dn1);
896 return dn;
899 /* Create a 'canonical name' string from a DN:
901 ie dc=samba,dc=org -> samba.org/
902 uid=administrator,ou=users,dc=samba,dc=org = samba.org/users/administrator
904 There are two formats, the EX format has the last / replaced with a newline (\n).
907 static char *ldb_dn_canonical(void *mem_ctx, const struct ldb_dn *dn, int ex_format) {
908 int i;
909 char *cracked = NULL;
911 /* Walk backwards down the DN, grabbing 'dc' components at first */
912 for (i = dn->comp_num - 1 ; i >= 0; i--) {
913 if (ldb_attr_cmp(dn->components[i].name, "dc") != 0) {
914 break;
916 if (cracked) {
917 cracked = talloc_asprintf(mem_ctx, "%s.%s",
918 ldb_dn_escape_value(mem_ctx, dn->components[i].value),
919 cracked);
920 } else {
921 cracked = ldb_dn_escape_value(mem_ctx, dn->components[i].value);
923 if (!cracked) {
924 return NULL;
928 /* Only domain components? Finish here */
929 if (i < 0) {
930 if (ex_format) {
931 cracked = talloc_asprintf(mem_ctx, "%s\n", cracked);
932 } else {
933 cracked = talloc_asprintf(mem_ctx, "%s/", cracked);
935 return cracked;
938 /* Now walk backwards appending remaining components */
939 for (; i > 0; i--) {
940 cracked = talloc_asprintf(mem_ctx, "%s/%s", cracked,
941 ldb_dn_escape_value(mem_ctx, dn->components[i].value));
942 if (!cracked) {
943 return NULL;
947 /* Last one, possibly a newline for the 'ex' format */
948 if (ex_format) {
949 cracked = talloc_asprintf(mem_ctx, "%s\n%s", cracked,
950 ldb_dn_escape_value(mem_ctx, dn->components[i].value));
951 } else {
952 cracked = talloc_asprintf(mem_ctx, "%s/%s", cracked,
953 ldb_dn_escape_value(mem_ctx, dn->components[i].value));
955 return cracked;
958 /* Wrapper functions for the above, for the two different string formats */
959 char *ldb_dn_canonical_string(void *mem_ctx, const struct ldb_dn *dn) {
960 return ldb_dn_canonical(mem_ctx, dn, 0);
964 char *ldb_dn_canonical_ex_string(void *mem_ctx, const struct ldb_dn *dn) {
965 return ldb_dn_canonical(mem_ctx, dn, 1);
968 int ldb_dn_get_comp_num(const struct ldb_dn *dn)
970 return dn->comp_num;
973 const char *ldb_dn_get_component_name(const struct ldb_dn *dn, unsigned int num)
975 if (num >= dn->comp_num) return NULL;
976 return dn->components[num].name;
979 const struct ldb_val *ldb_dn_get_component_val(const struct ldb_dn *dn, unsigned int num)
981 if (num >= dn->comp_num) return NULL;
982 return &dn->components[num].value;
985 const char *ldb_dn_get_rdn_name(const struct ldb_dn *dn) {
986 if (dn->comp_num == 0) return NULL;
987 return dn->components[0].name;
990 const struct ldb_val *ldb_dn_get_rdn_val(const struct ldb_dn *dn) {
991 if (dn->comp_num == 0) return NULL;
992 return &dn->components[0].value;
995 int ldb_dn_set_component(struct ldb_dn *dn, int num, const char *name, const struct ldb_val val)
997 char *n;
998 struct ldb_val v;
1000 if (num >= dn->comp_num) {
1001 return LDB_ERR_OTHER;
1004 n = talloc_strdup(dn, name);
1005 if ( ! n) {
1006 return LDB_ERR_OTHER;
1009 v.length = val.length;
1010 v.data = (uint8_t *)talloc_memdup(dn, val.data, v.length+1);
1011 if ( ! v.data) {
1012 return LDB_ERR_OTHER;
1015 talloc_free(dn->components[num].name);
1016 talloc_free(dn->components[num].value.data);
1017 dn->components[num].name = n;
1018 dn->components[num].value = v;
1020 return LDB_SUCCESS;