554a74c6474e041736980cf8cfeafd815a595246
[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),
65                         ((len >= 0) && 
66                          ((len != 3) ||
67                           strcmp(ChrPtr(Buf), "000")))))
68                 {
69                         Value = NewStrBuf();
70  
71                         StrBufExtract_token(CfgToken, Buf, 1, '|');
72                         StrBufExtract_token(Value, Buf, 0, '|');
73                         GetHash(WCC->InetCfg, ChrPtr(CfgToken), StrLength(CfgToken), &vHash);
74                         Hash = (HashList*) vHash;
75                         if (Hash == NULL) {
76                                 syslog(LOG_WARNING, "ERROR Loading inet config line: [%s]\n", 
77                                         ChrPtr(Buf));
78                                 FreeStrBuf(&Value);
79                                 continue;
80                         }
81                         nUsed = GetCount(Hash);
82                         nUsed = snprintf(nnn, sizeof(nnn), "%d", nUsed+1);
83                         Put(Hash, nnn, nUsed, Value, HFreeStrBuf); 
84                 }
85                 FreeStrBuf(&CfgToken);
86         }
87         FreeStrBuf(&Buf);
88 }
89
90
91 /*
92  * save changes to the inet config
93  */
94 void new_save_inetconf(void) {
95         wcsession *WCC = WC;
96         HashList *Hash;
97         StrBuf *Str;
98         StrBuf *Buf;
99         const StrBuf *eType, *eNum, *eName;
100         char nnn[64];
101         void *vHash, *vStr;
102         int i, nUsed;
103
104         load_inetconf();
105         eType = sbstr("etype");
106
107         GetHash(WCC->InetCfg, ChrPtr(eType), StrLength(eType), &vHash);
108         Hash = (HashList*) vHash;
109         if (Hash == NULL) {
110                 AppendImportantMessage(_("Invalid Parameter"), -1);
111                 url_do_template();
112                 return;
113         }
114
115         if (strcasecmp(bstr("oper"), "delete") == 0) {
116                 eNum = sbstr("ename");
117                 if (!GetHash(Hash, ChrPtr(eNum), StrLength(eNum), &vStr) ||
118                     (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", 
169                                                             ChrPtr(Str),
170                                                             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 void DeleteInetConfHash(StrBuf *Target, WCTemplputParams *TP)
183 {
184         wcsession *WCC = WC;
185
186         if (WCC->InetCfg != NULL)
187                 DeleteHash(&WCC->InetCfg);
188
189 }
190
191
192 HashList *GetInetConfHash(StrBuf *Target, WCTemplputParams *TP)
193 {
194         wcsession *WCC = WC;
195         void *vHash;
196
197         if (WCC->InetCfg == NULL)
198                 load_inetconf();
199         GetHash(WCC->InetCfg, TKEY(5), &vHash);
200         PutBstr(HKEY("__SERVCFG:INET:TYPE"), NewStrBufPlain(TKEY(5)));
201         return vHash;
202 }
203
204
205 HashList *GetValidDomainNames(StrBuf *Target, WCTemplputParams *TP) 
206 {
207         StrBuf *Line;
208         HashList *ValidDomainNames = NULL;
209         long State;
210         int gvdnlevel = 0;
211         
212         serv_printf("GVDN %d", gvdnlevel);
213         Line = NewStrBuf();
214         StrBuf_ServGetln(Line);
215         if (GetServerStatus(Line, &State) == 1) 
216         {
217                 int Done = 0;
218                 int n = 0;
219
220                 ValidDomainNames = NewHash(1, NULL);
221                 while(!Done && (StrBuf_ServGetln(Line) >= 0))
222                         if ( (StrLength(Line)==3) && 
223                              !strcmp(ChrPtr(Line), "000"))
224                         {
225                                 Done = 1;
226                         }
227                         else
228                         {
229                                 Put(ValidDomainNames, 
230                                     IKEY(n),
231                                     NewStrBufDup(Line), 
232                                     HFreeStrBuf);
233                                 n++; /* #0 is the type... */
234                         }
235         }
236         else if (State == 550)
237                 AppendImportantMessage(_("Higher access is required to access this function."), -1);
238
239         FreeStrBuf(&Line);
240
241         return ValidDomainNames;
242 }
243
244
245
246 void 
247 InitModule_INETCONF
248 (void)
249 {
250         WebcitAddUrlHandler(HKEY("save_inetconf"), "", 0, new_save_inetconf, 0);
251         RegisterIterator("SERVCFG:INET", 1, NULL, GetInetConfHash, NULL, NULL, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
252         RegisterNamespace("SERVCFG:FLUSHINETCFG",0, 0, DeleteInetConfHash, NULL, CTX_NONE);
253         RegisterIterator("ITERATE:VALID:DOMAINNAMES", 1, NULL, GetValidDomainNames, NULL, DeleteHash, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
254 }