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/includes.h"
41 #include "system/locale.h"
46 static int ldb_read_data_file(void *mem_ctx, struct ldb_val *value)
50 int count, size, bytes;
53 const char *fname = (const char *)value->data;
55 if (strncmp(fname, "file://", 7) != 0) {
56 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
60 f = open(fname, O_RDONLY);
65 if (fstat(f, &statbuf) != 0) {
70 if (statbuf.st_size == 0) {
75 value->data = talloc_size(mem_ctx, statbuf.st_size + 1);
76 if (value->data == NULL) {
80 value->data[statbuf.st_size] = 0;
83 size = statbuf.st_size;
84 buf = (char *)value->data;
85 while (count < statbuf.st_size) {
86 bytes = read(f, buf, size);
88 talloc_free(value->data);
97 value->length = statbuf.st_size;
98 ret = statbuf.st_size;
106 this base64 decoder was taken from jitterbug (written by tridge).
107 we might need to replace it with a new version
109 int ldb_base64_decode(char *s)
111 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
112 int bit_offset=0, byte_offset, idx, i, n;
113 uint8_t *d = (uint8_t *)s;
118 while (*s && (p=strchr(b64,*s))) {
119 idx = (int)(p - b64);
120 byte_offset = (i*6)/8;
121 bit_offset = (i*6)%8;
122 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
123 if (bit_offset < 3) {
124 d[byte_offset] |= (idx << (2-bit_offset));
127 d[byte_offset] |= (idx >> (bit_offset-2));
128 d[byte_offset+1] = 0;
129 d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
134 if (bit_offset >= 3) {
139 /* the only termination allowed */
155 char *ldb_base64_encode(void *mem_ctx, const char *buf, int len)
157 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
158 int bit_offset, byte_offset, idx, i;
159 const uint8_t *d = (const uint8_t *)buf;
160 int bytes = (len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
163 out = talloc_array(mem_ctx, char, bytes+pad_bytes+1);
164 if (!out) return NULL;
166 for (i=0;i<bytes;i++) {
167 byte_offset = (i*6)/8;
168 bit_offset = (i*6)%8;
169 if (bit_offset < 3) {
170 idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
172 idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
173 if (byte_offset+1 < len) {
174 idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
180 for (;i<bytes+pad_bytes;i++)
188 see if a buffer should be base64 encoded
190 int ldb_should_b64_encode(const struct ldb_val *val)
193 uint8_t *p = val->data;
195 if (val->length == 0) {
199 if (p[0] == ' ' || p[0] == ':') {
203 for (i=0; i<val->length; i++) {
204 if (!isprint(p[i]) || p[i] == '\n') {
211 /* this macro is used to handle the return checking on fprintf_fn() */
212 #define CHECK_RET do { if (ret < 0) return ret; total += ret; } while (0)
215 write a line folded string onto a file
217 static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *private_data,
218 const char *buf, size_t length, int start_pos)
223 for (i=0;i<length;i++) {
224 ret = fprintf_fn(private_data, "%c", buf[i]);
226 if (i != (length-1) && (i + start_pos) % 77 == 0) {
227 ret = fprintf_fn(private_data, "\n ");
236 encode as base64 to a file
238 static int base64_encode_f(struct ldb_context *ldb,
239 int (*fprintf_fn)(void *, const char *, ...),
241 const char *buf, int len, int start_pos)
243 char *b = ldb_base64_encode(ldb, buf, len);
250 ret = fold_string(fprintf_fn, private_data, b, strlen(b), start_pos);
257 static const struct {
259 enum ldb_changetype changetype;
260 } ldb_changetypes[] = {
261 {"add", LDB_CHANGETYPE_ADD},
262 {"delete", LDB_CHANGETYPE_DELETE},
263 {"modify", LDB_CHANGETYPE_MODIFY},
268 write to ldif, using a caller supplied write method
270 int ldb_ldif_write(struct ldb_context *ldb,
271 int (*fprintf_fn)(void *, const char *, ...),
273 const struct ldb_ldif *ldif)
277 const struct ldb_message *msg;
281 ret = fprintf_fn(private_data, "dn: %s\n", ldb_dn_linearize(msg->dn, msg->dn));
284 if (ldif->changetype != LDB_CHANGETYPE_NONE) {
285 for (i=0;ldb_changetypes[i].name;i++) {
286 if (ldb_changetypes[i].changetype == ldif->changetype) {
290 if (!ldb_changetypes[i].name) {
291 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d\n",
295 ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
299 for (i=0;i<msg->num_elements;i++) {
300 const struct ldb_attrib_handler *h;
302 h = ldb_attrib_handler(ldb, msg->elements[i].name);
304 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
305 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
306 case LDB_FLAG_MOD_ADD:
307 fprintf_fn(private_data, "add: %s\n",
308 msg->elements[i].name);
310 case LDB_FLAG_MOD_DELETE:
311 fprintf_fn(private_data, "delete: %s\n",
312 msg->elements[i].name);
314 case LDB_FLAG_MOD_REPLACE:
315 fprintf_fn(private_data, "replace: %s\n",
316 msg->elements[i].name);
321 for (j=0;j<msg->elements[i].num_values;j++) {
323 ret = h->ldif_write_fn(ldb, ldb, &msg->elements[i].values[j], &v);
325 if (ldb_should_b64_encode(&v)) {
326 ret = fprintf_fn(private_data, "%s:: ",
327 msg->elements[i].name);
329 ret = base64_encode_f(ldb, fprintf_fn, private_data,
330 (char *)v.data, v.length,
331 strlen(msg->elements[i].name)+3);
333 ret = fprintf_fn(private_data, "\n");
336 ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
338 ret = fold_string(fprintf_fn, private_data,
339 (char *)v.data, v.length,
340 strlen(msg->elements[i].name)+2);
342 ret = fprintf_fn(private_data, "\n");
345 if (v.data != msg->elements[i].values[j].data) {
349 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
350 fprintf_fn(private_data, "-\n");
353 ret = fprintf_fn(private_data,"\n");
363 pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
364 this routine removes any RFC2849 continuations and comments
368 static char *next_chunk(struct ldb_context *ldb,
369 int (*fgetc_fn)(void *), void *private_data)
371 size_t alloc_size=0, chunk_size = 0;
376 while ((c = fgetc_fn(private_data)) != EOF) {
377 if (chunk_size+1 >= alloc_size) {
380 c2 = talloc_realloc(ldb, chunk, char, alloc_size);
396 /* handle continuation lines - see RFC2849 */
397 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
402 /* chunks are terminated by a double line-feed */
403 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
404 chunk[chunk_size-1] = 0;
408 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
413 /* ignore leading blank lines */
414 if (chunk_size == 0 && c == '\n') {
418 chunk[chunk_size++] = c;
422 chunk[chunk_size] = 0;
429 /* simple ldif attribute parser */
430 static int next_attr(void *mem_ctx, char **s, const char **attr, struct ldb_val *value)
433 int base64_encoded = 0;
436 if (strncmp(*s, "-\n", 2) == 0) {
462 while (*p == ' ' || *p == '\t') {
466 value->data = (uint8_t *)p;
471 value->length = strlen((char *)value->data);
472 *s = ((char *)value->data) + value->length;
474 value->length = p - (char *)value->data;
479 if (base64_encoded) {
480 int len = ldb_base64_decode((char *)value->data);
482 /* it wasn't valid base64 data */
489 int len = ldb_read_data_file(mem_ctx, value);
491 /* an error occured hile trying to retrieve the file */
501 free a message from a ldif_read
503 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
509 read from a LDIF source, creating a ldb_message
511 struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
512 int (*fgetc_fn)(void *), void *private_data)
514 struct ldb_ldif *ldif;
515 struct ldb_message *msg;
516 const char *attr=NULL;
517 char *chunk=NULL, *s;
518 struct ldb_val value;
523 ldif = talloc(ldb, struct ldb_ldif);
524 if (!ldif) return NULL;
526 ldif->msg = talloc(ldif, struct ldb_message);
527 if (ldif->msg == NULL) {
532 ldif->changetype = LDB_CHANGETYPE_NONE;
536 msg->elements = NULL;
537 msg->num_elements = 0;
538 msg->private_data = NULL;
540 chunk = next_chunk(ldb, fgetc_fn, private_data);
545 msg->private_data = chunk;
548 if (next_attr(ldif, &s, &attr, &value) != 0) {
552 /* first line must be a dn */
553 if (ldb_attr_cmp(attr, "dn") != 0) {
554 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'\n",
559 msg->dn = ldb_dn_explode(msg, (char *)value.data);
561 if (msg->dn == NULL) {
562 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Unable to parse dn '%s'\n",
567 while (next_attr(ldif, &s, &attr, &value) == 0) {
568 const struct ldb_attrib_handler *h;
569 struct ldb_message_element *el;
572 if (ldb_attr_cmp(attr, "changetype") == 0) {
574 for (i=0;ldb_changetypes[i].name;i++) {
575 if (ldb_attr_cmp((char *)value.data, ldb_changetypes[i].name) == 0) {
576 ldif->changetype = ldb_changetypes[i].changetype;
580 if (!ldb_changetypes[i].name) {
581 ldb_debug(ldb, LDB_DEBUG_ERROR,
582 "Error: Bad ldif changetype '%s'\n",(char *)value.data);
588 if (ldb_attr_cmp(attr, "add") == 0) {
589 flags = LDB_FLAG_MOD_ADD;
592 if (ldb_attr_cmp(attr, "delete") == 0) {
593 flags = LDB_FLAG_MOD_DELETE;
596 if (ldb_attr_cmp(attr, "replace") == 0) {
597 flags = LDB_FLAG_MOD_REPLACE;
600 if (ldb_attr_cmp(attr, "-") == 0) {
606 if (ldb_msg_add_empty(msg, (char *)value.data, flags) != 0) {
612 el = &msg->elements[msg->num_elements-1];
614 h = ldb_attrib_handler(ldb, attr);
616 if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
617 flags == el->flags) {
618 /* its a continuation */
620 talloc_realloc(msg->elements, el->values,
621 struct ldb_val, el->num_values+1);
625 ret = h->ldif_read_fn(ldb, ldif, &value, &el->values[el->num_values]);
629 if (value.length == 0) {
630 ldb_debug(ldb, LDB_DEBUG_ERROR,
631 "Error: Attribute value cannot be empty for attribute '%s'\n", el->name);
634 if (value.data != el->values[el->num_values].data) {
635 talloc_steal(el->values, el->values[el->num_values].data);
639 /* its a new attribute */
640 msg->elements = talloc_realloc(ldif, msg->elements,
641 struct ldb_message_element,
642 msg->num_elements+1);
643 if (!msg->elements) {
646 el = &msg->elements[msg->num_elements];
648 el->name = talloc_strdup(msg->elements, attr);
649 el->values = talloc(msg->elements, struct ldb_val);
650 if (!el->values || !el->name) {
654 ret = h->ldif_read_fn(ldb, ldif, &value, &el->values[0]);
658 if (value.data != el->values[0].data) {
659 talloc_steal(el->values, el->values[0].data);
675 a wrapper around ldif_read() for reading from FILE*
677 struct ldif_read_file_state {
681 static int fgetc_file(void *private_data)
683 struct ldif_read_file_state *state = private_data;
684 return fgetc(state->f);
687 struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f)
689 struct ldif_read_file_state state;
691 return ldb_ldif_read(ldb, fgetc_file, &state);
696 a wrapper around ldif_read() for reading from const char*
698 struct ldif_read_string_state {
702 static int fgetc_string(void *private_data)
704 struct ldif_read_string_state *state = private_data;
705 if (state->s[0] != 0) {
711 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char **s)
713 struct ldif_read_string_state state;
714 struct ldb_ldif *ldif;
716 ldif = ldb_ldif_read(ldb, fgetc_string, &state);
723 wrapper around ldif_write() for a file
725 struct ldif_write_file_state {
729 static int fprintf_file(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
731 static int fprintf_file(void *private_data, const char *fmt, ...)
733 struct ldif_write_file_state *state = private_data;
738 ret = vfprintf(state->f, fmt, ap);
743 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
745 struct ldif_write_file_state state;
747 return ldb_ldif_write(ldb, fprintf_file, &state, ldif);