Fix the mess with ldb includes.
[Samba/ekacnet.git] / source4 / lib / ldb / common / ldb_ldif.c
blob400fb352ff4aba05ef06d976f4034b81cf8e9a10
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: ldif routines
29 * Description: ldif pack/unpack routines
31 * Author: Andrew Tridgell
35 see RFC2849 for the LDIF format definition
38 #include "ldb_private.h"
39 #include "system/locale.h"
44 static int ldb_read_data_file(void *mem_ctx, struct ldb_val *value)
46 struct stat statbuf;
47 char *buf;
48 int count, size, bytes;
49 int ret;
50 int f;
51 const char *fname = (const char *)value->data;
53 if (strncmp(fname, "file://", 7) != 0) {
54 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
56 fname += 7;
58 f = open(fname, O_RDONLY);
59 if (f == -1) {
60 return -1;
63 if (fstat(f, &statbuf) != 0) {
64 ret = -1;
65 goto done;
68 if (statbuf.st_size == 0) {
69 ret = -1;
70 goto done;
73 value->data = (uint8_t *)talloc_size(mem_ctx, statbuf.st_size + 1);
74 if (value->data == NULL) {
75 ret = -1;
76 goto done;
78 value->data[statbuf.st_size] = 0;
80 count = 0;
81 size = statbuf.st_size;
82 buf = (char *)value->data;
83 while (count < statbuf.st_size) {
84 bytes = read(f, buf, size);
85 if (bytes == -1) {
86 talloc_free(value->data);
87 ret = -1;
88 goto done;
90 count += bytes;
91 buf += bytes;
92 size -= bytes;
95 value->length = statbuf.st_size;
96 ret = statbuf.st_size;
98 done:
99 close(f);
100 return ret;
104 this base64 decoder was taken from jitterbug (written by tridge).
105 we might need to replace it with a new version
107 int ldb_base64_decode(char *s)
109 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
110 int bit_offset=0, byte_offset, idx, i, n;
111 uint8_t *d = (uint8_t *)s;
112 char *p=NULL;
114 n=i=0;
116 while (*s && (p=strchr(b64,*s))) {
117 idx = (int)(p - b64);
118 byte_offset = (i*6)/8;
119 bit_offset = (i*6)%8;
120 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
121 if (bit_offset < 3) {
122 d[byte_offset] |= (idx << (2-bit_offset));
123 n = byte_offset+1;
124 } else {
125 d[byte_offset] |= (idx >> (bit_offset-2));
126 d[byte_offset+1] = 0;
127 d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
128 n = byte_offset+2;
130 s++; i++;
132 if (bit_offset >= 3) {
133 n--;
136 if (*s && !p) {
137 /* the only termination allowed */
138 if (*s != '=') {
139 return -1;
143 /* null terminate */
144 d[n] = 0;
145 return n;
150 encode as base64
151 caller frees
153 char *ldb_base64_encode(void *mem_ctx, const char *buf, int len)
155 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
156 int bit_offset, byte_offset, idx, i;
157 const uint8_t *d = (const uint8_t *)buf;
158 int bytes = (len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
159 char *out;
161 out = talloc_array(mem_ctx, char, bytes+pad_bytes+1);
162 if (!out) return NULL;
164 for (i=0;i<bytes;i++) {
165 byte_offset = (i*6)/8;
166 bit_offset = (i*6)%8;
167 if (bit_offset < 3) {
168 idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
169 } else {
170 idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
171 if (byte_offset+1 < len) {
172 idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
175 out[i] = b64[idx];
178 for (;i<bytes+pad_bytes;i++)
179 out[i] = '=';
180 out[i] = 0;
182 return out;
186 see if a buffer should be base64 encoded
188 int ldb_should_b64_encode(const struct ldb_val *val)
190 unsigned int i;
191 uint8_t *p = val->data;
193 if (val->length == 0) {
194 return 0;
197 if (p[0] == ' ' || p[0] == ':') {
198 return 1;
201 for (i=0; i<val->length; i++) {
202 if (!isprint(p[i]) || p[i] == '\n') {
203 return 1;
206 return 0;
209 /* this macro is used to handle the return checking on fprintf_fn() */
210 #define CHECK_RET do { if (ret < 0) return ret; total += ret; } while (0)
213 write a line folded string onto a file
215 static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *private_data,
216 const char *buf, size_t length, int start_pos)
218 unsigned int i;
219 int total=0, ret;
221 for (i=0;i<length;i++) {
222 ret = fprintf_fn(private_data, "%c", buf[i]);
223 CHECK_RET;
224 if (i != (length-1) && (i + start_pos) % 77 == 0) {
225 ret = fprintf_fn(private_data, "\n ");
226 CHECK_RET;
230 return total;
233 #undef CHECK_RET
236 encode as base64 to a file
238 static int base64_encode_f(struct ldb_context *ldb,
239 int (*fprintf_fn)(void *, const char *, ...),
240 void *private_data,
241 const char *buf, int len, int start_pos)
243 char *b = ldb_base64_encode(ldb, buf, len);
244 int ret;
246 if (!b) {
247 return -1;
250 ret = fold_string(fprintf_fn, private_data, b, strlen(b), start_pos);
252 talloc_free(b);
253 return ret;
257 static const struct {
258 const char *name;
259 enum ldb_changetype changetype;
260 } ldb_changetypes[] = {
261 {"add", LDB_CHANGETYPE_ADD},
262 {"delete", LDB_CHANGETYPE_DELETE},
263 {"modify", LDB_CHANGETYPE_MODIFY},
264 {NULL, 0}
267 /* this macro is used to handle the return checking on fprintf_fn() */
268 #define CHECK_RET do { if (ret < 0) { talloc_free(mem_ctx); return ret; } total += ret; } while (0)
271 write to ldif, using a caller supplied write method
273 int ldb_ldif_write(struct ldb_context *ldb,
274 int (*fprintf_fn)(void *, const char *, ...),
275 void *private_data,
276 const struct ldb_ldif *ldif)
278 TALLOC_CTX *mem_ctx;
279 unsigned int i, j;
280 int total=0, ret;
281 char *p;
282 const struct ldb_message *msg;
284 mem_ctx = talloc_named_const(NULL, 0, "ldb_ldif_write");
286 msg = ldif->msg;
287 p = ldb_dn_get_extended_linearized(mem_ctx, msg->dn, 1);
288 ret = fprintf_fn(private_data, "dn: %s\n", p);
289 talloc_free(p);
290 CHECK_RET;
292 if (ldif->changetype != LDB_CHANGETYPE_NONE) {
293 for (i=0;ldb_changetypes[i].name;i++) {
294 if (ldb_changetypes[i].changetype == ldif->changetype) {
295 break;
298 if (!ldb_changetypes[i].name) {
299 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d\n",
300 ldif->changetype);
301 talloc_free(mem_ctx);
302 return -1;
304 ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
305 CHECK_RET;
308 for (i=0;i<msg->num_elements;i++) {
309 const struct ldb_schema_attribute *a;
311 a = ldb_schema_attribute_by_name(ldb, msg->elements[i].name);
313 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
314 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
315 case LDB_FLAG_MOD_ADD:
316 fprintf_fn(private_data, "add: %s\n",
317 msg->elements[i].name);
318 break;
319 case LDB_FLAG_MOD_DELETE:
320 fprintf_fn(private_data, "delete: %s\n",
321 msg->elements[i].name);
322 break;
323 case LDB_FLAG_MOD_REPLACE:
324 fprintf_fn(private_data, "replace: %s\n",
325 msg->elements[i].name);
326 break;
330 for (j=0;j<msg->elements[i].num_values;j++) {
331 struct ldb_val v;
332 ret = a->syntax->ldif_write_fn(ldb, mem_ctx, &msg->elements[i].values[j], &v);
333 if (ret != LDB_SUCCESS) {
334 v = msg->elements[i].values[j];
336 if (ret != LDB_SUCCESS || ldb_should_b64_encode(&v)) {
337 ret = fprintf_fn(private_data, "%s:: ",
338 msg->elements[i].name);
339 CHECK_RET;
340 ret = base64_encode_f(ldb, fprintf_fn, private_data,
341 (char *)v.data, v.length,
342 strlen(msg->elements[i].name)+3);
343 CHECK_RET;
344 ret = fprintf_fn(private_data, "\n");
345 CHECK_RET;
346 } else {
347 ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
348 CHECK_RET;
349 ret = fold_string(fprintf_fn, private_data,
350 (char *)v.data, v.length,
351 strlen(msg->elements[i].name)+2);
352 CHECK_RET;
353 ret = fprintf_fn(private_data, "\n");
354 CHECK_RET;
356 if (v.data != msg->elements[i].values[j].data) {
357 talloc_free(v.data);
360 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
361 fprintf_fn(private_data, "-\n");
364 ret = fprintf_fn(private_data,"\n");
365 CHECK_RET;
367 return total;
370 #undef CHECK_RET
374 pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
375 this routine removes any RFC2849 continuations and comments
377 caller frees
379 static char *next_chunk(struct ldb_context *ldb,
380 int (*fgetc_fn)(void *), void *private_data)
382 size_t alloc_size=0, chunk_size = 0;
383 char *chunk = NULL;
384 int c;
385 int in_comment = 0;
387 while ((c = fgetc_fn(private_data)) != EOF) {
388 if (chunk_size+1 >= alloc_size) {
389 char *c2;
390 alloc_size += 1024;
391 c2 = talloc_realloc(ldb, chunk, char, alloc_size);
392 if (!c2) {
393 talloc_free(chunk);
394 errno = ENOMEM;
395 return NULL;
397 chunk = c2;
400 if (in_comment) {
401 if (c == '\n') {
402 in_comment = 0;
404 continue;
407 /* handle continuation lines - see RFC2849 */
408 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
409 chunk_size--;
410 continue;
413 /* chunks are terminated by a double line-feed */
414 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
415 chunk[chunk_size-1] = 0;
416 return chunk;
419 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
420 in_comment = 1;
421 continue;
424 /* ignore leading blank lines */
425 if (chunk_size == 0 && c == '\n') {
426 continue;
429 chunk[chunk_size++] = c;
432 if (chunk) {
433 chunk[chunk_size] = 0;
436 return chunk;
440 /* simple ldif attribute parser */
441 static int next_attr(void *mem_ctx, char **s, const char **attr, struct ldb_val *value)
443 char *p;
444 int base64_encoded = 0;
445 int binary_file = 0;
447 if (strncmp(*s, "-\n", 2) == 0) {
448 value->length = 0;
449 *attr = "-";
450 *s += 2;
451 return 0;
454 p = strchr(*s, ':');
455 if (!p) {
456 return -1;
459 *p++ = 0;
461 if (*p == ':') {
462 base64_encoded = 1;
463 p++;
466 if (*p == '<') {
467 binary_file = 1;
468 p++;
471 *attr = *s;
473 while (*p == ' ' || *p == '\t') {
474 p++;
477 value->data = (uint8_t *)p;
479 p = strchr(p, '\n');
481 if (!p) {
482 value->length = strlen((char *)value->data);
483 *s = ((char *)value->data) + value->length;
484 } else {
485 value->length = p - (char *)value->data;
486 *s = p+1;
487 *p = 0;
490 if (base64_encoded) {
491 int len = ldb_base64_decode((char *)value->data);
492 if (len == -1) {
493 /* it wasn't valid base64 data */
494 return -1;
496 value->length = len;
499 if (binary_file) {
500 int len = ldb_read_data_file(mem_ctx, value);
501 if (len == -1) {
502 /* an error occured hile trying to retrieve the file */
503 return -1;
507 return 0;
512 free a message from a ldif_read
514 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
516 talloc_free(ldif);
520 read from a LDIF source, creating a ldb_message
522 struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
523 int (*fgetc_fn)(void *), void *private_data)
525 struct ldb_ldif *ldif;
526 struct ldb_message *msg;
527 const char *attr=NULL;
528 char *chunk=NULL, *s;
529 struct ldb_val value;
530 unsigned flags = 0;
532 value.data = NULL;
534 ldif = talloc(ldb, struct ldb_ldif);
535 if (!ldif) return NULL;
537 ldif->msg = talloc(ldif, struct ldb_message);
538 if (ldif->msg == NULL) {
539 talloc_free(ldif);
540 return NULL;
543 ldif->changetype = LDB_CHANGETYPE_NONE;
544 msg = ldif->msg;
546 msg->dn = NULL;
547 msg->elements = NULL;
548 msg->num_elements = 0;
550 chunk = next_chunk(ldb, fgetc_fn, private_data);
551 if (!chunk) {
552 goto failed;
554 talloc_steal(ldif, chunk);
556 s = chunk;
558 if (next_attr(ldif, &s, &attr, &value) != 0) {
559 goto failed;
562 /* first line must be a dn */
563 if (ldb_attr_cmp(attr, "dn") != 0) {
564 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'\n",
565 attr);
566 goto failed;
569 msg->dn = ldb_dn_from_ldb_val(msg, ldb, &value);
571 if ( ! ldb_dn_validate(msg->dn)) {
572 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Unable to parse dn '%s'\n",
573 (char *)value.data);
574 goto failed;
577 while (next_attr(ldif, &s, &attr, &value) == 0) {
578 const struct ldb_schema_attribute *a;
579 struct ldb_message_element *el;
580 int ret, empty = 0;
582 if (ldb_attr_cmp(attr, "changetype") == 0) {
583 int i;
584 for (i=0;ldb_changetypes[i].name;i++) {
585 if (ldb_attr_cmp((char *)value.data, ldb_changetypes[i].name) == 0) {
586 ldif->changetype = ldb_changetypes[i].changetype;
587 break;
590 if (!ldb_changetypes[i].name) {
591 ldb_debug(ldb, LDB_DEBUG_ERROR,
592 "Error: Bad ldif changetype '%s'\n",(char *)value.data);
594 flags = 0;
595 continue;
598 if (ldb_attr_cmp(attr, "add") == 0) {
599 flags = LDB_FLAG_MOD_ADD;
600 empty = 1;
602 if (ldb_attr_cmp(attr, "delete") == 0) {
603 flags = LDB_FLAG_MOD_DELETE;
604 empty = 1;
606 if (ldb_attr_cmp(attr, "replace") == 0) {
607 flags = LDB_FLAG_MOD_REPLACE;
608 empty = 1;
610 if (ldb_attr_cmp(attr, "-") == 0) {
611 flags = 0;
612 continue;
615 if (empty) {
616 if (ldb_msg_add_empty(msg, (char *)value.data, flags, NULL) != 0) {
617 goto failed;
619 continue;
622 el = &msg->elements[msg->num_elements-1];
624 a = ldb_schema_attribute_by_name(ldb, attr);
626 if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
627 flags == el->flags) {
628 /* its a continuation */
629 el->values =
630 talloc_realloc(msg->elements, el->values,
631 struct ldb_val, el->num_values+1);
632 if (!el->values) {
633 goto failed;
635 ret = a->syntax->ldif_read_fn(ldb, ldif, &value, &el->values[el->num_values]);
636 if (ret != 0) {
637 goto failed;
639 if (value.length == 0) {
640 ldb_debug(ldb, LDB_DEBUG_ERROR,
641 "Error: Attribute value cannot be empty for attribute '%s'\n", el->name);
642 goto failed;
644 if (value.data != el->values[el->num_values].data) {
645 talloc_steal(el->values, el->values[el->num_values].data);
647 el->num_values++;
648 } else {
649 /* its a new attribute */
650 msg->elements = talloc_realloc(ldif, msg->elements,
651 struct ldb_message_element,
652 msg->num_elements+1);
653 if (!msg->elements) {
654 goto failed;
656 el = &msg->elements[msg->num_elements];
657 el->flags = flags;
658 el->name = talloc_strdup(msg->elements, attr);
659 el->values = talloc(msg->elements, struct ldb_val);
660 if (!el->values || !el->name) {
661 goto failed;
663 el->num_values = 1;
664 ret = a->syntax->ldif_read_fn(ldb, ldif, &value, &el->values[0]);
665 if (ret != 0) {
666 goto failed;
668 if (value.data != el->values[0].data) {
669 talloc_steal(el->values, el->values[0].data);
671 msg->num_elements++;
675 return ldif;
677 failed:
678 talloc_free(ldif);
679 return NULL;
685 a wrapper around ldif_read() for reading from FILE*
687 struct ldif_read_file_state {
688 FILE *f;
691 static int fgetc_file(void *private_data)
693 struct ldif_read_file_state *state =
694 (struct ldif_read_file_state *)private_data;
695 return fgetc(state->f);
698 struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f)
700 struct ldif_read_file_state state;
701 state.f = f;
702 return ldb_ldif_read(ldb, fgetc_file, &state);
707 a wrapper around ldif_read() for reading from const char*
709 struct ldif_read_string_state {
710 const char *s;
713 static int fgetc_string(void *private_data)
715 struct ldif_read_string_state *state =
716 (struct ldif_read_string_state *)private_data;
717 if (state->s[0] != 0) {
718 return *state->s++;
720 return EOF;
723 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char **s)
725 struct ldif_read_string_state state;
726 struct ldb_ldif *ldif;
727 state.s = *s;
728 ldif = ldb_ldif_read(ldb, fgetc_string, &state);
729 *s = state.s;
730 return ldif;
735 wrapper around ldif_write() for a file
737 struct ldif_write_file_state {
738 FILE *f;
741 static int fprintf_file(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
743 static int fprintf_file(void *private_data, const char *fmt, ...)
745 struct ldif_write_file_state *state =
746 (struct ldif_write_file_state *)private_data;
747 int ret;
748 va_list ap;
750 va_start(ap, fmt);
751 ret = vfprintf(state->f, fmt, ap);
752 va_end(ap);
753 return ret;
756 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
758 struct ldif_write_file_state state;
759 state.f = f;
760 return ldb_ldif_write(ldb, fprintf_file, &state, ldif);