r792: - changed the ldb ldif_* functions to be in the ldb_ namespace
authorAndrew Tridgell <tridge@samba.org>
Thu, 20 May 2004 13:25:06 +0000 (13:25 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 17:53:50 +0000 (12:53 -0500)
 - 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)

12 files changed:
source4/lib/ldb/common/ldb_ldif.c
source4/lib/ldb/common/ldb_parse.c
source4/lib/ldb/include/ldb.h
source4/lib/ldb/ldb_tdb/ldb_index.c
source4/lib/ldb/ldb_tdb/ldb_match.c
source4/lib/ldb/ldb_tdb/ldb_pack.c
source4/lib/ldb/ldb_tdb/ldb_search.c
source4/lib/ldb/tools/ldbadd.c
source4/lib/ldb/tools/ldbdel.c
source4/lib/ldb/tools/ldbedit.c
source4/lib/ldb/tools/ldbmodify.c
source4/lib/ldb/tools/ldbsearch.c

index 513e2dd3655171df230011257fc0fc8467bf3d19..c120ee5f4eeb6c4c2a06f88a36d1d3cb1d158d78 100644 (file)
@@ -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);
 }
index 5d2a42fd20dde8425b683643b493524dddfb6974..d61d65548ca96669c89b399c8192745a052f2542 100644 (file)
@@ -42,6 +42,7 @@
 */
 
 #include "includes.h"
+#include "ldb/include/ldb_parse.h"
 
 
 /*
index 7215bf5705be4c8175f0d34e368eccdbee66a59f..448d5607a8db6bde2bc781deea434f246bb294eb 100644 (file)
@@ -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 */
index 3febdaa711bbd761a73349f2803ca71c59e4cdb9..b4ca66628792f656784d68d21406b4997fa39a98 100644 (file)
@@ -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;
index 80d147cb436677cf2f7fe85d2290b01812192a02..05a2826d4d908c37d13d71a8364a65df914128e4 100644 (file)
@@ -34,6 +34,7 @@
 
 #include "includes.h"
 #include "ldb/ldb_tdb/ldb_tdb.h"
+#include "ldb/include/ldb_parse.h"
 
 
 /*
index 8d1051be94c18d5558fcb49fbd2ade97548ab257..e71679646e317c8890137cebfd974b27e421075f 100644 (file)
@@ -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;i<message->num_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;i<message->num_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;j<message->elements[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:
index 6b38a282961d7773c050c56435d4467c2c53abd9..d97444a6e87426b2380ef9ef83c44e452bea965f 100644 (file)
@@ -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;              
        }
 
index a45021c1d965bf3363fc2c2cb022c9cb1cfd1a04..9383197ed037152b627af6f188e1053fd8110936 100644 (file)
@@ -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;
index 880713b25a3a023a8d7b0765bef2eceb7d41af96..2977357ced6f674cac3a12fc2b11ac719728e6b2 100644 (file)
@@ -90,7 +90,8 @@ static void usage(void)
        for (i=0;i<argc;i++) {
                ret = ldb_delete(ldb, argv[i]);
                if (ret != 0) {
-                       printf("delete of '%s' failed\n", argv[i]);
+                       printf("delete of '%s' failed - %s\n", 
+                              argv[i], ldb_errstring(ldb));
                }
        }
 
index 7e417024222713a02e8fc51f8a2f76297f2a0568..f8e0fbb68af2517892e0ba29af42e5ee063b3907 100644 (file)
@@ -173,7 +173,7 @@ static int save_ldif(struct ldb_context *ldb,
                ldif.changetype = LDB_CHANGETYPE_NONE;
                ldif.msg = *msgs[i];
 
-               ldif_write_file(ldb, f, &ldif);
+               ldb_ldif_write_file(ldb, f, &ldif);
        }
 
        return 0;
@@ -243,7 +243,7 @@ static int do_edit(struct ldb_context *ldb, struct ldb_message **msgs1, int coun
                return -1;
        }
 
-       while ((ldif = ldif_read_file(ldb, f))) {
+       while ((ldif = ldb_ldif_read_file(ldb, f))) {
                msgs2 = ldb_realloc_p(ldb, msgs2, struct ldb_message *, count2+1);
                if (!msgs2) {
                        fprintf(stderr, "out of memory");
index 828b7d4865f1f15a852e3547eaed7a9f92e34a61..b6ca2993ce139e74b2f1b55c62b4a814294770ab 100644 (file)
@@ -54,7 +54,7 @@ static int process_file(struct ldb_context *ldb, FILE *f)
        struct ldb_ldif *ldif;
        int ret = -1, count = 0;
        
-       while ((ldif = ldif_read_file(ldb, f))) {
+       while ((ldif = ldb_ldif_read_file(ldb, f))) {
                switch (ldif->changetype) {
                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;
index 478601ec7eb1196c1079c4318557792c574dd88d..137299d89b9ce2502518e9f4d39c3304f8ec92d8 100644 (file)
@@ -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) {