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 static int ldb_dn_is_valid_attribute_name(const char *name)
60 if (name == NULL) return 0;
63 if (! isascii(*name)) {
66 if (! (isalnum((unsigned char)*name) || *name == '-')) {
75 char *ldb_dn_escape_value(void *mem_ctx, struct ldb_val value)
77 const char *p, *s, *src;
84 p = s = src = (const char *)value.data;
87 /* allocate destination string, it will be at most 3 times the source */
88 dst = d = talloc_array(mem_ctx, char, len * 3 + 1);
89 LDB_DN_NULL_FAILED(dst);
91 while (p - src < len) {
93 p += strcspn(p, ",=\n+<>#;\\\"");
95 if (p - src == len) /* found no escapable chars */
98 memcpy(d, s, p - s); /* copy the part of the string before the stop */
99 d += (p - s); /* move to current position */
101 if (*p) { /* it is a normal escapable character */
104 } else { /* we have a zero byte in the string */
105 strncpy(d, "\00", 3); /* escape the zero */
107 p++; /* skip the zero */
109 s = p; /* move forward */
112 /* copy the last part (with zero) and return */
113 memcpy(d, s, &src[len] - s + 1);
122 static struct ldb_val ldb_dn_unescape_value(void *mem_ctx, const char *src)
124 struct ldb_val value;
126 char *p, *dst = NULL, *end;
128 memset(&value, 0, sizeof(value));
130 LDB_DN_NULL_FAILED(src);
132 dst = p = talloc_memdup(mem_ctx, src, strlen(src) + 1);
133 LDB_DN_NULL_FAILED(dst);
135 end = &dst[strlen(dst)];
138 p += strcspn(p, ",=\n+<>#;\\\"");
141 if (strchr(",=\n+<>#;\\\"", p[1])) {
142 memmove(p, p + 1, end - (p + 1) + 1);
148 if (sscanf(p + 1, "%02x", &x) == 1) {
149 *p = (unsigned char)x;
150 memmove(p + 1, p + 3, end - (p + 3) + 1);
157 /* a string with not escaped specials is invalid (tested) */
163 value.length = end - dst;
164 value.data = (uint8_t *)dst;
172 /* check if the string contains quotes
173 * skips leading and trailing spaces
174 * - returns 0 if no quotes found
175 * - returns 1 if quotes are found and put their position
176 * in *quote_start and *quote_end parameters
177 * - return -1 if there are open quotes
180 static int get_quotes_position(const char *source, int *quote_start, int *quote_end)
184 if (source == NULL || quote_start == NULL || quote_end == NULL) return -1;
188 /* check if there are quotes surrounding the value */
189 p += strspn(p, " \n"); /* skip white spaces */
192 *quote_start = p - source;
197 LDB_DN_NULL_FAILED(p);
199 if (*(p - 1) == '\\')
203 *quote_end = p - source;
213 static char *seek_to_separator(char *string, const char *separators)
216 int ret, qs, qe, escaped;
218 if (string == NULL || separators == NULL) return NULL;
220 p = strchr(string, '=');
221 LDB_DN_NULL_FAILED(p);
225 /* check if there are quotes surrounding the value */
227 ret = get_quotes_position(p, &qs, &qe);
231 if (ret == 1) { /* quotes found */
233 p += qe; /* positioning after quotes */
234 p += strspn(p, " \n"); /* skip white spaces after the quote */
236 if (strcspn(p, separators) != 0) /* if there are characters between quotes */
237 return NULL; /* and separators, the dn is invalid */
239 return p; /* return on the separator */
242 /* no quotes found seek to separators */
246 ret = strcspn(q, separators);
248 if (q[ret - 1] == '\\') {
254 if (ret == 0 && p == q) /* no separators ?! bail out */
263 static char *ldb_dn_trim_string(char *string, const char *edge)
267 /* seek out edge from start of string */
268 s = string + strspn(string, edge);
270 /* backwards skip from end of string */
271 p = &s[strlen(s) - 1];
272 while (p > s && strchr(edge, *p)) {
280 /* we choosed to not support multpile valued components */
281 static struct ldb_dn_component ldb_dn_explode_component(void *mem_ctx, char *raw_component)
283 struct ldb_dn_component dc;
287 memset(&dc, 0, sizeof(dc));
289 if (raw_component == NULL) {
293 /* find attribute type/value separator */
294 p = strchr(raw_component, '=');
295 LDB_DN_NULL_FAILED(p);
297 *p++ = '\0'; /* terminate name and point to value */
299 /* copy and trim name in the component */
300 dc.name = talloc_strdup(mem_ctx, ldb_dn_trim_string(raw_component, " \n"));
304 if (! ldb_dn_is_valid_attribute_name(dc.name)) {
308 ret = get_quotes_position(p, &qs, &qe);
311 case 0: /* no quotes trim the string */
312 p = ldb_dn_trim_string(p, " \n");
313 dc.value = ldb_dn_unescape_value(mem_ctx, p);
316 case 1: /* quotes found get the unquoted string */
319 dc.value.length = strlen(p);
320 dc.value.data = talloc_memdup(mem_ctx, p, dc.value.length + 1);
323 default: /* mismatched quotes ot other error, bail out */
327 if (dc.value.length == 0) {
334 talloc_free(dc.name);
339 struct ldb_dn *ldb_dn_new(void *mem_ctx)
343 edn = talloc(mem_ctx, struct ldb_dn);
344 LDB_DN_NULL_FAILED(edn);
346 /* Initially there are no components */
348 edn->components = NULL;
356 struct ldb_dn *ldb_dn_explode(void *mem_ctx, const char *dn)
358 struct ldb_dn *edn; /* the exploded dn */
361 if (dn == NULL) return NULL;
363 /* Allocate a structure to hold the exploded DN */
364 edn = ldb_dn_new(mem_ctx);
372 /* Special DNs case */
375 edn->components = talloc(edn, struct ldb_dn_component);
376 if (edn->components == NULL) goto failed;
377 edn->components[0].name = talloc_strdup(edn->components, LDB_SPECIAL);
378 if (edn->components[0].name == NULL) goto failed;
379 edn->components[0].value.data = (uint8_t *)talloc_strdup(edn->components, dn);
380 if (edn->components[0].value.data== NULL) goto failed;
381 edn->components[0].value.length = strlen(dn);
385 pdn = p = talloc_strdup(edn, dn);
386 LDB_DN_NULL_FAILED(pdn);
388 /* get the components */
392 /* terminate the current component and return pointer to the next one */
393 t = seek_to_separator(p, ",;");
394 LDB_DN_NULL_FAILED(t);
396 if (*t) { /* here there is a separator */
397 *t = '\0'; /*terminate */
398 t++; /* a separtor means another component follows */
401 /* allocate space to hold the dn component */
402 edn->components = talloc_realloc(edn, edn->components,
403 struct ldb_dn_component,
405 if (edn->components == NULL)
408 /* store the exploded component in the main structure */
409 edn->components[edn->comp_num] = ldb_dn_explode_component(edn, p);
410 LDB_DN_NULL_FAILED(edn->components[edn->comp_num].name);
414 /* jump to the next component if any */
428 struct ldb_dn *ldb_dn_explode_or_special(void *mem_ctx, const char *dn)
430 struct ldb_dn *edn; /* the exploded dn */
432 if (dn == NULL) return NULL;
434 if (strncasecmp(dn, "<GUID=", 6) == 0) {
435 /* this is special DN returned when the
436 * exploded_dn control is used
439 /* Allocate a structure to hold the exploded DN */
440 edn = ldb_dn_new(mem_ctx);
443 edn->components = talloc(edn, struct ldb_dn_component);
444 if (edn->components == NULL) goto failed;
445 edn->components[0].name = talloc_strdup(edn->components, LDB_SPECIAL);
446 if (edn->components[0].name == NULL) goto failed;
447 edn->components[0].value.data = (uint8_t *)talloc_strdup(edn->components, dn);
448 if (edn->components[0].value.data== NULL) goto failed;
449 edn->components[0].value.length = strlen(dn);
454 return ldb_dn_explode(mem_ctx, dn);
461 char *ldb_dn_linearize(void *mem_ctx, const struct ldb_dn *edn)
466 if (edn == NULL) return NULL;
469 if (ldb_dn_is_special(edn)) {
470 dn = talloc_strdup(mem_ctx, (char *)edn->components[0].value.data);
474 dn = talloc_strdup(mem_ctx, "");
475 LDB_DN_NULL_FAILED(dn);
477 for (i = 0; i < edn->comp_num; i++) {
478 value = ldb_dn_escape_value(dn, edn->components[i].value);
479 LDB_DN_NULL_FAILED(value);
482 dn = talloc_asprintf_append(dn, "%s=%s", edn->components[i].name, value);
484 dn = talloc_asprintf_append(dn, ",%s=%s", edn->components[i].name, value);
486 LDB_DN_NULL_FAILED(dn);
498 /* compare DNs using casefolding compare functions */
500 int ldb_dn_compare_base(struct ldb_context *ldb,
501 const struct ldb_dn *base,
502 const struct ldb_dn *dn)
507 if (base->comp_num > dn->comp_num) {
508 return (dn->comp_num - base->comp_num);
511 if (base == NULL || base->comp_num == 0) return 0;
512 if (dn == NULL || dn->comp_num == 0) return -1;
513 if (base->comp_num > dn->comp_num) return -1;
515 /* if the number of components doesn't match they differ */
516 n0 = base->comp_num - 1;
517 n1 = dn->comp_num - 1;
518 while (n0 >= 0 && n1 >= 0) {
519 const struct ldb_attrib_handler *h;
521 /* compare names (attribute names are guaranteed to be ASCII only) */
522 ret = ldb_caseless_cmp(base->components[n0].name,
523 dn->components[n1].name);
528 /* names match, compare values */
529 h = ldb_attrib_handler(ldb, base->components[n0].name);
530 ret = h->comparison_fn(ldb, ldb, &(base->components[n0].value),
531 &(dn->components[n1].value));
542 int ldb_dn_compare(struct ldb_context *ldb,
543 const struct ldb_dn *edn0,
544 const struct ldb_dn *edn1)
546 if (edn0 == NULL || edn1 == NULL) return edn1 - edn0;
548 if (edn0->comp_num != edn1->comp_num)
549 return (edn1->comp_num - edn0->comp_num);
551 return ldb_dn_compare_base(ldb, edn0, edn1);
554 int ldb_dn_cmp(struct ldb_context *ldb, const char *dn0, const char *dn1)
560 if (dn0 == NULL || dn1 == NULL) return dn1 - dn0;
562 edn0 = ldb_dn_explode_casefold(ldb, dn0);
563 if (edn0 == NULL) return 1;
565 edn1 = ldb_dn_explode_casefold(ldb, dn1);
571 ret = ldb_dn_compare(ldb, edn0, edn1);
580 casefold a dn. We need to casefold the attribute names, and canonicalize
581 attribute values of case insensitive attributes.
583 struct ldb_dn *ldb_dn_casefold(struct ldb_context *ldb, const struct ldb_dn *edn)
588 if (edn == NULL) return NULL;
590 cedn = ldb_dn_new(ldb);
591 LDB_DN_NULL_FAILED(cedn);
593 cedn->comp_num = edn->comp_num;
594 cedn->components = talloc_array(cedn, struct ldb_dn_component, edn->comp_num);
595 LDB_DN_NULL_FAILED(cedn->components);
597 for (i = 0; i < edn->comp_num; i++) {
598 struct ldb_dn_component dc;
599 const struct ldb_attrib_handler *h;
601 dc.name = ldb_casefold(cedn, edn->components[i].name);
602 LDB_DN_NULL_FAILED(dc.name);
604 h = ldb_attrib_handler(ldb, dc.name);
605 if (h->canonicalise_fn(ldb, cedn, &(edn->components[i].value), &(dc.value)) != 0) {
609 cedn->components[i] = dc;
619 struct ldb_dn *ldb_dn_explode_casefold(struct ldb_context *ldb, const char *dn)
621 struct ldb_dn *edn, *cdn;
623 if (dn == NULL) return NULL;
625 edn = ldb_dn_explode(ldb, dn);
626 if (edn == NULL) return NULL;
628 cdn = ldb_dn_casefold(ldb, edn);
634 char *ldb_dn_linearize_casefold(struct ldb_context *ldb, const struct ldb_dn *edn)
639 if (edn == NULL) return NULL;
642 if (ldb_dn_is_special(edn)) {
643 dn = talloc_strdup(ldb, (char *)edn->components[0].value.data);
647 cdn = ldb_dn_casefold(ldb, edn);
648 if (cdn == NULL) return NULL;
650 dn = ldb_dn_linearize(ldb, cdn);
660 static struct ldb_dn_component ldb_dn_copy_component(void *mem_ctx, struct ldb_dn_component *src)
662 struct ldb_dn_component dst;
664 memset(&dst, 0, sizeof(dst));
670 dst.value = ldb_val_dup(mem_ctx, &(src->value));
671 if (dst.value.data == NULL) {
675 dst.name = talloc_strdup(mem_ctx, src->name);
676 if (dst.name == NULL) {
677 talloc_free(dst.value.data);
683 /* copy specified number of elements of a dn into a new one
684 element are copied from top level up to the unique rdn
685 num_el may be greater then dn->comp_num (see ldb_dn_make_child)
687 struct ldb_dn *ldb_dn_copy_partial(void *mem_ctx, const struct ldb_dn *dn, int num_el)
692 if (dn == NULL) return NULL;
693 if (num_el <= 0) return NULL;
695 new = ldb_dn_new(mem_ctx);
696 LDB_DN_NULL_FAILED(new);
698 new->comp_num = num_el;
699 n = new->comp_num - 1;
700 new->components = talloc_array(new, struct ldb_dn_component, new->comp_num);
702 if (dn->comp_num == 0) return new;
703 e = dn->comp_num - 1;
705 for (i = 0; i < new->comp_num; i++) {
706 new->components[n - i] = ldb_dn_copy_component(new->components,
707 &(dn->components[e - i]));
720 struct ldb_dn *ldb_dn_copy(void *mem_ctx, const struct ldb_dn *dn)
722 if (dn == NULL) return NULL;
723 return ldb_dn_copy_partial(mem_ctx, dn, dn->comp_num);
726 struct ldb_dn *ldb_dn_get_parent(void *mem_ctx, const struct ldb_dn *dn)
728 if (dn == NULL) return NULL;
729 return ldb_dn_copy_partial(mem_ctx, dn, dn->comp_num - 1);
732 struct ldb_dn_component *ldb_dn_build_component(void *mem_ctx, const char *attr,
735 struct ldb_dn_component *dc;
737 if (attr == NULL || val == NULL) return NULL;
739 dc = talloc(mem_ctx, struct ldb_dn_component);
740 if (dc == NULL) return NULL;
742 dc->name = talloc_strdup(dc, attr);
743 if (dc->name == NULL) {
748 dc->value.data = (uint8_t *)talloc_strdup(dc, val);
749 if (dc->value.data == NULL) {
754 dc->value.length = strlen(val);
759 struct ldb_dn *ldb_dn_build_child(void *mem_ctx, const char *attr,
761 const struct ldb_dn *base)
764 if (! ldb_dn_is_valid_attribute_name(attr)) return NULL;
765 if (value == NULL || value == '\0') return NULL;
768 new = ldb_dn_copy_partial(mem_ctx, base, base->comp_num + 1);
769 LDB_DN_NULL_FAILED(new);
771 new = ldb_dn_new(mem_ctx);
772 LDB_DN_NULL_FAILED(new);
775 new->components = talloc_array(new, struct ldb_dn_component, new->comp_num);
778 new->components[0].name = talloc_strdup(new->components, attr);
779 LDB_DN_NULL_FAILED(new->components[0].name);
781 new->components[0].value.data = (uint8_t *)talloc_strdup(new->components, value);
782 LDB_DN_NULL_FAILED(new->components[0].value.data);
783 new->components[0].value.length = strlen((char *)new->components[0].value.data);
793 struct ldb_dn *ldb_dn_make_child(void *mem_ctx, const struct ldb_dn_component *component,
794 const struct ldb_dn *base)
796 if (component == NULL) return NULL;
798 return ldb_dn_build_child(mem_ctx, component->name,
799 (char *)component->value.data, base);
802 struct ldb_dn *ldb_dn_compose(void *mem_ctx, const struct ldb_dn *dn1, const struct ldb_dn *dn2)
807 if (dn2 == NULL && dn1 == NULL) {
812 new = ldb_dn_new(mem_ctx);
813 LDB_DN_NULL_FAILED(new);
815 new->comp_num = dn1->comp_num;
816 new->components = talloc_array(new, struct ldb_dn_component, new->comp_num);
818 int comp_num = dn2->comp_num;
819 if (dn1 != NULL) comp_num += dn1->comp_num;
820 new = ldb_dn_copy_partial(mem_ctx, dn2, comp_num);
827 for (i = 0; i < dn1->comp_num; i++) {
828 new->components[i] = ldb_dn_copy_component(new->components,
829 &(dn1->components[i]));
839 struct ldb_dn *ldb_dn_string_compose(void *mem_ctx, const struct ldb_dn *base, const char *child_fmt, ...)
846 if (child_fmt == NULL) return NULL;
848 va_start(ap, child_fmt);
849 ret = vasprintf(&child_str, child_fmt, ap);
852 if (ret <= 0) return NULL;
854 dn = ldb_dn_compose(mem_ctx, ldb_dn_explode(mem_ctx, child_str), base);
861 struct ldb_dn_component *ldb_dn_get_rdn(void *mem_ctx, const struct ldb_dn *dn)
863 struct ldb_dn_component *rdn;
865 if (dn == NULL) return NULL;
867 if (dn->comp_num < 1) {
871 rdn = talloc(mem_ctx, struct ldb_dn_component);
872 if (rdn == NULL) return NULL;
874 *rdn = ldb_dn_copy_component(mem_ctx, &dn->components[0]);
875 if (rdn->name == NULL) {
883 /* Create a 'canonical name' string from a DN:
885 ie dc=samba,dc=org -> samba.org/
886 uid=administrator,ou=users,dc=samba,dc=org = samba.org/users/administrator
888 There are two formats, the EX format has the last / replaced with a newline (\n).
891 static char *ldb_dn_canonical(void *mem_ctx, const struct ldb_dn *dn, int ex_format) {
893 char *cracked = NULL;
895 /* Walk backwards down the DN, grabbing 'dc' components at first */
896 for (i = dn->comp_num - 1 ; i >= 0; i--) {
897 if (ldb_attr_cmp(dn->components[i].name, "dc") != 0) {
901 cracked = talloc_asprintf(mem_ctx, "%s.%s",
902 ldb_dn_escape_value(mem_ctx, dn->components[i].value),
905 cracked = ldb_dn_escape_value(mem_ctx, dn->components[i].value);
912 /* Only domain components? Finish here */
915 cracked = talloc_asprintf(mem_ctx, "%s\n", cracked);
917 cracked = talloc_asprintf(mem_ctx, "%s/", cracked);
922 /* Now walk backwards appending remaining components */
924 cracked = talloc_asprintf(mem_ctx, "%s/%s", cracked,
925 ldb_dn_escape_value(mem_ctx, dn->components[i].value));
931 /* Last one, possibly a newline for the 'ex' format */
933 cracked = talloc_asprintf(mem_ctx, "%s\n%s", cracked,
934 ldb_dn_escape_value(mem_ctx, dn->components[i].value));
936 cracked = talloc_asprintf(mem_ctx, "%s/%s", cracked,
937 ldb_dn_escape_value(mem_ctx, dn->components[i].value));
942 /* Wrapper functions for the above, for the two different string formats */
943 char *ldb_dn_canonical_string(void *mem_ctx, const struct ldb_dn *dn) {
944 return ldb_dn_canonical(mem_ctx, dn, 0);
948 char *ldb_dn_canonical_ex_string(void *mem_ctx, const struct ldb_dn *dn) {
949 return ldb_dn_canonical(mem_ctx, dn, 1);