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
39 #include "ldb_includes.h"
40 #include "system/locale.h"
45 static int ldb_read_data_file(void *mem_ctx, struct ldb_val *value)
49 int count, size, bytes;
52 const char *fname = (const char *)value->data;
54 if (strncmp(fname, "file://", 7) != 0) {
55 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
59 f = open(fname, O_RDONLY);
64 if (fstat(f, &statbuf) != 0) {
69 if (statbuf.st_size == 0) {
74 value->data = (uint8_t *)talloc_size(mem_ctx, statbuf.st_size + 1);
75 if (value->data == NULL) {
79 value->data[statbuf.st_size] = 0;
82 size = statbuf.st_size;
83 buf = (char *)value->data;
84 while (count < statbuf.st_size) {
85 bytes = read(f, buf, size);
87 talloc_free(value->data);
96 value->length = statbuf.st_size;
97 ret = statbuf.st_size;
105 this base64 decoder was taken from jitterbug (written by tridge).
106 we might need to replace it with a new version
108 int ldb_base64_decode(char *s)
110 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
111 int bit_offset=0, byte_offset, idx, i, n;
112 uint8_t *d = (uint8_t *)s;
117 while (*s && (p=strchr(b64,*s))) {
118 idx = (int)(p - b64);
119 byte_offset = (i*6)/8;
120 bit_offset = (i*6)%8;
121 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
122 if (bit_offset < 3) {
123 d[byte_offset] |= (idx << (2-bit_offset));
126 d[byte_offset] |= (idx >> (bit_offset-2));
127 d[byte_offset+1] = 0;
128 d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
133 if (bit_offset >= 3) {
138 /* the only termination allowed */
154 char *ldb_base64_encode(void *mem_ctx, const char *buf, int len)
156 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
157 int bit_offset, byte_offset, idx, i;
158 const uint8_t *d = (const uint8_t *)buf;
159 int bytes = (len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
162 out = talloc_array(mem_ctx, char, bytes+pad_bytes+1);
163 if (!out) return NULL;
165 for (i=0;i<bytes;i++) {
166 byte_offset = (i*6)/8;
167 bit_offset = (i*6)%8;
168 if (bit_offset < 3) {
169 idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
171 idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
172 if (byte_offset+1 < len) {
173 idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
179 for (;i<bytes+pad_bytes;i++)
187 see if a buffer should be base64 encoded
189 int ldb_should_b64_encode(const struct ldb_val *val)
192 uint8_t *p = val->data;
194 if (val->length == 0) {
198 if (p[0] == ' ' || p[0] == ':') {
202 for (i=0; i<val->length; i++) {
203 if (!isprint(p[i]) || p[i] == '\n') {
210 /* this macro is used to handle the return checking on fprintf_fn() */
211 #define CHECK_RET do { if (ret < 0) return ret; total += ret; } while (0)
214 write a line folded string onto a file
216 static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *private_data,
217 const char *buf, size_t length, int start_pos)
222 for (i=0;i<length;i++) {
223 ret = fprintf_fn(private_data, "%c", buf[i]);
225 if (i != (length-1) && (i + start_pos) % 77 == 0) {
226 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},
268 /* this macro is used to handle the return checking on fprintf_fn() */
269 #define CHECK_RET do { if (ret < 0) { talloc_free(mem_ctx); return ret; } total += ret; } while (0)
272 write to ldif, using a caller supplied write method
274 int ldb_ldif_write(struct ldb_context *ldb,
275 int (*fprintf_fn)(void *, const char *, ...),
277 const struct ldb_ldif *ldif)
282 const struct ldb_message *msg;
284 mem_ctx = talloc_named_const(NULL, 0, "ldb_ldif_write");
288 ret = fprintf_fn(private_data, "dn: %s\n", ldb_dn_get_linearized(msg->dn));
291 if (ldif->changetype != LDB_CHANGETYPE_NONE) {
292 for (i=0;ldb_changetypes[i].name;i++) {
293 if (ldb_changetypes[i].changetype == ldif->changetype) {
297 if (!ldb_changetypes[i].name) {
298 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d\n",
300 talloc_free(mem_ctx);
303 ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
307 for (i=0;i<msg->num_elements;i++) {
308 const struct ldb_schema_attribute *a;
310 a = ldb_schema_attribute_by_name(ldb, msg->elements[i].name);
312 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
313 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
314 case LDB_FLAG_MOD_ADD:
315 fprintf_fn(private_data, "add: %s\n",
316 msg->elements[i].name);
318 case LDB_FLAG_MOD_DELETE:
319 fprintf_fn(private_data, "delete: %s\n",
320 msg->elements[i].name);
322 case LDB_FLAG_MOD_REPLACE:
323 fprintf_fn(private_data, "replace: %s\n",
324 msg->elements[i].name);
329 for (j=0;j<msg->elements[i].num_values;j++) {
331 ret = a->syntax->ldif_write_fn(ldb, mem_ctx, &msg->elements[i].values[j], &v);
333 if (ldb_should_b64_encode(&v)) {
334 ret = fprintf_fn(private_data, "%s:: ",
335 msg->elements[i].name);
337 ret = base64_encode_f(ldb, fprintf_fn, private_data,
338 (char *)v.data, v.length,
339 strlen(msg->elements[i].name)+3);
341 ret = fprintf_fn(private_data, "\n");
344 ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
346 ret = fold_string(fprintf_fn, private_data,
347 (char *)v.data, v.length,
348 strlen(msg->elements[i].name)+2);
350 ret = fprintf_fn(private_data, "\n");
353 if (v.data != msg->elements[i].values[j].data) {
357 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
358 fprintf_fn(private_data, "-\n");
361 ret = fprintf_fn(private_data,"\n");
371 pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
372 this routine removes any RFC2849 continuations and comments
376 static char *next_chunk(struct ldb_context *ldb,
377 int (*fgetc_fn)(void *), void *private_data)
379 size_t alloc_size=0, chunk_size = 0;
384 while ((c = fgetc_fn(private_data)) != EOF) {
385 if (chunk_size+1 >= alloc_size) {
388 c2 = talloc_realloc(ldb, chunk, char, alloc_size);
404 /* handle continuation lines - see RFC2849 */
405 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
410 /* chunks are terminated by a double line-feed */
411 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
412 chunk[chunk_size-1] = 0;
416 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
421 /* ignore leading blank lines */
422 if (chunk_size == 0 && c == '\n') {
426 chunk[chunk_size++] = c;
430 chunk[chunk_size] = 0;
437 /* simple ldif attribute parser */
438 static int next_attr(void *mem_ctx, char **s, const char **attr, struct ldb_val *value)
441 int base64_encoded = 0;
444 if (strncmp(*s, "-\n", 2) == 0) {
470 while (*p == ' ' || *p == '\t') {
474 value->data = (uint8_t *)p;
479 value->length = strlen((char *)value->data);
480 *s = ((char *)value->data) + value->length;
482 value->length = p - (char *)value->data;
487 if (base64_encoded) {
488 int len = ldb_base64_decode((char *)value->data);
490 /* it wasn't valid base64 data */
497 int len = ldb_read_data_file(mem_ctx, value);
499 /* an error occured hile trying to retrieve the file */
509 free a message from a ldif_read
511 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
517 read from a LDIF source, creating a ldb_message
519 struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
520 int (*fgetc_fn)(void *), void *private_data)
522 struct ldb_ldif *ldif;
523 struct ldb_message *msg;
524 const char *attr=NULL;
525 char *chunk=NULL, *s;
526 struct ldb_val value;
531 ldif = talloc(ldb, struct ldb_ldif);
532 if (!ldif) return NULL;
534 ldif->msg = talloc(ldif, struct ldb_message);
535 if (ldif->msg == NULL) {
540 ldif->changetype = LDB_CHANGETYPE_NONE;
544 msg->elements = NULL;
545 msg->num_elements = 0;
547 chunk = next_chunk(ldb, fgetc_fn, private_data);
551 talloc_steal(ldif, chunk);
555 if (next_attr(ldif, &s, &attr, &value) != 0) {
559 /* first line must be a dn */
560 if (ldb_attr_cmp(attr, "dn") != 0) {
561 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'\n",
566 msg->dn = ldb_dn_new(msg, ldb, (char *)value.data);
568 if ( ! ldb_dn_validate(msg->dn)) {
569 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Unable to parse dn '%s'\n",
574 while (next_attr(ldif, &s, &attr, &value) == 0) {
575 const struct ldb_schema_attribute *a;
576 struct ldb_message_element *el;
579 if (ldb_attr_cmp(attr, "changetype") == 0) {
581 for (i=0;ldb_changetypes[i].name;i++) {
582 if (ldb_attr_cmp((char *)value.data, ldb_changetypes[i].name) == 0) {
583 ldif->changetype = ldb_changetypes[i].changetype;
587 if (!ldb_changetypes[i].name) {
588 ldb_debug(ldb, LDB_DEBUG_ERROR,
589 "Error: Bad ldif changetype '%s'\n",(char *)value.data);
595 if (ldb_attr_cmp(attr, "add") == 0) {
596 flags = LDB_FLAG_MOD_ADD;
599 if (ldb_attr_cmp(attr, "delete") == 0) {
600 flags = LDB_FLAG_MOD_DELETE;
603 if (ldb_attr_cmp(attr, "replace") == 0) {
604 flags = LDB_FLAG_MOD_REPLACE;
607 if (ldb_attr_cmp(attr, "-") == 0) {
613 if (ldb_msg_add_empty(msg, (char *)value.data, flags, NULL) != 0) {
619 el = &msg->elements[msg->num_elements-1];
621 a = ldb_schema_attribute_by_name(ldb, attr);
623 if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
624 flags == el->flags) {
625 /* its a continuation */
627 talloc_realloc(msg->elements, el->values,
628 struct ldb_val, el->num_values+1);
632 ret = a->syntax->ldif_read_fn(ldb, ldif, &value, &el->values[el->num_values]);
636 if (value.length == 0) {
637 ldb_debug(ldb, LDB_DEBUG_ERROR,
638 "Error: Attribute value cannot be empty for attribute '%s'\n", el->name);
641 if (value.data != el->values[el->num_values].data) {
642 talloc_steal(el->values, el->values[el->num_values].data);
646 /* its a new attribute */
647 msg->elements = talloc_realloc(ldif, msg->elements,
648 struct ldb_message_element,
649 msg->num_elements+1);
650 if (!msg->elements) {
653 el = &msg->elements[msg->num_elements];
655 el->name = talloc_strdup(msg->elements, attr);
656 el->values = talloc(msg->elements, struct ldb_val);
657 if (!el->values || !el->name) {
661 ret = a->syntax->ldif_read_fn(ldb, ldif, &value, &el->values[0]);
665 if (value.data != el->values[0].data) {
666 talloc_steal(el->values, el->values[0].data);
682 a wrapper around ldif_read() for reading from FILE*
684 struct ldif_read_file_state {
688 static int fgetc_file(void *private_data)
690 struct ldif_read_file_state *state =
691 (struct ldif_read_file_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 =
713 (struct ldif_read_string_state *)private_data;
714 if (state->s[0] != 0) {
720 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char **s)
722 struct ldif_read_string_state state;
723 struct ldb_ldif *ldif;
725 ldif = ldb_ldif_read(ldb, fgetc_string, &state);
732 wrapper around ldif_write() for a file
734 struct ldif_write_file_state {
738 static int fprintf_file(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
740 static int fprintf_file(void *private_data, const char *fmt, ...)
742 struct ldif_write_file_state *state =
743 (struct ldif_write_file_state *)private_data;
748 ret = vfprintf(state->f, fmt, ap);
753 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
755 struct ldif_write_file_state state;
757 return ldb_ldif_write(ldb, fprintf_file, &state, ldif);