1 // Don't run this. It doesn't work and if you try to run it you will immediately die.
3 // Copyright (c) 2023 by Art Cancro citadel.org
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.
13 #include <sys/types.h>
14 #include <sys/socket.h>
22 #include <libcitadel.h>
25 #include "../server/sysdep.h"
26 #include "../server/citadel_defs.h"
27 #include "../server/server.h"
28 #include "../server/makeuserkey.h"
29 #include "../server/citadel_dirs.h"
32 // Wrapper for realloc() that crashes and burns if the call fails.
33 void *reallok(void *ptr, size_t size) {
34 void *p = realloc(ptr, size);
36 fprintf(stderr, "realloc() failed to resize %p to %ld bytes, error: %m\n", ptr, size);
43 // Open a database environment
44 DB_ENV *open_dbenv(char *dirname) {
51 int dbversion_major, dbversion_minor, dbversion_patch;
53 // Create synthetic integer version numbers and compare them.
54 // Never run with a libdb older than the one with which it was compiled.
55 int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
56 int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
57 if (compiled_db_version > linked_db_version) {
58 fprintf(stderr, "db: ctdlload is running with a version of libdb older than the one with which it was compiled.\n"
59 "db: This is an invalid configuration. ctdlload will now exit to prevent data loss.");
63 ret = db_env_create(&dbenv, 0);
65 fprintf(stderr,"db: db_env_create: %s\n", db_strerror(ret));
66 fprintf(stderr,"db: exit code %d\n", ret);
70 // We want to specify the shared memory buffer pool cachesize, but everything else is the default.
71 ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
73 fprintf(stderr,"db: set_cachesize: %s\n", db_strerror(ret));
74 dbenv->close(dbenv, 0);
75 fprintf(stderr,"db: exit code %d\n", ret);
79 if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
80 fprintf(stderr,"db: set_lk_detect: %s\n", db_strerror(ret));
81 dbenv->close(dbenv, 0);
82 fprintf(stderr,"db: exit code %d\n", ret);
86 flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
87 ret = dbenv->open(dbenv, dirname, flags, 0);
89 fprintf(stderr,"db: dbenv->open: %s\n", db_strerror(ret));
90 dbenv->close(dbenv, 0);
91 fprintf(stderr,"db: exit code %d\n", ret);
99 void close_dbenv(DB_ENV *dbenv) {
100 int ret = dbenv->close(dbenv, 0);
102 fprintf(stderr,"db: dbenv->close: %s\n", db_strerror(ret));
107 // Skeleton convert function
108 int convert_foo(char *line, DBT *out_key, DBT *out_data) {
113 // Convert a "msgtext" record to a message on disk. NOT THREADSAFE
114 int convert_msgtext(char *line, DBT *out_key, DBT *out_data) {
116 static char *b64_decoded_msg = NULL;
117 static size_t b64_decoded_alloc = 0;
121 token = strtok(line, "|");
122 msgnum = atol(strtok(NULL, "|"));
123 token = strtok(NULL, "|");
125 // The record key will be the message number
126 out_key->size = sizeof(long);
127 out_key->data = reallok(out_key->data, out_key->size);
128 memcpy(out_key->data, &msgnum, out_key->size);
130 // The record data will be the decoded message text.
131 // We are allocating more memory than we need, but BDB will only write the number of bytes we tell it to.
132 out_data->data = reallok(out_data->data, strlen(token));
133 out_data->size = CtdlDecodeBase64(out_data->data, token, strlen(token));
138 // Convert a "msgmeta" record to a message metadata record on disk. NOT THREADSAFE
139 int convert_msgmeta(char *line, DBT *out_key, DBT *out_data) {
141 struct MetaData *m = malloc(sizeof(struct MetaData));
142 token = strtok(line, "|");
143 m->meta_msgnum = atol(strtok(NULL, "|"));
144 m->meta_refcount = atoi(strtok(NULL, "|"));
145 strncpy(m->meta_content_type, strtok(NULL, "|"), sizeof(m->meta_content_type));
146 m->meta_rfc822_length = atol(strtok(NULL, "|"));
148 // metadata records are stored in the CDB_MSGMAIN table,
149 // but with the index being the *negative* of the message number.
150 long index = 0 - m->meta_msgnum;
151 out_key->size = sizeof(long);
152 out_key->data = reallok(NULL, out_key->size);
153 memcpy(out_key->data, &index, out_key->size);
156 out_data->size = sizeof(struct MetaData);
157 out_data->data = m; // out_data owns this memory now
163 // Convert a "user" record to a record on disk. NOT THREADSAFE
164 int convert_user(char *line, DBT *out_key, DBT *out_data) {
165 char userkey[USERNAME_SIZE];
167 struct ctdluser *u = malloc(sizeof(struct ctdluser));
169 memset(u, 0, sizeof(struct ctdluser));
172 for (int i=0; (token = strsep(&p, "|")); ++i) {
175 u->version = atoi(token);
178 u->uid = atoi(token);
181 strncpy(u->password, token, sizeof(u->password));
184 u->flags = atoi(token);
187 u->axlevel = atoi(token);
190 u->usernum = atol(token);
193 u->lastcall = atol(token);
196 u->USuserpurge = atoi(token);
199 strncpy(u->fullname, token, sizeof(u->fullname));
202 u->msgnum_bio = atol(token);
205 u->msgnum_pic = atol(token);
208 CtdlDecodeBase64(u->emailaddrs, token, strlen(token));
211 u->msgnum_inboxrules = atol(token);
214 u->lastproc_inboxrules = atol(token);
219 makeuserkey(userkey, u->fullname);
220 out_key->size = strlen(userkey);
221 out_key->data = strdup(userkey);
222 out_data->size = sizeof(struct ctdluser);
228 // Ingest one line of dump data. NOT REENTRANT
229 void ingest_one(char *line, DB_ENV *dst_dbenv) {
231 static int good_rows = 0;
232 static int bad_rows = 0;
233 static int previous_cdb = -1 ;
234 static int current_cdb = -1 ;
236 char record_type[32];
240 DBT out_key, out_data;
242 // We are assuming that the lines of the dump file will generally be sorted by table.
243 // By remembering the last table we worked with, we can do close/open if the table changes.
245 // Identify the record type we are currently working with
246 extract_token(record_type, line, 0, '|', sizeof record_type);
247 if (!strcasecmp(record_type, "msgtext")) current_cdb = CDB_MSGMAIN;
248 else if (!strcasecmp(record_type, "msgmeta")) current_cdb = CDB_MSGMAIN;
249 else if (!strcasecmp(record_type, "user")) current_cdb = CDB_USERS;
250 else if (!strcasecmp(record_type, "room")) current_cdb = CDB_ROOMS;
251 else if (!strcasecmp(record_type, "floor")) current_cdb = CDB_FLOORTAB;
252 else if (!strcasecmp(record_type, "msglist")) current_cdb = CDB_MSGLISTS;
253 else if (!strcasecmp(record_type, "visit")) current_cdb = CDB_VISIT;
254 else if (!strcasecmp(record_type, "dir")) current_cdb = CDB_DIRECTORY;
255 else if (!strcasecmp(record_type, "use")) current_cdb = CDB_USETABLE;
256 else if (!strcasecmp(record_type, "bigmsg")) current_cdb = CDB_BIGMSGS;
257 else if (!strcasecmp(record_type, "euidindex")) current_cdb = CDB_EUIDINDEX;
258 else if (!strcasecmp(record_type, "usersbynumber")) current_cdb = CDB_USERSBYNUMBER;
259 else if (!strcasecmp(record_type, "config")) current_cdb = CDB_CONFIG;
260 else current_cdb = -1 ;
262 if (current_cdb != previous_cdb) {
263 if (previous_cdb >= 0) {
264 ret = dst_dbp->close(dst_dbp, 0);
266 fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
269 if (previous_cdb > 0) {
270 fprintf(stderr, "\n");
273 if (current_cdb >= 0) {
276 snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", current_cdb);
278 // create a database handle for the destination table
279 ret = db_create(&dst_dbp, dst_dbenv, 0);
281 fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
282 fprintf(stderr, "db: exit code %d\n", ret);
286 // open the file containing the destination table
287 ret = dst_dbp->open(dst_dbp, NULL, dbfilename, NULL, DB_BTREE, (DB_CREATE | DB_TRUNCATE), 0600);
289 fprintf(stderr, "db: db_open: %s\n", db_strerror(ret));
290 fprintf(stderr, "db: exit code %d\n", ret);
295 previous_cdb = current_cdb;
298 // If we have a valid record type and a target database open, dispatch the correct record type handler.
299 memset(&out_key, 0, sizeof(DBT));
300 memset(&out_data, 0, sizeof(DBT));
302 if (!strcasecmp(record_type, "msgtext")) row_was_good = convert_msgtext(line, &out_key, &out_data);
303 else if (!strcasecmp(record_type, "msgmeta")) row_was_good = convert_msgmeta(line, &out_key, &out_data);
304 else if (!strcasecmp(record_type, "user")) row_was_good = convert_user(line, &out_key, &out_data);
305 else if (!strcasecmp(record_type, "room")) row_was_good = convert_foo(line, &out_key, &out_data);
306 else if (!strcasecmp(record_type, "floor")) row_was_good = convert_foo(line, &out_key, &out_data);
307 else if (!strcasecmp(record_type, "msglist")) row_was_good = convert_foo(line, &out_key, &out_data);
308 else if (!strcasecmp(record_type, "visit")) row_was_good = convert_foo(line, &out_key, &out_data);
309 else if (!strcasecmp(record_type, "dir")) row_was_good = convert_foo(line, &out_key, &out_data);
310 else if (!strcasecmp(record_type, "use")) row_was_good = convert_foo(line, &out_key, &out_data);
311 else if (!strcasecmp(record_type, "bigmsg")) row_was_good = convert_foo(line, &out_key, &out_data);
312 else if (!strcasecmp(record_type, "euidindex")) row_was_good = convert_foo(line, &out_key, &out_data);
313 else if (!strcasecmp(record_type, "usersbynumber")) row_was_good = convert_foo(line, &out_key, &out_data);
314 else if (!strcasecmp(record_type, "config")) row_was_good = convert_foo(line, &out_key, &out_data);
315 else row_was_good = 0;
319 ret = dst_dbp->put(dst_dbp, NULL, &out_key, &out_data, 0);
321 fprintf(stderr, "db: cdb_put(%x): %s", current_cdb, db_strerror(ret));
332 if (current_cdb > 0) {
333 fprintf(stderr, " %02x %9d %8d\r", current_cdb, good_rows, bad_rows);
339 // This is the loop that loads the dump data. NOT REENTRANT
340 void ingest(DB_ENV *dst_dbenv) {
341 static size_t line_alloc = 1;
343 static size_t line_len = 0;
346 fprintf(stderr, "table good_rows bad_rows\n");
347 fprintf(stderr, "----- --------- --------\n");
348 line = reallok(NULL, line_alloc);
353 while (ch = getc(stdin), ((ch != '\n') && (ch > 0))) {
354 if ((line_len+2) > line_alloc) {
356 line = reallok(line, line_alloc);
358 line[line_len++] = ch;
367 ingest_one(line, dst_dbenv);
375 int main(int argc, char **argv) {
376 char *dst_dir = NULL;
378 static DB_ENV *dst_dbenv; // Source DB environment (global)
380 // Parse command line
382 while ((a = getopt(argc, argv, "h:y")) != EOF) {
391 fprintf(stderr, "%s: usage: %s -h dest_dir [<dumpfile]\n", argv[0], argv[0]);
396 if (confirmed == 1) {
397 fprintf(stderr,"You have specified the [-y] flag, so processing will continue.\n");
400 fprintf(stderr,"Please read [ https://www.citadel.org/ctdlload.html ] to learn how to proceed.\n");
405 snprintf(cmd, sizeof cmd, "rm -fv %s/cdb.* %s/log.*", dst_dir, dst_dir);
408 dst_dbenv = open_dbenv(dst_dir);
410 close_dbenv(dst_dbenv);