1 // Don't run this. It doesn't work and if you try to run it you will immediately die.
3 // Copyright (c) 2023 by Art Cancro citadel.org
5 // This program is open source software. Use, duplication, or disclosure
6 // is subject to the terms of the GNU General Public License, version 3.
13 #include <sys/types.h>
14 #include <sys/socket.h>
22 #include <libcitadel.h>
25 #include "../server/sysdep.h"
26 #include "../server/citadel_defs.h"
27 #include "../server/server.h"
28 #include "../server/makeuserkey.h"
29 #include "../server/citadel_dirs.h"
32 // Wrapper for realloc() that crashes and burns if the call fails.
33 void *reallok(void *ptr, size_t size) {
34 void *p = realloc(ptr, size);
36 fprintf(stderr, "realloc() failed to resize %p to %ld bytes, error: %m\n", ptr, size);
43 // Open a database environment
44 DB_ENV *open_dbenv(char *dirname) {
51 int dbversion_major, dbversion_minor, dbversion_patch;
53 fprintf(stderr, "db: open_dbenv() starting\n"
54 "db: Linked zlib: %s\n"
55 "db: Compiled libdb: %s\n"
56 "db: Linked libdb: %s\n",
59 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch)
62 // Create synthetic integer version numbers and compare them.
63 // Never run with a libdb older than the one with which it was compiled.
64 int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
65 int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
66 if (compiled_db_version > linked_db_version) {
67 fprintf(stderr, "db: ctdlload is running with a version of libdb older than the one with which it was compiled.\n"
68 "db: This is an invalid configuration. ctdlload will now exit to prevent data loss.");
72 fprintf(stderr,"db: Setting up DB environment\n");
73 ret = db_env_create(&dbenv, 0);
75 fprintf(stderr,"db: db_env_create: %s\n", db_strerror(ret));
76 fprintf(stderr,"db: exit code %d\n", ret);
80 // We want to specify the shared memory buffer pool cachesize, but everything else is the default.
81 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
83 fprintf(stderr,"db: set_cachesize: %s\n", db_strerror(ret));
84 dbenv->close(dbenv, 0);
85 fprintf(stderr,"db: exit code %d\n", ret);
89 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
90 fprintf(stderr,"db: set_lk_detect: %s\n", db_strerror(ret));
91 dbenv->close(dbenv, 0);
92 fprintf(stderr,"db: exit code %d\n", ret);
96 flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
97 fprintf(stderr,"db: dbenv open(dir=%s, flags=%d)\n", dirname, flags);
98 ret = dbenv->open(dbenv, dirname, flags, 0);
100 fprintf(stderr,"db: dbenv->open: %s\n", db_strerror(ret));
101 dbenv->close(dbenv, 0);
102 fprintf(stderr,"db: exit code %d\n", ret);
110 void close_dbenv(DB_ENV *dbenv) {
111 fprintf(stderr,"db: closing dbenv\n");
112 int ret = dbenv->close(dbenv, 0);
114 fprintf(stderr,"db: dbenv->close: %s\n", db_strerror(ret));
119 // Skeleton convert function
120 int convert_foo(char *line, DBT *out_key, DBT *out_data) {
125 // Convert a "msgtext" record to a message on disk. NOT THREADSAFE
126 int convert_msgtext(char *line, DBT *out_key, DBT *out_data) {
128 static char *b64_decoded_msg = NULL;
129 static size_t b64_decoded_alloc = 0;
133 token = strtok(line, "|");
134 msgnum = atol(strtok(NULL, "|"));
135 token = strtok(NULL, "|");
137 // The record key will be the message number
138 out_key->size = sizeof(long);
139 out_key->data = reallok(out_key->data, out_key->size);
140 memcpy(out_key->data, &msgnum, out_key->size);
142 // The record data will be the decoded message text.
143 // We are allocating more memory than we need, but BDB will only write the number of bytes we tell it to.
144 out_data->data = reallok(out_data->data, strlen(token));
145 out_data->size = CtdlDecodeBase64(out_data->data, token, strlen(token));
150 // Convert a "msgmeta" record to a message metadata record on disk. NOT THREADSAFE
151 int convert_msgmeta(char *line, DBT *out_key, DBT *out_data) {
153 struct MetaData *m = malloc(sizeof(struct MetaData));
154 token = strtok(line, "|");
155 m->meta_msgnum = atol(strtok(NULL, "|"));
156 m->meta_refcount = atoi(strtok(NULL, "|"));
157 strncpy(m->meta_content_type, strtok(NULL, "|"), sizeof(m->meta_content_type));
158 m->meta_rfc822_length = atol(strtok(NULL, "|"));
160 // metadata records are stored in the CDB_MSGMAIN table,
161 // but with the index being the *negative* of the message number.
162 long index = 0 - m->meta_msgnum;
163 out_key->size = sizeof(long);
164 out_key->data = reallok(NULL, out_key->size);
165 memcpy(out_key->data, &index, out_key->size);
168 out_data->size = sizeof(struct MetaData);
169 out_data->data = m; // out_data owns this memory now
175 // Convert a "user" record to a record on disk. NOT THREADSAFE
176 int convert_user(char *line, DBT *out_key, DBT *out_data) {
177 char userkey[USERNAME_SIZE];
179 struct ctdluser *u = malloc(sizeof(struct ctdluser));
181 memset(u, 0, sizeof(struct ctdluser));
184 for (int i=0; (token = strsep(&p, "|")); ++i) {
187 u->version = atoi(token);
190 u->uid = atoi(token);
193 strncpy(u->password, token, sizeof(u->password));
196 u->flags = atoi(token);
199 u->axlevel = atoi(token);
202 u->usernum = atol(token);
205 u->lastcall = atol(token);
208 u->USuserpurge = atoi(token);
211 strncpy(u->fullname, token, sizeof(u->fullname));
214 u->msgnum_bio = atol(token);
217 u->msgnum_pic = atol(token);
220 CtdlDecodeBase64(u->emailaddrs, token, strlen(token));
223 u->msgnum_inboxrules = atol(token);
226 u->lastproc_inboxrules = atol(token);
231 makeuserkey(userkey, u->fullname);
232 out_key->size = strlen(userkey);
233 out_key->data = strdup(userkey);
234 out_data->size = sizeof(struct ctdluser);
240 // Ingest one line of dump data. NOT REENTRANT
241 void ingest_one(char *line, DB_ENV *dst_dbenv) {
243 static int previous_cdb = -1 ;
244 static int current_cdb = -1 ;
246 char record_type[32];
250 DBT out_key, out_data;
252 // We are assuming that the lines of the dump file will generally be sorted by table.
253 // By remembering the last table we worked with, we can do close/open if the table changes.
255 // Identify the record type we are currently working with
256 extract_token(record_type, line, 0, '|', sizeof record_type);
257 if (!strcasecmp(record_type, "msgtext")) current_cdb = CDB_MSGMAIN;
258 else if (!strcasecmp(record_type, "msgmeta")) current_cdb = CDB_MSGMAIN;
259 else if (!strcasecmp(record_type, "user")) current_cdb = CDB_USERS;
260 else if (!strcasecmp(record_type, "room")) current_cdb = CDB_ROOMS;
261 else if (!strcasecmp(record_type, "floor")) current_cdb = CDB_FLOORTAB;
262 else if (!strcasecmp(record_type, "msglist")) current_cdb = CDB_MSGLISTS;
263 else if (!strcasecmp(record_type, "visit")) current_cdb = CDB_VISIT;
264 else if (!strcasecmp(record_type, "dir")) current_cdb = CDB_DIRECTORY;
265 else if (!strcasecmp(record_type, "use")) current_cdb = CDB_USETABLE;
266 else if (!strcasecmp(record_type, "bigmsg")) current_cdb = CDB_BIGMSGS;
267 else if (!strcasecmp(record_type, "euidindex")) current_cdb = CDB_EUIDINDEX;
268 else if (!strcasecmp(record_type, "usersbynumber")) current_cdb = CDB_USERSBYNUMBER;
269 else if (!strcasecmp(record_type, "config")) current_cdb = CDB_CONFIG;
270 else current_cdb = -1 ;
272 if (current_cdb != previous_cdb) {
273 if (previous_cdb >= 0) {
274 ret = dst_dbp->close(dst_dbp, 0);
276 fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
280 if (current_cdb >= 0) {
281 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", current_cdb);
283 // create a database handle for the destination table
284 ret = db_create(&dst_dbp, dst_dbenv, 0);
286 fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
287 fprintf(stderr, "db: exit code %d\n", ret);
291 // open the file containing the destination table
292 ret = dst_dbp->open(dst_dbp, NULL, dbfilename, NULL, DB_BTREE, (DB_CREATE | DB_TRUNCATE), 0600);
294 fprintf(stderr, "db: db_open: %s\n", db_strerror(ret));
295 fprintf(stderr, "db: exit code %d\n", ret);
300 previous_cdb = current_cdb;
303 // If we have a valid record type and a target database open, dispatch the correct record type handler.
304 memset(&out_key, 0, sizeof(DBT));
305 memset(&out_data, 0, sizeof(DBT));
307 if (!strcasecmp(record_type, "msgtext")) row_was_good = convert_msgtext(line, &out_key, &out_data);
308 else if (!strcasecmp(record_type, "msgmeta")) row_was_good = convert_msgmeta(line, &out_key, &out_data);
309 else if (!strcasecmp(record_type, "user")) row_was_good = convert_user(line, &out_key, &out_data);
310 else if (!strcasecmp(record_type, "room")) row_was_good = convert_foo(line, &out_key, &out_data);
311 else if (!strcasecmp(record_type, "floor")) row_was_good = convert_foo(line, &out_key, &out_data);
312 else if (!strcasecmp(record_type, "msglist")) row_was_good = convert_foo(line, &out_key, &out_data);
313 else if (!strcasecmp(record_type, "visit")) row_was_good = convert_foo(line, &out_key, &out_data);
314 else if (!strcasecmp(record_type, "dir")) row_was_good = convert_foo(line, &out_key, &out_data);
315 else if (!strcasecmp(record_type, "use")) row_was_good = convert_foo(line, &out_key, &out_data);
316 else if (!strcasecmp(record_type, "bigmsg")) row_was_good = convert_foo(line, &out_key, &out_data);
317 else if (!strcasecmp(record_type, "euidindex")) row_was_good = convert_foo(line, &out_key, &out_data);
318 else if (!strcasecmp(record_type, "usersbynumber")) row_was_good = convert_foo(line, &out_key, &out_data);
319 else if (!strcasecmp(record_type, "config")) row_was_good = convert_foo(line, &out_key, &out_data);
320 else row_was_good = 0;
323 ret = dst_dbp->put(dst_dbp, NULL, &out_key, &out_data, 0);
325 fprintf(stderr, "db: cdb_put(%d): %s", current_cdb, db_strerror(ret));
335 // This is the loop that loads the dump data. NOT REENTRANT
336 void ingest(DB_ENV *dst_dbenv) {
337 static size_t line_alloc = 1;
339 static size_t line_len = 0;
342 line = reallok(NULL, line_alloc);
347 while (ch = getc(stdin), ((ch != '\n') && (ch > 0))) {
348 if ((line_len+2) > line_alloc) {
350 line = reallok(line, line_alloc);
352 line[line_len++] = ch;
361 ingest_one(line, dst_dbenv);
369 int main(int argc, char **argv) {
370 char *dst_dir = NULL;
372 static DB_ENV *dst_dbenv; // Source DB environment (global)
374 // Parse command line
376 while ((a = getopt(argc, argv, "h:y")) != EOF) {
385 fprintf(stderr, "%s: usage: %s -h dest_dir [<dumpfile]\n", argv[0], argv[0]);
390 if (confirmed == 1) {
391 fprintf(stderr,"You have specified the [-y] flag, so processing will continue.\n");
394 fprintf(stderr,"Please read [ https://www.citadel.org/ctdlload.html ] to learn how to proceed.\n");
399 snprintf(cmd, sizeof cmd, "rm -fv %s/cdb.* %s/log.*", dst_dir, dst_dir);
402 dst_dbenv = open_dbenv(dst_dir);
404 close_dbenv(dst_dbenv);