join.py: Add Replica-Locations for DomainDNS and ForestDNS
[Samba.git] / lib / ldb / common / ldb_dn.c
blob3fa5ab5705600707f2594bea22be8e3c1af6071e
1 /*
2 ldb database library
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
8 ** under the LGPL
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/>.
25 * Name: ldb
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
33 * Author: Simo Sorce
36 #include "ldb_private.h"
37 #include <ctype.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)
43 /**
44 internal ldb exploded dn structures
46 struct ldb_dn_component {
48 char *name;
49 struct ldb_val value;
51 char *cf_name;
52 struct ldb_val cf_value;
55 struct ldb_dn_ext_component {
57 const char *name;
58 struct ldb_val value;
61 struct ldb_dn {
63 struct ldb_context *ldb;
65 /* Special DNs are always linearized */
66 bool special;
67 bool invalid;
69 bool valid_case;
71 char *linearized;
72 char *ext_linearized;
73 char *casefold;
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)
85 dn->invalid = true;
88 /* strdn may be NULL */
89 struct ldb_dn *ldb_dn_from_ldb_val(TALLOC_CTX *mem_ctx,
90 struct ldb_context *ldb,
91 const struct ldb_val *strdn)
93 struct ldb_dn *dn;
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 */
100 return NULL;
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 */
110 talloc_free(dn);
111 return NULL;
114 if (strdn->data && strdn->length) {
115 const char *data = (const char *)strdn->data;
116 size_t length = strdn->length;
118 if (data[0] == '@') {
119 dn->special = true;
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;
126 do {
127 p_save = p;
128 p = strstr(p, ">;");
129 if (p) {
130 p = p + 2;
132 } while (p);
134 if (p_save == dn->ext_linearized) {
135 dn->linearized = talloc_strdup(dn, "");
136 } else {
137 dn->linearized = talloc_strdup(dn, p_save);
139 LDB_DN_NULL_FAILED(dn->linearized);
140 } else {
141 dn->linearized = dn->ext_linearized;
142 dn->ext_linearized = NULL;
144 } else {
145 dn->linearized = talloc_strdup(dn, "");
146 LDB_DN_NULL_FAILED(dn->linearized);
149 return dn;
151 failed:
152 talloc_free(dn);
153 return NULL;
156 /* strdn may be NULL */
157 struct ldb_dn *ldb_dn_new(TALLOC_CTX *mem_ctx,
158 struct ldb_context *ldb,
159 const char *strdn)
161 struct ldb_val blob;
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(TALLOC_CTX *mem_ctx,
168 struct ldb_context *ldb,
169 const char *new_fmt, ...)
171 char *strdn;
172 va_list ap;
174 if (! ldb) return NULL;
176 va_start(ap, new_fmt);
177 strdn = talloc_vasprintf(mem_ctx, new_fmt, ap);
178 va_end(ap);
180 if (strdn) {
181 struct ldb_dn *dn = ldb_dn_new(mem_ctx, ldb, strdn);
182 talloc_free(strdn);
183 return dn;
186 return NULL;
189 /* see RFC2253 section 2.4 */
190 static int ldb_dn_escape_internal(char *dst, const char *src, int len)
192 char c;
193 char *d;
194 int i;
195 d = dst;
197 for (i = 0; i < len; i++){
198 c = src[i];
199 switch (c) {
200 case ' ':
201 if (i == 0 || i == len - 1) {
202 /* if at the beginning or end
203 * of the string then escape */
204 *d++ = '\\';
205 *d++ = c;
206 } else {
207 /* otherwise don't escape */
208 *d++ = c;
210 break;
212 case '#':
213 /* despite the RFC, windows escapes a #
214 anywhere in the string */
215 case ',':
216 case '+':
217 case '"':
218 case '\\':
219 case '<':
220 case '>':
221 case '?':
222 /* these must be escaped using \c form */
223 *d++ = '\\';
224 *d++ = c;
225 break;
227 case ';':
228 case '\r':
229 case '\n':
230 case '=':
231 case '\0': {
232 /* any others get \XX form */
233 unsigned char v;
234 const char *hexbytes = "0123456789ABCDEF";
235 v = (const unsigned char)c;
236 *d++ = '\\';
237 *d++ = hexbytes[v>>4];
238 *d++ = hexbytes[v&0xF];
239 break;
241 default:
242 *d++ = c;
246 /* return the length of the resulting string */
247 return (d - dst);
250 char *ldb_dn_escape_value(TALLOC_CTX *mem_ctx, struct ldb_val value)
252 char *dst;
253 size_t len;
254 if (!value.length)
255 return NULL;
257 /* allocate destination string, it will be at most 3 times the source */
258 dst = talloc_array(mem_ctx, char, value.length * 3 + 1);
259 if ( ! dst) {
260 talloc_free(dst);
261 return NULL;
264 len = ldb_dn_escape_internal(dst, (const char *)value.data, value.length);
266 dst = talloc_realloc(mem_ctx, dst, char, len + 1);
267 if ( ! dst) {
268 talloc_free(dst);
269 return NULL;
271 dst[len] = '\0';
272 return dst;
276 explode a DN string into a ldb_dn structure
277 based on RFC4514 except that we don't support multiple valued RDNs
279 TODO: according to MS-ADTS:3.1.1.5.2 Naming Constraints
280 DN must be compliant with RFC2253
282 static bool ldb_dn_explode(struct ldb_dn *dn)
284 char *p, *ex_name = NULL, *ex_value = NULL, *data, *d, *dt, *t;
285 bool trim = true;
286 bool in_extended = true;
287 bool in_ex_name = false;
288 bool in_ex_value = false;
289 bool in_attr = false;
290 bool in_value = false;
291 bool in_quote = false;
292 bool is_oid = false;
293 bool escape = false;
294 unsigned int x;
295 size_t l = 0;
296 int ret;
297 char *parse_dn;
298 bool is_index;
300 if ( ! dn || dn->invalid) return false;
302 if (dn->components) {
303 return true;
306 if (dn->ext_linearized) {
307 parse_dn = dn->ext_linearized;
308 } else {
309 parse_dn = dn->linearized;
312 if ( ! parse_dn ) {
313 return false;
316 is_index = (strncmp(parse_dn, "DN=@INDEX:", 10) == 0);
318 /* Empty DNs */
319 if (parse_dn[0] == '\0') {
320 return true;
323 /* Special DNs case */
324 if (dn->special) {
325 return true;
328 /* make sure we free this if allocated previously before replacing */
329 LDB_FREE(dn->components);
330 dn->comp_num = 0;
332 LDB_FREE(dn->ext_components);
333 dn->ext_comp_num = 0;
335 /* in the common case we have 3 or more components */
336 /* make sure all components are zeroed, other functions depend on it */
337 dn->components = talloc_zero_array(dn, struct ldb_dn_component, 3);
338 if ( ! dn->components) {
339 return false;
342 /* Components data space is allocated here once */
343 data = talloc_array(dn->components, char, strlen(parse_dn) + 1);
344 if (!data) {
345 return false;
348 p = parse_dn;
349 t = NULL;
350 d = dt = data;
352 while (*p) {
353 if (in_extended) {
355 if (!in_ex_name && !in_ex_value) {
357 if (p[0] == '<') {
358 p++;
359 ex_name = d;
360 in_ex_name = true;
361 continue;
362 } else if (p[0] == '\0') {
363 p++;
364 continue;
365 } else {
366 in_extended = false;
367 in_attr = true;
368 dt = d;
370 continue;
374 if (in_ex_name && *p == '=') {
375 *d++ = '\0';
376 p++;
377 ex_value = d;
378 in_ex_name = false;
379 in_ex_value = true;
380 continue;
383 if (in_ex_value && *p == '>') {
384 const struct ldb_dn_extended_syntax *ext_syntax;
385 struct ldb_val ex_val = {
386 .data = (uint8_t *)ex_value,
387 .length = d - ex_value
390 *d++ = '\0';
391 p++;
392 in_ex_value = false;
394 /* Process name and ex_value */
396 dn->ext_components = talloc_realloc(dn,
397 dn->ext_components,
398 struct ldb_dn_ext_component,
399 dn->ext_comp_num + 1);
400 if ( ! dn->ext_components) {
401 /* ouch ! */
402 goto failed;
405 ext_syntax = ldb_dn_extended_syntax_by_name(dn->ldb, ex_name);
406 if (!ext_syntax) {
407 /* We don't know about this type of extended DN */
408 goto failed;
411 dn->ext_components[dn->ext_comp_num].name = ext_syntax->name;
412 ret = ext_syntax->read_fn(dn->ldb, dn->ext_components,
413 &ex_val, &dn->ext_components[dn->ext_comp_num].value);
414 if (ret != LDB_SUCCESS) {
415 ldb_dn_mark_invalid(dn);
416 goto failed;
419 dn->ext_comp_num++;
421 if (*p == '\0') {
422 /* We have reached the end (extended component only)! */
423 talloc_free(data);
424 return true;
426 } else if (*p == ';') {
427 p++;
428 continue;
429 } else {
430 ldb_dn_mark_invalid(dn);
431 goto failed;
435 *d++ = *p++;
436 continue;
438 if (in_attr) {
439 if (trim) {
440 if (*p == ' ') {
441 p++;
442 continue;
445 /* first char */
446 trim = false;
448 if (!isascii(*p)) {
449 /* attr names must be ascii only */
450 ldb_dn_mark_invalid(dn);
451 goto failed;
454 if (isdigit(*p)) {
455 is_oid = true;
456 } else
457 if ( ! isalpha(*p)) {
458 /* not a digit nor an alpha,
459 * invalid attribute name */
460 ldb_dn_mark_invalid(dn);
461 goto failed;
464 /* Copy this character across from parse_dn,
465 * now we have trimmed out spaces */
466 *d++ = *p++;
467 continue;
470 if (*p == ' ') {
471 p++;
472 /* valid only if we are at the end */
473 trim = true;
474 continue;
477 if (trim && (*p != '=')) {
478 /* spaces/tabs are not allowed */
479 ldb_dn_mark_invalid(dn);
480 goto failed;
483 if (*p == '=') {
484 /* attribute terminated */
485 in_attr = false;
486 in_value = true;
487 trim = true;
488 l = 0;
490 /* Terminate this string in d
491 * (which is a copy of parse_dn
492 * with spaces trimmed) */
493 *d++ = '\0';
494 dn->components[dn->comp_num].name = talloc_strdup(dn->components, dt);
495 if ( ! dn->components[dn->comp_num].name) {
496 /* ouch */
497 goto failed;
500 dt = d;
502 p++;
503 continue;
506 if (!isascii(*p)) {
507 /* attr names must be ascii only */
508 ldb_dn_mark_invalid(dn);
509 goto failed;
512 if (is_oid && ( ! (isdigit(*p) || (*p == '.')))) {
513 /* not a digit nor a dot,
514 * invalid attribute oid */
515 ldb_dn_mark_invalid(dn);
516 goto failed;
517 } else
518 if ( ! (isalpha(*p) || isdigit(*p) || (*p == '-'))) {
519 /* not ALPHA, DIGIT or HYPHEN */
520 ldb_dn_mark_invalid(dn);
521 goto failed;
524 *d++ = *p++;
525 continue;
528 if (in_value) {
529 if (in_quote) {
530 if (*p == '\"') {
531 if (p[-1] != '\\') {
532 p++;
533 in_quote = false;
534 continue;
537 *d++ = *p++;
538 l++;
539 continue;
542 if (trim) {
543 if (*p == ' ') {
544 p++;
545 continue;
548 /* first char */
549 trim = false;
551 if (*p == '\"') {
552 in_quote = true;
553 p++;
554 continue;
558 switch (*p) {
560 /* TODO: support ber encoded values
561 case '#':
564 case ',':
565 if (escape) {
566 *d++ = *p++;
567 l++;
568 escape = false;
569 continue;
571 /* ok found value terminator */
573 if ( t ) {
574 /* trim back */
575 d -= (p - t);
576 l -= (p - t);
579 in_attr = true;
580 in_value = false;
581 trim = true;
583 p++;
584 *d++ = '\0';
587 * This talloc_memdup() is OK with the
588 * +1 because *d has been set to '\0'
589 * just above
591 dn->components[dn->comp_num].value.data = \
592 (uint8_t *)talloc_memdup(dn->components, dt, l + 1);
593 dn->components[dn->comp_num].value.length = l;
594 if ( ! dn->components[dn->comp_num].value.data) {
595 /* ouch ! */
596 goto failed;
598 talloc_set_name_const(dn->components[dn->comp_num].value.data,
599 (const char *)dn->components[dn->comp_num].value.data);
601 dt = d;
603 dn->comp_num++;
604 if (dn->comp_num > 2) {
605 dn->components = talloc_realloc(dn,
606 dn->components,
607 struct ldb_dn_component,
608 dn->comp_num + 1);
609 if ( ! dn->components) {
610 /* ouch ! */
611 goto failed;
613 /* make sure all components are zeroed, other functions depend on this */
614 memset(&dn->components[dn->comp_num], '\0', sizeof(struct ldb_dn_component));
617 continue;
619 case '+':
620 case '=':
621 /* to main compatibility with earlier
622 versions of ldb indexing, we have to
623 accept the base64 encoded binary index
624 values, which contain a '+' or '='
625 which should normally be escaped */
626 if (is_index) {
627 if ( t ) t = NULL;
628 *d++ = *p++;
629 l++;
630 break;
632 /* fall through */
633 case '\"':
634 case '<':
635 case '>':
636 case ';':
637 /* a string with not escaped specials is invalid (tested) */
638 if ( ! escape) {
639 ldb_dn_mark_invalid(dn);
640 goto failed;
642 escape = false;
644 *d++ = *p++;
645 l++;
647 if ( t ) t = NULL;
648 break;
650 case '\\':
651 if ( ! escape) {
652 escape = true;
653 p++;
654 continue;
656 escape = false;
658 *d++ = *p++;
659 l++;
661 if ( t ) t = NULL;
662 break;
664 default:
665 if (escape) {
666 if (isxdigit(p[0]) && isxdigit(p[1])) {
667 if (sscanf(p, "%02x", &x) != 1) {
668 /* invalid escaping sequence */
669 ldb_dn_mark_invalid(dn);
670 goto failed;
672 p += 2;
673 *d++ = (unsigned char)x;
674 } else {
675 *d++ = *p++;
678 escape = false;
679 l++;
680 if ( t ) t = NULL;
681 break;
684 if (*p == ' ') {
685 if ( ! t) t = p;
686 } else {
687 if ( t ) t = NULL;
690 *d++ = *p++;
691 l++;
693 break;
699 if (in_attr || in_quote) {
700 /* invalid dn */
701 ldb_dn_mark_invalid(dn);
702 goto failed;
705 /* save last element */
706 if ( t ) {
707 /* trim back */
708 d -= (p - t);
709 l -= (p - t);
712 *d++ = '\0';
714 * This talloc_memdup() is OK with the
715 * +1 because *d has been set to '\0'
716 * just above.
718 dn->components[dn->comp_num].value.length = l;
719 dn->components[dn->comp_num].value.data =
720 (uint8_t *)talloc_memdup(dn->components, dt, l + 1);
721 if ( ! dn->components[dn->comp_num].value.data) {
722 /* ouch */
723 goto failed;
725 talloc_set_name_const(dn->components[dn->comp_num].value.data,
726 (const char *)dn->components[dn->comp_num].value.data);
728 dn->comp_num++;
730 talloc_free(data);
731 return true;
733 failed:
734 LDB_FREE(dn->components); /* "data" is implicitly free'd */
735 dn->comp_num = 0;
736 LDB_FREE(dn->ext_components);
737 dn->ext_comp_num = 0;
739 return false;
742 bool ldb_dn_validate(struct ldb_dn *dn)
744 return ldb_dn_explode(dn);
747 const char *ldb_dn_get_linearized(struct ldb_dn *dn)
749 unsigned int i;
750 size_t len;
751 char *d, *n;
753 if ( ! dn || ( dn->invalid)) return NULL;
755 if (dn->linearized) return dn->linearized;
757 if ( ! dn->components) {
758 ldb_dn_mark_invalid(dn);
759 return NULL;
762 if (dn->comp_num == 0) {
763 dn->linearized = talloc_strdup(dn, "");
764 if ( ! dn->linearized) return NULL;
765 return dn->linearized;
768 /* calculate maximum possible length of DN */
769 for (len = 0, i = 0; i < dn->comp_num; i++) {
770 /* name len */
771 len += strlen(dn->components[i].name);
772 /* max escaped data len */
773 len += (dn->components[i].value.length * 3);
774 len += 2; /* '=' and ',' */
776 dn->linearized = talloc_array(dn, char, len);
777 if ( ! dn->linearized) return NULL;
779 d = dn->linearized;
781 for (i = 0; i < dn->comp_num; i++) {
783 /* copy the name */
784 n = dn->components[i].name;
785 while (*n) *d++ = *n++;
787 *d++ = '=';
789 /* and the value */
790 d += ldb_dn_escape_internal( d,
791 (char *)dn->components[i].value.data,
792 dn->components[i].value.length);
793 *d++ = ',';
796 *(--d) = '\0';
798 /* don't waste more memory than necessary */
799 dn->linearized = talloc_realloc(dn, dn->linearized,
800 char, (d - dn->linearized + 1));
802 return dn->linearized;
805 static int ldb_dn_extended_component_compare(const void *p1, const void *p2)
807 const struct ldb_dn_ext_component *ec1 = (const struct ldb_dn_ext_component *)p1;
808 const struct ldb_dn_ext_component *ec2 = (const struct ldb_dn_ext_component *)p2;
809 return strcmp(ec1->name, ec2->name);
812 char *ldb_dn_get_extended_linearized(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, int mode)
814 const char *linearized = ldb_dn_get_linearized(dn);
815 char *p = NULL;
816 unsigned int i;
818 if (!linearized) {
819 return NULL;
822 if (!ldb_dn_has_extended(dn)) {
823 return talloc_strdup(mem_ctx, linearized);
826 if (!ldb_dn_validate(dn)) {
827 return NULL;
830 /* sort the extended components by name. The idea is to make
831 * the resulting DNs consistent, plus to ensure that we put
832 * 'DELETED' first, so it can be very quickly recognised
834 TYPESAFE_QSORT(dn->ext_components, dn->ext_comp_num,
835 ldb_dn_extended_component_compare);
837 for (i = 0; i < dn->ext_comp_num; i++) {
838 const struct ldb_dn_extended_syntax *ext_syntax;
839 const char *name = dn->ext_components[i].name;
840 struct ldb_val ec_val = dn->ext_components[i].value;
841 struct ldb_val val;
842 int ret;
844 ext_syntax = ldb_dn_extended_syntax_by_name(dn->ldb, name);
845 if (!ext_syntax) {
846 return NULL;
849 if (mode == 1) {
850 ret = ext_syntax->write_clear_fn(dn->ldb, mem_ctx,
851 &ec_val, &val);
852 } else if (mode == 0) {
853 ret = ext_syntax->write_hex_fn(dn->ldb, mem_ctx,
854 &ec_val, &val);
855 } else {
856 ret = -1;
859 if (ret != LDB_SUCCESS) {
860 return NULL;
863 if (i == 0) {
864 p = talloc_asprintf(mem_ctx, "<%s=%s>",
865 name, val.data);
866 } else {
867 p = talloc_asprintf_append_buffer(p, ";<%s=%s>",
868 name, val.data);
871 talloc_free(val.data);
873 if (!p) {
874 return NULL;
878 if (dn->ext_comp_num && *linearized) {
879 p = talloc_asprintf_append_buffer(p, ";%s", linearized);
882 if (!p) {
883 return NULL;
886 return p;
890 filter out all but an acceptable list of extended DN components
892 void ldb_dn_extended_filter(struct ldb_dn *dn, const char * const *accept_list)
894 unsigned int i;
895 for (i=0; i<dn->ext_comp_num; i++) {
896 if (!ldb_attr_in_list(accept_list, dn->ext_components[i].name)) {
897 memmove(&dn->ext_components[i],
898 &dn->ext_components[i+1],
899 (dn->ext_comp_num-(i+1))*sizeof(dn->ext_components[0]));
900 dn->ext_comp_num--;
901 i--;
904 LDB_FREE(dn->ext_linearized);
908 char *ldb_dn_alloc_linearized(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
910 return talloc_strdup(mem_ctx, ldb_dn_get_linearized(dn));
914 casefold a dn. We need to casefold the attribute names, and canonicalize
915 attribute values of case insensitive attributes.
918 static bool ldb_dn_casefold_internal(struct ldb_dn *dn)
920 unsigned int i;
921 int ret;
923 if ( ! dn || dn->invalid) return false;
925 if (dn->valid_case) return true;
927 if (( ! dn->components) && ( ! ldb_dn_explode(dn))) {
928 return false;
931 for (i = 0; i < dn->comp_num; i++) {
932 const struct ldb_schema_attribute *a;
934 dn->components[i].cf_name =
935 ldb_attr_casefold(dn->components,
936 dn->components[i].name);
937 if (!dn->components[i].cf_name) {
938 goto failed;
941 a = ldb_schema_attribute_by_name(dn->ldb,
942 dn->components[i].cf_name);
944 ret = a->syntax->canonicalise_fn(dn->ldb, dn->components,
945 &(dn->components[i].value),
946 &(dn->components[i].cf_value));
947 if (ret != 0) {
948 goto failed;
952 dn->valid_case = true;
954 return true;
956 failed:
957 for (i = 0; i < dn->comp_num; i++) {
958 LDB_FREE(dn->components[i].cf_name);
959 LDB_FREE(dn->components[i].cf_value.data);
961 return false;
964 const char *ldb_dn_get_casefold(struct ldb_dn *dn)
966 unsigned int i;
967 size_t len;
968 char *d, *n;
970 if (dn->casefold) return dn->casefold;
972 if (dn->special) {
973 dn->casefold = talloc_strdup(dn, dn->linearized);
974 if (!dn->casefold) return NULL;
975 dn->valid_case = true;
976 return dn->casefold;
979 if ( ! ldb_dn_casefold_internal(dn)) {
980 return NULL;
983 if (dn->comp_num == 0) {
984 dn->casefold = talloc_strdup(dn, "");
985 return dn->casefold;
988 /* calculate maximum possible length of DN */
989 for (len = 0, i = 0; i < dn->comp_num; i++) {
990 /* name len */
991 len += strlen(dn->components[i].cf_name);
992 /* max escaped data len */
993 len += (dn->components[i].cf_value.length * 3);
994 len += 2; /* '=' and ',' */
996 dn->casefold = talloc_array(dn, char, len);
997 if ( ! dn->casefold) return NULL;
999 d = dn->casefold;
1001 for (i = 0; i < dn->comp_num; i++) {
1003 /* copy the name */
1004 n = dn->components[i].cf_name;
1005 while (*n) *d++ = *n++;
1007 *d++ = '=';
1009 /* and the value */
1010 d += ldb_dn_escape_internal( d,
1011 (char *)dn->components[i].cf_value.data,
1012 dn->components[i].cf_value.length);
1013 *d++ = ',';
1015 *(--d) = '\0';
1017 /* don't waste more memory than necessary */
1018 dn->casefold = talloc_realloc(dn, dn->casefold,
1019 char, strlen(dn->casefold) + 1);
1021 return dn->casefold;
1024 char *ldb_dn_alloc_casefold(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
1026 return talloc_strdup(mem_ctx, ldb_dn_get_casefold(dn));
1029 /* Determine if dn is below base, in the ldap tree. Used for
1030 * evaluating a subtree search.
1031 * 0 if they match, otherwise non-zero
1034 int ldb_dn_compare_base(struct ldb_dn *base, struct ldb_dn *dn)
1036 int ret;
1037 unsigned int n_base, n_dn;
1039 if ( ! base || base->invalid) return 1;
1040 if ( ! dn || dn->invalid) return -1;
1042 if (( ! base->valid_case) || ( ! dn->valid_case)) {
1043 if (base->linearized && dn->linearized && dn->special == base->special) {
1044 /* try with a normal compare first, if we are lucky
1045 * we will avoid exploding and casfolding */
1046 int dif;
1047 dif = strlen(dn->linearized) - strlen(base->linearized);
1048 if (dif < 0) {
1049 return dif;
1051 if (strcmp(base->linearized,
1052 &dn->linearized[dif]) == 0) {
1053 return 0;
1057 if ( ! ldb_dn_casefold_internal(base)) {
1058 return 1;
1061 if ( ! ldb_dn_casefold_internal(dn)) {
1062 return -1;
1067 /* if base has more components,
1068 * they don't have the same base */
1069 if (base->comp_num > dn->comp_num) {
1070 return (dn->comp_num - base->comp_num);
1073 if ((dn->comp_num == 0) || (base->comp_num == 0)) {
1074 if (dn->special && base->special) {
1075 return strcmp(base->linearized, dn->linearized);
1076 } else if (dn->special) {
1077 return -1;
1078 } else if (base->special) {
1079 return 1;
1080 } else {
1081 return 0;
1085 n_base = base->comp_num - 1;
1086 n_dn = dn->comp_num - 1;
1088 while (n_base != (unsigned int) -1) {
1089 char *b_name = base->components[n_base].cf_name;
1090 char *dn_name = dn->components[n_dn].cf_name;
1092 char *b_vdata = (char *)base->components[n_base].cf_value.data;
1093 char *dn_vdata = (char *)dn->components[n_dn].cf_value.data;
1095 size_t b_vlen = base->components[n_base].cf_value.length;
1096 size_t dn_vlen = dn->components[n_dn].cf_value.length;
1098 /* compare attr names */
1099 ret = strcmp(b_name, dn_name);
1100 if (ret != 0) return ret;
1102 /* compare attr.cf_value. */
1103 if (b_vlen != dn_vlen) {
1104 return b_vlen - dn_vlen;
1106 ret = strncmp(b_vdata, dn_vdata, b_vlen);
1107 if (ret != 0) return ret;
1109 n_base--;
1110 n_dn--;
1113 return 0;
1116 /* compare DNs using casefolding compare functions.
1118 If they match, then return 0
1121 int ldb_dn_compare(struct ldb_dn *dn0, struct ldb_dn *dn1)
1123 unsigned int i;
1124 int ret;
1126 if (( ! dn0) || dn0->invalid || ! dn1 || dn1->invalid) {
1127 return -1;
1130 if (( ! dn0->valid_case) || ( ! dn1->valid_case)) {
1131 if (dn0->linearized && dn1->linearized) {
1132 /* try with a normal compare first, if we are lucky
1133 * we will avoid exploding and casfolding */
1134 if (strcmp(dn0->linearized, dn1->linearized) == 0) {
1135 return 0;
1139 if ( ! ldb_dn_casefold_internal(dn0)) {
1140 return 1;
1143 if ( ! ldb_dn_casefold_internal(dn1)) {
1144 return -1;
1149 if (dn0->comp_num != dn1->comp_num) {
1150 return (dn1->comp_num - dn0->comp_num);
1153 if (dn0->comp_num == 0) {
1154 if (dn0->special && dn1->special) {
1155 return strcmp(dn0->linearized, dn1->linearized);
1156 } else if (dn0->special) {
1157 return 1;
1158 } else if (dn1->special) {
1159 return -1;
1160 } else {
1161 return 0;
1165 for (i = 0; i < dn0->comp_num; i++) {
1166 char *dn0_name = dn0->components[i].cf_name;
1167 char *dn1_name = dn1->components[i].cf_name;
1169 char *dn0_vdata = (char *)dn0->components[i].cf_value.data;
1170 char *dn1_vdata = (char *)dn1->components[i].cf_value.data;
1172 size_t dn0_vlen = dn0->components[i].cf_value.length;
1173 size_t dn1_vlen = dn1->components[i].cf_value.length;
1175 /* compare attr names */
1176 ret = strcmp(dn0_name, dn1_name);
1177 if (ret != 0) {
1178 return ret;
1181 /* compare attr.cf_value. */
1182 if (dn0_vlen != dn1_vlen) {
1183 return dn0_vlen - dn1_vlen;
1185 ret = strncmp(dn0_vdata, dn1_vdata, dn0_vlen);
1186 if (ret != 0) {
1187 return ret;
1191 return 0;
1194 static struct ldb_dn_component ldb_dn_copy_component(
1195 TALLOC_CTX *mem_ctx,
1196 struct ldb_dn_component *src)
1198 struct ldb_dn_component dst;
1200 memset(&dst, 0, sizeof(dst));
1202 if (src == NULL) {
1203 return dst;
1206 dst.value = ldb_val_dup(mem_ctx, &(src->value));
1207 if (dst.value.data == NULL) {
1208 return dst;
1211 dst.name = talloc_strdup(mem_ctx, src->name);
1212 if (dst.name == NULL) {
1213 LDB_FREE(dst.value.data);
1214 return dst;
1217 if (src->cf_value.data) {
1218 dst.cf_value = ldb_val_dup(mem_ctx, &(src->cf_value));
1219 if (dst.cf_value.data == NULL) {
1220 LDB_FREE(dst.value.data);
1221 LDB_FREE(dst.name);
1222 return dst;
1225 dst.cf_name = talloc_strdup(mem_ctx, src->cf_name);
1226 if (dst.cf_name == NULL) {
1227 LDB_FREE(dst.cf_name);
1228 LDB_FREE(dst.value.data);
1229 LDB_FREE(dst.name);
1230 return dst;
1232 } else {
1233 dst.cf_value.data = NULL;
1234 dst.cf_name = NULL;
1237 return dst;
1240 static struct ldb_dn_ext_component ldb_dn_ext_copy_component(
1241 TALLOC_CTX *mem_ctx,
1242 struct ldb_dn_ext_component *src)
1244 struct ldb_dn_ext_component dst;
1246 memset(&dst, 0, sizeof(dst));
1248 if (src == NULL) {
1249 return dst;
1252 dst.value = ldb_val_dup(mem_ctx, &(src->value));
1253 if (dst.value.data == NULL) {
1254 return dst;
1257 dst.name = talloc_strdup(mem_ctx, src->name);
1258 if (dst.name == NULL) {
1259 LDB_FREE(dst.value.data);
1260 return dst;
1263 return dst;
1266 struct ldb_dn *ldb_dn_copy(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
1268 struct ldb_dn *new_dn;
1270 if (!dn || dn->invalid) {
1271 return NULL;
1274 new_dn = talloc_zero(mem_ctx, struct ldb_dn);
1275 if ( !new_dn) {
1276 return NULL;
1279 *new_dn = *dn;
1281 if (dn->components) {
1282 unsigned int i;
1284 new_dn->components =
1285 talloc_zero_array(new_dn,
1286 struct ldb_dn_component,
1287 dn->comp_num);
1288 if ( ! new_dn->components) {
1289 talloc_free(new_dn);
1290 return NULL;
1293 for (i = 0; i < dn->comp_num; i++) {
1294 new_dn->components[i] =
1295 ldb_dn_copy_component(new_dn->components,
1296 &dn->components[i]);
1297 if ( ! new_dn->components[i].value.data) {
1298 talloc_free(new_dn);
1299 return NULL;
1304 if (dn->ext_components) {
1305 unsigned int i;
1307 new_dn->ext_components =
1308 talloc_zero_array(new_dn,
1309 struct ldb_dn_ext_component,
1310 dn->ext_comp_num);
1311 if ( ! new_dn->ext_components) {
1312 talloc_free(new_dn);
1313 return NULL;
1316 for (i = 0; i < dn->ext_comp_num; i++) {
1317 new_dn->ext_components[i] =
1318 ldb_dn_ext_copy_component(
1319 new_dn->ext_components,
1320 &dn->ext_components[i]);
1321 if ( ! new_dn->ext_components[i].value.data) {
1322 talloc_free(new_dn);
1323 return NULL;
1328 if (dn->casefold) {
1329 new_dn->casefold = talloc_strdup(new_dn, dn->casefold);
1330 if ( ! new_dn->casefold) {
1331 talloc_free(new_dn);
1332 return NULL;
1336 if (dn->linearized) {
1337 new_dn->linearized = talloc_strdup(new_dn, dn->linearized);
1338 if ( ! new_dn->linearized) {
1339 talloc_free(new_dn);
1340 return NULL;
1344 if (dn->ext_linearized) {
1345 new_dn->ext_linearized = talloc_strdup(new_dn,
1346 dn->ext_linearized);
1347 if ( ! new_dn->ext_linearized) {
1348 talloc_free(new_dn);
1349 return NULL;
1353 return new_dn;
1356 /* modify the given dn by adding a base.
1358 * return true if successful and false if not
1359 * if false is returned the dn may be marked invalid
1361 bool ldb_dn_add_base(struct ldb_dn *dn, struct ldb_dn *base)
1363 const char *s;
1364 char *t;
1366 if ( !base || base->invalid || !dn || dn->invalid) {
1367 return false;
1370 if (dn->components) {
1371 unsigned int i;
1373 if ( ! ldb_dn_validate(base)) {
1374 return false;
1377 s = NULL;
1378 if (dn->valid_case) {
1379 if ( ! (s = ldb_dn_get_casefold(base))) {
1380 return false;
1384 dn->components = talloc_realloc(dn,
1385 dn->components,
1386 struct ldb_dn_component,
1387 dn->comp_num + base->comp_num);
1388 if ( ! dn->components) {
1389 ldb_dn_mark_invalid(dn);
1390 return false;
1393 for (i = 0; i < base->comp_num; dn->comp_num++, i++) {
1394 dn->components[dn->comp_num] =
1395 ldb_dn_copy_component(dn->components,
1396 &base->components[i]);
1397 if (dn->components[dn->comp_num].value.data == NULL) {
1398 ldb_dn_mark_invalid(dn);
1399 return false;
1403 if (dn->casefold && s) {
1404 if (*dn->casefold) {
1405 t = talloc_asprintf(dn, "%s,%s",
1406 dn->casefold, s);
1407 } else {
1408 t = talloc_strdup(dn, s);
1410 LDB_FREE(dn->casefold);
1411 dn->casefold = t;
1415 if (dn->linearized) {
1417 s = ldb_dn_get_linearized(base);
1418 if ( ! s) {
1419 return false;
1422 if (*dn->linearized) {
1423 t = talloc_asprintf(dn, "%s,%s",
1424 dn->linearized, s);
1425 } else {
1426 t = talloc_strdup(dn, s);
1428 if ( ! t) {
1429 ldb_dn_mark_invalid(dn);
1430 return false;
1432 LDB_FREE(dn->linearized);
1433 dn->linearized = t;
1436 /* Wipe the ext_linearized DN,
1437 * the GUID and SID are almost certainly no longer valid */
1438 LDB_FREE(dn->ext_linearized);
1439 LDB_FREE(dn->ext_components);
1440 dn->ext_comp_num = 0;
1442 return true;
1445 /* modify the given dn by adding a base.
1447 * return true if successful and false if not
1448 * if false is returned the dn may be marked invalid
1450 bool ldb_dn_add_base_fmt(struct ldb_dn *dn, const char *base_fmt, ...)
1452 struct ldb_dn *base;
1453 char *base_str;
1454 va_list ap;
1455 bool ret;
1457 if ( !dn || dn->invalid) {
1458 return false;
1461 va_start(ap, base_fmt);
1462 base_str = talloc_vasprintf(dn, base_fmt, ap);
1463 va_end(ap);
1465 if (base_str == NULL) {
1466 return false;
1469 base = ldb_dn_new(base_str, dn->ldb, base_str);
1471 ret = ldb_dn_add_base(dn, base);
1473 talloc_free(base_str);
1475 return ret;
1478 /* modify the given dn by adding children elements.
1480 * return true if successful and false if not
1481 * if false is returned the dn may be marked invalid
1483 bool ldb_dn_add_child(struct ldb_dn *dn, struct ldb_dn *child)
1485 const char *s;
1486 char *t;
1488 if ( !child || child->invalid || !dn || dn->invalid) {
1489 return false;
1492 if (dn->components) {
1493 unsigned int n;
1494 unsigned int i, j;
1496 if (dn->comp_num == 0) {
1497 return false;
1500 if ( ! ldb_dn_validate(child)) {
1501 return false;
1504 s = NULL;
1505 if (dn->valid_case) {
1506 if ( ! (s = ldb_dn_get_casefold(child))) {
1507 return false;
1511 n = dn->comp_num + child->comp_num;
1513 dn->components = talloc_realloc(dn,
1514 dn->components,
1515 struct ldb_dn_component,
1517 if ( ! dn->components) {
1518 ldb_dn_mark_invalid(dn);
1519 return false;
1522 for (i = dn->comp_num - 1, j = n - 1; i != (unsigned int) -1;
1523 i--, j--) {
1524 dn->components[j] = dn->components[i];
1527 for (i = 0; i < child->comp_num; i++) {
1528 dn->components[i] =
1529 ldb_dn_copy_component(dn->components,
1530 &child->components[i]);
1531 if (dn->components[i].value.data == NULL) {
1532 ldb_dn_mark_invalid(dn);
1533 return false;
1537 dn->comp_num = n;
1539 if (dn->casefold && s) {
1540 t = talloc_asprintf(dn, "%s,%s", s, dn->casefold);
1541 LDB_FREE(dn->casefold);
1542 dn->casefold = t;
1546 if (dn->linearized) {
1547 if (dn->linearized[0] == '\0') {
1548 return false;
1551 s = ldb_dn_get_linearized(child);
1552 if ( ! s) {
1553 return false;
1556 t = talloc_asprintf(dn, "%s,%s", s, dn->linearized);
1557 if ( ! t) {
1558 ldb_dn_mark_invalid(dn);
1559 return false;
1561 LDB_FREE(dn->linearized);
1562 dn->linearized = t;
1565 /* Wipe the ext_linearized DN,
1566 * the GUID and SID are almost certainly no longer valid */
1567 LDB_FREE(dn->ext_linearized);
1568 LDB_FREE(dn->ext_components);
1569 dn->ext_comp_num = 0;
1571 return true;
1574 /* modify the given dn by adding children elements.
1576 * return true if successful and false if not
1577 * if false is returned the dn may be marked invalid
1579 bool ldb_dn_add_child_fmt(struct ldb_dn *dn, const char *child_fmt, ...)
1581 struct ldb_dn *child;
1582 char *child_str;
1583 va_list ap;
1584 bool ret;
1586 if ( !dn || dn->invalid) {
1587 return false;
1590 va_start(ap, child_fmt);
1591 child_str = talloc_vasprintf(dn, child_fmt, ap);
1592 va_end(ap);
1594 if (child_str == NULL) {
1595 return false;
1598 child = ldb_dn_new(child_str, dn->ldb, child_str);
1600 ret = ldb_dn_add_child(dn, child);
1602 talloc_free(child_str);
1604 return ret;
1607 bool ldb_dn_remove_base_components(struct ldb_dn *dn, unsigned int num)
1609 unsigned int i;
1611 if ( ! ldb_dn_validate(dn)) {
1612 return false;
1615 if (dn->comp_num < num) {
1616 return false;
1619 /* free components */
1620 for (i = dn->comp_num - num; i < dn->comp_num; i++) {
1621 LDB_FREE(dn->components[i].name);
1622 LDB_FREE(dn->components[i].value.data);
1623 LDB_FREE(dn->components[i].cf_name);
1624 LDB_FREE(dn->components[i].cf_value.data);
1627 dn->comp_num -= num;
1629 if (dn->valid_case) {
1630 for (i = 0; i < dn->comp_num; i++) {
1631 LDB_FREE(dn->components[i].cf_name);
1632 LDB_FREE(dn->components[i].cf_value.data);
1634 dn->valid_case = false;
1637 LDB_FREE(dn->casefold);
1638 LDB_FREE(dn->linearized);
1640 /* Wipe the ext_linearized DN,
1641 * the GUID and SID are almost certainly no longer valid */
1642 LDB_FREE(dn->ext_linearized);
1643 LDB_FREE(dn->ext_components);
1644 dn->ext_comp_num = 0;
1646 return true;
1649 bool ldb_dn_remove_child_components(struct ldb_dn *dn, unsigned int num)
1651 unsigned int i, j;
1653 if ( ! ldb_dn_validate(dn)) {
1654 return false;
1657 if (dn->comp_num < num) {
1658 return false;
1661 for (i = 0, j = num; j < dn->comp_num; i++, j++) {
1662 if (i < num) {
1663 LDB_FREE(dn->components[i].name);
1664 LDB_FREE(dn->components[i].value.data);
1665 LDB_FREE(dn->components[i].cf_name);
1666 LDB_FREE(dn->components[i].cf_value.data);
1668 dn->components[i] = dn->components[j];
1671 dn->comp_num -= num;
1673 if (dn->valid_case) {
1674 for (i = 0; i < dn->comp_num; i++) {
1675 LDB_FREE(dn->components[i].cf_name);
1676 LDB_FREE(dn->components[i].cf_value.data);
1678 dn->valid_case = false;
1681 LDB_FREE(dn->casefold);
1682 LDB_FREE(dn->linearized);
1684 /* Wipe the ext_linearized DN,
1685 * the GUID and SID are almost certainly no longer valid */
1686 LDB_FREE(dn->ext_linearized);
1687 LDB_FREE(dn->ext_components);
1688 dn->ext_comp_num = 0;
1690 return true;
1694 /* replace the components of a DN with those from another DN, without
1695 * touching the extended components
1697 * return true if successful and false if not
1698 * if false is returned the dn may be marked invalid
1700 bool ldb_dn_replace_components(struct ldb_dn *dn, struct ldb_dn *new_dn)
1702 int i;
1704 if ( ! ldb_dn_validate(dn) || ! ldb_dn_validate(new_dn)) {
1705 return false;
1708 /* free components */
1709 for (i = 0; i < dn->comp_num; i++) {
1710 LDB_FREE(dn->components[i].name);
1711 LDB_FREE(dn->components[i].value.data);
1712 LDB_FREE(dn->components[i].cf_name);
1713 LDB_FREE(dn->components[i].cf_value.data);
1716 dn->components = talloc_realloc(dn,
1717 dn->components,
1718 struct ldb_dn_component,
1719 new_dn->comp_num);
1720 if (dn->components == NULL) {
1721 ldb_dn_mark_invalid(dn);
1722 return false;
1725 dn->comp_num = new_dn->comp_num;
1726 dn->valid_case = new_dn->valid_case;
1728 for (i = 0; i < dn->comp_num; i++) {
1729 dn->components[i] = ldb_dn_copy_component(dn->components, &new_dn->components[i]);
1730 if (dn->components[i].name == NULL) {
1731 ldb_dn_mark_invalid(dn);
1732 return false;
1735 if (new_dn->linearized == NULL) {
1736 dn->linearized = NULL;
1737 } else {
1738 dn->linearized = talloc_strdup(dn, new_dn->linearized);
1739 if (dn->linearized == NULL) {
1740 ldb_dn_mark_invalid(dn);
1741 return false;
1745 return true;
1749 struct ldb_dn *ldb_dn_get_parent(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
1751 struct ldb_dn *new_dn;
1753 new_dn = ldb_dn_copy(mem_ctx, dn);
1754 if ( !new_dn ) {
1755 return NULL;
1758 if ( ! ldb_dn_remove_child_components(new_dn, 1)) {
1759 talloc_free(new_dn);
1760 return NULL;
1763 return new_dn;
1766 /* Create a 'canonical name' string from a DN:
1768 ie dc=samba,dc=org -> samba.org/
1769 uid=administrator,ou=users,dc=samba,dc=org = samba.org/users/administrator
1771 There are two formats,
1772 the EX format has the last '/' replaced with a newline (\n).
1775 static char *ldb_dn_canonical(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, int ex_format) {
1776 unsigned int i;
1777 TALLOC_CTX *tmpctx;
1778 char *cracked = NULL;
1779 const char *format = (ex_format ? "\n" : "/" );
1781 if ( ! ldb_dn_validate(dn)) {
1782 return NULL;
1785 tmpctx = talloc_new(mem_ctx);
1787 /* Walk backwards down the DN, grabbing 'dc' components at first */
1788 for (i = dn->comp_num - 1; i != (unsigned int) -1; i--) {
1789 if (ldb_attr_cmp(dn->components[i].name, "dc") != 0) {
1790 break;
1792 if (cracked) {
1793 cracked = talloc_asprintf(tmpctx, "%s.%s",
1794 ldb_dn_escape_value(tmpctx,
1795 dn->components[i].value),
1796 cracked);
1797 } else {
1798 cracked = ldb_dn_escape_value(tmpctx,
1799 dn->components[i].value);
1801 if (!cracked) {
1802 goto done;
1806 /* Only domain components? Finish here */
1807 if (i == (unsigned int) -1) {
1808 cracked = talloc_strdup_append_buffer(cracked, format);
1809 talloc_steal(mem_ctx, cracked);
1810 goto done;
1813 /* Now walk backwards appending remaining components */
1814 for (; i > 0; i--) {
1815 cracked = talloc_asprintf_append_buffer(cracked, "/%s",
1816 ldb_dn_escape_value(tmpctx,
1817 dn->components[i].value));
1818 if (!cracked) {
1819 goto done;
1823 /* Last one, possibly a newline for the 'ex' format */
1824 cracked = talloc_asprintf_append_buffer(cracked, "%s%s", format,
1825 ldb_dn_escape_value(tmpctx,
1826 dn->components[i].value));
1828 talloc_steal(mem_ctx, cracked);
1829 done:
1830 talloc_free(tmpctx);
1831 return cracked;
1834 /* Wrapper functions for the above, for the two different string formats */
1835 char *ldb_dn_canonical_string(TALLOC_CTX *mem_ctx, struct ldb_dn *dn) {
1836 return ldb_dn_canonical(mem_ctx, dn, 0);
1840 char *ldb_dn_canonical_ex_string(TALLOC_CTX *mem_ctx, struct ldb_dn *dn) {
1841 return ldb_dn_canonical(mem_ctx, dn, 1);
1844 int ldb_dn_get_comp_num(struct ldb_dn *dn)
1846 if ( ! ldb_dn_validate(dn)) {
1847 return -1;
1849 return dn->comp_num;
1852 int ldb_dn_get_extended_comp_num(struct ldb_dn *dn)
1854 if ( ! ldb_dn_validate(dn)) {
1855 return -1;
1857 return dn->ext_comp_num;
1860 const char *ldb_dn_get_component_name(struct ldb_dn *dn, unsigned int num)
1862 if ( ! ldb_dn_validate(dn)) {
1863 return NULL;
1865 if (num >= dn->comp_num) return NULL;
1866 return dn->components[num].name;
1869 const struct ldb_val *ldb_dn_get_component_val(struct ldb_dn *dn,
1870 unsigned int num)
1872 if ( ! ldb_dn_validate(dn)) {
1873 return NULL;
1875 if (num >= dn->comp_num) return NULL;
1876 return &dn->components[num].value;
1879 const char *ldb_dn_get_rdn_name(struct ldb_dn *dn)
1881 if ( ! ldb_dn_validate(dn)) {
1882 return NULL;
1884 if (dn->comp_num == 0) return NULL;
1885 return dn->components[0].name;
1888 const struct ldb_val *ldb_dn_get_rdn_val(struct ldb_dn *dn)
1890 if ( ! ldb_dn_validate(dn)) {
1891 return NULL;
1893 if (dn->comp_num == 0) return NULL;
1894 return &dn->components[0].value;
1897 int ldb_dn_set_component(struct ldb_dn *dn, int num,
1898 const char *name, const struct ldb_val val)
1900 char *n;
1901 struct ldb_val v;
1903 if ( ! ldb_dn_validate(dn)) {
1904 return LDB_ERR_OTHER;
1907 if (num >= dn->comp_num) {
1908 return LDB_ERR_OTHER;
1911 if (num < 0) {
1912 return LDB_ERR_OTHER;
1915 if (val.length > val.length + 1) {
1916 return LDB_ERR_OTHER;
1919 n = talloc_strdup(dn, name);
1920 if ( ! n) {
1921 return LDB_ERR_OTHER;
1924 v.length = val.length;
1927 * This is like talloc_memdup(dn, v.data, v.length + 1), but
1928 * avoids the over-read
1930 v.data = (uint8_t *)talloc_size(dn, v.length+1);
1931 if ( ! v.data) {
1932 talloc_free(n);
1933 return LDB_ERR_OTHER;
1935 memcpy(v.data, val.data, val.length);
1938 * Enforce NUL termination outside the stated length, as is
1939 * traditional in LDB
1941 v.data[v.length] = '\0';
1943 talloc_free(dn->components[num].name);
1944 talloc_free(dn->components[num].value.data);
1945 dn->components[num].name = n;
1946 dn->components[num].value = v;
1948 if (dn->valid_case) {
1949 unsigned int i;
1950 for (i = 0; i < dn->comp_num; i++) {
1951 LDB_FREE(dn->components[i].cf_name);
1952 LDB_FREE(dn->components[i].cf_value.data);
1954 dn->valid_case = false;
1956 LDB_FREE(dn->casefold);
1957 LDB_FREE(dn->linearized);
1959 /* Wipe the ext_linearized DN,
1960 * the GUID and SID are almost certainly no longer valid */
1961 LDB_FREE(dn->ext_linearized);
1962 LDB_FREE(dn->ext_components);
1963 dn->ext_comp_num = 0;
1965 return LDB_SUCCESS;
1968 const struct ldb_val *ldb_dn_get_extended_component(struct ldb_dn *dn,
1969 const char *name)
1971 unsigned int i;
1972 if ( ! ldb_dn_validate(dn)) {
1973 return NULL;
1975 for (i=0; i < dn->ext_comp_num; i++) {
1976 if (ldb_attr_cmp(dn->ext_components[i].name, name) == 0) {
1977 return &dn->ext_components[i].value;
1980 return NULL;
1983 int ldb_dn_set_extended_component(struct ldb_dn *dn,
1984 const char *name, const struct ldb_val *val)
1986 struct ldb_dn_ext_component *p;
1987 unsigned int i;
1988 struct ldb_val v2;
1989 const struct ldb_dn_extended_syntax *ext_syntax;
1991 if ( ! ldb_dn_validate(dn)) {
1992 return LDB_ERR_OTHER;
1995 ext_syntax = ldb_dn_extended_syntax_by_name(dn->ldb, name);
1996 if (ext_syntax == NULL) {
1997 /* We don't know how to handle this type of thing */
1998 return LDB_ERR_INVALID_DN_SYNTAX;
2001 for (i=0; i < dn->ext_comp_num; i++) {
2002 if (ldb_attr_cmp(dn->ext_components[i].name, name) == 0) {
2003 if (val) {
2004 dn->ext_components[i].value =
2005 ldb_val_dup(dn->ext_components, val);
2007 dn->ext_components[i].name = ext_syntax->name;
2008 if (!dn->ext_components[i].value.data) {
2009 ldb_dn_mark_invalid(dn);
2010 return LDB_ERR_OPERATIONS_ERROR;
2012 } else {
2013 if (i != (dn->ext_comp_num - 1)) {
2014 memmove(&dn->ext_components[i],
2015 &dn->ext_components[i+1],
2016 ((dn->ext_comp_num-1) - i) *
2017 sizeof(*dn->ext_components));
2019 dn->ext_comp_num--;
2021 dn->ext_components = talloc_realloc(dn,
2022 dn->ext_components,
2023 struct ldb_dn_ext_component,
2024 dn->ext_comp_num);
2025 if (!dn->ext_components) {
2026 ldb_dn_mark_invalid(dn);
2027 return LDB_ERR_OPERATIONS_ERROR;
2030 LDB_FREE(dn->ext_linearized);
2032 return LDB_SUCCESS;
2036 if (val == NULL) {
2037 /* removing a value that doesn't exist is not an error */
2038 return LDB_SUCCESS;
2041 v2 = *val;
2043 p = dn->ext_components
2044 = talloc_realloc(dn,
2045 dn->ext_components,
2046 struct ldb_dn_ext_component,
2047 dn->ext_comp_num + 1);
2048 if (!dn->ext_components) {
2049 ldb_dn_mark_invalid(dn);
2050 return LDB_ERR_OPERATIONS_ERROR;
2053 p[dn->ext_comp_num].value = ldb_val_dup(dn->ext_components, &v2);
2054 p[dn->ext_comp_num].name = talloc_strdup(p, name);
2056 if (!dn->ext_components[i].name || !dn->ext_components[i].value.data) {
2057 ldb_dn_mark_invalid(dn);
2058 return LDB_ERR_OPERATIONS_ERROR;
2060 dn->ext_components = p;
2061 dn->ext_comp_num++;
2063 LDB_FREE(dn->ext_linearized);
2065 return LDB_SUCCESS;
2068 void ldb_dn_remove_extended_components(struct ldb_dn *dn)
2070 LDB_FREE(dn->ext_linearized);
2071 LDB_FREE(dn->ext_components);
2072 dn->ext_comp_num = 0;
2075 bool ldb_dn_is_valid(struct ldb_dn *dn)
2077 if ( ! dn) return false;
2078 return ! dn->invalid;
2081 bool ldb_dn_is_special(struct ldb_dn *dn)
2083 if ( ! dn || dn->invalid) return false;
2084 return dn->special;
2087 bool ldb_dn_has_extended(struct ldb_dn *dn)
2089 if ( ! dn || dn->invalid) return false;
2090 if (dn->ext_linearized && (dn->ext_linearized[0] == '<')) return true;
2091 return dn->ext_comp_num != 0;
2094 bool ldb_dn_check_special(struct ldb_dn *dn, const char *check)
2096 if ( ! dn || dn->invalid) return false;
2097 return ! strcmp(dn->linearized, check);
2100 bool ldb_dn_is_null(struct ldb_dn *dn)
2102 if ( ! dn || dn->invalid) return false;
2103 if (ldb_dn_has_extended(dn)) return false;
2104 if (dn->linearized && (dn->linearized[0] == '\0')) return true;
2105 return false;
2109 this updates dn->components, taking the components from ref_dn.
2110 This is used by code that wants to update the DN path of a DN
2111 while not impacting on the extended DN components
2113 int ldb_dn_update_components(struct ldb_dn *dn, const struct ldb_dn *ref_dn)
2115 dn->components = talloc_realloc(dn, dn->components,
2116 struct ldb_dn_component, ref_dn->comp_num);
2117 if (!dn->components) {
2118 return LDB_ERR_OPERATIONS_ERROR;
2120 memcpy(dn->components, ref_dn->components,
2121 sizeof(struct ldb_dn_component)*ref_dn->comp_num);
2122 dn->comp_num = ref_dn->comp_num;
2124 LDB_FREE(dn->casefold);
2125 LDB_FREE(dn->linearized);
2126 LDB_FREE(dn->ext_linearized);
2128 return LDB_SUCCESS;
2132 minimise a DN. The caller must pass in a validated DN.
2134 If the DN has an extended component then only the first extended
2135 component is kept, the DN string is stripped.
2137 The existing dn is modified
2139 bool ldb_dn_minimise(struct ldb_dn *dn)
2141 unsigned int i;
2143 if (!ldb_dn_validate(dn)) {
2144 return false;
2146 if (dn->ext_comp_num == 0) {
2147 return true;
2150 /* free components */
2151 for (i = 0; i < dn->comp_num; i++) {
2152 LDB_FREE(dn->components[i].name);
2153 LDB_FREE(dn->components[i].value.data);
2154 LDB_FREE(dn->components[i].cf_name);
2155 LDB_FREE(dn->components[i].cf_value.data);
2157 dn->comp_num = 0;
2158 dn->valid_case = false;
2160 LDB_FREE(dn->casefold);
2161 LDB_FREE(dn->linearized);
2163 /* note that we don't free dn->components as this there are
2164 * several places in ldb_dn.c that rely on it being non-NULL
2165 * for an exploded DN
2168 for (i = 1; i < dn->ext_comp_num; i++) {
2169 LDB_FREE(dn->ext_components[i].value.data);
2171 dn->ext_comp_num = 1;
2173 dn->ext_components = talloc_realloc(dn, dn->ext_components, struct ldb_dn_ext_component, 1);
2174 if (dn->ext_components == NULL) {
2175 ldb_dn_mark_invalid(dn);
2176 return false;
2179 LDB_FREE(dn->ext_linearized);
2181 return true;
2184 struct ldb_context *ldb_dn_get_ldb_context(struct ldb_dn *dn)
2186 return dn->ldb;