ctdldump/ctdlload: add version number to banner
[citadel.git] / citadel / utils / ctdlload.c
index 3a5f64a6c3d266bb1d3951eb4f7fcba3d011b630..a4bdb50478940fb2406b1d9d16a9a63147ae5770 100644 (file)
@@ -1,6 +1,6 @@
-// Don't run this.  It doesn't work and if you try to run it you will immediately die.
+// Load (restore) the Citadel database from a flat file created by ctdldump
 //
-// Copyright (c) 2023 by Art Cancro citadel.org
+// Copyright (c) 2023-2024 by Art Cancro citadel.org
 //
 // This program is open source software.  Use, duplication, or disclosure
 // is subject to the terms of the GNU General Public License, version 3.
 #include <errno.h>
 #include <stdarg.h>
 #include <limits.h>
+#include <syslog.h>
 #include <libcitadel.h>
 #include <zlib.h>
-#include <db.h>
 #include "../server/sysdep.h"
 #include "../server/citadel_defs.h"
 #include "../server/server.h"
 #include "../server/makeuserkey.h"
 #include "../server/citadel_dirs.h"
+#include "../server/database.h"
 
 uid_t ctdluid = 0;
 
@@ -41,80 +42,9 @@ void *reallok(void *ptr, size_t size) {
 }
 
 
-// Open a database environment
-DB_ENV *open_dbenv(char *dirname) {
-
-       DB_ENV *dbenv = NULL;
-
-       int ret;
-       int i;
-       u_int32_t flags = 0;
-       int dbversion_major, dbversion_minor, dbversion_patch;
-       db_version(&dbversion_major, &dbversion_minor, &dbversion_patch);
-
-       // Create synthetic integer version numbers and compare them.
-       // Never run with a libdb other than the one with which it was compiled.
-       int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
-       int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
-       if (compiled_db_version != linked_db_version) {
-               fprintf(stderr,
-                       "db: ctdlload is running with a version of libdb other than the one with which it was compiled.\n"
-                       "db: compiled: %d\n"
-                       "db:   linked: %d\n"
-                       "db: This is an invalid configuration.  ctdlload will now exit to prevent data loss.",
-                       compiled_db_version,
-                       linked_db_version
-               );
-               exit(CTDLEXIT_DB);
-       }
-
-       ret = db_env_create(&dbenv, 0);
-       if (ret) {
-               fprintf(stderr,"db: db_env_create: %s\n", db_strerror(ret));
-               fprintf(stderr,"db: exit code %d\n", ret);
-               exit(CTDLEXIT_DB);
-       }
-
-       // We want to specify the shared memory buffer pool cachesize, but everything else is the default.
-       ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
-       if (ret) {
-               fprintf(stderr,"db: set_cachesize: %s\n", db_strerror(ret));
-               dbenv->close(dbenv, 0);
-               fprintf(stderr,"db: exit code %d\n", ret);
-               exit(CTDLEXIT_DB);
-       }
-
-       if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
-               fprintf(stderr,"db: set_lk_detect: %s\n", db_strerror(ret));
-               dbenv->close(dbenv, 0);
-               fprintf(stderr,"db: exit code %d\n", ret);
-               exit(CTDLEXIT_DB);
-       }
-
-       flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
-       ret = dbenv->open(dbenv, dirname, flags, 0);
-       if (ret) {
-               fprintf(stderr,"db: dbenv->open: %s\n", db_strerror(ret));
-               dbenv->close(dbenv, 0);
-               fprintf(stderr,"db: exit code %d\n", ret);
-               exit(CTDLEXIT_DB);
-       }
-
-       return(dbenv);
-}
-
-
-void close_dbenv(DB_ENV *dbenv) {
-       int ret = dbenv->close(dbenv, 0);
-       if (ret) {
-               fprintf(stderr,"db: dbenv->close: %s\n", db_strerror(ret));
-       }
-}
-
-
 // Convert a "msgtext" record to a message on disk.   NOT THREADSAFE
 // This also works for "bigmsg" records.
