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 3 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, see <http://www.gnu.org/licenses/>.
27 * Component: ldif routines
29 * Description: ldif pack/unpack routines
31 * Author: Andrew Tridgell
35 see RFC2849 for the LDIF format definition
38 #include "ldb_private.h"
39 #include "system/locale.h"
44 static int ldb_read_data_file(void *mem_ctx, struct ldb_val *value)
48 int count, size, bytes;
51 const char *fname = (const char *)value->data;
53 if (strncmp(fname, "file://", 7) != 0) {
54 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
58 f = open(fname, O_RDONLY);
63 if (fstat(f, &statbuf) != 0) {
68 if (statbuf.st_size == 0) {
73 value->data = (uint8_t *)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;
82 buf = (char *)value->data;
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(struct ldb_context *ldb, const struct ldb_val *val)
191 uint8_t *p = val->data;
193 if (ldb->flags & LDB_FLG_SHOW_BINARY) {
197 if (val->length == 0) {
201 if (p[0] == ' ' || p[0] == ':') {
205 for (i=0; i<val->length; i++) {
206 if (!isprint(p[i]) || p[i] == '\n') {
213 /* this macro is used to handle the return checking on fprintf_fn() */
214 #define CHECK_RET do { if (ret < 0) return ret; total += ret; } while (0)
217 write a line folded string onto a file
219 static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *private_data,
220 const char *buf, size_t length, int start_pos)
225 for (i=0;i<length;i++) {
226 ret = fprintf_fn(private_data, "%c", buf[i]);
228 if (i != (length-1) && (i + start_pos) % 77 == 0) {
229 ret = fprintf_fn(private_data, "\n ");
240 encode as base64 to a file
242 static int base64_encode_f(struct ldb_context *ldb,
243 int (*fprintf_fn)(void *, const char *, ...),
245 const char *buf, int len, int start_pos)
247 char *b = ldb_base64_encode(ldb, buf, len);
254 ret = fold_string(fprintf_fn, private_data, b, strlen(b), start_pos);
261 static const struct {
263 enum ldb_changetype changetype;
264 } ldb_changetypes[] = {
265 {"add", LDB_CHANGETYPE_ADD},
266 {"delete", LDB_CHANGETYPE_DELETE},
267 {"modify", LDB_CHANGETYPE_MODIFY},
271 /* this macro is used to handle the return checking on fprintf_fn() */
272 #define CHECK_RET do { if (ret < 0) { talloc_free(mem_ctx); return ret; } total += ret; } while (0)
275 write to ldif, using a caller supplied write method
277 int ldb_ldif_write(struct ldb_context *ldb,
278 int (*fprintf_fn)(void *, const char *, ...),
280 const struct ldb_ldif *ldif)
286 const struct ldb_message *msg;
288 mem_ctx = talloc_named_const(NULL, 0, "ldb_ldif_write");
291 p = ldb_dn_get_extended_linearized(mem_ctx, msg->dn, 1);
292 ret = fprintf_fn(private_data, "dn: %s\n", p);
296 if (ldif->changetype != LDB_CHANGETYPE_NONE) {
297 for (i=0;ldb_changetypes[i].name;i++) {
298 if (ldb_changetypes[i].changetype == ldif->changetype) {
302 if (!ldb_changetypes[i].name) {
303 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d",
305 talloc_free(mem_ctx);
308 ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
312 for (i=0;i<msg->num_elements;i++) {
313 const struct ldb_schema_attribute *a;
315 a = ldb_schema_attribute_by_name(ldb, msg->elements[i].name);
317 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
318 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
319 case LDB_FLAG_MOD_ADD:
320 fprintf_fn(private_data, "add: %s\n",
321 msg->elements[i].name);
323 case LDB_FLAG_MOD_DELETE:
324 fprintf_fn(private_data, "delete: %s\n",
325 msg->elements[i].name);
327 case LDB_FLAG_MOD_REPLACE:
328 fprintf_fn(private_data, "replace: %s\n",
329 msg->elements[i].name);
334 for (j=0;j<msg->elements[i].num_values;j++) {
336 ret = a->syntax->ldif_write_fn(ldb, mem_ctx, &msg->elements[i].values[j], &v);
337 if (ret != LDB_SUCCESS) {
338 v = msg->elements[i].values[j];
340 if (ret != LDB_SUCCESS || ldb_should_b64_encode(ldb, &v)) {
341 ret = fprintf_fn(private_data, "%s:: ",
342 msg->elements[i].name);
344 ret = base64_encode_f(ldb, fprintf_fn, private_data,
345 (char *)v.data, v.length,
346 strlen(msg->elements[i].name)+3);
348 ret = fprintf_fn(private_data, "\n");
351 ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
353 if (ldb->flags & LDB_FLG_SHOW_BINARY) {
354 ret = fprintf_fn(private_data, "%*.*s",
355 v.length, v.length, (char *)v.data);
357 ret = fold_string(fprintf_fn, private_data,
358 (char *)v.data, v.length,
359 strlen(msg->elements[i].name)+2);
362 ret = fprintf_fn(private_data, "\n");
365 if (v.data != msg->elements[i].values[j].data) {
369 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
370 fprintf_fn(private_data, "-\n");
373 ret = fprintf_fn(private_data,"\n");
376 talloc_free(mem_ctx);
385 pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
386 this routine removes any RFC2849 continuations and comments
390 static char *next_chunk(struct ldb_context *ldb,
391 int (*fgetc_fn)(void *), void *private_data)
393 size_t alloc_size=0, chunk_size = 0;
398 while ((c = fgetc_fn(private_data)) != EOF) {
399 if (chunk_size+1 >= alloc_size) {
402 c2 = talloc_realloc(ldb, chunk, char, alloc_size);
418 /* handle continuation lines - see RFC2849 */
419 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
424 /* chunks are terminated by a double line-feed */
425 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
426 chunk[chunk_size-1] = 0;
430 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
435 /* ignore leading blank lines */
436 if (chunk_size == 0 && c == '\n') {
440 chunk[chunk_size++] = c;
444 chunk[chunk_size] = 0;
451 /* simple ldif attribute parser */
452 static int next_attr(void *mem_ctx, char **s, const char **attr, struct ldb_val *value)
455 int base64_encoded = 0;
458 if (strncmp(*s, "-\n", 2) == 0) {
484 while (*p == ' ' || *p == '\t') {
488 value->data = (uint8_t *)p;
493 value->length = strlen((char *)value->data);
494 *s = ((char *)value->data) + value->length;
496 value->length = p - (char *)value->data;
501 if (base64_encoded) {
502 int len = ldb_base64_decode((char *)value->data);
504 /* it wasn't valid base64 data */
511 int len = ldb_read_data_file(mem_ctx, value);
513 /* an error occured hile trying to retrieve the file */
523 free a message from a ldif_read
525 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
531 read from a LDIF source, creating a ldb_message
533 struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
534 int (*fgetc_fn)(void *), void *private_data)
536 struct ldb_ldif *ldif;
537 struct ldb_message *msg;
538 const char *attr=NULL;
539 char *chunk=NULL, *s;
540 struct ldb_val value;
545 ldif = talloc(ldb, struct ldb_ldif);
546 if (!ldif) return NULL;
548 ldif->msg = talloc(ldif, struct ldb_message);
549 if (ldif->msg == NULL) {
554 ldif->changetype = LDB_CHANGETYPE_NONE;
558 msg->elements = NULL;
559 msg->num_elements = 0;
561 chunk = next_chunk(ldb, fgetc_fn, private_data);
565 talloc_steal(ldif, chunk);
569 if (next_attr(ldif, &s, &attr, &value) != 0) {
573 /* first line must be a dn */
574 if (ldb_attr_cmp(attr, "dn") != 0) {
575 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'",
580 msg->dn = ldb_dn_from_ldb_val(msg, ldb, &value);
582 if ( ! ldb_dn_validate(msg->dn)) {
583 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Unable to parse dn '%s'",
588 while (next_attr(ldif, &s, &attr, &value) == 0) {
589 const struct ldb_schema_attribute *a;
590 struct ldb_message_element *el;
593 if (ldb_attr_cmp(attr, "changetype") == 0) {
595 for (i=0;ldb_changetypes[i].name;i++) {
596 if (ldb_attr_cmp((char *)value.data, ldb_changetypes[i].name) == 0) {
597 ldif->changetype = ldb_changetypes[i].changetype;
601 if (!ldb_changetypes[i].name) {
602 ldb_debug(ldb, LDB_DEBUG_ERROR,
603 "Error: Bad ldif changetype '%s'",(char *)value.data);
609 if (ldb_attr_cmp(attr, "add") == 0) {
610 flags = LDB_FLAG_MOD_ADD;
613 if (ldb_attr_cmp(attr, "delete") == 0) {
614 flags = LDB_FLAG_MOD_DELETE;
617 if (ldb_attr_cmp(attr, "replace") == 0) {
618 flags = LDB_FLAG_MOD_REPLACE;
621 if (ldb_attr_cmp(attr, "-") == 0) {
627 if (ldb_msg_add_empty(msg, (char *)value.data, flags, NULL) != 0) {
633 el = &msg->elements[msg->num_elements-1];
635 a = ldb_schema_attribute_by_name(ldb, attr);
637 if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
638 flags == el->flags) {
639 /* its a continuation */
641 talloc_realloc(msg->elements, el->values,
642 struct ldb_val, el->num_values+1);
646 ret = a->syntax->ldif_read_fn(ldb, el->values, &value, &el->values[el->num_values]);
650 if (value.length == 0) {
651 ldb_debug(ldb, LDB_DEBUG_ERROR,
652 "Error: Attribute value cannot be empty for attribute '%s'", el->name);
655 if (value.data != el->values[el->num_values].data) {
656 talloc_steal(el->values, el->values[el->num_values].data);
660 /* its a new attribute */
661 msg->elements = talloc_realloc(msg, msg->elements,
662 struct ldb_message_element,
663 msg->num_elements+1);
664 if (!msg->elements) {
667 el = &msg->elements[msg->num_elements];
669 el->name = talloc_strdup(msg->elements, attr);
670 el->values = talloc(msg->elements, struct ldb_val);
671 if (!el->values || !el->name) {
675 ret = a->syntax->ldif_read_fn(ldb, el->values, &value, &el->values[0]);
679 if (value.data != el->values[0].data) {
680 talloc_steal(el->values, el->values[0].data);
696 a wrapper around ldif_read() for reading from FILE*
698 struct ldif_read_file_state {
702 static int fgetc_file(void *private_data)
704 struct ldif_read_file_state *state =
705 (struct ldif_read_file_state *)private_data;
706 return fgetc(state->f);
709 struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f)
711 struct ldif_read_file_state state;
713 return ldb_ldif_read(ldb, fgetc_file, &state);
718 a wrapper around ldif_read() for reading from const char*
720 struct ldif_read_string_state {
724 static int fgetc_string(void *private_data)
726 struct ldif_read_string_state *state =
727 (struct ldif_read_string_state *)private_data;
728 if (state->s[0] != 0) {
734 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char **s)
736 struct ldif_read_string_state state;
737 struct ldb_ldif *ldif;
739 ldif = ldb_ldif_read(ldb, fgetc_string, &state);
746 wrapper around ldif_write() for a file
748 struct ldif_write_file_state {
752 static int fprintf_file(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
754 static int fprintf_file(void *private_data, const char *fmt, ...)
756 struct ldif_write_file_state *state =
757 (struct ldif_write_file_state *)private_data;
762 ret = vfprintf(state->f, fmt, ap);
767 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
769 struct ldif_write_file_state state;
771 return ldb_ldif_write(ldb, fprintf_file, &state, ldif);
775 wrapper around ldif_write() for a string
777 struct ldif_write_string_state {
781 static int ldif_printf_string(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
783 static int ldif_printf_string(void *private_data, const char *fmt, ...)
785 struct ldif_write_string_state *state =
786 (struct ldif_write_string_state *)private_data;
788 size_t oldlen = talloc_get_size(state->string);
791 state->string = talloc_vasprintf_append(state->string, fmt, ap);
793 if (!state->string) {
797 return talloc_get_size(state->string) - oldlen;
800 char *ldb_ldif_write_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
801 const struct ldb_ldif *ldif)
803 struct ldif_write_string_state state;
804 state.string = talloc_strdup(mem_ctx, "");
808 if (ldb_ldif_write(ldb, ldif_printf_string, &state, ldif) == -1) {
815 convenient function to turn a ldb_message into a string. Useful for
818 char *ldb_ldif_message_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
819 enum ldb_changetype changetype,
820 const struct ldb_message *msg)
822 struct ldb_ldif ldif;
824 ldif.changetype = changetype;
825 ldif.msg = discard_const_p(struct ldb_message, msg);
827 return ldb_ldif_write_string(ldb, mem_ctx, &ldif);