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
44 this base64 decoder was taken from jitterbug (written by tridge).
45 we might need to replace it with a new version
47 static int base64_decode(char *s)
49 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
50 int bit_offset, byte_offset, idx, i, n;
51 uint8_t *d = (uint8_t *)s;
56 while (*s && (p=strchr(b64,*s))) {
58 byte_offset = (i*6)/8;
60 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
62 d[byte_offset] |= (idx << (2-bit_offset));
65 d[byte_offset] |= (idx >> (bit_offset-2));
67 d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
74 /* the only termination allowed */
90 char *ldb_base64_encode(struct ldb_context *ldb, const char *buf, int len)
92 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
93 int bit_offset, byte_offset, idx, i;
94 const uint8_t *d = (const uint8_t *)buf;
95 int bytes = (len*8 + 5)/6;
98 out = ldb_malloc(ldb, bytes+2);
99 if (!out) return NULL;
101 for (i=0;i<bytes;i++) {
102 byte_offset = (i*6)/8;
103 bit_offset = (i*6)%8;
104 if (bit_offset < 3) {
105 idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
107 idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
108 if (byte_offset+1 < len) {
109 idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
122 see if a buffer should be base64 encoded
124 int ldb_should_b64_encode(const struct ldb_val *val)
127 uint8_t *p = val->data;
129 if (val->length == 0 || p[0] == ' ' || p[0] == ':') {
133 for (i=0; i<val->length; i++) {
134 if (!isprint(p[i]) || p[i] == '\n') {
141 /* this macro is used to handle the return checking on fprintf_fn() */
142 #define CHECK_RET do { if (ret < 0) return ret; total += ret; } while (0)
145 write a line folded string onto a file
147 static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *private_data,
148 const char *buf, size_t length, int start_pos)
153 for (i=0;i<length;i++) {
154 ret = fprintf_fn(private_data, "%c", buf[i]);
156 if (i != (length-1) && (i + start_pos) % 77 == 0) {
157 ret = fprintf_fn(private_data, "\n ");
166 encode as base64 to a file
168 static int base64_encode_f(struct ldb_context *ldb,
169 int (*fprintf_fn)(void *, const char *, ...),
171 const char *buf, int len, int start_pos)
173 char *b = ldb_base64_encode(ldb, buf, len);
180 ret = fold_string(fprintf_fn, private_data, b, strlen(b), start_pos);
187 static const struct {
189 enum ldb_changetype changetype;
190 } ldb_changetypes[] = {
191 {"add", LDB_CHANGETYPE_ADD},
192 {"delete", LDB_CHANGETYPE_DELETE},
193 {"modify", LDB_CHANGETYPE_MODIFY},
198 write to ldif, using a caller supplied write method
200 int ldb_ldif_write(struct ldb_context *ldb,
201 int (*fprintf_fn)(void *, const char *, ...),
203 const struct ldb_ldif *ldif)
207 const struct ldb_message *msg;
211 ret = fprintf_fn(private_data, "dn: %s\n", msg->dn);
214 if (ldif->changetype != LDB_CHANGETYPE_NONE) {
215 for (i=0;ldb_changetypes[i].name;i++) {
216 if (ldb_changetypes[i].changetype == ldif->changetype) {
220 if (!ldb_changetypes[i].name) {
221 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d\n",
225 ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
229 for (i=0;i<msg->num_elements;i++) {
230 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
231 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
232 case LDB_FLAG_MOD_ADD:
233 fprintf_fn(private_data, "add: %s\n",
234 msg->elements[i].name);
236 case LDB_FLAG_MOD_DELETE:
237 fprintf_fn(private_data, "delete: %s\n",
238 msg->elements[i].name);
240 case LDB_FLAG_MOD_REPLACE:
241 fprintf_fn(private_data, "replace: %s\n",
242 msg->elements[i].name);
247 for (j=0;j<msg->elements[i].num_values;j++) {
248 if (ldb_should_b64_encode(&msg->elements[i].values[j])) {
249 ret = fprintf_fn(private_data, "%s:: ",
250 msg->elements[i].name);
252 ret = base64_encode_f(ldb, fprintf_fn, private_data,
253 msg->elements[i].values[j].data,
254 msg->elements[i].values[j].length,
255 strlen(msg->elements[i].name)+3);
257 ret = fprintf_fn(private_data, "\n");
260 ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
262 ret = fold_string(fprintf_fn, private_data,
263 msg->elements[i].values[j].data,
264 msg->elements[i].values[j].length,
265 strlen(msg->elements[i].name)+2);
267 ret = fprintf_fn(private_data, "\n");
271 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
272 fprintf_fn(private_data, "-\n");
275 ret = fprintf_fn(private_data,"\n");
285 pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
286 this routine removes any RFC2849 continuations and comments
290 static char *next_chunk(struct ldb_context *ldb,
291 int (*fgetc_fn)(void *), void *private_data)
293 size_t alloc_size=0, chunk_size = 0;
298 while ((c = fgetc_fn(private_data)) != EOF) {
299 if (chunk_size+1 >= alloc_size) {
302 c2 = ldb_realloc_p(ldb, chunk, char, alloc_size);
304 ldb_free(ldb, chunk);
318 /* handle continuation lines - see RFC2849 */
319 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
324 /* chunks are terminated by a double line-feed */
325 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
326 chunk[chunk_size-1] = 0;
330 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
335 /* ignore leading blank lines */
336 if (chunk_size == 0 && c == '\n') {
340 chunk[chunk_size++] = c;
344 chunk[chunk_size] = 0;
351 /* simple ldif attribute parser */
352 static int next_attr(char **s, const char **attr, struct ldb_val *value)
355 int base64_encoded = 0;
357 if (strncmp(*s, "-\n", 2) == 0) {
378 while (isspace(*p)) {
387 value->length = strlen((char *)value->data);
388 *s = ((char *)value->data) + value->length;
390 value->length = p - (char *)value->data;
395 if (base64_encoded) {
396 int len = base64_decode(value->data);
398 /* it wasn't valid base64 data */
409 free a message from a ldif_read
411 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
413 struct ldb_message *msg = &ldif->msg;
415 for (i=0;i<msg->num_elements;i++) {
416 if (msg->elements[i].name) ldb_free(ldb, msg->elements[i].name);
417 if (msg->elements[i].values) ldb_free(ldb, msg->elements[i].values);
419 if (msg->elements) ldb_free(ldb, msg->elements);
420 if (msg->private_data) ldb_free(ldb, msg->private_data);
427 static int msg_add_empty(struct ldb_context *ldb,
428 struct ldb_message *msg, const char *name, unsigned flags)
430 struct ldb_message_element *el2, *el;
432 el2 = ldb_realloc_p(ldb, msg->elements,
433 struct ldb_message_element, msg->num_elements+1);
441 el = &msg->elements[msg->num_elements];
443 el->name = ldb_strdup(ldb, name);
459 read from a LDIF source, creating a ldb_message
461 struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
462 int (*fgetc_fn)(void *), void *private_data)
464 struct ldb_ldif *ldif;
465 struct ldb_message *msg;
466 const char *attr=NULL;
467 char *chunk=NULL, *s;
468 struct ldb_val value;
473 ldif = ldb_malloc_p(ldb, struct ldb_ldif);
474 if (!ldif) return NULL;
476 ldif->changetype = LDB_CHANGETYPE_NONE;
480 msg->elements = NULL;
481 msg->num_elements = 0;
482 msg->private_data = NULL;
484 chunk = next_chunk(ldb, fgetc_fn, private_data);
489 msg->private_data = chunk;
492 if (next_attr(&s, &attr, &value) != 0) {
496 /* first line must be a dn */
497 if (ldb_attr_cmp(attr, "dn") != 0) {
498 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'\n",
503 msg->dn = value.data;
505 while (next_attr(&s, &attr, &value) == 0) {
506 struct ldb_message_element *el;
509 if (ldb_attr_cmp(attr, "changetype") == 0) {
511 for (i=0;ldb_changetypes[i].name;i++) {
512 if (ldb_attr_cmp((char *)value.data, ldb_changetypes[i].name) == 0) {
513 ldif->changetype = ldb_changetypes[i].changetype;
517 if (!ldb_changetypes[i].name) {
518 ldb_debug(ldb, LDB_DEBUG_ERROR,
519 "Error: Bad ldif changetype '%s'\n",(char *)value.data);
525 if (ldb_attr_cmp(attr, "add") == 0) {
526 flags = LDB_FLAG_MOD_ADD;
529 if (ldb_attr_cmp(attr, "delete") == 0) {
530 flags = LDB_FLAG_MOD_DELETE;
533 if (ldb_attr_cmp(attr, "replace") == 0) {
534 flags = LDB_FLAG_MOD_REPLACE;
537 if (ldb_attr_cmp(attr, "-") == 0) {
543 if (msg_add_empty(ldb, msg, (char *)value.data, flags) != 0) {
549 el = &msg->elements[msg->num_elements-1];
551 if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
552 flags == el->flags) {
553 /* its a continuation */
555 ldb_realloc_p(ldb, el->values,
556 struct ldb_val, el->num_values+1);
560 el->values[el->num_values] = value;
563 /* its a new attribute */
564 msg->elements = ldb_realloc_p(ldb, msg->elements,
565 struct ldb_message_element,
566 msg->num_elements+1);
567 if (!msg->elements) {
570 el = &msg->elements[msg->num_elements];
572 el->name = ldb_strdup(ldb, attr);
573 el->values = ldb_malloc_p(ldb, struct ldb_val);
574 if (!el->values || !el->name) {
578 el->values[0] = value;
586 if (ldif) ldb_ldif_read_free(ldb, ldif);
593 a wrapper around ldif_read() for reading from FILE*
595 struct ldif_read_file_state {
599 static int fgetc_file(void *private_data)
601 struct ldif_read_file_state *state = private_data;
602 return fgetc(state->f);
605 struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f)
607 struct ldif_read_file_state state;
609 return ldb_ldif_read(ldb, fgetc_file, &state);
614 a wrapper around ldif_read() for reading from const char*
616 struct ldif_read_string_state {
620 static int fgetc_string(void *private_data)
622 struct ldif_read_string_state *state = private_data;
623 if (state->s[0] != 0) {
629 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char *s)
631 struct ldif_read_string_state state;
633 return ldb_ldif_read(ldb, fgetc_string, &state);
638 wrapper around ldif_write() for a file
640 struct ldif_write_file_state {
644 static int fprintf_file(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
646 static int fprintf_file(void *private_data, const char *fmt, ...)
648 struct ldif_write_file_state *state = private_data;
653 ret = vfprintf(state->f, fmt, ap);
658 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
660 struct ldif_write_file_state state;
662 return ldb_ldif_write(ldb, fprintf_file, &state, ldif);