]> code.citadel.org Git - citadel.git/blob - citadel/utils/ctdlload.c
Pretty statistics
[citadel.git] / citadel / utils / ctdlload.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/makeuserkey.h"
29 #include "../server/citadel_dirs.h"
30
31
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);
35         if (!p) {
36                 fprintf(stderr, "realloc() failed to resize %p to %ld bytes, error: %m\n", ptr, size);
37                 exit(1);
38         }
39         return p;
40 }
41
42
43 // Open a database environment
44 DB_ENV *open_dbenv(char *dirname) {
45
46         DB_ENV *dbenv = NULL;
47
48         int ret;
49         int i;
50         u_int32_t flags = 0;
51         int dbversion_major, dbversion_minor, dbversion_patch;
52
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.");
60                 exit(CTDLEXIT_DB);
61         }
62
63         ret = db_env_create(&dbenv, 0);
64         if (ret) {
65                 fprintf(stderr,"db: db_env_create: %s\n", db_strerror(ret));
66                 fprintf(stderr,"db: exit code %d\n", ret);
67                 exit(CTDLEXIT_DB);
68         }
69
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);
72         if (ret) {
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);
76                 exit(CTDLEXIT_DB);
77         }
78
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);
83                 exit(CTDLEXIT_DB);
84         }
85
86         flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
87         ret = dbenv->open(dbenv, dirname, flags, 0);
88         if (ret) {
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);
92                 exit(CTDLEXIT_DB);
93         }
94
95         return(dbenv);
96 }
97
98
99 void close_dbenv(DB_ENV *dbenv) {
100         int ret = dbenv->close(dbenv, 0);
101         if (ret) {
102                 fprintf(stderr,"db: dbenv->close: %s\n", db_strerror(ret));
103         }
104 }
105
106
107 // Skeleton convert function
108 int convert_foo(char *line, DBT *out_key, DBT *out_data) {
109         return(0);
110 }
111
112
113 // Convert a "msgtext" record to a message on disk.   NOT THREADSAFE
114 int convert_msgtext(char *line, DBT *out_key, DBT *out_data) {
115
116         static char *b64_decoded_msg = NULL;
117         static size_t b64_decoded_alloc = 0;
118         long msgnum;
119         char *token;
120
121         token = strtok(line, "|");
122         msgnum = atol(strtok(NULL, "|"));
123         token = strtok(NULL, "|");
124
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);
129
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));
134         return(1);
135 }
136
137
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) {
140         char *token;
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, "|"));
147
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);
154
155         // data
156         out_data->size = sizeof(struct MetaData);
157         out_data->data = m;                             // out_data owns this memory now
158
159         return(1);
160 }
161
162
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];
166         char *token;
167         struct ctdluser *u = malloc(sizeof(struct ctdluser));
168
169         memset(u, 0, sizeof(struct ctdluser));
170         char *p = line;
171
172         for (int i=0; (token = strsep(&p, "|")); ++i) {
173                 switch(i) {
174                         case 1:
175                                 u->version = atoi(token);
176                                 break;
177                         case 2:
178                                 u->uid = atoi(token);
179                                 break;
180                         case 3:
181                                 strncpy(u->password, token, sizeof(u->password));
182                                 break;
183                         case 4:
184                                 u->flags = atoi(token);
185                                 break;
186                         case 5:
187                                 u->axlevel = atoi(token);
188                                 break;
189                         case 6:
190                                 u->usernum = atol(token);
191                                 break;
192                         case 7:
193                                 u->lastcall = atol(token);
194                                 break;
195                         case 8:
196                                 u->USuserpurge = atoi(token);
197                                 break;
198                         case 9:
199                                 strncpy(u->fullname, token, sizeof(u->fullname));
200                                 break;
201                         case 10:
202                                 u->msgnum_bio = atol(token);
203                                 break;
204                         case 11:
205                                 u->msgnum_pic = atol(token);
206                                 break;
207                         case 12:
208                                 CtdlDecodeBase64(u->emailaddrs, token, strlen(token));
209                                 break;
210                         case 13:
211                                 u->msgnum_inboxrules = atol(token);
212                                 break;
213                         case 14:
214                                 u->lastproc_inboxrules = atol(token);
215                                 break;
216                 }
217         }
218         
219         makeuserkey(userkey, u->fullname);
220         out_key->size = strlen(userkey);
221         out_key->data = strdup(userkey);
222         out_data->size = sizeof(struct ctdluser);
223         out_data->data = u;
224         return(1);
225 }
226
227
228 // Ingest one line of dump data.  NOT REENTRANT
229 void ingest_one(char *line, DB_ENV *dst_dbenv) {
230
231         static int good_rows = 0;
232         static int bad_rows = 0;
233         static int previous_cdb = -1 ;
234         static int current_cdb = -1 ;
235         static DB *dst_dbp;
236         char record_type[32];
237         int ret;
238         char dbfilename[32];
239         int row_was_good;
240         DBT out_key, out_data;
241
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.
244
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 ;
261
262         if (current_cdb != previous_cdb) {
263                 if (previous_cdb >= 0) {
264                         ret = dst_dbp->close(dst_dbp, 0);
265                         if (ret) {
266                                 fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
267                         }
268                 }
269                 if (previous_cdb > 0) {
270                         fprintf(stderr, "\n");
271                 }
272
273                 if (current_cdb >= 0) {
274                         good_rows = 0;
275                         bad_rows = 0;
276                         snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", current_cdb);
277
278                         // create a database handle for the destination table
279                         ret = db_create(&dst_dbp, dst_dbenv, 0);
280                         if (ret) {
281                                 fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
282                                 fprintf(stderr, "db: exit code %d\n", ret);
283                                 exit(CTDLEXIT_DB);
284                         }
285                 
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);
288                         if (ret) {
289                                 fprintf(stderr, "db: db_open: %s\n", db_strerror(ret));
290                                 fprintf(stderr, "db: exit code %d\n", ret);
291                                 exit(CTDLEXIT_DB);
292                         }
293                 }
294
295                 previous_cdb = current_cdb;
296         }
297
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));
301         row_was_good = 0;
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;
316
317         if (row_was_good) {
318                 ++good_rows;
319                 ret = dst_dbp->put(dst_dbp, NULL, &out_key, &out_data, 0);
320                 if (ret) {
321                         fprintf(stderr, "db: cdb_put(%x): %s", current_cdb, db_strerror(ret));
322                         exit(CTDLEXIT_DB);
323                 }
324         }
325         else {
326                 ++bad_rows;
327         }
328
329         free(out_key.data);
330         free(out_data.data);
331
332         if (current_cdb > 0) {
333                 fprintf(stderr, "   %02x %9d %8d\r", current_cdb, good_rows, bad_rows);
334                 fflush(stderr);
335         }
336 }
337
338
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;
342         static char *line;
343         static size_t line_len = 0;
344         char ch;
345
346         fprintf(stderr, "table good_rows bad_rows\n");
347         fprintf(stderr, "----- --------- --------\n");
348         line = reallok(NULL, line_alloc);
349
350         do {
351                 line_len = 0;
352
353                 while (ch = getc(stdin), ((ch != '\n') && (ch > 0))) {
354                         if ((line_len+2) > line_alloc) {
355                                 line_alloc *= 2;
356                                 line = reallok(line, line_alloc);
357                         }
358                         line[line_len++] = ch;
359                         line[line_len] = 0;
360                 }
361         
362                 if (ch <= 0) {
363                         return;
364                 }
365
366                 if (line_len > 0) {
367                         ingest_one(line, dst_dbenv);
368                 }
369
370         } while (ch >= 0);
371 }
372
373
374 // Main entry point
375 int main(int argc, char **argv) {
376         char *dst_dir = NULL;
377         int confirmed = 0;
378         static DB_ENV *dst_dbenv;               // Source DB environment (global)
379
380         // Parse command line
381         int a;
382         while ((a = getopt(argc, argv, "h:y")) != EOF) {
383                 switch (a) {
384                 case 'h':
385                         dst_dir = optarg;
386                         break;
387                 case 'y':
388                         confirmed = 1;
389                         break;
390                 default:
391                         fprintf(stderr, "%s: usage: %s -h dest_dir [<dumpfile]\n", argv[0], argv[0]);
392                         exit(2);
393                 }
394         }
395
396         if (confirmed == 1) {
397                 fprintf(stderr,"You have specified the [-y] flag, so processing will continue.\n");
398         }
399         else {
400                 fprintf(stderr,"Please read [ https://www.citadel.org/ctdlload.html ] to learn how to proceed.\n");
401                 exit(0);
402         }
403
404         char cmd[1024];
405         snprintf(cmd, sizeof cmd, "rm -fv %s/cdb.* %s/log.*", dst_dir, dst_dir);
406         system(cmd);
407
408         dst_dbenv = open_dbenv(dst_dir);
409         ingest(dst_dbenv);
410         close_dbenv(dst_dbenv);
411
412         exit(0);
413 }