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"
46 add to the list of ldif handlers for this ldb context
48 int ldb_ldif_add_handlers(struct ldb_context *ldb,
49 const struct ldb_ldif_handler *handlers,
50 unsigned num_handlers)
52 struct ldb_ldif_handler *h;
53 h = talloc_realloc(ldb, ldb->ldif_handlers,
54 struct ldb_ldif_handler,
55 ldb->ldif_num_handlers + num_handlers);
60 ldb->ldif_handlers = h;
61 memcpy(h + ldb->ldif_num_handlers,
62 handlers, sizeof(*h) * num_handlers);
63 ldb->ldif_num_handlers += num_handlers;
69 default function for ldif read/write
71 static int ldb_ldif_default(struct ldb_context *ldb, const struct ldb_val *in,
80 return a function for reading an ldif encoded attributes into a ldb_val
82 static ldb_ldif_handler_t ldb_ldif_read_fn(struct ldb_context *ldb, const char *attr)
85 for (i=0;i<ldb->ldif_num_handlers;i++) {
86 if (ldb_attr_cmp(attr, ldb->ldif_handlers[i].attr) == 0) {
87 return ldb->ldif_handlers[i].read_fn;
90 return ldb_ldif_default;
94 return a function for writing an ldif encoded attribute from a ldb_val
96 static ldb_ldif_handler_t ldb_ldif_write_fn(struct ldb_context *ldb, const char *attr)
99 for (i=0;i<ldb->ldif_num_handlers;i++) {
100 if (ldb_attr_cmp(attr, ldb->ldif_handlers[i].attr) == 0) {
101 return ldb->ldif_handlers[i].write_fn;
104 return ldb_ldif_default;
108 this base64 decoder was taken from jitterbug (written by tridge).
109 we might need to replace it with a new version
111 int ldb_base64_decode(char *s)
113 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
114 int bit_offset=0, byte_offset, idx, i, n;
115 uint8_t *d = (uint8_t *)s;
120 while (*s && (p=strchr(b64,*s))) {
121 idx = (int)(p - b64);
122 byte_offset = (i*6)/8;
123 bit_offset = (i*6)%8;
124 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
125 if (bit_offset < 3) {
126 d[byte_offset] |= (idx << (2-bit_offset));
129 d[byte_offset] |= (idx >> (bit_offset-2));
130 d[byte_offset+1] = 0;
131 d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
136 if (bit_offset >= 3) {
141 /* the only termination allowed */
157 char *ldb_base64_encode(void *mem_ctx, const char *buf, int len)
159 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
160 int bit_offset, byte_offset, idx, i;
161 const uint8_t *d = (const uint8_t *)buf;
162 int bytes = (len*8 + 5)/6;
165 out = talloc_array(mem_ctx, char, bytes+2);
166 if (!out) return NULL;
168 for (i=0;i<bytes;i++) {
169 byte_offset = (i*6)/8;
170 bit_offset = (i*6)%8;
171 if (bit_offset < 3) {
172 idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
174 idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
175 if (byte_offset+1 < len) {
176 idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
189 see if a buffer should be base64 encoded
191 int ldb_should_b64_encode(const struct ldb_val *val)
194 uint8_t *p = val->data;
196 if (val->length == 0) {
200 if (p[0] == ' ' || p[0] == ':') {
204 for (i=0; i<val->length; i++) {
205 if (!isprint(p[i]) || p[i] == '\n') {
212 /* this macro is used to handle the return checking on fprintf_fn() */
213 #define CHECK_RET do { if (ret < 0) return ret; total += ret; } while (0)
216 write a line folded string onto a file
218 static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *private_data,
219 const char *buf, size_t length, int start_pos)
224 for (i=0;i<length;i++) {
225 ret = fprintf_fn(private_data, "%c", buf[i]);
227 if (i != (length-1) && (i + start_pos) % 77 == 0) {
228 ret = fprintf_fn(private_data, "\n ");
237 encode as base64 to a file
239 static int base64_encode_f(struct ldb_context *ldb,
240 int (*fprintf_fn)(void *, const char *, ...),
242 const char *buf, int len, int start_pos)
244 char *b = ldb_base64_encode(ldb, buf, len);
251 ret = fold_string(fprintf_fn, private_data, b, strlen(b), start_pos);
258 static const struct {
260 enum ldb_changetype changetype;
261 } ldb_changetypes[] = {
262 {"add", LDB_CHANGETYPE_ADD},
263 {"delete", LDB_CHANGETYPE_DELETE},
264 {"modify", LDB_CHANGETYPE_MODIFY},
269 write to ldif, using a caller supplied write method
271 int ldb_ldif_write(struct ldb_context *ldb,
272 int (*fprintf_fn)(void *, const char *, ...),
274 const struct ldb_ldif *ldif)
278 const struct ldb_message *msg;
282 ret = fprintf_fn(private_data, "dn: %s\n", msg->dn);
285 if (ldif->changetype != LDB_CHANGETYPE_NONE) {
286 for (i=0;ldb_changetypes[i].name;i++) {
287 if (ldb_changetypes[i].changetype == ldif->changetype) {
291 if (!ldb_changetypes[i].name) {
292 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d\n",
296 ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
300 for (i=0;i<msg->num_elements;i++) {
301 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
302 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
303 case LDB_FLAG_MOD_ADD:
304 fprintf_fn(private_data, "add: %s\n",
305 msg->elements[i].name);
307 case LDB_FLAG_MOD_DELETE:
308 fprintf_fn(private_data, "delete: %s\n",
309 msg->elements[i].name);
311 case LDB_FLAG_MOD_REPLACE:
312 fprintf_fn(private_data, "replace: %s\n",
313 msg->elements[i].name);
318 for (j=0;j<msg->elements[i].num_values;j++) {
319 ldb_ldif_handler_t write_fn = ldb_ldif_write_fn(ldb,
320 msg->elements[i].name);
322 ret = write_fn(ldb, &msg->elements[i].values[j], &v);
324 if (ldb_should_b64_encode(&v)) {
325 ret = fprintf_fn(private_data, "%s:: ",
326 msg->elements[i].name);
328 ret = base64_encode_f(ldb, fprintf_fn, private_data,
330 strlen(msg->elements[i].name)+3);
332 ret = fprintf_fn(private_data, "\n");
335 ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
337 ret = fold_string(fprintf_fn, private_data,
339 strlen(msg->elements[i].name)+2);
341 ret = fprintf_fn(private_data, "\n");
344 if (v.data != msg->elements[i].values[j].data) {
348 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
349 fprintf_fn(private_data, "-\n");
352 ret = fprintf_fn(private_data,"\n");
362 pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
363 this routine removes any RFC2849 continuations and comments
367 static char *next_chunk(struct ldb_context *ldb,
368 int (*fgetc_fn)(void *), void *private_data)
370 size_t alloc_size=0, chunk_size = 0;
375 while ((c = fgetc_fn(private_data)) != EOF) {
376 if (chunk_size+1 >= alloc_size) {
379 c2 = talloc_realloc(ldb, chunk, char, alloc_size);
395 /* handle continuation lines - see RFC2849 */
396 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
401 /* chunks are terminated by a double line-feed */
402 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
403 chunk[chunk_size-1] = 0;
407 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
412 /* ignore leading blank lines */
413 if (chunk_size == 0 && c == '\n') {
417 chunk[chunk_size++] = c;
421 chunk[chunk_size] = 0;
428 /* simple ldif attribute parser */
429 static int next_attr(char **s, const char **attr, struct ldb_val *value)
432 int base64_encoded = 0;
434 if (strncmp(*s, "-\n", 2) == 0) {
455 while (*p == ' ' || *p == '\t') {
464 value->length = strlen((char *)value->data);
465 *s = ((char *)value->data) + value->length;
467 value->length = p - (char *)value->data;
472 if (base64_encoded) {
473 int len = ldb_base64_decode(value->data);
475 /* it wasn't valid base64 data */
486 free a message from a ldif_read
488 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
496 static int msg_add_empty(struct ldb_context *ldb,
497 struct ldb_message *msg, const char *name, unsigned flags)
499 struct ldb_message_element *el2, *el;
501 el2 = talloc_realloc(msg, msg->elements,
502 struct ldb_message_element, msg->num_elements+1);
510 el = &msg->elements[msg->num_elements];
512 el->name = talloc_strdup(msg->elements, name);
528 read from a LDIF source, creating a ldb_message
530 struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
531 int (*fgetc_fn)(void *), void *private_data)
533 struct ldb_ldif *ldif;
534 struct ldb_message *msg;
535 const char *attr=NULL;
536 char *chunk=NULL, *s;
537 struct ldb_val value;
542 ldif = talloc(ldb, struct ldb_ldif);
543 if (!ldif) return NULL;
545 ldif->msg = talloc(ldif, struct ldb_message);
546 if (ldif->msg == NULL) {
551 ldif->changetype = LDB_CHANGETYPE_NONE;
555 msg->elements = NULL;
556 msg->num_elements = 0;
557 msg->private_data = NULL;
559 chunk = next_chunk(ldb, fgetc_fn, private_data);
564 msg->private_data = chunk;
567 if (next_attr(&s, &attr, &value) != 0) {
571 /* first line must be a dn */
572 if (ldb_attr_cmp(attr, "dn") != 0) {
573 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'\n",
578 msg->dn = value.data;
580 while (next_attr(&s, &attr, &value) == 0) {
581 ldb_ldif_handler_t read_fn;
582 struct ldb_message_element *el;
585 if (ldb_attr_cmp(attr, "changetype") == 0) {
587 for (i=0;ldb_changetypes[i].name;i++) {
588 if (ldb_attr_cmp((char *)value.data, ldb_changetypes[i].name) == 0) {
589 ldif->changetype = ldb_changetypes[i].changetype;
593 if (!ldb_changetypes[i].name) {
594 ldb_debug(ldb, LDB_DEBUG_ERROR,
595 "Error: Bad ldif changetype '%s'\n",(char *)value.data);
601 if (ldb_attr_cmp(attr, "add") == 0) {
602 flags = LDB_FLAG_MOD_ADD;
605 if (ldb_attr_cmp(attr, "delete") == 0) {
606 flags = LDB_FLAG_MOD_DELETE;
609 if (ldb_attr_cmp(attr, "replace") == 0) {
610 flags = LDB_FLAG_MOD_REPLACE;
613 if (ldb_attr_cmp(attr, "-") == 0) {
619 if (msg_add_empty(ldb, msg, (char *)value.data, flags) != 0) {
625 el = &msg->elements[msg->num_elements-1];
627 read_fn = ldb_ldif_read_fn(ldb, attr);
629 if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
630 flags == el->flags) {
631 /* its a continuation */
633 talloc_realloc(msg->elements, el->values,
634 struct ldb_val, el->num_values+1);
638 ret = read_fn(ldb, &value, &el->values[el->num_values]);
642 if (value.data != el->values[el->num_values].data) {
643 talloc_steal(el->values, el->values[el->num_values].data);
647 /* its a new attribute */
648 msg->elements = talloc_realloc(ldif, msg->elements,
649 struct ldb_message_element,
650 msg->num_elements+1);
651 if (!msg->elements) {
654 el = &msg->elements[msg->num_elements];
656 el->name = talloc_strdup(msg->elements, attr);
657 el->values = talloc(msg->elements, struct ldb_val);
658 if (!el->values || !el->name) {
662 ret = read_fn(ldb, &value, &el->values[0]);
666 if (value.data != el->values[0].data) {
667 talloc_steal(el->values, el->values[0].data);
683 a wrapper around ldif_read() for reading from FILE*
685 struct ldif_read_file_state {
689 static int fgetc_file(void *private_data)
691 struct ldif_read_file_state *state = private_data;
692 return fgetc(state->f);
695 struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f)
697 struct ldif_read_file_state state;
699 return ldb_ldif_read(ldb, fgetc_file, &state);
704 a wrapper around ldif_read() for reading from const char*
706 struct ldif_read_string_state {
710 static int fgetc_string(void *private_data)
712 struct ldif_read_string_state *state = private_data;
713 if (state->s[0] != 0) {
719 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char *s)
721 struct ldif_read_string_state state;
723 return ldb_ldif_read(ldb, fgetc_string, &state);
728 wrapper around ldif_write() for a file
730 struct ldif_write_file_state {
734 static int fprintf_file(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
736 static int fprintf_file(void *private_data, const char *fmt, ...)
738 struct ldif_write_file_state *state = private_data;
743 ret = vfprintf(state->f, fmt, ap);
748 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
750 struct ldif_write_file_state state;
752 return ldb_ldif_write(ldb, fprintf_file, &state, ldif);