s4:ldb always talloc_free() the ldb_ldif_write context, even on success
[tridge/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(void *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(void *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 (ldb->flags & LDB_FLG_SHOW_BINARY) {
194                 return 0;
195         }
196
197         if (val->length == 0) {
198                 return 0;
199         }
200
201         if (p[0] == ' ' || p[0] == ':') {
202                 return 1;
203         }
204
205         for (i=0; i<val->length; i++) {
206                 if (!isprint(p[i]) || p[i] == '\n') {
207                         return 1;
208                 }
209         }
210         return 0;
211 }
212
213 /* this macro is used to handle the return checking on fprintf_fn() */
214 #define CHECK_RET do { if (ret < 0) return ret; total += ret; } while (0)
215
216 /*
217   write a line folded string onto a file
218 */
219 static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *private_data,
220                         const char *buf, size_t length, int start_pos)
221 {
222         unsigned int i;
223         int total=0, ret;
224
225         for (i=0;i<length;i++) {
226                 ret = fprintf_fn(private_data, "%c", buf[i]);
227                 CHECK_RET;
228                 if (i != (length-1) && (i + start_pos) % 77 == 0) {
229                         ret = fprintf_fn(private_data, "\n ");
230                         CHECK_RET;
231                 }
232         }
233
234         return total;
235 }
236
237 #undef CHECK_RET
238
239 /*
240   encode as base64 to a file
241 */
242 static int base64_encode_f(struct ldb_context *ldb,
243                            int (*fprintf_fn)(void *, const char *, ...), 
244                            void *private_data,
245                            const char *buf, int len, int start_pos)
246 {
247         char *b = ldb_base64_encode(ldb, buf, len);
248         int ret;
249
250         if (!b) {
251                 return -1;
252         }
253
254         ret = fold_string(fprintf_fn, private_data, b, strlen(b), start_pos);
255
256         talloc_free(b);
257         return ret;
258 }
259
260
261 static const struct {
262         const char *name;
263         enum ldb_changetype changetype;
264 } ldb_changetypes[] = {
265         {"add",    LDB_CHANGETYPE_ADD},
266         {"delete", LDB_CHANGETYPE_DELETE},
267         {"modify", LDB_CHANGETYPE_MODIFY},
268         {NULL, 0}
269 };
270
271 /* this macro is used to handle the return checking on fprintf_fn() */
272 #define CHECK_RET do { if (ret < 0) { talloc_free(mem_ctx); return ret; } total += ret; } while (0)
273
274 /*
275   write to ldif, using a caller supplied write method
276 */
277 int ldb_ldif_write(struct ldb_context *ldb,
278                    int (*fprintf_fn)(void *, const char *, ...), 
279                    void *private_data,
280                    const struct ldb_ldif *ldif)
281 {
282         TALLOC_CTX *mem_ctx;
283         unsigned int i, j;
284         int total=0, ret;
285         char *p;
286         const struct ldb_message *msg;
287
288         mem_ctx = talloc_named_const(NULL, 0, "ldb_ldif_write");
289
290         msg = ldif->msg;
291         p = ldb_dn_get_extended_linearized(mem_ctx, msg->dn, 1);
292         ret = fprintf_fn(private_data, "dn: %s\n", p);
293         talloc_free(p);
294         CHECK_RET;
295
296         if (ldif->changetype != LDB_CHANGETYPE_NONE) {
297                 for (i=0;ldb_changetypes[i].name;i++) {
298                         if (ldb_changetypes[i].changetype == ldif->changetype) {
299                                 break;
300                         }
301                 }
302                 if (!ldb_changetypes[i].name) {
303                         ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d",
304                                   ldif->changetype);
305                         talloc_free(mem_ctx);
306                         return -1;
307                 }
308                 ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
309                 CHECK_RET;
310         }
311
312         for (i=0;i<msg->num_elements;i++) {
313                 const struct ldb_schema_attribute *a;
314
315                 a = ldb_schema_attribute_by_name(ldb, msg->elements[i].name);
316
317                 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
318                         switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
319                         case LDB_FLAG_MOD_ADD:
320                                 fprintf_fn(private_data, "add: %s\n", 
321                                            msg->elements[i].name);
322                                 break;
323                         case LDB_FLAG_MOD_DELETE:
324                                 fprintf_fn(private_data, "delete: %s\n", 
325                                            msg->elements[i].name);
326                                 break;
327                         case LDB_FLAG_MOD_REPLACE:
328                                 fprintf_fn(private_data, "replace: %s\n", 
329                                            msg->elements[i].name);
330                                 break;
331                         }
332                 }
333
334                 for (j=0;j<msg->elements[i].num_values;j++) {
335                         struct ldb_val v;
336                         ret = a->syntax->ldif_write_fn(ldb, mem_ctx, &msg->elements[i].values[j], &v);
337                         if (ret != LDB_SUCCESS) {
338                                 v = msg->elements[i].values[j];
339                         }
340                         if (ret != LDB_SUCCESS || ldb_should_b64_encode(ldb, &v)) {
341                                 ret = fprintf_fn(private_data, "%s:: ", 
342                                                  msg->elements[i].name);
343                                 CHECK_RET;
344                                 ret = base64_encode_f(ldb, fprintf_fn, private_data, 
345                                                       (char *)v.data, v.length,
346                                                       strlen(msg->elements[i].name)+3);
347                                 CHECK_RET;
348                                 ret = fprintf_fn(private_data, "\n");
349                                 CHECK_RET;
350                         } else {
351                                 ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
352                                 CHECK_RET;
353                                 if (ldb->flags & LDB_FLG_SHOW_BINARY) {
354                                         ret = fprintf_fn(private_data, "%*.*s", 
355                                                          v.length, v.length, (char *)v.data);
356                                 } else {
357                                         ret = fold_string(fprintf_fn, private_data,
358                                                           (char *)v.data, v.length,
359                                                           strlen(msg->elements[i].name)+2);
360                                 }
361                                 CHECK_RET;
362                                 ret = fprintf_fn(private_data, "\n");
363                                 CHECK_RET;
364                         }
365                         if (v.data != msg->elements[i].values[j].data) {
366                                 talloc_free(v.data);
367                         }
368                 }
369                 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
370                         fprintf_fn(private_data, "-\n");
371                 }
372         }
373         ret = fprintf_fn(private_data,"\n");
374         CHECK_RET;
375
376         talloc_free(mem_ctx);
377         return total;
378 }
379
380 #undef CHECK_RET
381
382
383 /*
384   pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
385   this routine removes any RFC2849 continuations and comments
386
387   caller frees
388 */
389 static char *next_chunk(struct ldb_context *ldb, 
390                         int (*fgetc_fn)(void *), void *private_data)
391 {
392         size_t alloc_size=0, chunk_size = 0;
393         char *chunk = NULL;
394         int c;
395         int in_comment = 0;
396
397         while ((c = fgetc_fn(private_data)) != EOF) {
398                 if (chunk_size+1 >= alloc_size) {
399                         char *c2;
400                         alloc_size += 1024;
401                         c2 = talloc_realloc(ldb, chunk, char, alloc_size);
402                         if (!c2) {
403                                 talloc_free(chunk);
404                                 errno = ENOMEM;
405                                 return NULL;
406                         }
407                         chunk = c2;
408                 }
409
410                 if (in_comment) {
411                         if (c == '\n') {
412                                 in_comment = 0;
413                         }
414                         continue;                       
415                 }
416                 
417                 /* handle continuation lines - see RFC2849 */
418                 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
419                         chunk_size--;
420                         continue;
421                 }
422                 
423                 /* chunks are terminated by a double line-feed */
424                 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
425                         chunk[chunk_size-1] = 0;
426                         return chunk;
427                 }
428
429                 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
430                         in_comment = 1;
431                         continue;
432                 }
433
434                 /* ignore leading blank lines */
435                 if (chunk_size == 0 && c == '\n') {
436                         continue;
437                 }
438
439                 chunk[chunk_size++] = c;
440         }
441
442         if (chunk) {
443                 chunk[chunk_size] = 0;
444         }
445
446         return chunk;
447 }
448
449
450 /* simple ldif attribute parser */
451 static int next_attr(void *mem_ctx, char **s, const char **attr, struct ldb_val *value)
452 {
453         char *p;
454         int base64_encoded = 0;
455         int binary_file = 0;
456
457         if (strncmp(*s, "-\n", 2) == 0) {
458                 value->length = 0;
459                 *attr = "-";
460                 *s += 2;
461                 return 0;
462         }
463
464         p = strchr(*s, ':');
465         if (!p) {
466                 return -1;
467         }
468
469         *p++ = 0;
470
471         if (*p == ':') {
472                 base64_encoded = 1;
473                 p++;
474         }
475
476         if (*p == '<') {
477                 binary_file = 1;
478                 p++;
479         }
480
481         *attr = *s;
482
483         while (*p == ' ' || *p == '\t') {
484                 p++;
485         }
486
487         value->data = (uint8_t *)p;
488
489         p = strchr(p, '\n');
490
491         if (!p) {
492                 value->length = strlen((char *)value->data);
493                 *s = ((char *)value->data) + value->length;
494         } else {
495                 value->length = p - (char *)value->data;
496                 *s = p+1;
497                 *p = 0;
498         }
499
500         if (base64_encoded) {
501                 int len = ldb_base64_decode((char *)value->data);
502                 if (len == -1) {
503                         /* it wasn't valid base64 data */
504                         return -1;
505                 }
506                 value->length = len;
507         }
508
509         if (binary_file) {
510                 int len = ldb_read_data_file(mem_ctx, value);
511                 if (len == -1) {
512                         /* an error occured hile trying to retrieve the file */
513                         return -1;
514                 }
515         }
516
517         return 0;
518 }
519
520
521 /*
522   free a message from a ldif_read
523 */
524 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
525 {
526         talloc_free(ldif);
527 }
528
529 /*
530  read from a LDIF source, creating a ldb_message
531 */
532 struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
533                                int (*fgetc_fn)(void *), void *private_data)
534 {
535         struct ldb_ldif *ldif;
536         struct ldb_message *msg;
537         const char *attr=NULL;
538         char *chunk=NULL, *s;
539         struct ldb_val value;
540         unsigned flags = 0;
541
542         value.data = NULL;
543
544         ldif = talloc(ldb, struct ldb_ldif);
545         if (!ldif) return NULL;
546
547         ldif->msg = talloc(ldif, struct ldb_message);
548         if (ldif->msg == NULL) {
549                 talloc_free(ldif);
550                 return NULL;
551         }
552
553         ldif->changetype = LDB_CHANGETYPE_NONE;
554         msg = ldif->msg;
555
556         msg->dn = NULL;
557         msg->elements = NULL;
558         msg->num_elements = 0;
559
560         chunk = next_chunk(ldb, fgetc_fn, private_data);
561         if (!chunk) {
562                 goto failed;
563         }
564         talloc_steal(ldif, chunk);
565
566         s = chunk;
567
568         if (next_attr(ldif, &s, &attr, &value) != 0) {
569                 goto failed;
570         }
571         
572         /* first line must be a dn */
573         if (ldb_attr_cmp(attr, "dn") != 0) {
574                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'",
575                           attr);
576                 goto failed;
577         }
578
579         msg->dn = ldb_dn_from_ldb_val(msg, ldb, &value);
580
581         if ( ! ldb_dn_validate(msg->dn)) {
582                 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Unable to parse dn '%s'",
583                           (char *)value.data);
584                 goto failed;
585         }
586
587         while (next_attr(ldif, &s, &attr, &value) == 0) {
588                 const struct ldb_schema_attribute *a;
589                 struct ldb_message_element *el;
590                 int ret, empty = 0;
591
592                 if (ldb_attr_cmp(attr, "changetype") == 0) {
593                         int i;
594                         for (i=0;ldb_changetypes[i].name;i++) {
595                                 if (ldb_attr_cmp((char *)value.data, ldb_changetypes[i].name) == 0) {
596                                         ldif->changetype = ldb_changetypes[i].changetype;
597                                         break;
598                                 }
599                         }
600                         if (!ldb_changetypes[i].name) {
601                                 ldb_debug(ldb, LDB_DEBUG_ERROR,
602                                           "Error: Bad ldif changetype '%s'",(char *)value.data);
603                         }
604                         flags = 0;
605                         continue;
606                 }
607
608                 if (ldb_attr_cmp(attr, "add") == 0) {
609                         flags = LDB_FLAG_MOD_ADD;
610                         empty = 1;
611                 }
612                 if (ldb_attr_cmp(attr, "delete") == 0) {
613                         flags = LDB_FLAG_MOD_DELETE;
614                         empty = 1;
615                 }
616                 if (ldb_attr_cmp(attr, "replace") == 0) {
617                         flags = LDB_FLAG_MOD_REPLACE;
618                         empty = 1;
619                 }
620                 if (ldb_attr_cmp(attr, "-") == 0) {
621                         flags = 0;
622                         continue;
623                 }
624
625                 if (empty) {
626                         if (ldb_msg_add_empty(msg, (char *)value.data, flags, NULL) != 0) {
627                                 goto failed;
628                         }
629                         continue;
630                 }
631                 
632                 el = &msg->elements[msg->num_elements-1];
633
634                 a = ldb_schema_attribute_by_name(ldb, attr);
635
636                 if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
637                     flags == el->flags) {
638                         /* its a continuation */
639                         el->values = 
640                                 talloc_realloc(msg->elements, el->values, 
641                                                  struct ldb_val, el->num_values+1);
642                         if (!el->values) {
643                                 goto failed;
644                         }
645                         ret = a->syntax->ldif_read_fn(ldb, el->values, &value, &el->values[el->num_values]);
646                         if (ret != 0) {
647                                 goto failed;
648                         }
649                         if (value.length == 0) {
650                                 ldb_debug(ldb, LDB_DEBUG_ERROR,
651                                           "Error: Attribute value cannot be empty for attribute '%s'", el->name);
652                                 goto failed;
653                         }
654                         if (value.data != el->values[el->num_values].data) {
655                                 talloc_steal(el->values, el->values[el->num_values].data);
656                         }
657                         el->num_values++;
658                 } else {
659                         /* its a new attribute */
660                         msg->elements = talloc_realloc(msg, msg->elements, 
661                                                          struct ldb_message_element, 
662                                                          msg->num_elements+1);
663                         if (!msg->elements) {
664                                 goto failed;
665                         }
666                         el = &msg->elements[msg->num_elements];
667                         el->flags = flags;
668                         el->name = talloc_strdup(msg->elements, attr);
669                         el->values = talloc(msg->elements, struct ldb_val);
670                         if (!el->values || !el->name) {
671                                 goto failed;
672                         }
673                         el->num_values = 1;
674                         ret = a->syntax->ldif_read_fn(ldb, el->values, &value, &el->values[0]);
675                         if (ret != 0) {
676                                 goto failed;
677                         }
678                         if (value.data != el->values[0].data) {
679                                 talloc_steal(el->values, el->values[0].data);
680                         }
681                         msg->num_elements++;
682                 }
683         }
684
685         return ldif;
686
687 failed:
688         talloc_free(ldif);
689         return NULL;
690 }
691
692
693
694 /*
695   a wrapper around ldif_read() for reading from FILE*
696 */
697 struct ldif_read_file_state {
698         FILE *f;
699 };
700
701 static int fgetc_file(void *private_data)
702 {
703         struct ldif_read_file_state *state =
704                 (struct ldif_read_file_state *)private_data;
705         return fgetc(state->f);
706 }
707
708 struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f)
709 {
710         struct ldif_read_file_state state;
711         state.f = f;
712         return ldb_ldif_read(ldb, fgetc_file, &state);
713 }
714
715
716 /*
717   a wrapper around ldif_read() for reading from const char*
718 */
719 struct ldif_read_string_state {
720         const char *s;
721 };
722
723 static int fgetc_string(void *private_data)
724 {
725         struct ldif_read_string_state *state =
726                 (struct ldif_read_string_state *)private_data;
727         if (state->s[0] != 0) {
728                 return *state->s++;
729         }
730         return EOF;
731 }
732
733 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char **s)
734 {
735         struct ldif_read_string_state state;
736         struct ldb_ldif *ldif;
737         state.s = *s;
738         ldif = ldb_ldif_read(ldb, fgetc_string, &state);
739         *s = state.s;
740         return ldif;
741 }
742
743
744 /*
745   wrapper around ldif_write() for a file
746 */
747 struct ldif_write_file_state {
748         FILE *f;
749 };
750
751 static int fprintf_file(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
752
753 static int fprintf_file(void *private_data, const char *fmt, ...)
754 {
755         struct ldif_write_file_state *state =
756                 (struct ldif_write_file_state *)private_data;
757         int ret;
758         va_list ap;
759
760         va_start(ap, fmt);
761         ret = vfprintf(state->f, fmt, ap);
762         va_end(ap);
763         return ret;
764 }
765
766 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
767 {
768         struct ldif_write_file_state state;
769         state.f = f;
770         return ldb_ldif_write(ldb, fprintf_file, &state, ldif);
771 }
772
773 /*
774   wrapper around ldif_write() for a string
775 */
776 struct ldif_write_string_state {
777         char *string;
778 };
779
780 static int ldif_printf_string(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
781
782 static int ldif_printf_string(void *private_data, const char *fmt, ...)
783 {
784         struct ldif_write_string_state *state =
785                 (struct ldif_write_string_state *)private_data;
786         va_list ap;
787         size_t oldlen = talloc_get_size(state->string);
788         va_start(ap, fmt);
789         
790         state->string = talloc_vasprintf_append(state->string, fmt, ap);
791         va_end(ap);
792         if (!state->string) {
793                 return -1;
794         }
795
796         return talloc_get_size(state->string) - oldlen;
797 }
798
799 char *ldb_ldif_write_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, 
800                             const struct ldb_ldif *ldif)
801 {
802         struct ldif_write_string_state state;
803         state.string = talloc_strdup(mem_ctx, "");
804         if (!state.string) {
805                 return NULL;
806         }
807         if (ldb_ldif_write(ldb, ldif_printf_string, &state, ldif) == -1) {
808                 return NULL;
809         }
810         return state.string;
811 }
812
813 /*
814   convenient function to turn a ldb_message into a string. Useful for
815   debugging
816  */
817 char *ldb_ldif_message_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, 
818                               enum ldb_changetype changetype,
819                               const struct ldb_message *msg)
820 {
821         struct ldb_ldif ldif;
822
823         ldif.changetype = changetype;
824         ldif.msg = discard_const_p(struct ldb_message, msg);
825
826         return ldb_ldif_write_string(ldb, mem_ctx, &ldif);
827 }