191f90e76787a6b3525f6893654446c411d90869
[citadel.git] / webcit / inetconf.c
1 /* 
2  * $Id$
3  *
4  * Functions which handle Internet domain configuration etc.
5  */
6
7 #include "webcit.h"
8 #include "webserver.h"
9
10 /*
11  * display the inet config dialog 
12  */
13 void display_inetconf(void)
14 {
15         char buf[SIZ];
16         char ename[SIZ];
17         char etype[SIZ];
18         int i;
19         int which;
20         int bg = 0;
21
22         enum {
23                 ic_localhost,
24                 ic_directory,
25                 ic_smarthost,
26                 ic_rbl,
27                 ic_spamass,
28                 ic_masq,
29                 ic_clamav,
30                 ic_max
31         };
32
33         char *ic_spec[ic_max];
34         char *ic_misc;
35         char *ic_keyword[ic_max];
36         char *ic_boxtitle[ic_max];
37         char *ic_desc[ic_max];
38
39         /* These are server config keywords; do not localize! */
40         ic_keyword[0] = "localhost";
41         ic_keyword[1] = "directory";
42         ic_keyword[2] = "smarthost";
43         ic_keyword[3] = "rbl";
44         ic_keyword[4] = "spamassassin";
45         ic_keyword[5] = "masqdomain";
46         ic_keyword[6] = "clamav";
47
48         ic_boxtitle[0] = _("Local host aliases");
49         ic_boxtitle[1] = _("Directory domains");
50         ic_boxtitle[2] = _("Smart hosts");
51         ic_boxtitle[3] = _("RBL hosts");
52         ic_boxtitle[4] = _("SpamAssassin hosts");
53         ic_boxtitle[5] = _("Masqueradable domains");
54         ic_boxtitle[6] = _("ClamAV clamd hosts");
55
56         ic_desc[0] = _("(domains for which this host receives mail)");
57         ic_desc[1] = _("(domains mapped with the Global Address Book)");
58         ic_desc[2] = _("(if present, forward all outbound mail to one of these hosts)");
59         ic_desc[3] = _("(hosts running a Realtime Blackhole List)");
60         ic_desc[4] = _("(hosts running the SpamAssassin service)");
61         ic_desc[5] = _("(Domains as which users are allowed to masquerade)");
62         ic_desc[6] = _("(hosts running the ClamAV clamd service)");
63
64         for (i=0; i<ic_max; ++i) {
65                 ic_spec[i] = strdup("");
66         }
67
68         ic_misc = strdup("");
69
70         serv_printf("CONF GETSYS|application/x-citadel-internet-config");
71         serv_getln(buf, sizeof buf);
72         if (buf[0] == '1') while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
73
74                 extract_token(ename, buf, 0, '|', sizeof ename);
75                 extract_token(etype, buf, 1, '|', sizeof etype);
76                 which = (-1);
77                 for (i=0; i<ic_max; ++i) {
78                         if (!strcasecmp(etype, ic_keyword[i])) {
79                                 which = i;
80                         }
81                 }
82
83                 if (which >= 0) {
84                         ic_spec[which] = realloc(ic_spec[which], strlen(ic_spec[which]) + strlen(ename) + 2);
85                         if (!IsEmptyStr(ic_spec[which])) strcat(ic_spec[which], "\n");
86                         strcat(ic_spec[which], ename);
87                 }
88                 else {
89                         ic_misc = realloc(ic_misc, strlen(ic_misc) + strlen(buf) + 2);
90                         if (!IsEmptyStr(ic_misc)) strcat(ic_misc, "\n");
91                         strcat(ic_misc, buf);
92                 }
93
94         }
95
96         output_headers(1, 1, 2, 0, 0, 0);
97         wprintf("<div id=\"banner\">\n");
98         wprintf("<h1>");
99         wprintf(_("Internet configuration"));
100         wprintf("</h1>");
101         wprintf("</div>\n");
102
103         wprintf("<div id=\"content\" class=\"service\">\n");
104
105         wprintf("<div class=\"fix_scrollbar_bug\">"
106                 "<table border=0 width=100%% cellspacing=\"10\" cellpadding=\"10\"> "
107                 "<tr><td valign=top width=50%%>\n");
108         for (which=0; which<ic_max; ++which) {
109                 if (which == (ic_max / 2)) {
110                         wprintf("</td><td valign=top>");
111                 }
112                 svput("BOXTITLE", WCS_STRING, ic_boxtitle[which]);
113                 do_template("beginboxx", NULL);
114                 wprintf("<span class=\"menudesc\">");
115                 escputs(ic_desc[which]);
116                 wprintf("</span><br />");
117                 wprintf("<table border=0 cellspacing=\"2px\" cellpadding=\"2px\" width=94%% "
118                         "class=\"altern\" >\n");
119                 bg = 0;
120                 if (!IsEmptyStr(ic_spec[which])) {
121                         for (i=0; i<num_tokens(ic_spec[which], '\n'); ++i) {
122                                 bg = 1 - bg;
123                                 wprintf("<tr class=\"%s\">",
124                                         (bg ? "even" : "odd")
125                                 );
126                                 wprintf("<td align=left>");
127                                 extract_token(buf, ic_spec[which], i, '\n', sizeof buf);
128                                 escputs(buf);
129                                 wprintf("</td><td align=left>"
130                                         "<span class=\"button_link\">"
131                                         "<a href=\"save_inetconf?oper=delete&ename=");
132                                 escputs(buf);
133                                 wprintf("&etype=%s\" ", ic_keyword[which]);
134                                 wprintf("onClick=\"return confirm('%s');\">",
135                                         _("Delete this entry?"));
136                                 wprintf(_("Delete"));
137                                 wprintf("</a></span></td></tr>\n");
138                         }
139
140                 }
141                 wprintf("<form method=\"post\" action=\"save_inetconf\">\n");
142                 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
143                 wprintf("<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=left>"
147                         "<input type=\"submit\" name=\"oper\" value=\"Add\">"
148                         "</td></tr></table></form>\n");
149                 do_template("endbox", NULL);
150                 wprintf("<br />");
151         }
152         wprintf("</td></tr></table></div>\n");
153         wDumpContent(1);
154
155         for (i=0; i<ic_max; ++i) {
156                 free(ic_spec[i]);
157         }
158         free(ic_misc);
159 }
160
161
162
163 /*
164  * save changes to the inet config
165  */
166 void save_inetconf(void) {
167         char *buf;
168         char *ename;
169         char *etype;
170         char *newconfig;
171
172         buf = malloc(SIZ);
173         ename = malloc(SIZ);
174         etype = malloc(SIZ);
175         newconfig = malloc(65536);
176
177         strcpy(newconfig, "");
178         serv_printf("CONF GETSYS|application/x-citadel-internet-config");
179         serv_getln(buf, SIZ);
180         if (buf[0] == '1') while (serv_getln(buf, SIZ), strcmp(buf, "000")) {
181                 extract_token(ename, buf, 0, '|', SIZ);
182                 extract_token(etype, buf, 1, '|', SIZ);
183                 if (IsEmptyStr(buf)) {
184                         /* skip blank lines */
185                 }
186                 else if ((!strcasecmp(ename, bstr("ename")))
187                    &&   (!strcasecmp(etype, bstr("etype")))
188                    &&   (!strcasecmp(bstr("oper"), "delete"))
189                 ) {
190                         sprintf(WC->ImportantMessage, _("%s has been deleted."), ename);
191                 }
192                 else {
193                         if (!IsEmptyStr(newconfig)) strcat(newconfig, "\n");
194                         strcat(newconfig, buf);
195                 }
196         }
197
198         serv_printf("CONF PUTSYS|application/x-citadel-internet-config");
199         serv_getln(buf, SIZ);
200         if (buf[0] == '4') {
201                 serv_puts(newconfig);
202                 if (!strcasecmp(bstr("oper"), "add")) {
203                         serv_printf("%s|%s", bstr("ename"), bstr("etype") );
204                         sprintf(WC->ImportantMessage, _("%s added."), bstr("ename"));
205                 }
206                 serv_puts("000");
207         }
208         
209         display_inetconf();
210
211         free(buf);
212         free(ename);
213         free(etype);
214         free(newconfig);
215 }
216
217 typedef enum _e_cfg {
218         ic_localhost,
219         ic_directory,
220         ic_smarthost,
221         ic_rbl,
222         ic_spamass,
223         ic_masq,
224         ic_clamav,
225         ic_max
226 } ECfg;
227
228 typedef struct _ConstStrBuf {
229         const char *name;
230         size_t len;
231 } ConstStrBuf;
232
233
234   /* These are server config keywords; do not localize! */
235 ConstStrBuf CfgNames[] = {
236         { HKEY("localhost") },
237         { HKEY("directory") },
238         { HKEY("smarthost") },
239         { HKEY("rbl") },
240         { HKEY("spamassassin") },
241         { HKEY("masqdomain") },
242         { HKEY("clamav") }
243 };
244
245         
246
247
248 /*
249  * display the inet config dialog 
250  */
251 void load_inetconf(void)
252 {
253         struct wcsession *WCC = WC;
254         StrBuf *Buf, *Token, *Value;
255         void *vHash;
256         HashList *Hash;
257         char nnn[64];
258         char buf[SIZ];
259         int i, len, nUsed;
260         
261         WCC->InetCfg = NewHash(1, NULL);
262
263         for (i = 0; i < (sizeof(CfgNames) / sizeof(ConstStrBuf)); i++) {
264                 Hash = NewHash(1, NULL);
265                 Put(WCC->InetCfg, CfgNames[i].name, CfgNames[i].len, Hash, HDeleteHash);
266         }
267
268         serv_printf("CONF GETSYS|application/x-citadel-internet-config");
269         serv_getln(buf, sizeof buf);
270         
271         if (buf[0] == '1') {
272                 Buf = NewStrBuf();
273                 Token = NewStrBuf();
274                 while ((len = StrBuf_ServGetln(Buf),
275                         strcmp(ChrPtr(Buf), "000"))) {
276                         Value = NewStrBuf();
277  
278                         StrBufExtract_token(Token, Buf, 1, '|');
279                         StrBufExtract_token(Value, Buf, 0, '|');
280                         GetHash(WCC->InetCfg, ChrPtr(Token), StrLength(Token), &vHash);
281                         Hash = (HashList*) vHash;
282                         if (Hash == NULL) {
283                                 lprintf(1, "ERROR Loading inet config line: [%s]\n", 
284                                         ChrPtr(Buf));
285                                 FreeStrBuf(&Value);
286                                 continue;
287                         }
288                         nUsed = GetCount(Hash);
289                         nUsed = snprintf(nnn, sizeof(nnn), "%d", nUsed+1);
290                         Put(Hash, nnn, nUsed, Value, HFreeStrBuf); 
291                 }
292                 FreeStrBuf(&Buf);
293                 FreeStrBuf(&Token);
294         }
295 }
296
297
298 /*
299  * save changes to the inet config
300  */
301 void new_save_inetconf(void) {
302         struct wcsession *WCC = WC;
303         HashList *Hash;
304         StrBuf *Str;
305         const StrBuf *eType, *eNum, *eName;
306         char nnn[64];
307         void *vHash, *vStr;
308         char buf[SIZ];
309         int i, nUsed;
310
311         load_inetconf();
312         eType = sbstr("etype");
313
314         GetHash(WCC->InetCfg, ChrPtr(eType), StrLength(eType), &vHash);
315         Hash = (HashList*) vHash;
316         if (Hash == NULL) {
317                 sprintf(WC->ImportantMessage, _("Invalid Parameter"));
318                 url_do_template();
319                 return;
320         }
321
322         if (strcasecmp(bstr("oper"), "delete") == 0) {
323                 eNum = sbstr("ename");
324                 if (!GetHash(Hash, ChrPtr(eNum), StrLength(eNum), &vStr) ||
325                     (vStr == NULL)) {
326                         sprintf(WC->ImportantMessage, _("Invalid Parameter"));
327                         url_do_template();
328                         return;
329                 }
330
331                 Str = (StrBuf*)vStr;
332                 sprintf(WC->ImportantMessage, _("%s has been deleted."), ChrPtr(Str));
333                 FlushStrBuf(Str);       
334         }
335         else if (!strcasecmp(bstr("oper"), "add")) {
336                 eName = sbstr("ename");
337                 if (eName == NULL) {
338                         sprintf(WC->ImportantMessage, _("Invalid Parameter"));
339                         url_do_template();
340                         return;
341                 }
342
343                 nUsed = GetCount(Hash);
344                 nUsed = snprintf(nnn, sizeof(nnn), "%d", nUsed+1);
345         
346                 Put(Hash, nnn, nUsed, NewStrBufDup(eName), HFreeStrBuf); 
347                 sprintf(WC->ImportantMessage, "%s added.", ChrPtr(eName));
348         }
349
350         serv_printf("CONF PUTSYS|application/x-citadel-internet-config");
351         serv_getln(buf, SIZ);
352         if (buf[0] == '4') {
353                 for (i = 0; i < (sizeof(CfgNames) / sizeof(ConstStrBuf)); i++) {
354                         HashPos *where;
355                         const char *Key;
356                         long KeyLen;
357
358                         GetHash(WCC->InetCfg, CfgNames[i].name, CfgNames[i].len, &vHash);
359                         Hash = (HashList*) vHash;
360                         if (Hash == NULL) {
361                                 sprintf(WC->ImportantMessage, _("Invalid Parameter"));
362                                 url_do_template();
363                                 return;
364                         }
365                         if (GetCount(Hash) > 0) {
366                                 where = GetNewHashPos();
367                                 while (GetNextHashPos(Hash, where, &KeyLen, &Key, &vStr)) {
368                                         Str = (StrBuf*) vStr;
369                                         if ((Str!= NULL) && (StrLength(Str) > 0))
370                                                 serv_printf("%s|%s", 
371                                                             ChrPtr(Str),
372                                                             CfgNames[i].name); 
373                                 }
374                                 DeleteHashPos(&where);
375                         }                       
376                 }
377                 serv_puts("000");
378                 DeleteHash(&WCC->InetCfg);
379         }
380         
381         url_do_template();
382 }
383
384 void InetCfgSubst(StrBuf *TemplBuffer, void *vContext, WCTemplateToken *Token)
385 {
386         SVPutBuf("SERVCFG:INET:HOSTNAME", vContext, 1);
387 }
388
389 void DeleteInetConfHash(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType)
390 {
391         struct wcsession *WCC = WC;
392
393         if (WCC->InetCfg != NULL)
394                 DeleteHash(&WCC->InetCfg);
395
396 }
397
398
399 HashList *GetInetConfHash(StrBuf *Target, int nArgs, WCTemplateToken *Tokens, void *Context, int ContextType)
400 {
401         struct wcsession *WCC = WC;
402         void *vHash;
403
404         if (WCC->InetCfg == NULL)
405                 load_inetconf();
406         GetHash(WCC->InetCfg, 
407                 Tokens->Params[2]->Start, 
408                 Tokens->Params[2]->len,
409                 &vHash);
410         svprintf(HKEY("SERVCFG:INET:TYPE"), WCS_STRING, Tokens->Params[2]->Start);
411         return vHash;
412 }
413
414 void 
415 InitModule_INETCONF
416 (void)
417 {
418         WebcitAddUrlHandler(HKEY("display_inetconf"), display_inetconf, 0);
419         WebcitAddUrlHandler(HKEY("save_inetconf"), new_save_inetconf, AJAX);
420         RegisterIterator("SERVCFG:INET", 1, NULL, GetInetConfHash, InetCfgSubst, NULL, CTX_INETCFG, CTX_NONE);
421         RegisterNamespace("SERVCFG:FLUSHINETCFG",0, 0, DeleteInetConfHash, CTX_NONE);
422 }