* Citadel configuration screens in the text mode client
[citadel.git] / citadel / tuiconfig.c
1 /* $Id:  $
2  *
3  * Configuration screens that are part of the text mode client.
4  *
5  */
6
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <ctype.h>
14 #include <string.h>
15 #include <limits.h>
16
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
19 # include <time.h>
20 #else
21 # if HAVE_SYS_TIME_H
22 #  include <sys/time.h>
23 # else
24 #  include <time.h>
25 # endif
26 #endif
27
28 #include <signal.h>
29 #include <pwd.h>
30 #include <errno.h>
31 #include <stdarg.h>
32 #include "sysdep.h"
33 #include "citadel.h"
34 #include "citadel_ipc.h"
35 #include "citadel_decls.h"
36 #include "tuiconfig.h"
37 #include "messages.h"
38 #include "routines.h"
39 #include "commands.h"
40 #include "tools.h"
41 #ifndef HAVE_SNPRINTF
42 #include "snprintf.h"
43 #endif
44 #include "screen.h"
45
46 /* work around solaris include files */
47 #ifdef reg
48 #undef reg
49 #endif
50
51 extern char temp[];
52 extern char tempdir[];
53 extern char *axdefs[8];
54 extern long highest_msg_read;
55 extern long maxmsgnum;
56 extern unsigned room_flags;
57 extern int screenwidth;
58
59
60 /* 
61  * General system configuration command
62  */
63 void do_system_configuration(CtdlIPC *ipc)
64 {
65
66 #define NUM_CONFIGS 60
67
68         char buf[256];
69         char sc[NUM_CONFIGS][256];
70         char *resp = NULL;
71         struct ExpirePolicy *site_expirepolicy = NULL;
72         struct ExpirePolicy *mbx_expirepolicy = NULL;
73         int a;
74         int logpages = 0;
75         int r;                  /* IPC response code */
76         int server_configs = 0;
77
78         /* Clear out the config buffers */
79         memset(&sc[0][0], 0, sizeof(sc));
80
81         /* Fetch the current config */
82         r = CtdlIPCGetSystemConfig(ipc, &resp, buf);
83         if (r / 100 == 1) {
84                 server_configs = num_tokens(resp, '\n');
85                 for (a=0; a<server_configs; ++a) {
86                         if (a < NUM_CONFIGS) {
87                                 extract_token(&sc[a][0], resp, a, '\n', sizeof sc[a]);
88                         }
89                 }
90         }
91         if (resp) free(resp);
92         resp = NULL;
93         /* Fetch the expire policy (this will silently fail on old servers,
94          * resulting in "default" policy)
95          */
96         r = CtdlIPCGetMessageExpirationPolicy(ipc, 2, &site_expirepolicy, buf);
97         r = CtdlIPCGetMessageExpirationPolicy(ipc, 3, &mbx_expirepolicy, buf);
98
99         /* Identification parameters */
100
101         strprompt("Node name", &sc[0][0], 15);
102         strprompt("Fully qualified domain name", &sc[1][0], 63);
103         strprompt("Human readable node name", &sc[2][0], 20);
104         strprompt("Telephone number", &sc[3][0], 15);
105         strprompt("Geographic location of this system", &sc[12][0], 31);
106         strprompt("Name of system administrator", &sc[13][0], 25);
107         strprompt("Paginator prompt", &sc[10][0], 79);
108
109         /* Security parameters */
110
111         snprintf(sc[7], sizeof sc[7], "%d", (boolprompt(
112                 "Require registration for new users",
113                 atoi(&sc[7][0]))));
114         snprintf(sc[29], sizeof sc[29], "%d", (boolprompt(
115                 "Disable self-service user account creation",
116                 atoi(&sc[29][0]))));
117         strprompt("Initial access level for new users", &sc[6][0], 1);
118         strprompt("Access level required to create rooms", &sc[19][0], 1);
119         snprintf(sc[4], sizeof sc[4], "%d", (boolprompt(
120                 "Automatically give room aide privs to a user who creates a private room",
121                 atoi(&sc[4][0]))));
122
123         snprintf(sc[8], sizeof sc[8], "%d", (boolprompt(
124                 "Automatically move problem user messages to twit room",
125                 atoi(&sc[8][0]))));
126
127         strprompt("Name of twit room", &sc[9][0], ROOMNAMELEN);
128         snprintf(sc[11], sizeof sc[11], "%d", (boolprompt(
129                 "Restrict Internet mail to only those with that privilege",
130                 atoi(&sc[11][0]))));
131         snprintf(sc[26], sizeof sc[26], "%d", (boolprompt(
132                 "Allow Aides to Zap (forget) rooms",
133                 atoi(&sc[26][0]))));
134
135         if (!IsEmptyStr(&sc[18][0])) logpages = 1;
136         else logpages = 0;
137         logpages = boolprompt("Log all pages", logpages);
138         if (logpages) {
139                 strprompt("Name of logging room", &sc[18][0], ROOMNAMELEN);
140         }
141         else {
142                 sc[18][0] = 0;
143         }
144
145         /* Commented out because this setting isn't really appropriate to
146          * change while the server is running.
147          *
148          * snprintf(sc[52], sizeof sc[52], "%d", (boolprompt(
149          *      "Use system authentication",
150          *      atoi(&sc[52][0]))));
151          */
152
153         /* Server tuning */
154
155         strprompt("Server connection idle timeout (in seconds)", &sc[5][0], 4);
156         strprompt("Maximum concurrent sessions", &sc[14][0], 4);
157         strprompt("Maximum message length", &sc[20][0], 20);
158         strprompt("Minimum number of worker threads", &sc[21][0], 3);
159         strprompt("Maximum number of worker threads", &sc[22][0], 3);
160         snprintf(sc[43], sizeof sc[43], "%d", (boolprompt(
161                 "Automatically delete committed database logs",
162                 atoi(&sc[43][0]))));
163
164         strprompt("Server IP address (0.0.0.0 for 'any')", &sc[37][0], 15);
165         strprompt("POP3 server port (-1 to disable)", &sc[23][0], 5);
166         strprompt("POP3S server port (-1 to disable)", &sc[40][0], 5);
167         strprompt("IMAP server port (-1 to disable)", &sc[27][0], 5);
168         strprompt("IMAPS server port (-1 to disable)", &sc[39][0], 5);
169         strprompt("SMTP MTA server port (-1 to disable)", &sc[24][0], 5);
170         strprompt("SMTP MSA server port (-1 to disable)", &sc[38][0], 5);
171         strprompt("SMTPS server port (-1 to disable)", &sc[41][0], 5);
172         strprompt("Postfix TCP Dictionary Port server port (-1 to disable)", &sc[50][0], 5);
173         strprompt("ManageSieve server port (-1 to disable)", &sc[51][0], 5);
174         /* This logic flips the question around, because it's one of those
175          * situations where 0=yes and 1=no
176          */
177         a = atoi(sc[25]);
178         a = (a ? 0 : 1);
179         a = boolprompt("Correct forged From: lines during authenticated SMTP",
180                 a);
181         a = (a ? 0 : 1);
182         snprintf(sc[25], sizeof sc[25], "%d", a);
183         snprintf(sc[45], sizeof sc[45], "%d", (boolprompt(
184                 "Allow unauthenticated SMTP clients to spoof my domains",
185                 atoi(&sc[45][0]))));
186         snprintf(sc[57], sizeof sc[57], "%d", (boolprompt(
187                 "Perform RBL checks at greeting instead of after RCPT",
188                 atoi(&sc[57][0]))));
189         snprintf(sc[44], sizeof sc[44], "%d", (boolprompt(
190                 "Instantly expunge deleted IMAP messages",
191                 atoi(&sc[44][0]))));
192
193         /* LDAP settings */
194         if (ipc->ServInfo.supports_ldap) {
195                 a = strlen(&sc[32][0]);
196                 a = (a ? 1 : 0);        /* Set only to 1 or 0 */
197                 a = boolprompt("Connect this Citadel to an external LDAP directory", a);
198                 if (a) {
199                         strprompt("Host name of LDAP server",
200                                 &sc[32][0], 127);
201                         strprompt("Port number of LDAP service",
202                                 &sc[33][0], 5);
203                         strprompt("Base DN", &sc[34][0], 255);
204                         strprompt("Bind DN", &sc[35][0], 255);
205                         strprompt("Password for bind DN", &sc[36][0], 255);
206                 }
207                 else {
208                         strcpy(&sc[32][0], "");
209                 }
210         }
211
212         /* Expiry settings */
213         strprompt("Default user purge time (days)", &sc[16][0], 5);
214         strprompt("Default room purge time (days)", &sc[17][0], 5);
215
216         /* Angels and demons dancing in my head... */
217         do {
218                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_mode);
219                 strprompt("System default message expire policy (? for list)",
220                           buf, 1);
221                 if (buf[0] == '?') {
222                         scr_printf("\n"
223                                 "1. Never automatically expire messages\n"
224                                 "2. Expire by message count\n"
225                                 "3. Expire by message age\n");
226                 }
227         } while ((buf[0] < '1') || (buf[0] > '3'));
228         site_expirepolicy->expire_mode = buf[0] - '0';
229
230         /* ...lunatics and monsters underneath my bed */
231         if (site_expirepolicy->expire_mode == 2) {
232                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_value);
233                 strprompt("Keep how many messages online?", buf, 10);
234                 site_expirepolicy->expire_value = atol(buf);
235         }
236         if (site_expirepolicy->expire_mode == 3) {
237                 snprintf(buf, sizeof buf, "%d", site_expirepolicy->expire_value);
238                 strprompt("Keep messages for how many days?", buf, 10);
239                 site_expirepolicy->expire_value = atol(buf);
240         }
241
242         /* Media messiahs preying on my fears... */
243         do {
244                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_mode);
245                 strprompt("Mailbox default message expire policy (? for list)",
246                           buf, 1);
247                 if (buf[0] == '?') {
248                         scr_printf("\n"
249                                 "0. Go with the system default\n"
250                                 "1. Never automatically expire messages\n"
251                                 "2. Expire by message count\n"
252                                 "3. Expire by message age\n");
253                 }
254         } while ((buf[0] < '0') || (buf[0] > '3'));
255         mbx_expirepolicy->expire_mode = buf[0] - '0';
256
257         /* ...Pop culture prophets playing in my ears */
258         if (mbx_expirepolicy->expire_mode == 2) {
259                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_value);
260                 strprompt("Keep how many messages online?", buf, 10);
261                 mbx_expirepolicy->expire_value = atol(buf);
262         }
263         if (mbx_expirepolicy->expire_mode == 3) {
264                 snprintf(buf, sizeof buf, "%d", mbx_expirepolicy->expire_value);
265                 strprompt("Keep messages for how many days?", buf, 10);
266                 mbx_expirepolicy->expire_value = atol(buf);
267         }
268
269         strprompt("How often to run network jobs (in seconds)", &sc[28][0], 5);
270         strprompt("Hour to run purges (0-23)", &sc[31][0], 2);
271         snprintf(sc[42], sizeof sc[42], "%d", (boolprompt(
272                 "Enable full text search index (warning: resource intensive)",
273                 atoi(&sc[42][0]))));
274
275         snprintf(sc[46], sizeof sc[46], "%d", (boolprompt(
276                 "Perform journaling of email messages",
277                 atoi(&sc[46][0]))));
278         snprintf(sc[47], sizeof sc[47], "%d", (boolprompt(
279                 "Perform journaling of non-email messages",
280                 atoi(&sc[47][0]))));
281         if ( (atoi(&sc[46][0])) || (atoi(&sc[47][0])) ) {
282                 strprompt("Email destination of journalized messages",
283                         &sc[48][0], 127);
284         }
285
286         /* Funambol push stuff */
287         int yes_funambol = 0;
288         if (strlen(sc[53]) > 0) yes_funambol = 1;
289         yes_funambol = boolprompt("Connect to an external Funambol sync server", yes_funambol);
290         if (yes_funambol) {
291                 strprompt("Funambol server (blank to disable)", &sc[53][0], 63);
292                 strprompt("Funambol server port", &sc[54][0], 5);
293                 strprompt("Funambol sync source", &sc[55][0], 63);
294                 strprompt("Funambol authentication details (user:pass in Base64)", &sc[56][0],63);
295         }
296         else {
297                 sc[53][0] = 0;
298                 sc[54][0] = 0;
299                 sc[55][0] = 0;
300                 sc[56][0] = 0;
301         }
302
303         /* Master user account */
304         int yes_muacct = 0;
305         if (strlen(sc[58]) > 0) yes_muacct = 1;
306         yes_muacct = boolprompt("Enable a 'master user' account", yes_muacct);
307         if (yes_muacct) {
308                 strprompt("Master user name", &sc[58][0], 31);
309                 strprompt("Master user password", &sc[59][0], -31);
310         }
311         else {
312                 strcpy(&sc[58][0], "");
313                 strcpy(&sc[59][0], "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
314         }
315
316         /* Save it */
317         scr_printf("Save this configuration? ");
318         if (yesno()) {
319                 r = 1;
320                 for (a = 0; a < NUM_CONFIGS; a++)
321                         r += 1 + strlen(sc[a]);
322                 resp = (char *)calloc(1, r);
323                 if (!resp) {
324                         err_printf("Can't save config - out of memory!\n");
325                         logoff(ipc, 1);
326                 }
327                 for (a = 0; a < NUM_CONFIGS; a++) {
328                         strcat(resp, sc[a]);
329                         strcat(resp, "\n");
330                 }
331                 r = CtdlIPCSetSystemConfig(ipc, resp, buf);
332                 if (r / 100 != 4) {
333                         err_printf("%s\n", buf);
334                 }
335                 free(resp);
336
337                 r = CtdlIPCSetMessageExpirationPolicy(ipc, 2, site_expirepolicy, buf);
338                 if (r / 100 != 2) {
339                         err_printf("%s\n", buf);
340                 }
341
342                 r = CtdlIPCSetMessageExpirationPolicy(ipc, 3, mbx_expirepolicy, buf);
343                 if (r / 100 != 2) {
344                         err_printf("%s\n", buf);
345                 }
346
347         }
348     if (site_expirepolicy) free(site_expirepolicy);
349     if (mbx_expirepolicy) free(mbx_expirepolicy);
350 }
351
352
353 /*
354  * support function for do_internet_configuration()
355  */
356 void get_inet_rec_type(CtdlIPC *ipc, char *buf) {
357         int sel;
358
359         keyopt(" <1> localhost      (Alias for this computer)\n");
360         keyopt(" <2> gateway domain (Domain for all Citadel systems)\n");
361         keyopt(" <3> smart-host     (Forward all outbound mail to this host)\n");
362         keyopt(" <4> directory      (Consult the Global Address Book)\n");
363         keyopt(" <5> SpamAssassin   (Address of SpamAssassin server)\n");
364         keyopt(" <6> RBL            (domain suffix of spam hunting RBL)\n");
365         keyopt(" <7> masq domains   (Domains as which users are allowed to masquerade)\n");
366         sel = intprompt("Which one", 1, 1, 6);
367         switch(sel) {
368                 case 1: strcpy(buf, "localhost");
369                         return;
370                 case 2: strcpy(buf, "gatewaydomain");
371                         return;
372                 case 3: strcpy(buf, "smarthost");
373                         return;
374                 case 4: strcpy(buf, "directory");
375                         return;
376                 case 5: strcpy(buf, "spamassassin");
377                         return;
378                 case 6: strcpy(buf, "rbl");
379                         return;
380                 case 7: strcpy(buf, "masqdomain");
381                         return;
382         }
383 }
384
385
386 /*
387  * Internet mail configuration
388  */
389 void do_internet_configuration(CtdlIPC *ipc)
390 {
391         char buf[256];
392         char *resp = NULL;
393         int num_recs = 0;
394         char **recs = NULL;
395         char ch;
396         int badkey;
397         int i, j;
398         int quitting = 0;
399         int modified = 0;
400         int r;
401         
402         r = CtdlIPCGetSystemConfigByType(ipc, INTERNETCFG, &resp, buf);
403         if (r / 100 == 1) {
404                 while (!IsEmptyStr(resp)) {
405                         extract_token(buf, resp, 0, '\n', sizeof buf);
406                         remove_token(resp, 0, '\n');
407                         ++num_recs;
408                         if (num_recs == 1) recs = malloc(sizeof(char *));
409                         else recs = realloc(recs, (sizeof(char *)) * num_recs);
410                         recs[num_recs-1] = malloc(strlen(buf) + 1);
411                         strcpy(recs[num_recs-1], buf);
412                 }
413         }
414         if (resp) free(resp);
415
416         do {
417                 scr_printf("\n");
418                 color(BRIGHT_WHITE);
419                 scr_printf("###                    Host or domain                     Record type      \n");
420                 color(DIM_WHITE);
421                 scr_printf("--- -------------------------------------------------- --------------------\n");
422                 for (i=0; i<num_recs; ++i) {
423                 color(DIM_WHITE);
424                 scr_printf("%3d ", i+1);
425                 extract_token(buf, recs[i], 0, '|', sizeof buf);
426                 color(BRIGHT_CYAN);
427                 scr_printf("%-50s ", buf);
428                 extract_token(buf, recs[i], 1, '|', sizeof buf);
429                 color(BRIGHT_MAGENTA);
430                 scr_printf("%-20s\n", buf);
431                 color(DIM_WHITE);
432                 }
433
434                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
435                 switch(ch) {
436                         case 'a':
437                                 newprompt("Enter host name: ",
438                                         buf, 50);
439                                 striplt(buf);
440                                 if (!IsEmptyStr(buf)) {
441                                         ++num_recs;
442                                         if (num_recs == 1)
443                                                 recs = malloc(sizeof(char *));
444                                         else recs = realloc(recs,
445                                                 (sizeof(char *)) * num_recs);
446                                         strcat(buf, "|");
447                                         get_inet_rec_type(ipc,
448                                                         &buf[strlen(buf)]);
449                                         recs[num_recs-1] = strdup(buf);
450                                 }
451                                 modified = 1;
452                                 break;
453                         case 'd':
454                                 i = intprompt("Delete which one",
455                                         1, 1, num_recs) - 1;
456                                 free(recs[i]);
457                                 --num_recs;
458                                 for (j=i; j<num_recs; ++j)
459                                         recs[j] = recs[j+1];
460                                 modified = 1;
461                                 break;
462                         case 's':
463                                 r = 1;
464                                 for (i = 0; i < num_recs; i++)
465                                         r += 1 + strlen(recs[i]);
466                                 resp = (char *)calloc(1, r);
467                                 if (!resp) {
468                                         err_printf("Can't save config - out of memory!\n");
469                                         logoff(ipc, 1);
470                                 }
471                                 if (num_recs) for (i = 0; i < num_recs; i++) {
472                                         strcat(resp, recs[i]);
473                                         strcat(resp, "\n");
474                                 }
475                                 r = CtdlIPCSetSystemConfigByType(ipc, INTERNETCFG, resp, buf);
476                                 if (r / 100 != 4) {
477                                         err_printf("%s\n", buf);
478                                 } else {
479                                         scr_printf("Wrote %d records.\n", num_recs);
480                                         modified = 0;
481                                 }
482                 free(resp);
483                                 break;
484                         case 'q':
485                                 quitting = !modified || boolprompt(
486                                         "Quit without saving", 0);
487                                 break;
488                         default:
489                                 badkey = 1;
490                 }
491         } while (!quitting);
492
493         if (recs != NULL) {
494                 for (i=0; i<num_recs; ++i) free(recs[i]);
495                 free(recs);
496         }
497 }
498
499
500
501 /*
502  * Edit network configuration for room sharing, mailing lists, etc.
503  */
504 void network_config_management(CtdlIPC *ipc, char *entrytype, char *comment)
505 {
506         char filename[PATH_MAX];
507         char changefile[PATH_MAX];
508         int e_ex_code;
509         pid_t editor_pid;
510         int cksum;
511         int b, i, tokens;
512         char buf[1024];
513         char instr[1024];
514         char addr[1024];
515         FILE *tempfp;
516         FILE *changefp;
517         char *listing = NULL;
518         int r;
519
520         if (IsEmptyStr(editor_paths[0])) {
521                 scr_printf("You must have an external editor configured in"
522                         " order to use this function.\n");
523                 return;
524         }
525
526         CtdlMakeTempFileName(filename, sizeof filename);
527         CtdlMakeTempFileName(changefile, sizeof changefile);
528
529         tempfp = fopen(filename, "w");
530         if (tempfp == NULL) {
531                 err_printf("Cannot open %s: %s\n", filename, strerror(errno));
532                 return;
533         }
534
535         fprintf(tempfp, "# Configuration for room: %s\n", room_name);
536         fprintf(tempfp, "# %s\n", comment);
537         fprintf(tempfp, "# Specify one per line.\n"
538                         "\n\n");
539
540         r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
541         if (r / 100 == 1) {
542                 while(listing && !IsEmptyStr(listing)) {
543                         extract_token(buf, listing, 0, '\n', sizeof buf);
544                         remove_token(listing, 0, '\n');
545                         extract_token(instr, buf, 0, '|', sizeof instr);
546                         if (!strcasecmp(instr, entrytype)) {
547                                 tokens = num_tokens(buf, '|');
548                                 for (i=1; i<tokens; ++i) {
549                                         extract_token(addr, buf, i, '|', sizeof addr);
550                                         fprintf(tempfp, "%s", addr);
551                                         if (i < (tokens-1)) {
552                                                 fprintf(tempfp, "|");
553                                         }
554                                 }
555                                 fprintf(tempfp, "\n");
556                         }
557                 }
558         }
559         if (listing) {
560                 free(listing);
561                 listing = NULL;
562         }
563         fclose(tempfp);
564
565         e_ex_code = 1;  /* start with a failed exit code */
566         screen_reset();
567         stty_ctdl(SB_RESTORE);
568         editor_pid = fork();
569         cksum = file_checksum(filename);
570         if (editor_pid == 0) {
571                 chmod(filename, 0600);
572                 putenv("WINDOW_TITLE=Network configuration");
573                 execlp(editor_paths[0], editor_paths[0], filename, NULL);
574                 exit(1);
575         }
576         if (editor_pid > 0) {
577                 do {
578                         e_ex_code = 0;
579                         b = ka_wait(&e_ex_code);
580                 } while ((b != editor_pid) && (b >= 0));
581         editor_pid = (-1);
582         stty_ctdl(0);
583         screen_set();
584         }
585
586         if (file_checksum(filename) == cksum) {
587                 err_printf("*** No changes to save.\n");
588                 e_ex_code = 1;
589         }
590
591         if (e_ex_code == 0) {           /* Save changes */
592                 changefp = fopen(changefile, "w");
593
594                 /* Load all netconfig entries that are *not* of the type we are editing */
595                 r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
596                 if (r / 100 == 1) {
597                         while(listing && !IsEmptyStr(listing)) {
598                                 extract_token(buf, listing, 0, '\n', sizeof buf);
599                                 remove_token(listing, 0, '\n');
600                                 extract_token(instr, buf, 0, '|', sizeof instr);
601                                 if (strcasecmp(instr, entrytype)) {
602                                         fprintf(changefp, "%s\n", buf);
603                                 }
604                         }
605                 }
606                 if (listing) {
607                         free(listing);
608                         listing = NULL;
609                 }
610
611                 /* ...and merge that with the data we just edited */
612                 tempfp = fopen(filename, "r");
613                 while (fgets(buf, sizeof buf, tempfp) != NULL) {
614                         for (i=0; i<strlen(buf); ++i) {
615                                 if (buf[i] == '#') buf[i] = 0;
616                         }
617                         striplt(buf);
618                         if (!IsEmptyStr(buf)) {
619                                 fprintf(changefp, "%s|%s\n", entrytype, buf);
620                         }
621                 }
622                 fclose(tempfp);
623                 fclose(changefp);
624
625                 /* now write it to the server... */
626                 changefp = fopen(changefile, "r");
627                 if (changefp != NULL) {
628                         listing = load_message_from_file(changefp);
629                         if (listing) {
630                                 r = CtdlIPCSetRoomNetworkConfig(ipc, listing, buf);
631                                 free(listing);
632                                 listing = NULL;
633                         }
634                         fclose(changefp);
635                 }
636         }
637
638         unlink(filename);               /* Delete the temporary files */
639         unlink(changefile);
640 }
641
642
643 /*
644  * IGnet node configuration
645  */
646 void do_ignet_configuration(CtdlIPC *ipc) {
647         char buf[SIZ];
648         int num_recs = 0;
649         char **recs = NULL;
650         char ch;
651         int badkey;
652         int i, j;
653         int quitting = 0;
654         int modified = 0;
655         char *listing = NULL;
656         int r;
657
658         r = CtdlIPCGetSystemConfigByType(ipc, IGNETCFG, &listing, buf);
659         if (r / 100 == 1) while (*listing && !IsEmptyStr(listing)) {
660                 extract_token(buf, listing, 0, '\n', sizeof buf);
661                 remove_token(listing, 0, '\n');
662
663                 ++num_recs;
664                 if (num_recs == 1) recs = malloc(sizeof(char *));
665                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
666                 recs[num_recs-1] = malloc(SIZ);
667                 strcpy(recs[num_recs-1], buf);
668         }
669         if (listing) free(listing);
670
671         do {
672                 scr_printf("\n");
673                 color(BRIGHT_WHITE);
674                 scr_printf(     "### "
675                         "   Node          "
676                         "  Secret           "
677                         "          Host or IP             "
678                         "Port#\n");
679                 color(DIM_WHITE);
680                 scr_printf(     "--- "
681                         "---------------- "
682                         "------------------ "
683                         "-------------------------------- "
684                         "-----\n");
685                 for (i=0; i<num_recs; ++i) {
686                 color(DIM_WHITE);
687                 scr_printf("%3d ", i+1);
688                 extract_token(buf, recs[i], 0, '|', sizeof buf);
689                 color(BRIGHT_CYAN);
690                 scr_printf("%-16s ", buf);
691                 extract_token(buf, recs[i], 1, '|', sizeof buf);
692                 color(BRIGHT_MAGENTA);
693                 scr_printf("%-18s ", buf);
694                 extract_token(buf, recs[i], 2, '|', sizeof buf);
695                 color(BRIGHT_CYAN);
696                 scr_printf("%-32s ", buf);
697                 extract_token(buf, recs[i], 3, '|', sizeof buf);
698                 color(BRIGHT_MAGENTA);
699                 scr_printf("%-3s\n", buf);
700                 color(DIM_WHITE);
701                 }
702                 scr_printf("\n");
703
704                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
705                 switch(ch) {
706                         case 'a':
707                                 ++num_recs;
708                                 if (num_recs == 1)
709                                         recs = malloc(sizeof(char *));
710                                 else recs = realloc(recs,
711                                         (sizeof(char *)) * num_recs);
712                                 newprompt("Enter node name    : ", buf, 16);
713                                 strcat(buf, "|");
714                                 newprompt("Enter shared secret: ",
715                                         &buf[strlen(buf)], 18);
716                                 strcat(buf, "|");
717                                 newprompt("Enter host or IP   : ",
718                                         &buf[strlen(buf)], 32);
719                                 strcat(buf, "|504");
720                                 strprompt("Enter port number  : ",
721                                         &buf[strlen(buf)-3], 5);
722                                 recs[num_recs-1] = strdup(buf);
723                                 modified = 1;
724                                 break;
725                         case 'd':
726                                 i = intprompt("Delete which one",
727                                         1, 1, num_recs) - 1;
728                                 free(recs[i]);
729                                 --num_recs;
730                                 for (j=i; j<num_recs; ++j)
731                                         recs[j] = recs[j+1];
732                                 modified = 1;
733                                 break;
734                         case 's':
735                                 r = 1;
736                                 for (i = 0; i < num_recs; ++i)
737                                         r += 1 + strlen(recs[i]);
738                                 listing = (char*) calloc(1, r);
739                                 if (!listing) {
740                                         err_printf("Can't save config - out of memory!\n");
741                                         logoff(ipc, 1);
742                                 }
743                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
744                                         strcat(listing, recs[i]);
745                                         strcat(listing, "\n");
746                                 }
747                                 r = CtdlIPCSetSystemConfigByType(ipc, IGNETCFG, listing, buf);
748                                 if (r / 100 != 4) {
749                                         scr_printf("%s\n", buf);
750                                 } else {
751                                         scr_printf("Wrote %d records.\n", num_recs);
752                                         modified = 0;
753                                 }
754                 free(listing);
755                                 break;
756                         case 'q':
757                                 quitting = !modified || boolprompt(
758                                         "Quit without saving", 0);
759                                 break;
760                         default:
761                                 badkey = 1;
762                 }
763         } while (!quitting);
764
765         if (recs != NULL) {
766                 for (i=0; i<num_recs; ++i) free(recs[i]);
767                 free(recs);
768         }
769 }
770
771
772 /*
773  * Filter list configuration
774  */
775 void do_filterlist_configuration(CtdlIPC *ipc)
776 {
777         char buf[SIZ];
778         int num_recs = 0;
779         char **recs = NULL;
780         char ch;
781         int badkey;
782         int i, j;
783         int quitting = 0;
784         int modified = 0;
785         char *listing = NULL;
786         int r;
787
788         r = CtdlIPCGetSystemConfigByType(ipc, FILTERLIST, &listing, buf);
789         if (r / 100 == 1) while (*listing && !IsEmptyStr(listing)) {
790                 extract_token(buf, listing, 0, '\n', sizeof buf);
791                 remove_token(listing, 0, '\n');
792
793                 ++num_recs;
794                 if (num_recs == 1) recs = malloc(sizeof(char *));
795                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
796                 recs[num_recs-1] = malloc(SIZ);
797                 strcpy(recs[num_recs-1], buf);
798         }
799         if (listing) free(listing);
800
801         do {
802                 scr_printf("\n");
803                 color(BRIGHT_WHITE);
804                 scr_printf(     "### "
805                         "         User name           "
806                         "         Room name           "
807                         "    Node name    "
808                         "\n");
809                 color(DIM_WHITE);
810                 scr_printf(     "--- "
811                         "---------------------------- "
812                         "---------------------------- "
813                         "---------------- "
814                         "\n");
815                 for (i=0; i<num_recs; ++i) {
816                 color(DIM_WHITE);
817                 scr_printf("%3d ", i+1);
818                 extract_token(buf, recs[i], 0, '|', sizeof buf);
819                 color(BRIGHT_CYAN);
820                 scr_printf("%-28s ", buf);
821                 extract_token(buf, recs[i], 1, '|', sizeof buf);
822                 color(BRIGHT_MAGENTA);
823                 scr_printf("%-28s ", buf);
824                 extract_token(buf, recs[i], 2, '|', sizeof buf);
825                 color(BRIGHT_CYAN);
826                 scr_printf("%-16s\n", buf);
827                 extract_token(buf, recs[i], 3, '|', sizeof buf);
828                 color(DIM_WHITE);
829                 }
830
831                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
832                 switch(ch) {
833                         case 'a':
834                                 ++num_recs;
835                                 if (num_recs == 1)
836                                         recs = malloc(sizeof(char *));
837                                 else recs = realloc(recs,
838                                         (sizeof(char *)) * num_recs);
839                                 newprompt("Enter user name: ", buf, 28);
840                                 strcat(buf, "|");
841                                 newprompt("Enter room name: ",
842                                         &buf[strlen(buf)], 28);
843                                 strcat(buf, "|");
844                                 newprompt("Enter node name: ",
845                                         &buf[strlen(buf)], 16);
846                                 strcat(buf, "|");
847                                 recs[num_recs-1] = strdup(buf);
848                                 modified = 1;
849                                 break;
850                         case 'd':
851                                 i = intprompt("Delete which one",
852                                         1, 1, num_recs) - 1;
853                                 free(recs[i]);
854                                 --num_recs;
855                                 for (j=i; j<num_recs; ++j)
856                                         recs[j] = recs[j+1];
857                                 modified = 1;
858                                 break;
859                         case 's':
860                                 r = 1;
861                                 for (i = 0; i < num_recs; ++i)
862                                         r += 1 + strlen(recs[i]);
863                                 listing = (char*) calloc(1, r);
864                                 if (!listing) {
865                                         err_printf("Can't save config - out of memory!\n");
866                                         logoff(ipc, 1);
867                                 }
868                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
869                                         strcat(listing, recs[i]);
870                                         strcat(listing, "\n");
871                                 }
872                                 r = CtdlIPCSetSystemConfigByType(ipc, FILTERLIST, listing, buf);
873                                 if (r / 100 != 4) {
874                                         scr_printf("%s\n", buf);
875                                 } else {
876                                         scr_printf("Wrote %d records.\n", num_recs);
877                                         modified = 0;
878                                 }
879                 free(listing);
880                                 break;
881                         case 'q':
882                                 quitting = !modified || boolprompt(
883                                         "Quit without saving", 0);
884                                 break;
885                         default:
886                                 badkey = 1;
887                 }
888         } while (!quitting);
889
890         if (recs != NULL) {
891                 for (i=0; i<num_recs; ++i) free(recs[i]);
892                 free(recs);
893         }
894 }
895
896
897
898
899 /*
900  * POP3 aggregation client configuration
901  */
902 void do_pop3client_configuration(CtdlIPC *ipc)
903 {
904         char buf[SIZ];
905         int num_recs = 0;
906         char **recs = NULL;
907         char ch;
908         int badkey;
909         int i, j;
910         int quitting = 0;
911         int modified = 0;
912         char *listing = NULL;
913         char *other_listing = NULL;
914         int r;
915         char instr[SIZ];
916
917         r = CtdlIPCGetRoomNetworkConfig(ipc, &listing, buf);
918         if (r / 100 == 1) {
919                 while(listing && !IsEmptyStr(listing)) {
920                         extract_token(buf, listing, 0, '\n', sizeof buf);
921                         remove_token(listing, 0, '\n');
922                         extract_token(instr, buf, 0, '|', sizeof instr);
923                         if (!strcasecmp(instr, "pop3client")) {
924
925                                 ++num_recs;
926                                 if (num_recs == 1) recs = malloc(sizeof(char *));
927                                 else recs = realloc(recs, (sizeof(char *)) * num_recs);
928                                 recs[num_recs-1] = malloc(SIZ);
929                                 strcpy(recs[num_recs-1], buf);
930
931                         }
932                 }
933         }
934         if (listing) {
935                 free(listing);
936                 listing = NULL;
937         }
938
939         do {
940                 scr_printf("\n");
941                 color(BRIGHT_WHITE);
942                 scr_printf(     "### "
943                         "      Remote POP3 host       "
944                         "         User name           "
945                         "    Password     "
946                         "\n");
947                 color(DIM_WHITE);
948                 scr_printf(     "--- "
949                         "---------------------------- "
950                         "---------------------------- "
951                         "---------------- "
952                         "\n");
953                 for (i=0; i<num_recs; ++i) {
954                 color(DIM_WHITE);
955                 scr_printf("%3d ", i+1);
956                 extract_token(buf, recs[i], 1, '|', sizeof buf);
957                 color(BRIGHT_CYAN);
958                 scr_printf("%-28s ", buf);
959                 extract_token(buf, recs[i], 2, '|', sizeof buf);
960                 color(BRIGHT_MAGENTA);
961                 scr_printf("%-28s ", buf);
962                 extract_token(buf, recs[i], 3, '|', sizeof buf);
963                 color(BRIGHT_CYAN);
964                 scr_printf("%-16s\n", buf);
965                 extract_token(buf, recs[i], 4, '|', sizeof buf);
966                 color(DIM_WHITE);
967                 }
968
969                 ch = keymenu("", "<A>dd|<D>elete|<S>ave|<Q>uit");
970                 switch(ch) {
971                         case 'a':
972                                 ++num_recs;
973                                 if (num_recs == 1)
974                                         recs = malloc(sizeof(char *));
975                                 else recs = realloc(recs,
976                                         (sizeof(char *)) * num_recs);
977                                 strcpy(buf, "pop3client|");
978                                 newprompt("Enter host name: ",
979                                         &buf[strlen(buf)], 28);
980                                 strcat(buf, "|");
981                                 newprompt("Enter user name: ",
982                                         &buf[strlen(buf)], 28);
983                                 strcat(buf, "|");
984                                 newprompt("Enter password : ",
985                                         &buf[strlen(buf)], 16);
986                                 strcat(buf, "|");
987                                 recs[num_recs-1] = strdup(buf);
988                                 modified = 1;
989                                 break;
990                         case 'd':
991                                 i = intprompt("Delete which one",
992                                         1, 1, num_recs) - 1;
993                                 free(recs[i]);
994                                 --num_recs;
995                                 for (j=i; j<num_recs; ++j)
996                                         recs[j] = recs[j+1];
997                                 modified = 1;
998                                 break;
999                         case 's':
1000                                 r = 1;
1001                                 for (i = 0; i < num_recs; ++i) {
1002                                         r += 1 + strlen(recs[i]);
1003                                 }
1004                                 listing = (char*) calloc(1, r);
1005                                 if (!listing) {
1006                                         err_printf("Can't save config - out of memory!\n");
1007                                         logoff(ipc, 1);
1008                                 }
1009                                 if (num_recs) for (i = 0; i < num_recs; ++i) {
1010                                         strcat(listing, recs[i]);
1011                                         strcat(listing, "\n");
1012                                 }
1013
1014                                 /* Retrieve all the *other* records for merging */
1015                                 r = CtdlIPCGetRoomNetworkConfig(ipc, &other_listing, buf);
1016                                 if (r / 100 == 1) {
1017                                         for (i=0; i<num_tokens(other_listing, '\n'); ++i) {
1018                                                 extract_token(buf, other_listing, i, '\n', sizeof buf);
1019                                                 if (strncasecmp(buf, "pop3client|", 11)) {
1020                                                         listing = realloc(listing, strlen(listing) +
1021                                                                 strlen(buf) + 10);
1022                                                         strcat(listing, buf);
1023                                                         strcat(listing, "\n");
1024                                                 }
1025                                         }
1026                                 }
1027                                 free(other_listing);
1028                                 r = CtdlIPCSetRoomNetworkConfig(ipc, listing, buf);
1029                                 free(listing);
1030                                 listing = NULL;
1031
1032                                 if (r / 100 != 4) {
1033                                         scr_printf("%s\n", buf);
1034                                 } else {
1035                                         scr_printf("Wrote %d records.\n", num_recs);
1036                                         modified = 0;
1037                                 }
1038                                 quitting = 1;
1039                                 break;
1040                         case 'q':
1041                                 quitting = !modified || boolprompt(
1042                                         "Quit without saving", 0);
1043                                 break;
1044                         default:
1045                                 badkey = 1;
1046                 }
1047         } while (!quitting);
1048
1049         if (recs != NULL) {
1050                 for (i=0; i<num_recs; ++i) free(recs[i]);
1051                 free(recs);
1052         }
1053 }
1054
1055