]> code.citadel.org Git - citadel.git/blob - citadel/utils/ctdlload.c
ctdlload: import floor table
[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         db_version(&dbversion_major, &dbversion_minor, &dbversion_patch);
53
54         // Create synthetic integer version numbers and compare them.
55         // Never run with a libdb other than the one with which it was compiled.
56         int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
57         int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
58         if (compiled_db_version != linked_db_version) {
59                 fprintf(stderr,
60                         "db: ctdlload is running with a version of libdb other than the one with which it was compiled.\n"
61                         "db: compiled: %d\n"
62                         "db:   linked: %d\n"
63                         "db: This is an invalid configuration.  ctdlload will now exit to prevent data loss.",
64                         compiled_db_version,
65                         linked_db_version
66                 );
67                 exit(CTDLEXIT_DB);
68         }
69
70         ret = db_env_create(&dbenv, 0);
71         if (ret) {
72                 fprintf(stderr,"db: db_env_create: %s\n", db_strerror(ret));
73                 fprintf(stderr,"db: exit code %d\n", ret);
74                 exit(CTDLEXIT_DB);
75         }
76
77         // We want to specify the shared memory buffer pool cachesize, but everything else is the default.
78         ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
79         if (ret) {
80                 fprintf(stderr,"db: set_cachesize: %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         if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
87                 fprintf(stderr,"db: set_lk_detect: %s\n", db_strerror(ret));
88                 dbenv->close(dbenv, 0);
89                 fprintf(stderr,"db: exit code %d\n", ret);
90                 exit(CTDLEXIT_DB);
91         }
92
93         flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
94         ret = dbenv->open(dbenv, dirname, flags, 0);
95         if (ret) {
96                 fprintf(stderr,"db: dbenv->open: %s\n", db_strerror(ret));
97                 dbenv->close(dbenv, 0);
98                 fprintf(stderr,"db: exit code %d\n", ret);
99                 exit(CTDLEXIT_DB);
100         }
101
102         return(dbenv);
103 }
104
105
106 void close_dbenv(DB_ENV *dbenv) {
107         int ret = dbenv->close(dbenv, 0);
108         if (ret) {
109                 fprintf(stderr,"db: dbenv->close: %s\n", db_strerror(ret));
110         }
111 }
112
113
114 // Skeleton convert function
115 int convert_foo(char *line, DBT *out_key, DBT *out_data) {
116         return(0);
117 }
118
119
120 // Convert a "msgtext" record to a message on disk.   NOT THREADSAFE
121 int convert_msgtext(char *line, DBT *out_key, DBT *out_data) {
122
123         static char *b64_decoded_msg = NULL;
124         static size_t b64_decoded_alloc = 0;
125         long msgnum;
126         char *token;
127
128         token = strtok(line, "|");
129         msgnum = atol(strtok(NULL, "|"));
130         token = strtok(NULL, "|");
131
132         // The record key will be the message number
133         out_key->size = sizeof(long);
134         out_key->data = reallok(out_key->data, out_key->size);
135         memcpy(out_key->data, &msgnum, out_key->size);
136
137         // The record data will be the decoded message text.
138         // We are allocating more memory than we need, but BDB will only write the number of bytes we tell it to.
139         out_data->data = reallok(out_data->data, strlen(token));
140         out_data->size = CtdlDecodeBase64(out_data->data, token, strlen(token));
141         return(1);
142 }
143
144
145 // Convert a "msgmeta" record to a message metadata record on disk.  NOT THREADSAFE
146 int convert_msgmeta(char *line, DBT *out_key, DBT *out_data) {
147         char *token;
148         struct MetaData *m = malloc(sizeof(struct MetaData));
149         token = strtok(line, "|");
150         m->meta_msgnum = atol(strtok(NULL, "|"));
151         m->meta_refcount = atoi(strtok(NULL, "|"));
152         strncpy(m->meta_content_type, strtok(NULL, "|"), sizeof(m->meta_content_type));
153         m->meta_rfc822_length = atol(strtok(NULL, "|"));
154
155         // metadata records are stored in the CDB_MSGMAIN table,
156         // but with the index being the *negative* of the message number.
157         long index = 0 - m->meta_msgnum;
158         out_key->size = sizeof(long);
159         out_key->data = reallok(NULL, out_key->size);
160         memcpy(out_key->data, &index, out_key->size);
161
162         // data
163         out_data->size = sizeof(struct MetaData);
164         out_data->data = m;                             // out_data owns this memory now
165
166         return(1);
167 }
168
169
170 // Convert a "user" record to a record on disk.  (Source string is unusable after this function is called.)
171 int convert_user(char *line, DBT *out_key, DBT *out_data) {
172         char userkey[USERNAME_SIZE];
173         char *token;
174         struct ctdluser *u = malloc(sizeof(struct ctdluser));
175
176         memset(u, 0, sizeof(struct ctdluser));
177         char *p = line;
178
179         for (int i=0; (token = strsep(&p, "|")); ++i) {
180                 switch(i) {
181                         case 1:
182                                 u->version = atoi(token);
183                                 break;
184                         case 2:
185                                 u->uid = atoi(token);
186                                 break;
187                         case 3:
188                                 strncpy(u->password, token, sizeof(u->password));
189                                 break;
190                         case 4:
191                                 u->flags = atoi(token);
192                                 break;
193                         case 5:
194                                 u->axlevel = atoi(token);
195                                 break;
196                         case 6:
197                                 u->usernum = atol(token);
198                                 break;
199                         case 7:
200                                 u->lastcall = atol(token);
201                                 break;
202                         case 8:
203                                 u->USuserpurge = atoi(token);
204                                 break;
205                         case 9:
206                                 strncpy(u->fullname, token, sizeof(u->fullname));
207                                 break;
208                         case 10:
209                                 u->msgnum_bio = atol(token);
210                                 break;
211                         case 11:
212                                 u->msgnum_pic = atol(token);
213                                 break;
214                         case 12:
215                                 CtdlDecodeBase64(u->emailaddrs, token, strlen(token));
216                                 break;
217                         case 13:
218                                 u->msgnum_inboxrules = atol(token);
219                                 break;
220                         case 14:
221                                 u->lastproc_inboxrules = atol(token);
222                                 break;
223                 }
224         }
225         
226         makeuserkey(userkey, u->fullname);
227         out_key->size = strlen(userkey);
228         out_key->data = strdup(userkey);
229         out_data->size = sizeof(struct ctdluser);
230         out_data->data = u;
231         return(1);
232 }
233
234
235 // Convert a "room" record to a record on disk.  (Source string is unusable after this function is called.)
236 int convert_room(char *line, DBT *out_key, DBT *out_data) {
237         char *token;
238         struct ctdlroom *r = malloc(sizeof(struct ctdlroom));
239
240         memset(r, 0, sizeof(struct ctdlroom));
241         char *p = line;
242
243         for (int i=0; (token = strsep(&p, "|")); ++i) {
244                 switch(i) {
245                         case 1:
246                                 strncpy(r->QRname, token, sizeof(r->QRname));
247                                 break;
248                         case 2:
249                                 strncpy(r->QRpasswd, token, sizeof (r->QRpasswd));
250                                 break;
251                         case 3:
252                                 r->QRroomaide = atol(token);
253                                 break;
254                         case 4:
255                                 r->QRhighest = atol(token);
256                                 break;
257                         case 5:
258                                 r->QRgen = atol(token);
259                                 break;
260                         case 6:
261                                 r->QRflags = atoi(token);
262                                 break;
263                         case 7:
264                                 strncpy(r->QRdirname, token, sizeof(r->QRdirname));
265                                 break;
266                         case 8:
267                                 r->msgnum_info = atol(token);
268                                 break;
269                         case 9:
270                                 r->QRfloor = atoi(token);
271                                 break;
272                         case 10:
273                                 r->QRmtime = atol(token);
274                                 break;
275                         case 11:
276                                 r->QRep.expire_mode = atoi(token);
277                                 break;
278                         case 12:
279                                 r->QRep.expire_value = atoi(token);
280                                 break;
281                         case 13:
282                                 r->QRnumber = atol(token);
283                                 break;
284                         case 14:
285                                 r->QRorder = atoi(token);
286                                 break;
287                         case 15:
288                                 r->QRflags2 = atoi(token);
289                                 break;
290                         case 16:
291                                 r->QRdefaultview = atoi(token);
292                                 break;
293                         case 17:
294                                 r->msgnum_pic = atol(token);
295                                 break;
296                 }
297         }
298
299         // The key is the room name in all lower case
300         out_key->size = strlen(r->QRname);
301         out_key->data = strdup(r->QRname);
302         char *k = (char *)out_key->data;
303         for (int i=0; i<=out_key->size; ++i) {
304                 k[i] = tolower(k[i]);
305         }
306
307         out_data->size = sizeof(struct ctdlroom);
308         out_data->data = r;
309         return(1);
310 }
311
312
313 // Convert a floor record to a record on disk.
314 int convert_floor(char *line, DBT *out_key, DBT *out_data) {
315         char *token;
316         struct floor *f = malloc(sizeof(struct floor));
317         int floor_num;
318
319         memset(f, 0, sizeof(struct floor));
320         char *p = line;
321
322         for (int i=0; (token = strsep(&p, "|")); ++i) {
323                 switch(i) {
324                         case 1:
325                                 floor_num = atoi(token);
326                                 break;
327                         case 2:
328                                 f->f_flags = atoi(token);
329                                 break;
330                         case 3:
331                                 strncpy(f->f_name, token, sizeof(f->f_name));
332                                 break;
333                         case 4:
334                                 f->f_ref_count = atoi(token);
335                                 break;
336                         case 5:
337                                 f->f_ep.expire_mode = atoi(token);
338                                 break;
339                         case 6:
340                                 f->f_ep.expire_value = atoi(token);
341                                 break;
342                 }
343         }
344
345         out_key->size = sizeof(int);
346         out_key->data = malloc(out_key->size);
347         memcpy(out_key->data, &floor_num, out_key->size);
348
349         out_data->size = sizeof(struct floor);
350         out_data->data = f;
351         return(1);
352 }
353
354
355 #if 0
356 // Convert a "foo" record to a record on disk.
357 int convert_foo(char *line, DBT *out_key, DBT *out_data) {
358         char *token;
359         struct ctdlfoo *r = malloc(sizeof(struct ctdlfoo));
360
361         memset(r, 0, sizeof(struct ctdlfoo));
362         char *p = line;
363
364         for (int i=0; (token = strsep(&p, "|")); ++i) {
365                 switch(i) {
366                 }
367         }
368
369         out_key->size = strlen(r->QRname);
370         out_key->data = strdup(r->QRname);
371         out_data->size = sizeof(struct ctdlfoo);
372         out_data->data = r;
373         return(0);
374 }
375 #endif
376
377
378 // Ingest one line of dump data.  NOT REENTRANT
379 void ingest_one(char *line, DB_ENV *dst_dbenv) {
380
381         static int good_rows = 0;
382         static int bad_rows = 0;
383         static int previous_cdb = -1 ;
384         static int current_cdb = -1 ;
385         static DB *dst_dbp;
386         char record_type[32];
387         int ret;
388         char dbfilename[32];
389         int row_was_good;
390         DBT out_key, out_data;
391
392         // We are assuming that the lines of the dump file will generally be sorted by table.
393         // By remembering the last table we worked with, we can do close/open if the table changes.
394
395         if (current_cdb >= 0) {
396                 fprintf(stderr, "   \033[33m%02x \033[32m%9d \033[31m%8d\033[0m\r", current_cdb, good_rows, bad_rows);
397                 fflush(stderr);
398         }
399
400         // Identify the record type we are currently working with
401         extract_token(record_type, line, 0, '|', sizeof record_type);
402         if (!strcasecmp(record_type, "msgtext"))                current_cdb = CDB_MSGMAIN;
403         else if (!strcasecmp(record_type, "msgmeta"))           current_cdb = CDB_MSGMAIN;
404         else if (!strcasecmp(record_type, "user"))              current_cdb = CDB_USERS;
405         else if (!strcasecmp(record_type, "room"))              current_cdb = CDB_ROOMS;
406         else if (!strcasecmp(record_type, "floor"))             current_cdb = CDB_FLOORTAB;
407         else if (!strcasecmp(record_type, "msglist"))           current_cdb = CDB_MSGLISTS;
408         else if (!strcasecmp(record_type, "visit"))             current_cdb = CDB_VISIT;
409         else if (!strcasecmp(record_type, "dir"))               current_cdb = CDB_DIRECTORY;
410         else if (!strcasecmp(record_type, "use"))               current_cdb = CDB_USETABLE;
411         else if (!strcasecmp(record_type, "bigmsg"))            current_cdb = CDB_BIGMSGS;
412         else if (!strcasecmp(record_type, "euidindex"))         current_cdb = CDB_EUIDINDEX;
413         else if (!strcasecmp(record_type, "usersbynumber"))     current_cdb = CDB_USERSBYNUMBER;
414         else if (!strcasecmp(record_type, "config"))            current_cdb = CDB_CONFIG;
415         else                                                    current_cdb = -1 ;
416
417         if (current_cdb != previous_cdb) {
418                 if (previous_cdb >= 0) {
419                         fprintf(stderr, "\n");
420                 }
421                 if (previous_cdb >= 0) {
422                         ret = dst_dbp->close(dst_dbp, 0);
423                         if (ret) {
424                                 fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
425                         }
426                 }
427
428                 if (current_cdb >= 0) {
429                         good_rows = 0;
430                         bad_rows = 0;
431                         snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", current_cdb);
432
433                         // create a database handle for the destination table
434                         ret = db_create(&dst_dbp, dst_dbenv, 0);
435                         if (ret) {
436                                 fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
437                                 fprintf(stderr, "db: exit code %d\n", ret);
438                                 exit(CTDLEXIT_DB);
439                         }
440                 
441                         // open the file containing the destination table
442                         ret = dst_dbp->open(dst_dbp, NULL, dbfilename, NULL, DB_BTREE, (DB_CREATE | DB_TRUNCATE), 0600);
443                         if (ret) {
444                                 fprintf(stderr, "db: db_open: %s\n", db_strerror(ret));
445                                 fprintf(stderr, "db: exit code %d\n", ret);
446                                 exit(CTDLEXIT_DB);
447                         }
448                 }
449
450                 previous_cdb = current_cdb;
451         }
452
453         // If we have a valid record type and a target database open, dispatch the correct record type handler.
454         memset(&out_key, 0, sizeof(DBT));
455         memset(&out_data, 0, sizeof(DBT));
456         row_was_good = 0;
457         if      (!strcasecmp(record_type, "msgtext"))           row_was_good = convert_msgtext(line, &out_key, &out_data);
458         else if (!strcasecmp(record_type, "msgmeta"))           row_was_good = convert_msgmeta(line, &out_key, &out_data);
459         else if (!strcasecmp(record_type, "user"))              row_was_good = convert_user(line, &out_key, &out_data);
460         else if (!strcasecmp(record_type, "room"))              row_was_good = convert_room(line, &out_key, &out_data);
461         else if (!strcasecmp(record_type, "floor"))             row_was_good = convert_floor(line, &out_key, &out_data);
462         else if (!strcasecmp(record_type, "msglist"))           row_was_good = convert_foo(line, &out_key, &out_data);
463         else if (!strcasecmp(record_type, "visit"))             row_was_good = convert_foo(line, &out_key, &out_data);
464         else if (!strcasecmp(record_type, "dir"))               row_was_good = convert_foo(line, &out_key, &out_data);
465         else if (!strcasecmp(record_type, "use"))               row_was_good = convert_foo(line, &out_key, &out_data);
466         else if (!strcasecmp(record_type, "bigmsg"))            row_was_good = convert_foo(line, &out_key, &out_data);
467         else if (!strcasecmp(record_type, "euidindex"))         row_was_good = convert_foo(line, &out_key, &out_data);
468         else if (!strcasecmp(record_type, "usersbynumber"))     row_was_good = convert_foo(line, &out_key, &out_data);
469         else if (!strcasecmp(record_type, "config"))            row_was_good = convert_foo(line, &out_key, &out_data);
470         else                                                    row_was_good = 0;
471
472         if (row_was_good) {
473                 ++good_rows;
474                 ret = dst_dbp->put(dst_dbp, NULL, &out_key, &out_data, 0);
475                 if (ret) {
476                         fprintf(stderr, "db: cdb_put(%x): %s", current_cdb, db_strerror(ret));
477                         exit(CTDLEXIT_DB);
478                 }
479         }
480         else {
481                 ++bad_rows;
482         }
483
484         free(out_key.data);
485         free(out_data.data);
486 }
487
488
489 // This is the loop that loads the dump data.  NOT REENTRANT
490 void ingest(DB_ENV *dst_dbenv) {
491         static size_t line_alloc = 1;
492         static char *line;
493         static size_t line_len = 0;
494         char ch;
495
496         fprintf(stderr, "\033[7mtable\033[0m \033[7mgood_rows\033[0m \033[7mbad_rows\033[0m\n");
497         line = reallok(NULL, line_alloc);
498
499         do {
500                 line_len = 0;
501
502                 while (ch = getc(stdin), ((ch != '\n') && (ch > 0))) {
503                         if ((line_len+2) > line_alloc) {
504                                 line_alloc *= 2;
505                                 line = reallok(line, line_alloc);
506                         }
507                         line[line_len++] = ch;
508                         line[line_len] = 0;
509                 }
510         
511                 if (ch <= 0) {
512                         return;
513                 }
514
515                 if (line_len > 0) {
516                         ingest_one(line, dst_dbenv);
517                 }
518
519         } while (ch >= 0);
520 }
521
522
523 // Main entry point
524 int main(int argc, char **argv) {
525         char *dst_dir = NULL;
526         int confirmed = 0;
527         static DB_ENV *dst_dbenv;               // Source DB environment (global)
528
529         // Parse command line
530         int a;
531         while ((a = getopt(argc, argv, "h:y")) != EOF) {
532                 switch (a) {
533                 case 'h':
534                         dst_dir = optarg;
535                         break;
536                 case 'y':
537                         confirmed = 1;
538                         break;
539                 default:
540                         fprintf(stderr, "%s: usage: %s -h dest_dir [<dumpfile]\n", argv[0], argv[0]);
541                         exit(2);
542                 }
543         }
544
545         if (confirmed == 1) {
546                 fprintf(stderr,"You have specified the [-y] flag, so processing will continue.\n");
547         }
548         else {
549                 fprintf(stderr,"Please read [ https://www.citadel.org/ctdlload.html ] to learn how to proceed.\n");
550                 exit(0);
551         }
552
553         char cmd[1024];
554         snprintf(cmd, sizeof cmd, "rm -fv %s/cdb.* %s/log.*", dst_dir, dst_dir);
555         system(cmd);
556
557         dst_dbenv = open_dbenv(dst_dir);
558         ingest(dst_dbenv);
559         close_dbenv(dst_dbenv);
560
561         exit(0);
562 }