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;
49 if ((outbuf == NULL) || (outlen < (len * 2))) {
50 outbuf = reallok(outbuf, (len * 2));
54 CtdlEncodeBase64(outbuf, data, len, 0);
59 // Open a database environment
60 DB_ENV *open_dbenv(char *dirname) {
67 int dbversion_major, dbversion_minor, dbversion_patch;
69 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch);
71 // Create synthetic integer version numbers and compare them.
72 // Never run with a libdb other than the one with which it was compiled.
73 int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
74 int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
75 if (compiled_db_version != linked_db_version) {
76 fprintf(stderr, "ctdldump: ctdldump is running with a version of libdb other than the one with which it was compiled.\n"
77 "ctdldump: This is an invalid configuration. ctdldump will now exit to prevent data loss.");
81 ret = db_env_create(&dbenv, 0);
83 fprintf(stderr, "ctdldump: db_env_create: %s\n", db_strerror(ret));
84 fprintf(stderr, "ctdldump: exit code %d\n", ret);
88 // We want to specify the shared memory buffer pool cachesize, but everything else is the default.
89 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
91 fprintf(stderr, "ctdldump: set_cachesize: %s\n", db_strerror(ret));
92 dbenv->close(dbenv, 0);
93 fprintf(stderr, "ctdldump: exit code %d\n", ret);
97 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
98 fprintf(stderr, "ctdldump: set_lk_detect: %s\n", db_strerror(ret));
99 dbenv->close(dbenv, 0);
100 fprintf(stderr, "ctdldump: exit code %d\n", ret);
104 flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
105 ret = dbenv->open(dbenv, dirname, flags, 0);
107 fprintf(stderr, "ctdldump: dbenv->open: %s\n", db_strerror(ret));
108 dbenv->close(dbenv, 0);
109 fprintf(stderr, "ctdldump: exit code %d\n", ret);
117 void close_dbenv(DB_ENV *dbenv) {
118 int ret = dbenv->close(dbenv, 0);
120 fprintf(stderr, "ctdldump: dbenv->close: %s\n", db_strerror(ret));
125 // export function for a message in msgmain
126 void export_msgmain(int which_cdb, DBT *in_key, DBT *in_data) {
129 memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
131 // If the msgnum is negative, we are looking at METADATA
133 struct MetaData *meta = (struct MetaData *)in_data->data;
134 printf("msgmeta|%ld|%d|%s|%ld|\n",
137 meta->meta_content_type,
138 meta->meta_rfc822_length
142 // If the msgnum is positive, we are looking at a MESSAGE
143 else if (in_msgnum > 0) {
144 printf("msgtext|%ld|%s|\n", in_msgnum, b64out(in_data->data, in_data->size));
147 // If the msgnum is 0 it's probably not a valid record.
151 // export function for a user record
152 void export_user(int which_cdb, DBT *in_key, DBT *in_data) {
154 struct ctdluser *user = (struct ctdluser *)in_data->data;
156 printf("user|%d|%d|%s|%u|%d|%ld|%ld|%d|%s|%ld|%ld|%s|%ld|%ld|\n",
168 b64out(user->emailaddrs, strlen(user->emailaddrs)),
169 user->msgnum_inboxrules,
170 user->lastproc_inboxrules
175 // export function for a room record
176 void export_room(int which_cdb, DBT *in_key, DBT *in_data) {
178 struct ctdlroom *room = (struct ctdlroom *)in_data->data;
180 printf("room|%s|%s|%ld|%ld|%ld|%u|%s|%ld|%d|%ld|%d|%d|%ld|%d|%u|%d|%ld|\n",
191 room->QRep.expire_mode,
192 room->QRep.expire_value,
202 // export function for a floor record
203 void export_floor(int which_cdb, DBT *in_key, DBT *in_data) {
206 memcpy(&floor_num, in_key->data, sizeof(int));
208 struct floor *floor = (struct floor *)in_data->data;
210 printf("floor|%d|%u|%s|%d|%d|%d|\n",
215 floor->f_ep.expire_mode,
216 floor->f_ep.expire_value
221 // export function for a msglist or a fulltext index record
222 // (both are indexed by a long and the data is arrays of longs)
223 void export_msglist(int which_cdb, DBT *in_key, DBT *in_data) {
228 // records are indexed by a single "long" and contains an array of zero or more "long"s
230 memcpy(&roomnum, in_key->data, sizeof(long));
232 printf("msglist|%ld|", roomnum);
234 if (in_data->size > 0) {
235 num_msgs = in_data->size / sizeof(long);
236 for (i=0; i<num_msgs; ++i) {
237 memcpy(&msg, (in_data->data + (i * sizeof(long))), sizeof(long));
248 // export function for a visit record
249 void export_visit(int which_cdb, DBT *in_key, DBT *in_data) {
250 struct visit *visit = (struct visit *)in_data->data;
251 printf("visit|%ld|%ld|%ld|%ld|%u|%s|%s|%d|\n",
264 // export function for a directory record
265 // (This is a secondary index -- should we just regenerate the data after import?)
266 // void export_dir(int which_cdb, DBT *in_key, DBT *in_data) {
268 // fwrite(in_key->data, in_key->size, 1, stdout);
269 // printf("|%s|\n", (char *)in_data->data);
273 // export function for a use table record
274 void export_usetable(int which_cdb, DBT *in_key, DBT *in_data) {
275 struct UseTable *u = (struct UseTable *)in_data->data;
276 printf("use|%d|%ld|\n", u->hash, u->timestamp);
280 // export function for large message texts
281 void export_bigmsg(int which_cdb, DBT *in_key, DBT *in_data) {
284 memcpy(&msgnum, in_key->data, sizeof(msgnum));
285 printf("bigmsg|%ld|%s|\n", msgnum, b64out(in_data->data, in_data->size));
289 // export function for EUID Index records
290 //void export_euidindex(int which_cdb, DBT *in_key, DBT *in_data) {
292 // The structure of an euidindex record *key* is:
293 // |----room_number----|----------EUID-------------|
294 // (sizeof long) (actual length of euid)
296 // The structure of an euidindex record *value* is:
297 // |-----msg_number----|----room_number----|----------EUID-------------|
298 // (sizeof long) (sizeof long) (actual length of euid)
300 //long msgnum, roomnum;
303 //memcpy(&msgnum, in_data->data, sizeof(long));
304 //memcpy(&roomnum, in_data->data+sizeof(long), sizeof(msgnum));
305 //euid = in_data->data+(sizeof(long)*2);
307 //printf("euidindex|%ld|%ld|%s|\n", msgnum, roomnum, euid);
311 // export users-by-number records
312 // (This is a secondary index -- should we just regenerate the data after import?)
313 //void export_usersbynumber(int which_cdb, DBT *in_key, DBT *in_data) {
317 //memcpy(&usernum, in_key->data, sizeof(usernum));
320 //printf("usersbynumber|%ld|%s|\n", usernum, (char *)in_data->data);
324 // export function for a config record
325 void export_config(int which_cdb, DBT *in_key, DBT *in_data) {
327 printf("config|%s|%s|\n",
328 (char *)in_data->data,
329 (char *)in_data->data + strlen(in_data->data) + 1
335 // For obsolete databases, zero all the output
336 void zero_function(int which_cdb, DBT *in_key, DBT *in_data) {
341 void (*export_functions[])(int which_cdb, DBT *in_key, DBT *in_data) = {
342 export_msgmain, // CDB_MSGMAIN
343 export_user, // CDB_USERS
344 export_room, // CDB_ROOMS
345 export_floor, // CDB_FLOORTAB
346 export_msglist, // CDB_MSGLISTS
347 export_visit, // CDB_VISIT
348 zero_function, // CDB_DIRECTORY (regenerate this on the server)
349 export_usetable, // CDB_USETABLE
350 export_bigmsg, // CDB_BIGMSGS
351 zero_function, // CDB_FULLTEXT (regenerate this on the server)
352 zero_function, // CDB_EUIDINDEX (regenerate this on the server)
353 zero_function, // CDB_USERSBYNUMBER (regenerate this on the server)
354 zero_function, // CDB_UNUSED1 (obsolete)
355 export_config // CDB_CONFIG
359 void export_table(int which_cdb, DB_ENV *src_dbenv) {
365 // shamelessly swiped from https://docs.oracle.com/database/bdb181/html/programmer_reference/am_cursor.html
368 DBT in_key, in_data, uncomp_data;
369 int num_good_rows = 0;
370 int num_bad_rows = 0;
372 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", which_cdb);
374 // create a database handle for the source table
375 ret = db_create(&src_dbp, src_dbenv, 0);
377 fprintf(stderr, "ctdldump: db_create: %s\n", db_strerror(ret));
378 fprintf(stderr, "ctdldump: exit code %d\n", ret);
382 // open the file containing the source table
383 ret = src_dbp->open(src_dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
385 fprintf(stderr, "ctdldump: db_open: %s\n", db_strerror(ret));
386 fprintf(stderr, "ctdldump: exit code %d\n", ret);
390 // Acquire a cursor to read the source table
391 if ((ret = src_dbp->cursor(src_dbp, NULL, &src_dbcp, 0)) != 0) {
392 fprintf(stderr, "ctdldump: db_cursor: %s\n", db_strerror(ret));
393 fprintf(stderr, "ctdldump: exit code %d\n", ret);
397 // Zero out these database keys
398 memset(&in_key, 0, sizeof(DBT)); // input
399 memset(&in_data, 0, sizeof(DBT));
400 memset(&uncomp_data, 0, sizeof(DBT)); // decompressed input (the key doesn't change)
402 // Walk through the database, calling export functions as we go and clearing buffers before each call.
403 while (ret = src_dbcp->get(src_dbcp, &in_key, &in_data, DB_NEXT) == 0) {
405 // If either the key or data are zero length, skip this record
406 if ((in_key.size == 0) || (in_data.size == 0)) {
410 else { // Both key and data are >0 length so we're good to go
412 // Do we need to decompress?
413 static int32_t magic = COMPRESS_MAGIC;
415 if ((in_data.size >= sizeof(struct CtdlCompressHeader)) && (!memcmp(in_data.data, &magic, sizeof(magic)))) {
417 // yes, we need to decompress
419 struct CtdlCompressHeader comp;
420 memcpy(&comp, in_data.data, sizeof(struct CtdlCompressHeader));
421 uncomp_data.size = comp.uncompressed_len;
422 uncomp_data.data = reallok(uncomp_data.data, uncomp_data.size);
423 destLen = (uLongf)comp.uncompressed_len;
425 ret = uncompress((Bytef *)uncomp_data.data, (uLongf *)&destLen,
426 (const Bytef *)in_data.data+sizeof(struct CtdlCompressHeader),
427 (uLong)comp.compressed_len);
429 fprintf(stderr, "ctdldump: uncompress() error %d\n", ret);
434 // Call the export function registered to this table
435 export_functions[which_cdb](which_cdb, &in_key, (compressed ? &uncomp_data : &in_data));
437 // Knowing the total number of rows isn't critical to the program. It's just for the user to know.
442 // free any leftover out_data pointers
443 free(uncomp_data.data);
445 // ...and close the database (table)
446 ret = src_dbp->close(src_dbp, 0);
448 fprintf(stderr, "ctdldump: db_close: %s\n", db_strerror(ret));
455 int main(int argc, char **argv) {
457 char *src_dir = NULL;
458 char *dst_dir = NULL;
460 static DB_ENV *src_dbenv; // Source DB environment (global)
462 // display the greeting
463 fprintf(stderr, "\033[44m\033[30m \033[K\033[0m\n"
464 "\033[44m\033[30m DB Dump utility for Citadel \033[K\033[0m\n"
465 "\033[44m\033[30m Copyright (c) 2023 by citadel.org et al. \033[K\033[0m\n"
466 "\033[44m\033[30m This program is open source software. Use, duplication, or disclosure \033[K\033[0m\n"
467 "\033[44m\033[30m is subject to the terms of the GNU General Public license v3. \033[K\033[0m\n"
468 "\033[44m\033[30m \033[K\033[0m\n");
470 // Parse command line
472 while ((a = getopt(argc, argv, "h:y")) != EOF) {
481 fprintf(stderr, "%s: usage: %s -s source_dir [>dumpfile]\n", argv[0], argv[0]);
486 if (confirmed == 1) {
487 fprintf(stderr, "ctdldump: You have specified the [-y] flag, so processing will continue.\n");
490 fprintf(stderr, "ctdldump: Please read [ https://www.citadel.org/dump-and-load.html ] to learn how to proceed.\n");
494 src_dbenv = open_dbenv(src_dir);
496 for (i = 0; i < MAXCDB; ++i) {
497 export_table(i, src_dbenv);
498 if (i == CDB_CONFIG) {
499 printf("config|regenerate_secondary_indices|1|\n"); // Force citserver to rebuild those tables
500 printf("config|MM_fulltext_wordbreaker|0|\n"); // Burn the full text search index
503 close_dbenv(src_dbenv);