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"
45 default function for ldif read/write
47 static int ldb_ldif_default(struct ldb_context *ldb, const struct ldb_val *in,
56 return a function for reading an ldif encoded attributes into a ldb_val
58 static ldb_ldif_handler_t ldb_ldif_read_fn(struct ldb_context *ldb, const char *attr)
61 for (i=0;i<ldb->ldif_num_handlers;i++) {
62 if (strcmp(attr, ldb->ldif_handlers[i].attr) == 0) {
63 return ldb->ldif_handlers[i].read_fn;
66 return ldb_ldif_default;
70 return a function for writing an ldif encoded attribute from a ldb_val
72 static ldb_ldif_handler_t ldb_ldif_write_fn(struct ldb_context *ldb, const char *attr)
75 for (i=0;i<ldb->ldif_num_handlers;i++) {
76 if (strcmp(attr, ldb->ldif_handlers[i].attr) == 0) {
77 return ldb->ldif_handlers[i].write_fn;
80 return ldb_ldif_default;
84 this base64 decoder was taken from jitterbug (written by tridge).
85 we might need to replace it with a new version
87 int ldb_base64_decode(char *s)
89 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
90 int bit_offset=0, byte_offset, idx, i, n;
91 uint8_t *d = (uint8_t *)s;
96 while (*s && (p=strchr(b64,*s))) {
98 byte_offset = (i*6)/8;
100 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
101 if (bit_offset < 3) {
102 d[byte_offset] |= (idx << (2-bit_offset));
105 d[byte_offset] |= (idx >> (bit_offset-2));
106 d[byte_offset+1] = 0;
107 d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
112 if (bit_offset >= 3) {
117 /* the only termination allowed */
133 char *ldb_base64_encode(void *mem_ctx, const char *buf, int len)
135 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
136 int bit_offset, byte_offset, idx, i;
137 const uint8_t *d = (const uint8_t *)buf;
138 int bytes = (len*8 + 5)/6;
141 out = talloc_array(mem_ctx, char, bytes+2);
142 if (!out) return NULL;
144 for (i=0;i<bytes;i++) {
145 byte_offset = (i*6)/8;
146 bit_offset = (i*6)%8;
147 if (bit_offset < 3) {
148 idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
150 idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
151 if (byte_offset+1 < len) {
152 idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
165 see if a buffer should be base64 encoded
167 int ldb_should_b64_encode(const struct ldb_val *val)
170 uint8_t *p = val->data;
172 if (val->length == 0) {
176 if (p[0] == ' ' || p[0] == ':') {
180 for (i=0; i<val->length; i++) {
181 if (!isprint(p[i]) || p[i] == '\n') {
188 /* this macro is used to handle the return checking on fprintf_fn() */
189 #define CHECK_RET do { if (ret < 0) return ret; total += ret; } while (0)
192 write a line folded string onto a file
194 static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *private_data,
195 const char *buf, size_t length, int start_pos)
200 for (i=0;i<length;i++) {
201 ret = fprintf_fn(private_data, "%c", buf[i]);
203 if (i != (length-1) && (i + start_pos) % 77 == 0) {
204 ret = fprintf_fn(private_data, "\n ");
213 encode as base64 to a file
215 static int base64_encode_f(struct ldb_context *ldb,
216 int (*fprintf_fn)(void *, const char *, ...),
218 const char *buf, int len, int start_pos)
220 char *b = ldb_base64_encode(ldb, buf, len);
227 ret = fold_string(fprintf_fn, private_data, b, strlen(b), start_pos);
234 static const struct {
236 enum ldb_changetype changetype;
237 } ldb_changetypes[] = {
238 {"add", LDB_CHANGETYPE_ADD},
239 {"delete", LDB_CHANGETYPE_DELETE},
240 {"modify", LDB_CHANGETYPE_MODIFY},
245 write to ldif, using a caller supplied write method
247 int ldb_ldif_write(struct ldb_context *ldb,
248 int (*fprintf_fn)(void *, const char *, ...),
250 const struct ldb_ldif *ldif)
254 const struct ldb_message *msg;
258 ret = fprintf_fn(private_data, "dn: %s\n", msg->dn);
261 if (ldif->changetype != LDB_CHANGETYPE_NONE) {
262 for (i=0;ldb_changetypes[i].name;i++) {
263 if (ldb_changetypes[i].changetype == ldif->changetype) {
267 if (!ldb_changetypes[i].name) {
268 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d\n",
272 ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
276 for (i=0;i<msg->num_elements;i++) {
277 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
278 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
279 case LDB_FLAG_MOD_ADD:
280 fprintf_fn(private_data, "add: %s\n",
281 msg->elements[i].name);
283 case LDB_FLAG_MOD_DELETE:
284 fprintf_fn(private_data, "delete: %s\n",
285 msg->elements[i].name);
287 case LDB_FLAG_MOD_REPLACE:
288 fprintf_fn(private_data, "replace: %s\n",
289 msg->elements[i].name);
294 for (j=0;j<msg->elements[i].num_values;j++) {
295 ldb_ldif_handler_t write_fn = ldb_ldif_write_fn(ldb,
296 msg->elements[i].name);
298 ret = write_fn(ldb, &msg->elements[i].values[j], &v);
300 if (ldb_should_b64_encode(&v)) {
301 ret = fprintf_fn(private_data, "%s:: ",
302 msg->elements[i].name);
304 ret = base64_encode_f(ldb, fprintf_fn, private_data,
306 strlen(msg->elements[i].name)+3);
308 ret = fprintf_fn(private_data, "\n");
311 ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
313 ret = fold_string(fprintf_fn, private_data,
315 strlen(msg->elements[i].name)+2);
317 ret = fprintf_fn(private_data, "\n");
320 if (v.data != msg->elements[i].values[j].data) {
324 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
325 fprintf_fn(private_data, "-\n");
328 ret = fprintf_fn(private_data,"\n");
338 pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
339 this routine removes any RFC2849 continuations and comments
343 static char *next_chunk(struct ldb_context *ldb,
344 int (*fgetc_fn)(void *), void *private_data)
346 size_t alloc_size=0, chunk_size = 0;
351 while ((c = fgetc_fn(private_data)) != EOF) {
352 if (chunk_size+1 >= alloc_size) {
355 c2 = talloc_realloc(ldb, chunk, char, alloc_size);
371 /* handle continuation lines - see RFC2849 */
372 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
377 /* chunks are terminated by a double line-feed */
378 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
379 chunk[chunk_size-1] = 0;
383 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
388 /* ignore leading blank lines */
389 if (chunk_size == 0 && c == '\n') {
393 chunk[chunk_size++] = c;
397 chunk[chunk_size] = 0;
404 /* simple ldif attribute parser */
405 static int next_attr(char **s, const char **attr, struct ldb_val *value)
408 int base64_encoded = 0;
410 if (strncmp(*s, "-\n", 2) == 0) {
431 while (*p == ' ' || *p == '\t') {
440 value->length = strlen((char *)value->data);
441 *s = ((char *)value->data) + value->length;
443 value->length = p - (char *)value->data;
448 if (base64_encoded) {
449 int len = ldb_base64_decode(value->data);
451 /* it wasn't valid base64 data */
462 free a message from a ldif_read
464 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
472 static int msg_add_empty(struct ldb_context *ldb,
473 struct ldb_message *msg, const char *name, unsigned flags)
475 struct ldb_message_element *el2, *el;
477 el2 = talloc_realloc(msg, msg->elements,
478 struct ldb_message_element, msg->num_elements+1);
486 el = &msg->elements[msg->num_elements];
488 el->name = talloc_strdup(msg->elements, name);
504 read from a LDIF source, creating a ldb_message
506 struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
507 int (*fgetc_fn)(void *), void *private_data)
509 struct ldb_ldif *ldif;
510 struct ldb_message *msg;
511 const char *attr=NULL;
512 char *chunk=NULL, *s;
513 struct ldb_val value;
518 ldif = talloc(ldb, struct ldb_ldif);
519 if (!ldif) return NULL;
521 ldif->msg = talloc(ldif, struct ldb_message);
522 if (ldif->msg == NULL) {
527 ldif->changetype = LDB_CHANGETYPE_NONE;
531 msg->elements = NULL;
532 msg->num_elements = 0;
533 msg->private_data = NULL;
535 chunk = next_chunk(ldb, fgetc_fn, private_data);
540 msg->private_data = chunk;
543 if (next_attr(&s, &attr, &value) != 0) {
547 /* first line must be a dn */
548 if (ldb_attr_cmp(attr, "dn") != 0) {
549 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'\n",
554 msg->dn = value.data;
556 while (next_attr(&s, &attr, &value) == 0) {
557 ldb_ldif_handler_t read_fn;
558 struct ldb_message_element *el;
561 if (ldb_attr_cmp(attr, "changetype") == 0) {
563 for (i=0;ldb_changetypes[i].name;i++) {
564 if (ldb_attr_cmp((char *)value.data, ldb_changetypes[i].name) == 0) {
565 ldif->changetype = ldb_changetypes[i].changetype;
569 if (!ldb_changetypes[i].name) {
570 ldb_debug(ldb, LDB_DEBUG_ERROR,
571 "Error: Bad ldif changetype '%s'\n",(char *)value.data);
577 if (ldb_attr_cmp(attr, "add") == 0) {
578 flags = LDB_FLAG_MOD_ADD;
581 if (ldb_attr_cmp(attr, "delete") == 0) {
582 flags = LDB_FLAG_MOD_DELETE;
585 if (ldb_attr_cmp(attr, "replace") == 0) {
586 flags = LDB_FLAG_MOD_REPLACE;
589 if (ldb_attr_cmp(attr, "-") == 0) {
595 if (msg_add_empty(ldb, msg, (char *)value.data, flags) != 0) {
601 el = &msg->elements[msg->num_elements-1];
603 read_fn = ldb_ldif_read_fn(ldb, attr);
605 if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
606 flags == el->flags) {
607 /* its a continuation */
609 talloc_realloc(msg->elements, el->values,
610 struct ldb_val, el->num_values+1);
614 ret = read_fn(ldb, &value, &el->values[el->num_values]);
618 if (value.data != el->values[el->num_values].data) {
619 talloc_steal(el->values, el->values[el->num_values].data);
623 /* its a new attribute */
624 msg->elements = talloc_realloc(ldif, msg->elements,
625 struct ldb_message_element,
626 msg->num_elements+1);
627 if (!msg->elements) {
630 el = &msg->elements[msg->num_elements];
632 el->name = talloc_strdup(msg->elements, attr);
633 el->values = talloc(msg->elements, struct ldb_val);
634 if (!el->values || !el->name) {
638 ret = read_fn(ldb, &value, &el->values[0]);
642 if (value.data != el->values[0].data) {
643 talloc_steal(el->values, el->values[0].data);
659 a wrapper around ldif_read() for reading from FILE*
661 struct ldif_read_file_state {
665 static int fgetc_file(void *private_data)
667 struct ldif_read_file_state *state = private_data;
668 return fgetc(state->f);
671 struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f)
673 struct ldif_read_file_state state;
675 return ldb_ldif_read(ldb, fgetc_file, &state);
680 a wrapper around ldif_read() for reading from const char*
682 struct ldif_read_string_state {
686 static int fgetc_string(void *private_data)
688 struct ldif_read_string_state *state = private_data;
689 if (state->s[0] != 0) {
695 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char *s)
697 struct ldif_read_string_state state;
699 return ldb_ldif_read(ldb, fgetc_string, &state);
704 wrapper around ldif_write() for a file
706 struct ldif_write_file_state {
710 static int fprintf_file(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
712 static int fprintf_file(void *private_data, const char *fmt, ...)
714 struct ldif_write_file_state *state = private_data;
719 ret = vfprintf(state->f, fmt, ap);
724 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
726 struct ldif_write_file_state state;
728 return ldb_ldif_write(ldb, fprintf_file, &state, ldif);