]> code.citadel.org Git - citadel.git/blob - citadel/utils/ctdldump.c
9bfe93c59e11a8545da2498180542755c3edcbe0
[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
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         int i;
47         char ch;
48
49         if ((outbuf == NULL) || (outlen < (len * 2))) {
50                 outbuf = reallok(outbuf, (len * 2));
51                 outlen = len * 2;
52         }
53
54         CtdlEncodeBase64(outbuf, data, len, 0);
55         return(outbuf);
56 }
57
58
59 // Open a database environment
60 DB_ENV *open_dbenv(char *dirname) {
61
62         DB_ENV *dbenv = NULL;
63
64         int ret;
65         int i;
66         u_int32_t flags = 0;
67         int dbversion_major, dbversion_minor, dbversion_patch;
68
69         db_version(&dbversion_major, &dbversion_minor, &dbversion_patch);
70
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.");
78                 exit(CTDLEXIT_DB);
79         }
80
81         ret = db_env_create(&dbenv, 0);
82         if (ret) {
83                 fprintf(stderr, "ctdldump: db_env_create: %s\n", db_strerror(ret));
84                 fprintf(stderr, "ctdldump: exit code %d\n", ret);
85                 exit(CTDLEXIT_DB);
86         }
87
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);
90         if (ret) {
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);
94                 exit(CTDLEXIT_DB);
95         }
96
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);
101                 exit(CTDLEXIT_DB);
102         }
103
104         flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
105         ret = dbenv->open(dbenv, dirname, flags, 0);
106         if (ret) {
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);
110                 exit(CTDLEXIT_DB);
111         }
112
113         return(dbenv);
114 }
115
116
117 void close_dbenv(DB_ENV *dbenv) {
118         int ret = dbenv->close(dbenv, 0);
119         if (ret) {
120                 fprintf(stderr, "ctdldump: dbenv->close: %s\n", db_strerror(ret));
121         }
122 }
123
124
125 // export function for a message in msgmain
126 void export_msgmain(int which_cdb, DBT *in_key, DBT *in_data) {
127         long in_msgnum;
128
129         memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
130
131         // If the msgnum is negative, we are looking at METADATA
132         if (in_msgnum < 0) {
133                 struct MetaData *meta = (struct MetaData *)in_data->data;
134                 printf("msgmeta|%ld|%d|%s|%ld|\n",
135                         meta->meta_msgnum,
136                         meta->meta_refcount,
137                         meta->meta_content_type,
138                         meta->meta_rfc822_length
139                 );
140         }
141
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));
145         }
146
147         // If the msgnum is 0 it's probably not a valid record.
148 }
149
150
151 // export function for a user record
152 void export_user(int which_cdb, DBT *in_key, DBT *in_data) {
153
154         struct ctdluser *user = (struct ctdluser *)in_data->data;
155
156         printf("user|%d|%d|%s|%u|%d|%ld|%ld|%d|%s|%ld|%ld|%s|%ld|%ld|\n",
157                 user->version,
158                 user->uid,
159                 user->password,
160                 user->flags,
161                 user->axlevel,
162                 user->usernum,
163                 user->lastcall,
164                 user->USuserpurge,
165                 user->fullname,
166                 user->msgnum_bio,
167                 user->msgnum_pic,
168                 b64out(user->emailaddrs, strlen(user->emailaddrs)),
169                 user->msgnum_inboxrules,
170                 user->lastproc_inboxrules
171         );
172 }
173
174
175 // export function for a room record
176 void export_room(int which_cdb, DBT *in_key, DBT *in_data) {
177
178         struct ctdlroom *room = (struct ctdlroom *)in_data->data;
179
180         printf("room|%s|%s|%ld|%ld|%ld|%u|%s|%ld|%d|%ld|%d|%d|%ld|%d|%u|%d|%ld|\n",
181                 room->QRname,
182                 room->QRpasswd,
183                 room->QRroomaide,
184                 room->QRhighest,
185                 room->QRgen,
186                 room->QRflags,
187                 room->QRdirname,
188                 room->msgnum_info,
189                 room->QRfloor,
190                 room->QRmtime,
191                 room->QRep.expire_mode,
192                 room->QRep.expire_value,
193                 room->QRnumber,
194                 room->QRorder,
195                 room->QRflags2,
196                 room->QRdefaultview,
197                 room->msgnum_pic
198         );
199 }
200
201
202 // export function for a floor record
203 void export_floor(int which_cdb, DBT *in_key, DBT *in_data) {
204
205         int floor_num;
206         memcpy(&floor_num, in_key->data, sizeof(int));
207
208         struct floor *floor = (struct floor *)in_data->data;
209
210         printf("floor|%d|%u|%s|%d|%d|%d|\n",
211                 floor_num,
212                 floor->f_flags,
213                 floor->f_name,
214                 floor->f_ref_count,
215                 floor->f_ep.expire_mode,
216                 floor->f_ep.expire_value
217         );
218 }
219
220
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) {
224         int i;
225         int num_msgs;
226         long msg;
227
228         // records are indexed by a single "long" and contains an array of zero or more "long"s
229         long roomnum;
230         memcpy(&roomnum, in_key->data, sizeof(long));
231
232         printf("msglist|%ld|", roomnum);
233
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));
238                         if (i != 0) {
239                                 printf(",");
240                         }
241                         printf("%ld", msg);
242                 }
243         }
244         printf("|\n");
245 }
246
247
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",
252                 visit->v_roomnum,
253                 visit->v_roomgen,
254                 visit->v_usernum,
255                 visit->v_lastseen,
256                 visit->v_flags,
257                 visit->v_seen,
258                 visit->v_answered,
259                 visit->v_view
260         );
261 }
262
263
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) {
267         // printf("dir|");
268         // fwrite(in_key->data, in_key->size, 1, stdout);
269         // printf("|%s|\n", (char *)in_data->data);
270 // }
271
272
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);
277 }
278
279
280 // export function for large message texts
281 void export_bigmsg(int which_cdb, DBT *in_key, DBT *in_data) {
282         long msgnum;
283
284         memcpy(&msgnum, in_key->data, sizeof(msgnum));
285         printf("bigmsg|%ld|%s|\n", msgnum, b64out(in_data->data, in_data->size));
286 }
287
288
289 // export function for EUID Index records
290 //void export_euidindex(int which_cdb, DBT *in_key, DBT *in_data) {
291
292         // The structure of an euidindex record *key* is:
293         // |----room_number----|----------EUID-------------|
294         //    (sizeof long)       (actual length of euid)
295
296         // The structure of an euidindex record *value* is:
297         // |-----msg_number----|----room_number----|----------EUID-------------|
298         //    (sizeof long)       (sizeof long)       (actual length of euid)
299
300         //long msgnum, roomnum;
301         //char *euid;
302
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);
306 //
307         //printf("euidindex|%ld|%ld|%s|\n", msgnum, roomnum, euid);
308 //}
309
310
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) {
314
315         // key is a long
316         //long usernum;
317         //memcpy(&usernum, in_key->data, sizeof(usernum));
318
319         // value is a string
320         //printf("usersbynumber|%ld|%s|\n", usernum, (char *)in_data->data);
321 //}
322
323
324 // export function for a config record
325 void export_config(int which_cdb, DBT *in_key, DBT *in_data) {
326
327         printf("config|%s|%s|\n",
328                 (char *)in_data->data,
329                 (char *)in_data->data + strlen(in_data->data) + 1
330         );
331
332 }
333
334
335 // For obsolete databases, zero all the output
336 void zero_function(int which_cdb, DBT *in_key, DBT *in_data) {
337         // do nothing
338 }
339
340
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
356 };
357
358
359 void export_table(int which_cdb, DB_ENV *src_dbenv) {
360         int ret;
361         int compressed;
362         char dbfilename[32];
363         uLongf destLen = 0;
364
365         // shamelessly swiped from https://docs.oracle.com/database/bdb181/html/programmer_reference/am_cursor.html
366         DB *src_dbp;
367         DBC *src_dbcp;
368         DBT in_key, in_data, uncomp_data;
369         int num_good_rows = 0;
370         int num_bad_rows = 0;
371
372         snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", which_cdb);
373
374         // create a database handle for the source table
375         ret = db_create(&src_dbp, src_dbenv, 0);
376         if (ret) {
377                 fprintf(stderr, "ctdldump: db_create: %s\n", db_strerror(ret));
378                 fprintf(stderr, "ctdldump: exit code %d\n", ret);
379                 exit(CTDLEXIT_DB);
380         }
381
382         // open the file containing the source table
383         ret = src_dbp->open(src_dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
384         if (ret) {
385                 fprintf(stderr, "ctdldump: db_open: %s\n", db_strerror(ret));
386                 fprintf(stderr, "ctdldump: exit code %d\n", ret);
387                 exit(CTDLEXIT_DB);
388         }
389
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);
394                 exit(CTDLEXIT_DB);
395         }
396
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)
401
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) {
404         
405                 // If either the key or data are zero length, skip this record
406                 if ((in_key.size == 0) || (in_data.size == 0)) {
407                         ++num_bad_rows;
408                 }
409
410                 else {  // Both key and data are >0 length so we're good to go
411
412                         // Do we need to decompress?
413                         static int32_t magic = COMPRESS_MAGIC;
414                         compressed = 0;
415                         if ((in_data.size >= sizeof(struct CtdlCompressHeader)) && (!memcmp(in_data.data, &magic, sizeof(magic)))) {
416         
417                                 // yes, we need to decompress
418                                 compressed = 1;
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;
424         
425                                 ret = uncompress((Bytef *)uncomp_data.data, (uLongf *)&destLen,
426                                                 (const Bytef *)in_data.data+sizeof(struct CtdlCompressHeader),
427                                                 (uLong)comp.compressed_len);
428                                 if (ret != Z_OK) {
429                                         fprintf(stderr, "ctdldump: uncompress() error %d\n", ret);
430                                         exit(CTDLEXIT_DB);
431                                 }
432                         }
433         
434                         // Call the export function registered to this table
435                         export_functions[which_cdb](which_cdb, &in_key, (compressed ? &uncomp_data : &in_data));
436         
437                         // Knowing the total number of rows isn't critical to the program.  It's just for the user to know.
438                         fflush(stdout);
439                 }
440         }
441
442         // free any leftover out_data pointers
443         free(uncomp_data.data);
444
445         // ...and close the database (table)
446         ret = src_dbp->close(src_dbp, 0);
447         if (ret) {
448                 fprintf(stderr, "ctdldump: db_close: %s\n", db_strerror(ret));
449         }
450
451
452 }
453
454
455 int main(int argc, char **argv) {
456         int i = 0;
457         char *src_dir = NULL;
458         char *dst_dir = NULL;
459         int confirmed = 0;
460         static DB_ENV *src_dbenv;               // Source DB environment (global)
461
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");
469
470         // Parse command line
471         int a;
472         while ((a = getopt(argc, argv, "h:y")) != EOF) {
473                 switch (a) {
474                 case 'h':
475                         src_dir = optarg;
476                         break;
477                 case 'y':
478                         confirmed = 1;
479                         break;
480                 default:
481                         fprintf(stderr, "%s: usage: %s -s source_dir [>dumpfile]\n", argv[0], argv[0]);
482                         exit(2);
483                 }
484         }
485
486         if (confirmed == 1) {
487                 fprintf(stderr, "ctdldump: You have specified the [-y] flag, so processing will continue.\n");
488         }
489         else {
490                 fprintf(stderr, "ctdldump: Please read [ https://www.citadel.org/dump-and-load.html ] to learn how to proceed.\n");
491                 exit(0);
492         }
493
494         src_dbenv = open_dbenv(src_dir);
495         printf("begin|\n");
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
501                 }
502         }
503         close_dbenv(src_dbenv);
504         printf("end|\n");
505
506         exit(0);
507 }