r18866: Jeremy and Volker have given the go-ahead on the group mapping ldb
[kai/samba-autobuild/.git] / source3 / 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 int ldb_dn_is_special(const struct ldb_dn *dn)
45 {
46         if (dn == NULL || dn->comp_num != 1) return 0;
47
48         return ! strcmp(dn->components[0].name, LDB_SPECIAL);
49 }
50
51 int ldb_dn_check_special(const struct ldb_dn *dn, const char *check)
52 {
53         if (dn == NULL || dn->comp_num != 1) return 0;
54
55         return ! strcmp((char *)dn->components[0].value.data, check);
56 }
57
58 char *ldb_dn_escape_value(void *mem_ctx, struct ldb_val value)
59 {
60         const char *p, *s, *src;
61         char *d, *dst;
62         int len;
63
64         if (!value.length)
65                 return NULL;
66
67         p = s = src = (const char *)value.data;
68         len = value.length;
69
70         /* allocate destination string, it will be at most 3 times the source */
71         dst = d = talloc_array(mem_ctx, char, len * 3 + 1);
72         LDB_DN_NULL_FAILED(dst);
73
74         while (p - src < len) {
75
76                 p += strcspn(p, ",=\n+<>#;\\\"");
77
78                 if (p - src == len) /* found no escapable chars */
79                         break;
80
81                 memcpy(d, s, p - s); /* copy the part of the string before the stop */
82                 d += (p - s); /* move to current position */
83
84                 if (*p) { /* it is a normal escapable character */
85                         *d++ = '\\';
86                         *d++ = *p++;
87                 } else { /* we have a zero byte in the string */
88                         strncpy(d, "\00", 3); /* escape the zero */
89                         d = d + 3;
90                         p++; /* skip the zero */
91                 }
92                 s = p; /* move forward */
93         }
94
95         /* copy the last part (with zero) and return */
96         memcpy(d, s, &src[len] - s + 1);
97
98         return dst;
99
100 failed:
101         talloc_free(dst);
102         return NULL;
103 }
104
105 static struct ldb_val ldb_dn_unescape_value(void *mem_ctx, const char *src)
106 {
107         struct ldb_val value;
108         unsigned x;
109         char *p, *dst = NULL, *end;
110
111         memset(&value, 0, sizeof(value));
112
113         LDB_DN_NULL_FAILED(src);
114
115         dst = p = talloc_memdup(mem_ctx, src, strlen(src) + 1);
116         LDB_DN_NULL_FAILED(dst);
117
118         end = &dst[strlen(dst)];
119
120         while (*p) {
121                 p += strcspn(p, ",=\n+<>#;\\\"");
122
123                 if (*p == '\\') {
124                         if (strchr(",=\n+<>#;\\\"", p[1])) {
125                                 memmove(p, p + 1, end - (p + 1) + 1);
126                                 end--;
127                                 p++;
128                                 continue;
129                         }
130
131                         if (sscanf(p + 1, "%02x", &x) == 1) {
132                                 *p = (unsigned char)x;
133                                 memmove(p + 1, p + 3, end - (p + 3) + 1);
134                                 end -= 2;
135                                 p++;
136                                 continue;
137                         }
138                 }
139
140                 /* a string with not escaped specials is invalid (tested) */
141                 if (*p != '\0') {
142                         goto failed;
143                 }
144         }
145
146         value.length = end - dst;
147         value.data = (uint8_t *)dst;
148         return value;
149
150 failed:
151         talloc_free(dst);
152         return value;
153 }
154
155 /* check if the string contains quotes
156  * skips leading and trailing spaces
157  * - returns 0 if no quotes found
158  * - returns 1 if quotes are found and put their position
159  *   in *quote_start and *quote_end parameters
160  * - return -1 if there are open quotes
161  */
162
163 static int get_quotes_position(const char *source, int *quote_start, int *quote_end)
164 {
165         const char *p;
166
167         if (source == NULL || quote_start == NULL || quote_end == NULL) return -1;
168
169         p = source;
170
171         /* check if there are quotes surrounding the value */
172         p += strspn(p, " \n"); /* skip white spaces */
173
174         if (*p == '\"') {
175                 *quote_start = p - source;
176
177                 p++;
178                 while (*p != '\"') {
179                         p = strchr(p, '\"');
180                         LDB_DN_NULL_FAILED(p);
181
182                         if (*(p - 1) == '\\')
183                                 p++;
184                 }
185
186                 *quote_end = p - source;
187                 return 1;
188         }
189
190         return 0;
191
192 failed:
193         return -1;
194 }
195
196 static char *seek_to_separator(char *string, const char *separators)
197 {
198         char *p, *q;
199         int ret, qs, qe, escaped;
200
201         if (string == NULL || separators == NULL) return NULL;
202
203         p = strchr(string, '=');
204         LDB_DN_NULL_FAILED(p);
205
206         p++;
207
208         /* check if there are quotes surrounding the value */
209
210         ret = get_quotes_position(p, &qs, &qe);
211         if (ret == -1)
212                 return NULL;
213
214         if (ret == 1) { /* quotes found */
215
216                 p += qe; /* positioning after quotes */
217                 p += strspn(p, " \n"); /* skip white spaces after the quote */
218
219                 if (strcspn(p, separators) != 0) /* if there are characters between quotes */
220                         return NULL;        /* and separators, the dn is invalid */
221
222                 return p; /* return on the separator */
223         }
224
225         /* no quotes found seek to separators */
226         q = p;
227         do {
228                 escaped = 0;
229                 ret = strcspn(q, separators);
230                 
231                 if (q[ret - 1] == '\\') {
232                         escaped = 1;
233                         q = q + ret + 1;
234                 }
235         } while (escaped);
236
237         if (ret == 0 && p == q) /* no separators ?! bail out */
238                 return NULL;
239
240         return q + ret;
241
242 failed:
243         return NULL;
244 }
245
246 static char *ldb_dn_trim_string(char *string, const char *edge)
247 {
248         char *s, *p;
249
250         /* seek out edge from start of string */
251         s = string + strspn(string, edge);
252
253         /* backwards skip from end of string */
254         p = &s[strlen(s) - 1];
255         while (p > s && strchr(edge, *p)) {
256                 *p = '\0';
257                 p--;
258         }
259
260         return s;
261 }
262
263 /* we choosed to not support multpile valued components */
264 static struct ldb_dn_component ldb_dn_explode_component(void *mem_ctx, char *raw_component)
265 {
266         struct ldb_dn_component dc;
267         char *p;
268         int ret, qs, qe;
269
270         memset(&dc, 0, sizeof(dc));
271
272         if (raw_component == NULL) {
273                 return dc;
274         }
275
276         /* find attribute type/value separator */
277         p = strchr(raw_component, '=');
278         LDB_DN_NULL_FAILED(p);
279
280         *p++ = '\0'; /* terminate name and point to value */
281
282         /* copy and trim name in the component */
283         dc.name = talloc_strdup(mem_ctx, ldb_dn_trim_string(raw_component, " \n"));
284         if (!dc.name)
285                 return dc;
286
287         if (! ldb_valid_attr_name(dc.name)) {
288                 goto failed;
289         }
290
291         ret = get_quotes_position(p, &qs, &qe);
292
293         switch (ret) {
294         case 0: /* no quotes trim the string */
295                 p = ldb_dn_trim_string(p, " \n");
296                 dc.value = ldb_dn_unescape_value(mem_ctx, p);
297                 break;
298
299         case 1: /* quotes found get the unquoted string */
300                 p[qe] = '\0';
301                 p = p + qs + 1;
302                 dc.value.length = strlen(p);
303                 dc.value.data = talloc_memdup(mem_ctx, p, dc.value.length + 1);
304                 break;
305
306         default: /* mismatched quotes ot other error, bail out */
307                 goto failed;
308         }
309
310         if (dc.value.length == 0) {
311                 goto failed;
312         }
313
314         return dc;
315
316 failed:
317         talloc_free(dc.name);
318         dc.name = NULL;
319         return dc;
320 }
321
322 struct ldb_dn *ldb_dn_new(void *mem_ctx)
323 {
324         struct ldb_dn *edn;
325
326         edn = talloc(mem_ctx, struct ldb_dn);
327         LDB_DN_NULL_FAILED(edn);
328
329         /* Initially there are no components */
330         edn->comp_num = 0;
331         edn->components = NULL;
332
333         return edn;
334
335 failed:
336         return NULL;
337 }
338
339 struct ldb_dn *ldb_dn_explode(void *mem_ctx, const char *dn)
340 {
341         struct ldb_dn *edn; /* the exploded dn */
342         char *pdn, *p;
343
344         if (dn == NULL) return NULL;
345
346         /* Allocate a structure to hold the exploded DN */
347         edn = ldb_dn_new(mem_ctx);
348         pdn = NULL;
349
350         /* Empty DNs */
351         if (dn[0] == '\0') {
352                 return edn;
353         }
354
355         /* Special DNs case */
356         if (dn[0] == '@') {
357                 edn->comp_num = 1;
358                 edn->components = talloc(edn, struct ldb_dn_component);
359                 if (edn->components == NULL) goto failed;
360                 edn->components[0].name = talloc_strdup(edn->components, LDB_SPECIAL);
361                 if (edn->components[0].name == NULL) goto failed;
362                 edn->components[0].value.data = (uint8_t *)talloc_strdup(edn->components, dn);
363                 if (edn->components[0].value.data== NULL) goto failed;
364                 edn->components[0].value.length = strlen(dn);
365                 return edn;
366         }
367
368         pdn = p = talloc_strdup(edn, dn);
369         LDB_DN_NULL_FAILED(pdn);
370
371         /* get the components */
372         do {
373                 char *t;
374
375                 /* terminate the current component and return pointer to the next one */
376                 t = seek_to_separator(p, ",;");
377                 LDB_DN_NULL_FAILED(t);
378
379                 if (*t) { /* here there is a separator */
380                         *t = '\0'; /*terminate */
381                         t++; /* a separtor means another component follows */
382                 }
383
384                 /* allocate space to hold the dn component */
385                 edn->components = talloc_realloc(edn, edn->components,
386                                                  struct ldb_dn_component,
387                                                  edn->comp_num + 1);
388                 if (edn->components == NULL)
389                         goto failed;
390
391                 /* store the exploded component in the main structure */
392                 edn->components[edn->comp_num] = ldb_dn_explode_component(edn, p);
393                 LDB_DN_NULL_FAILED(edn->components[edn->comp_num].name);
394
395                 edn->comp_num++;
396
397                 /* jump to the next component if any */
398                 p = t;
399
400         } while(*p);
401
402         talloc_free(pdn);
403         return edn;
404
405 failed:
406         talloc_free(pdn);
407         talloc_free(edn);
408         return NULL;
409 }
410
411 struct ldb_dn *ldb_dn_explode_or_special(void *mem_ctx, const char *dn)
412 {
413         struct ldb_dn *edn; /* the exploded dn */
414
415         if (dn == NULL) return NULL;
416
417         if (strncasecmp(dn, "<GUID=", 6) == 0) {
418                 /* this is special DN returned when the
419                  * exploded_dn control is used
420                  */
421
422                 /* Allocate a structure to hold the exploded DN */
423                 edn = ldb_dn_new(mem_ctx);
424
425                 edn->comp_num = 1;
426                 edn->components = talloc(edn, struct ldb_dn_component);
427                 if (edn->components == NULL) goto failed;
428                 edn->components[0].name = talloc_strdup(edn->components, LDB_SPECIAL);
429                 if (edn->components[0].name == NULL) goto failed;
430                 edn->components[0].value.data = (uint8_t *)talloc_strdup(edn->components, dn);
431                 if (edn->components[0].value.data== NULL) goto failed;
432                 edn->components[0].value.length = strlen(dn);
433                 return edn;
434
435         }
436         
437         return ldb_dn_explode(mem_ctx, dn);
438
439 failed:
440         talloc_free(edn);
441         return NULL;
442 }
443
444 char *ldb_dn_linearize(void *mem_ctx, const struct ldb_dn *edn)
445 {
446         char *dn, *value;
447         int i;
448
449         if (edn == NULL) return NULL;
450
451         /* Special DNs */
452         if (ldb_dn_is_special(edn)) {
453                 dn = talloc_strdup(mem_ctx, (char *)edn->components[0].value.data);
454                 return dn;
455         }
456
457         dn = talloc_strdup(mem_ctx, "");
458         LDB_DN_NULL_FAILED(dn);
459
460         for (i = 0; i < edn->comp_num; i++) {
461                 value = ldb_dn_escape_value(dn, edn->components[i].value);
462                 LDB_DN_NULL_FAILED(value);
463
464                 if (i == 0) {
465                         dn = talloc_asprintf_append(dn, "%s=%s", edn->components[i].name, value);
466                 } else {
467                         dn = talloc_asprintf_append(dn, ",%s=%s", edn->components[i].name, value);
468                 }
469                 LDB_DN_NULL_FAILED(dn);
470
471                 talloc_free(value);
472         }
473
474         return dn;
475
476 failed:
477         talloc_free(dn);
478         return NULL;
479 }
480
481 /* Determine if dn is below base, in the ldap tree.  Used for
482  * evaluating a subtree search.
483  * 0 if they match, otherwise non-zero
484  */
485
486 int ldb_dn_compare_base(struct ldb_context *ldb,
487                         const struct ldb_dn *base,
488                         const struct ldb_dn *dn)
489 {
490         int ret;
491         int n0, n1;
492
493         if (base == NULL || base->comp_num == 0) return 0;
494         if (dn == NULL || dn->comp_num == 0) return -1;
495
496         /* if the base has more componts than the dn, then they differ */
497         if (base->comp_num > dn->comp_num) {
498                 return (dn->comp_num - base->comp_num);
499         }
500
501         n0 = base->comp_num - 1;
502         n1 = dn->comp_num - 1;
503         while (n0 >= 0 && n1 >= 0) {
504                 const struct ldb_attrib_handler *h;
505
506                 /* compare names (attribute names are guaranteed to be ASCII only) */
507                 ret = ldb_attr_cmp(base->components[n0].name,
508                                    dn->components[n1].name);
509                 if (ret) {
510                         return ret;
511                 }
512
513                 /* names match, compare values */
514                 h = ldb_attrib_handler(ldb, base->components[n0].name);
515                 ret = h->comparison_fn(ldb, ldb, &(base->components[n0].value),
516                                                   &(dn->components[n1].value));
517                 if (ret) {
518                         return ret;
519                 }
520                 n1--;
521                 n0--;
522         }
523
524         return 0;
525 }
526
527 /* compare DNs using casefolding compare functions.  
528
529    If they match, then return 0
530  */
531
532 int ldb_dn_compare(struct ldb_context *ldb,
533                    const struct ldb_dn *edn0,
534                    const struct ldb_dn *edn1)
535 {
536         if (edn0 == NULL || edn1 == NULL) return edn1 - edn0;
537
538         if (edn0->comp_num != edn1->comp_num)
539                 return (edn1->comp_num - edn0->comp_num);
540
541         return ldb_dn_compare_base(ldb, edn0, edn1);
542 }
543
544 int ldb_dn_cmp(struct ldb_context *ldb, const char *dn0, const char *dn1)
545 {
546         struct ldb_dn *edn0;
547         struct ldb_dn *edn1;
548         int ret;
549
550         if (dn0 == NULL || dn1 == NULL) return dn1 - dn0;
551
552         edn0 = ldb_dn_explode_casefold(ldb, dn0);
553         if (edn0 == NULL) return 1;
554
555         edn1 = ldb_dn_explode_casefold(ldb, dn1);
556         if (edn1 == NULL) {
557                 talloc_free(edn0);
558                 return -1;
559         }
560
561         ret = ldb_dn_compare(ldb, edn0, edn1);
562
563         talloc_free(edn0);
564         talloc_free(edn1);
565
566         return ret;
567 }
568
569 /*
570   casefold a dn. We need to casefold the attribute names, and canonicalize 
571   attribute values of case insensitive attributes.
572 */
573 struct ldb_dn *ldb_dn_casefold(struct ldb_context *ldb, const struct ldb_dn *edn)
574 {
575         struct ldb_dn *cedn;
576         int i;
577
578         if (edn == NULL) return NULL;
579
580         cedn = ldb_dn_new(ldb);
581         if (!cedn) {
582                 return NULL;
583         }
584
585         cedn->comp_num = edn->comp_num;
586         cedn->components = talloc_array(cedn, struct ldb_dn_component, edn->comp_num);
587         if (!cedn->components) {
588                 talloc_free(cedn);
589                 return NULL;
590         }
591
592         for (i = 0; i < edn->comp_num; i++) {
593                 struct ldb_dn_component dc;
594                 const struct ldb_attrib_handler *h;
595
596                 dc.name = ldb_attr_casefold(cedn, edn->components[i].name);
597                 if (!dc.name) {
598                         talloc_free(cedn);
599                         return NULL;
600                 }
601
602                 h = ldb_attrib_handler(ldb, dc.name);
603                 if (h->canonicalise_fn(ldb, cedn, &(edn->components[i].value), &(dc.value)) != 0) {
604                         talloc_free(cedn);
605                         return NULL;
606                 }
607
608                 cedn->components[i] = dc;
609         }
610
611         return cedn;
612 }
613
614 struct ldb_dn *ldb_dn_explode_casefold(struct ldb_context *ldb, const char *dn)
615 {
616         struct ldb_dn *edn, *cdn;
617
618         if (dn == NULL) return NULL;
619
620         edn = ldb_dn_explode(ldb, dn);
621         if (edn == NULL) return NULL;
622
623         cdn = ldb_dn_casefold(ldb, edn);
624         
625         talloc_free(edn);
626         return cdn;
627 }
628
629 char *ldb_dn_linearize_casefold(struct ldb_context *ldb, const struct ldb_dn *edn)
630 {
631         struct ldb_dn *cdn;
632         char *dn;
633
634         if (edn == NULL) return NULL;
635
636         /* Special DNs */
637         if (ldb_dn_is_special(edn)) {
638                 dn = talloc_strdup(ldb, (char *)edn->components[0].value.data);
639                 return dn;
640         }
641
642         cdn = ldb_dn_casefold(ldb, edn);
643         if (cdn == NULL) return NULL;
644
645         dn = ldb_dn_linearize(ldb, cdn);
646         if (dn == NULL) {
647                 talloc_free(cdn);
648                 return NULL;
649         }
650
651         talloc_free(cdn);
652         return dn;
653 }
654
655 static struct ldb_dn_component ldb_dn_copy_component(void *mem_ctx, struct ldb_dn_component *src)
656 {
657         struct ldb_dn_component dst;
658
659         memset(&dst, 0, sizeof(dst));
660
661         if (src == NULL) {
662                 return dst;
663         }
664
665         dst.value = ldb_val_dup(mem_ctx, &(src->value));
666         if (dst.value.data == NULL) {
667                 return dst;
668         }
669
670         dst.name = talloc_strdup(mem_ctx, src->name);
671         if (dst.name == NULL) {
672                 talloc_free(dst.value.data);
673         }
674
675         return dst;
676 }
677
678 /* copy specified number of elements of a dn into a new one
679    element are copied from top level up to the unique rdn
680    num_el may be greater than dn->comp_num (see ldb_dn_make_child)
681 */
682 struct ldb_dn *ldb_dn_copy_partial(void *mem_ctx, const struct ldb_dn *dn, int num_el)
683 {
684         struct ldb_dn *newdn;
685         int i, n, e;
686
687         if (dn == NULL) return NULL;
688         if (num_el <= 0) return NULL;
689
690         newdn = ldb_dn_new(mem_ctx);
691         LDB_DN_NULL_FAILED(newdn);
692
693         newdn->comp_num = num_el;
694         n = newdn->comp_num - 1;
695         newdn->components = talloc_array(newdn, struct ldb_dn_component, newdn->comp_num);
696
697         if (dn->comp_num == 0) return newdn;
698         e = dn->comp_num - 1;
699
700         for (i = 0; i < newdn->comp_num; i++) {
701                 newdn->components[n - i] = ldb_dn_copy_component(newdn->components,
702                                                                 &(dn->components[e - i]));
703                 if ((e - i) == 0) {
704                         return newdn;
705                 }
706         }
707
708         return newdn;
709
710 failed:
711         talloc_free(newdn);
712         return NULL;
713 }
714
715 struct ldb_dn *ldb_dn_copy(void *mem_ctx, const struct ldb_dn *dn)
716 {
717         if (dn == NULL) return NULL;
718         return ldb_dn_copy_partial(mem_ctx, dn, dn->comp_num);
719 }
720
721 struct ldb_dn *ldb_dn_get_parent(void *mem_ctx, const struct ldb_dn *dn)
722 {
723         if (dn == NULL) return NULL;
724         return ldb_dn_copy_partial(mem_ctx, dn, dn->comp_num - 1);
725 }
726
727 struct ldb_dn_component *ldb_dn_build_component(void *mem_ctx, const char *attr,
728                                                 const char *val)
729 {
730         struct ldb_dn_component *dc;
731
732         if (attr == NULL || val == NULL) return NULL;
733
734         dc = talloc(mem_ctx, struct ldb_dn_component);
735         if (dc == NULL) return NULL;
736
737         dc->name = talloc_strdup(dc, attr);
738         if (dc->name ==  NULL) {
739                 talloc_free(dc);
740                 return NULL;
741         }
742
743         dc->value.data = (uint8_t *)talloc_strdup(dc, val);
744         if (dc->value.data ==  NULL) {
745                 talloc_free(dc);
746                 return NULL;
747         }
748
749         dc->value.length = strlen(val);
750
751         return dc;
752 }
753
754 struct ldb_dn *ldb_dn_build_child(void *mem_ctx, const char *attr,
755                                                  const char * value,
756                                                  const struct ldb_dn *base)
757 {
758         struct ldb_dn *newdn;
759         if (! ldb_valid_attr_name(attr)) return NULL;
760         if (value == NULL || value == '\0') return NULL; 
761
762         if (base != NULL) {
763                 newdn = ldb_dn_copy_partial(mem_ctx, base, base->comp_num + 1);
764                 LDB_DN_NULL_FAILED(newdn);
765         } else {
766                 newdn = ldb_dn_new(mem_ctx);
767                 LDB_DN_NULL_FAILED(newdn);
768
769                 newdn->comp_num = 1;
770                 newdn->components = talloc_array(newdn, struct ldb_dn_component, newdn->comp_num);
771         }
772
773         newdn->components[0].name = talloc_strdup(newdn->components, attr);
774         LDB_DN_NULL_FAILED(newdn->components[0].name);
775
776         newdn->components[0].value.data = (uint8_t *)talloc_strdup(newdn->components, value);
777         LDB_DN_NULL_FAILED(newdn->components[0].value.data);
778         newdn->components[0].value.length = strlen((char *)newdn->components[0].value.data);
779
780         return newdn;
781
782 failed:
783         talloc_free(newdn);
784         return NULL;
785
786 }
787
788 struct ldb_dn *ldb_dn_make_child(void *mem_ctx, const struct ldb_dn_component *component,
789                                                 const struct ldb_dn *base)
790 {
791         if (component == NULL) return NULL;
792
793         return ldb_dn_build_child(mem_ctx, component->name, 
794                                   (char *)component->value.data, base);
795 }
796
797 struct ldb_dn *ldb_dn_compose(void *mem_ctx, const struct ldb_dn *dn1, const struct ldb_dn *dn2)
798 {
799         int i;
800         struct ldb_dn *newdn;
801
802         if (dn2 == NULL && dn1 == NULL) {
803                 return NULL;
804         }
805
806         if (dn2 == NULL) {
807                 newdn = ldb_dn_new(mem_ctx);
808                 LDB_DN_NULL_FAILED(newdn);
809
810                 newdn->comp_num = dn1->comp_num;
811                 newdn->components = talloc_array(newdn, struct ldb_dn_component, newdn->comp_num);
812         } else {
813                 int comp_num = dn2->comp_num;
814                 if (dn1 != NULL) comp_num += dn1->comp_num;
815                 newdn = ldb_dn_copy_partial(mem_ctx, dn2, comp_num);
816         }
817
818         if (dn1 == NULL) {
819                 return newdn;
820         }
821
822         for (i = 0; i < dn1->comp_num; i++) {
823                 newdn->components[i] = ldb_dn_copy_component(newdn->components,
824                                                            &(dn1->components[i]));
825         }
826
827         return newdn;
828
829 failed:
830         talloc_free(newdn);
831         return NULL;
832 }
833
834 struct ldb_dn *ldb_dn_string_compose(void *mem_ctx, const struct ldb_dn *base, const char *child_fmt, ...)
835 {
836         struct ldb_dn *dn;
837         char *child_str;
838         va_list ap;
839         
840         if (child_fmt == NULL) return NULL;
841
842         va_start(ap, child_fmt);
843         child_str = talloc_vasprintf(mem_ctx, child_fmt, ap);
844         va_end(ap);
845
846         if (child_str == NULL) return NULL;
847
848         dn = ldb_dn_compose(mem_ctx, ldb_dn_explode(mem_ctx, child_str), base);
849
850         talloc_free(child_str);
851
852         return dn;
853 }
854
855 struct ldb_dn_component *ldb_dn_get_rdn(void *mem_ctx, const struct ldb_dn *dn)
856 {
857         struct ldb_dn_component *rdn;
858
859         if (dn == NULL) return NULL;
860
861         if (dn->comp_num < 1) {
862                 return NULL;
863         }
864
865         rdn = talloc(mem_ctx, struct ldb_dn_component);
866         if (rdn == NULL) return NULL;
867
868         *rdn = ldb_dn_copy_component(mem_ctx, &dn->components[0]);
869         if (rdn->name == NULL) {
870                 talloc_free(rdn);
871                 return NULL;
872         }
873
874         return rdn;
875 }
876
877 /* Create a 'canonical name' string from a DN:
878
879    ie dc=samba,dc=org -> samba.org/
880       uid=administrator,ou=users,dc=samba,dc=org = samba.org/users/administrator
881
882    There are two formats, the EX format has the last / replaced with a newline (\n).
883
884 */
885 static char *ldb_dn_canonical(void *mem_ctx, const struct ldb_dn *dn, int ex_format) {
886         int i;
887         char *cracked = NULL;
888
889         /* Walk backwards down the DN, grabbing 'dc' components at first */
890         for (i = dn->comp_num - 1 ; i >= 0; i--) {
891                 if (ldb_attr_cmp(dn->components[i].name, "dc") != 0) {
892                         break;
893                 }
894                 if (cracked) {
895                         cracked = talloc_asprintf(mem_ctx, "%s.%s",
896                                                   ldb_dn_escape_value(mem_ctx, dn->components[i].value),
897                                                   cracked);
898                 } else {
899                         cracked = ldb_dn_escape_value(mem_ctx, dn->components[i].value);
900                 }
901                 if (!cracked) {
902                         return NULL;
903                 }
904         }
905
906         /* Only domain components?  Finish here */
907         if (i < 0) {
908                 if (ex_format) {
909                         cracked = talloc_asprintf(mem_ctx, "%s\n", cracked);
910                 } else {
911                         cracked = talloc_asprintf(mem_ctx, "%s/", cracked);
912                 }
913                 return cracked;
914         }
915
916         /* Now walk backwards appending remaining components */
917         for (; i > 0; i--) {
918                 cracked = talloc_asprintf(mem_ctx, "%s/%s", cracked, 
919                                           ldb_dn_escape_value(mem_ctx, dn->components[i].value));
920                 if (!cracked) {
921                         return NULL;
922                 }
923         }
924
925         /* Last one, possibly a newline for the 'ex' format */
926         if (ex_format) {
927                 cracked = talloc_asprintf(mem_ctx, "%s\n%s", cracked, 
928                                           ldb_dn_escape_value(mem_ctx, dn->components[i].value));
929         } else {
930                 cracked = talloc_asprintf(mem_ctx, "%s/%s", cracked, 
931                                           ldb_dn_escape_value(mem_ctx, dn->components[i].value));
932         }
933         return cracked;
934 }
935
936 /* Wrapper functions for the above, for the two different string formats */
937 char *ldb_dn_canonical_string(void *mem_ctx, const struct ldb_dn *dn) {
938         return ldb_dn_canonical(mem_ctx, dn, 0);
939
940 }
941
942 char *ldb_dn_canonical_ex_string(void *mem_ctx, const struct ldb_dn *dn) {
943         return ldb_dn_canonical(mem_ctx, dn, 1);
944 }