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