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 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
39 #include "ldb/include/includes.h"
41 #define LDB_DN_NULL_FAILED(x) if (!(x)) goto failed
43 #define LDB_FREE(x) do { talloc_free(x); x = NULL; } while(0)
46 internal ldb exploded dn structures
48 struct ldb_dn_component {
54 struct ldb_val cf_value;
59 struct ldb_context *ldb;
61 /* Special DNs are always linearized */
70 unsigned int comp_num;
71 struct ldb_dn_component *components;
75 /* strdn may be NULL */
76 struct ldb_dn *ldb_dn_new(void *mem_ctx, struct ldb_context *ldb, const char *strdn)
80 if ( (! mem_ctx) || (! ldb)) return NULL;
82 dn = talloc_zero(mem_ctx, struct ldb_dn);
83 LDB_DN_NULL_FAILED(dn);
88 if (strdn[0] == '@') {
91 if (strncasecmp(strdn, "<GUID=", 6) == 0) {
92 /* this is special DN returned when the
93 * exploded_dn control is used */
95 /* FIXME: add a GUID string to ldb_dn structure */
97 dn->linearized = talloc_strdup(dn, strdn);
99 dn->linearized = talloc_strdup(dn, "");
101 LDB_DN_NULL_FAILED(dn->linearized);
110 struct ldb_dn *ldb_dn_new_fmt(void *mem_ctx, struct ldb_context *ldb, const char *new_fmt, ...)
116 if ( (! mem_ctx) || (! ldb)) return NULL;
118 dn = talloc_zero(mem_ctx, struct ldb_dn);
119 LDB_DN_NULL_FAILED(dn);
123 va_start(ap, new_fmt);
124 strdn = talloc_vasprintf(dn, new_fmt, ap);
126 LDB_DN_NULL_FAILED(strdn);
128 if (strdn[0] == '@') {
131 if (strncasecmp(strdn, "<GUID=", 6) == 0) {
132 /* this is special DN returned when the
133 * exploded_dn control is used */
135 /* FIXME: add a GUID string to ldb_dn structure */
137 dn->linearized = strdn;
146 static int ldb_dn_escape_internal(char *dst, const char *src, int len)
155 while (p - src < len) {
157 p += strcspn(p, ",=\n+<>#;\\\"");
159 if (p - src == len) /* found no escapable chars */
162 memcpy(d, s, p - s); /* copy the part of the string before the stop */
163 d += (p - s); /* move to current position */
165 if (*p) { /* it is a normal escapable character */
168 } else { /* we have a zero byte in the string */
169 strncpy(d, "\00", 3); /* escape the zero */
171 p++; /* skip the zero */
173 s = p; /* move forward */
176 /* copy the last part (with zero) and return */
180 /* return the length of the resulting string */
181 return (l + (d - dst));
184 char *ldb_dn_escape_value(void *mem_ctx, struct ldb_val value)
191 /* allocate destination string, it will be at most 3 times the source */
192 dst = talloc_array(mem_ctx, char, value.length * 3 + 1);
198 ldb_dn_escape_internal(dst, (const char *)value.data, value.length);
200 dst = talloc_realloc(mem_ctx, dst, char, strlen(dst) + 1);
206 explode a DN string into a ldb_dn structure
207 based on RFC4514 except that we don't support multiple valued RDNs
209 static bool ldb_dn_explode(struct ldb_dn *dn)
211 char *p, *data, *d, *dt, *t;
213 bool in_attr = false;
214 bool in_value = false;
215 bool in_quote = false;
221 if ( ! dn || dn->invalid) return false;
223 if (dn->components) {
227 if ( ! dn->linearized) {
232 if (dn->linearized[0] == '\0') {
236 /* Special DNs case */
241 /* make sure we free this if alloced previously before replacing */
242 talloc_free(dn->components);
244 /* in the common case we have 3 or more components */
245 /* make sure all components are zeroed, other functions depend on this */
246 dn->components = talloc_zero_array(dn, struct ldb_dn_component, 3);
247 if ( ! dn->components) {
252 /* Components data space is allocated here once */
253 data = talloc_array(dn->components, char, strlen(dn->linearized) + 1);
276 if ( ! isalpha(*p)) {
277 /* not a digit nor an alpha, invalid attribute name */
288 /* valid only if we are at the end */
293 if (trim && (*p != '=')) {
294 /* spaces/tabs are not allowed in attribute names */
300 /* attribute terminated */
307 dn->components[dn->comp_num].name = talloc_strdup(dn->components, dt);
308 if ( ! dn->components[dn->comp_num].name) {
319 if (is_oid && ( ! (isdigit(*p) || (*p == '.')))) {
320 /* not a digit nor a dot, invalid attribute oid */
324 if ( ! (isalpha(*p) || isdigit(*p) || (*p == '-'))) {
325 /* not ALPHA, DIGIT or HYPHEN */
366 /* TODO: support ber encoded values
377 /* ok found value terminator */
391 dn->components[dn->comp_num].value.data = (uint8_t *)talloc_strdup(dn->components, dt);
392 dn->components[dn->comp_num].value.length = l;
393 if ( ! dn->components[dn->comp_num].value.data) {
401 if (dn->comp_num > 2) {
402 dn->components = talloc_realloc(dn,
404 struct ldb_dn_component,
406 if ( ! dn->components) {
410 /* make sure all components are zeroed, other functions depend on this */
411 memset(&dn->components[dn->comp_num], '\0', sizeof(struct ldb_dn_component));
424 /* a string with not escaped specials is invalid (tested) */
453 if (sscanf(p, "%02x", &x) != 1) {
454 /* invalid escaping sequence */
461 *d++ = (unsigned char)x;
483 if (in_attr || in_quote) {
489 /* save last element */
497 dn->components[dn->comp_num].value.data = (uint8_t *)talloc_strdup(dn->components, dt);
498 dn->components[dn->comp_num].value.length = l;
500 if ( ! dn->components[dn->comp_num].value.data) {
512 talloc_free(dn->components);
516 bool ldb_dn_validate(struct ldb_dn *dn)
518 return ldb_dn_explode(dn);
521 const char *ldb_dn_get_linearized(struct ldb_dn *dn)
526 if ( ! dn || ( dn->invalid)) return NULL;
528 if (dn->linearized) return dn->linearized;
530 if ( ! dn->components) {
535 if (dn->comp_num == 0) {
536 dn->linearized = talloc_strdup(dn, "");
537 if ( ! dn->linearized) return NULL;
538 return dn->linearized;
541 /* calculate maximum possible length of DN */
542 for (len = 0, i = 0; i < dn->comp_num; i++) {
543 len += strlen(dn->components[i].name); /* name len */
544 len += (dn->components[i].value.length * 3); /* max escaped data len */
545 len += 2; /* '=' and ',' */
547 dn->linearized = talloc_array(dn, char, len);
548 if ( ! dn->linearized) return NULL;
552 for (i = 0; i < dn->comp_num; i++) {
555 n = dn->components[i].name;
556 while (*n) *d++ = *n++;
561 d += ldb_dn_escape_internal( d,
562 (char *)dn->components[i].value.data,
563 dn->components[i].value.length);
569 /* don't waste more memory than necessary */
570 dn->linearized = talloc_realloc(dn, dn->linearized, char, (d - dn->linearized + 1));
572 return dn->linearized;
575 char *ldb_dn_alloc_linearized(void *mem_ctx, struct ldb_dn *dn)
577 return talloc_strdup(mem_ctx, ldb_dn_get_linearized(dn));
581 casefold a dn. We need to casefold the attribute names, and canonicalize
582 attribute values of case insensitive attributes.
585 static bool ldb_dn_casefold_internal(struct ldb_dn *dn)
589 if ( ! dn || dn->invalid) return false;
591 if (dn->valid_case) return true;
593 if (( ! dn->components) && ( ! ldb_dn_explode(dn))) {
597 for (i = 0; i < dn->comp_num; i++) {
598 const struct ldb_attrib_handler *h;
600 dn->components[i].cf_name = ldb_attr_casefold(dn->components, dn->components[i].name);
601 if (!dn->components[i].cf_name) {
605 h = ldb_attrib_handler(dn->ldb, dn->components[i].cf_name);
606 ret = h->canonicalise_fn(dn->ldb, dn->components,
607 &(dn->components[i].value),
608 &(dn->components[i].cf_value));
614 dn->valid_case = true;
619 for (i = 0; i < dn->comp_num; i++) {
620 LDB_FREE(dn->components[i].cf_name);
621 LDB_FREE(dn->components[i].cf_value.data);
626 const char *ldb_dn_get_casefold(struct ldb_dn *dn)
631 if ( ! ldb_dn_casefold_internal(dn)) {
635 if (dn->casefold) return dn->casefold;
637 if (dn->comp_num == 0) {
639 len = strlen(dn->linearized);
640 dn->casefold = talloc_array(dn, char, len * 3 + 1);
641 if ( ! dn->casefold) return NULL;
642 ldb_dn_escape_internal(dn->casefold, dn->linearized, len);
643 /* don't waste more memory than necessary */
644 dn->casefold = talloc_realloc(dn, dn->casefold, char, strlen(dn->casefold) + 1);
646 dn->casefold = talloc_strdup(dn, "");
647 if ( ! dn->casefold) return NULL;
649 dn->valid_case = true;
653 /* calculate maximum possible length of DN */
654 for (len = 0, i = 0; i < dn->comp_num; i++) {
655 len += strlen(dn->components[i].cf_name); /* name len */
656 len += (dn->components[i].cf_value.length * 3); /* max escaped data len */
657 len += 2; /* '=' and ',' */
659 dn->casefold = talloc_array(dn, char, len);
660 if ( ! dn->casefold) return NULL;
664 for (i = 0; i < dn->comp_num; i++) {
667 n = dn->components[i].cf_name;
668 while (*n) *d++ = *n++;
673 d += ldb_dn_escape_internal( d,
674 (char *)dn->components[i].cf_value.data,
675 dn->components[i].cf_value.length);
680 /* don't waste more memory than necessary */
681 dn->casefold = talloc_realloc(dn, dn->casefold, char, strlen(dn->casefold) + 1);
686 char *ldb_dn_alloc_casefold(void *mem_ctx, struct ldb_dn *dn)
688 return talloc_strdup(mem_ctx, ldb_dn_get_casefold(dn));
691 /* Determine if dn is below base, in the ldap tree. Used for
692 * evaluating a subtree search.
693 * 0 if they match, otherwise non-zero
696 int ldb_dn_compare_base(struct ldb_dn *base, struct ldb_dn *dn)
701 if ( ! base || base->invalid) return 1;
702 if ( ! dn || dn->invalid) return -1;
704 if (( ! base->valid_case) || ( ! dn->valid_case)) {
705 if (base->linearized && dn->linearized) {
706 /* try with a normal compare first, if we are lucky
707 * we will avoid exploding and casfolding */
709 dif = strlen(dn->linearized) - strlen(base->linearized);
710 if (dif < 0) return dif;
711 if (strcmp(base->linearized, &dn->linearized[dif]) == 0) return 0;
714 if ( ! ldb_dn_casefold_internal(base)) {
718 if ( ! ldb_dn_casefold_internal(dn)) {
724 /* if base has more components,
725 * they don't have the same base */
726 if (base->comp_num > dn->comp_num) {
727 return (dn->comp_num - base->comp_num);
730 if (dn->comp_num == 0) {
731 if (dn->special && base->special) {
732 return strcmp(base->linearized, dn->linearized);
733 } else if (dn->special) {
735 } else if (base->special) {
742 n_base = base->comp_num - 1;
743 n_dn = dn->comp_num - 1;
745 while (n_base >= 0) {
746 /* compare attr names */
747 ret = strcmp(base->components[n_base].cf_name, dn->components[n_dn].cf_name);
748 if (ret != 0) return ret;
750 /* compare attr.cf_value. */
751 if (base->components[n_base].cf_value.length != dn->components[n_dn].cf_value.length) {
752 return base->components[n_base].cf_value.length - dn->components[n_dn].cf_value.length;
754 ret = strcmp((char *)base->components[n_base].cf_value.data, (char *)dn->components[n_dn].cf_value.data);
755 if (ret != 0) return ret;
764 /* compare DNs using casefolding compare functions.
766 If they match, then return 0
769 int ldb_dn_compare(struct ldb_dn *dn0, struct ldb_dn *dn1)
773 if (( ! dn0) || dn0->invalid || ! dn1 || dn1->invalid) return -1;
775 if (( ! dn0->valid_case) || ( ! dn1->valid_case)) {
776 if (dn0->linearized && dn1->linearized) {
777 /* try with a normal compare first, if we are lucky
778 * we will avoid exploding and casfolding */
779 if (strcmp(dn0->linearized, dn1->linearized) == 0) return 0;
782 if ( ! ldb_dn_casefold_internal(dn0)) {
786 if ( ! ldb_dn_casefold_internal(dn1)) {
792 if (dn0->comp_num != dn1->comp_num) {
793 return (dn1->comp_num - dn0->comp_num);
796 if (dn0->comp_num == 0) {
797 if (dn0->special && dn1->special) {
798 return strcmp(dn0->linearized, dn1->linearized);
799 } else if (dn0->special) {
801 } else if (dn1->special) {
808 for (i = 0; i < dn0->comp_num; i++) {
809 /* compare attr names */
810 ret = strcmp(dn0->components[i].cf_name, dn1->components[i].cf_name);
811 if (ret != 0) return ret;
813 /* compare attr.cf_value. */
814 if (dn0->components[i].cf_value.length != dn1->components[i].cf_value.length) {
815 return dn0->components[i].cf_value.length - dn1->components[i].cf_value.length;
817 ret = strcmp((char *)dn0->components[i].cf_value.data, (char *)dn1->components[i].cf_value.data);
818 if (ret != 0) return ret;
824 static struct ldb_dn_component ldb_dn_copy_component(void *mem_ctx, struct ldb_dn_component *src)
826 struct ldb_dn_component dst;
828 memset(&dst, 0, sizeof(dst));
834 dst.value = ldb_val_dup(mem_ctx, &(src->value));
835 if (dst.value.data == NULL) {
839 dst.name = talloc_strdup(mem_ctx, src->name);
840 if (dst.name == NULL) {
841 LDB_FREE(dst.value.data);
845 if (src->cf_value.data) {
846 dst.cf_value = ldb_val_dup(mem_ctx, &(src->cf_value));
847 if (dst.cf_value.data == NULL) {
848 LDB_FREE(dst.value.data);
853 dst.cf_name = talloc_strdup(mem_ctx, src->cf_name);
854 if (dst.cf_name == NULL) {
855 LDB_FREE(dst.cf_name);
856 LDB_FREE(dst.value.data);
861 dst.cf_value.data = NULL;
868 struct ldb_dn *ldb_dn_copy(void *mem_ctx, struct ldb_dn *dn)
870 struct ldb_dn *new_dn;
872 if (!dn || dn->invalid) {
876 new_dn = talloc_zero(mem_ctx, struct ldb_dn);
883 if (dn->components) {
886 new_dn->components = talloc_zero_array(new_dn, struct ldb_dn_component, dn->comp_num);
887 if ( ! new_dn->components) {
892 for (i = 0; i < dn->comp_num; i++) {
893 new_dn->components[i] = ldb_dn_copy_component(new_dn->components, &dn->components[i]);
894 if ( ! new_dn->components[i].value.data) {
902 new_dn->casefold = talloc_strdup(new_dn, dn->casefold);
903 if ( ! new_dn->casefold) {
909 if (dn->linearized) {
910 new_dn->linearized = talloc_strdup(new_dn, dn->linearized);
911 if ( ! new_dn->linearized) {
920 /* modify the given dn by adding a base.
922 * return true if successful and false if not
923 * if false is returned the dn may be marked invalid
925 bool ldb_dn_add_base(struct ldb_dn *dn, struct ldb_dn *base)
930 if ( !base || base->invalid || !dn || dn->invalid) {
934 if (dn->components) {
937 if ( ! ldb_dn_validate(base)) {
942 if (dn->valid_case) {
943 if ( ! (s = ldb_dn_get_casefold(base))) {
948 dn->components = talloc_realloc(dn,
950 struct ldb_dn_component,
951 dn->comp_num + base->comp_num);
952 if ( ! dn->components) {
957 for (i = 0; i < base->comp_num; dn->comp_num++, i++) {
958 dn->components[dn->comp_num] = ldb_dn_copy_component(dn->components, &base->components[i]);
959 if (dn->components[dn->comp_num].value.data == NULL) {
965 if (dn->casefold && s) {
966 t = talloc_asprintf(dn, "%s,%s", dn->casefold, s);
967 LDB_FREE(dn->casefold);
972 if (dn->linearized) {
974 s = ldb_dn_get_linearized(base);
979 t = talloc_asprintf(dn, "%s,%s", dn->linearized, s);
984 LDB_FREE(dn->linearized);
991 /* modify the given dn by adding a base.
993 * return true if successful and false if not
994 * if false is returned the dn may be marked invalid
996 bool ldb_dn_add_base_fmt(struct ldb_dn *dn, const char *base_fmt, ...)
1003 if ( !dn || dn->invalid) {
1007 va_start(ap, base_fmt);
1008 base_str = talloc_vasprintf(dn, base_fmt, ap);
1011 if (base_str == NULL) {
1015 base = ldb_dn_new(base_str, dn->ldb, base_str);
1017 ret = ldb_dn_add_base(dn, base);
1019 talloc_free(base_str);
1024 /* modify the given dn by adding children elements.
1026 * return true if successful and false if not
1027 * if false is returned the dn may be marked invalid
1029 bool ldb_dn_add_child(struct ldb_dn *dn, struct ldb_dn *child)
1034 if ( !child || child->invalid || !dn || dn->invalid) {
1038 if (dn->components) {
1041 if ( ! ldb_dn_validate(child)) {
1046 if (dn->valid_case) {
1047 if ( ! (s = ldb_dn_get_casefold(child))) {
1052 n = dn->comp_num + child->comp_num;
1054 dn->components = talloc_realloc(dn,
1056 struct ldb_dn_component,
1058 if ( ! dn->components) {
1063 for (i = dn->comp_num - 1, j = n - 1; i >= 0; i--, j--) {
1064 dn->components[j] = dn->components[i];
1067 for (i = 0; i < child->comp_num; i++) {
1068 dn->components[i] = ldb_dn_copy_component(dn->components, &child->components[i]);
1069 if (dn->components[i].value.data == NULL) {
1077 if (dn->casefold && s) {
1078 t = talloc_asprintf(dn, "%s,%s", s, dn->casefold);
1079 LDB_FREE(dn->casefold);
1084 if (dn->linearized) {
1086 s = ldb_dn_get_linearized(child);
1091 t = talloc_asprintf(dn, "%s,%s", s, dn->linearized);
1096 LDB_FREE(dn->linearized);
1103 /* modify the given dn by adding children elements.
1105 * return true if successful and false if not
1106 * if false is returned the dn may be marked invalid
1108 bool ldb_dn_add_child_fmt(struct ldb_dn *dn, const char *child_fmt, ...)
1110 struct ldb_dn *child;
1115 if ( !dn || dn->invalid) {
1119 va_start(ap, child_fmt);
1120 child_str = talloc_vasprintf(dn, child_fmt, ap);
1123 if (child_str == NULL) {
1127 child = ldb_dn_new(child_str, dn->ldb, child_str);
1129 ret = ldb_dn_add_child(dn, child);
1131 talloc_free(child_str);
1136 bool ldb_dn_remove_base_components(struct ldb_dn *dn, unsigned int num)
1140 if ( ! ldb_dn_validate(dn)) {
1144 if (dn->comp_num < num) {
1148 /* free components */
1149 for (i = num; i > 0; i--) {
1150 LDB_FREE(dn->components[dn->comp_num - i].name);
1151 LDB_FREE(dn->components[dn->comp_num - i].value.data);
1152 LDB_FREE(dn->components[dn->comp_num - i].cf_name);
1153 LDB_FREE(dn->components[dn->comp_num - i].cf_value.data);
1156 dn->comp_num -= num;
1158 if (dn->valid_case) {
1159 for (i = 0; i < dn->comp_num; i++) {
1160 LDB_FREE(dn->components[i].cf_name);
1161 LDB_FREE(dn->components[i].cf_value.data);
1163 dn->valid_case = false;
1166 LDB_FREE(dn->casefold);
1167 LDB_FREE(dn->linearized);
1172 bool ldb_dn_remove_child_components(struct ldb_dn *dn, unsigned int num)
1176 if ( ! ldb_dn_validate(dn)) {
1180 if (dn->comp_num < num) {
1184 for (i = 0, j = num; j < dn->comp_num; i++, j++) {
1186 LDB_FREE(dn->components[i].name);
1187 LDB_FREE(dn->components[i].value.data);
1188 LDB_FREE(dn->components[i].cf_name);
1189 LDB_FREE(dn->components[i].cf_value.data);
1191 dn->components[i] = dn->components[j];
1194 dn->comp_num -= num;
1196 if (dn->valid_case) {
1197 for (i = 0; i < dn->comp_num; i++) {
1198 LDB_FREE(dn->components[i].cf_name);
1199 LDB_FREE(dn->components[i].cf_value.data);
1201 dn->valid_case = false;
1204 LDB_FREE(dn->casefold);
1205 LDB_FREE(dn->linearized);
1210 struct ldb_dn *ldb_dn_get_parent(void *mem_ctx, struct ldb_dn *dn)
1212 struct ldb_dn *new_dn;
1214 new_dn = ldb_dn_copy(mem_ctx, dn);
1219 if ( ! ldb_dn_remove_child_components(new_dn, 1)) {
1220 talloc_free(new_dn);
1227 /* Create a 'canonical name' string from a DN:
1229 ie dc=samba,dc=org -> samba.org/
1230 uid=administrator,ou=users,dc=samba,dc=org = samba.org/users/administrator
1232 There are two formats, the EX format has the last / replaced with a newline (\n).
1235 static char *ldb_dn_canonical(void *mem_ctx, struct ldb_dn *dn, int ex_format) {
1238 char *cracked = NULL;
1240 if ( ! ldb_dn_validate(dn)) {
1244 tmpctx = talloc_new(mem_ctx);
1246 /* Walk backwards down the DN, grabbing 'dc' components at first */
1247 for (i = dn->comp_num - 1 ; i >= 0; i--) {
1248 if (ldb_attr_cmp(dn->components[i].name, "dc") != 0) {
1252 cracked = talloc_asprintf(tmpctx, "%s.%s",
1253 ldb_dn_escape_value(tmpctx, dn->components[i].value),
1256 cracked = ldb_dn_escape_value(tmpctx, dn->components[i].value);
1263 /* Only domain components? Finish here */
1266 cracked = talloc_asprintf(tmpctx, "%s\n", cracked);
1268 cracked = talloc_asprintf(tmpctx, "%s/", cracked);
1270 talloc_steal(mem_ctx, cracked);
1274 /* Now walk backwards appending remaining components */
1275 for (; i > 0; i--) {
1276 cracked = talloc_asprintf(tmpctx, "%s/%s", cracked,
1277 ldb_dn_escape_value(tmpctx, dn->components[i].value));
1283 /* Last one, possibly a newline for the 'ex' format */
1285 cracked = talloc_asprintf(tmpctx, "%s\n%s", cracked,
1286 ldb_dn_escape_value(tmpctx, dn->components[i].value));
1288 cracked = talloc_asprintf(tmpctx, "%s/%s", cracked,
1289 ldb_dn_escape_value(tmpctx, dn->components[i].value));
1292 talloc_steal(mem_ctx, cracked);
1294 talloc_free(tmpctx);
1298 /* Wrapper functions for the above, for the two different string formats */
1299 char *ldb_dn_canonical_string(void *mem_ctx, struct ldb_dn *dn) {
1300 return ldb_dn_canonical(mem_ctx, dn, 0);
1304 char *ldb_dn_canonical_ex_string(void *mem_ctx, struct ldb_dn *dn) {
1305 return ldb_dn_canonical(mem_ctx, dn, 1);
1308 int ldb_dn_get_comp_num(struct ldb_dn *dn)
1310 if ( ! ldb_dn_validate(dn)) {
1313 return dn->comp_num;
1316 const char *ldb_dn_get_component_name(struct ldb_dn *dn, unsigned int num)
1318 if ( ! ldb_dn_validate(dn)) {
1321 if (num >= dn->comp_num) return NULL;
1322 return dn->components[num].name;
1325 const struct ldb_val *ldb_dn_get_component_val(struct ldb_dn *dn, unsigned int num)
1327 if ( ! ldb_dn_validate(dn)) {
1330 if (num >= dn->comp_num) return NULL;
1331 return &dn->components[num].value;
1334 const char *ldb_dn_get_rdn_name(struct ldb_dn *dn)
1336 if ( ! ldb_dn_validate(dn)) {
1339 if (dn->comp_num == 0) return NULL;
1340 return dn->components[0].name;
1343 const struct ldb_val *ldb_dn_get_rdn_val(struct ldb_dn *dn)
1345 if ( ! ldb_dn_validate(dn)) {
1348 if (dn->comp_num == 0) return NULL;
1349 return &dn->components[0].value;
1352 int ldb_dn_set_component(struct ldb_dn *dn, int num, const char *name, const struct ldb_val val)
1357 if ( ! ldb_dn_validate(dn)) {
1358 return LDB_ERR_OTHER;
1361 if (num >= dn->comp_num) {
1362 return LDB_ERR_OTHER;
1365 n = talloc_strdup(dn, name);
1367 return LDB_ERR_OTHER;
1370 v.length = val.length;
1371 v.data = (uint8_t *)talloc_memdup(dn, val.data, v.length+1);
1374 return LDB_ERR_OTHER;
1377 talloc_free(dn->components[num].name);
1378 talloc_free(dn->components[num].value.data);
1379 dn->components[num].name = n;
1380 dn->components[num].value = v;
1382 if (dn->valid_case) {
1384 for (i = 0; i < dn->comp_num; i++) {
1385 LDB_FREE(dn->components[i].cf_name);
1386 LDB_FREE(dn->components[i].cf_value.data);
1388 dn->valid_case = false;
1390 LDB_FREE(dn->casefold);
1395 bool ldb_dn_is_valid(struct ldb_dn *dn)
1397 if ( ! dn) return false;
1398 return ! dn->invalid;
1401 bool ldb_dn_is_special(struct ldb_dn *dn)
1403 if ( ! dn || dn->invalid) return false;
1407 bool ldb_dn_check_special(struct ldb_dn *dn, const char *check)
1409 if ( ! dn || dn->invalid) return false;
1410 return ! strcmp(dn->linearized, check);
1413 bool ldb_dn_is_null(struct ldb_dn *dn)
1415 if ( ! dn || dn->invalid) return false;
1416 if (dn->linearized && (dn->linearized[0] == '\0')) return true;