]> code.citadel.org Git - citadel.git/blob - citadel/utils/ctdl3264.c
detect compressed records
[citadel.git] / citadel / utils / ctdl3264.c
1 // Attempt to convert your database from 32-bit to 64-bit.
2 // Don't run this.  It doesn't work and if you try to run it you will immediately die.
3 //
4 // Copyright (c) 2023 by Art Cancro citadel.org
5 //
6 // This program is open source software.  Use, duplication, or disclosure
7 // is subject to the terms of the GNU General Public License, version 3.
8
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <ctype.h>
12 #include <stdio.h>
13 #include <signal.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <sys/un.h>
17 #include <netdb.h>
18 #include <string.h>
19 #include <pwd.h>
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <limits.h>
23 #include <libcitadel.h>
24 #include <zlib.h>
25 #include <db.h>
26 #include "../server/sysdep.h"
27 #include "../server/citadel_defs.h"
28 #include "../server/server.h"
29 #include "../server/citadel_dirs.h"
30 #include "ctdl3264_structs.h"
31
32 static DB_ENV *dbenv;           // The DB environment (global)
33
34 // Open the database environment
35 void open_dbenv(char *src_dir) {
36         int ret;
37         int i;
38         u_int32_t flags = 0;
39         int dbversion_major, dbversion_minor, dbversion_patch;
40
41         printf( "db: open_dbenv() starting\n"
42                 "db:    Linked zlib: %s\n"
43                 "db: Compiled libdb: %s\n"
44                 "db:   Linked libdb: %s\n",
45                 zlibVersion(),
46                 DB_VERSION_STRING,
47                 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch)
48         );
49
50         // Create synthetic integer version numbers and compare them.
51         // Never allow citserver to run with a libdb older then the one with which it was compiled.
52         int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
53         int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
54         if (compiled_db_version > linked_db_version) {
55                 printf( "db: ctdl3264 is running with a version of libdb older than the one with which it was compiled.\n"
56                         "db: This is an invalid configuration.  ctdl3264 will now exit to prevent data loss.");
57                 exit(CTDLEXIT_DB);
58         }
59
60         printf("db: Setting up DB environment\n");
61         ret = db_env_create(&dbenv, 0);
62         if (ret) {
63                 printf("db: db_env_create: %s\n", db_strerror(ret));
64                 printf("db: exit code %d\n", ret);
65                 exit(CTDLEXIT_DB);
66         }
67
68         // We want to specify the shared memory buffer pool cachesize, but everything else is the default.
69         ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
70         if (ret) {
71                 printf("db: set_cachesize: %s\n", db_strerror(ret));
72                 dbenv->close(dbenv, 0);
73                 printf("db: exit code %d\n", ret);
74                 exit(CTDLEXIT_DB);
75         }
76
77         if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
78                 printf("db: set_lk_detect: %s\n", db_strerror(ret));
79                 dbenv->close(dbenv, 0);
80                 printf("db: exit code %d\n", ret);
81                 exit(CTDLEXIT_DB);
82         }
83
84         flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
85         printf("db: dbenv open(dir=%s, flags=%d)\n", src_dir, flags);
86         ret = dbenv->open(dbenv, src_dir, flags, 0);
87         if (ret) {
88                 printf("db: dbenv->open: %s\n", db_strerror(ret));
89                 dbenv->close(dbenv, 0);
90                 printf("db: exit code %d\n", ret);
91                 exit(CTDLEXIT_DB);
92         }
93 }
94
95
96 void close_dbenv(void) {
97         printf("db: closing dbenv\n");
98         int ret = dbenv->close(dbenv, 0);
99         if (ret) {
100                 printf("db: DBENV->close: %s\n", db_strerror(ret));
101         }
102 }
103
104
105 // placeholder convert function for the data types not yet implemented
106 void null_function(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
107 }
108
109
110 // convert function for a message in msgmain
111 void convert_msgmain(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
112         int32_t in_msgnum;
113         long out_msgnum;
114         memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
115         out_msgnum = (long)in_msgnum;
116
117         if (in_key->size != 4) {
118                 printf("\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
119                 abort();
120         }
121
122         printf("Message %ld\n", out_msgnum);
123
124         // If the msgnum is negative, we are looking at METADATA
125         if (in_msgnum < 0) {
126                 struct MetaData_32 *meta32 = (struct MetaData_32 *)in_data->data;
127
128                 //printf("metadata: msgnum=%d , refcount=%d , content_type=\"%s\" , rfc822len=%d\n", meta32->meta_msgnum, meta32->meta_refcount, meta32->meta_content_type, meta32->meta_rfc822_length);
129
130                 out_key->size = sizeof(long);
131                 out_key->data = realloc(out_key->data, out_key->size);
132                 memcpy(out_key->data, &out_msgnum, sizeof(long));
133
134                 out_data->size = sizeof(struct MetaData);
135                 out_data->data = realloc(out_data->data, out_data->size);
136                 struct MetaData *meta64 = (struct MetaData *)out_data->data;
137                 memset(meta64, 0, sizeof(struct MetaData));
138                 meta64->meta_msgnum             = (long)        meta32->meta_msgnum;
139                 meta64->meta_refcount           = (int)         meta32->meta_refcount;
140                 strcpy(meta64->meta_content_type,               meta32->meta_content_type);
141                 meta64->meta_rfc822_length      = (long)        meta32->meta_rfc822_length;
142         }
143
144         // If the msgnum is positive, we are looking at a MESSAGE
145         else if (in_msgnum > 0) {
146                 out_key->size = sizeof(long);
147                 out_key->data = realloc(out_key->data, out_key->size);
148                 memcpy(out_key->data, &out_msgnum, sizeof(long));
149
150                 // copy the message verbatim
151                 out_data->size = in_data->size;
152                 out_data->data = realloc(out_data->data, out_data->size);
153                 memcpy(out_data->data, in_data->data, out_data->size);
154         }
155
156         // If the msgnum is 0 it's probably not a valid record.
157         else {
158                 printf("msgmain: record 0 is impossible\n");
159         }
160 }
161
162
163 // convert function for a user record
164 void convert_users(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
165
166         // The key is a string so we can just copy it over
167         out_key->size = in_key->size;
168         out_key->data = realloc(out_key->data, out_key->size);
169         memcpy(out_key->data, in_key->data, in_key->size);
170
171         struct ctdluser_32 *user32 = (struct ctdluser_32 *)in_data->data;
172
173         out_data->size = sizeof(struct ctdluser);
174         out_data->data = realloc(out_data->data, out_data->size);
175         struct ctdluser *user64 = (struct ctdluser *)out_data->data;
176
177         user64->version                 = (int)         user32->version;
178         user64->uid                     = (uid_t)       user32->uid;
179         strcpy(user64->password,                        user32->password);
180         user64->flags                   = (unsigned)    user32->flags;
181         user64->timescalled             = (long)        user32->timescalled;
182         user64->posted                  = (long)        user32->posted;
183         user64->axlevel                 = (cit_uint8_t) user32->axlevel;
184         user64->usernum                 = (long)        user32->usernum;
185         user64->lastcall                = (time_t)      user32->lastcall;
186         user64->USuserpurge             = (int)         user32->USuserpurge;
187         strcpy(user64->fullname,                        user32->fullname);
188         user64->msgnum_bio              = (long)        user32->msgnum_bio;
189         user64->msgnum_pic              = (long)        user32->msgnum_pic;
190         strcpy(user64->emailaddrs,                      user32->emailaddrs);
191         user64->msgnum_inboxrules       = (long)        user32->msgnum_inboxrules;
192         user64->lastproc_inboxrules     = (long)        user32->lastproc_inboxrules;
193
194         printf("User: %s\n", user64->fullname);
195 }
196
197
198 // convert function for a room record
199 void convert_rooms(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
200
201         // The key is a string so we can just copy it over
202         out_key->size = in_key->size;
203         out_key->data = realloc(out_key->data, out_key->size);
204         memcpy(out_key->data, in_key->data, in_key->size);
205
206         // data
207         struct ctdlroom_32 *room32 = (struct ctdlroom_32 *)in_data->data;
208         out_data->size = sizeof(struct ctdlroom);
209         out_data->data = realloc(out_data->data, out_data->size);
210         struct ctdlroom *room64 = (struct ctdlroom *)out_data->data;
211
212         strcpy(room64->QRname,                          room32->QRname);
213         strcpy(room64->QRpasswd,                        room32->QRpasswd);
214         room64->QRroomaide              = (long)        room32->QRroomaide;
215         room64->QRhighest               = (long)        room32->QRhighest;
216         room64->QRgen                   = (time_t)      room32->QRgen;
217         room64->QRflags                 = (unsigned)    room32->QRflags;
218         strcpy(room64->QRdirname,                       room32->QRdirname);
219         room64->msgnum_info             = (long)        room32->msgnum_info;
220         room64->QRfloor                 = (char)        room32->QRfloor;
221         room64->QRmtime                 = (time_t)      room32->QRmtime;
222         room64->QRep.expire_mode        = (int)         room32->QRep.expire_mode;
223         room64->QRep.expire_value       = (int)         room32->QRep.expire_value;
224         room64->QRnumber                = (long)        room32->QRnumber;
225         room64->QRorder                 = (char)        room32->QRorder;
226         room64->QRflags2                = (unsigned)    room32->QRflags2;
227         room64->QRdefaultview           = (int)         room32->QRdefaultview;
228         room64->msgnum_pic              = (long)        room32->msgnum_pic;
229
230         printf("Room: %s\n", room64->QRname);
231 }
232
233
234 // convert function for a floor record
235 void convert_floors(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
236
237         // the key is an "int", and "int" is 32-bits on both 32 and 64 bit platforms.
238         out_key->size = in_key->size;
239         out_key->data = realloc(out_key->data, out_key->size);
240         memcpy(out_key->data, in_key->data, in_key->size);
241
242         // data
243         struct floor_32 *floor32 = (struct floor_32 *)in_data->data;
244         out_data->size = sizeof(struct floor);
245         out_data->data = realloc(out_data->data, out_data->size);
246         struct floor *floor64 = (struct floor *)out_data->data;
247
248         // these are probably bit-for-bit identical, actually ... but we do it the "right" way anyway
249         floor64->f_flags                = (unsigned short)      floor32->f_flags;
250         strcpy(floor64->f_name,                                 floor32->f_name);
251         floor64->f_ref_count            = (int)                 floor32->f_ref_count;
252         floor64->f_ep.expire_mode       = (int)                 floor32->f_ep.expire_mode;
253         floor64->f_ep.expire_value      = (int)                 floor32->f_ep.expire_value;
254 }
255
256
257 // convert function for a msglist
258 void convert_msglists(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
259         int i;
260
261         // msglist records are indexed by a single "long" and contains an array of zero or more "long"s
262         // and remember ... "long" is int32_t on the source system
263         int32_t in_roomnum;
264         long out_roomnum;
265         memcpy(&in_roomnum, in_key->data, sizeof(in_roomnum));
266         out_roomnum = (long) in_roomnum;
267
268         if (in_key->size != 4) {
269                 printf("\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
270                 abort();
271         }
272
273         int num_msgs = in_data->size / sizeof(int32_t);
274         printf("msglist for room %ld (%d messages)\n", out_roomnum, num_msgs);
275
276         // the key is a "long"
277         out_key->size = sizeof(out_roomnum);
278         out_key->data = realloc(out_key->data, out_key->size);
279         memcpy(out_key->data, &out_roomnum, sizeof(out_roomnum));
280
281         // the data is another array, but a wider type
282         out_data->size = sizeof(long) * num_msgs;
283         out_data->data = realloc(out_data->data, out_data->size);
284
285         int32_t in_msg = 0;
286         long out_msg = 0;
287         for (i=0; i<num_msgs; ++i) {
288                 memcpy(&in_msg, (in_data->data + (i * sizeof(int32_t))), sizeof(int32_t));
289                 out_msg = (long) in_msg;
290                 memcpy((out_data->data + (i * sizeof(long))), &out_msg, sizeof(long));
291                 printf("#%ld\n", out_msg);
292         }
293 }
294
295
296 void (*convert_functions[])(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) = {
297         convert_msgmain,        // CDB_MSGMAIN
298         convert_users,          // CDB_USERS
299         convert_rooms,          // CDB_ROOMS
300         convert_floors,         // CDB_FLOORTAB
301         convert_msglists,       // CDB_MSGLISTS
302         null_function,          // CDB_VISIT
303         null_function,          // CDB_DIRECTORY
304         null_function,          // CDB_USETABLE
305         null_function,          // CDB_BIGMSGS
306         null_function,          // CDB_FULLTEXT
307         null_function,          // CDB_EUIDINDEX
308         null_function,          // CDB_USERSBYNUMBER
309         null_function,          // CDB_EXTAUTH
310         null_function           // CDB_CONFIG
311 };
312
313
314 void convert_table(int which_cdb) {
315         int ret;
316         char dbfilename[32];
317
318         printf("\033[32m\033[1mConverting table %d\033[0m\n", which_cdb);
319
320         // shamelessly swiped from https://docs.oracle.com/database/bdb181/html/programmer_reference/am_cursor.html
321         DB *dbp;
322         DBC *dbcp;
323         DBT in_key, in_data, out_key, out_data;
324         int num_rows = 0;
325
326         // create a database handle
327         ret = db_create(&dbp, dbenv, 0);
328         if (ret) {
329                 printf("db: db_create: %s\n", db_strerror(ret));
330                 printf("db: exit code %d\n", ret);
331                 exit(CTDLEXIT_DB);
332         }
333
334         // open the file
335         snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", which_cdb);
336         printf("\033[33m\033[1mdb: opening %s\033[0m\n", dbfilename);
337         ret = dbp->open(dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
338         if (ret) {
339                 printf("db: db_open: %s\n", db_strerror(ret));
340                 printf("db: exit code %d\n", ret);
341                 exit(CTDLEXIT_DB);
342         }
343
344         // Acquire a cursor
345         if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
346                 printf("db: db_cursor: %s\n", db_strerror(ret));
347                 printf("db: exit code %d\n", ret);
348                 exit(CTDLEXIT_DB);
349         }
350
351         // Zero out these database keys
352         memset(&in_key, 0, sizeof(in_key));
353         memset(&in_data, 0, sizeof(in_data));
354         memset(&out_key, 0, sizeof(out_key));
355         memset(&out_data, 0, sizeof(out_data));
356
357         // Walk through the database, calling convert functions as we go and clearing buffers before each call.
358         while (out_key.size = 0, out_data.size = 0, (ret = dbcp->get(dbcp, &in_key, &in_data, DB_NEXT)) == 0) {
359
360                 // Do we need to decompress?
361                 static int32_t magic = COMPRESS_MAGIC;
362                 if ( (in_data.size >= sizeof(magic)) && (!memcmp(in_data.data, &magic, sizeof(magic))) ) {
363                         printf("\033[31m\033[1m COMPRESSED \033[0m\n");
364                         // FIXME do the decompression
365                 }
366
367                 // Call the convert function registered to this table
368                 convert_functions[which_cdb](which_cdb, &in_key, &in_data, &out_key, &out_data);
369
370                 // write the converted record to the new database
371                 if (out_key.size > 0) {
372                         printf("DB: %02x ,  in_keylen: %3d ,  in_datalen: %d , dataptr: %lx\n", which_cdb, (int)in_key.size, (int)in_data.size, (long unsigned int)in_data.data);
373                         printf("DB: %02x , out_keylen: %3d , out_datalen: %d , dataptr: %lx\n", which_cdb, (int)out_key.size, (int)out_data.size, (long unsigned int)out_data.data);
374
375                         // Knowing the total number of rows isn't critical to the program.  It's just for the user to know.
376                         ++num_rows;
377                 }
378         }
379
380         if (ret != DB_NOTFOUND) {
381                 printf("db: db_get: %s\n", db_strerror(ret));
382                 printf("db: exit code %d\n", ret);
383                 exit(CTDLEXIT_DB);
384         }
385
386         printf("%d rows\n", num_rows);
387
388         // free any leftover out_data pointers
389         free(out_key.data);
390         free(out_data.data);
391
392         // Flush the logs...
393         //printf("\033[33m\033[1mdb: flushing the database logs\033[0m\n");
394         //if ((ret = dbenv->log_flush(dbenv, NULL))) {
395                 //printf("db: log_flush: %s\n", db_strerror(ret));
396         //}
397
398         // ...and close the database (table)
399         printf("\033[33m\033[1mdb: closing database %02x\033[0m\n", which_cdb);
400         ret = dbp->close(dbp, 0);
401         if (ret) {
402                 printf("db: db_close: %s\n", db_strerror(ret));
403         }
404
405 }
406
407
408 int main(int argc, char **argv) {
409         char *src_dir = NULL;
410         int confirmed = 0;
411
412         // Check to make sure we're running on the target 64-bit system
413         if (sizeof(void *) != 8) {
414                 fprintf(stderr, "%s: this is a %ld-bit system.\n", argv[0], sizeof(void *)*8);
415                 fprintf(stderr, "%s: you must run this on a 64-bit system, onto which a 32-bit database has been copied.\n", argv[0]);
416                 exit(1);
417         }
418
419         // Parse command line
420         int a;
421         while ((a = getopt(argc, argv, "s:d:y")) != EOF) {
422                 switch (a) {
423                 case 's':
424                         src_dir = optarg;
425                         break;
426                 case 'y':
427                         confirmed = 1;
428                         break;
429                 default:
430                         fprintf(stderr, "%s: usage: %s -s source_dir -d dest_dir\n", argv[0], argv[0]);
431                         exit(2);
432                 }
433         }
434
435         // Warn the user
436         printf("------------------------------------------------------------------------\n");
437         printf("ctdl3264 converts a Citadel database written on a 32-bit system to one  \n");
438         printf("that can be run on a 64-bit system.  It is intended to be run OFFLINE.  \n");
439         printf("Neither the source nor the target data directories should be mounted by \n");
440         printf("a running Citadel server.  We guarantee data corruption if you do not   \n");
441         printf("observe this warning!  The source [-s] directory should contain a copy  \n");
442         printf("of the database from your 32-bit system.  The destination [-d] directory\n");
443         printf("should be empty and will receive your 64-bit database.                  \n");
444         printf("------------------------------------------------------------------------\n");
445         printf("     Source 32-bit directory: %s\n", src_dir);
446         printf("Destination 64-bit directory: %s\n", "FIXME");
447         printf("------------------------------------------------------------------------\n");
448
449         if (confirmed == 1) {
450                 printf("You have specified the [-y] flag, so processing will continue.\n");
451         }
452         else {
453                 printf("Please consult the documentation to learn how to proceed.\n");
454                 exit(0);
455         }
456
457         open_dbenv(src_dir);
458         for (int i = 0; i < MAXCDB; ++i) {
459                 convert_table(i);
460         }
461         close_dbenv();
462
463         exit(0);
464 }