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