Env.cc

00001 // ----------------------------------------------------------------------^
00002 // Copyright (C) 2004, 2005, 2006, 2007, 2008 Giorgio Calderone
00003 // (mailto: <gcalderone@ifc.inaf.it>)
00004 // 
00005 // This file is part of MCS.
00006 // 
00007 // MCS is free software; you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation; either version 2 of the License, or
00010 // (at your option) any later version.
00011 // 
00012 // MCS is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 // 
00017 // You should have received a copy of the GNU General Public License
00018 // along with MCS; if not, write to the Free Software
00019 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 // 
00021 // ----------------------------------------------------------------------$
00022 
00023 #include "mcs.hh"
00024 using namespace mcs;
00025 
00026 
00027 
00028 
00029 void mcs::Env::chkTree()
00030 {
00031   string s = path;
00032   unsigned int size;
00033 
00034   if (! File_Dir_Exist(s, size)) {
00035       if (cl_work)
00036     mkDir(s);
00037       else
00038       throw MCS_ERROR(MSG_DIRECTORY_NOT_FOUND, s.csz);
00039   }
00040 }
00041 
00042 
00043 void mcs::Env::LoadConf()
00044 {
00045   //General values                                                 Default values
00046   appvers            = cnf->sval("MCS_Generic", "APPVERS"        , "0.0"                     );
00047   path               = cnf->sval("MCS_Generic", "PATH"           , Pwd()                     );
00048   interface          = cnf->sval("MCS_Generic", "INTERFACE"      , "lo"                      );
00049   port               = cnf->ival("MCS_Generic", "PORT"           , MCS_DEFAULT_PORT          );
00050   timeout            = cnf->ival("MCS_Generic", "TIMEOUT"        , MCS_DEFAULT_CLIENT_TIMEOUT);
00051   max_users          = cnf->ival("MCS_Generic", "MAX_USERS"      , MCS_DEFAULT_MAX_USERS     );
00052   db_user            = cnf->sval("MCS_Generic", "DB_USER"        , ""                        );
00053   db_pass            = cnf->sval("MCS_Generic", "DB_PASS"        , ""                        );
00054   db_name            = cnf->sval("MCS_Generic", "DB_NAME"        , ""                        );
00055   cl_local_kills_mcs = cnf->ival("MCS_Generic", "local_kills_mcs", 0                         );
00056   cl_have_db         = cnf->ival("MCS_Generic", "have_db"        , 0                         );
00057   cl_custom_auth     = cnf->ival("MCS_Generic", "custom_auth"    , 0                         );
00058   cl_logfile         = cnf->ival("MCS_Generic", "logfile"        , 0                         );
00059   cl_work            = cnf->ival("MCS_Generic", "work"           , 1                         );
00060   cl_work_cid        = cnf->ival("MCS_Generic", "work_cid"       , 0                         );
00061   cl_logstdout       = cnf->ival("MCS_Generic", "logstdout"      , 1                         );
00062   cl_createlocal     = cnf->ival("MCS_Generic", "createlocal"    , 1                         );
00063   cl_restartlocal    = cnf->ival("MCS_Generic", "restartlocal"   , 0                         );
00064   cl_clean_logout    = cnf->ival("MCS_Generic", "clean_logout"   , 0                         );
00065   cl_read_grants     = cnf->ival("MCS_Generic", "read_grants"    , 0                         );
00066   cl_autoexec        = cnf->ival("MCS_Generic", "autoexec"       , 0                         );
00067   cl_use_ssl         = cnf->ival("MCS_Generic", "use_ssl"        , 0                         );
00068   cl_chunksize       = cnf->ival("MCS_Generic", "chunksize"      , MCS_DEFAULTCHUNKSIZE      );
00069 
00070   //Application specific values
00071   appvers            = cnf->sval(appname      , "APPVERS"        , appvers           );
00072   path               = cnf->sval(appname      , "PATH"           , path              );
00073   interface          = cnf->sval(appname      , "INTERFACE"      , interface         );
00074   port               = cnf->ival(appname      , "PORT"           , port              );
00075   timeout            = cnf->ival(appname      , "TIMEOUT"        , (int) timeout     );
00076   max_users          = cnf->ival(appname      , "MAX_USERS"      , max_users         );
00077   db_user            = cnf->sval(appname      , "DB_USER"        , db_user           );
00078   db_pass            = cnf->sval(appname      , "DB_PASS"        , db_pass           );
00079   db_name            = cnf->sval(appname      , "DB_NAME"        , db_name           );
00080   cl_local_kills_mcs = cnf->ival(appname      , "local_kills_mcs", cl_local_kills_mcs);
00081   cl_have_db         = cnf->ival(appname      , "have_db"        , cl_have_db        );
00082   cl_custom_auth     = cnf->ival(appname      , "custom_auth"    , cl_custom_auth    );
00083   cl_logfile         = cnf->ival(appname      , "logfile"        , cl_logfile        );
00084   cl_work            = cnf->ival(appname      , "work"           , cl_work           );
00085   cl_work_cid        = cnf->ival(appname      , "work_cid"       , cl_work_cid       );
00086   cl_logstdout       = cnf->ival(appname      , "logstdout"      , cl_logstdout      );
00087   cl_createlocal     = cnf->ival(appname      , "createlocal"    , cl_createlocal    );
00088   cl_restartlocal    = cnf->ival(appname      , "restartlocal"   , cl_restartlocal   );
00089   cl_clean_logout    = cnf->ival(appname      , "clean_logout"   , cl_clean_logout   );
00090   cl_read_grants     = cnf->ival(appname      , "read_grants"    , cl_read_grants    );
00091   cl_autoexec        = cnf->ival(appname      , "autoexec"       , cl_autoexec       );
00092   cl_use_ssl         = cnf->ival(appname      , "use_ssl"        , cl_use_ssl        );
00093   cl_chunksize       = cnf->ival(appname      , "chunksize"      , cl_chunksize      );
00094 }
00095 
00096 void mcs::Env::DumpConf()
00097 {
00098   Conf* cnf;
00099 
00100   cnf = new Conf(inipath);
00101 
00102   cnf->setval(appname, "APPVERS"        , appvers           );
00103   cnf->setval(appname, "PATH"           , path              );
00104   cnf->setval(appname, "INTERFACE"      , interface         );
00105   cnf->setval(appname, "PORT"           , port              );
00106   cnf->setval(appname, "TIMEOUT"        , (int) timeout     );
00107   cnf->setval(appname, "MAX_USERS"      , max_users         );
00108   cnf->setval(appname, "DB_USER"        , db_user           );
00109   cnf->setval(appname, "DB_PASS"        , db_pass           );
00110   cnf->setval(appname, "DB_NAME"        , db_name           );
00111   cnf->setval(appname, "local_kills_mcs", cl_local_kills_mcs);
00112   cnf->setval(appname, "have_db"        , cl_have_db        );
00113   cnf->setval(appname, "custom_auth"    , cl_custom_auth    );
00114   cnf->setval(appname, "logfile"        , cl_logfile        );
00115   cnf->setval(appname, "work"           , cl_work           );
00116   cnf->setval(appname, "work_cid"       , cl_work_cid       );
00117   cnf->setval(appname, "logstdout"      , cl_logstdout      );
00118   cnf->setval(appname, "createlocal"    , cl_createlocal    );
00119   cnf->setval(appname, "restartlocal"   , cl_restartlocal   );
00120   cnf->setval(appname, "clean_logout"   , cl_clean_logout   );
00121   cnf->setval(appname, "read_grants"    , cl_read_grants    );
00122   cnf->setval(appname, "autoexec"       , cl_autoexec       );
00123   cnf->setval(appname, "use_ssl"        , cl_use_ssl        );
00124   cnf->setval(appname, "chunksize"      , cl_chunksize      );
00125   cnf->save();
00126   delete cnf;
00127 }
00128 
00129 
00130 mcs::Env::Env(string app, string inipath) {
00131   char* p;
00132   string s;
00133 
00134   flog=NULL;
00135   cnf = NULL;
00136   appname=app;
00137   server = NULL;
00138   local=NULL;
00139   srand(time(0));
00140 
00141   ssl_ctx = NULL;
00142   sslcert = "";
00143   sslpriv = "";
00144 
00145 
00146   //Getting host name
00147   localhost="localhost";
00148   if ((p=getenv("HOSTNAME")))
00149     localhost=p;
00150 
00151   this->inipath = inipath;
00152   cnf = new Conf(inipath);
00153   LoadConf();
00154 
00155   //Remove trailing slash
00156   remTrailing(path, "/");
00157   chkTree();
00158 
00159   //Eventually open log file
00160   if (cl_logfile) {
00161     char ts[20];
00162     const char *format="%Y%m%d%H%M%S";   //Format time string
00163     const int lenfmt=strlen(format)+3;   //Three more for century and NULL
00164     time_t tp=time(NULL);
00165     strftime(ts, lenfmt, format, localtime(&tp));   //Time-stamp
00166 
00167     s=path;
00168     s+=string("/mcs-") + appname + string("-");
00169     s+=ts;
00170     s+=".log";
00171 
00172     flog=new ofstream(s.csz, ios::app);
00173     if (!(flog->is_open()))
00174     throw MCS_ERROR(MSG_CANT_OPEN_FILE, s.csz);
00175   }
00176 
00177 #ifdef HAVE_OPENSSL_SSL_H
00178   if (cl_use_ssl) {
00179     unsigned int ui;
00180     string s;
00181     sslcert = path + "/" + string(MCS_SSLCERT);
00182     sslpriv = path + "/" + string(MCS_SSLPRIV);
00183 
00184     if ((! File_Dir_Exist(sslcert, ui))   ||
00185     (! File_Dir_Exist(sslpriv, ui))      )
00186       throw MCS_ERROR(MSG_ERROR, "Can't found certificate and private key files, run \"mcscreatekey\"");
00187   }
00188 #endif //HAVE_OPENSSL_SSL_H
00189 
00190   //DumpConf();
00191 }
00192 
00193 
00194 
00195 mcs::Env::~Env()
00196 {
00197 #if ENABLE_MYSQL
00198     if (cl_have_db)
00199     db.close();
00200 #endif
00201 
00202   if (cnf)
00203     delete cnf;
00204 
00205   if (flog)
00206     if (flog->is_open())
00207       flog->close();
00208 
00209   //if (x509)
00210   //  delete x509;
00211   //DumpConf();
00212 }
00213 

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha3
Documentation generated on Thu Mar 22 13:22:23 UTC 2012