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);
42 // convert a binary blob to base64 (non-reentrant!)
43 char *b64out(void *data, size_t len) {
44 static char *outbuf = NULL;
45 static size_t outlen = 0;
47 if ((outbuf == NULL) || (outlen < (len * 2))) {
48 outbuf = reallok(outbuf, (len * 2));
52 CtdlEncodeBase64(outbuf, data, len, 0);
57 // Open a database environment
58 DB_ENV *open_dbenv(char *dirname) {
65 int dbversion_major, dbversion_minor, dbversion_patch;
67 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch);
69 // Create synthetic integer version numbers and compare them.
70 // Never run with a libdb other than the one with which it was compiled.
71 int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
72 int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
73 if (compiled_db_version != linked_db_version) {
74 fprintf(stderr, "ctdldump: ctdldump is running with a version of libdb other than the one with which it was compiled.\n"
75 "ctdldump: This is an invalid configuration. ctdldump will now exit to prevent data loss.");
79 ret = db_env_create(&dbenv, 0);
81 fprintf(stderr, "ctdldump: db_env_create: %s\n", db_strerror(ret));
82 fprintf(stderr, "ctdldump: exit code %d\n", ret);
86 // We want to specify the shared memory buffer pool cachesize, but everything else is the default.
87 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
89 fprintf(stderr, "ctdldump: set_cachesize: %s\n", db_strerror(ret));
90 dbenv->close(dbenv, 0);
91 fprintf(stderr, "ctdldump: exit code %d\n", ret);
95 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
96 fprintf(stderr, "ctdldump: set_lk_detect: %s\n", db_strerror(ret));
97 dbenv->close(dbenv, 0);
98 fprintf(stderr, "ctdldump: exit code %d\n", ret);
102 flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
103 ret = dbenv->open(dbenv, dirname, flags, 0);
105 fprintf(stderr, "ctdldump: dbenv->open: %s\n", db_strerror(ret));
106 dbenv->close(dbenv, 0);
107 fprintf(stderr, "ctdldump: exit code %d\n", ret);
115 void close_dbenv(DB_ENV *dbenv) {
116 int ret = dbenv->close(dbenv, 0);
118 fprintf(stderr, "ctdldump: dbenv->close: %s\n", db_strerror(ret));
123 // export function for a message in msgmain
124 void export_msgmain(int which_cdb, DBT *in_key, DBT *in_data) {
127 memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
129 // If the msgnum is negative, we are looking at METADATA
131 struct MetaData *meta = (struct MetaData *)in_data->data;
132 printf("msgmeta|%ld|%d|%s|%ld|\n",
135 meta->meta_content_type,
136 meta->meta_rfc822_length
140 // If the msgnum is positive, we are looking at a MESSAGE
141 else if (in_msgnum > 0) {
142 printf("msgtext|%ld|%s|\n", in_msgnum, b64out(in_data->data, in_data->size));
145 // If the msgnum is 0 it's probably not a valid record.
149 // export function for a user record
150 void export_user(int which_cdb, DBT *in_key, DBT *in_data) {
152 struct ctdluser *user = (struct ctdluser *)in_data->data;
154 printf("user|%d|%d|%s|%u|%d|%ld|%ld|%d|%s|%ld|%ld|%s|%ld|%ld|\n",
166 b64out(user->emailaddrs, strlen(user->emailaddrs)),
167 user->msgnum_inboxrules,
168 user->lastproc_inboxrules
173 // export function for a room record
174 void export_room(int which_cdb, DBT *in_key, DBT *in_data) {
176 struct ctdlroom *room = (struct ctdlroom *)in_data->data;
178 printf("room|%s|%s|%ld|%ld|%ld|%u|%s|%ld|%d|%ld|%d|%d|%ld|%d|%u|%d|%ld|\n",
189 room->QRep.expire_mode,
190 room->QRep.expire_value,
200 // export function for a floor record
201 void export_floor(int which_cdb, DBT *in_key, DBT *in_data) {
204 memcpy(&floor_num, in_key->data, sizeof(int));
206 struct floor *floor = (struct floor *)in_data->data;
208 printf("floor|%d|%u|%s|%d|%d|%d|\n",
213 floor->f_ep.expire_mode,
214 floor->f_ep.expire_value
219 // export function for a msglist or a fulltext index record
220 // (both are indexed by a long and the data is arrays of longs)
221 void export_msglist(int which_cdb, DBT *in_key, DBT *in_data) {
226 // records are indexed by a single "long" and contains an array of zero or more "long"s
228 memcpy(&roomnum, in_key->data, sizeof(long));
230 printf("msglist|%ld|", roomnum);
232 if (in_data->size > 0) {
233 num_msgs = in_data->size / sizeof(long);
234 for (i=0; i<num_msgs; ++i) {
235 memcpy(&msg, (in_data->data + (i * sizeof(long))), sizeof(long));
246 // export function for a visit record
247 void export_visit(int which_cdb, DBT *in_key, DBT *in_data) {
248 struct visit *visit = (struct visit *)in_data->data;
251 // If there is corrupt data in the "seen" array, cut that out before exporting
252 len = strlen(visit->v_seen);
253 for (i=0; i<len; ++i) {
254 if (!isprint(visit->v_seen[i])) {
255 visit->v_seen[i] = 0;
259 // If there is corrupt data in the "answered" array, cut that out before exporting
260 len = strlen(visit->v_answered);
261 for (i=0; i<len; ++i) {
262 if (!isprint(visit->v_answered[i])) {
263 visit->v_answered[i] = 0;
268 printf("visit|%ld|%ld|%ld|%ld|%u|%s|%s|%d|\n",
281 // export function for a directory record
282 void export_dir(int which_cdb, DBT *in_key, DBT *in_data) {
284 fwrite(in_key->data, in_key->size, 1, stdout);
285 printf("|%s|\n", (char *)in_data->data);
289 // export function for a use table record
290 void export_usetable(int which_cdb, DBT *in_key, DBT *in_data) {
291 struct UseTable *u = (struct UseTable *)in_data->data;
292 printf("use|%d|%ld|\n", u->hash, u->timestamp);
296 // export function for large message texts
297 void export_bigmsg(int which_cdb, DBT *in_key, DBT *in_data) {
300 memcpy(&msgnum, in_key->data, sizeof(msgnum));
301 printf("bigmsg|%ld|%s|\n", msgnum, b64out(in_data->data, in_data->size));
305 // export function for EUID Index records
306 //void export_euidindex(int which_cdb, DBT *in_key, DBT *in_data) {
308 // The structure of an euidindex record *key* is:
309 // |----room_number----|----------EUID-------------|
310 // (sizeof long) (actual length of euid)
312 // The structure of an euidindex record *value* is:
313 // |-----msg_number----|----room_number----|----------EUID-------------|
314 // (sizeof long) (sizeof long) (actual length of euid)
316 //long msgnum, roomnum;
319 //memcpy(&msgnum, in_data->data, sizeof(long));
320 //memcpy(&roomnum, in_data->data+sizeof(long), sizeof(msgnum));
321 //euid = in_data->data+(sizeof(long)*2);
323 //printf("euidindex|%ld|%ld|%s|\n", msgnum, roomnum, euid);
327 // export users-by-number records
328 // (This is a secondary index -- should we just regenerate the data after import?)
329 void export_usersbynumber(int which_cdb, DBT *in_key, DBT *in_data) {
333 memcpy(&usernum, in_key->data, sizeof(usernum));
336 printf("usersbynumber|%ld|%s|\n", usernum, (char *)in_data->data);
340 // export function for a config record
341 void export_config(int which_cdb, DBT *in_key, DBT *in_data) {
343 printf("config|%s|%s|\n",
344 (char *)in_data->data,
345 (char *)in_data->data + strlen(in_data->data) + 1
351 // For obsolete databases, zero all the output
352 void zero_function(int which_cdb, DBT *in_key, DBT *in_data) {
357 void (*export_functions[])(int which_cdb, DBT *in_key, DBT *in_data) = {
358 export_msgmain, // CDB_MSGMAIN
359 export_user, // CDB_USERS
360 export_room, // CDB_ROOMS
361 export_floor, // CDB_FLOORTAB
362 export_msglist, // CDB_MSGLISTS
363 export_visit, // CDB_VISIT
364 export_dir, // CDB_DIRECTORY
365 export_usetable, // CDB_USETABLE
366 export_bigmsg, // CDB_BIGMSGS
367 zero_function, // CDB_FULLTEXT
368 zero_function, // CDB_EUIDINDEX
369 export_usersbynumber, // CDB_USERSBYNUMBER
370 zero_function, // CDB_UNUSED1 (obsolete)
371 export_config // CDB_CONFIG
375 void export_table(int which_cdb, DB_ENV *src_dbenv) {
381 // shamelessly swiped from https://docs.oracle.com/database/bdb181/html/programmer_reference/am_cursor.html
384 DBT in_key, in_data, uncomp_data;
385 int num_good_rows = 0;
386 int num_bad_rows = 0;
388 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", which_cdb);
390 // create a database handle for the source table
391 ret = db_create(&src_dbp, src_dbenv, 0);
393 fprintf(stderr, "ctdldump: db_create: %s\n", db_strerror(ret));
394 fprintf(stderr, "ctdldump: exit code %d\n", ret);
398 // open the file containing the source table
399 ret = src_dbp->open(src_dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
401 fprintf(stderr, "ctdldump: db_open: %s\n", db_strerror(ret));
402 fprintf(stderr, "ctdldump: exit code %d\n", ret);
406 // Acquire a cursor to read the source table
407 if ((ret = src_dbp->cursor(src_dbp, NULL, &src_dbcp, 0)) != 0) {
408 fprintf(stderr, "ctdldump: db_cursor: %s\n", db_strerror(ret));
409 fprintf(stderr, "ctdldump: exit code %d\n", ret);
413 // Zero out these database keys
414 memset(&in_key, 0, sizeof(DBT)); // input
415 memset(&in_data, 0, sizeof(DBT));
416 memset(&uncomp_data, 0, sizeof(DBT)); // decompressed input (the key doesn't change)
418 // Walk through the database, calling export functions as we go and clearing buffers before each call.
419 while (ret = src_dbcp->get(src_dbcp, &in_key, &in_data, DB_NEXT) == 0) {
421 // If either the key or data are zero length, skip this record
422 if ((in_key.size == 0) || (in_data.size == 0)) {
426 else { // Both key and data are >0 length so we're good to go
428 // Do we need to decompress?
429 static int32_t magic = COMPRESS_MAGIC;
431 if ((in_data.size >= sizeof(struct CtdlCompressHeader)) && (!memcmp(in_data.data, &magic, sizeof(magic)))) {
433 // yes, we need to decompress
435 struct CtdlCompressHeader comp;
436 memcpy(&comp, in_data.data, sizeof(struct CtdlCompressHeader));
437 uncomp_data.size = comp.uncompressed_len;
438 uncomp_data.data = reallok(uncomp_data.data, uncomp_data.size);
439 destLen = (uLongf)comp.uncompressed_len;
441 ret = uncompress((Bytef *)uncomp_data.data, (uLongf *)&destLen,
442 (const Bytef *)in_data.data+sizeof(struct CtdlCompressHeader),
443 (uLong)comp.compressed_len);
445 fprintf(stderr, "ctdldump: uncompress() error %d\n", ret);
450 // Call the export function registered to this table
451 export_functions[which_cdb](which_cdb, &in_key, (compressed ? &uncomp_data : &in_data));
453 // Knowing the total number of rows isn't critical to the program. It's just for the user to know.
458 // free any leftover out_data pointers
459 free(uncomp_data.data);
461 // ...and close the database (table)
462 ret = src_dbp->close(src_dbp, 0);
464 fprintf(stderr, "ctdldump: db_close: %s\n", db_strerror(ret));
471 int main(int argc, char **argv) {
473 char *src_dir = NULL;
474 char *dst_dir = NULL;
476 static DB_ENV *src_dbenv; // Source DB environment (global)
478 // display the greeting
479 fprintf(stderr, "\033[44m\033[30m \033[K\033[0m\n"
480 "\033[44m\033[30m DB Dump utility for Citadel \033[K\033[0m\n"
481 "\033[44m\033[30m Copyright (c) 2023 by citadel.org et al. \033[K\033[0m\n"
482 "\033[44m\033[30m This program is open source software. Use, duplication, or disclosure \033[K\033[0m\n"
483 "\033[44m\033[30m is subject to the terms of the GNU General Public license v3. \033[K\033[0m\n"
484 "\033[44m\033[30m \033[K\033[0m\n");
486 // Parse command line
488 while ((a = getopt(argc, argv, "h:y")) != EOF) {
497 fprintf(stderr, "%s: usage: %s -s source_dir [>dumpfile]\n", argv[0], argv[0]);
502 if (confirmed == 1) {
503 fprintf(stderr, "ctdldump: You have specified the [-y] flag, so processing will continue.\n");
506 fprintf(stderr, "ctdldump: Please read [ https://www.citadel.org/dump-and-load.html ] to learn how to proceed.\n");
510 src_dbenv = open_dbenv(src_dir);
512 for (i = 0; i < MAXCDB; ++i) {
513 export_table(i, src_dbenv);
515 close_dbenv(src_dbenv);