]> code.citadel.org Git - citadel.git/blob - citadel/utils/ctdldump.c
Wrote the first of 14 export functions (msgmain)
[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 // convert 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         if (in_key->size != sizeof(long)) {
146                 fprintf(stderr, "\033[31m\033[1m *** BAD DATA *** ABORTING *** \033[0m\n");
147                 abort();
148         }
149
150         memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
151
152         // If the msgnum is negative, we are looking at METADATA
153         if (in_msgnum < 0) {
154                 struct MetaData *meta = (struct MetaData *)in_data->data;
155                 printf("msgmeta|%ld|%d|%s|%ld\n", meta->meta_msgnum, meta->meta_refcount, meta->meta_content_type, meta->meta_rfc822_length);
156         }
157
158         // If the msgnum is positive, we are looking at a MESSAGE
159         else if (in_msgnum > 0) {
160                 printf("msgtext|%s\n", 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 #if 0
167
168 // convert function for a user record
169 void export_users(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
170
171         // The key is a string so we can just copy it over
172         out_key->size = in_key->size;
173         out_key->data = realloc(out_key->data, out_key->size);
174         memcpy(out_key->data, in_key->data, in_key->size);
175
176         struct ctdluser_32 *user32 = (struct ctdluser_32 *)in_data->data;
177
178         out_data->size = sizeof(struct ctdluser);
179         out_data->data = realloc(out_data->data, out_data->size);
180         struct ctdluser *user64 = (struct ctdluser *)out_data->data;
181
182         user64->version                 = (int)         user32->version;
183         user64->uid                     = (uid_t)       user32->uid;
184         strcpy(user64->password,                        user32->password);
185         user64->flags                   = (unsigned)    user32->flags;
186         user64->axlevel                 = (cit_uint8_t) user32->axlevel;
187         user64->usernum                 = (long)        user32->usernum;
188         user64->lastcall                = (time_t)      user32->lastcall;
189         user64->USuserpurge             = (int)         user32->USuserpurge;
190         strcpy(user64->fullname,                        user32->fullname);
191         user64->msgnum_bio              = (long)        user32->msgnum_bio;
192         user64->msgnum_pic              = (long)        user32->msgnum_pic;
193         strcpy(user64->emailaddrs,                      user32->emailaddrs);
194         user64->msgnum_inboxrules       = (long)        user32->msgnum_inboxrules;
195         user64->lastproc_inboxrules     = (long)        user32->lastproc_inboxrules;
196
197         // printf("\033[32m\033[1mUser: %s\033[0m\n", user64->fullname);
198 }
199
200
201 // convert function for a room record
202 void export_rooms(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
203
204         // The key is a string so we can just copy it over
205         out_key->size = in_key->size;
206         out_key->data = realloc(out_key->data, out_key->size);
207         memcpy(out_key->data, in_key->data, in_key->size);
208
209         // data
210         struct ctdlroom_32 *room32 = (struct ctdlroom_32 *)in_data->data;
211         out_data->size = sizeof(struct ctdlroom);
212         out_data->data = realloc(out_data->data, out_data->size);
213         struct ctdlroom *room64 = (struct ctdlroom *)out_data->data;
214
215         strcpy(room64->QRname,                          room32->QRname);
216         strcpy(room64->QRpasswd,                        room32->QRpasswd);
217         room64->QRroomaide              = (long)        room32->QRroomaide;
218         room64->QRhighest               = (long)        room32->QRhighest;
219         room64->QRgen                   = (time_t)      room32->QRgen;
220         room64->QRflags                 = (unsigned)    room32->QRflags;
221         strcpy(room64->QRdirname,                       room32->QRdirname);
222         room64->msgnum_info             = (long)        room32->msgnum_info;
223         room64->QRfloor                 = (char)        room32->QRfloor;
224         room64->QRmtime                 = (time_t)      room32->QRmtime;
225         room64->QRep.expire_mode        = (int)         room32->QRep.expire_mode;
226         room64->QRep.expire_value       = (int)         room32->QRep.expire_value;
227         room64->QRnumber                = (long)        room32->QRnumber;
228         room64->QRorder                 = (char)        room32->QRorder;
229         room64->QRflags2                = (unsigned)    room32->QRflags2;
230         room64->QRdefaultview           = (int)         room32->QRdefaultview;
231         room64->msgnum_pic              = (long)        room32->msgnum_pic;
232
233         // printf("\033[32m\033[1mRoom: %s\033[0m\n", room64->QRname);
234 }
235
236
237 // convert function for a floor record
238 void export_floors(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
239
240         // the key is an "int", and "int" is 32-bits on both 32 and 64 bit platforms.
241         out_key->size = in_key->size;
242         out_key->data = realloc(out_key->data, out_key->size);
243         memcpy(out_key->data, in_key->data, in_key->size);
244
245         // data
246         struct floor_32 *floor32 = (struct floor_32 *)in_data->data;
247         out_data->size = sizeof(struct floor);
248         out_data->data = realloc(out_data->data, out_data->size);
249         struct floor *floor64 = (struct floor *)out_data->data;
250
251         // these are probably bit-for-bit identical, actually ... but we do it the "right" way anyway
252         floor64->f_flags                = (unsigned short)      floor32->f_flags;
253         strcpy(floor64->f_name,                                 floor32->f_name);
254         floor64->f_ref_count            = (int)                 floor32->f_ref_count;
255         floor64->f_ep.expire_mode       = (int)                 floor32->f_ep.expire_mode;
256         floor64->f_ep.expire_value      = (int)                 floor32->f_ep.expire_value;
257
258         // printf("\033[32m\033[1mFloor: %s\033[0m\n", floor64->f_name);
259 }
260
261
262 // convert function for a msglist or a fulltext index record
263 // (both are indexed by a long and the data is arrays of longs)
264 void export_msglists(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
265         int i;
266
267         char *table = (which_cdb == CDB_FULLTEXT) ? "FullText" : "Msglist";
268
269         // records are indexed by a single "long" and contains an array of zero or more "long"s
270         // and remember ... "long" is int32_t on the source system
271         int32_t in_roomnum;
272         long out_roomnum;
273         memcpy(&in_roomnum, in_key->data, sizeof(in_roomnum));
274         out_roomnum = (long) in_roomnum;
275
276         if (in_key->size != 4) {
277                 fprintf(stderr, "\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
278                 abort();
279         }
280
281         int num_msgs = in_data->size / sizeof(int32_t);
282         // printf("\033[32m\033[1m%s: key %ld (%d messages)\033[0m\n", table, out_roomnum, num_msgs);
283
284         // the key is a "long"
285         out_key->size = sizeof(out_roomnum);
286         out_key->data = realloc(out_key->data, out_key->size);
287         memcpy(out_key->data, &out_roomnum, sizeof(out_roomnum));
288
289         // the data is another array, but a wider type
290         out_data->size = sizeof(long) * num_msgs;
291         out_data->data = realloc(out_data->data, out_data->size);
292
293         int32_t in_msg = 0;
294         long out_msg = 0;
295         for (i=0; i<num_msgs; ++i) {
296                 memcpy(&in_msg, (in_data->data + (i * sizeof(int32_t))), sizeof(int32_t));
297                 out_msg = (long) in_msg;
298                 memcpy((out_data->data + (i * sizeof(long))), &out_msg, sizeof(long));
299                 // printf("msg#%ld\n", out_msg);
300         }
301 }
302
303
304 // convert function for a visit record
305 void export_visits(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
306
307         // data
308         struct visit_32 *visit32 = (struct visit_32 *)in_data->data;
309         out_data->size = sizeof(struct visit);
310         out_data->data = realloc(out_data->data, out_data->size);
311         struct visit *visit64 = (struct visit *)out_data->data;
312
313         //  the data (zero it out so it will compress well)
314         memset(visit64, 0, sizeof(struct visit));
315         visit64->v_roomnum              = (long)        visit32->v_roomnum;
316         visit64->v_roomgen              = (long)        visit32->v_roomgen;
317         visit64->v_usernum              = (long)        visit32->v_usernum;
318         visit64->v_lastseen             = (long)        visit32->v_lastseen;
319         visit64->v_flags                = (unsigned)    visit32->v_flags;
320         strcpy(visit64->v_seen,                         visit32->v_seen);
321         strcpy(visit64->v_answered,                     visit32->v_answered);
322         visit64->v_view                 = (int)         visit32->v_view;
323
324         // printf("\033[32m\033[1mVisit: room %10ld, gen %10ld, user %10ld\033[0m\n", visit64->v_roomnum, visit64->v_roomgen, visit64->v_usernum);
325
326         // create the key (which is based on the data, so there is no need to convert the old key)
327         out_key->size = sizeof(struct visit_index);
328         out_key->data = realloc(out_key->data, out_key->size);
329         struct visit_index *newvisitindex = (struct visit_index *) out_key->data;
330         newvisitindex->iRoomID          =               visit64->v_roomnum;
331         newvisitindex->iRoomGen         =               visit64->v_roomgen;
332         newvisitindex->iUserID          =               visit64->v_usernum;
333 }
334
335
336 // convert function for a directory record
337 void export_dir(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
338
339         // the key is a string
340         out_key->size = in_key->size;
341         out_key->data = realloc(out_key->data, out_key->size + 1);
342         memcpy(out_key->data, in_key->data, in_key->size);
343         char *k = (char *)out_key->data;
344         k[out_key->size] = 0;
345
346         // the data is also a string
347         out_data->size = in_data->size;
348         out_data->data = realloc(out_data->data, out_data->size + 1);
349         memcpy(out_data->data, in_data->data, in_data->size);
350         char *d = (char *)out_data->data;
351         d[out_data->size] = 0;
352
353         // please excuse my friend, he isn't null terminated
354         // printf("\033[32m\033[1mDirectory entry: %s -> %s\033[0m\n", (char *)out_key->data, (char *)out_data->data);
355 }
356
357
358 // convert function for a use table record
359 void export_usetable(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
360
361         // the key is an int, which is the same size (32 bits) on both 32 and 64 bit systems
362         out_key->size = in_key->size;
363         out_key->data = realloc(out_key->data, out_key->size);
364         memcpy(out_key->data, in_key->data, in_key->size);
365
366         // the data is a "struct UseTable"
367         struct UseTable_32 *use32 = (struct UseTable_32 *)in_data->data;
368         out_data->size = sizeof(struct UseTable);
369         out_data->data = realloc(out_data->data, out_data->size);
370         memset(out_data->data, 0, out_data->size);
371         struct UseTable *use64 = (struct UseTable *)out_data->data;
372
373         //  the data
374         use64->hash                     =               use32->hash;
375         use64->timestamp                = (time_t)      use32->timestamp;
376
377         // printf("\033[32m\033[1muse table: %d , %s\033[0m\n", use64->hash, asctime(localtime(&use64->timestamp)));
378 }
379
380
381 // convert function for large message texts
382 void export_bigmsgs(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
383
384         // The key is a packed long
385         int32_t in_msgnum;
386         long out_msgnum;
387         memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
388         out_msgnum = (long)in_msgnum;
389
390         if (in_key->size != 4) {
391                 fprintf(stderr, "\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
392                 abort();
393         }
394
395         out_key->size = sizeof(long);
396         out_key->data = realloc(out_key->data, out_key->size);
397         memcpy(out_key->data, &out_msgnum, sizeof(long));
398
399         // the data is binary-ish but has no packed integers
400         out_data->size = in_data->size;
401         out_data->data = realloc(out_data->data, out_data->size);
402         memcpy(out_data->data, in_data->data, in_data->size);
403
404         // printf("\033[32m\033[1mBigmsg %ld , length %d\033[0m\n", out_msgnum, out_data->size);
405 }
406
407
408 // convert function for EUID Index records
409 void export_euidindex(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
410
411         // The structure of an euidindex record *key* is:
412         // |----room_number----|----------EUID-------------|
413         //    (sizeof long)       (actual length of euid)
414
415         // The structure of an euidindex record *value* is:
416         // |-----msg_number----|----room_number----|----------EUID-------------|
417         //    (sizeof long)       (sizeof long)       (actual length of euid)
418
419         int32_t in_msgnum = 0;
420         int32_t in_roomnum = 0;
421         char euid[SIZ];
422         long out_msgnum = 0;
423         long out_roomnum = 0;
424
425         memcpy(&in_msgnum, in_data->data, sizeof(in_msgnum));
426         memcpy(&in_roomnum, in_data->data+sizeof(int32_t), sizeof(in_msgnum));
427         strcpy(euid, in_data->data+(sizeof(int32_t)*2));
428
429         out_msgnum = (long) in_msgnum;
430         out_roomnum = (long) in_roomnum;
431         // printf("euidindex: msgnum=%ld, roomnum=%ld, euid=\"%s\"\n", out_msgnum, out_roomnum, euid);
432
433         out_key->size = sizeof(long) + strlen(euid) + 1;
434         out_key->data = realloc(out_key->data, out_key->size);
435         memcpy(out_key->data, &out_roomnum, sizeof(out_roomnum));
436         strcpy(out_key->data+sizeof(out_roomnum), euid);
437
438         out_data->size = sizeof(long) + sizeof(long) + strlen(euid) + 1;
439         out_data->data = realloc(out_data->data, out_data->size);
440         memcpy(out_data->data, &out_msgnum, sizeof(out_msgnum));
441         memcpy(out_data->data+sizeof(out_msgnum), &out_roomnum, sizeof(out_roomnum));
442         strcpy(out_data->data+sizeof(out_msgnum)+sizeof(out_roomnum), euid);
443 }
444
445
446 // convert users-by-number records
447 void export_usersbynumber(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
448
449         // key is a long
450         // and remember ... "long" is int32_t on the source system
451         int32_t in_usernum;
452         long out_usernum;
453         memcpy(&in_usernum, in_key->data, sizeof(in_usernum));
454         out_usernum = (long) in_usernum;
455
456         if (in_key->size != 4) {
457                 fprintf(stderr, "\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
458                 abort();
459         }
460
461         out_key->size = sizeof(out_usernum);
462         out_key->data = realloc(out_key->data, out_key->size);
463         memcpy(out_key->data, &out_usernum, sizeof(out_usernum));
464
465         // value is a string
466         out_data->size = in_data->size;
467         out_data->data = realloc(out_data->data, out_data->size);
468         memcpy(out_data->data, in_data->data, in_data->size);
469
470         // printf("usersbynumber: %ld --> %s\n", out_usernum, (char *)out_data->data);
471 }
472
473
474 // convert function for a config record
475 void export_config(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
476
477         // the key is a string
478         out_key->size = in_key->size;
479         out_key->data = realloc(out_key->data, out_key->size + 1);
480         memcpy(out_key->data, in_key->data, in_key->size);
481         char *k = (char *)out_key->data;
482         k[out_key->size] = 0;
483
484         // the data is a pair of strings
485         out_data->size = in_data->size;
486         out_data->data = realloc(out_data->data, out_data->size + 1);
487         memcpy(out_data->data, in_data->data, in_data->size);
488         char *d = (char *)out_data->data;
489         d[out_data->size] = 0;
490
491         // please excuse my friend, he isn't null terminated
492         // printf("\033[32m\033[1mConfig entry: %s -> %s\033[0m\n", (char *)out_key->data, (char *)out_data->data+strlen(out_data->data)+1);
493 }
494
495 #endif
496
497 // For obsolete databases, zero all the output
498 void zero_function(int which_cdb, DBT *in_key, DBT *in_data) {
499         //printf("Table %02x, keylen=%d, datalen=%d\n", which_cdb, in_key->size, in_data->size);
500 }
501
502
503 void (*export_functions[])(int which_cdb, DBT *in_key, DBT *in_data) = {
504         export_msgmain,         // CDB_MSGMAIN
505         zero_function,          // CDB_USERS
506         zero_function,          // CDB_ROOMS
507         zero_function,          // CDB_FLOORTAB
508         zero_function,          // CDB_MSGLISTS
509         zero_function,          // CDB_VISIT
510         zero_function,          // CDB_DIRECTORY
511         zero_function,          // CDB_USETABLE
512         zero_function,          // CDB_BIGMSGS
513         zero_function,          // CDB_FULLTEXT
514         zero_function,          // CDB_EUIDINDEX
515         zero_function,          // CDB_USERSBYNUMBER
516         zero_function,          // CDB_UNUSED1 (obsolete)
517         zero_function           // CDB_CONFIG
518 };
519
520
521 void export_table(int which_cdb, DB_ENV *src_dbenv) {
522         int ret;
523         int compressed;
524         char dbfilename[32];
525         uLongf destLen = 0;
526
527         // shamelessly swiped from https://docs.oracle.com/database/bdb181/html/programmer_reference/am_cursor.html
528         DB *src_dbp;
529         DBC *src_dbcp;
530         DBT in_key, in_data, uncomp_data;
531         int num_good_rows = 0;
532         int num_bad_rows = 0;
533
534         snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", which_cdb);
535
536         // create a database handle for the source table
537         ret = db_create(&src_dbp, src_dbenv, 0);
538         if (ret) {
539                 fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
540                 fprintf(stderr, "db: exit code %d\n", ret);
541                 exit(CTDLEXIT_DB);
542         }
543
544         // open the file containing the source table
545         ret = src_dbp->open(src_dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
546         if (ret) {
547                 fprintf(stderr, "db: db_open: %s\n", db_strerror(ret));
548                 fprintf(stderr, "db: exit code %d\n", ret);
549                 exit(CTDLEXIT_DB);
550         }
551
552         // Acquire a cursor to read the source table
553         if ((ret = src_dbp->cursor(src_dbp, NULL, &src_dbcp, 0)) != 0) {
554                 fprintf(stderr, "db: db_cursor: %s\n", db_strerror(ret));
555                 fprintf(stderr, "db: exit code %d\n", ret);
556                 exit(CTDLEXIT_DB);
557         }
558
559         // Zero out these database keys
560         memset(&in_key,         0, sizeof(DBT));        // input
561         memset(&in_data,        0, sizeof(DBT));
562         memset(&uncomp_data,    0, sizeof(DBT));        // decompressed input (the key doesn't change)
563
564         // Walk through the database, calling convert functions as we go and clearing buffers before each call.
565         while (ret = src_dbcp->get(src_dbcp, &in_key, &in_data, DB_NEXT) == 0) {
566         
567                 // If either the key or data are zero length, skip this record
568                 if ((in_key.size == 0) || (in_data.size == 0)) {
569                         ++num_bad_rows;
570                 }
571
572                 else {  // Both key and data are >0 length so we're good to go
573
574                         // Do we need to decompress?
575                         static int32_t magic = COMPRESS_MAGIC;
576                         compressed = 0;
577                         if ( (in_data.size >= sizeof(struct CtdlCompressHeader)) && (!memcmp(in_data.data, &magic, sizeof(magic))) ) {
578         
579                                 // yes, we need to decompress
580                                 compressed = 1;
581                                 struct CtdlCompressHeader comp;
582                                 memcpy(&comp, in_data.data, sizeof(struct CtdlCompressHeader));
583                                 uncomp_data.size = comp.uncompressed_len;
584                                 uncomp_data.data = realloc(uncomp_data.data, uncomp_data.size);
585                                 destLen = (uLongf)comp.uncompressed_len;
586         
587                                 ret = uncompress((Bytef *)uncomp_data.data, (uLongf *)&destLen, (const Bytef *)in_data.data+sizeof(struct CtdlCompressHeader), (uLong)comp.compressed_len);
588                                 if (ret != Z_OK) {
589                                         fprintf(stderr, "db: uncompress() error %d\n", ret);
590                                         exit(CTDLEXIT_DB);
591                                 }
592                         }
593         
594                         // Call the convert function registered to this table
595                         export_functions[which_cdb](which_cdb, &in_key, (compressed ? &uncomp_data : &in_data));
596         
597                         // Knowing the total number of rows isn't critical to the program.  It's just for the user to know.
598                         fflush(stdout);
599                 }
600         }
601
602         // free any leftover out_data pointers
603         free(uncomp_data.data);
604
605         // ...and close the database (table)
606         ret = src_dbp->close(src_dbp, 0);
607         if (ret) {
608                 fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
609         }
610
611
612 }
613
614
615 int main(int argc, char **argv) {
616         int i = 0;
617         char *src_dir = NULL;
618         char *dst_dir = NULL;
619         int confirmed = 0;
620         static DB_ENV *src_dbenv;               // Source DB environment (global)
621
622         // Parse command line
623         int a;
624         while ((a = getopt(argc, argv, "h:d:y")) != EOF) {
625                 switch (a) {
626                 case 'h':
627                         src_dir = optarg;
628                         break;
629                 case 'd':
630                         dst_dir = optarg;
631                         break;
632                 case 'y':
633                         confirmed = 1;
634                         break;
635                 default:
636                         fprintf(stderr, "%s: usage: %s -s source_dir -d dest_dir\n", argv[0], argv[0]);
637                         exit(2);
638                 }
639         }
640
641         // Warn the user
642         fprintf(stderr, "------------------------------------------------------------------------\n");
643         fprintf(stderr, "This utility must be run while the server is OFFLINE.                   \n");
644         fprintf(stderr, "We \033[1mguarantee\033[0m data corruption if you do not                              \n");
645         fprintf(stderr, "observe this warning!  The source [-s] directory should contain a copy  \n");
646         fprintf(stderr, "of the database from your source system.  The dump [-d] directory       \n");
647         fprintf(stderr, "should be empty and will receive your dump file.                        \n");
648         fprintf(stderr, "------------------------------------------------------------------------\n");
649         fprintf(stderr, " Source (database) directory: %s\n", src_dir);
650         fprintf(stderr, "------------------------------------------------------------------------\n");
651
652         if (confirmed == 1) {
653                 fprintf(stderr, "You have specified the [-y] flag, so processing will continue.\n");
654         }
655         else {
656                 fprintf(stderr, "Please read [ https://www.citadel.org/ctdldump.html ] to learn how to proceed.\n");
657                 exit(0);
658         }
659
660         src_dbenv = open_dbenv(src_dir);
661         for (i = 0; i < MAXCDB; ++i) {
662                 export_table(i, src_dbenv);
663         }
664         close_dbenv(src_dbenv);
665
666         exit(0);
667 }