48f471bf6fdf86a1d8a1b439ff7db02a01c36bd6
[jelmer/samba4-debian.git] / source / lib / ldb / common / ldb_dn.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce 2005
5
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
9    
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.
14
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.
19
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
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb dn explode and utility functions
29  *
30  *  Description: - explode a dn into it's own basic elements
31  *                 and put them in a structure
32  *               - manipulate ldb_dn structures
33  *
34  *  Author: Simo Sorce
35  */
36
37 #include "includes.h"
38 #include "ldb/include/includes.h"
39
40 #define LDB_DN_NULL_FAILED(x) if (!(x)) goto failed
41
42 #define LDB_SPECIAL "@SPECIAL"
43
44 /**
45    internal ldb exploded dn structures
46 */
47 struct ldb_dn_component {
48         char *name;  
49         struct ldb_val value;
50 };
51
52 struct ldb_dn {
53         int comp_num;
54         struct ldb_dn_component *components;
55 };
56
57 int ldb_dn_is_special(const struct ldb_dn *dn)
58 {
59         if (dn == NULL || dn->comp_num != 1) return 0;
60
61         return ! strcmp(dn->components[0].name, LDB_SPECIAL);
62 }
63
64 int ldb_dn_check_special(const struct ldb_dn *dn, const char *check)
65 {
66         if (dn == NULL || dn->comp_num != 1) return 0;
67
68         return ! strcmp((char *)dn->components[0].value.data, check);
69 }
70
71 char *ldb_dn_escape_value(void *mem_ctx, struct ldb_val value)
72 {
73         const char *p, *s, *src;
74         char *d, *dst;
75         int len;
76
77         if (!value.length)
78                 return NULL;
79
80         p = s = src = (const char *)value.data;
81         len = value.length;
82
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);
86
87         while (p - src < len) {
88
89                 p += strcspn(p, ",=\n+<>#;\\\"");
90
91                 if (p - src == len) /* found no escapable chars */
92                         break;
93
94                 memcpy(d, s, p - s); /* copy the part of the string before the stop */
95                 d += (p - s); /* move to current position */
96
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 */
104                 }
105                 s = p; /* move forward */
106         }
107
108         /* copy the last part (with zero) and return */
109         memcpy(d, s, &src[len] - s + 1);
110
111         return dst;
112
113 failed:
114         talloc_free(dst);
115         return NULL;
116 }
117
118 static struct ldb_val ldb_dn_unescape_value(void *mem_ctx, const char *src)
119 {
120         struct ldb_val value;
121         unsigned x;
122         char *p, *dst = NULL, *end;
123
124         memset(&value, 0, sizeof(value));
125
126         LDB_DN_NULL_FAILED(src);
127
128         dst = p = (char *)talloc_memdup(mem_ctx, src, strlen(src) + 1);
129         LDB_DN_NULL_FAILED(dst);
130
131         end = &dst[strlen(dst)];
132
133         while (*p) {
134                 p += strcspn(p, ",=\n+<>#;\\\"");
135
136                 if (*p == '\\') {
137                         if (strchr(",=\n+<>#;\\\"", p[1])) {
138                                 memmove(p, p + 1, end - (p + 1) + 1);
139                                 end--;
140                                 p++;
141                                 continue;
142                         }
143
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;
150                         }
151                 }
152
153                 /* a string with not escaped specials is invalid (tested) */
154                 if (*p != '\0') {
155                         goto failed;
156                 }
157         }
158
159         value.length = end - dst;
160         value.data = (uint8_t *)dst;
161         return value;
162
163 failed:
164         talloc_free(dst);
165         return value;
166 }
167
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
174  */
175
176 static int get_quotes_position(const char *source, int *quote_start, int *quote_end)
177 {
178         const char *p;
179
180         if (source == NULL || quote_start == NULL || quote_end == NULL) return -1;
181
182         p = source;
183
184         /* check if there are quotes surrounding the value */
185         p += strspn(p, " \n"); /* skip white spaces */
186
187         if (*p == '\"') {
188                 *quote_start = p - source;
189
190                 p++;
191                 while (*p != '\"') {
192                         p = strchr(p, '\"');
193                         LDB_DN_NULL_FAILED(p);
194
195                         if (*(p - 1) == '\\')
196                                 p++;
197                 }
198
199                 *quote_end = p - source;
200                 return 1;
201         }
202
203         return 0;
204
205 failed:
206         return -1;
207 }
208
209 static char *seek_to_separator(char *string, const char *separators)
210 {
211         char *p, *q;
212         int ret, qs, qe, escaped;
213
214         if (string == NULL || separators == NULL) return NULL;
215
216         p = strchr(string, '=');
217         LDB_DN_NULL_FAILED(p);
218
219         p++;
220
221         /* check if there are quotes surrounding the value */
222
223         ret = get_quotes_position(p, &qs, &qe);
224         if (ret == -1)
225                 return NULL;
226
227         if (ret == 1) { /* quotes found */
228
229                 p += qe; /* positioning after quotes */
230                 p += strspn(p, " \n"); /* skip white spaces after the quote */
231
232                 if (strcspn(p, separators) != 0) /* if there are characters between quotes */
233                         return NULL;        /* and separators, the dn is invalid */
234
235                 return p; /* return on the separator */
236         }
237
238         /* no quotes found seek to separators */
239         q = p;
240         do {
241                 escaped = 0;
242                 ret = strcspn(q, separators);
243                 
244                 if (q[ret - 1] == '\\') {
245                         escaped = 1;
246                         q = q + ret + 1;
247                 }
248         } while (escaped);
249
250         if (ret == 0 && p == q) /* no separators ?! bail out */
251                 return NULL;
252
253         return q + ret;
254
255 failed:
256         return NULL;
257 }
258
259 static char *ldb_dn_trim_string(char *string, const char *edge)
260 {
261         char *s, *p;
262
263         /* seek out edge from start of string */
264         s = string + strspn(string, edge);
265
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--;
271         }
272
273         return s;
274 }
275
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)
278 {
279         struct ldb_dn_component dc;
280         char *p;
281         int ret, qs, qe;
282
283         memset(&dc, 0, sizeof(dc));
284
285         if (raw_component == NULL) {
286                 return dc;
287         }
288
289         /* find attribute type/value separator */
290         p = strchr(raw_component, '=');
291         LDB_DN_NULL_FAILED(p);
292
293         *p++ = '\0'; /* terminate name and point to value */
294
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;
299
300         if (! ldb_valid_attr_name(dc.name)) {
301                 goto failed;
302         }
303
304         ret = get_quotes_position(p, &qs, &qe);
305
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;
311
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;
319
320         default: /* mismatched quotes ot other error, bail out */
321                 goto failed;
322         }
323
324         if (dc.value.length == 0) {
325                 goto failed;
326         }
327
328         return dc;
329
330 failed:
331         talloc_free(dc.name);
332         dc.name = NULL;
333         return dc;
334 }
335
336 struct ldb_dn *ldb_dn_new(void *mem_ctx)
337 {
338         struct ldb_dn *edn;
339
340         edn = talloc(mem_ctx, struct ldb_dn);
341         LDB_DN_NULL_FAILED(edn);
342
343         /* Initially there are no components */
344         edn->comp_num = 0;
345         edn->components = NULL;
346
347         return edn;
348
349 failed:
350         return NULL;
351 }
352
353 /*
354   explode a DN string into a ldb_dn structure
355 */
356 struct ldb_dn *ldb_dn_explode(void *mem_ctx, const char *dn)
357 {
358         struct ldb_dn *edn; /* the exploded dn */
359         char *pdn, *p;
360
361         if (dn == NULL) return NULL;
362
363         /* Allocate a structure to hold the exploded DN */
364         edn = ldb_dn_new(mem_ctx);
365         if (edn == NULL) {
366                 return NULL;
367         }
368
369         pdn = NULL;
370
371         /* Empty DNs */
372         if (dn[0] == '\0') {
373                 return edn;
374         }
375
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;
387         }
388
389         pdn = p = talloc_strdup(edn, dn);
390         LDB_DN_NULL_FAILED(pdn);
391
392         /* get the components */
393         do {
394                 char *t;
395
396                 /* terminate the current component and return pointer to the next one */
397                 t = seek_to_separator(p, ",;");
398                 LDB_DN_NULL_FAILED(t);
399
400                 if (*t) { /* here there is a separator */
401                         *t = '\0'; /*terminate */
402                         t++; /* a separtor means another component follows */
403                 }
404
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;
411
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);
415
416                 edn->comp_num++;
417
418                 /* jump to the next component if any */
419                 p = t;
420
421         } while(*p);
422
423         talloc_free(pdn);
424         return edn;
425
426 failed:
427         talloc_free(pdn);
428         talloc_free(edn);
429         return NULL;
430 }
431
432 struct ldb_dn *ldb_dn_explode_or_special(void *mem_ctx, const char *dn)
433 {
434         struct ldb_dn *edn; /* the exploded dn */
435
436         if (dn == NULL) return NULL;
437
438         if (strncasecmp(dn, "<GUID=", 6) == 0) {
439                 /* this is special DN returned when the
440                  * exploded_dn control is used
441                  */
442
443                 /* Allocate a structure to hold the exploded DN */
444                 edn = ldb_dn_new(mem_ctx);
445
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;
455
456         }
457         
458         return ldb_dn_explode(mem_ctx, dn);
459
460 failed:
461         talloc_free(edn);
462         return NULL;
463 }
464
465 char *ldb_dn_linearize(void *mem_ctx, const struct ldb_dn *edn)
466 {
467         char *dn, *value;
468         int i;
469
470         if (edn == NULL) return NULL;
471
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;
476         }
477
478         dn = talloc_strdup(mem_ctx, "");
479         LDB_DN_NULL_FAILED(dn);
480
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);
484
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);
489                 }
490                 LDB_DN_NULL_FAILED(dn);
491
492                 talloc_free(value);
493         }
494
495         return dn;
496
497 failed:
498         talloc_free(dn);
499         return NULL;
500 }
501
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
505  */
506
507 int ldb_dn_compare_base(struct ldb_context *ldb,
508                         const struct ldb_dn *base,
509                         const struct ldb_dn *dn)
510 {
511         int ret;
512         int n0, n1;
513
514         if (base == NULL || base->comp_num == 0) return 0;
515         if (dn == NULL || dn->comp_num == 0) return -1;
516
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);
520         }
521
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;
526
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;
532                 }
533
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;
540                 }
541                 n1--;
542                 n0--;
543         }
544
545         return 0;
546 }
547
548 /* compare DNs using casefolding compare functions.  
549
550    If they match, then return 0
551  */
552
553 int ldb_dn_compare(struct ldb_context *ldb,
554                    const struct ldb_dn *edn0,
555                    const struct ldb_dn *edn1)
556 {
557         if (edn0 == NULL || edn1 == NULL) return edn1 - edn0;
558
559         if (edn0->comp_num != edn1->comp_num)
560                 return (edn1->comp_num - edn0->comp_num);
561
562         return ldb_dn_compare_base(ldb, edn0, edn1);
563 }
564
565 int ldb_dn_cmp(struct ldb_context *ldb, const char *dn0, const char *dn1)
566 {
567         struct ldb_dn *edn0;
568         struct ldb_dn *edn1;
569         int ret;
570
571         if (dn0 == NULL || dn1 == NULL) return dn1 - dn0;
572
573         edn0 = ldb_dn_explode_casefold(ldb, ldb, dn0);
574         if (edn0 == NULL) return 1;
575
576         edn1 = ldb_dn_explode_casefold(ldb, ldb, dn1);
577         if (edn1 == NULL) {
578                 talloc_free(edn0);
579                 return -1;
580         }
581
582         ret = ldb_dn_compare(ldb, edn0, edn1);
583
584         talloc_free(edn0);
585         talloc_free(edn1);
586
587         return ret;
588 }
589
590 /*
591   casefold a dn. We need to casefold the attribute names, and canonicalize 
592   attribute values of case insensitive attributes.
593 */
594 struct ldb_dn *ldb_dn_casefold(struct ldb_context *ldb, void *mem_ctx, const struct ldb_dn *edn)
595 {
596         struct ldb_dn *cedn;
597         int i, ret;
598
599         if (edn == NULL) return NULL;
600
601         cedn = ldb_dn_new(mem_ctx);
602         if (!cedn) {
603                 return NULL;
604         }
605
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;
611         }
612
613         for (i = 0; i < edn->comp_num; i++) {
614                 struct ldb_dn_component dc;
615                 const struct ldb_attrib_handler *h;
616
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;
622                 }
623
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;
631                 }
632
633                 cedn->components[i] = dc;
634         }
635
636         return cedn;
637 }
638
639 struct ldb_dn *ldb_dn_explode_casefold(struct ldb_context *ldb, void *mem_ctx, const char *dn)
640 {
641         struct ldb_dn *edn, *cdn;
642
643         if (dn == NULL) return NULL;
644
645         edn = ldb_dn_explode(ldb, dn);
646         if (edn == NULL) return NULL;
647
648         cdn = ldb_dn_casefold(ldb, mem_ctx, edn);
649         
650         talloc_free(edn);
651         return cdn;
652 }
653
654 char *ldb_dn_linearize_casefold(struct ldb_context *ldb, void *mem_ctx, const struct ldb_dn *edn)
655 {
656         struct ldb_dn *cdn;
657         char *dn;
658
659         if (edn == NULL) return NULL;
660
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;
665         }
666
667         cdn = ldb_dn_casefold(ldb, mem_ctx, edn);
668         if (cdn == NULL) return NULL;
669
670         dn = ldb_dn_linearize(ldb, cdn);
671         if (dn == NULL) {
672                 talloc_free(cdn);
673                 return NULL;
674         }
675
676         talloc_free(cdn);
677         return dn;
678 }
679
680 static struct ldb_dn_component ldb_dn_copy_component(void *mem_ctx, struct ldb_dn_component *src)
681 {
682         struct ldb_dn_component dst;
683
684         memset(&dst, 0, sizeof(dst));
685
686         if (src == NULL) {
687                 return dst;
688         }
689
690         dst.value = ldb_val_dup(mem_ctx, &(src->value));
691         if (dst.value.data == NULL) {
692                 return dst;
693         }
694
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;
699         }
700
701         return dst;
702 }
703
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)
706 {
707         struct ldb_dn *new_dn;
708         int i, offset;
709
710         /* Perhaps we don't need to rebase at all? */
711         if (!old_base || !new_base) {
712                 return ldb_dn_copy(mem_ctx, old);
713         }
714
715         offset = old->comp_num - old_base->comp_num;
716         new_dn = ldb_dn_copy_partial(mem_ctx, new_base, offset + new_base->comp_num);
717         for (i = 0; i < offset; i++) {
718                 new_dn->components[i] = ldb_dn_copy_component(new_dn->components, &(old->components[i]));
719         }
720
721         return new_dn;
722 }
723
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)
727 */
728 struct ldb_dn *ldb_dn_copy_partial(void *mem_ctx, const struct ldb_dn *dn, int num_el)
729 {
730         struct ldb_dn *newdn;
731         int i, n, e;
732
733         if (dn == NULL) return NULL;
734         if (num_el <= 0) return NULL;
735
736         newdn = ldb_dn_new(mem_ctx);
737         LDB_DN_NULL_FAILED(newdn);
738
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;
743
744         if (dn->comp_num == 0) return newdn;
745         e = dn->comp_num - 1;
746
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;
752                 }
753         }
754
755         return newdn;
756
757 failed:
758         talloc_free(newdn);
759         return NULL;
760 }
761
762 struct ldb_dn *ldb_dn_copy(void *mem_ctx, const struct ldb_dn *dn)
763 {
764         if (dn == NULL) return NULL;
765         return ldb_dn_copy_partial(mem_ctx, dn, dn->comp_num);
766 }
767
768 struct ldb_dn *ldb_dn_get_parent(void *mem_ctx, const struct ldb_dn *dn)
769 {
770         if (dn == NULL) return NULL;
771         return ldb_dn_copy_partial(mem_ctx, dn, dn->comp_num - 1);
772 }
773
774 struct ldb_dn_component *ldb_dn_build_component(void *mem_ctx, const char *attr,
775                                                 const char *val)
776 {
777         struct ldb_dn_component *dc;
778
779         if (attr == NULL || val == NULL) return NULL;
780
781         dc = talloc(mem_ctx, struct ldb_dn_component);
782         if (dc == NULL) return NULL;
783
784         dc->name = talloc_strdup(dc, attr);
785         if (dc->name ==  NULL) {
786                 talloc_free(dc);
787                 return NULL;
788         }
789
790         dc->value.data = (uint8_t *)talloc_strdup(dc, val);
791         if (dc->value.data ==  NULL) {
792                 talloc_free(dc);
793                 return NULL;
794         }
795
796         dc->value.length = strlen(val);
797
798         return dc;
799 }
800
801 struct ldb_dn *ldb_dn_build_child(void *mem_ctx, const char *attr,
802                                                  const char * value,
803                                                  const struct ldb_dn *base)
804 {
805         struct ldb_dn *newdn;
806         if (! ldb_valid_attr_name(attr)) return NULL;
807         if (value == NULL || value == '\0') return NULL; 
808
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);
815
816                 newdn->comp_num = 1;
817                 newdn->components = talloc_array(newdn, struct ldb_dn_component, newdn->comp_num);
818         }
819
820         newdn->components[0].name = talloc_strdup(newdn->components, attr);
821         LDB_DN_NULL_FAILED(newdn->components[0].name);
822
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);
826
827         return newdn;
828
829 failed:
830         talloc_free(newdn);
831         return NULL;
832
833 }
834
835 struct ldb_dn *ldb_dn_compose(void *mem_ctx, const struct ldb_dn *dn1, const struct ldb_dn *dn2)
836 {
837         int i;
838         struct ldb_dn *newdn;
839
840         if (dn2 == NULL && dn1 == NULL) {
841                 return NULL;
842         }
843
844         if (dn2 == NULL) {
845                 newdn = ldb_dn_new(mem_ctx);
846                 LDB_DN_NULL_FAILED(newdn);
847
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);
855         }
856
857         if (dn1 == NULL) {
858                 return newdn;
859         }
860
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;
866                 }
867         }
868
869         return newdn;
870
871 failed:
872         talloc_free(newdn);
873         return NULL;
874 }
875
876 struct ldb_dn *ldb_dn_string_compose(void *mem_ctx, const struct ldb_dn *base, const char *child_fmt, ...)
877 {
878         struct ldb_dn *dn, *dn1;
879         char *child_str;
880         va_list ap;
881         
882         if (child_fmt == NULL) return NULL;
883
884         va_start(ap, child_fmt);
885         child_str = talloc_vasprintf(mem_ctx, child_fmt, ap);
886         va_end(ap);
887
888         if (child_str == NULL) return NULL;
889
890         dn1 = ldb_dn_explode(mem_ctx, child_str);
891         dn = ldb_dn_compose(mem_ctx, dn1, base);
892
893         talloc_free(child_str);
894         talloc_free(dn1);
895
896         return dn;
897 }
898
899 /* Create a 'canonical name' string from a DN:
900
901    ie dc=samba,dc=org -> samba.org/
902       uid=administrator,ou=users,dc=samba,dc=org = samba.org/users/administrator
903
904    There are two formats, the EX format has the last / replaced with a newline (\n).
905
906 */
907 static char *ldb_dn_canonical(void *mem_ctx, const struct ldb_dn *dn, int ex_format) {
908         int i;
909         char *cracked = NULL;
910
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;
915                 }
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);
922                 }
923                 if (!cracked) {
924                         return NULL;
925                 }
926         }
927
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);
934                 }
935                 return cracked;
936         }
937
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;
944                 }
945         }
946
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));
954         }
955         return cracked;
956 }
957
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);
961
962 }
963
964 char *ldb_dn_canonical_ex_string(void *mem_ctx, const struct ldb_dn *dn) {
965         return ldb_dn_canonical(mem_ctx, dn, 1);
966 }
967
968 int ldb_dn_get_comp_num(const struct ldb_dn *dn)
969 {
970         return dn->comp_num;
971 }
972
973 const char *ldb_dn_get_component_name(const struct ldb_dn *dn, unsigned int num)
974 {
975         if (num >= dn->comp_num) return NULL;
976         return dn->components[num].name;
977 }
978
979 const struct ldb_val *ldb_dn_get_component_val(const struct ldb_dn *dn, unsigned int num)
980 {
981         if (num >= dn->comp_num) return NULL;
982         return &dn->components[num].value;
983 }
984
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;
988 }
989
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;
993 }
994
995 int ldb_dn_set_component(struct ldb_dn *dn, int num, const char *name, const struct ldb_val val)
996 {
997         char *n;
998         struct ldb_val v;
999
1000         if (num >= dn->comp_num) {
1001                 return LDB_ERR_OTHER;
1002         }
1003
1004         n = talloc_strdup(dn, name);
1005         if ( ! n) {
1006                 return LDB_ERR_OTHER;
1007         }
1008
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;
1013         }
1014
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;
1019
1020         return LDB_SUCCESS;
1021 }