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;
52 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch);
54 // Create synthetic integer version numbers and compare them.
55 // Never run with a libdb other than the one with which it was compiled.
56 int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
57 int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
58 if (compiled_db_version != linked_db_version) {
60 "db: ctdlload is running with a version of libdb other than the one with which it was compiled.\n"
63 "db: This is an invalid configuration. ctdlload will now exit to prevent data loss.",
70 ret = db_env_create(&dbenv, 0);
72 fprintf(stderr,"db: db_env_create: %s\n", db_strerror(ret));
73 fprintf(stderr,"db: exit code %d\n", ret);
77 // We want to specify the shared memory buffer pool cachesize, but everything else is the default.
78 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
80 fprintf(stderr,"db: set_cachesize: %s\n", db_strerror(ret));
81 dbenv->close(dbenv, 0);
82 fprintf(stderr,"db: exit code %d\n", ret);
86 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
87 fprintf(stderr,"db: set_lk_detect: %s\n", db_strerror(ret));
88 dbenv->close(dbenv, 0);
89 fprintf(stderr,"db: exit code %d\n", ret);
93 flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
94 ret = dbenv->open(dbenv, dirname, flags, 0);
96 fprintf(stderr,"db: dbenv->open: %s\n", db_strerror(ret));
97 dbenv->close(dbenv, 0);
98 fprintf(stderr,"db: exit code %d\n", ret);
106 void close_dbenv(DB_ENV *dbenv) {
107 int ret = dbenv->close(dbenv, 0);
109 fprintf(stderr,"db: dbenv->close: %s\n", db_strerror(ret));
114 // Skeleton convert function
115 int convert_foo(char *line, DBT *out_key, DBT *out_data) {
120 // Convert a "msgtext" record to a message on disk. NOT THREADSAFE
121 int convert_msgtext(char *line, DBT *out_key, DBT *out_data) {
123 static char *b64_decoded_msg = NULL;
124 static size_t b64_decoded_alloc = 0;
128 token = strtok(line, "|");
129 msgnum = atol(strtok(NULL, "|"));
130 token = strtok(NULL, "|");
132 // The record key will be the message number
133 out_key->size = sizeof(long);
134 out_key->data = reallok(out_key->data, out_key->size);
135 memcpy(out_key->data, &msgnum, out_key->size);
137 // The record data will be the decoded message text.
138 // We are allocating more memory than we need, but BDB will only write the number of bytes we tell it to.
139 out_data->data = reallok(out_data->data, strlen(token));
140 out_data->size = CtdlDecodeBase64(out_data->data, token, strlen(token));
145 // Convert a "msgmeta" record to a message metadata record on disk. NOT THREADSAFE
146 int convert_msgmeta(char *line, DBT *out_key, DBT *out_data) {
148 struct MetaData *m = malloc(sizeof(struct MetaData));
149 token = strtok(line, "|");
150 m->meta_msgnum = atol(strtok(NULL, "|"));
151 m->meta_refcount = atoi(strtok(NULL, "|"));
152 strncpy(m->meta_content_type, strtok(NULL, "|"), sizeof(m->meta_content_type));
153 m->meta_rfc822_length = atol(strtok(NULL, "|"));
155 // metadata records are stored in the CDB_MSGMAIN table,
156 // but with the index being the *negative* of the message number.
157 long index = 0 - m->meta_msgnum;
158 out_key->size = sizeof(long);
159 out_key->data = reallok(NULL, out_key->size);
160 memcpy(out_key->data, &index, out_key->size);
163 out_data->size = sizeof(struct MetaData);
164 out_data->data = m; // out_data owns this memory now
170 // Convert a "user" record to a record on disk. (Source string is unusable after this function is called.)
171 int convert_user(char *line, DBT *out_key, DBT *out_data) {
172 char userkey[USERNAME_SIZE];
174 struct ctdluser *u = malloc(sizeof(struct ctdluser));
176 memset(u, 0, sizeof(struct ctdluser));
179 for (int i=0; (token = strsep(&p, "|")); ++i) {
182 u->version = atoi(token);
185 u->uid = atoi(token);
188 strncpy(u->password, token, sizeof(u->password));
191 u->flags = atoi(token);
194 u->axlevel = atoi(token);
197 u->usernum = atol(token);
200 u->lastcall = atol(token);
203 u->USuserpurge = atoi(token);
206 strncpy(u->fullname, token, sizeof(u->fullname));
209 u->msgnum_bio = atol(token);
212 u->msgnum_pic = atol(token);
215 CtdlDecodeBase64(u->emailaddrs, token, strlen(token));
218 u->msgnum_inboxrules = atol(token);
221 u->lastproc_inboxrules = atol(token);
226 makeuserkey(userkey, u->fullname);
227 out_key->size = strlen(userkey);
228 out_key->data = strdup(userkey);
229 out_data->size = sizeof(struct ctdluser);
235 // Convert a "room" record to a record on disk. (Source string is unusable after this function is called.)
236 int convert_room(char *line, DBT *out_key, DBT *out_data) {
238 struct ctdlroom *r = malloc(sizeof(struct ctdlroom));
240 memset(r, 0, sizeof(struct ctdlroom));
243 for (int i=0; (token = strsep(&p, "|")); ++i) {
246 strncpy(r->QRname, token, sizeof(r->QRname));
249 strncpy(r->QRpasswd, token, sizeof (r->QRpasswd));
252 r->QRroomaide = atol(token);
255 r->QRhighest = atol(token);
258 r->QRgen = atol(token);
261 r->QRflags = atoi(token);
264 strncpy(r->QRdirname, token, sizeof(r->QRdirname));
267 r->msgnum_info = atol(token);
270 r->QRfloor = atoi(token);
273 r->QRmtime = atol(token);
276 r->QRep.expire_mode = atoi(token);
279 r->QRep.expire_value = atoi(token);
282 r->QRnumber = atol(token);
285 r->QRorder = atoi(token);
288 r->QRflags2 = atoi(token);
291 r->QRdefaultview = atoi(token);
294 r->msgnum_pic = atol(token);
299 // The key is the room name in all lower case
300 out_key->size = strlen(r->QRname);
301 out_key->data = strdup(r->QRname);
302 char *k = (char *)out_key->data;
303 for (int i=0; i<=out_key->size; ++i) {
304 k[i] = tolower(k[i]);
307 out_data->size = sizeof(struct ctdlroom);
314 // Convert a "foo" record to a record on disk.
315 int convert_oom(char *line, DBT *out_key, DBT *out_data) {
317 struct ctdloom *r = malloc(sizeof(struct ctdloom));
319 memset(r, 0, sizeof(struct ctdloom));
322 for (int i=0; (token = strsep(&p, "|")); ++i) {
327 out_key->size = strlen(r->QRname);
328 out_key->data = strdup(r->QRname);
329 out_data->size = sizeof(struct ctdloom);
336 // Ingest one line of dump data. NOT REENTRANT
337 void ingest_one(char *line, DB_ENV *dst_dbenv) {
339 static int good_rows = 0;
340 static int bad_rows = 0;
341 static int previous_cdb = -1 ;
342 static int current_cdb = -1 ;
344 char record_type[32];
348 DBT out_key, out_data;
350 // We are assuming that the lines of the dump file will generally be sorted by table.
351 // By remembering the last table we worked with, we can do close/open if the table changes.
353 if (current_cdb >= 0) {
354 fprintf(stderr, " \033[33m%02x \033[32m%9d \033[31m%8d\033[0m\r", current_cdb, good_rows, bad_rows);
358 // Identify the record type we are currently working with
359 extract_token(record_type, line, 0, '|', sizeof record_type);
360 if (!strcasecmp(record_type, "msgtext")) current_cdb = CDB_MSGMAIN;
361 else if (!strcasecmp(record_type, "msgmeta")) current_cdb = CDB_MSGMAIN;
362 else if (!strcasecmp(record_type, "user")) current_cdb = CDB_USERS;
363 else if (!strcasecmp(record_type, "room")) current_cdb = CDB_ROOMS;
364 else if (!strcasecmp(record_type, "floor")) current_cdb = CDB_FLOORTAB;
365 else if (!strcasecmp(record_type, "msglist")) current_cdb = CDB_MSGLISTS;
366 else if (!strcasecmp(record_type, "visit")) current_cdb = CDB_VISIT;
367 else if (!strcasecmp(record_type, "dir")) current_cdb = CDB_DIRECTORY;
368 else if (!strcasecmp(record_type, "use")) current_cdb = CDB_USETABLE;
369 else if (!strcasecmp(record_type, "bigmsg")) current_cdb = CDB_BIGMSGS;
370 else if (!strcasecmp(record_type, "euidindex")) current_cdb = CDB_EUIDINDEX;
371 else if (!strcasecmp(record_type, "usersbynumber")) current_cdb = CDB_USERSBYNUMBER;
372 else if (!strcasecmp(record_type, "config")) current_cdb = CDB_CONFIG;
373 else current_cdb = -1 ;
375 if (current_cdb != previous_cdb) {
376 if (previous_cdb >= 0) {
377 fprintf(stderr, "\n");
379 if (previous_cdb >= 0) {
380 ret = dst_dbp->close(dst_dbp, 0);
382 fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
386 if (current_cdb >= 0) {
389 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", current_cdb);
391 // create a database handle for the destination table
392 ret = db_create(&dst_dbp, dst_dbenv, 0);
394 fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
395 fprintf(stderr, "db: exit code %d\n", ret);
399 // open the file containing the destination table
400 ret = dst_dbp->open(dst_dbp, NULL, dbfilename, NULL, DB_BTREE, (DB_CREATE | DB_TRUNCATE), 0600);
402 fprintf(stderr, "db: db_open: %s\n", db_strerror(ret));
403 fprintf(stderr, "db: exit code %d\n", ret);
408 previous_cdb = current_cdb;
411 // If we have a valid record type and a target database open, dispatch the correct record type handler.
412 memset(&out_key, 0, sizeof(DBT));
413 memset(&out_data, 0, sizeof(DBT));
415 if (!strcasecmp(record_type, "msgtext")) row_was_good = convert_msgtext(line, &out_key, &out_data);
416 else if (!strcasecmp(record_type, "msgmeta")) row_was_good = convert_msgmeta(line, &out_key, &out_data);
417 else if (!strcasecmp(record_type, "user")) row_was_good = convert_user(line, &out_key, &out_data);
418 else if (!strcasecmp(record_type, "room")) row_was_good = convert_room(line, &out_key, &out_data);
419 else if (!strcasecmp(record_type, "floor")) row_was_good = convert_foo(line, &out_key, &out_data);
420 else if (!strcasecmp(record_type, "msglist")) row_was_good = convert_foo(line, &out_key, &out_data);
421 else if (!strcasecmp(record_type, "visit")) row_was_good = convert_foo(line, &out_key, &out_data);
422 else if (!strcasecmp(record_type, "dir")) row_was_good = convert_foo(line, &out_key, &out_data);
423 else if (!strcasecmp(record_type, "use")) row_was_good = convert_foo(line, &out_key, &out_data);
424 else if (!strcasecmp(record_type, "bigmsg")) row_was_good = convert_foo(line, &out_key, &out_data);
425 else if (!strcasecmp(record_type, "euidindex")) row_was_good = convert_foo(line, &out_key, &out_data);
426 else if (!strcasecmp(record_type, "usersbynumber")) row_was_good = convert_foo(line, &out_key, &out_data);
427 else if (!strcasecmp(record_type, "config")) row_was_good = convert_foo(line, &out_key, &out_data);
428 else row_was_good = 0;
432 ret = dst_dbp->put(dst_dbp, NULL, &out_key, &out_data, 0);
434 fprintf(stderr, "db: cdb_put(%x): %s", current_cdb, db_strerror(ret));
447 // This is the loop that loads the dump data. NOT REENTRANT
448 void ingest(DB_ENV *dst_dbenv) {
449 static size_t line_alloc = 1;
451 static size_t line_len = 0;
454 fprintf(stderr, "\033[7mtable\033[0m \033[7mgood_rows\033[0m \033[7mbad_rows\033[0m\n");
455 line = reallok(NULL, line_alloc);
460 while (ch = getc(stdin), ((ch != '\n') && (ch > 0))) {
461 if ((line_len+2) > line_alloc) {
463 line = reallok(line, line_alloc);
465 line[line_len++] = ch;
474 ingest_one(line, dst_dbenv);
482 int main(int argc, char **argv) {
483 char *dst_dir = NULL;
485 static DB_ENV *dst_dbenv; // Source DB environment (global)
487 // Parse command line
489 while ((a = getopt(argc, argv, "h:y")) != EOF) {
498 fprintf(stderr, "%s: usage: %s -h dest_dir [<dumpfile]\n", argv[0], argv[0]);
503 if (confirmed == 1) {
504 fprintf(stderr,"You have specified the [-y] flag, so processing will continue.\n");
507 fprintf(stderr,"Please read [ https://www.citadel.org/ctdlload.html ] to learn how to proceed.\n");
512 snprintf(cmd, sizeof cmd, "rm -fv %s/cdb.* %s/log.*", dst_dir, dst_dir);
515 dst_dbenv = open_dbenv(dst_dir);
517 close_dbenv(dst_dbenv);