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);
279 if ( ! isalpha(*p)) {
280 /* not a digit nor an alpha, invalid attribute name */
291 /* valid only if we are at the end */
296 if (trim && (*p != '=')) {
297 /* spaces/tabs are not allowed in attribute names */
303 /* attribute terminated */
310 dn->components[dn->comp_num].name = talloc_strdup(dn->components, dt);
311 if ( ! dn->components[dn->comp_num].name) {
322 if (is_oid && ( ! (isdigit(*p) || (*p == '.')))) {
323 /* not a digit nor a dot, invalid attribute oid */
327 if ( ! (isalpha(*p) || isdigit(*p) || (*p == '-'))) {
328 /* not ALPHA, DIGIT or HYPHEN */
369 /* TODO: support ber encoded values
380 /* ok found value terminator */
394 dn->components[dn->comp_num].value.data = (uint8_t *)talloc_strdup(dn->components, dt);
395 dn->components[dn->comp_num].value.length = l;
396 if ( ! dn->components[dn->comp_num].value.data) {
404 if (dn->comp_num > 2) {
405 dn->components = talloc_realloc(dn,
407 struct ldb_dn_component,
409 if ( ! dn->components) {
413 /* make sure all components are zeroed, other functions depend on this */
414 memset(&dn->components[dn->comp_num], '\0', sizeof(struct ldb_dn_component));
427 /* a string with not escaped specials is invalid (tested) */
456 if (sscanf(p, "%02x", &x) != 1) {
457 /* invalid escaping sequence */
464 *d++ = (unsigned char)x;
486 if (in_attr || in_quote) {
492 /* save last element */
500 dn->components[dn->comp_num].value.data = (uint8_t *)talloc_strdup(dn->components, dt);
501 dn->components[dn->comp_num].value.length = l;
503 if ( ! dn->components[dn->comp_num].value.data) {
515 talloc_free(dn->components);
519 bool ldb_dn_validate(struct ldb_dn *dn)
521 return ldb_dn_explode(dn);
524 const char *ldb_dn_get_linearized(struct ldb_dn *dn)
529 if ( ! dn || ( dn->invalid)) return NULL;
531 if (dn->linearized) return dn->linearized;
533 if ( ! dn->components) {
538 if (dn->comp_num == 0) {
539 dn->linearized = talloc_strdup(dn, "");
540 if ( ! dn->linearized) return NULL;
541 return dn->linearized;
544 /* calculate maximum possible length of DN */
545 for (len = 0, i = 0; i < dn->comp_num; i++) {
546 len += strlen(dn->components[i].name); /* name len */
547 len += (dn->components[i].value.length * 3); /* max escaped data len */
548 len += 2; /* '=' and ',' */
550 dn->linearized = talloc_array(dn, char, len);
551 if ( ! dn->linearized) return NULL;
555 for (i = 0; i < dn->comp_num; i++) {
558 n = dn->components[i].name;
559 while (*n) *d++ = *n++;
564 d += ldb_dn_escape_internal( d,
565 (char *)dn->components[i].value.data,
566 dn->components[i].value.length);
572 /* don't waste more memory than necessary */
573 dn->linearized = talloc_realloc(dn, dn->linearized, char, (d - dn->linearized + 1));
575 return dn->linearized;
578 char *ldb_dn_alloc_linearized(void *mem_ctx, struct ldb_dn *dn)
580 return talloc_strdup(mem_ctx, ldb_dn_get_linearized(dn));
584 casefold a dn. We need to casefold the attribute names, and canonicalize
585 attribute values of case insensitive attributes.
588 static bool ldb_dn_casefold_internal(struct ldb_dn *dn)
592 if ( ! dn || dn->invalid) return false;
594 if (dn->valid_case) return true;
596 if (( ! dn->components) && ( ! ldb_dn_explode(dn))) {
600 for (i = 0; i < dn->comp_num; i++) {
601 const struct ldb_attrib_handler *h;
603 dn->components[i].cf_name = ldb_attr_casefold(dn->components, dn->components[i].name);
604 if (!dn->components[i].cf_name) {
608 h = ldb_attrib_handler(dn->ldb, dn->components[i].cf_name);
609 ret = h->canonicalise_fn(dn->ldb, dn->components,
610 &(dn->components[i].value),
611 &(dn->components[i].cf_value));
617 dn->valid_case = true;
622 for (i = 0; i < dn->comp_num; i++) {
623 LDB_FREE(dn->components[i].cf_name);
624 LDB_FREE(dn->components[i].cf_value.data);
629 const char *ldb_dn_get_casefold(struct ldb_dn *dn)
634 if (dn->casefold) return dn->casefold;
637 dn->casefold = talloc_strdup(dn, dn->linearized);
638 if (!dn->casefold) return NULL;
639 dn->valid_case = true;
643 if ( ! ldb_dn_casefold_internal(dn)) {
647 if (dn->comp_num == 0) {
648 if (dn->linearized && dn->linearized[0] == '\0') {
649 /* hmm a NULL dn, should we faild casefolding ? */
650 dn->casefold = talloc_strdup(dn, "");
653 /* A DN must be NULL, special, or have components */
658 /* calculate maximum possible length of DN */
659 for (len = 0, i = 0; i < dn->comp_num; i++) {
660 len += strlen(dn->components[i].cf_name); /* name len */
661 len += (dn->components[i].cf_value.length * 3); /* max escaped data len */
662 len += 2; /* '=' and ',' */
664 dn->casefold = talloc_array(dn, char, len);
665 if ( ! dn->casefold) return NULL;
669 for (i = 0; i < dn->comp_num; i++) {
672 n = dn->components[i].cf_name;
673 while (*n) *d++ = *n++;
678 d += ldb_dn_escape_internal( d,
679 (char *)dn->components[i].cf_value.data,
680 dn->components[i].cf_value.length);
685 /* don't waste more memory than necessary */
686 dn->casefold = talloc_realloc(dn, dn->casefold, char, strlen(dn->casefold) + 1);
691 char *ldb_dn_alloc_casefold(void *mem_ctx, struct ldb_dn *dn)
693 return talloc_strdup(mem_ctx, ldb_dn_get_casefold(dn));
696 /* Determine if dn is below base, in the ldap tree. Used for
697 * evaluating a subtree search.
698 * 0 if they match, otherwise non-zero
701 int ldb_dn_compare_base(struct ldb_dn *base, struct ldb_dn *dn)
706 if ( ! base || base->invalid) return 1;
707 if ( ! dn || dn->invalid) return -1;
709 if (( ! base->valid_case) || ( ! dn->valid_case)) {
710 if (base->linearized && dn->linearized) {
711 /* try with a normal compare first, if we are lucky
712 * we will avoid exploding and casfolding */
714 dif = strlen(dn->linearized) - strlen(base->linearized);
715 if (dif < 0) return dif;
716 if (strcmp(base->linearized, &dn->linearized[dif]) == 0) return 0;
719 if ( ! ldb_dn_casefold_internal(base)) {
723 if ( ! ldb_dn_casefold_internal(dn)) {
729 /* if base has more components,
730 * they don't have the same base */
731 if (base->comp_num > dn->comp_num) {
732 return (dn->comp_num - base->comp_num);
735 if (dn->comp_num == 0) {
736 if (dn->special && base->special) {
737 return strcmp(base->linearized, dn->linearized);
738 } else if (dn->special) {
740 } else if (base->special) {
747 n_base = base->comp_num - 1;
748 n_dn = dn->comp_num - 1;
750 while (n_base >= 0) {
751 /* compare attr names */
752 ret = strcmp(base->components[n_base].cf_name, dn->components[n_dn].cf_name);
753 if (ret != 0) return ret;
755 /* compare attr.cf_value. */
756 if (base->components[n_base].cf_value.length != dn->components[n_dn].cf_value.length) {
757 return base->components[n_base].cf_value.length - dn->components[n_dn].cf_value.length;
759 ret = strcmp((char *)base->components[n_base].cf_value.data, (char *)dn->components[n_dn].cf_value.data);
760 if (ret != 0) return ret;
769 /* compare DNs using casefolding compare functions.
771 If they match, then return 0
774 int ldb_dn_compare(struct ldb_dn *dn0, struct ldb_dn *dn1)
778 if (( ! dn0) || dn0->invalid || ! dn1 || dn1->invalid) return -1;
780 if (( ! dn0->valid_case) || ( ! dn1->valid_case)) {
781 if (dn0->linearized && dn1->linearized) {
782 /* try with a normal compare first, if we are lucky
783 * we will avoid exploding and casfolding */
784 if (strcmp(dn0->linearized, dn1->linearized) == 0) return 0;
787 if ( ! ldb_dn_casefold_internal(dn0)) {
791 if ( ! ldb_dn_casefold_internal(dn1)) {
797 if (dn0->comp_num != dn1->comp_num) {
798 return (dn1->comp_num - dn0->comp_num);
801 if (dn0->comp_num == 0) {
802 if (dn0->special && dn1->special) {
803 return strcmp(dn0->linearized, dn1->linearized);
804 } else if (dn0->special) {
806 } else if (dn1->special) {
813 for (i = 0; i < dn0->comp_num; i++) {
814 /* compare attr names */
815 ret = strcmp(dn0->components[i].cf_name, dn1->components[i].cf_name);
816 if (ret != 0) return ret;
818 /* compare attr.cf_value. */
819 if (dn0->components[i].cf_value.length != dn1->components[i].cf_value.length) {
820 return dn0->components[i].cf_value.length - dn1->components[i].cf_value.length;
822 ret = strcmp((char *)dn0->components[i].cf_value.data, (char *)dn1->components[i].cf_value.data);
823 if (ret != 0) return ret;
829 static struct ldb_dn_component ldb_dn_copy_component(void *mem_ctx, struct ldb_dn_component *src)
831 struct ldb_dn_component dst;
833 memset(&dst, 0, sizeof(dst));
839 dst.value = ldb_val_dup(mem_ctx, &(src->value));
840 if (dst.value.data == NULL) {
844 dst.name = talloc_strdup(mem_ctx, src->name);
845 if (dst.name == NULL) {
846 LDB_FREE(dst.value.data);
850 if (src->cf_value.data) {
851 dst.cf_value = ldb_val_dup(mem_ctx, &(src->cf_value));
852 if (dst.cf_value.data == NULL) {
853 LDB_FREE(dst.value.data);
858 dst.cf_name = talloc_strdup(mem_ctx, src->cf_name);
859 if (dst.cf_name == NULL) {
860 LDB_FREE(dst.cf_name);
861 LDB_FREE(dst.value.data);
866 dst.cf_value.data = NULL;
873 struct ldb_dn *ldb_dn_copy(void *mem_ctx, struct ldb_dn *dn)
875 struct ldb_dn *new_dn;
877 if (!dn || dn->invalid) {
881 new_dn = talloc_zero(mem_ctx, struct ldb_dn);
888 if (dn->components) {
891 new_dn->components = talloc_zero_array(new_dn, struct ldb_dn_component, dn->comp_num);
892 if ( ! new_dn->components) {
897 for (i = 0; i < dn->comp_num; i++) {
898 new_dn->components[i] = ldb_dn_copy_component(new_dn->components, &dn->components[i]);
899 if ( ! new_dn->components[i].value.data) {
907 new_dn->casefold = talloc_strdup(new_dn, dn->casefold);
908 if ( ! new_dn->casefold) {
914 if (dn->linearized) {
915 new_dn->linearized = talloc_strdup(new_dn, dn->linearized);
916 if ( ! new_dn->linearized) {
925 /* modify the given dn by adding a base.
927 * return true if successful and false if not
928 * if false is returned the dn may be marked invalid
930 bool ldb_dn_add_base(struct ldb_dn *dn, struct ldb_dn *base)
935 if ( !base || base->invalid || !dn || dn->invalid) {
939 if (dn->components) {
942 if ( ! ldb_dn_validate(base)) {
947 if (dn->valid_case) {
948 if ( ! (s = ldb_dn_get_casefold(base))) {
953 dn->components = talloc_realloc(dn,
955 struct ldb_dn_component,
956 dn->comp_num + base->comp_num);
957 if ( ! dn->components) {
962 for (i = 0; i < base->comp_num; dn->comp_num++, i++) {
963 dn->components[dn->comp_num] = ldb_dn_copy_component(dn->components, &base->components[i]);
964 if (dn->components[dn->comp_num].value.data == NULL) {
970 if (dn->casefold && s) {
971 t = talloc_asprintf(dn, "%s,%s", dn->casefold, s);
972 LDB_FREE(dn->casefold);
977 if (dn->linearized) {
979 s = ldb_dn_get_linearized(base);
984 t = talloc_asprintf(dn, "%s,%s", dn->linearized, s);
989 LDB_FREE(dn->linearized);
996 /* modify the given dn by adding a base.
998 * return true if successful and false if not
999 * if false is returned the dn may be marked invalid
1001 bool ldb_dn_add_base_fmt(struct ldb_dn *dn, const char *base_fmt, ...)
1003 struct ldb_dn *base;
1008 if ( !dn || dn->invalid) {
1012 va_start(ap, base_fmt);
1013 base_str = talloc_vasprintf(dn, base_fmt, ap);
1016 if (base_str == NULL) {
1020 base = ldb_dn_new(base_str, dn->ldb, base_str);
1022 ret = ldb_dn_add_base(dn, base);
1024 talloc_free(base_str);
1029 /* modify the given dn by adding children elements.
1031 * return true if successful and false if not
1032 * if false is returned the dn may be marked invalid
1034 bool ldb_dn_add_child(struct ldb_dn *dn, struct ldb_dn *child)
1039 if ( !child || child->invalid || !dn || dn->invalid) {
1043 if (dn->components) {
1046 if ( ! ldb_dn_validate(child)) {
1051 if (dn->valid_case) {
1052 if ( ! (s = ldb_dn_get_casefold(child))) {
1057 n = dn->comp_num + child->comp_num;
1059 dn->components = talloc_realloc(dn,
1061 struct ldb_dn_component,
1063 if ( ! dn->components) {
1068 for (i = dn->comp_num - 1, j = n - 1; i >= 0; i--, j--) {
1069 dn->components[j] = dn->components[i];
1072 for (i = 0; i < child->comp_num; i++) {
1073 dn->components[i] = ldb_dn_copy_component(dn->components, &child->components[i]);
1074 if (dn->components[i].value.data == NULL) {
1082 if (dn->casefold && s) {
1083 t = talloc_asprintf(dn, "%s,%s", s, dn->casefold);
1084 LDB_FREE(dn->casefold);
1089 if (dn->linearized) {
1091 s = ldb_dn_get_linearized(child);
1096 t = talloc_asprintf(dn, "%s,%s", s, dn->linearized);
1101 LDB_FREE(dn->linearized);
1108 /* modify the given dn by adding children elements.
1110 * return true if successful and false if not
1111 * if false is returned the dn may be marked invalid
1113 bool ldb_dn_add_child_fmt(struct ldb_dn *dn, const char *child_fmt, ...)
1115 struct ldb_dn *child;
1120 if ( !dn || dn->invalid) {
1124 va_start(ap, child_fmt);
1125 child_str = talloc_vasprintf(dn, child_fmt, ap);
1128 if (child_str == NULL) {
1132 child = ldb_dn_new(child_str, dn->ldb, child_str);
1134 ret = ldb_dn_add_child(dn, child);
1136 talloc_free(child_str);
1141 bool ldb_dn_remove_base_components(struct ldb_dn *dn, unsigned int num)
1145 if ( ! ldb_dn_validate(dn)) {
1149 if (dn->comp_num < num) {
1153 /* free components */
1154 for (i = num; i > 0; i--) {
1155 LDB_FREE(dn->components[dn->comp_num - i].name);
1156 LDB_FREE(dn->components[dn->comp_num - i].value.data);
1157 LDB_FREE(dn->components[dn->comp_num - i].cf_name);
1158 LDB_FREE(dn->components[dn->comp_num - i].cf_value.data);
1161 dn->comp_num -= num;
1163 if (dn->valid_case) {
1164 for (i = 0; i < dn->comp_num; i++) {
1165 LDB_FREE(dn->components[i].cf_name);
1166 LDB_FREE(dn->components[i].cf_value.data);
1168 dn->valid_case = false;
1171 LDB_FREE(dn->casefold);
1172 LDB_FREE(dn->linearized);
1177 bool ldb_dn_remove_child_components(struct ldb_dn *dn, unsigned int num)
1181 if ( ! ldb_dn_validate(dn)) {
1185 if (dn->comp_num < num) {
1189 for (i = 0, j = num; j < dn->comp_num; i++, j++) {
1191 LDB_FREE(dn->components[i].name);
1192 LDB_FREE(dn->components[i].value.data);
1193 LDB_FREE(dn->components[i].cf_name);
1194 LDB_FREE(dn->components[i].cf_value.data);
1196 dn->components[i] = dn->components[j];
1199 dn->comp_num -= num;
1201 if (dn->valid_case) {
1202 for (i = 0; i < dn->comp_num; i++) {
1203 LDB_FREE(dn->components[i].cf_name);
1204 LDB_FREE(dn->components[i].cf_value.data);
1206 dn->valid_case = false;
1209 LDB_FREE(dn->casefold);
1210 LDB_FREE(dn->linearized);
1215 struct ldb_dn *ldb_dn_get_parent(void *mem_ctx, struct ldb_dn *dn)
1217 struct ldb_dn *new_dn;
1219 new_dn = ldb_dn_copy(mem_ctx, dn);
1224 if ( ! ldb_dn_remove_child_components(new_dn, 1)) {
1225 talloc_free(new_dn);
1232 /* Create a 'canonical name' string from a DN:
1234 ie dc=samba,dc=org -> samba.org/
1235 uid=administrator,ou=users,dc=samba,dc=org = samba.org/users/administrator
1237 There are two formats, the EX format has the last / replaced with a newline (\n).
1240 static char *ldb_dn_canonical(void *mem_ctx, struct ldb_dn *dn, int ex_format) {
1243 char *cracked = NULL;
1245 if ( ! ldb_dn_validate(dn)) {
1249 tmpctx = talloc_new(mem_ctx);
1251 /* Walk backwards down the DN, grabbing 'dc' components at first */
1252 for (i = dn->comp_num - 1 ; i >= 0; i--) {
1253 if (ldb_attr_cmp(dn->components[i].name, "dc") != 0) {
1257 cracked = talloc_asprintf(tmpctx, "%s.%s",
1258 ldb_dn_escape_value(tmpctx, dn->components[i].value),
1261 cracked = ldb_dn_escape_value(tmpctx, dn->components[i].value);
1268 /* Only domain components? Finish here */
1271 cracked = talloc_append_string(tmpctx, cracked, "\n");
1273 cracked = talloc_append_string(tmpctx, cracked, "/");
1275 talloc_steal(mem_ctx, cracked);
1279 /* Now walk backwards appending remaining components */
1280 for (; i > 0; i--) {
1281 cracked = talloc_asprintf_append(cracked, "/%s",
1282 ldb_dn_escape_value(tmpctx, dn->components[i].value));
1288 /* Last one, possibly a newline for the 'ex' format */
1290 cracked = talloc_asprintf_append(cracked, "\n%s",
1291 ldb_dn_escape_value(tmpctx, dn->components[i].value));
1293 cracked = talloc_asprintf_append(cracked, "/%s",
1294 ldb_dn_escape_value(tmpctx, dn->components[i].value));
1297 talloc_steal(mem_ctx, cracked);
1299 talloc_free(tmpctx);
1303 /* Wrapper functions for the above, for the two different string formats */
1304 char *ldb_dn_canonical_string(void *mem_ctx, struct ldb_dn *dn) {
1305 return ldb_dn_canonical(mem_ctx, dn, 0);
1309 char *ldb_dn_canonical_ex_string(void *mem_ctx, struct ldb_dn *dn) {
1310 return ldb_dn_canonical(mem_ctx, dn, 1);
1313 int ldb_dn_get_comp_num(struct ldb_dn *dn)
1315 if ( ! ldb_dn_validate(dn)) {
1318 return dn->comp_num;
1321 const char *ldb_dn_get_component_name(struct ldb_dn *dn, unsigned int num)
1323 if ( ! ldb_dn_validate(dn)) {
1326 if (num >= dn->comp_num) return NULL;
1327 return dn->components[num].name;
1330 const struct ldb_val *ldb_dn_get_component_val(struct ldb_dn *dn, unsigned int num)
1332 if ( ! ldb_dn_validate(dn)) {
1335 if (num >= dn->comp_num) return NULL;
1336 return &dn->components[num].value;
1339 const char *ldb_dn_get_rdn_name(struct ldb_dn *dn)
1341 if ( ! ldb_dn_validate(dn)) {
1344 if (dn->comp_num == 0) return NULL;
1345 return dn->components[0].name;
1348 const struct ldb_val *ldb_dn_get_rdn_val(struct ldb_dn *dn)
1350 if ( ! ldb_dn_validate(dn)) {
1353 if (dn->comp_num == 0) return NULL;
1354 return &dn->components[0].value;
1357 int ldb_dn_set_component(struct ldb_dn *dn, int num, const char *name, const struct ldb_val val)
1362 if ( ! ldb_dn_validate(dn)) {
1363 return LDB_ERR_OTHER;
1366 if (num >= dn->comp_num) {
1367 return LDB_ERR_OTHER;
1370 n = talloc_strdup(dn, name);
1372 return LDB_ERR_OTHER;
1375 v.length = val.length;
1376 v.data = (uint8_t *)talloc_memdup(dn, val.data, v.length+1);
1379 return LDB_ERR_OTHER;
1382 talloc_free(dn->components[num].name);
1383 talloc_free(dn->components[num].value.data);
1384 dn->components[num].name = n;
1385 dn->components[num].value = v;
1387 if (dn->valid_case) {
1389 for (i = 0; i < dn->comp_num; i++) {
1390 LDB_FREE(dn->components[i].cf_name);
1391 LDB_FREE(dn->components[i].cf_value.data);
1393 dn->valid_case = false;
1395 LDB_FREE(dn->casefold);
1400 bool ldb_dn_is_valid(struct ldb_dn *dn)
1402 if ( ! dn) return false;
1403 return ! dn->invalid;
1406 bool ldb_dn_is_special(struct ldb_dn *dn)
1408 if ( ! dn || dn->invalid) return false;
1412 bool ldb_dn_check_special(struct ldb_dn *dn, const char *check)
1414 if ( ! dn || dn->invalid) return false;
1415 return ! strcmp(dn->linearized, check);
1418 bool ldb_dn_is_null(struct ldb_dn *dn)
1420 if ( ! dn || dn->invalid) return false;
1421 if (dn->linearized && (dn->linearized[0] == '\0')) return true;