1 // Load (restore) the Citadel database from a flat file created by ctdldump
3 // Copyright (c) 2023-2024 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>
23 #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"
30 #include "../server/database.h"
34 // Wrapper for realloc() that crashes and burns if the call fails.
35 void *reallok(void *ptr, size_t size) {
36 void *p = realloc(ptr, size);
38 fprintf(stderr, "realloc() failed to resize %p to %ld bytes, error: %m\n", ptr, size);
45 // Convert a "msgtext" record to a message on disk. NOT THREADSAFE
46 // This also works for "bigmsg" records.
47 int import_msgtext(char *line, struct cdbkeyval *kv) {
49 static char *b64_decoded_msg = NULL;
50 static size_t b64_decoded_alloc = 0;
54 token = strtok(line, "|");
55 msgnum = atol(strtok(NULL, "|"));
56 token = strtok(NULL, "|");
58 // The record key will be the message number
59 kv->key.len = sizeof(long);
60 kv->key.ptr = reallok(kv->key.ptr, kv->key.len);
61 memcpy(kv->key.ptr, &msgnum, kv->key.len);
63 // The record data will be the decoded message text.
64 // We are allocating more memory than we need, but BDB will only write the number of bytes we tell it to.
65 kv->val.ptr = (char *) reallok(kv->val.ptr, strlen(token));
66 kv->val.len = CtdlDecodeBase64(kv->val.ptr, token, strlen(token));
71 // Convert a "msgmeta" record to a message metadata record on disk. NOT THREADSAFE
72 int import_msgmeta(char *line, struct cdbkeyval *kv) {
76 m = malloc(sizeof(struct MetaData));
78 fprintf(stderr, "import_msgmeta: malloc: %s\n", strerror(errno));
81 memset(m, 0, sizeof(struct MetaData));
84 for (int i=0; (token = strsep(&p, "|")); ++i) {
87 m->meta_msgnum = atol(token);
90 m->meta_refcount = atoi(token);
93 strncpy(m->meta_content_type, token, sizeof(m->meta_content_type));
96 m->meta_rfc822_length = atol(token);
101 // metadata records are stored in the CDB_MSGMAIN table,
102 // but with the index being the *negative* of the message number.
103 long index = 0 - m->meta_msgnum;
104 kv->key.len = sizeof(long);
105 kv->key.ptr = reallok(NULL, kv->key.len);
106 memcpy(kv->key.ptr, &index, kv->key.len);
109 kv->val.len = sizeof(struct MetaData);
110 kv->val.ptr = (char *) m; // out_data owns this memory now
116 // Convert a "user" record to a record on disk. (Source string is unusable after this function is called.)
117 int import_user(char *line, struct cdbkeyval *kv) {
118 char userkey[USERNAME_SIZE];
122 u = malloc(sizeof(struct ctdluser));
124 fprintf(stderr, "malloc failed\n");
127 memset(u, 0, sizeof(struct ctdluser));
130 for (int i=0; (token = strsep(&p, "|")); ++i) {
133 u->version = atoi(token);
136 u->uid = atoi(token);
139 safestrncpy(u->password, token, sizeof(u->password));
142 u->flags = atoi(token);
145 u->axlevel = atoi(token);
148 u->usernum = atol(token);
151 u->lastcall = atol(token);
154 u->USuserpurge = atoi(token);
157 safestrncpy(u->fullname, token, sizeof(u->fullname));
160 u->msgnum_bio = atol(token);
163 u->msgnum_pic = atol(token);
166 CtdlDecodeBase64(token, token, strlen(token)); // Decode in place
167 safestrncpy(u->emailaddrs, token, sizeof(u->emailaddrs));
170 u->msgnum_inboxrules = atol(token);
173 u->lastproc_inboxrules = atol(token);
178 // reject any user records without names
179 if (strlen(u->fullname) == 0) {
185 makeuserkey(userkey, u->fullname);
186 kv->key.len = strlen(userkey);
187 kv->key.ptr = strdup(userkey);
188 kv->val.len = sizeof(struct ctdluser);
189 kv->val.ptr = (char *) u;
194 // Convert a "room" record to a record on disk. (Source string is unusable after this function is called.)
195 int import_room(char *line, struct cdbkeyval *kv) {
199 r = malloc(sizeof(struct ctdlroom));
201 fprintf(stderr, "import_room: malloc: %s\n", strerror(errno));
204 memset(r, 0, sizeof(struct ctdlroom));
207 for (int i=0; (token = strsep(&p, "|")); ++i) {
210 strncpy(r->QRname, token, sizeof(r->QRname));
213 strncpy(r->QRpasswd, token, sizeof (r->QRpasswd));
216 r->QRroomaide = atol(token);
219 r->QRhighest = atol(token);
222 r->QRgen = atol(token);
225 r->QRflags = atoi(token);
228 strncpy(r->QRdirname, token, sizeof(r->QRdirname));
231 r->msgnum_info = atol(token);
234 r->QRfloor = atoi(token);
237 r->QRmtime = atol(token);
240 r->QRep.expire_mode = atoi(token);
243 r->QRep.expire_value = atoi(token);
246 r->QRnumber = atol(token);
249 r->QRorder = atoi(token);
252 r->QRflags2 = atoi(token);
255 r->QRdefaultview = atoi(token);
258 r->msgnum_pic = atol(token);
263 // The key is the room name in all lower case
264 kv->key.len = strlen(r->QRname);
265 kv->key.ptr = strdup(r->QRname);
266 char *k = (char *)kv->key.ptr;
267 for (int i=0; i<=kv->key.len; ++i) {
268 k[i] = tolower(k[i]);
271 kv->val.len = sizeof(struct ctdlroom);
272 kv->val.ptr = (char *) r;
277 // Convert a floor record to a record on disk.
278 int import_floor(char *line, struct cdbkeyval *kv) {
283 f = malloc(sizeof(struct floor));
285 fprintf(stderr, "import_floor: malloc: %s\n", strerror(errno));
288 memset(f, 0, sizeof(struct floor));
291 for (int i=0; (token = strsep(&p, "|")); ++i) {
294 floor_num = atoi(token);
297 f->f_flags = atoi(token);
300 strncpy(f->f_name, token, sizeof(f->f_name));
303 f->f_ref_count = atoi(token);
306 f->f_ep.expire_mode = atoi(token);
309 f->f_ep.expire_value = atoi(token);
314 kv->key.len = sizeof(int);
315 kv->key.ptr = malloc(kv->key.len);
316 memcpy(kv->key.ptr, &floor_num, kv->key.len);
318 kv->val.len = sizeof(struct floor);
319 kv->val.ptr = (char *) f;
324 // Import a msglist record
325 // msglist|26|32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51|
326 int import_msglist(char *line, struct cdbkeyval *kv) {
328 char *token, *mtoken;
332 long *msglist = NULL;
334 for (int i=0; (token = strsep(&p, "|")); ++i) {
337 roomnum = atol(token);
341 for (int j=0; (mtoken = strsep(&q, ",")); ++j) {
342 msglist = realloc(msglist, (num_msgs+1) * sizeof(long));
343 msglist[num_msgs++] = atol(mtoken);
349 kv->key.len = sizeof(long);
350 kv->key.ptr = malloc(kv->key.len);
351 memcpy(kv->key.ptr, &roomnum, kv->key.len);
353 kv->val.len = num_msgs * sizeof(long);
354 kv->val.ptr = (char *) msglist;
360 // Convert a "visit" record to a record on disk.
361 int import_visit(char *line, struct cdbkeyval *kv) {
365 v = malloc(sizeof(struct visit));
367 fprintf(stderr, "import_visit: malloc: %s\n", strerror(errno));
370 memset(v, 0, sizeof(struct visit));
373 for (int i=0; (token = strsep(&p, "|")); ++i) {
376 v->v_roomnum = atol(token);
379 v->v_roomgen = atol(token);
382 v->v_usernum = atol(token);
385 v->v_lastseen = atoi(token);
388 v->v_flags = atoi(token);
391 strncpy(v->v_seen, token, sizeof(v->v_seen));
394 strncpy(v->v_answered, token, sizeof(v->v_answered));
397 v->v_view = atoi(token);
402 // The key is the same as the first three data members (3 x long)
403 kv->key.len = sizeof(long) * 3;
404 kv->key.ptr = reallok(NULL, kv->key.len);
405 memcpy(kv->key.ptr, v, kv->key.len);
407 kv->val.len = sizeof(struct visit);
408 kv->val.ptr = (char *) v;
413 // Convert a "dir" record to a record on disk.
414 int import_dir(char *line, struct cdbkeyval *kv) {
416 char username[USERNAME_SIZE];
420 for (int i=0; (token = strsep(&p, "|")); ++i) {
423 strncpy(dirkey, token, sizeof(dirkey));
426 strncpy(username, token, sizeof(username));
431 kv->key.len = strlen(dirkey);
432 kv->key.ptr = reallok(NULL, kv->key.len);
433 memcpy(kv->key.ptr, dirkey, strlen(dirkey));
435 kv->val.len = strlen(username) + 1;
436 kv->val.ptr = strdup(username);
442 // Convert a "usetable" record to a record on disk.
443 int import_usetable(char *line, struct cdbkeyval *kv) {
447 u = malloc(sizeof(struct UseTable));
449 fprintf(stderr, "import_usetable: malloc: %s\n", strerror(errno));
452 memset(u, 0, sizeof(struct UseTable));
455 for (int i=0; (token = strsep(&p, "|")); ++i) {
458 u->hash = atoi(token);
461 u->timestamp = atol(token);
466 // the key is just an int (the hash)
467 kv->key.len = sizeof(int);
468 kv->key.ptr = reallok(NULL, kv->key.len);
469 memcpy(kv->key.ptr, &u->hash, kv->key.len);
471 kv->val.len = sizeof(struct UseTable);
472 kv->val.ptr = (char *) u;
477 // Import a full text search index record.
478 // It's just like a msglists record: a key and a list of message numbers, but the key is "int" instead of "long"
479 int import_fulltext(char *line, struct cdbkeyval *kv) {
481 char *token, *mtoken;
485 long *msglist = NULL;
487 for (int i=0; (token = strsep(&p, "|")); ++i) {
490 indexnum = atoi(token);
494 for (int j=0; (mtoken = strsep(&q, ",")); ++j) {
495 msglist = realloc(msglist, (num_msgs+1) * sizeof(long));
496 msglist[num_msgs++] = atol(mtoken);
502 kv->key.len = sizeof(int);
503 kv->key.ptr = malloc(kv->key.len);
504 memcpy(kv->key.ptr, &indexnum, kv->key.len);
506 kv->val.len = num_msgs * sizeof(long);
507 kv->val.ptr = (char *) msglist;
513 // Import an EUID Index record
514 // euidindex|msgnum|roomnum|euid|
515 int import_euidindex(char *line, struct cdbkeyval *kv) {
522 for (int i=0; (token = strsep(&p, "|")); ++i) {
525 msgnum = atol(token);
528 roomnum = atol(token);
531 strncpy(euid, token, sizeof(euid));
536 // The structure of an euidindex record *key* is:
537 // |----room_number----|----------EUID-------------|
538 // (sizeof long) (actual length of euid)
539 kv->key.len = sizeof(long) + strlen(euid) + 1;
540 kv->key.ptr = reallok(NULL, kv->key.len);
541 memcpy(kv->key.ptr, &roomnum, sizeof(long));
542 strcpy(kv->key.ptr + sizeof(long), euid);
544 // The structure of an euidindex record *value* is:
545 // |-----msg_number----|----room_number----|----------EUID-------------|
546 // (sizeof long) (sizeof long) (actual length of euid)
547 kv->val.len = sizeof(long) + sizeof(long) + strlen(euid) + 1;
548 kv->val.ptr = (char *) reallok(NULL, kv->val.len);
549 memcpy(kv->val.ptr, &msgnum, sizeof(long));
550 memcpy(kv->val.ptr + sizeof(long), &roomnum, sizeof(long));
551 strcpy(kv->val.ptr + sizeof(long) + sizeof(long), euid);
557 // Import a "users by number" (secondary index) record
558 // The key is a "long"
559 int import_usersbynumber(char *line, struct cdbkeyval *kv) {
564 for (int i=0; (token = strsep(&p, "|")); ++i) {
567 usernum = atol(token);
570 kv->key.len = sizeof(long);
571 kv->key.ptr = reallok(NULL, sizeof(long));
572 memcpy(kv->key.ptr, &usernum, kv->key.len);
573 kv->val.ptr = (char *) strdup(token);
574 kv->val.len = strlen(kv->val.ptr) + 1;
579 return(0); // should never get here unless it's a bad record
583 // Import a config record
584 // The key is the config key
585 // The data is the config key, a null, the value, and another null
586 int import_config(char *line, struct cdbkeyval *kv) {
591 for (int i=0; (token = strsep(&p, "|")); ++i) {
595 kv->key.len = strlen(token);
596 kv->key.ptr = strdup(token);
600 kv->val.len = strlen(k) + strlen(v) + 2;
601 kv->val.ptr = (char *) reallok(NULL, kv->val.len);
602 strcpy(kv->val.ptr, k);
603 strcpy(kv->val.ptr + strlen(k) + 1, v);
612 // Ingest one line of dump data. NOT REENTRANT
613 void ingest_one(char *line, struct cdbkeyval *kv) {
615 static int good_rows = 0;
616 static int bad_rows = 0;
617 static int current_cdb = -1 ;
618 char record_type[32];
621 // Clear out our record buffer
622 memset(kv, 0, sizeof(struct cdbkeyval));
625 // Identify the record type we are currently working with,
626 // then call the correct conversion function to load up our record buffer.
627 extract_token(record_type, line, 0, '|', sizeof record_type);
628 if (!strcasecmp(record_type, "msgtext")) {
629 current_cdb = CDB_MSGMAIN;
630 row_was_good = import_msgtext(line, kv);
632 else if (!strcasecmp(record_type, "msgmeta")) {
633 current_cdb = CDB_MSGMAIN;
634 row_was_good = import_msgmeta(line, kv);
636 else if (!strcasecmp(record_type, "user")) {
637 current_cdb = CDB_USERS;
638 row_was_good = import_user(line, kv);
640 else if (!strcasecmp(record_type, "room")) {
641 current_cdb = CDB_ROOMS;
642 row_was_good = import_room(line, kv);
644 else if (!strcasecmp(record_type, "floor")) {
645 current_cdb = CDB_FLOORTAB;
646 row_was_good = import_floor(line, kv);
648 else if (!strcasecmp(record_type, "msglist")) {
649 current_cdb = CDB_MSGLISTS;
650 row_was_good = import_msglist(line, kv);
652 else if (!strcasecmp(record_type, "visit")) {
653 current_cdb = CDB_VISIT;
654 row_was_good = import_visit(line, kv);
656 else if (!strcasecmp(record_type, "dir")) {
657 current_cdb = CDB_DIRECTORY;
658 row_was_good = import_dir(line, kv);
660 else if (!strcasecmp(record_type, "use")) {
661 current_cdb = CDB_USETABLE;
662 row_was_good = import_usetable(line, kv);
664 else if (!strcasecmp(record_type, "bigmsg")) {
665 current_cdb = CDB_BIGMSGS;
666 row_was_good = import_msgtext(line, kv);
668 else if (!strcasecmp(record_type, "fulltext")) {
669 current_cdb = CDB_FULLTEXT;
670 row_was_good = import_fulltext(line, kv);
672 else if (!strcasecmp(record_type, "euidindex")) {
673 current_cdb = CDB_EUIDINDEX;
674 row_was_good = import_euidindex(line, kv);
676 else if (!strcasecmp(record_type, "usersbynumber")) {
677 current_cdb = CDB_USERSBYNUMBER;
678 row_was_good = import_usersbynumber(line, kv);
680 else if (!strcasecmp(record_type, "config")) {
681 current_cdb = CDB_CONFIG;
682 row_was_good = import_config(line, kv);
688 // If the conversion function was successful, write the record to the database.
689 if ( (row_was_good) && (current_cdb >= 0) ) {
690 cdb_store(current_cdb, kv->key.ptr, kv->key.len, kv->val.ptr, kv->val.len);
697 fprintf(stderr, "\r \033[32m%9d\033[0m / \033[31m%8d\033[0m ", good_rows, bad_rows);
703 // This is the loop that loads the dump data. NOT REENTRANT
705 static size_t line_alloc = 1;
707 static size_t line_len = 0;
713 memset(&kv, 0, sizeof(struct cdbkeyval));
715 line = reallok(NULL, line_alloc);
720 while (ch = getc(stdin), ((ch != '\n') && (ch > 0))) {
721 if ((line_len+2) > line_alloc) {
723 line = reallok(line, line_alloc);
725 line[line_len++] = ch;
734 if ( (begin_found) && (!end_found) ) {
735 ingest_one(line, &kv);
737 if (!strncasecmp(line, HKEY("begin|"))) {
739 fprintf(stderr, " good rows / bad rows:\n");
741 if (!strncasecmp(line, HKEY("end|"))) {
742 fprintf(stderr, "\n");
750 fprintf(stderr, "\033[31mWARNING: \"begin\" line was not found in the loaded data.\033[0m\n");
753 fprintf(stderr, "\033[31mWARNING: \"end\" line was not found in the loaded data.\033[0m\n");
759 int main(int argc, char **argv) {
761 char *ctdldir = CTDLDIR;
763 // display the greeting
765 "\033[44m\033[1m╔════════════════════════════════════════════════════════════════════════╗\033[0m\n"
766 "\033[44m\033[1m║ DB Load utility for Citadel ║\033[0m\n"
767 "\033[44m\033[1m║ Copyright (c) 2023-2024 by citadel.org et al. ║\033[0m\n"
768 "\033[44m\033[1m║ This program is open source software. Use, duplication, or disclosure ║\033[0m\n"
769 "\033[44m\033[1m║ is subject to the terms of the GNU General Public license v3. ║\033[0m\n"
770 "\033[44m\033[1m╚════════════════════════════════════════════════════════════════════════╝\033[0m\n"
773 // Parse command line
775 while ((a = getopt(argc, argv, "h:y")) != EOF) {
784 fprintf(stderr, "%s: usage: %s -h citadel_dir [<dumpfile]\n", argv[0], argv[0]);
789 if (confirmed == 1) {
790 fprintf(stderr, "ctdlload: You have specified the [-y] flag, so processing will continue.\n");
793 fprintf(stderr, "ctdlload: usage: ctdlload -y -h[citadel_dir] <[dump_file]\n");
794 fprintf(stderr, " -y : yes, I know this program can do damage and I want to run it anyway.\n");
795 fprintf(stderr, " -h : [citadel_dir] is your server directory, usually /usr/local/citadel\n");
796 fprintf(stderr, " Please read [ https://www.citadel.org/dump-and-load.html ] to learn how to proceed.\n");
800 if (chdir(ctdldir) != 0) {
801 fprintf(stderr, "ctdlload: unable to change directory to [%s]: %m", ctdldir);
805 // backend modules use syslog -- redirect to stderr
806 openlog("ctdlload", LOG_PERROR , LOG_DAEMON);
808 // Remove any database that is already in the target directory (yes, delete it, be careful)
809 system("rm -fv ./data/*");
811 // initialize the database backend
813 cdb_open_databases();
815 // Load rows from the input source
819 cdb_close_databases();
821 fprintf(stderr, "ctdlload: \033[32m\033[1mfinished\033[0m\n");