4 Copyright (C) Andrew Tridgell 2004
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 pack/unpack
29 * Description: pack/unpack routines for ldb messages as key/value blobs
31 * Author: Andrew Tridgell
34 #include "ldb_private.h"
37 * These macros are from byte_array.h via libssh
38 * TODO: This will be replaced with use of the byte_array.h header when it
41 * Macros for handling integer types in byte arrays
43 * This file is originally from the libssh.org project
45 * Copyright (c) 2018 Andreas Schneider <asn@cryptomilk.org>
47 * This library is free software; you can redistribute it and/or
48 * modify it under the terms of the GNU Lesser General Public
49 * License as published by the Free Software Foundation; either
50 * version 2.1 of the License, or (at your option) any later version.
52 * This library is distributed in the hope that it will be useful,
53 * but WITHOUT ANY WARRANTY; without even the implied warranty of
54 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
55 * Lesser General Public License for more details.
57 * You should have received a copy of the GNU Lesser General Public
58 * License along with this library; if not, write to the Free Software
59 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
61 #define _DATA_BYTE_CONST(data, pos) \
62 ((uint8_t)(((const uint8_t *)(data))[(pos)]))
63 #define PULL_LE_U8(data, pos) \
64 (_DATA_BYTE_CONST(data, pos))
65 #define PULL_LE_U16(data, pos) \
66 ((uint16_t)PULL_LE_U8(data, pos) |\
67 ((uint16_t)(PULL_LE_U8(data, (pos) + 1))) << 8)
68 #define PULL_LE_U32(data, pos) \
69 ((uint32_t)(PULL_LE_U16(data, pos) |\
70 ((uint32_t)PULL_LE_U16(data, (pos) + 2)) << 16))
72 #define _DATA_BYTE(data, pos) \
73 (((uint8_t *)(data))[(pos)])
74 #define PUSH_LE_U8(data, pos, val) \
75 (_DATA_BYTE(data, pos) = ((uint8_t)(val)))
76 #define PUSH_LE_U16(data, pos, val) \
77 (PUSH_LE_U8((data), (pos), (uint8_t)((uint16_t)(val) & 0xff)),\
78 PUSH_LE_U8((data), (pos) + 1,\
79 (uint8_t)((uint16_t)(val) >> 8)))
80 #define PUSH_LE_U32(data, pos, val) \
81 (PUSH_LE_U16((data), (pos), (uint16_t)((uint32_t)(val) & 0xffff)),\
82 PUSH_LE_U16((data), (pos) + 2, (uint16_t)((uint32_t)(val) >> 16)))
87 #define NULL_PAD_BYTE_LEN 1
89 static int attribute_storable_values(const struct ldb_message_element
*el
)
91 if (el
->num_values
== 0) return 0;
93 if (ldb_attr_cmp(el
->name
, "distinguishedName") == 0) return 0;
95 return el
->num_values
;
98 static int ldb_pack_data_v1(struct ldb_context
*ldb
,
99 const struct ldb_message
*message
,
100 struct ldb_val
*data
)
102 unsigned int i
, j
, real_elements
=0;
103 size_t size
, dn_len
, attr_len
, value_len
;
108 dn
= ldb_dn_get_linearized(message
->dn
);
114 /* work out how big it needs to be */
115 size
= U32_LEN
* 2 + NULL_PAD_BYTE_LEN
;
118 if (size
+ dn_len
< size
) {
125 * First calcuate the buffer size we need, and check for
128 for (i
=0;i
<message
->num_elements
;i
++) {
129 if (attribute_storable_values(&message
->elements
[i
]) == 0) {
135 if (size
+ U32_LEN
+ NULL_PAD_BYTE_LEN
< size
) {
139 size
+= U32_LEN
+ NULL_PAD_BYTE_LEN
;
141 attr_len
= strlen(message
->elements
[i
].name
);
142 if (size
+ attr_len
< size
) {
148 for (j
=0;j
<message
->elements
[i
].num_values
;j
++) {
149 if (size
+ U32_LEN
+ NULL_PAD_BYTE_LEN
< size
) {
153 size
+= U32_LEN
+ NULL_PAD_BYTE_LEN
;
155 value_len
= message
->elements
[i
].values
[j
].length
;
156 if (size
+ value_len
< size
) {
165 data
->data
= talloc_array(ldb
, uint8_t, size
);
173 PUSH_LE_U32(p
, 0, LDB_PACKING_FORMAT
);
175 PUSH_LE_U32(p
, 0, real_elements
);
178 /* the dn needs to be packed so we can be case preserving
179 while hashing on a case folded dn */
181 memcpy(p
, dn
, len
+NULL_PAD_BYTE_LEN
);
182 p
+= len
+ NULL_PAD_BYTE_LEN
;
184 for (i
=0;i
<message
->num_elements
;i
++) {
185 if (attribute_storable_values(&message
->elements
[i
]) == 0) {
188 len
= strlen(message
->elements
[i
].name
);
189 memcpy(p
, message
->elements
[i
].name
, len
+NULL_PAD_BYTE_LEN
);
190 p
+= len
+ NULL_PAD_BYTE_LEN
;
191 PUSH_LE_U32(p
, 0, message
->elements
[i
].num_values
);
193 for (j
=0;j
<message
->elements
[i
].num_values
;j
++) {
195 message
->elements
[i
].values
[j
].length
);
197 memcpy(p
, message
->elements
[i
].values
[j
].data
,
198 message
->elements
[i
].values
[j
].length
);
199 p
[message
->elements
[i
].values
[j
].length
] = 0;
200 p
+= message
->elements
[i
].values
[j
].length
+
209 * New pack version designed based on performance profiling of version 1.
210 * The approach is to separate value data from the rest of the record's data.
211 * This improves performance because value data is not needed during unpacking
212 * or filtering of the message's attribute list. During filtering we only copy
213 * attributes which are present in the attribute list, however at the parse
214 * stage we need to point to all attributes as they may be referenced in the
216 * With this new format, we don't lose time loading data (eg via
217 * talloc_memdup()) that is never needed (for the vast majority of attributes
218 * are are never found in either the search expression or attribute list).
219 * Additional changes include adding a canonicalized DN (for later
220 * optimizations) and variable width length fields for faster unpacking.
221 * The pack and unpack performance improvement is tested in the torture
222 * test torture_ldb_pack_format_perf.
227 * Number of Elements (4 bytes)
228 * DN length (4 bytes)
229 * DN with null terminator (DN length + 1 bytes)
230 * Canonicalized DN length (4 bytes)
231 * Canonicalized DN with null terminator (Canonicalized DN length + 1 bytes)
232 * Number of bytes from here to value data section (4 bytes)
233 * # For each element:
234 * Element name length (4 bytes)
235 * Element name with null terminator (Element name length + 1 bytes)
236 * Number of values (4 bytes)
237 * Width of value lengths
239 * Value data length (#bytes given by width field above)
240 * # For each element:
242 * Value data (#bytes given by corresponding length above)
244 static int ldb_pack_data_v2(struct ldb_context
*ldb
,
245 const struct ldb_message
*message
,
246 struct ldb_val
*data
)
248 unsigned int i
, j
, real_elements
=0;
249 size_t size
, dn_len
, dn_canon_len
, attr_len
, value_len
;
250 const char *dn
, *dn_canon
;
254 uint8_t val_len_width
;
257 * First half of this function will calculate required size for
258 * packed data. Initial size is 20 = 5 * 4. 5 fixed fields are:
259 * version, num elements, dn len, canon dn len, attr section len
264 * Get linearized and canonicalized form of the DN and add the lengths
265 * of each to size, plus 1 for null terminator.
267 dn
= ldb_dn_get_linearized(message
->dn
);
273 dn_len
= strlen(dn
) + NULL_PAD_BYTE_LEN
;
274 if (size
+ dn_len
< size
) {
280 if (ldb_dn_is_special(message
->dn
)) {
281 dn_canon_len
= NULL_PAD_BYTE_LEN
;
282 dn_canon
= discard_const_p(char, "\0");
284 dn_canon
= ldb_dn_canonical_string(message
->dn
, message
->dn
);
285 if (dn_canon
== NULL
) {
290 dn_canon_len
= strlen(dn_canon
) + NULL_PAD_BYTE_LEN
;
291 if (size
+ dn_canon_len
< size
) {
296 size
+= dn_canon_len
;
298 /* Add the size required by each element */
299 for (i
=0;i
<message
->num_elements
;i
++) {
300 if (attribute_storable_values(&message
->elements
[i
]) == 0) {
307 * Add length of element name + 9 for:
308 * 1 for null terminator
309 * 4 for element name length field
310 * 4 for number of values field
312 attr_len
= strlen(message
->elements
[i
].name
);
313 if (size
+ attr_len
+ U32_LEN
* 2 + NULL_PAD_BYTE_LEN
< size
) {
317 size
+= attr_len
+ U32_LEN
* 2 + NULL_PAD_BYTE_LEN
;
320 * Find the max value length, so we can calculate the width
321 * required for the value length fields.
324 for (j
=0;j
<message
->elements
[i
].num_values
;j
++) {
325 value_len
= message
->elements
[i
].values
[j
].length
;
326 if (value_len
> max_val_len
) {
327 max_val_len
= value_len
;
330 if (size
+ value_len
+ NULL_PAD_BYTE_LEN
< size
) {
334 size
+= value_len
+ NULL_PAD_BYTE_LEN
;
337 if (max_val_len
<= UCHAR_MAX
) {
338 val_len_width
= U8_LEN
;
339 } else if (max_val_len
<= USHRT_MAX
) {
340 val_len_width
= U16_LEN
;
341 } else if (max_val_len
<= UINT_MAX
) {
342 val_len_width
= U32_LEN
;
348 /* Total size required for val lengths (re-using variable) */
349 max_val_len
= (val_len_width
*message
->elements
[i
].num_values
);
351 /* Add one for storing the width */
352 max_val_len
+= U8_LEN
;
353 if (size
+ max_val_len
< size
) {
361 data
->data
= talloc_array(ldb
, uint8_t, size
);
368 /* Packing format version and number of element */
370 PUSH_LE_U32(p
, 0, LDB_PACKING_FORMAT_V2
);
372 PUSH_LE_U32(p
, 0, real_elements
);
375 /* Pack DN and Canonicalized DN */
376 PUSH_LE_U32(p
, 0, dn_len
-NULL_PAD_BYTE_LEN
);
378 memcpy(p
, dn
, dn_len
);
381 PUSH_LE_U32(p
, 0, dn_canon_len
-NULL_PAD_BYTE_LEN
);
383 memcpy(p
, dn_canon
, dn_canon_len
);
387 * Save pointer at this point and leave a U32_LEN gap for
388 * storing the size of the attribute names and value lengths
394 for (i
=0;i
<message
->num_elements
;i
++) {
395 if (attribute_storable_values(&message
->elements
[i
]) == 0) {
399 /* Length of el name */
400 len
= strlen(message
->elements
[i
].name
);
401 PUSH_LE_U32(p
, 0, len
);
405 * Even though we have the element name's length, put a null
406 * terminator at the end so if any code uses the name
407 * directly, it'll be safe to do things requiring null
408 * termination like strlen
410 memcpy(p
, message
->elements
[i
].name
, len
+NULL_PAD_BYTE_LEN
);
411 p
+= len
+ NULL_PAD_BYTE_LEN
;
413 PUSH_LE_U32(p
, 0, message
->elements
[i
].num_values
);
417 * Calculate value length width again. It's faster to
418 * calculate it again than do the array management to
419 * store the result during size calculation.
422 for (j
=0;j
<message
->elements
[i
].num_values
;j
++) {
423 value_len
= message
->elements
[i
].values
[j
].length
;
424 if (value_len
> max_val_len
) {
425 max_val_len
= value_len
;
429 if (max_val_len
<= UCHAR_MAX
) {
430 val_len_width
= U8_LEN
;
431 } else if (max_val_len
<= USHRT_MAX
) {
432 val_len_width
= U16_LEN
;
433 } else if (max_val_len
<= UINT_MAX
) {
434 val_len_width
= U32_LEN
;
441 *p
= val_len_width
& 0xFF;
445 * Pack each value's length using the minimum number of bytes
446 * required, which we just calculated. We repeat the loop
447 * for each case here so the compiler can inline code.
449 if (val_len_width
== U8_LEN
) {
450 for (j
=0;j
<message
->elements
[i
].num_values
;j
++) {
452 message
->elements
[i
].values
[j
].length
);
455 } else if (val_len_width
== U16_LEN
) {
456 for (j
=0;j
<message
->elements
[i
].num_values
;j
++) {
458 message
->elements
[i
].values
[j
].length
);
461 } else if (val_len_width
== U32_LEN
) {
462 for (j
=0;j
<message
->elements
[i
].num_values
;j
++) {
464 message
->elements
[i
].values
[j
].length
);
471 * We've finished packing the attr names and value lengths
472 * section, so store the size in the U32_LEN gap we left
475 PUSH_LE_U32(q
, 0, p
-q
);
477 /* Now pack the values */
478 for (i
=0;i
<message
->num_elements
;i
++) {
479 if (attribute_storable_values(&message
->elements
[i
]) == 0) {
482 for (j
=0;j
<message
->elements
[i
].num_values
;j
++) {
483 memcpy(p
, message
->elements
[i
].values
[j
].data
,
484 message
->elements
[i
].values
[j
].length
);
487 * Even though we have the data length, put a null
488 * terminator at the end of each value's data so if
489 * any code uses the data directly, it'll be safe to
490 * do things requiring null termination like strlen.
492 p
[message
->elements
[i
].values
[j
].length
] = 0;
493 p
+= message
->elements
[i
].values
[j
].length
+
499 * If we didn't end up at the end of the data here, something has
502 if (p
!= data
->data
+ size
) {
511 pack a ldb message into a linear buffer in a ldb_val
513 note that this routine avoids saving elements with zero values,
514 as these are equivalent to having no element
516 caller frees the data buffer after use
518 int ldb_pack_data(struct ldb_context
*ldb
,
519 const struct ldb_message
*message
,
520 struct ldb_val
*data
,
521 uint32_t pack_format_version
) {
523 if (pack_format_version
== LDB_PACKING_FORMAT
) {
524 return ldb_pack_data_v1(ldb
, message
, data
);
525 } else if (pack_format_version
== LDB_PACKING_FORMAT_V2
) {
526 return ldb_pack_data_v2(ldb
, message
, data
);
534 * Unpack a ldb message from a linear buffer in ldb_val
536 static int ldb_unpack_data_flags_v1(struct ldb_context
*ldb
,
537 const struct ldb_val
*data
,
538 struct ldb_message
*message
,
546 unsigned int nelem
= 0;
548 struct ldb_val
*ldb_val_single_array
= NULL
;
550 message
->elements
= NULL
;
554 /* Format (U32, already read) + U32 for num_elements */
555 if (data
->length
< U32_LEN
* 2) {
560 /* Skip first 4 bytes, format already read */
562 message
->num_elements
= PULL_LE_U32(p
, 0);
565 remaining
= data
->length
- U32_LEN
* 2;
568 case LDB_PACKING_FORMAT_NODN
:
572 case LDB_PACKING_FORMAT
:
574 * With this check, we know that the DN at p is \0
577 dn_len
= strnlen((char *)p
, remaining
);
578 if (dn_len
== remaining
) {
582 if (flags
& LDB_UNPACK_DATA_FLAG_NO_DN
) {
586 blob
.data
= discard_const_p(uint8_t, p
);
587 blob
.length
= dn_len
;
588 message
->dn
= ldb_dn_from_ldb_val(message
, ldb
, &blob
);
589 if (message
->dn
== NULL
) {
595 * Redundant: by definition, remaining must be more
596 * than one less than dn_len, as otherwise it would be
599 if (remaining
< dn_len
+ NULL_PAD_BYTE_LEN
) {
603 remaining
-= dn_len
+ NULL_PAD_BYTE_LEN
;
604 p
+= dn_len
+ NULL_PAD_BYTE_LEN
;
612 if (flags
& LDB_UNPACK_DATA_FLAG_NO_ATTRS
) {
616 if (message
->num_elements
== 0) {
620 if (message
->num_elements
> remaining
/ 6) {
625 message
->elements
= talloc_zero_array(message
, struct ldb_message_element
,
626 message
->num_elements
);
627 if (!message
->elements
) {
633 * In typical use, most values are single-valued. This makes
634 * it quite expensive to allocate an array of ldb_val for each
635 * of these, just to then hold the pointer to the data buffer
636 * So with LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC we allocate this
637 * ahead of time and use it for the single values where possible.
638 * (This is used the the normal search case, but not in the
639 * index case because of caller requirements).
641 if (flags
& LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC
) {
642 ldb_val_single_array
= talloc_array(message
->elements
, struct ldb_val
,
643 message
->num_elements
);
644 if (ldb_val_single_array
== NULL
) {
650 for (i
=0;i
<message
->num_elements
;i
++) {
651 const char *attr
= NULL
;
653 struct ldb_message_element
*element
= NULL
;
656 * Sanity check: Element must be at least the size of empty
657 * attr name and value and NULL terms for each.
659 if (remaining
< U32_LEN
* 2 + NULL_PAD_BYTE_LEN
* 2) {
665 * With this check, we know that the attribute name at
666 * p is \0 terminated.
668 attr_len
= strnlen((char *)p
, remaining
-6);
669 if (attr_len
== remaining
-6) {
679 element
= &message
->elements
[nelem
];
680 element
->name
= attr
;
683 if (remaining
< (attr_len
+ NULL_PAD_BYTE_LEN
)) {
687 remaining
-= attr_len
+ NULL_PAD_BYTE_LEN
;
688 p
+= attr_len
+ NULL_PAD_BYTE_LEN
;
689 element
->num_values
= PULL_LE_U32(p
, 0);
690 element
->values
= NULL
;
691 if ((flags
& LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC
) && element
->num_values
== 1) {
692 element
->values
= &ldb_val_single_array
[nelem
];
693 element
->flags
|= LDB_FLAG_INTERNAL_SHARED_VALUES
;
694 } else if (element
->num_values
!= 0) {
695 element
->values
= talloc_array(message
->elements
,
697 element
->num_values
);
698 if (!element
->values
) {
704 if (remaining
< U32_LEN
) {
708 remaining
-= U32_LEN
;
709 for (j
= 0; j
< element
->num_values
; j
++) {
711 * Sanity check: Value must be at least the size of
712 * empty val and NULL terminator.
714 if (remaining
< U32_LEN
+ NULL_PAD_BYTE_LEN
) {
718 remaining
-= U32_LEN
+ NULL_PAD_BYTE_LEN
;
720 len
= PULL_LE_U32(p
, 0);
721 if (remaining
< len
) {
725 if (len
+ NULL_PAD_BYTE_LEN
< len
) {
730 element
->values
[j
].length
= len
;
731 element
->values
[j
].data
= p
+ U32_LEN
;
733 p
+= len
+ U32_LEN
+ NULL_PAD_BYTE_LEN
;
738 * Adapt the number of elements to the real number of unpacked elements,
739 * it means that we overallocated elements array.
741 message
->num_elements
= nelem
;
744 * Shrink the allocated size. On current talloc behaviour
745 * this will help if we skipped 32 or more attributes.
747 message
->elements
= talloc_realloc(message
, message
->elements
,
748 struct ldb_message_element
,
749 message
->num_elements
);
751 if (remaining
!= 0) {
752 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
753 "Error: %zu bytes unread in ldb_unpack_data_flags",
760 talloc_free(message
->elements
);
765 * Unpack a ldb message from a linear buffer in ldb_val
767 static int ldb_unpack_data_flags_v2(struct ldb_context
*ldb
,
768 const struct ldb_val
*data
,
769 struct ldb_message
*message
,
772 uint8_t *p
, *q
, *end_p
, *value_section_p
;
774 unsigned int nelem
= 0;
776 struct ldb_val
*ldb_val_single_array
= NULL
;
777 uint8_t val_len_width
;
779 message
->elements
= NULL
;
782 end_p
= p
+ data
->length
;
784 /* Skip first 4 bytes, format already read */
787 /* First fields are fixed: num_elements, DN length */
788 if (p
+ U32_LEN
* 2 > end_p
) {
793 message
->num_elements
= PULL_LE_U32(p
, 0);
796 len
= PULL_LE_U32(p
, 0);
799 if (p
+ len
+ NULL_PAD_BYTE_LEN
> end_p
) {
804 if (flags
& LDB_UNPACK_DATA_FLAG_NO_DN
) {
808 blob
.data
= discard_const_p(uint8_t, p
);
810 message
->dn
= ldb_dn_from_ldb_val(message
, ldb
, &blob
);
811 if (message
->dn
== NULL
) {
817 p
+= len
+ NULL_PAD_BYTE_LEN
;
819 if (*(p
-NULL_PAD_BYTE_LEN
) != '\0') {
824 /* Now skip the canonicalized DN and its length */
825 len
= PULL_LE_U32(p
, 0) + NULL_PAD_BYTE_LEN
;
828 if (p
+ len
> end_p
) {
835 if (*(p
-NULL_PAD_BYTE_LEN
) != '\0') {
840 if (flags
& LDB_UNPACK_DATA_FLAG_NO_ATTRS
) {
844 if (message
->num_elements
== 0) {
849 * Sanity check (17 bytes is the minimum element size)
851 if (message
->num_elements
> (end_p
- p
) / 17) {
856 message
->elements
= talloc_zero_array(message
,
857 struct ldb_message_element
,
858 message
->num_elements
);
859 if (!message
->elements
) {
865 * In typical use, most values are single-valued. This makes
866 * it quite expensive to allocate an array of ldb_val for each
867 * of these, just to then hold the pointer to the data buffer.
868 * So with LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC we allocate this
869 * ahead of time and use it for the single values where possible.
870 * (This is used the the normal search case, but not in the
871 * index case because of caller requirements).
873 if (flags
& LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC
) {
874 ldb_val_single_array
= talloc_array(message
->elements
,
876 message
->num_elements
);
877 if (ldb_val_single_array
== NULL
) {
883 q
= p
+ PULL_LE_U32(p
, 0);
887 for (i
=0;i
<message
->num_elements
;i
++) {
888 const char *attr
= NULL
;
890 struct ldb_message_element
*element
= NULL
;
892 /* Sanity check: minimum element size */
893 if (p
+ (U32_LEN
* 2) + /* attr name len, num values */
894 (U8_LEN
* 2) + /* value length width, one val length */
895 (NULL_PAD_BYTE_LEN
* 2) /* null for attr name + val */
901 attr_len
= PULL_LE_U32(p
, 0);
910 p
+= attr_len
+ NULL_PAD_BYTE_LEN
;
912 * num_values, val_len_width
914 * val_len_width is the width specifier
915 * for the variable length encoding
917 if (p
+ U32_LEN
+ U8_LEN
> value_section_p
) {
922 if (*(p
-NULL_PAD_BYTE_LEN
) != '\0') {
927 element
= &message
->elements
[nelem
];
928 element
->name
= attr
;
931 element
->num_values
= PULL_LE_U32(p
, 0);
932 element
->values
= NULL
;
933 if ((flags
& LDB_UNPACK_DATA_FLAG_NO_VALUES_ALLOC
) &&
934 element
->num_values
== 1) {
935 element
->values
= &ldb_val_single_array
[nelem
];
936 element
->flags
|= LDB_FLAG_INTERNAL_SHARED_VALUES
;
937 } else if (element
->num_values
!= 0) {
938 element
->values
= talloc_array(message
->elements
,
940 element
->num_values
);
941 if (!element
->values
) {
950 * Here we read how wide the remaining lengths are
951 * which avoids storing and parsing a lot of leading
957 if (p
+ val_len_width
* element
->num_values
>
964 * This is structured weird for compiler optimization
965 * purposes, but we need to pull the array of widths
966 * with different macros depending on how wide the
967 * biggest one is (specified by val_len_width)
969 if (val_len_width
== U8_LEN
) {
970 for (j
= 0; j
< element
->num_values
; j
++) {
971 element
->values
[j
].length
= PULL_LE_U8(p
, 0);
974 } else if (val_len_width
== U16_LEN
) {
975 for (j
= 0; j
< element
->num_values
; j
++) {
976 element
->values
[j
].length
= PULL_LE_U16(p
, 0);
979 } else if (val_len_width
== U32_LEN
) {
980 for (j
= 0; j
< element
->num_values
; j
++) {
981 element
->values
[j
].length
= PULL_LE_U32(p
, 0);
989 for (j
= 0; j
< element
->num_values
; j
++) {
990 len
= element
->values
[j
].length
;
991 if (len
+ NULL_PAD_BYTE_LEN
< len
) {
995 if (q
+ len
+ NULL_PAD_BYTE_LEN
> end_p
) {
1000 element
->values
[j
].data
= q
;
1001 q
+= len
+ NULL_PAD_BYTE_LEN
;
1007 * If p isn't now pointing at the beginning of the value section,
1008 * something went very wrong.
1010 if (p
!= value_section_p
) {
1011 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
1012 "Error: Data corruption in ldb_unpack_data_flags");
1018 * Adapt the number of elements to the real number of unpacked
1019 * elements it means that we overallocated elements array.
1021 message
->num_elements
= nelem
;
1024 * Shrink the allocated size. On current talloc behaviour
1025 * this will help if we skipped 32 or more attributes.
1027 message
->elements
= talloc_realloc(message
, message
->elements
,
1028 struct ldb_message_element
,
1029 message
->num_elements
);
1032 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
1033 "Error: %zu bytes unread in ldb_unpack_data_flags",
1042 talloc_free(message
->elements
);
1046 int ldb_unpack_get_format(const struct ldb_val
*data
,
1047 uint32_t *pack_format_version
)
1049 if (data
->length
< U32_LEN
) {
1050 return LDB_ERR_OPERATIONS_ERROR
;
1052 *pack_format_version
= PULL_LE_U32(data
->data
, 0);
1057 * Unpack a ldb message from a linear buffer in ldb_val
1059 int ldb_unpack_data_flags(struct ldb_context
*ldb
,
1060 const struct ldb_val
*data
,
1061 struct ldb_message
*message
,
1066 if (data
->length
< U32_LEN
) {
1071 format
= PULL_LE_U32(data
->data
, 0);
1072 if (format
== LDB_PACKING_FORMAT_V2
) {
1073 return ldb_unpack_data_flags_v2(ldb
, data
, message
, flags
);
1077 * The v1 function we're about to call takes either LDB_PACKING_FORMAT
1078 * or LDB_PACKING_FORMAT_NODN packing format versions, and will error
1079 * if given some other version, so we don't need to do any further
1080 * checks on 'format'.
1082 return ldb_unpack_data_flags_v1(ldb
, data
, message
, flags
, format
);
1087 * Unpack a ldb message from a linear buffer in ldb_val
1089 * Free with ldb_unpack_data_free()
1091 int ldb_unpack_data(struct ldb_context
*ldb
,
1092 const struct ldb_val
*data
,
1093 struct ldb_message
*message
)
1095 return ldb_unpack_data_flags(ldb
, data
, message
, 0);
1099 add the special distinguishedName element
1101 int ldb_msg_add_distinguished_name(struct ldb_message
*msg
)
1103 const char *dn_attr
= "distinguishedName";
1106 if (ldb_msg_find_element(msg
, dn_attr
)) {
1108 * This should not happen, but this is
1109 * existing behaviour...
1114 dn
= ldb_dn_alloc_linearized(msg
, msg
->dn
);
1116 return LDB_ERR_OPERATIONS_ERROR
;
1119 return ldb_msg_add_steal_string(msg
, dn_attr
, dn
);
1123 * filter the specified list of attributes from msg,
1124 * adding requested attributes, and perhaps all for *,
1125 * but not the DN to filtered_msg.
1127 int ldb_filter_attrs(struct ldb_context
*ldb
,
1128 const struct ldb_message
*msg
,
1129 const char *const *attrs
,
1130 struct ldb_message
*filtered_msg
)
1133 bool keep_all
= false;
1134 bool add_dn
= false;
1135 uint32_t num_elements
;
1136 uint32_t elements_size
;
1139 /* check for special attrs */
1140 for (i
= 0; attrs
[i
]; i
++) {
1141 int cmp
= strcmp(attrs
[i
], "*");
1146 cmp
= ldb_attr_cmp(attrs
[i
], "distinguishedName");
1157 elements_size
= msg
->num_elements
+ 1;
1159 /* Shortcuts for the simple cases */
1160 } else if (add_dn
&& i
== 1) {
1161 if (ldb_msg_add_distinguished_name(filtered_msg
) != 0) {
1165 } else if (i
== 0) {
1169 * Otherwise we are copying at most as many elements as we
1176 filtered_msg
->elements
= talloc_array(filtered_msg
,
1177 struct ldb_message_element
,
1179 if (filtered_msg
->elements
== NULL
) goto failed
;
1183 for (i
= 0; i
< msg
->num_elements
; i
++) {
1184 struct ldb_message_element
*el
= &msg
->elements
[i
];
1187 * el2 is assigned after the Pigeonhole principle
1188 * check below for clarity
1190 struct ldb_message_element
*el2
= NULL
;
1193 if (keep_all
== false) {
1195 for (j
= 0; attrs
[j
]; j
++) {
1196 int cmp
= ldb_attr_cmp(el
->name
, attrs
[j
]);
1202 if (found
== false) {
1208 * Pigeonhole principle: we can't have more elements
1209 * than the number of attributes if they are unique in
1212 if (num_elements
>= elements_size
) {
1216 el2
= &filtered_msg
->elements
[num_elements
];
1219 el2
->name
= talloc_strdup(filtered_msg
->elements
,
1221 if (el2
->name
== NULL
) {
1224 el2
->values
= talloc_array(filtered_msg
->elements
,
1225 struct ldb_val
, el
->num_values
);
1226 if (el2
->values
== NULL
) {
1229 for (j
=0;j
<el
->num_values
;j
++) {
1230 el2
->values
[j
] = ldb_val_dup(el2
->values
, &el
->values
[j
]);
1231 if (el2
->values
[j
].data
== NULL
&& el
->values
[j
].length
!= 0) {
1238 filtered_msg
->num_elements
= num_elements
;
1241 if (ldb_msg_add_distinguished_name(filtered_msg
) != 0) {
1246 if (filtered_msg
->num_elements
> 0) {
1247 filtered_msg
->elements
1248 = talloc_realloc(filtered_msg
,
1249 filtered_msg
->elements
,
1250 struct ldb_message_element
,
1251 filtered_msg
->num_elements
);
1252 if (filtered_msg
->elements
== NULL
) {
1256 TALLOC_FREE(filtered_msg
->elements
);
1261 TALLOC_FREE(filtered_msg
->elements
);
1266 * filter the specified list of attributes from msg,
1267 * adding requested attributes, and perhaps all for *.
1268 * Unlike ldb_filter_attrs(), the DN will not be added
1271 int ldb_filter_attrs_in_place(struct ldb_message
*msg
,
1272 const char *const *attrs
)
1275 bool keep_all
= false;
1276 unsigned int num_del
= 0;
1279 /* check for special attrs */
1280 for (i
= 0; attrs
[i
]; i
++) {
1281 int cmp
= strcmp(attrs
[i
], "*");
1287 if (!keep_all
&& i
== 0) {
1288 msg
->num_elements
= 0;
1295 for (i
= 0; i
< msg
->num_elements
; i
++) {
1302 for (j
= 0; attrs
[j
]; j
++) {
1303 int cmp
= ldb_attr_cmp(msg
->elements
[i
].name
, attrs
[j
]);
1313 } else if (num_del
!= 0) {
1314 msg
->elements
[i
- num_del
] = msg
->elements
[i
];
1318 msg
->num_elements
-= num_del
;
1323 /* Have an unpacked ldb message take talloc ownership of its elements. */
1324 int ldb_msg_elements_take_ownership(struct ldb_message
*msg
)
1328 for (i
= 0; i
< msg
->num_elements
; i
++) {
1329 struct ldb_message_element
*el
= &msg
->elements
[i
];
1333 name
= talloc_strdup(msg
->elements
,
1340 if (el
->flags
& LDB_FLAG_INTERNAL_SHARED_VALUES
) {
1341 struct ldb_val
*values
= talloc_memdup(msg
->elements
, el
->values
,
1342 sizeof(struct ldb_val
) * el
->num_values
);
1343 if (values
== NULL
) {
1346 el
->values
= values
;
1347 el
->flags
&= ~LDB_FLAG_INTERNAL_SHARED_VALUES
;
1350 for (j
= 0; j
< el
->num_values
; j
++) {
1351 struct ldb_val val
= ldb_val_dup(el
->values
, &el
->values
[j
]);
1352 if (val
.data
== NULL
&& el
->values
[j
].length
!= 0) {
1355 el
->values
[j
] = val
;