Line data Source code
1 : /*
2 : ldb database library
3 :
4 : Copyright (C) Simo Sorce 2005
5 :
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
9 :
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.
14 :
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.
19 :
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/>.
22 : */
23 :
24 : /*
25 : * Name: ldb
26 : *
27 : * Component: ldb dn creation and manipulation utility functions
28 : *
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
32 : *
33 : * Author: Simo Sorce
34 : */
35 :
36 : #include "ldb_private.h"
37 : #include <ctype.h>
38 :
39 : #define LDB_DN_NULL_FAILED(x) if (!(x)) goto failed
40 :
41 : #define LDB_FREE(x) do { talloc_free(x); x = NULL; } while(0)
42 :
43 : /**
44 : internal ldb exploded dn structures
45 : */
46 : struct ldb_dn_component {
47 :
48 : char *name;
49 : struct ldb_val value;
50 :
51 : char *cf_name;
52 : struct ldb_val cf_value;
53 : };
54 :
55 : struct ldb_dn_ext_component {
56 :
57 : const char *name;
58 : struct ldb_val value;
59 : };
60 :
61 : struct ldb_dn {
62 :
63 : struct ldb_context *ldb;
64 :
65 : /* Special DNs are always linearized */
66 : bool special;
67 : bool invalid;
68 :
69 : bool valid_case;
70 :
71 : char *linearized;
72 : char *ext_linearized;
73 : char *casefold;
74 :
75 : unsigned int comp_num;
76 : struct ldb_dn_component *components;
77 :
78 : unsigned int ext_comp_num;
79 : struct ldb_dn_ext_component *ext_components;
80 : };
81 :
82 : /* it is helpful to be able to break on this in gdb */
83 20200 : static void ldb_dn_mark_invalid(struct ldb_dn *dn)
84 : {
85 20200 : dn->invalid = true;
86 20183 : }
87 :
88 : /* strdn may be NULL */
89 926328327 : struct ldb_dn *ldb_dn_from_ldb_val(TALLOC_CTX *mem_ctx,
90 : struct ldb_context *ldb,
91 : const struct ldb_val *strdn)
92 : {
93 25063742 : struct ldb_dn *dn;
94 :
95 926328327 : if (ldb == NULL || strdn == NULL) {
96 0 : return NULL;
97 : }
98 926328327 : if (strdn->data
99 896104877 : && (strnlen((const char*)strdn->data, strdn->length) != strdn->length)) {
100 : /* The RDN must not contain a character with value 0x0 */
101 0 : return NULL;
102 : }
103 :
104 926328324 : dn = talloc_zero(mem_ctx, struct ldb_dn);
105 926328324 : LDB_DN_NULL_FAILED(dn);
106 :
107 926328324 : dn->ldb = talloc_get_type(ldb, struct ldb_context);
108 926328324 : if (dn->ldb == NULL) {
109 : /* the caller probably got the arguments to
110 : ldb_dn_new() mixed up */
111 0 : talloc_free(dn);
112 0 : return NULL;
113 : }
114 :
115 1797075838 : if (strdn->data && strdn->length) {
116 895202875 : const char *data = (const char *)strdn->data;
117 895202875 : size_t length = strdn->length;
118 :
119 895202875 : if (data[0] == '@') {
120 418665034 : dn->special = true;
121 : }
122 895202875 : dn->ext_linearized = talloc_strndup(dn, data, length);
123 895202875 : LDB_DN_NULL_FAILED(dn->ext_linearized);
124 :
125 895202875 : if (data[0] == '<') {
126 43229396 : const char *p_save, *p = dn->ext_linearized;
127 2258153 : do {
128 124007500 : p_save = p;
129 124007500 : p = strstr(p, ">;");
130 124007500 : if (p) {
131 79697594 : p = p + 2;
132 : }
133 124007500 : } while (p);
134 :
135 44309906 : if (p_save == dn->ext_linearized) {
136 8488600 : dn->linearized = talloc_strdup(dn, "");
137 : } else {
138 35821306 : dn->linearized = talloc_strdup(dn, p_save);
139 : }
140 44309906 : LDB_DN_NULL_FAILED(dn->linearized);
141 : } else {
142 850892969 : dn->linearized = dn->ext_linearized;
143 850892969 : dn->ext_linearized = NULL;
144 : }
145 : } else {
146 31125449 : dn->linearized = talloc_strdup(dn, "");
147 31125449 : LDB_DN_NULL_FAILED(dn->linearized);
148 : }
149 :
150 901264585 : return dn;
151 :
152 0 : failed:
153 0 : talloc_free(dn);
154 0 : return NULL;
155 : }
156 :
157 : /* strdn may be NULL */
158 350031429 : struct ldb_dn *ldb_dn_new(TALLOC_CTX *mem_ctx,
159 : struct ldb_context *ldb,
160 : const char *strdn)
161 : {
162 11244759 : struct ldb_val blob;
163 350031429 : blob.data = discard_const_p(uint8_t, strdn);
164 350031429 : blob.length = strdn ? strlen(strdn) : 0;
165 350031429 : return ldb_dn_from_ldb_val(mem_ctx, ldb, &blob);
166 : }
167 :
168 204991571 : struct ldb_dn *ldb_dn_new_fmt(TALLOC_CTX *mem_ctx,
169 : struct ldb_context *ldb,
170 : const char *new_fmt, ...)
171 : {
172 6569656 : char *strdn;
173 6569656 : va_list ap;
174 :
175 204991571 : if (! ldb) return NULL;
176 :
177 204991571 : va_start(ap, new_fmt);
178 204991571 : strdn = talloc_vasprintf(mem_ctx, new_fmt, ap);
179 204991571 : va_end(ap);
180 :
181 204991571 : if (strdn) {
182 204991571 : struct ldb_dn *dn = ldb_dn_new(mem_ctx, ldb, strdn);
183 204991571 : talloc_free(strdn);
184 204991571 : return dn;
185 : }
186 :
187 0 : return NULL;
188 : }
189 :
190 : /* see RFC2253 section 2.4 */
191 233978717 : static int ldb_dn_escape_internal(char *dst, const char *src, int len)
192 : {
193 14283900 : char c;
194 14283900 : char *d;
195 14283900 : int i;
196 233978717 : d = dst;
197 :
198 2253058871 : for (i = 0; i < len; i++){
199 2019080154 : c = src[i];
200 2019080154 : switch (c) {
201 15024375 : case ' ':
202 15024375 : if (i == 0 || i == len - 1) {
203 : /* if at the beginning or end
204 : * of the string then escape */
205 0 : *d++ = '\\';
206 0 : *d++ = c;
207 : } else {
208 : /* otherwise don't escape */
209 15024375 : *d++ = c;
210 : }
211 14093206 : break;
212 :
213 37089 : case '#':
214 : /* despite the RFC, windows escapes a #
215 : anywhere in the string */
216 : case ',':
217 : case '+':
218 : case '"':
219 : case '\\':
220 : case '<':
221 : case '>':
222 : case '?':
223 : /* these must be escaped using \c form */
224 37089 : *d++ = '\\';
225 37089 : *d++ = c;
226 37089 : break;
227 :
228 1842718 : case ';':
229 : case '\r':
230 : case '\n':
231 : case '=':
232 : case '\0': {
233 : /* any others get \XX form */
234 925 : unsigned char v;
235 1842718 : const char *hexbytes = "0123456789ABCDEF";
236 1842718 : v = (const unsigned char)c;
237 1842718 : *d++ = '\\';
238 1842718 : *d++ = hexbytes[v>>4];
239 1842718 : *d++ = hexbytes[v&0xF];
240 1842718 : break;
241 : }
242 2002175972 : default:
243 2002175972 : *d++ = c;
244 : }
245 : }
246 :
247 : /* return the length of the resulting string */
248 233978717 : return (d - dst);
249 : }
250 :
251 15745150 : char *ldb_dn_escape_value(TALLOC_CTX *mem_ctx, struct ldb_val value)
252 : {
253 952973 : char *dst;
254 952973 : size_t len;
255 15745150 : if (!value.length)
256 2 : return NULL;
257 :
258 : /* allocate destination string, it will be at most 3 times the source */
259 15745148 : dst = talloc_array(mem_ctx, char, value.length * 3 + 1);
260 15745148 : if ( ! dst) {
261 0 : talloc_free(dst);
262 0 : return NULL;
263 : }
264 :
265 15745148 : len = ldb_dn_escape_internal(dst, (const char *)value.data, value.length);
266 :
267 15745148 : dst = talloc_realloc(mem_ctx, dst, char, len + 1);
268 15745148 : if ( ! dst) {
269 0 : talloc_free(dst);
270 0 : return NULL;
271 : }
272 15745148 : dst[len] = '\0';
273 15745148 : return dst;
274 : }
275 :
276 : /*
277 : explode a DN string into a ldb_dn structure
278 : based on RFC4514 except that we don't support multiple valued RDNs
279 :
280 : TODO: according to MS-ADTS:3.1.1.5.2 Naming Constraints
281 : DN must be compliant with RFC2253
282 : */
283 1242933995 : static bool ldb_dn_explode(struct ldb_dn *dn)
284 : {
285 1242933995 : char *p, *ex_name = NULL, *ex_value = NULL, *data, *d, *dt, *t;
286 1242933995 : bool trim = true;
287 1242933995 : bool in_extended = true;
288 1242933995 : bool in_ex_name = false;
289 1242933995 : bool in_ex_value = false;
290 1242933995 : bool in_attr = false;
291 1242933995 : bool in_value = false;
292 1242933995 : bool in_quote = false;
293 1242933995 : bool is_oid = false;
294 1242933995 : bool escape = false;
295 36018072 : unsigned int x;
296 1242933995 : size_t l = 0;
297 36018072 : int ret;
298 36018072 : char *parse_dn;
299 36018072 : bool is_index;
300 :
301 1242933995 : if (dn == NULL || dn->invalid) {
302 723 : return false;
303 : }
304 :
305 1242933272 : if (dn->components != NULL) {
306 652603940 : return true;
307 : }
308 :
309 567953406 : if (dn->ext_linearized != NULL) {
310 39667400 : parse_dn = dn->ext_linearized;
311 : } else {
312 527425874 : parse_dn = dn->linearized;
313 : }
314 :
315 567953406 : if (parse_dn == NULL) {
316 0 : return false;
317 : }
318 :
319 567953406 : is_index = (strncmp(parse_dn, "DN=@INDEX:", 10) == 0);
320 :
321 : /* Empty DNs */
322 567953406 : if (parse_dn[0] == '\0') {
323 30526198 : return true;
324 : }
325 :
326 : /* Special DNs case */
327 536491404 : if (dn->special) {
328 260328540 : return true;
329 : }
330 :
331 268497463 : LDB_FREE(dn->ext_components);
332 268497463 : dn->ext_comp_num = 0;
333 268497463 : dn->comp_num = 0;
334 :
335 : /* in the common case we have 3 or more components */
336 : /* make sure all components are zeroed, other functions depend on it */
337 268497463 : dn->components = talloc_zero_array(dn, struct ldb_dn_component, 3);
338 268497463 : if (dn->components == NULL) {
339 0 : return false;
340 : }
341 :
342 : /* Components data space is allocated here once */
343 268497463 : data = talloc_array(dn->components, char, strlen(parse_dn) + 1);
344 268497463 : if (data == NULL) {
345 0 : goto failed;
346 : }
347 :
348 263456522 : p = parse_dn;
349 263456522 : t = NULL;
350 263456522 : d = dt = data;
351 :
352 26212756878 : while (*p) {
353 25952749245 : if (in_extended) {
354 :
355 3311194081 : if (!in_ex_name && !in_ex_value) {
356 :
357 339770690 : if (p[0] == '<') {
358 79761843 : p++;
359 79761843 : ex_name = d;
360 79761843 : in_ex_name = true;
361 79761843 : continue;
362 : } else {
363 260008847 : in_extended = false;
364 260008847 : in_attr = true;
365 260008847 : dt = d;
366 :
367 260008847 : continue;
368 : }
369 : }
370 :
371 2971423391 : if (in_ex_name && *p == '=') {
372 79761839 : *d++ = '\0';
373 79761839 : p++;
374 79761839 : ex_value = d;
375 79761839 : in_ex_name = false;
376 79761839 : in_ex_value = true;
377 79761839 : continue;
378 : }
379 :
380 2891661552 : if (in_ex_value && *p == '>') {
381 79761839 : struct ldb_dn_ext_component *ext_comp = NULL;
382 1158612 : const struct ldb_dn_extended_syntax *ext_syntax;
383 79761839 : struct ldb_val ex_val = {
384 : .data = (uint8_t *)ex_value,
385 79761839 : .length = d - ex_value
386 : };
387 :
388 79761839 : *d++ = '\0';
389 79761839 : p++;
390 79761839 : in_ex_value = false;
391 :
392 : /* Process name and ex_value */
393 :
394 79761839 : ext_comp = talloc_realloc(
395 : dn,
396 : dn->ext_components,
397 : struct ldb_dn_ext_component,
398 : dn->ext_comp_num + 1);
399 :
400 79761839 : if (ext_comp == NULL) {
401 : /* ouch ! */
402 450 : goto failed;
403 : }
404 :
405 79761839 : dn->ext_components = ext_comp;
406 :
407 79761839 : ext_syntax = ldb_dn_extended_syntax_by_name(dn->ldb, ex_name);
408 79761839 : if (ext_syntax == NULL) {
409 : /* We don't know about this type of extended DN */
410 9 : goto failed;
411 : }
412 :
413 79761830 : dn->ext_components[dn->ext_comp_num].name = ext_syntax->name;
414 79761830 : ret = ext_syntax->read_fn(dn->ldb, dn->ext_components,
415 78603219 : &ex_val, &dn->ext_components[dn->ext_comp_num].value);
416 79761830 : if (ret != LDB_SUCCESS) {
417 441 : ldb_dn_mark_invalid(dn);
418 441 : goto failed;
419 : }
420 :
421 79761389 : dn->ext_comp_num++;
422 :
423 79761389 : if (*p == '\0') {
424 : /* We have reached the end (extended component only)! */
425 8488162 : talloc_free(data);
426 8488162 : return true;
427 :
428 71273227 : } else if (*p == ';') {
429 71273227 : p++;
430 71273227 : continue;
431 : } else {
432 0 : ldb_dn_mark_invalid(dn);
433 0 : goto failed;
434 : }
435 : }
436 :
437 2811899713 : *d++ = *p++;
438 2811899713 : continue;
439 : }
440 22641555164 : if (in_attr) {
441 4504706212 : if (trim) {
442 1522979903 : if (*p == ' ') {
443 34066664 : p++;
444 34066664 : continue;
445 : }
446 :
447 : /* first char */
448 1488913239 : trim = false;
449 :
450 1488913239 : if (!isascii(*p)) {
451 : /* attr names must be ascii only */
452 0 : ldb_dn_mark_invalid(dn);
453 0 : goto failed;
454 : }
455 :
456 1488913239 : if (isdigit(*p)) {
457 0 : is_oid = true;
458 : } else
459 1488913239 : if ( ! isalpha(*p)) {
460 : /* not a digit nor an alpha,
461 : * invalid attribute name */
462 7 : ldb_dn_mark_invalid(dn);
463 7 : goto failed;
464 : }
465 :
466 : /* Copy this character across from parse_dn,
467 : * now we have trimmed out spaces */
468 1488913232 : *d++ = *p++;
469 1488913232 : continue;
470 : }
471 :
472 2981726309 : if (*p == ' ') {
473 96 : p++;
474 : /* valid only if we are at the end */
475 96 : trim = true;
476 96 : continue;
477 : }
478 :
479 2981726213 : if (*p == '=') {
480 : /* attribute terminated */
481 1488893389 : in_attr = false;
482 1488893389 : in_value = true;
483 1488893389 : trim = true;
484 1488893389 : l = 0;
485 :
486 : /* Terminate this string in d
487 : * (which is a copy of parse_dn
488 : * with spaces trimmed) */
489 1488893389 : *d++ = '\0';
490 1488893389 : dn->components[dn->comp_num].name = talloc_strdup(dn->components, dt);
491 1488893389 : if (dn->components[dn->comp_num].name == NULL) {
492 : /* ouch */
493 0 : goto failed;
494 : }
495 :
496 1488893389 : dt = d;
497 :
498 1488893389 : p++;
499 1488893389 : continue;
500 : }
501 :
502 1492832824 : if (!isascii(*p)) {
503 : /* attr names must be ascii only */
504 0 : ldb_dn_mark_invalid(dn);
505 0 : goto failed;
506 : }
507 :
508 1492832824 : if (is_oid && ( ! (isdigit(*p) || (*p == '.')))) {
509 : /* not a digit nor a dot,
510 : * invalid attribute oid */
511 0 : ldb_dn_mark_invalid(dn);
512 0 : goto failed;
513 : } else
514 1492832824 : if ( ! (isalpha(*p) || isdigit(*p) || (*p == '-'))) {
515 : /* not ALPHA, DIGIT or HYPHEN */
516 1211 : ldb_dn_mark_invalid(dn);
517 1211 : goto failed;
518 : }
519 :
520 1492831613 : *d++ = *p++;
521 1492831613 : continue;
522 : }
523 :
524 18136848952 : if (in_value) {
525 18136848952 : if (in_quote) {
526 0 : if (*p == '\"') {
527 0 : if (p[-1] != '\\') {
528 0 : p++;
529 0 : in_quote = false;
530 0 : continue;
531 : }
532 : }
533 0 : *d++ = *p++;
534 0 : l++;
535 0 : continue;
536 : }
537 :
538 18136848952 : if (trim) {
539 1488893412 : if (*p == ' ') {
540 31 : p++;
541 31 : continue;
542 : }
543 :
544 : /* first char */
545 1488893381 : trim = false;
546 :
547 1488893381 : if (*p == '\"') {
548 0 : in_quote = true;
549 0 : p++;
550 0 : continue;
551 : }
552 : }
553 :
554 18136848921 : switch (*p) {
555 :
556 : /* TODO: support ber encoded values
557 : case '#':
558 : */
559 :
560 1228931153 : case ',':
561 1228931153 : if (escape) {
562 26852 : *d++ = *p++;
563 26852 : l++;
564 26852 : escape = false;
565 26852 : continue;
566 : }
567 : /* ok found value terminator */
568 :
569 1228904301 : if (t != NULL) {
570 : /* trim back */
571 44 : d -= (p - t);
572 44 : l -= (p - t);
573 44 : t = NULL;
574 : }
575 :
576 1228904301 : in_attr = true;
577 1228904301 : in_value = false;
578 1228904301 : trim = true;
579 :
580 1228904301 : p++;
581 1228904301 : *d++ = '\0';
582 :
583 : /*
584 : * This talloc_memdup() is OK with the
585 : * +1 because *d has been set to '\0'
586 : * just above
587 : */
588 2457808602 : dn->components[dn->comp_num].value.data = \
589 1228904301 : (uint8_t *)talloc_memdup(dn->components, dt, l + 1);
590 1228904301 : dn->components[dn->comp_num].value.length = l;
591 1228904301 : if (dn->components[dn->comp_num].value.data == NULL) {
592 : /* ouch ! */
593 0 : goto failed;
594 : }
595 1228904301 : talloc_set_name_const(dn->components[dn->comp_num].value.data,
596 1204734684 : (const char *)dn->components[dn->comp_num].value.data);
597 :
598 1228904301 : dt = d;
599 :
600 1228904301 : dn->comp_num++;
601 1228904301 : if (dn->comp_num > 2) {
602 717632497 : dn->components = talloc_realloc(dn,
603 : dn->components,
604 : struct ldb_dn_component,
605 : dn->comp_num + 1);
606 717632497 : if (dn->components == NULL) {
607 : /* ouch ! */
608 0 : goto failed;
609 : }
610 : /* make sure all components are zeroed, other functions depend on this */
611 717632497 : memset(&dn->components[dn->comp_num], '\0', sizeof(struct ldb_dn_component));
612 : }
613 :
614 1228904301 : continue;
615 :
616 0 : case '+':
617 : case '=':
618 : /* to main compatibility with earlier
619 : versions of ldb indexing, we have to
620 : accept the base64 encoded binary index
621 : values, which contain a '+' or '='
622 : which should normally be escaped */
623 0 : if (is_index) {
624 0 : if (t != NULL) {
625 0 : t = NULL;
626 : }
627 0 : *d++ = *p++;
628 0 : l++;
629 0 : break;
630 : }
631 :
632 0 : FALL_THROUGH;
633 : case '\"':
634 : case '<':
635 : case '>':
636 : case ';':
637 : /* a string with not escaped specials is invalid (tested) */
638 0 : if (!escape) {
639 0 : ldb_dn_mark_invalid(dn);
640 0 : goto failed;
641 : }
642 0 : escape = false;
643 :
644 0 : *d++ = *p++;
645 0 : l++;
646 :
647 0 : if (t != NULL) {
648 0 : t = NULL;
649 : }
650 0 : break;
651 :
652 85780611 : case '\\':
653 85780611 : if (!escape) {
654 85769811 : escape = true;
655 85769811 : p++;
656 85769811 : continue;
657 : }
658 10800 : escape = false;
659 :
660 10800 : *d++ = *p++;
661 10800 : l++;
662 :
663 10800 : if (t != NULL) {
664 0 : t = NULL;
665 : }
666 10800 : break;
667 :
668 16822137157 : default:
669 16822137157 : if (escape) {
670 85732159 : if (isxdigit(p[0]) && isxdigit(p[1])) {
671 85732159 : if (sscanf(p, "%02x", &x) != 1) {
672 : /* invalid escaping sequence */
673 0 : ldb_dn_mark_invalid(dn);
674 0 : goto failed;
675 : }
676 85732159 : p += 2;
677 85732159 : *d++ = (unsigned char)x;
678 : } else {
679 0 : *d++ = *p++;
680 : }
681 :
682 85732159 : escape = false;
683 85732159 : l++;
684 85732159 : if (t != NULL) {
685 0 : t = NULL;
686 : }
687 85731251 : break;
688 : }
689 :
690 16736404998 : if (*p == ' ') {
691 125134254 : if (t == NULL) {
692 125130161 : t = p;
693 : }
694 : } else {
695 16356772785 : if (t != NULL) {
696 124027017 : t = NULL;
697 : }
698 : }
699 :
700 16736404998 : *d++ = *p++;
701 16736404998 : l++;
702 :
703 16736404998 : break;
704 : }
705 :
706 : }
707 : }
708 :
709 260007633 : if (in_attr || in_quote) {
710 : /* invalid dn */
711 18541 : ldb_dn_mark_invalid(dn);
712 18541 : goto failed;
713 : }
714 :
715 259989092 : if (in_value) {
716 : /* save last element */
717 259989088 : if (t != NULL) {
718 : /* trim back */
719 190 : d -= (p - t);
720 190 : l -= (p - t);
721 : }
722 :
723 259989088 : *d++ = '\0';
724 : /*
725 : * This talloc_memdup() is OK with the
726 : * +1 because *d has been set to '\0'
727 : * just above.
728 : */
729 259989088 : dn->components[dn->comp_num].value.length = l;
730 519978176 : dn->components[dn->comp_num].value.data =
731 259989088 : (uint8_t *)talloc_memdup(dn->components, dt, l + 1);
732 259989088 : if (dn->components[dn->comp_num].value.data == NULL) {
733 : /* ouch */
734 0 : goto failed;
735 : }
736 259989088 : talloc_set_name_const(dn->components[dn->comp_num].value.data,
737 255177162 : (const char *)dn->components[dn->comp_num].value.data);
738 :
739 259989088 : dn->comp_num++;
740 : }
741 259989092 : talloc_free(data);
742 259989092 : return true;
743 :
744 20209 : failed:
745 20209 : LDB_FREE(dn->components); /* "data" is implicitly free'd */
746 20209 : dn->comp_num = 0;
747 20209 : LDB_FREE(dn->ext_components);
748 20209 : dn->ext_comp_num = 0;
749 :
750 20209 : return false;
751 : }
752 :
753 1145312070 : bool ldb_dn_validate(struct ldb_dn *dn)
754 : {
755 1145312070 : return ldb_dn_explode(dn);
756 : }
757 :
758 777616800 : const char *ldb_dn_get_linearized(struct ldb_dn *dn)
759 : {
760 24949772 : unsigned int i;
761 24949772 : size_t len;
762 24949772 : char *d, *n;
763 :
764 777616800 : if ( ! dn || ( dn->invalid)) return NULL;
765 :
766 777616399 : if (dn->linearized) return dn->linearized;
767 :
768 10846794 : if ( ! dn->components) {
769 0 : ldb_dn_mark_invalid(dn);
770 0 : return NULL;
771 : }
772 :
773 10846794 : if (dn->comp_num == 0) {
774 525758 : dn->linearized = talloc_strdup(dn, "");
775 525758 : if ( ! dn->linearized) return NULL;
776 525758 : return dn->linearized;
777 : }
778 :
779 : /* calculate maximum possible length of DN */
780 67115559 : for (len = 0, i = 0; i < dn->comp_num; i++) {
781 : /* name len */
782 56794523 : len += strlen(dn->components[i].name);
783 : /* max escaped data len */
784 56794523 : len += (dn->components[i].value.length * 3);
785 56794523 : len += 2; /* '=' and ',' */
786 : }
787 10321036 : dn->linearized = talloc_array(dn, char, len);
788 10321036 : if ( ! dn->linearized) return NULL;
789 :
790 9701575 : d = dn->linearized;
791 :
792 67115559 : for (i = 0; i < dn->comp_num; i++) {
793 :
794 : /* copy the name */
795 56794523 : n = dn->components[i].name;
796 170384148 : while (*n) *d++ = *n++;
797 :
798 56794523 : *d++ = '=';
799 :
800 : /* and the value */
801 113589046 : d += ldb_dn_escape_internal( d,
802 56794523 : (char *)dn->components[i].value.data,
803 56794523 : dn->components[i].value.length);
804 56794523 : *d++ = ',';
805 : }
806 :
807 10321036 : *(--d) = '\0';
808 :
809 : /* don't waste more memory than necessary */
810 10321036 : dn->linearized = talloc_realloc(dn, dn->linearized,
811 : char, (d - dn->linearized + 1));
812 :
813 10321036 : return dn->linearized;
814 : }
815 :
816 49411517 : static int ldb_dn_extended_component_compare(const void *p1, const void *p2)
817 : {
818 49411517 : const struct ldb_dn_ext_component *ec1 = (const struct ldb_dn_ext_component *)p1;
819 49411517 : const struct ldb_dn_ext_component *ec2 = (const struct ldb_dn_ext_component *)p2;
820 49411517 : return strcmp(ec1->name, ec2->name);
821 : }
822 :
823 28694379 : char *ldb_dn_get_extended_linearized(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, int mode)
824 : {
825 28694379 : const char *linearized = ldb_dn_get_linearized(dn);
826 28694379 : char *p = NULL;
827 619957 : unsigned int i;
828 :
829 28694379 : if (!linearized) {
830 96 : return NULL;
831 : }
832 :
833 28694283 : if (!ldb_dn_has_extended(dn)) {
834 1553505 : return talloc_strdup(mem_ctx, linearized);
835 : }
836 :
837 27140778 : if (!ldb_dn_validate(dn)) {
838 3 : return NULL;
839 : }
840 :
841 : /* sort the extended components by name. The idea is to make
842 : * the resulting DNs consistent, plus to ensure that we put
843 : * 'DELETED' first, so it can be very quickly recognised
844 : */
845 27140775 : TYPESAFE_QSORT(dn->ext_components, dn->ext_comp_num,
846 : ldb_dn_extended_component_compare);
847 :
848 79207883 : for (i = 0; i < dn->ext_comp_num; i++) {
849 626756 : const struct ldb_dn_extended_syntax *ext_syntax;
850 52067108 : const char *name = dn->ext_components[i].name;
851 52067108 : struct ldb_val ec_val = dn->ext_components[i].value;
852 626756 : struct ldb_val val;
853 626756 : int ret;
854 :
855 52067108 : ext_syntax = ldb_dn_extended_syntax_by_name(dn->ldb, name);
856 52067108 : if (!ext_syntax) {
857 0 : return NULL;
858 : }
859 :
860 52067108 : if (mode == 1) {
861 41798888 : ret = ext_syntax->write_clear_fn(dn->ldb, mem_ctx,
862 : &ec_val, &val);
863 10268220 : } else if (mode == 0) {
864 10268220 : ret = ext_syntax->write_hex_fn(dn->ldb, mem_ctx,
865 : &ec_val, &val);
866 : } else {
867 0 : ret = -1;
868 : }
869 :
870 52067108 : if (ret != LDB_SUCCESS) {
871 0 : return NULL;
872 : }
873 :
874 52067108 : if (i == 0) {
875 27140771 : p = talloc_asprintf(mem_ctx, "<%s=%.*s>",
876 : name,
877 27140771 : (int)val.length,
878 : val.data);
879 : } else {
880 24926337 : talloc_asprintf_addbuf(&p, ";<%s=%.*s>",
881 : name,
882 24926337 : (int)val.length,
883 : val.data);
884 : }
885 :
886 52067108 : talloc_free(val.data);
887 : }
888 :
889 27140775 : if (dn->ext_comp_num && *linearized) {
890 25901732 : talloc_asprintf_addbuf(&p, ";%s", linearized);
891 : }
892 :
893 27140775 : if (!p) {
894 4 : return NULL;
895 : }
896 :
897 26629725 : return p;
898 : }
899 :
900 : /*
901 : filter out all but an acceptable list of extended DN components
902 : */
903 17437185 : void ldb_dn_extended_filter(struct ldb_dn *dn, const char * const *accept_list)
904 : {
905 203042 : unsigned int i;
906 59866271 : for (i=0; i<dn->ext_comp_num; i++) {
907 42429086 : if (!ldb_attr_in_list(accept_list, dn->ext_components[i].name)) {
908 19032937 : ARRAY_DEL_ELEMENT(
909 3052 : dn->ext_components, i, dn->ext_comp_num);
910 19032937 : dn->ext_comp_num--;
911 19032937 : i--;
912 : }
913 : }
914 17437185 : LDB_FREE(dn->ext_linearized);
915 17437185 : }
916 :
917 :
918 172563021 : char *ldb_dn_alloc_linearized(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
919 : {
920 172563021 : return talloc_strdup(mem_ctx, ldb_dn_get_linearized(dn));
921 : }
922 :
923 : /*
924 : casefold a dn. We need to casefold the attribute names, and canonicalize
925 : attribute values of case insensitive attributes.
926 : */
927 :
928 278727964 : static bool ldb_dn_casefold_internal(struct ldb_dn *dn)
929 : {
930 4257099 : unsigned int i;
931 4257099 : int ret;
932 :
933 278727964 : if ( ! dn || dn->invalid) return false;
934 :
935 278727964 : if (dn->valid_case) return true;
936 :
937 139625165 : if (( ! dn->components) && ( ! ldb_dn_explode(dn))) {
938 892 : return false;
939 : }
940 :
941 883257983 : for (i = 0; i < dn->comp_num; i++) {
942 12193343 : const struct ldb_schema_attribute *a;
943 :
944 1487267420 : dn->components[i].cf_name =
945 743633710 : ldb_attr_casefold(dn->components,
946 743633710 : dn->components[i].name);
947 743633710 : if (!dn->components[i].cf_name) {
948 0 : goto failed;
949 : }
950 :
951 743633710 : a = ldb_schema_attribute_by_name(dn->ldb,
952 731440367 : dn->components[i].cf_name);
953 :
954 755827053 : ret = a->syntax->canonicalise_fn(dn->ldb, dn->components,
955 743633710 : &(dn->components[i].value),
956 743633710 : &(dn->components[i].cf_value));
957 743633710 : if (ret != 0) {
958 0 : goto failed;
959 : }
960 : }
961 :
962 139624273 : dn->valid_case = true;
963 :
964 139624273 : return true;
965 :
966 0 : failed:
967 0 : for (i = 0; i < dn->comp_num; i++) {
968 0 : LDB_FREE(dn->components[i].cf_name);
969 0 : LDB_FREE(dn->components[i].cf_value.data);
970 : }
971 0 : return false;
972 : }
973 :
974 509761522 : const char *ldb_dn_get_casefold(struct ldb_dn *dn)
975 : {
976 15879190 : unsigned int i;
977 15879190 : size_t len;
978 15879190 : char *d, *n;
979 :
980 509761522 : if (dn->casefold) return dn->casefold;
981 :
982 317423935 : if (dn->special) {
983 284856923 : dn->casefold = talloc_strdup(dn, dn->linearized);
984 284856923 : if (!dn->casefold) return NULL;
985 284856923 : dn->valid_case = true;
986 284856923 : return dn->casefold;
987 : }
988 :
989 32567012 : if ( ! ldb_dn_casefold_internal(dn)) {
990 0 : return NULL;
991 : }
992 :
993 32567012 : if (dn->comp_num == 0) {
994 1506 : dn->casefold = talloc_strdup(dn, "");
995 1506 : return dn->casefold;
996 : }
997 :
998 : /* calculate maximum possible length of DN */
999 194004552 : for (len = 0, i = 0; i < dn->comp_num; i++) {
1000 : /* name len */
1001 161439046 : len += strlen(dn->components[i].cf_name);
1002 : /* max escaped data len */
1003 161439046 : len += (dn->components[i].cf_value.length * 3);
1004 161439046 : len += 2; /* '=' and ',' */
1005 : }
1006 32565506 : dn->casefold = talloc_array(dn, char, len);
1007 32565506 : if ( ! dn->casefold) return NULL;
1008 :
1009 30593529 : d = dn->casefold;
1010 :
1011 194004552 : for (i = 0; i < dn->comp_num; i++) {
1012 :
1013 : /* copy the name */
1014 161439046 : n = dn->components[i].cf_name;
1015 485830867 : while (*n) *d++ = *n++;
1016 :
1017 161439046 : *d++ = '=';
1018 :
1019 : /* and the value */
1020 322878092 : d += ldb_dn_escape_internal( d,
1021 161439046 : (char *)dn->components[i].cf_value.data,
1022 161439046 : dn->components[i].cf_value.length);
1023 161439046 : *d++ = ',';
1024 : }
1025 32565506 : *(--d) = '\0';
1026 :
1027 : /* don't waste more memory than necessary */
1028 32565506 : dn->casefold = talloc_realloc(dn, dn->casefold,
1029 : char, strlen(dn->casefold) + 1);
1030 :
1031 32565506 : return dn->casefold;
1032 : }
1033 :
1034 2464891 : char *ldb_dn_alloc_casefold(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
1035 : {
1036 2464891 : return talloc_strdup(mem_ctx, ldb_dn_get_casefold(dn));
1037 : }
1038 :
1039 : /* Determine if dn is below base, in the ldap tree. Used for
1040 : * evaluating a subtree search.
1041 : * 0 if they match, otherwise non-zero
1042 : */
1043 :
1044 579276699 : int ldb_dn_compare_base(struct ldb_dn *base, struct ldb_dn *dn)
1045 : {
1046 12233054 : int ret;
1047 12233054 : unsigned int n_base, n_dn;
1048 :
1049 579276699 : if ( ! base || base->invalid) return 1;
1050 579276699 : if ( ! dn || dn->invalid) return -1;
1051 :
1052 579276699 : if (( ! base->valid_case) || ( ! dn->valid_case)) {
1053 399529768 : if (base->linearized && dn->linearized && dn->special == base->special) {
1054 : /* try with a normal compare first, if we are lucky
1055 : * we will avoid exploding and casefolding */
1056 6413257 : int dif;
1057 391838100 : dif = strlen(dn->linearized) - strlen(base->linearized);
1058 391838100 : if (dif < 0) {
1059 130174347 : return dif;
1060 : }
1061 259329472 : if (strcmp(base->linearized,
1062 259329472 : &dn->linearized[dif]) == 0) {
1063 149251350 : return 0;
1064 : }
1065 : }
1066 :
1067 114074755 : if ( ! ldb_dn_casefold_internal(base)) {
1068 0 : return 1;
1069 : }
1070 :
1071 114074755 : if ( ! ldb_dn_casefold_internal(dn)) {
1072 0 : return -1;
1073 : }
1074 :
1075 : }
1076 :
1077 : /* if base has more components,
1078 : * they don't have the same base */
1079 293821686 : if (base->comp_num > dn->comp_num) {
1080 55060439 : return (dn->comp_num - base->comp_num);
1081 : }
1082 :
1083 238761247 : if ((dn->comp_num == 0) || (base->comp_num == 0)) {
1084 1 : if (dn->special && base->special) {
1085 0 : return strcmp(base->linearized, dn->linearized);
1086 1 : } else if (dn->special) {
1087 0 : return -1;
1088 1 : } else if (base->special) {
1089 0 : return 1;
1090 : } else {
1091 0 : return 0;
1092 : }
1093 : }
1094 :
1095 238761246 : n_base = base->comp_num - 1;
1096 238761246 : n_dn = dn->comp_num - 1;
1097 :
1098 1119884421 : while (n_base != (unsigned int) -1) {
1099 1023382035 : char *b_name = base->components[n_base].cf_name;
1100 1023382035 : char *dn_name = dn->components[n_dn].cf_name;
1101 :
1102 1023382035 : char *b_vdata = (char *)base->components[n_base].cf_value.data;
1103 1023382035 : char *dn_vdata = (char *)dn->components[n_dn].cf_value.data;
1104 :
1105 1023382035 : size_t b_vlen = base->components[n_base].cf_value.length;
1106 1023382035 : size_t dn_vlen = dn->components[n_dn].cf_value.length;
1107 :
1108 : /* compare attr names */
1109 1023382035 : ret = strcmp(b_name, dn_name);
1110 1023382035 : if (ret != 0) return ret;
1111 :
1112 : /* compare attr.cf_value. */
1113 930984450 : if (b_vlen != dn_vlen) {
1114 48188781 : return b_vlen - dn_vlen;
1115 : }
1116 882795669 : ret = strncmp(b_vdata, dn_vdata, b_vlen);
1117 882795669 : if (ret != 0) return ret;
1118 :
1119 881123175 : n_base--;
1120 881123175 : n_dn--;
1121 : }
1122 :
1123 92962842 : return 0;
1124 : }
1125 :
1126 : /* compare DNs using casefolding compare functions.
1127 :
1128 : If they match, then return 0
1129 : */
1130 :
1131 68617441 : int ldb_dn_compare(struct ldb_dn *dn0, struct ldb_dn *dn1)
1132 : {
1133 4485326 : unsigned int i;
1134 4485326 : int ret;
1135 :
1136 68617441 : if (( ! dn0) || dn0->invalid || ! dn1 || dn1->invalid) {
1137 2 : return -1;
1138 : }
1139 :
1140 68617439 : if (( ! dn0->valid_case) || ( ! dn1->valid_case)) {
1141 10464789 : if (dn0->linearized && dn1->linearized) {
1142 : /* try with a normal compare first, if we are lucky
1143 : * we will avoid exploding and casefolding */
1144 8337320 : if (strcmp(dn0->linearized, dn1->linearized) == 0) {
1145 1445188 : return 0;
1146 : }
1147 : }
1148 :
1149 9005721 : if ( ! ldb_dn_casefold_internal(dn0)) {
1150 0 : return 1;
1151 : }
1152 :
1153 9005721 : if ( ! ldb_dn_casefold_internal(dn1)) {
1154 892 : return -1;
1155 : }
1156 :
1157 : }
1158 :
1159 67157479 : if (dn0->comp_num != dn1->comp_num) {
1160 41456629 : return (dn1->comp_num - dn0->comp_num);
1161 : }
1162 :
1163 25700850 : if (dn0->comp_num == 0) {
1164 1091449 : if (dn0->special && dn1->special) {
1165 1091449 : return strcmp(dn0->linearized, dn1->linearized);
1166 0 : } else if (dn0->special) {
1167 0 : return 1;
1168 0 : } else if (dn1->special) {
1169 0 : return -1;
1170 : } else {
1171 0 : return 0;
1172 : }
1173 : }
1174 :
1175 72293650 : for (i = 0; i < dn0->comp_num; i++) {
1176 61952018 : char *dn0_name = dn0->components[i].cf_name;
1177 61952018 : char *dn1_name = dn1->components[i].cf_name;
1178 :
1179 61952018 : char *dn0_vdata = (char *)dn0->components[i].cf_value.data;
1180 61952018 : char *dn1_vdata = (char *)dn1->components[i].cf_value.data;
1181 :
1182 61952018 : size_t dn0_vlen = dn0->components[i].cf_value.length;
1183 61952018 : size_t dn1_vlen = dn1->components[i].cf_value.length;
1184 :
1185 : /* compare attr names */
1186 61952018 : ret = strcmp(dn0_name, dn1_name);
1187 61952018 : if (ret != 0) {
1188 3393619 : return ret;
1189 : }
1190 :
1191 : /* compare attr.cf_value. */
1192 58558399 : if (dn0_vlen != dn1_vlen) {
1193 6104844 : return dn0_vlen - dn1_vlen;
1194 : }
1195 52453555 : ret = strncmp(dn0_vdata, dn1_vdata, dn0_vlen);
1196 52453555 : if (ret != 0) {
1197 4769306 : return ret;
1198 : }
1199 : }
1200 :
1201 9803716 : return 0;
1202 : }
1203 :
1204 414174963 : static struct ldb_dn_component ldb_dn_copy_component(
1205 : TALLOC_CTX *mem_ctx,
1206 : struct ldb_dn_component *src)
1207 : {
1208 20745974 : struct ldb_dn_component dst;
1209 :
1210 414174963 : memset(&dst, 0, sizeof(dst));
1211 :
1212 414174963 : if (src == NULL) {
1213 0 : return dst;
1214 : }
1215 :
1216 414174963 : dst.value = ldb_val_dup(mem_ctx, &(src->value));
1217 414174963 : if (dst.value.data == NULL) {
1218 0 : return dst;
1219 : }
1220 :
1221 414174963 : dst.name = talloc_strdup(mem_ctx, src->name);
1222 414174963 : if (dst.name == NULL) {
1223 0 : LDB_FREE(dst.value.data);
1224 0 : return dst;
1225 : }
1226 :
1227 414174963 : if (src->cf_value.data) {
1228 344213148 : dst.cf_value = ldb_val_dup(mem_ctx, &(src->cf_value));
1229 344213148 : if (dst.cf_value.data == NULL) {
1230 0 : LDB_FREE(dst.value.data);
1231 0 : LDB_FREE(dst.name);
1232 0 : return dst;
1233 : }
1234 :
1235 344213148 : dst.cf_name = talloc_strdup(mem_ctx, src->cf_name);
1236 344213148 : if (dst.cf_name == NULL) {
1237 0 : LDB_FREE(dst.cf_name);
1238 0 : LDB_FREE(dst.value.data);
1239 0 : LDB_FREE(dst.name);
1240 0 : return dst;
1241 : }
1242 : } else {
1243 66889614 : dst.cf_value.data = NULL;
1244 66889614 : dst.cf_name = NULL;
1245 : }
1246 :
1247 414174963 : return dst;
1248 : }
1249 :
1250 32306117 : static struct ldb_dn_ext_component ldb_dn_ext_copy_component(
1251 : TALLOC_CTX *mem_ctx,
1252 : struct ldb_dn_ext_component *src)
1253 : {
1254 932020 : struct ldb_dn_ext_component dst;
1255 :
1256 32306117 : memset(&dst, 0, sizeof(dst));
1257 :
1258 32306117 : if (src == NULL) {
1259 0 : return dst;
1260 : }
1261 :
1262 32306117 : dst.value = ldb_val_dup(mem_ctx, &(src->value));
1263 32306117 : if (dst.value.data == NULL) {
1264 0 : return dst;
1265 : }
1266 :
1267 32306117 : dst.name = talloc_strdup(mem_ctx, src->name);
1268 32306117 : if (dst.name == NULL) {
1269 0 : LDB_FREE(dst.value.data);
1270 0 : return dst;
1271 : }
1272 :
1273 32306117 : return dst;
1274 : }
1275 :
1276 79163295 : struct ldb_dn *ldb_dn_copy(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
1277 : {
1278 3790365 : struct ldb_dn *new_dn;
1279 :
1280 79163295 : if (!dn || dn->invalid) {
1281 2 : return NULL;
1282 : }
1283 :
1284 79163293 : new_dn = talloc_zero(mem_ctx, struct ldb_dn);
1285 79163293 : if ( !new_dn) {
1286 0 : return NULL;
1287 : }
1288 :
1289 79163293 : *new_dn = *dn;
1290 :
1291 79163293 : if (dn->components) {
1292 3568617 : unsigned int i;
1293 :
1294 74163068 : new_dn->components =
1295 70594451 : talloc_zero_array(new_dn,
1296 : struct ldb_dn_component,
1297 : dn->comp_num);
1298 70594451 : if ( ! new_dn->components) {
1299 0 : talloc_free(new_dn);
1300 0 : return NULL;
1301 : }
1302 :
1303 468976548 : for (i = 0; i < dn->comp_num; i++) {
1304 398382097 : new_dn->components[i] =
1305 398382097 : ldb_dn_copy_component(new_dn->components,
1306 398382097 : &dn->components[i]);
1307 398382097 : if ( ! new_dn->components[i].value.data) {
1308 0 : talloc_free(new_dn);
1309 0 : return NULL;
1310 : }
1311 : }
1312 : }
1313 :
1314 79163293 : if (dn->ext_components) {
1315 801394 : unsigned int i;
1316 :
1317 26096501 : new_dn->ext_components =
1318 25295107 : talloc_zero_array(new_dn,
1319 : struct ldb_dn_ext_component,
1320 : dn->ext_comp_num);
1321 25295107 : if ( ! new_dn->ext_components) {
1322 0 : talloc_free(new_dn);
1323 0 : return NULL;
1324 : }
1325 :
1326 57601224 : for (i = 0; i < dn->ext_comp_num; i++) {
1327 32306117 : new_dn->ext_components[i] =
1328 32306117 : ldb_dn_ext_copy_component(
1329 32306117 : new_dn->ext_components,
1330 32306117 : &dn->ext_components[i]);
1331 32306117 : if ( ! new_dn->ext_components[i].value.data) {
1332 0 : talloc_free(new_dn);
1333 0 : return NULL;
1334 : }
1335 : }
1336 : }
1337 :
1338 79163293 : if (dn->casefold) {
1339 44991583 : new_dn->casefold = talloc_strdup(new_dn, dn->casefold);
1340 44991583 : if ( ! new_dn->casefold) {
1341 0 : talloc_free(new_dn);
1342 0 : return NULL;
1343 : }
1344 : }
1345 :
1346 79163293 : if (dn->linearized) {
1347 79020862 : new_dn->linearized = talloc_strdup(new_dn, dn->linearized);
1348 79020862 : if ( ! new_dn->linearized) {
1349 0 : talloc_free(new_dn);
1350 0 : return NULL;
1351 : }
1352 : }
1353 :
1354 79163293 : if (dn->ext_linearized) {
1355 2999250 : new_dn->ext_linearized = talloc_strdup(new_dn,
1356 1451218 : dn->ext_linearized);
1357 1548032 : if ( ! new_dn->ext_linearized) {
1358 0 : talloc_free(new_dn);
1359 0 : return NULL;
1360 : }
1361 : }
1362 :
1363 75372928 : return new_dn;
1364 : }
1365 :
1366 : /* modify the given dn by adding a base.
1367 : *
1368 : * return true if successful and false if not
1369 : * if false is returned the dn may be marked invalid
1370 : */
1371 651519 : bool ldb_dn_add_base(struct ldb_dn *dn, struct ldb_dn *base)
1372 : {
1373 4928 : const char *s;
1374 4928 : char *t;
1375 :
1376 651519 : if ( !base || base->invalid || !dn || dn->invalid) {
1377 0 : return false;
1378 : }
1379 :
1380 651519 : if (dn == base) {
1381 0 : return false; /* or we will visit infinity */
1382 : }
1383 :
1384 651519 : if (dn->components) {
1385 479 : unsigned int i;
1386 :
1387 477771 : if ( ! ldb_dn_validate(base)) {
1388 0 : return false;
1389 : }
1390 :
1391 477771 : s = NULL;
1392 477771 : if (dn->valid_case) {
1393 2 : if ( ! (s = ldb_dn_get_casefold(base))) {
1394 0 : return false;
1395 : }
1396 : }
1397 :
1398 477771 : dn->components = talloc_realloc(dn,
1399 : dn->components,
1400 : struct ldb_dn_component,
1401 : dn->comp_num + base->comp_num);
1402 477771 : if ( ! dn->components) {
1403 0 : ldb_dn_mark_invalid(dn);
1404 0 : return false;
1405 : }
1406 :
1407 3111546 : for (i = 0; i < base->comp_num; dn->comp_num++, i++) {
1408 2633775 : dn->components[dn->comp_num] =
1409 2633775 : ldb_dn_copy_component(dn->components,
1410 2633775 : &base->components[i]);
1411 2633775 : if (dn->components[dn->comp_num].value.data == NULL) {
1412 0 : ldb_dn_mark_invalid(dn);
1413 0 : return false;
1414 : }
1415 : }
1416 :
1417 477771 : if (dn->casefold && s) {
1418 0 : if (*dn->casefold) {
1419 0 : t = talloc_asprintf(dn, "%s,%s",
1420 : dn->casefold, s);
1421 : } else {
1422 0 : t = talloc_strdup(dn, s);
1423 : }
1424 0 : LDB_FREE(dn->casefold);
1425 0 : dn->casefold = t;
1426 : }
1427 : }
1428 :
1429 651519 : if (dn->linearized) {
1430 :
1431 176394 : s = ldb_dn_get_linearized(base);
1432 176394 : if ( ! s) {
1433 0 : return false;
1434 : }
1435 :
1436 176394 : if (*dn->linearized) {
1437 14343 : t = talloc_asprintf(dn, "%s,%s",
1438 : dn->linearized, s);
1439 : } else {
1440 162051 : t = talloc_strdup(dn, s);
1441 : }
1442 176394 : if ( ! t) {
1443 0 : ldb_dn_mark_invalid(dn);
1444 0 : return false;
1445 : }
1446 176394 : LDB_FREE(dn->linearized);
1447 176394 : dn->linearized = t;
1448 : }
1449 :
1450 : /* Wipe the ext_linearized DN,
1451 : * the GUID and SID are almost certainly no longer valid */
1452 651519 : LDB_FREE(dn->ext_linearized);
1453 651519 : LDB_FREE(dn->ext_components);
1454 651519 : dn->ext_comp_num = 0;
1455 :
1456 651519 : return true;
1457 : }
1458 :
1459 : /* modify the given dn by adding a base.
1460 : *
1461 : * return true if successful and false if not
1462 : * if false is returned the dn may be marked invalid
1463 : */
1464 2 : bool ldb_dn_add_base_fmt(struct ldb_dn *dn, const char *base_fmt, ...)
1465 : {
1466 2 : struct ldb_dn *base;
1467 2 : char *base_str;
1468 2 : va_list ap;
1469 2 : bool ret;
1470 :
1471 2 : if ( !dn || dn->invalid) {
1472 0 : return false;
1473 : }
1474 :
1475 2 : va_start(ap, base_fmt);
1476 2 : base_str = talloc_vasprintf(dn, base_fmt, ap);
1477 2 : va_end(ap);
1478 :
1479 2 : if (base_str == NULL) {
1480 0 : return false;
1481 : }
1482 :
1483 2 : base = ldb_dn_new(base_str, dn->ldb, base_str);
1484 :
1485 2 : ret = ldb_dn_add_base(dn, base);
1486 :
1487 2 : talloc_free(base_str);
1488 :
1489 2 : return ret;
1490 : }
1491 :
1492 : /* modify the given dn by adding children elements.
1493 : *
1494 : * return true if successful and false if not
1495 : * if false is returned the dn may be marked invalid
1496 : */
1497 6766234 : bool ldb_dn_add_child(struct ldb_dn *dn, struct ldb_dn *child)
1498 : {
1499 368564 : const char *s;
1500 368564 : char *t;
1501 :
1502 6766234 : if ( !child || child->invalid || !dn || dn->invalid) {
1503 0 : return false;
1504 : }
1505 :
1506 6766234 : if (dn->components) {
1507 364877 : unsigned int n;
1508 364877 : unsigned int i, j;
1509 :
1510 6525712 : if (dn->comp_num == 0) {
1511 0 : return false;
1512 : }
1513 :
1514 6525712 : if ( ! ldb_dn_validate(child)) {
1515 0 : return false;
1516 : }
1517 :
1518 6525712 : s = NULL;
1519 6525712 : if (dn->valid_case) {
1520 4666239 : if ( ! (s = ldb_dn_get_casefold(child))) {
1521 0 : return false;
1522 : }
1523 : }
1524 :
1525 6525712 : n = dn->comp_num + child->comp_num;
1526 :
1527 6525712 : dn->components = talloc_realloc(dn,
1528 : dn->components,
1529 : struct ldb_dn_component,
1530 : n);
1531 6525712 : if ( ! dn->components) {
1532 0 : ldb_dn_mark_invalid(dn);
1533 0 : return false;
1534 : }
1535 :
1536 35219349 : for (i = dn->comp_num - 1, j = n - 1; i != (unsigned int) -1;
1537 28693637 : i--, j--) {
1538 28693637 : dn->components[j] = dn->components[i];
1539 : }
1540 :
1541 19065791 : for (i = 0; i < child->comp_num; i++) {
1542 12540079 : dn->components[i] =
1543 12540079 : ldb_dn_copy_component(dn->components,
1544 12540079 : &child->components[i]);
1545 12540079 : if (dn->components[i].value.data == NULL) {
1546 0 : ldb_dn_mark_invalid(dn);
1547 0 : return false;
1548 : }
1549 : }
1550 :
1551 6525712 : dn->comp_num = n;
1552 :
1553 6525712 : if (dn->casefold && s) {
1554 3372133 : t = talloc_asprintf(dn, "%s,%s", s, dn->casefold);
1555 3372133 : LDB_FREE(dn->casefold);
1556 3372133 : dn->casefold = t;
1557 : }
1558 : }
1559 :
1560 6766234 : if (dn->linearized) {
1561 6759202 : if (dn->linearized[0] == '\0') {
1562 0 : return false;
1563 : }
1564 :
1565 6759201 : s = ldb_dn_get_linearized(child);
1566 6759201 : if ( ! s) {
1567 0 : return false;
1568 : }
1569 :
1570 6759201 : t = talloc_asprintf(dn, "%s,%s", s, dn->linearized);
1571 6759201 : if ( ! t) {
1572 0 : ldb_dn_mark_invalid(dn);
1573 0 : return false;
1574 : }
1575 6759201 : LDB_FREE(dn->linearized);
1576 6759201 : dn->linearized = t;
1577 : }
1578 :
1579 : /* Wipe the ext_linearized DN,
1580 : * the GUID and SID are almost certainly no longer valid */
1581 6766233 : LDB_FREE(dn->ext_linearized);
1582 6766233 : LDB_FREE(dn->ext_components);
1583 6766233 : dn->ext_comp_num = 0;
1584 :
1585 6766233 : return true;
1586 : }
1587 :
1588 : /* modify the given dn by adding children elements.
1589 : *
1590 : * return true if successful and false if not
1591 : * if false is returned the dn may be marked invalid
1592 : */
1593 6538814 : bool ldb_dn_add_child_fmt(struct ldb_dn *dn, const char *child_fmt, ...)
1594 : {
1595 367284 : struct ldb_dn *child;
1596 367284 : char *child_str;
1597 367284 : va_list ap;
1598 367284 : bool ret;
1599 :
1600 6538814 : if ( !dn || dn->invalid) {
1601 0 : return false;
1602 : }
1603 :
1604 6538814 : va_start(ap, child_fmt);
1605 6538814 : child_str = talloc_vasprintf(dn, child_fmt, ap);
1606 6538814 : va_end(ap);
1607 :
1608 6538814 : if (child_str == NULL) {
1609 0 : return false;
1610 : }
1611 :
1612 6538814 : child = ldb_dn_new(child_str, dn->ldb, child_str);
1613 :
1614 6538814 : ret = ldb_dn_add_child(dn, child);
1615 :
1616 6538814 : talloc_free(child_str);
1617 :
1618 6538814 : return ret;
1619 : }
1620 :
1621 : /* modify the given dn by adding a single child element.
1622 : *
1623 : * return true if successful and false if not
1624 : * if false is returned the dn may be marked invalid
1625 : */
1626 24545 : bool ldb_dn_add_child_val(struct ldb_dn *dn,
1627 : const char *rdn,
1628 : struct ldb_val value)
1629 : {
1630 8 : bool ret;
1631 8 : int ldb_ret;
1632 24545 : struct ldb_dn *child = NULL;
1633 :
1634 24545 : if ( !dn || dn->invalid) {
1635 0 : return false;
1636 : }
1637 :
1638 24545 : child = ldb_dn_new(dn, dn->ldb, "X=Y");
1639 24545 : ret = ldb_dn_add_child(dn, child);
1640 :
1641 24545 : if (ret == false) {
1642 0 : return false;
1643 : }
1644 :
1645 24545 : ldb_ret = ldb_dn_set_component(dn,
1646 : 0,
1647 : rdn,
1648 : value);
1649 24545 : if (ldb_ret != LDB_SUCCESS) {
1650 0 : return false;
1651 : }
1652 :
1653 24537 : return true;
1654 : }
1655 :
1656 654566 : bool ldb_dn_remove_base_components(struct ldb_dn *dn, unsigned int num)
1657 : {
1658 454 : unsigned int i;
1659 :
1660 654566 : if ( ! ldb_dn_validate(dn)) {
1661 0 : return false;
1662 : }
1663 :
1664 654566 : if (dn->comp_num < num) {
1665 0 : return false;
1666 : }
1667 :
1668 : /* free components */
1669 4093159 : for (i = dn->comp_num - num; i < dn->comp_num; i++) {
1670 3438593 : LDB_FREE(dn->components[i].name);
1671 3438593 : LDB_FREE(dn->components[i].value.data);
1672 3438593 : LDB_FREE(dn->components[i].cf_name);
1673 3438593 : LDB_FREE(dn->components[i].cf_value.data);
1674 : }
1675 :
1676 654566 : dn->comp_num -= num;
1677 :
1678 654566 : if (dn->valid_case) {
1679 359495 : for (i = 0; i < dn->comp_num; i++) {
1680 179741 : LDB_FREE(dn->components[i].cf_name);
1681 179741 : LDB_FREE(dn->components[i].cf_value.data);
1682 : }
1683 179754 : dn->valid_case = false;
1684 : }
1685 :
1686 654566 : LDB_FREE(dn->casefold);
1687 654566 : LDB_FREE(dn->linearized);
1688 :
1689 : /* Wipe the ext_linearized DN,
1690 : * the GUID and SID are almost certainly no longer valid */
1691 654566 : LDB_FREE(dn->ext_linearized);
1692 654566 : LDB_FREE(dn->ext_components);
1693 654566 : dn->ext_comp_num = 0;
1694 :
1695 654566 : return true;
1696 : }
1697 :
1698 13033364 : bool ldb_dn_remove_child_components(struct ldb_dn *dn, unsigned int num)
1699 : {
1700 774662 : unsigned int i, j;
1701 :
1702 13033364 : if ( ! ldb_dn_validate(dn)) {
1703 0 : return false;
1704 : }
1705 :
1706 13033364 : if (dn->comp_num < num) {
1707 1 : return false;
1708 : }
1709 :
1710 84173949 : for (i = 0, j = num; j < dn->comp_num; i++, j++) {
1711 71140586 : if (i < num) {
1712 13031917 : LDB_FREE(dn->components[i].name);
1713 13031917 : LDB_FREE(dn->components[i].value.data);
1714 13031917 : LDB_FREE(dn->components[i].cf_name);
1715 13031917 : LDB_FREE(dn->components[i].cf_value.data);
1716 : }
1717 71140586 : dn->components[i] = dn->components[j];
1718 : }
1719 :
1720 13033363 : dn->comp_num -= num;
1721 :
1722 13033363 : if (dn->valid_case) {
1723 61402914 : for (i = 0; i < dn->comp_num; i++) {
1724 51912499 : LDB_FREE(dn->components[i].cf_name);
1725 51912499 : LDB_FREE(dn->components[i].cf_value.data);
1726 : }
1727 9490415 : dn->valid_case = false;
1728 : }
1729 :
1730 13033363 : LDB_FREE(dn->casefold);
1731 13033363 : LDB_FREE(dn->linearized);
1732 :
1733 : /* Wipe the ext_linearized DN,
1734 : * the GUID and SID are almost certainly no longer valid */
1735 13033363 : LDB_FREE(dn->ext_linearized);
1736 13033363 : LDB_FREE(dn->ext_components);
1737 13033363 : dn->ext_comp_num = 0;
1738 :
1739 13033363 : return true;
1740 : }
1741 :
1742 :
1743 : /* replace the components of a DN with those from another DN, without
1744 : * touching the extended components
1745 : *
1746 : * return true if successful and false if not
1747 : * if false is returned the dn may be marked invalid
1748 : */
1749 126571 : bool ldb_dn_replace_components(struct ldb_dn *dn, struct ldb_dn *new_dn)
1750 : {
1751 268 : unsigned int i;
1752 :
1753 126571 : if ( ! ldb_dn_validate(dn) || ! ldb_dn_validate(new_dn)) {
1754 0 : return false;
1755 : }
1756 :
1757 : /* free components */
1758 864874 : for (i = 0; i < dn->comp_num; i++) {
1759 738303 : LDB_FREE(dn->components[i].name);
1760 738303 : LDB_FREE(dn->components[i].value.data);
1761 738303 : LDB_FREE(dn->components[i].cf_name);
1762 738303 : LDB_FREE(dn->components[i].cf_value.data);
1763 : }
1764 :
1765 126571 : dn->components = talloc_realloc(dn,
1766 : dn->components,
1767 : struct ldb_dn_component,
1768 : new_dn->comp_num);
1769 126571 : if (dn->components == NULL) {
1770 0 : ldb_dn_mark_invalid(dn);
1771 0 : return false;
1772 : }
1773 :
1774 126571 : dn->comp_num = new_dn->comp_num;
1775 126571 : dn->valid_case = new_dn->valid_case;
1776 :
1777 745583 : for (i = 0; i < dn->comp_num; i++) {
1778 619012 : dn->components[i] = ldb_dn_copy_component(dn->components, &new_dn->components[i]);
1779 619012 : if (dn->components[i].name == NULL) {
1780 0 : ldb_dn_mark_invalid(dn);
1781 0 : return false;
1782 : }
1783 : }
1784 126571 : if (new_dn->linearized == NULL) {
1785 0 : dn->linearized = NULL;
1786 : } else {
1787 126571 : dn->linearized = talloc_strdup(dn, new_dn->linearized);
1788 126571 : if (dn->linearized == NULL) {
1789 0 : ldb_dn_mark_invalid(dn);
1790 0 : return false;
1791 : }
1792 : }
1793 :
1794 126303 : return true;
1795 : }
1796 :
1797 :
1798 13029069 : struct ldb_dn *ldb_dn_get_parent(TALLOC_CTX *mem_ctx, struct ldb_dn *dn)
1799 : {
1800 774632 : struct ldb_dn *new_dn;
1801 :
1802 13029069 : new_dn = ldb_dn_copy(mem_ctx, dn);
1803 13029069 : if ( !new_dn ) {
1804 2 : return NULL;
1805 : }
1806 :
1807 13029067 : if ( ! ldb_dn_remove_child_components(new_dn, 1)) {
1808 1 : talloc_free(new_dn);
1809 1 : return NULL;
1810 : }
1811 :
1812 12254434 : return new_dn;
1813 : }
1814 :
1815 : /* Create a 'canonical name' string from a DN:
1816 :
1817 : ie dc=samba,dc=org -> samba.org/
1818 : uid=administrator,ou=users,dc=samba,dc=org = samba.org/users/administrator
1819 :
1820 : There are two formats,
1821 : the EX format has the last '/' replaced with a newline (\n).
1822 :
1823 : */
1824 2676243 : static char *ldb_dn_canonical(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, int ex_format) {
1825 149595 : unsigned int i;
1826 149595 : TALLOC_CTX *tmpctx;
1827 2676243 : char *cracked = NULL;
1828 2676243 : const char *format = (ex_format ? "\n" : "/" );
1829 :
1830 2676243 : if ( ! ldb_dn_validate(dn)) {
1831 0 : return NULL;
1832 : }
1833 :
1834 2676243 : tmpctx = talloc_new(mem_ctx);
1835 :
1836 : /* Walk backwards down the DN, grabbing 'dc' components at first */
1837 11612975 : for (i = dn->comp_num - 1; i != (unsigned int) -1; i--) {
1838 11316118 : if (ldb_attr_cmp(dn->components[i].name, "dc") != 0) {
1839 2238032 : break;
1840 : }
1841 8936732 : if (cracked) {
1842 6260559 : cracked = talloc_asprintf(tmpctx, "%s.%s",
1843 : ldb_dn_escape_value(tmpctx,
1844 5935499 : dn->components[i].value),
1845 : cracked);
1846 : } else {
1847 2676173 : cracked = ldb_dn_escape_value(tmpctx,
1848 2526579 : dn->components[i].value);
1849 : }
1850 8936732 : if (!cracked) {
1851 0 : goto done;
1852 : }
1853 : }
1854 :
1855 : /* Only domain components? Finish here */
1856 2676243 : if (i == (unsigned int) -1) {
1857 296857 : cracked = talloc_strdup_append_buffer(cracked, format);
1858 296857 : talloc_steal(mem_ctx, cracked);
1859 296857 : goto done;
1860 : }
1861 :
1862 : /* Now walk backwards appending remaining components */
1863 6543437 : for (; i > 0; i--) {
1864 4164051 : cracked = talloc_asprintf_append_buffer(cracked, "/%s",
1865 : ldb_dn_escape_value(tmpctx,
1866 4164051 : dn->components[i].value));
1867 4164051 : if (!cracked) {
1868 0 : goto done;
1869 : }
1870 : }
1871 :
1872 : /* Last one, possibly a newline for the 'ex' format */
1873 2379386 : cracked = talloc_asprintf_append_buffer(cracked, "%s%s", format,
1874 : ldb_dn_escape_value(tmpctx,
1875 2379386 : dn->components[i].value));
1876 :
1877 2379386 : talloc_steal(mem_ctx, cracked);
1878 2676243 : done:
1879 2676243 : talloc_free(tmpctx);
1880 2676243 : return cracked;
1881 : }
1882 :
1883 : /* Wrapper functions for the above, for the two different string formats */
1884 2675979 : char *ldb_dn_canonical_string(TALLOC_CTX *mem_ctx, struct ldb_dn *dn) {
1885 2675979 : return ldb_dn_canonical(mem_ctx, dn, 0);
1886 :
1887 : }
1888 :
1889 264 : char *ldb_dn_canonical_ex_string(TALLOC_CTX *mem_ctx, struct ldb_dn *dn) {
1890 264 : return ldb_dn_canonical(mem_ctx, dn, 1);
1891 : }
1892 :
1893 26342664 : int ldb_dn_get_comp_num(struct ldb_dn *dn)
1894 : {
1895 26342664 : if ( ! ldb_dn_validate(dn)) {
1896 182 : return -1;
1897 : }
1898 26342482 : return dn->comp_num;
1899 : }
1900 :
1901 14756835 : int ldb_dn_get_extended_comp_num(struct ldb_dn *dn)
1902 : {
1903 14756835 : if ( ! ldb_dn_validate(dn)) {
1904 182 : return -1;
1905 : }
1906 14756653 : return dn->ext_comp_num;
1907 : }
1908 :
1909 9151 : const char *ldb_dn_get_component_name(struct ldb_dn *dn, unsigned int num)
1910 : {
1911 9151 : if ( ! ldb_dn_validate(dn)) {
1912 0 : return NULL;
1913 : }
1914 9151 : if (num >= dn->comp_num) return NULL;
1915 9139 : return dn->components[num].name;
1916 : }
1917 :
1918 597037 : const struct ldb_val *ldb_dn_get_component_val(struct ldb_dn *dn,
1919 : unsigned int num)
1920 : {
1921 597037 : if ( ! ldb_dn_validate(dn)) {
1922 0 : return NULL;
1923 : }
1924 597037 : if (num >= dn->comp_num) return NULL;
1925 597037 : return &dn->components[num].value;
1926 : }
1927 :
1928 70447146 : const char *ldb_dn_get_rdn_name(struct ldb_dn *dn)
1929 : {
1930 70447146 : if ( ! ldb_dn_validate(dn)) {
1931 0 : return NULL;
1932 : }
1933 70447146 : if (dn->comp_num == 0) return NULL;
1934 56625038 : return dn->components[0].name;
1935 : }
1936 :
1937 56325356 : const struct ldb_val *ldb_dn_get_rdn_val(struct ldb_dn *dn)
1938 : {
1939 56325356 : if ( ! ldb_dn_validate(dn)) {
1940 2 : return NULL;
1941 : }
1942 56325354 : if (dn->comp_num == 0) return NULL;
1943 42503246 : return &dn->components[0].value;
1944 : }
1945 :
1946 1223705 : int ldb_dn_set_component(struct ldb_dn *dn, int num,
1947 : const char *name, const struct ldb_val val)
1948 : {
1949 156807 : char *n;
1950 156807 : struct ldb_val v;
1951 :
1952 1223705 : if ( ! ldb_dn_validate(dn)) {
1953 0 : return LDB_ERR_OTHER;
1954 : }
1955 :
1956 1223705 : if (num < 0) {
1957 0 : return LDB_ERR_OTHER;
1958 : }
1959 :
1960 1223705 : if ((unsigned)num >= dn->comp_num) {
1961 3 : return LDB_ERR_OTHER;
1962 : }
1963 :
1964 1223702 : if (val.length > val.length + 1) {
1965 0 : return LDB_ERR_OTHER;
1966 : }
1967 :
1968 1223702 : n = talloc_strdup(dn, name);
1969 1223702 : if ( ! n) {
1970 0 : return LDB_ERR_OTHER;
1971 : }
1972 :
1973 1223702 : v.length = val.length;
1974 :
1975 : /*
1976 : * This is like talloc_memdup(dn, v.data, v.length + 1), but
1977 : * avoids the over-read
1978 : */
1979 1223702 : v.data = (uint8_t *)talloc_size(dn, v.length+1);
1980 1223702 : if ( ! v.data) {
1981 0 : talloc_free(n);
1982 0 : return LDB_ERR_OTHER;
1983 : }
1984 1223702 : memcpy(v.data, val.data, val.length);
1985 :
1986 : /*
1987 : * Enforce NUL termination outside the stated length, as is
1988 : * traditional in LDB
1989 : */
1990 1223702 : v.data[v.length] = '\0';
1991 :
1992 1223702 : talloc_free(dn->components[num].name);
1993 1223702 : talloc_free(dn->components[num].value.data);
1994 1223702 : dn->components[num].name = n;
1995 1223702 : dn->components[num].value = v;
1996 :
1997 1223702 : if (dn->valid_case) {
1998 : unsigned int i;
1999 4922449 : for (i = 0; i < dn->comp_num; i++) {
2000 4253400 : LDB_FREE(dn->components[i].cf_name);
2001 4253400 : LDB_FREE(dn->components[i].cf_value.data);
2002 : }
2003 669049 : dn->valid_case = false;
2004 : }
2005 1223702 : LDB_FREE(dn->casefold);
2006 1223702 : LDB_FREE(dn->linearized);
2007 :
2008 : /* Wipe the ext_linearized DN,
2009 : * the GUID and SID are almost certainly no longer valid */
2010 1223702 : LDB_FREE(dn->ext_linearized);
2011 1223702 : LDB_FREE(dn->ext_components);
2012 1223702 : dn->ext_comp_num = 0;
2013 :
2014 1223702 : return LDB_SUCCESS;
2015 : }
2016 :
2017 260852856 : const struct ldb_val *ldb_dn_get_extended_component(struct ldb_dn *dn,
2018 : const char *name)
2019 : {
2020 6723751 : unsigned int i;
2021 260852856 : if ( ! ldb_dn_validate(dn)) {
2022 718 : return NULL;
2023 : }
2024 325384927 : for (i=0; i < dn->ext_comp_num; i++) {
2025 129082301 : if (ldb_attr_cmp(dn->ext_components[i].name, name) == 0) {
2026 64549512 : return &dn->ext_components[i].value;
2027 : }
2028 : }
2029 191231421 : return NULL;
2030 : }
2031 :
2032 156177784 : int ldb_dn_set_extended_component(struct ldb_dn *dn,
2033 : const char *name, const struct ldb_val *val)
2034 : {
2035 3714232 : struct ldb_dn_ext_component *p;
2036 3714232 : unsigned int i;
2037 3714232 : struct ldb_val v2;
2038 3714232 : const struct ldb_dn_extended_syntax *ext_syntax;
2039 :
2040 156177784 : if ( ! ldb_dn_validate(dn)) {
2041 0 : return LDB_ERR_OTHER;
2042 : }
2043 :
2044 156177784 : ext_syntax = ldb_dn_extended_syntax_by_name(dn->ldb, name);
2045 156177784 : if (ext_syntax == NULL) {
2046 : /* We don't know how to handle this type of thing */
2047 0 : return LDB_ERR_INVALID_DN_SYNTAX;
2048 : }
2049 :
2050 247361255 : for (i=0; i < dn->ext_comp_num; i++) {
2051 91194166 : if (ldb_attr_cmp(dn->ext_components[i].name, name) == 0) {
2052 10695 : if (val) {
2053 10695 : dn->ext_components[i].value =
2054 10695 : ldb_val_dup(dn->ext_components, val);
2055 :
2056 10695 : dn->ext_components[i].name = ext_syntax->name;
2057 10695 : if (!dn->ext_components[i].value.data) {
2058 0 : ldb_dn_mark_invalid(dn);
2059 0 : return LDB_ERR_OPERATIONS_ERROR;
2060 : }
2061 : } else {
2062 0 : ARRAY_DEL_ELEMENT(
2063 : dn->ext_components,
2064 : i,
2065 0 : dn->ext_comp_num);
2066 0 : dn->ext_comp_num--;
2067 :
2068 0 : dn->ext_components = talloc_realloc(dn,
2069 : dn->ext_components,
2070 : struct ldb_dn_ext_component,
2071 : dn->ext_comp_num);
2072 0 : if (!dn->ext_components) {
2073 0 : ldb_dn_mark_invalid(dn);
2074 0 : return LDB_ERR_OPERATIONS_ERROR;
2075 : }
2076 : }
2077 10695 : LDB_FREE(dn->ext_linearized);
2078 :
2079 10695 : return LDB_SUCCESS;
2080 : }
2081 : }
2082 :
2083 156167089 : if (val == NULL) {
2084 : /* removing a value that doesn't exist is not an error */
2085 0 : return LDB_SUCCESS;
2086 : }
2087 :
2088 156167089 : v2 = *val;
2089 :
2090 159881261 : p = dn->ext_components
2091 156167089 : = talloc_realloc(dn,
2092 : dn->ext_components,
2093 : struct ldb_dn_ext_component,
2094 : dn->ext_comp_num + 1);
2095 156167089 : if (!dn->ext_components) {
2096 0 : ldb_dn_mark_invalid(dn);
2097 0 : return LDB_ERR_OPERATIONS_ERROR;
2098 : }
2099 :
2100 156167089 : p[dn->ext_comp_num].value = ldb_val_dup(dn->ext_components, &v2);
2101 156167089 : p[dn->ext_comp_num].name = talloc_strdup(p, name);
2102 :
2103 156167089 : if (!dn->ext_components[i].name || !dn->ext_components[i].value.data) {
2104 0 : ldb_dn_mark_invalid(dn);
2105 0 : return LDB_ERR_OPERATIONS_ERROR;
2106 : }
2107 156167089 : dn->ext_components = p;
2108 156167089 : dn->ext_comp_num++;
2109 :
2110 156167089 : LDB_FREE(dn->ext_linearized);
2111 :
2112 156167089 : return LDB_SUCCESS;
2113 : }
2114 :
2115 46074867 : void ldb_dn_remove_extended_components(struct ldb_dn *dn)
2116 : {
2117 46074867 : LDB_FREE(dn->ext_linearized);
2118 46074867 : LDB_FREE(dn->ext_components);
2119 46074867 : dn->ext_comp_num = 0;
2120 46074867 : }
2121 :
2122 4506 : bool ldb_dn_is_valid(struct ldb_dn *dn)
2123 : {
2124 4506 : if ( ! dn) return false;
2125 4506 : return ! dn->invalid;
2126 : }
2127 :
2128 1860346249 : bool ldb_dn_is_special(struct ldb_dn *dn)
2129 : {
2130 1860346249 : if ( ! dn || dn->invalid) return false;
2131 1860346248 : return dn->special;
2132 : }
2133 :
2134 467992703 : bool ldb_dn_has_extended(struct ldb_dn *dn)
2135 : {
2136 467992703 : if ( ! dn || dn->invalid) return false;
2137 467992703 : if (dn->ext_linearized && (dn->ext_linearized[0] == '<')) return true;
2138 452940644 : return dn->ext_comp_num != 0;
2139 : }
2140 :
2141 16082613 : bool ldb_dn_check_special(struct ldb_dn *dn, const char *check)
2142 : {
2143 16082613 : if ( ! dn || dn->invalid) return false;
2144 16082613 : return ! strcmp(dn->linearized, check);
2145 : }
2146 :
2147 403830240 : bool ldb_dn_is_null(struct ldb_dn *dn)
2148 : {
2149 403830240 : if ( ! dn || dn->invalid) return false;
2150 403830240 : if (ldb_dn_has_extended(dn)) return false;
2151 350993812 : if (dn->linearized && (dn->linearized[0] == '\0')) return true;
2152 313436691 : return false;
2153 : }
2154 :
2155 : /*
2156 : this updates dn->components, taking the components from ref_dn.
2157 : This is used by code that wants to update the DN path of a DN
2158 : while not impacting on the extended DN components
2159 : */
2160 10897 : int ldb_dn_update_components(struct ldb_dn *dn, const struct ldb_dn *ref_dn)
2161 : {
2162 10897 : dn->components = talloc_realloc(dn, dn->components,
2163 : struct ldb_dn_component, ref_dn->comp_num);
2164 10897 : if (!dn->components) {
2165 0 : return LDB_ERR_OPERATIONS_ERROR;
2166 : }
2167 10897 : memcpy(dn->components, ref_dn->components,
2168 10897 : sizeof(struct ldb_dn_component)*ref_dn->comp_num);
2169 10897 : dn->comp_num = ref_dn->comp_num;
2170 :
2171 10897 : LDB_FREE(dn->casefold);
2172 10897 : LDB_FREE(dn->linearized);
2173 10897 : LDB_FREE(dn->ext_linearized);
2174 :
2175 10897 : return LDB_SUCCESS;
2176 : }
2177 :
2178 : /*
2179 : minimise a DN. The caller must pass in a validated DN.
2180 :
2181 : If the DN has an extended component then only the first extended
2182 : component is kept, the DN string is stripped.
2183 :
2184 : The existing dn is modified
2185 : */
2186 8057152 : bool ldb_dn_minimise(struct ldb_dn *dn)
2187 : {
2188 291240 : unsigned int i;
2189 :
2190 8057152 : if (!ldb_dn_validate(dn)) {
2191 0 : return false;
2192 : }
2193 8057152 : if (dn->ext_comp_num == 0) {
2194 0 : return true;
2195 : }
2196 :
2197 : /* free components */
2198 41283436 : for (i = 0; i < dn->comp_num; i++) {
2199 33226284 : LDB_FREE(dn->components[i].name);
2200 33226284 : LDB_FREE(dn->components[i].value.data);
2201 33226284 : LDB_FREE(dn->components[i].cf_name);
2202 33226284 : LDB_FREE(dn->components[i].cf_value.data);
2203 : }
2204 8057152 : dn->comp_num = 0;
2205 8057152 : dn->valid_case = false;
2206 :
2207 8057152 : LDB_FREE(dn->casefold);
2208 8057152 : LDB_FREE(dn->linearized);
2209 :
2210 : /* note that we don't free dn->components as this there are
2211 : * several places in ldb_dn.c that rely on it being non-NULL
2212 : * for an exploded DN
2213 : */
2214 :
2215 11459176 : for (i = 1; i < dn->ext_comp_num; i++) {
2216 3402024 : LDB_FREE(dn->ext_components[i].value.data);
2217 : }
2218 8057152 : dn->ext_comp_num = 1;
2219 :
2220 8057152 : dn->ext_components = talloc_realloc(dn, dn->ext_components, struct ldb_dn_ext_component, 1);
2221 8057152 : if (dn->ext_components == NULL) {
2222 0 : ldb_dn_mark_invalid(dn);
2223 0 : return false;
2224 : }
2225 :
2226 8057152 : LDB_FREE(dn->ext_linearized);
2227 :
2228 8057152 : return true;
2229 : }
2230 :
2231 917457 : struct ldb_context *ldb_dn_get_ldb_context(struct ldb_dn *dn)
2232 : {
2233 917457 : return dn->ldb;
2234 : }
|