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
39 this base64 decoder was taken from jitterbug (written by tridge).
40 we might need to replace it with a new version
42 static int base64_decode(char *s)
44 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
45 int bit_offset, byte_offset, idx, i, n;
46 unsigned char *d = (unsigned char *)s;
51 while (*s && (p=strchr(b64,*s))) {
53 byte_offset = (i*6)/8;
55 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
57 d[byte_offset] |= (idx << (2-bit_offset));
60 d[byte_offset] |= (idx >> (bit_offset-2));
62 d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
69 /* the only termination allowed */
85 char *ldb_base64_encode(const char *buf, int len)
87 const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
88 int bit_offset, byte_offset, idx, i;
89 unsigned char *d = (unsigned char *)buf;
90 int bytes = (len*8 + 5)/6;
93 out = malloc(bytes+2);
94 if (!out) return NULL;
96 for (i=0;i<bytes;i++) {
97 byte_offset = (i*6)/8;
100 idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
102 idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
103 if (byte_offset+1 < len) {
104 idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
117 see if a buffer should be base64 encoded
119 int ldb_should_b64_encode(const struct ldb_val *val)
122 unsigned char *p = val->data;
124 if (val->length == 0 || p[0] == ' ' || p[0] == ':') {
128 for (i=0; i<val->length; i++) {
129 if (!isprint(p[i]) || p[i] == '\n') {
136 /* this macro is used to handle the return checking on fprintf_fn() */
137 #define CHECK_RET do { if (ret < 0) return ret; total += ret; } while (0)
140 write a line folded string onto a file
142 static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *private,
143 const char *buf, size_t length, int start_pos)
148 for (i=0;i<length;i++) {
149 ret = fprintf_fn(private, "%c", buf[i]);
151 if (i != (length-1) && (i + start_pos) % 77 == 0) {
152 ret = fprintf_fn(private, "\n ");
161 encode as base64 to a file
163 static int base64_encode_f(int (*fprintf_fn)(void *, const char *, ...), void *private,
164 const char *buf, int len, int start_pos)
166 char *b = ldb_base64_encode(buf, len);
173 ret = fold_string(fprintf_fn, private, b, strlen(b), start_pos);
180 write to ldif, using a caller supplied write method
182 int ldif_write(int (*fprintf_fn)(void *, const char *, ...),
184 const struct ldb_message *msg)
189 ret = fprintf_fn(private, "dn: %s\n", msg->dn);
192 for (i=0;i<msg->num_elements;i++) {
193 if (ldb_should_b64_encode(&msg->elements[i].value)) {
194 ret = fprintf_fn(private, "%s:: ", msg->elements[i].name);
196 ret = base64_encode_f(fprintf_fn, private,
197 msg->elements[i].value.data,
198 msg->elements[i].value.length,
199 strlen(msg->elements[i].name)+3);
201 ret = fprintf_fn(private, "\n");
204 ret = fprintf_fn(private, "%s: ", msg->elements[i].name);
206 ret = fold_string(fprintf_fn, private,
207 msg->elements[i].value.data,
208 msg->elements[i].value.length,
209 strlen(msg->elements[i].name)+2);
211 ret = fprintf_fn(private, "\n");
215 ret = fprintf_fn(private,"\n");
225 pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
226 this routine removes any RFC2849 continuations and comments
230 static char *next_chunk(int (*fgetc_fn)(void *), void *private)
232 size_t alloc_size=0, chunk_size = 0;
237 while ((c = fgetc_fn(private)) != EOF) {
238 if (chunk_size == alloc_size) {
241 c2 = realloc_p(chunk, char, alloc_size);
257 /* handle continuation lines - see RFC2849 */
258 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
263 /* chunks are terminated by a double line-feed */
264 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
265 chunk[chunk_size-1] = 0;
269 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
274 /* ignore leading blank lines */
275 if (chunk_size == 0 && c == '\n') {
279 chunk[chunk_size++] = c;
286 /* simple ldif attribute parser */
287 static int next_attr(char **s, char **attr, struct ldb_val *value)
290 int base64_encoded = 0;
306 while (isspace(*p)) {
315 value->length = strlen((char *)value->data);
316 *s = ((char *)value->data) + value->length;
318 value->length = p - (char *)value->data;
323 if (base64_encoded) {
324 int len = base64_decode(value->data);
326 /* it wasn't valid base64 data */
337 free a message from a ldif_read
339 void ldif_read_free(struct ldb_message *msg)
341 if (msg->elements) free(msg->elements);
342 if (msg->private) free(msg->private);
347 read from a LDIF source, creating a ldb_message
349 struct ldb_message *ldif_read(int (*fgetc_fn)(void *), void *private)
351 struct ldb_message *msg;
352 char *attr=NULL, *chunk=NULL, *s;
353 struct ldb_val value;
357 msg = malloc_p(struct ldb_message);
358 if (!msg) return NULL;
361 msg->elements = NULL;
362 msg->num_elements = 0;
365 chunk = next_chunk(fgetc_fn, private);
370 msg->private = chunk;
373 if (next_attr(&s, &attr, &value) != 0) {
377 /* first line must be a dn */
378 if (strcmp(attr, "dn") != 0) {
379 fprintf(stderr, "First line must be a dn not '%s'\n", attr);
383 msg->dn = value.data;
385 while (next_attr(&s, &attr, &value) == 0) {
386 msg->elements = realloc_p(msg->elements,
387 struct ldb_message_element,
388 msg->num_elements+1);
389 if (!msg->elements) {
392 msg->elements[msg->num_elements].flags = 0;
393 msg->elements[msg->num_elements].name = attr;
394 msg->elements[msg->num_elements].value = value;
401 if (msg) ldif_read_free(msg);
408 a wrapper around ldif_read() for reading from FILE*
410 struct ldif_read_file_state {
414 static int fgetc_file(void *private)
416 struct ldif_read_file_state *state = private;
417 return fgetc(state->f);
420 struct ldb_message *ldif_read_file(FILE *f)
422 struct ldif_read_file_state state;
424 return ldif_read(fgetc_file, &state);
429 a wrapper around ldif_read() for reading from const char*
431 struct ldif_read_string_state {
435 static int fgetc_string(void *private)
437 struct ldif_read_string_state *state = private;
438 if (state->s[0] != 0) {
444 struct ldb_message *ldif_read_string(const char *s)
446 struct ldif_read_string_state state;
448 return ldif_read(fgetc_string, &state);
453 wrapper around ldif_write() for a file
455 struct ldif_write_file_state {
459 static int fprintf_file(void *private, const char *fmt, ...)
461 struct ldif_write_file_state *state = private;
466 ret = vfprintf(state->f, fmt, ap);
471 int ldif_write_file(FILE *f, const struct ldb_message *msg)
473 struct ldif_write_file_state state;
475 return ldif_write(fprintf_file, &state, msg);