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