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"
50 static int ldb_read_data_file(void *mem_ctx, struct ldb_val *value)
54 int count, size, bytes;
58 f = open(value->data, O_RDONLY);
63 if (fstat(f, &statbuf) != 0) {
68 if (statbuf.st_size == 0) {
73 value->data = talloc_size(mem_ctx, statbuf.st_size + 1);
74 if (value->data == NULL) {
78 value->data[statbuf.st_size] = 0;
81 size = statbuf.st_size;
83 while (count < statbuf.st_size) {
84 bytes = read(f, buf, size);
86 talloc_free(value->data);
95 value->length = statbuf.st_size;
96 ret = statbuf.st_size;
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;
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));
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;
132 if (bit_offset >= 3) {
137 /* the only termination allowed */
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;
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;
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)));
178 for (;i<bytes+pad_bytes;i++)
186 see if a buffer should be base64 encoded
188 int ldb_should_b64_encode(const struct ldb_val *val)
191 uint8_t *p = val->data;
193 if (val->length == 0) {
197 if (p[0] == ' ' || p[0] == ':') {
201 for (i=0; i<val->length; i++) {
202 if (!isprint(p[i]) || p[i] == '\n') {
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)
221 for (i=0;i<length;i++) {
222 ret = fprintf_fn(private_data, "%c", buf[i]);
224 if (i != (length-1) && (i + start_pos) % 77 == 0) {
225 ret = fprintf_fn(private_data, "\n ");
234 encode as base64 to a file
236 static int base64_encode_f(struct ldb_context *ldb,
237 int (*fprintf_fn)(void *, const char *, ...),
239 const char *buf, int len, int start_pos)
241 char *b = ldb_base64_encode(ldb, buf, len);
248 ret = fold_string(fprintf_fn, private_data, b, strlen(b), start_pos);
255 static const struct {
257 enum ldb_changetype changetype;
258 } ldb_changetypes[] = {
259 {"add", LDB_CHANGETYPE_ADD},
260 {"delete", LDB_CHANGETYPE_DELETE},
261 {"modify", LDB_CHANGETYPE_MODIFY},
266 write to ldif, using a caller supplied write method
268 int ldb_ldif_write(struct ldb_context *ldb,
269 int (*fprintf_fn)(void *, const char *, ...),
271 const struct ldb_ldif *ldif)
275 const struct ldb_message *msg;
279 ret = fprintf_fn(private_data, "dn: %s\n", ldb_dn_linearize(msg->dn, msg->dn));
282 if (ldif->changetype != LDB_CHANGETYPE_NONE) {
283 for (i=0;ldb_changetypes[i].name;i++) {
284 if (ldb_changetypes[i].changetype == ldif->changetype) {
288 if (!ldb_changetypes[i].name) {
289 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d\n",
293 ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
297 for (i=0;i<msg->num_elements;i++) {
298 const struct ldb_attrib_handler *h;
300 h = ldb_attrib_handler(ldb, msg->elements[i].name);
302 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
303 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
304 case LDB_FLAG_MOD_ADD:
305 fprintf_fn(private_data, "add: %s\n",
306 msg->elements[i].name);
308 case LDB_FLAG_MOD_DELETE:
309 fprintf_fn(private_data, "delete: %s\n",
310 msg->elements[i].name);
312 case LDB_FLAG_MOD_REPLACE:
313 fprintf_fn(private_data, "replace: %s\n",
314 msg->elements[i].name);
319 for (j=0;j<msg->elements[i].num_values;j++) {
321 ret = h->ldif_write_fn(ldb, ldb, &msg->elements[i].values[j], &v);
323 if (ldb_should_b64_encode(&v)) {
324 ret = fprintf_fn(private_data, "%s:: ",
325 msg->elements[i].name);
327 ret = base64_encode_f(ldb, fprintf_fn, private_data,
329 strlen(msg->elements[i].name)+3);
331 ret = fprintf_fn(private_data, "\n");
334 ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
336 ret = fold_string(fprintf_fn, private_data,
338 strlen(msg->elements[i].name)+2);
340 ret = fprintf_fn(private_data, "\n");
343 if (v.data != msg->elements[i].values[j].data) {
347 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
348 fprintf_fn(private_data, "-\n");
351 ret = fprintf_fn(private_data,"\n");
361 pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
362 this routine removes any RFC2849 continuations and comments
366 static char *next_chunk(struct ldb_context *ldb,
367 int (*fgetc_fn)(void *), void *private_data)
369 size_t alloc_size=0, chunk_size = 0;
374 while ((c = fgetc_fn(private_data)) != EOF) {
375 if (chunk_size+1 >= alloc_size) {
378 c2 = talloc_realloc(ldb, chunk, char, alloc_size);
394 /* handle continuation lines - see RFC2849 */
395 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
400 /* chunks are terminated by a double line-feed */
401 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
402 chunk[chunk_size-1] = 0;
406 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
411 /* ignore leading blank lines */
412 if (chunk_size == 0 && c == '\n') {
416 chunk[chunk_size++] = c;
420 chunk[chunk_size] = 0;
427 /* simple ldif attribute parser */
428 static int next_attr(void *mem_ctx, char **s, const char **attr, struct ldb_val *value)
431 int base64_encoded = 0;
434 if (strncmp(*s, "-\n", 2) == 0) {
460 while (*p == ' ' || *p == '\t') {
469 value->length = strlen((char *)value->data);
470 *s = ((char *)value->data) + value->length;
472 value->length = p - (char *)value->data;
477 if (base64_encoded) {
478 int len = ldb_base64_decode(value->data);
480 /* it wasn't valid base64 data */
487 int len = ldb_read_data_file(mem_ctx, value);
489 /* an error occured hile trying to retrieve the file */
499 free a message from a ldif_read
501 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
509 static int msg_add_empty(struct ldb_context *ldb,
510 struct ldb_message *msg, const char *name, unsigned flags)
512 struct ldb_message_element *el2, *el;
514 el2 = talloc_realloc(msg, msg->elements,
515 struct ldb_message_element, msg->num_elements+1);
523 el = &msg->elements[msg->num_elements];
525 el->name = talloc_strdup(msg->elements, name);
541 read from a LDIF source, creating a ldb_message
543 struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
544 int (*fgetc_fn)(void *), void *private_data)
546 struct ldb_ldif *ldif;
547 struct ldb_message *msg;
548 const char *attr=NULL;
549 char *chunk=NULL, *s;
550 struct ldb_val value;
555 ldif = talloc(ldb, struct ldb_ldif);
556 if (!ldif) return NULL;
558 ldif->msg = talloc(ldif, struct ldb_message);
559 if (ldif->msg == NULL) {
564 ldif->changetype = LDB_CHANGETYPE_NONE;
568 msg->elements = NULL;
569 msg->num_elements = 0;
570 msg->private_data = NULL;
572 chunk = next_chunk(ldb, fgetc_fn, private_data);
577 msg->private_data = chunk;
580 if (next_attr(ldif, &s, &attr, &value) != 0) {
584 /* first line must be a dn */
585 if (ldb_attr_cmp(attr, "dn") != 0) {
586 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'\n",
591 msg->dn = ldb_dn_explode(msg, value.data);
593 if (msg->dn == NULL) {
594 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Unable to parse dn '%s'\n",
599 while (next_attr(ldif, &s, &attr, &value) == 0) {
600 const struct ldb_attrib_handler *h;
601 struct ldb_message_element *el;
604 if (ldb_attr_cmp(attr, "changetype") == 0) {
606 for (i=0;ldb_changetypes[i].name;i++) {
607 if (ldb_attr_cmp((char *)value.data, ldb_changetypes[i].name) == 0) {
608 ldif->changetype = ldb_changetypes[i].changetype;
612 if (!ldb_changetypes[i].name) {
613 ldb_debug(ldb, LDB_DEBUG_ERROR,
614 "Error: Bad ldif changetype '%s'\n",(char *)value.data);
620 if (ldb_attr_cmp(attr, "add") == 0) {
621 flags = LDB_FLAG_MOD_ADD;
624 if (ldb_attr_cmp(attr, "delete") == 0) {
625 flags = LDB_FLAG_MOD_DELETE;
628 if (ldb_attr_cmp(attr, "replace") == 0) {
629 flags = LDB_FLAG_MOD_REPLACE;
632 if (ldb_attr_cmp(attr, "-") == 0) {
638 if (msg_add_empty(ldb, msg, (char *)value.data, flags) != 0) {
644 el = &msg->elements[msg->num_elements-1];
646 h = ldb_attrib_handler(ldb, attr);
648 if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
649 flags == el->flags) {
650 /* its a continuation */
652 talloc_realloc(msg->elements, el->values,
653 struct ldb_val, el->num_values+1);
657 ret = h->ldif_read_fn(ldb, ldif, &value, &el->values[el->num_values]);
661 if (value.length == 0) {
662 ldb_debug(ldb, LDB_DEBUG_ERROR,
663 "Error: Attribute value cannot be empty for attribute '%s'\n", el->name);
666 if (value.data != el->values[el->num_values].data) {
667 talloc_steal(el->values, el->values[el->num_values].data);
671 /* its a new attribute */
672 msg->elements = talloc_realloc(ldif, msg->elements,
673 struct ldb_message_element,
674 msg->num_elements+1);
675 if (!msg->elements) {
678 el = &msg->elements[msg->num_elements];
680 el->name = talloc_strdup(msg->elements, attr);
681 el->values = talloc(msg->elements, struct ldb_val);
682 if (!el->values || !el->name) {
686 ret = h->ldif_read_fn(ldb, ldif, &value, &el->values[0]);
690 if (value.data != el->values[0].data) {
691 talloc_steal(el->values, el->values[0].data);
707 a wrapper around ldif_read() for reading from FILE*
709 struct ldif_read_file_state {
713 static int fgetc_file(void *private_data)
715 struct ldif_read_file_state *state = private_data;
716 return fgetc(state->f);
719 struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f)
721 struct ldif_read_file_state state;
723 return ldb_ldif_read(ldb, fgetc_file, &state);
728 a wrapper around ldif_read() for reading from const char*
730 struct ldif_read_string_state {
734 static int fgetc_string(void *private_data)
736 struct ldif_read_string_state *state = private_data;
737 if (state->s[0] != 0) {
743 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char **s)
745 struct ldif_read_string_state state;
746 struct ldb_ldif *ldif;
748 ldif = ldb_ldif_read(ldb, fgetc_string, &state);
755 wrapper around ldif_write() for a file
757 struct ldif_write_file_state {
761 static int fprintf_file(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
763 static int fprintf_file(void *private_data, const char *fmt, ...)
765 struct ldif_write_file_state *state = private_data;
770 ret = vfprintf(state->f, fmt, ap);
775 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
777 struct ldif_write_file_state state;
779 return ldb_ldif_write(ldb, fprintf_file, &state, ldif);