]> code.citadel.org Git - citadel.git/blob - citadel/utils/ctdlload.c
ctdlload: load message metadata
[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/citadel_dirs.h"
29
30
31 // Wrapper for realloc() that crashes and burns if the call fails.
32 void *reallok(void *ptr, size_t size) {
33         void *p = realloc(ptr, size);
34         if (!p) {
35                 fprintf(stderr, "realloc() failed to resize %p to %ld bytes, error: %m\n", ptr, size);
36                 exit(1);
37         }
38         return p;
39 }
40
41
42 // Open a database environment
43 DB_ENV *open_dbenv(char *dirname) {
44
45         DB_ENV *dbenv = NULL;
46
47         int ret;
48         int i;
49         u_int32_t flags = 0;
50         int dbversion_major, dbversion_minor, dbversion_patch;
51
52         fprintf(stderr, "db: open_dbenv() starting\n"
53                 "db:    Linked zlib: %s\n"
54                 "db: Compiled libdb: %s\n"
55                 "db:   Linked libdb: %s\n",
56                 zlibVersion(),
57                 DB_VERSION_STRING,
58                 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch)
59         );
60
61         // Create synthetic integer version numbers and compare them.
62         // Never run with a libdb older than the one with which it was compiled.
63         int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
64         int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
65         if (compiled_db_version > linked_db_version) {
66                 fprintf(stderr, "db: ctdlload is running with a version of libdb older than the one with which it was compiled.\n"
67                         "db: This is an invalid configuration.  ctdlload will now exit to prevent data loss.");
68                 exit(CTDLEXIT_DB);
69         }
70
71         fprintf(stderr,"db: Setting up DB environment\n");
72         ret = db_env_create(&dbenv, 0);
73         if (ret) {
74                 fprintf(stderr,"db: db_env_create: %s\n", db_strerror(ret));
75                 fprintf(stderr,"db: exit code %d\n", ret);
76                 exit(CTDLEXIT_DB);
77         }
78
79         // We want to specify the shared memory buffer pool cachesize, but everything else is the default.
80         ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
81         if (ret) {
82                 fprintf(stderr,"db: set_cachesize: %s\n", db_strerror(ret));
83                 dbenv->close(dbenv, 0);
84                 fprintf(stderr,"db: exit code %d\n", ret);
85                 exit(CTDLEXIT_DB);
86         }
87
88         if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
89                 fprintf(stderr,"db: set_lk_detect: %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         flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
96         fprintf(stderr,"db: dbenv open(dir=%s, flags=%d)\n", dirname, flags);
97         ret = dbenv->open(dbenv, dirname, flags, 0);
98         if (ret) {
99                 fprintf(stderr,"db: dbenv->open: %s\n", db_strerror(ret));
100                 dbenv->close(dbenv, 0);
101                 fprintf(stderr,"db: exit code %d\n", ret);
102                 exit(CTDLEXIT_DB);
103         }
104
105         return(dbenv);
106 }
107
108
109 void close_dbenv(DB_ENV *dbenv) {
110         fprintf(stderr,"db: closing dbenv\n");
111         int ret = dbenv->close(dbenv, 0);
112         if (ret) {
113                 fprintf(stderr,"db: dbenv->close: %s\n", db_strerror(ret));
114         }
115 }
116
117
118 // Skeleton convert function
119 int convert_foo(char *line, DBT *out_key, DBT *out_data) {
120         return(0);
121 }
122
123
124 // Convert a "msgtext" record to a message on disk.   NOT THREADSAFE
125 int convert_msgtext(char *line, DBT *out_key, DBT *out_data) {
126
127         static char *b64_decoded_msg = NULL;
128         static size_t b64_decoded_alloc = 0;
129         long msgnum;
130         char *token;
131
132         token = strtok(line, "|");
133         msgnum = atol(strtok(NULL, "|"));
134         token = strtok(NULL, "|");
135
136         // The record key will be the message number
137         out_key->size = sizeof(long);
138         out_key->data = reallok(out_key->data, out_key->size);
139         memcpy(out_key->data, &msgnum, out_key->size);
140
141         // The record data will be the decoded message text.
142         // We are allocating more memory than we need, but BDB will only write the number of bytes we tell it to.
143         out_data->data = reallok(out_data->data, strlen(token));
144         out_data->size = CtdlDecodeBase64(out_data->data, token, strlen(token));
145         return(1);
146 }
147
148
149 // Convert a "msgmeta" record to a message metadata record on disk.  NOT THREADSAFE
150 int convert_msgmeta(char *line, DBT *out_key, DBT *out_data) {
151         char *token;
152         struct MetaData *m = malloc(sizeof(struct MetaData));
153         token = strtok(line, "|");
154         m->meta_msgnum = atol(strtok(NULL, "|"));
155         m->meta_refcount = atoi(strtok(NULL, "|"));
156         strncpy(m->meta_content_type, strtok(NULL, "|"), sizeof(m->meta_content_type));
157         m->meta_rfc822_length = atol(strtok(NULL, "|"));
158
159         // metadata records are stored in the CDB_MSGMAIN table,
160         // but with the index being the *negative* of the message number.
161         long index = 0 - m->meta_msgnum;
162         out_key->size = sizeof(long);
163         out_key->data = reallok(NULL, out_key->size);
164         memcpy(out_key->data, &index, out_key->size);
165
166         // data
167         out_data->size = sizeof(struct MetaData);
168         out_data->data = m;                             // out_data owns this memory now
169
170         return(1);
171 }
172
173
174 // Ingest one line of dump data.  NOT REENTRANT
175 void ingest_one(char *line, DB_ENV *dst_dbenv) {
176
177         static int previous_cdb = -1 ;
178         static int current_cdb = -1 ;
179         static DB *dst_dbp;
180         char record_type[32];
181         int ret;
182         char dbfilename[32];
183         int row_was_good;
184         DBT out_key, out_data;
185
186         // We are assuming that the lines of the dump file will generally be sorted by table.
187         // By remembering the last table we worked with, we can do close/open if the table changes.
188
189         // Identify the record type we are currently working with
190         extract_token(record_type, line, 0, '|', sizeof record_type);
191         if (!strcasecmp(record_type, "msgtext"))                current_cdb = CDB_MSGMAIN;
192         else if (!strcasecmp(record_type, "msgmeta"))           current_cdb = CDB_MSGMAIN;
193         else if (!strcasecmp(record_type, "user"))              current_cdb = CDB_USERS;
194         else if (!strcasecmp(record_type, "room"))              current_cdb = CDB_ROOMS;
195         else if (!strcasecmp(record_type, "floor"))             current_cdb = CDB_FLOORTAB;
196         else if (!strcasecmp(record_type, "msglist"))           current_cdb = CDB_MSGLISTS;
197         else if (!strcasecmp(record_type, "visit"))             current_cdb = CDB_VISIT;
198         else if (!strcasecmp(record_type, "dir"))               current_cdb = CDB_DIRECTORY;
199         else if (!strcasecmp(record_type, "use"))               current_cdb = CDB_USETABLE;
200         else if (!strcasecmp(record_type, "bigmsg"))            current_cdb = CDB_BIGMSGS;
201         else if (!strcasecmp(record_type, "euidindex"))         current_cdb = CDB_EUIDINDEX;
202         else if (!strcasecmp(record_type, "usersbynumber"))     current_cdb = CDB_USERSBYNUMBER;
203         else if (!strcasecmp(record_type, "config"))            current_cdb = CDB_CONFIG;
204         else                                                    current_cdb = -1 ;
205
206         if (current_cdb != previous_cdb) {
207                 if (previous_cdb >= 0) {
208                         fprintf(stderr, "Close %d\n", previous_cdb);
209                         ret = dst_dbp->close(dst_dbp, 0);
210                         if (ret) {
211                                 fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
212                         }
213                 }
214
215                 if (current_cdb >= 0) {
216                         fprintf(stderr, " Open %d\n", current_cdb);
217                         snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", current_cdb);
218
219                         // create a database handle for the destination table
220                         ret = db_create(&dst_dbp, dst_dbenv, 0);
221                         if (ret) {
222                                 fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
223                                 fprintf(stderr, "db: exit code %d\n", ret);
224                                 exit(CTDLEXIT_DB);
225                         }
226                 
227                         // open the file containing the destination table
228                         ret = dst_dbp->open(dst_dbp, NULL, dbfilename, NULL, DB_BTREE, (DB_CREATE | DB_TRUNCATE), 0600);
229                         if (ret) {
230                                 fprintf(stderr, "db: db_open: %s\n", db_strerror(ret));
231                                 fprintf(stderr, "db: exit code %d\n", ret);
232                                 exit(CTDLEXIT_DB);
233                         }
234                 }
235
236                 previous_cdb = current_cdb;
237         }
238
239         // If we have a valid record type and a target database open, dispatch the correct record type handler.
240         memset(&out_key, 0, sizeof(DBT));
241         memset(&out_data, 0, sizeof(DBT));
242         row_was_good = 0;
243         if      (!strcasecmp(record_type, "msgtext"))           row_was_good = convert_msgtext(line, &out_key, &out_data);
244         else if (!strcasecmp(record_type, "msgmeta"))           row_was_good = convert_msgmeta(line, &out_key, &out_data);
245         else if (!strcasecmp(record_type, "user"))              row_was_good = convert_foo(line, &out_key, &out_data);
246         else if (!strcasecmp(record_type, "room"))              row_was_good = convert_foo(line, &out_key, &out_data);
247         else if (!strcasecmp(record_type, "floor"))             row_was_good = convert_foo(line, &out_key, &out_data);
248         else if (!strcasecmp(record_type, "msglist"))           row_was_good = convert_foo(line, &out_key, &out_data);
249         else if (!strcasecmp(record_type, "visit"))             row_was_good = convert_foo(line, &out_key, &out_data);
250         else if (!strcasecmp(record_type, "dir"))               row_was_good = convert_foo(line, &out_key, &out_data);
251         else if (!strcasecmp(record_type, "use"))               row_was_good = convert_foo(line, &out_key, &out_data);
252         else if (!strcasecmp(record_type, "bigmsg"))            row_was_good = convert_foo(line, &out_key, &out_data);
253         else if (!strcasecmp(record_type, "euidindex"))         row_was_good = convert_foo(line, &out_key, &out_data);
254         else if (!strcasecmp(record_type, "usersbynumber"))     row_was_good = convert_foo(line, &out_key, &out_data);
255         else if (!strcasecmp(record_type, "config"))            row_was_good = convert_foo(line, &out_key, &out_data);
256         else                                                    row_was_good = 0;
257
258         if (row_was_good) {
259                 ret = dst_dbp->put(dst_dbp, NULL, &out_key, &out_data, 0);
260                 if (ret) {
261                         fprintf(stderr, "db: cdb_put(%d): %s", current_cdb, db_strerror(ret));
262                         exit(CTDLEXIT_DB);
263                 }
264         }
265
266         free(out_key.data);
267         free(out_data.data);
268 }
269
270
271 // This is the loop that loads the dump data.  NOT REENTRANT
272 void ingest(DB_ENV *dst_dbenv) {
273         static size_t line_alloc = 1;
274         static char *line;
275         static size_t line_len = 0;
276         char ch;
277
278         line = reallok(NULL, line_alloc);
279
280         do {
281                 line_len = 0;
282
283                 while (ch = getc(stdin), ((ch != '\n') && (ch > 0))) {
284                         if ((line_len+2) > line_alloc) {
285                                 line_alloc *= 2;
286                                 line = reallok(line, line_alloc);
287                         }
288                         line[line_len++] = ch;
289                         line[line_len] = 0;
290                 }
291         
292                 if (ch <= 0) {
293                         return;
294                 }
295
296                 if (line_len > 0) {
297                         ingest_one(line, dst_dbenv);
298                 }
299
300         } while (ch >= 0);
301 }
302
303
304 // Main entry point
305 int main(int argc, char **argv) {
306         char *dst_dir = NULL;
307         int confirmed = 0;
308         static DB_ENV *dst_dbenv;               // Source DB environment (global)
309
310         // Parse command line
311         int a;
312         while ((a = getopt(argc, argv, "h:y")) != EOF) {
313                 switch (a) {
314                 case 'h':
315                         dst_dir = optarg;
316                         break;
317                 case 'y':
318                         confirmed = 1;
319                         break;
320                 default:
321                         fprintf(stderr, "%s: usage: %s -h dest_dir [<dumpfile]\n", argv[0], argv[0]);
322                         exit(2);
323                 }
324         }
325
326         if (confirmed == 1) {
327                 fprintf(stderr,"You have specified the [-y] flag, so processing will continue.\n");
328         }
329         else {
330                 fprintf(stderr,"Please read [ https://www.citadel.org/ctdlload.html ] to learn how to proceed.\n");
331                 exit(0);
332         }
333
334         char cmd[1024];
335         snprintf(cmd, sizeof cmd, "rm -fv %s/cdb.* %s/log.*", dst_dir, dst_dir);
336         system(cmd);
337
338         dst_dbenv = open_dbenv(dst_dir);
339         ingest(dst_dbenv);
340         close_dbenv(dst_dbenv);
341
342         exit(0);
343 }