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 2 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, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: ldif routines
30 * Description: ldif pack/unpack routines
32 * Author: Andrew Tridgell
36 see RFC2849 for the LDIF format definition
40 #include "ldb/include/ldb.h"
41 #include "ldb/include/ldb_private.h"
44 #include "system/filesys.h"
48 add to the list of ldif handlers for this ldb context
50 int ldb_ldif_add_handlers(struct ldb_context *ldb,
51 const struct ldb_ldif_handler *handlers,
52 unsigned num_handlers)
54 struct ldb_ldif_handler *h;
55 h = talloc_realloc(ldb, ldb->ldif_handlers,
56 struct ldb_ldif_handler,
57 ldb->ldif_num_handlers + num_handlers);
62 ldb->ldif_handlers = h;
63 memcpy(h + ldb->ldif_num_handlers,
64 handlers, sizeof(*h) * num_handlers);
65 ldb->ldif_num_handlers += num_handlers;
71 default function for ldif read/write
73 static int ldb_ldif_default(struct ldb_context *ldb, const struct ldb_val *in,
82 return a function for reading an ldif encoded attributes into a ldb_val
84 static ldb_ldif_handler_t ldb_ldif_read_fn(struct ldb_context *ldb, const char *attr)
87 for (i=0;i<ldb->ldif_num_handlers;i++) {
88 if (ldb_attr_cmp(attr, ldb->ldif_handlers[i].attr) == 0) {
89 return ldb->ldif_handlers[i].read_fn;
92 return ldb_ldif_default;
96 return a function for writing an ldif encoded attribute from a ldb_val
98 static ldb_ldif_handler_t ldb_ldif_write_fn(struct ldb_context *ldb, const char *attr)
101 for (i=0;i<ldb->ldif_num_handlers;i++) {
102 if (ldb_attr_cmp(attr, ldb->ldif_handlers[i].attr) == 0) {
103 return ldb->ldif_handlers[i].write_fn;
106 return ldb_ldif_default;
112 static int ldb_read_data_file(void *mem_ctx, struct ldb_val *value)
116 int count, size, bytes;
120 f = open(value->data, O_RDONLY);
125 if (fstat(f, &statbuf) != 0) {
130 if (statbuf.st_size == 0) {
135 value->data = talloc_size(mem_ctx, statbuf.st_size + 1);
136 if (value->data == NULL) {
140 value->data[statbuf.st_size] = 0;
143 size = statbuf.st_size;
145 while (count < statbuf.st_size) {
146 bytes = read(f, buf, size);
148 talloc_free(value->data);
157 value->length = statbuf.st_size;
158 ret = statbuf.st_size;
166 this base64 decoder was taken from jitterbug (written by tridge).
167 we might need to replace it with a new version
169 int ldb_base64_decode(char *s)
171 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
172 int bit_offset=0, byte_offset, idx, i, n;
173 uint8_t *d = (uint8_t *)s;
178 while (*s && (p=strchr(b64,*s))) {
179 idx = (int)(p - b64);
180 byte_offset = (i*6)/8;
181 bit_offset = (i*6)%8;
182 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
183 if (bit_offset < 3) {
184 d[byte_offset] |= (idx << (2-bit_offset));
187 d[byte_offset] |= (idx >> (bit_offset-2));
188 d[byte_offset+1] = 0;
189 d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
194 if (bit_offset >= 3) {
199 /* the only termination allowed */
215 char *ldb_base64_encode(void *mem_ctx, const char *buf, int len)
217 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
218 int bit_offset, byte_offset, idx, i;
219 const uint8_t *d = (const uint8_t *)buf;
220 int bytes = (len*8 + 5)/6;
223 out = talloc_array(mem_ctx, char, bytes+2);
224 if (!out) return NULL;
226 for (i=0;i<bytes;i++) {
227 byte_offset = (i*6)/8;
228 bit_offset = (i*6)%8;
229 if (bit_offset < 3) {
230 idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
232 idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
233 if (byte_offset+1 < len) {
234 idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
247 see if a buffer should be base64 encoded
249 int ldb_should_b64_encode(const struct ldb_val *val)
252 uint8_t *p = val->data;
254 if (val->length == 0) {
258 if (p[0] == ' ' || p[0] == ':') {
262 for (i=0; i<val->length; i++) {
263 if (!isprint(p[i]) || p[i] == '\n') {
270 /* this macro is used to handle the return checking on fprintf_fn() */
271 #define CHECK_RET do { if (ret < 0) return ret; total += ret; } while (0)
274 write a line folded string onto a file
276 static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *private_data,
277 const char *buf, size_t length, int start_pos)
282 for (i=0;i<length;i++) {
283 ret = fprintf_fn(private_data, "%c", buf[i]);
285 if (i != (length-1) && (i + start_pos) % 77 == 0) {
286 ret = fprintf_fn(private_data, "\n ");
295 encode as base64 to a file
297 static int base64_encode_f(struct ldb_context *ldb,
298 int (*fprintf_fn)(void *, const char *, ...),
300 const char *buf, int len, int start_pos)
302 char *b = ldb_base64_encode(ldb, buf, len);
309 ret = fold_string(fprintf_fn, private_data, b, strlen(b), start_pos);
316 static const struct {
318 enum ldb_changetype changetype;
319 } ldb_changetypes[] = {
320 {"add", LDB_CHANGETYPE_ADD},
321 {"delete", LDB_CHANGETYPE_DELETE},
322 {"modify", LDB_CHANGETYPE_MODIFY},
327 write to ldif, using a caller supplied write method
329 int ldb_ldif_write(struct ldb_context *ldb,
330 int (*fprintf_fn)(void *, const char *, ...),
332 const struct ldb_ldif *ldif)
336 const struct ldb_message *msg;
340 ret = fprintf_fn(private_data, "dn: %s\n", msg->dn);
343 if (ldif->changetype != LDB_CHANGETYPE_NONE) {
344 for (i=0;ldb_changetypes[i].name;i++) {
345 if (ldb_changetypes[i].changetype == ldif->changetype) {
349 if (!ldb_changetypes[i].name) {
350 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d\n",
354 ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
358 for (i=0;i<msg->num_elements;i++) {
359 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
360 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
361 case LDB_FLAG_MOD_ADD:
362 fprintf_fn(private_data, "add: %s\n",
363 msg->elements[i].name);
365 case LDB_FLAG_MOD_DELETE:
366 fprintf_fn(private_data, "delete: %s\n",
367 msg->elements[i].name);
369 case LDB_FLAG_MOD_REPLACE:
370 fprintf_fn(private_data, "replace: %s\n",
371 msg->elements[i].name);
376 for (j=0;j<msg->elements[i].num_values;j++) {
377 ldb_ldif_handler_t write_fn = ldb_ldif_write_fn(ldb,
378 msg->elements[i].name);
380 ret = write_fn(ldb, &msg->elements[i].values[j], &v);
382 if (ldb_should_b64_encode(&v)) {
383 ret = fprintf_fn(private_data, "%s:: ",
384 msg->elements[i].name);
386 ret = base64_encode_f(ldb, fprintf_fn, private_data,
388 strlen(msg->elements[i].name)+3);
390 ret = fprintf_fn(private_data, "\n");
393 ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
395 ret = fold_string(fprintf_fn, private_data,
397 strlen(msg->elements[i].name)+2);
399 ret = fprintf_fn(private_data, "\n");
402 if (v.data != msg->elements[i].values[j].data) {
406 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
407 fprintf_fn(private_data, "-\n");
410 ret = fprintf_fn(private_data,"\n");
420 pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
421 this routine removes any RFC2849 continuations and comments
425 static char *next_chunk(struct ldb_context *ldb,
426 int (*fgetc_fn)(void *), void *private_data)
428 size_t alloc_size=0, chunk_size = 0;
433 while ((c = fgetc_fn(private_data)) != EOF) {
434 if (chunk_size+1 >= alloc_size) {
437 c2 = talloc_realloc(ldb, chunk, char, alloc_size);
453 /* handle continuation lines - see RFC2849 */
454 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
459 /* chunks are terminated by a double line-feed */
460 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
461 chunk[chunk_size-1] = 0;
465 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
470 /* ignore leading blank lines */
471 if (chunk_size == 0 && c == '\n') {
475 chunk[chunk_size++] = c;
479 chunk[chunk_size] = 0;
486 /* simple ldif attribute parser */
487 static int next_attr(void *mem_ctx, char **s, const char **attr, struct ldb_val *value)
490 int base64_encoded = 0;
493 if (strncmp(*s, "-\n", 2) == 0) {
519 while (*p == ' ' || *p == '\t') {
528 value->length = strlen((char *)value->data);
529 *s = ((char *)value->data) + value->length;
531 value->length = p - (char *)value->data;
536 if (base64_encoded) {
537 int len = ldb_base64_decode(value->data);
539 /* it wasn't valid base64 data */
546 int len = ldb_read_data_file(mem_ctx, value);
548 /* an error occured hile trying to retrieve the file */
558 free a message from a ldif_read
560 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
568 static int msg_add_empty(struct ldb_context *ldb,
569 struct ldb_message *msg, const char *name, unsigned flags)
571 struct ldb_message_element *el2, *el;
573 el2 = talloc_realloc(msg, msg->elements,
574 struct ldb_message_element, msg->num_elements+1);
582 el = &msg->elements[msg->num_elements];
584 el->name = talloc_strdup(msg->elements, name);
600 read from a LDIF source, creating a ldb_message
602 struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
603 int (*fgetc_fn)(void *), void *private_data)
605 struct ldb_ldif *ldif;
606 struct ldb_message *msg;
607 const char *attr=NULL;
608 char *chunk=NULL, *s;
609 struct ldb_val value;
614 ldif = talloc(ldb, struct ldb_ldif);
615 if (!ldif) return NULL;
617 ldif->msg = talloc(ldif, struct ldb_message);
618 if (ldif->msg == NULL) {
623 ldif->changetype = LDB_CHANGETYPE_NONE;
627 msg->elements = NULL;
628 msg->num_elements = 0;
629 msg->private_data = NULL;
631 chunk = next_chunk(ldb, fgetc_fn, private_data);
636 msg->private_data = chunk;
639 if (next_attr(ldif, &s, &attr, &value) != 0) {
643 /* first line must be a dn */
644 if (ldb_attr_cmp(attr, "dn") != 0) {
645 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'\n",
650 msg->dn = value.data;
652 while (next_attr(ldif, &s, &attr, &value) == 0) {
653 ldb_ldif_handler_t read_fn;
654 struct ldb_message_element *el;
657 if (ldb_attr_cmp(attr, "changetype") == 0) {
659 for (i=0;ldb_changetypes[i].name;i++) {
660 if (ldb_attr_cmp((char *)value.data, ldb_changetypes[i].name) == 0) {
661 ldif->changetype = ldb_changetypes[i].changetype;
665 if (!ldb_changetypes[i].name) {
666 ldb_debug(ldb, LDB_DEBUG_ERROR,
667 "Error: Bad ldif changetype '%s'\n",(char *)value.data);
673 if (ldb_attr_cmp(attr, "add") == 0) {
674 flags = LDB_FLAG_MOD_ADD;
677 if (ldb_attr_cmp(attr, "delete") == 0) {
678 flags = LDB_FLAG_MOD_DELETE;
681 if (ldb_attr_cmp(attr, "replace") == 0) {
682 flags = LDB_FLAG_MOD_REPLACE;
685 if (ldb_attr_cmp(attr, "-") == 0) {
691 if (msg_add_empty(ldb, msg, (char *)value.data, flags) != 0) {
697 el = &msg->elements[msg->num_elements-1];
699 read_fn = ldb_ldif_read_fn(ldb, attr);
701 if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
702 flags == el->flags) {
703 /* its a continuation */
705 talloc_realloc(msg->elements, el->values,
706 struct ldb_val, el->num_values+1);
710 ret = read_fn(ldb, &value, &el->values[el->num_values]);
714 if (value.data != el->values[el->num_values].data) {
715 talloc_steal(el->values, el->values[el->num_values].data);
719 /* its a new attribute */
720 msg->elements = talloc_realloc(ldif, msg->elements,
721 struct ldb_message_element,
722 msg->num_elements+1);
723 if (!msg->elements) {
726 el = &msg->elements[msg->num_elements];
728 el->name = talloc_strdup(msg->elements, attr);
729 el->values = talloc(msg->elements, struct ldb_val);
730 if (!el->values || !el->name) {
734 ret = read_fn(ldb, &value, &el->values[0]);
738 if (value.data != el->values[0].data) {
739 talloc_steal(el->values, el->values[0].data);
755 a wrapper around ldif_read() for reading from FILE*
757 struct ldif_read_file_state {
761 static int fgetc_file(void *private_data)
763 struct ldif_read_file_state *state = private_data;
764 return fgetc(state->f);
767 struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f)
769 struct ldif_read_file_state state;
771 return ldb_ldif_read(ldb, fgetc_file, &state);
776 a wrapper around ldif_read() for reading from const char*
778 struct ldif_read_string_state {
782 static int fgetc_string(void *private_data)
784 struct ldif_read_string_state *state = private_data;
785 if (state->s[0] != 0) {
791 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char *s)
793 struct ldif_read_string_state state;
795 return ldb_ldif_read(ldb, fgetc_string, &state);
800 wrapper around ldif_write() for a file
802 struct ldif_write_file_state {
806 static int fprintf_file(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
808 static int fprintf_file(void *private_data, const char *fmt, ...)
810 struct ldif_write_file_state *state = private_data;
815 ret = vfprintf(state->f, fmt, ap);
820 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
822 struct ldif_write_file_state state;
824 return ldb_ldif_write(ldb, fprintf_file, &state, ldif);