Trivial commit to test automated builds
[citadel] / webcit / siteconfig.c
1 /*
2  * Administrative screen for site-wide configuration
3  *
4  * Copyright (c) 1996-2021 by the citadel.org team
5  *
6  * This program is open source software.  You can redistribute it and/or
7  * modify it under the terms of the GNU General Public License, version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "webcit.h"
16 #include "webserver.h"
17
18 /*
19  * Expiry policy for the autopurger
20  */
21 #define EXPIRE_NEXTLEVEL        0       /* Inherit expiration policy    */
22 #define EXPIRE_MANUAL           1       /* Don't expire messages at all */
23 #define EXPIRE_NUMMSGS          2       /* Keep only latest n messages  */
24 #define EXPIRE_AGE              3       /* Expire messages after n days */
25
26 CtxType CTX_SRVLOG = CTX_NONE;
27
28 HashList *ZoneHash = NULL;
29
30 ConstStr ExpirePolicyString = {CStrOf(roompolicy)     };
31
32 ConstStr ExpirePolicyStrings[][2] = {
33         { { CStrOf(roompolicy)     } , { strof(roompolicy)     "_value", sizeof(strof(roompolicy)     "_value") - 1 } },
34         { { CStrOf(floorpolicy)    } , { strof(floorpolicy)    "_value", sizeof(strof(floorpolicy)    "_value") - 1 } },
35         { { CStrOf(sitepolicy)     } , { strof(sitepolicy)     "_value", sizeof(strof(sitepolicy)     "_value") - 1 } },
36         { { CStrOf(mailboxespolicy)} , { strof(mailboxespolicy)"_value", sizeof(strof(mailboxespolicy)"_value") - 1 } }
37 };
38
39 void LoadExpirePolicy(GPEXWhichPolicy which)
40 {
41         StrBuf *Buf;
42         long State;
43         const char *Pos = NULL;
44
45         serv_printf("GPEX %s", ExpirePolicyStrings[which][0].Key);
46         Buf = NewStrBuf();
47         StrBuf_ServGetln(Buf);
48         if (GetServerStatus(Buf, &State) == 2) {
49                 Pos = ChrPtr(Buf) + 4;
50                 WC->Policy[which].expire_mode = StrBufExtractNext_long(Buf, &Pos, '|');
51                 WC->Policy[which].expire_value = StrBufExtractNext_long(Buf, &Pos, '|');
52         }
53         else if (State == 550)
54                 AppendImportantMessage(_("Higher access is required to access this function."), -1);
55         FreeStrBuf(&Buf);
56 }
57
58 void SaveExpirePolicyFromHTTP(GPEXWhichPolicy which)
59 {
60         StrBuf *Buf;
61         long State;
62
63         serv_printf("SPEX %s|%d|%d", 
64                             ExpirePolicyStrings[which][0].Key,
65                     ibcstr( ExpirePolicyStrings[which][1] ),
66                     ibcstr( ExpirePolicyStrings[which][1] )  );
67
68         Buf = NewStrBuf();
69         StrBuf_ServGetln(Buf);
70         GetServerStatus(Buf, &State);
71         if (State == 550)
72                 AppendImportantMessage(_("Higher access is required to access this function."), -1);
73         FreeStrBuf(&Buf);
74 }
75
76 int ConditionalExpire(StrBuf *Target, WCTemplputParams *TP)
77 {
78         GPEXWhichPolicy which;
79         int CompareWith;
80
81         which = GetTemplateTokenNumber(Target, TP, 2, 0);
82         CompareWith = GetTemplateTokenNumber(Target, TP, 3, 0);
83
84         LoadExpirePolicy(which);
85         
86         return WC->Policy[which].expire_mode == CompareWith;
87 }
88
89 void tmplput_ExpireValue(StrBuf *Target, WCTemplputParams *TP)
90 {
91         GPEXWhichPolicy which;
92                 
93         which = GetTemplateTokenNumber(Target, TP, 0, 0);
94         LoadExpirePolicy(which);
95         StrBufAppendPrintf(Target, "%d", WC->Policy[which].expire_value);
96 }
97
98
99 void tmplput_ExpireMode(StrBuf *Target, WCTemplputParams *TP)
100 {
101         GPEXWhichPolicy which;
102                 
103         which = GetTemplateTokenNumber(Target, TP, 2, 0);
104         LoadExpirePolicy(which);
105         StrBufAppendPrintf(Target, "%d", WC->Policy[which].expire_mode);
106 }
107
108
109 void LoadZoneFiles(void)
110 {
111         icalarray *zones;
112         int z;
113         long len;
114         const char *this_zone;
115         StrBuf *ZName;
116         
117         ZoneHash = NewHash(1, NULL);
118         ZName = NewStrBufPlain(HKEY("UTC"));
119         Put(ZoneHash, HKEY("UTC"), ZName, HFreeStrBuf);
120         zones = icaltimezone_get_builtin_timezones();
121         for (z = 0; z < zones->num_elements; ++z) {
122                 /* syslog(LOG_DEBUG, "Location: %-40s tzid: %s\n",
123                         icaltimezone_get_location(icalarray_element_at(zones, z)),
124                         icaltimezone_get_tzid(icalarray_element_at(zones, z))
125                 ); */
126                 this_zone = icaltimezone_get_location(icalarray_element_at(zones, z));
127                 len = strlen(this_zone);
128                 ZName = NewStrBufPlain(this_zone, len);
129                 Put(ZoneHash, this_zone, len, ZName, HFreeStrBuf);
130         }
131         SortByHashKey(ZoneHash, 0);
132 }
133
134
135 typedef struct _CfgMapping {
136         int type;
137         int min;
138         int max;
139         const char *defval;
140         const char *Key;
141         long len;
142 } CfgMapping;
143
144 #define CFG_STR 1
145 #define CFG_YES 2
146 #define CFG_NO 3
147 #define CFG_INT 4
148
149 CfgMapping ServerConfig[] = {
150         {CFG_STR, 0, 0, "", HKEY("c_nodename")},
151         {CFG_STR, 0, 0, "", HKEY("c_fqdn")},
152         {CFG_STR, 0, 0, "", HKEY("c_humannode")},
153         {CFG_STR, 0, 0, "", HKEY("c_phonenum")},
154         {CFG_YES, 0, 0, "", HKEY("c_creataide")},
155         {CFG_STR, 0, 0, "", HKEY("c_sleeping")},
156         {CFG_STR, 0, 0, "", HKEY("c_initax")},
157         {CFG_YES, 0, 0, "", HKEY("c_regiscall")},
158         {CFG_YES, 0, 0, "", HKEY("c_twitdetect")},
159         {CFG_STR, 0, 0, "", HKEY("c_twitroom")},
160         {CFG_STR, 0, 0, "", HKEY("c_moreprompt")},
161         {CFG_YES, 0, 0, "", HKEY("c_restrict")},
162         {CFG_STR, 0, 0, "", HKEY("c_bbs_city")},
163         {CFG_STR, 0, 0, "", HKEY("c_sysadm")},
164         {CFG_STR, 0, 0, "", HKEY("c_maxsessions")},
165         {CFG_STR, 0, 0, "", HKEY("reserved1")},
166         {CFG_STR, 0, 0, "", HKEY("c_userpurge")},
167         {CFG_STR, 0, 0, "", HKEY("c_roompurge")},
168         {CFG_STR, 0, 0, "", HKEY("c_logpages")},
169         {CFG_STR, 0, 0, "", HKEY("c_createax")},
170         {CFG_STR, 0, 0, "", HKEY("c_maxmsglen")},
171         {CFG_STR, 0, 0, "", HKEY("c_min_workers")},
172         {CFG_STR, 0, 0, "", HKEY("c_max_workers")},
173         {CFG_STR, 0, 0, "", HKEY("c_pop3_port")},
174         {CFG_STR, 0, 0, "", HKEY("c_smtp_port")},
175         {CFG_INT, CFG_SMTP_FROM_FILTERALL, CFG_SMTP_FROM_REJECT, "0", HKEY("c_rfc822_strict_from")},    
176         {CFG_YES, 0, 0, "", HKEY("c_aide_zap")},
177         {CFG_STR, 0, 0, "", HKEY("c_imap_port")},
178         {CFG_STR, 0, 0, "", HKEY("c_net_freq")},
179         {CFG_YES, 0, 0, "", HKEY("c_disable_newu")},
180         {CFG_STR, 0, 0, "", HKEY("reserved2")},
181         {CFG_STR, 0, 0, "", HKEY("c_purge_hour")},
182         {CFG_STR, 0, 0, "", HKEY("c_ldap_host")},
183         {CFG_STR, 0, 0, "", HKEY("c_ldap_port")},
184         {CFG_STR, 0, 0, "", HKEY("c_ldap_base_dn")},
185         {CFG_STR, 0, 0, "", HKEY("c_ldap_bind_dn")},
186         {CFG_STR, 0, 0, "", HKEY("c_ldap_bind_pw")},
187         {CFG_STR, 0, 0, "", HKEY("c_ip_addr")},
188         {CFG_STR, 0, 0, "", HKEY("c_msa_port")},
189         {CFG_STR, 0, 0, "", HKEY("c_imaps_port")},
190         {CFG_STR, 0, 0, "", HKEY("c_pop3s_port")},
191         {CFG_STR, 0, 0, "", HKEY("c_smtps_port")},
192         {CFG_YES, 0, 0, "", HKEY("c_enable_fulltext")},
193         {CFG_YES, 0, 0, "", HKEY("c_auto_cull")},
194         {CFG_YES, 0, 0, "", HKEY("reserved3")},
195         {CFG_YES, 0, 0, "", HKEY("c_allow_spoofing")},
196         {CFG_YES, 0, 0, "", HKEY("c_journal_email")},
197         {CFG_YES, 0, 0, "", HKEY("c_journal_pubmsgs")},
198         {CFG_STR, 0, 0, "", HKEY("c_journal_dest")},
199         {CFG_STR, 0, 0, "", HKEY("c_default_cal_zone")},
200         {CFG_STR, 0, 0, "", HKEY("c_pftcpdict_port")},
201         {CFG_STR, 0, 0, "", HKEY("c_mgesve_port")},
202         {CFG_STR, 0, 0, "", HKEY("c_auth_mode")},
203         {CFG_STR, 0, 0, "", HKEY("c_funambol_host")},
204         {CFG_STR, 0, 0, "", HKEY("c_funambol_port")},
205         {CFG_STR, 0, 0, "", HKEY("c_funambol_source")},
206         {CFG_STR, 0, 0, "", HKEY("c_funambol_auth")},
207         {CFG_YES, 0, 0, "", HKEY("c_rbl_at_greeting")},
208         {CFG_STR, 0, 0, "", HKEY("c_master_user")},
209         {CFG_STR, 0, 0, "", HKEY("c_master_pass")},
210         {CFG_STR, 0, 0, "", HKEY("c_pager_program")},
211         {CFG_YES, 0, 0, "", HKEY("c_imap_keep_from")},
212         {CFG_STR, 0, 0, "", HKEY("c_xmpp_c2s_port")},
213         {CFG_STR, 0, 0, "", HKEY("c_xmpp_s2s_port")},
214         {CFG_STR, 0, 0, "", HKEY("c_pop3_fetch")},
215         {CFG_STR, 0, 0, "", HKEY("c_pop3_fastest")},
216         {CFG_YES, 0, 0, "", HKEY("c_spam_flag_only")},
217         {CFG_YES, 0, 0, "", HKEY("c_guest_logins")},
218         {CFG_STR, 0, 0, "", HKEY("c_port_number")},
219         {CFG_STR, 0, 0, "", HKEY("c_ctdluid")},
220         {CFG_STR, 0, 0, "", HKEY("c_nntp_port")},
221         {CFG_STR, 0, 0, "", HKEY("c_nntps_port")}
222 };
223
224
225
226 /*
227  *  display all configuration items
228  */
229 void load_siteconfig(void)
230 {
231         StrBuf *Buf;
232         HashList *Cfg;
233         long len;
234         int i, j;
235         
236         if (WC->ServCfg == NULL)
237                 WC->ServCfg = NewHash(1, NULL);
238         Cfg = WC->ServCfg;
239
240         Buf = NewStrBuf();
241
242         serv_printf("CONF get");
243         StrBuf_ServGetln(Buf);
244         if (GetServerStatus(Buf, NULL) != 1) {
245                 StrBufCutLeft(Buf, 4);
246                 AppendImportantMessage(SKEY(Buf));
247                 FreeStrBuf(&Buf);
248                 return;
249                 
250         }
251         j = i = 0;
252         while (len = StrBuf_ServGetln(Buf),
253                (len >= 0) && 
254                ((len != 3) || strcmp(ChrPtr(Buf), "000")))
255         {
256                 if (i < (sizeof(ServerConfig) / sizeof(CfgMapping)))
257                 {
258                         Put(Cfg,
259                             ServerConfig[i].Key, 
260                             ServerConfig[i].len, 
261                             Buf, 
262                             HFreeStrBuf);
263                         i++;
264                         Buf = NewStrBuf();
265                 }
266                 else {
267                         if (j == 0) {
268                                 syslog(LOG_WARNING, "The server sent more configuration data than this version of webcit is capable of changing.  Unknown configuration values will remain unchanged.");
269                         }
270                         j++;
271                 }
272         }
273         FreeStrBuf(&Buf);
274
275         LoadExpirePolicy(roompolicy);
276         LoadExpirePolicy(floorpolicy);
277         LoadExpirePolicy(mailboxespolicy);
278         LoadExpirePolicy(sitepolicy);
279 }
280
281
282
283 /*
284  * parse siteconfig changes 
285  */
286 void siteconfig(void)
287 {
288         int i, value;
289         StrBuf *Line;
290
291         if (strlen(bstr("ok_button")) == 0) {
292                 display_aide_menu();
293                 return;
294         }
295         Line = NewStrBuf();
296         serv_printf("CONF set");
297         StrBuf_ServGetln(Line);
298         if (GetServerStatusMsg(Line, NULL, 1, 4) != 4) {
299                 display_aide_menu();
300                 FreeStrBuf(&Line);
301                 return;
302         }
303
304         FreeStrBuf(&Line);
305
306         for (i=0; i < (sizeof(ServerConfig) / sizeof(CfgMapping)); i ++)
307         {
308                 switch (ServerConfig[i].type) {
309                 default:
310                 case CFG_STR:
311                         serv_putbuf(SBstr(ServerConfig[i].Key, ServerConfig[i].len));
312                         break;
313                 case CFG_YES:
314                         serv_puts(YesBstr(ServerConfig[i].Key, 
315                                           ServerConfig[i].len) ?
316                                   "1" : "0");
317                         break;
318                 case CFG_NO:
319                         serv_puts(YesBstr(ServerConfig[i].Key, 
320                                           ServerConfig[i].len) ?
321                                   "0" : "1");
322                         break;
323                 case CFG_INT:
324                         value = IBstr(ServerConfig[i].Key, 
325                                       ServerConfig[i].len);
326                         if ((value < ServerConfig[i].min) ||
327                             (value > ServerConfig[i].max))
328                                 value = atol(ServerConfig[i].defval);
329                         serv_printf("%d", value);
330                         break;
331                 }
332         }
333         serv_puts("000");
334
335         SaveExpirePolicyFromHTTP(sitepolicy);
336         SaveExpirePolicyFromHTTP(mailboxespolicy);
337
338         FreeStrBuf(&WC->serv_info->serv_default_cal_zone);
339         WC->serv_info->serv_default_cal_zone = NewStrBufDup(sbstr("c_default_cal_zone"));
340
341         AppendImportantMessage(_("Your system configuration has been updated."), -1);
342         DeleteHash(&WC->ServCfg);
343         display_aide_menu();
344 }
345
346
347 // if WebCit Classic wasn't obsolete we would replace this with a "CONF GETVAL" type of thing
348 void tmplput_servcfg(StrBuf *Target, WCTemplputParams *TP)
349 {
350         void *vBuf;
351         StrBuf *Buf;
352
353         if (WC->is_aide) {
354                 if (WC->ServCfg == NULL)
355                         load_siteconfig();
356                 GetHash(WC->ServCfg, TKEY(0), &vBuf);
357                 Buf = (StrBuf*) vBuf;
358                 StrBufAppendTemplate(Target, TP, Buf, 1);
359         }
360 }
361
362 // output the global alias table
363 void tmplput_servcfg_globalaliases(StrBuf *Target, WCTemplputParams *TP) {
364         TRACE;
365         StrBufAppendTemplate(Target, TP, "wow\n", 0);
366         TRACE;
367 }
368
369
370
371
372
373
374
375
376
377
378 int ConditionalServCfg(StrBuf *Target, WCTemplputParams *TP)
379 {
380         void *vBuf;
381         StrBuf *Buf;
382
383         if (WC->is_aide) {
384                 if (WC->ServCfg == NULL)
385                         load_siteconfig();
386                 GetHash(WC->ServCfg, TKEY(2), &vBuf);
387                 if (vBuf == NULL) return 0;
388                 Buf = (StrBuf*) vBuf;
389                 if (TP->Tokens->nParameters == 3) {
390                         return 1;
391                 }
392                 else if (IS_NUMBER(TP->Tokens->Params[3]->Type))
393                         return (StrTol(Buf) == GetTemplateTokenNumber (Target, TP, 3, 0));
394                 else
395                 {
396                         const char *pch;
397                         long len;
398                         
399                         GetTemplateTokenString(Target, TP, 3, &pch, &len);
400                 
401                         return ((len == StrLength(Buf)) &&
402                                 (strcmp(pch, ChrPtr(Buf)) == 0));
403                 }
404
405         }
406         else return 0;
407 }
408
409 int ConditionalServCfgCTXStrBuf(StrBuf *Target, WCTemplputParams *TP)
410 {
411         void *vBuf;
412         StrBuf *Buf;
413         StrBuf *ZoneToCheck = (StrBuf*) CTX(CTX_STRBUF);
414
415         if ((WC->is_aide) || (ZoneToCheck == NULL)) {
416                 if (WC->ServCfg == NULL)
417                         load_siteconfig();
418                 GetHash(WC->ServCfg, TKEY(2), &vBuf);
419                 if (vBuf == NULL) return 0;
420                 Buf = (StrBuf*) vBuf;
421
422                 return strcmp(ChrPtr(Buf), ChrPtr(ZoneToCheck)) == 0;
423         }
424         else return 0;
425 }
426
427 /*----------------------------------------------------------------------------*
428  *              Displaying Logging                                            *
429  *----------------------------------------------------------------------------*/
430 typedef struct __LogStatusStruct {
431         int Enable;
432         StrBuf *Name;
433 }LogStatusStruct;
434
435 void DeleteLogStatusStruct(void *v)
436 {
437         LogStatusStruct *Stat = (LogStatusStruct*)v;
438
439         FreeStrBuf(&Stat->Name);
440         free(Stat);
441 }
442
443 void tmplput_servcfg_LogName(StrBuf *Target, WCTemplputParams *TP)
444 {
445         LogStatusStruct *Stat = (LogStatusStruct*) CTX(CTX_SRVLOG);
446         StrBufAppendTemplate(Target, TP, Stat->Name, 1);
447 }
448
449
450
451 int ConditionalServCfgThisLogEnabled(StrBuf *Target, WCTemplputParams *TP)
452 {
453         LogStatusStruct *Stat = (LogStatusStruct*) CTX(CTX_SRVLOG);
454         return Stat->Enable;
455 }
456
457 HashList *iterate_GetSrvLogEnable(StrBuf *Target, WCTemplputParams *TP)
458 {
459         HashList *Hash = NULL;
460         StrBuf *Buf;
461         LogStatusStruct *Stat;
462         const char *Pos;
463         int Done = 0;
464         long len;
465         int num_logs = 0;
466
467         serv_puts("LOGP");
468         Buf = NewStrBuf();
469         StrBuf_ServGetln(Buf);
470         if (GetServerStatus(Buf, NULL) == 1) {
471                 Hash = NewHash(1, Flathash);
472                 while (!Done) {
473                         len = StrBuf_ServGetln(Buf);
474                         if ((len <0) || 
475                             ((len == 3) &&
476                              !strcmp(ChrPtr(Buf), "000")))
477                         {
478                                 Done = 1;
479                                 break;
480                         }
481                         Stat = (LogStatusStruct*) malloc (sizeof(LogStatusStruct));
482                         Pos = NULL;
483                         Stat->Name = NewStrBufPlain(NULL, len);
484                         StrBufExtract_NextToken(Stat->Name, Buf, &Pos, '|');
485                         Stat->Enable = StrBufExtractNext_int(Buf, &Pos, '|');
486
487                         Put(Hash, IKEY(num_logs), Stat, DeleteLogStatusStruct); 
488                         num_logs++;
489                 }
490         }
491         FreeStrBuf(&Buf);
492         return Hash;
493 }
494
495
496 void 
497 InitModule_SITECONFIG
498 (void)
499 {
500         RegisterCTX(CTX_SRVLOG);
501         WebcitAddUrlHandler(HKEY("siteconfig"), "", 0, siteconfig, CTX_NONE);
502
503         RegisterNamespace("SERV:CFG", 1, 2, tmplput_servcfg, NULL, CTX_NONE);
504         RegisterNamespace("SERV:GLOBALALIASES", 0, 1, tmplput_servcfg_globalaliases, NULL, CTX_NONE);
505         RegisterConditional("COND:SERVCFG", 3, ConditionalServCfg, CTX_NONE);
506         RegisterConditional("COND:SERVCFG:CTXSTRBUF", 4, ConditionalServCfgCTXStrBuf, CTX_STRBUF);
507         RegisterIterator("PREF:ZONE", 0, ZoneHash, NULL, NULL, NULL, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
508
509         REGISTERTokenParamDefine(roompolicy);
510         REGISTERTokenParamDefine(floorpolicy);
511         REGISTERTokenParamDefine(sitepolicy);
512         REGISTERTokenParamDefine(mailboxespolicy);
513
514         REGISTERTokenParamDefine(EXPIRE_NEXTLEVEL);
515         REGISTERTokenParamDefine(EXPIRE_MANUAL);
516         REGISTERTokenParamDefine(EXPIRE_NUMMSGS);
517         REGISTERTokenParamDefine(EXPIRE_AGE);
518
519         REGISTERTokenParamDefine(CFG_SMTP_FROM_FILTERALL);
520         REGISTERTokenParamDefine(CFG_SMTP_FROM_NOFILTER);
521         REGISTERTokenParamDefine(CFG_SMTP_FROM_CORRECT);
522         REGISTERTokenParamDefine(CFG_SMTP_FROM_REJECT);
523
524         RegisterConditional("COND:EXPIRE:MODE", 2, ConditionalExpire, CTX_NONE);
525         RegisterNamespace("EXPIRE:VALUE", 1, 2, tmplput_ExpireValue, NULL, CTX_NONE);
526         RegisterNamespace("EXPIRE:MODE", 1, 2, tmplput_ExpireMode, NULL, CTX_NONE);
527         RegisterConditional("COND:SERVCFG:THISLOGENABLE", 4, ConditionalServCfgThisLogEnabled, CTX_SRVLOG);
528         RegisterIterator("SERVCFG:LOGENABLE", 0, NULL, iterate_GetSrvLogEnable, NULL, DeleteHash, CTX_SRVLOG, CTX_NONE, IT_NOFLAG);
529         RegisterNamespace("SERVCFG:LOGNAME", 0, 1, tmplput_servcfg_LogName, NULL, CTX_SRVLOG);
530 }
531
532 void 
533 ServerStartModule_SITECONFIG
534 (void)
535 {
536         LoadZoneFiles();
537 }
538
539 void 
540 ServerShutdownModule_SITECONFIG
541 (void)
542 {
543         DeleteHash(&ZoneHash);
544 }
545
546
547 void 
548 SessionDestroyModule_SITECONFIG
549 (wcsession *sess)
550 {
551         DeleteHash(&sess->ServCfg);
552 }