]> code.citadel.org Git - citadel.git/blob - citadel/utils/ctdlload.c
ctdlload: import room records
[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 #if 0
314 // Convert a "foo" record to a record on disk.
315 int convert_oom(char *line, DBT *out_key, DBT *out_data) {
316         char *token;
317         struct ctdloom *r = malloc(sizeof(struct ctdloom));
318
319         memset(r, 0, sizeof(struct ctdloom));
320         char *p = line;
321
322         for (int i=0; (token = strsep(&p, "|")); ++i) {
323                 switch(i) {
324                 }
325         }
326
327         out_key->size = strlen(r->QRname);
328         out_key->data = strdup(r->QRname);
329         out_data->size = sizeof(struct ctdloom);
330         out_data->data = r;
331         return(0);
332 }
333 #endif
334
335
336 // Ingest one line of dump data.  NOT REENTRANT
337 void ingest_one(char *line, DB_ENV *dst_dbenv) {
338
339         static int good_rows = 0;
340         static int bad_rows = 0;
341         static int previous_cdb = -1 ;
342         static int current_cdb = -1 ;
343         static DB *dst_dbp;
344         char record_type[32];
345         int ret;
346         char dbfilename[32];
347         int row_was_good;
348         DBT out_key, out_data;
349
350         // We are assuming that the lines of the dump file will generally be sorted by table.
351         // By remembering the last table we worked with, we can do close/open if the table changes.
352
353         if (current_cdb >= 0) {
354                 fprintf(stderr, "   \033[33m%02x \033[32m%9d \033[31m%8d\033[0m\r", current_cdb, good_rows, bad_rows);
355                 fflush(stderr);
356         }
357
358         // Identify the record type we are currently working with
359         extract_token(record_type, line, 0, '|', sizeof record_type);
360         if (!strcasecmp(record_type, "msgtext"))                current_cdb = CDB_MSGMAIN;
361         else if (!strcasecmp(record_type, "msgmeta"))           current_cdb = CDB_MSGMAIN;
362         else if (!strcasecmp(record_type, "user"))              current_cdb = CDB_USERS;
363         else if (!strcasecmp(record_type, "room"))              current_cdb = CDB_ROOMS;
364         else if (!strcasecmp(record_type, "floor"))             current_cdb = CDB_FLOORTAB;
365         else if (!strcasecmp(record_type, "msglist"))           current_cdb = CDB_MSGLISTS;
366         else if (!strcasecmp(record_type, "visit"))             current_cdb = CDB_VISIT;
367         else if (!strcasecmp(record_type, "dir"))               current_cdb = CDB_DIRECTORY;
368         else if (!strcasecmp(record_type, "use"))               current_cdb = CDB_USETABLE;
369         else if (!strcasecmp(record_type, "bigmsg"))            current_cdb = CDB_BIGMSGS;
370         else if (!strcasecmp(record_type, "euidindex"))         current_cdb = CDB_EUIDINDEX;
371         else if (!strcasecmp(record_type, "usersbynumber"))     current_cdb = CDB_USERSBYNUMBER;
372         else if (!strcasecmp(record_type, "config"))            current_cdb = CDB_CONFIG;
373         else                                                    current_cdb = -1 ;
374
375         if (current_cdb != previous_cdb) {
376                 if (previous_cdb >= 0) {
377                         fprintf(stderr, "\n");
378                 }
379                 if (previous_cdb >= 0) {
380                         ret = dst_dbp->close(dst_dbp, 0);
381                         if (ret) {
382                                 fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
383                         }
384                 }
385
386                 if (current_cdb >= 0) {
387                         good_rows = 0;
388                         bad_rows = 0;
389                         snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", current_cdb);
390
391                         // create a database handle for the destination table
392                         ret = db_create(&dst_dbp, dst_dbenv, 0);
393                         if (ret) {
394                                 fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
395                                 fprintf(stderr, "db: exit code %d\n", ret);
396                                 exit(CTDLEXIT_DB);
397                         }
398                 
399                         // open the file containing the destination table
400                         ret = dst_dbp->open(dst_dbp, NULL, dbfilename, NULL, DB_BTREE, (DB_CREATE | DB_TRUNCATE), 0600);
401                         if (ret) {
402                                 fprintf(stderr, "db: db_open: %s\n", db_strerror(ret));
403                                 fprintf(stderr, "db: exit code %d\n", ret);
404                                 exit(CTDLEXIT_DB);
405                         }
406                 }
407
408                 previous_cdb = current_cdb;
409         }
410
411         // If we have a valid record type and a target database open, dispatch the correct record type handler.
412         memset(&out_key, 0, sizeof(DBT));
413         memset(&out_data, 0, sizeof(DBT));
414         row_was_good = 0;
415         if      (!strcasecmp(record_type, "msgtext"))           row_was_good = convert_msgtext(line, &out_key, &out_data);
416         else if (!strcasecmp(record_type, "msgmeta"))           row_was_good = convert_msgmeta(line, &out_key, &out_data);
417         else if (!strcasecmp(record_type, "user"))              row_was_good = convert_user(line, &out_key, &out_data);
418         else if (!strcasecmp(record_type, "room"))              row_was_good = convert_room(line, &out_key, &out_data);
419         else if (!strcasecmp(record_type, "floor"))             row_was_good = convert_foo(line, &out_key, &out_data);
420         else if (!strcasecmp(record_type, "msglist"))           row_was_good = convert_foo(line, &out_key, &out_data);
421         else if (!strcasecmp(record_type, "visit"))             row_was_good = convert_foo(line, &out_key, &out_data);
422         else if (!strcasecmp(record_type, "dir"))               row_was_good = convert_foo(line, &out_key, &out_data);
423         else if (!strcasecmp(record_type, "use"))               row_was_good = convert_foo(line, &out_key, &out_data);
424         else if (!strcasecmp(record_type, "bigmsg"))            row_was_good = convert_foo(line, &out_key, &out_data);
425         else if (!strcasecmp(record_type, "euidindex"))         row_was_good = convert_foo(line, &out_key, &out_data);
426         else if (!strcasecmp(record_type, "usersbynumber"))     row_was_good = convert_foo(line, &out_key, &out_data);
427         else if (!strcasecmp(record_type, "config"))            row_was_good = convert_foo(line, &out_key, &out_data);
428         else                                                    row_was_good = 0;
429
430         if (row_was_good) {
431                 ++good_rows;
432                 ret = dst_dbp->put(dst_dbp, NULL, &out_key, &out_data, 0);
433                 if (ret) {
434                         fprintf(stderr, "db: cdb_put(%x): %s", current_cdb, db_strerror(ret));
435                         exit(CTDLEXIT_DB);
436                 }
437         }
438         else {
439                 ++bad_rows;
440         }
441
442         free(out_key.data);
443         free(out_data.data);
444 }
445
446
447 // This is the loop that loads the dump data.  NOT REENTRANT
448 void ingest(DB_ENV *dst_dbenv) {
449         static size_t line_alloc = 1;
450         static char *line;
451         static size_t line_len = 0;
452         char ch;
453
454         fprintf(stderr, "\033[7mtable\033[0m \033[7mgood_rows\033[0m \033[7mbad_rows\033[0m\n");
455         line = reallok(NULL, line_alloc);
456
457         do {
458                 line_len = 0;
459
460                 while (ch = getc(stdin), ((ch != '\n') && (ch > 0))) {
461                         if ((line_len+2) > line_alloc) {
462                                 line_alloc *= 2;
463                                 line = reallok(line, line_alloc);
464                         }
465                         line[line_len++] = ch;
466                         line[line_len] = 0;
467                 }
468         
469                 if (ch <= 0) {
470                         return;
471                 }
472
473                 if (line_len > 0) {
474                         ingest_one(line, dst_dbenv);
475                 }
476
477         } while (ch >= 0);
478 }
479
480
481 // Main entry point
482 int main(int argc, char **argv) {
483         char *dst_dir = NULL;
484         int confirmed = 0;
485         static DB_ENV *dst_dbenv;               // Source DB environment (global)
486
487         // Parse command line
488         int a;
489         while ((a = getopt(argc, argv, "h:y")) != EOF) {
490                 switch (a) {
491                 case 'h':
492                         dst_dir = optarg;
493                         break;
494                 case 'y':
495                         confirmed = 1;
496                         break;
497                 default:
498                         fprintf(stderr, "%s: usage: %s -h dest_dir [<dumpfile]\n", argv[0], argv[0]);
499                         exit(2);
500                 }
501         }
502
503         if (confirmed == 1) {
504                 fprintf(stderr,"You have specified the [-y] flag, so processing will continue.\n");
505         }
506         else {
507                 fprintf(stderr,"Please read [ https://www.citadel.org/ctdlload.html ] to learn how to proceed.\n");
508                 exit(0);
509         }
510
511         char cmd[1024];
512         snprintf(cmd, sizeof cmd, "rm -fv %s/cdb.* %s/log.*", dst_dir, dst_dir);
513         system(cmd);
514
515         dst_dbenv = open_dbenv(dst_dir);
516         ingest(dst_dbenv);
517         close_dbenv(dst_dbenv);
518
519         exit(0);
520 }