Back to index

webcit  8.12-dfsg
siteconfig.c
Go to the documentation of this file.
00001 /*
00002  * Administrative screen for site-wide configuration
00003  *
00004  * Copyright (c) 1996-2012 by the citadel.org team
00005  *
00006  * This program is open source software.  You can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License, version 3.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  */
00014 
00015 #include "webcit.h"
00016 #include "webserver.h"
00017 
00018 HashList *ZoneHash = NULL;
00019 
00020 ConstStr ExpirePolicyString = {CStrOf(roompolicy)     };
00021 
00022 ConstStr ExpirePolicyStrings[][2] = {
00023        { { CStrOf(roompolicy)     } , { strof(roompolicy)     "_value", sizeof(strof(roompolicy)     "_value") - 1 } },
00024        { { CStrOf(floorpolicy)    } , { strof(floorpolicy)    "_value", sizeof(strof(floorpolicy)    "_value") - 1 } },
00025        { { CStrOf(sitepolicy)     } , { strof(sitepolicy)     "_value", sizeof(strof(sitepolicy)     "_value") - 1 } },
00026        { { CStrOf(mailboxespolicy)} , { strof(mailboxespolicy)"_value", sizeof(strof(mailboxespolicy)"_value") - 1 } }
00027 };
00028 
00029 void LoadExpirePolicy(GPEXWhichPolicy which)
00030 {
00031        StrBuf *Buf;
00032        wcsession *WCC = WC;
00033        long State;
00034        const char *Pos = NULL;
00035 
00036        serv_printf("GPEX %s", ExpirePolicyStrings[which][0].Key);
00037        Buf = NewStrBuf();
00038        StrBuf_ServGetln(Buf);
00039        if (GetServerStatus(Buf, &State) == 2) {
00040               Pos = ChrPtr(Buf) + 4;
00041               WCC->Policy[which].expire_mode = StrBufExtractNext_long(Buf, &Pos, '|');
00042               WCC->Policy[which].expire_value = StrBufExtractNext_long(Buf, &Pos, '|');
00043        }
00044        else if (State == 550)
00045               AppendImportantMessage(_("Higher access is required to access this function."), -1);
00046        FreeStrBuf(&Buf);
00047 }
00048 
00049 void SaveExpirePolicyFromHTTP(GPEXWhichPolicy which)
00050 {
00051        StrBuf *Buf;
00052        long State;
00053 
00054        serv_printf("SPEX %s|%d|%d", 
00055                           ExpirePolicyStrings[which][0].Key,
00056                   ibcstr( ExpirePolicyStrings[which][1] ),
00057                   ibcstr( ExpirePolicyStrings[which][1] )  );
00058 
00059        Buf = NewStrBuf();
00060        StrBuf_ServGetln(Buf);
00061        GetServerStatus(Buf, &State);
00062        if (State == 550)
00063               AppendImportantMessage(_("Higher access is required to access this function."), -1);
00064        FreeStrBuf(&Buf);
00065 }
00066 
00067 int ConditionalExpire(StrBuf *Target, WCTemplputParams *TP)
00068 {
00069        wcsession *WCC = WC;
00070        GPEXWhichPolicy which;
00071        int CompareWith;
00072 
00073        which = GetTemplateTokenNumber(Target, TP, 2, 0);
00074        CompareWith = GetTemplateTokenNumber(Target, TP, 3, 0);
00075 
00076        LoadExpirePolicy(which);
00077        
00078        return WCC->Policy[which].expire_mode == CompareWith;
00079 }
00080 
00081 void tmplput_ExpireValue(StrBuf *Target, WCTemplputParams *TP)
00082 {
00083        GPEXWhichPolicy which;
00084        wcsession *WCC = WC;
00085               
00086        which = GetTemplateTokenNumber(Target, TP, 0, 0);
00087        LoadExpirePolicy(which);
00088        StrBufAppendPrintf(Target, "%d", WCC->Policy[which].expire_value);
00089 }
00090 
00091 
00092 void tmplput_ExpireMode(StrBuf *Target, WCTemplputParams *TP)
00093 {
00094        GPEXWhichPolicy which;
00095        wcsession *WCC = WC;
00096               
00097        which = GetTemplateTokenNumber(Target, TP, 2, 0);
00098        LoadExpirePolicy(which);
00099        StrBufAppendPrintf(Target, "%d", WCC->Policy[which].expire_mode);
00100 }
00101 
00102 
00103 void LoadZoneFiles(void)
00104 {
00105        icalarray *zones;
00106        int z;
00107        long len;
00108        const char *this_zone;
00109        StrBuf *ZName;
00110        
00111        ZoneHash = NewHash(1, NULL);
00112        ZName = NewStrBufPlain(HKEY("UTC"));
00113        Put(ZoneHash, HKEY("UTC"), ZName, HFreeStrBuf);
00114        zones = icaltimezone_get_builtin_timezones();
00115        for (z = 0; z < zones->num_elements; ++z) {
00116               /* syslog(9, "Location: %-40s tzid: %s\n",
00117                      icaltimezone_get_location(icalarray_element_at(zones, z)),
00118                      icaltimezone_get_tzid(icalarray_element_at(zones, z))
00119               ); */
00120               this_zone = icaltimezone_get_location(icalarray_element_at(zones, z));
00121               len = strlen(this_zone);
00122               ZName = NewStrBufPlain(this_zone, len);
00123               Put(ZoneHash, this_zone, len, ZName, HFreeStrBuf);
00124        }
00125        SortByHashKey(ZoneHash, 0);
00126 }
00127 
00128 
00129 typedef struct _CfgMapping {
00130        int type;
00131        const char *Key;
00132        long len;
00133 } CfgMapping;
00134 
00135 #define CFG_STR 1
00136 #define CFG_YES 2
00137 #define CFG_NO 3
00138 
00139 CfgMapping ServerConfig[] = {
00140        {CFG_STR, HKEY("c_nodename")},
00141        {CFG_STR, HKEY("c_fqdn")},
00142        {CFG_STR, HKEY("c_humannode")},
00143        {CFG_STR, HKEY("c_phonenum")},
00144        {CFG_YES, HKEY("c_creataide")},
00145        {CFG_STR, HKEY("c_sleeping")},
00146        {CFG_STR, HKEY("c_initax")},
00147        {CFG_YES, HKEY("c_regiscall")},
00148        {CFG_YES, HKEY("c_twitdetect")},
00149        {CFG_STR, HKEY("c_twitroom")},
00150        {CFG_STR, HKEY("c_moreprompt")},
00151        {CFG_YES, HKEY("c_restrict")},
00152        {CFG_STR, HKEY("c_bbs_city")},
00153        {CFG_STR, HKEY("c_sysadm")},
00154        {CFG_STR, HKEY("c_maxsessions")},
00155        {CFG_STR, HKEY("reserved1")},
00156        {CFG_STR, HKEY("c_userpurge")},
00157        {CFG_STR, HKEY("c_roompurge")},
00158        {CFG_STR, HKEY("c_logpages")},
00159        {CFG_STR, HKEY("c_createax")},
00160        {CFG_STR, HKEY("c_maxmsglen")},
00161        {CFG_STR, HKEY("c_min_workers")},
00162        {CFG_STR, HKEY("c_max_workers")},
00163        {CFG_STR, HKEY("c_pop3_port")},
00164        {CFG_STR, HKEY("c_smtp_port")},
00165        {CFG_NO , HKEY("c_rfc822_strict_from")},  /* note: reverse bool */
00166        {CFG_YES, HKEY("c_aide_zap")},
00167        {CFG_STR, HKEY("c_imap_port")},
00168        {CFG_STR, HKEY("c_net_freq")},
00169        {CFG_YES, HKEY("c_disable_newu")},
00170        {CFG_STR, HKEY("reserved2")},
00171        {CFG_STR, HKEY("c_purge_hour")},
00172        {CFG_STR, HKEY("c_ldap_host")},
00173        {CFG_STR, HKEY("c_ldap_port")},
00174        {CFG_STR, HKEY("c_ldap_base_dn")},
00175        {CFG_STR, HKEY("c_ldap_bind_dn")},
00176        {CFG_STR, HKEY("c_ldap_bind_pw")},
00177        {CFG_STR, HKEY("c_ip_addr")},
00178        {CFG_STR, HKEY("c_msa_port")},
00179        {CFG_STR, HKEY("c_imaps_port")},
00180        {CFG_STR, HKEY("c_pop3s_port")},
00181        {CFG_STR, HKEY("c_smtps_port")},
00182        {CFG_YES, HKEY("c_enable_fulltext")},
00183        {CFG_YES, HKEY("c_auto_cull")},
00184        {CFG_YES, HKEY("c_instant_expunge")},
00185        {CFG_YES, HKEY("c_allow_spoofing")},
00186        {CFG_YES, HKEY("c_journal_email")},
00187        {CFG_YES, HKEY("c_journal_pubmsgs")},
00188        {CFG_STR, HKEY("c_journal_dest")},
00189        {CFG_STR, HKEY("c_default_cal_zone")},
00190        {CFG_STR, HKEY("c_pftcpdict_port")},
00191        {CFG_STR, HKEY("c_mgesve_port")},
00192        {CFG_STR, HKEY("c_auth_mode")},
00193        {CFG_STR, HKEY("c_funambol_host")},
00194        {CFG_STR, HKEY("c_funambol_port")},
00195        {CFG_STR, HKEY("c_funambol_source")},
00196        {CFG_STR, HKEY("c_funambol_auth")},
00197        {CFG_YES, HKEY("c_rbl_at_greeting")},
00198        {CFG_STR, HKEY("c_master_user")},
00199        {CFG_STR, HKEY("c_master_pass")},
00200        {CFG_STR, HKEY("c_pager_program")},
00201        {CFG_YES, HKEY("c_imap_keep_from")},
00202        {CFG_STR, HKEY("c_xmpp_c2s_port")},
00203        {CFG_STR, HKEY("c_xmpp_s2s_port")},
00204        {CFG_STR, HKEY("c_pop3_fetch")},
00205        {CFG_STR, HKEY("c_pop3_fastest")},
00206        {CFG_YES, HKEY("c_spam_flag_only")},
00207        {CFG_YES, HKEY("c_guest_logins")}
00208 };
00209 
00210 
00211 
00212 /*
00213  *  display all configuration items
00214  */
00215 void load_siteconfig(void)
00216 {
00217        wcsession *WCC = WC;
00218        StrBuf *Buf;
00219        HashList *Cfg;
00220        long len;
00221        int i, j;
00222        
00223        if (WCC->ServCfg == NULL)
00224               WCC->ServCfg = NewHash(1, NULL);
00225        Cfg = WCC->ServCfg;
00226 
00227        Buf = NewStrBuf();
00228 
00229        serv_printf("CONF get");
00230        StrBuf_ServGetln(Buf);
00231        if (GetServerStatus(Buf, NULL) != 1) {
00232               StrBufCutLeft(Buf, 4);
00233               AppendImportantMessage(SKEY(Buf));
00234               FreeStrBuf(&Buf);
00235               return;
00236               
00237        }
00238        j = i = 0;
00239        while (len = StrBuf_ServGetln(Buf),
00240               (len >= 0) && 
00241               ((len != 3) || strcmp(ChrPtr(Buf), "000")))
00242        {
00243               if (i < (sizeof(ServerConfig) / sizeof(CfgMapping)))
00244               {
00245                      Put(Cfg,
00246                          ServerConfig[i].Key, 
00247                          ServerConfig[i].len, 
00248                          Buf, 
00249                          HFreeStrBuf);
00250                      i++;
00251                      Buf = NewStrBuf();
00252               }
00253               else {
00254                      if (j == 0)
00255                             AppendImportantMessage(_("WARNING: Failed to parse Server Config; do you run a to new citserver?"), -1);
00256                      j++;
00257               }
00258        }
00259        FreeStrBuf(&Buf);
00260 
00261        LoadExpirePolicy(roompolicy);
00262        LoadExpirePolicy(floorpolicy);
00263        LoadExpirePolicy(mailboxespolicy);
00264        LoadExpirePolicy(sitepolicy);
00265 }
00266 
00267 
00268 
00269 /*
00270  * parse siteconfig changes 
00271  */
00272 void siteconfig(void)
00273 {
00274        wcsession *WCC = WC;
00275        int i;
00276        StrBuf *Line;
00277 
00278        if (strlen(bstr("ok_button")) == 0) {
00279               display_aide_menu();
00280               return;
00281        }
00282        Line = NewStrBuf();
00283        serv_printf("CONF set");
00284        StrBuf_ServGetln(Line);
00285        if (GetServerStatusMsg(Line, NULL, 1, 4) != 4) {
00286               display_aide_menu();
00287               FreeStrBuf(&Line);
00288               return;
00289        }
00290 
00291        FreeStrBuf(&Line);
00292 
00293        for (i=0; i < (sizeof(ServerConfig) / sizeof(CfgMapping)); i ++)
00294        {
00295               switch (ServerConfig[i].type) {
00296               default:
00297               case CFG_STR:
00298                      serv_putbuf(SBstr(ServerConfig[i].Key, ServerConfig[i].len));
00299                      break;
00300               case CFG_YES:
00301                      serv_puts(YesBstr(ServerConfig[i].Key, 
00302                                      ServerConfig[i].len) ?
00303                               "1" : "0");
00304                      break;
00305               case CFG_NO:
00306                      serv_puts(YesBstr(ServerConfig[i].Key, 
00307                                      ServerConfig[i].len) ?
00308                               "0" : "1");
00309                      break;
00310               }
00311        }
00312         serv_puts("000");
00313 
00314        SaveExpirePolicyFromHTTP(sitepolicy);
00315        SaveExpirePolicyFromHTTP(mailboxespolicy);
00316 
00317        FreeStrBuf(&WCC->serv_info->serv_default_cal_zone);
00318        WCC->serv_info->serv_default_cal_zone = NewStrBufDup(sbstr("c_default_cal_zone"));
00319 
00320        AppendImportantMessage(_("Your system configuration has been updated."), -1);
00321        DeleteHash(&WCC->ServCfg);
00322        display_aide_menu();
00323 }
00324 
00325 void tmplput_servcfg(StrBuf *Target, WCTemplputParams *TP)
00326 {
00327        wcsession *WCC = WC;
00328        void *vBuf;
00329        StrBuf *Buf;
00330 
00331        if (WCC->is_aide) {
00332               if (WCC->ServCfg == NULL)
00333                      load_siteconfig();
00334               GetHash(WCC->ServCfg, TKEY(0), &vBuf);
00335               Buf = (StrBuf*) vBuf;
00336               StrBufAppendTemplate(Target, TP, Buf, 1);
00337        }
00338 }
00339 
00340 int ConditionalServCfg(StrBuf *Target, WCTemplputParams *TP)
00341 {
00342        wcsession *WCC = WC;
00343        void *vBuf;
00344        StrBuf *Buf;
00345 
00346        if (WCC->is_aide) {
00347               if (WCC->ServCfg == NULL)
00348                      load_siteconfig();
00349               GetHash(WCC->ServCfg, TKEY(2), &vBuf);
00350               if (vBuf == NULL) return 0;
00351               Buf = (StrBuf*) vBuf;
00352               if (TP->Tokens->nParameters == 3) {
00353                      return 1;
00354               }
00355               else if (IS_NUMBER(TP->Tokens->Params[3]->Type))
00356                      return (StrTol(Buf) == GetTemplateTokenNumber (Target, TP, 3, 0));
00357               else
00358               {
00359                      const char *pch;
00360                      long len;
00361                      
00362                      GetTemplateTokenString(Target, TP, 3, &pch, &len);
00363               
00364                      return ((len == StrLength(Buf)) &&
00365                             (strcmp(pch, ChrPtr(Buf)) == 0));
00366               }
00367 
00368        }
00369        else return 0;
00370 }
00371 
00372 int ConditionalServCfgCTXStrBuf(StrBuf *Target, WCTemplputParams *TP)
00373 {
00374        wcsession *WCC = WC;
00375        void *vBuf;
00376        StrBuf *Buf;
00377        StrBuf *ZoneToCheck = (StrBuf*) CTX;
00378 
00379        if ((WCC->is_aide) || (ZoneToCheck == NULL)) {
00380               if (WCC->ServCfg == NULL)
00381                      load_siteconfig();
00382               GetHash(WCC->ServCfg, TKEY(2), &vBuf);
00383               if (vBuf == NULL) return 0;
00384               Buf = (StrBuf*) vBuf;
00385 
00386               return strcmp(ChrPtr(Buf), ChrPtr(ZoneToCheck)) == 0;
00387        }
00388        else return 0;
00389 }
00390 
00391 /*----------------------------------------------------------------------------*
00392  *              Displaying Logging                                            *
00393  *----------------------------------------------------------------------------*/
00394 typedef struct __LogStatusStruct {
00395        int Enable;
00396        StrBuf *Name;
00397 }LogStatusStruct;
00398 
00399 void DeleteLogStatusStruct(void *v)
00400 {
00401        LogStatusStruct *Stat = (LogStatusStruct*)v;
00402 
00403        FreeStrBuf(&Stat->Name);
00404        free(Stat);
00405 }
00406 
00407 void tmplput_servcfg_LogName(StrBuf *Target, WCTemplputParams *TP)
00408 {
00409         LogStatusStruct *Stat = (LogStatusStruct*) CTX;
00410        StrBufAppendTemplate(Target, TP, Stat->Name, 1);
00411 }
00412 
00413 int ConditionalServCfgThisLogEnabled(StrBuf *Target, WCTemplputParams *TP)
00414 {
00415         LogStatusStruct *Stat = (LogStatusStruct*) CTX;
00416        return Stat->Enable;
00417 }
00418 
00419 HashList *iterate_GetSrvLogEnable(StrBuf *Target, WCTemplputParams *TP)
00420 {
00421         HashList *Hash = NULL;
00422        StrBuf *Buf;
00423        LogStatusStruct *Stat;
00424        const char *Pos;
00425        int Done = 0;
00426        long len;
00427        int num_logs = 0;
00428 
00429         serv_puts("LOGP");
00430         Buf = NewStrBuf();
00431         StrBuf_ServGetln(Buf);
00432         if (GetServerStatus(Buf, NULL) == 1) {
00433                 Hash = NewHash(1, Flathash);
00434                 while (!Done) {
00435                         len = StrBuf_ServGetln(Buf);
00436                         if ((len <0) || 
00437                             ((len == 3) &&
00438                              !strcmp(ChrPtr(Buf), "000")))
00439                         {
00440                                 Done = 1;
00441                                 break;
00442                         }
00443                      Stat = (LogStatusStruct*) malloc (sizeof(LogStatusStruct));
00444                      Pos = NULL;
00445                      Stat->Name = NewStrBufPlain(NULL, len);
00446                         StrBufExtract_NextToken(Stat->Name, Buf, &Pos, '|');
00447                         Stat->Enable = StrBufExtractNext_int(Buf, &Pos, '|');
00448 
00449                         Put(Hash, IKEY(num_logs), Stat, DeleteLogStatusStruct); 
00450                      num_logs++;
00451                 }
00452        }
00453        FreeStrBuf(&Buf);
00454        return Hash;
00455 }
00456 
00457 
00458 void 
00459 InitModule_SITECONFIG
00460 (void)
00461 {
00462        WebcitAddUrlHandler(HKEY("siteconfig"), "", 0, siteconfig, CTX_NONE);
00463 
00464        RegisterNamespace("SERV:CFG", 1, 2, tmplput_servcfg, NULL, CTX_NONE);
00465        RegisterConditional(HKEY("COND:SERVCFG"), 3, ConditionalServCfg, CTX_NONE);
00466        RegisterConditional(HKEY("COND:SERVCFG:CTXSTRBUF"), 4, ConditionalServCfgCTXStrBuf, CTX_STRBUF);
00467        RegisterIterator("PREF:ZONE", 0, ZoneHash, NULL, NULL, NULL, CTX_STRBUF, CTX_NONE, IT_NOFLAG);
00468 
00469        REGISTERTokenParamDefine(roompolicy);
00470        REGISTERTokenParamDefine(floorpolicy);
00471        REGISTERTokenParamDefine(sitepolicy);
00472        REGISTERTokenParamDefine(mailboxespolicy);
00473 
00474        REGISTERTokenParamDefine(EXPIRE_NEXTLEVEL);
00475        REGISTERTokenParamDefine(EXPIRE_MANUAL);
00476        REGISTERTokenParamDefine(EXPIRE_NUMMSGS);
00477        REGISTERTokenParamDefine(EXPIRE_AGE);
00478 
00479        RegisterConditional(HKEY("COND:EXPIRE:MODE"), 2, ConditionalExpire, CTX_NONE);
00480        RegisterNamespace("EXPIRE:VALUE", 1, 2, tmplput_ExpireValue, NULL, CTX_NONE);
00481        RegisterNamespace("EXPIRE:MODE", 1, 2, tmplput_ExpireMode, NULL, CTX_NONE);
00482 
00483        RegisterConditional(HKEY("COND:SERVCFG:THISLOGENABLE"), 4, ConditionalServCfgThisLogEnabled, CTX_SRVLOG);
00484        RegisterIterator("SERVCFG:LOGENABLE", 0, NULL, iterate_GetSrvLogEnable, NULL, DeleteHash, CTX_SRVLOG, CTX_NONE, IT_NOFLAG);
00485        RegisterNamespace("SERVCFG:LOGNAME", 0, 1, tmplput_servcfg_LogName, NULL, CTX_SRVLOG);
00486 }
00487 
00488 void 
00489 ServerStartModule_SITECONFIG
00490 (void)
00491 {
00492        LoadZoneFiles();
00493 }
00494 
00495 void 
00496 ServerShutdownModule_SITECONFIG
00497 (void)
00498 {
00499        DeleteHash(&ZoneHash);
00500 }
00501 
00502 
00503 void 
00504 SessionDestroyModule_SITECONFIG
00505 (wcsession *sess)
00506 {
00507        DeleteHash(&sess->ServCfg);
00508 }