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 3 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 creation and manipulation utility functions
30 * Description: - explode a dn into it's own basic elements
31 * and put them in a structure (only if necessary)
32 * - manipulate ldb_dn structures
37 #include "ldb_includes.h"
40 #define LDB_DN_NULL_FAILED(x) if (!(x)) goto failed
42 #define LDB_FREE(x) do { talloc_free(x); x = NULL; } while(0)
45 internal ldb exploded dn structures
47 struct ldb_dn_component {
53 struct ldb_val cf_value;
58 struct ldb_context *ldb;
60 /* Special DNs are always linearized */
69 unsigned int comp_num;
70 struct ldb_dn_component *components;
74 /* strdn may be NULL */
75 struct ldb_dn *ldb_dn_new(void *mem_ctx, struct ldb_context *ldb, const char *strdn)
79 if ( (! mem_ctx) || (! ldb)) return NULL;
81 dn = talloc_zero(mem_ctx, struct ldb_dn);
82 LDB_DN_NULL_FAILED(dn);
87 if (strdn[0] == '@') {
90 if (strncasecmp(strdn, "<GUID=", 6) == 0) {
91 /* this is special DN returned when the
92 * exploded_dn control is used */
94 /* FIXME: add a GUID string to ldb_dn structure */
95 } else if (strncasecmp(strdn, "<SID=", 8) == 0) {
96 /* this is special DN returned when the
97 * exploded_dn control is used */
99 /* FIXME: add a SID string to ldb_dn structure */
100 } else if (strncasecmp(strdn, "<WKGUID=", 8) == 0) {
101 /* this is special DN returned when the
102 * exploded_dn control is used */
104 /* FIXME: add a WKGUID string to ldb_dn structure */
106 dn->linearized = talloc_strdup(dn, strdn);
108 dn->linearized = talloc_strdup(dn, "");
110 LDB_DN_NULL_FAILED(dn->linearized);
119 struct ldb_dn *ldb_dn_new_fmt(void *mem_ctx, struct ldb_context *ldb, const char *new_fmt, ...)
125 if ( (! mem_ctx) || (! ldb)) return NULL;
127 dn = talloc_zero(mem_ctx, struct ldb_dn);
128 LDB_DN_NULL_FAILED(dn);
132 va_start(ap, new_fmt);
133 strdn = talloc_vasprintf(dn, new_fmt, ap);
135 LDB_DN_NULL_FAILED(strdn);
137 if (strdn[0] == '@') {
140 if (strncasecmp(strdn, "<GUID=", 6) == 0) {
141 /* this is special DN returned when the
142 * exploded_dn control is used */
144 /* FIXME: add a GUID string to ldb_dn structure */
145 } else if (strncasecmp(strdn, "<SID=", 8) == 0) {
146 /* this is special DN returned when the
147 * exploded_dn control is used */
149 /* FIXME: add a SID string to ldb_dn structure */
150 } else if (strncasecmp(strdn, "<WKGUID=", 8) == 0) {
151 /* this is special DN returned when the
152 * exploded_dn control is used */
154 /* FIXME: add a WKGUID string to ldb_dn structure */
156 dn->linearized = strdn;
165 static int ldb_dn_escape_internal(char *dst, const char *src, int len)
174 while (p - src < len) {
176 p += strcspn(p, ",=\n+<>#;\\\"");
178 if (p - src == len) /* found no escapable chars */
181 memcpy(d, s, p - s); /* copy the part of the string before the stop */
182 d += (p - s); /* move to current position */
184 if (*p) { /* it is a normal escapable character */
187 } else { /* we have a zero byte in the string */
188 strncpy(d, "\00", 3); /* escape the zero */
190 p++; /* skip the zero */
192 s = p; /* move forward */
195 /* copy the last part (with zero) and return */
199 /* return the length of the resulting string */
200 return (l + (d - dst));
203 char *ldb_dn_escape_value(void *mem_ctx, struct ldb_val value)
210 /* allocate destination string, it will be at most 3 times the source */
211 dst = talloc_array(mem_ctx, char, value.length * 3 + 1);
217 ldb_dn_escape_internal(dst, (const char *)value.data, value.length);
219 dst = talloc_realloc(mem_ctx, dst, char, strlen(dst) + 1);
225 explode a DN string into a ldb_dn structure
226 based on RFC4514 except that we don't support multiple valued RDNs
228 static bool ldb_dn_explode(struct ldb_dn *dn)
230 char *p, *data, *d, *dt, *t;
232 bool in_attr = false;
233 bool in_value = false;
234 bool in_quote = false;
240 if ( ! dn || dn->invalid) return false;
242 if (dn->components) {
246 if ( ! dn->linearized) {
251 if (dn->linearized[0] == '\0') {
255 /* Special DNs case */
260 /* make sure we free this if alloced previously before replacing */
261 talloc_free(dn->components);
263 /* in the common case we have 3 or more components */
264 /* make sure all components are zeroed, other functions depend on this */
265 dn->components = talloc_zero_array(dn, struct ldb_dn_component, 3);
266 if ( ! dn->components) {
271 /* Components data space is allocated here once */
272 data = talloc_array(dn->components, char, strlen(dn->linearized) + 1);
296 /* attr names must be ascii only */
304 if ( ! isalpha(*p)) {
305 /* not a digit nor an alpha, invalid attribute name */
316 /* valid only if we are at the end */
321 if (trim && (*p != '=')) {
322 /* spaces/tabs are not allowed in attribute names */
328 /* attribute terminated */
335 dn->components[dn->comp_num].name = talloc_strdup(dn->components, dt);
336 if ( ! dn->components[dn->comp_num].name) {
348 /* attr names must be ascii only */
353 if (is_oid && ( ! (isdigit(*p) || (*p == '.')))) {
354 /* not a digit nor a dot, invalid attribute oid */
358 if ( ! (isalpha(*p) || isdigit(*p) || (*p == '-'))) {
359 /* not ALPHA, DIGIT or HYPHEN */
400 /* TODO: support ber encoded values
411 /* ok found value terminator */
425 dn->components[dn->comp_num].value.data = (uint8_t *)talloc_strdup(dn->components, dt);
426 dn->components[dn->comp_num].value.length = l;
427 if ( ! dn->components[dn->comp_num].value.data) {
435 if (dn->comp_num > 2) {
436 dn->components = talloc_realloc(dn,
438 struct ldb_dn_component,
440 if ( ! dn->components) {
444 /* make sure all components are zeroed, other functions depend on this */
445 memset(&dn->components[dn->comp_num], '\0', sizeof(struct ldb_dn_component));
458 /* a string with not escaped specials is invalid (tested) */
487 if (sscanf(p, "%02x", &x) != 1) {
488 /* invalid escaping sequence */
495 *d++ = (unsigned char)x;
517 if (in_attr || in_quote) {
523 /* save last element */
531 dn->components[dn->comp_num].value.data = (uint8_t *)talloc_strdup(dn->components, dt);
532 dn->components[dn->comp_num].value.length = l;
534 if ( ! dn->components[dn->comp_num].value.data) {
546 talloc_free(dn->components);
550 bool ldb_dn_validate(struct ldb_dn *dn)
552 return ldb_dn_explode(dn);
555 const char *ldb_dn_get_linearized(struct ldb_dn *dn)
560 if ( ! dn || ( dn->invalid)) return NULL;
562 if (dn->linearized) return dn->linearized;
564 if ( ! dn->components) {
569 if (dn->comp_num == 0) {
570 dn->linearized = talloc_strdup(dn, "");
571 if ( ! dn->linearized) return NULL;
572 return dn->linearized;
575 /* calculate maximum possible length of DN */
576 for (len = 0, i = 0; i < dn->comp_num; i++) {
577 len += strlen(dn->components[i].name); /* name len */
578 len += (dn->components[i].value.length * 3); /* max escaped data len */
579 len += 2; /* '=' and ',' */
581 dn->linearized = talloc_array(dn, char, len);
582 if ( ! dn->linearized) return NULL;
586 for (i = 0; i < dn->comp_num; i++) {
589 n = dn->components[i].name;
590 while (*n) *d++ = *n++;
595 d += ldb_dn_escape_internal( d,
596 (char *)dn->components[i].value.data,
597 dn->components[i].value.length);
603 /* don't waste more memory than necessary */
604 dn->linearized = talloc_realloc(dn, dn->linearized, char, (d - dn->linearized + 1));
606 return dn->linearized;
609 char *ldb_dn_alloc_linearized(void *mem_ctx, struct ldb_dn *dn)
611 return talloc_strdup(mem_ctx, ldb_dn_get_linearized(dn));
615 casefold a dn. We need to casefold the attribute names, and canonicalize
616 attribute values of case insensitive attributes.
619 static bool ldb_dn_casefold_internal(struct ldb_dn *dn)
623 if ( ! dn || dn->invalid) return false;
625 if (dn->valid_case) return true;
627 if (( ! dn->components) && ( ! ldb_dn_explode(dn))) {
631 for (i = 0; i < dn->comp_num; i++) {
632 const struct ldb_schema_attribute *a;
634 dn->components[i].cf_name = ldb_attr_casefold(dn->components, dn->components[i].name);
635 if (!dn->components[i].cf_name) {
639 a = ldb_schema_attribute_by_name(dn->ldb, dn->components[i].cf_name);
640 ret = a->syntax->canonicalise_fn(dn->ldb, dn->components,
641 &(dn->components[i].value),
642 &(dn->components[i].cf_value));
648 dn->valid_case = true;
653 for (i = 0; i < dn->comp_num; i++) {
654 LDB_FREE(dn->components[i].cf_name);
655 LDB_FREE(dn->components[i].cf_value.data);
660 const char *ldb_dn_get_casefold(struct ldb_dn *dn)
665 if (dn->casefold) return dn->casefold;
668 dn->casefold = talloc_strdup(dn, dn->linearized);
669 if (!dn->casefold) return NULL;
670 dn->valid_case = true;
674 if ( ! ldb_dn_casefold_internal(dn)) {
678 if (dn->comp_num == 0) {
679 if (dn->linearized && dn->linearized[0] == '\0') {
680 /* hmm a NULL dn, should we faild casefolding ? */
681 dn->casefold = talloc_strdup(dn, "");
684 /* A DN must be NULL, special, or have components */
689 /* calculate maximum possible length of DN */
690 for (len = 0, i = 0; i < dn->comp_num; i++) {
691 len += strlen(dn->components[i].cf_name); /* name len */
692 len += (dn->components[i].cf_value.length * 3); /* max escaped data len */
693 len += 2; /* '=' and ',' */
695 dn->casefold = talloc_array(dn, char, len);
696 if ( ! dn->casefold) return NULL;
700 for (i = 0; i < dn->comp_num; i++) {
703 n = dn->components[i].cf_name;
704 while (*n) *d++ = *n++;
709 d += ldb_dn_escape_internal( d,
710 (char *)dn->components[i].cf_value.data,
711 dn->components[i].cf_value.length);
716 /* don't waste more memory than necessary */
717 dn->casefold = talloc_realloc(dn, dn->casefold, char, strlen(dn->casefold) + 1);
722 char *ldb_dn_alloc_casefold(void *mem_ctx, struct ldb_dn *dn)
724 return talloc_strdup(mem_ctx, ldb_dn_get_casefold(dn));
727 /* Determine if dn is below base, in the ldap tree. Used for
728 * evaluating a subtree search.
729 * 0 if they match, otherwise non-zero
732 int ldb_dn_compare_base(struct ldb_dn *base, struct ldb_dn *dn)
737 if ( ! base || base->invalid) return 1;
738 if ( ! dn || dn->invalid) return -1;
740 if (( ! base->valid_case) || ( ! dn->valid_case)) {
741 if (base->linearized && dn->linearized) {
742 /* try with a normal compare first, if we are lucky
743 * we will avoid exploding and casfolding */
745 dif = strlen(dn->linearized) - strlen(base->linearized);
746 if (dif < 0) return dif;
747 if (strcmp(base->linearized, &dn->linearized[dif]) == 0) return 0;
750 if ( ! ldb_dn_casefold_internal(base)) {
754 if ( ! ldb_dn_casefold_internal(dn)) {
760 /* if base has more components,
761 * they don't have the same base */
762 if (base->comp_num > dn->comp_num) {
763 return (dn->comp_num - base->comp_num);
766 if (dn->comp_num == 0) {
767 if (dn->special && base->special) {
768 return strcmp(base->linearized, dn->linearized);
769 } else if (dn->special) {
771 } else if (base->special) {
778 n_base = base->comp_num - 1;
779 n_dn = dn->comp_num - 1;
781 while (n_base >= 0) {
782 /* compare attr names */
783 ret = strcmp(base->components[n_base].cf_name, dn->components[n_dn].cf_name);
784 if (ret != 0) return ret;
786 /* compare attr.cf_value. */
787 if (base->components[n_base].cf_value.length != dn->components[n_dn].cf_value.length) {
788 return base->components[n_base].cf_value.length - dn->components[n_dn].cf_value.length;
790 ret = strcmp((char *)base->components[n_base].cf_value.data, (char *)dn->components[n_dn].cf_value.data);
791 if (ret != 0) return ret;
800 /* compare DNs using casefolding compare functions.
802 If they match, then return 0
805 int ldb_dn_compare(struct ldb_dn *dn0, struct ldb_dn *dn1)
809 if (( ! dn0) || dn0->invalid || ! dn1 || dn1->invalid) return -1;
811 if (( ! dn0->valid_case) || ( ! dn1->valid_case)) {
812 if (dn0->linearized && dn1->linearized) {
813 /* try with a normal compare first, if we are lucky
814 * we will avoid exploding and casfolding */
815 if (strcmp(dn0->linearized, dn1->linearized) == 0) return 0;
818 if ( ! ldb_dn_casefold_internal(dn0)) {
822 if ( ! ldb_dn_casefold_internal(dn1)) {
828 if (dn0->comp_num != dn1->comp_num) {
829 return (dn1->comp_num - dn0->comp_num);
832 if (dn0->comp_num == 0) {
833 if (dn0->special && dn1->special) {
834 return strcmp(dn0->linearized, dn1->linearized);
835 } else if (dn0->special) {
837 } else if (dn1->special) {
844 for (i = 0; i < dn0->comp_num; i++) {
845 /* compare attr names */
846 ret = strcmp(dn0->components[i].cf_name, dn1->components[i].cf_name);
847 if (ret != 0) return ret;
849 /* compare attr.cf_value. */
850 if (dn0->components[i].cf_value.length != dn1->components[i].cf_value.length) {
851 return dn0->components[i].cf_value.length - dn1->components[i].cf_value.length;
853 ret = strcmp((char *)dn0->components[i].cf_value.data, (char *)dn1->components[i].cf_value.data);
854 if (ret != 0) return ret;
860 static struct ldb_dn_component ldb_dn_copy_component(void *mem_ctx, struct ldb_dn_component *src)
862 struct ldb_dn_component dst;
864 memset(&dst, 0, sizeof(dst));
870 dst.value = ldb_val_dup(mem_ctx, &(src->value));
871 if (dst.value.data == NULL) {
875 dst.name = talloc_strdup(mem_ctx, src->name);
876 if (dst.name == NULL) {
877 LDB_FREE(dst.value.data);
881 if (src->cf_value.data) {
882 dst.cf_value = ldb_val_dup(mem_ctx, &(src->cf_value));
883 if (dst.cf_value.data == NULL) {
884 LDB_FREE(dst.value.data);
889 dst.cf_name = talloc_strdup(mem_ctx, src->cf_name);
890 if (dst.cf_name == NULL) {
891 LDB_FREE(dst.cf_name);
892 LDB_FREE(dst.value.data);
897 dst.cf_value.data = NULL;
904 struct ldb_dn *ldb_dn_copy(void *mem_ctx, struct ldb_dn *dn)
906 struct ldb_dn *new_dn;
908 if (!dn || dn->invalid) {
912 new_dn = talloc_zero(mem_ctx, struct ldb_dn);
919 if (dn->components) {
922 new_dn->components = talloc_zero_array(new_dn, struct ldb_dn_component, dn->comp_num);
923 if ( ! new_dn->components) {
928 for (i = 0; i < dn->comp_num; i++) {
929 new_dn->components[i] = ldb_dn_copy_component(new_dn->components, &dn->components[i]);
930 if ( ! new_dn->components[i].value.data) {
938 new_dn->casefold = talloc_strdup(new_dn, dn->casefold);
939 if ( ! new_dn->casefold) {
945 if (dn->linearized) {
946 new_dn->linearized = talloc_strdup(new_dn, dn->linearized);
947 if ( ! new_dn->linearized) {
956 /* modify the given dn by adding a base.
958 * return true if successful and false if not
959 * if false is returned the dn may be marked invalid
961 bool ldb_dn_add_base(struct ldb_dn *dn, struct ldb_dn *base)
966 if ( !base || base->invalid || !dn || dn->invalid) {
970 if (dn->components) {
973 if ( ! ldb_dn_validate(base)) {
978 if (dn->valid_case) {
979 if ( ! (s = ldb_dn_get_casefold(base))) {
984 dn->components = talloc_realloc(dn,
986 struct ldb_dn_component,
987 dn->comp_num + base->comp_num);
988 if ( ! dn->components) {
993 for (i = 0; i < base->comp_num; dn->comp_num++, i++) {
994 dn->components[dn->comp_num] = ldb_dn_copy_component(dn->components, &base->components[i]);
995 if (dn->components[dn->comp_num].value.data == NULL) {
1001 if (dn->casefold && s) {
1002 if (*dn->casefold) {
1003 t = talloc_asprintf(dn, "%s,%s", dn->casefold, s);
1005 t = talloc_strdup(dn, s);
1007 LDB_FREE(dn->casefold);
1012 if (dn->linearized) {
1014 s = ldb_dn_get_linearized(base);
1019 if (*dn->linearized) {
1020 t = talloc_asprintf(dn, "%s,%s", dn->linearized, s);
1022 t = talloc_strdup(dn, s);
1028 LDB_FREE(dn->linearized);
1035 /* modify the given dn by adding a base.
1037 * return true if successful and false if not
1038 * if false is returned the dn may be marked invalid
1040 bool ldb_dn_add_base_fmt(struct ldb_dn *dn, const char *base_fmt, ...)
1042 struct ldb_dn *base;
1047 if ( !dn || dn->invalid) {
1051 va_start(ap, base_fmt);
1052 base_str = talloc_vasprintf(dn, base_fmt, ap);
1055 if (base_str == NULL) {
1059 base = ldb_dn_new(base_str, dn->ldb, base_str);
1061 ret = ldb_dn_add_base(dn, base);
1063 talloc_free(base_str);
1068 /* modify the given dn by adding children elements.
1070 * return true if successful and false if not
1071 * if false is returned the dn may be marked invalid
1073 bool ldb_dn_add_child(struct ldb_dn *dn, struct ldb_dn *child)
1078 if ( !child || child->invalid || !dn || dn->invalid) {
1082 if (dn->components) {
1085 if ( ! ldb_dn_validate(child)) {
1090 if (dn->valid_case) {
1091 if ( ! (s = ldb_dn_get_casefold(child))) {
1096 n = dn->comp_num + child->comp_num;
1098 dn->components = talloc_realloc(dn,
1100 struct ldb_dn_component,
1102 if ( ! dn->components) {
1107 for (i = dn->comp_num - 1, j = n - 1; i >= 0; i--, j--) {
1108 dn->components[j] = dn->components[i];
1111 for (i = 0; i < child->comp_num; i++) {
1112 dn->components[i] = ldb_dn_copy_component(dn->components, &child->components[i]);
1113 if (dn->components[i].value.data == NULL) {
1121 if (dn->casefold && s) {
1122 t = talloc_asprintf(dn, "%s,%s", s, dn->casefold);
1123 LDB_FREE(dn->casefold);
1128 if (dn->linearized) {
1130 s = ldb_dn_get_linearized(child);
1135 t = talloc_asprintf(dn, "%s,%s", s, dn->linearized);
1140 LDB_FREE(dn->linearized);
1147 /* modify the given dn by adding children elements.
1149 * return true if successful and false if not
1150 * if false is returned the dn may be marked invalid
1152 bool ldb_dn_add_child_fmt(struct ldb_dn *dn, const char *child_fmt, ...)
1154 struct ldb_dn *child;
1159 if ( !dn || dn->invalid) {
1163 va_start(ap, child_fmt);
1164 child_str = talloc_vasprintf(dn, child_fmt, ap);
1167 if (child_str == NULL) {
1171 child = ldb_dn_new(child_str, dn->ldb, child_str);
1173 ret = ldb_dn_add_child(dn, child);
1175 talloc_free(child_str);
1180 bool ldb_dn_remove_base_components(struct ldb_dn *dn, unsigned int num)
1184 if ( ! ldb_dn_validate(dn)) {
1188 if (dn->comp_num < num) {
1192 /* free components */
1193 for (i = num; i > 0; i--) {
1194 LDB_FREE(dn->components[dn->comp_num - i].name);
1195 LDB_FREE(dn->components[dn->comp_num - i].value.data);
1196 LDB_FREE(dn->components[dn->comp_num - i].cf_name);
1197 LDB_FREE(dn->components[dn->comp_num - i].cf_value.data);
1200 dn->comp_num -= num;
1202 if (dn->valid_case) {
1203 for (i = 0; i < dn->comp_num; i++) {
1204 LDB_FREE(dn->components[i].cf_name);
1205 LDB_FREE(dn->components[i].cf_value.data);
1207 dn->valid_case = false;
1210 LDB_FREE(dn->casefold);
1211 LDB_FREE(dn->linearized);
1216 bool ldb_dn_remove_child_components(struct ldb_dn *dn, unsigned int num)
1220 if ( ! ldb_dn_validate(dn)) {
1224 if (dn->comp_num < num) {
1228 for (i = 0, j = num; j < dn->comp_num; i++, j++) {
1230 LDB_FREE(dn->components[i].name);
1231 LDB_FREE(dn->components[i].value.data);
1232 LDB_FREE(dn->components[i].cf_name);
1233 LDB_FREE(dn->components[i].cf_value.data);
1235 dn->components[i] = dn->components[j];
1238 dn->comp_num -= num;
1240 if (dn->valid_case) {
1241 for (i = 0; i < dn->comp_num; i++) {
1242 LDB_FREE(dn->components[i].cf_name);
1243 LDB_FREE(dn->components[i].cf_value.data);
1245 dn->valid_case = false;
1248 LDB_FREE(dn->casefold);
1249 LDB_FREE(dn->linearized);
1254 struct ldb_dn *ldb_dn_get_parent(void *mem_ctx, struct ldb_dn *dn)
1256 struct ldb_dn *new_dn;
1258 new_dn = ldb_dn_copy(mem_ctx, dn);
1263 if ( ! ldb_dn_remove_child_components(new_dn, 1)) {
1264 talloc_free(new_dn);
1271 /* Create a 'canonical name' string from a DN:
1273 ie dc=samba,dc=org -> samba.org/
1274 uid=administrator,ou=users,dc=samba,dc=org = samba.org/users/administrator
1276 There are two formats, the EX format has the last / replaced with a newline (\n).
1279 static char *ldb_dn_canonical(void *mem_ctx, struct ldb_dn *dn, int ex_format) {
1282 char *cracked = NULL;
1284 if ( ! ldb_dn_validate(dn)) {
1288 tmpctx = talloc_new(mem_ctx);
1290 /* Walk backwards down the DN, grabbing 'dc' components at first */
1291 for (i = dn->comp_num - 1 ; i >= 0; i--) {
1292 if (ldb_attr_cmp(dn->components[i].name, "dc") != 0) {
1296 cracked = talloc_asprintf(tmpctx, "%s.%s",
1297 ldb_dn_escape_value(tmpctx, dn->components[i].value),
1300 cracked = ldb_dn_escape_value(tmpctx, dn->components[i].value);
1307 /* Only domain components? Finish here */
1310 cracked = talloc_append_string(tmpctx, cracked, "\n");
1312 cracked = talloc_append_string(tmpctx, cracked, "/");
1314 talloc_steal(mem_ctx, cracked);
1318 /* Now walk backwards appending remaining components */
1319 for (; i > 0; i--) {
1320 cracked = talloc_asprintf_append(cracked, "/%s",
1321 ldb_dn_escape_value(tmpctx, dn->components[i].value));
1327 /* Last one, possibly a newline for the 'ex' format */
1329 cracked = talloc_asprintf_append(cracked, "\n%s",
1330 ldb_dn_escape_value(tmpctx, dn->components[i].value));
1332 cracked = talloc_asprintf_append(cracked, "/%s",
1333 ldb_dn_escape_value(tmpctx, dn->components[i].value));
1336 talloc_steal(mem_ctx, cracked);
1338 talloc_free(tmpctx);
1342 /* Wrapper functions for the above, for the two different string formats */
1343 char *ldb_dn_canonical_string(void *mem_ctx, struct ldb_dn *dn) {
1344 return ldb_dn_canonical(mem_ctx, dn, 0);
1348 char *ldb_dn_canonical_ex_string(void *mem_ctx, struct ldb_dn *dn) {
1349 return ldb_dn_canonical(mem_ctx, dn, 1);
1352 int ldb_dn_get_comp_num(struct ldb_dn *dn)
1354 if ( ! ldb_dn_validate(dn)) {
1357 return dn->comp_num;
1360 const char *ldb_dn_get_component_name(struct ldb_dn *dn, unsigned int num)
1362 if ( ! ldb_dn_validate(dn)) {
1365 if (num >= dn->comp_num) return NULL;
1366 return dn->components[num].name;
1369 const struct ldb_val *ldb_dn_get_component_val(struct ldb_dn *dn, unsigned int num)
1371 if ( ! ldb_dn_validate(dn)) {
1374 if (num >= dn->comp_num) return NULL;
1375 return &dn->components[num].value;
1378 const char *ldb_dn_get_rdn_name(struct ldb_dn *dn)
1380 if ( ! ldb_dn_validate(dn)) {
1383 if (dn->comp_num == 0) return NULL;
1384 return dn->components[0].name;
1387 const struct ldb_val *ldb_dn_get_rdn_val(struct ldb_dn *dn)
1389 if ( ! ldb_dn_validate(dn)) {
1392 if (dn->comp_num == 0) return NULL;
1393 return &dn->components[0].value;
1396 int ldb_dn_set_component(struct ldb_dn *dn, int num, const char *name, const struct ldb_val val)
1401 if ( ! ldb_dn_validate(dn)) {
1402 return LDB_ERR_OTHER;
1405 if (num >= dn->comp_num) {
1406 return LDB_ERR_OTHER;
1409 n = talloc_strdup(dn, name);
1411 return LDB_ERR_OTHER;
1414 v.length = val.length;
1415 v.data = (uint8_t *)talloc_memdup(dn, val.data, v.length+1);
1418 return LDB_ERR_OTHER;
1421 talloc_free(dn->components[num].name);
1422 talloc_free(dn->components[num].value.data);
1423 dn->components[num].name = n;
1424 dn->components[num].value = v;
1426 if (dn->valid_case) {
1428 for (i = 0; i < dn->comp_num; i++) {
1429 LDB_FREE(dn->components[i].cf_name);
1430 LDB_FREE(dn->components[i].cf_value.data);
1432 dn->valid_case = false;
1434 LDB_FREE(dn->casefold);
1435 LDB_FREE(dn->linearized);
1440 bool ldb_dn_is_valid(struct ldb_dn *dn)
1442 if ( ! dn) return false;
1443 return ! dn->invalid;
1446 bool ldb_dn_is_special(struct ldb_dn *dn)
1448 if ( ! dn || dn->invalid) return false;
1452 bool ldb_dn_check_special(struct ldb_dn *dn, const char *check)
1454 if ( ! dn || dn->invalid) return false;
1455 return ! strcmp(dn->linearized, check);
1458 bool ldb_dn_is_null(struct ldb_dn *dn)
1460 if ( ! dn || dn->invalid) return false;
1461 if (dn->linearized && (dn->linearized[0] == '\0')) return true;