b8ed51e07d064a36fe118e9331dc0791ac3ae09f
[citadel.git] / citadel / serv_vandelay.c
1 /*
2  * $Id$
3  *
4  * This is the "Art Vandelay" module.  It is an importer/exporter.
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <pwd.h>
15 #include <errno.h>
16 #include <sys/types.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include <sys/wait.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <limits.h>
33 #include "citadel.h"
34 #include "server.h"
35 #include "sysdep_decls.h"
36 #include "citserver.h"
37 #include "support.h"
38 #include "config.h"
39 #include "serv_extensions.h"
40 #include "database.h"
41 #include "msgbase.h"
42 #include "tools.h"
43 #include "user_ops.h"
44 #include "room_ops.h"
45 #include "control.h"
46
47 #define END_OF_MESSAGE  "---eom---dbd---"
48
49 char artv_tempfilename1[PATH_MAX];
50 char artv_tempfilename2[PATH_MAX];
51 FILE *artv_global_message_list;
52
53 void artv_export_users_backend(struct ctdluser *usbuf, void *data) {
54         cprintf("user\n");
55         cprintf("%d\n", usbuf->version);
56         cprintf("%ld\n", (long)usbuf->uid);
57         cprintf("%s\n", usbuf->password);
58         cprintf("%u\n", usbuf->flags);
59         cprintf("%ld\n", usbuf->timescalled);
60         cprintf("%ld\n", usbuf->posted);
61         cprintf("%d\n", usbuf->axlevel);
62         cprintf("%ld\n", usbuf->usernum);
63         cprintf("%ld\n", (long)usbuf->lastcall);
64         cprintf("%d\n", usbuf->USuserpurge);
65         cprintf("%s\n", usbuf->fullname);
66         cprintf("%d\n", usbuf->USscreenwidth);
67         cprintf("%d\n", usbuf->USscreenheight);
68 }
69
70
71 void artv_export_users(void) {
72         ForEachUser(artv_export_users_backend, NULL);
73 }
74
75
76 void artv_export_room_msg(long msgnum, void *userdata) {
77         cprintf("%ld\n", msgnum);
78         fprintf(artv_global_message_list, "%ld\n", msgnum);
79 }
80
81
82 void artv_export_rooms_backend(struct ctdlroom *qrbuf, void *data) {
83         cprintf("room\n");
84         cprintf("%s\n", qrbuf->QRname);
85         cprintf("%s\n", qrbuf->QRpasswd);
86         cprintf("%ld\n", qrbuf->QRroomaide);
87         cprintf("%ld\n", qrbuf->QRhighest);
88         cprintf("%ld\n", (long)qrbuf->QRgen);
89         cprintf("%u\n", qrbuf->QRflags);
90         cprintf("%s\n", qrbuf->QRdirname);
91         cprintf("%ld\n", qrbuf->QRinfo);
92         cprintf("%d\n", qrbuf->QRfloor);
93         cprintf("%ld\n", (long)qrbuf->QRmtime);
94         cprintf("%d\n", qrbuf->QRep.expire_mode);
95         cprintf("%d\n", qrbuf->QRep.expire_value);
96         cprintf("%ld\n", qrbuf->QRnumber);
97         cprintf("%d\n", qrbuf->QRorder);
98         cprintf("%u\n", qrbuf->QRflags2);
99         cprintf("%d\n", qrbuf->QRdefaultview);
100
101         getroom(&CC->room, qrbuf->QRname);
102         /* format of message list export is all message numbers output
103          * one per line terminated by a 0.
104          */
105         CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL,
106                 artv_export_room_msg, NULL);
107         cprintf("0\n");
108
109 }
110
111
112
113 void artv_export_rooms(void) {
114         char cmd[SIZ];
115         artv_global_message_list = fopen(artv_tempfilename1, "w");
116         if (artv_global_message_list != NULL) {
117                 ForEachRoom(artv_export_rooms_backend, NULL);
118                 fclose(artv_global_message_list);
119         }
120
121         /*
122          * Process the 'global' message list.  (Sort it and remove dups.
123          * Dups are ok because a message may be in more than one room, but
124          * this will be handled by exporting the reference count, not by
125          * exporting the message multiple times.)
126          */
127         snprintf(cmd, sizeof cmd, "sort <%s >%s", artv_tempfilename1, artv_tempfilename2);
128         system(cmd);
129         snprintf(cmd, sizeof cmd, "uniq <%s >%s", artv_tempfilename2, artv_tempfilename1);
130         system(cmd);
131 }
132
133
134 void artv_export_floors(void) {
135         struct floor flbuf;
136         int i;
137
138         for (i=0; i < MAXFLOORS; ++i) {
139                 cprintf("floor\n");
140                 cprintf("%d\n", i);
141                 getfloor(&flbuf, i);
142                 cprintf("%u\n", flbuf.f_flags);
143                 cprintf("%s\n", flbuf.f_name);
144                 cprintf("%d\n", flbuf.f_ref_count);
145                 cprintf("%d\n", flbuf.f_ep.expire_mode);
146                 cprintf("%d\n", flbuf.f_ep.expire_value);
147         }
148 }
149
150
151
152
153
154 /* 
155  *  Traverse the visits file...
156  */
157 void artv_export_visits(void) {
158         struct visit vbuf;
159         struct cdbdata *cdbv;
160
161         cdb_rewind(CDB_VISIT);
162
163         while (cdbv = cdb_next_item(CDB_VISIT), cdbv != NULL) {
164                 memset(&vbuf, 0, sizeof(struct visit));
165                 memcpy(&vbuf, cdbv->ptr,
166                        ((cdbv->len > sizeof(struct visit)) ?
167                         sizeof(struct visit) : cdbv->len));
168                 cdb_free(cdbv);
169
170                 cprintf("visit\n");
171                 cprintf("%ld\n", vbuf.v_roomnum);
172                 cprintf("%ld\n", vbuf.v_roomgen);
173                 cprintf("%ld\n", vbuf.v_usernum);
174
175                 if (strlen(vbuf.v_seen) > 0) {
176                         cprintf("%s\n", vbuf.v_seen);
177                 }
178                 else {
179                         cprintf("%ld\n", vbuf.v_lastseen);
180                 }
181
182                 cprintf("%s\n", vbuf.v_answered);
183                 cprintf("%u\n", vbuf.v_flags);
184                 cprintf("%d\n", vbuf.v_view);
185         }
186 }
187
188
189 void artv_export_message(long msgnum) {
190         struct MetaData smi;
191         struct CtdlMessage *msg;
192         struct ser_ret smr;
193         FILE *fp;
194         char buf[SIZ];
195         char tempfile[SIZ];
196
197         msg = CtdlFetchMessage(msgnum, 1);
198         if (msg == NULL) return;        /* fail silently */
199
200         cprintf("message\n");
201         GetMetaData(&smi, msgnum);
202         cprintf("%ld\n", msgnum);
203         cprintf("%d\n", smi.meta_refcount);
204         cprintf("%s\n", smi.meta_content_type);
205
206         serialize_message(&smr, msg);
207         CtdlFreeMessage(msg);
208
209         /* write it in base64 */
210         strcpy(tempfile, tmpnam(NULL));
211         snprintf(buf, sizeof buf, "./base64 -e >%s", tempfile);
212         fp = popen(buf, "w");
213         fwrite(smr.ser, smr.len, 1, fp);
214         pclose(fp);
215
216         free(smr.ser);
217
218         fp = fopen(tempfile, "r");
219         unlink(tempfile);
220         if (fp != NULL) {
221                 while (fgets(buf, sizeof(buf), fp) != NULL) {
222                         buf[strlen(buf)-1] = 0;
223                         cprintf("%s\n", buf);
224                 }
225                 fclose(fp);
226         }
227         cprintf("%s\n", END_OF_MESSAGE);
228 }
229
230
231
232 void artv_export_messages(void) {
233         char buf[SIZ];
234         long msgnum;
235         int count = 0;
236
237         artv_global_message_list = fopen(artv_tempfilename1, "r");
238         if (artv_global_message_list != NULL) {
239                 lprintf(CTDL_INFO, "Opened %s\n", artv_tempfilename1);
240                 while (fgets(buf, sizeof(buf),
241                       artv_global_message_list) != NULL) {
242                         msgnum = atol(buf);
243                         if (msgnum > 0L) {
244                                 artv_export_message(msgnum);
245                                 ++count;
246                         }
247                 }
248                 fclose(artv_global_message_list);
249         }
250         lprintf(CTDL_INFO, "Exported %d messages.\n", count);
251 }
252
253
254
255
256 void artv_do_export(void) {
257         cprintf("%d Exporting all Citadel databases.\n", LISTING_FOLLOWS);
258
259         cprintf("version\n%d\n", REV_LEVEL);
260
261         /* export the config file */
262         cprintf("config\n");
263         cprintf("%s\n", config.c_nodename);
264         cprintf("%s\n", config.c_fqdn);
265         cprintf("%s\n", config.c_humannode);
266         cprintf("%s\n", config.c_phonenum);
267         cprintf("%ld\n", (long)config.c_bbsuid);
268         cprintf("%d\n", config.c_creataide);
269         cprintf("%d\n", config.c_sleeping);
270         cprintf("%d\n", config.c_initax);
271         cprintf("%d\n", config.c_regiscall);
272         cprintf("%d\n", config.c_twitdetect);
273         cprintf("%s\n", config.c_twitroom);
274         cprintf("%s\n", config.c_moreprompt);
275         cprintf("%d\n", config.c_restrict);
276         cprintf("%s\n", config.c_bbs_city);
277         cprintf("%s\n", config.c_sysadm);
278         cprintf("%d\n", config.c_setup_level);
279         cprintf("%d\n", config.c_maxsessions);
280         cprintf("%d\n", config.c_port_number);
281         cprintf("%d\n", config.c_ep.expire_mode);
282         cprintf("%d\n", config.c_ep.expire_value);
283         cprintf("%d\n", config.c_userpurge);
284         cprintf("%d\n", config.c_roompurge);
285         cprintf("%s\n", config.c_logpages);
286         cprintf("%d\n", config.c_createax);
287         cprintf("%ld\n", config.c_maxmsglen);
288         cprintf("%d\n", config.c_min_workers);
289         cprintf("%d\n", config.c_max_workers);
290         cprintf("%d\n", config.c_pop3_port);
291         cprintf("%d\n", config.c_smtp_port);
292         cprintf("%d\n", config.c_purge_hour);
293         cprintf("%d\n", config.c_mbxep.expire_mode);
294         cprintf("%d\n", config.c_mbxep.expire_value);
295         cprintf("%s\n", config.c_ldap_host);
296         cprintf("%d\n", config.c_ldap_port);
297         cprintf("%s\n", config.c_ldap_base_dn);
298         cprintf("%s\n", config.c_ldap_bind_dn);
299         cprintf("%s\n", config.c_ldap_bind_pw);
300         cprintf("%s\n", config.c_ip_addr);
301
302         /* Export the control file */
303         get_control();
304         cprintf("control\n");
305         cprintf("%ld\n", CitControl.MMhighest);
306         cprintf("%u\n", CitControl.MMflags);
307         cprintf("%ld\n", CitControl.MMnextuser);
308         cprintf("%ld\n", CitControl.MMnextroom);
309         cprintf("%d\n", CitControl.version);
310
311         artv_export_users();
312         artv_export_rooms();
313         artv_export_floors();
314         artv_export_visits();
315         artv_export_messages();
316
317         cprintf("000\n");
318 }
319
320
321
322 void artv_import_config(void) {
323         char buf[SIZ];
324
325         lprintf(CTDL_DEBUG, "Importing config file\n");
326         client_gets(config.c_nodename);
327         lprintf(CTDL_DEBUG, "c_nodename = %s\n", config.c_nodename);
328         client_gets(config.c_fqdn);
329         client_gets(config.c_humannode);
330         client_gets(config.c_phonenum);
331         client_gets(buf);       config.c_bbsuid = atoi(buf);
332         client_gets(buf);       config.c_creataide = atoi(buf);
333         client_gets(buf);       config.c_sleeping = atoi(buf);
334         client_gets(buf);       config.c_initax = atoi(buf);
335         client_gets(buf);       config.c_regiscall = atoi(buf);
336         client_gets(buf);       config.c_twitdetect = atoi(buf);
337         client_gets(config.c_twitroom);
338         client_gets(config.c_moreprompt);
339         client_gets(buf);       config.c_restrict = atoi(buf);
340         client_gets(config.c_bbs_city);
341         client_gets(config.c_sysadm);
342         lprintf(CTDL_DEBUG, "c_sysadm = %s\n", config.c_sysadm);
343         client_gets(buf);       config.c_setup_level = atoi(buf);
344         client_gets(buf);       config.c_maxsessions = atoi(buf);
345         client_gets(buf);       config.c_port_number = atoi(buf);
346         client_gets(buf);       config.c_ep.expire_mode = atoi(buf);
347         client_gets(buf);       config.c_ep.expire_value = atoi(buf);
348         client_gets(buf);       config.c_userpurge = atoi(buf);
349         client_gets(buf);       config.c_roompurge = atoi(buf);
350         client_gets(config.c_logpages);
351         client_gets(buf);       config.c_createax = atoi(buf);
352         client_gets(buf);       config.c_maxmsglen = atol(buf);
353         client_gets(buf);       config.c_min_workers = atoi(buf);
354         client_gets(buf);       config.c_max_workers = atoi(buf);
355         client_gets(buf);       config.c_pop3_port = atoi(buf);
356         client_gets(buf);       config.c_smtp_port = atoi(buf);
357         client_gets(buf);       config.c_purge_hour = atoi(buf);
358         client_gets(buf);       config.c_mbxep.expire_mode = atoi(buf);
359         client_gets(buf);       config.c_mbxep.expire_value = atoi(buf);
360         client_gets(config.c_ldap_host);
361         client_gets(buf);       config.c_ldap_port = atoi(buf);
362         client_gets(config.c_ldap_base_dn);
363         client_gets(config.c_ldap_bind_dn);
364         client_gets(config.c_ldap_bind_pw);
365         client_gets(config.c_ip_addr);
366         put_config();
367         lprintf(CTDL_INFO, "Imported config file\n");
368 }
369
370
371
372 void artv_import_control(void) {
373         char buf[SIZ];
374
375         lprintf(CTDL_DEBUG, "Importing control file\n");
376         client_gets(buf);       CitControl.MMhighest = atol(buf);
377         client_gets(buf);       CitControl.MMflags = atoi(buf);
378         client_gets(buf);       CitControl.MMnextuser = atol(buf);
379         client_gets(buf);       CitControl.MMnextroom = atol(buf);
380         client_gets(buf);       CitControl.version = atoi(buf);
381         put_control();
382         lprintf(CTDL_INFO, "Imported control file\n");
383 }
384
385
386 void artv_import_user(void) {
387         char buf[SIZ];
388         struct ctdluser usbuf;
389
390         client_gets(buf);       usbuf.version = atoi(buf);
391         client_gets(buf);       usbuf.uid = atoi(buf);
392         client_gets(usbuf.password);
393         client_gets(buf);       usbuf.flags = atoi(buf);
394         client_gets(buf);       usbuf.timescalled = atol(buf);
395         client_gets(buf);       usbuf.posted = atol(buf);
396         client_gets(buf);       usbuf.axlevel = atoi(buf);
397         client_gets(buf);       usbuf.usernum = atol(buf);
398         client_gets(buf);       usbuf.lastcall = atol(buf);
399         client_gets(buf);       usbuf.USuserpurge = atoi(buf);
400         client_gets(usbuf.fullname);
401         client_gets(buf);       usbuf.USscreenwidth = atoi(buf);
402         client_gets(buf);       usbuf.USscreenheight = atoi(buf);
403         putuser(&usbuf);
404 }
405
406
407 void artv_import_room(void) {
408         char buf[SIZ];
409         struct ctdlroom qrbuf;
410         long msgnum;
411         int msgcount = 0;
412
413         client_gets(qrbuf.QRname);
414         client_gets(qrbuf.QRpasswd);
415         client_gets(buf);       qrbuf.QRroomaide = atol(buf);
416         client_gets(buf);       qrbuf.QRhighest = atol(buf);
417         client_gets(buf);       qrbuf.QRgen = atol(buf);
418         client_gets(buf);       qrbuf.QRflags = atoi(buf);
419         client_gets(qrbuf.QRdirname);
420         client_gets(buf);       qrbuf.QRinfo = atol(buf);
421         client_gets(buf);       qrbuf.QRfloor = atoi(buf);
422         client_gets(buf);       qrbuf.QRmtime = atol(buf);
423         client_gets(buf);       qrbuf.QRep.expire_mode = atoi(buf);
424         client_gets(buf);       qrbuf.QRep.expire_value = atoi(buf);
425         client_gets(buf);       qrbuf.QRnumber = atol(buf);
426         client_gets(buf);       qrbuf.QRorder = atoi(buf);
427         client_gets(buf);       qrbuf.QRflags2 = atoi(buf);
428         client_gets(buf);       qrbuf.QRdefaultview = atoi(buf);
429         putroom(&qrbuf);
430         lprintf(CTDL_INFO, "Imported room <%s>\n", qrbuf.QRname);
431         /* format of message list export is all message numbers output
432          * one per line terminated by a 0.
433          */
434         while (client_gets(buf), msgnum = atol(buf), msgnum > 0) {
435                 CtdlSaveMsgPointerInRoom(qrbuf.QRname, msgnum, 0);
436                 ++msgcount;
437         }
438         lprintf(CTDL_INFO, "(%d messages)\n", msgcount);
439 }
440
441
442 void artv_import_floor(void) {
443         struct floor flbuf;
444         int i;
445         char buf[SIZ];
446
447         client_gets(buf);               i = atoi(buf);
448         client_gets(buf);               flbuf.f_flags = atoi(buf);
449         client_gets(flbuf.f_name);
450         client_gets(buf);               flbuf.f_ref_count = atoi(buf);
451         client_gets(buf);               flbuf.f_ep.expire_mode = atoi(buf);
452         client_gets(buf);               flbuf.f_ep.expire_value = atoi(buf);
453         putfloor(&flbuf, i);
454         lprintf(CTDL_INFO, "Imported floor #%d (%s)\n", i, flbuf.f_name);
455 }
456
457
458 /* 
459  */
460 void artv_import_visit(void) {
461         struct visit vbuf;
462         char buf[SIZ];
463         int i;
464         int is_textual_seen = 0;
465
466         client_gets(buf);       vbuf.v_roomnum = atol(buf);
467         client_gets(buf);       vbuf.v_roomgen = atol(buf);
468         client_gets(buf);       vbuf.v_usernum = atol(buf);
469
470         client_gets(buf);
471         vbuf.v_lastseen = atol(buf);
472         for (i=0; i<strlen(buf); ++i) if (!isdigit(buf[i])) is_textual_seen = 1;
473         if (is_textual_seen)    strcpy(vbuf.v_seen, buf);
474
475         client_gets(vbuf.v_answered);
476         client_gets(buf);       vbuf.v_flags = atoi(buf);
477         client_gets(buf);       vbuf.v_view = atoi(buf);
478         put_visit(&vbuf);
479         lprintf(CTDL_INFO, "Imported visit %ld/%ld/%ld\n",
480                 vbuf.v_roomnum, vbuf.v_roomgen, vbuf.v_usernum);
481 }
482
483
484
485 void artv_import_message(void) {
486         struct MetaData smi;
487         long msgnum;
488         long msglen;
489         FILE *fp;
490         char buf[SIZ];
491         char tempfile[SIZ];
492         char *mbuf;
493
494         memset(&smi, 0, sizeof(struct MetaData));
495         client_gets(buf);       msgnum = atol(buf);
496                                 smi.meta_msgnum = msgnum;
497         client_gets(buf);       smi.meta_refcount = atoi(buf);
498         client_gets(smi.meta_content_type);
499
500         lprintf(CTDL_INFO, "message #%ld\n", msgnum);
501
502         /* decode base64 message text */
503         strcpy(tempfile, tmpnam(NULL));
504         snprintf(buf, sizeof buf, "./base64 -d >%s", tempfile);
505         fp = popen(buf, "w");
506         while (client_gets(buf), strcasecmp(buf, END_OF_MESSAGE)) {
507                 fprintf(fp, "%s\n", buf);
508         }
509         pclose(fp);
510         fp = fopen(tempfile, "rb");
511         fseek(fp, 0L, SEEK_END);
512         msglen = ftell(fp);
513         fclose(fp);
514         lprintf(CTDL_DEBUG, "msglen = %ld\n", msglen);
515
516         mbuf = malloc(msglen);
517         fp = fopen(tempfile, "rb");
518         fread(mbuf, msglen, 1, fp);
519         fclose(fp);
520
521         cdb_store(CDB_MSGMAIN, &msgnum, sizeof(long), mbuf, msglen);
522
523         free(mbuf);
524         unlink(tempfile);
525
526         PutMetaData(&smi);
527         lprintf(CTDL_INFO, "Imported message %ld\n", msgnum);
528 }
529
530
531
532
533 void artv_do_import(void) {
534         char buf[SIZ];
535         char s_version[SIZ];
536         int version;
537
538         cprintf("%d sock it to me\n", SEND_LISTING);
539         while (client_gets(buf), strcmp(buf, "000")) {
540
541                 lprintf(CTDL_DEBUG, "import keyword: <%s>\n", buf);
542
543                 if (!strcasecmp(buf, "version")) {
544                         client_gets(s_version);
545                         version = atoi(s_version);
546                         if ((version<EXPORT_REV_MIN) || (version>REV_LEVEL)) {
547                                 lprintf(CTDL_ERR, "Version mismatch in ARTV import; aborting\n");
548                                 break;
549                         }
550                 }
551                 else if (!strcasecmp(buf, "config")) artv_import_config();
552                 else if (!strcasecmp(buf, "control")) artv_import_control();
553                 else if (!strcasecmp(buf, "user")) artv_import_user();
554                 else if (!strcasecmp(buf, "room")) artv_import_room();
555                 else if (!strcasecmp(buf, "floor")) artv_import_floor();
556                 else if (!strcasecmp(buf, "visit")) artv_import_visit();
557                 else if (!strcasecmp(buf, "message")) artv_import_message();
558                 else break;
559
560         }
561         lprintf(CTDL_INFO, "Invalid keyword <%s>.  Flushing input.\n", buf);
562         while (client_gets(buf), strcmp(buf, "000"))  ;;
563 }
564
565
566
567 void cmd_artv(char *cmdbuf) {
568         char cmd[SIZ];
569         static int is_running = 0;
570
571         if (CtdlAccessCheck(ac_internal)) return;
572         if (is_running) {
573                 cprintf("%d The importer/exporter is already running.\n",
574                         ERROR + RESOURCE_BUSY);
575                 return;
576         }
577         is_running = 1;
578
579         strcpy(artv_tempfilename1, tmpnam(NULL));
580         strcpy(artv_tempfilename2, tmpnam(NULL));
581
582         extract(cmd, cmdbuf, 0);
583         if (!strcasecmp(cmd, "export")) artv_do_export();
584         else if (!strcasecmp(cmd, "import")) artv_do_import();
585         else cprintf("%d illegal command\n", ERROR + ILLEGAL_VALUE);
586
587         unlink(artv_tempfilename1);
588         unlink(artv_tempfilename2);
589
590         is_running = 0;
591 }
592
593
594
595
596 char *serv_vandelay_init(void)
597 {
598         CtdlRegisterProtoHook(cmd_artv, "ARTV", "import/export data store");
599         return "$Id$";
600 }