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(struct ldb_context *ldb, 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 = ldb_malloc(ldb, 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_data,
147 const char *buf, size_t length, int start_pos)
152 for (i=0;i<length;i++) {
153 ret = fprintf_fn(private_data, "%c", buf[i]);
155 if (i != (length-1) && (i + start_pos) % 77 == 0) {
156 ret = fprintf_fn(private_data, "\n ");
165 encode as base64 to a file
167 static int base64_encode_f(struct ldb_context *ldb,
168 int (*fprintf_fn)(void *, const char *, ...),
170 const char *buf, int len, int start_pos)
172 char *b = ldb_base64_encode(ldb, buf, len);
179 ret = fold_string(fprintf_fn, private_data, b, strlen(b), start_pos);
186 static const struct {
188 enum ldb_changetype changetype;
189 } ldb_changetypes[] = {
190 {"add", LDB_CHANGETYPE_ADD},
191 {"delete", LDB_CHANGETYPE_DELETE},
192 {"modify", LDB_CHANGETYPE_MODIFY},
197 write to ldif, using a caller supplied write method
199 int ldif_write(struct ldb_context *ldb,
200 int (*fprintf_fn)(void *, const char *, ...),
202 const struct ldb_ldif *ldif)
206 const struct ldb_message *msg;
210 ret = fprintf_fn(private_data, "dn: %s\n", msg->dn);
213 if (ldif->changetype != LDB_CHANGETYPE_NONE) {
214 for (i=0;ldb_changetypes[i].name;i++) {
215 if (ldb_changetypes[i].changetype == ldif->changetype) {
219 if (!ldb_changetypes[i].name) {
220 fprintf(stderr,"Invalid changetype\n");
223 ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
227 for (i=0;i<msg->num_elements;i++) {
228 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
229 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
230 case LDB_FLAG_MOD_ADD:
231 fprintf_fn(private_data, "add: %s\n",
232 msg->elements[i].name);
234 case LDB_FLAG_MOD_DELETE:
235 fprintf_fn(private_data, "delete: %s\n",
236 msg->elements[i].name);
238 case LDB_FLAG_MOD_REPLACE:
239 fprintf_fn(private_data, "replace: %s\n",
240 msg->elements[i].name);
245 for (j=0;j<msg->elements[i].num_values;j++) {
246 if (ldb_should_b64_encode(&msg->elements[i].values[j])) {
247 ret = fprintf_fn(private_data, "%s:: ",
248 msg->elements[i].name);
250 ret = base64_encode_f(ldb, fprintf_fn, private_data,
251 msg->elements[i].values[j].data,
252 msg->elements[i].values[j].length,
253 strlen(msg->elements[i].name)+3);
255 ret = fprintf_fn(private_data, "\n");
258 ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
260 ret = fold_string(fprintf_fn, private_data,
261 msg->elements[i].values[j].data,
262 msg->elements[i].values[j].length,
263 strlen(msg->elements[i].name)+2);
265 ret = fprintf_fn(private_data, "\n");
269 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
270 fprintf_fn(private_data, "-\n");
273 ret = fprintf_fn(private_data,"\n");
283 pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
284 this routine removes any RFC2849 continuations and comments
288 static char *next_chunk(struct ldb_context *ldb,
289 int (*fgetc_fn)(void *), void *private_data)
291 size_t alloc_size=0, chunk_size = 0;
296 while ((c = fgetc_fn(private_data)) != EOF) {
297 if (chunk_size+1 >= alloc_size) {
300 c2 = ldb_realloc_p(ldb, chunk, char, alloc_size);
302 ldb_free(ldb, chunk);
316 /* handle continuation lines - see RFC2849 */
317 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
322 /* chunks are terminated by a double line-feed */
323 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
324 chunk[chunk_size-1] = 0;
328 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
333 /* ignore leading blank lines */
334 if (chunk_size == 0 && c == '\n') {
338 chunk[chunk_size++] = c;
342 chunk[chunk_size] = 0;
349 /* simple ldif attribute parser */
350 static int next_attr(char **s, const char **attr, struct ldb_val *value)
353 int base64_encoded = 0;
355 if (strncmp(*s, "-\n", 2) == 0) {
376 while (isspace(*p)) {
385 value->length = strlen((char *)value->data);
386 *s = ((char *)value->data) + value->length;
388 value->length = p - (char *)value->data;
393 if (base64_encoded) {
394 int len = base64_decode(value->data);
396 /* it wasn't valid base64 data */
407 free a message from a ldif_read
409 void ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
411 struct ldb_message *msg = &ldif->msg;
413 for (i=0;i<msg->num_elements;i++) {
414 if (msg->elements[i].name) ldb_free(ldb, msg->elements[i].name);
415 if (msg->elements[i].values) ldb_free(ldb, msg->elements[i].values);
417 if (msg->elements) ldb_free(ldb, msg->elements);
418 if (msg->private_data) ldb_free(ldb, msg->private_data);
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 = ldb_realloc_p(ldb, msg->elements,
431 struct ldb_message_element, msg->num_elements+1);
439 el = &msg->elements[msg->num_elements];
441 el->name = ldb_strdup(ldb, name);
457 read from a LDIF source, creating a ldb_message
459 struct ldb_ldif *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 = ldb_malloc_p(ldb, struct ldb_ldif);
472 if (!ldif) return NULL;
474 ldif->changetype = LDB_CHANGETYPE_NONE;
478 msg->elements = NULL;
479 msg->num_elements = 0;
480 msg->private_data = NULL;
482 chunk = next_chunk(ldb, fgetc_fn, private_data);
487 msg->private_data = chunk;
490 if (next_attr(&s, &attr, &value) != 0) {
494 /* first line must be a dn */
495 if (ldb_attr_cmp(attr, "dn") != 0) {
496 fprintf(stderr, "First line must be a dn not '%s'\n", attr);
500 msg->dn = value.data;
502 while (next_attr(&s, &attr, &value) == 0) {
503 struct ldb_message_element *el;
506 if (ldb_attr_cmp(attr, "changetype") == 0) {
508 for (i=0;ldb_changetypes[i].name;i++) {
509 if (ldb_attr_cmp((char *)value.data, ldb_changetypes[i].name) == 0) {
510 ldif->changetype = ldb_changetypes[i].changetype;
514 if (!ldb_changetypes[i].name) {
515 fprintf(stderr,"Bad changetype '%s'\n",
522 if (ldb_attr_cmp(attr, "add") == 0) {
523 flags = LDB_FLAG_MOD_ADD;
526 if (ldb_attr_cmp(attr, "delete") == 0) {
527 flags = LDB_FLAG_MOD_DELETE;
530 if (ldb_attr_cmp(attr, "replace") == 0) {
531 flags = LDB_FLAG_MOD_REPLACE;
534 if (ldb_attr_cmp(attr, "-") == 0) {
540 if (msg_add_empty(ldb, msg, (char *)value.data, flags) != 0) {
546 el = &msg->elements[msg->num_elements-1];
548 if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
549 flags == el->flags) {
550 /* its a continuation */
552 ldb_realloc_p(ldb, el->values,
553 struct ldb_val, el->num_values+1);
557 el->values[el->num_values] = value;
560 /* its a new attribute */
561 msg->elements = ldb_realloc_p(ldb, msg->elements,
562 struct ldb_message_element,
563 msg->num_elements+1);
564 if (!msg->elements) {
567 el = &msg->elements[msg->num_elements];
569 el->name = ldb_strdup(ldb, attr);
570 el->values = ldb_malloc_p(ldb, struct ldb_val);
571 if (!el->values || !el->name) {
575 el->values[0] = value;
583 if (ldif) ldif_read_free(ldb, ldif);
590 a wrapper around ldif_read() for reading from FILE*
592 struct ldif_read_file_state {
596 static int fgetc_file(void *private_data)
598 struct ldif_read_file_state *state = private_data;
599 return fgetc(state->f);
602 struct ldb_ldif *ldif_read_file(struct ldb_context *ldb, FILE *f)
604 struct ldif_read_file_state state;
606 return ldif_read(ldb, fgetc_file, &state);
611 a wrapper around ldif_read() for reading from const char*
613 struct ldif_read_string_state {
617 static int fgetc_string(void *private_data)
619 struct ldif_read_string_state *state = private_data;
620 if (state->s[0] != 0) {
626 struct ldb_ldif *ldif_read_string(struct ldb_context *ldb, const char *s)
628 struct ldif_read_string_state state;
630 return ldif_read(ldb, fgetc_string, &state);
635 wrapper around ldif_write() for a file
637 struct ldif_write_file_state {
641 static int fprintf_file(void *private_data, const char *fmt, ...)
643 struct ldif_write_file_state *state = private_data;
648 ret = vfprintf(state->f, fmt, ap);
653 int ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
655 struct ldif_write_file_state state;
657 return ldif_write(ldb, fprintf_file, &state, ldif);