indent -kr -i8 -brf -bbb -fnc -l132 -nce on all of webcit-classic
[citadel.git] / webcit / inetconf.c
1
2 /* 
3  * Functions which handle Internet domain configuration etc.
4  */
5
6 #include "webcit.h"
7 #include "webserver.h"
8
9
10 typedef enum _e_cfg {
11         ic_localhost,
12         ic_directory,
13         ic_smarthost,
14         ic_fallback,
15         ic_rbl,
16         ic_spamass,
17         ic_masq,
18         ic_clamav,
19         ic_notify,
20         ic_max
21 } ECfg;
22
23
24   /* These are server config keywords; do not localize! */
25 ConstStr CfgNames[] = {
26         { HKEY("localhost") },
27         { HKEY("directory") },
28         { HKEY("smarthost") },
29         { HKEY("fallbackhost") },
30         { HKEY("rbl") },
31         { HKEY("spamassassin") },
32         { HKEY("masqdomain") },
33         { HKEY("clamav") },
34         { HKEY("notify") }
35 };
36
37
38
39
40 /*
41  * display the inet config dialog 
42  */
43 void load_inetconf(void) {
44         wcsession *WCC = WC;
45         StrBuf *Buf, *CfgToken, *Value;
46         void *vHash;
47         HashList *Hash;
48         char nnn[64];
49         int i, len, nUsed;
50
51         WCC->InetCfg = NewHash(1, NULL);
52
53         for (i = 0; i < (sizeof(CfgNames) / sizeof(ConstStr)); i++) {
54                 Hash = NewHash(1, NULL);
55                 Put(WCC->InetCfg, CKEY(CfgNames[i]), Hash, HDeleteHash);
56         }
57
58         serv_printf("CONF GETSYS|application/x-citadel-internet-config");
59         Buf = NewStrBuf();
60         StrBuf_ServGetln(Buf);
61
62         if (GetServerStatus(Buf, NULL) == 1) {
63                 CfgToken = NewStrBuf();
64                 while ((len = StrBuf_ServGetln(Buf), ((len >= 0) && ((len != 3) || strcmp(ChrPtr(Buf), "000"))))) {
65                         Value = NewStrBuf();
66                         StrBufExtract_token(CfgToken, Buf, 1, '|');
67
68                         // VILE SLEAZY HACK: change obsolete "directory" domains to "localhost" domains
69                         if (!strcasecmp(ChrPtr(CfgToken), "directory")) {
70                                 FreeStrBuf(&CfgToken);
71                                 CfgToken = NewStrBufPlain(HKEY("localhost"));
72                         }
73
74                         StrBufExtract_token(Value, Buf, 0, '|');
75                         GetHash(WCC->InetCfg, ChrPtr(CfgToken), StrLength(CfgToken), &vHash);
76                         Hash = (HashList *) vHash;
77                         if (Hash == NULL) {
78                                 syslog(LOG_WARNING, "ERROR Loading inet config line: [%s]", ChrPtr(Buf));
79                                 FreeStrBuf(&Value);
80                                 continue;
81                         }
82                         nUsed = GetCount(Hash);
83                         nUsed = snprintf(nnn, sizeof(nnn), "%d", nUsed + 1);
84                         Put(Hash, nnn, nUsed, Value, HFreeStrBuf);
85                 }
86                 FreeStrBuf(&CfgToken);
87         }
88         FreeStrBuf(&Buf);
89 }
90
91
92 /*
93  * save changes to the inet config
94  */
95 void new_save_inetconf(void) {
96         wcsession *WCC = WC;
97         HashList *Hash;
98         StrBuf *Str;
99         StrBuf *Buf;
100         const StrBuf *eType, *eNum, *eName;
101         char nnn[64];
102         void *vHash, *vStr;
103         int i, nUsed;
104
105         load_inetconf();
106         eType = sbstr("etype");
107
108         GetHash(WCC->InetCfg, ChrPtr(eType), StrLength(eType), &vHash);
109         Hash = (HashList *) vHash;
110         if (Hash == NULL) {
111                 AppendImportantMessage(_("Invalid Parameter"), -1);
112                 url_do_template();
113                 return;
114         }
115
116         if (strcasecmp(bstr("oper"), "delete") == 0) {
117                 eNum = sbstr("ename");
118                 if (!GetHash(Hash, ChrPtr(eNum), StrLength(eNum), &vStr) || (vStr == NULL)) {
119                         AppendImportantMessage(_("Invalid Parameter"), -1);
120                         url_do_template();
121                         return;
122                 }
123
124                 Str = (StrBuf *) vStr;
125                 AppendImportantMessage(SKEY(Str));
126                 AppendImportantMessage(_(" has been deleted."), -1);
127                 FlushStrBuf(Str);
128         }
129         else if (!strcasecmp(bstr("oper"), "add")) {
130                 StrBuf *name;
131                 eName = sbstr("ename");
132                 if (eName == NULL) {
133                         AppendImportantMessage(_("Invalid Parameter"), -1);
134                         url_do_template();
135                         return;
136                 }
137
138                 nUsed = GetCount(Hash);
139                 nUsed = snprintf(nnn, sizeof(nnn), "%d", nUsed + 1);
140                 name = NewStrBufDup(eName);
141                 StrBufTrim(name);
142                 Put(Hash, nnn, nUsed, name, HFreeStrBuf);
143                 AppendImportantMessage(SKEY(eName));
144                 AppendImportantMessage( /*<domain> added status message */ _(" added."), -1);
145         }
146
147         Buf = NewStrBuf();
148         serv_printf("CONF PUTSYS|application/x-citadel-internet-config");
149         StrBuf_ServGetln(Buf);
150         if (GetServerStatus(Buf, NULL) == 4) {
151                 for (i = 0; i < (sizeof(CfgNames) / sizeof(ConstStr)); i++) {
152                         HashPos *where;
153                         const char *Key;
154                         long KeyLen;
155
156                         GetHash(WCC->InetCfg, CKEY(CfgNames[i]), &vHash);
157                         Hash = (HashList *) vHash;
158                         if (Hash == NULL) {
159                                 AppendImportantMessage(_("Invalid Parameter"), -1);
160                                 url_do_template();
161                                 return;
162                         }
163                         if (GetCount(Hash) > 0) {
164                                 where = GetNewHashPos(Hash, 0);
165                                 while (GetNextHashPos(Hash, where, &KeyLen, &Key, &vStr)) {
166                                         Str = (StrBuf *) vStr;
167                                         if ((Str != NULL) && (StrLength(Str) > 0))
168                                                 serv_printf("%s|%s", ChrPtr(Str), CfgNames[i].Key);
169                                 }
170                                 DeleteHashPos(&where);
171                         }
172                 }
173                 serv_puts("000");
174                 DeleteHash(&WCC->InetCfg);
175         }
176         FreeStrBuf(&Buf);
177         url_do_template();
178 }
179
180
181 void DeleteInetConfHash(StrBuf * Target, WCTemplputParams * TP) {
182         wcsession *WCC = WC;
183
184         if (WCC->InetCfg != NULL)
185                 DeleteHash(&WCC->InetCfg);
186
187 }
188
189
190 HashList *GetInetConfHash(StrBuf * Target, WCTemplputParams * TP) {
191         wcsession *WCC = WC;
192         void *vHash;
193
194         if (WCC->InetCfg == NULL)
195                 load_inetconf();
196         GetHash(WCC->InetCfg, TKEY(5), &vHash);
197         PutBstr(HKEY("__SERVCFG:INET:TYPE"), NewStrBufPlain(TKEY(5)));
198         return vHash;
199 }
200
201
202 HashList *GetValidDomainNames(StrBuf * Target, WCTemplputParams * TP) {
203         StrBuf *Line;
204         HashList *ValidDomainNames = NULL;
205         long State;
206         int gvdnlevel = 0;
207
208         serv_printf("GVDN %d", gvdnlevel);
209         Line = NewStrBuf();
210         StrBuf_ServGetln(Line);
211         if (GetServerStatus(Line, &State) == 1) {
212                 int Done = 0;
213                 int n = 0;
214
215                 ValidDomainNames = NewHash(1, NULL);
216                 while (!Done && (StrBuf_ServGetln(Line) >= 0))
217                         if ((StrLength(Line) == 3) && !strcmp(ChrPtr(Line), "000")) {
218                                 Done = 1;
219                         }
220                         else {
221                                 Put(ValidDomainNames, IKEY(n), NewStrBufDup(Line), HFreeStrBuf);
222                                 n++;    /* #0 is the type... */
223                         }
224         }
225         else if (State == 550)
226                 AppendImportantMessage(_("Higher access is required to access this function."), -1);
227
228         FreeStrBuf(&Line);
229
230         return ValidDomainNames;
231 }
232
233
234
235 void InitModule_INETCONF(void) {
236         WebcitAddUrlHandler(HKEY("save_inetconf"), "", 0, new_save_inetconf, 0);
237         RegisterIterator("SERVCFG:INET", 1, NULL, GetInetConfHash, NULL, NULL, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
238         RegisterNamespace("SERVCFG:FLUSHINETCFG", 0, 0, DeleteInetConfHash, NULL, CTX_NONE);
239         RegisterIterator("ITERATE:VALID:DOMAINNAMES", 1, NULL, GetValidDomainNames, NULL, DeleteHash, CTX_STRBUF, CTX_NONE,
240                          IT_NOFLAG);
241 }