]> code.citadel.org Git - citadel.git/blob - citadel/utils/ctdldump.c
ctdldump: dump config records. This is now functionally complete.
[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                 exit(1);
37         }
38         return p;
39 }
40 #define realloc reallok
41
42
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;
47         int i;
48         char ch;
49
50         if ((outbuf == NULL) || (outlen < (len * 2))) {
51                 outbuf = reallok(outbuf, (len * 2));
52                 outlen = len * 2;
53         }
54
55         for (i=0; i<len; ++i) {
56                 ch = 0;
57                 memcpy(&ch, data+i, 1);
58                 sprintf((outbuf + (i * 2)), "%02X", (int) ch);
59         }
60
61         return(outbuf);
62 }
63
64
65 // Open a database environment
66 DB_ENV *open_dbenv(char *dirname) {
67
68         DB_ENV *dbenv = NULL;
69
70         int ret;
71         int i;
72         u_int32_t flags = 0;
73         int dbversion_major, dbversion_minor, dbversion_patch;
74
75         fprintf(stderr,
76                 "db: open_dbenv() starting\n"
77                 "db:    Linked zlib: %s\n"
78                 "db: Compiled libdb: %s\n"
79                 "db:   Linked libdb: %s\n",
80                 zlibVersion(),
81                 DB_VERSION_STRING,
82                 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch)
83         );
84
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.");
92                 exit(CTDLEXIT_DB);
93         }
94
95         fprintf(stderr, "db: Setting up DB environment\n");
96         ret = db_env_create(&dbenv, 0);
97         if (ret) {
98                 fprintf(stderr, "db: db_env_create: %s\n", db_strerror(ret));
99                 fprintf(stderr, "db: exit code %d\n", ret);
100                 exit(CTDLEXIT_DB);
101         }
102
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);
105         if (ret) {
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);
109                 exit(CTDLEXIT_DB);
110         }
111
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);
116                 exit(CTDLEXIT_DB);
117         }
118
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);
122         if (ret) {
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);
126                 exit(CTDLEXIT_DB);
127         }
128
129         return(dbenv);
130 }
131
132
133 void close_dbenv(DB_ENV *dbenv) {
134         int ret = dbenv->close(dbenv, 0);
135         if (ret) {
136                 fprintf(stderr, "db: dbenv->close: %s\n", db_strerror(ret));
137         }
138 }
139
140
141 // export function for a message in msgmain
142 void export_msgmain(int which_cdb, DBT *in_key, DBT *in_data) {
143         long in_msgnum;
144
145         memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
146
147         // If the msgnum is negative, we are looking at METADATA
148         if (in_msgnum < 0) {
149                 struct MetaData *meta = (struct MetaData *)in_data->data;
150                 printf("msgmeta|%ld|%d|%s|%ld|\n",
151                         meta->meta_msgnum,
152                         meta->meta_refcount,
153                         meta->meta_content_type,
154                         meta->meta_rfc822_length
155                 );
156         }
157
158         // If the msgnum is positive, we are looking at a MESSAGE
159         else if (in_msgnum > 0) {
160                 printf("msgtext|%ld|%s|\n", in_msgnum, hexout(in_data->data, in_data->size));
161         }
162
163         // If the msgnum is 0 it's probably not a valid record.
164 }
165
166
167 // export function for a user record
168 void export_user(int which_cdb, DBT *in_key, DBT *in_data) {
169
170         struct ctdluser *user = (struct ctdluser *)in_data->data;
171
172         printf("user|%d|%d|%s|%u|%d|%ld|%ld|%d|%s|%ld|%ld|%s|%ld|%ld|\n",
173                 user->version,
174                 user->uid,
175                 user->password,
176                 user->flags,
177                 user->axlevel,
178                 user->usernum,
179                 user->lastcall,
180                 user->USuserpurge,
181                 user->fullname,
182                 user->msgnum_bio,
183                 user->msgnum_pic,
184                 hexout(user->emailaddrs, strlen(user->emailaddrs)),
185                 user->msgnum_inboxrules,
186                 user->lastproc_inboxrules
187         );
188 }
189
190
191 // export function for a room record
192 void export_room(int which_cdb, DBT *in_key, DBT *in_data) {
193
194         struct ctdlroom *room = (struct ctdlroom *)in_data->data;
195
196         printf("room|%s|%s|%ld|%ld|%ld|%u|%s|%ld|%d|%ld|%d|%d|%ld|%d|%u|%d|%ld|\n",
197                 room->QRname,
198                 room->QRpasswd,
199                 room->QRroomaide,
200                 room->QRhighest,
201                 room->QRgen,
202                 room->QRflags,
203                 room->QRdirname,
204                 room->msgnum_info,
205                 room->QRfloor,
206                 room->QRmtime,
207                 room->QRep.expire_mode,
208                 room->QRep.expire_value,
209                 room->QRnumber,
210                 room->QRorder,
211                 room->QRflags2,
212                 room->QRdefaultview,
213                 room->msgnum_pic
214         );
215 }
216
217
218 // export function for a floor record
219 void export_floor(int which_cdb, DBT *in_key, DBT *in_data) {
220
221         int floor_num;
222         memcpy(&floor_num, in_key->data, sizeof(int));
223
224         struct floor *floor = (struct floor *)in_data->data;
225
226         printf("floor|%d|%u|%s|%d|%d|%d|\n",
227                 floor_num,
228                 floor->f_flags,
229                 floor->f_name,
230                 floor->f_ref_count,
231                 floor->f_ep.expire_mode,
232                 floor->f_ep.expire_value
233         );
234 }
235
236
237 // export function for a msglist or a fulltext index record
238 // (both are indexed by a long and the data is arrays of longs)
239 void export_msglist(int which_cdb, DBT *in_key, DBT *in_data) {
240         int i;
241         int num_msgs;
242         long msg;
243
244         // records are indexed by a single "long" and contains an array of zero or more "long"s
245         long roomnum;
246         memcpy(&roomnum, in_key->data, sizeof(long));
247
248         printf("msglist|%ld|", roomnum);
249
250         if (in_data->size > 0) {
251                 num_msgs = in_data->size / sizeof(long);
252                 for (i=0; i<num_msgs; ++i) {
253                         memcpy(&msg, (in_data->data + (i * sizeof(long))), sizeof(long));
254                         if (i != 0) {
255                                 printf(",");
256                         }
257                         printf("%ld", msg);
258                 }
259         }
260         printf("|\n");
261 }
262
263
264 // export function for a visit record
265 void export_visit(int which_cdb, DBT *in_key, DBT *in_data) {
266         struct visit *visit = (struct visit *)in_data->data;
267         printf("visit|%ld|%ld|%ld|%ld|%u|%s|%s|%d|\n",
268                 visit->v_roomnum,
269                 visit->v_roomgen,
270                 visit->v_usernum,
271                 visit->v_lastseen,
272                 visit->v_flags,
273                 visit->v_seen,
274                 visit->v_answered,
275                 visit->v_view
276         );
277 }
278
279
280 // export function for a directory record
281 // (This is a secondary index -- should we just regenerate the data after import?)
282 void export_dir(int which_cdb, DBT *in_key, DBT *in_data) {
283         printf("dir|");
284         fwrite(in_key->data, in_key->size, 1, stdout);
285         printf("|%s|\n", (char *)in_data->data);
286 }
287
288
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);
293 }
294
295
296 // export function for large message texts
297 void export_bigmsg(int which_cdb, DBT *in_key, DBT *in_data) {
298         long msgnum;
299
300         memcpy(&msgnum, in_key->data, sizeof(msgnum));
301         printf("bigmsg|%ld|%s|\n", msgnum, hexout(in_data->data, in_data->size));
302 }
303
304
305 // export function for EUID Index records
306 void export_euidindex(int which_cdb, DBT *in_key, DBT *in_data) {
307
308         // The structure of an euidindex record *key* is:
309         // |----room_number----|----------EUID-------------|
310         //    (sizeof long)       (actual length of euid)
311
312         // The structure of an euidindex record *value* is:
313         // |-----msg_number----|----room_number----|----------EUID-------------|
314         //    (sizeof long)       (sizeof long)       (actual length of euid)
315
316         long msgnum, roomnum;
317         char *euid;
318
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);
322
323         printf("euidindex|%ld|%ld|%s|\n", msgnum, roomnum, euid);
324 }
325
326
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) {
330
331         // key is a long
332         long usernum;
333         memcpy(&usernum, in_key->data, sizeof(usernum));
334
335         // value is a string
336         printf("usersbynumber|%ld|%s|\n", usernum, (char *)in_data->data);
337 }
338
339
340 // export function for a config record
341 void export_config(int which_cdb, DBT *in_key, DBT *in_data) {
342
343         size_t keylen = in_key->size;
344         printf("config|");
345         fwrite(in_data->data, keylen, 1, stdout);
346
347         printf("|");
348         fwrite(in_data->data + keylen, in_data->size - keylen, 1, stdout);
349         printf("|\n");
350 }
351
352
353 // For obsolete databases, zero all the output
354 void zero_function(int which_cdb, DBT *in_key, DBT *in_data) {
355         // do nothing
356 }
357
358
359 void (*export_functions[])(int which_cdb, DBT *in_key, DBT *in_data) = {
360         export_msgmain,         // CDB_MSGMAIN
361         export_user,            // CDB_USERS
362         export_room,            // CDB_ROOMS
363         export_floor,           // CDB_FLOORTAB
364         export_msglist,         // CDB_MSGLISTS
365         export_visit,           // CDB_VISIT
366         export_dir,             // CDB_DIRECTORY
367         export_usetable,        // CDB_USETABLE
368         export_bigmsg,          // CDB_BIGMSGS
369         zero_function,          // CDB_FULLTEXT (regenerate this on the server)
370         export_euidindex,       // CDB_EUIDINDEX
371         export_usersbynumber,   // CDB_USERSBYNUMBER
372         zero_function,          // CDB_UNUSED1 (obsolete)
373         export_config           // CDB_CONFIG
374 };
375
376
377 void export_table(int which_cdb, DB_ENV *src_dbenv) {
378         int ret;
379         int compressed;
380         char dbfilename[32];
381         uLongf destLen = 0;
382
383         // shamelessly swiped from https://docs.oracle.com/database/bdb181/html/programmer_reference/am_cursor.html
384         DB *src_dbp;
385         DBC *src_dbcp;
386         DBT in_key, in_data, uncomp_data;
387         int num_good_rows = 0;
388         int num_bad_rows = 0;
389
390         snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", which_cdb);
391
392         // create a database handle for the source table
393         ret = db_create(&src_dbp, src_dbenv, 0);
394         if (ret) {
395                 fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
396                 fprintf(stderr, "db: exit code %d\n", ret);
397                 exit(CTDLEXIT_DB);
398         }
399
400         // open the file containing the source table
401         ret = src_dbp->open(src_dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
402         if (ret) {
403                 fprintf(stderr, "db: db_open: %s\n", db_strerror(ret));
404                 fprintf(stderr, "db: exit code %d\n", ret);
405                 exit(CTDLEXIT_DB);
406         }
407
408         // Acquire a cursor to read the source table
409         if ((ret = src_dbp->cursor(src_dbp, NULL, &src_dbcp, 0)) != 0) {
410                 fprintf(stderr, "db: db_cursor: %s\n", db_strerror(ret));
411                 fprintf(stderr, "db: exit code %d\n", ret);
412                 exit(CTDLEXIT_DB);
413         }
414
415         // Zero out these database keys
416         memset(&in_key,         0, sizeof(DBT));        // input
417         memset(&in_data,        0, sizeof(DBT));
418         memset(&uncomp_data,    0, sizeof(DBT));        // decompressed input (the key doesn't change)
419
420         // Walk through the database, calling export functions as we go and clearing buffers before each call.
421         while (ret = src_dbcp->get(src_dbcp, &in_key, &in_data, DB_NEXT) == 0) {
422         
423                 // If either the key or data are zero length, skip this record
424                 if ((in_key.size == 0) || (in_data.size == 0)) {
425                         ++num_bad_rows;
426                 }
427
428                 else {  // Both key and data are >0 length so we're good to go
429
430                         // Do we need to decompress?
431                         static int32_t magic = COMPRESS_MAGIC;
432                         compressed = 0;
433                         if ((in_data.size >= sizeof(struct CtdlCompressHeader)) && (!memcmp(in_data.data, &magic, sizeof(magic)))) {
434         
435                                 // yes, we need to decompress
436                                 compressed = 1;
437                                 struct CtdlCompressHeader comp;
438                                 memcpy(&comp, in_data.data, sizeof(struct CtdlCompressHeader));
439                                 uncomp_data.size = comp.uncompressed_len;
440                                 uncomp_data.data = realloc(uncomp_data.data, uncomp_data.size);
441                                 destLen = (uLongf)comp.uncompressed_len;
442         
443                                 ret = uncompress((Bytef *)uncomp_data.data, (uLongf *)&destLen,
444                                                 (const Bytef *)in_data.data+sizeof(struct CtdlCompressHeader),
445                                                 (uLong)comp.compressed_len);
446                                 if (ret != Z_OK) {
447                                         fprintf(stderr, "db: uncompress() error %d\n", ret);
448                                         exit(CTDLEXIT_DB);
449                                 }
450                         }
451         
452                         // Call the export function registered to this table
453                         export_functions[which_cdb](which_cdb, &in_key, (compressed ? &uncomp_data : &in_data));
454         
455                         // Knowing the total number of rows isn't critical to the program.  It's just for the user to know.
456                         fflush(stdout);
457                 }
458         }
459
460         // free any leftover out_data pointers
461         free(uncomp_data.data);
462
463         // ...and close the database (table)
464         ret = src_dbp->close(src_dbp, 0);
465         if (ret) {
466                 fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
467         }
468
469
470 }
471
472
473 int main(int argc, char **argv) {
474         int i = 0;
475         char *src_dir = NULL;
476         char *dst_dir = NULL;
477         int confirmed = 0;
478         static DB_ENV *src_dbenv;               // Source DB environment (global)
479
480         // Parse command line
481         int a;
482         while ((a = getopt(argc, argv, "h:d:y")) != EOF) {
483                 switch (a) {
484                 case 'h':
485                         src_dir = optarg;
486                         break;
487                 case 'd':
488                         dst_dir = optarg;
489                         break;
490                 case 'y':
491                         confirmed = 1;
492                         break;
493                 default:
494                         fprintf(stderr, "%s: usage: %s -s source_dir -d dest_dir\n", argv[0], argv[0]);
495                         exit(2);
496                 }
497         }
498
499         // Warn the user
500         fprintf(stderr, "------------------------------------------------------------------------\n");
501         fprintf(stderr, "This utility must be run while the server is NOT RUNNING.               \n");
502         fprintf(stderr, "We \033[1mguarantee\033[0m data corruption if you do not                              \n");
503         fprintf(stderr, "observe this warning!  The source [-s] directory should contain a copy  \n");
504         fprintf(stderr, "of the database from your source system.  The dump [-d] directory       \n");
505         fprintf(stderr, "should be empty and will receive your dump file.                        \n");
506         fprintf(stderr, "------------------------------------------------------------------------\n");
507         fprintf(stderr, " Source (database) directory: %s\n", src_dir);
508         fprintf(stderr, "------------------------------------------------------------------------\n");
509
510         if (confirmed == 1) {
511                 fprintf(stderr, "You have specified the [-y] flag, so processing will continue.\n");
512         }
513         else {
514                 fprintf(stderr, "Please read [ https://www.citadel.org/ctdldump.html ] to learn how to proceed.\n");
515                 exit(0);
516         }
517
518         src_dbenv = open_dbenv(src_dir);
519         for (i = 0; i < MAXCDB; ++i) {
520                 export_table(i, src_dbenv);
521         }
522         close_dbenv(src_dbenv);
523
524         exit(0);
525 }