63b797c4f3d97ddd2283c06511531a3ccf7340dd
[samba.git] / source4 / lib / ldb / common / ldb_ldif.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Tridgell  2004
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
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 3 of the License, or (at your option) any later version.
14
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.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25  *  Name: ldb
26  *
27  *  Component: ldif routines
28  *
29  *  Description: ldif pack/unpack routines
30  *
31  *  Author: Andrew Tridgell
32  */
33
34 /*
35   see RFC2849 for the LDIF format definition
36 */
37
38 #include "ldb_private.h"
39 #include "system/locale.h"
40
41 /*
42   
43 */
44 static int ldb_read_data_file(TALLOC_CTX *mem_ctx, struct ldb_val *value)
45 {
46         struct stat statbuf;
47         char *buf;
48         int count, size, bytes;
49         int ret;
50         int f;
51         const char *fname = (const char *)value->data;
52
53         if (strncmp(fname, "file://", 7) != 0) {
54                 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
55         }
56         fname += 7;
57
58         f = open(fname, O_RDONLY);
59         if (f == -1) {
60                 return -1;
61         }
62
63         if (fstat(f, &statbuf) != 0) {
64                 ret = -1;
65                 goto done;
66         }
67
68         if (statbuf.st_size == 0) {
69                 ret = -1;
70                 goto done;
71         }
72
73         value->data = (uint8_t *)talloc_size(mem_ctx, statbuf.st_size + 1);
74         if (value->data == NULL) {
75                 ret = -1;
76                 goto done;
77         }
78         value->data[statbuf.st_size] = 0;
79
80         count = 0;
81         size = statbuf.st_size;
82         buf = (char *)value->data;
83         while (count < statbuf.st_size) {
84                 bytes = read(f, buf, size);
85                 if (bytes == -1) {
86                         talloc_free(value->data);
87                         ret = -1;
88                         goto done;
89                 }
90                 count += bytes;
91                 buf += bytes;
92                 size -= bytes;
93         }
94
95         value->length = statbuf.st_size;
96         ret = statbuf.st_size;
97
98 done:
99         close(f);
100         return ret;
101 }
102
103 /*
104   this base64 decoder was taken from jitterbug (written by tridge).
105   we might need to replace it with a new version
106 */
107 int ldb_base64_decode(char *s)
108 {
109         const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
110         int bit_offset=0, byte_offset, idx, i, n;
111         uint8_t *d = (uint8_t *)s;
112         char *p=NULL;
113
114         n=i=0;
115
116         while (*s && (p=strchr(b64,*s))) {
117                 idx = (int)(p - b64);
118                 byte_offset = (i*6)/8;
119                 bit_offset = (i*6)%8;
120                 d[byte_offset] &= ~((1<<(8-bit_offset))-1);
121                 if (bit_offset < 3) {
122                         d[byte_offset] |= (idx << (2-bit_offset));
123                         n = byte_offset+1;
124                 } else {
125                         d[byte_offset] |= (idx >> (bit_offset-2));
126                         d[byte_offset+1] = 0;
127                         d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;
128                         n = byte_offset+2;
129                 }
130                 s++; i++;
131         }
132         if (bit_offset >= 3) {
133                 n--;
134         }
135
136         if (*s && !p) {
137                 /* the only termination allowed */
138                 if (*s != '=') {
139                         return -1;
140                 }
141         }
142
143         /* null terminate */
144         d[n] = 0;
145         return n;
146 }
147
148
149 /*
150   encode as base64
151   caller frees
152 */
153 char *ldb_base64_encode(TALLOC_CTX *mem_ctx, const char *buf, int len)
154 {
155         const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
156         int bit_offset, byte_offset, idx, i;
157         const uint8_t *d = (const uint8_t *)buf;
158         int bytes = (len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
159         char *out;
160
161         out = talloc_array(mem_ctx, char, bytes+pad_bytes+1);
162         if (!out) return NULL;
163
164         for (i=0;i<bytes;i++) {
165                 byte_offset = (i*6)/8;
166                 bit_offset = (i*6)%8;
167                 if (bit_offset < 3) {
168                         idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;
169                 } else {
170                         idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;
171                         if (byte_offset+1 < len) {
172                                 idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));
173                         }
174                 }
175                 out[i] = b64[idx];
176         }
177
178         for (;i<bytes+pad_bytes;i++)
179                 out[i] = '=';
180         out[i] = 0;
181
182         return out;
183 }
184
185 /*
186   see if a buffer should be base64 encoded
187 */
188 int ldb_should_b64_encode(struct ldb_context *ldb, const struct ldb_val *val)
189 {
190         unsigned int i;
191         uint8_t *p = val->data;
192
193         if (val->length == 0) {
194                 return 0;
195         }
196
197         if (p[0] == ' ' || p[0] == ':') {
198                 return 1;
199         }
200
201         for (i=0; i<val->length; i++) {
202                 if (!isprint(p[i]) || p[i] == '\n') {
203                         return 1;
204                 }
205         }
206         return 0;
207 }
208
209 /* this macro is used to handle the return checking on fprintf_fn() */
210 #define CHECK_RET do { if (ret < 0) return ret; total += ret; } while (0)
211
212 /*
213   write a line folded string onto a file
214 */
215 static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *private_data,
216                         const char *buf, size_t length, int start_pos)
217 {
218         size_t i;
219         int total=0, ret;
220
221         for (i=0;i<length;i++) {
222                 ret = fprintf_fn(private_data, "%c", buf[i]);
223                 CHECK_RET;
224                 if (i != (length-1) && (i + start_pos) % 77 == 0) {
225                         ret = fprintf_fn(private_data, "\n ");
226                         CHECK_RET;
227                 }
228         }
229
230         return total;
231 }
232
233 #undef CHECK_RET
234
235 /*
236   encode as base64 to a file
237 */
238 static int base64_encode_f(struct ldb_context *ldb,
239                            int (*fprintf_fn)(void *, const char *, ...), 
240                            void *private_data,
241                            const char *buf, int len, int start_pos)
242 {
243         char *b = ldb_base64_encode(ldb, buf, len);
244         int ret;
245
246         if (!b) {
247                 return -1;
248         }
249
250         ret = fold_string(fprintf_fn, private_data, b, strlen(b), start_pos);
251
252         talloc_free(b);
253         return ret;
254 }
255
256
257 static const struct {
258         const char *name;
259         enum ldb_changetype changetype;
260 } ldb_changetypes[] = {
261         {"add",    LDB_CHANGETYPE_ADD},
262         {"delete", LDB_CHANGETYPE_DELETE},
263         {"modify", LDB_CHANGETYPE_MODIFY},
264         {"modrdn", LDB_CHANGETYPE_MODRDN},
265         {"moddn",  LDB_CHANGETYPE_MODRDN},
266         {NULL, 0}
267 };
268
269 /* this macro is used to handle the return checking on fprintf_fn() */
270 #define CHECK_RET do { if (ret < 0) { talloc_free(mem_ctx); return ret; } total += ret; } while (0)
271
272 /*
273   write to ldif, using a caller supplied write method
274 */
275 int ldb_ldif_write(struct ldb_context *ldb,
276                    int (*fprintf_fn)(void *, const char *, ...), 
277                    void *private_data,
278                    const struct ldb_ldif *ldif)
279 {
280         TALLOC_CTX *mem_ctx;
281         unsigned int i, j;
282         int total=0, ret;
283         char *p;
284         const struct ldb_message *msg;
285
286         mem_ctx = talloc_named_const(NULL, 0, "ldb_ldif_write");
287
288         msg = ldif->msg;
289         p = ldb_dn_get_extended_linearized(mem_ctx, msg->dn, 1);
290         ret = fprintf_fn(private_data, "dn: %s\n", p);
291         talloc_free(p);
292         CHECK_RET;
293
294         if (ldif->changetype != LDB_CHANGETYPE_NONE) {
295                 for (i=0;ldb_changetypes[i].name;i++) {
296                         if (ldb_changetypes[i].changetype == ldif->changetype) {
297                                 break;
298                         }
299                 }
300                 if (!ldb_changetypes[i].name) {
301                         ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d",
302                                   ldif->changetype);
303                         talloc_free(mem_ctx);
304                         return -1;
305                 }
306                 ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
307                 CHECK_RET;
308         }
309
310         for (i=0;i<msg->num_elements;i++) {
311                 const struct ldb_schema_attribute *a;
312
313                 a = ldb_schema_attribute_by_name(ldb, msg->elements[i].name);
314
315                 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
316                         switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
317                         case LDB_FLAG_MOD_ADD:
318                                 fprintf_fn(private_data, "add: %s\n", 
319                                            msg->elements[i].name);
320                                 break;
321                         case LDB_FLAG_MOD_DELETE:
322                                 fprintf_fn(private_data, "delete: %s\n", 
323                                            msg->elements[i].name);
324                                 break;
325                         case LDB_FLAG_MOD_REPLACE:
326                                 fprintf_fn(private_data, "replace: %s\n", 
327                                            msg->elements[i].name);
328                                 break;
329                         }
330                 }
331
332                 for (j=0;j<msg->elements[i].num_values;j++) {
333                         struct ldb_val v;
334                         bool use_b64_encode;
335                         ret = a->syntax->ldif_write_fn(ldb, mem_ctx, &msg->elements[i].values[j], &v);
336                         if (ret != LDB_SUCCESS) {
337                                 v = msg->elements[i].values[j];
338                         }
339                         use_b64_encode = !(ldb->flags & LDB_FLG_SHOW_BINARY)
340                                         && ldb_should_b64_encode(ldb, &v);
341                         if (ret != LDB_SUCCESS || use_b64_encode) {
342                                 ret = fprintf_fn(private_data, "%s:: ", 
343                                                  msg->elements[i].name);
344                                 CHECK_RET;
345                                 ret = base64_encode_f(ldb, fprintf_fn, private_data, 
346                                                       (char *)v.data, v.length,
347                                                       strlen(msg->elements[i].name)+3);
348                                 CHECK_RET;
349                                 ret = fprintf_fn(private_data, "\n");
350                                 CHECK_RET;
351                         } else {
352                                 ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
353                                 CHECK_RET;
354                                 if (ldb->flags & LDB_FLG_SHOW_BINARY) {
355                                         ret = fprintf_fn(private_data, "%*.*s", 
356                                                          v.length, v.length, (char *)v.data);
357                                 } else {
358                                         ret = fold_string(fprintf_fn, private_data,
359                                                           (char *)v.data, v.length,
360                                                           strlen(msg->elements[i].name)+2);
361                                 }
362                                 CHECK_RET;
363                                 ret = fprintf_fn(private_data, "\n");
364                                 CHECK_RET;
365                         }
366                         if (v.data != msg->elements[i].values[j].data) {
367                                 talloc_free(v.data);
368                         }
369                 }
370                 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
371                         fprintf_fn(private_data, "-\n");
372                 }
373         }
374         ret = fprintf_fn(private_data,"\n");
375         CHECK_RET;
376
377         talloc_free(mem_ctx);
378
379         return total;
380 }
381
382 #undef CHECK_RET
383
384
385 /*
386   pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
387   this routine removes any RFC2849 continuations and comments
388
389   caller frees
390 */
391 static char *next_chunk(struct ldb_context *ldb, 
392                         int (*fgetc_fn)(void *), void *private_data)
393 {
394         size_t alloc_size=0, chunk_size = 0;
395         char *chunk = NULL;
396         int c;
397         int in_comment = 0;
398
399         while ((c = fgetc_fn(private_data)) != EOF) {
400                 if (chunk_size+1 >= alloc_size) {
401                         char *c2;
402                         alloc_size += 1024;
403                         c2 = talloc_realloc(ldb, chunk, char, alloc_size);
404                         if (!c2) {
405                                 talloc_free(chunk);
406                                 errno = ENOMEM;
407                                 return NULL;
408                         }
409                         chunk = c2;
410                 }
411
412                 if (in_comment) {
413                         if (c == '\n') {
414                                 in_comment = 0;
415                         }
416                         continue;                       
417                 }
418                 
419                 /* handle continuation lines - see RFC2849 */
420                 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
421                         chunk_size--;
422                         continue;
423                 }
424                 
425                 /* chunks are terminated by a double line-feed */
426                 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
427                         chunk[chunk_size-1] = 0;
428                         return chunk;
429                 }
430
431                 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
432                         in_comment = 1;
433                         continue;
434                 }
435
436                 /* ignore leading blank lines */
437                 if (chunk_size == 0 && c == '\n') {
438                         continue;
439                 }
440
441                 chunk[chunk_size++] = c;
442         }
443
444         if (chunk) {
445                 chunk[chunk_size] = 0;
446         }
447
448         return chunk;
449 }
450
451
452 /* simple ldif attribute parser */
453 static int next_attr(TALLOC_CTX *mem_ctx, char **s, const char **attr, struct ldb_val *value)
454 {
455         char *p;
456         int base64_encoded = 0;
457         int binary_file = 0;
458
459         if (strncmp(*s, "-\n", 2) == 0) {
460                 value->length = 0;
461                 *attr = "-";
462                 *s += 2;
463                 return 0;
464         }
465
466         p = strchr(*s, ':');
467         if (!p) {
468                 return -1;
469         }
470
471         *p++ = 0;
472
473         if (*p == ':') {
474                 base64_encoded = 1;
475                 p++;
476         }
477
478         if (*p == '<') {
479                 binary_file = 1;
480                 p++;
481         }
482
483         *attr = *s;
484
485         while (*p == ' ' || *p == '\t') {
486                 p++;
487         }
488
489         value->data = (uint8_t *)p;
490
491         p = strchr(p, '\n');
492
493         if (!p) {
494                 value->length = strlen((char *)value->data);
495                 *s = ((char *)value->data) + value->length;
496         } else {
497                 value->length = p - (char *)value->data;
498                 *s = p+1;
499                 *p = 0;
500         }
501
502         if (base64_encoded) {
503                 int len = ldb_base64_decode((char *)value->data);
504                 if (len == -1) {
505                         /* it wasn't valid base64 data */
506                         return -1;
507                 }
508                 value->length = len;
509         }
510
511         if (binary_file) {
512                 int len = ldb_read_data_file(mem_ctx, value);
513                 if (len == -1) {
514                         /* an error occurred while trying to retrieve the file */
515                         return -1;
516                 }
517         }
518
519         return 0;
520 }
521
522
523 /*
524   free a message from a ldif_read
525 */
526 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
527 {
528         talloc_free(ldif);
529 }
530
531 int ldb_ldif_parse_modrdn(struct ldb_context *ldb,
532                           const struct ldb_ldif *ldif,
533                           TALLOC_CTX *mem_ctx,
534                           struct ldb_dn **_olddn,
535                           struct ldb_dn **_newrdn,
536                           bool *_deleteoldrdn,
537                           struct ldb_dn **_newsuperior,
538                           struct ldb_dn **_newdn)
539 {
540         struct ldb_message *msg = ldif->msg;
541         struct ldb_val *newrdn_val = NULL;
542         struct ldb_val *deleteoldrdn_val = NULL;
543         struct ldb_val *newsuperior_val = NULL;
544         struct ldb_dn *olddn = NULL;
545         struct ldb_dn *newrdn = NULL;
546         bool deleteoldrdn = true;
547         struct ldb_dn *newsuperior = NULL;
548         struct ldb_dn *newdn = NULL;
549         struct ldb_val tmp_false;
550         struct ldb_val tmp_true;
551         bool ok;
552         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
553
554         if (tmp_ctx == NULL) {
555                 ldb_debug(ldb, LDB_DEBUG_FATAL,
556                           "Error: talloc_new() failed");
557                 goto err_op;
558         }
559
560         if (ldif->changetype != LDB_CHANGETYPE_MODRDN) {
561                 ldb_debug(ldb, LDB_DEBUG_ERROR,
562                           "Error: invalid changetype '%d'",
563                           ldif->changetype);
564                 goto err_other;
565         }
566
567         if (msg->num_elements < 2) {
568                 ldb_debug(ldb, LDB_DEBUG_ERROR,
569                           "Error: num_elements[%u] < 2",
570                           msg->num_elements);
571                 goto err_other;
572         }
573
574         if (msg->num_elements > 3) {
575                 ldb_debug(ldb, LDB_DEBUG_ERROR,
576                           "Error: num_elements[%u] > 3",
577                           msg->num_elements);
578                 goto err_other;
579         }
580
581 #define CHECK_ELEMENT(i, _name, v, needed) do { \
582         v = NULL; \
583         if (msg->num_elements < (i + 1)) { \
584                 if (needed) { \
585                         ldb_debug(ldb, LDB_DEBUG_ERROR, \
586                                   "Error: num_elements[%u] < (%u + 1)", \
587                                   msg->num_elements, i); \
588                         goto err_other; \
589                 } \
590         } else if (ldb_attr_cmp(msg->elements[i].name, _name) != 0) { \
591                 ldb_debug(ldb, LDB_DEBUG_ERROR, \
592                           "Error: elements[%u].name[%s] != [%s]", \
593                           i, msg->elements[i].name, _name); \
594                 goto err_other; \
595         } else if (msg->elements[i].flags != 0) { \
596                 ldb_debug(ldb, LDB_DEBUG_ERROR, \
597                           "Error: elements[%u].flags[0x%X} != [0x0]", \
598                           i, msg->elements[i].flags); \
599                 goto err_other; \
600         } else if (msg->elements[i].num_values != 1) { \
601                 ldb_debug(ldb, LDB_DEBUG_ERROR, \
602                           "Error: elements[%u].num_values[%u] != 1", \
603                           i, msg->elements[i].num_values); \
604                 goto err_other; \
605         } else { \
606                 v = &msg->elements[i].values[0]; \
607         } \
608 } while (0)
609
610         CHECK_ELEMENT(0, "newrdn", newrdn_val, true);
611         CHECK_ELEMENT(1, "deleteoldrdn", deleteoldrdn_val, true);
612         CHECK_ELEMENT(2, "newsuperior", newsuperior_val, false);
613
614 #undef CHECK_ELEMENT
615
616         olddn = ldb_dn_copy(tmp_ctx, msg->dn);
617         if (olddn == NULL) {
618                 ldb_debug(ldb, LDB_DEBUG_ERROR,
619                           "Error: failed to copy olddn '%s'",
620                           ldb_dn_get_linearized(msg->dn));
621                 goto err_op;
622         }
623
624         newrdn = ldb_dn_from_ldb_val(tmp_ctx, ldb, newrdn_val);
625         if (!ldb_dn_validate(newrdn)) {
626                 ldb_debug(ldb, LDB_DEBUG_ERROR,
627                           "Error: Unable to parse dn '%s'",
628                           (char *)newrdn_val->data);
629                 goto err_dn;
630         }
631
632         tmp_false.length = 1;
633         tmp_false.data = discard_const_p(uint8_t, "0");
634         tmp_true.length = 1;
635         tmp_true.data = discard_const_p(uint8_t, "1");
636         if (ldb_val_equal_exact(deleteoldrdn_val, &tmp_false) == 1) {
637                 deleteoldrdn = false;
638         } else if (ldb_val_equal_exact(deleteoldrdn_val, &tmp_true) == 1) {
639                 deleteoldrdn = true;
640         } else {
641                 ldb_debug(ldb, LDB_DEBUG_ERROR,
642                           "Error: deleteoldrdn value invalid '%s' not '0'/'1'",
643                           (char *)deleteoldrdn_val->data);
644                 goto err_attr;
645         }
646
647         if (newsuperior_val) {
648                 newsuperior = ldb_dn_from_ldb_val(tmp_ctx, ldb, newsuperior_val);
649                 if (!ldb_dn_validate(newsuperior)) {
650                         ldb_debug(ldb, LDB_DEBUG_ERROR,
651                                   "Error: Unable to parse dn '%s'",
652                                   (char *)newsuperior_val->data);
653                         goto err_dn;
654                 }
655         } else {
656                 newsuperior = ldb_dn_get_parent(tmp_ctx, msg->dn);
657                 if (newsuperior == NULL) {
658                         ldb_debug(ldb, LDB_DEBUG_ERROR,
659                                   "Error: Unable to get parent dn '%s'",
660                                   ldb_dn_get_linearized(msg->dn));
661                         goto err_dn;
662                 }
663         }
664
665         newdn = ldb_dn_copy(tmp_ctx, newrdn);
666         if (newdn == NULL) {
667                 ldb_debug(ldb, LDB_DEBUG_ERROR,
668                           "Error: failed to copy newrdn '%s'",
669                           ldb_dn_get_linearized(newrdn));
670                 goto err_op;
671         }
672
673         ok = ldb_dn_add_base(newdn, newsuperior);
674         if (!ok) {
675                 ldb_debug(ldb, LDB_DEBUG_ERROR,
676                           "Error: failed to base '%s' to newdn '%s'",
677                           ldb_dn_get_linearized(newsuperior),
678                           ldb_dn_get_linearized(newdn));
679                 goto err_op;
680         }
681
682         if (_olddn) {
683                 *_olddn = talloc_move(mem_ctx, &olddn);
684         }
685         if (_newrdn) {
686                 *_newrdn = talloc_move(mem_ctx, &newrdn);
687         }
688         if (_deleteoldrdn) {
689                 *_deleteoldrdn = deleteoldrdn;
690         }
691         if (_newsuperior) {
692                 if (newsuperior_val) {
693                         *_newrdn = talloc_move(mem_ctx, &newrdn);
694                 } else {
695                         *_newrdn = NULL;
696                 }
697         }
698         if (_newdn) {
699                 *_newdn = talloc_move(mem_ctx, &newdn);
700         }
701
702         talloc_free(tmp_ctx);
703         return LDB_SUCCESS;
704 err_other:
705         talloc_free(tmp_ctx);
706         return LDB_ERR_OTHER;
707 err_op:
708         talloc_free(tmp_ctx);
709         return LDB_ERR_OPERATIONS_ERROR;
710 err_attr:
711         talloc_free(tmp_ctx);
712         return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
713 err_dn:
714         talloc_free(tmp_ctx);
715         return LDB_ERR_INVALID_DN_SYNTAX;
716 }
717
718 /*
719  read from a LDIF source, creating a ldb_message
720 */
721 struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
722                                int (*fgetc_fn)(void *), void *private_data)
723 {
724         struct ldb_ldif *ldif;
725         struct ldb_message *msg;
726         const char *attr=NULL;
727         char *chunk=NULL, *s;
728         struct ldb_val value;
729         unsigned flags = 0;
730
731         value.data = NULL;
732
733         ldif = talloc(ldb, struct ldb_ldif);
734         if (!ldif) return NULL;
735
736         ldif->msg = talloc(ldif, struct ldb_message);
737         if (ldif->msg == NULL) {
738                 talloc_free(ldif);
739                 return NULL;
740         }
741
742         ldif->changetype = LDB_CHANGETYPE_NONE;
743         msg = ldif->msg;
744
745         msg->dn = NULL;
746         msg->elements = NULL;
747         msg->num_elements = 0;
748
749         chunk = next_chunk(ldb, fgetc_fn, private_data);
750         if (!chunk) {
751                 goto failed;
752         }
753         talloc_steal(ldif, chunk);
754
755         s = chunk;
756
757         if (next_attr(ldif, &s, &attr, &value) != 0) {
758                 goto failed;
759         }
760         
761         /* first line must be a dn */
762         if (ldb_attr_cmp(attr, "dn") != 0) {
763                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'",
764                           attr);
765                 goto failed;
766         }
767
768         msg->dn = ldb_dn_from_ldb_val(msg, ldb, &value);
769
770         if ( ! ldb_dn_validate(msg->dn)) {
771                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Unable to parse dn '%s'",
772                           (char *)value.data);
773                 goto failed;
774         }
775
776         while (next_attr(ldif, &s, &attr, &value) == 0) {
777                 const struct ldb_schema_attribute *a;
778                 struct ldb_message_element *el;
779                 int ret, empty = 0;
780
781                 if (ldb_attr_cmp(attr, "changetype") == 0) {
782                         int i;
783                         for (i=0;ldb_changetypes[i].name;i++) {
784                                 if (ldb_attr_cmp((char *)value.data, ldb_changetypes[i].name) == 0) {
785                                         ldif->changetype = ldb_changetypes[i].changetype;
786                                         break;
787                                 }
788                         }
789                         if (!ldb_changetypes[i].name) {
790                                 ldb_debug(ldb, LDB_DEBUG_ERROR,
791                                           "Error: Bad ldif changetype '%s'",(char *)value.data);
792                         }
793                         flags = 0;
794                         continue;
795                 }
796
797                 if (ldb_attr_cmp(attr, "add") == 0) {
798                         flags = LDB_FLAG_MOD_ADD;
799                         empty = 1;
800                 }
801                 if (ldb_attr_cmp(attr, "delete") == 0) {
802                         flags = LDB_FLAG_MOD_DELETE;
803                         empty = 1;
804                 }
805                 if (ldb_attr_cmp(attr, "replace") == 0) {
806                         flags = LDB_FLAG_MOD_REPLACE;
807                         empty = 1;
808                 }
809                 if (ldb_attr_cmp(attr, "-") == 0) {
810                         flags = 0;
811                         continue;
812                 }
813
814                 if (empty) {
815                         if (ldb_msg_add_empty(msg, (char *)value.data, flags, NULL) != 0) {
816                                 goto failed;
817                         }
818                         continue;
819                 }
820                 
821                 el = &msg->elements[msg->num_elements-1];
822
823                 a = ldb_schema_attribute_by_name(ldb, attr);
824
825                 if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
826                     flags == el->flags) {
827                         /* its a continuation */
828                         el->values = 
829                                 talloc_realloc(msg->elements, el->values, 
830                                                  struct ldb_val, el->num_values+1);
831                         if (!el->values) {
832                                 goto failed;
833                         }
834                         ret = a->syntax->ldif_read_fn(ldb, el->values, &value, &el->values[el->num_values]);
835                         if (ret != 0) {
836                                 goto failed;
837                         }
838                         if (value.length == 0) {
839                                 ldb_debug(ldb, LDB_DEBUG_ERROR,
840                                           "Error: Attribute value cannot be empty for attribute '%s'", el->name);
841                                 goto failed;
842                         }
843                         if (value.data != el->values[el->num_values].data) {
844                                 talloc_steal(el->values, el->values[el->num_values].data);
845                         }
846                         el->num_values++;
847                 } else {
848                         /* its a new attribute */
849                         msg->elements = talloc_realloc(msg, msg->elements, 
850                                                          struct ldb_message_element, 
851                                                          msg->num_elements+1);
852                         if (!msg->elements) {
853                                 goto failed;
854                         }
855                         el = &msg->elements[msg->num_elements];
856                         el->flags = flags;
857                         el->name = talloc_strdup(msg->elements, attr);
858                         el->values = talloc(msg->elements, struct ldb_val);
859                         if (!el->values || !el->name) {
860                                 goto failed;
861                         }
862                         el->num_values = 1;
863                         ret = a->syntax->ldif_read_fn(ldb, el->values, &value, &el->values[0]);
864                         if (ret != 0) {
865                                 goto failed;
866                         }
867                         if (value.data != el->values[0].data) {
868                                 talloc_steal(el->values, el->values[0].data);
869                         }
870                         msg->num_elements++;
871                 }
872         }
873
874         if (ldif->changetype == LDB_CHANGETYPE_MODRDN) {
875                 int ret;
876
877                 ret = ldb_ldif_parse_modrdn(ldb, ldif, ldif,
878                                             NULL, NULL, NULL, NULL, NULL);
879                 if (ret != LDB_SUCCESS) {
880                         goto failed;
881                 }
882         }
883
884         return ldif;
885
886 failed:
887         talloc_free(ldif);
888         return NULL;
889 }
890
891
892
893 /*
894   a wrapper around ldif_read() for reading from FILE*
895 */
896 struct ldif_read_file_state {
897         FILE *f;
898 };
899
900 static int fgetc_file(void *private_data)
901 {
902         struct ldif_read_file_state *state =
903                 (struct ldif_read_file_state *)private_data;
904         return fgetc(state->f);
905 }
906
907 struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f)
908 {
909         struct ldif_read_file_state state;
910         state.f = f;
911         return ldb_ldif_read(ldb, fgetc_file, &state);
912 }
913
914
915 /*
916   a wrapper around ldif_read() for reading from const char*
917 */
918 struct ldif_read_string_state {
919         const char *s;
920 };
921
922 static int fgetc_string(void *private_data)
923 {
924         struct ldif_read_string_state *state =
925                 (struct ldif_read_string_state *)private_data;
926         if (state->s[0] != 0) {
927                 return *state->s++;
928         }
929         return EOF;
930 }
931
932 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char **s)
933 {
934         struct ldif_read_string_state state;
935         struct ldb_ldif *ldif;
936         state.s = *s;
937         ldif = ldb_ldif_read(ldb, fgetc_string, &state);
938         *s = state.s;
939         return ldif;
940 }
941
942
943 /*
944   wrapper around ldif_write() for a file
945 */
946 struct ldif_write_file_state {
947         FILE *f;
948 };
949
950 static int fprintf_file(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
951
952 static int fprintf_file(void *private_data, const char *fmt, ...)
953 {
954         struct ldif_write_file_state *state =
955                 (struct ldif_write_file_state *)private_data;
956         int ret;
957         va_list ap;
958
959         va_start(ap, fmt);
960         ret = vfprintf(state->f, fmt, ap);
961         va_end(ap);
962         return ret;
963 }
964
965 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
966 {
967         struct ldif_write_file_state state;
968         state.f = f;
969         return ldb_ldif_write(ldb, fprintf_file, &state, ldif);
970 }
971
972 /*
973   wrapper around ldif_write() for a string
974 */
975 struct ldif_write_string_state {
976         char *string;
977 };
978
979 static int ldif_printf_string(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
980
981 static int ldif_printf_string(void *private_data, const char *fmt, ...)
982 {
983         struct ldif_write_string_state *state =
984                 (struct ldif_write_string_state *)private_data;
985         va_list ap;
986         size_t oldlen = talloc_get_size(state->string);
987         va_start(ap, fmt);
988         
989         state->string = talloc_vasprintf_append(state->string, fmt, ap);
990         va_end(ap);
991         if (!state->string) {
992                 return -1;
993         }
994
995         return talloc_get_size(state->string) - oldlen;
996 }
997
998 char *ldb_ldif_write_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, 
999                             const struct ldb_ldif *ldif)
1000 {
1001         struct ldif_write_string_state state;
1002         state.string = talloc_strdup(mem_ctx, "");
1003         if (!state.string) {
1004                 return NULL;
1005         }
1006         if (ldb_ldif_write(ldb, ldif_printf_string, &state, ldif) == -1) {
1007                 return NULL;
1008         }
1009         return state.string;
1010 }
1011
1012 /*
1013   convenient function to turn a ldb_message into a string. Useful for
1014   debugging
1015  */
1016 char *ldb_ldif_message_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, 
1017                               enum ldb_changetype changetype,
1018                               const struct ldb_message *msg)
1019 {
1020         struct ldb_ldif ldif;
1021
1022         ldif.changetype = changetype;
1023         ldif.msg = discard_const_p(struct ldb_message, msg);
1024
1025         return ldb_ldif_write_string(ldb, mem_ctx, &ldif);
1026 }