]> code.citadel.org Git - citadel.git/blob - citadel/utils/ctdlload.c
Now I have a machine gun that can write ctdluser records. Ho-ho-ho.
[citadel.git] / citadel / utils / ctdlload.c
1 // Don't run this.  It doesn't work and if you try to run it you will immediately die.
2 //
3 // Copyright (c) 2023 by Art Cancro citadel.org
4 //
5 // This program is open source software.  Use, duplication, or disclosure
6 // is subject to the terms of the GNU General Public License, version 3.
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <ctype.h>
11 #include <stdio.h>
12 #include <signal.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <sys/un.h>
16 #include <netdb.h>
17 #include <string.h>
18 #include <pwd.h>
19 #include <errno.h>
20 #include <stdarg.h>
21 #include <limits.h>
22 #include <libcitadel.h>
23 #include <zlib.h>
24 #include <db.h>
25 #include "../server/sysdep.h"
26 #include "../server/citadel_defs.h"
27 #include "../server/server.h"
28 #include "../server/makeuserkey.h"
29 #include "../server/citadel_dirs.h"
30
31
32 // Wrapper for realloc() that crashes and burns if the call fails.
33 void *reallok(void *ptr, size_t size) {
34         void *p = realloc(ptr, size);
35         if (!p) {
36                 fprintf(stderr, "realloc() failed to resize %p to %ld bytes, error: %m\n", ptr, size);
37                 exit(1);
38         }
39         return p;
40 }
41
42
43 // Open a database environment
44 DB_ENV *open_dbenv(char *dirname) {
45
46         DB_ENV *dbenv = NULL;
47
48         int ret;
49         int i;
50         u_int32_t flags = 0;
51         int dbversion_major, dbversion_minor, dbversion_patch;
52
53         fprintf(stderr, "db: open_dbenv() starting\n"
54                 "db:    Linked zlib: %s\n"
55                 "db: Compiled libdb: %s\n"
56                 "db:   Linked libdb: %s\n",
57                 zlibVersion(),
58                 DB_VERSION_STRING,
59                 db_version(&dbversion_major, &dbversion_minor, &dbversion_patch)
60         );
61
62         // Create synthetic integer version numbers and compare them.
63         // Never run with a libdb older than the one with which it was compiled.
64         int compiled_db_version = ( (DB_VERSION_MAJOR * 1000000) + (DB_VERSION_MINOR * 1000) + (DB_VERSION_PATCH) );
65         int linked_db_version = ( (dbversion_major * 1000000) + (dbversion_minor * 1000) + (dbversion_patch) );
66         if (compiled_db_version > linked_db_version) {
67                 fprintf(stderr, "db: ctdlload is running with a version of libdb older than the one with which it was compiled.\n"
68                         "db: This is an invalid configuration.  ctdlload will now exit to prevent data loss.");
69                 exit(CTDLEXIT_DB);
70         }
71
72         fprintf(stderr,"db: Setting up DB environment\n");
73         ret = db_env_create(&dbenv, 0);
74         if (ret) {
75                 fprintf(stderr,"db: db_env_create: %s\n", db_strerror(ret));
76                 fprintf(stderr,"db: exit code %d\n", ret);
77                 exit(CTDLEXIT_DB);
78         }
79
80         // We want to specify the shared memory buffer pool cachesize, but everything else is the default.
81         ret = dbenv->set_cachesize(dbenv, 0, 64 * 1024, 0);
82         if (ret) {
83                 fprintf(stderr,"db: set_cachesize: %s\n", db_strerror(ret));
84                 dbenv->close(dbenv, 0);
85                 fprintf(stderr,"db: exit code %d\n", ret);
86                 exit(CTDLEXIT_DB);
87         }
88
89         if ((ret = dbenv->set_lk_detect(dbenv, DB_LOCK_DEFAULT))) {
90                 fprintf(stderr,"db: set_lk_detect: %s\n", db_strerror(ret));
91                 dbenv->close(dbenv, 0);
92                 fprintf(stderr,"db: exit code %d\n", ret);
93                 exit(CTDLEXIT_DB);
94         }
95
96         flags = DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_INIT_LOG;
97         fprintf(stderr,"db: dbenv open(dir=%s, flags=%d)\n", dirname, flags);
98         ret = dbenv->open(dbenv, dirname, flags, 0);
99         if (ret) {
100                 fprintf(stderr,"db: dbenv->open: %s\n", db_strerror(ret));
101                 dbenv->close(dbenv, 0);
102                 fprintf(stderr,"db: exit code %d\n", ret);
103                 exit(CTDLEXIT_DB);
104         }
105
106         return(dbenv);
107 }
108
109
110 void close_dbenv(DB_ENV *dbenv) {
111         fprintf(stderr,"db: closing dbenv\n");
112         int ret = dbenv->close(dbenv, 0);
113         if (ret) {
114                 fprintf(stderr,"db: dbenv->close: %s\n", db_strerror(ret));
115         }
116 }
117
118
119 // Skeleton convert function
120 int convert_foo(char *line, DBT *out_key, DBT *out_data) {
121         return(0);
122 }
123
124
125 // Convert a "msgtext" record to a message on disk.   NOT THREADSAFE
126 int convert_msgtext(char *line, DBT *out_key, DBT *out_data) {
127
128         static char *b64_decoded_msg = NULL;
129         static size_t b64_decoded_alloc = 0;
130         long msgnum;
131         char *token;
132
133         token = strtok(line, "|");
134         msgnum = atol(strtok(NULL, "|"));
135         token = strtok(NULL, "|");
136
137         // The record key will be the message number
138         out_key->size = sizeof(long);
139         out_key->data = reallok(out_key->data, out_key->size);
140         memcpy(out_key->data, &msgnum, out_key->size);
141
142         // The record data will be the decoded message text.
143         // We are allocating more memory than we need, but BDB will only write the number of bytes we tell it to.
144         out_data->data = reallok(out_data->data, strlen(token));
145         out_data->size = CtdlDecodeBase64(out_data->data, token, strlen(token));
146         return(1);
147 }
148
149
150 // Convert a "msgmeta" record to a message metadata record on disk.  NOT THREADSAFE
151 int convert_msgmeta(char *line, DBT *out_key, DBT *out_data) {
152         char *token;
153         struct MetaData *m = malloc(sizeof(struct MetaData));
154         token = strtok(line, "|");
155         m->meta_msgnum = atol(strtok(NULL, "|"));
156         m->meta_refcount = atoi(strtok(NULL, "|"));
157         strncpy(m->meta_content_type, strtok(NULL, "|"), sizeof(m->meta_content_type));
158         m->meta_rfc822_length = atol(strtok(NULL, "|"));
159
160         // metadata records are stored in the CDB_MSGMAIN table,
161         // but with the index being the *negative* of the message number.
162         long index = 0 - m->meta_msgnum;
163         out_key->size = sizeof(long);
164         out_key->data = reallok(NULL, out_key->size);
165         memcpy(out_key->data, &index, out_key->size);
166
167         // data
168         out_data->size = sizeof(struct MetaData);
169         out_data->data = m;                             // out_data owns this memory now
170
171         return(1);
172 }
173
174
175 // Convert a "user" record to a record on disk.  NOT THREADSAFE
176 int convert_user(char *line, DBT *out_key, DBT *out_data) {
177         char userkey[USERNAME_SIZE];
178         char *token;
179         struct ctdluser *u = malloc(sizeof(struct ctdluser));
180
181         memset(u, 0, sizeof(struct ctdluser));
182         char *p = line;
183
184         for (int i=0; (token = strsep(&p, "|")); ++i) {
185                 switch(i) {
186                         case 1:
187                                 u->version = atoi(token);
188                                 break;
189                         case 2:
190                                 u->uid = atoi(token);
191                                 break;
192                         case 3:
193                                 strncpy(u->password, token, sizeof(u->password));
194                                 break;
195                         case 4:
196                                 u->flags = atoi(token);
197                                 break;
198                         case 5:
199                                 u->axlevel = atoi(token);
200                                 break;
201                         case 6:
202                                 u->usernum = atol(token);
203                                 break;
204                         case 7:
205                                 u->lastcall = atol(token);
206                                 break;
207                         case 8:
208                                 u->USuserpurge = atoi(token);
209                                 break;
210                         case 9:
211                                 strncpy(u->fullname, token, sizeof(u->fullname));
212                                 break;
213                         case 10:
214                                 u->msgnum_bio = atol(token);
215                                 break;
216                         case 11:
217                                 u->msgnum_pic = atol(token);
218                                 break;
219                         case 12:
220                                 CtdlDecodeBase64(u->emailaddrs, token, strlen(token));
221                                 break;
222                         case 13:
223                                 u->msgnum_inboxrules = atol(token);
224                                 break;
225                         case 14:
226                                 u->lastproc_inboxrules = atol(token);
227                                 break;
228                 }
229         }
230         
231         makeuserkey(userkey, u->fullname);
232         out_key->size = strlen(userkey);
233         out_key->data = strdup(userkey);
234         out_data->size = sizeof(struct ctdluser);
235         out_data->data = u;
236         return(1);
237 }
238
239
240 // Ingest one line of dump data.  NOT REENTRANT
241 void ingest_one(char *line, DB_ENV *dst_dbenv) {
242
243         static int previous_cdb = -1 ;
244         static int current_cdb = -1 ;
245         static DB *dst_dbp;
246         char record_type[32];
247         int ret;
248         char dbfilename[32];
249         int row_was_good;
250         DBT out_key, out_data;
251
252         // We are assuming that the lines of the dump file will generally be sorted by table.
253         // By remembering the last table we worked with, we can do close/open if the table changes.
254
255         // Identify the record type we are currently working with
256         extract_token(record_type, line, 0, '|', sizeof record_type);
257         if (!strcasecmp(record_type, "msgtext"))                current_cdb = CDB_MSGMAIN;
258         else if (!strcasecmp(record_type, "msgmeta"))           current_cdb = CDB_MSGMAIN;
259         else if (!strcasecmp(record_type, "user"))              current_cdb = CDB_USERS;
260         else if (!strcasecmp(record_type, "room"))              current_cdb = CDB_ROOMS;
261         else if (!strcasecmp(record_type, "floor"))             current_cdb = CDB_FLOORTAB;
262         else if (!strcasecmp(record_type, "msglist"))           current_cdb = CDB_MSGLISTS;
263         else if (!strcasecmp(record_type, "visit"))             current_cdb = CDB_VISIT;
264         else if (!strcasecmp(record_type, "dir"))               current_cdb = CDB_DIRECTORY;
265         else if (!strcasecmp(record_type, "use"))               current_cdb = CDB_USETABLE;
266         else if (!strcasecmp(record_type, "bigmsg"))            current_cdb = CDB_BIGMSGS;
267         else if (!strcasecmp(record_type, "euidindex"))         current_cdb = CDB_EUIDINDEX;
268         else if (!strcasecmp(record_type, "usersbynumber"))     current_cdb = CDB_USERSBYNUMBER;
269         else if (!strcasecmp(record_type, "config"))            current_cdb = CDB_CONFIG;
270         else                                                    current_cdb = -1 ;
271
272         if (current_cdb != previous_cdb) {
273                 if (previous_cdb >= 0) {
274                         ret = dst_dbp->close(dst_dbp, 0);
275                         if (ret) {
276                                 fprintf(stderr, "db: db_close: %s\n", db_strerror(ret));
277                         }
278                 }
279
280                 if (current_cdb >= 0) {
281                         snprintf(dbfilename, sizeof dbfilename, "cdb.%02x", current_cdb);
282
283                         // create a database handle for the destination table
284                         ret = db_create(&dst_dbp, dst_dbenv, 0);
285                         if (ret) {
286                                 fprintf(stderr, "db: db_create: %s\n", db_strerror(ret));
287                                 fprintf(stderr, "db: exit code %d\n", ret);
288                                 exit(CTDLEXIT_DB);
289                         }
290                 
291                         // open the file containing the destination table
292                         ret = dst_dbp->open(dst_dbp, NULL, dbfilename, NULL, DB_BTREE, (DB_CREATE | DB_TRUNCATE), 0600);
293                         if (ret) {
294                                 fprintf(stderr, "db: db_open: %s\n", db_strerror(ret));
295                                 fprintf(stderr, "db: exit code %d\n", ret);
296                                 exit(CTDLEXIT_DB);
297                         }
298                 }
299
300                 previous_cdb = current_cdb;
301         }
302
303         // If we have a valid record type and a target database open, dispatch the correct record type handler.
304         memset(&out_key, 0, sizeof(DBT));
305         memset(&out_data, 0, sizeof(DBT));
306         row_was_good = 0;
307         if      (!strcasecmp(record_type, "msgtext"))           row_was_good = convert_msgtext(line, &out_key, &out_data);
308         else if (!strcasecmp(record_type, "msgmeta"))           row_was_good = convert_msgmeta(line, &out_key, &out_data);
309         else if (!strcasecmp(record_type, "user"))              row_was_good = convert_user(line, &out_key, &out_data);
310         else if (!strcasecmp(record_type, "room"))              row_was_good = convert_foo(line, &out_key, &out_data);
311         else if (!strcasecmp(record_type, "floor"))             row_was_good = convert_foo(line, &out_key, &out_data);
312         else if (!strcasecmp(record_type, "msglist"))           row_was_good = convert_foo(line, &out_key, &out_data);
313         else if (!strcasecmp(record_type, "visit"))             row_was_good = convert_foo(line, &out_key, &out_data);
314         else if (!strcasecmp(record_type, "dir"))               row_was_good = convert_foo(line, &out_key, &out_data);
315         else if (!strcasecmp(record_type, "use"))               row_was_good = convert_foo(line, &out_key, &out_data);
316         else if (!strcasecmp(record_type, "bigmsg"))            row_was_good = convert_foo(line, &out_key, &out_data);
317         else if (!strcasecmp(record_type, "euidindex"))         row_was_good = convert_foo(line, &out_key, &out_data);
318         else if (!strcasecmp(record_type, "usersbynumber"))     row_was_good = convert_foo(line, &out_key, &out_data);
319         else if (!strcasecmp(record_type, "config"))            row_was_good = convert_foo(line, &out_key, &out_data);
320         else                                                    row_was_good = 0;
321
322         if (row_was_good) {
323                 ret = dst_dbp->put(dst_dbp, NULL, &out_key, &out_data, 0);
324                 if (ret) {
325                         fprintf(stderr, "db: cdb_put(%d): %s", current_cdb, db_strerror(ret));
326                         exit(CTDLEXIT_DB);
327                 }
328         }
329
330         free(out_key.data);
331         free(out_data.data);
332 }
333
334
335 // This is the loop that loads the dump data.  NOT REENTRANT
336 void ingest(DB_ENV *dst_dbenv) {
337         static size_t line_alloc = 1;
338         static char *line;
339         static size_t line_len = 0;
340         char ch;
341
342         line = reallok(NULL, line_alloc);
343
344         do {
345                 line_len = 0;
346
347                 while (ch = getc(stdin), ((ch != '\n') && (ch > 0))) {
348                         if ((line_len+2) > line_alloc) {
349                                 line_alloc *= 2;
350                                 line = reallok(line, line_alloc);
351                         }
352                         line[line_len++] = ch;
353                         line[line_len] = 0;
354                 }
355         
356                 if (ch <= 0) {
357                         return;
358                 }
359
360                 if (line_len > 0) {
361                         ingest_one(line, dst_dbenv);
362                 }
363
364         } while (ch >= 0);
365 }
366
367
368 // Main entry point
369 int main(int argc, char **argv) {
370         char *dst_dir = NULL;
371         int confirmed = 0;
372         static DB_ENV *dst_dbenv;               // Source DB environment (global)
373
374         // Parse command line
375         int a;
376         while ((a = getopt(argc, argv, "h:y")) != EOF) {
377                 switch (a) {
378                 case 'h':
379                         dst_dir = optarg;
380                         break;
381                 case 'y':
382                         confirmed = 1;
383                         break;
384                 default:
385                         fprintf(stderr, "%s: usage: %s -h dest_dir [<dumpfile]\n", argv[0], argv[0]);
386                         exit(2);
387                 }
388         }
389
390         if (confirmed == 1) {
391                 fprintf(stderr,"You have specified the [-y] flag, so processing will continue.\n");
392         }
393         else {
394                 fprintf(stderr,"Please read [ https://www.citadel.org/ctdlload.html ] to learn how to proceed.\n");
395                 exit(0);
396         }
397
398         char cmd[1024];
399         snprintf(cmd, sizeof cmd, "rm -fv %s/cdb.* %s/log.*", dst_dir, dst_dir);
400         system(cmd);
401
402         dst_dbenv = open_dbenv(dst_dir);
403         ingest(dst_dbenv);
404         close_dbenv(dst_dbenv);
405
406         exit(0);
407 }