* Completed the "Internet Configuration" screen
[citadel.git] / webcit / inetconf.c
1 /* 
2  * inetconf.c
3  *
4  * Functions which handle Internet domain configuration etc.
5  *
6  * $Id$
7  */
8
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <sys/socket.h>
18 #include <sys/time.h>
19 #include <limits.h>
20 #include <netinet/in.h>
21 #include <netdb.h>
22 #include <string.h>
23 #include <pwd.h>
24 #include <errno.h>
25 #include <stdarg.h>
26 #include <pthread.h>
27 #include <signal.h>
28 #include "webcit.h"
29
30
31
32 void display_inetconf(void)
33 {
34         char buf[SIZ];
35         char ename[SIZ];
36         char etype[SIZ];
37         int i;
38         int which;
39
40         enum {
41                 ic_localhost,
42                 ic_gwdom,
43                 ic_directory,
44                 ic_spamass,
45                 ic_rbl,
46                 ic_smarthost,
47                 ic_max
48         };
49         char *ic_spec[ic_max];
50         char *ic_misc;
51
52         char *ic_keyword[] = {
53                 "localhost",
54                 "gatewaydomain",
55                 "directory",
56                 "spamassassin",
57                 "rbl",
58                 "smarthost"
59         };
60
61         char *ic_boxtitle[] = {
62                 "Local host aliases",
63                 "Gateway domains",
64                 "Directory domains",
65                 "SpamAssassin hosts",
66                 "RBL hosts",
67                 "Smart hosts"
68         };
69
70         char *ic_desc[] = {
71                 "(domains for which this host receives mail)",
72                 "(domains whose subdomains match Citadel hosts)",
73                 "(domains mapped with the Global Address Book)",
74                 "(hosts running the SpamAssassin service)",
75                 "(hosts running a Realtime Blackhole List)",
76                 "(if present, forward all outbound mail to one of these hosts)"
77         };
78
79         for (i=0; i<ic_max; ++i) {
80                 ic_spec[i] = strdup("");
81         }
82         ic_misc = strdup("");
83
84         output_headers(3);
85         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
86         wprintf("<SPAN CLASS=\"titlebar\">Internet configuration</SPAN>\n");
87         wprintf("</TD></TR></TABLE>\n");
88
89         serv_printf("CONF GETSYS|application/x-citadel-internet-config");
90         serv_gets(buf);
91         if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
92
93                 extract(ename, buf, 0);
94                 extract(etype, buf, 1);
95                 which = (-1);
96                 for (i=0; i<ic_max; ++i) {
97                         if (!strcasecmp(etype, ic_keyword[i])) {
98                                 which = i;
99                         }
100                 }
101
102                 if (which >= 0) {
103                         ic_spec[which] = realloc(ic_spec[which], strlen(ic_spec[which]) + strlen(ename) + 2);
104                         if (strlen(ic_spec[which]) > 0) strcat(ic_spec[which], "\n");
105                         strcat(ic_spec[which], ename);
106                 }
107                 else {
108                         ic_misc = realloc(ic_misc, strlen(ic_misc) + strlen(buf) + 2);
109                         if (strlen(ic_misc) > 0) strcat(ic_misc, "\n");
110                         strcat(ic_misc, buf);
111                 }
112
113         }
114
115         wprintf("<TABLE border=0 width=100%%>\n");
116         for (which=0; which<ic_max; ++which) {
117                 if (which % 2 == 0) {
118                         wprintf("<TR>");
119                 }
120                 wprintf("<TD>");
121                 svprintf("BOXTITLE", WCS_STRING, ic_boxtitle[which]);
122                 do_template("beginbox");
123                 wprintf("<span class=\"menudesc\">");
124                 escputs(ic_desc[which]);
125                 wprintf("</span><br>");
126                 wprintf("<TABLE border=0 cellspacing=0 cellpadding=0 width=100%%>\n");
127                 if (strlen(ic_spec[which]) > 0) {
128                         for (i=0; i<num_tokens(ic_spec[which], '\n'); ++i) {
129                                 wprintf("<TR><TD ALIGN=LEFT>");
130                                 extract_token(buf, ic_spec[which], i, '\n');
131                                 escputs(buf);
132                                 wprintf("</TD><TD ALIGN=RIGHT>"
133                                         "<A HREF=\"/save_inetconf?oper=delete&ename=");
134                                 escputs(buf);
135                                 wprintf("&etype=%s\" ", ic_keyword[which]);
136                                 wprintf("onClick=\"return confirm('Delete ");
137                                 jsescputs(buf);
138                                 wprintf("?');\">");
139                                 wprintf("<font size=-1>(Delete)</font></a></TD></TR>\n");
140                         }
141                 }
142                 wprintf("<FORM METHOD=\"POST\" ACTION=\"/save_inetconf\">\n"
143                         "<TR><TD>"
144                         "<INPUT TYPE=\"text\" NAME=\"ename\" MAXLENGTH=\"64\">"
145                         "<INPUT TYPE=\"hidden\" NAME=\"etype\" VALUE=\"%s\">", ic_keyword[which]);
146                 wprintf("</TD><TD ALIGN=RIGHT>"
147                         "<INPUT TYPE=\"submit\" NAME=\"oper\" VALUE=\"Add\">"
148                         "</TD></TR></TABLE></FORM>\n");
149                 do_template("endbox");
150                 wprintf("</TD>");
151                 if (which % 2 != 0) {
152                         wprintf("</TR>");
153                 }
154         }
155         wprintf("</TABLE>\n");
156
157         wDumpContent(1);
158
159         for (i=0; i<ic_max; ++i) {
160                 free(ic_spec[i]);
161         }
162         free(ic_misc);
163 }
164
165
166 void save_inetconf(void) {
167         char buf[SIZ];
168         char ename[SIZ];
169         char etype[SIZ];
170         char newconfig[65536];
171
172         strcpy(newconfig, "");
173         serv_printf("CONF GETSYS|application/x-citadel-internet-config");
174         serv_gets(buf);
175         if (buf[0] == '1') while (serv_gets(buf), strcmp(buf, "000")) {
176                 extract(ename, buf, 0);
177                 extract(etype, buf, 1);
178                 if (strlen(buf) == 0) {
179                         /* skip blank lines */
180                 }
181                 else if ((!strcasecmp(ename, bstr("ename")))
182                    &&   (!strcasecmp(etype, bstr("etype")))
183                    &&   (!strcasecmp(bstr("oper"), "delete"))
184                 ) {
185                         sprintf(WC->ImportantMessage, "%s deleted.", ename);
186                 }
187                 else {
188                         if (strlen(newconfig) > 0) strcat(newconfig, "\n");
189                         strcat(newconfig, buf);
190                 }
191         }
192
193         serv_printf("CONF PUTSYS|application/x-citadel-internet-config");
194         serv_gets(buf);
195         if (buf[0] == '4') {
196                 serv_puts(newconfig);
197                 if (!strcasecmp(bstr("oper"), "add")) {
198                         serv_printf("%s|%s", bstr("ename"), bstr("etype") );
199                         sprintf(WC->ImportantMessage, "%s added.", bstr("ename"));
200                 }
201                 serv_puts("000");
202         }
203         
204         display_inetconf();
205 }