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