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 ldb_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 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d\n",
224 ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
228 for (i=0;i<msg->num_elements;i++) {
229 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
230 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
231 case LDB_FLAG_MOD_ADD:
232 fprintf_fn(private_data, "add: %s\n",
233 msg->elements[i].name);
235 case LDB_FLAG_MOD_DELETE:
236 fprintf_fn(private_data, "delete: %s\n",
237 msg->elements[i].name);
239 case LDB_FLAG_MOD_REPLACE:
240 fprintf_fn(private_data, "replace: %s\n",
241 msg->elements[i].name);
246 for (j=0;j<msg->elements[i].num_values;j++) {
247 if (ldb_should_b64_encode(&msg->elements[i].values[j])) {
248 ret = fprintf_fn(private_data, "%s:: ",
249 msg->elements[i].name);
251 ret = base64_encode_f(ldb, fprintf_fn, private_data,
252 msg->elements[i].values[j].data,
253 msg->elements[i].values[j].length,
254 strlen(msg->elements[i].name)+3);
256 ret = fprintf_fn(private_data, "\n");
259 ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
261 ret = fold_string(fprintf_fn, private_data,
262 msg->elements[i].values[j].data,
263 msg->elements[i].values[j].length,
264 strlen(msg->elements[i].name)+2);
266 ret = fprintf_fn(private_data, "\n");
270 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
271 fprintf_fn(private_data, "-\n");
274 ret = fprintf_fn(private_data,"\n");
284 pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
285 this routine removes any RFC2849 continuations and comments
289 static char *next_chunk(struct ldb_context *ldb,
290 int (*fgetc_fn)(void *), void *private_data)
292 size_t alloc_size=0, chunk_size = 0;
297 while ((c = fgetc_fn(private_data)) != EOF) {
298 if (chunk_size+1 >= alloc_size) {
301 c2 = ldb_realloc_p(ldb, chunk, char, alloc_size);
303 ldb_free(ldb, chunk);
317 /* handle continuation lines - see RFC2849 */
318 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
323 /* chunks are terminated by a double line-feed */
324 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
325 chunk[chunk_size-1] = 0;
329 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
334 /* ignore leading blank lines */
335 if (chunk_size == 0 && c == '\n') {
339 chunk[chunk_size++] = c;
343 chunk[chunk_size] = 0;
350 /* simple ldif attribute parser */
351 static int next_attr(char **s, const char **attr, struct ldb_val *value)
354 int base64_encoded = 0;
356 if (strncmp(*s, "-\n", 2) == 0) {
377 while (isspace(*p)) {
386 value->length = strlen((char *)value->data);
387 *s = ((char *)value->data) + value->length;
389 value->length = p - (char *)value->data;
394 if (base64_encoded) {
395 int len = base64_decode(value->data);
397 /* it wasn't valid base64 data */
408 free a message from a ldif_read
410 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
412 struct ldb_message *msg = &ldif->msg;
414 for (i=0;i<msg->num_elements;i++) {
415 if (msg->elements[i].name) ldb_free(ldb, msg->elements[i].name);
416 if (msg->elements[i].values) ldb_free(ldb, msg->elements[i].values);
418 if (msg->elements) ldb_free(ldb, msg->elements);
419 if (msg->private_data) ldb_free(ldb, msg->private_data);
426 static int msg_add_empty(struct ldb_context *ldb,
427 struct ldb_message *msg, const char *name, unsigned flags)
429 struct ldb_message_element *el2, *el;
431 el2 = ldb_realloc_p(ldb, msg->elements,
432 struct ldb_message_element, msg->num_elements+1);
440 el = &msg->elements[msg->num_elements];
442 el->name = ldb_strdup(ldb, name);
458 read from a LDIF source, creating a ldb_message
460 struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
461 int (*fgetc_fn)(void *), void *private_data)
463 struct ldb_ldif *ldif;
464 struct ldb_message *msg;
465 const char *attr=NULL;
466 char *chunk=NULL, *s;
467 struct ldb_val value;
472 ldif = ldb_malloc_p(ldb, struct ldb_ldif);
473 if (!ldif) return NULL;
475 ldif->changetype = LDB_CHANGETYPE_NONE;
479 msg->elements = NULL;
480 msg->num_elements = 0;
481 msg->private_data = NULL;
483 chunk = next_chunk(ldb, fgetc_fn, private_data);
488 msg->private_data = chunk;
491 if (next_attr(&s, &attr, &value) != 0) {
495 /* first line must be a dn */
496 if (ldb_attr_cmp(attr, "dn") != 0) {
497 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'\n",
502 msg->dn = value.data;
504 while (next_attr(&s, &attr, &value) == 0) {
505 struct ldb_message_element *el;
508 if (ldb_attr_cmp(attr, "changetype") == 0) {
510 for (i=0;ldb_changetypes[i].name;i++) {
511 if (ldb_attr_cmp((char *)value.data, ldb_changetypes[i].name) == 0) {
512 ldif->changetype = ldb_changetypes[i].changetype;
516 if (!ldb_changetypes[i].name) {
517 ldb_debug(ldb, LDB_DEBUG_ERROR,
518 "Error: Bad ldif changetype '%s'\n",(char *)value.data);
524 if (ldb_attr_cmp(attr, "add") == 0) {
525 flags = LDB_FLAG_MOD_ADD;
528 if (ldb_attr_cmp(attr, "delete") == 0) {
529 flags = LDB_FLAG_MOD_DELETE;
532 if (ldb_attr_cmp(attr, "replace") == 0) {
533 flags = LDB_FLAG_MOD_REPLACE;
536 if (ldb_attr_cmp(attr, "-") == 0) {
542 if (msg_add_empty(ldb, msg, (char *)value.data, flags) != 0) {
548 el = &msg->elements[msg->num_elements-1];
550 if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
551 flags == el->flags) {
552 /* its a continuation */
554 ldb_realloc_p(ldb, el->values,
555 struct ldb_val, el->num_values+1);
559 el->values[el->num_values] = value;
562 /* its a new attribute */
563 msg->elements = ldb_realloc_p(ldb, msg->elements,
564 struct ldb_message_element,
565 msg->num_elements+1);
566 if (!msg->elements) {
569 el = &msg->elements[msg->num_elements];
571 el->name = ldb_strdup(ldb, attr);
572 el->values = ldb_malloc_p(ldb, struct ldb_val);
573 if (!el->values || !el->name) {
577 el->values[0] = value;
585 if (ldif) ldb_ldif_read_free(ldb, ldif);
592 a wrapper around ldif_read() for reading from FILE*
594 struct ldif_read_file_state {
598 static int fgetc_file(void *private_data)
600 struct ldif_read_file_state *state = private_data;
601 return fgetc(state->f);
604 struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f)
606 struct ldif_read_file_state state;
608 return ldb_ldif_read(ldb, fgetc_file, &state);
613 a wrapper around ldif_read() for reading from const char*
615 struct ldif_read_string_state {
619 static int fgetc_string(void *private_data)
621 struct ldif_read_string_state *state = private_data;
622 if (state->s[0] != 0) {
628 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char *s)
630 struct ldif_read_string_state state;
632 return ldb_ldif_read(ldb, fgetc_string, &state);
637 wrapper around ldif_write() for a file
639 struct ldif_write_file_state {
643 static int fprintf_file(void *private_data, const char *fmt, ...)
645 struct ldif_write_file_state *state = private_data;
650 ret = vfprintf(state->f, fmt, ap);
655 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
657 struct ldif_write_file_state state;
659 return ldb_ldif_write(ldb, fprintf_file, &state, ldif);