]> code.citadel.org Git - citadel.git/blob - citadel/utils/ctdldump.c
ctdldump/ctdlload: -h now specifies the top-level Citadel directory, just like all...
[citadel.git] / citadel / utils / ctdldump.c
1 // Don't run this.  It doesn't work and if you try to run it you will immediately die.
2 //
3 // Copyright (c) 2023 by Art Cancro citadel.org
4 //
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.
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <ctype.h>
11 #include <stdio.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <sys/un.h>
16 #include <netdb.h>
17 #include <string.h>
18 #include <pwd.h>
19 #include <errno.h>
20 #include <stdarg.h>
21 #include <limits.h>
22 #include <libcitadel.h>
23 #include <zlib.h>
24 #include <db.h>
25 #include "../server/sysdep.h"
26 #include "../server/citadel_defs.h"
27 #include "../server/server.h"
28 #include "../server/citadel_dirs.h"
29
30
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);
34         if (!p) {
35                 fprintf(stderr, "realloc() failed to resize %p to %ld bytes, error: %m\n", ptr, size);
36                 abort();
37         }
38         return p;
39 }
40
41
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;
46
47         if ((outbuf == NULL) || (outlen < (len * 2))) {
48                 outbuf = reallok(outbuf, (len * 2));
49                 outlen = len * 2;
50         }
51
52         CtdlEncodeBase64(outbuf, data, len, 0);
53         return(outbuf);
54 }
55
56
57 // Open a database environment
58 DB_ENV *open_dbenv(char *dirname) {
59
60         DB_ENV *dbenv = NULL;
61
62         int ret;
63         int i;
64         u_int32_t flags = 0;
65         int dbversion_major, dbversion_minor, dbversion_patch;
66
67         db_version(&dbversion_major, &dbversion_minor, &dbversion_patch);
68
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.");
76                 exit(CTDLEXIT_DB);
77         }
78
79         ret = db_env_create(&dbenv, 0);
80         if (ret) {
81                 fprintf(stderr, "ctdldump: db_env_create: %s\n", db_strerror(ret));
82                 fprintf(stderr, "ctdldump: exit code %d\n", ret);
83                 exit(CTDLEXIT_DB);
84         }
85
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);
88         if (ret) {
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);
92                 exit(CTDLEXIT_DB);
93         }
94
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);
99                 exit(CTDLEXIT_DB);
100         }
101
102         flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
103         ret = dbenv->open(dbenv, dirname, flags, 0);
104         if (ret) {
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);
108                 exit(CTDLEXIT_DB);
109         }
110
111         return(dbenv);
112 }
113
114
115 void close_dbenv(DB_ENV *dbenv) {
116         int ret = dbenv->close(dbenv, 0);
117         if (ret) {
118                 fprintf(stderr, "ctdldump: dbenv->close: %s\n", db_strerror(ret));
119         }
120 }
121
122
123 // export function for a message in msgmain
124 void export_msgmain(int which_cdb, DBT *in_key, DBT *in_data) {
125         long in_msgnum;
126
127         memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
128
129         // If the msgnum is negative, we are looking at METADATA
130         if (in_msgnum < 0) {
131                 struct MetaData *meta = (struct MetaData *)in_data->data;
132                 printf("msgmeta|%ld|%d|%s|%ld|\n",
133                         meta->meta_msgnum,
134                         meta->meta_refcount,
135                         meta->meta_content_type,
136                         meta->meta_rfc822_length
137                 );
138         }
139
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));
143         }
144
145         // If the msgnum is 0 it's probably not a valid record.
146 }
147
148
149 // export function for a user record
150 void export_user(int which_cdb, DBT *in_key, DBT *in_data) {
151
152         struct ctdluser *user = (struct ctdluser *)in_data->data;
153
154         printf("user|%d|%d|%s|%u|%d|%ld|%ld|%d|%s|%ld|%ld|%s|%ld|%ld|\n",
155                 user->version,
156                 user->uid,
157                 user->password,
158                 user->flags,
159                 user->axlevel,
160                 user->usernum,
161                 user->lastcall,
162                 user->USuserpurge,
163                 user->fullname,
164                 user->msgnum_bio,
165                 user->msgnum_pic,
166                 b64out(user->emailaddrs, strlen(user->emailaddrs)),
167                 user->msgnum_inboxrules,
168                 user->lastproc_inboxrules
169         );
170 }
171
172
173 // export function for a room record
174 void export_room(int which_cdb, DBT *in_key, DBT *in_data) {
175
176         struct ctdlroom *room = (struct ctdlroom *)in_data->data;
177
178         printf("room|%s|%s|%ld|%ld|%ld|%u|%s|%ld|%d|%ld|%d|%d|%ld|%d|%u|%d|%ld|\n",
179                 room->QRname,
180                 room->QRpasswd,
181                 room->QRroomaide,
182                 room->QRhighest,
183                 room->QRgen,
184                 room->QRflags,
185                 room->QRdirname,
186                 room->msgnum_info,
187                 room->QRfloor,
188                 room->QRmtime,
189                 room->QRep.expire_mode,
190                 room->QRep.expire_value,
191                 room->QRnumber,
192                 room->QRorder,
193                 room->QRflags2,
194                 room->QRdefaultview,
195                 room->msgnum_pic
196         );
197 }
198
199
200 // export function for a floor record
201 void export_floor(int which_cdb, DBT *in_key, DBT *in_data) {
202
203         int floor_num;
204         memcpy(&floor_num, in_key->data, sizeof(int));
205
206         struct floor *floor = (struct floor *)in_data->data;
207
208         printf("floor|%d|%u|%s|%d|%d|%d|\n",
209                 floor_num,
210                 floor->f_flags,
211                 floor->f_name,
212                 floor->f_ref_count,
213                 floor->f_ep.expire_mode,
214                 floor->f_ep.expire_value
215         );
216 }
217
218
219 // export function for a msglist
220 // (indexed by a long and the data is arrays of longs)
221 void export_msglist(int which_cdb, DBT *in_key, DBT *in_data) {
222         int i;
223         int num_msgs;
224         long msg;
225
226         // records are indexed by a single "long" and contains an array of zero or more "long"s
227         long roomnum;
228         memcpy(&roomnum, in_key->data, sizeof(long));
229
230         printf("msglist|%ld|", roomnum);
231
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));
236                         if (i != 0) {
237                                 printf(",");
238                         }
239                         printf("%ld", msg);
240                 }
241         }
242         printf("|\n");
243 }
244
245
246 // export function for a full text search index record
247 // (indexed by an int and the data is arrays of longs)
248 void export_fulltext(int which_cdb, DBT *in_key, DBT *in_data) {
249         int i;
250         int num_msgs;
251         long msg;
252
253         // records are indexed by a single "int" and contains an array of zero or more "long"s
254         int indexnum;
255         memcpy(&indexnum, in_key->data, sizeof(int));
256
257         printf("fulltext|%d|", indexnum);
258
259         if (in_data->size > 0) {
260                 num_msgs = in_data->size / sizeof(long);
261                 for (i=0; i<num_msgs; ++i) {
262                         memcpy(&msg, (in_data->data + (i * sizeof(long))), sizeof(long));
263                         if (i != 0) {
264                                 printf(",");
265                         }
266                         printf("%ld", msg);
267                 }
268         }
269         printf("|\n");
270 }
271
272
273 // export function for a visit record
274 void export_visit(int which_cdb, DBT *in_key, DBT *in_data) {
275         struct visit *visit = (struct visit *)in_data->data;
276         int i, len;
277
278         // If there is corrupt data in the "seen" array, cut that out before exporting
279         len = strlen(visit->v_seen);
280         for (i=0; i<len; ++i) {
281                 if (!isprint(visit->v_seen[i])) {
282                         visit->v_seen[i] = 0;
283                 }
284         }
285
286         // If there is corrupt data in the "answered" array, cut that out before exporting
287         len = strlen(visit->v_answered);
288         for (i=0; i<len; ++i) {
289                 if (!isprint(visit->v_answered[i])) {
290                         visit->v_answered[i] = 0;
291                 }
292         }
293
294         // output the record
295         printf("visit|%ld|%ld|%ld|%ld|%u|%s|%s|%d|\n",
296                 visit->v_roomnum,
297                 visit->v_roomgen,
298                 visit->v_usernum,
299                 visit->v_lastseen,
300                 visit->v_flags,
301                 visit->v_seen,
302                 visit->v_answered,
303                 visit->v_view
304         );
305 }
306
307
308 // export function for a directory record
309 void export_dir(int which_cdb, DBT *in_key, DBT *in_data) {
310         printf("dir|");
311         fwrite(in_key->data, in_key->size, 1, stdout);
312         printf("|%s|\n", (char *)in_data->data);
313 }
314
315
316 // export function for a use table record
317 void export_usetable(int which_cdb, DBT *in_key, DBT *in_data) {
318         struct UseTable *u = (struct UseTable *)in_data->data;
319         printf("use|%d|%ld|\n", u->hash, u->timestamp);
320 }
321
322
323 // export function for large message texts
324 void export_bigmsg(int which_cdb, DBT *in_key, DBT *in_data) {
325         long msgnum;
326
327         memcpy(&msgnum, in_key->data, sizeof(msgnum));
328         printf("bigmsg|%ld|%s|\n", msgnum, b64out(in_data->data, in_data->size));
329 }
330
331
332 // export function for EUID Index records
333 void export_euidindex(int which_cdb, DBT *in_key, DBT *in_data) {
334
335         // The structure of an euidindex record *key* is:
336         // |----room_number----|----------EUID-------------|
337         //    (sizeof long)       (actual length of euid)
338
339         // The structure of an euidindex record *value* is:
340         // |-----msg_number----|----room_number----|----------EUID-------------|
341         //    (sizeof long)       (sizeof long)       (actual length of euid)
342
343         long msgnum, roomnum;
344         char *euid;
345
346         memcpy(&msgnum, in_data->data, sizeof(long));
347         memcpy(&roomnum, in_data->data+sizeof(long), sizeof(msgnum));
348         euid = in_data->data+(sizeof(long)*2);
349
350         printf("euidindex|%ld|%ld|%s|\n", msgnum, roomnum, euid);
351 }
352
353
354 // export users-by-number records
355 // (This is a secondary index -- should we just regenerate the data after import?)
356 void export_usersbynumber(int which_cdb, DBT *in_key, DBT *in_data) {
357
358         // key is a long
359         long usernum;
360         memcpy(&usernum, in_key->data, sizeof(usernum));
361
362         // value is a string
363         printf("usersbynumber|%ld|%s|\n", usernum, (char *)in_data->data);
364 }
365
366
367 // export function for a config record
368 void export_config(int which_cdb, DBT *in_key, DBT *in_data) {
369
370         printf("config|%s|%s|\n",
371                 (char *)in_data->data,
372                 (char *)in_data->data + strlen(in_data->data) + 1
373         );
374
375 }
376
377
378 // For obsolete databases, zero all the output
379 void zero_function(int which_cdb, DBT *in_key, DBT *in_data) {
380         // do nothing
381 }
382
383
384 void (*export_functions[])(int which_cdb, DBT *in_key, DBT *in_data) = {
385         export_msgmain,         // 00 CDB_MSGMAIN       
386         export_user,            // 01 CDB_USERS
387         export_room,            // 02 CDB_ROOMS
388         export_floor,           // 03 CDB_FLOORTAB
389         export_msglist,         // 04 CDB_MSGLISTS
390         export_visit,           // 05 CDB_VISIT
391         export_dir,             // 06 CDB_DIRECTORY
392         export_usetable,        // 07 CDB_USETABLE
393         export_bigmsg,          // 08 CDB_BIGMSGS
394         export_fulltext,        // 09 CDB_FULLTEXT
395         export_euidindex,       // 0a CDB_EUIDINDEX
396         export_usersbynumber,   // 0b CDB_USERSBYNUMBER
397         zero_function,          // 0c CDB_UNUSED1 (obsolete)
398         export_config           // 0d CDB_CONFIG
399 };
400
401
402 void export_table(int which_cdb, DB_ENV *src_dbenv) {
403         int ret;
404         int compressed;
405         char dbfilename[32];
406         uLongf destLen = 0;
407
408         // shamelessly swiped from https://docs.oracle.com/database/bdb181/html/programmer_reference/am_cursor.html
409         DB *src_dbp;
410         DBC *src_dbcp;
411         DBT in_key, in_data, uncomp_data;
412         int num_good_rows = 0;
413         int num_bad_rows = 0;
414
415         snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", which_cdb);
416
417         // create a database handle for the source table
418         ret = db_create(&src_dbp, src_dbenv, 0);
419         if (ret) {
420                 fprintf(stderr, "ctdldump: db_create: %s\n", db_strerror(ret));
421                 fprintf(stderr, "ctdldump: exit code %d\n", ret);
422                 exit(CTDLEXIT_DB);
423         }
424
425         // open the file containing the source table
426         ret = src_dbp->open(src_dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
427         if (ret) {
428                 fprintf(stderr, "ctdldump: db_open(%s): %s (skipping empty or missing table)\n", dbfilename, db_strerror(ret));
429         }
430         else {
431
432                 // Acquire a cursor to read the source table
433                 if ((ret = src_dbp->cursor(src_dbp, NULL, &src_dbcp, 0)) != 0) {
434                         fprintf(stderr, "ctdldump: db_cursor: %s\n", db_strerror(ret));
435                         fprintf(stderr, "ctdldump: exit code %d\n", ret);
436                         exit(CTDLEXIT_DB);
437                 }
438         
439                 // Zero out these database keys
440                 memset(&in_key,         0, sizeof(DBT));        // input
441                 memset(&in_data,        0, sizeof(DBT));
442                 memset(&uncomp_data,    0, sizeof(DBT));        // decompressed input (the key doesn't change)
443         
444                 // Walk through the database, calling export functions as we go and clearing buffers before each call.
445                 while (ret = src_dbcp->get(src_dbcp, &in_key, &in_data, DB_NEXT) == 0) {
446                 
447                         // If either the key or data are zero length, skip this record
448                         if ((in_key.size == 0) || (in_data.size == 0)) {
449                                 ++num_bad_rows;
450                         }
451         
452                         else {  // Both key and data are >0 length so we're good to go
453         
454                                 // Do we need to decompress?
455                                 static int32_t magic = COMPRESS_MAGIC;
456                                 compressed = 0;
457                                 if ((in_data.size >= sizeof(struct CtdlCompressHeader)) && (!memcmp(in_data.data, &magic, sizeof(magic)))) {
458                 
459                                         // yes, we need to decompress
460                                         compressed = 1;
461                                         struct CtdlCompressHeader comp;
462                                         memcpy(&comp, in_data.data, sizeof(struct CtdlCompressHeader));
463                                         uncomp_data.size = comp.uncompressed_len;
464                                         uncomp_data.data = reallok(uncomp_data.data, uncomp_data.size);
465                                         destLen = (uLongf)comp.uncompressed_len;
466                 
467                                         ret = uncompress((Bytef *)uncomp_data.data, (uLongf *)&destLen,
468                                                         (const Bytef *)in_data.data+sizeof(struct CtdlCompressHeader),
469                                                         (uLong)comp.compressed_len);
470                                         if (ret != Z_OK) {
471                                                 fprintf(stderr, "ctdldump: uncompress() error %d\n", ret);
472                                                 exit(CTDLEXIT_DB);
473                                         }
474                                 }
475                 
476                                 // Call the export function registered to this table
477                                 export_functions[which_cdb](which_cdb, &in_key, (compressed ? &uncomp_data : &in_data));
478                 
479                                 // Knowing the total number of rows isn't critical to the program.  It's just for the user to know.
480                                 fflush(stdout);
481                         }
482                 }
483         }
484
485         // free any leftover out_data pointers
486         free(uncomp_data.data);
487
488         // ...and close the database (table)
489         ret = src_dbp->close(src_dbp, 0);
490         if (ret) {
491                 fprintf(stderr, "ctdldump: db_close: %s\n", db_strerror(ret));
492         }
493
494
495 }
496
497
498 int main(int argc, char **argv) {
499         int i = 0;
500         char src_dir[PATH_MAX];
501         int confirmed = 0;
502         static DB_ENV *src_dbenv;               // Source DB environment (global)
503
504         // display the greeting
505         fprintf(stderr, "\033[44m\033[33m\033[1m \033[K\033[0m\n"
506                         "\033[44m\033[33m\033[1m DB Dump utility for Citadel \033[K\033[0m\n"
507                         "\033[44m\033[33m\033[1m Copyright (c) 2023 by citadel.org et al.  \033[K\033[0m\n"
508                         "\033[44m\033[33m\033[1m This program is open source software.  Use, duplication, or disclosure \033[K\033[0m\n"
509                         "\033[44m\033[33m\033[1m is subject to the terms of the GNU General Public license v3. \033[K\033[0m\n"
510                         "\033[44m\033[33m\033[1m \033[K\033[0m\n");
511
512         // Default source directory unless overridden
513         snprintf(src_dir, sizeof(src_dir), "%s/data", CTDLDIR);
514
515         // Parse command line
516         int a;
517         while ((a = getopt(argc, argv, "h:y")) != EOF) {
518                 switch (a) {
519                 case 'h':
520                         snprintf(src_dir, sizeof(src_dir), "%s/data", optarg);
521                         break;
522                 case 'y':
523                         confirmed = 1;
524                         break;
525                 default:
526                         fprintf(stderr, "%s: usage: %s -s citadel_dir [>dumpfile]\n", argv[0], argv[0]);
527                         exit(2);
528                 }
529         }
530
531         if (src_dir == NULL) {
532                 fprintf(stderr, "ctdldump: no source directory was specified.\n");
533                 exit(1);
534         }
535
536         if (confirmed == 1) {
537                 fprintf(stderr, "ctdldump: You have specified the [-y] flag, so processing will continue.\n");
538         }
539         else {
540                 fprintf(stderr, "ctdldump: usage: ctdldump -y -h[citadel_dir] >[dump_file]\n");
541                 fprintf(stderr, "          [citadel_dir] is your database directory, usually /usr/local/citadel\n");
542                 fprintf(stderr, "          Please read [ https://www.citadel.org/dump-and-load.html ] to learn how to proceed.\n");
543                 exit(1);
544         }
545
546         src_dbenv = open_dbenv(src_dir);
547         printf("begin|\n");
548         for (i = 0; i < MAXCDB; ++i) {
549                 export_table(i, src_dbenv);
550         }
551         close_dbenv(src_dbenv);
552         printf("end|\n");
553
554         fprintf(stderr, "ctdldump: \033[32m\033[1mfinished\033[0m\n");
555         exit(0);
556 }