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
43 this base64 decoder was taken from jitterbug (written by tridge).
44 we might need to replace it with a new version
46 static int base64_decode(char *s)
48 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
49 int bit_offset, byte_offset, idx, i, n;
50 unsigned char *d = (unsigned char *)s;
55 while (*s && (p=strchr(b64,*s))) {
57 byte_offset = (i*6)/8;
59 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
61 d[byte_offset] |= (idx << (2-bit_offset));
64 d[byte_offset] |= (idx >> (bit_offset-2));
66 d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
73 /* the only termination allowed */
89 char *ldb_base64_encode(const char *buf, int len)
91 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
92 int bit_offset, byte_offset, idx, i;
93 const unsigned char *d = (const unsigned char *)buf;
94 int bytes = (len*8 + 5)/6;
97 out = malloc(bytes+2);
98 if (!out) return NULL;
100 for (i=0;i<bytes;i++) {
101 byte_offset = (i*6)/8;
102 bit_offset = (i*6)%8;
103 if (bit_offset < 3) {
104 idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
106 idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
107 if (byte_offset+1 < len) {
108 idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
121 see if a buffer should be base64 encoded
123 int ldb_should_b64_encode(const struct ldb_val *val)
126 unsigned char *p = val->data;
128 if (val->length == 0 || p[0] == ' ' || p[0] == ':') {
132 for (i=0; i<val->length; i++) {
133 if (!isprint(p[i]) || p[i] == '\n') {
140 /* this macro is used to handle the return checking on fprintf_fn() */
141 #define CHECK_RET do { if (ret < 0) return ret; total += ret; } while (0)
144 write a line folded string onto a file
146 static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *private,
147 const char *buf, size_t length, int start_pos)
152 for (i=0;i<length;i++) {
153 ret = fprintf_fn(private, "%c", buf[i]);
155 if (i != (length-1) && (i + start_pos) % 77 == 0) {
156 ret = fprintf_fn(private, "\n ");
165 encode as base64 to a file
167 static int base64_encode_f(int (*fprintf_fn)(void *, const char *, ...), void *private,
168 const char *buf, int len, int start_pos)
170 char *b = ldb_base64_encode(buf, len);
177 ret = fold_string(fprintf_fn, private, b, strlen(b), start_pos);
184 static const struct {
186 enum ldb_changetype changetype;
187 } ldb_changetypes[] = {
188 {"add", LDB_CHANGETYPE_ADD},
189 {"delete", LDB_CHANGETYPE_DELETE},
190 {"modify", LDB_CHANGETYPE_MODIFY},
195 write to ldif, using a caller supplied write method
197 int ldif_write(int (*fprintf_fn)(void *, const char *, ...),
199 const struct ldb_ldif *ldif)
203 const struct ldb_message *msg;
207 ret = fprintf_fn(private, "dn: %s\n", msg->dn);
210 if (ldif->changetype != LDB_CHANGETYPE_NONE) {
211 for (i=0;ldb_changetypes[i].name;i++) {
212 if (ldb_changetypes[i].changetype == ldif->changetype) {
216 if (!ldb_changetypes[i].name) {
217 fprintf(stderr,"Invalid changetype\n");
220 ret = fprintf_fn(private, "changetype: %s\n", ldb_changetypes[i].name);
224 for (i=0;i<msg->num_elements;i++) {
225 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
226 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
227 case LDB_FLAG_MOD_ADD:
228 fprintf_fn(private, "add: %s\n",
229 msg->elements[i].name);
231 case LDB_FLAG_MOD_DELETE:
232 fprintf_fn(private, "delete: %s\n",
233 msg->elements[i].name);
235 case LDB_FLAG_MOD_REPLACE:
236 fprintf_fn(private, "replace: %s\n",
237 msg->elements[i].name);
242 for (j=0;j<msg->elements[i].num_values;j++) {
243 if (ldb_should_b64_encode(&msg->elements[i].values[j])) {
244 ret = fprintf_fn(private, "%s:: ",
245 msg->elements[i].name);
247 ret = base64_encode_f(fprintf_fn, private,
248 msg->elements[i].values[j].data,
249 msg->elements[i].values[j].length,
250 strlen(msg->elements[i].name)+3);
252 ret = fprintf_fn(private, "\n");
255 ret = fprintf_fn(private, "%s: ", msg->elements[i].name);
257 ret = fold_string(fprintf_fn, private,
258 msg->elements[i].values[j].data,
259 msg->elements[i].values[j].length,
260 strlen(msg->elements[i].name)+2);
262 ret = fprintf_fn(private, "\n");
266 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
267 fprintf_fn(private, "-\n");
270 ret = fprintf_fn(private,"\n");
280 pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
281 this routine removes any RFC2849 continuations and comments
285 static char *next_chunk(int (*fgetc_fn)(void *), void *private)
287 size_t alloc_size=0, chunk_size = 0;
292 while ((c = fgetc_fn(private)) != EOF) {
293 if (chunk_size+1 >= alloc_size) {
296 c2 = realloc_p(chunk, char, alloc_size);
312 /* handle continuation lines - see RFC2849 */
313 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
318 /* chunks are terminated by a double line-feed */
319 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
320 chunk[chunk_size-1] = 0;
324 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
329 /* ignore leading blank lines */
330 if (chunk_size == 0 && c == '\n') {
334 chunk[chunk_size++] = c;
338 chunk[chunk_size] = 0;
345 /* simple ldif attribute parser */
346 static int next_attr(char **s, char **attr, struct ldb_val *value)
349 int base64_encoded = 0;
351 if (strncmp(*s, "-\n", 2) == 0) {
372 while (isspace(*p)) {
381 value->length = strlen((char *)value->data);
382 *s = ((char *)value->data) + value->length;
384 value->length = p - (char *)value->data;
389 if (base64_encoded) {
390 int len = base64_decode(value->data);
392 /* it wasn't valid base64 data */
403 free a message from a ldif_read
405 void ldif_read_free(struct ldb_ldif *ldif)
407 struct ldb_message *msg = &ldif->msg;
409 for (i=0;i<msg->num_elements;i++) {
410 if (msg->elements[i].values) free(msg->elements[i].values);
412 if (msg->elements) free(msg->elements);
413 if (msg->private_data) free(msg->private_data);
420 static int msg_add_empty(struct ldb_message *msg, const char *name, unsigned flags)
422 struct ldb_message_element *el2, *el;
424 el2 = realloc_p(msg->elements, struct ldb_message_element, msg->num_elements+1);
432 el = &msg->elements[msg->num_elements];
445 read from a LDIF source, creating a ldb_message
447 struct ldb_ldif *ldif_read(int (*fgetc_fn)(void *), void *private_data)
449 struct ldb_ldif *ldif;
450 struct ldb_message *msg;
451 char *attr=NULL, *chunk=NULL, *s;
452 struct ldb_val value;
457 ldif = malloc_p(struct ldb_ldif);
458 if (!ldif) return NULL;
460 ldif->changetype = LDB_CHANGETYPE_NONE;
464 msg->elements = NULL;
465 msg->num_elements = 0;
466 msg->private_data = NULL;
468 chunk = next_chunk(fgetc_fn, private_data);
473 msg->private_data = chunk;
476 if (next_attr(&s, &attr, &value) != 0) {
480 /* first line must be a dn */
481 if (strcmp(attr, "dn") != 0) {
482 fprintf(stderr, "First line must be a dn not '%s'\n", attr);
486 msg->dn = value.data;
488 while (next_attr(&s, &attr, &value) == 0) {
489 struct ldb_message_element *el;
492 if (strcmp(attr, "changetype") == 0) {
494 for (i=0;ldb_changetypes[i].name;i++) {
495 if (strcmp((char *)value.data, ldb_changetypes[i].name) == 0) {
496 ldif->changetype = ldb_changetypes[i].changetype;
500 if (!ldb_changetypes[i].name) {
501 fprintf(stderr,"Bad changetype '%s'\n",
508 if (strcmp(attr, "add") == 0) {
509 flags = LDB_FLAG_MOD_ADD;
512 if (strcmp(attr, "delete") == 0) {
513 flags = LDB_FLAG_MOD_DELETE;
516 if (strcmp(attr, "replace") == 0) {
517 flags = LDB_FLAG_MOD_REPLACE;
520 if (strcmp(attr, "-") == 0) {
526 if (msg_add_empty(msg, (char *)value.data, flags) != 0) {
532 el = &msg->elements[msg->num_elements-1];
534 if (msg->num_elements > 0 && strcmp(attr, el->name) == 0 &&
535 flags == el->flags) {
536 /* its a continuation */
538 realloc_p(el->values, struct ldb_val, el->num_values+1);
542 el->values[el->num_values] = value;
545 /* its a new attribute */
546 msg->elements = realloc_p(msg->elements,
547 struct ldb_message_element,
548 msg->num_elements+1);
549 if (!msg->elements) {
552 msg->elements[msg->num_elements].flags = flags;
553 msg->elements[msg->num_elements].name = attr;
554 el = &msg->elements[msg->num_elements];
555 el->values = malloc_p(struct ldb_val);
560 el->values[0] = value;
568 if (ldif) ldif_read_free(ldif);
575 a wrapper around ldif_read() for reading from FILE*
577 struct ldif_read_file_state {
581 static int fgetc_file(void *private)
583 struct ldif_read_file_state *state = private;
584 return fgetc(state->f);
587 struct ldb_ldif *ldif_read_file(FILE *f)
589 struct ldif_read_file_state state;
591 return ldif_read(fgetc_file, &state);
596 a wrapper around ldif_read() for reading from const char*
598 struct ldif_read_string_state {
602 static int fgetc_string(void *private)
604 struct ldif_read_string_state *state = private;
605 if (state->s[0] != 0) {
611 struct ldb_ldif *ldif_read_string(const char *s)
613 struct ldif_read_string_state state;
615 return ldif_read(fgetc_string, &state);
620 wrapper around ldif_write() for a file
622 struct ldif_write_file_state {
626 static int fprintf_file(void *private, const char *fmt, ...)
628 struct ldif_write_file_state *state = private;
633 ret = vfprintf(state->f, fmt, ap);
638 int ldif_write_file(FILE *f, const struct ldb_ldif *ldif)
640 struct ldif_write_file_state state;
642 return ldif_write(fprintf_file, &state, ldif);