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_private.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;
55 struct ldb_dn_ext_component {
63 struct ldb_context *ldb;
65 /* Special DNs are always linearized */
75 unsigned int comp_num;
76 struct ldb_dn_component *components;
78 unsigned int ext_comp_num;
79 struct ldb_dn_ext_component *ext_components;
82 /* it is helpful to be able to break on this in gdb */
83 static void ldb_dn_mark_invalid(struct ldb_dn *dn)
88 /* strdn may be NULL */
89 struct ldb_dn *ldb_dn_from_ldb_val(void *mem_ctx,
90 struct ldb_context *ldb,
91 const struct ldb_val *strdn)
95 if (! ldb) return NULL;
97 if (strdn && strdn->data
98 && (strnlen((const char*)strdn->data, strdn->length) != strdn->length)) {
99 /* The RDN must not contain a character with value 0x0 */
103 dn = talloc_zero(mem_ctx, struct ldb_dn);
104 LDB_DN_NULL_FAILED(dn);
106 dn->ldb = talloc_get_type(ldb, struct ldb_context);
107 if (dn->ldb == NULL) {
108 /* the caller probably got the arguments to
109 ldb_dn_new() mixed up */
114 if (strdn->data && strdn->length) {
115 const char *data = (const char *)strdn->data;
116 size_t length = strdn->length;
118 if (data[0] == '@') {
121 dn->ext_linearized = talloc_strndup(dn, data, length);
122 LDB_DN_NULL_FAILED(dn->ext_linearized);
124 if (data[0] == '<') {
125 const char *p_save, *p = dn->ext_linearized;
134 if (p_save == dn->ext_linearized) {
135 dn->linearized = talloc_strdup(dn, "");
137 dn->linearized = talloc_strdup(dn, p_save);
139 LDB_DN_NULL_FAILED(dn->linearized);
141 dn->linearized = dn->ext_linearized;
142 dn->ext_linearized = NULL;
145 dn->linearized = talloc_strdup(dn, "");
146 LDB_DN_NULL_FAILED(dn->linearized);
156 /* strdn may be NULL */
157 struct ldb_dn *ldb_dn_new(void *mem_ctx,
158 struct ldb_context *ldb,
162 blob.data = discard_const_p(uint8_t, strdn);
163 blob.length = strdn ? strlen(strdn) : 0;
164 return ldb_dn_from_ldb_val(mem_ctx, ldb, &blob);
167 struct ldb_dn *ldb_dn_new_fmt(void *mem_ctx,
168 struct ldb_context *ldb,
169 const char *new_fmt, ...)
174 if ( (! mem_ctx) || (! ldb)) return NULL;
176 va_start(ap, new_fmt);
177 strdn = talloc_vasprintf(mem_ctx, new_fmt, ap);
181 struct ldb_dn *dn = ldb_dn_new(mem_ctx, ldb, strdn);
189 /* see RFC2253 section 2.4 */
190 static int ldb_dn_escape_internal(char *dst, const char *src, int len)
199 while (p - src < len) {
200 p += strcspn(p, ",=\n\r+<>#;\\\" ");
202 if (p - src == len) /* found no escapable chars */
205 /* copy the part of the string before the stop */
207 d += (p - s); /* move to current position */
211 if (p == src || (p-src)==(len-1)) {
212 /* if at the beginning or end
213 * of the string then escape */
217 /* otherwise don't escape */
223 /* despite the RFC, windows escapes a #
224 anywhere in the string */
232 /* these must be escaped using \c form */
238 /* any others get \XX form */
240 const char *hexbytes = "0123456789ABCDEF";
241 v = *(const unsigned char *)p;
243 *d++ = hexbytes[v>>4];
244 *d++ = hexbytes[v&0xF];
249 s = p; /* move forward */
252 /* copy the last part (with zero) and return */
256 /* return the length of the resulting string */
257 return (l + (d - dst));
260 char *ldb_dn_escape_value(void *mem_ctx, struct ldb_val value)
267 /* allocate destination string, it will be at most 3 times the source */
268 dst = talloc_array(mem_ctx, char, value.length * 3 + 1);
274 ldb_dn_escape_internal(dst, (const char *)value.data, value.length);
276 dst = talloc_realloc(mem_ctx, dst, char, strlen(dst) + 1);
282 explode a DN string into a ldb_dn structure
283 based on RFC4514 except that we don't support multiple valued RDNs
285 TODO: according to MS-ADTS:3.1.1.5.2 Naming Constraints
286 DN must be compliant with RFC2253
288 static bool ldb_dn_explode(struct ldb_dn *dn)
290 char *p, *ex_name, *ex_value, *data, *d, *dt, *t;
292 bool in_extended = false;
293 bool in_ex_name = false;
294 bool in_ex_value = false;
295 bool in_attr = false;
296 bool in_value = false;
297 bool in_quote = false;
305 if ( ! dn || dn->invalid) return false;
307 if (dn->components) {
311 if (dn->ext_linearized) {
312 parse_dn = dn->ext_linearized;
314 parse_dn = dn->linearized;
321 is_index = (strncmp(parse_dn, "DN=@INDEX:", 10) == 0);
324 if (parse_dn[0] == '\0') {
328 /* Special DNs case */
333 /* make sure we free this if alloced previously before replacing */
334 talloc_free(dn->components);
336 talloc_free(dn->ext_components);
337 dn->ext_components = NULL;
339 /* in the common case we have 3 or more components */
340 /* make sure all components are zeroed, other functions depend on it */
341 dn->components = talloc_zero_array(dn, struct ldb_dn_component, 3);
342 if ( ! dn->components) {
347 /* Components data space is allocated here once */
348 data = talloc_array(dn->components, char, strlen(parse_dn) + 1);
364 if (!in_ex_name && !in_ex_value) {
371 } else if (p[0] == '\0') {
383 if (in_ex_name && *p == '=') {
392 if (in_ex_value && *p == '>') {
393 const struct ldb_dn_extended_syntax *ext_syntax;
394 struct ldb_val ex_val = {
395 .data = (uint8_t *)ex_value,
396 .length = d - ex_value
403 /* Process name and ex_value */
405 dn->ext_components = talloc_realloc(dn,
407 struct ldb_dn_ext_component,
408 dn->ext_comp_num + 1);
409 if ( ! dn->ext_components) {
414 ext_syntax = ldb_dn_extended_syntax_by_name(dn->ldb, ex_name);
416 /* We don't know about this type of extended DN */
420 dn->ext_components[dn->ext_comp_num].name = talloc_strdup(dn->ext_components, ex_name);
421 if (!dn->ext_components[dn->ext_comp_num].name) {
425 ret = ext_syntax->read_fn(dn->ldb, dn->ext_components,
426 &ex_val, &dn->ext_components[dn->ext_comp_num].value);
427 if (ret != LDB_SUCCESS) {
428 ldb_dn_mark_invalid(dn);
435 /* We have reached the end (extended component only)! */
439 } else if (*p == ';') {
443 ldb_dn_mark_invalid(dn);
462 /* attr names must be ascii only */
463 ldb_dn_mark_invalid(dn);
470 if ( ! isalpha(*p)) {
471 /* not a digit nor an alpha,
472 * invalid attribute name */
473 ldb_dn_mark_invalid(dn);
477 /* Copy this character across from parse_dn,
478 * now we have trimmed out spaces */
485 /* valid only if we are at the end */
490 if (trim && (*p != '=')) {
491 /* spaces/tabs are not allowed */
492 ldb_dn_mark_invalid(dn);
497 /* attribute terminated */
503 /* Terminate this string in d
504 * (which is a copy of parse_dn
505 * with spaces trimmed) */
507 dn->components[dn->comp_num].name = talloc_strdup(dn->components, dt);
508 if ( ! dn->components[dn->comp_num].name) {
520 /* attr names must be ascii only */
521 ldb_dn_mark_invalid(dn);
525 if (is_oid && ( ! (isdigit(*p) || (*p == '.')))) {
526 /* not a digit nor a dot,
527 * invalid attribute oid */
528 ldb_dn_mark_invalid(dn);
531 if ( ! (isalpha(*p) || isdigit(*p) || (*p == '-'))) {
532 /* not ALPHA, DIGIT or HYPHEN */
533 ldb_dn_mark_invalid(dn);
573 /* TODO: support ber encoded values
584 /* ok found value terminator */
598 dn->components[dn->comp_num].value.data = (uint8_t *)talloc_strdup(dn->components, dt);
599 dn->components[dn->comp_num].value.length = l;
600 if ( ! dn->components[dn->comp_num].value.data) {
608 if (dn->comp_num > 2) {
609 dn->components = talloc_realloc(dn,
611 struct ldb_dn_component,
613 if ( ! dn->components) {
617 /* make sure all components are zeroed, other functions depend on this */
618 memset(&dn->components[dn->comp_num], '\0', sizeof(struct ldb_dn_component));
625 /* to main compatibility with earlier
626 versions of ldb indexing, we have to
627 accept the base64 encoded binary index
628 values, which contain a '+' or '='
629 which should normally be escaped */
641 /* a string with not escaped specials is invalid (tested) */
643 ldb_dn_mark_invalid(dn);
670 if (isxdigit(p[0]) && isxdigit(p[1])) {
671 if (sscanf(p, "%02x", &x) != 1) {
672 /* invalid escaping sequence */
673 ldb_dn_mark_invalid(dn);
677 *d++ = (unsigned char)x;
703 if (in_attr || in_quote) {
705 ldb_dn_mark_invalid(dn);
709 /* save last element */
717 dn->components[dn->comp_num].value.length = l;
718 dn->components[dn->comp_num].value.data =
719 (uint8_t *)talloc_strdup(dn->components, dt);
720 if ( ! dn->components[dn->comp_num].value.data) {
732 talloc_free(dn->components);
736 bool ldb_dn_validate(struct ldb_dn *dn)
738 return ldb_dn_explode(dn);
741 const char *ldb_dn_get_linearized(struct ldb_dn *dn)
746 if ( ! dn || ( dn->invalid)) return NULL;
748 if (dn->linearized) return dn->linearized;
750 if ( ! dn->components) {
751 ldb_dn_mark_invalid(dn);
755 if (dn->comp_num == 0) {
756 dn->linearized = talloc_strdup(dn, "");
757 if ( ! dn->linearized) return NULL;
758 return dn->linearized;
761 /* calculate maximum possible length of DN */
762 for (len = 0, i = 0; i < dn->comp_num; i++) {
764 len += strlen(dn->components[i].name);
765 /* max escaped data len */
766 len += (dn->components[i].value.length * 3);
767 len += 2; /* '=' and ',' */
769 dn->linearized = talloc_array(dn, char, len);
770 if ( ! dn->linearized) return NULL;
774 for (i = 0; i < dn->comp_num; i++) {
777 n = dn->components[i].name;
778 while (*n) *d++ = *n++;
783 d += ldb_dn_escape_internal( d,
784 (char *)dn->components[i].value.data,
785 dn->components[i].value.length);
791 /* don't waste more memory than necessary */
792 dn->linearized = talloc_realloc(dn, dn->linearized,
793 char, (d - dn->linearized + 1));
795 return dn->linearized;
798 static int ldb_dn_extended_component_compare(const void *p1, const void *p2)
800 const struct ldb_dn_ext_component *ec1 = (const struct ldb_dn_ext_component *)p1;
801 const struct ldb_dn_ext_component *ec2 = (const struct ldb_dn_ext_component *)p2;
802 return strcmp(ec1->name, ec2->name);
805 char *ldb_dn_get_extended_linearized(void *mem_ctx, struct ldb_dn *dn, int mode)
807 const char *linearized = ldb_dn_get_linearized(dn);
815 if (!ldb_dn_has_extended(dn)) {
816 return talloc_strdup(mem_ctx, linearized);
819 if (!ldb_dn_validate(dn)) {
823 /* sort the extended components by name. The idea is to make
824 * the resulting DNs consistent, plus to ensure that we put
825 * 'DELETED' first, so it can be very quickly recognised
827 TYPESAFE_QSORT(dn->ext_components, dn->ext_comp_num,
828 ldb_dn_extended_component_compare);
830 for (i = 0; i < dn->ext_comp_num; i++) {
831 const struct ldb_dn_extended_syntax *ext_syntax;
832 const char *name = dn->ext_components[i].name;
833 struct ldb_val ec_val = dn->ext_components[i].value;
837 ext_syntax = ldb_dn_extended_syntax_by_name(dn->ldb, name);
843 ret = ext_syntax->write_clear_fn(dn->ldb, mem_ctx,
845 } else if (mode == 0) {
846 ret = ext_syntax->write_hex_fn(dn->ldb, mem_ctx,
852 if (ret != LDB_SUCCESS) {
857 p = talloc_asprintf(mem_ctx, "<%s=%s>",
860 p = talloc_asprintf_append_buffer(p, ";<%s=%s>",
864 talloc_free(val.data);
871 if (dn->ext_comp_num && *linearized) {
872 p = talloc_asprintf_append_buffer(p, ";%s", linearized);
883 filter out all but an acceptable list of extended DN components
885 void ldb_dn_extended_filter(struct ldb_dn *dn, const char * const *accept)
888 for (i=0; i<dn->ext_comp_num; i++) {
889 if (!ldb_attr_in_list(accept, dn->ext_components[i].name)) {
890 memmove(&dn->ext_components[i],
891 &dn->ext_components[i+1],
892 (dn->ext_comp_num-(i+1))*sizeof(dn->ext_components[0]));
900 char *ldb_dn_alloc_linearized(void *mem_ctx, struct ldb_dn *dn)
902 return talloc_strdup(mem_ctx, ldb_dn_get_linearized(dn));
906 casefold a dn. We need to casefold the attribute names, and canonicalize
907 attribute values of case insensitive attributes.
910 static bool ldb_dn_casefold_internal(struct ldb_dn *dn)
914 if ( ! dn || dn->invalid) return false;
916 if (dn->valid_case) return true;
918 if (( ! dn->components) && ( ! ldb_dn_explode(dn))) {
922 for (i = 0; i < dn->comp_num; i++) {
923 const struct ldb_schema_attribute *a;
925 dn->components[i].cf_name =
926 ldb_attr_casefold(dn->components,
927 dn->components[i].name);
928 if (!dn->components[i].cf_name) {
932 a = ldb_schema_attribute_by_name(dn->ldb,
933 dn->components[i].cf_name);
935 ret = a->syntax->canonicalise_fn(dn->ldb, dn->components,
936 &(dn->components[i].value),
937 &(dn->components[i].cf_value));
943 dn->valid_case = true;
948 for (i = 0; i < dn->comp_num; i++) {
949 LDB_FREE(dn->components[i].cf_name);
950 LDB_FREE(dn->components[i].cf_value.data);
955 const char *ldb_dn_get_casefold(struct ldb_dn *dn)
960 if (dn->casefold) return dn->casefold;
963 dn->casefold = talloc_strdup(dn, dn->linearized);
964 if (!dn->casefold) return NULL;
965 dn->valid_case = true;
969 if ( ! ldb_dn_casefold_internal(dn)) {
973 if (dn->comp_num == 0) {
974 dn->casefold = talloc_strdup(dn, "");
978 /* calculate maximum possible length of DN */
979 for (len = 0, i = 0; i < dn->comp_num; i++) {
981 len += strlen(dn->components[i].cf_name);
982 /* max escaped data len */
983 len += (dn->components[i].cf_value.length * 3);
984 len += 2; /* '=' and ',' */
986 dn->casefold = talloc_array(dn, char, len);
987 if ( ! dn->casefold) return NULL;
991 for (i = 0; i < dn->comp_num; i++) {
994 n = dn->components[i].cf_name;
995 while (*n) *d++ = *n++;
1000 d += ldb_dn_escape_internal( d,
1001 (char *)dn->components[i].cf_value.data,
1002 dn->components[i].cf_value.length);
1007 /* don't waste more memory than necessary */
1008 dn->casefold = talloc_realloc(dn, dn->casefold,
1009 char, strlen(dn->casefold) + 1);
1011 return dn->casefold;
1014 char *ldb_dn_alloc_casefold(void *mem_ctx, struct ldb_dn *dn)
1016 return talloc_strdup(mem_ctx, ldb_dn_get_casefold(dn));
1019 /* Determine if dn is below base, in the ldap tree. Used for
1020 * evaluating a subtree search.
1021 * 0 if they match, otherwise non-zero
1024 int ldb_dn_compare_base(struct ldb_dn *base, struct ldb_dn *dn)
1029 if ( ! base || base->invalid) return 1;
1030 if ( ! dn || dn->invalid) return -1;
1032 if (( ! base->valid_case) || ( ! dn->valid_case)) {
1033 if (base->linearized && dn->linearized) {
1034 /* try with a normal compare first, if we are lucky
1035 * we will avoid exploding and casfolding */
1037 dif = strlen(dn->linearized) - strlen(base->linearized);
1041 if (strcmp(base->linearized,
1042 &dn->linearized[dif]) == 0) {
1047 if ( ! ldb_dn_casefold_internal(base)) {
1051 if ( ! ldb_dn_casefold_internal(dn)) {
1057 /* if base has more components,
1058 * they don't have the same base */
1059 if (base->comp_num > dn->comp_num) {
1060 return (dn->comp_num - base->comp_num);
1063 if (dn->comp_num == 0) {
1064 if (dn->special && base->special) {
1065 return strcmp(base->linearized, dn->linearized);
1066 } else if (dn->special) {
1068 } else if (base->special) {
1075 n_base = base->comp_num - 1;
1076 n_dn = dn->comp_num - 1;
1078 while (n_base >= 0) {
1079 char *b_name = base->components[n_base].cf_name;
1080 char *dn_name = dn->components[n_dn].cf_name;
1082 char *b_vdata = (char *)base->components[n_base].cf_value.data;
1083 char *dn_vdata = (char *)dn->components[n_dn].cf_value.data;
1085 size_t b_vlen = base->components[n_base].cf_value.length;
1086 size_t dn_vlen = dn->components[n_dn].cf_value.length;
1088 /* compare attr names */
1089 ret = strcmp(b_name, dn_name);
1090 if (ret != 0) return ret;
1092 /* compare attr.cf_value. */
1093 if (b_vlen != dn_vlen) {
1094 return b_vlen - dn_vlen;
1096 ret = strcmp(b_vdata, dn_vdata);
1097 if (ret != 0) return ret;
1106 /* compare DNs using casefolding compare functions.
1108 If they match, then return 0
1111 int ldb_dn_compare(struct ldb_dn *dn0, struct ldb_dn *dn1)
1115 if (( ! dn0) || dn0->invalid || ! dn1 || dn1->invalid) {
1119 if (( ! dn0->valid_case) || ( ! dn1->valid_case)) {
1120 if (dn0->linearized && dn1->linearized) {
1121 /* try with a normal compare first, if we are lucky
1122 * we will avoid exploding and casfolding */
1123 if (strcmp(dn0->linearized, dn1->linearized) == 0) {
1128 if ( ! ldb_dn_casefold_internal(dn0)) {
1132 if ( ! ldb_dn_casefold_internal(dn1)) {
1138 if (dn0->comp_num != dn1->comp_num) {
1139 return (dn1->comp_num - dn0->comp_num);
1142 if (dn0->comp_num == 0) {
1143 if (dn0->special && dn1->special) {
1144 return strcmp(dn0->linearized, dn1->linearized);
1145 } else if (dn0->special) {
1147 } else if (dn1->special) {
1154 for (i = 0; i < dn0->comp_num; i++) {
1155 char *dn0_name = dn0->components[i].cf_name;
1156 char *dn1_name = dn1->components[i].cf_name;
1158 char *dn0_vdata = (char *)dn0->components[i].cf_value.data;
1159 char *dn1_vdata = (char *)dn1->components[i].cf_value.data;
1161 size_t dn0_vlen = dn0->components[i].cf_value.length;
1162 size_t dn1_vlen = dn1->components[i].cf_value.length;
1164 /* compare attr names */
1165 ret = strcmp(dn0_name, dn1_name);
1170 /* compare attr.cf_value. */
1171 if (dn0_vlen != dn1_vlen) {
1172 return dn0_vlen - dn1_vlen;
1174 ret = strcmp(dn0_vdata, dn1_vdata);
1183 static struct ldb_dn_component ldb_dn_copy_component(
1185 struct ldb_dn_component *src)
1187 struct ldb_dn_component dst;
1189 memset(&dst, 0, sizeof(dst));
1195 dst.value = ldb_val_dup(mem_ctx, &(src->value));
1196 if (dst.value.data == NULL) {
1200 dst.name = talloc_strdup(mem_ctx, src->name);
1201 if (dst.name == NULL) {
1202 LDB_FREE(dst.value.data);
1206 if (src->cf_value.data) {
1207 dst.cf_value = ldb_val_dup(mem_ctx, &(src->cf_value));
1208 if (dst.cf_value.data == NULL) {
1209 LDB_FREE(dst.value.data);
1214 dst.cf_name = talloc_strdup(mem_ctx, src->cf_name);
1215 if (dst.cf_name == NULL) {
1216 LDB_FREE(dst.cf_name);
1217 LDB_FREE(dst.value.data);
1222 dst.cf_value.data = NULL;
1229 static struct ldb_dn_ext_component ldb_dn_ext_copy_component(
1231 struct ldb_dn_ext_component *src)
1233 struct ldb_dn_ext_component dst;
1235 memset(&dst, 0, sizeof(dst));
1241 dst.value = ldb_val_dup(mem_ctx, &(src->value));
1242 if (dst.value.data == NULL) {
1246 dst.name = talloc_strdup(mem_ctx, src->name);
1247 if (dst.name == NULL) {
1248 LDB_FREE(dst.value.data);
1255 struct ldb_dn *ldb_dn_copy(void *mem_ctx, struct ldb_dn *dn)
1257 struct ldb_dn *new_dn;
1259 if (!dn || dn->invalid) {
1263 new_dn = talloc_zero(mem_ctx, struct ldb_dn);
1270 if (dn->components) {
1273 new_dn->components =
1274 talloc_zero_array(new_dn,
1275 struct ldb_dn_component,
1277 if ( ! new_dn->components) {
1278 talloc_free(new_dn);
1282 for (i = 0; i < dn->comp_num; i++) {
1283 new_dn->components[i] =
1284 ldb_dn_copy_component(new_dn->components,
1285 &dn->components[i]);
1286 if ( ! new_dn->components[i].value.data) {
1287 talloc_free(new_dn);
1293 if (dn->ext_components) {
1296 new_dn->ext_components =
1297 talloc_zero_array(new_dn,
1298 struct ldb_dn_ext_component,
1300 if ( ! new_dn->ext_components) {
1301 talloc_free(new_dn);
1305 for (i = 0; i < dn->ext_comp_num; i++) {
1306 new_dn->ext_components[i] =
1307 ldb_dn_ext_copy_component(
1308 new_dn->ext_components,
1309 &dn->ext_components[i]);
1310 if ( ! new_dn->ext_components[i].value.data) {
1311 talloc_free(new_dn);
1318 new_dn->casefold = talloc_strdup(new_dn, dn->casefold);
1319 if ( ! new_dn->casefold) {
1320 talloc_free(new_dn);
1325 if (dn->linearized) {
1326 new_dn->linearized = talloc_strdup(new_dn, dn->linearized);
1327 if ( ! new_dn->linearized) {
1328 talloc_free(new_dn);
1333 if (dn->ext_linearized) {
1334 new_dn->ext_linearized = talloc_strdup(new_dn,
1335 dn->ext_linearized);
1336 if ( ! new_dn->ext_linearized) {
1337 talloc_free(new_dn);
1345 /* modify the given dn by adding a base.
1347 * return true if successful and false if not
1348 * if false is returned the dn may be marked invalid
1350 bool ldb_dn_add_base(struct ldb_dn *dn, struct ldb_dn *base)
1355 if ( !base || base->invalid || !dn || dn->invalid) {
1359 if (dn->components) {
1362 if ( ! ldb_dn_validate(base)) {
1367 if (dn->valid_case) {
1368 if ( ! (s = ldb_dn_get_casefold(base))) {
1373 dn->components = talloc_realloc(dn,
1375 struct ldb_dn_component,
1376 dn->comp_num + base->comp_num);
1377 if ( ! dn->components) {
1378 ldb_dn_mark_invalid(dn);
1382 for (i = 0; i < base->comp_num; dn->comp_num++, i++) {
1383 dn->components[dn->comp_num] =
1384 ldb_dn_copy_component(dn->components,
1385 &base->components[i]);
1386 if (dn->components[dn->comp_num].value.data == NULL) {
1387 ldb_dn_mark_invalid(dn);
1392 if (dn->casefold && s) {
1393 if (*dn->casefold) {
1394 t = talloc_asprintf(dn, "%s,%s",
1397 t = talloc_strdup(dn, s);
1399 LDB_FREE(dn->casefold);
1404 if (dn->linearized) {
1406 s = ldb_dn_get_linearized(base);
1411 if (*dn->linearized) {
1412 t = talloc_asprintf(dn, "%s,%s",
1415 t = talloc_strdup(dn, s);
1418 ldb_dn_mark_invalid(dn);
1421 LDB_FREE(dn->linearized);
1425 /* Wipe the ext_linearized DN,
1426 * the GUID and SID are almost certainly no longer valid */
1427 if (dn->ext_linearized) {
1428 LDB_FREE(dn->ext_linearized);
1431 LDB_FREE(dn->ext_components);
1432 dn->ext_comp_num = 0;
1436 /* modify the given dn by adding a base.
1438 * return true if successful and false if not
1439 * if false is returned the dn may be marked invalid
1441 bool ldb_dn_add_base_fmt(struct ldb_dn *dn, const char *base_fmt, ...)
1443 struct ldb_dn *base;
1448 if ( !dn || dn->invalid) {
1452 va_start(ap, base_fmt);
1453 base_str = talloc_vasprintf(dn, base_fmt, ap);
1456 if (base_str == NULL) {
1460 base = ldb_dn_new(base_str, dn->ldb, base_str);
1462 ret = ldb_dn_add_base(dn, base);
1464 talloc_free(base_str);
1469 /* modify the given dn by adding children elements.
1471 * return true if successful and false if not
1472 * if false is returned the dn may be marked invalid
1474 bool ldb_dn_add_child(struct ldb_dn *dn, struct ldb_dn *child)
1479 if ( !child || child->invalid || !dn || dn->invalid) {
1483 if (dn->components) {
1486 if ( ! ldb_dn_validate(child)) {
1491 if (dn->valid_case) {
1492 if ( ! (s = ldb_dn_get_casefold(child))) {
1497 n = dn->comp_num + child->comp_num;
1499 dn->components = talloc_realloc(dn,
1501 struct ldb_dn_component,
1503 if ( ! dn->components) {
1504 ldb_dn_mark_invalid(dn);
1508 for (i = dn->comp_num - 1, j = n - 1; i >= 0; i--, j--) {
1509 dn->components[j] = dn->components[i];
1512 for (i = 0; i < child->comp_num; i++) {
1514 ldb_dn_copy_component(dn->components,
1515 &child->components[i]);
1516 if (dn->components[i].value.data == NULL) {
1517 ldb_dn_mark_invalid(dn);
1524 if (dn->casefold && s) {
1525 t = talloc_asprintf(dn, "%s,%s", s, dn->casefold);
1526 LDB_FREE(dn->casefold);
1531 if (dn->linearized) {
1533 s = ldb_dn_get_linearized(child);
1538 t = talloc_asprintf(dn, "%s,%s", s, dn->linearized);
1540 ldb_dn_mark_invalid(dn);
1543 LDB_FREE(dn->linearized);
1547 /* Wipe the ext_linearized DN,
1548 * the GUID and SID are almost certainly no longer valid */
1549 LDB_FREE(dn->ext_linearized);
1551 LDB_FREE(dn->ext_components);
1552 dn->ext_comp_num = 0;
1557 /* modify the given dn by adding children elements.
1559 * return true if successful and false if not
1560 * if false is returned the dn may be marked invalid
1562 bool ldb_dn_add_child_fmt(struct ldb_dn *dn, const char *child_fmt, ...)
1564 struct ldb_dn *child;
1569 if ( !dn || dn->invalid) {
1573 va_start(ap, child_fmt);
1574 child_str = talloc_vasprintf(dn, child_fmt, ap);
1577 if (child_str == NULL) {
1581 child = ldb_dn_new(child_str, dn->ldb, child_str);
1583 ret = ldb_dn_add_child(dn, child);
1585 talloc_free(child_str);
1590 bool ldb_dn_remove_base_components(struct ldb_dn *dn, unsigned int num)
1594 if ( ! ldb_dn_validate(dn)) {
1598 if (dn->comp_num < num) {
1602 /* free components */
1603 for (i = num; i > 0; i--) {
1604 LDB_FREE(dn->components[dn->comp_num - i].name);
1605 LDB_FREE(dn->components[dn->comp_num - i].value.data);
1606 LDB_FREE(dn->components[dn->comp_num - i].cf_name);
1607 LDB_FREE(dn->components[dn->comp_num - i].cf_value.data);
1610 dn->comp_num -= num;
1612 if (dn->valid_case) {
1613 for (i = 0; i < dn->comp_num; i++) {
1614 LDB_FREE(dn->components[i].cf_name);
1615 LDB_FREE(dn->components[i].cf_value.data);
1617 dn->valid_case = false;
1620 LDB_FREE(dn->casefold);
1621 LDB_FREE(dn->linearized);
1623 /* Wipe the ext_linearized DN,
1624 * the GUID and SID are almost certainly no longer valid */
1625 LDB_FREE(dn->ext_linearized);
1627 LDB_FREE(dn->ext_components);
1628 dn->ext_comp_num = 0;
1633 bool ldb_dn_remove_child_components(struct ldb_dn *dn, unsigned int num)
1637 if ( ! ldb_dn_validate(dn)) {
1641 if (dn->comp_num < num) {
1645 for (i = 0, j = num; j < dn->comp_num; i++, j++) {
1647 LDB_FREE(dn->components[i].name);
1648 LDB_FREE(dn->components[i].value.data);
1649 LDB_FREE(dn->components[i].cf_name);
1650 LDB_FREE(dn->components[i].cf_value.data);
1652 dn->components[i] = dn->components[j];
1655 dn->comp_num -= num;
1657 if (dn->valid_case) {
1658 for (i = 0; i < dn->comp_num; i++) {
1659 LDB_FREE(dn->components[i].cf_name);
1660 LDB_FREE(dn->components[i].cf_value.data);
1662 dn->valid_case = false;
1665 LDB_FREE(dn->casefold);
1666 LDB_FREE(dn->linearized);
1668 /* Wipe the ext_linearized DN,
1669 * the GUID and SID are almost certainly no longer valid */
1670 LDB_FREE(dn->ext_linearized);
1672 LDB_FREE(dn->ext_components);
1673 dn->ext_comp_num = 0;
1677 struct ldb_dn *ldb_dn_get_parent(void *mem_ctx, struct ldb_dn *dn)
1679 struct ldb_dn *new_dn;
1681 new_dn = ldb_dn_copy(mem_ctx, dn);
1686 if ( ! ldb_dn_remove_child_components(new_dn, 1)) {
1687 talloc_free(new_dn);
1691 /* Wipe the ext_linearized DN,
1692 * the GUID and SID are almost certainly no longer valid */
1693 LDB_FREE(dn->ext_linearized);
1695 LDB_FREE(dn->ext_components);
1696 dn->ext_comp_num = 0;
1700 /* Create a 'canonical name' string from a DN:
1702 ie dc=samba,dc=org -> samba.org/
1703 uid=administrator,ou=users,dc=samba,dc=org = samba.org/users/administrator
1705 There are two formats,
1706 the EX format has the last '/' replaced with a newline (\n).
1709 static char *ldb_dn_canonical(void *mem_ctx, struct ldb_dn *dn, int ex_format) {
1712 char *cracked = NULL;
1713 const char *format = (ex_format ? "\n" : "/" );
1715 if ( ! ldb_dn_validate(dn)) {
1719 tmpctx = talloc_new(mem_ctx);
1721 /* Walk backwards down the DN, grabbing 'dc' components at first */
1722 for (i = dn->comp_num - 1 ; i >= 0; i--) {
1723 if (ldb_attr_cmp(dn->components[i].name, "dc") != 0) {
1727 cracked = talloc_asprintf(tmpctx, "%s.%s",
1728 ldb_dn_escape_value(tmpctx,
1729 dn->components[i].value),
1732 cracked = ldb_dn_escape_value(tmpctx,
1733 dn->components[i].value);
1740 /* Only domain components? Finish here */
1742 cracked = talloc_strdup_append_buffer(cracked, format);
1743 talloc_steal(mem_ctx, cracked);
1747 /* Now walk backwards appending remaining components */
1748 for (; i > 0; i--) {
1749 cracked = talloc_asprintf_append_buffer(cracked, "/%s",
1750 ldb_dn_escape_value(tmpctx,
1751 dn->components[i].value));
1757 /* Last one, possibly a newline for the 'ex' format */
1758 cracked = talloc_asprintf_append_buffer(cracked, "%s%s", format,
1759 ldb_dn_escape_value(tmpctx,
1760 dn->components[i].value));
1762 talloc_steal(mem_ctx, cracked);
1764 talloc_free(tmpctx);
1768 /* Wrapper functions for the above, for the two different string formats */
1769 char *ldb_dn_canonical_string(void *mem_ctx, struct ldb_dn *dn) {
1770 return ldb_dn_canonical(mem_ctx, dn, 0);
1774 char *ldb_dn_canonical_ex_string(void *mem_ctx, struct ldb_dn *dn) {
1775 return ldb_dn_canonical(mem_ctx, dn, 1);
1778 int ldb_dn_get_comp_num(struct ldb_dn *dn)
1780 if ( ! ldb_dn_validate(dn)) {
1783 return dn->comp_num;
1786 const char *ldb_dn_get_component_name(struct ldb_dn *dn, unsigned int num)
1788 if ( ! ldb_dn_validate(dn)) {
1791 if (num >= dn->comp_num) return NULL;
1792 return dn->components[num].name;
1795 const struct ldb_val *ldb_dn_get_component_val(struct ldb_dn *dn,
1798 if ( ! ldb_dn_validate(dn)) {
1801 if (num >= dn->comp_num) return NULL;
1802 return &dn->components[num].value;
1805 const char *ldb_dn_get_rdn_name(struct ldb_dn *dn)
1807 if ( ! ldb_dn_validate(dn)) {
1810 if (dn->comp_num == 0) return NULL;
1811 return dn->components[0].name;
1814 const struct ldb_val *ldb_dn_get_rdn_val(struct ldb_dn *dn)
1816 if ( ! ldb_dn_validate(dn)) {
1819 if (dn->comp_num == 0) return NULL;
1820 return &dn->components[0].value;
1823 int ldb_dn_set_component(struct ldb_dn *dn, int num,
1824 const char *name, const struct ldb_val val)
1829 if ( ! ldb_dn_validate(dn)) {
1830 return LDB_ERR_OTHER;
1833 if (num >= dn->comp_num) {
1834 return LDB_ERR_OTHER;
1837 n = talloc_strdup(dn, name);
1839 return LDB_ERR_OTHER;
1842 v.length = val.length;
1843 v.data = (uint8_t *)talloc_memdup(dn, val.data, v.length+1);
1846 return LDB_ERR_OTHER;
1849 talloc_free(dn->components[num].name);
1850 talloc_free(dn->components[num].value.data);
1851 dn->components[num].name = n;
1852 dn->components[num].value = v;
1854 if (dn->valid_case) {
1856 for (i = 0; i < dn->comp_num; i++) {
1857 LDB_FREE(dn->components[i].cf_name);
1858 LDB_FREE(dn->components[i].cf_value.data);
1860 dn->valid_case = false;
1862 LDB_FREE(dn->casefold);
1863 LDB_FREE(dn->linearized);
1865 /* Wipe the ext_linearized DN,
1866 * the GUID and SID are almost certainly no longer valid */
1867 LDB_FREE(dn->ext_linearized);
1869 dn->ext_comp_num = 0;
1870 LDB_FREE(dn->ext_components);
1874 const struct ldb_val *ldb_dn_get_extended_component(struct ldb_dn *dn,
1878 if ( ! ldb_dn_validate(dn)) {
1881 for (i=0; i < dn->ext_comp_num; i++) {
1882 if (ldb_attr_cmp(dn->ext_components[i].name, name) == 0) {
1883 return &dn->ext_components[i].value;
1889 int ldb_dn_set_extended_component(struct ldb_dn *dn,
1890 const char *name, const struct ldb_val *val)
1892 struct ldb_dn_ext_component *p;
1896 if ( ! ldb_dn_validate(dn)) {
1897 return LDB_ERR_OTHER;
1900 if (!ldb_dn_extended_syntax_by_name(dn->ldb, name)) {
1901 /* We don't know how to handle this type of thing */
1902 return LDB_ERR_INVALID_DN_SYNTAX;
1905 for (i=0; i < dn->ext_comp_num; i++) {
1906 if (ldb_attr_cmp(dn->ext_components[i].name, name) == 0) {
1908 dn->ext_components[i].value =
1909 ldb_val_dup(dn->ext_components, val);
1911 dn->ext_components[i].name =
1912 talloc_strdup(dn->ext_components, name);
1913 if (!dn->ext_components[i].name ||
1914 !dn->ext_components[i].value.data) {
1915 ldb_dn_mark_invalid(dn);
1916 return LDB_ERR_OPERATIONS_ERROR;
1920 if (i != (dn->ext_comp_num - 1)) {
1921 memmove(&dn->ext_components[i],
1922 &dn->ext_components[i+1],
1923 ((dn->ext_comp_num-1) - i) *
1924 sizeof(*dn->ext_components));
1928 dn->ext_components = talloc_realloc(dn,
1930 struct ldb_dn_ext_component,
1932 if (!dn->ext_components) {
1933 ldb_dn_mark_invalid(dn);
1934 return LDB_ERR_OPERATIONS_ERROR;
1942 /* removing a value that doesn't exist is not an error */
1948 p = dn->ext_components
1949 = talloc_realloc(dn,
1951 struct ldb_dn_ext_component,
1952 dn->ext_comp_num + 1);
1953 if (!dn->ext_components) {
1954 ldb_dn_mark_invalid(dn);
1955 return LDB_ERR_OPERATIONS_ERROR;
1958 p[dn->ext_comp_num].value = ldb_val_dup(dn->ext_components, &v2);
1959 p[dn->ext_comp_num].name = talloc_strdup(p, name);
1961 if (!dn->ext_components[i].name || !dn->ext_components[i].value.data) {
1962 ldb_dn_mark_invalid(dn);
1963 return LDB_ERR_OPERATIONS_ERROR;
1965 dn->ext_components = p;
1971 void ldb_dn_remove_extended_components(struct ldb_dn *dn)
1973 dn->ext_comp_num = 0;
1974 LDB_FREE(dn->ext_components);
1977 bool ldb_dn_is_valid(struct ldb_dn *dn)
1979 if ( ! dn) return false;
1980 return ! dn->invalid;
1983 bool ldb_dn_is_special(struct ldb_dn *dn)
1985 if ( ! dn || dn->invalid) return false;
1989 bool ldb_dn_has_extended(struct ldb_dn *dn)
1991 if ( ! dn || dn->invalid) return false;
1992 if (dn->ext_linearized && (dn->ext_linearized[0] == '<')) return true;
1993 return dn->ext_comp_num != 0;
1996 bool ldb_dn_check_special(struct ldb_dn *dn, const char *check)
1998 if ( ! dn || dn->invalid) return false;
1999 return ! strcmp(dn->linearized, check);
2002 bool ldb_dn_is_null(struct ldb_dn *dn)
2004 if ( ! dn || dn->invalid) return false;
2005 if (ldb_dn_has_extended(dn)) return false;
2006 if (dn->linearized && (dn->linearized[0] == '\0')) return true;
2011 this updates dn->components, taking the components from ref_dn.
2012 This is used by code that wants to update the DN path of a DN
2013 while not impacting on the extended DN components
2015 int ldb_dn_update_components(struct ldb_dn *dn, const struct ldb_dn *ref_dn)
2017 dn->components = talloc_realloc(dn, dn->components,
2018 struct ldb_dn_component, ref_dn->comp_num);
2019 if (!dn->components) {
2020 return LDB_ERR_OPERATIONS_ERROR;
2022 memcpy(dn->components, ref_dn->components,
2023 sizeof(struct ldb_dn_component)*ref_dn->comp_num);
2024 dn->comp_num = ref_dn->comp_num;
2026 talloc_free(dn->linearized);
2027 talloc_free(dn->ext_linearized);
2028 dn->ext_linearized = NULL;
2029 dn->linearized = NULL;