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 explode and utility functions
29 * Description: - explode a dn into its own basic elements
30 * and put them in a structure
31 * - manipulate ldb_dn structures
37 #include "ldb/include/includes.h"
39 #define LDB_DN_NULL_FAILED(x) if (!(x)) goto failed
41 #define LDB_SPECIAL "@SPECIAL"
44 internal ldb exploded dn structures
46 struct ldb_dn_component
{
53 struct ldb_dn_component
*components
;
56 int ldb_dn_is_special(const struct ldb_dn
*dn
)
58 if (dn
== NULL
|| dn
->comp_num
!= 1) return 0;
60 return ! strcmp(dn
->components
[0].name
, LDB_SPECIAL
);
63 int ldb_dn_check_special(const struct ldb_dn
*dn
, const char *check
)
65 if (dn
== NULL
|| dn
->comp_num
!= 1) return 0;
67 return ! strcmp((char *)dn
->components
[0].value
.data
, check
);
70 char *ldb_dn_escape_value(void *mem_ctx
, struct ldb_val value
)
72 const char *p
, *s
, *src
;
79 p
= s
= src
= (const char *)value
.data
;
82 /* allocate destination string, it will be at most 3 times the source */
83 dst
= d
= talloc_array(mem_ctx
, char, len
* 3 + 1);
84 LDB_DN_NULL_FAILED(dst
);
86 while (p
- src
< len
) {
88 p
+= strcspn(p
, ",=\n+<>#;\\\"");
90 if (p
- src
== len
) /* found no escapable chars */
93 memcpy(d
, s
, p
- s
); /* copy the part of the string before the stop */
94 d
+= (p
- s
); /* move to current position */
96 if (*p
) { /* it is a normal escapable character */
99 } else { /* we have a zero byte in the string */
100 strncpy(d
, "\00", 3); /* escape the zero */
102 p
++; /* skip the zero */
104 s
= p
; /* move forward */
107 /* copy the last part (with zero) and return */
108 memcpy(d
, s
, &src
[len
] - s
+ 1);
117 static struct ldb_val
ldb_dn_unescape_value(void *mem_ctx
, const char *src
)
119 struct ldb_val value
;
121 char *p
, *dst
= NULL
, *end
;
123 memset(&value
, 0, sizeof(value
));
125 LDB_DN_NULL_FAILED(src
);
127 dst
= p
= (char *)talloc_memdup(mem_ctx
, src
, strlen(src
) + 1);
128 LDB_DN_NULL_FAILED(dst
);
130 end
= &dst
[strlen(dst
)];
133 p
+= strcspn(p
, ",=\n+<>#;\\\"");
136 if (strchr(",=\n+<>#;\\\"", p
[1])) {
137 memmove(p
, p
+ 1, end
- (p
+ 1) + 1);
143 if (sscanf(p
+ 1, "%02x", &x
) == 1) {
144 *p
= (unsigned char)x
;
145 memmove(p
+ 1, p
+ 3, end
- (p
+ 3) + 1);
152 /* a string with not escaped specials is invalid (tested) */
158 value
.length
= end
- dst
;
159 value
.data
= (uint8_t *)dst
;
167 /* check if the string contains quotes
168 * skips leading and trailing spaces
169 * - returns 0 if no quotes found
170 * - returns 1 if quotes are found and put their position
171 * in *quote_start and *quote_end parameters
172 * - return -1 if there are open quotes
175 static int get_quotes_position(const char *source
, int *quote_start
, int *quote_end
)
179 if (source
== NULL
|| quote_start
== NULL
|| quote_end
== NULL
) return -1;
183 /* check if there are quotes surrounding the value */
184 p
+= strspn(p
, " \n"); /* skip white spaces */
187 *quote_start
= p
- source
;
192 LDB_DN_NULL_FAILED(p
);
194 if (*(p
- 1) == '\\')
198 *quote_end
= p
- source
;
208 static char *seek_to_separator(char *string
, const char *separators
)
211 int ret
, qs
, qe
, escaped
;
213 if (string
== NULL
|| separators
== NULL
) return NULL
;
215 p
= strchr(string
, '=');
216 LDB_DN_NULL_FAILED(p
);
220 /* check if there are quotes surrounding the value */
222 ret
= get_quotes_position(p
, &qs
, &qe
);
226 if (ret
== 1) { /* quotes found */
228 p
+= qe
; /* positioning after quotes */
229 p
+= strspn(p
, " \n"); /* skip white spaces after the quote */
231 if (strcspn(p
, separators
) != 0) /* if there are characters between quotes */
232 return NULL
; /* and separators, the dn is invalid */
234 return p
; /* return on the separator */
237 /* no quotes found seek to separators */
241 ret
= strcspn(q
, separators
);
243 if (q
[ret
- 1] == '\\') {
249 if (ret
== 0 && p
== q
) /* no separators ?! bail out */
258 static char *ldb_dn_trim_string(char *string
, const char *edge
)
262 /* seek out edge from start of string */
263 s
= string
+ strspn(string
, edge
);
265 /* backwards skip from end of string */
266 p
= &s
[strlen(s
) - 1];
267 while (p
> s
&& strchr(edge
, *p
)) {
275 /* we choosed to not support multpile valued components */
276 static struct ldb_dn_component
ldb_dn_explode_component(void *mem_ctx
, char *raw_component
)
278 struct ldb_dn_component dc
;
282 memset(&dc
, 0, sizeof(dc
));
284 if (raw_component
== NULL
) {
288 /* find attribute type/value separator */
289 p
= strchr(raw_component
, '=');
290 LDB_DN_NULL_FAILED(p
);
292 *p
++ = '\0'; /* terminate name and point to value */
294 /* copy and trim name in the component */
295 dc
.name
= talloc_strdup(mem_ctx
, ldb_dn_trim_string(raw_component
, " \n"));
299 if (! ldb_valid_attr_name(dc
.name
)) {
303 ret
= get_quotes_position(p
, &qs
, &qe
);
306 case 0: /* no quotes trim the string */
307 p
= ldb_dn_trim_string(p
, " \n");
308 dc
.value
= ldb_dn_unescape_value(mem_ctx
, p
);
311 case 1: /* quotes found get the unquoted string */
314 dc
.value
.length
= strlen(p
);
315 dc
.value
.data
= (uint8_t *)talloc_memdup(mem_ctx
, p
,
316 dc
.value
.length
+ 1);
319 default: /* mismatched quotes ot other error, bail out */
323 if (dc
.value
.length
== 0) {
330 talloc_free(dc
.name
);
335 struct ldb_dn
*ldb_dn_new(void *mem_ctx
)
339 edn
= talloc(mem_ctx
, struct ldb_dn
);
340 LDB_DN_NULL_FAILED(edn
);
342 /* Initially there are no components */
344 edn
->components
= NULL
;
353 explode a DN string into a ldb_dn structure
355 struct ldb_dn
*ldb_dn_explode(void *mem_ctx
, const char *dn
)
357 struct ldb_dn
*edn
; /* the exploded dn */
360 if (dn
== NULL
) return NULL
;
362 /* Allocate a structure to hold the exploded DN */
363 edn
= ldb_dn_new(mem_ctx
);
375 /* Special DNs case */
378 edn
->components
= talloc(edn
, struct ldb_dn_component
);
379 if (edn
->components
== NULL
) goto failed
;
380 edn
->components
[0].name
= talloc_strdup(edn
->components
, LDB_SPECIAL
);
381 if (edn
->components
[0].name
== NULL
) goto failed
;
382 edn
->components
[0].value
.data
= (uint8_t *)talloc_strdup(edn
->components
, dn
);
383 if (edn
->components
[0].value
.data
== NULL
) goto failed
;
384 edn
->components
[0].value
.length
= strlen(dn
);
388 pdn
= p
= talloc_strdup(edn
, dn
);
389 LDB_DN_NULL_FAILED(pdn
);
391 /* get the components */
395 /* terminate the current component and return pointer to the next one */
396 t
= seek_to_separator(p
, ",;");
397 LDB_DN_NULL_FAILED(t
);
399 if (*t
) { /* here there is a separator */
400 *t
= '\0'; /*terminate */
401 t
++; /* a separtor means another component follows */
404 /* allocate space to hold the dn component */
405 edn
->components
= talloc_realloc(edn
, edn
->components
,
406 struct ldb_dn_component
,
408 if (edn
->components
== NULL
)
411 /* store the exploded component in the main structure */
412 edn
->components
[edn
->comp_num
] = ldb_dn_explode_component(edn
, p
);
413 LDB_DN_NULL_FAILED(edn
->components
[edn
->comp_num
].name
);
417 /* jump to the next component if any */
431 struct ldb_dn
*ldb_dn_explode_or_special(void *mem_ctx
, const char *dn
)
433 struct ldb_dn
*edn
; /* the exploded dn */
435 if (dn
== NULL
) return NULL
;
437 if (strncasecmp(dn
, "<GUID=", 6) == 0) {
438 /* this is special DN returned when the
439 * exploded_dn control is used
442 /* Allocate a structure to hold the exploded DN */
443 if (!(edn
= ldb_dn_new(mem_ctx
))) {
448 edn
->components
= talloc(edn
, struct ldb_dn_component
);
449 if (edn
->components
== NULL
) goto failed
;
450 edn
->components
[0].name
= talloc_strdup(edn
->components
, LDB_SPECIAL
);
451 if (edn
->components
[0].name
== NULL
) goto failed
;
452 edn
->components
[0].value
.data
= (uint8_t *)talloc_strdup(edn
->components
, dn
);
453 if (edn
->components
[0].value
.data
== NULL
) goto failed
;
454 edn
->components
[0].value
.length
= strlen(dn
);
459 return ldb_dn_explode(mem_ctx
, dn
);
466 char *ldb_dn_linearize(void *mem_ctx
, const struct ldb_dn
*edn
)
471 if (edn
== NULL
) return NULL
;
474 if (ldb_dn_is_special(edn
)) {
475 dn
= talloc_strdup(mem_ctx
, (char *)edn
->components
[0].value
.data
);
479 dn
= talloc_strdup(mem_ctx
, "");
480 LDB_DN_NULL_FAILED(dn
);
482 for (i
= 0; i
< edn
->comp_num
; i
++) {
483 value
= ldb_dn_escape_value(dn
, edn
->components
[i
].value
);
484 LDB_DN_NULL_FAILED(value
);
487 dn
= talloc_asprintf_append(dn
, "%s=%s", edn
->components
[i
].name
, value
);
489 dn
= talloc_asprintf_append(dn
, ",%s=%s", edn
->components
[i
].name
, value
);
491 LDB_DN_NULL_FAILED(dn
);
503 /* Determine if dn is below base, in the ldap tree. Used for
504 * evaluating a subtree search.
505 * 0 if they match, otherwise non-zero
508 int ldb_dn_compare_base(struct ldb_context
*ldb
,
509 const struct ldb_dn
*base
,
510 const struct ldb_dn
*dn
)
515 if (base
== NULL
|| base
->comp_num
== 0) return 0;
516 if (dn
== NULL
|| dn
->comp_num
== 0) return -1;
518 /* if the base has more componts than the dn, then they differ */
519 if (base
->comp_num
> dn
->comp_num
) {
520 return (dn
->comp_num
- base
->comp_num
);
523 n0
= base
->comp_num
- 1;
524 n1
= dn
->comp_num
- 1;
525 while (n0
>= 0 && n1
>= 0) {
526 const struct ldb_attrib_handler
*h
;
528 /* compare names (attribute names are guaranteed to be ASCII only) */
529 ret
= ldb_attr_cmp(base
->components
[n0
].name
,
530 dn
->components
[n1
].name
);
535 /* names match, compare values */
536 h
= ldb_attrib_handler(ldb
, base
->components
[n0
].name
);
537 ret
= h
->comparison_fn(ldb
, ldb
, &(base
->components
[n0
].value
),
538 &(dn
->components
[n1
].value
));
549 /* compare DNs using casefolding compare functions.
551 If they match, then return 0
554 int ldb_dn_compare(struct ldb_context
*ldb
,
555 const struct ldb_dn
*edn0
,
556 const struct ldb_dn
*edn1
)
558 if (edn0
== NULL
|| edn1
== NULL
) return edn1
- edn0
;
560 if (edn0
->comp_num
!= edn1
->comp_num
)
561 return (edn1
->comp_num
- edn0
->comp_num
);
563 return ldb_dn_compare_base(ldb
, edn0
, edn1
);
566 int ldb_dn_cmp(struct ldb_context
*ldb
, const char *dn0
, const char *dn1
)
572 if (dn0
== NULL
|| dn1
== NULL
) return dn1
- dn0
;
574 edn0
= ldb_dn_explode_casefold(ldb
, ldb
, dn0
);
575 if (edn0
== NULL
) return 1;
577 edn1
= ldb_dn_explode_casefold(ldb
, ldb
, dn1
);
583 ret
= ldb_dn_compare(ldb
, edn0
, edn1
);
592 casefold a dn. We need to casefold the attribute names, and canonicalize
593 attribute values of case insensitive attributes.
595 struct ldb_dn
*ldb_dn_casefold(struct ldb_context
*ldb
, void *mem_ctx
, const struct ldb_dn
*edn
)
600 if (edn
== NULL
) return NULL
;
602 cedn
= ldb_dn_new(mem_ctx
);
607 cedn
->comp_num
= edn
->comp_num
;
608 cedn
->components
= talloc_array(cedn
, struct ldb_dn_component
, edn
->comp_num
);
609 if (!cedn
->components
) {
614 for (i
= 0; i
< edn
->comp_num
; i
++) {
615 struct ldb_dn_component dc
;
616 const struct ldb_attrib_handler
*h
;
618 memset(&dc
, 0, sizeof(dc
));
619 dc
.name
= ldb_attr_casefold(cedn
->components
, edn
->components
[i
].name
);
625 h
= ldb_attrib_handler(ldb
, dc
.name
);
626 ret
= h
->canonicalise_fn(ldb
, cedn
->components
,
627 &(edn
->components
[i
].value
),
634 cedn
->components
[i
] = dc
;
640 struct ldb_dn
*ldb_dn_explode_casefold(struct ldb_context
*ldb
, void *mem_ctx
, const char *dn
)
642 struct ldb_dn
*edn
, *cdn
;
644 if (dn
== NULL
) return NULL
;
646 edn
= ldb_dn_explode(ldb
, dn
);
647 if (edn
== NULL
) return NULL
;
649 cdn
= ldb_dn_casefold(ldb
, mem_ctx
, edn
);
655 char *ldb_dn_linearize_casefold(struct ldb_context
*ldb
, void *mem_ctx
, const struct ldb_dn
*edn
)
660 if (edn
== NULL
) return NULL
;
663 if (ldb_dn_is_special(edn
)) {
664 dn
= talloc_strdup(mem_ctx
, (char *)edn
->components
[0].value
.data
);
668 cdn
= ldb_dn_casefold(ldb
, mem_ctx
, edn
);
669 if (cdn
== NULL
) return NULL
;
671 dn
= ldb_dn_linearize(ldb
, cdn
);
681 static struct ldb_dn_component
ldb_dn_copy_component(void *mem_ctx
, struct ldb_dn_component
*src
)
683 struct ldb_dn_component dst
;
685 memset(&dst
, 0, sizeof(dst
));
691 dst
.value
= ldb_val_dup(mem_ctx
, &(src
->value
));
692 if (dst
.value
.data
== NULL
) {
696 dst
.name
= talloc_strdup(mem_ctx
, src
->name
);
697 if (dst
.name
== NULL
) {
698 talloc_free(dst
.value
.data
);
699 dst
.value
.data
= NULL
;
705 /* Copy a DN but replace the old with the new base DN. */
706 struct ldb_dn
*ldb_dn_copy_rebase(void *mem_ctx
, const struct ldb_dn
*old
, const struct ldb_dn
*old_base
, const struct ldb_dn
*new_base
)
708 struct ldb_dn
*new_dn
;
711 /* Perhaps we don't need to rebase at all? */
712 if (!old_base
|| !new_base
) {
713 return ldb_dn_copy(mem_ctx
, old
);
716 offset
= old
->comp_num
- old_base
->comp_num
;
717 if (!(new_dn
= ldb_dn_copy_partial(mem_ctx
, new_base
,
718 offset
+ new_base
->comp_num
))) {
721 for (i
= 0; i
< offset
; i
++) {
722 new_dn
->components
[i
] = ldb_dn_copy_component(new_dn
->components
, &(old
->components
[i
]));
728 /* copy specified number of elements of a dn into a new one
729 element are copied from top level up to the unique rdn
730 num_el may be greater than dn->comp_num (see ldb_dn_make_child)
732 struct ldb_dn
*ldb_dn_copy_partial(void *mem_ctx
, const struct ldb_dn
*dn
, int num_el
)
734 struct ldb_dn
*newdn
;
737 if (dn
== NULL
) return NULL
;
738 if (num_el
<= 0) return NULL
;
740 newdn
= ldb_dn_new(mem_ctx
);
741 LDB_DN_NULL_FAILED(newdn
);
743 newdn
->comp_num
= num_el
;
744 n
= newdn
->comp_num
- 1;
745 newdn
->components
= talloc_array(newdn
, struct ldb_dn_component
, newdn
->comp_num
);
746 if (newdn
->components
== NULL
) goto failed
;
748 if (dn
->comp_num
== 0) return newdn
;
749 e
= dn
->comp_num
- 1;
751 for (i
= 0; i
< newdn
->comp_num
; i
++) {
752 newdn
->components
[n
- i
] = ldb_dn_copy_component(newdn
->components
,
753 &(dn
->components
[e
- i
]));
766 struct ldb_dn
*ldb_dn_copy(void *mem_ctx
, const struct ldb_dn
*dn
)
768 if (dn
== NULL
) return NULL
;
769 return ldb_dn_copy_partial(mem_ctx
, dn
, dn
->comp_num
);
772 struct ldb_dn
*ldb_dn_get_parent(void *mem_ctx
, const struct ldb_dn
*dn
)
774 if (dn
== NULL
) return NULL
;
775 return ldb_dn_copy_partial(mem_ctx
, dn
, dn
->comp_num
- 1);
778 struct ldb_dn_component
*ldb_dn_build_component(void *mem_ctx
, const char *attr
,
781 struct ldb_dn_component
*dc
;
783 if (attr
== NULL
|| val
== NULL
) return NULL
;
785 dc
= talloc(mem_ctx
, struct ldb_dn_component
);
786 if (dc
== NULL
) return NULL
;
788 dc
->name
= talloc_strdup(dc
, attr
);
789 if (dc
->name
== NULL
) {
794 dc
->value
.data
= (uint8_t *)talloc_strdup(dc
, val
);
795 if (dc
->value
.data
== NULL
) {
800 dc
->value
.length
= strlen(val
);
805 struct ldb_dn
*ldb_dn_build_child(void *mem_ctx
, const char *attr
,
807 const struct ldb_dn
*base
)
809 struct ldb_dn
*newdn
;
810 if (! ldb_valid_attr_name(attr
)) return NULL
;
811 if (value
== NULL
|| value
== '\0') return NULL
;
814 newdn
= ldb_dn_copy_partial(mem_ctx
, base
, base
->comp_num
+ 1);
815 LDB_DN_NULL_FAILED(newdn
);
817 newdn
= ldb_dn_new(mem_ctx
);
818 LDB_DN_NULL_FAILED(newdn
);
821 newdn
->components
= talloc_array(newdn
, struct ldb_dn_component
, newdn
->comp_num
);
822 LDB_DN_NULL_FAILED(newdn
->components
);
825 newdn
->components
[0].name
= talloc_strdup(newdn
->components
, attr
);
826 LDB_DN_NULL_FAILED(newdn
->components
[0].name
);
828 newdn
->components
[0].value
.data
= (uint8_t *)talloc_strdup(newdn
->components
, value
);
829 LDB_DN_NULL_FAILED(newdn
->components
[0].value
.data
);
830 newdn
->components
[0].value
.length
= strlen((char *)newdn
->components
[0].value
.data
);
840 struct ldb_dn
*ldb_dn_compose(void *mem_ctx
, const struct ldb_dn
*dn1
, const struct ldb_dn
*dn2
)
843 struct ldb_dn
*newdn
;
845 if (dn2
== NULL
&& dn1
== NULL
) {
850 newdn
= ldb_dn_new(mem_ctx
);
851 LDB_DN_NULL_FAILED(newdn
);
853 newdn
->comp_num
= dn1
->comp_num
;
854 newdn
->components
= talloc_array(newdn
, struct ldb_dn_component
, newdn
->comp_num
);
855 LDB_DN_NULL_FAILED(newdn
->components
);
857 int comp_num
= dn2
->comp_num
;
858 if (dn1
!= NULL
) comp_num
+= dn1
->comp_num
;
859 newdn
= ldb_dn_copy_partial(mem_ctx
, dn2
, comp_num
);
860 LDB_DN_NULL_FAILED(newdn
);
867 for (i
= 0; i
< dn1
->comp_num
; i
++) {
868 newdn
->components
[i
] = ldb_dn_copy_component(newdn
->components
,
869 &(dn1
->components
[i
]));
870 if (newdn
->components
[i
].value
.data
== NULL
) {
882 struct ldb_dn
*ldb_dn_string_compose(void *mem_ctx
, const struct ldb_dn
*base
, const char *child_fmt
, ...)
884 struct ldb_dn
*dn
, *dn1
;
888 if (child_fmt
== NULL
) return NULL
;
890 va_start(ap
, child_fmt
);
891 child_str
= talloc_vasprintf(mem_ctx
, child_fmt
, ap
);
894 if (child_str
== NULL
) return NULL
;
896 dn1
= ldb_dn_explode(mem_ctx
, child_str
);
897 dn
= ldb_dn_compose(mem_ctx
, dn1
, base
);
899 talloc_free(child_str
);
905 /* Create a 'canonical name' string from a DN:
907 ie dc=samba,dc=org -> samba.org/
908 uid=administrator,ou=users,dc=samba,dc=org = samba.org/users/administrator
910 There are two formats, the EX format has the last / replaced with a newline (\n).
913 static char *ldb_dn_canonical(void *mem_ctx
, const struct ldb_dn
*dn
, int ex_format
) {
915 char *cracked
= NULL
;
917 /* Walk backwards down the DN, grabbing 'dc' components at first */
918 for (i
= dn
->comp_num
- 1 ; i
>= 0; i
--) {
919 if (ldb_attr_cmp(dn
->components
[i
].name
, "dc") != 0) {
923 cracked
= talloc_asprintf(mem_ctx
, "%s.%s",
924 ldb_dn_escape_value(mem_ctx
, dn
->components
[i
].value
),
927 cracked
= ldb_dn_escape_value(mem_ctx
, dn
->components
[i
].value
);
934 /* Only domain components? Finish here */
937 cracked
= talloc_asprintf(mem_ctx
, "%s\n", cracked
);
939 cracked
= talloc_asprintf(mem_ctx
, "%s/", cracked
);
944 /* Now walk backwards appending remaining components */
946 cracked
= talloc_asprintf(mem_ctx
, "%s/%s", cracked
,
947 ldb_dn_escape_value(mem_ctx
, dn
->components
[i
].value
));
953 /* Last one, possibly a newline for the 'ex' format */
955 cracked
= talloc_asprintf(mem_ctx
, "%s\n%s", cracked
,
956 ldb_dn_escape_value(mem_ctx
, dn
->components
[i
].value
));
958 cracked
= talloc_asprintf(mem_ctx
, "%s/%s", cracked
,
959 ldb_dn_escape_value(mem_ctx
, dn
->components
[i
].value
));
964 /* Wrapper functions for the above, for the two different string formats */
965 char *ldb_dn_canonical_string(void *mem_ctx
, const struct ldb_dn
*dn
) {
966 return ldb_dn_canonical(mem_ctx
, dn
, 0);
970 char *ldb_dn_canonical_ex_string(void *mem_ctx
, const struct ldb_dn
*dn
) {
971 return ldb_dn_canonical(mem_ctx
, dn
, 1);
974 int ldb_dn_get_comp_num(const struct ldb_dn
*dn
)
979 const char *ldb_dn_get_component_name(const struct ldb_dn
*dn
, unsigned int num
)
981 if (num
>= dn
->comp_num
) return NULL
;
982 return dn
->components
[num
].name
;
985 const struct ldb_val
*ldb_dn_get_component_val(const struct ldb_dn
*dn
, unsigned int num
)
987 if (num
>= dn
->comp_num
) return NULL
;
988 return &dn
->components
[num
].value
;
991 const char *ldb_dn_get_rdn_name(const struct ldb_dn
*dn
) {
992 if (dn
->comp_num
== 0) return NULL
;
993 return dn
->components
[0].name
;
996 const struct ldb_val
*ldb_dn_get_rdn_val(const struct ldb_dn
*dn
) {
997 if (dn
->comp_num
== 0) return NULL
;
998 return &dn
->components
[0].value
;
1001 int ldb_dn_set_component(struct ldb_dn
*dn
, int num
, const char *name
, const struct ldb_val val
)
1006 if (num
>= dn
->comp_num
) {
1007 return LDB_ERR_OTHER
;
1010 n
= talloc_strdup(dn
, name
);
1012 return LDB_ERR_OTHER
;
1015 v
.length
= val
.length
;
1016 v
.data
= (uint8_t *)talloc_memdup(dn
, val
.data
, v
.length
+1);
1018 return LDB_ERR_OTHER
;
1021 talloc_free(dn
->components
[num
].name
);
1022 talloc_free(dn
->components
[num
].value
.data
);
1023 dn
->components
[num
].name
= n
;
1024 dn
->components
[num
].value
= v
;