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 // convert 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", meta->meta_msgnum, meta->meta_refcount, meta->meta_content_type, meta->meta_rfc822_length);
158 // If the msgnum is positive, we are looking at a MESSAGE
159 else if (in_msgnum > 0) {
160 printf("msgtext|%s\n", hexout(in_data->data, in_data->size));
163 // If the msgnum is 0 it's probably not a valid record.
167 // convert function for a user record
168 void export_user(int which_cdb, DBT *in_key, DBT *in_data) {
170 struct ctdluser *user = (struct ctdluser *)in_data->data;
172 printf("user|%d|%d|%s|%u|%d|%ld|%ld|%d|%s|%ld|%ld|%s|%ld|%ld\n",
173 user->version, // Citadel version which created this record
174 user->uid, // Associate with a unix account?
175 user->password, // password
176 user->flags, // See US_ flags
177 user->axlevel, // Access level
178 user->usernum, // User number (never recycled)
179 user->lastcall, // Date/time of most recent login
180 user->USuserpurge, // Purge time (in days) for user
181 user->fullname, // Display name (primary identifier)
182 user->msgnum_bio, // msgnum of user's profile (bio)
183 user->msgnum_pic, // msgnum of user's avatar (photo)
184 hexout(user->emailaddrs, strlen(user->emailaddrs)), // Internet email addresses
185 user->msgnum_inboxrules, // msgnum of user's inbox filtering rules
186 user->lastproc_inboxrules // msgnum of last message filtered
192 // convert function for a room record
193 void export_rooms(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
195 // The key is a string so we can just copy it over
196 out_key->size = in_key->size;
197 out_key->data = realloc(out_key->data, out_key->size);
198 memcpy(out_key->data, in_key->data, in_key->size);
201 struct ctdlroom_32 *room32 = (struct ctdlroom_32 *)in_data->data;
202 out_data->size = sizeof(struct ctdlroom);
203 out_data->data = realloc(out_data->data, out_data->size);
204 struct ctdlroom *room64 = (struct ctdlroom *)out_data->data;
206 strcpy(room64->QRname, room32->QRname);
207 strcpy(room64->QRpasswd, room32->QRpasswd);
208 room64->QRroomaide = (long) room32->QRroomaide;
209 room64->QRhighest = (long) room32->QRhighest;
210 room64->QRgen = (time_t) room32->QRgen;
211 room64->QRflags = (unsigned) room32->QRflags;
212 strcpy(room64->QRdirname, room32->QRdirname);
213 room64->msgnum_info = (long) room32->msgnum_info;
214 room64->QRfloor = (char) room32->QRfloor;
215 room64->QRmtime = (time_t) room32->QRmtime;
216 room64->QRep.expire_mode = (int) room32->QRep.expire_mode;
217 room64->QRep.expire_value = (int) room32->QRep.expire_value;
218 room64->QRnumber = (long) room32->QRnumber;
219 room64->QRorder = (char) room32->QRorder;
220 room64->QRflags2 = (unsigned) room32->QRflags2;
221 room64->QRdefaultview = (int) room32->QRdefaultview;
222 room64->msgnum_pic = (long) room32->msgnum_pic;
224 // printf("\033[32m\033[1mRoom: %s\033[0m\n", room64->QRname);
228 // convert function for a floor record
229 void export_floors(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
231 // the key is an "int", and "int" is 32-bits on both 32 and 64 bit platforms.
232 out_key->size = in_key->size;
233 out_key->data = realloc(out_key->data, out_key->size);
234 memcpy(out_key->data, in_key->data, in_key->size);
237 struct floor_32 *floor32 = (struct floor_32 *)in_data->data;
238 out_data->size = sizeof(struct floor);
239 out_data->data = realloc(out_data->data, out_data->size);
240 struct floor *floor64 = (struct floor *)out_data->data;
242 // these are probably bit-for-bit identical, actually ... but we do it the "right" way anyway
243 floor64->f_flags = (unsigned short) floor32->f_flags;
244 strcpy(floor64->f_name, floor32->f_name);
245 floor64->f_ref_count = (int) floor32->f_ref_count;
246 floor64->f_ep.expire_mode = (int) floor32->f_ep.expire_mode;
247 floor64->f_ep.expire_value = (int) floor32->f_ep.expire_value;
249 // printf("\033[32m\033[1mFloor: %s\033[0m\n", floor64->f_name);
253 // convert function for a msglist or a fulltext index record
254 // (both are indexed by a long and the data is arrays of longs)
255 void export_msglists(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
258 char *table = (which_cdb == CDB_FULLTEXT) ? "FullText" : "Msglist";
260 // records are indexed by a single "long" and contains an array of zero or more "long"s
261 // and remember ... "long" is int32_t on the source system
264 memcpy(&in_roomnum, in_key->data, sizeof(in_roomnum));
265 out_roomnum = (long) in_roomnum;
267 if (in_key->size != 4) {
268 fprintf(stderr, "\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
272 int num_msgs = in_data->size / sizeof(int32_t);
273 // printf("\033[32m\033[1m%s: key %ld (%d messages)\033[0m\n", table, out_roomnum, num_msgs);
275 // the key is a "long"
276 out_key->size = sizeof(out_roomnum);
277 out_key->data = realloc(out_key->data, out_key->size);
278 memcpy(out_key->data, &out_roomnum, sizeof(out_roomnum));
280 // the data is another array, but a wider type
281 out_data->size = sizeof(long) * num_msgs;
282 out_data->data = realloc(out_data->data, out_data->size);
286 for (i=0; i<num_msgs; ++i) {
287 memcpy(&in_msg, (in_data->data + (i * sizeof(int32_t))), sizeof(int32_t));
288 out_msg = (long) in_msg;
289 memcpy((out_data->data + (i * sizeof(long))), &out_msg, sizeof(long));
290 // printf("msg#%ld\n", out_msg);
295 // convert function for a visit record
296 void export_visits(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
299 struct visit_32 *visit32 = (struct visit_32 *)in_data->data;
300 out_data->size = sizeof(struct visit);
301 out_data->data = realloc(out_data->data, out_data->size);
302 struct visit *visit64 = (struct visit *)out_data->data;
304 // the data (zero it out so it will compress well)
305 memset(visit64, 0, sizeof(struct visit));
306 visit64->v_roomnum = (long) visit32->v_roomnum;
307 visit64->v_roomgen = (long) visit32->v_roomgen;
308 visit64->v_usernum = (long) visit32->v_usernum;
309 visit64->v_lastseen = (long) visit32->v_lastseen;
310 visit64->v_flags = (unsigned) visit32->v_flags;
311 strcpy(visit64->v_seen, visit32->v_seen);
312 strcpy(visit64->v_answered, visit32->v_answered);
313 visit64->v_view = (int) visit32->v_view;
315 // printf("\033[32m\033[1mVisit: room %10ld, gen %10ld, user %10ld\033[0m\n", visit64->v_roomnum, visit64->v_roomgen, visit64->v_usernum);
317 // create the key (which is based on the data, so there is no need to convert the old key)
318 out_key->size = sizeof(struct visit_index);
319 out_key->data = realloc(out_key->data, out_key->size);
320 struct visit_index *newvisitindex = (struct visit_index *) out_key->data;
321 newvisitindex->iRoomID = visit64->v_roomnum;
322 newvisitindex->iRoomGen = visit64->v_roomgen;
323 newvisitindex->iUserID = visit64->v_usernum;
327 // convert function for a directory record
328 void export_dir(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
330 // the key is a string
331 out_key->size = in_key->size;
332 out_key->data = realloc(out_key->data, out_key->size + 1);
333 memcpy(out_key->data, in_key->data, in_key->size);
334 char *k = (char *)out_key->data;
335 k[out_key->size] = 0;
337 // the data is also a string
338 out_data->size = in_data->size;
339 out_data->data = realloc(out_data->data, out_data->size + 1);
340 memcpy(out_data->data, in_data->data, in_data->size);
341 char *d = (char *)out_data->data;
342 d[out_data->size] = 0;
344 // please excuse my friend, he isn't null terminated
345 // printf("\033[32m\033[1mDirectory entry: %s -> %s\033[0m\n", (char *)out_key->data, (char *)out_data->data);
349 // convert function for a use table record
350 void export_usetable(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
352 // the key is an int, which is the same size (32 bits) on both 32 and 64 bit systems
353 out_key->size = in_key->size;
354 out_key->data = realloc(out_key->data, out_key->size);
355 memcpy(out_key->data, in_key->data, in_key->size);
357 // the data is a "struct UseTable"
358 struct UseTable_32 *use32 = (struct UseTable_32 *)in_data->data;
359 out_data->size = sizeof(struct UseTable);
360 out_data->data = realloc(out_data->data, out_data->size);
361 memset(out_data->data, 0, out_data->size);
362 struct UseTable *use64 = (struct UseTable *)out_data->data;
365 use64->hash = use32->hash;
366 use64->timestamp = (time_t) use32->timestamp;
368 // printf("\033[32m\033[1muse table: %d , %s\033[0m\n", use64->hash, asctime(localtime(&use64->timestamp)));
372 // convert function for large message texts
373 void export_bigmsgs(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
375 // The key is a packed long
378 memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
379 out_msgnum = (long)in_msgnum;
381 if (in_key->size != 4) {
382 fprintf(stderr, "\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
386 out_key->size = sizeof(long);
387 out_key->data = realloc(out_key->data, out_key->size);
388 memcpy(out_key->data, &out_msgnum, sizeof(long));
390 // the data is binary-ish but has no packed integers
391 out_data->size = in_data->size;
392 out_data->data = realloc(out_data->data, out_data->size);
393 memcpy(out_data->data, in_data->data, in_data->size);
395 // printf("\033[32m\033[1mBigmsg %ld , length %d\033[0m\n", out_msgnum, out_data->size);
399 // convert function for EUID Index records
400 void export_euidindex(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
402 // The structure of an euidindex record *key* is:
403 // |----room_number----|----------EUID-------------|
404 // (sizeof long) (actual length of euid)
406 // The structure of an euidindex record *value* is:
407 // |-----msg_number----|----room_number----|----------EUID-------------|
408 // (sizeof long) (sizeof long) (actual length of euid)
410 int32_t in_msgnum = 0;
411 int32_t in_roomnum = 0;
414 long out_roomnum = 0;
416 memcpy(&in_msgnum, in_data->data, sizeof(in_msgnum));
417 memcpy(&in_roomnum, in_data->data+sizeof(int32_t), sizeof(in_msgnum));
418 strcpy(euid, in_data->data+(sizeof(int32_t)*2));
420 out_msgnum = (long) in_msgnum;
421 out_roomnum = (long) in_roomnum;
422 // printf("euidindex: msgnum=%ld, roomnum=%ld, euid=\"%s\"\n", out_msgnum, out_roomnum, euid);
424 out_key->size = sizeof(long) + strlen(euid) + 1;
425 out_key->data = realloc(out_key->data, out_key->size);
426 memcpy(out_key->data, &out_roomnum, sizeof(out_roomnum));
427 strcpy(out_key->data+sizeof(out_roomnum), euid);
429 out_data->size = sizeof(long) + sizeof(long) + strlen(euid) + 1;
430 out_data->data = realloc(out_data->data, out_data->size);
431 memcpy(out_data->data, &out_msgnum, sizeof(out_msgnum));
432 memcpy(out_data->data+sizeof(out_msgnum), &out_roomnum, sizeof(out_roomnum));
433 strcpy(out_data->data+sizeof(out_msgnum)+sizeof(out_roomnum), euid);
437 // convert users-by-number records
438 void export_usersbynumber(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
441 // and remember ... "long" is int32_t on the source system
444 memcpy(&in_usernum, in_key->data, sizeof(in_usernum));
445 out_usernum = (long) in_usernum;
447 if (in_key->size != 4) {
448 fprintf(stderr, "\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
452 out_key->size = sizeof(out_usernum);
453 out_key->data = realloc(out_key->data, out_key->size);
454 memcpy(out_key->data, &out_usernum, sizeof(out_usernum));
457 out_data->size = in_data->size;
458 out_data->data = realloc(out_data->data, out_data->size);
459 memcpy(out_data->data, in_data->data, in_data->size);
461 // printf("usersbynumber: %ld --> %s\n", out_usernum, (char *)out_data->data);
465 // convert function for a config record
466 void export_config(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
468 // the key is a string
469 out_key->size = in_key->size;
470 out_key->data = realloc(out_key->data, out_key->size + 1);
471 memcpy(out_key->data, in_key->data, in_key->size);
472 char *k = (char *)out_key->data;
473 k[out_key->size] = 0;
475 // the data is a pair of strings
476 out_data->size = in_data->size;
477 out_data->data = realloc(out_data->data, out_data->size + 1);
478 memcpy(out_data->data, in_data->data, in_data->size);
479 char *d = (char *)out_data->data;
480 d[out_data->size] = 0;
482 // please excuse my friend, he isn't null terminated
483 // printf("\033[32m\033[1mConfig entry: %s -> %s\033[0m\n", (char *)out_key->data, (char *)out_data->data+strlen(out_data->data)+1);
488 // For obsolete databases, zero all the output
489 void zero_function(int which_cdb, DBT *in_key, DBT *in_data) {
490 //printf("Table %02x, keylen=%d, datalen=%d\n", which_cdb, in_key->size, in_data->size);
494 void (*export_functions[])(int which_cdb, DBT *in_key, DBT *in_data) = {
495 export_msgmain, // CDB_MSGMAIN
496 export_user, // CDB_USERS
497 zero_function, // CDB_ROOMS
498 zero_function, // CDB_FLOORTAB
499 zero_function, // CDB_MSGLISTS
500 zero_function, // CDB_VISIT
501 zero_function, // CDB_DIRECTORY
502 zero_function, // CDB_USETABLE
503 zero_function, // CDB_BIGMSGS
504 zero_function, // CDB_FULLTEXT
505 zero_function, // CDB_EUIDINDEX
506 zero_function, // CDB_USERSBYNUMBER
507 zero_function, // CDB_UNUSED1 (obsolete)
508 zero_function // CDB_CONFIG
512 void export_table(int which_cdb, DB_ENV *src_dbenv) {
518 // shamelessly swiped from https://docs.oracle.com/database/bdb181/html/programmer_reference/am_cursor.html
521 DBT in_key, in_data, uncomp_data;
522 int num_good_rows = 0;
523 int num_bad_rows = 0;
525 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", which_cdb);
527 // create a database handle for the source table
528 ret = db_create(&src_dbp, src_dbenv, 0);
530 fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
531 fprintf(stderr, "db: exit code %d\n", ret);
535 // open the file containing the source table
536 ret = src_dbp->open(src_dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
538 fprintf(stderr, "db: db_open: %s\n", db_strerror(ret));
539 fprintf(stderr, "db: exit code %d\n", ret);
543 // Acquire a cursor to read the source table
544 if ((ret = src_dbp->cursor(src_dbp, NULL, &src_dbcp, 0)) != 0) {
545 fprintf(stderr, "db: db_cursor: %s\n", db_strerror(ret));
546 fprintf(stderr, "db: exit code %d\n", ret);
550 // Zero out these database keys
551 memset(&in_key, 0, sizeof(DBT)); // input
552 memset(&in_data, 0, sizeof(DBT));
553 memset(&uncomp_data, 0, sizeof(DBT)); // decompressed input (the key doesn't change)
555 // Walk through the database, calling convert functions as we go and clearing buffers before each call.
556 while (ret = src_dbcp->get(src_dbcp, &in_key, &in_data, DB_NEXT) == 0) {
558 // If either the key or data are zero length, skip this record
559 if ((in_key.size == 0) || (in_data.size == 0)) {
563 else { // Both key and data are >0 length so we're good to go
565 // Do we need to decompress?
566 static int32_t magic = COMPRESS_MAGIC;
568 if ( (in_data.size >= sizeof(struct CtdlCompressHeader)) && (!memcmp(in_data.data, &magic, sizeof(magic))) ) {
570 // yes, we need to decompress
572 struct CtdlCompressHeader comp;
573 memcpy(&comp, in_data.data, sizeof(struct CtdlCompressHeader));
574 uncomp_data.size = comp.uncompressed_len;
575 uncomp_data.data = realloc(uncomp_data.data, uncomp_data.size);
576 destLen = (uLongf)comp.uncompressed_len;
578 ret = uncompress((Bytef *)uncomp_data.data, (uLongf *)&destLen, (const Bytef *)in_data.data+sizeof(struct CtdlCompressHeader), (uLong)comp.compressed_len);
580 fprintf(stderr, "db: uncompress() error %d\n", ret);
585 // Call the convert function registered to this table
586 export_functions[which_cdb](which_cdb, &in_key, (compressed ? &uncomp_data : &in_data));
588 // Knowing the total number of rows isn't critical to the program. It's just for the user to know.
593 // free any leftover out_data pointers
594 free(uncomp_data.data);
596 // ...and close the database (table)
597 ret = src_dbp->close(src_dbp, 0);
599 fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
606 int main(int argc, char **argv) {
608 char *src_dir = NULL;
609 char *dst_dir = NULL;
611 static DB_ENV *src_dbenv; // Source DB environment (global)
613 // Parse command line
615 while ((a = getopt(argc, argv, "h:d:y")) != EOF) {
627 fprintf(stderr, "%s: usage: %s -s source_dir -d dest_dir\n", argv[0], argv[0]);
633 fprintf(stderr, "------------------------------------------------------------------------\n");
634 fprintf(stderr, "This utility must be run while the server is OFFLINE. \n");
635 fprintf(stderr, "We \033[1mguarantee\033[0m data corruption if you do not \n");
636 fprintf(stderr, "observe this warning! The source [-s] directory should contain a copy \n");
637 fprintf(stderr, "of the database from your source system. The dump [-d] directory \n");
638 fprintf(stderr, "should be empty and will receive your dump file. \n");
639 fprintf(stderr, "------------------------------------------------------------------------\n");
640 fprintf(stderr, " Source (database) directory: %s\n", src_dir);
641 fprintf(stderr, "------------------------------------------------------------------------\n");
643 if (confirmed == 1) {
644 fprintf(stderr, "You have specified the [-y] flag, so processing will continue.\n");
647 fprintf(stderr, "Please read [ https://www.citadel.org/ctdldump.html ] to learn how to proceed.\n");
651 src_dbenv = open_dbenv(src_dir);
652 for (i = 0; i < MAXCDB; ++i) {
653 export_table(i, src_dbenv);
655 close_dbenv(src_dbenv);