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/citadel_dirs.h"
31 // Wrapper for realloc() that crashes and burns if the call fails.
32 void *reallok(void *ptr, size_t size) {
33 void *p = realloc(ptr, size);
35 fprintf(stderr, "realloc() failed to resize %p to %ld bytes, error: %m\n", ptr, size);
40 #define realloc reallok
43 // convert a binary blob to hex (non-reentrant!!!)
44 char *hexout(void *data, size_t len) {
45 static char *outbuf = NULL;
46 static size_t outlen = 0;
50 if ((outbuf == NULL) || (outlen < (len * 2))) {
51 outbuf = reallok(outbuf, (len * 2));
55 for (i=0; i<len; ++i) {
57 memcpy(&ch, data+i, 1);
58 sprintf((outbuf + (i * 2)), "%02X", (int) ch);
65 // Open a database environment
66 DB_ENV *open_dbenv(char *dirname) {
73 int dbversion_major, dbversion_minor, dbversion_patch;
76 "db: open_dbenv() starting\n"
77 "db: Linked zlib: %s\n"
78 "db: Compiled libdb: %s\n"
79 "db: Linked libdb: %s\n",
82 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch)
85 // Create synthetic integer version numbers and compare them.
86 // Never run with a libdb older than the one with which it was compiled.
87 int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
88 int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
89 if (compiled_db_version > linked_db_version) {
90 fprintf(stderr, "db: ctdldump is running with a version of libdb older than the one with which it was compiled.\n"
91 "db: This is an invalid configuration. ctdldump will now exit to prevent data loss.");
95 fprintf(stderr, "db: Setting up DB environment\n");
96 ret = db_env_create(&dbenv, 0);
98 fprintf(stderr, "db: db_env_create: %s\n", db_strerror(ret));
99 fprintf(stderr, "db: exit code %d\n", ret);
103 // We want to specify the shared memory buffer pool cachesize, but everything else is the default.
104 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
106 fprintf(stderr, "db: set_cachesize: %s\n", db_strerror(ret));
107 dbenv->close(dbenv, 0);
108 fprintf(stderr, "db: exit code %d\n", ret);
112 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
113 fprintf(stderr, "db: set_lk_detect: %s\n", db_strerror(ret));
114 dbenv->close(dbenv, 0);
115 fprintf(stderr, "db: exit code %d\n", ret);
119 flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
120 fprintf(stderr, "db: dbenv open(dir=%s, flags=%d)\n", dirname, flags);
121 ret = dbenv->open(dbenv, dirname, flags, 0);
123 fprintf(stderr, "db: dbenv->open: %s\n", db_strerror(ret));
124 dbenv->close(dbenv, 0);
125 fprintf(stderr, "db: exit code %d\n", ret);
133 void close_dbenv(DB_ENV *dbenv) {
134 int ret = dbenv->close(dbenv, 0);
136 fprintf(stderr, "db: dbenv->close: %s\n", db_strerror(ret));
141 // export function for a message in msgmain
142 void export_msgmain(int which_cdb, DBT *in_key, DBT *in_data) {
145 if (in_key->size != sizeof(long)) {
146 fprintf(stderr, "\033[31m\033[1m *** BAD DATA *** ABORTING *** \033[0m\n");
150 memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
152 // If the msgnum is negative, we are looking at METADATA
154 struct MetaData *meta = (struct MetaData *)in_data->data;
155 printf("msgmeta|%ld|%d|%s|%ld\n",
158 meta->meta_content_type,
159 meta->meta_rfc822_length
163 // If the msgnum is positive, we are looking at a MESSAGE
164 else if (in_msgnum > 0) {
165 printf("msgtext|%s\n", hexout(in_data->data, in_data->size));
168 // If the msgnum is 0 it's probably not a valid record.
172 // export function for a user record
173 void export_user(int which_cdb, DBT *in_key, DBT *in_data) {
175 struct ctdluser *user = (struct ctdluser *)in_data->data;
177 printf("user|%d|%d|%s|%u|%d|%ld|%ld|%d|%s|%ld|%ld|%s|%ld|%ld\n",
189 hexout(user->emailaddrs, strlen(user->emailaddrs)),
190 user->msgnum_inboxrules,
191 user->lastproc_inboxrules
196 // export function for a room record
197 void export_room(int which_cdb, DBT *in_key, DBT *in_data) {
199 struct ctdlroom *room = (struct ctdlroom *)in_data->data;
201 printf("room|%s|%s|%ld|%ld|%ld|%u|%s|%ld|%d|%ld|%d|%d|%ld|%d|%u|%d|%ld\n",
212 room->QRep.expire_mode,
213 room->QRep.expire_value,
223 // export function for a floor record
224 void export_floor(int which_cdb, DBT *in_key, DBT *in_data) {
227 memcpy(&floor_num, in_key->data, sizeof(int));
229 struct floor *floor = (struct floor *)in_data->data;
231 printf("floor|%d|%u|%s|%d|%d|%d\n",
236 floor->f_ep.expire_mode,
237 floor->f_ep.expire_value
242 // export function for a msglist or a fulltext index record
243 // (both are indexed by a long and the data is arrays of longs)
244 void export_msglist(int which_cdb, DBT *in_key, DBT *in_data) {
249 // records are indexed by a single "long" and contains an array of zero or more "long"s
251 memcpy(&roomnum, in_key->data, sizeof(long));
253 printf("msglist|%ld|", roomnum);
255 if (in_data->size > 0) {
256 num_msgs = in_data->size / sizeof(long);
257 for (i=0; i<num_msgs; ++i) {
258 memcpy(&msg, (in_data->data + (i * sizeof(long))), sizeof(long));
269 // export function for a visit record
270 void export_visit(int which_cdb, DBT *in_key, DBT *in_data) {
271 struct visit *visit = (struct visit *)in_data->data;
272 printf("visit|%ld|%ld|%ld|%ld|%u|%s|%s|%d\n",
285 // export function for a directory record
286 // (This is a secondary index -- should we just regenerate the data after import?)
287 void export_dir(int which_cdb, DBT *in_key, DBT *in_data) {
289 fwrite(in_key->data, in_key->size, 1, stdout);
291 fwrite(in_data->data, in_data->size, 1, stdout);
296 // export function for a use table record
297 void export_usetable(int which_cdb, DBT *in_key, DBT *in_data) {
298 struct UseTable *u = (struct UseTable *)in_data->data;
299 printf("use|%d|%ld\n", u->hash, u->timestamp);
304 // export function for large message texts
305 void export_bigmsgs(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
307 // The key is a packed long
310 memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
311 out_msgnum = (long)in_msgnum;
313 if (in_key->size != 4) {
314 fprintf(stderr, "\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
318 out_key->size = sizeof(long);
319 out_key->data = realloc(out_key->data, out_key->size);
320 memcpy(out_key->data, &out_msgnum, sizeof(long));
322 // the data is binary-ish but has no packed integers
323 out_data->size = in_data->size;
324 out_data->data = realloc(out_data->data, out_data->size);
325 memcpy(out_data->data, in_data->data, in_data->size);
327 // printf("\033[32m\033[1mBigmsg %ld , length %d\033[0m\n", out_msgnum, out_data->size);
333 // export function for EUID Index records
334 void export_euidindex(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
336 // The structure of an euidindex record *key* is:
337 // |----room_number----|----------EUID-------------|
338 // (sizeof long) (actual length of euid)
340 // The structure of an euidindex record *value* is:
341 // |-----msg_number----|----room_number----|----------EUID-------------|
342 // (sizeof long) (sizeof long) (actual length of euid)
344 int32_t in_msgnum = 0;
345 int32_t in_roomnum = 0;
348 long out_roomnum = 0;
350 memcpy(&in_msgnum, in_data->data, sizeof(in_msgnum));
351 memcpy(&in_roomnum, in_data->data+sizeof(int32_t), sizeof(in_msgnum));
352 strcpy(euid, in_data->data+(sizeof(int32_t)*2));
354 out_msgnum = (long) in_msgnum;
355 out_roomnum = (long) in_roomnum;
356 // printf("euidindex: msgnum=%ld, roomnum=%ld, euid=\"%s\"\n", out_msgnum, out_roomnum, euid);
358 out_key->size = sizeof(long) + strlen(euid) + 1;
359 out_key->data = realloc(out_key->data, out_key->size);
360 memcpy(out_key->data, &out_roomnum, sizeof(out_roomnum));
361 strcpy(out_key->data+sizeof(out_roomnum), euid);
363 out_data->size = sizeof(long) + sizeof(long) + strlen(euid) + 1;
364 out_data->data = realloc(out_data->data, out_data->size);
365 memcpy(out_data->data, &out_msgnum, sizeof(out_msgnum));
366 memcpy(out_data->data+sizeof(out_msgnum), &out_roomnum, sizeof(out_roomnum));
367 strcpy(out_data->data+sizeof(out_msgnum)+sizeof(out_roomnum), euid);
373 // export users-by-number records
374 // (This is a secondary index -- should we just regenerate the data after import?)
375 void export_usersbynumber(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
378 // and remember ... "long" is int32_t on the source system
381 memcpy(&in_usernum, in_key->data, sizeof(in_usernum));
382 out_usernum = (long) in_usernum;
384 if (in_key->size != 4) {
385 fprintf(stderr, "\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
389 out_key->size = sizeof(out_usernum);
390 out_key->data = realloc(out_key->data, out_key->size);
391 memcpy(out_key->data, &out_usernum, sizeof(out_usernum));
394 out_data->size = in_data->size;
395 out_data->data = realloc(out_data->data, out_data->size);
396 memcpy(out_data->data, in_data->data, in_data->size);
398 // printf("usersbynumber: %ld --> %s\n", out_usernum, (char *)out_data->data);
404 // export function for a config record
405 void export_config(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
407 // the key is a string
408 out_key->size = in_key->size;
409 out_key->data = realloc(out_key->data, out_key->size + 1);
410 memcpy(out_key->data, in_key->data, in_key->size);
411 char *k = (char *)out_key->data;
412 k[out_key->size] = 0;
414 // the data is a pair of strings
415 out_data->size = in_data->size;
416 out_data->data = realloc(out_data->data, out_data->size + 1);
417 memcpy(out_data->data, in_data->data, in_data->size);
418 char *d = (char *)out_data->data;
419 d[out_data->size] = 0;
421 // please excuse my friend, he isn't null terminated
422 // printf("\033[32m\033[1mConfig entry: %s -> %s\033[0m\n", (char *)out_key->data, (char *)out_data->data+strlen(out_data->data)+1);
427 // For obsolete databases, zero all the output
428 void zero_function(int which_cdb, DBT *in_key, DBT *in_data) {
433 void (*export_functions[])(int which_cdb, DBT *in_key, DBT *in_data) = {
434 export_msgmain, // CDB_MSGMAIN
435 export_user, // CDB_USERS
436 export_room, // CDB_ROOMS
437 export_floor, // CDB_FLOORTAB
438 export_msglist, // CDB_MSGLISTS
439 export_visit, // CDB_VISIT
440 export_dir, // CDB_DIRECTORY
441 export_usetable, // CDB_USETABLE
442 zero_function, // CDB_BIGMSGS
443 zero_function, // CDB_FULLTEXT
444 zero_function, // CDB_EUIDINDEX
445 zero_function, // CDB_USERSBYNUMBER
446 zero_function, // CDB_UNUSED1 (obsolete)
447 zero_function // CDB_CONFIG
451 void export_table(int which_cdb, DB_ENV *src_dbenv) {
457 // shamelessly swiped from https://docs.oracle.com/database/bdb181/html/programmer_reference/am_cursor.html
460 DBT in_key, in_data, uncomp_data;
461 int num_good_rows = 0;
462 int num_bad_rows = 0;
464 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", which_cdb);
466 // create a database handle for the source table
467 ret = db_create(&src_dbp, src_dbenv, 0);
469 fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
470 fprintf(stderr, "db: exit code %d\n", ret);
474 // open the file containing the source table
475 ret = src_dbp->open(src_dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
477 fprintf(stderr, "db: db_open: %s\n", db_strerror(ret));
478 fprintf(stderr, "db: exit code %d\n", ret);
482 // Acquire a cursor to read the source table
483 if ((ret = src_dbp->cursor(src_dbp, NULL, &src_dbcp, 0)) != 0) {
484 fprintf(stderr, "db: db_cursor: %s\n", db_strerror(ret));
485 fprintf(stderr, "db: exit code %d\n", ret);
489 // Zero out these database keys
490 memset(&in_key, 0, sizeof(DBT)); // input
491 memset(&in_data, 0, sizeof(DBT));
492 memset(&uncomp_data, 0, sizeof(DBT)); // decompressed input (the key doesn't change)
494 // Walk through the database, calling export functions as we go and clearing buffers before each call.
495 while (ret = src_dbcp->get(src_dbcp, &in_key, &in_data, DB_NEXT) == 0) {
497 // If either the key or data are zero length, skip this record
498 if ((in_key.size == 0) || (in_data.size == 0)) {
502 else { // Both key and data are >0 length so we're good to go
504 // Do we need to decompress?
505 static int32_t magic = COMPRESS_MAGIC;
507 if ((in_data.size >= sizeof(struct CtdlCompressHeader)) && (!memcmp(in_data.data, &magic, sizeof(magic)))) {
509 // yes, we need to decompress
511 struct CtdlCompressHeader comp;
512 memcpy(&comp, in_data.data, sizeof(struct CtdlCompressHeader));
513 uncomp_data.size = comp.uncompressed_len;
514 uncomp_data.data = realloc(uncomp_data.data, uncomp_data.size);
515 destLen = (uLongf)comp.uncompressed_len;
517 ret = uncompress((Bytef *)uncomp_data.data, (uLongf *)&destLen,
518 (const Bytef *)in_data.data+sizeof(struct CtdlCompressHeader),
519 (uLong)comp.compressed_len);
521 fprintf(stderr, "db: uncompress() error %d\n", ret);
526 // Call the export function registered to this table
527 export_functions[which_cdb](which_cdb, &in_key, (compressed ? &uncomp_data : &in_data));
529 // Knowing the total number of rows isn't critical to the program. It's just for the user to know.
534 // free any leftover out_data pointers
535 free(uncomp_data.data);
537 // ...and close the database (table)
538 ret = src_dbp->close(src_dbp, 0);
540 fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
547 int main(int argc, char **argv) {
549 char *src_dir = NULL;
550 char *dst_dir = NULL;
552 static DB_ENV *src_dbenv; // Source DB environment (global)
554 // Parse command line
556 while ((a = getopt(argc, argv, "h:d:y")) != EOF) {
568 fprintf(stderr, "%s: usage: %s -s source_dir -d dest_dir\n", argv[0], argv[0]);
574 fprintf(stderr, "------------------------------------------------------------------------\n");
575 fprintf(stderr, "This utility must be run while the server is NOT RUNNING. \n");
576 fprintf(stderr, "We \033[1mguarantee\033[0m data corruption if you do not \n");
577 fprintf(stderr, "observe this warning! The source [-s] directory should contain a copy \n");
578 fprintf(stderr, "of the database from your source system. The dump [-d] directory \n");
579 fprintf(stderr, "should be empty and will receive your dump file. \n");
580 fprintf(stderr, "------------------------------------------------------------------------\n");
581 fprintf(stderr, " Source (database) directory: %s\n", src_dir);
582 fprintf(stderr, "------------------------------------------------------------------------\n");
584 if (confirmed == 1) {
585 fprintf(stderr, "You have specified the [-y] flag, so processing will continue.\n");
588 fprintf(stderr, "Please read [ https://www.citadel.org/ctdldump.html ] to learn how to proceed.\n");
592 src_dbenv = open_dbenv(src_dir);
593 for (i = 0; i < MAXCDB; ++i) {
594 export_table(i, src_dbenv);
596 close_dbenv(src_dbenv);