winbind/idmap_ad: be verbose about the user that we fail to map
[Samba.git] / lib / ldb / common / ldb_pack.c
blob4382d5b1be07beaf86c938e34472d5a819d82342
1 /*
2 ldb database library
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
8 ** under the LGPL
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 * Name: ldb
27 * Component: ldb pack/unpack
29 * Description: pack/unpack routines for ldb messages as key/value blobs
31 * Author: Andrew Tridgell
34 #include "ldb_private.h"
36 /* change this if the data format ever changes */
37 #define LDB_PACKING_FORMAT 0x26011967
39 /* old packing formats */
40 #define LDB_PACKING_FORMAT_NODN 0x26011966
42 /* use a portable integer format */
43 static void put_uint32(uint8_t *p, int ofs, unsigned int val)
45 p += ofs;
46 p[0] = val&0xFF;
47 p[1] = (val>>8) & 0xFF;
48 p[2] = (val>>16) & 0xFF;
49 p[3] = (val>>24) & 0xFF;
52 static unsigned int pull_uint32(uint8_t *p, int ofs)
54 p += ofs;
55 return p[0] | (p[1]<<8) | (p[2]<<16) | (p[3]<<24);
58 static int attribute_storable_values(const struct ldb_message_element *el)
60 if (el->num_values == 0) return 0;
62 if (ldb_attr_cmp(el->name, "distinguishedName") == 0) return 0;
64 return el->num_values;
68 pack a ldb message into a linear buffer in a ldb_val
70 note that this routine avoids saving elements with zero values,
71 as these are equivalent to having no element
73 caller frees the data buffer after use
75 int ldb_pack_data(struct ldb_context *ldb,
76 const struct ldb_message *message,
77 struct ldb_val *data)
79 unsigned int i, j, real_elements=0;
80 size_t size;
81 const char *dn;
82 uint8_t *p;
83 size_t len;
85 dn = ldb_dn_get_linearized(message->dn);
86 if (dn == NULL) {
87 errno = ENOMEM;
88 return -1;
91 /* work out how big it needs to be */
92 size = 8;
94 size += 1 + strlen(dn);
96 for (i=0;i<message->num_elements;i++) {
97 if (attribute_storable_values(&message->elements[i]) == 0) {
98 continue;
101 real_elements++;
103 size += 1 + strlen(message->elements[i].name) + 4;
104 for (j=0;j<message->elements[i].num_values;j++) {
105 size += 4 + message->elements[i].values[j].length + 1;
109 /* allocate it */
110 data->data = talloc_array(ldb, uint8_t, size);
111 if (!data->data) {
112 errno = ENOMEM;
113 return -1;
115 data->length = size;
117 p = data->data;
118 put_uint32(p, 0, LDB_PACKING_FORMAT);
119 put_uint32(p, 4, real_elements);
120 p += 8;
122 /* the dn needs to be packed so we can be case preserving
123 while hashing on a case folded dn */
124 len = strlen(dn);
125 memcpy(p, dn, len+1);
126 p += len + 1;
128 for (i=0;i<message->num_elements;i++) {
129 if (attribute_storable_values(&message->elements[i]) == 0) {
130 continue;
132 len = strlen(message->elements[i].name);
133 memcpy(p, message->elements[i].name, len+1);
134 p += len + 1;
135 put_uint32(p, 0, message->elements[i].num_values);
136 p += 4;
137 for (j=0;j<message->elements[i].num_values;j++) {
138 put_uint32(p, 0, message->elements[i].values[j].length);
139 memcpy(p+4, message->elements[i].values[j].data,
140 message->elements[i].values[j].length);
141 p[4+message->elements[i].values[j].length] = 0;
142 p += 4 + message->elements[i].values[j].length + 1;
146 return 0;
150 unpack a ldb message from a linear buffer in ldb_val
152 Free with ldb_unpack_data_free()
154 int ldb_unpack_data(struct ldb_context *ldb,
155 const struct ldb_val *data,
156 struct ldb_message *message)
158 uint8_t *p;
159 unsigned int remaining;
160 unsigned int i, j;
161 unsigned format;
162 size_t len;
164 message->elements = NULL;
166 p = data->data;
167 if (data->length < 8) {
168 errno = EIO;
169 goto failed;
172 format = pull_uint32(p, 0);
173 message->num_elements = pull_uint32(p, 4);
174 p += 8;
176 remaining = data->length - 8;
178 switch (format) {
179 case LDB_PACKING_FORMAT_NODN:
180 message->dn = NULL;
181 break;
183 case LDB_PACKING_FORMAT:
184 len = strnlen((char *)p, remaining);
185 if (len == remaining) {
186 errno = EIO;
187 goto failed;
189 message->dn = ldb_dn_new(message, ldb, (char *)p);
190 if (message->dn == NULL) {
191 errno = ENOMEM;
192 goto failed;
194 remaining -= len + 1;
195 p += len + 1;
196 break;
198 default:
199 errno = EIO;
200 goto failed;
203 if (message->num_elements == 0) {
204 return 0;
207 if (message->num_elements > remaining / 6) {
208 errno = EIO;
209 goto failed;
212 message->elements = talloc_array(message, struct ldb_message_element, message->num_elements);
213 if (!message->elements) {
214 errno = ENOMEM;
215 goto failed;
218 memset(message->elements, 0,
219 message->num_elements * sizeof(struct ldb_message_element));
221 for (i=0;i<message->num_elements;i++) {
222 if (remaining < 10) {
223 errno = EIO;
224 goto failed;
226 len = strnlen((char *)p, remaining-6);
227 if (len == remaining-6) {
228 errno = EIO;
229 goto failed;
231 if (len == 0) {
232 errno = EIO;
233 goto failed;
235 message->elements[i].flags = 0;
236 message->elements[i].name = talloc_strndup(message->elements, (char *)p, len);
237 if (message->elements[i].name == NULL) {
238 errno = ENOMEM;
239 goto failed;
241 remaining -= len + 1;
242 p += len + 1;
243 message->elements[i].num_values = pull_uint32(p, 0);
244 message->elements[i].values = NULL;
245 if (message->elements[i].num_values != 0) {
246 message->elements[i].values = talloc_array(message->elements,
247 struct ldb_val,
248 message->elements[i].num_values);
249 if (!message->elements[i].values) {
250 errno = ENOMEM;
251 goto failed;
254 p += 4;
255 remaining -= 4;
256 for (j=0;j<message->elements[i].num_values;j++) {
257 len = pull_uint32(p, 0);
258 if (len > remaining-5) {
259 errno = EIO;
260 goto failed;
263 message->elements[i].values[j].length = len;
264 message->elements[i].values[j].data = talloc_size(message->elements[i].values, len+1);
265 if (message->elements[i].values[j].data == NULL) {
266 errno = ENOMEM;
267 goto failed;
269 memcpy(message->elements[i].values[j].data, p+4, len);
270 message->elements[i].values[j].data[len] = 0;
272 remaining -= len+4+1;
273 p += len+4+1;
277 if (remaining != 0) {
278 ldb_debug(ldb, LDB_DEBUG_ERROR,
279 "Error: %d bytes unread in ldb_unpack_data", remaining);
282 return 0;
284 failed:
285 talloc_free(message->elements);
286 return -1;