]> code.citadel.org Git - citadel.git/blob - citadel/routines2.c
* routines2.c: fixed an off-by-one error in filename sanitization for
[citadel.git] / citadel / routines2.c
1 /* $Id$
2  *
3  * More client-side support functions.
4  * Unlike routines.c, some of these DO use global variables.
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <ctype.h>
16 #include <string.h>
17 #include <limits.h>
18
19 #if TIME_WITH_SYS_TIME
20 # include <sys/time.h>
21 # include <time.h>
22 #else
23 # if HAVE_SYS_TIME_H
24 #  include <sys/time.h>
25 # else
26 #  include <time.h>
27 # endif
28 #endif
29
30 #include <signal.h>
31 #include <pwd.h>
32 #include <errno.h>
33 #include <stdarg.h>
34 #include "citadel.h"
35 #include "citadel_ipc.h"
36 #include "citadel_decls.h"
37 #include "routines2.h"
38 #include "routines.h"
39 #include "commands.h"
40 #include "tools.h"
41 #include "messages.h"
42 #ifndef HAVE_SNPRINTF
43 #include "snprintf.h"
44 #endif
45 #include "screen.h"
46
47 /* work around solaris include files */
48 #ifdef reg
49 #undef reg
50 #endif
51
52 extern char temp[];
53 extern char tempdir[];
54 extern char *axdefs[7];
55 extern long highest_msg_read;
56 extern long maxmsgnum;
57 extern unsigned room_flags;
58 extern int screenwidth;
59
60
61 /*
62 int eopen(char *name, int mode)
63 {
64         int ret;
65         ret = open(name, mode);
66         if (ret < 0) {
67                 err_printf("Cannot open file '%s', mode=%d, errno=%d\n",
68                         name, mode, errno);
69                 interr(errno);
70         }
71         return (ret);
72 }
73 */
74
75
76 int room_prompt(unsigned int qrflags)
77 {                               /* return proper room prompt character */
78         int a;
79         a = '>';
80         if (qrflags & QR_DIRECTORY)
81                 a = ']';
82         if ((a == ']') && (qrflags & QR_NETWORK))
83                 a = '}';
84         if ((a == '>') && (qrflags & QR_NETWORK))
85                 a = ')';
86         return (a);
87 }
88
89 void entregis(CtdlIPC *ipc)
90 {                               /* register with name and address */
91
92         char buf[SIZ];
93         char tmpname[SIZ];
94         char tmpaddr[SIZ];
95         char tmpcity[SIZ];
96         char tmpstate[SIZ];
97         char tmpzip[SIZ];
98         char tmpphone[SIZ];
99         char tmpemail[SIZ];
100         char tmpcountry[SIZ];
101         char diruser[SIZ];
102         char dirnode[SIZ];
103         char holdemail[SIZ];
104         char *reg = NULL;
105         int ok = 0;
106         int r;                          /* IPC response code */
107
108         strcpy(tmpname, "");
109         strcpy(tmpaddr, "");
110         strcpy(tmpcity, "");
111         strcpy(tmpstate, "");
112         strcpy(tmpzip, "");
113         strcpy(tmpphone, "");
114         strcpy(tmpemail, "");
115         strcpy(tmpcountry, "");
116
117         r = CtdlIPCGetUserRegistration(ipc, NULL, &reg, buf);
118         if (r / 100 == 1) {
119                 int a = 0;
120
121                 while (reg && strlen(reg) > 0) {
122
123                         extract_token(buf, reg, 0, '\n');
124                         remove_token(reg, 0, '\n');
125
126                         if (a == 2)
127                                 strcpy(tmpname, buf);
128                         else if (a == 3)
129                                 strcpy(tmpaddr, buf);
130                         else if (a == 4)
131                                 strcpy(tmpcity, buf);
132                         else if (a == 5)
133                                 strcpy(tmpstate, buf);
134                         else if (a == 6)
135                                 strcpy(tmpzip, buf);
136                         else if (a == 7)
137                                 strcpy(tmpphone, buf);
138                         else if (a == 9)
139                                 strcpy(tmpemail, buf);
140                         else if (a == 10)
141                                 strcpy(tmpcountry, buf);
142                         ++a;
143                 }
144         }
145         strprompt("REAL name", tmpname, 29);
146         strprompt("Address", tmpaddr, 24);
147         strprompt("City/town", tmpcity, 14);
148         strprompt("State/province", tmpstate, 2);
149         strprompt("ZIP/Postal Code", tmpzip, 10);
150         strprompt("Country", tmpcountry, 31);
151         strprompt("Telephone number", tmpphone, 14);
152
153         do {
154                 ok = 1;
155                 strcpy(holdemail, tmpemail);
156                 strprompt("Email address", tmpemail, 31);
157                 r = CtdlIPCDirectoryLookup(ipc, tmpemail, buf);
158                 if (r / 100 == 2) {
159                         extract_token(diruser, buf, 0, '@');
160                         extract_token(dirnode, buf, 1, '@');
161                         striplt(diruser);
162                         striplt(dirnode);
163                         if ((strcasecmp(diruser, fullname))
164                            || (strcasecmp(dirnode, serv_info.serv_nodename))) {
165                                 scr_printf(
166                                         "\nYou can't use %s as your address.\n",
167                                         tmpemail);
168                                 scr_printf(
169                                         "It is already in use by %s @ %s.\n",
170                                         diruser, dirnode);
171                                 ok = 0;
172                                 strcpy(tmpemail, holdemail);
173                         }
174                 }
175         } while (ok == 0);
176
177         /* now send the registration info back to the server */
178         reg = (char *)realloc(reg, 4096);       /* Overkill? */
179         if (reg) {
180                 sprintf(reg, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
181                         tmpname, tmpaddr, tmpcity, tmpstate,
182                         tmpzip, tmpphone, tmpemail, tmpcountry);
183                 r = CtdlIPCSetRegistration(ipc, reg, buf);
184                 if (r / 100 != 4)
185                         scr_printf("%s\n", buf);
186                 free(reg);
187         }
188         scr_printf("\n");
189 }
190
191 void updatels(CtdlIPC *ipc)
192 {                               /* make all messages old in current room */
193         char buf[SIZ];
194         int r;                          /* IPC response code */
195
196         if (rc_alt_semantics) {
197                 if (maxmsgnum == 0 && highest_msg_read == 0) {
198                         return;
199                 }
200                 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ?
201                                  maxmsgnum : highest_msg_read, buf);
202         } else {
203                 r = CtdlIPCSetLastRead(ipc, (maxmsgnum > highest_msg_read) ?
204                                  maxmsgnum : highest_msg_read, buf);
205 /*              r = CtdlIPCSetLastRead(ipc, maxmsgnum, buf); */
206 /* This is a quick-and-dirty fix to all msgs becoming new in Mail>.
207  * It will need to be rethought when messages.c is rewritten.
208  */
209         }
210         if (r / 100 != 2)
211                 scr_printf("%s\n", buf);
212 }
213
214 /*
215  * only make messages old in this room that have been read
216  */
217 void updatelsa(CtdlIPC *ipc)
218 {
219         char buf[SIZ];
220         int r;                          /* IPC response code */
221
222         r = CtdlIPCSetLastRead(ipc, highest_msg_read, buf);
223         if (r / 100 != 2)
224                 scr_printf("%s\n", &buf[4]);
225 }
226
227
228 /*
229  * client-based uploads (for users with their own clientware)
230  */
231 void cli_upload(CtdlIPC *ipc)
232 {
233         char flnm[SIZ];
234         char desc[151];
235         char buf[SIZ];
236         char tbuf[SIZ];
237         int r;          /* IPC response code */
238         int a;
239         int fd;
240
241         if ((room_flags & QR_UPLOAD) == 0) {
242                 scr_printf("*** You cannot upload to this room.\n");
243                 return;
244         }
245         newprompt("File to be uploaded: ", flnm, 55);
246         fd = open(flnm, O_RDONLY);
247         if (fd < 0) {
248                 scr_printf("Cannot open '%s': %s\n", flnm, strerror(errno));
249                 return;
250         }
251         scr_printf("Enter a description of this file:\n");
252         newprompt(": ", desc, 75);
253
254         /* Keep generating filenames in hope of finding a unique one */
255         a = 0;
256         while (a < 10) {
257                 /* basename of filename */
258                 strcpy(tbuf, flnm);
259                 if (haschar(tbuf, '/'))
260                         extract_token(tbuf, flnm,
261                                 num_tokens(tbuf, '/') - 1,
262                                 '/'
263                         );
264                 /* filename.1, filename.2, etc */
265                 if (a > 0) {
266                         sprintf(&tbuf[strlen(tbuf)], ".%d", a);
267                 }
268                 /* Try upload */
269                 r = CtdlIPCFileUpload(ipc, tbuf, desc, flnm, progress, buf);
270                 if (r / 100 == 5 || r < 0)
271                         scr_printf("%s\n", buf);
272                 else
273                         break;
274                 ++a;
275         }
276         if (a > 0) scr_printf("Saved as '%s'\n", tbuf);
277 }
278
279
280 /*
281  * Function used for various image upload commands
282  */
283 void cli_image_upload(CtdlIPC *ipc, char *keyname)
284 {
285         char flnm[SIZ];
286         char buf[SIZ];
287         int r;
288
289         /* Can we upload this image? */
290         r = CtdlIPCImageUpload(ipc, 0, NULL, keyname, NULL, buf);
291         if (r / 100 != 2) {
292                 err_printf("%s\n", buf);
293                 return;
294         }
295         newprompt("Image file to be uploaded: ", flnm, 55);
296         r = CtdlIPCImageUpload(ipc, 1, flnm, keyname, progress, buf);
297         if (r / 100 == 5) {
298                 err_printf("%s\n", buf);
299         } else if (r < 0) {
300                 err_printf("Cannot upload '%s': %s\n", flnm, strerror(errno));
301         }
302         /* else upload succeeded */
303 }
304
305
306 /*
307  * protocol-based uploads (Xmodem, Ymodem, Zmodem)
308  */
309 void upload(CtdlIPC *ipc, int c)
310 {                               /* c = upload mode */
311         char flnm[SIZ];
312         char desc[151];
313         char buf[SIZ];
314         char tbuf[4096];
315         int xfer_pid;
316         int a, b;
317         FILE *fp, *lsfp;
318         int r;
319
320         if ((room_flags & QR_UPLOAD) == 0) {
321                 scr_printf("*** You cannot upload to this room.\n");
322                 return;
323         }
324         /* we don't need a filename when receiving batch y/z modem */
325         if ((c == 2) || (c == 3))
326                 strcpy(flnm, "x");
327         else
328                 newprompt("Enter filename: ", flnm, 15);
329
330         for (a = 0; a < strlen(flnm); ++a)
331                 if ((flnm[a] == '/') || (flnm[a] == '\\') || (flnm[a] == '>')
332                     || (flnm[a] == '?') || (flnm[a] == '*')
333                     || (flnm[a] == ';') || (flnm[a] == '&'))
334                         flnm[a] = '_';
335
336         /* create a temporary directory... */
337         if (mkdir(tempdir, 0700) != 0) {
338                 scr_printf("*** Could not create temporary directory %s: %s\n",
339                        tempdir, strerror(errno));
340                 return;
341         }
342         /* now do the transfer ... in a separate process */
343         xfer_pid = fork();
344         if (xfer_pid == 0) {
345                 chdir(tempdir);
346                 switch (c) {
347                 case 0:
348                         sttybbs(0);
349                         scr_printf("Receiving %s - press Ctrl-D to end.\n", flnm);
350                         fp = fopen(flnm, "w");
351                         do {
352                                 b = inkey();
353                                 if (b == 13) {
354                                         b = 10;
355                                 }
356                                 if (b != 4) {
357                                         scr_printf("%c", b);
358                                         putc(b, fp);
359                                 }
360                         } while (b != 4);
361                         fclose(fp);
362                         exit(0);
363                 case 1:
364                         screen_reset();
365                         sttybbs(3);
366                         execlp("rx", "rx", flnm, NULL);
367                         exit(1);
368                 case 2:
369                         screen_reset();
370                         sttybbs(3);
371                         execlp("rb", "rb", NULL);
372                         exit(1);
373                 case 3:
374                         screen_reset();
375                         sttybbs(3);
376                         execlp("rz", "rz", NULL);
377                         exit(1);
378                 }
379         } else
380                 do {
381                         b = ka_wait(&a);
382                 } while ((b != xfer_pid) && (b != (-1)));
383         sttybbs(0);
384         screen_set();
385
386         if (a != 0) {
387                 scr_printf("\r*** Transfer unsuccessful.\n");
388                 nukedir(tempdir);
389                 return;
390         }
391         scr_printf("\r*** Transfer successful.\n");
392         snprintf(buf, sizeof buf, "cd %s; ls", tempdir);
393         lsfp = popen(buf, "r");
394         if (lsfp != NULL) {
395                 while (fgets(flnm, sizeof flnm, lsfp) != NULL) {
396                         flnm[strlen(flnm) - 1] = 0;     /* chop newline */
397                         snprintf(buf, sizeof buf,
398                                  "Enter a short description of '%s':\n: ",
399                                  flnm);
400                         newprompt(buf, desc, 150);
401                         snprintf(buf, sizeof buf, "%s/%s", tempdir, flnm);
402                         r = CtdlIPCFileUpload(ipc, flnm, desc, buf, progress, tbuf);
403                         scr_printf("%s\n", tbuf);
404                 }
405                 pclose(lsfp);
406         }
407         nukedir(tempdir);
408 }
409
410 /* 
411  * validate a user (returns 0 for successful validation, nonzero if quitting)
412  */
413 int val_user(CtdlIPC *ipc, char *user, int do_validate)
414 {
415         int a;
416         char cmd[SIZ];
417         char buf[SIZ];
418         char *resp = NULL;
419         int ax = 0;
420         char answer[2];
421         int r;                          /* IPC response code */
422
423         scr_printf("\n");
424         r = CtdlIPCGetUserRegistration(ipc, user, &resp, cmd);
425         if (r / 100 == 1) {
426                 a = 0;
427                 do {
428                         extract_token(buf, resp, 0, '\n');
429                         remove_token(resp, 0, '\n');
430                         ++a;
431                         if (a == 1)
432                                 scr_printf("User #%s - %s  ", buf, cmd);
433                         if (a == 2)
434                                 scr_printf("PW: %s\n", buf);
435                         if (a == 3)
436                                 scr_printf("%s\n", buf);
437                         if (a == 4)
438                                 scr_printf("%s\n", buf);
439                         if (a == 5)
440                                 scr_printf("%s, ", buf);
441                         if (a == 6)
442                                 scr_printf("%s ", buf);
443                         if (a == 7)
444                                 scr_printf("%s\n", buf);
445                         if (a == 8)
446                                 scr_printf("%s\n", buf);
447                         if (a == 9)
448                                 ax = atoi(buf);
449                         if (a == 10)
450                                 scr_printf("%s\n", buf);
451                         if (a == 11)
452                                 scr_printf("%s\n", buf);
453                 } while (strlen(resp));
454                 scr_printf("Current access level: %d (%s)\n", ax, axdefs[ax]);
455         } else {
456                 scr_printf("%s\n%s\n", user, &cmd[4]);
457         }
458         if (resp) free(resp);
459
460         if (do_validate) {
461                 /* now set the access level */
462                 while(1) {
463                         sprintf(answer, "%d", ax);
464                         strprompt("New access level (? for help, q to quit)",
465                                 answer, 1);
466                         if ((answer[0] >= '0') && (answer[0] <= '6')) {
467                                 ax = atoi(answer);
468                                 r = CtdlIPCValidateUser(ipc, user, ax, cmd);
469                                 if (r / 100 != 2)
470                                 scr_printf("%s\n\n", cmd);
471                                 return(0);
472                         }
473                         if (tolower(answer[0]) == 'q') {
474                                 scr_printf("*** Aborted.\n\n");
475                                 return(1);
476                         }
477                         if (answer[0] == '?') {
478                                 scr_printf("Available access levels:\n");
479                                 for (a=0; a<7; ++a) {
480                                         scr_printf("%d - %s\n",
481                                                 a, axdefs[a]);
482                                 }
483                         }
484                 }
485         }
486         return(0);
487 }
488
489
490 void validate(CtdlIPC *ipc)
491 {                               /* validate new users */
492         char cmd[SIZ];
493         char buf[SIZ];
494         int finished = 0;
495         int r;                          /* IPC response code */
496
497         do {
498                 r = CtdlIPCNextUnvalidatedUser(ipc, cmd);
499                 if (r / 100 != 3)
500                         finished = 1;
501                 if (r / 100 == 2)
502                         scr_printf("%s\n", cmd);
503                 if (r / 100 == 3) {
504                         extract(buf, cmd, 0);
505                         if (val_user(ipc, buf, 1) != 0) finished = 1;
506                 }
507         } while (finished == 0);
508 }
509
510 void subshell(void)
511 {
512         int a, b;
513         a = fork();
514         if (a == 0) {
515                 screen_reset();
516                 sttybbs(SB_RESTORE);
517                 signal(SIGINT, SIG_DFL);
518                 signal(SIGQUIT, SIG_DFL);
519                 execlp(getenv("SHELL"), getenv("SHELL"), NULL);
520                 err_printf("Could not open a shell: %s\n", strerror(errno));
521                 exit(errno);
522         }
523         do {
524                 b = ka_wait(NULL);
525         } while ((a != b) && (a != (-1)));
526         sttybbs(0);
527         screen_set();
528 }
529
530 /*
531  * <.A>ide <F>ile <D>elete command
532  */
533 void deletefile(CtdlIPC *ipc)
534 {
535         char filename[32];
536         char buf[SIZ];
537
538         newprompt("Filename: ", filename, 31);
539         if (strlen(filename) == 0)
540                 return;
541         CtdlIPCDeleteFile(ipc, filename, buf);
542         err_printf("%s\n", buf);
543 }
544
545 /*
546  * <.A>ide <F>ile <S>end command
547  */
548 void netsendfile(CtdlIPC *ipc)
549 {
550         char filename[32], destsys[20], buf[SIZ];
551
552         newprompt("Filename: ", filename, 31);
553         if (strlen(filename) == 0)
554                 return;
555         newprompt("System to send to: ", destsys, 19);
556         CtdlIPCNetSendFile(ipc, filename, destsys, buf);
557         err_printf("%s\n", buf);
558         return;
559 }
560
561 /*
562  * <.A>ide <F>ile <M>ove command
563  */
564 void movefile(CtdlIPC *ipc)
565 {
566         char filename[64];
567         char newroom[ROOMNAMELEN];
568         char buf[SIZ];
569
570         newprompt("Filename: ", filename, 63);
571         if (strlen(filename) == 0)
572                 return;
573         newprompt("Enter target room: ", newroom, ROOMNAMELEN - 1);
574         CtdlIPCMoveFile(ipc, filename, newroom, buf);
575         err_printf("%s\n", buf);
576 }
577
578
579 /* 
580  * list of users who have filled out a bio
581  */
582 void list_bio(CtdlIPC *ipc)
583 {
584         char buf[SIZ];
585         char *resp = NULL;
586         int pos = 1;
587         int r;                  /* IPC response code */
588
589         r = CtdlIPCListUsersWithBios(ipc, &resp, buf);
590         if (r / 100 != 1) {
591                 pprintf("%s\n", buf);
592                 return;
593         }
594         while (strlen(resp)) {
595                 extract_token(buf, resp, 0, '\n');
596                 remove_token(resp, 0, '\n');
597                 if ((pos + strlen(buf) + 5) > screenwidth) {
598                         pprintf("\n");
599                         pos = 1;
600                 }
601                 pprintf("%s, ", buf);
602                 pos = pos + strlen(buf) + 2;
603         }
604         pprintf("%c%c  \n\n", 8, 8);
605         if (resp) free(resp);
606 }
607
608
609 /*
610  * read bio
611  */
612 void read_bio(CtdlIPC *ipc)
613 {
614         char who[SIZ];
615         char buf[SIZ];
616         char *resp = NULL;
617         int r;                  /* IPC response code */
618
619         do {
620                 newprompt("Read bio for who ('?' for list) : ", who, 25);
621                 pprintf("\n");
622                 if (!strcmp(who, "?"))
623                         list_bio(ipc);
624         } while (!strcmp(who, "?"));
625
626         r = CtdlIPCGetBio(ipc, who, &resp, buf);
627         if (r / 100 != 1) {
628                 pprintf("%s\n", buf);
629                 return;
630         }
631         while (strlen(resp)) {
632                 extract_token(buf, resp, 0, '\n');
633                 remove_token(resp, 0, '\n');
634                 pprintf("%s\n", buf);
635         }
636         if (resp) free(resp);
637 }
638
639
640 /* 
641  * General system configuration command
642  */
643 void do_system_configuration(CtdlIPC *ipc)
644 {
645         char buf[SIZ];
646         char sc[32][SIZ];
647         char *resp = NULL;
648         struct ExpirePolicy *expirepolicy = NULL;
649         int a;
650         int logpages = 0;
651         int r;                  /* IPC response code */
652
653         /* Clear out the config buffers */
654         memset(&sc[0][0], 0, sizeof(sc));
655
656         /* Fetch the current config */
657         r = CtdlIPCGetSystemConfig(ipc, &resp, buf);
658         if (r / 100 == 1) {
659                 a = 0;
660                 while (strlen(resp)) {
661                         extract_token(buf, resp, 0, '\n');
662                         remove_token(resp, 0, '\n');
663                         if (a < 32) {
664                                 strcpy(&sc[a][0], buf);
665                         }
666                         ++a;
667                 }
668         }
669         if (resp) free(resp);
670         resp = NULL;
671         /* Fetch the expire policy (this will silently fail on old servers,
672          * resulting in "default" policy)
673          */
674         r = CtdlIPCGetMessageExpirationPolicy(ipc, 2, &expirepolicy, buf);
675
676         /* Identification parameters */
677
678         strprompt("Node name", &sc[0][0], 15);
679         strprompt("Fully qualified domain name", &sc[1][0], 63);
680         strprompt("Human readable node name", &sc[2][0], 20);
681         strprompt("Modem dialup number", &sc[3][0], 15);
682         strprompt("Geographic location of this system", &sc[12][0], 31);
683         strprompt("Name of system administrator", &sc[13][0], 25);
684         strprompt("Paginator prompt", &sc[10][0], 79);
685
686         /* Security parameters */
687
688         snprintf(sc[7], sizeof sc[7], "%d", (boolprompt(
689                                     "Require registration for new users",
690                                                     atoi(&sc[7][0]))));
691         snprintf(sc[29], sizeof sc[29], "%d", (boolprompt(
692               "Disable self-service user account creation",
693                                                      atoi(&sc[29][0]))));
694         strprompt("Initial access level for new users", &sc[6][0], 1);
695         strprompt("Access level required to create rooms", &sc[19][0], 1);
696         snprintf(sc[4], sizeof sc[4], "%d", (boolprompt(
697                                                     "Automatically give room aide privs to a user who creates a private room",
698                                                     atoi(&sc[4][0]))));
699
700         snprintf(sc[8], sizeof sc[8], "%d", (boolprompt(
701                  "Automatically move problem user messages to twit room",
702                                                     atoi(&sc[8][0]))));
703
704         strprompt("Name of twit room", &sc[9][0], ROOMNAMELEN);
705         snprintf(sc[11], sizeof sc[11], "%d", (boolprompt(
706               "Restrict Internet mail to only those with that privilege",
707                                                      atoi(&sc[11][0]))));
708         snprintf(sc[26], sizeof sc[26], "%d", (boolprompt(
709               "Allow Aides to Zap (forget) rooms",
710                                                      atoi(&sc[26][0]))));
711         snprintf(sc[30], sizeof sc[30], "%d", (boolprompt(
712               "Allow system Aides access to user mailboxes",
713                                                      atoi(&sc[30][0]))));
714
715         if (strlen(&sc[18][0]) > 0) logpages = 1;
716         else logpages = 0;
717         logpages = boolprompt("Log all pages", logpages);
718         if (logpages) {
719                 strprompt("Name of logging room", &sc[18][0], ROOMNAMELEN);
720         }
721         else {
722                 sc[18][0] = 0;
723         }
724
725
726         /* Server tuning */
727
728         strprompt("Server connection idle timeout (in seconds)", &sc[5][0], 4);
729         strprompt("Maximum concurrent sessions", &sc[14][0], 4);
730         strprompt("Maximum message length", &sc[20][0], 20);
731         strprompt("Minimum number of worker threads", &sc[21][0], 3);
732         strprompt("Maximum number of worker threads", &sc[22][0], 3);
733
734         strprompt("POP3 server port (-1 to disable)", &sc[23][0], 5);
735         strprompt("IMAP server port (-1 to disable)", &sc[27][0], 5);
736         strprompt("SMTP server port (-1 to disable)", &sc[24][0], 5);
737
738         /* This logic flips the question around, because it's one of those
739          * situations where 0=yes and 1=no
740          */
741         a = atoi(sc[25]);
742         a = (a ? 0 : 1);
743         a = boolprompt("Correct forged From: lines during authenticated SMTP",
744                 a);
745         a = (a ? 0 : 1);
746         snprintf(sc[25], sizeof sc[25], "%d", a);
747
748         /* Expiry settings */
749         strprompt("Default user purge time (days)", &sc[16][0], 5);
750         strprompt("Default room purge time (days)", &sc[17][0], 5);
751
752         /* Angels and demons dancing in my head... */
753         do {
754                 snprintf(buf, sizeof buf, "%d", expirepolicy->expire_mode);
755                 strprompt("System default message expire policy (? for list)",
756                           buf, 1);
757                 if (buf[0] == '?') {
758                         scr_printf("\n"
759                                 "1. Never automatically expire messages\n"
760                                 "2. Expire by message count\n"
761                                 "3. Expire by message age\n");
762                 }
763         } while ((buf[0] < '1') || (buf[0] > '3'));
764         expirepolicy->expire_mode = buf[0] - '0';
765
766         /* ...lunatics and monsters underneath my bed */
767         if (expirepolicy->expire_mode == 2) {
768                 snprintf(buf, sizeof buf, "%d", expirepolicy->expire_value);
769                 strprompt("Keep how many messages online?", buf, 10);
770                 expirepolicy->expire_value = atol(buf);
771         }
772         if (expirepolicy->expire_mode == 3) {
773                 snprintf(buf, sizeof buf, "%d", expirepolicy->expire_value);
774                 strprompt("Keep messages for how many days?", buf, 10);
775                 expirepolicy->expire_value = atol(buf);
776         }
777
778         strprompt("How often to run network jobs (in seconds)", &sc[28][0], 5);
779         strprompt("Hour to run purges (0-23)", &sc[31][0], 2);
780
781         /* Save it */
782         scr_printf("Save this configuration? ");
783         if (yesno()) {
784                 r = 1;
785                 for (a = 0; a < 32; a++)
786                         r += 1 + strlen(sc[a]);
787                 resp = (char *)calloc(1, r);
788                 if (!resp) {
789                         err_printf("Can't save config - out of memory!\n");
790                         logoff(ipc, 1);
791                 }
792                 for (a = 0; a < 32; a++) {
793                         strcat(resp, sc[a]);
794                         strcat(resp, "\n");
795                 }
796                 r = CtdlIPCSetSystemConfig(ipc, resp, buf);
797                 if (r / 100 != 4) {
798                         err_printf("%s\n", buf);
799                 }
800                 free(resp);
801                 r = CtdlIPCSetMessageExpirationPolicy(ipc, 2, expirepolicy, buf);
802                 if (r / 100 != 2) {
803                         err_printf("%s\n", buf);
804                 }
805         }
806 }
807
808
809 /*
810  * support function for do_internet_configuration()
811  */
812 void get_inet_rec_type(CtdlIPC *ipc, char *buf) {
813         int sel;
814
815         keyopt(" <1> localhost      (Alias for this computer)\n");
816         keyopt(" <2> gateway domain (Domain for all Citadel systems)\n");
817         keyopt(" <3> smart-host     (Forward all outbound mail to this host)\n");
818         keyopt(" <4> directory      (Consult the Global Address Book)\n");
819         keyopt(" <5> SpamAssassin   (Address of SpamAssassin server)\n");
820         keyopt(" <6> RBL            (domain suffix of spam hunting RBL)\n");
821         sel = intprompt("Which one", 1, 1, 6);
822         switch(sel) {
823                 case 1: strcpy(buf, "localhost");
824                         return;
825                 case 2: strcpy(buf, "gatewaydomain");
826                         return;
827                 case 3: strcpy(buf, "smarthost");
828                         return;
829                 case 4: strcpy(buf, "directory");
830                         return;
831                 case 5: strcpy(buf, "spamassassin");
832                         return;
833                 case 6: strcpy(buf, "rbl");
834                         return;
835         }
836 }
837
838
839 /*
840  * Internet mail configuration
841  */
842 void do_internet_configuration(CtdlIPC *ipc)
843 {
844         char buf[SIZ];
845         char *resp = NULL;
846         int num_recs = 0;
847         char **recs = NULL;
848         char ch;
849         int badkey;
850         int i, j;
851         int quitting = 0;
852         int r;
853         
854         r = CtdlIPCGetSystemConfigByType(ipc, INTERNETCFG, &resp, buf);
855         if (r / 100 == 1) {
856                 while (strlen(resp)) {
857                         extract_token(buf, resp, 0, '\n');
858                         remove_token(resp, 0, '\n');
859                         ++num_recs;
860                         if (num_recs == 1) recs = malloc(sizeof(char *));
861                         else recs = realloc(recs, (sizeof(char *)) * num_recs);
862                         recs[num_recs-1] = malloc(strlen(buf) + 1);
863                         strcpy(recs[num_recs-1], buf);
864                 }
865         }
866         if (resp) free(resp);
867
868         do {
869                 scr_printf("\n");
870                 color(BRIGHT_WHITE);
871                 scr_printf("###                    Host or domain                     Record type      \n");
872                 color(DIM_WHITE);
873                 scr_printf("--- -------------------------------------------------- --------------------\n");
874                 for (i=0; i<num_recs; ++i) {
875                 color(DIM_WHITE);
876                 scr_printf("%3d ", i+1);
877                 extract(buf, recs[i], 0);
878                 color(BRIGHT_CYAN);
879                 scr_printf("%-50s ", buf);
880                 extract(buf, recs[i], 1);
881                 color(BRIGHT_MAGENTA);
882                 scr_printf("%-20s\n", buf);
883                 color(DIM_WHITE);
884                 }
885
886                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
887                 switch(ch) {
888                         case 'a':
889                                 newprompt("Enter host name: ",
890                                         buf, 50);
891                                 striplt(buf);
892                                 if (strlen(buf) > 0) {
893                                         ++num_recs;
894                                         if (num_recs == 1)
895                                                 recs = malloc(sizeof(char *));
896                                         else recs = realloc(recs,
897                                                 (sizeof(char *)) * num_recs);
898                                         strcat(buf, "|");
899                                         get_inet_rec_type(ipc,
900                                                         &buf[strlen(buf)]);
901                                         recs[num_recs-1] = strdup(buf);
902                                 }
903                                 break;
904                         case 'd':
905                                 i = intprompt("Delete which one",
906                                         1, 1, num_recs) - 1;
907                                 free(recs[i]);
908                                 --num_recs;
909                                 for (j=i; j<num_recs; ++j)
910                                         recs[j] = recs[j+1];
911                                 break;
912                         case 's':
913                                 r = 1;
914                                 for (i = 0; i < num_recs; i++)
915                                         r += 1 + strlen(recs[i]);
916                                 resp = (char *)calloc(1, r);
917                                 if (!resp) {
918                                         err_printf("Can't save config - out of memory!\n");
919                                         logoff(ipc, 1);
920                                 }
921                                 if (num_recs) for (i = 0; i < num_recs; i++) {
922                                         strcat(resp, recs[i]);
923                                         strcat(resp, "\n");
924                                 }
925                                 r = CtdlIPCSetSystemConfigByType(ipc, INTERNETCFG, resp, buf);
926                                 if (r / 100 != 4) {
927                                         err_printf("%s\n", buf);
928                                 }
929                                 quitting = 1;
930                                 break;
931                         case 'q':
932                                 quitting = boolprompt(
933                                         "Quit without saving", 0);
934                                 break;
935                         default:
936                                 badkey = 1;
937                 }
938         } while (quitting == 0);
939
940         if (recs != NULL) {
941                 for (i=0; i<num_recs; ++i) free(recs[i]);
942                 free(recs);
943         }
944 }
945
946
947
948 /*
949  * Edit network configuration for room sharing, mailing lists, etc.
950  */
951 void network_config_management(CtdlIPC *ipc, char *entrytype, char *comment)
952 {
953         char filename[PATH_MAX];
954         char changefile[PATH_MAX];
955         int e_ex_code;
956         pid_t editor_pid;
957         int cksum;
958         int b, i;
959         char buf[SIZ];
960         char instr[SIZ];
961         char addr[SIZ];
962         FILE *tempfp;
963         FILE *changefp;
964
965         if (strlen(editor_paths[0]) == 0) {
966                 scr_printf("You must have an external editor configured in"
967                         " order to use this function.\n");
968                 return;
969         }
970
971         snprintf(filename, sizeof filename, "%s.listedit", tmpnam(NULL));
972         snprintf(changefile, sizeof changefile, "%s.listedit", tmpnam(NULL));
973
974         tempfp = fopen(filename, "w");
975         if (tempfp == NULL) {
976                 err_printf("Cannot open %s: %s\n", filename, strerror(errno));
977                 return;
978         }
979
980         fprintf(tempfp, "# Configuration for room: %s\n", room_name);
981         fprintf(tempfp, "# %s\n", comment);
982         fprintf(tempfp, "# Specify one per line.\n"
983                         "\n\n");
984
985         CtdlIPC_putline(ipc, "GNET");
986         CtdlIPC_getline(ipc, buf);
987         if (buf[0] == '1') {
988                 while(CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
989                         extract(instr, buf, 0);
990                         if (!strcasecmp(instr, entrytype)) {
991                                 extract(addr, buf, 1);
992                                 fprintf(tempfp, "%s\n", addr);
993                         }
994                 }
995         }
996         fclose(tempfp);
997
998         e_ex_code = 1;  /* start with a failed exit code */
999         screen_reset();
1000         sttybbs(SB_RESTORE);
1001         editor_pid = fork();
1002         cksum = file_checksum(filename);
1003         if (editor_pid == 0) {
1004                 chmod(filename, 0600);
1005                 putenv("WINDOW_TITLE=Network configuration");
1006                 execlp(editor_paths[0], editor_paths[0], filename, NULL);
1007                 exit(1);
1008         }
1009         if (editor_pid > 0) {
1010                 do {
1011                         e_ex_code = 0;
1012                         b = ka_wait(&e_ex_code);
1013                 } while ((b != editor_pid) && (b >= 0));
1014         editor_pid = (-1);
1015         sttybbs(0);
1016         screen_set();
1017         }
1018
1019         if (file_checksum(filename) == cksum) {
1020                 err_printf("*** Not saving changes.\n");
1021                 e_ex_code = 1;
1022         }
1023
1024         if (e_ex_code == 0) {           /* Save changes */
1025                 changefp = fopen(changefile, "w");
1026                 CtdlIPC_putline(ipc, "GNET");
1027                 CtdlIPC_getline(ipc, buf);
1028                 if (buf[0] == '1') {
1029                         while(CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
1030                                 extract(instr, buf, 0);
1031                                 if (strcasecmp(instr, entrytype)) {
1032                                         fprintf(changefp, "%s\n", buf);
1033                                 }
1034                         }
1035                 }
1036                 tempfp = fopen(filename, "r");
1037                 while (fgets(buf, sizeof buf, tempfp) != NULL) {
1038                         for (i=0; i<strlen(buf); ++i) {
1039                                 if (buf[i] == '#') buf[i] = 0;
1040                         }
1041                         striplt(buf);
1042                         if (strlen(buf) > 0) {
1043                                 fprintf(changefp, "%s|%s\n", entrytype, buf);
1044                         }
1045                 }
1046                 fclose(tempfp);
1047                 fclose(changefp);
1048
1049                 /* now write it to the server... */
1050                 CtdlIPC_putline(ipc, "SNET");
1051                 CtdlIPC_getline(ipc, buf);
1052                 if (buf[0] == '4') {
1053                         changefp = fopen(changefile, "r");
1054                         if (changefp != NULL) {
1055                                 while (fgets(buf, sizeof buf,
1056                                        changefp) != NULL) {
1057                                         buf[strlen(buf) - 1] = 0;
1058                                         CtdlIPC_putline(ipc, buf);
1059                                 }
1060                                 fclose(changefp);
1061                         }
1062                         CtdlIPC_putline(ipc, "000");
1063                 }
1064         }
1065
1066         unlink(filename);               /* Delete the temporary files */
1067         unlink(changefile);
1068 }
1069
1070
1071 /*
1072  * IGnet node configuration
1073  */
1074 void do_ignet_configuration(CtdlIPC *ipc) {
1075         char buf[SIZ];
1076         int num_recs = 0;
1077         char **recs = NULL;
1078         char ch;
1079         int badkey;
1080         int i, j;
1081         int quitting = 0;
1082         
1083
1084         snprintf(buf, sizeof buf, "CONF getsys|%s", IGNETCFG);
1085         CtdlIPC_putline(ipc, buf);
1086         CtdlIPC_getline(ipc, buf);
1087         if (buf[0] == '1') while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
1088                 ++num_recs;
1089                 if (num_recs == 1) recs = malloc(sizeof(char *));
1090                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1091                 recs[num_recs-1] = malloc(SIZ);
1092                 strcpy(recs[num_recs-1], buf);
1093         }
1094
1095         do {
1096                 scr_printf("\n");
1097                 color(BRIGHT_WHITE);
1098                 scr_printf(     "### "
1099                         "   Node          "
1100                         "  Secret           "
1101                         "          Host or IP             "
1102                         "Port#\n");
1103                 color(DIM_WHITE);
1104                 scr_printf(     "--- "
1105                         "---------------- "
1106                         "------------------ "
1107                         "-------------------------------- "
1108                         "-----\n");
1109                 for (i=0; i<num_recs; ++i) {
1110                 color(DIM_WHITE);
1111                 scr_printf("%3d ", i+1);
1112                 extract(buf, recs[i], 0);
1113                 color(BRIGHT_CYAN);
1114                 scr_printf("%-16s ", buf);
1115                 extract(buf, recs[i], 1);
1116                 color(BRIGHT_MAGENTA);
1117                 scr_printf("%-18s ", buf);
1118                 extract(buf, recs[i], 2);
1119                 color(BRIGHT_CYAN);
1120                 scr_printf("%-32s ", buf);
1121                 extract(buf, recs[i], 3);
1122                 color(BRIGHT_MAGENTA);
1123                 scr_printf("%-3s\n", buf);
1124                 color(DIM_WHITE);
1125                 }
1126
1127                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1128                 switch(ch) {
1129                         case 'a':
1130                                 ++num_recs;
1131                                 if (num_recs == 1)
1132                                         recs = malloc(sizeof(char *));
1133                                 else recs = realloc(recs,
1134                                         (sizeof(char *)) * num_recs);
1135                                 newprompt("Enter node name    : ", buf, 16);
1136                                 strcat(buf, "|");
1137                                 newprompt("Enter shared secret: ",
1138                                         &buf[strlen(buf)], 18);
1139                                 strcat(buf, "|");
1140                                 newprompt("Enter host or IP   : ",
1141                                         &buf[strlen(buf)], 32);
1142                                 strcat(buf, "|504");
1143                                 strprompt("Enter port number  : ",
1144                                         &buf[strlen(buf)-3], 5);
1145                                 recs[num_recs-1] = strdup(buf);
1146                                 break;
1147                         case 'd':
1148                                 i = intprompt("Delete which one",
1149                                         1, 1, num_recs) - 1;
1150                                 free(recs[i]);
1151                                 --num_recs;
1152                                 for (j=i; j<num_recs; ++j)
1153                                         recs[j] = recs[j+1];
1154                                 break;
1155                         case 's':
1156                                 snprintf(buf, sizeof buf, "CONF putsys|%s", IGNETCFG);
1157                                 CtdlIPC_putline(ipc, buf);
1158                                 CtdlIPC_getline(ipc, buf);
1159                                 if (buf[0] == '4') {
1160                                         for (i=0; i<num_recs; ++i) {
1161                                                 CtdlIPC_putline(ipc, recs[i]);
1162                                         }
1163                                         CtdlIPC_putline(ipc, "000");
1164                                 }
1165                                 else {
1166                                         scr_printf("%s\n", &buf[4]);
1167                                 }
1168                                 quitting = 1;
1169                                 break;
1170                         case 'q':
1171                                 quitting = boolprompt(
1172                                         "Quit without saving", 0);
1173                                 break;
1174                         default:
1175                                 badkey = 1;
1176                 }
1177         } while (quitting == 0);
1178
1179         if (recs != NULL) {
1180                 for (i=0; i<num_recs; ++i) free(recs[i]);
1181                 free(recs);
1182         }
1183 }
1184
1185 /*
1186  * Filter list configuration
1187  */
1188 void do_filterlist_configuration(CtdlIPC *ipc)
1189 {
1190         char buf[SIZ];
1191         int num_recs = 0;
1192         char **recs = NULL;
1193         char ch;
1194         int badkey;
1195         int i, j;
1196         int quitting = 0;
1197         
1198
1199         snprintf(buf, sizeof buf, "CONF getsys|%s", FILTERLIST);
1200         CtdlIPC_putline(ipc, buf);
1201         CtdlIPC_getline(ipc, buf);
1202         if (buf[0] == '1') while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) {
1203                 ++num_recs;
1204                 if (num_recs == 1) recs = malloc(sizeof(char *));
1205                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
1206                 recs[num_recs-1] = malloc(SIZ);
1207                 strcpy(recs[num_recs-1], buf);
1208         }
1209
1210         do {
1211                 scr_printf("\n");
1212                 color(BRIGHT_WHITE);
1213                 scr_printf(     "### "
1214                         "         User name           "
1215                         "         Room name           "
1216                         "    Node name    "
1217                         "\n");
1218                 color(DIM_WHITE);
1219                 scr_printf(     "--- "
1220                         "---------------------------- "
1221                         "---------------------------- "
1222                         "---------------- "
1223                         "\n");
1224                 for (i=0; i<num_recs; ++i) {
1225                 color(DIM_WHITE);
1226                 scr_printf("%3d ", i+1);
1227                 extract(buf, recs[i], 0);
1228                 color(BRIGHT_CYAN);
1229                 scr_printf("%-28s ", buf);
1230                 extract(buf, recs[i], 1);
1231                 color(BRIGHT_MAGENTA);
1232                 scr_printf("%-28s ", buf);
1233                 extract(buf, recs[i], 2);
1234                 color(BRIGHT_CYAN);
1235                 scr_printf("%-16s\n", buf);
1236                 extract(buf, recs[i], 3);
1237                 color(DIM_WHITE);
1238                 }
1239
1240                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
1241                 switch(ch) {
1242                         case 'a':
1243                                 ++num_recs;
1244                                 if (num_recs == 1)
1245                                         recs = malloc(sizeof(char *));
1246                                 else recs = realloc(recs,
1247                                         (sizeof(char *)) * num_recs);
1248                                 newprompt("Enter user name: ", buf, 28);
1249                                 strcat(buf, "|");
1250                                 newprompt("Enter room name: ",
1251                                         &buf[strlen(buf)], 28);
1252                                 strcat(buf, "|");
1253                                 newprompt("Enter node name: ",
1254                                         &buf[strlen(buf)], 16);
1255                                 strcat(buf, "|");
1256                                 recs[num_recs-1] = strdup(buf);
1257                                 break;
1258                         case 'd':
1259                                 i = intprompt("Delete which one",
1260                                         1, 1, num_recs) - 1;
1261                                 free(recs[i]);
1262                                 --num_recs;
1263                                 for (j=i; j<num_recs; ++j)
1264                                         recs[j] = recs[j+1];
1265                                 break;
1266                         case 's':
1267                                 snprintf(buf, sizeof buf, "CONF putsys|%s", FILTERLIST);
1268                                 CtdlIPC_putline(ipc, buf);
1269                                 CtdlIPC_getline(ipc, buf);
1270                                 if (buf[0] == '4') {
1271                                         for (i=0; i<num_recs; ++i) {
1272                                                 CtdlIPC_putline(ipc, recs[i]);
1273                                         }
1274                                         CtdlIPC_putline(ipc, "000");
1275                                 }
1276                                 else {
1277                                         scr_printf("%s\n", &buf[4]);
1278                                 }
1279                                 quitting = 1;
1280                                 break;
1281                         case 'q':
1282                                 quitting = boolprompt(
1283                                         "Quit without saving", 0);
1284                                 break;
1285                         default:
1286                                 badkey = 1;
1287                 }
1288         } while (quitting == 0);
1289
1290         if (recs != NULL) {
1291                 for (i=0; i<num_recs; ++i) free(recs[i]);
1292                 free(recs);
1293         }
1294 }
1295
1296