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 // Convert a "msgtext" record to a message on disk. NOT THREADSAFE
115 // This also works for "bigmsg" records.
116 int convert_msgtext(char *line, DBT *out_key, DBT *out_data) {
118 static char *b64_decoded_msg = NULL;
119 static size_t b64_decoded_alloc = 0;
123 token = strtok(line, "|");
124 msgnum = atol(strtok(NULL, "|"));
125 token = strtok(NULL, "|");
127 // The record key will be the message number
128 out_key->size = sizeof(long);
129 out_key->data = reallok(out_key->data, out_key->size);
130 memcpy(out_key->data, &msgnum, out_key->size);
132 // The record data will be the decoded message text.
133 // We are allocating more memory than we need, but BDB will only write the number of bytes we tell it to.
134 out_data->data = reallok(out_data->data, strlen(token));
135 out_data->size = CtdlDecodeBase64(out_data->data, token, strlen(token));
140 // Convert a "msgmeta" record to a message metadata record on disk. NOT THREADSAFE
141 int convert_msgmeta(char *line, DBT *out_key, DBT *out_data) {
143 struct MetaData *m = malloc(sizeof(struct MetaData));
145 memset(m, 0, sizeof(struct MetaData));
148 for (int i=0; (token = strsep(&p, "|")); ++i) {
151 m->meta_msgnum = atol(token);
154 m->meta_refcount = atoi(token);
157 strncpy(m->meta_content_type, token, sizeof(m->meta_content_type));
160 m->meta_rfc822_length = atol(token);
165 // metadata records are stored in the CDB_MSGMAIN table,
166 // but with the index being the *negative* of the message number.
167 long index = 0 - m->meta_msgnum;
168 out_key->size = sizeof(long);
169 out_key->data = reallok(NULL, out_key->size);
170 memcpy(out_key->data, &index, out_key->size);
173 out_data->size = sizeof(struct MetaData);
174 out_data->data = m; // out_data owns this memory now
180 // Convert a "user" record to a record on disk. (Source string is unusable after this function is called.)
181 int convert_user(char *line, DBT *out_key, DBT *out_data) {
182 char userkey[USERNAME_SIZE];
184 struct ctdluser *u = malloc(sizeof(struct ctdluser));
186 memset(u, 0, sizeof(struct ctdluser));
189 for (int i=0; (token = strsep(&p, "|")); ++i) {
192 u->version = atoi(token);
195 u->uid = atoi(token);
198 strncpy(u->password, token, sizeof(u->password));
201 u->flags = atoi(token);
204 u->axlevel = atoi(token);
207 u->usernum = atol(token);
210 u->lastcall = atol(token);
213 u->USuserpurge = atoi(token);
216 strncpy(u->fullname, token, sizeof(u->fullname));
219 u->msgnum_bio = atol(token);
222 u->msgnum_pic = atol(token);
225 CtdlDecodeBase64(u->emailaddrs, token, strlen(token));
228 u->msgnum_inboxrules = atol(token);
231 u->lastproc_inboxrules = atol(token);
236 makeuserkey(userkey, u->fullname);
237 out_key->size = strlen(userkey);
238 out_key->data = strdup(userkey);
239 out_data->size = sizeof(struct ctdluser);
245 // Convert a "room" record to a record on disk. (Source string is unusable after this function is called.)
246 int convert_room(char *line, DBT *out_key, DBT *out_data) {
248 struct ctdlroom *r = malloc(sizeof(struct ctdlroom));
250 memset(r, 0, sizeof(struct ctdlroom));
253 for (int i=0; (token = strsep(&p, "|")); ++i) {
256 strncpy(r->QRname, token, sizeof(r->QRname));
259 strncpy(r->QRpasswd, token, sizeof (r->QRpasswd));
262 r->QRroomaide = atol(token);
265 r->QRhighest = atol(token);
268 r->QRgen = atol(token);
271 r->QRflags = atoi(token);
274 strncpy(r->QRdirname, token, sizeof(r->QRdirname));
277 r->msgnum_info = atol(token);
280 r->QRfloor = atoi(token);
283 r->QRmtime = atol(token);
286 r->QRep.expire_mode = atoi(token);
289 r->QRep.expire_value = atoi(token);
292 r->QRnumber = atol(token);
295 r->QRorder = atoi(token);
298 r->QRflags2 = atoi(token);
301 r->QRdefaultview = atoi(token);
304 r->msgnum_pic = atol(token);
309 // The key is the room name in all lower case
310 out_key->size = strlen(r->QRname);
311 out_key->data = strdup(r->QRname);
312 char *k = (char *)out_key->data;
313 for (int i=0; i<=out_key->size; ++i) {
314 k[i] = tolower(k[i]);
317 out_data->size = sizeof(struct ctdlroom);
323 // Convert a floor record to a record on disk.
324 int convert_floor(char *line, DBT *out_key, DBT *out_data) {
326 struct floor *f = malloc(sizeof(struct floor));
329 memset(f, 0, sizeof(struct floor));
332 for (int i=0; (token = strsep(&p, "|")); ++i) {
335 floor_num = atoi(token);
338 f->f_flags = atoi(token);
341 strncpy(f->f_name, token, sizeof(f->f_name));
344 f->f_ref_count = atoi(token);
347 f->f_ep.expire_mode = atoi(token);
350 f->f_ep.expire_value = atoi(token);
355 out_key->size = sizeof(int);
356 out_key->data = malloc(out_key->size);
357 memcpy(out_key->data, &floor_num, out_key->size);
359 out_data->size = sizeof(struct floor);
365 // Import a msglist record
366 // msglist|26|32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51|
367 int convert_msglist(char *line, DBT *out_key, DBT *out_data) {
369 char *token, *mtoken;
373 long *msglist = NULL;
375 for (int i=0; (token = strsep(&p, "|")); ++i) {
378 roomnum = atol(token);
382 for (int j=0; (mtoken = strsep(&q, ",")); ++j) {
383 msglist = realloc(msglist, (num_msgs+1) * sizeof(long));
384 msglist[num_msgs++] = atol(mtoken);
390 out_key->size = sizeof(long);
391 out_key->data = malloc(out_key->size);
392 memcpy(out_key->data, &roomnum, out_key->size);
394 out_data->size = num_msgs * sizeof(long);
395 out_data->data = msglist;
401 // Convert a "visit" record to a record on disk.
402 int convert_visit(char *line, DBT *out_key, DBT *out_data) {
404 struct visit *v = malloc(sizeof(struct visit));
406 memset(v, 0, sizeof(struct visit));
409 for (int i=0; (token = strsep(&p, "|")); ++i) {
412 v->v_roomnum = atol(token);
415 v->v_roomgen = atol(token);
418 v->v_usernum = atol(token);
421 v->v_lastseen = atoi(token);
424 v->v_flags = atoi(token);
427 strncpy(v->v_seen, token, sizeof(v->v_seen));
430 strncpy(v->v_answered, token, sizeof(v->v_answered));
433 v->v_view = atoi(token);
438 // The key is the same as the first three data members (3 x long)
439 out_key->size = sizeof(long) * 3;
440 out_key->data = reallok(NULL, out_key->size);
441 memcpy(out_key->data, v, out_key->size);
443 out_data->size = sizeof(struct visit);
449 // Convert a "usetable" record to a record on disk.
450 int convert_usetable(char *line, DBT *out_key, DBT *out_data) {
452 struct UseTable *u = malloc(sizeof(struct UseTable));
454 memset(u, 0, sizeof(struct UseTable));
457 for (int i=0; (token = strsep(&p, "|")); ++i) {
460 u->hash = atoi(token);
463 u->timestamp = atol(token);
468 // the key is just an int (the hash)
469 out_key->size = sizeof(int);
470 out_key->data = reallok(NULL, out_key->size);
471 memcpy(out_key->data, &u->hash, out_key->size);
473 out_data->size = sizeof(struct UseTable);
479 // Import a config record
480 // The key is the config key
481 // The data is the config key, a null, the value, and another null
482 int convert_config(char *line, DBT *out_key, DBT *out_data) {
487 for (int i=0; (token = strsep(&p, "|")); ++i) {
491 out_key->size = strlen(token);
492 out_key->data = strdup(token);
496 out_data->size = strlen(k) + strlen(v) + 2;
497 out_data->data = reallok(NULL, out_data->size);
498 strcpy(out_data->data, k);
499 strcpy(out_data->data + strlen(k) + 1, v);
508 // Ingest one line of dump data. NOT REENTRANT
509 void ingest_one(char *line, DB_ENV *dst_dbenv) {
511 static int good_rows = 0;
512 static int bad_rows = 0;
513 static int previous_cdb = -1 ;
514 static int current_cdb = -1 ;
516 char record_type[32];
520 DBT out_key, out_data;
522 // We are assuming that the lines of the dump file will generally be sorted by table.
523 // By remembering the last table we worked with, we can do close/open if the table changes.
525 if (current_cdb >= 0) {
526 fprintf(stderr, " \033[33m%02x \033[32m%9d \033[31m%8d\033[0m\r", current_cdb, good_rows, bad_rows);
530 // Identify the record type we are currently working with
531 extract_token(record_type, line, 0, '|', sizeof record_type);
532 if (!strcasecmp(record_type, "msgtext")) current_cdb = CDB_MSGMAIN;
533 else if (!strcasecmp(record_type, "msgmeta")) current_cdb = CDB_MSGMAIN;
534 else if (!strcasecmp(record_type, "user")) current_cdb = CDB_USERS;
535 else if (!strcasecmp(record_type, "room")) current_cdb = CDB_ROOMS;
536 else if (!strcasecmp(record_type, "floor")) current_cdb = CDB_FLOORTAB;
537 else if (!strcasecmp(record_type, "msglist")) current_cdb = CDB_MSGLISTS;
538 else if (!strcasecmp(record_type, "visit")) current_cdb = CDB_VISIT;
539 else if (!strcasecmp(record_type, "dir")) current_cdb = CDB_DIRECTORY;
540 else if (!strcasecmp(record_type, "use")) current_cdb = CDB_USETABLE;
541 else if (!strcasecmp(record_type, "bigmsg")) current_cdb = CDB_BIGMSGS;
542 else if (!strcasecmp(record_type, "euidindex")) current_cdb = CDB_EUIDINDEX;
543 else if (!strcasecmp(record_type, "usersbynumber")) current_cdb = CDB_USERSBYNUMBER;
544 else if (!strcasecmp(record_type, "config")) current_cdb = CDB_CONFIG;
545 else current_cdb = -1 ;
547 if (current_cdb != previous_cdb) {
548 if (previous_cdb >= 0) {
549 fprintf(stderr, "\n");
551 if (previous_cdb >= 0) {
552 ret = dst_dbp->close(dst_dbp, 0);
554 fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
558 if (current_cdb >= 0) {
561 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", current_cdb);
563 // create a database handle for the destination table
564 ret = db_create(&dst_dbp, dst_dbenv, 0);
566 fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
567 fprintf(stderr, "db: exit code %d\n", ret);
571 // open the file containing the destination table
572 ret = dst_dbp->open(dst_dbp, NULL, dbfilename, NULL, DB_BTREE, (DB_CREATE | DB_TRUNCATE), 0600);
574 fprintf(stderr, "db: db_open: %s\n", db_strerror(ret));
575 fprintf(stderr, "db: exit code %d\n", ret);
580 previous_cdb = current_cdb;
583 // If we have a valid record type and a target database open, dispatch the correct record type handler.
584 memset(&out_key, 0, sizeof(DBT));
585 memset(&out_data, 0, sizeof(DBT));
587 if (!strcasecmp(record_type, "msgtext")) row_was_good = convert_msgtext(line, &out_key, &out_data);
588 else if (!strcasecmp(record_type, "msgmeta")) row_was_good = convert_msgmeta(line, &out_key, &out_data);
589 else if (!strcasecmp(record_type, "user")) row_was_good = convert_user(line, &out_key, &out_data);
590 else if (!strcasecmp(record_type, "room")) row_was_good = convert_room(line, &out_key, &out_data);
591 else if (!strcasecmp(record_type, "floor")) row_was_good = convert_floor(line, &out_key, &out_data);
592 else if (!strcasecmp(record_type, "msglist")) row_was_good = convert_msglist(line, &out_key, &out_data);
593 else if (!strcasecmp(record_type, "visit")) row_was_good = convert_visit(line, &out_key, &out_data);
594 else if (!strcasecmp(record_type, "use")) row_was_good = convert_usetable(line, &out_key, &out_data);
595 else if (!strcasecmp(record_type, "bigmsg")) row_was_good = convert_msgtext(line, &out_key, &out_data);
596 else if (!strcasecmp(record_type, "config")) row_was_good = convert_config(line, &out_key, &out_data);
597 else row_was_good = 0;
601 ret = dst_dbp->put(dst_dbp, NULL, &out_key, &out_data, 0);
603 fprintf(stderr, "db: cdb_put(%x): %s", current_cdb, db_strerror(ret));
616 // This is the loop that loads the dump data. NOT REENTRANT
617 void ingest(DB_ENV *dst_dbenv) {
618 static size_t line_alloc = 1;
620 static size_t line_len = 0;
623 fprintf(stderr, "\033[7mtable\033[0m \033[7mgood_rows\033[0m \033[7mbad_rows\033[0m\n");
624 line = reallok(NULL, line_alloc);
629 while (ch = getc(stdin), ((ch != '\n') && (ch > 0))) {
630 if ((line_len+2) > line_alloc) {
632 line = reallok(line, line_alloc);
634 line[line_len++] = ch;
643 ingest_one(line, dst_dbenv);
651 int main(int argc, char **argv) {
652 char *dst_dir = NULL;
654 static DB_ENV *dst_dbenv; // Source DB environment (global)
656 // display the greeting
657 fprintf(stderr, "\033[44m\033[33m\033[1m \033[K\033[0m\n"
658 "\033[44m\033[33m\033[1m DB Load utility for Citadel \033[K\033[0m\n"
659 "\033[44m\033[33m\033[1m Copyright (c) 2023 by citadel.org et al. \033[K\033[0m\n"
660 "\033[44m\033[33m\033[1m This program is open source software. Use, duplication, or disclosure \033[K\033[0m\n"
661 "\033[44m\033[33m\033[1m is subject to the terms of the GNU General Public license v3. \033[K\033[0m\n"
662 "\033[44m\033[33m\033[1m \033[K\033[0m\n");
664 // Parse command line
666 while ((a = getopt(argc, argv, "h:y")) != EOF) {
675 fprintf(stderr, "%s: usage: %s -h dest_dir [<dumpfile]\n", argv[0], argv[0]);
680 if (confirmed == 1) {
681 fprintf(stderr, "ctdlload: You have specified the [-y] flag, so processing will continue.\n");
684 fprintf(stderr, "ctdlload: Please read [ https://www.citadel.org/dump-and-load.html ] to learn how to proceed.\n");
689 snprintf(cmd, sizeof cmd, "rm -fv %s/cdb.* %s/log.*", dst_dir, dst_dir);
692 dst_dbenv = open_dbenv(dst_dir);
694 close_dbenv(dst_dbenv);