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"
46 this base64 decoder was taken from jitterbug (written by tridge).
47 we might need to replace it with a new version
49 static int base64_decode(char *s)
51 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
52 int bit_offset, byte_offset, idx, i, n;
53 uint8_t *d = (uint8_t *)s;
58 while (*s && (p=strchr(b64,*s))) {
60 byte_offset = (i*6)/8;
62 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
64 d[byte_offset] |= (idx << (2-bit_offset));
67 d[byte_offset] |= (idx >> (bit_offset-2));
69 d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
76 /* the only termination allowed */
92 char *ldb_base64_encode(struct ldb_context *ldb, const char *buf, int len)
94 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
95 int bit_offset, byte_offset, idx, i;
96 const uint8_t *d = (const uint8_t *)buf;
97 int bytes = (len*8 + 5)/6;
100 out = talloc_array_p(ldb, char, bytes+2);
101 if (!out) return NULL;
103 for (i=0;i<bytes;i++) {
104 byte_offset = (i*6)/8;
105 bit_offset = (i*6)%8;
106 if (bit_offset < 3) {
107 idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
109 idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
110 if (byte_offset+1 < len) {
111 idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
124 see if a buffer should be base64 encoded
126 int ldb_should_b64_encode(const struct ldb_val *val)
129 uint8_t *p = val->data;
131 if (val->length == 0) {
135 if (p[0] == ' ' || p[0] == ':') {
139 for (i=0; i<val->length; i++) {
140 if (!isprint(p[i]) || p[i] == '\n') {
147 /* this macro is used to handle the return checking on fprintf_fn() */
148 #define CHECK_RET do { if (ret < 0) return ret; total += ret; } while (0)
151 write a line folded string onto a file
153 static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *private_data,
154 const char *buf, size_t length, int start_pos)
159 for (i=0;i<length;i++) {
160 ret = fprintf_fn(private_data, "%c", buf[i]);
162 if (i != (length-1) && (i + start_pos) % 77 == 0) {
163 ret = fprintf_fn(private_data, "\n ");
172 encode as base64 to a file
174 static int base64_encode_f(struct ldb_context *ldb,
175 int (*fprintf_fn)(void *, const char *, ...),
177 const char *buf, int len, int start_pos)
179 char *b = ldb_base64_encode(ldb, buf, len);
186 ret = fold_string(fprintf_fn, private_data, b, strlen(b), start_pos);
193 static const struct {
195 enum ldb_changetype changetype;
196 } ldb_changetypes[] = {
197 {"add", LDB_CHANGETYPE_ADD},
198 {"delete", LDB_CHANGETYPE_DELETE},
199 {"modify", LDB_CHANGETYPE_MODIFY},
204 write to ldif, using a caller supplied write method
206 int ldb_ldif_write(struct ldb_context *ldb,
207 int (*fprintf_fn)(void *, const char *, ...),
209 const struct ldb_ldif *ldif)
213 const struct ldb_message *msg;
217 ret = fprintf_fn(private_data, "dn: %s\n", msg->dn);
220 if (ldif->changetype != LDB_CHANGETYPE_NONE) {
221 for (i=0;ldb_changetypes[i].name;i++) {
222 if (ldb_changetypes[i].changetype == ldif->changetype) {
226 if (!ldb_changetypes[i].name) {
227 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d\n",
231 ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
235 for (i=0;i<msg->num_elements;i++) {
236 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
237 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
238 case LDB_FLAG_MOD_ADD:
239 fprintf_fn(private_data, "add: %s\n",
240 msg->elements[i].name);
242 case LDB_FLAG_MOD_DELETE:
243 fprintf_fn(private_data, "delete: %s\n",
244 msg->elements[i].name);
246 case LDB_FLAG_MOD_REPLACE:
247 fprintf_fn(private_data, "replace: %s\n",
248 msg->elements[i].name);
253 for (j=0;j<msg->elements[i].num_values;j++) {
254 if (ldb_should_b64_encode(&msg->elements[i].values[j])) {
255 ret = fprintf_fn(private_data, "%s:: ",
256 msg->elements[i].name);
258 ret = base64_encode_f(ldb, fprintf_fn, private_data,
259 msg->elements[i].values[j].data,
260 msg->elements[i].values[j].length,
261 strlen(msg->elements[i].name)+3);
263 ret = fprintf_fn(private_data, "\n");
266 ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
268 ret = fold_string(fprintf_fn, private_data,
269 msg->elements[i].values[j].data,
270 msg->elements[i].values[j].length,
271 strlen(msg->elements[i].name)+2);
273 ret = fprintf_fn(private_data, "\n");
277 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
278 fprintf_fn(private_data, "-\n");
281 ret = fprintf_fn(private_data,"\n");
291 pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
292 this routine removes any RFC2849 continuations and comments
296 static char *next_chunk(struct ldb_context *ldb,
297 int (*fgetc_fn)(void *), void *private_data)
299 size_t alloc_size=0, chunk_size = 0;
304 while ((c = fgetc_fn(private_data)) != EOF) {
305 if (chunk_size+1 >= alloc_size) {
308 c2 = talloc_realloc_p(ldb, chunk, char, alloc_size);
324 /* handle continuation lines - see RFC2849 */
325 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
330 /* chunks are terminated by a double line-feed */
331 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
332 chunk[chunk_size-1] = 0;
336 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
341 /* ignore leading blank lines */
342 if (chunk_size == 0 && c == '\n') {
346 chunk[chunk_size++] = c;
350 chunk[chunk_size] = 0;
357 /* simple ldif attribute parser */
358 static int next_attr(char **s, const char **attr, struct ldb_val *value)
361 int base64_encoded = 0;
363 if (strncmp(*s, "-\n", 2) == 0) {
384 while (*p == ' ' || *p == '\t') {
393 value->length = strlen((char *)value->data);
394 *s = ((char *)value->data) + value->length;
396 value->length = p - (char *)value->data;
401 if (base64_encoded) {
402 int len = base64_decode(value->data);
404 /* it wasn't valid base64 data */
415 free a message from a ldif_read
417 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
425 static int msg_add_empty(struct ldb_context *ldb,
426 struct ldb_message *msg, const char *name, unsigned flags)
428 struct ldb_message_element *el2, *el;
430 el2 = talloc_realloc_p(msg, msg->elements,
431 struct ldb_message_element, msg->num_elements+1);
439 el = &msg->elements[msg->num_elements];
441 el->name = talloc_strdup(msg->elements, name);
457 read from a LDIF source, creating a ldb_message
459 struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
460 int (*fgetc_fn)(void *), void *private_data)
462 struct ldb_ldif *ldif;
463 struct ldb_message *msg;
464 const char *attr=NULL;
465 char *chunk=NULL, *s;
466 struct ldb_val value;
471 ldif = talloc_p(ldb, struct ldb_ldif);
472 if (!ldif) return NULL;
474 ldif->msg = talloc_p(ldif, struct ldb_message);
475 if (ldif->msg == NULL) {
480 ldif->changetype = LDB_CHANGETYPE_NONE;
484 msg->elements = NULL;
485 msg->num_elements = 0;
486 msg->private_data = NULL;
488 chunk = next_chunk(ldb, fgetc_fn, private_data);
493 msg->private_data = chunk;
496 if (next_attr(&s, &attr, &value) != 0) {
500 /* first line must be a dn */
501 if (ldb_attr_cmp(attr, "dn") != 0) {
502 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'\n",
507 msg->dn = value.data;
509 while (next_attr(&s, &attr, &value) == 0) {
510 struct ldb_message_element *el;
513 if (ldb_attr_cmp(attr, "changetype") == 0) {
515 for (i=0;ldb_changetypes[i].name;i++) {
516 if (ldb_attr_cmp((char *)value.data, ldb_changetypes[i].name) == 0) {
517 ldif->changetype = ldb_changetypes[i].changetype;
521 if (!ldb_changetypes[i].name) {
522 ldb_debug(ldb, LDB_DEBUG_ERROR,
523 "Error: Bad ldif changetype '%s'\n",(char *)value.data);
529 if (ldb_attr_cmp(attr, "add") == 0) {
530 flags = LDB_FLAG_MOD_ADD;
533 if (ldb_attr_cmp(attr, "delete") == 0) {
534 flags = LDB_FLAG_MOD_DELETE;
537 if (ldb_attr_cmp(attr, "replace") == 0) {
538 flags = LDB_FLAG_MOD_REPLACE;
541 if (ldb_attr_cmp(attr, "-") == 0) {
547 if (msg_add_empty(ldb, msg, (char *)value.data, flags) != 0) {
553 el = &msg->elements[msg->num_elements-1];
555 if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
556 flags == el->flags) {
557 /* its a continuation */
559 talloc_realloc_p(msg->elements, el->values,
560 struct ldb_val, el->num_values+1);
564 el->values[el->num_values] = value;
567 /* its a new attribute */
568 msg->elements = talloc_realloc_p(ldif, msg->elements,
569 struct ldb_message_element,
570 msg->num_elements+1);
571 if (!msg->elements) {
574 el = &msg->elements[msg->num_elements];
576 el->name = talloc_strdup(msg->elements, attr);
577 el->values = talloc_p(msg->elements, struct ldb_val);
578 if (!el->values || !el->name) {
582 el->values[0] = value;
597 a wrapper around ldif_read() for reading from FILE*
599 struct ldif_read_file_state {
603 static int fgetc_file(void *private_data)
605 struct ldif_read_file_state *state = private_data;
606 return fgetc(state->f);
609 struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f)
611 struct ldif_read_file_state state;
613 return ldb_ldif_read(ldb, fgetc_file, &state);
618 a wrapper around ldif_read() for reading from const char*
620 struct ldif_read_string_state {
624 static int fgetc_string(void *private_data)
626 struct ldif_read_string_state *state = private_data;
627 if (state->s[0] != 0) {
633 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char *s)
635 struct ldif_read_string_state state;
637 return ldb_ldif_read(ldb, fgetc_string, &state);
642 wrapper around ldif_write() for a file
644 struct ldif_write_file_state {
648 static int fprintf_file(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
650 static int fprintf_file(void *private_data, const char *fmt, ...)
652 struct ldif_write_file_state *state = private_data;
657 ret = vfprintf(state->f, fmt, ap);
662 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
664 struct ldif_write_file_state state;
666 return ldb_ldif_write(ldb, fprintf_file, &state, ldif);