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
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
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
38 #include "ldb/include/includes.h"
40 #define LDB_DN_NULL_FAILED(x) if (!(x)) goto failed
42 #define LDB_SPECIAL "@SPECIAL"
44 int ldb_dn_is_special(const struct ldb_dn *dn)
46 if (dn == NULL || dn->comp_num != 1) return 0;
48 return ! strcmp(dn->components[0].name, LDB_SPECIAL);
51 int ldb_dn_check_special(const struct ldb_dn *dn, const char *check)
53 if (dn == NULL || dn->comp_num != 1) return 0;
55 return ! strcmp((char *)dn->components[0].value.data, check);
58 char *ldb_dn_escape_value(void *mem_ctx, struct ldb_val value)
60 const char *p, *s, *src;
67 p = s = src = (const char *)value.data;
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);
74 while (p - src < len) {
76 p += strcspn(p, ",=\n+<>#;\\\"");
78 if (p - src == len) /* found no escapable chars */
81 memcpy(d, s, p - s); /* copy the part of the string before the stop */
82 d += (p - s); /* move to current position */
84 if (*p) { /* it is a normal escapable character */
87 } else { /* we have a zero byte in the string */
88 strncpy(d, "\00", 3); /* escape the zero */
90 p++; /* skip the zero */
92 s = p; /* move forward */
95 /* copy the last part (with zero) and return */
96 memcpy(d, s, &src[len] - s + 1);
105 static struct ldb_val ldb_dn_unescape_value(void *mem_ctx, const char *src)
107 struct ldb_val value;
109 char *p, *dst = NULL, *end;
111 memset(&value, 0, sizeof(value));
113 LDB_DN_NULL_FAILED(src);
115 dst = p = talloc_memdup(mem_ctx, src, strlen(src) + 1);
116 LDB_DN_NULL_FAILED(dst);
118 end = &dst[strlen(dst)];
121 p += strcspn(p, ",=\n+<>#;\\\"");
124 if (strchr(",=\n+<>#;\\\"", p[1])) {
125 memmove(p, p + 1, end - (p + 1) + 1);
131 if (sscanf(p + 1, "%02x", &x) == 1) {
132 *p = (unsigned char)x;
133 memmove(p + 1, p + 3, end - (p + 3) + 1);
140 /* a string with not escaped specials is invalid (tested) */
146 value.length = end - dst;
147 value.data = (uint8_t *)dst;
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
163 static int get_quotes_position(const char *source, int *quote_start, int *quote_end)
167 if (source == NULL || quote_start == NULL || quote_end == NULL) return -1;
171 /* check if there are quotes surrounding the value */
172 p += strspn(p, " \n"); /* skip white spaces */
175 *quote_start = p - source;
180 LDB_DN_NULL_FAILED(p);
182 if (*(p - 1) == '\\')
186 *quote_end = p - source;
196 static char *seek_to_separator(char *string, const char *separators)
199 int ret, qs, qe, escaped;
201 if (string == NULL || separators == NULL) return NULL;
203 p = strchr(string, '=');
204 LDB_DN_NULL_FAILED(p);
208 /* check if there are quotes surrounding the value */
210 ret = get_quotes_position(p, &qs, &qe);
214 if (ret == 1) { /* quotes found */
216 p += qe; /* positioning after quotes */
217 p += strspn(p, " \n"); /* skip white spaces after the quote */
219 if (strcspn(p, separators) != 0) /* if there are characters between quotes */
220 return NULL; /* and separators, the dn is invalid */
222 return p; /* return on the separator */
225 /* no quotes found seek to separators */
229 ret = strcspn(q, separators);
231 if (q[ret - 1] == '\\') {
237 if (ret == 0 && p == q) /* no separators ?! bail out */
246 static char *ldb_dn_trim_string(char *string, const char *edge)
250 /* seek out edge from start of string */
251 s = string + strspn(string, edge);
253 /* backwards skip from end of string */
254 p = &s[strlen(s) - 1];
255 while (p > s && strchr(edge, *p)) {
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)
266 struct ldb_dn_component dc;
270 memset(&dc, 0, sizeof(dc));
272 if (raw_component == NULL) {
276 /* find attribute type/value separator */
277 p = strchr(raw_component, '=');
278 LDB_DN_NULL_FAILED(p);
280 *p++ = '\0'; /* terminate name and point to value */
282 /* copy and trim name in the component */
283 dc.name = talloc_strdup(mem_ctx, ldb_dn_trim_string(raw_component, " \n"));
287 if (! ldb_valid_attr_name(dc.name)) {
291 ret = get_quotes_position(p, &qs, &qe);
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);
299 case 1: /* quotes found get the unquoted string */
302 dc.value.length = strlen(p);
303 dc.value.data = talloc_memdup(mem_ctx, p, dc.value.length + 1);
306 default: /* mismatched quotes ot other error, bail out */
310 if (dc.value.length == 0) {
317 talloc_free(dc.name);
322 struct ldb_dn *ldb_dn_new(void *mem_ctx)
326 edn = talloc(mem_ctx, struct ldb_dn);
327 LDB_DN_NULL_FAILED(edn);
329 /* Initially there are no components */
331 edn->components = NULL;
339 struct ldb_dn *ldb_dn_explode(void *mem_ctx, const char *dn)
341 struct ldb_dn *edn; /* the exploded dn */
344 if (dn == NULL) return NULL;
346 /* Allocate a structure to hold the exploded DN */
347 edn = ldb_dn_new(mem_ctx);
355 /* Special DNs case */
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);
368 pdn = p = talloc_strdup(edn, dn);
369 LDB_DN_NULL_FAILED(pdn);
371 /* get the components */
375 /* terminate the current component and return pointer to the next one */
376 t = seek_to_separator(p, ",;");
377 LDB_DN_NULL_FAILED(t);
379 if (*t) { /* here there is a separator */
380 *t = '\0'; /*terminate */
381 t++; /* a separtor means another component follows */
384 /* allocate space to hold the dn component */
385 edn->components = talloc_realloc(edn, edn->components,
386 struct ldb_dn_component,
388 if (edn->components == NULL)
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);
397 /* jump to the next component if any */
411 struct ldb_dn *ldb_dn_explode_or_special(void *mem_ctx, const char *dn)
413 struct ldb_dn *edn; /* the exploded dn */
415 if (dn == NULL) return NULL;
417 if (strncasecmp(dn, "<GUID=", 6) == 0) {
418 /* this is special DN returned when the
419 * exploded_dn control is used
422 /* Allocate a structure to hold the exploded DN */
423 edn = ldb_dn_new(mem_ctx);
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);
437 return ldb_dn_explode(mem_ctx, dn);
444 char *ldb_dn_linearize(void *mem_ctx, const struct ldb_dn *edn)
449 if (edn == NULL) return NULL;
452 if (ldb_dn_is_special(edn)) {
453 dn = talloc_strdup(mem_ctx, (char *)edn->components[0].value.data);
457 dn = talloc_strdup(mem_ctx, "");
458 LDB_DN_NULL_FAILED(dn);
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);
465 dn = talloc_asprintf_append(dn, "%s=%s", edn->components[i].name, value);
467 dn = talloc_asprintf_append(dn, ",%s=%s", edn->components[i].name, value);
469 LDB_DN_NULL_FAILED(dn);
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
486 int ldb_dn_compare_base(struct ldb_context *ldb,
487 const struct ldb_dn *base,
488 const struct ldb_dn *dn)
493 if (base == NULL || base->comp_num == 0) return 0;
494 if (dn == NULL || dn->comp_num == 0) return -1;
496 if (base->comp_num > dn->comp_num) {
497 return (dn->comp_num - base->comp_num);
500 /* if the number of components doesn't match they differ */
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;
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);
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));
527 /* compare DNs using casefolding compare functions.
529 If they match, then return 0
532 int ldb_dn_compare(struct ldb_context *ldb,
533 const struct ldb_dn *edn0,
534 const struct ldb_dn *edn1)
536 if (edn0 == NULL || edn1 == NULL) return edn1 - edn0;
538 if (edn0->comp_num != edn1->comp_num)
539 return (edn1->comp_num - edn0->comp_num);
541 return ldb_dn_compare_base(ldb, edn0, edn1);
544 int ldb_dn_cmp(struct ldb_context *ldb, const char *dn0, const char *dn1)
550 if (dn0 == NULL || dn1 == NULL) return dn1 - dn0;
552 edn0 = ldb_dn_explode_casefold(ldb, dn0);
553 if (edn0 == NULL) return 1;
555 edn1 = ldb_dn_explode_casefold(ldb, dn1);
561 ret = ldb_dn_compare(ldb, edn0, edn1);
570 casefold a dn. We need to casefold the attribute names, and canonicalize
571 attribute values of case insensitive attributes.
573 struct ldb_dn *ldb_dn_casefold(struct ldb_context *ldb, const struct ldb_dn *edn)
578 if (edn == NULL) return NULL;
580 cedn = ldb_dn_new(ldb);
581 LDB_DN_NULL_FAILED(cedn);
583 cedn->comp_num = edn->comp_num;
584 cedn->components = talloc_array(cedn, struct ldb_dn_component, edn->comp_num);
585 LDB_DN_NULL_FAILED(cedn->components);
587 for (i = 0; i < edn->comp_num; i++) {
588 struct ldb_dn_component dc;
589 const struct ldb_attrib_handler *h;
591 dc.name = ldb_attr_casefold(cedn, edn->components[i].name);
592 LDB_DN_NULL_FAILED(dc.name);
594 h = ldb_attrib_handler(ldb, dc.name);
595 if (h->canonicalise_fn(ldb, cedn, &(edn->components[i].value), &(dc.value)) != 0) {
599 cedn->components[i] = dc;
609 struct ldb_dn *ldb_dn_explode_casefold(struct ldb_context *ldb, const char *dn)
611 struct ldb_dn *edn, *cdn;
613 if (dn == NULL) return NULL;
615 edn = ldb_dn_explode(ldb, dn);
616 if (edn == NULL) return NULL;
618 cdn = ldb_dn_casefold(ldb, edn);
624 char *ldb_dn_linearize_casefold(struct ldb_context *ldb, const struct ldb_dn *edn)
629 if (edn == NULL) return NULL;
632 if (ldb_dn_is_special(edn)) {
633 dn = talloc_strdup(ldb, (char *)edn->components[0].value.data);
637 cdn = ldb_dn_casefold(ldb, edn);
638 if (cdn == NULL) return NULL;
640 dn = ldb_dn_linearize(ldb, cdn);
650 static struct ldb_dn_component ldb_dn_copy_component(void *mem_ctx, struct ldb_dn_component *src)
652 struct ldb_dn_component dst;
654 memset(&dst, 0, sizeof(dst));
660 dst.value = ldb_val_dup(mem_ctx, &(src->value));
661 if (dst.value.data == NULL) {
665 dst.name = talloc_strdup(mem_ctx, src->name);
666 if (dst.name == NULL) {
667 talloc_free(dst.value.data);
673 /* copy specified number of elements of a dn into a new one
674 element are copied from top level up to the unique rdn
675 num_el may be greater than dn->comp_num (see ldb_dn_make_child)
677 struct ldb_dn *ldb_dn_copy_partial(void *mem_ctx, const struct ldb_dn *dn, int num_el)
682 if (dn == NULL) return NULL;
683 if (num_el <= 0) return NULL;
685 new = ldb_dn_new(mem_ctx);
686 LDB_DN_NULL_FAILED(new);
688 new->comp_num = num_el;
689 n = new->comp_num - 1;
690 new->components = talloc_array(new, struct ldb_dn_component, new->comp_num);
692 if (dn->comp_num == 0) return new;
693 e = dn->comp_num - 1;
695 for (i = 0; i < new->comp_num; i++) {
696 new->components[n - i] = ldb_dn_copy_component(new->components,
697 &(dn->components[e - i]));
710 struct ldb_dn *ldb_dn_copy(void *mem_ctx, const struct ldb_dn *dn)
712 if (dn == NULL) return NULL;
713 return ldb_dn_copy_partial(mem_ctx, dn, dn->comp_num);
716 struct ldb_dn *ldb_dn_get_parent(void *mem_ctx, const struct ldb_dn *dn)
718 if (dn == NULL) return NULL;
719 return ldb_dn_copy_partial(mem_ctx, dn, dn->comp_num - 1);
722 struct ldb_dn_component *ldb_dn_build_component(void *mem_ctx, const char *attr,
725 struct ldb_dn_component *dc;
727 if (attr == NULL || val == NULL) return NULL;
729 dc = talloc(mem_ctx, struct ldb_dn_component);
730 if (dc == NULL) return NULL;
732 dc->name = talloc_strdup(dc, attr);
733 if (dc->name == NULL) {
738 dc->value.data = (uint8_t *)talloc_strdup(dc, val);
739 if (dc->value.data == NULL) {
744 dc->value.length = strlen(val);
749 struct ldb_dn *ldb_dn_build_child(void *mem_ctx, const char *attr,
751 const struct ldb_dn *base)
754 if (! ldb_valid_attr_name(attr)) return NULL;
755 if (value == NULL || value == '\0') return NULL;
758 new = ldb_dn_copy_partial(mem_ctx, base, base->comp_num + 1);
759 LDB_DN_NULL_FAILED(new);
761 new = ldb_dn_new(mem_ctx);
762 LDB_DN_NULL_FAILED(new);
765 new->components = talloc_array(new, struct ldb_dn_component, new->comp_num);
768 new->components[0].name = talloc_strdup(new->components, attr);
769 LDB_DN_NULL_FAILED(new->components[0].name);
771 new->components[0].value.data = (uint8_t *)talloc_strdup(new->components, value);
772 LDB_DN_NULL_FAILED(new->components[0].value.data);
773 new->components[0].value.length = strlen((char *)new->components[0].value.data);
783 struct ldb_dn *ldb_dn_make_child(void *mem_ctx, const struct ldb_dn_component *component,
784 const struct ldb_dn *base)
786 if (component == NULL) return NULL;
788 return ldb_dn_build_child(mem_ctx, component->name,
789 (char *)component->value.data, base);
792 struct ldb_dn *ldb_dn_compose(void *mem_ctx, const struct ldb_dn *dn1, const struct ldb_dn *dn2)
797 if (dn2 == NULL && dn1 == NULL) {
802 new = ldb_dn_new(mem_ctx);
803 LDB_DN_NULL_FAILED(new);
805 new->comp_num = dn1->comp_num;
806 new->components = talloc_array(new, struct ldb_dn_component, new->comp_num);
808 int comp_num = dn2->comp_num;
809 if (dn1 != NULL) comp_num += dn1->comp_num;
810 new = ldb_dn_copy_partial(mem_ctx, dn2, comp_num);
817 for (i = 0; i < dn1->comp_num; i++) {
818 new->components[i] = ldb_dn_copy_component(new->components,
819 &(dn1->components[i]));
829 struct ldb_dn *ldb_dn_string_compose(void *mem_ctx, const struct ldb_dn *base, const char *child_fmt, ...)
836 if (child_fmt == NULL) return NULL;
838 va_start(ap, child_fmt);
839 ret = vasprintf(&child_str, child_fmt, ap);
842 if (ret <= 0) return NULL;
844 dn = ldb_dn_compose(mem_ctx, ldb_dn_explode(mem_ctx, child_str), base);
851 struct ldb_dn_component *ldb_dn_get_rdn(void *mem_ctx, const struct ldb_dn *dn)
853 struct ldb_dn_component *rdn;
855 if (dn == NULL) return NULL;
857 if (dn->comp_num < 1) {
861 rdn = talloc(mem_ctx, struct ldb_dn_component);
862 if (rdn == NULL) return NULL;
864 *rdn = ldb_dn_copy_component(mem_ctx, &dn->components[0]);
865 if (rdn->name == NULL) {
873 /* Create a 'canonical name' string from a DN:
875 ie dc=samba,dc=org -> samba.org/
876 uid=administrator,ou=users,dc=samba,dc=org = samba.org/users/administrator
878 There are two formats, the EX format has the last / replaced with a newline (\n).
881 static char *ldb_dn_canonical(void *mem_ctx, const struct ldb_dn *dn, int ex_format) {
883 char *cracked = NULL;
885 /* Walk backwards down the DN, grabbing 'dc' components at first */
886 for (i = dn->comp_num - 1 ; i >= 0; i--) {
887 if (ldb_attr_cmp(dn->components[i].name, "dc") != 0) {
891 cracked = talloc_asprintf(mem_ctx, "%s.%s",
892 ldb_dn_escape_value(mem_ctx, dn->components[i].value),
895 cracked = ldb_dn_escape_value(mem_ctx, dn->components[i].value);
902 /* Only domain components? Finish here */
905 cracked = talloc_asprintf(mem_ctx, "%s\n", cracked);
907 cracked = talloc_asprintf(mem_ctx, "%s/", cracked);
912 /* Now walk backwards appending remaining components */
914 cracked = talloc_asprintf(mem_ctx, "%s/%s", cracked,
915 ldb_dn_escape_value(mem_ctx, dn->components[i].value));
921 /* Last one, possibly a newline for the 'ex' format */
923 cracked = talloc_asprintf(mem_ctx, "%s\n%s", cracked,
924 ldb_dn_escape_value(mem_ctx, dn->components[i].value));
926 cracked = talloc_asprintf(mem_ctx, "%s/%s", cracked,
927 ldb_dn_escape_value(mem_ctx, dn->components[i].value));
932 /* Wrapper functions for the above, for the two different string formats */
933 char *ldb_dn_canonical_string(void *mem_ctx, const struct ldb_dn *dn) {
934 return ldb_dn_canonical(mem_ctx, dn, 0);
938 char *ldb_dn_canonical_ex_string(void *mem_ctx, const struct ldb_dn *dn) {
939 return ldb_dn_canonical(mem_ctx, dn, 1);