s4-dsdb/samldb: Skip 'sAMAccountType' and 'primaryGroupID' during Tombstone reanimate
[Samba.git] / lib / ldb / common / ldb_ldif.c
blobf9743dca8b78b105dd71c9aefa3fa3229b8a55cc
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, and only printing secrets if we are not in a trace
275 static int ldb_ldif_write_trace(struct ldb_context *ldb,
276 int (*fprintf_fn)(void *, const char *, ...),
277 void *private_data,
278 const struct ldb_ldif *ldif,
279 bool in_trace)
281 TALLOC_CTX *mem_ctx;
282 unsigned int i, j;
283 int total=0, ret;
284 char *p;
285 const struct ldb_message *msg;
286 const char * const * secret_attributes = ldb_get_opaque(ldb, LDB_SECRET_ATTRIBUTE_LIST_OPAQUE);
288 mem_ctx = talloc_named_const(NULL, 0, "ldb_ldif_write");
290 msg = ldif->msg;
291 p = ldb_dn_get_extended_linearized(mem_ctx, msg->dn, 1);
292 ret = fprintf_fn(private_data, "dn: %s\n", p);
293 talloc_free(p);
294 CHECK_RET;
296 if (ldif->changetype != LDB_CHANGETYPE_NONE) {
297 for (i=0;ldb_changetypes[i].name;i++) {
298 if (ldb_changetypes[i].changetype == ldif->changetype) {
299 break;
302 if (!ldb_changetypes[i].name) {
303 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d",
304 ldif->changetype);
305 talloc_free(mem_ctx);
306 return -1;
308 ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);
309 CHECK_RET;
312 for (i=0;i<msg->num_elements;i++) {
313 const struct ldb_schema_attribute *a;
315 if (msg->elements[i].name == NULL) {
316 ldb_debug(ldb, LDB_DEBUG_ERROR,
317 "Error: Invalid element name (NULL) at position %d", i);
318 talloc_free(mem_ctx);
319 return -1;
322 a = ldb_schema_attribute_by_name(ldb, msg->elements[i].name);
324 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
325 switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {
326 case LDB_FLAG_MOD_ADD:
327 fprintf_fn(private_data, "add: %s\n",
328 msg->elements[i].name);
329 break;
330 case LDB_FLAG_MOD_DELETE:
331 fprintf_fn(private_data, "delete: %s\n",
332 msg->elements[i].name);
333 break;
334 case LDB_FLAG_MOD_REPLACE:
335 fprintf_fn(private_data, "replace: %s\n",
336 msg->elements[i].name);
337 break;
341 if (in_trace && secret_attributes && ldb_attr_in_list(secret_attributes, msg->elements[i].name)) {
342 /* Deliberatly skip printing this password */
343 ret = fprintf_fn(private_data, "# %s::: REDACTED SECRET ATTRIBUTE\n",
344 msg->elements[i].name);
345 CHECK_RET;
346 continue;
349 for (j=0;j<msg->elements[i].num_values;j++) {
350 struct ldb_val v;
351 bool use_b64_encode;
352 ret = a->syntax->ldif_write_fn(ldb, mem_ctx, &msg->elements[i].values[j], &v);
353 if (ret != LDB_SUCCESS) {
354 v = msg->elements[i].values[j];
356 use_b64_encode = !(ldb->flags & LDB_FLG_SHOW_BINARY)
357 && ldb_should_b64_encode(ldb, &v);
358 if (ret != LDB_SUCCESS || use_b64_encode) {
359 ret = fprintf_fn(private_data, "%s:: ",
360 msg->elements[i].name);
361 CHECK_RET;
362 ret = base64_encode_f(ldb, fprintf_fn, private_data,
363 (char *)v.data, v.length,
364 strlen(msg->elements[i].name)+3);
365 CHECK_RET;
366 ret = fprintf_fn(private_data, "\n");
367 CHECK_RET;
368 } else {
369 ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);
370 CHECK_RET;
371 if (ldb->flags & LDB_FLG_SHOW_BINARY) {
372 ret = fprintf_fn(private_data, "%*.*s",
373 v.length, v.length, (char *)v.data);
374 } else {
375 ret = fold_string(fprintf_fn, private_data,
376 (char *)v.data, v.length,
377 strlen(msg->elements[i].name)+2);
379 CHECK_RET;
380 ret = fprintf_fn(private_data, "\n");
381 CHECK_RET;
383 if (v.data != msg->elements[i].values[j].data) {
384 talloc_free(v.data);
387 if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {
388 fprintf_fn(private_data, "-\n");
391 ret = fprintf_fn(private_data,"\n");
392 CHECK_RET;
394 talloc_free(mem_ctx);
396 return total;
399 #undef CHECK_RET
403 write to ldif, using a caller supplied write method
405 int ldb_ldif_write(struct ldb_context *ldb,
406 int (*fprintf_fn)(void *, const char *, ...),
407 void *private_data,
408 const struct ldb_ldif *ldif)
410 return ldb_ldif_write_trace(ldb, fprintf_fn, private_data, ldif, false);
415 pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF
416 this routine removes any RFC2849 continuations and comments
418 caller frees
420 static char *next_chunk(struct ldb_context *ldb,
421 int (*fgetc_fn)(void *), void *private_data)
423 size_t alloc_size=0, chunk_size = 0;
424 char *chunk = NULL;
425 int c;
426 int in_comment = 0;
428 while ((c = fgetc_fn(private_data)) != EOF) {
429 if (chunk_size+1 >= alloc_size) {
430 char *c2;
431 alloc_size += 1024;
432 c2 = talloc_realloc(ldb, chunk, char, alloc_size);
433 if (!c2) {
434 talloc_free(chunk);
435 errno = ENOMEM;
436 return NULL;
438 chunk = c2;
441 if (in_comment) {
442 if (c == '\n') {
443 in_comment = 0;
445 continue;
448 /* handle continuation lines - see RFC2849 */
449 if (c == ' ' && chunk_size > 1 && chunk[chunk_size-1] == '\n') {
450 chunk_size--;
451 continue;
454 /* chunks are terminated by a double line-feed */
455 if (c == '\n' && chunk_size > 0 && chunk[chunk_size-1] == '\n') {
456 chunk[chunk_size-1] = 0;
457 return chunk;
460 if (c == '#' && (chunk_size == 0 || chunk[chunk_size-1] == '\n')) {
461 in_comment = 1;
462 continue;
465 /* ignore leading blank lines */
466 if (chunk_size == 0 && c == '\n') {
467 continue;
470 chunk[chunk_size++] = c;
473 if (chunk) {
474 chunk[chunk_size] = 0;
477 return chunk;
481 /* simple ldif attribute parser */
482 static int next_attr(TALLOC_CTX *mem_ctx, char **s, const char **attr, struct ldb_val *value)
484 char *p;
485 int base64_encoded = 0;
486 int binary_file = 0;
488 if (strncmp(*s, "-\n", 2) == 0) {
489 value->length = 0;
490 *attr = "-";
491 *s += 2;
492 return 0;
495 p = strchr(*s, ':');
496 if (!p) {
497 return -1;
500 *p++ = 0;
502 if (*p == ':') {
503 base64_encoded = 1;
504 p++;
507 if (*p == '<') {
508 binary_file = 1;
509 p++;
512 *attr = *s;
514 while (*p == ' ' || *p == '\t') {
515 p++;
518 value->data = (uint8_t *)p;
520 p = strchr(p, '\n');
522 if (!p) {
523 value->length = strlen((char *)value->data);
524 *s = ((char *)value->data) + value->length;
525 } else {
526 value->length = p - (char *)value->data;
527 *s = p+1;
528 *p = 0;
531 if (base64_encoded) {
532 int len = ldb_base64_decode((char *)value->data);
533 if (len == -1) {
534 /* it wasn't valid base64 data */
535 return -1;
537 value->length = len;
540 if (binary_file) {
541 int len = ldb_read_data_file(mem_ctx, value);
542 if (len == -1) {
543 /* an error occurred while trying to retrieve the file */
544 return -1;
548 return 0;
553 free a message from a ldif_read
555 void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif)
557 talloc_free(ldif);
560 int ldb_ldif_parse_modrdn(struct ldb_context *ldb,
561 const struct ldb_ldif *ldif,
562 TALLOC_CTX *mem_ctx,
563 struct ldb_dn **_olddn,
564 struct ldb_dn **_newrdn,
565 bool *_deleteoldrdn,
566 struct ldb_dn **_newsuperior,
567 struct ldb_dn **_newdn)
569 struct ldb_message *msg = ldif->msg;
570 struct ldb_val *newrdn_val = NULL;
571 struct ldb_val *deleteoldrdn_val = NULL;
572 struct ldb_val *newsuperior_val = NULL;
573 struct ldb_dn *olddn = NULL;
574 struct ldb_dn *newrdn = NULL;
575 bool deleteoldrdn = true;
576 struct ldb_dn *newsuperior = NULL;
577 struct ldb_dn *newdn = NULL;
578 struct ldb_val tmp_false;
579 struct ldb_val tmp_true;
580 bool ok;
581 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
583 if (tmp_ctx == NULL) {
584 ldb_debug(ldb, LDB_DEBUG_FATAL,
585 "Error: talloc_new() failed");
586 goto err_op;
589 if (ldif->changetype != LDB_CHANGETYPE_MODRDN) {
590 ldb_debug(ldb, LDB_DEBUG_ERROR,
591 "Error: invalid changetype '%d'",
592 ldif->changetype);
593 goto err_other;
596 if (msg->num_elements < 2) {
597 ldb_debug(ldb, LDB_DEBUG_ERROR,
598 "Error: num_elements[%u] < 2",
599 msg->num_elements);
600 goto err_other;
603 if (msg->num_elements > 3) {
604 ldb_debug(ldb, LDB_DEBUG_ERROR,
605 "Error: num_elements[%u] > 3",
606 msg->num_elements);
607 goto err_other;
610 #define CHECK_ELEMENT(i, _name, v, needed) do { \
611 v = NULL; \
612 if (msg->num_elements < (i + 1)) { \
613 if (needed) { \
614 ldb_debug(ldb, LDB_DEBUG_ERROR, \
615 "Error: num_elements[%u] < (%u + 1)", \
616 msg->num_elements, i); \
617 goto err_other; \
619 } else if (ldb_attr_cmp(msg->elements[i].name, _name) != 0) { \
620 ldb_debug(ldb, LDB_DEBUG_ERROR, \
621 "Error: elements[%u].name[%s] != [%s]", \
622 i, msg->elements[i].name, _name); \
623 goto err_other; \
624 } else if (msg->elements[i].flags != 0) { \
625 ldb_debug(ldb, LDB_DEBUG_ERROR, \
626 "Error: elements[%u].flags[0x%X} != [0x0]", \
627 i, msg->elements[i].flags); \
628 goto err_other; \
629 } else if (msg->elements[i].num_values != 1) { \
630 ldb_debug(ldb, LDB_DEBUG_ERROR, \
631 "Error: elements[%u].num_values[%u] != 1", \
632 i, msg->elements[i].num_values); \
633 goto err_other; \
634 } else { \
635 v = &msg->elements[i].values[0]; \
637 } while (0)
639 CHECK_ELEMENT(0, "newrdn", newrdn_val, true);
640 CHECK_ELEMENT(1, "deleteoldrdn", deleteoldrdn_val, true);
641 CHECK_ELEMENT(2, "newsuperior", newsuperior_val, false);
643 #undef CHECK_ELEMENT
645 olddn = ldb_dn_copy(tmp_ctx, msg->dn);
646 if (olddn == NULL) {
647 ldb_debug(ldb, LDB_DEBUG_ERROR,
648 "Error: failed to copy olddn '%s'",
649 ldb_dn_get_linearized(msg->dn));
650 goto err_op;
653 newrdn = ldb_dn_from_ldb_val(tmp_ctx, ldb, newrdn_val);
654 if (!ldb_dn_validate(newrdn)) {
655 ldb_debug(ldb, LDB_DEBUG_ERROR,
656 "Error: Unable to parse dn '%s'",
657 (char *)newrdn_val->data);
658 goto err_dn;
661 tmp_false.length = 1;
662 tmp_false.data = discard_const_p(uint8_t, "0");
663 tmp_true.length = 1;
664 tmp_true.data = discard_const_p(uint8_t, "1");
665 if (ldb_val_equal_exact(deleteoldrdn_val, &tmp_false) == 1) {
666 deleteoldrdn = false;
667 } else if (ldb_val_equal_exact(deleteoldrdn_val, &tmp_true) == 1) {
668 deleteoldrdn = true;
669 } else {
670 ldb_debug(ldb, LDB_DEBUG_ERROR,
671 "Error: deleteoldrdn value invalid '%s' not '0'/'1'",
672 (char *)deleteoldrdn_val->data);
673 goto err_attr;
676 if (newsuperior_val) {
677 newsuperior = ldb_dn_from_ldb_val(tmp_ctx, ldb, newsuperior_val);
678 if (!ldb_dn_validate(newsuperior)) {
679 ldb_debug(ldb, LDB_DEBUG_ERROR,
680 "Error: Unable to parse dn '%s'",
681 (char *)newsuperior_val->data);
682 goto err_dn;
684 } else {
685 newsuperior = ldb_dn_get_parent(tmp_ctx, msg->dn);
686 if (newsuperior == NULL) {
687 ldb_debug(ldb, LDB_DEBUG_ERROR,
688 "Error: Unable to get parent dn '%s'",
689 ldb_dn_get_linearized(msg->dn));
690 goto err_dn;
694 newdn = ldb_dn_copy(tmp_ctx, newrdn);
695 if (newdn == NULL) {
696 ldb_debug(ldb, LDB_DEBUG_ERROR,
697 "Error: failed to copy newrdn '%s'",
698 ldb_dn_get_linearized(newrdn));
699 goto err_op;
702 ok = ldb_dn_add_base(newdn, newsuperior);
703 if (!ok) {
704 ldb_debug(ldb, LDB_DEBUG_ERROR,
705 "Error: failed to base '%s' to newdn '%s'",
706 ldb_dn_get_linearized(newsuperior),
707 ldb_dn_get_linearized(newdn));
708 goto err_op;
711 if (_olddn) {
712 *_olddn = talloc_move(mem_ctx, &olddn);
714 if (_newrdn) {
715 *_newrdn = talloc_move(mem_ctx, &newrdn);
717 if (_deleteoldrdn) {
718 *_deleteoldrdn = deleteoldrdn;
720 if (_newsuperior) {
721 if (newsuperior_val) {
722 *_newrdn = talloc_move(mem_ctx, &newrdn);
723 } else {
724 *_newrdn = NULL;
727 if (_newdn) {
728 *_newdn = talloc_move(mem_ctx, &newdn);
731 talloc_free(tmp_ctx);
732 return LDB_SUCCESS;
733 err_other:
734 talloc_free(tmp_ctx);
735 return LDB_ERR_OTHER;
736 err_op:
737 talloc_free(tmp_ctx);
738 return LDB_ERR_OPERATIONS_ERROR;
739 err_attr:
740 talloc_free(tmp_ctx);
741 return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;
742 err_dn:
743 talloc_free(tmp_ctx);
744 return LDB_ERR_INVALID_DN_SYNTAX;
748 read from a LDIF source, creating a ldb_message
750 struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb,
751 int (*fgetc_fn)(void *), void *private_data)
753 struct ldb_ldif *ldif;
754 struct ldb_message *msg;
755 const char *attr=NULL;
756 char *chunk=NULL, *s;
757 struct ldb_val value;
758 unsigned flags = 0;
759 value.data = NULL;
761 ldif = talloc(ldb, struct ldb_ldif);
762 if (!ldif) return NULL;
764 ldif->msg = talloc(ldif, struct ldb_message);
765 if (ldif->msg == NULL) {
766 talloc_free(ldif);
767 return NULL;
770 ldif->changetype = LDB_CHANGETYPE_NONE;
771 msg = ldif->msg;
773 msg->dn = NULL;
774 msg->elements = NULL;
775 msg->num_elements = 0;
777 chunk = next_chunk(ldb, fgetc_fn, private_data);
778 if (!chunk) {
779 goto failed;
781 talloc_steal(ldif, chunk);
783 s = chunk;
785 if (next_attr(ldif, &s, &attr, &value) != 0) {
786 goto failed;
789 /* first line must be a dn */
790 if (ldb_attr_cmp(attr, "dn") != 0) {
791 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: First line of ldif must be a dn not '%s'",
792 attr);
793 goto failed;
796 msg->dn = ldb_dn_from_ldb_val(msg, ldb, &value);
798 if ( ! ldb_dn_validate(msg->dn)) {
799 ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Unable to parse dn '%s'",
800 (char *)value.data);
801 goto failed;
804 while (next_attr(ldif, &s, &attr, &value) == 0) {
805 const struct ldb_schema_attribute *a;
806 struct ldb_message_element *el;
807 int ret, empty = 0;
809 if (ldb_attr_cmp(attr, "changetype") == 0) {
810 int i;
811 for (i=0;ldb_changetypes[i].name;i++) {
812 if (ldb_attr_cmp((char *)value.data, ldb_changetypes[i].name) == 0) {
813 ldif->changetype = ldb_changetypes[i].changetype;
814 break;
817 if (!ldb_changetypes[i].name) {
818 ldb_debug(ldb, LDB_DEBUG_ERROR,
819 "Error: Bad ldif changetype '%s'",(char *)value.data);
821 flags = 0;
822 continue;
825 if (ldb_attr_cmp(attr, "add") == 0) {
826 flags = LDB_FLAG_MOD_ADD;
827 empty = 1;
829 if (ldb_attr_cmp(attr, "delete") == 0) {
830 flags = LDB_FLAG_MOD_DELETE;
831 empty = 1;
833 if (ldb_attr_cmp(attr, "replace") == 0) {
834 flags = LDB_FLAG_MOD_REPLACE;
835 empty = 1;
837 if (ldb_attr_cmp(attr, "-") == 0) {
838 flags = 0;
839 continue;
842 if (empty) {
843 if (ldb_msg_add_empty(msg, (char *)value.data, flags, NULL) != 0) {
844 goto failed;
846 continue;
849 el = &msg->elements[msg->num_elements-1];
851 a = ldb_schema_attribute_by_name(ldb, attr);
853 if (msg->num_elements > 0 && ldb_attr_cmp(attr, el->name) == 0 &&
854 flags == el->flags) {
855 /* its a continuation */
856 el->values =
857 talloc_realloc(msg->elements, el->values,
858 struct ldb_val, el->num_values+1);
859 if (!el->values) {
860 goto failed;
862 ret = a->syntax->ldif_read_fn(ldb, el->values, &value, &el->values[el->num_values]);
863 if (ret != 0) {
864 goto failed;
866 if (value.length == 0) {
867 ldb_debug(ldb, LDB_DEBUG_ERROR,
868 "Error: Attribute value cannot be empty for attribute '%s'", el->name);
869 goto failed;
871 if (value.data != el->values[el->num_values].data) {
872 talloc_steal(el->values, el->values[el->num_values].data);
874 el->num_values++;
875 } else {
876 /* its a new attribute */
877 msg->elements = talloc_realloc(msg, msg->elements,
878 struct ldb_message_element,
879 msg->num_elements+1);
880 if (!msg->elements) {
881 goto failed;
883 el = &msg->elements[msg->num_elements];
884 el->flags = flags;
885 el->name = talloc_strdup(msg->elements, attr);
886 el->values = talloc(msg->elements, struct ldb_val);
887 if (!el->values || !el->name) {
888 goto failed;
890 el->num_values = 1;
891 ret = a->syntax->ldif_read_fn(ldb, el->values, &value, &el->values[0]);
892 if (ret != 0) {
893 goto failed;
895 if (value.data != el->values[0].data) {
896 talloc_steal(el->values, el->values[0].data);
898 msg->num_elements++;
902 if (ldif->changetype == LDB_CHANGETYPE_MODRDN) {
903 int ret;
905 ret = ldb_ldif_parse_modrdn(ldb, ldif, ldif,
906 NULL, NULL, NULL, NULL, NULL);
907 if (ret != LDB_SUCCESS) {
908 goto failed;
912 return ldif;
914 failed:
915 talloc_free(ldif);
916 return NULL;
922 a wrapper around ldif_read() for reading from FILE*
925 static int fgetc_file(void *private_data)
927 int c;
928 struct ldif_read_file_state *state =
929 (struct ldif_read_file_state *)private_data;
930 c = fgetc(state->f);
931 if (c == '\n') {
932 state->line_no++;
934 return c;
937 struct ldb_ldif *ldb_ldif_read_file_state(struct ldb_context *ldb,
938 struct ldif_read_file_state *state)
940 return ldb_ldif_read(ldb, fgetc_file, state);
943 struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f)
945 struct ldif_read_file_state state;
946 state.f = f;
947 return ldb_ldif_read_file_state(ldb, &state);
951 a wrapper around ldif_read() for reading from const char*
953 struct ldif_read_string_state {
954 const char *s;
957 static int fgetc_string(void *private_data)
959 struct ldif_read_string_state *state =
960 (struct ldif_read_string_state *)private_data;
961 if (state->s[0] != 0) {
962 return *state->s++;
964 return EOF;
967 struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char **s)
969 struct ldif_read_string_state state;
970 struct ldb_ldif *ldif;
971 state.s = *s;
972 ldif = ldb_ldif_read(ldb, fgetc_string, &state);
973 *s = state.s;
974 return ldif;
979 wrapper around ldif_write() for a file
981 struct ldif_write_file_state {
982 FILE *f;
985 static int fprintf_file(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
987 static int fprintf_file(void *private_data, const char *fmt, ...)
989 struct ldif_write_file_state *state =
990 (struct ldif_write_file_state *)private_data;
991 int ret;
992 va_list ap;
994 va_start(ap, fmt);
995 ret = vfprintf(state->f, fmt, ap);
996 va_end(ap);
997 return ret;
1000 int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif)
1002 struct ldif_write_file_state state;
1003 state.f = f;
1004 return ldb_ldif_write(ldb, fprintf_file, &state, ldif);
1008 wrapper around ldif_write() for a string
1010 struct ldif_write_string_state {
1011 char *string;
1014 static int ldif_printf_string(void *private_data, const char *fmt, ...) PRINTF_ATTRIBUTE(2, 3);
1016 static int ldif_printf_string(void *private_data, const char *fmt, ...)
1018 struct ldif_write_string_state *state =
1019 (struct ldif_write_string_state *)private_data;
1020 va_list ap;
1021 size_t oldlen = talloc_get_size(state->string);
1022 va_start(ap, fmt);
1024 state->string = talloc_vasprintf_append(state->string, fmt, ap);
1025 va_end(ap);
1026 if (!state->string) {
1027 return -1;
1030 return talloc_get_size(state->string) - oldlen;
1033 char *ldb_ldif_write_redacted_trace_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1034 const struct ldb_ldif *ldif)
1036 struct ldif_write_string_state state;
1037 state.string = talloc_strdup(mem_ctx, "");
1038 if (!state.string) {
1039 return NULL;
1041 if (ldb_ldif_write_trace(ldb, ldif_printf_string, &state, ldif, true) == -1) {
1042 return NULL;
1044 return state.string;
1047 char *ldb_ldif_write_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1048 const struct ldb_ldif *ldif)
1050 struct ldif_write_string_state state;
1051 state.string = talloc_strdup(mem_ctx, "");
1052 if (!state.string) {
1053 return NULL;
1055 if (ldb_ldif_write(ldb, ldif_printf_string, &state, ldif) == -1) {
1056 return NULL;
1058 return state.string;
1062 convenient function to turn a ldb_message into a string. Useful for
1063 debugging
1065 char *ldb_ldif_message_string(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
1066 enum ldb_changetype changetype,
1067 const struct ldb_message *msg)
1069 struct ldb_ldif ldif;
1071 ldif.changetype = changetype;
1072 ldif.msg = discard_const_p(struct ldb_message, msg);
1074 return ldb_ldif_write_string(ldb, mem_ctx, &ldif);