-int import_msgtext(char *line, DBT *out_key, DBT *out_data) {
+int import_msgtext(char *line, struct cdbkeyval *kv) {
 
        static char *b64_decoded_msg = NULL;
        static size_t b64_decoded_alloc = 0;
@@ -126,23 +56,28 @@ int import_msgtext(char *line, DBT *out_key, DBT *out_data) {
        token = strtok(NULL, "|");
 
        // The record key will be the message number
-       out_key->size = sizeof(long);
-       out_key->data = reallok(out_key->data, out_key->size);
-       memcpy(out_key->data, &msgnum, out_key->size);
+       kv->key.len = sizeof(long);
+       kv->key.ptr = reallok(kv->key.ptr, kv->key.len);
+       memcpy(kv->key.ptr, &msgnum, kv->key.len);
 
        // The record data will be the decoded message text.
        // We are allocating more memory than we need, but BDB will only write the number of bytes we tell it to.
-       out_data->data = reallok(out_data->data, strlen(token));
-       out_data->size = CtdlDecodeBase64(out_data->data, token, strlen(token));
+       kv->val.ptr = (char *) reallok(kv->val.ptr, strlen(token));
+       kv->val.len = CtdlDecodeBase64(kv->val.ptr, token, strlen(token));
        return(1);
 }
 
 
 // Convert a "msgmeta" record to a message metadata record on disk.  NOT THREADSAFE
-int import_msgmeta(char *line, DBT *out_key, DBT *out_data) {
+int import_msgmeta(char *line, struct cdbkeyval *kv) {
        char *token;
-       struct MetaData *m = malloc(sizeof(struct MetaData));
+       struct MetaData *m;
 
+       m = malloc(sizeof(struct MetaData));
+       if (!m) {
+               fprintf(stderr, "import_msgmeta: malloc: %s\n", strerror(errno));
+               return(0);
+       }
        memset(m, 0, sizeof(struct MetaData));
        char *p = line;
 
@@ -166,24 +101,29 @@ int import_msgmeta(char *line, DBT *out_key, DBT *out_data) {
        // metadata records are stored in the CDB_MSGMAIN table,
        // but with the index being the *negative* of the message number.
        long index = 0 - m->meta_msgnum;
-       out_key->size = sizeof(long);
-       out_key->data = reallok(NULL, out_key->size);
-       memcpy(out_key->data, &index, out_key->size);
+       kv->key.len = sizeof(long);
+       kv->key.ptr = reallok(NULL, kv->key.len);
+       memcpy(kv->key.ptr, &index, kv->key.len);
 
        // data
-       out_data->size = sizeof(struct MetaData);
-       out_data->data = m;                             // out_data owns this memory now
+       kv->val.len = sizeof(struct MetaData);
+       kv->val.ptr = (char *) m;                               // out_data owns this memory now
 
        return(1);
 }
 
 
 // Convert a "user" record to a record on disk.  (Source string is unusable after this function is called.)
-int import_user(char *line, DBT *out_key, DBT *out_data) {
+int import_user(char *line, struct cdbkeyval *kv) {
        char userkey[USERNAME_SIZE];
        char *token;
-       struct ctdluser *u = malloc(sizeof(struct ctdluser));
+       struct ctdluser *u;
 
+       u = malloc(sizeof(struct ctdluser));
+       if (!u) {
+               fprintf(stderr, "malloc failed\n");
+               return(0);
+       }
        memset(u, 0, sizeof(struct ctdluser));
        char *p = line;
 
@@ -196,7 +136,7 @@ int import_user(char *line, DBT *out_key, DBT *out_data) {
                                u->uid = atoi(token);
                                break;
                        case 3:
-                               strncpy(u->password, token, sizeof(u->password));
+                               safestrncpy(u->password, token, sizeof(u->password));
                                break;
                        case 4:
                                u->flags = atoi(token);
@@ -214,7 +154,7 @@ int import_user(char *line, DBT *out_key, DBT *out_data) {
                                u->USuserpurge = atoi(token);
                                break;
                        case 9:
-                               strncpy(u->fullname, token, sizeof(u->fullname));
+                               safestrncpy(u->fullname, token, sizeof(u->fullname));
                                break;
                        case 10:
                                u->msgnum_bio = atol(token);
@@ -223,7 +163,8 @@ int import_user(char *line, DBT *out_key, DBT *out_data) {
                                u->msgnum_pic = atol(token);
                                break;
                        case 12:
-                               CtdlDecodeBase64(u->emailaddrs, token, strlen(token));
+                               CtdlDecodeBase64(token, token, strlen(token));                  // Decode in place
+                               safestrncpy(u->emailaddrs, token, sizeof(u->emailaddrs));
                                break;
                        case 13:
                                u->msgnum_inboxrules = atol(token);
@@ -233,21 +174,33 @@ int import_user(char *line, DBT *out_key, DBT *out_data) {
                                break;
                }
        }
+
+       // reject any user records without names
+       if (strlen(u->fullname) == 0) {
+               free(u);
+               return(0);
+       }
        
+       // ok, good to go
        makeuserkey(userkey, u->fullname);
-       out_key->size = strlen(userkey);
-       out_key->data = strdup(userkey);
-       out_data->size = sizeof(struct ctdluser);
-       out_data->data = u;
+       kv->key.len = strlen(userkey);
+       kv->key.ptr = strdup(userkey);
+       kv->val.len = sizeof(struct ctdluser);
+       kv->val.ptr = (char *) u;
        return(1);
 }
 
 
 // Convert a "room" record to a record on disk.  (Source string is unusable after this function is called.)
-int import_room(char *line, DBT *out_key, DBT *out_data) {
+int import_room(char *line, struct cdbkeyval *kv) {
        char *token;
-       struct ctdlroom *r = malloc(sizeof(struct ctdlroom));
+       struct ctdlroom *r;
 
+       r = malloc(sizeof(struct ctdlroom));
+       if (!r) {
+               fprintf(stderr, "import_room: malloc: %s\n", strerror(errno));
+               return(0);
+       }
        memset(r, 0, sizeof(struct ctdlroom));
        char *p = line;
 
@@ -308,25 +261,30 @@ int import_room(char *line, DBT *out_key, DBT *out_data) {
        }
 
        // The key is the room name in all lower case
-       out_key->size = strlen(r->QRname);
-       out_key->data = strdup(r->QRname);
-       char *k = (char *)out_key->data;
-       for (int i=0; i<=out_key->size; ++i) {
+       kv->key.len = strlen(r->QRname);
+       kv->key.ptr = strdup(r->QRname);
+       char *k = (char *)kv->key.ptr;
+       for (int i=0; i<=kv->key.len; ++i) {
                k[i] = tolower(k[i]);
        }
 
-       out_data->size = sizeof(struct ctdlroom);
-       out_data->data = r;
+       kv->val.len = sizeof(struct ctdlroom);
+       kv->val.ptr = (char *) r;
        return(1);
 }
 
 
 // Convert a floor record to a record on disk.
-int import_floor(char *line, DBT *out_key, DBT *out_data) {
+int import_floor(char *line, struct cdbkeyval *kv) {
        char *token;
-       struct floor *f = malloc(sizeof(struct floor));
+       struct floor *f;
        int floor_num;
 
+       f = malloc(sizeof(struct floor));
+       if (!f) {
+               fprintf(stderr, "import_floor: malloc: %s\n", strerror(errno));
+               return(0);
+       }
        memset(f, 0, sizeof(struct floor));
        char *p = line;
 
@@ -353,19 +311,19 @@ int import_floor(char *line, DBT *out_key, DBT *out_data) {
                }
        }
 
-       out_key->size = sizeof(int);
-       out_key->data = malloc(out_key->size);
-       memcpy(out_key->data, &floor_num, out_key->size);
+       kv->key.len = sizeof(int);
+       kv->key.ptr = malloc(kv->key.len);
+       memcpy(kv->key.ptr, &floor_num, kv->key.len);
 
-       out_data->size = sizeof(struct floor);
-       out_data->data = f;
+       kv->val.len = sizeof(struct floor);
+       kv->val.ptr = (char *) f;
        return(1);
 }
 
 
 // Import a msglist record
 // msglist|26|32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51|
-int import_msglist(char *line, DBT *out_key, DBT *out_data) {
+int import_msglist(char *line, struct cdbkeyval *kv) {
        long roomnum;
        char *token, *mtoken;
        char *p = line;
@@ -388,22 +346,27 @@ int import_msglist(char *line, DBT *out_key, DBT *out_data) {
                }
        }
 
-       out_key->size = sizeof(long);
-       out_key->data = malloc(out_key->size);
-       memcpy(out_key->data, &roomnum, out_key->size);
+       kv->key.len = sizeof(long);
+       kv->key.ptr = malloc(kv->key.len);
+       memcpy(kv->key.ptr, &roomnum, kv->key.len);
 
-       out_data->size = num_msgs * sizeof(long);
-       out_data->data = msglist;
+       kv->val.len = num_msgs * sizeof(long);
+       kv->val.ptr = (char *) msglist;
 
        return(1);
 }
 
 
 // Convert a "visit" record to a record on disk.
-int import_visit(char *line, DBT *out_key, DBT *out_data) {
+int import_visit(char *line, struct cdbkeyval *kv) {
        char *token;
-       struct visit *v = malloc(sizeof(struct visit));
+       struct visit *v;
 
+       v = malloc(sizeof(struct visit));
+       if (!v) {
+               fprintf(stderr, "import_visit: malloc: %s\n", strerror(errno));
+               return(0);
+       }
        memset(v, 0, sizeof(struct visit));
        char *p = line;
 
@@ -437,18 +400,18 @@ int import_visit(char *line, DBT *out_key, DBT *out_data) {
        }
 
        // The key is the same as the first three data members (3 x long)
-       out_key->size = sizeof(long) * 3;
-       out_key->data = reallok(NULL, out_key->size);
-       memcpy(out_key->data, v, out_key->size);
+       kv->key.len = sizeof(long) * 3;
+       kv->key.ptr = reallok(NULL, kv->key.len);
+       memcpy(kv->key.ptr, v, kv->key.len);
 
-       out_data->size = sizeof(struct visit);
-       out_data->data = v;
+       kv->val.len = sizeof(struct visit);
+       kv->val.ptr = (char *) v;
        return(1);
 }
 
 
 // Convert a "dir" record to a record on disk.
-int import_dir(char *line, DBT *out_key, DBT *out_data) {
+int import_dir(char *line, struct cdbkeyval *kv) {
        char dirkey[SIZ];
        char username[USERNAME_SIZE];
        char *token;
@@ -465,22 +428,27 @@ int import_dir(char *line, DBT *out_key, DBT *out_data) {
                }
        }
 
-       out_key->size = strlen(dirkey);
-       out_key->data = reallok(NULL, out_key->size);
-       memcpy(out_key->data, dirkey, strlen(dirkey));
+       kv->key.len = strlen(dirkey);
+       kv->key.ptr = reallok(NULL, kv->key.len);
+       memcpy(kv->key.ptr, dirkey, strlen(dirkey));
 
-       out_data->size = strlen(username) + 1;
-       out_data->data = strdup(username);
+       kv->val.len = strlen(username) + 1;
+       kv->val.ptr = strdup(username);
 
        return(1);
 }
 
 
 // Convert a "usetable" record to a record on disk.
-int import_usetable(char *line, DBT *out_key, DBT *out_data) {
+int import_usetable(char *line, struct cdbkeyval *kv) {
        char *token;
-       struct UseTable *u = malloc(sizeof(struct UseTable));
+       struct UseTable *u;
 
+       u = malloc(sizeof(struct UseTable));
+       if (!u) {
+               fprintf(stderr, "import_usetable: malloc: %s\n", strerror(errno));
+               return(0);
+       }
        memset(u, 0, sizeof(struct UseTable));
        char *p = line;
 
@@ -496,19 +464,19 @@ int import_usetable(char *line, DBT *out_key, DBT *out_data) {
        }
 
        // the key is just an int (the hash)
-       out_key->size = sizeof(int);
-       out_key->data = reallok(NULL, out_key->size);
-       memcpy(out_key->data, &u->hash, out_key->size);
+       kv->key.len = sizeof(int);
+       kv->key.ptr = reallok(NULL, kv->key.len);
+       memcpy(kv->key.ptr, &u->hash, kv->key.len);
 
-       out_data->size = sizeof(struct UseTable);
-       out_data->data = u;
+       kv->val.len = sizeof(struct UseTable);
+       kv->val.ptr = (char *) u;
        return(1);
 }
 
 
 // Import a full text search index record.
 // It's just like a msglists record: a key and a list of message numbers, but the key is "int" instead of "long"
-int import_fulltext(char *line, DBT *out_key, DBT *out_data) {
+int import_fulltext(char *line, struct cdbkeyval *kv) {
        int indexnum;
        char *token, *mtoken;
        char *p = line;
@@ -531,12 +499,12 @@ int import_fulltext(char *line, DBT *out_key, DBT *out_data) {
                }
        }
 
-       out_key->size = sizeof(int);
-       out_key->data = malloc(out_key->size);
-       memcpy(out_key->data, &indexnum, out_key->size);
+       kv->key.len = sizeof(int);
+       kv->key.ptr = malloc(kv->key.len);
+       memcpy(kv->key.ptr, &indexnum, kv->key.len);
 
-       out_data->size = num_msgs * sizeof(long);
-       out_data->data = msglist;
+       kv->val.len = num_msgs * sizeof(long);
+       kv->val.ptr = (char *) msglist;
 
        return(1);
 }
@@ -544,7 +512,7 @@ int import_fulltext(char *line, DBT *out_key, DBT *out_data) {
 
 // Import an EUID Index record
 // euidindex|msgnum|roomnum|euid|
-int import_euidindex(char *line, DBT *out_key, DBT *out_data) {
+int import_euidindex(char *line, struct cdbkeyval *kv) {
        char euid[SIZ];
        long msgnum;
        long roomnum;
@@ -568,19 +536,19 @@ int import_euidindex(char *line, DBT *out_key, DBT *out_data) {
        // The structure of an euidindex record *key* is:
        // |----room_number----|----------EUID-------------|
        //    (sizeof long)       (actual length of euid)
-       out_key->size = sizeof(long) + strlen(euid) + 1;
-       out_key->data = reallok(NULL, out_key->size);
-       memcpy(out_key->data, &roomnum, sizeof(long));
-       strcpy(out_key->data + sizeof(long), euid);
+       kv->key.len = sizeof(long) + strlen(euid) + 1;
+       kv->key.ptr = reallok(NULL, kv->key.len);
+       memcpy(kv->key.ptr, &roomnum, sizeof(long));
+       strcpy(kv->key.ptr + sizeof(long), euid);
 
        // The structure of an euidindex record *value* is:
        // |-----msg_number----|----room_number----|----------EUID-------------|
        //    (sizeof long)       (sizeof long)       (actual length of euid)
-       out_data->size = sizeof(long) + sizeof(long) + strlen(euid) + 1;
-       out_data->data = reallok(NULL, out_data->size);
-       memcpy(out_data->data, &msgnum, sizeof(long));
-       memcpy(out_data->data + sizeof(long), &roomnum, sizeof(long));
-       strcpy(out_data->data + sizeof(long) + sizeof(long), euid);
+       kv->val.len = sizeof(long) + sizeof(long) + strlen(euid) + 1;
+       kv->val.ptr = (char *) reallok(NULL, kv->val.len);
+       memcpy(kv->val.ptr, &msgnum, sizeof(long));
+       memcpy(kv->val.ptr + sizeof(long), &roomnum, sizeof(long));
+       strcpy(kv->val.ptr + sizeof(long) + sizeof(long), euid);
 
        return(1);
 }
@@ -588,7 +556,7 @@ int import_euidindex(char *line, DBT *out_key, DBT *out_data) {
 
 // Import a "users by number" (secondary index) record
 // The key is a "long"
-int import_usersbynumber(char *line, DBT *out_key, DBT *out_data) {
+int import_usersbynumber(char *line, struct cdbkeyval *kv) {
        char *token;
        long usernum;
 
@@ -599,11 +567,11 @@ int import_usersbynumber(char *line, DBT *out_key, DBT *out_data) {
                                usernum = atol(token);
                                break;
                        case 2:
-                               out_key->size = sizeof(long);
-                               out_key->data = reallok(NULL, sizeof(long));
-                               memcpy(out_key->data, &usernum, out_key->size);
-                               out_data->data = strdup(token);
-                               out_data->size = strlen(out_data->data) + 1;
+                               kv->key.len = sizeof(long);
+                               kv->key.ptr = reallok(NULL, sizeof(long));
+                               memcpy(kv->key.ptr, &usernum, kv->key.len);
+                               kv->val.ptr = (char *) strdup(token);
+                               kv->val.len = strlen(kv->val.ptr) + 1;
                                return(1);
                }
        }
@@ -615,7 +583,7 @@ int import_usersbynumber(char *line, DBT *out_key, DBT *out_data) {
 // Import a config record
 // The key is the config key
 // The data is the config key, a null, the value, and another null
-int import_config(char *line, DBT *out_key, DBT *out_data) {
+int import_config(char *line, struct cdbkeyval *kv) {
        char *token;
        char *p = line;
        char *k, *v;
@@ -624,15 +592,15 @@ int import_config(char *line, DBT *out_key, DBT *out_data) {
                switch(i) {
                        case 1:
                                k = token;
-                               out_key->size = strlen(token);
-                               out_key->data = strdup(token);
+                               kv->key.len = strlen(token);
+                               kv->key.ptr = strdup(token);
                                break;
                        case 2:
                                v = token;
-                               out_data->size = strlen(k) + strlen(v) + 2;
-                               out_data->data = reallok(NULL, out_data->size);
-                               strcpy(out_data->data, k);
-                               strcpy(out_data->data + strlen(k) + 1, v);
+                               kv->val.len = strlen(k) + strlen(v) + 2;
+                               kv->val.ptr = (char *) reallok(NULL, kv->val.len);
+                               strcpy(kv->val.ptr, k);
+                               strcpy(kv->val.ptr + strlen(k) + 1, v);
                                break;
                }
        }
@@ -642,30 +610,16 @@ int import_config(char *line, DBT *out_key, DBT *out_data) {
 
 
 // Ingest one line of dump data.  NOT REENTRANT
-void ingest_one(char *line, DB_ENV *dst_dbenv) {
+void ingest_one(char *line, struct cdbkeyval *kv) {
 
        static int good_rows = 0;
        static int bad_rows = 0;
-       static int previous_cdb = -1 ;
        static int current_cdb = -1 ;
-       static DB *dst_dbp;
        char record_type[32];
-       int ret;
-       char dbfilename[32];
        int row_was_good;
-       DBT out_key, out_data;
-
-       // We are assuming that the lines of the dump file will generally be sorted by table.
-       // By remembering the last table we worked with, we can do close/open if the table changes.
-
-       if (current_cdb >= 0) {
-               fprintf(stderr, "   \033[33m%02x \033[32m%9d \033[31m%8d\033[0m\r", current_cdb, good_rows, bad_rows);
-               fflush(stderr);
-       }
 
        // Clear out our record buffer
-       memset(&out_key, 0, sizeof(DBT));
-       memset(&out_data, 0, sizeof(DBT));
+       memset(kv, 0, sizeof(struct cdbkeyval));
        row_was_good = 0;
 
        // Identify the record type we are currently working with,
@@ -673,130 +627,91 @@ void ingest_one(char *line, DB_ENV *dst_dbenv) {
        extract_token(record_type, line, 0, '|', sizeof record_type);
        if (!strcasecmp(record_type, "msgtext")) {
                current_cdb = CDB_MSGMAIN;
-               row_was_good = import_msgtext(line, &out_key, &out_data);
+               row_was_good = import_msgtext(line, kv);
        }
        else if (!strcasecmp(record_type, "msgmeta")) {
                current_cdb = CDB_MSGMAIN;
-               row_was_good = import_msgmeta(line, &out_key, &out_data);
+               row_was_good = import_msgmeta(line, kv);
        }
        else if (!strcasecmp(record_type, "user")) {
                current_cdb = CDB_USERS;
-               row_was_good = import_user(line, &out_key, &out_data);
+               row_was_good = import_user(line, kv);
        }
        else if (!strcasecmp(record_type, "room")) {
                current_cdb = CDB_ROOMS;
-               row_was_good = import_room(line, &out_key, &out_data);
+               row_was_good = import_room(line, kv);
        }
        else if (!strcasecmp(record_type, "floor")) {
                current_cdb = CDB_FLOORTAB;
-               row_was_good = import_floor(line, &out_key, &out_data);
+               row_was_good = import_floor(line, kv);
        }
        else if (!strcasecmp(record_type, "msglist")) {
                current_cdb = CDB_MSGLISTS;
-               row_was_good = import_msglist(line, &out_key, &out_data);
+               row_was_good = import_msglist(line, kv);
        }
        else if (!strcasecmp(record_type, "visit")) {
                current_cdb = CDB_VISIT;
-               row_was_good = import_visit(line, &out_key, &out_data);
+               row_was_good = import_visit(line, kv);
        }
        else if (!strcasecmp(record_type, "dir")) {
                current_cdb = CDB_DIRECTORY;
-               row_was_good = import_dir(line, &out_key, &out_data);
+               row_was_good = import_dir(line, kv);
        }
        else if (!strcasecmp(record_type, "use")) {
                current_cdb = CDB_USETABLE;
-               row_was_good = import_usetable(line, &out_key, &out_data);
+               row_was_good = import_usetable(line, kv);
        }
        else if (!strcasecmp(record_type, "bigmsg")) {
                current_cdb = CDB_BIGMSGS;
-               row_was_good = import_msgtext(line, &out_key, &out_data);
+               row_was_good = import_msgtext(line, kv);
        }
        else if (!strcasecmp(record_type, "fulltext")) {
                current_cdb = CDB_FULLTEXT;
-               row_was_good = import_fulltext(line, &out_key, &out_data);
+               row_was_good = import_fulltext(line, kv);
        }
        else if (!strcasecmp(record_type, "euidindex")) {
                current_cdb = CDB_EUIDINDEX;
-               row_was_good = import_euidindex(line, &out_key, &out_data);
+               row_was_good = import_euidindex(line, kv);
        }
        else if (!strcasecmp(record_type, "usersbynumber")) {
                current_cdb = CDB_USERSBYNUMBER;
-               row_was_good = import_usersbynumber(line, &out_key, &out_data);
+               row_was_good = import_usersbynumber(line, kv);
        }
        else if (!strcasecmp(record_type, "config")) {
                current_cdb = CDB_CONFIG;
-               row_was_good = import_config(line, &out_key, &out_data);
+               row_was_good = import_config(line, kv);
        }
        else {
                current_cdb = -1 ;
        }
 
-       // If the current record is a different table than the previous record,
-       // then close the other table and open the correct one.
-       if (current_cdb != previous_cdb) {
-               if (previous_cdb >= 0) {
-                       fprintf(stderr, "\n");
-               }
-               if (previous_cdb >= 0) {
-                       ret = dst_dbp->close(dst_dbp, 0);
-                       if (ret) {
-                               fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
-                       }
-               }
-
-               if (current_cdb >= 0) {
-                       good_rows = 0;
-                       bad_rows = 0;
-                       snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", current_cdb);
-
-                       // create a database handle for the destination table
-                       ret = db_create(&dst_dbp, dst_dbenv, 0);
-                       if (ret) {
-                               fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
-                               fprintf(stderr, "db: exit code %d\n", ret);
-                               exit(CTDLEXIT_DB);
-                       }
-               
-                       // open the file containing the destination table
-                       ret = dst_dbp->open(dst_dbp, NULL, dbfilename, NULL, DB_BTREE, (DB_CREATE | DB_TRUNCATE), 0600);
-                       if (ret) {
-                               fprintf(stderr, "db: db_open(%s): %s\n", dbfilename, db_strerror(ret));
-                               fprintf(stderr, "db: exit code %d\n", ret);
-                               exit(CTDLEXIT_DB);
-                       }
-               }
-
-               previous_cdb = current_cdb;
-       }
-
        // If the conversion function was successful, write the record to the database.
-       if (row_was_good) {
+       if ( (row_was_good) && (current_cdb >= 0) ) {
+               cdb_store(current_cdb, kv->key.ptr, kv->key.len, kv->val.ptr, kv->val.len);
                ++good_rows;
-               ret = dst_dbp->put(dst_dbp, NULL, &out_key, &out_data, 0);
-               if (ret) {
-                       fprintf(stderr, "db: cdb_put(%x): %s", current_cdb, db_strerror(ret));
-                       exit(CTDLEXIT_DB);
-               }
        }
        else {
                ++bad_rows;
        }
 
-       free(out_key.data);
-       free(out_data.data);
+       fprintf(stderr, "\r   \033[32m%9d\033[0m / \033[31m%8d\033[0m ", good_rows, bad_rows);
+       fflush(stderr);
+
 }
 
 
 // This is the loop that loads the dump data.  NOT REENTRANT
-void ingest(DB_ENV *dst_dbenv) {
+void ingest(void) {
        static size_t line_alloc = 1;
        static char *line;
        static size_t line_len = 0;
        int ch;
        int begin_found = 0;
        int end_found = 0;
+       struct cdbkeyval kv;
+
+       memset(&kv, 0, sizeof(struct cdbkeyval));
 
-       fprintf(stderr, "\033[7mtable\033[0m \033[7mgood_rows\033[0m \033[7mbad_rows\033[0m\n");
        line = reallok(NULL, line_alloc);
 
        do {
@@ -816,9 +731,17 @@ void ingest(DB_ENV *dst_dbenv) {
                }
 
                if (line_len > 0) {
-                       ingest_one(line, dst_dbenv);
-                       if (!strncasecmp(line, HKEY("begin|"))) begin_found = 1;
-                       if (!strncasecmp(line, HKEY("end|"))) end_found = 1;
+                       if ( (begin_found) && (!end_found) ) {
+                               ingest_one(line, &kv);
+                       }
+                       if (!strncasecmp(line, HKEY("begin|"))) {
+                               begin_found = 1;
+                               fprintf(stderr, "   good rows / bad rows:\n");
+                       }
+                       if (!strncasecmp(line, HKEY("end|"))) {
+                               fprintf(stderr, "\n");
+                               end_found = 1;
+                       }
                }
 
        } while (ch >= 0);
@@ -834,60 +757,67 @@ void ingest(DB_ENV *dst_dbenv) {
 
 // Main entry point
 int main(int argc, char **argv) {
-       char dst_dir[PATH_MAX];
        int confirmed = 0;
-       static DB_ENV *dst_dbenv;               // Target DB environment
+       char *ctdldir = CTDLDIR;
 
        // display the greeting
-       fprintf(stderr, "\033[44m\033[33m\033[1m \033[K\033[0m\n"
-                       "\033[44m\033[33m\033[1m DB Load utility for Citadel \033[K\033[0m\n"
-                       "\033[44m\033[33m\033[1m Copyright (c) 2023 by citadel.org et al.  \033[K\033[0m\n"
-                       "\033[44m\033[33m\033[1m This program is open source software.  Use, duplication, or disclosure \033[K\033[0m\n"
-                       "\033[44m\033[33m\033[1m is subject to the terms of the GNU General Public license v3. \033[K\033[0m\n"
-                       "\033[44m\033[33m\033[1m \033[K\033[0m\n");
-
-       // Default destination directory unless overridden
-       snprintf(dst_dir, sizeof(dst_dir), "%s/data", CTDLDIR);
+       fprintf(stderr,
+               "\033[44m\033[1m╔════════════════════════════════════════════════════════════════════════╗\033[0m\n"
+               "\033[44m\033[1m║ DB Load utility for Citadel                               version %-4d ║\033[0m\n"
+               "\033[44m\033[1m║ Copyright (c) 2023-2024 by citadel.org et al.                          ║\033[0m\n"
+               "\033[44m\033[1m║ This program is open source software.  Use, duplication, or disclosure ║\033[0m\n"
+               "\033[44m\033[1m║ is subject to the terms of the GNU General Public license v3.          ║\033[0m\n"
+               "\033[44m\033[1m╚════════════════════════════════════════════════════════════════════════╝\033[0m\n",
+               REV_LEVEL
+       );
 
        // Parse command line
        int a;
        while ((a = getopt(argc, argv, "h:y")) != EOF) {
                switch (a) {
                case 'h':
-                       snprintf(dst_dir, sizeof(dst_dir), "%s/data", optarg);
+                       ctdldir = optarg;
                        break;
                case 'y':
                        confirmed = 1;
                        break;
                default:
-                       fprintf(stderr, "%s: usage: %s -h dest_dir [<dumpfile]\n", argv[0], argv[0]);
+                       fprintf(stderr, "%s: usage: %s -h citadel_dir [<dumpfile]\n", argv[0], argv[0]);
                        exit(2);
                }
        }
 
-       if (dst_dir == NULL) {
-               fprintf(stderr, "ctdlload: no destination directory was specified.\n");
-               exit(1);
-       }
-
        if (confirmed == 1) {
                fprintf(stderr, "ctdlload: You have specified the [-y] flag, so processing will continue.\n");
        }
        else {
-               fprintf(stderr, "ctdlload: usage: ctdlload -y -h[data_dir] <[dump_file]\n");
-               fprintf(stderr, "          [data_dir] is your database directory, usually /usr/local/citadel/data\n");
+               fprintf(stderr, "ctdlload: usage: ctdlload -y -h[citadel_dir] <[dump_file]\n");
+               fprintf(stderr, "          -y : yes, I know this program can do damage and I want to run it anyway.\n");
+               fprintf(stderr, "          -h : [citadel_dir] is your server directory, usually /usr/local/citadel\n");
                fprintf(stderr, "          Please read [ https://www.citadel.org/dump-and-load.html ] to learn how to proceed.\n");
                exit(1);
        }
 
+       if (chdir(ctdldir) != 0) {
+               fprintf(stderr, "ctdlload: unable to change directory to [%s]: %m", ctdldir);
+               exit(2);
+       }
+
+       // backend modules use syslog -- redirect to stderr
+       openlog("ctdlload", LOG_PERROR , LOG_DAEMON);
+
        // Remove any database that is already in the target directory (yes, delete it, be careful)
-       char cmd[PATH_MAX];
-       snprintf(cmd, sizeof cmd, "rm -fv %s/cdb.* %s/log.*", dst_dir, dst_dir);
-       system(cmd);
+       system("rm -fv ./data/*");
+
+       // initialize the database backend
+       cdb_init_backends();
+       cdb_open_databases();
+
+       // Load rows from the input source
+       ingest();
 
-       dst_dbenv = open_dbenv(dst_dir);
-       ingest(dst_dbenv);
-       close_dbenv(dst_dbenv);
+       // close databases
+       cdb_close_databases();
 
        fprintf(stderr, "ctdlload: \033[32m\033[1mfinished\033[0m\n");
        exit(0);