From f0a8f718ff474009300af6746fa0fbb61c649ea9 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 20 May 2004 13:25:06 +0000 Subject: r792: - changed the ldb ldif_* functions to be in the ldb_ namespace - added better error reporting in ldbdel - fixed a bug in handling packing of records which contain elements with no values (it caused db corruption) - allow search with "dn" as target attribute (This used to be commit 36575396234e3d35dbd442c8f1ff54a17ae64e64) --- source4/lib/ldb/common/ldb_ldif.c | 28 ++++++++++++++-------------- source4/lib/ldb/common/ldb_parse.c | 1 + source4/lib/ldb/include/ldb.h | 23 ++++++++++------------- source4/lib/ldb/ldb_tdb/ldb_index.c | 1 + source4/lib/ldb/ldb_tdb/ldb_match.c | 1 + source4/lib/ldb/ldb_tdb/ldb_pack.c | 20 +++++++++++++++++--- source4/lib/ldb/ldb_tdb/ldb_search.c | 28 +++++++++++++++++++++++++++- source4/lib/ldb/tools/ldbadd.c | 4 ++-- source4/lib/ldb/tools/ldbdel.c | 3 ++- source4/lib/ldb/tools/ldbedit.c | 4 ++-- source4/lib/ldb/tools/ldbmodify.c | 4 ++-- source4/lib/ldb/tools/ldbsearch.c | 2 +- 12 files changed, 80 insertions(+), 39 deletions(-) diff --git a/source4/lib/ldb/common/ldb_ldif.c b/source4/lib/ldb/common/ldb_ldif.c index 513e2dd365..c120ee5f4e 100644 --- a/source4/lib/ldb/common/ldb_ldif.c +++ b/source4/lib/ldb/common/ldb_ldif.c @@ -196,10 +196,10 @@ static const struct { /* write to ldif, using a caller supplied write method */ -int ldif_write(struct ldb_context *ldb, - int (*fprintf_fn)(void *, const char *, ...), - void *private_data, - const struct ldb_ldif *ldif) +int ldb_ldif_write(struct ldb_context *ldb, + int (*fprintf_fn)(void *, const char *, ...), + void *private_data, + const struct ldb_ldif *ldif) { int i, j; int total=0, ret; @@ -407,7 +407,7 @@ static int next_attr(char **s, const char **attr, struct ldb_val *value) /* free a message from a ldif_read */ -void ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif) +void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *ldif) { struct ldb_message *msg = &ldif->msg; int i; @@ -457,8 +457,8 @@ static int msg_add_empty(struct ldb_context *ldb, /* read from a LDIF source, creating a ldb_message */ -struct ldb_ldif *ldif_read(struct ldb_context *ldb, - int (*fgetc_fn)(void *), void *private_data) +struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb, + int (*fgetc_fn)(void *), void *private_data) { struct ldb_ldif *ldif; struct ldb_message *msg; @@ -582,7 +582,7 @@ struct ldb_ldif *ldif_read(struct ldb_context *ldb, return ldif; failed: - if (ldif) ldif_read_free(ldb, ldif); + if (ldif) ldb_ldif_read_free(ldb, ldif); return NULL; } @@ -601,11 +601,11 @@ static int fgetc_file(void *private_data) return fgetc(state->f); } -struct ldb_ldif *ldif_read_file(struct ldb_context *ldb, FILE *f) +struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f) { struct ldif_read_file_state state; state.f = f; - return ldif_read(ldb, fgetc_file, &state); + return ldb_ldif_read(ldb, fgetc_file, &state); } @@ -625,11 +625,11 @@ static int fgetc_string(void *private_data) return EOF; } -struct ldb_ldif *ldif_read_string(struct ldb_context *ldb, const char *s) +struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char *s) { struct ldif_read_string_state state; state.s = s; - return ldif_read(ldb, fgetc_string, &state); + return ldb_ldif_read(ldb, fgetc_string, &state); } @@ -652,9 +652,9 @@ static int fprintf_file(void *private_data, const char *fmt, ...) return ret; } -int ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif) +int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *ldif) { struct ldif_write_file_state state; state.f = f; - return ldif_write(ldb, fprintf_file, &state, ldif); + return ldb_ldif_write(ldb, fprintf_file, &state, ldif); } diff --git a/source4/lib/ldb/common/ldb_parse.c b/source4/lib/ldb/common/ldb_parse.c index 5d2a42fd20..d61d65548c 100644 --- a/source4/lib/ldb/common/ldb_parse.c +++ b/source4/lib/ldb/common/ldb_parse.c @@ -42,6 +42,7 @@ */ #include "includes.h" +#include "ldb/include/ldb_parse.h" /* diff --git a/source4/lib/ldb/include/ldb.h b/source4/lib/ldb/include/ldb.h index 7215bf5705..448d5607a8 100644 --- a/source4/lib/ldb/include/ldb.h +++ b/source4/lib/ldb/include/ldb.h @@ -60,9 +60,6 @@ struct ldb_val { void *data; }; -#include "ldb_parse.h" - - /* these flags are used in ldd_message_element.flags fields. The LDA_FLAGS_MOD_* flags are used in ldap_modify() calls to specify whether attributes are being added, deleted or modified */ @@ -256,16 +253,16 @@ char *ldb_casefold(struct ldb_context *ldb, const char *s); /* ldif manipulation functions */ -int ldif_write(struct ldb_context *ldb, - int (*fprintf_fn)(void *, const char *, ...), - void *private_data, - const struct ldb_ldif *ldif); -void ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *); -struct ldb_ldif *ldif_read(struct ldb_context *ldb, - int (*fgetc_fn)(void *), void *private_data); -struct ldb_ldif *ldif_read_file(struct ldb_context *ldb, FILE *f); -struct ldb_ldif *ldif_read_string(struct ldb_context *ldb, const char *s); -int ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *msg); +int ldb_ldif_write(struct ldb_context *ldb, + int (*fprintf_fn)(void *, const char *, ...), + void *private_data, + const struct ldb_ldif *ldif); +void ldb_ldif_read_free(struct ldb_context *ldb, struct ldb_ldif *); +struct ldb_ldif *ldb_ldif_read(struct ldb_context *ldb, + int (*fgetc_fn)(void *), void *private_data); +struct ldb_ldif *ldb_ldif_read_file(struct ldb_context *ldb, FILE *f); +struct ldb_ldif *ldb_ldif_read_string(struct ldb_context *ldb, const char *s); +int ldb_ldif_write_file(struct ldb_context *ldb, FILE *f, const struct ldb_ldif *msg); /* useful functions for ldb_message structure manipulation */ diff --git a/source4/lib/ldb/ldb_tdb/ldb_index.c b/source4/lib/ldb/ldb_tdb/ldb_index.c index 3febdaa711..b4ca666287 100644 --- a/source4/lib/ldb/ldb_tdb/ldb_index.c +++ b/source4/lib/ldb/ldb_tdb/ldb_index.c @@ -34,6 +34,7 @@ #include "includes.h" #include "ldb/ldb_tdb/ldb_tdb.h" +#include "ldb/include/ldb_parse.h" struct dn_list { unsigned int count; diff --git a/source4/lib/ldb/ldb_tdb/ldb_match.c b/source4/lib/ldb/ldb_tdb/ldb_match.c index 80d147cb43..05a2826d4d 100644 --- a/source4/lib/ldb/ldb_tdb/ldb_match.c +++ b/source4/lib/ldb/ldb_tdb/ldb_match.c @@ -34,6 +34,7 @@ #include "includes.h" #include "ldb/ldb_tdb/ldb_tdb.h" +#include "ldb/include/ldb_parse.h" /* diff --git a/source4/lib/ldb/ldb_tdb/ldb_pack.c b/source4/lib/ldb/ldb_tdb/ldb_pack.c index 8d1051be94..e71679646e 100644 --- a/source4/lib/ldb/ldb_tdb/ldb_pack.c +++ b/source4/lib/ldb/ldb_tdb/ldb_pack.c @@ -69,11 +69,17 @@ int ltdb_pack_data(struct ldb_context *ldb, const struct ldb_message *message, struct TDB_DATA *data) { - int i, j; + int i, j, real_elements=0; size_t size; char *p; size_t len; + for (i=0;inum_elements;i++) { + if (message->elements[i].num_values != 0) { + real_elements++; + } + } + /* work out how big it needs to be */ size = 8; @@ -99,7 +105,7 @@ int ltdb_pack_data(struct ldb_context *ldb, p = data->dptr; put_uint32(p, 0, LTDB_PACKING_FORMAT); - put_uint32(p, 4, message->num_elements); + put_uint32(p, 4, real_elements); p += 8; /* the dn needs to be packed so we can be case preserving @@ -211,12 +217,14 @@ int ltdb_unpack_data(struct ldb_context *ldb, message->elements = ldb_malloc_array_p(ldb, struct ldb_message_element, message->num_elements); - if (!message->elements) { errno = ENOMEM; goto failed; } + memset(message->elements, 0, + message->num_elements * sizeof(struct ldb_message_element)); + for (i=0;inum_elements;i++) { if (remaining < 10) { errno = EIO; @@ -243,6 +251,7 @@ int ltdb_unpack_data(struct ldb_context *ldb, } } p += 4; + remaining -= 4; for (j=0;jelements[i].num_values;j++) { len = pull_uint32(p, 0); if (len > remaining-5) { @@ -257,6 +266,11 @@ int ltdb_unpack_data(struct ldb_context *ldb, } } + if (remaining != 0) { + ldb_debug(ldb, LDB_DEBUG_ERROR, + "Error: %d bytes unread in ltdb_unpack_data\n", remaining); + } + return 0; failed: diff --git a/source4/lib/ldb/ldb_tdb/ldb_search.c b/source4/lib/ldb/ldb_tdb/ldb_search.c index 6b38a28296..d97444a6e8 100644 --- a/source4/lib/ldb/ldb_tdb/ldb_search.c +++ b/source4/lib/ldb/ldb_tdb/ldb_search.c @@ -34,6 +34,7 @@ #include "includes.h" #include "ldb/ldb_tdb/ldb_tdb.h" +#include "ldb/include/ldb_parse.h" /* free a message that has all parts separately allocated @@ -192,6 +193,31 @@ static struct ldb_message *ltdb_pull_attrs(struct ldb_context *ldb, continue; } + if (ldb_attr_cmp(attrs[i], "dn") == 0) { + struct ldb_message_element el2; + struct ldb_val val; + + el2.flags = 0; + el2.name = ldb_strdup(ldb, "dn"); + if (!el2.name) { + msg_free_all_parts(ldb, ret); + ldb_free(ldb, el2.name); + return NULL; + } + el2.num_values = 1; + el2.values = &val; + val.data = ret->dn; + val.length = strlen(ret->dn); + + if (msg_add_element(ldb, ret, &el2) != 0) { + msg_free_all_parts(ldb, ret); + ldb_free(ldb, el2.name); + return NULL; + } + ldb_free(ldb, el2.name); + continue; + } + el = ldb_msg_find_element(msg, attrs[i]); if (!el) { continue; @@ -290,7 +316,7 @@ int ltdb_search_dn1(struct ldb_context *ldb, const char *dn, struct ldb_message ret = ltdb_unpack_data(ldb, &tdb_data2, msg); if (ret == -1) { - free(tdb_data2.dptr); + ldb_free(ldb, tdb_data2.dptr); return -1; } diff --git a/source4/lib/ldb/tools/ldbadd.c b/source4/lib/ldb/tools/ldbadd.c index a45021c1d9..9383197ed0 100644 --- a/source4/lib/ldb/tools/ldbadd.c +++ b/source4/lib/ldb/tools/ldbadd.c @@ -55,7 +55,7 @@ static int process_file(struct ldb_context *ldb, FILE *f) struct ldb_ldif *ldif; int ret, count=0; - while ((ldif = ldif_read_file(ldb, f))) { + while ((ldif = ldb_ldif_read_file(ldb, f))) { if (ldif->changetype != LDB_CHANGETYPE_ADD && ldif->changetype != LDB_CHANGETYPE_NONE) { fprintf(stderr, "Only CHANGETYPE_ADD records allowed\n"); @@ -70,7 +70,7 @@ static int process_file(struct ldb_context *ldb, FILE *f) } else { count++; } - ldif_read_free(ldb, ldif); + ldb_ldif_read_free(ldb, ldif); } return count; diff --git a/source4/lib/ldb/tools/ldbdel.c b/source4/lib/ldb/tools/ldbdel.c index 880713b25a..2977357ced 100644 --- a/source4/lib/ldb/tools/ldbdel.c +++ b/source4/lib/ldb/tools/ldbdel.c @@ -90,7 +90,8 @@ static void usage(void) for (i=0;ichangetype) { case LDB_CHANGETYPE_NONE: case LDB_CHANGETYPE_ADD: @@ -74,7 +74,7 @@ static int process_file(struct ldb_context *ldb, FILE *f) } else { count++; } - ldif_read_free(ldb, ldif); + ldb_ldif_read_free(ldb, ldif); } return count; diff --git a/source4/lib/ldb/tools/ldbsearch.c b/source4/lib/ldb/tools/ldbsearch.c index 478601ec7e..137299d89b 100644 --- a/source4/lib/ldb/tools/ldbsearch.c +++ b/source4/lib/ldb/tools/ldbsearch.c @@ -69,7 +69,7 @@ static int do_search(struct ldb_context *ldb, ldif.changetype = LDB_CHANGETYPE_NONE; ldif.msg = *msgs[i]; - ldif_write_file(ldb, stdout, &ldif); + ldb_ldif_write_file(ldb, stdout, &ldif); } if (ret > 0) { -- cgit