aac23fcc8767c0d5b5207b46a230c65720322ec6
[citadel.git] / citadel / utils / ctdldump.c
1 // Dump the Citadel database to a flat file that can be restored by ctdlload on any architecture
2 //
3 // Copyright (c) 2023-2024 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 <syslog.h>
23 #include <libcitadel.h>
24 #include <zlib.h>
25 #include "../server/sysdep.h"
26 #include "../server/citadel_defs.h"
27 #include "../server/server.h"
28 #include "../server/citadel_dirs.h"
29 #include "../server/database.h"
30
31 uid_t ctdluid = 0;
32
33 // Wrapper for realloc() that crashes and burns if the call fails.
34 void *reallok(void *ptr, size_t size) {
35         void *p = realloc(ptr, size);
36         if (!p) {
37                 fprintf(stderr, "realloc() failed to resize %p to %ld bytes, error: %m\n", ptr, size);
38                 abort();
39         }
40         return p;
41 }
42
43
44 // convert a binary blob to base64 (non-reentrant!)
45 char *b64out(void *data, size_t len) {
46         static char *outbuf = NULL;
47         static size_t outlen = 0;
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 // export function for a message in msgmain
60 void export_msgmain(int which_cdb, struct cdbkeyval kv) {
61         long in_msgnum;
62
63         memcpy(&in_msgnum, kv.key.ptr, sizeof(in_msgnum));
64
65         // If the msgnum is negative, we are looking at METADATA
66         if (in_msgnum < 0) {
67                 struct MetaData *meta = (struct MetaData *)kv.val.ptr;
68                 printf("msgmeta|%ld|%d|%s|%ld|\n",
69                         meta->meta_msgnum,
70                         meta->meta_refcount,
71                         meta->meta_content_type,
72                         meta->meta_rfc822_length
73                 );
74         }
75
76         // If the msgnum is positive, we are looking at a MESSAGE
77         else if (in_msgnum > 0) {
78                 printf("msgtext|%ld|%s|\n", in_msgnum, b64out(kv.val.ptr, kv.val.len));
79         }
80
81         // If the msgnum is 0 it's probably not a valid record.
82 }
83
84
85 // export function for a user record
86 void export_user(int which_cdb, struct cdbkeyval kv) {
87
88         struct ctdluser *user = (struct ctdluser *)kv.val.ptr;
89
90         printf("user|%d|%d|%s|%u|%d|%ld|%ld|%d|%s|%ld|%ld|%s|%ld|%ld|\n",
91                 user->version,
92                 user->uid,
93                 user->password,
94                 user->flags,
95                 user->axlevel,
96                 user->usernum,
97                 user->lastcall,
98                 user->USuserpurge,
99                 user->fullname,
100                 user->msgnum_bio,
101                 user->msgnum_pic,
102                 b64out(user->emailaddrs, strlen(user->emailaddrs)),
103                 user->msgnum_inboxrules,
104                 user->lastproc_inboxrules
105         );
106 }
107
108
109 // export function for a room record
110 void export_room(int which_cdb, struct cdbkeyval kv) {
111
112         struct ctdlroom *room = (struct ctdlroom *)kv.val.ptr;
113
114         printf("room|%s|%s|%ld|%ld|%ld|%u|%s|%ld|%d|%ld|%d|%d|%ld|%d|%u|%d|%ld|\n",
115                 room->QRname,
116                 room->QRpasswd,
117                 room->QRroomaide,
118                 room->QRhighest,
119                 room->QRgen,
120                 room->QRflags,
121                 room->QRdirname,
122                 room->msgnum_info,
123                 room->QRfloor,
124                 room->QRmtime,
125                 room->QRep.expire_mode,
126                 room->QRep.expire_value,
127                 room->QRnumber,
128                 room->QRorder,
129                 room->QRflags2,
130                 room->QRdefaultview,
131                 room->msgnum_pic
132         );
133 }
134
135
136 // export function for a floor record
137 void export_floor(int which_cdb, struct cdbkeyval kv) {
138
139         int floor_num;
140         memcpy(&floor_num, kv.key.ptr, sizeof(int));
141
142         struct floor *floor = (struct floor *)kv.val.ptr;
143
144         printf("floor|%d|%u|%s|%d|%d|%d|\n",
145                 floor_num,
146                 floor->f_flags,
147                 floor->f_name,
148                 floor->f_ref_count,
149                 floor->f_ep.expire_mode,
150                 floor->f_ep.expire_value
151         );
152 }
153
154
155 // export function for a msglist
156 // (indexed by a long and the data is arrays of longs)
157 void export_msglist(int which_cdb, struct cdbkeyval kv) {
158         int i;
159         int num_msgs;
160         long msg;
161
162         // records are indexed by a single "long" and contains an array of zero or more "long"s
163         long roomnum;
164         memcpy(&roomnum, kv.key.ptr, sizeof(long));
165
166         printf("msglist|%ld|", roomnum);
167
168         if (kv.val.len > 0) {
169                 num_msgs = kv.val.len / sizeof(long);
170                 for (i=0; i<num_msgs; ++i) {
171                         memcpy(&msg, (kv.val.ptr + (i * sizeof(long))), sizeof(long));
172                         if (i != 0) {
173                                 printf(",");
174                         }
175                         printf("%ld", msg);
176                 }
177         }
178         printf("|\n");
179 }
180
181
182 // export function for a full text search index record
183 // (indexed by an int and the data is arrays of longs)
184 void export_fulltext(int which_cdb, struct cdbkeyval kv) {
185         int i;
186         int num_msgs;
187         long msg;
188
189         // records are indexed by a single "int" and contains an array of zero or more "long"s
190         int indexnum;
191         memcpy(&indexnum, kv.key.ptr, sizeof(int));
192
193         printf("fulltext|%d|", indexnum);
194
195         if (kv.val.len > 0) {
196                 num_msgs = kv.val.len / sizeof(long);
197                 for (i=0; i<num_msgs; ++i) {
198                         memcpy(&msg, (kv.val.ptr + (i * sizeof(long))), sizeof(long));
199                         if (i != 0) {
200                                 printf(",");
201                         }
202                         printf("%ld", msg);
203                 }
204         }
205         printf("|\n");
206 }
207
208
209 // export function for a visit record
210 void export_visit(int which_cdb, struct cdbkeyval kv) {
211         struct visit *visit = (struct visit *)kv.val.ptr;
212         int i, len;
213
214         // If there is corrupt data in the "seen" array, cut that out before exporting
215         len = strlen(visit->v_seen);
216         for (i=0; i<len; ++i) {
217                 if (!isprint(visit->v_seen[i])) {
218                         visit->v_seen[i] = 0;
219                 }
220         }
221
222         // If there is corrupt data in the "answered" array, cut that out before exporting
223         len = strlen(visit->v_answered);
224         for (i=0; i<len; ++i) {
225                 if (!isprint(visit->v_answered[i])) {
226                         visit->v_answered[i] = 0;
227                 }
228         }
229
230         // output the record
231         printf("visit|%ld|%ld|%ld|%ld|%u|%s|%s|%d|\n",
232                 visit->v_roomnum,
233                 visit->v_roomgen,
234                 visit->v_usernum,
235                 visit->v_lastseen,
236                 visit->v_flags,
237                 visit->v_seen,
238                 visit->v_answered,
239                 visit->v_view
240         );
241 }
242
243
244 // export function for a directory record
245 void export_dir(int which_cdb, struct cdbkeyval kv) {
246         printf("dir|");
247         fwrite(kv.key.ptr, kv.key.len, 1, stdout);
248         printf("|%s|\n", (char *)kv.val.ptr);
249 }
250
251
252 // export function for a use table record
253 void export_usetable(int which_cdb, struct cdbkeyval kv) {
254         struct UseTable *u = (struct UseTable *)kv.val.ptr;
255         printf("use|%d|%ld|\n", u->hash, u->timestamp);
256 }
257
258
259 // export function for large message texts
260 void export_bigmsg(int which_cdb, struct cdbkeyval kv) {
261         long msgnum;
262
263         memcpy(&msgnum, kv.key.ptr, sizeof(msgnum));
264         printf("bigmsg|%ld|%s|\n", msgnum, b64out(kv.val.ptr, kv.val.len));
265 }
266
267
268 // export function for EUID Index records
269 void export_euidindex(int which_cdb, struct cdbkeyval kv) {
270
271         // The structure of an euidindex record *key* is:
272         // |----room_number----|----------EUID-------------|
273         //    (sizeof long)       (actual length of euid)
274
275         // The structure of an euidindex record *value* is:
276         // |-----msg_number----|----room_number----|----------EUID-------------|
277         //    (sizeof long)       (sizeof long)       (actual length of euid)
278
279         long msgnum, roomnum;
280         char *euid;
281
282         memcpy(&msgnum, kv.val.ptr, sizeof(long));
283         memcpy(&roomnum, kv.val.ptr+sizeof(long), sizeof(msgnum));
284         euid = kv.val.ptr+(sizeof(long)*2);
285
286         printf("euidindex|%ld|%ld|%s|\n", msgnum, roomnum, euid);
287 }
288
289
290 // export users-by-number records
291 // (This is a secondary index -- should we just regenerate the data after import?)
292 void export_usersbynumber(int which_cdb, struct cdbkeyval kv) {
293
294         // key is a long
295         long usernum;
296         memcpy(&usernum, kv.key.ptr, sizeof(usernum));
297
298         // value is a string
299         printf("usersbynumber|%ld|%s|\n", usernum, (char *)kv.val.ptr);
300 }
301
302
303 // export function for a config record
304 void export_config(int which_cdb, struct cdbkeyval kv) {
305
306         printf("config|%s|%s|\n",
307                 (char *)kv.val.ptr,
308                 (char *)kv.val.ptr + strlen(kv.val.ptr) + 1
309         );
310
311 }
312
313
314 // For obsolete databases, zero all the output
315 void zero_function(int which_cdb, struct cdbkeyval kv) {
316         // do nothing
317 }
318
319
320 void (*export_functions[])(int which_cdb, struct cdbkeyval kv) = {
321         export_msgmain,         // 00 CDB_MSGMAIN       
322         export_user,            // 01 CDB_USERS
323         export_room,            // 02 CDB_ROOMS
324         export_floor,           // 03 CDB_FLOORTAB
325         export_msglist,         // 04 CDB_MSGLISTS
326         export_visit,           // 05 CDB_VISIT
327         export_dir,             // 06 CDB_DIRECTORY
328         export_usetable,        // 07 CDB_USETABLE
329         export_bigmsg,          // 08 CDB_BIGMSGS
330         export_fulltext,        // 09 CDB_FULLTEXT
331         export_euidindex,       // 0a CDB_EUIDINDEX
332         export_usersbynumber,   // 0b CDB_USERSBYNUMBER
333         zero_function,          // 0c CDB_UNUSED1 (obsolete)
334         export_config           // 0d CDB_CONFIG
335 };
336
337
338 void export_table(int which_cdb) {
339         int ret;
340         struct cdbkeyval ckv;
341
342         int num_good_rows = 0;
343         int num_bad_rows = 0;
344
345         cdb_rewind(which_cdb);
346         while (ckv = cdb_next_item(which_cdb), ckv.val.ptr!=NULL) {             // always read through to the end
347                 // Call the export function registered to this table
348                 export_functions[which_cdb](which_cdb, ckv);
349         }
350
351         // Knowing the total number of rows isn't critical to the program.  It's just for the user to know.
352         fflush(stdout);
353 }
354
355
356 int main(int argc, char **argv) {
357         int i = 0;
358         int confirmed = 0;
359         char *ctdldir = CTDLDIR;
360
361         // display the greeting
362         fprintf(stderr, "\033[44m\033[1m"
363                 "╔════════════════════════════════════════════════════════════════════════╗\n"
364                 "║ DB Dump utility for Citadel                                            ║\n"
365                 "║ Copyright (c) 2023-2024 by citadel.org et al.                          ║\n"
366                 "║ This program is open source software.  Use, duplication, or disclosure ║\n"
367                 "║ is subject to the terms of the GNU General Public license v3.          ║\n"
368                 "╚════════════════════════════════════════════════════════════════════════╝\033[0m\n"
369         );
370
371         // Parse command line
372         int a;
373         while ((a = getopt(argc, argv, "h:y")) != EOF) {
374                 switch (a) {
375                 case 'h':
376                         ctdldir = optarg;
377                         break;
378                 case 'y':
379                         confirmed = 1;
380                         break;
381                 default:
382                         fprintf(stderr, "%s: usage: %s -s citadel_dir [>dumpfile]\n", argv[0], argv[0]);
383                         exit(2);
384                 }
385         }
386
387         if (confirmed == 1) {
388                 fprintf(stderr, "ctdldump: You have specified the [-y] flag, so processing will continue.\n");
389         }
390         else {
391                 fprintf(stderr, "ctdldump: usage: ctdldump -y -h[citadel_dir] >[dump_file]\n");
392                 fprintf(stderr, "          -y : yes, I know this program can do damage and I want to run it anyway.\n");
393                 fprintf(stderr, "          -h : [citadel_dir] is your server directory, usually /usr/local/citadel\n");
394                 fprintf(stderr, "          Please read [ https://www.citadel.org/dump-and-load.html ] to learn how to proceed.\n");
395                 exit(1);
396         }
397
398         if (chdir(ctdldir) != 0) {
399                 fprintf(stderr, "ctdlload: unable to change directory to [%s]: %m", ctdldir);
400                 exit(2);
401         }
402
403         // backend modules use syslog -- redirect to stderr
404         openlog("ctdldump", LOG_PERROR , LOG_DAEMON);
405
406         // initialize the database backend
407         cdb_init_backends();
408         cdb_open_databases();
409
410         printf("begin|\n");
411         for (i = 0; i < MAXCDB; ++i) {
412                 export_table(i);
413         }
414         printf("end|\n");
415         fflush(stdout);
416
417         // close databases
418         cdb_close_databases();
419
420         fprintf(stderr, "ctdldump: \033[32m\033[1mfinished\033[0m\n");
421         exit(0);
422 }