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