docs-xml: fix typos and format in smb.conf server max protocol man
[Samba/gebeck_regimport.git] / lib / ldb / common / ldb_ldif.c
blobc083401c6ec98a31dd3c4b5d28830ae8d77ce9e7
1 /*
2 ldb database library
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
8 ** under the LGPL
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/>.
25 * Name: ldb
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)
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;
53 if (strncmp(fname, "file://", 7) != 0) {
54 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
56 fname += 7;
58 f = open(fname, O_RDONLY);
59 if (f == -1) {
60 return -1;
63 if (fstat(f, &statbuf) != 0) {
64 ret = -1;
65 goto done;
68 if (statbuf.st_size == 0) {
69 ret = -1;
70 goto done;
73 value->data = (uint8_t *)talloc_size(mem_ctx, statbuf.st_size + 1);
74 if (value->data == NULL) {
75 ret = -1;
76 goto done;
78 value->data[statbuf.st_size] = 0;
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;
90 count += bytes;
91 buf += bytes;
92 size -= bytes;
95 value->length = statbuf.st_size;
96 ret = statbuf.st_size;
98 done:
99 close(f);
100 return ret;
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;
112 char *p=NULL;
114 n=i=0;
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;
130 s++; i++;
132 if (bit_offset >= 3) {
133 n--;
136 if (*s && !p) {
137 /* the only termination allowed */
138 if (*s != '=') {
139 return -1;
143 /* null terminate */
144 d[n] = 0;
145 return n;
150 encode as base64
151 caller frees
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;
159 char *out;
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;
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)));
175 out[i] = b64[idx];
178 for (;i<bytes+pad_bytes;i++)
179 out[i] = '=';
180 out[i] = 0;
182 return out;
186 see if a buffer should be base64 encoded
188 int ldb_should_b64_encode(struct ldb_context *ldb, const struct ldb_val *val)
190 unsigned int i;
191 uint8_t *p = val->data;
193 if (val->length == 0) {
194 return 0;
197 if (p[0] == ' ' || p[0] == ':') {
198 return 1;
201 for (i=0; i<val->length; i++) {
202 if (!isprint(p[i]) || p[i] == '\n') {
203 return 1;
206 return 0;
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)
218 size_t i;
219 int total=0, ret;
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;
230 return total;
233 #undef CHECK_RET
236 encode as base64 to a file
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)
243 char *b = ldb_base64_encode(ldb, buf, len);
244 int ret;
246 if (!b) {
247 return -1;
250 ret = fold_string(fprintf_fn, private_data, b, strlen(b), start_pos);
252 talloc_free(b);
253 return ret;
257 static const struct {
258 const char *name;
259 enum ldb_changetype changetype;
260 } ldb_changetypes[] = {
261 {"add", LDB_CHANGETYPE_ADD},
262 {"delete", LDB_CHANGETYPE_DELETE},
263 {"modify", LDB_CHANGETYPE_MODIFY},
264 {"modrdn", LDB_CHANGETYPE_MODRDN},
265 {"moddn", LDB_CHANGETYPE_MODRDN},
266 {NULL, 0}
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 *, ...),
277 void *private_data,
278 const struct ldb_ldif *ldif)
280 TALLOC_CTX *mem_ctx;
281 unsigned int i, j;
282 int total=0, ret;
283 char *p;
284 const struct ldb_message *msg;
286 mem_ctx = talloc_named_const(NULL, 0, "ldb_ldif_write");
288 msg = ldif->msg;
289 p = ldb_dn_get_extended_linearized(mem_ctx, msg->dn, 1);
290 ret = fprintf_fn(private_data, "dn: %s\n", p);
291 talloc_free(p);
292 CHECK_RET;
294 if (ldif->changetype != LDB_CHANGETYPE_NONE) {
295 for (i=0;ldb_changetypes[i].name;i++) {
296 if (ldb_changetypes[i].changetype == ldif->changetype) {
297 break;
300 if (!ldb_changetypes[i].name) {
301 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d",
302 ldif->changetype);
303 talloc_free(mem_ctx);
304 return -1;
306 ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
307 CHECK_RET;
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);
320 break;
321 case LDB_FLAG_MOD_DELETE:
322 fprintf_fn(private_data, "delete: %s\n",
323 msg->elements[i].name);
324 break;
325 case LDB_FLAG_MOD_REPLACE:
326 fprintf_fn(private_data, "replace: %s\n",
327 msg->elements[i].name);
328 break;
332 for (j=0;j<msg->elements[i].num_values;j++) {
333 struct ldb_val v;
334 bool use_b64_encode;
335 ret = a->syntax->ldif_write_fn(ldb, mem_ctx, &msg->elements[i].values[j], &v);
336 if (ret != LDB_SUCCESS) {
337 v = msg->elements[i].values[j];
339 use_b64_encode = !(ldb->flags & LDB_FLG_SHOW_BINARY)
340 && ldb_should_b64_encode(ldb, &v);
341 if (ret != LDB_SUCCESS || use_b64_encode) {
342 ret = fprintf_fn(private_data, "%s:: ",
343 msg->elements[i].name);
344 CHECK_RET;
345 ret = base64_encode_f(ldb, fprintf_fn, private_data,
346 (char *)v.data, v.length,
347 strlen(msg->elements[i].name)+3);
348 CHECK_RET;
349 ret = fprintf_fn(private_data, "\n");
350 CHECK_RET;
351 } else {
352 ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
353 CHECK_RET;
354 if (ldb->flags & LDB_FLG_SHOW_BINARY) {
355 ret = fprintf_fn(private_data, "%*.*s",
356 v.length, v.length, (char *)v.data);
357 } else {
358 ret = fold_string(fprintf_fn, private_data,
359 (char *)v.data, v.length,
360 strlen(msg->elements[i].name)+2);
362 CHECK_RET;
363 ret = fprintf_fn(private_data, "\n");
364 CHECK_RET;
366 if (v.data != msg->elements[i].values[j].data) {
367 talloc_free(v.data);
370 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
371 fprintf_fn(private_data, "-\n");
374 ret = fprintf_fn(private_data,"\n");
375 CHECK_RET;
377 talloc_free(mem_ctx);
379 return total;
382 #undef CHECK_RET
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
389 caller frees
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;
395 char *chunk = NULL;
396 int c;
397 int in_comment = 0;
399 while ((c = fgetc_fn(private_data)) != EOF) {
400 if (chunk_size+1 >= alloc_size) {
401 char *c2;
402 alloc_size += 1024;
403 c2 = talloc_realloc(ldb, chunk, char, alloc_size);
404 if (!c2) {
405 talloc_free(chunk);
406 errno = ENOMEM;
407 return NULL;
409 chunk = c2;
412 if (in_comment) {
413 if (c == '\n') {
414 in_comment = 0;
416 continue;
419 /* handle continuation lines - see RFC2849 */
420 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
421 chunk_size--;
422 continue;
425 /* chunks are terminated by a double line-feed */
426 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
427 chunk[chunk_size-1] = 0;
428 return chunk;
431 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
432 in_comment = 1;
433 continue;
436 /* ignore leading blank lines */
437 if (chunk_size == 0 && c == '\n') {
438 continue;
441 chunk[chunk_size++] = c;
444 if (chunk) {
445 chunk[chunk_size] = 0;
448 return chunk;
452 /* simple ldif attribute parser */
453 static int next_attr(TALLOC_CTX *mem_ctx, char **s, const char **attr, struct ldb_val *value)
455 char *p;
456 int base64_encoded = 0;
457 int binary_file = 0;
459 if (strncmp(*s, "-\n", 2) == 0) {
460 value->length = 0;
461 *attr = "-";
462 *s += 2;
463 return 0;
466 p = strchr(*s, ':');
467 if (!p) {
468 return -1;
471 *p++ = 0;
473 if (*p == ':') {
474 base64_encoded = 1;
475 p++;
478 if (*p == '<') {
479 binary_file = 1;
480 p++;
483 *attr = *s;
485 while (*p == ' ' || *p == '\t') {
486 p++;
489 value->data = (uint8_t *)p;
491 p = strchr(p, '\n');
493 if (!p) {
494 value->length = strlen((char *)value->data);
495 *s = ((char *)value->data) + value->length;
496 } else {
497 value->length = p - (char *)value->data;
498 *s = p+1;
499 *p = 0;
502 if (base64_encoded) {
503 int len = ldb_base64_decode((char *)value->data);
504 if (len == -1) {
505 /* it wasn't valid base64 data */
506 return -1;
508 value->length = len;
511 if (binary_file) {
512 int len = ldb_read_data_file(mem_ctx, value);
513 if (len == -1) {
514 /* an error occurred while trying to retrieve the file */
515 return -1;
519 return 0;
524 free a message from a ldif_read
526 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
528 talloc_free(ldif);
531 int ldb_ldif_parse_modrdn(struct ldb_context *ldb,
532 const struct ldb_ldif *ldif,
533 TALLOC_CTX *mem_ctx,
534 struct ldb_dn **_olddn,
535 struct ldb_dn **_newrdn,
536 bool *_deleteoldrdn,
537 struct ldb_dn **_newsuperior,
538 struct ldb_dn **_newdn)
540 struct ldb_message *msg = ldif->msg;
541 struct ldb_val *newrdn_val = NULL;
542 struct ldb_val *deleteoldrdn_val = NULL;
543 struct ldb_val *newsuperior_val = NULL;
544 struct ldb_dn *olddn = NULL;
545 struct ldb_dn *newrdn = NULL;
546 bool deleteoldrdn = true;
547 struct ldb_dn *newsuperior = NULL;
548 struct ldb_dn *newdn = NULL;
549 struct ldb_val tmp_false;
550 struct ldb_val tmp_true;
551 bool ok;
552 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
554 if (tmp_ctx == NULL) {
555 ldb_debug(ldb, LDB_DEBUG_FATAL,
556 "Error: talloc_new() failed");
557 goto err_op;
560 if (ldif->changetype != LDB_CHANGETYPE_MODRDN) {
561 ldb_debug(ldb, LDB_DEBUG_ERROR,
562 "Error: invalid changetype '%d'",
563 ldif->changetype);
564 goto err_other;
567 if (msg->num_elements < 2) {
568 ldb_debug(ldb, LDB_DEBUG_ERROR,
569 "Error: num_elements[%u] < 2",
570 msg->num_elements);
571 goto err_other;
574 if (msg->num_elements > 3) {
575 ldb_debug(ldb, LDB_DEBUG_ERROR,
576 "Error: num_elements[%u] > 3",
577 msg->num_elements);
578 goto err_other;
581 #define CHECK_ELEMENT(i, _name, v, needed) do { \
582 v = NULL; \
583 if (msg->num_elements < (i + 1)) { \
584 if (needed) { \
585 ldb_debug(ldb, LDB_DEBUG_ERROR, \
586 "Error: num_elements[%u] < (%u + 1)", \
587 msg->num_elements, i); \
588 goto err_other; \
590 } else if (ldb_attr_cmp(msg->elements[i].name, _name) != 0) { \
591 ldb_debug(ldb, LDB_DEBUG_ERROR, \
592 "Error: elements[%u].name[%s] != [%s]", \
593 i, msg->elements[i].name, _name); \
594 goto err_other; \
595 } else if (msg->elements[i].flags != 0) { \
596 ldb_debug(ldb, LDB_DEBUG_ERROR, \
597 "Error: elements[%u].flags[0x%X} != [0x0]", \
598 i, msg->elements[i].flags); \
599 goto err_other; \
600 } else if (msg->elements[i].num_values != 1) { \
601 ldb_debug(ldb, LDB_DEBUG_ERROR, \
602 "Error: elements[%u].num_values[%u] != 1", \
603 i, msg->elements[i].num_values); \
604 goto err_other; \
605 } else { \
606 v = &msg->elements[i].values[0]; \
608 } while (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);
614 #undef CHECK_ELEMENT
616 olddn = ldb_dn_copy(tmp_ctx, msg->dn);
617 if (olddn == NULL) {
618 ldb_debug(ldb, LDB_DEBUG_ERROR,
619 "Error: failed to copy olddn '%s'",
620 ldb_dn_get_linearized(msg->dn));
621 goto err_op;
624 newrdn = ldb_dn_from_ldb_val(tmp_ctx, ldb, newrdn_val);
625 if (!ldb_dn_validate(newrdn)) {
626 ldb_debug(ldb, LDB_DEBUG_ERROR,
627 "Error: Unable to parse dn '%s'",
628 (char *)newrdn_val->data);
629 goto err_dn;
632 tmp_false.length = 1;
633 tmp_false.data = discard_const_p(uint8_t, "0");
634 tmp_true.length = 1;
635 tmp_true.data = discard_const_p(uint8_t, "1");
636 if (ldb_val_equal_exact(deleteoldrdn_val, &tmp_false) == 1) {
637 deleteoldrdn = false;
638 } else if (ldb_val_equal_exact(deleteoldrdn_val, &tmp_true) == 1) {
639 deleteoldrdn = true;
640 } else {
641 ldb_debug(ldb, LDB_DEBUG_ERROR,
642 "Error: deleteoldrdn value invalid '%s' not '0'/'1'",
643 (char *)deleteoldrdn_val->data);
644 goto err_attr;
647 if (newsuperior_val) {
648 newsuperior = ldb_dn_from_ldb_val(tmp_ctx, ldb, newsuperior_val);
649 if (!ldb_dn_validate(newsuperior)) {
650 ldb_debug(ldb, LDB_DEBUG_ERROR,
651 "Error: Unable to parse dn '%s'",
652 (char *)newsuperior_val->data);
653 goto err_dn;
655 } else {
656 newsuperior = ldb_dn_get_parent(tmp_ctx, msg->dn);
657 if (newsuperior == NULL) {
658 ldb_debug(ldb, LDB_DEBUG_ERROR,
659 "Error: Unable to get parent dn '%s'",
660 ldb_dn_get_linearized(msg->dn));
661 goto err_dn;
665 newdn = ldb_dn_copy(tmp_ctx, newrdn);
666 if (newdn == NULL) {
667 ldb_debug(ldb, LDB_DEBUG_ERROR,
668 "Error: failed to copy newrdn '%s'",
669 ldb_dn_get_linearized(newrdn));
670 goto err_op;
673 ok = ldb_dn_add_base(newdn, newsuperior);
674 if (!ok) {
675 ldb_debug(ldb, LDB_DEBUG_ERROR,
676 "Error: failed to base '%s' to newdn '%s'",
677 ldb_dn_get_linearized(newsuperior),
678 ldb_dn_get_linearized(newdn));
679 goto err_op;
682 if (_olddn) {
683 *_olddn = talloc_move(mem_ctx, &olddn);
685 if (_newrdn) {
686 *_newrdn = talloc_move(mem_ctx, &newrdn);
688 if (_deleteoldrdn) {
689 *_deleteoldrdn = deleteoldrdn;
691 if (_newsuperior) {
692 if (newsuperior_val) {
693 *_newrdn = talloc_move(mem_ctx, &newrdn);
694 } else {
695 *_newrdn = NULL;
698 if (_newdn) {
699 *_newdn = talloc_move(mem_ctx, &newdn);
702 talloc_free(tmp_ctx);
703 return LDB_SUCCESS;
704 err_other:
705 talloc_free(tmp_ctx);
706 return LDB_ERR_OTHER;
707 err_op:
708 talloc_free(tmp_ctx);
709 return LDB_ERR_OPERATIONS_ERROR;
710 err_attr:
711 talloc_free(tmp_ctx);
712 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
713 err_dn:
714 talloc_free(tmp_ctx);
715 return LDB_ERR_INVALID_DN_SYNTAX;
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;
729 unsigned flags = 0;
731 value.data = NULL;
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) {
738 talloc_free(ldif);
739 return NULL;
742 ldif->changetype = LDB_CHANGETYPE_NONE;
743 msg = ldif->msg;
745 msg->dn = NULL;
746 msg->elements = NULL;
747 msg->num_elements = 0;
749 chunk = next_chunk(ldb, fgetc_fn, private_data);
750 if (!chunk) {
751 goto failed;
753 talloc_steal(ldif, chunk);
755 s = chunk;
757 if (next_attr(ldif, &s, &attr, &value) != 0) {
758 goto failed;
761 /* first line must be a dn */
762 if (ldb_attr_cmp(attr, "dn") != 0) {
763 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'",
764 attr);
765 goto failed;
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'",
772 (char *)value.data);
773 goto failed;
776 while (next_attr(ldif, &s, &attr, &value) == 0) {
777 const struct ldb_schema_attribute *a;
778 struct ldb_message_element *el;
779 int ret, empty = 0;
781 if (ldb_attr_cmp(attr, "changetype") == 0) {
782 int i;
783 for (i=0;ldb_changetypes[i].name;i++) {
784 if (ldb_attr_cmp((char *)value.data, ldb_changetypes[i].name) == 0) {
785 ldif->changetype = ldb_changetypes[i].changetype;
786 break;
789 if (!ldb_changetypes[i].name) {
790 ldb_debug(ldb, LDB_DEBUG_ERROR,
791 "Error: Bad ldif changetype '%s'",(char *)value.data);
793 flags = 0;
794 continue;
797 if (ldb_attr_cmp(attr, "add") == 0) {
798 flags = LDB_FLAG_MOD_ADD;
799 empty = 1;
801 if (ldb_attr_cmp(attr, "delete") == 0) {
802 flags = LDB_FLAG_MOD_DELETE;
803 empty = 1;
805 if (ldb_attr_cmp(attr, "replace") == 0) {
806 flags = LDB_FLAG_MOD_REPLACE;
807 empty = 1;
809 if (ldb_attr_cmp(attr, "-") == 0) {
810 flags = 0;
811 continue;
814 if (empty) {
815 if (ldb_msg_add_empty(msg, (char *)value.data, flags, NULL) != 0) {
816 goto failed;
818 continue;
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 */
828 el->values =
829 talloc_realloc(msg->elements, el->values,
830 struct ldb_val, el->num_values+1);
831 if (!el->values) {
832 goto failed;
834 ret = a->syntax->ldif_read_fn(ldb, el->values, &value, &el->values[el->num_values]);
835 if (ret != 0) {
836 goto failed;
838 if (value.length == 0) {
839 ldb_debug(ldb, LDB_DEBUG_ERROR,
840 "Error: Attribute value cannot be empty for attribute '%s'", el->name);
841 goto failed;
843 if (value.data != el->values[el->num_values].data) {
844 talloc_steal(el->values, el->values[el->num_values].data);
846 el->num_values++;
847 } else {
848 /* its a new attribute */
849 msg->elements = talloc_realloc(msg, msg->elements,
850 struct ldb_message_element,
851 msg->num_elements+1);
852 if (!msg->elements) {
853 goto failed;
855 el = &msg->elements[msg->num_elements];
856 el->flags = flags;
857 el->name = talloc_strdup(msg->elements, attr);
858 el->values = talloc(msg->elements, struct ldb_val);
859 if (!el->values || !el->name) {
860 goto failed;
862 el->num_values = 1;
863 ret = a->syntax->ldif_read_fn(ldb, el->values, &value, &el->values[0]);
864 if (ret != 0) {
865 goto failed;
867 if (value.data != el->values[0].data) {
868 talloc_steal(el->values, el->values[0].data);
870 msg->num_elements++;
874 if (ldif->changetype == LDB_CHANGETYPE_MODRDN) {
875 int ret;
877 ret = ldb_ldif_parse_modrdn(ldb, ldif, ldif,
878 NULL, NULL, NULL, NULL, NULL);
879 if (ret != LDB_SUCCESS) {
880 goto failed;
884 return ldif;
886 failed:
887 talloc_free(ldif);
888 return NULL;
894 a wrapper around ldif_read() for reading from FILE*
897 static int fgetc_file(void *private_data)
899 int c;
900 struct ldif_read_file_state *state =
901 (struct ldif_read_file_state *)private_data;
902 c = fgetc(state->f);
903 if (c == '\n') {
904 state->line_no++;
906 return c;
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;
918 state.f = f;
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 {
926 const char *s;
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) {
934 return *state->s++;
936 return EOF;
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;
943 state.s = *s;
944 ldif = ldb_ldif_read(ldb, fgetc_string, &state);
945 *s = state.s;
946 return ldif;
951 wrapper around ldif_write() for a file
953 struct ldif_write_file_state {
954 FILE *f;
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;
963 int ret;
964 va_list ap;
966 va_start(ap, fmt);
967 ret = vfprintf(state->f, fmt, ap);
968 va_end(ap);
969 return ret;
972 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
974 struct ldif_write_file_state state;
975 state.f = f;
976 return ldb_ldif_write(ldb, fprintf_file, &state, ldif);
980 wrapper around ldif_write() for a string
982 struct ldif_write_string_state {
983 char *string;
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;
992 va_list ap;
993 size_t oldlen = talloc_get_size(state->string);
994 va_start(ap, fmt);
996 state->string = talloc_vasprintf_append(state->string, fmt, ap);
997 va_end(ap);
998 if (!state->string) {
999 return -1;
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) {
1011 return NULL;
1013 if (ldb_ldif_write(ldb, ldif_printf_string, &state, ldif) == -1) {
1014 return NULL;
1016 return state.string;
1020 convenient function to turn a ldb_message into a string. Useful for
1021 debugging
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);