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
)
52 const char *fname
= (const char *)value
->data
;
54 if (strncmp(fname
, "file://", 7) != 0) {
55 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
59 f
= open(fname
, O_RDONLY
);
64 if (fstat(f
, &statbuf
) != 0) {
69 if (statbuf
.st_size
== 0) {
74 value
->data
= (uint8_t *)talloc_size(mem_ctx
, statbuf
.st_size
+ 1);
75 if (value
->data
== NULL
) {
79 value
->data
[statbuf
.st_size
] = 0;
82 size
= statbuf
.st_size
;
83 buf
= (char *)value
->data
;
84 while (count
< statbuf
.st_size
) {
85 bytes
= read(f
, buf
, size
);
87 talloc_free(value
->data
);
96 value
->length
= statbuf
.st_size
;
97 ret
= statbuf
.st_size
;
105 this base64 decoder was taken from jitterbug (written by tridge).
106 we might need to replace it with a new version
108 int ldb_base64_decode(char *s
)
110 const char *b64
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
111 int bit_offset
=0, byte_offset
, idx
, i
, n
;
112 uint8_t *d
= (uint8_t *)s
;
117 while (*s
&& (p
=strchr(b64
,*s
))) {
118 idx
= (int)(p
- b64
);
119 byte_offset
= (i
*6)/8;
120 bit_offset
= (i
*6)%8;
121 d
[byte_offset
] &= ~((1<<(8-bit_offset
))-1);
122 if (bit_offset
< 3) {
123 d
[byte_offset
] |= (idx
<< (2-bit_offset
));
126 d
[byte_offset
] |= (idx
>> (bit_offset
-2));
127 d
[byte_offset
+1] = 0;
128 d
[byte_offset
+1] |= (idx
<< (8-(bit_offset
-2))) & 0xFF;
133 if (bit_offset
>= 3) {
138 /* the only termination allowed */
154 char *ldb_base64_encode(TALLOC_CTX
*mem_ctx
, const char *buf
, int len
)
156 const char *b64
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
157 int bit_offset
, byte_offset
, idx
, i
;
158 const uint8_t *d
= (const uint8_t *)buf
;
159 int bytes
= (len
*8 + 5)/6, pad_bytes
= (bytes
% 4) ? 4 - (bytes
% 4) : 0;
162 out
= talloc_array(mem_ctx
, char, bytes
+pad_bytes
+1);
163 if (!out
) return NULL
;
165 for (i
=0;i
<bytes
;i
++) {
166 byte_offset
= (i
*6)/8;
167 bit_offset
= (i
*6)%8;
168 if (bit_offset
< 3) {
169 idx
= (d
[byte_offset
] >> (2-bit_offset
)) & 0x3F;
171 idx
= (d
[byte_offset
] << (bit_offset
-2)) & 0x3F;
172 if (byte_offset
+1 < len
) {
173 idx
|= (d
[byte_offset
+1] >> (8-(bit_offset
-2)));
179 for (;i
<bytes
+pad_bytes
;i
++)
187 see if a buffer should be base64 encoded
189 int ldb_should_b64_encode(struct ldb_context
*ldb
, const struct ldb_val
*val
)
192 uint8_t *p
= val
->data
;
194 if (val
->length
== 0) {
198 if (p
[0] == ' ' || p
[0] == ':') {
202 for (i
=0; i
<val
->length
; i
++) {
203 if (!isprint(p
[i
]) || p
[i
] == '\n') {
210 /* this macro is used to handle the return checking on fprintf_fn() */
211 #define CHECK_RET do { if (ret < 0) return ret; total += ret; } while (0)
214 write a line folded string onto a file
216 static int fold_string(int (*fprintf_fn
)(void *, const char *, ...), void *private_data
,
217 const char *buf
, size_t length
, int start_pos
)
223 for (i
=0;i
<length
;i
++) {
224 ret
= fprintf_fn(private_data
, "%c", buf
[i
]);
226 if (i
!= (length
-1) && (i
+ start_pos
) % 77 == 0) {
227 ret
= fprintf_fn(private_data
, "\n ");
238 encode as base64 to a file
240 static int base64_encode_f(struct ldb_context
*ldb
,
241 int (*fprintf_fn
)(void *, const char *, ...),
243 const char *buf
, int len
, int start_pos
)
245 char *b
= ldb_base64_encode(ldb
, buf
, len
);
252 ret
= fold_string(fprintf_fn
, private_data
, b
, strlen(b
), start_pos
);
259 static const struct {
261 enum ldb_changetype changetype
;
262 } ldb_changetypes
[] = {
263 {"add", LDB_CHANGETYPE_ADD
},
264 {"delete", LDB_CHANGETYPE_DELETE
},
265 {"modify", LDB_CHANGETYPE_MODIFY
},
266 {"modrdn", LDB_CHANGETYPE_MODRDN
},
267 {"moddn", LDB_CHANGETYPE_MODRDN
},
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)
275 write to ldif, using a caller supplied write method, and only printing secrets if we are not in a trace
277 static int ldb_ldif_write_trace(struct ldb_context
*ldb
,
278 int (*fprintf_fn
)(void *, const char *, ...),
280 const struct ldb_ldif
*ldif
,
288 const struct ldb_message
*msg
;
289 const char * const * secret_attributes
= ldb_get_opaque(ldb
, LDB_SECRET_ATTRIBUTE_LIST_OPAQUE
);
291 mem_ctx
= talloc_named_const(NULL
, 0, "ldb_ldif_write");
294 p
= ldb_dn_get_extended_linearized(mem_ctx
, msg
->dn
, 1);
295 ret
= fprintf_fn(private_data
, "dn: %s\n", p
);
299 if (ldif
->changetype
!= LDB_CHANGETYPE_NONE
) {
300 for (i
=0;ldb_changetypes
[i
].name
;i
++) {
301 if (ldb_changetypes
[i
].changetype
== ldif
->changetype
) {
305 if (!ldb_changetypes
[i
].name
) {
306 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "Error: Invalid ldif changetype %d",
308 talloc_free(mem_ctx
);
311 ret
= fprintf_fn(private_data
, "changetype: %s\n", ldb_changetypes
[i
].name
);
315 for (i
=0;i
<msg
->num_elements
;i
++) {
316 const struct ldb_schema_attribute
*a
;
319 if (msg
->elements
[i
].name
== NULL
) {
320 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
321 "Error: Invalid element name (NULL) at position %d", i
);
322 talloc_free(mem_ctx
);
326 namelen
= strlen(msg
->elements
[i
].name
);
327 a
= ldb_schema_attribute_by_name(ldb
, msg
->elements
[i
].name
);
329 if (ldif
->changetype
== LDB_CHANGETYPE_MODIFY
) {
330 switch (msg
->elements
[i
].flags
& LDB_FLAG_MOD_MASK
) {
331 case LDB_FLAG_MOD_ADD
:
332 fprintf_fn(private_data
, "add: %s\n",
333 msg
->elements
[i
].name
);
335 case LDB_FLAG_MOD_DELETE
:
336 fprintf_fn(private_data
, "delete: %s\n",
337 msg
->elements
[i
].name
);
339 case LDB_FLAG_MOD_REPLACE
:
340 fprintf_fn(private_data
, "replace: %s\n",
341 msg
->elements
[i
].name
);
346 if (in_trace
&& secret_attributes
&& ldb_attr_in_list(secret_attributes
, msg
->elements
[i
].name
)) {
347 /* Deliberately skip printing this password */
348 ret
= fprintf_fn(private_data
, "# %s::: REDACTED SECRET ATTRIBUTE\n",
349 msg
->elements
[i
].name
);
353 for (j
=0;j
<msg
->elements
[i
].num_values
;j
++) {
355 bool use_b64_encode
= false;
356 bool copy_raw_bytes
= false;
358 ret
= a
->syntax
->ldif_write_fn(ldb
, mem_ctx
, &msg
->elements
[i
].values
[j
], &v
);
359 if (ret
!= LDB_SUCCESS
) {
360 v
= msg
->elements
[i
].values
[j
];
363 if (ldb
->flags
& LDB_FLG_SHOW_BINARY
) {
364 use_b64_encode
= false;
365 copy_raw_bytes
= true;
366 } else if (a
->flags
& LDB_ATTR_FLAG_FORCE_BASE64_LDIF
) {
367 use_b64_encode
= true;
368 } else if (msg
->elements
[i
].flags
&
369 LDB_FLAG_FORCE_NO_BASE64_LDIF
) {
370 use_b64_encode
= false;
371 copy_raw_bytes
= true;
373 use_b64_encode
= ldb_should_b64_encode(ldb
, &v
);
376 if (ret
!= LDB_SUCCESS
|| use_b64_encode
) {
377 ret
= fprintf_fn(private_data
, "%s:: ",
378 msg
->elements
[i
].name
);
380 ret
= base64_encode_f(ldb
, fprintf_fn
, private_data
,
381 (char *)v
.data
, v
.length
,
384 ret
= fprintf_fn(private_data
, "\n");
387 ret
= fprintf_fn(private_data
, "%s: ", msg
->elements
[i
].name
);
389 if (copy_raw_bytes
) {
390 ret
= fprintf_fn(private_data
, "%*.*s",
391 v
.length
, v
.length
, (char *)v
.data
);
393 ret
= fold_string(fprintf_fn
, private_data
,
394 (char *)v
.data
, v
.length
,
398 ret
= fprintf_fn(private_data
, "\n");
401 if (v
.data
!= msg
->elements
[i
].values
[j
].data
) {
405 if (ldif
->changetype
== LDB_CHANGETYPE_MODIFY
) {
406 fprintf_fn(private_data
, "-\n");
409 ret
= fprintf_fn(private_data
,"\n");
412 talloc_free(mem_ctx
);
421 write to ldif, using a caller supplied write method
423 int ldb_ldif_write(struct ldb_context
*ldb
,
424 int (*fprintf_fn
)(void *, const char *, ...),
426 const struct ldb_ldif
*ldif
)
428 return ldb_ldif_write_trace(ldb
, fprintf_fn
, private_data
, ldif
, false);
433 pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
434 this routine removes any RFC2849 continuations and comments
438 static char *next_chunk(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
439 int (*fgetc_fn
)(void *), void *private_data
)
441 size_t alloc_size
=0, chunk_size
= 0;
446 while ((c
= fgetc_fn(private_data
)) != EOF
) {
447 if (chunk_size
+1 >= alloc_size
) {
450 c2
= talloc_realloc(mem_ctx
, chunk
, char, alloc_size
);
466 /* handle continuation lines - see RFC2849 */
467 if (c
== ' ' && chunk_size
> 1 && chunk
[chunk_size
-1] == '\n') {
472 /* chunks are terminated by a double line-feed */
473 if (c
== '\n' && chunk_size
> 0 && chunk
[chunk_size
-1] == '\n') {
474 chunk
[chunk_size
-1] = 0;
478 if (c
== '#' && (chunk_size
== 0 || chunk
[chunk_size
-1] == '\n')) {
483 /* ignore leading blank lines */
484 if (chunk_size
== 0 && c
== '\n') {
488 chunk
[chunk_size
++] = c
;
492 chunk
[chunk_size
] = 0;
499 /* simple ldif attribute parser */
500 static int next_attr(TALLOC_CTX
*mem_ctx
, char **s
, const char **attr
, struct ldb_val
*value
)
503 int base64_encoded
= 0;
506 if (strncmp(*s
, "-\n", 2) == 0) {
532 while (*p
== ' ' || *p
== '\t') {
536 value
->data
= (uint8_t *)p
;
541 value
->length
= strlen((char *)value
->data
);
542 *s
= ((char *)value
->data
) + value
->length
;
544 value
->length
= p
- (char *)value
->data
;
549 if (base64_encoded
) {
550 int len
= ldb_base64_decode((char *)value
->data
);
552 /* it wasn't valid base64 data */
559 int len
= ldb_read_data_file(mem_ctx
, value
);
561 /* an error occurred while trying to retrieve the file */
571 free a message from a ldif_read
573 void ldb_ldif_read_free(struct ldb_context
*ldb
, struct ldb_ldif
*ldif
)
578 int ldb_ldif_parse_modrdn(struct ldb_context
*ldb
,
579 const struct ldb_ldif
*ldif
,
581 struct ldb_dn
**_olddn
,
582 struct ldb_dn
**_newrdn
,
584 struct ldb_dn
**_newsuperior
,
585 struct ldb_dn
**_newdn
)
587 struct ldb_message
*msg
= ldif
->msg
;
588 struct ldb_val _newrdn_val
= {};
589 struct ldb_val
*newrdn_val
= NULL
;
590 struct ldb_val
*deleteoldrdn_val
= NULL
;
591 struct ldb_val
*newsuperior_val
= NULL
;
592 struct ldb_dn
*olddn
= NULL
;
593 struct ldb_dn
*newrdn
= NULL
;
594 bool deleteoldrdn
= true;
595 struct ldb_dn
*newsuperior
= NULL
;
596 struct ldb_dn
*newdn
= NULL
;
597 struct ldb_val tmp_false
;
598 struct ldb_val tmp_true
;
600 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
602 if (tmp_ctx
== NULL
) {
603 ldb_debug(ldb
, LDB_DEBUG_FATAL
,
604 "Error: talloc_new() failed");
608 if (ldif
->changetype
!= LDB_CHANGETYPE_MODRDN
) {
609 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
610 "Error: invalid changetype '%d'",
615 if (msg
->num_elements
< 2) {
616 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
617 "Error: num_elements[%u] < 2",
622 if (msg
->num_elements
> 3) {
623 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
624 "Error: num_elements[%u] > 3",
629 #define CHECK_ELEMENT(i, _name, v, needed) do { \
631 if (msg->num_elements < (i + 1)) { \
633 ldb_debug(ldb, LDB_DEBUG_ERROR, \
634 "Error: num_elements[%u] < (%u + 1)", \
635 msg->num_elements, i); \
638 } else if (ldb_attr_cmp(msg->elements[i].name, _name) != 0) { \
639 ldb_debug(ldb, LDB_DEBUG_ERROR, \
640 "Error: elements[%u].name[%s] != [%s]", \
641 i, msg->elements[i].name, _name); \
643 } else if (msg->elements[i].flags != 0) { \
644 ldb_debug(ldb, LDB_DEBUG_ERROR, \
645 "Error: elements[%u].flags[0x%X} != [0x0]", \
646 i, msg->elements[i].flags); \
648 } else if (msg->elements[i].num_values != 1) { \
649 ldb_debug(ldb, LDB_DEBUG_ERROR, \
650 "Error: elements[%u].num_values[%u] != 1", \
651 i, msg->elements[i].num_values); \
654 v = &msg->elements[i].values[0]; \
658 CHECK_ELEMENT(0, "newrdn", newrdn_val
, true);
659 CHECK_ELEMENT(1, "deleteoldrdn", deleteoldrdn_val
, true);
660 CHECK_ELEMENT(2, "newsuperior", newsuperior_val
, false);
664 olddn
= ldb_dn_copy(tmp_ctx
, msg
->dn
);
666 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
667 "Error: failed to copy olddn '%s'",
668 ldb_dn_get_linearized(msg
->dn
));
672 if (newrdn_val
->length
!= 0 && strchr((const char *)newrdn_val
->data
, '=') == NULL
) {
673 const char *rdn_name
= ldb_dn_get_rdn_name(olddn
);
674 char *new_rdn
= NULL
;
676 new_rdn
= talloc_asprintf(tmp_ctx
,
679 (const char *)newrdn_val
->data
);
680 if (new_rdn
== NULL
) {
681 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
682 "Error: failed to allocate '%s=%s'",
683 rdn_name
, (char *)newrdn_val
->data
);
686 _newrdn_val
.data
= (uint8_t *)new_rdn
;
687 _newrdn_val
.length
= strlen(new_rdn
);
688 newrdn_val
= &_newrdn_val
;
691 newrdn
= ldb_dn_from_ldb_val(tmp_ctx
, ldb
, newrdn_val
);
692 if (!ldb_dn_validate(newrdn
)) {
693 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
694 "Error: Unable to parse dn '%s'",
695 (char *)newrdn_val
->data
);
699 tmp_false
.length
= 1;
700 tmp_false
.data
= discard_const_p(uint8_t, "0");
702 tmp_true
.data
= discard_const_p(uint8_t, "1");
703 if (ldb_val_equal_exact(deleteoldrdn_val
, &tmp_false
) == 1) {
704 deleteoldrdn
= false;
705 } else if (ldb_val_equal_exact(deleteoldrdn_val
, &tmp_true
) == 1) {
708 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
709 "Error: deleteoldrdn value invalid '%s' not '0'/'1'",
710 (char *)deleteoldrdn_val
->data
);
714 if (newsuperior_val
) {
715 newsuperior
= ldb_dn_from_ldb_val(tmp_ctx
, ldb
, newsuperior_val
);
716 if (!ldb_dn_validate(newsuperior
)) {
717 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
718 "Error: Unable to parse dn '%s'",
719 (char *)newsuperior_val
->data
);
723 newsuperior
= ldb_dn_get_parent(tmp_ctx
, msg
->dn
);
724 if (newsuperior
== NULL
) {
725 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
726 "Error: Unable to get parent dn '%s'",
727 ldb_dn_get_linearized(msg
->dn
));
732 newdn
= ldb_dn_copy(tmp_ctx
, newrdn
);
734 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
735 "Error: failed to copy newrdn '%s'",
736 ldb_dn_get_linearized(newrdn
));
740 ok
= ldb_dn_add_base(newdn
, newsuperior
);
742 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
743 "Error: failed to base '%s' to newdn '%s'",
744 ldb_dn_get_linearized(newsuperior
),
745 ldb_dn_get_linearized(newdn
));
750 *_olddn
= talloc_move(mem_ctx
, &olddn
);
753 *_newrdn
= talloc_move(mem_ctx
, &newrdn
);
756 *_deleteoldrdn
= deleteoldrdn
;
758 if (_newsuperior
!= NULL
&& _newrdn
!= NULL
) {
759 if (newsuperior_val
) {
760 *_newrdn
= talloc_move(mem_ctx
, &newrdn
);
766 *_newdn
= talloc_move(mem_ctx
, &newdn
);
769 talloc_free(tmp_ctx
);
772 talloc_free(tmp_ctx
);
773 return LDB_ERR_OTHER
;
775 talloc_free(tmp_ctx
);
776 return LDB_ERR_OPERATIONS_ERROR
;
778 talloc_free(tmp_ctx
);
779 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
;
781 talloc_free(tmp_ctx
);
782 return LDB_ERR_INVALID_DN_SYNTAX
;
786 read from a LDIF source, creating a ldb_message
788 struct ldb_ldif
*ldb_ldif_read(struct ldb_context
*ldb
,
789 int (*fgetc_fn
)(void *), void *private_data
)
791 struct ldb_ldif
*ldif
;
792 struct ldb_message
*msg
;
793 const char *attr
=NULL
;
794 char *chunk
=NULL
, *s
;
795 struct ldb_val value
;
799 ldif
= talloc(ldb
, struct ldb_ldif
);
800 if (!ldif
) return NULL
;
802 ldif
->msg
= ldb_msg_new(ldif
);
803 if (ldif
->msg
== NULL
) {
808 ldif
->changetype
= LDB_CHANGETYPE_NONE
;
811 chunk
= next_chunk(ldb
, ldif
, fgetc_fn
, private_data
);
818 if (next_attr(ldif
, &s
, &attr
, &value
) != 0) {
822 /* first line must be a dn */
823 if (ldb_attr_cmp(attr
, "dn") != 0) {
824 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "Error: First line of ldif must be a dn not '%s'",
829 msg
->dn
= ldb_dn_from_ldb_val(msg
, ldb
, &value
);
831 if ( ! ldb_dn_validate(msg
->dn
)) {
832 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "Error: Unable to parse dn '%s'",
837 while (next_attr(ldif
, &s
, &attr
, &value
) == 0) {
838 const struct ldb_schema_attribute
*a
;
839 struct ldb_message_element
*el
;
842 if (ldb_attr_cmp(attr
, "changetype") == 0) {
844 for (i
=0;ldb_changetypes
[i
].name
;i
++) {
845 if (ldb_attr_cmp((char *)value
.data
, ldb_changetypes
[i
].name
) == 0) {
846 ldif
->changetype
= ldb_changetypes
[i
].changetype
;
850 if (!ldb_changetypes
[i
].name
) {
851 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
852 "Error: Bad ldif changetype '%s'",(char *)value
.data
);
858 if (ldb_attr_cmp(attr
, "add") == 0) {
859 flags
= LDB_FLAG_MOD_ADD
;
862 if (ldb_attr_cmp(attr
, "delete") == 0) {
863 flags
= LDB_FLAG_MOD_DELETE
;
866 if (ldb_attr_cmp(attr
, "replace") == 0) {
867 flags
= LDB_FLAG_MOD_REPLACE
;
870 if (ldb_attr_cmp(attr
, "-") == 0) {
876 if (ldb_msg_add_empty(msg
, (char *)value
.data
, flags
, NULL
) != 0) {
882 a
= ldb_schema_attribute_by_name(ldb
, attr
);
883 el
= (msg
->num_elements
> 0
884 ? &msg
->elements
[msg
->num_elements
- 1]
887 if (el
&& ldb_attr_cmp(attr
, el
->name
) == 0 && flags
== el
->flags
) {
888 /* its a continuation */
890 talloc_realloc(msg
->elements
, el
->values
,
891 struct ldb_val
, el
->num_values
+1);
895 ret
= a
->syntax
->ldif_read_fn(ldb
, el
->values
, &value
, &el
->values
[el
->num_values
]);
899 if (value
.length
== 0) {
900 ldb_debug(ldb
, LDB_DEBUG_ERROR
,
901 "Error: Attribute value cannot be empty for attribute '%s'", el
->name
);
904 if (value
.data
!= el
->values
[el
->num_values
].data
) {
905 talloc_steal(el
->values
, el
->values
[el
->num_values
].data
);
909 /* its a new attribute */
910 msg
->elements
= talloc_realloc(msg
, msg
->elements
,
911 struct ldb_message_element
,
912 msg
->num_elements
+1);
913 if (!msg
->elements
) {
916 el
= &msg
->elements
[msg
->num_elements
];
918 el
->name
= talloc_strdup(msg
->elements
, attr
);
919 el
->values
= talloc(msg
->elements
, struct ldb_val
);
920 if (!el
->values
|| !el
->name
) {
924 ret
= a
->syntax
->ldif_read_fn(ldb
, el
->values
, &value
, &el
->values
[0]);
928 if (value
.data
!= el
->values
[0].data
) {
929 talloc_steal(el
->values
, el
->values
[0].data
);
935 if (ldif
->changetype
== LDB_CHANGETYPE_MODRDN
) {
938 ret
= ldb_ldif_parse_modrdn(ldb
, ldif
, ldif
,
939 NULL
, NULL
, NULL
, NULL
, NULL
);
940 if (ret
!= LDB_SUCCESS
) {
955 a wrapper around ldif_read() for reading from FILE*
958 static int fgetc_file(void *private_data
)
961 struct ldif_read_file_state
*state
=
962 (struct ldif_read_file_state
*)private_data
;
970 struct ldb_ldif
*ldb_ldif_read_file_state(struct ldb_context
*ldb
,
971 struct ldif_read_file_state
*state
)
973 return ldb_ldif_read(ldb
, fgetc_file
, state
);
976 struct ldb_ldif
*ldb_ldif_read_file(struct ldb_context
*ldb
, FILE *f
)
978 struct ldif_read_file_state state
;
980 return ldb_ldif_read_file_state(ldb
, &state
);
984 a wrapper around ldif_read() for reading from const char*
986 struct ldif_read_string_state
{
990 static int fgetc_string(void *private_data
)
992 struct ldif_read_string_state
*state
=
993 (struct ldif_read_string_state
*)private_data
;
994 if (state
->s
[0] != 0) {
1000 struct ldb_ldif
*ldb_ldif_read_string(struct ldb_context
*ldb
, const char **s
)
1002 struct ldif_read_string_state state
;
1003 struct ldb_ldif
*ldif
;
1005 ldif
= ldb_ldif_read(ldb
, fgetc_string
, &state
);
1012 wrapper around ldif_write() for a file
1014 struct ldif_write_file_state
{
1018 static int fprintf_file(void *private_data
, const char *fmt
, ...) PRINTF_ATTRIBUTE(2, 3);
1020 static int fprintf_file(void *private_data
, const char *fmt
, ...)
1022 struct ldif_write_file_state
*state
=
1023 (struct ldif_write_file_state
*)private_data
;
1028 ret
= vfprintf(state
->f
, fmt
, ap
);
1033 int ldb_ldif_write_file(struct ldb_context
*ldb
, FILE *f
, const struct ldb_ldif
*ldif
)
1035 struct ldif_write_file_state state
;
1037 return ldb_ldif_write(ldb
, fprintf_file
, &state
, ldif
);
1041 wrapper around ldif_write() for a string
1043 struct ldif_write_string_state
{
1047 static int ldif_printf_string(void *private_data
, const char *fmt
, ...) PRINTF_ATTRIBUTE(2, 3);
1049 static int ldif_printf_string(void *private_data
, const char *fmt
, ...)
1051 struct ldif_write_string_state
*state
=
1052 (struct ldif_write_string_state
*)private_data
;
1054 size_t oldlen
= talloc_get_size(state
->string
);
1057 state
->string
= talloc_vasprintf_append(state
->string
, fmt
, ap
);
1059 if (!state
->string
) {
1063 return talloc_get_size(state
->string
) - oldlen
;
1066 char *ldb_ldif_write_redacted_trace_string(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
1067 const struct ldb_ldif
*ldif
)
1069 struct ldif_write_string_state state
;
1070 state
.string
= talloc_strdup(mem_ctx
, "");
1071 if (!state
.string
) {
1074 if (ldb_ldif_write_trace(ldb
, ldif_printf_string
, &state
, ldif
, true) == -1) {
1077 return state
.string
;
1080 char *ldb_ldif_write_string(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
1081 const struct ldb_ldif
*ldif
)
1083 struct ldif_write_string_state state
;
1084 state
.string
= talloc_strdup(mem_ctx
, "");
1085 if (!state
.string
) {
1088 if (ldb_ldif_write(ldb
, ldif_printf_string
, &state
, ldif
) == -1) {
1091 return state
.string
;
1095 convenient function to turn a ldb_message into a string. Useful for
1098 char *ldb_ldif_message_string(struct ldb_context
*ldb
, TALLOC_CTX
*mem_ctx
,
1099 enum ldb_changetype changetype
,
1100 const struct ldb_message
*msg
)
1102 struct ldb_ldif ldif
;
1104 ldif
.changetype
= changetype
;
1105 ldif
.msg
= discard_const_p(struct ldb_message
, msg
);
1107 return ldb_ldif_write_string(ldb
, mem_ctx
, &ldif
);
1111 * convenient function to turn a ldb_message into a string. Useful for
1112 * debugging but also safer if some of the LDIF could be sensitive.
1114 * The secret attributes are specified in a 'const char * const *' within
1115 * the LDB_SECRET_ATTRIBUTE_LIST opaque set on the ldb
1118 char *ldb_ldif_message_redacted_string(struct ldb_context
*ldb
,
1119 TALLOC_CTX
*mem_ctx
,
1120 enum ldb_changetype changetype
,
1121 const struct ldb_message
*msg
)
1123 struct ldb_ldif ldif
;
1125 ldif
.changetype
= changetype
;
1126 ldif
.msg
= discard_const_p(struct ldb_message
, msg
);
1128 return ldb_ldif_write_redacted_trace_string(ldb
, mem_ctx
, &ldif
);