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.
4 // Copyright (c) 2023 by Art Cancro citadel.org
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.
14 #include <sys/types.h>
15 #include <sys/socket.h>
23 #include <libcitadel.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"
32 static DB_ENV *dbenv; // The DB environment (global)
34 // Open the database environment
35 void open_dbenv(char *src_dir) {
39 int dbversion_major, dbversion_minor, dbversion_patch;
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",
47 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch)
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.");
60 printf("db: Setting up DB environment\n");
61 ret = db_env_create(&dbenv, 0);
63 printf("db: db_env_create: %s\n", db_strerror(ret));
64 printf("db: exit code %d\n", ret);
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);
71 printf("db: set_cachesize: %s\n", db_strerror(ret));
72 dbenv->close(dbenv, 0);
73 printf("db: exit code %d\n", ret);
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);
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);
88 printf("db: dbenv->open: %s\n", db_strerror(ret));
89 dbenv->close(dbenv, 0);
90 printf("db: exit code %d\n", ret);
96 void close_dbenv(void) {
97 printf("db: closing dbenv\n");
98 int ret = dbenv->close(dbenv, 0);
100 printf("db: DBENV->close: %s\n", db_strerror(ret));
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) {
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) {
114 memcpy(&in_msgnum, in_key->data, sizeof(in_msgnum));
116 if (in_key->size != 4) {
117 printf("\033[31m\033[1m *** SOURCE DATABASE IS NOT 32-BIT *** ABORTING *** \033[0m\n");
122 // If the msgnum is negative, we are looking at METADATA
124 struct MetaData_32 *meta32 = (struct MetaData_32 *)in_data->data;
126 //printf("metadata: msgnum=%d , refcount=%d , content_type=\"%s\" , rfc822len=%d\n",
127 //meta32->meta_msgnum, meta32->meta_refcount, meta32->meta_content_type, meta32->meta_rfc822_length);
129 out_key->size = sizeof(long);
130 out_key->data = realloc(out_key->data, out_key->size);
131 out_msgnum = (long)in_msgnum;
132 memcpy(out_key->data, &out_msgnum, sizeof(long));
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;
145 // If the msgnum is positive, we are looking at a MESSAGE
146 else if (in_msgnum > 0) {
147 out_key->size = sizeof(long);
148 out_key->data = realloc(out_key->data, out_key->size);
149 out_msgnum = (long)in_msgnum;
150 memcpy(out_key->data, &out_msgnum, sizeof(long));
152 // copy the message verbatim
153 out_data->size = in_data->size;
154 out_data->data = realloc(out_data->data, out_data->size);
155 memcpy(out_data->data, in_data->data, out_data->size);
158 // If the msgnum is 0 it's probably not a valid record.
160 printf("msgmain: record 0 is impossible\n");
165 // convert function for a user record
166 void convert_users(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
168 // The key is a string so we can just copy it over
169 out_key->size = in_key->size;
170 out_key->data = realloc(out_key->data, out_key->size);
171 memcpy(out_key->data, in_key->data, in_key->size);
173 struct ctdluser_32 *user32 = (struct ctdluser_32 *)in_data->data;
175 out_data->size = sizeof(struct ctdluser);
176 out_data->data = realloc(out_data->data, out_data->size);
177 struct ctdluser *user64 = (struct ctdluser *)out_data->data;
179 user64->version = (int) user32->version;
180 user64->uid = (uid_t) user32->uid;
181 strcpy(user64->password, user32->password);
182 user64->flags = (unsigned) user32->flags;
183 user64->timescalled = (long) user32->timescalled;
184 user64->posted = (long) user32->posted;
185 user64->axlevel = (cit_uint8_t) user32->axlevel;
186 user64->usernum = (long) user32->usernum;
187 user64->lastcall = (time_t) user32->lastcall;
188 user64->USuserpurge = (int) user32->USuserpurge;
189 strcpy(user64->fullname, user32->fullname);
190 user64->msgnum_bio = (long) user32->msgnum_bio;
191 user64->msgnum_pic = (long) user32->msgnum_pic;
192 strcpy(user64->emailaddrs, user32->emailaddrs);
193 user64->msgnum_inboxrules = (long) user32->msgnum_inboxrules;
194 user64->lastproc_inboxrules = (long) user32->lastproc_inboxrules;
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) {
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);
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;
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;
232 // convert function for a floor record
233 void convert_floors(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) {
235 // the key is an "int", and "int" is 32-bits on both 32 and 64 bit platforms.
236 out_key->size = in_key->size;
237 out_key->data = realloc(out_key->data, out_key->size);
238 memcpy(out_key->data, in_key->data, in_key->size);
241 struct floor_32 *floor32 = (struct floor_32 *)in_data->data;
242 out_data->size = sizeof(struct floor);
243 out_data->data = realloc(out_data->data, out_data->size);
244 struct floor *floor64 = (struct floor *)out_data->data;
246 // these are probably bit-for-bit identical, actually ... but we do it the "right" way anyway
247 floor64->f_flags = (unsigned short) floor32->f_flags;
248 strcpy(floor64->f_name, floor32->f_name);
249 floor64->f_ref_count = (int) floor32->f_ref_count;
250 floor64->f_ep.expire_mode = (int) floor32->f_ep.expire_mode;
251 floor64->f_ep.expire_value = (int) floor32->f_ep.expire_value;
255 void (*convert_functions[])(int which_cdb, DBT *in_key, DBT *in_data, DBT *out_key, DBT *out_data) = {
256 convert_msgmain, // CDB_MSGMAIN
257 convert_users, // CDB_USERS
258 convert_rooms, // CDB_ROOMS
259 convert_floors, // CDB_FLOORTAB
260 null_function, // CDB_MSGLISTS
261 null_function, // CDB_VISIT
262 null_function, // CDB_DIRECTORY
263 null_function, // CDB_USETABLE
264 null_function, // CDB_BIGMSGS
265 null_function, // CDB_FULLTEXT
266 null_function, // CDB_EUIDINDEX
267 null_function, // CDB_USERSBYNUMBER
268 null_function, // CDB_EXTAUTH
269 null_function // CDB_CONFIG
273 void convert_table(int which_cdb) {
277 printf("\033[32m\033[1mConverting table %d\033[0m\n", which_cdb);
279 // shamelessly swiped from https://docs.oracle.com/database/bdb181/html/programmer_reference/am_cursor.html
282 DBT in_key, in_data, out_key, out_data;
285 // create a database handle
286 ret = db_create(&dbp, dbenv, 0);
288 printf("db: db_create: %s\n", db_strerror(ret));
289 printf("db: exit code %d\n", ret);
294 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", which_cdb);
295 printf("\033[33m\033[1mdb: opening %s\033[0m\n", dbfilename);
296 ret = dbp->open(dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
298 printf("db: db_open: %s\n", db_strerror(ret));
299 printf("db: exit code %d\n", ret);
304 if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
305 printf("db: db_cursor: %s\n", db_strerror(ret));
306 printf("db: exit code %d\n", ret);
310 // Zero out these database keys
311 memset(&in_key, 0, sizeof(in_key));
312 memset(&in_data, 0, sizeof(in_data));
313 memset(&out_key, 0, sizeof(out_key));
314 memset(&out_data, 0, sizeof(out_data));
316 // Walk through the database, calling convert functions as we go and clearing buffers before each call.
317 while (out_key.size = 0, out_data.size = 0, (ret = dbcp->get(dbcp, &in_key, &in_data, DB_NEXT)) == 0) {
319 // FIXME handle compressed records here
321 // Call the convert function registered to this table
322 convert_functions[which_cdb](which_cdb, &in_key, &in_data, &out_key, &out_data);
324 // write the converted record to the new database
325 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);
326 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);
328 // Knowing the total number of rows isn't critical to the program. It's just for the user to know.
332 if (ret != DB_NOTFOUND) {
333 printf("db: db_get: %s\n", db_strerror(ret));
334 printf("db: exit code %d\n", ret);
338 printf("%d rows\n", num_rows);
340 // free any leftover out_data pointers
345 //printf("\033[33m\033[1mdb: flushing the database logs\033[0m\n");
346 //if ((ret = dbenv->log_flush(dbenv, NULL))) {
347 //printf("db: log_flush: %s\n", db_strerror(ret));
350 // ...and close the database (table)
351 printf("\033[33m\033[1mdb: closing database %02x\033[0m\n", which_cdb);
352 ret = dbp->close(dbp, 0);
354 printf("db: db_close: %s\n", db_strerror(ret));
360 int main(int argc, char **argv) {
361 char *src_dir = NULL;
364 // Check to make sure we're running on the target 64-bit system
365 if (sizeof(void *) != 8) {
366 fprintf(stderr, "%s: this is a %ld-bit system.\n", argv[0], sizeof(void *)*8);
367 fprintf(stderr, "%s: you must run this on a 64-bit system, onto which a 32-bit database has been copied.\n", argv[0]);
371 // Parse command line
373 while ((a = getopt(argc, argv, "s:d:y")) != EOF) {
382 fprintf(stderr, "%s: usage: %s -s source_dir -d dest_dir\n", argv[0], argv[0]);
388 printf("------------------------------------------------------------------------\n");
389 printf("ctdl3264 converts a Citadel database written on a 32-bit system to one \n");
390 printf("that can be run on a 64-bit system. It is intended to be run OFFLINE. \n");
391 printf("Neither the source nor the target data directories should be mounted by \n");
392 printf("a running Citadel server. We guarantee data corruption if you do not \n");
393 printf("observe this warning! The source [-s] directory should contain a copy \n");
394 printf("of the database from your 32-bit system. The destination [-d] directory\n");
395 printf("should be empty and will receive your 64-bit database. \n");
396 printf("------------------------------------------------------------------------\n");
397 printf(" Source 32-bit directory: %s\n", src_dir);
398 printf("Destination 64-bit directory: %s\n", "FIXME");
399 printf("------------------------------------------------------------------------\n");
401 if (confirmed == 1) {
402 printf("You have specified the [-y] flag, so processing will continue.\n");
405 printf("Please consult the documentation to learn how to proceed.\n");
410 for (int i = 0; i < MAXCDB; ++i) {