verify that 'begin' and 'end' lines were found
[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                 abort();
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 // Convert a "msgtext" record to a message on disk.   NOT THREADSAFE
115 // This also works for "bigmsg" records.
116 int import_msgtext(char *line, DBT *out_key, DBT *out_data) {
117
118         static char *b64_decoded_msg = NULL;
119         static size_t b64_decoded_alloc = 0;
120         long msgnum;
121         char *token;
122
123         token = strtok(line, "|");
124         msgnum = atol(strtok(NULL, "|"));
125         token = strtok(NULL, "|");
126
127         // The record key will be the message number
128         out_key->size = sizeof(long);
129         out_key->data = reallok(out_key->data, out_key->size);
130         memcpy(out_key->data, &msgnum, out_key->size);
131
132         // The record data will be the decoded message text.
133         // We are allocating more memory than we need, but BDB will only write the number of bytes we tell it to.
134         out_data->data = reallok(out_data->data, strlen(token));
135         out_data->size = CtdlDecodeBase64(out_data->data, token, strlen(token));
136         return(1);
137 }
138
139
140 // Convert a "msgmeta" record to a message metadata record on disk.  NOT THREADSAFE
141 int import_msgmeta(char *line, DBT *out_key, DBT *out_data) {
142         char *token;
143         struct MetaData *m = malloc(sizeof(struct MetaData));
144
145         memset(m, 0, sizeof(struct MetaData));
146         char *p = line;
147
148         for (int i=0; (token = strsep(&p, "|")); ++i) {
149                 switch(i) {
150                         case 1:
151                                 m->meta_msgnum = atol(token);
152                                 break;
153                         case 2:
154                                 m->meta_refcount = atoi(token);
155                                 break;
156                         case 3:
157                                 strncpy(m->meta_content_type, token, sizeof(m->meta_content_type));
158                                 break;
159                         case 4:
160                                 m->meta_rfc822_length = atol(token);
161                                 break;
162                 }
163         }
164
165         // metadata records are stored in the CDB_MSGMAIN table,
166         // but with the index being the *negative* of the message number.
167         long index = 0 - m->meta_msgnum;
168         out_key->size = sizeof(long);
169         out_key->data = reallok(NULL, out_key->size);
170         memcpy(out_key->data, &index, out_key->size);
171
172         // data
173         out_data->size = sizeof(struct MetaData);
174         out_data->data = m;                             // out_data owns this memory now
175
176         return(1);
177 }
178
179
180 // Convert a "user" record to a record on disk.  (Source string is unusable after this function is called.)
181 int import_user(char *line, DBT *out_key, DBT *out_data) {
182         char userkey[USERNAME_SIZE];
183         char *token;
184         struct ctdluser *u = malloc(sizeof(struct ctdluser));
185
186         memset(u, 0, sizeof(struct ctdluser));
187         char *p = line;
188
189         for (int i=0; (token = strsep(&p, "|")); ++i) {
190                 switch(i) {
191                         case 1:
192                                 u->version = atoi(token);
193                                 break;
194                         case 2:
195                                 u->uid = atoi(token);
196                                 break;
197                         case 3:
198                                 strncpy(u->password, token, sizeof(u->password));
199                                 break;
200                         case 4:
201                                 u->flags = atoi(token);
202                                 break;
203                         case 5:
204                                 u->axlevel = atoi(token);
205                                 break;
206                         case 6:
207                                 u->usernum = atol(token);
208                                 break;
209                         case 7:
210                                 u->lastcall = atol(token);
211                                 break;
212                         case 8:
213                                 u->USuserpurge = atoi(token);
214                                 break;
215                         case 9:
216                                 strncpy(u->fullname, token, sizeof(u->fullname));
217                                 break;
218                         case 10:
219                                 u->msgnum_bio = atol(token);
220                                 break;
221                         case 11:
222                                 u->msgnum_pic = atol(token);
223                                 break;
224                         case 12:
225                                 CtdlDecodeBase64(u->emailaddrs, token, strlen(token));
226                                 break;
227                         case 13:
228                                 u->msgnum_inboxrules = atol(token);
229                                 break;
230                         case 14:
231                                 u->lastproc_inboxrules = atol(token);
232                                 break;
233                 }
234         }
235         
236         makeuserkey(userkey, u->fullname);
237         out_key->size = strlen(userkey);
238         out_key->data = strdup(userkey);
239         out_data->size = sizeof(struct ctdluser);
240         out_data->data = u;
241         return(1);
242 }
243
244
245 // Convert a "room" record to a record on disk.  (Source string is unusable after this function is called.)
246 int import_room(char *line, DBT *out_key, DBT *out_data) {
247         char *token;
248         struct ctdlroom *r = malloc(sizeof(struct ctdlroom));
249
250         memset(r, 0, sizeof(struct ctdlroom));
251         char *p = line;
252
253         for (int i=0; (token = strsep(&p, "|")); ++i) {
254                 switch(i) {
255                         case 1:
256                                 strncpy(r->QRname, token, sizeof(r->QRname));
257                                 break;
258                         case 2:
259                                 strncpy(r->QRpasswd, token, sizeof (r->QRpasswd));
260                                 break;
261                         case 3:
262                                 r->QRroomaide = atol(token);
263                                 break;
264                         case 4:
265                                 r->QRhighest = atol(token);
266                                 break;
267                         case 5:
268                                 r->QRgen = atol(token);
269                                 break;
270                         case 6:
271                                 r->QRflags = atoi(token);
272                                 break;
273                         case 7:
274                                 strncpy(r->QRdirname, token, sizeof(r->QRdirname));
275                                 break;
276                         case 8:
277                                 r->msgnum_info = atol(token);
278                                 break;
279                         case 9:
280                                 r->QRfloor = atoi(token);
281                                 break;
282                         case 10:
283                                 r->QRmtime = atol(token);
284                                 break;
285                         case 11:
286                                 r->QRep.expire_mode = atoi(token);
287                                 break;
288                         case 12:
289                                 r->QRep.expire_value = atoi(token);
290                                 break;
291                         case 13:
292                                 r->QRnumber = atol(token);
293                                 break;
294                         case 14:
295                                 r->QRorder = atoi(token);
296                                 break;
297                         case 15:
298                                 r->QRflags2 = atoi(token);
299                                 break;
300                         case 16:
301                                 r->QRdefaultview = atoi(token);
302                                 break;
303                         case 17:
304                                 r->msgnum_pic = atol(token);
305                                 break;
306                 }
307         }
308
309         // The key is the room name in all lower case
310         out_key->size = strlen(r->QRname);
311         out_key->data = strdup(r->QRname);
312         char *k = (char *)out_key->data;
313         for (int i=0; i<=out_key->size; ++i) {
314                 k[i] = tolower(k[i]);
315         }
316
317         out_data->size = sizeof(struct ctdlroom);
318         out_data->data = r;
319         return(1);
320 }
321
322
323 // Convert a floor record to a record on disk.
324 int import_floor(char *line, DBT *out_key, DBT *out_data) {
325         char *token;
326         struct floor *f = malloc(sizeof(struct floor));
327         int floor_num;
328
329         memset(f, 0, sizeof(struct floor));
330         char *p = line;
331
332         for (int i=0; (token = strsep(&p, "|")); ++i) {
333                 switch(i) {
334                         case 1:
335                                 floor_num = atoi(token);
336                                 break;
337                         case 2:
338                                 f->f_flags = atoi(token);
339                                 break;
340                         case 3:
341                                 strncpy(f->f_name, token, sizeof(f->f_name));
342                                 break;
343                         case 4:
344                                 f->f_ref_count = atoi(token);
345                                 break;
346                         case 5:
347                                 f->f_ep.expire_mode = atoi(token);
348                                 break;
349                         case 6:
350                                 f->f_ep.expire_value = atoi(token);
351                                 break;
352                 }
353         }
354
355         out_key->size = sizeof(int);
356         out_key->data = malloc(out_key->size);
357         memcpy(out_key->data, &floor_num, out_key->size);
358
359         out_data->size = sizeof(struct floor);
360         out_data->data = f;
361         return(1);
362 }
363
364
365 // Import a msglist record
366 // msglist|26|32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51|
367 int import_msglist(char *line, DBT *out_key, DBT *out_data) {
368         long roomnum;
369         char *token, *mtoken;
370         char *p = line;
371         char *q = NULL;
372         int num_msgs = 0;
373         long *msglist = NULL;
374
375         for (int i=0; (token = strsep(&p, "|")); ++i) {
376                 switch(i) {
377                         case 1:
378                                 roomnum = atol(token);
379                                 break;
380                         case 2:
381                                 q = token;
382                                 for (int j=0; (mtoken = strsep(&q, ",")); ++j) {
383                                         msglist = realloc(msglist, (num_msgs+1) * sizeof(long));
384                                         msglist[num_msgs++] = atol(mtoken);
385                                 }
386                                 break;
387                 }
388         }
389
390         out_key->size = sizeof(long);
391         out_key->data = malloc(out_key->size);
392         memcpy(out_key->data, &roomnum, out_key->size);
393
394         out_data->size = num_msgs * sizeof(long);
395         out_data->data = msglist;
396
397         return(1);
398 }
399
400
401 // Convert a "visit" record to a record on disk.
402 int import_visit(char *line, DBT *out_key, DBT *out_data) {
403         char *token;
404         struct visit *v = malloc(sizeof(struct visit));
405
406         memset(v, 0, sizeof(struct visit));
407         char *p = line;
408
409         for (int i=0; (token = strsep(&p, "|")); ++i) {
410                 switch(i) {
411                         case 1:
412                                 v->v_roomnum = atol(token);
413                                 break;
414                         case 2:
415                                 v->v_roomgen = atol(token);
416                                 break;
417                         case 3:
418                                 v->v_usernum = atol(token);
419                                 break;
420                         case 4:
421                                 v->v_lastseen = atoi(token);
422                                 break;
423                         case 5:
424                                 v->v_flags = atoi(token);
425                                 break;
426                         case 6:
427                                 strncpy(v->v_seen, token, sizeof(v->v_seen));
428                                 break;
429                         case 7:
430                                 strncpy(v->v_answered, token, sizeof(v->v_answered));
431                                 break;
432                         case 8:
433                                 v->v_view = atoi(token);
434                                 break;
435                 }
436         }
437
438         // The key is the same as the first three data members (3 x long)
439         out_key->size = sizeof(long) * 3;
440         out_key->data = reallok(NULL, out_key->size);
441         memcpy(out_key->data, v, out_key->size);
442
443         out_data->size = sizeof(struct visit);
444         out_data->data = v;
445         return(1);
446 }
447
448
449 // Convert a "dir" record to a record on disk.
450 int import_dir(char *line, DBT *out_key, DBT *out_data) {
451         char dirkey[SIZ];
452         char username[USERNAME_SIZE];
453         char *token;
454
455         char *p = line;
456         for (int i=0; (token = strsep(&p, "|")); ++i) {
457                 switch(i) {
458                         case 1:
459                                 strncpy(dirkey, token, sizeof(dirkey));
460                                 break;
461                         case 2:
462                                 strncpy(username, token, sizeof(username));
463                                 break;
464                 }
465         }
466
467         out_key->size = strlen(dirkey);
468         out_key->data = reallok(NULL, out_key->size);
469         memcpy(out_key->data, dirkey, strlen(dirkey));
470
471         out_data->size = strlen(username) + 1;
472         out_data->data = strdup(username);
473
474         return(1);
475 }
476
477
478 // Convert a "usetable" record to a record on disk.
479 int import_usetable(char *line, DBT *out_key, DBT *out_data) {
480         char *token;
481         struct UseTable *u = malloc(sizeof(struct UseTable));
482
483         memset(u, 0, sizeof(struct UseTable));
484         char *p = line;
485
486         for (int i=0; (token = strsep(&p, "|")); ++i) {
487                 switch(i) {
488                         case 1:
489                                 u->hash = atoi(token);
490                                 break;
491                         case 2:
492                                 u->timestamp = atol(token);
493                                 break;
494                 }
495         }
496
497         // the key is just an int (the hash)
498         out_key->size = sizeof(int);
499         out_key->data = reallok(NULL, out_key->size);
500         memcpy(out_key->data, &u->hash, out_key->size);
501
502         out_data->size = sizeof(struct UseTable);
503         out_data->data = u;
504         return(1);
505 }
506
507
508 // Import a full text search index record.
509 // It's just like a msglists record: a key and a list of message numbers, but the key is "int" instead of "long"
510 int import_fulltext(char *line, DBT *out_key, DBT *out_data) {
511         int indexnum;
512         char *token, *mtoken;
513         char *p = line;
514         char *q = NULL;
515         int num_msgs = 0;
516         long *msglist = NULL;
517
518         for (int i=0; (token = strsep(&p, "|")); ++i) {
519                 switch(i) {
520                         case 1:
521                                 indexnum = atoi(token);
522                                 break;
523                         case 2:
524                                 q = token;
525                                 for (int j=0; (mtoken = strsep(&q, ",")); ++j) {
526                                         msglist = realloc(msglist, (num_msgs+1) * sizeof(long));
527                                         msglist[num_msgs++] = atol(mtoken);
528                                 }
529                                 break;
530                 }
531         }
532
533         out_key->size = sizeof(int);
534         out_key->data = malloc(out_key->size);
535         memcpy(out_key->data, &indexnum, out_key->size);
536
537         out_data->size = num_msgs * sizeof(long);
538         out_data->data = msglist;
539
540         return(1);
541 }
542
543
544 // Import an EUID Index record
545 // euidindex|msgnum|roomnum|euid|
546 int import_euidindex(char *line, DBT *out_key, DBT *out_data) {
547         char euid[SIZ];
548         long msgnum;
549         long roomnum;
550         char *token;
551
552         char *p = line;
553         for (int i=0; (token = strsep(&p, "|")); ++i) {
554                 switch(i) {
555                         case 1:
556                                 msgnum = atol(token);
557                                 break;
558                         case 2:
559                                 roomnum = atol(token);
560                                 break;
561                         case 3:
562                                 strncpy(euid, token, sizeof(euid));
563                                 break;
564                 }
565         }
566
567         // The structure of an euidindex record *key* is:
568         // |----room_number----|----------EUID-------------|
569         //    (sizeof long)       (actual length of euid)
570         out_key->size = sizeof(long) + strlen(euid) + 1;
571         out_key->data = reallok(NULL, out_key->size);
572         memcpy(out_key->data, &roomnum, sizeof(long));
573         strcpy(out_key->data + sizeof(long), euid);
574
575         // The structure of an euidindex record *value* is:
576         // |-----msg_number----|----room_number----|----------EUID-------------|
577         //    (sizeof long)       (sizeof long)       (actual length of euid)
578         out_data->size = sizeof(long) + sizeof(long) + strlen(euid) + 1;
579         out_data->data = reallok(NULL, out_data->size);
580         memcpy(out_data->data, &msgnum, sizeof(long));
581         memcpy(out_data->data + sizeof(long), &roomnum, sizeof(long));
582         strcpy(out_data->data + sizeof(long) + sizeof(long), euid);
583
584         return(1);
585 }
586
587
588 // Import a "users by number" (secondary index) record
589 // The key is a "long"
590 int import_usersbynumber(char *line, DBT *out_key, DBT *out_data) {
591         char *token;
592         long usernum;
593
594         char *p = line;
595         for (int i=0; (token = strsep(&p, "|")); ++i) {
596                 switch(i) {
597                         case 1:
598                                 usernum = atol(token);
599                                 break;
600                         case 2:
601                                 out_key->size = sizeof(long);
602                                 out_key->data = reallok(NULL, sizeof(long));
603                                 memcpy(out_key->data, &usernum, out_key->size);
604                                 out_data->data = strdup(token);
605                                 out_data->size = strlen(out_data->data) + 1;
606                                 return(1);
607                 }
608         }
609
610         return(0);              // should never get here unless it's a bad record
611 }
612
613
614 // Import a config record
615 // The key is the config key
616 // The data is the config key, a null, the value, and another null
617 int import_config(char *line, DBT *out_key, DBT *out_data) {
618         char *token;
619         char *p = line;
620         char *k, *v;
621
622         for (int i=0; (token = strsep(&p, "|")); ++i) {
623                 switch(i) {
624                         case 1:
625                                 k = token;
626                                 out_key->size = strlen(token);
627                                 out_key->data = strdup(token);
628                                 break;
629                         case 2:
630                                 v = token;
631                                 out_data->size = strlen(k) + strlen(v) + 2;
632                                 out_data->data = reallok(NULL, out_data->size);
633                                 strcpy(out_data->data, k);
634                                 strcpy(out_data->data + strlen(k) + 1, v);
635                                 break;
636                 }
637         }
638
639         return(1);
640 }
641
642
643 // Ingest one line of dump data.  NOT REENTRANT
644 void ingest_one(char *line, DB_ENV *dst_dbenv) {
645
646         static int good_rows = 0;
647         static int bad_rows = 0;
648         static int previous_cdb = -1 ;
649         static int current_cdb = -1 ;
650         static DB *dst_dbp;
651         char record_type[32];
652         int ret;
653         char dbfilename[32];
654         int row_was_good;
655         DBT out_key, out_data;
656
657         // We are assuming that the lines of the dump file will generally be sorted by table.
658         // By remembering the last table we worked with, we can do close/open if the table changes.
659
660         if (current_cdb >= 0) {
661                 fprintf(stderr, "   \033[33m%02x \033[32m%9d \033[31m%8d\033[0m\r", current_cdb, good_rows, bad_rows);
662                 fflush(stderr);
663         }
664
665         // Clear out our record buffer
666         memset(&out_key, 0, sizeof(DBT));
667         memset(&out_data, 0, sizeof(DBT));
668         row_was_good = 0;
669
670         // Identify the record type we are currently working with,
671         // then call the correct conversion function to load up our record buffer.
672         extract_token(record_type, line, 0, '|', sizeof record_type);
673         if (!strcasecmp(record_type, "msgtext")) {
674                 current_cdb = CDB_MSGMAIN;
675                 row_was_good = import_msgtext(line, &out_key, &out_data);
676         }
677         else if (!strcasecmp(record_type, "msgmeta")) {
678                 current_cdb = CDB_MSGMAIN;
679                 row_was_good = import_msgmeta(line, &out_key, &out_data);
680         }
681         else if (!strcasecmp(record_type, "user")) {
682                 current_cdb = CDB_USERS;
683                 row_was_good = import_user(line, &out_key, &out_data);
684         }
685         else if (!strcasecmp(record_type, "room")) {
686                 current_cdb = CDB_ROOMS;
687                 row_was_good = import_room(line, &out_key, &out_data);
688         }
689         else if (!strcasecmp(record_type, "floor")) {
690                 current_cdb = CDB_FLOORTAB;
691                 row_was_good = import_floor(line, &out_key, &out_data);
692         }
693         else if (!strcasecmp(record_type, "msglist")) {
694                 current_cdb = CDB_MSGLISTS;
695                 row_was_good = import_msglist(line, &out_key, &out_data);
696         }
697         else if (!strcasecmp(record_type, "visit")) {
698                 current_cdb = CDB_VISIT;
699                 row_was_good = import_visit(line, &out_key, &out_data);
700         }
701         else if (!strcasecmp(record_type, "dir")) {
702                 current_cdb = CDB_DIRECTORY;
703                 row_was_good = import_dir(line, &out_key, &out_data);
704         }
705         else if (!strcasecmp(record_type, "use")) {
706                 current_cdb = CDB_USETABLE;
707                 row_was_good = import_usetable(line, &out_key, &out_data);
708         }
709         else if (!strcasecmp(record_type, "bigmsg")) {
710                 current_cdb = CDB_BIGMSGS;
711                 row_was_good = import_msgtext(line, &out_key, &out_data);
712         }
713         else if (!strcasecmp(record_type, "fulltext")) {
714                 current_cdb = CDB_FULLTEXT;
715                 row_was_good = import_fulltext(line, &out_key, &out_data);
716         }
717         else if (!strcasecmp(record_type, "euidindex")) {
718                 current_cdb = CDB_EUIDINDEX;
719                 row_was_good = import_euidindex(line, &out_key, &out_data);
720         }
721         else if (!strcasecmp(record_type, "usersbynumber")) {
722                 current_cdb = CDB_USERSBYNUMBER;
723                 row_was_good = import_usersbynumber(line, &out_key, &out_data);
724         }
725         else if (!strcasecmp(record_type, "config")) {
726                 current_cdb = CDB_CONFIG;
727                 row_was_good = import_config(line, &out_key, &out_data);
728         }
729         else {
730                 current_cdb = -1 ;
731         }
732
733         // If the current record is a different table than the previous record,
734         // then close the other table and open the correct one.
735         if (current_cdb != previous_cdb) {
736                 if (previous_cdb >= 0) {
737                         fprintf(stderr, "\n");
738                 }
739                 if (previous_cdb >= 0) {
740                         ret = dst_dbp->close(dst_dbp, 0);
741                         if (ret) {
742                                 fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
743                         }
744                 }
745
746                 if (current_cdb >= 0) {
747                         good_rows = 0;
748                         bad_rows = 0;
749                         snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", current_cdb);
750
751                         // create a database handle for the destination table
752                         ret = db_create(&dst_dbp, dst_dbenv, 0);
753                         if (ret) {
754                                 fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
755                                 fprintf(stderr, "db: exit code %d\n", ret);
756                                 exit(CTDLEXIT_DB);
757                         }
758                 
759                         // open the file containing the destination table
760                         ret = dst_dbp->open(dst_dbp, NULL, dbfilename, NULL, DB_BTREE, (DB_CREATE | DB_TRUNCATE), 0600);
761                         if (ret) {
762                                 fprintf(stderr, "db: db_open(%s): %s\n", dbfilename, db_strerror(ret));
763                                 fprintf(stderr, "db: exit code %d\n", ret);
764                                 exit(CTDLEXIT_DB);
765                         }
766                 }
767
768                 previous_cdb = current_cdb;
769         }
770
771         // If the conversion function was successful, write the record to the database.
772         if (row_was_good) {
773                 ++good_rows;
774                 ret = dst_dbp->put(dst_dbp, NULL, &out_key, &out_data, 0);
775                 if (ret) {
776                         fprintf(stderr, "db: cdb_put(%x): %s", current_cdb, db_strerror(ret));
777                         exit(CTDLEXIT_DB);
778                 }
779         }
780         else {
781                 ++bad_rows;
782         }
783
784         free(out_key.data);
785         free(out_data.data);
786 }
787
788
789 // This is the loop that loads the dump data.  NOT REENTRANT
790 void ingest(DB_ENV *dst_dbenv) {
791         static size_t line_alloc = 1;
792         static char *line;
793         static size_t line_len = 0;
794         int ch;
795         int begin_found = 0;
796         int end_found = 0;
797
798         fprintf(stderr, "\033[7mtable\033[0m \033[7mgood_rows\033[0m \033[7mbad_rows\033[0m\n");
799         line = reallok(NULL, line_alloc);
800
801         do {
802                 line_len = 0;
803
804                 while (ch = getc(stdin), ((ch != '\n') && (ch > 0))) {
805                         if ((line_len+2) > line_alloc) {
806                                 line_alloc *= 2;
807                                 line = reallok(line, line_alloc);
808                         }
809                         line[line_len++] = ch;
810                         line[line_len] = 0;
811                 }
812         
813                 if (ch <= 0) {
814                         return;
815                 }
816
817                 if (line_len > 0) {
818                         ingest_one(line, dst_dbenv);
819                         if (!strncasecmp(line, HKEY("begin|"))) begin_found = 1;
820                         if (!strncasecmp(line, HKEY("end|"))) end_found = 1;
821                 }
822
823         } while (ch >= 0);
824
825         if (!begin_found) {
826                 fprintf(stderr, "\033[31mWARNING: \"begin\" line was not found in the loaded data.\033[0m\n");
827         }
828         if (!end_found) {
829                 fprintf(stderr, "\033[31mWARNING: \"end\" line was not found in the loaded data.\033[0m\n");
830         }
831 }
832
833
834 // Main entry point
835 int main(int argc, char **argv) {
836         char dst_dir[PATH_MAX];
837         int confirmed = 0;
838         static DB_ENV *dst_dbenv;               // Target DB environment
839
840         // display the greeting
841         fprintf(stderr, "\033[44m\033[33m\033[1m \033[K\033[0m\n"
842                         "\033[44m\033[33m\033[1m DB Load utility for Citadel \033[K\033[0m\n"
843                         "\033[44m\033[33m\033[1m Copyright (c) 2023 by citadel.org et al.  \033[K\033[0m\n"
844                         "\033[44m\033[33m\033[1m This program is open source software.  Use, duplication, or disclosure \033[K\033[0m\n"
845                         "\033[44m\033[33m\033[1m is subject to the terms of the GNU General Public license v3. \033[K\033[0m\n"
846                         "\033[44m\033[33m\033[1m \033[K\033[0m\n");
847
848         // Default destination directory unless overridden
849         snprintf(dst_dir, sizeof(dst_dir), "%s/data", CTDLDIR);
850
851         // Parse command line
852         int a;
853         while ((a = getopt(argc, argv, "h:y")) != EOF) {
854                 switch (a) {
855                 case 'h':
856                         snprintf(dst_dir, sizeof(dst_dir), "%s/data", optarg);
857                         break;
858                 case 'y':
859                         confirmed = 1;
860                         break;
861                 default:
862                         fprintf(stderr, "%s: usage: %s -h dest_dir [<dumpfile]\n", argv[0], argv[0]);
863                         exit(2);
864                 }
865         }
866
867         if (dst_dir == NULL) {
868                 fprintf(stderr, "ctdlload: no destination directory was specified.\n");
869                 exit(1);
870         }
871
872         if (confirmed == 1) {
873                 fprintf(stderr, "ctdlload: You have specified the [-y] flag, so processing will continue.\n");
874         }
875         else {
876                 fprintf(stderr, "ctdlload: usage: ctdlload -y -h[data_dir] <[dump_file]\n");
877                 fprintf(stderr, "          [data_dir] is your database directory, usually /usr/local/citadel/data\n");
878                 fprintf(stderr, "          Please read [ https://www.citadel.org/dump-and-load.html ] to learn how to proceed.\n");
879                 exit(1);
880         }
881
882         // Remove any database that is already in the target directory (yes, delete it, be careful)
883         char cmd[PATH_MAX];
884         snprintf(cmd, sizeof cmd, "rm -fv %s/cdb.* %s/log.*", dst_dir, dst_dir);
885         system(cmd);
886
887         dst_dbenv = open_dbenv(dst_dir);
888         ingest(dst_dbenv);
889         close_dbenv(dst_dbenv);
890
891         fprintf(stderr, "ctdlload: \033[32m\033[1mfinished\033[0m\n");
892         exit(0);
893 }