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;
161 out = talloc_array(mem_ctx, char, bytes+2);
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)));
185 see if a buffer should be base64 encoded
187 int ldb_should_b64_encode(const struct ldb_val *val)
190 uint8_t *p = val->data;
192 if (val->length == 0) {
196 if (p[0] == ' ' || p[0] == ':') {
200 for (i=0; i<val->length; i++) {
201 if (!isprint(p[i]) || p[i] == '\n') {
208 /* this macro is used to handle the return checking on fprintf_fn() */
209 #define CHECK_RET do { if (ret < 0) return ret; total += ret; } while (0)
212 write a line folded string onto a file
214 static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *private_data,
215 const char *buf, size_t length, int start_pos)
220 for (i=0;i<length;i++) {
221 ret = fprintf_fn(private_data, "%c", buf[i]);
223 if (i != (length-1) && (i + start_pos) % 77 == 0) {
224 ret = fprintf_fn(private_data, "\n ");
233 encode as base64 to a file
235 static int base64_encode_f(struct ldb_context *ldb,
236 int (*fprintf_fn)(void *, const char *, ...),
238 const char *buf, int len, int start_pos)
240 char *b = ldb_base64_encode(ldb, buf, len);
247 ret = fold_string(fprintf_fn, private_data, b, strlen(b), start_pos);
254 static const struct {
256 enum ldb_changetype changetype;
257 } ldb_changetypes[] = {
258 {"add", LDB_CHANGETYPE_ADD},
259 {"delete", LDB_CHANGETYPE_DELETE},
260 {"modify", LDB_CHANGETYPE_MODIFY},
265 write to ldif, using a caller supplied write method
267 int ldb_ldif_write(struct ldb_context *ldb,
268 int (*fprintf_fn)(void *, const char *, ...),
270 const struct ldb_ldif *ldif)
274 const struct ldb_message *msg;
278 ret = fprintf_fn(private_data, "dn: %s\n", msg->dn);
281 if (ldif->changetype != LDB_CHANGETYPE_NONE) {
282 for (i=0;ldb_changetypes[i].name;i++) {
283 if (ldb_changetypes[i].changetype == ldif->changetype) {
287 if (!ldb_changetypes[i].name) {
288 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d\n",
292 ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
296 for (i=0;i<msg->num_elements;i++) {
297 const struct ldb_attrib_handler *h;
299 h = ldb_attrib_handler(ldb, msg->elements[i].name);
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++) {
320 ret = h->ldif_write_fn(ldb, ldb, &msg->elements[i].values[j], &v);
322 if (ldb_should_b64_encode(&v)) {
323 ret = fprintf_fn(private_data, "%s:: ",
324 msg->elements[i].name);
326 ret = base64_encode_f(ldb, fprintf_fn, private_data,
328 strlen(msg->elements[i].name)+3);
330 ret = fprintf_fn(private_data, "\n");
333 ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
335 ret = fold_string(fprintf_fn, private_data,
337 strlen(msg->elements[i].name)+2);
339 ret = fprintf_fn(private_data, "\n");
342 if (v.data != msg->elements[i].values[j].data) {
346 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
347 fprintf_fn(private_data, "-\n");
350 ret = fprintf_fn(private_data,"\n");
360 pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
361 this routine removes any RFC2849 continuations and comments
365 static char *next_chunk(struct ldb_context *ldb,
366 int (*fgetc_fn)(void *), void *private_data)
368 size_t alloc_size=0, chunk_size = 0;
373 while ((c = fgetc_fn(private_data)) != EOF) {
374 if (chunk_size+1 >= alloc_size) {
377 c2 = talloc_realloc(ldb, chunk, char, alloc_size);
393 /* handle continuation lines - see RFC2849 */
394 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
399 /* chunks are terminated by a double line-feed */
400 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
401 chunk[chunk_size-1] = 0;
405 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
410 /* ignore leading blank lines */
411 if (chunk_size == 0 && c == '\n') {
415 chunk[chunk_size++] = c;
419 chunk[chunk_size] = 0;
426 /* simple ldif attribute parser */
427 static int next_attr(void *mem_ctx, char **s, const char **attr, struct ldb_val *value)
430 int base64_encoded = 0;
433 if (strncmp(*s, "-\n", 2) == 0) {
459 while (*p == ' ' || *p == '\t') {
468 value->length = strlen((char *)value->data);
469 *s = ((char *)value->data) + value->length;
471 value->length = p - (char *)value->data;
476 if (base64_encoded) {
477 int len = ldb_base64_decode(value->data);
479 /* it wasn't valid base64 data */
486 int len = ldb_read_data_file(mem_ctx, value);
488 /* an error occured hile trying to retrieve the file */
498 free a message from a ldif_read
500 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
508 static int msg_add_empty(struct ldb_context *ldb,
509 struct ldb_message *msg, const char *name, unsigned flags)
511 struct ldb_message_element *el2, *el;
513 el2 = talloc_realloc(msg, msg->elements,
514 struct ldb_message_element, msg->num_elements+1);
522 el = &msg->elements[msg->num_elements];
524 el->name = talloc_strdup(msg->elements, name);
540 read from a LDIF source, creating a ldb_message
542 struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
543 int (*fgetc_fn)(void *), void *private_data)
545 struct ldb_ldif *ldif;
546 struct ldb_message *msg;
547 const char *attr=NULL;
548 char *chunk=NULL, *s;
549 struct ldb_val value;
554 ldif = talloc(ldb, struct ldb_ldif);
555 if (!ldif) return NULL;
557 ldif->msg = talloc(ldif, struct ldb_message);
558 if (ldif->msg == NULL) {
563 ldif->changetype = LDB_CHANGETYPE_NONE;
567 msg->elements = NULL;
568 msg->num_elements = 0;
569 msg->private_data = NULL;
571 chunk = next_chunk(ldb, fgetc_fn, private_data);
576 msg->private_data = chunk;
579 if (next_attr(ldif, &s, &attr, &value) != 0) {
583 /* first line must be a dn */
584 if (ldb_attr_cmp(attr, "dn") != 0) {
585 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'\n",
590 msg->dn = value.data;
592 while (next_attr(ldif, &s, &attr, &value) == 0) {
593 const struct ldb_attrib_handler *h;
594 struct ldb_message_element *el;
597 if (ldb_attr_cmp(attr, "changetype") == 0) {
599 for (i=0;ldb_changetypes[i].name;i++) {
600 if (ldb_attr_cmp((char *)value.data, ldb_changetypes[i].name) == 0) {
601 ldif->changetype = ldb_changetypes[i].changetype;
605 if (!ldb_changetypes[i].name) {
606 ldb_debug(ldb, LDB_DEBUG_ERROR,
607 "Error: Bad ldif changetype '%s'\n",(char *)value.data);
613 if (ldb_attr_cmp(attr, "add") == 0) {
614 flags = LDB_FLAG_MOD_ADD;
617 if (ldb_attr_cmp(attr, "delete") == 0) {
618 flags = LDB_FLAG_MOD_DELETE;
621 if (ldb_attr_cmp(attr, "replace") == 0) {
622 flags = LDB_FLAG_MOD_REPLACE;
625 if (ldb_attr_cmp(attr, "-") == 0) {
631 if (msg_add_empty(ldb, msg, (char *)value.data, flags) != 0) {
637 el = &msg->elements[msg->num_elements-1];
639 h = ldb_attrib_handler(ldb, attr);
641 if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
642 flags == el->flags) {
643 /* its a continuation */
645 talloc_realloc(msg->elements, el->values,
646 struct ldb_val, el->num_values+1);
650 ret = h->ldif_read_fn(ldb, ldif, &value, &el->values[el->num_values]);
654 if (value.data != el->values[el->num_values].data) {
655 talloc_steal(el->values, el->values[el->num_values].data);
659 /* its a new attribute */
660 msg->elements = talloc_realloc(ldif, msg->elements,
661 struct ldb_message_element,
662 msg->num_elements+1);
663 if (!msg->elements) {
666 el = &msg->elements[msg->num_elements];
668 el->name = talloc_strdup(msg->elements, attr);
669 el->values = talloc(msg->elements, struct ldb_val);
670 if (!el->values || !el->name) {
674 ret = h->ldif_read_fn(ldb, ldif, &value, &el->values[0]);
678 if (value.data != el->values[0].data) {
679 talloc_steal(el->values, el->values[0].data);
695 a wrapper around ldif_read() for reading from FILE*
697 struct ldif_read_file_state {
701 static int fgetc_file(void *private_data)
703 struct ldif_read_file_state *state = private_data;
704 return fgetc(state->f);
707 struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f)
709 struct ldif_read_file_state state;
711 return ldb_ldif_read(ldb, fgetc_file, &state);
716 a wrapper around ldif_read() for reading from const char*
718 struct ldif_read_string_state {
722 static int fgetc_string(void *private_data)
724 struct ldif_read_string_state *state = private_data;
725 if (state->s[0] != 0) {
731 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char *s)
733 struct ldif_read_string_state state;
735 return ldb_ldif_read(ldb, fgetc_string, &state);
740 wrapper around ldif_write() for a file
742 struct ldif_write_file_state {
746 static int fprintf_file(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
748 static int fprintf_file(void *private_data, const char *fmt, ...)
750 struct ldif_write_file_state *state = private_data;
755 ret = vfprintf(state->f, fmt, ap);
760 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
762 struct ldif_write_file_state state;
764 return ldb_ldif_write(ldb, fprintf_file, &state, ldif);