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