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, see <http://www.gnu.org/licenses/>.
27 * Component: ldb dn creation and manipulation utility functions
29 * Description: - explode a dn into it's own basic elements
30 * and put them in a structure (only if necessary)
31 * - manipulate ldb_dn structures
36 #include "ldb_includes.h"
39 #define LDB_DN_NULL_FAILED(x) if (!(x)) goto failed
41 #define LDB_FREE(x) do { talloc_free(x); x = NULL; } while(0)
44 internal ldb exploded dn structures
46 struct ldb_dn_component {
52 struct ldb_val cf_value;
57 struct ldb_context *ldb;
59 /* Special DNs are always linearized */
68 unsigned int comp_num;
69 struct ldb_dn_component *components;
73 /* strdn may be NULL */
74 struct ldb_dn *ldb_dn_from_ldb_val(void *mem_ctx, struct ldb_context *ldb, const struct ldb_val *strdn)
78 if (! ldb) return NULL;
80 dn = talloc_zero(mem_ctx, struct ldb_dn);
81 LDB_DN_NULL_FAILED(dn);
85 if (strdn->data && strdn->length) {
86 if (strdn->data[0] == '@') {
89 if (strdn->length >= 6 && strncasecmp((const char *)strdn->data, "<GUID=", 6) == 0) {
90 /* this is special DN returned when the
91 * exploded_dn control is used */
93 /* FIXME: add a GUID string to ldb_dn structure */
94 } else if (strdn->length >= 5 && strncasecmp((const char *)strdn->data, "<SID=", 5) == 0) {
95 /* this is special DN returned when the
96 * exploded_dn control is used */
98 /* FIXME: add a SID string to ldb_dn structure */
99 } else if (strdn->length >= 8 && strncasecmp((const char *)strdn->data, "<WKGUID=", 8) == 0) {
100 /* this is special DN returned when the
101 * exploded_dn control is used */
103 /* FIXME: add a WKGUID string to ldb_dn structure */
105 dn->linearized = talloc_strndup(dn, (const char *)strdn->data, strdn->length);
107 dn->linearized = talloc_strdup(dn, "");
109 LDB_DN_NULL_FAILED(dn->linearized);
118 /* strdn may be NULL */
119 struct ldb_dn *ldb_dn_new(void *mem_ctx, struct ldb_context *ldb, const char *strdn)
123 blob.length = strdn ? strlen(strdn) : 0;
124 return ldb_dn_from_ldb_val(mem_ctx, ldb, &blob);
127 struct ldb_dn *ldb_dn_new_fmt(void *mem_ctx, struct ldb_context *ldb, const char *new_fmt, ...)
133 if ( (! mem_ctx) || (! ldb)) return NULL;
135 dn = talloc_zero(mem_ctx, struct ldb_dn);
136 LDB_DN_NULL_FAILED(dn);
140 va_start(ap, new_fmt);
141 strdn = talloc_vasprintf(dn, new_fmt, ap);
143 LDB_DN_NULL_FAILED(strdn);
145 if (strdn[0] == '@') {
148 if (strncasecmp(strdn, "<GUID=", 6) == 0) {
149 /* this is special DN returned when the
150 * exploded_dn control is used */
152 /* FIXME: add a GUID string to ldb_dn structure */
153 } else if (strncasecmp(strdn, "<SID=", 5) == 0) {
154 /* this is special DN returned when the
155 * exploded_dn control is used */
157 /* FIXME: add a SID string to ldb_dn structure */
158 } else if (strncasecmp(strdn, "<WKGUID=", 8) == 0) {
159 /* this is special DN returned when the
160 * exploded_dn control is used */
162 /* FIXME: add a WKGUID string to ldb_dn structure */
164 dn->linearized = strdn;
173 static int ldb_dn_escape_internal(char *dst, const char *src, int len)
182 while (p - src < len) {
184 p += strcspn(p, ",=\n+<>#;\\\"");
186 if (p - src == len) /* found no escapable chars */
189 memcpy(d, s, p - s); /* copy the part of the string before the stop */
190 d += (p - s); /* move to current position */
192 if (*p) { /* it is a normal escapable character */
195 } else { /* we have a zero byte in the string */
196 strncpy(d, "\00", 3); /* escape the zero */
198 p++; /* skip the zero */
200 s = p; /* move forward */
203 /* copy the last part (with zero) and return */
207 /* return the length of the resulting string */
208 return (l + (d - dst));
211 char *ldb_dn_escape_value(void *mem_ctx, struct ldb_val value)
218 /* allocate destination string, it will be at most 3 times the source */
219 dst = talloc_array(mem_ctx, char, value.length * 3 + 1);
225 ldb_dn_escape_internal(dst, (const char *)value.data, value.length);
227 dst = talloc_realloc(mem_ctx, dst, char, strlen(dst) + 1);
233 explode a DN string into a ldb_dn structure
234 based on RFC4514 except that we don't support multiple valued RDNs
236 static bool ldb_dn_explode(struct ldb_dn *dn)
238 char *p, *data, *d, *dt, *t;
240 bool in_attr = false;
241 bool in_value = false;
242 bool in_quote = false;
248 if ( ! dn || dn->invalid) return false;
250 if (dn->components) {
254 if ( ! dn->linearized) {
259 if (dn->linearized[0] == '\0') {
263 /* Special DNs case */
268 /* make sure we free this if alloced previously before replacing */
269 talloc_free(dn->components);
271 /* in the common case we have 3 or more components */
272 /* make sure all components are zeroed, other functions depend on this */
273 dn->components = talloc_zero_array(dn, struct ldb_dn_component, 3);
274 if ( ! dn->components) {
279 /* Components data space is allocated here once */
280 data = talloc_array(dn->components, char, strlen(dn->linearized) + 1);
304 /* attr names must be ascii only */
312 if ( ! isalpha(*p)) {
313 /* not a digit nor an alpha, invalid attribute name */
324 /* valid only if we are at the end */
329 if (trim && (*p != '=')) {
330 /* spaces/tabs are not allowed in attribute names */
336 /* attribute terminated */
343 dn->components[dn->comp_num].name = talloc_strdup(dn->components, dt);
344 if ( ! dn->components[dn->comp_num].name) {
356 /* attr names must be ascii only */
361 if (is_oid && ( ! (isdigit(*p) || (*p == '.')))) {
362 /* not a digit nor a dot, invalid attribute oid */
366 if ( ! (isalpha(*p) || isdigit(*p) || (*p == '-'))) {
367 /* not ALPHA, DIGIT or HYPHEN */
408 /* TODO: support ber encoded values
419 /* ok found value terminator */
433 dn->components[dn->comp_num].value.data = (uint8_t *)talloc_strdup(dn->components, dt);
434 dn->components[dn->comp_num].value.length = l;
435 if ( ! dn->components[dn->comp_num].value.data) {
443 if (dn->comp_num > 2) {
444 dn->components = talloc_realloc(dn,
446 struct ldb_dn_component,
448 if ( ! dn->components) {
452 /* make sure all components are zeroed, other functions depend on this */
453 memset(&dn->components[dn->comp_num], '\0', sizeof(struct ldb_dn_component));
466 /* a string with not escaped specials is invalid (tested) */
495 if (sscanf(p, "%02x", &x) != 1) {
496 /* invalid escaping sequence */
503 *d++ = (unsigned char)x;
525 if (in_attr || in_quote) {
531 /* save last element */
539 dn->components[dn->comp_num].value.data = (uint8_t *)talloc_strdup(dn->components, dt);
540 dn->components[dn->comp_num].value.length = l;
542 if ( ! dn->components[dn->comp_num].value.data) {
554 talloc_free(dn->components);
558 bool ldb_dn_validate(struct ldb_dn *dn)
560 return ldb_dn_explode(dn);
563 const char *ldb_dn_get_linearized(struct ldb_dn *dn)
568 if ( ! dn || ( dn->invalid)) return NULL;
570 if (dn->linearized) return dn->linearized;
572 if ( ! dn->components) {
577 if (dn->comp_num == 0) {
578 dn->linearized = talloc_strdup(dn, "");
579 if ( ! dn->linearized) return NULL;
580 return dn->linearized;
583 /* calculate maximum possible length of DN */
584 for (len = 0, i = 0; i < dn->comp_num; i++) {
585 len += strlen(dn->components[i].name); /* name len */
586 len += (dn->components[i].value.length * 3); /* max escaped data len */
587 len += 2; /* '=' and ',' */
589 dn->linearized = talloc_array(dn, char, len);
590 if ( ! dn->linearized) return NULL;
594 for (i = 0; i < dn->comp_num; i++) {
597 n = dn->components[i].name;
598 while (*n) *d++ = *n++;
603 d += ldb_dn_escape_internal( d,
604 (char *)dn->components[i].value.data,
605 dn->components[i].value.length);
611 /* don't waste more memory than necessary */
612 dn->linearized = talloc_realloc(dn, dn->linearized, char, (d - dn->linearized + 1));
614 return dn->linearized;
617 char *ldb_dn_alloc_linearized(void *mem_ctx, struct ldb_dn *dn)
619 return talloc_strdup(mem_ctx, ldb_dn_get_linearized(dn));
623 casefold a dn. We need to casefold the attribute names, and canonicalize
624 attribute values of case insensitive attributes.
627 static bool ldb_dn_casefold_internal(struct ldb_dn *dn)
631 if ( ! dn || dn->invalid) return false;
633 if (dn->valid_case) return true;
635 if (( ! dn->components) && ( ! ldb_dn_explode(dn))) {
639 for (i = 0; i < dn->comp_num; i++) {
640 const struct ldb_schema_attribute *a;
642 dn->components[i].cf_name = ldb_attr_casefold(dn->components, dn->components[i].name);
643 if (!dn->components[i].cf_name) {
647 a = ldb_schema_attribute_by_name(dn->ldb, dn->components[i].cf_name);
648 ret = a->syntax->canonicalise_fn(dn->ldb, dn->components,
649 &(dn->components[i].value),
650 &(dn->components[i].cf_value));
656 dn->valid_case = true;
661 for (i = 0; i < dn->comp_num; i++) {
662 LDB_FREE(dn->components[i].cf_name);
663 LDB_FREE(dn->components[i].cf_value.data);
668 const char *ldb_dn_get_casefold(struct ldb_dn *dn)
673 if (dn->casefold) return dn->casefold;
676 dn->casefold = talloc_strdup(dn, dn->linearized);
677 if (!dn->casefold) return NULL;
678 dn->valid_case = true;
682 if ( ! ldb_dn_casefold_internal(dn)) {
686 if (dn->comp_num == 0) {
687 if (dn->linearized && dn->linearized[0] == '\0') {
688 /* hmm a NULL dn, should we faild casefolding ? */
689 dn->casefold = talloc_strdup(dn, "");
692 /* A DN must be NULL, special, or have components */
697 /* calculate maximum possible length of DN */
698 for (len = 0, i = 0; i < dn->comp_num; i++) {
699 len += strlen(dn->components[i].cf_name); /* name len */
700 len += (dn->components[i].cf_value.length * 3); /* max escaped data len */
701 len += 2; /* '=' and ',' */
703 dn->casefold = talloc_array(dn, char, len);
704 if ( ! dn->casefold) return NULL;
708 for (i = 0; i < dn->comp_num; i++) {
711 n = dn->components[i].cf_name;
712 while (*n) *d++ = *n++;
717 d += ldb_dn_escape_internal( d,
718 (char *)dn->components[i].cf_value.data,
719 dn->components[i].cf_value.length);
724 /* don't waste more memory than necessary */
725 dn->casefold = talloc_realloc(dn, dn->casefold, char, strlen(dn->casefold) + 1);
730 char *ldb_dn_alloc_casefold(void *mem_ctx, struct ldb_dn *dn)
732 return talloc_strdup(mem_ctx, ldb_dn_get_casefold(dn));
735 /* Determine if dn is below base, in the ldap tree. Used for
736 * evaluating a subtree search.
737 * 0 if they match, otherwise non-zero
740 int ldb_dn_compare_base(struct ldb_dn *base, struct ldb_dn *dn)
745 if ( ! base || base->invalid) return 1;
746 if ( ! dn || dn->invalid) return -1;
748 if (( ! base->valid_case) || ( ! dn->valid_case)) {
749 if (base->linearized && dn->linearized) {
750 /* try with a normal compare first, if we are lucky
751 * we will avoid exploding and casfolding */
753 dif = strlen(dn->linearized) - strlen(base->linearized);
754 if (dif < 0) return dif;
755 if (strcmp(base->linearized, &dn->linearized[dif]) == 0) return 0;
758 if ( ! ldb_dn_casefold_internal(base)) {
762 if ( ! ldb_dn_casefold_internal(dn)) {
768 /* if base has more components,
769 * they don't have the same base */
770 if (base->comp_num > dn->comp_num) {
771 return (dn->comp_num - base->comp_num);
774 if (dn->comp_num == 0) {
775 if (dn->special && base->special) {
776 return strcmp(base->linearized, dn->linearized);
777 } else if (dn->special) {
779 } else if (base->special) {
786 n_base = base->comp_num - 1;
787 n_dn = dn->comp_num - 1;
789 while (n_base >= 0) {
790 /* compare attr names */
791 ret = strcmp(base->components[n_base].cf_name, dn->components[n_dn].cf_name);
792 if (ret != 0) return ret;
794 /* compare attr.cf_value. */
795 if (base->components[n_base].cf_value.length != dn->components[n_dn].cf_value.length) {
796 return base->components[n_base].cf_value.length - dn->components[n_dn].cf_value.length;
798 ret = strcmp((char *)base->components[n_base].cf_value.data, (char *)dn->components[n_dn].cf_value.data);
799 if (ret != 0) return ret;
808 /* compare DNs using casefolding compare functions.
810 If they match, then return 0
813 int ldb_dn_compare(struct ldb_dn *dn0, struct ldb_dn *dn1)
817 if (( ! dn0) || dn0->invalid || ! dn1 || dn1->invalid) return -1;
819 if (( ! dn0->valid_case) || ( ! dn1->valid_case)) {
820 if (dn0->linearized && dn1->linearized) {
821 /* try with a normal compare first, if we are lucky
822 * we will avoid exploding and casfolding */
823 if (strcmp(dn0->linearized, dn1->linearized) == 0) return 0;
826 if ( ! ldb_dn_casefold_internal(dn0)) {
830 if ( ! ldb_dn_casefold_internal(dn1)) {
836 if (dn0->comp_num != dn1->comp_num) {
837 return (dn1->comp_num - dn0->comp_num);
840 if (dn0->comp_num == 0) {
841 if (dn0->special && dn1->special) {
842 return strcmp(dn0->linearized, dn1->linearized);
843 } else if (dn0->special) {
845 } else if (dn1->special) {
852 for (i = 0; i < dn0->comp_num; i++) {
853 /* compare attr names */
854 ret = strcmp(dn0->components[i].cf_name, dn1->components[i].cf_name);
855 if (ret != 0) return ret;
857 /* compare attr.cf_value. */
858 if (dn0->components[i].cf_value.length != dn1->components[i].cf_value.length) {
859 return dn0->components[i].cf_value.length - dn1->components[i].cf_value.length;
861 ret = strcmp((char *)dn0->components[i].cf_value.data, (char *)dn1->components[i].cf_value.data);
862 if (ret != 0) return ret;
868 static struct ldb_dn_component ldb_dn_copy_component(void *mem_ctx, struct ldb_dn_component *src)
870 struct ldb_dn_component dst;
872 memset(&dst, 0, sizeof(dst));
878 dst.value = ldb_val_dup(mem_ctx, &(src->value));
879 if (dst.value.data == NULL) {
883 dst.name = talloc_strdup(mem_ctx, src->name);
884 if (dst.name == NULL) {
885 LDB_FREE(dst.value.data);
889 if (src->cf_value.data) {
890 dst.cf_value = ldb_val_dup(mem_ctx, &(src->cf_value));
891 if (dst.cf_value.data == NULL) {
892 LDB_FREE(dst.value.data);
897 dst.cf_name = talloc_strdup(mem_ctx, src->cf_name);
898 if (dst.cf_name == NULL) {
899 LDB_FREE(dst.cf_name);
900 LDB_FREE(dst.value.data);
905 dst.cf_value.data = NULL;
912 struct ldb_dn *ldb_dn_copy(void *mem_ctx, struct ldb_dn *dn)
914 struct ldb_dn *new_dn;
916 if (!dn || dn->invalid) {
920 new_dn = talloc_zero(mem_ctx, struct ldb_dn);
927 if (dn->components) {
930 new_dn->components = talloc_zero_array(new_dn, struct ldb_dn_component, dn->comp_num);
931 if ( ! new_dn->components) {
936 for (i = 0; i < dn->comp_num; i++) {
937 new_dn->components[i] = ldb_dn_copy_component(new_dn->components, &dn->components[i]);
938 if ( ! new_dn->components[i].value.data) {
946 new_dn->casefold = talloc_strdup(new_dn, dn->casefold);
947 if ( ! new_dn->casefold) {
953 if (dn->linearized) {
954 new_dn->linearized = talloc_strdup(new_dn, dn->linearized);
955 if ( ! new_dn->linearized) {
964 /* modify the given dn by adding a base.
966 * return true if successful and false if not
967 * if false is returned the dn may be marked invalid
969 bool ldb_dn_add_base(struct ldb_dn *dn, struct ldb_dn *base)
974 if ( !base || base->invalid || !dn || dn->invalid) {
978 if (dn->components) {
981 if ( ! ldb_dn_validate(base)) {
986 if (dn->valid_case) {
987 if ( ! (s = ldb_dn_get_casefold(base))) {
992 dn->components = talloc_realloc(dn,
994 struct ldb_dn_component,
995 dn->comp_num + base->comp_num);
996 if ( ! dn->components) {
1001 for (i = 0; i < base->comp_num; dn->comp_num++, i++) {
1002 dn->components[dn->comp_num] = ldb_dn_copy_component(dn->components, &base->components[i]);
1003 if (dn->components[dn->comp_num].value.data == NULL) {
1009 if (dn->casefold && s) {
1010 if (*dn->casefold) {
1011 t = talloc_asprintf(dn, "%s,%s", dn->casefold, s);
1013 t = talloc_strdup(dn, s);
1015 LDB_FREE(dn->casefold);
1020 if (dn->linearized) {
1022 s = ldb_dn_get_linearized(base);
1027 if (*dn->linearized) {
1028 t = talloc_asprintf(dn, "%s,%s", dn->linearized, s);
1030 t = talloc_strdup(dn, s);
1036 LDB_FREE(dn->linearized);
1043 /* modify the given dn by adding a base.
1045 * return true if successful and false if not
1046 * if false is returned the dn may be marked invalid
1048 bool ldb_dn_add_base_fmt(struct ldb_dn *dn, const char *base_fmt, ...)
1050 struct ldb_dn *base;
1055 if ( !dn || dn->invalid) {
1059 va_start(ap, base_fmt);
1060 base_str = talloc_vasprintf(dn, base_fmt, ap);
1063 if (base_str == NULL) {
1067 base = ldb_dn_new(base_str, dn->ldb, base_str);
1069 ret = ldb_dn_add_base(dn, base);
1071 talloc_free(base_str);
1076 /* modify the given dn by adding children elements.
1078 * return true if successful and false if not
1079 * if false is returned the dn may be marked invalid
1081 bool ldb_dn_add_child(struct ldb_dn *dn, struct ldb_dn *child)
1086 if ( !child || child->invalid || !dn || dn->invalid) {
1090 if (dn->components) {
1093 if ( ! ldb_dn_validate(child)) {
1098 if (dn->valid_case) {
1099 if ( ! (s = ldb_dn_get_casefold(child))) {
1104 n = dn->comp_num + child->comp_num;
1106 dn->components = talloc_realloc(dn,
1108 struct ldb_dn_component,
1110 if ( ! dn->components) {
1115 for (i = dn->comp_num - 1, j = n - 1; i >= 0; i--, j--) {
1116 dn->components[j] = dn->components[i];
1119 for (i = 0; i < child->comp_num; i++) {
1120 dn->components[i] = ldb_dn_copy_component(dn->components, &child->components[i]);
1121 if (dn->components[i].value.data == NULL) {
1129 if (dn->casefold && s) {
1130 t = talloc_asprintf(dn, "%s,%s", s, dn->casefold);
1131 LDB_FREE(dn->casefold);
1136 if (dn->linearized) {
1138 s = ldb_dn_get_linearized(child);
1143 t = talloc_asprintf(dn, "%s,%s", s, dn->linearized);
1148 LDB_FREE(dn->linearized);
1155 /* modify the given dn by adding children elements.
1157 * return true if successful and false if not
1158 * if false is returned the dn may be marked invalid
1160 bool ldb_dn_add_child_fmt(struct ldb_dn *dn, const char *child_fmt, ...)
1162 struct ldb_dn *child;
1167 if ( !dn || dn->invalid) {
1171 va_start(ap, child_fmt);
1172 child_str = talloc_vasprintf(dn, child_fmt, ap);
1175 if (child_str == NULL) {
1179 child = ldb_dn_new(child_str, dn->ldb, child_str);
1181 ret = ldb_dn_add_child(dn, child);
1183 talloc_free(child_str);
1188 bool ldb_dn_remove_base_components(struct ldb_dn *dn, unsigned int num)
1192 if ( ! ldb_dn_validate(dn)) {
1196 if (dn->comp_num < num) {
1200 /* free components */
1201 for (i = num; i > 0; i--) {
1202 LDB_FREE(dn->components[dn->comp_num - i].name);
1203 LDB_FREE(dn->components[dn->comp_num - i].value.data);
1204 LDB_FREE(dn->components[dn->comp_num - i].cf_name);
1205 LDB_FREE(dn->components[dn->comp_num - i].cf_value.data);
1208 dn->comp_num -= num;
1210 if (dn->valid_case) {
1211 for (i = 0; i < dn->comp_num; i++) {
1212 LDB_FREE(dn->components[i].cf_name);
1213 LDB_FREE(dn->components[i].cf_value.data);
1215 dn->valid_case = false;
1218 LDB_FREE(dn->casefold);
1219 LDB_FREE(dn->linearized);
1224 bool ldb_dn_remove_child_components(struct ldb_dn *dn, unsigned int num)
1228 if ( ! ldb_dn_validate(dn)) {
1232 if (dn->comp_num < num) {
1236 for (i = 0, j = num; j < dn->comp_num; i++, j++) {
1238 LDB_FREE(dn->components[i].name);
1239 LDB_FREE(dn->components[i].value.data);
1240 LDB_FREE(dn->components[i].cf_name);
1241 LDB_FREE(dn->components[i].cf_value.data);
1243 dn->components[i] = dn->components[j];
1246 dn->comp_num -= num;
1248 if (dn->valid_case) {
1249 for (i = 0; i < dn->comp_num; i++) {
1250 LDB_FREE(dn->components[i].cf_name);
1251 LDB_FREE(dn->components[i].cf_value.data);
1253 dn->valid_case = false;
1256 LDB_FREE(dn->casefold);
1257 LDB_FREE(dn->linearized);
1262 struct ldb_dn *ldb_dn_get_parent(void *mem_ctx, struct ldb_dn *dn)
1264 struct ldb_dn *new_dn;
1266 new_dn = ldb_dn_copy(mem_ctx, dn);
1271 if ( ! ldb_dn_remove_child_components(new_dn, 1)) {
1272 talloc_free(new_dn);
1279 /* Create a 'canonical name' string from a DN:
1281 ie dc=samba,dc=org -> samba.org/
1282 uid=administrator,ou=users,dc=samba,dc=org = samba.org/users/administrator
1284 There are two formats, the EX format has the last / replaced with a newline (\n).
1287 static char *ldb_dn_canonical(void *mem_ctx, struct ldb_dn *dn, int ex_format) {
1290 char *cracked = NULL;
1291 const char *format = (ex_format ? "\n" : "/" );
1293 if ( ! ldb_dn_validate(dn)) {
1297 tmpctx = talloc_new(mem_ctx);
1299 /* Walk backwards down the DN, grabbing 'dc' components at first */
1300 for (i = dn->comp_num - 1 ; i >= 0; i--) {
1301 if (ldb_attr_cmp(dn->components[i].name, "dc") != 0) {
1305 cracked = talloc_asprintf(tmpctx, "%s.%s",
1306 ldb_dn_escape_value(tmpctx, dn->components[i].value),
1309 cracked = ldb_dn_escape_value(tmpctx, dn->components[i].value);
1316 /* Only domain components? Finish here */
1318 cracked = talloc_strdup_append_buffer(cracked, format);
1319 talloc_steal(mem_ctx, cracked);
1323 /* Now walk backwards appending remaining components */
1324 for (; i > 0; i--) {
1325 cracked = talloc_asprintf_append_buffer(cracked, "/%s",
1326 ldb_dn_escape_value(tmpctx, dn->components[i].value));
1332 /* Last one, possibly a newline for the 'ex' format */
1333 cracked = talloc_asprintf_append_buffer(cracked, "%s%s", format,
1334 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;