]> code.citadel.org Git - citadel.git/blob - citadel/utils/ctdl3264.c
ctdl3264: sooper seekrit command line flag to make it not fail
[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 the citadel.org team
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 void null_function(int which_cdb, DBT *key, DBT *data) {
106         //printf("DB: %02x , keylen: %3d , datalen: %d , dataptr: %x\n", which_cdb, (int)key->size, (int)data->size, data->data);
107 }
108
109
110 void (*convert_functions[])(int which_cdb, DBT *key, DBT *data) = {
111         null_function,          // CDB_MSGMAIN
112         null_function,          // CDB_USERS
113         null_function,          // CDB_ROOMS
114         null_function,          // CDB_FLOORTAB
115         null_function,          // CDB_MSGLISTS
116         null_function,          // CDB_VISIT
117         null_function,          // CDB_DIRECTORY
118         null_function,          // CDB_USETABLE
119         null_function,          // CDB_BIGMSGS
120         null_function,          // CDB_FULLTEXT
121         null_function,          // CDB_EUIDINDEX
122         null_function,          // CDB_USERSBYNUMBER
123         null_function,          // CDB_EXTAUTH
124         null_function           // CDB_CONFIG
125 };
126
127
128 void convert_table(int which_cdb) {
129         int ret;
130         char dbfilename[32];
131
132         printf("Converting table %d\n", which_cdb);
133
134         // shamelessly swiped from https://docs.oracle.com/database/bdb181/html/programmer_reference/am_cursor.html
135         DB *dbp;
136         DBC *dbcp;
137         DBT key, data;
138         int num_rows = 0;
139
140         // create a database handle
141         ret = db_create(&dbp, dbenv, 0);
142         if (ret) {
143                 printf("db: db_create: %s\n", db_strerror(ret));
144                 printf("db: exit code %d\n", ret);
145                 exit(CTDLEXIT_DB);
146         }
147
148         // open the file
149         snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", which_cdb);
150         printf("\033[33m\033[1mdb: opening %s\033[0m\n", dbfilename);
151         ret = dbp->open(dbp, NULL, dbfilename, NULL, DB_BTREE, 0, 0600);
152         if (ret) {
153                 printf("db: db_open: %s\n", db_strerror(ret));
154                 printf("db: exit code %d\n", ret);
155                 exit(CTDLEXIT_DB);
156         }
157
158         // Acquire a cursor
159         if ((ret = dbp->cursor(dbp, NULL, &dbcp, 0)) != 0) {
160                 printf("db: db_cursor: %s\n", db_strerror(ret));
161                 printf("db: exit code %d\n", ret);
162                 exit(CTDLEXIT_DB);
163         }
164
165         // Initialize the key/data return pair.
166         memset(&key, 0, sizeof(key));
167         memset(&data, 0, sizeof(data));
168
169         // Walk through the database and print out the key/data pairs.
170         while ((ret = dbcp->get(dbcp, &key, &data, DB_NEXT)) == 0) {
171
172                 ++num_rows;
173
174                 // Call the convert function (this will need some parameters later)
175                 convert_functions[which_cdb](which_cdb, &key, &data);
176         }
177
178         if (ret != DB_NOTFOUND) {
179                 printf("db: db_get: %s\n", db_strerror(ret));
180                 printf("db: exit code %d\n", ret);
181                 exit(CTDLEXIT_DB);
182         }
183
184         printf("%d rows\n", num_rows);
185
186         // Flush the logs...
187         //printf("\033[33m\033[1mdb: flushing the database logs\033[0m\n");
188         //if ((ret = dbenv->log_flush(dbenv, NULL))) {
189                 //printf("db: log_flush: %s\n", db_strerror(ret));
190         //}
191
192         // ...and close the database (table)
193         printf("\033[33m\033[1mdb: closing database %02x\033[0m\n", which_cdb);
194         ret = dbp->close(dbp, 0);
195         if (ret) {
196                 printf("db: db_close: %s\n", db_strerror(ret));
197         }
198
199 }
200
201
202 int main(int argc, char **argv) {
203         char *src_dir = NULL;
204         int confirmed = 0;
205
206         // Check to make sure we're running on the target 64-bit system
207         if (sizeof(void *) != 8) {
208                 fprintf(stderr, "%s: this is a %ld-bit system.\n", argv[0], sizeof(void *)*8);
209                 fprintf(stderr, "%s: you must run this on a 64-bit system, onto which a 32-bit database has been copied.\n", argv[0]);
210                 exit(1);
211         }
212
213         // Parse command line
214         int a;
215         while ((a = getopt(argc, argv, "s:d:y")) != EOF) {
216                 switch (a) {
217                 case 's':
218                         src_dir = optarg;
219                         break;
220                 case 'y':
221                         confirmed = 1;
222                         break;
223                 default:
224                         fprintf(stderr, "%s: usage: %s -s source_dir -d dest_dir\n", argv[0], argv[0]);
225                         exit(2);
226                 }
227         }
228
229         // Warn the user
230         printf("------------------------------------------------------------------------\n");
231         printf("ctdl3264 converts a Citadel database written on a 32-bit system to one  \n");
232         printf("that can be run on a 64-bit system.  It is intended to be run OFFLINE.  \n");
233         printf("Neither the source nor the target data directories should be mounted by \n");
234         printf("a running Citadel server.  We guarantee data corruption if you do not   \n");
235         printf("observe this warning!  The source [-s] directory should contain a copy  \n");
236         printf("of the database from your 32-bit system.  The destination [-d] directory\n");
237         printf("should be empty and will receive your 64-bit database.                  \n");
238         printf("------------------------------------------------------------------------\n");
239         printf("     Source 32-bit directory: %s\n", src_dir);
240         printf("Destination 64-bit directory: %s\n", "FIXME");
241         printf("------------------------------------------------------------------------\n");
242
243         if (confirmed == 1) {
244                 printf("You have specified the [-y] flag, so processing will continue.\n");
245         }
246         else {
247                 printf("Please consult the documentation to learn how to proceed.\n");
248                 exit(0);
249         }
250
251         open_dbenv(src_dir);
252         for (int i = 0; i < MAXCDB; ++i) {
253                 convert_table(i);
254         }
255         close_dbenv();
256
257         exit(0);
258 }