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