4 Copyright (C) Andrew Tridgell 2004
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 3 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, see <http://www.gnu.org/licenses/>.
27 * Component: ldif routines
29 * Description: ldif pack/unpack routines
31 * Author: Andrew Tridgell
35 see RFC2849 for the LDIF format definition
38 #include "ldb_private.h"
39 #include "system/locale.h"
44 static int ldb_read_data_file(TALLOC_CTX
*mem_ctx
, struct ldb_val
*value
)
48 int count
, size
, bytes
;
51 const char *fname
= (const char *)value
->data
;
53 if (strncmp(fname
, "file://", 7) != 0) {
54 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
58 f
= open(fname
, O_RDONLY
);
63 if (fstat(f
, &statbuf
) != 0) {
68 if (statbuf
.st_size
== 0) {
73 value
->data
= (uint8_t *)talloc_size(mem_ctx
, statbuf
.st_size
+ 1);
74 if (value
->data
== NULL
) {
78 value
->data
[statbuf
.st_size
] = 0;
81 size
= statbuf
.st_size
;
82 buf
= (char *)value
->data
;
83 while (count
< statbuf
.st_size
) {
84 bytes
= read(f
, buf
, size
);
86 talloc_free(value
->data
);
95 value
->length
= statbuf
.st_size
;
96 ret
= statbuf
.st_size
;
104 this base64 decoder was taken from jitterbug (written by tridge).
105 we might need to replace it with a new version
107 int ldb_base64_decode(char *s
)
109 const char *b64
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
110 int bit_offset
=0, byte_offset
, idx
, i
, n
;
111 uint8_t *d
= (uint8_t *)s
;
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
));
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;
132 if (bit_offset
>= 3) {
137 /* the only termination allowed */
153 char *ldb_base64_encode(TALLOC_CTX
*mem_ctx
, const char *buf
, int len
)
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;
161 out
= talloc_array(mem_ctx
, char, bytes
+pad_bytes
+1);
162 if (!out
) return NULL
;
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;
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)));
178 for (;i
<bytes
+pad_bytes
;i
++)
186 see if a buffer should be base64 encoded
188 int ldb_should_b64_encode(struct ldb_context
*ldb
, const struct ldb_val
*val
)
191 uint8_t *p
= val
->data
;
193 if (val
->length
== 0) {
197 if (p
[0] == ' ' || p
[0] == ':') {
201 for (i
=0; i
<val
->length
; i
++) {
202 if (!isprint(p
[i
]) || p
[i
] == '\n') {
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)
213 write a line folded string onto a file
215 static int fold_string(int (*fprintf_fn
)(void *, const char *, ...), void *private_data
,
216 const char *buf
, size_t length
, int start_pos
)
221 for (i
=0;i
<length
;i
++) {
222 ret
= fprintf_fn(private_data
, "%c", buf
[i
]);
224 if (i
!= (length
-1) && (i
+ start_pos
) % 77 == 0) {
225 ret
= fprintf_fn(private_data
, "\n ");
236 encode as base64 to a file
238 static int base64_encode_f(struct ldb_context
*ldb
,
239 int (*fprintf_fn
)(void *, const char *, ...),
241 const char *buf
, int len
, int start_pos
)
243 char *b
= ldb_base64_encode(ldb
, buf
, len
);
250 ret
= fold_string(fprintf_fn
, private_data
, b
, strlen(b
), start_pos
);
257 static const struct {
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
},
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)
273 write to ldif, using a caller supplied write method
275 int ldb_ldif_write(struct ldb_context
*ldb
,
276 int (*fprintf_fn
)(void *, const char *, ...),
278 const struct ldb_ldif
*ldif
)
284 const struct ldb_message
*msg
;
286 mem_ctx
= talloc_named_const(NULL
, 0, "ldb_ldif_write");
289 p
= ldb_dn_get_extended_linearized(mem_ctx
, msg
->dn
, 1);
290 ret
= fprintf_fn(private_data
, "dn: %s\n", p
);
294 if (ldif
->changetype
!= LDB_CHANGETYPE_NONE
) {
295 for (i
=0;ldb_changetypes
[i
].name
;i
++) {
296 if (ldb_changetypes
[i
].changetype
== ldif
->changetype
) {
300 if (!ldb_changetypes
[i
].name
) {
301 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "Error: Invalid ldif changetype %d",
303 talloc_free(mem_ctx
);
306 ret
= fprintf_fn(private_data
, "changetype: %s\n", ldb_changetypes
[i
].name
);
310 for (i
=0;i
<msg
->num_elements
;i
++) {
311 const struct ldb_schema_attribute
*a
;
313 a
= ldb_schema_attribute_by_name(ldb
, msg
->elements
[i
].name
);
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
);
321 case LDB_FLAG_MOD_DELETE
:
322 fprintf_fn(private_data
, "delete: %s\n",
323 msg
->elements
[i
].name
);
325 case LDB_FLAG_MOD_REPLACE
:
326 fprintf_fn(private_data
, "replace: %s\n",
327 msg
->elements
[i
].name
);
332 for (j
=0;j
<msg
->elements
[i
].num_values
;j
++) {
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
];
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
);
345 ret
= base64_encode_f(ldb
, fprintf_fn
, private_data
,
346 (char *)v
.data
, v
.length
,
347 strlen(msg
->elements
[i
].name
)+3);
349 ret
= fprintf_fn(private_data
, "\n");
352 ret
= fprintf_fn(private_data
, "%s: ", msg
->elements
[i
].name
);
354 if (ldb
->flags
& LDB_FLG_SHOW_BINARY
) {
355 ret
= fprintf_fn(private_data
, "%*.*s",
356 v
.length
, v
.length
, (char *)v
.data
);
358 ret
= fold_string(fprintf_fn
, private_data
,
359 (char *)v
.data
, v
.length
,
360 strlen(msg
->elements
[i
].name
)+2);
363 ret
= fprintf_fn(private_data
, "\n");
366 if (v
.data
!= msg
->elements
[i
].values
[j
].data
) {
370 if (ldif
->changetype
== LDB_CHANGETYPE_MODIFY
) {
371 fprintf_fn(private_data
, "-\n");
374 ret
= fprintf_fn(private_data
,"\n");
377 talloc_free(mem_ctx
);
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
391 static char *next_chunk(struct ldb_context
*ldb
,
392 int (*fgetc_fn
)(void *), void *private_data
)
394 size_t alloc_size
=0, chunk_size
= 0;
399 while ((c
= fgetc_fn(private_data
)) != EOF
) {
400 if (chunk_size
+1 >= alloc_size
) {
403 c2
= talloc_realloc(ldb
, chunk
, char, alloc_size
);
419 /* handle continuation lines - see RFC2849 */
420 if (c
== ' ' && chunk_size
> 1 && chunk
[chunk_size
-1] == '\n') {
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;
431 if (c
== '#' && (chunk_size
== 0 || chunk
[chunk_size
-1] == '\n')) {
436 /* ignore leading blank lines */
437 if (chunk_size
== 0 && c
== '\n') {
441 chunk
[chunk_size
++] = c
;
445 chunk
[chunk_size
] = 0;
452 /* simple ldif attribute parser */
453 static int next_attr(TALLOC_CTX
*mem_ctx
, char **s
, const char **attr
, struct ldb_val
*value
)
456 int base64_encoded
= 0;
459 if (strncmp(*s
, "-\n", 2) == 0) {
485 while (*p
== ' ' || *p
== '\t') {
489 value
->data
= (uint8_t *)p
;
494 value
->length
= strlen((char *)value
->data
);
495 *s
= ((char *)value
->data
) + value
->length
;
497 value
->length
= p
- (char *)value
->data
;
502 if (base64_encoded
) {
503 int len
= ldb_base64_decode((char *)value
->data
);
505 /* it wasn't valid base64 data */
512 int len
= ldb_read_data_file(mem_ctx
, value
);
514 /* an error occurred while trying to retrieve the file */
524 free a message from a ldif_read
526 void ldb_ldif_read_free(struct ldb_context
*ldb
, struct ldb_ldif
*ldif
)
531 int ldb_ldif_parse_modrdn(struct ldb_context
*ldb
,
532 const struct ldb_ldif
*ldif
,
534 struct ldb_dn
**_olddn
,
535 struct ldb_dn
**_newrdn
,
537 struct ldb_dn
**_newsuperior
,
538 struct ldb_dn
**_newdn
)
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
;
552 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
554 if (tmp_ctx
== NULL
) {
555 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
556 "Error: talloc_new() failed");
560 if (ldif
->changetype
!= LDB_CHANGETYPE_MODRDN
) {
561 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
562 "Error: invalid changetype '%d'",
567 if (msg
->num_elements
< 2) {
568 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
569 "Error: num_elements[%u] < 2",
574 if (msg
->num_elements
> 3) {
575 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
576 "Error: num_elements[%u] > 3",
581 #define CHECK_ELEMENT(i, _name, v, needed) do { \
583 if (msg->num_elements < (i + 1)) { \
585 ldb_debug(ldb, LDB_DEBUG_ERROR, \
586 "Error: num_elements[%u] < (%u + 1)", \
587 msg->num_elements, i); \
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); \
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); \
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); \
606 v = &msg->elements[i].values[0]; \
610 CHECK_ELEMENT(0, "newrdn", newrdn_val
, true);
611 CHECK_ELEMENT(1, "deleteoldrdn", deleteoldrdn_val
, true);
612 CHECK_ELEMENT(2, "newsuperior", newsuperior_val
, false);
616 olddn
= ldb_dn_copy(tmp_ctx
, msg
->dn
);
618 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
619 "Error: failed to copy olddn '%s'",
620 ldb_dn_get_linearized(msg
->dn
));
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
);
632 tmp_false
.length
= 1;
633 tmp_false
.data
= discard_const_p(uint8_t, "0");
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) {
641 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
642 "Error: deleteoldrdn value invalid '%s' not '0'/'1'",
643 (char *)deleteoldrdn_val
->data
);
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
);
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
));
665 newdn
= ldb_dn_copy(tmp_ctx
, newrdn
);
667 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
668 "Error: failed to copy newrdn '%s'",
669 ldb_dn_get_linearized(newrdn
));
673 ok
= ldb_dn_add_base(newdn
, newsuperior
);
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
));
683 *_olddn
= talloc_move(mem_ctx
, &olddn
);
686 *_newrdn
= talloc_move(mem_ctx
, &newrdn
);
689 *_deleteoldrdn
= deleteoldrdn
;
692 if (newsuperior_val
) {
693 *_newrdn
= talloc_move(mem_ctx
, &newrdn
);
699 *_newdn
= talloc_move(mem_ctx
, &newdn
);
702 talloc_free(tmp_ctx
);
705 talloc_free(tmp_ctx
);
706 return LDB_ERR_OTHER
;
708 talloc_free(tmp_ctx
);
709 return LDB_ERR_OPERATIONS_ERROR
;
711 talloc_free(tmp_ctx
);
712 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
714 talloc_free(tmp_ctx
);
715 return LDB_ERR_INVALID_DN_SYNTAX
;
719 read from a LDIF source, creating a ldb_message
721 struct ldb_ldif
*ldb_ldif_read(struct ldb_context
*ldb
,
722 int (*fgetc_fn
)(void *), void *private_data
)
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
;
733 ldif
= talloc(ldb
, struct ldb_ldif
);
734 if (!ldif
) return NULL
;
736 ldif
->msg
= talloc(ldif
, struct ldb_message
);
737 if (ldif
->msg
== NULL
) {
742 ldif
->changetype
= LDB_CHANGETYPE_NONE
;
746 msg
->elements
= NULL
;
747 msg
->num_elements
= 0;
749 chunk
= next_chunk(ldb
, fgetc_fn
, private_data
);
753 talloc_steal(ldif
, chunk
);
757 if (next_attr(ldif
, &s
, &attr
, &value
) != 0) {
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'",
768 msg
->dn
= ldb_dn_from_ldb_val(msg
, ldb
, &value
);
770 if ( ! ldb_dn_validate(msg
->dn
)) {
771 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "Error: Unable to parse dn '%s'",
776 while (next_attr(ldif
, &s
, &attr
, &value
) == 0) {
777 const struct ldb_schema_attribute
*a
;
778 struct ldb_message_element
*el
;
781 if (ldb_attr_cmp(attr
, "changetype") == 0) {
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
;
789 if (!ldb_changetypes
[i
].name
) {
790 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
791 "Error: Bad ldif changetype '%s'",(char *)value
.data
);
797 if (ldb_attr_cmp(attr
, "add") == 0) {
798 flags
= LDB_FLAG_MOD_ADD
;
801 if (ldb_attr_cmp(attr
, "delete") == 0) {
802 flags
= LDB_FLAG_MOD_DELETE
;
805 if (ldb_attr_cmp(attr
, "replace") == 0) {
806 flags
= LDB_FLAG_MOD_REPLACE
;
809 if (ldb_attr_cmp(attr
, "-") == 0) {
815 if (ldb_msg_add_empty(msg
, (char *)value
.data
, flags
, NULL
) != 0) {
821 el
= &msg
->elements
[msg
->num_elements
-1];
823 a
= ldb_schema_attribute_by_name(ldb
, attr
);
825 if (msg
->num_elements
> 0 && ldb_attr_cmp(attr
, el
->name
) == 0 &&
826 flags
== el
->flags
) {
827 /* its a continuation */
829 talloc_realloc(msg
->elements
, el
->values
,
830 struct ldb_val
, el
->num_values
+1);
834 ret
= a
->syntax
->ldif_read_fn(ldb
, el
->values
, &value
, &el
->values
[el
->num_values
]);
838 if (value
.length
== 0) {
839 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
840 "Error: Attribute value cannot be empty for attribute '%s'", el
->name
);
843 if (value
.data
!= el
->values
[el
->num_values
].data
) {
844 talloc_steal(el
->values
, el
->values
[el
->num_values
].data
);
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
) {
855 el
= &msg
->elements
[msg
->num_elements
];
857 el
->name
= talloc_strdup(msg
->elements
, attr
);
858 el
->values
= talloc(msg
->elements
, struct ldb_val
);
859 if (!el
->values
|| !el
->name
) {
863 ret
= a
->syntax
->ldif_read_fn(ldb
, el
->values
, &value
, &el
->values
[0]);
867 if (value
.data
!= el
->values
[0].data
) {
868 talloc_steal(el
->values
, el
->values
[0].data
);
874 if (ldif
->changetype
== LDB_CHANGETYPE_MODRDN
) {
877 ret
= ldb_ldif_parse_modrdn(ldb
, ldif
, ldif
,
878 NULL
, NULL
, NULL
, NULL
, NULL
);
879 if (ret
!= LDB_SUCCESS
) {
894 a wrapper around ldif_read() for reading from FILE*
897 static int fgetc_file(void *private_data
)
900 struct ldif_read_file_state
*state
=
901 (struct ldif_read_file_state
*)private_data
;
909 struct ldb_ldif
*ldb_ldif_read_file_state(struct ldb_context
*ldb
,
910 struct ldif_read_file_state
*state
)
912 return ldb_ldif_read(ldb
, fgetc_file
, state
);
915 struct ldb_ldif
*ldb_ldif_read_file(struct ldb_context
*ldb
, FILE *f
)
917 struct ldif_read_file_state state
;
919 return ldb_ldif_read_file_state(ldb
, &state
);
923 a wrapper around ldif_read() for reading from const char*
925 struct ldif_read_string_state
{
929 static int fgetc_string(void *private_data
)
931 struct ldif_read_string_state
*state
=
932 (struct ldif_read_string_state
*)private_data
;
933 if (state
->s
[0] != 0) {
939 struct ldb_ldif
*ldb_ldif_read_string(struct ldb_context
*ldb
, const char **s
)
941 struct ldif_read_string_state state
;
942 struct ldb_ldif
*ldif
;
944 ldif
= ldb_ldif_read(ldb
, fgetc_string
, &state
);
951 wrapper around ldif_write() for a file
953 struct ldif_write_file_state
{
957 static int fprintf_file(void *private_data
, const char *fmt
, ...) PRINTF_ATTRIBUTE(2, 3);
959 static int fprintf_file(void *private_data
, const char *fmt
, ...)
961 struct ldif_write_file_state
*state
=
962 (struct ldif_write_file_state
*)private_data
;
967 ret
= vfprintf(state
->f
, fmt
, ap
);
972 int ldb_ldif_write_file(struct ldb_context
*ldb
, FILE *f
, const struct ldb_ldif
*ldif
)
974 struct ldif_write_file_state state
;
976 return ldb_ldif_write(ldb
, fprintf_file
, &state
, ldif
);
980 wrapper around ldif_write() for a string
982 struct ldif_write_string_state
{
986 static int ldif_printf_string(void *private_data
, const char *fmt
, ...) PRINTF_ATTRIBUTE(2, 3);
988 static int ldif_printf_string(void *private_data
, const char *fmt
, ...)
990 struct ldif_write_string_state
*state
=
991 (struct ldif_write_string_state
*)private_data
;
993 size_t oldlen
= talloc_get_size(state
->string
);
996 state
->string
= talloc_vasprintf_append(state
->string
, fmt
, ap
);
998 if (!state
->string
) {
1002 return talloc_get_size(state
->string
) - oldlen
;
1005 char *ldb_ldif_write_string(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
1006 const struct ldb_ldif
*ldif
)
1008 struct ldif_write_string_state state
;
1009 state
.string
= talloc_strdup(mem_ctx
, "");
1010 if (!state
.string
) {
1013 if (ldb_ldif_write(ldb
, ldif_printf_string
, &state
, ldif
) == -1) {
1016 return state
.string
;
1020 convenient function to turn a ldb_message into a string. Useful for
1023 char *ldb_ldif_message_string(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
1024 enum ldb_changetype changetype
,
1025 const struct ldb_message
*msg
)
1027 struct ldb_ldif ldif
;
1029 ldif
.changetype
= changetype
;
1030 ldif
.msg
= discard_const_p(struct ldb_message
, msg
);
1032 return ldb_ldif_write_string(ldb
, mem_ctx
, &ldif
);