MCS  0.3.3-alpha7
Env.cc
1 // ----------------------------------------------------------------------^
2 // Copyright (C) 2004, 2005, 2006, 2007, 2008 Giorgio Calderone
3 // (mailto: <gcalderone@ifc.inaf.it>)
4 //
5 // This file is part of MCS.
6 //
7 // MCS is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // MCS is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with MCS; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 //
21 // ----------------------------------------------------------------------$
22 
23 #include "mcs.hh"
24 using namespace mcs;
25 
26 
27 
28 
30 {
31  string s = path;
32  unsigned int size;
33 
34  if (! File_Dir_Exist(s, size)) {
35  if (cl_work)
36  mkDir(s);
37  else
38  throw MCS_ERROR(MSG_DIRECTORY_NOT_FOUND, s.csz);
39  }
40 }
41 
42 
44 {
45  //General values Default values
46  appvers = cnf->sval("MCS_Generic", "APPVERS" , "0.0" );
47  path = cnf->sval("MCS_Generic", "PATH" , Pwd() );
48 //LN-darwin
49 #ifdef __APPLE__
50  interface = cnf->sval("MCS_Generic", "INTERFACE" , "lo0" );
51 #else
52  interface = cnf->sval("MCS_Generic", "INTERFACE" , "lo" );
53 #endif
54 //interface = cnf->sval("MCS_Generic", "INTERFACE" , "127.0.0.1" );
55  port = cnf->ival("MCS_Generic", "PORT" , MCS_DEFAULT_PORT );
56  timeout = cnf->ival("MCS_Generic", "TIMEOUT" , MCS_DEFAULT_CLIENT_TIMEOUT);
57  max_users = cnf->ival("MCS_Generic", "MAX_USERS" , MCS_DEFAULT_MAX_USERS );
58  db_user = cnf->sval("MCS_Generic", "DB_USER" , "" );
59  db_pass = cnf->sval("MCS_Generic", "DB_PASS" , "" );
60  db_name = cnf->sval("MCS_Generic", "DB_NAME" , "" );
61  cl_local_kills_mcs = cnf->ival("MCS_Generic", "local_kills_mcs", 0 );
62  cl_have_db = cnf->ival("MCS_Generic", "have_db" , 0 );
63  cl_custom_auth = cnf->ival("MCS_Generic", "custom_auth" , 0 );
64  cl_logfile = cnf->ival("MCS_Generic", "logfile" , 0 );
65  cl_work = cnf->ival("MCS_Generic", "work" , 1 );
66  cl_work_cid = cnf->ival("MCS_Generic", "work_cid" , 0 );
67  cl_logstdout = cnf->ival("MCS_Generic", "logstdout" , 1 );
68  cl_createlocal = cnf->ival("MCS_Generic", "createlocal" , 1 );
69  cl_restartlocal = cnf->ival("MCS_Generic", "restartlocal" , 0 );
70  cl_clean_logout = cnf->ival("MCS_Generic", "clean_logout" , 0 );
71  cl_read_grants = cnf->ival("MCS_Generic", "read_grants" , 0 );
72  cl_autoexec = cnf->ival("MCS_Generic", "autoexec" , 0 );
73  cl_use_ssl = cnf->ival("MCS_Generic", "use_ssl" , 0 );
74  cl_chunksize = cnf->ival("MCS_Generic", "chunksize" , MCS_DEFAULTCHUNKSIZE );
75 
76  //Application specific values
77  appvers = cnf->sval(appname , "APPVERS" , appvers );
78  path = cnf->sval(appname , "PATH" , path );
79  interface = cnf->sval(appname , "INTERFACE" , interface );
80  port = cnf->ival(appname , "PORT" , port );
81  timeout = cnf->ival(appname , "TIMEOUT" , (int) timeout );
82  max_users = cnf->ival(appname , "MAX_USERS" , max_users );
83  db_user = cnf->sval(appname , "DB_USER" , db_user );
84  db_pass = cnf->sval(appname , "DB_PASS" , db_pass );
85  db_name = cnf->sval(appname , "DB_NAME" , db_name );
86  cl_local_kills_mcs = cnf->ival(appname , "local_kills_mcs", cl_local_kills_mcs);
87  cl_have_db = cnf->ival(appname , "have_db" , cl_have_db );
88  cl_custom_auth = cnf->ival(appname , "custom_auth" , cl_custom_auth );
89  cl_logfile = cnf->ival(appname , "logfile" , cl_logfile );
90  cl_work = cnf->ival(appname , "work" , cl_work );
91  cl_work_cid = cnf->ival(appname , "work_cid" , cl_work_cid );
92  cl_logstdout = cnf->ival(appname , "logstdout" , cl_logstdout );
93  cl_createlocal = cnf->ival(appname , "createlocal" , cl_createlocal );
94  cl_restartlocal = cnf->ival(appname , "restartlocal" , cl_restartlocal );
95  cl_clean_logout = cnf->ival(appname , "clean_logout" , cl_clean_logout );
96  cl_read_grants = cnf->ival(appname , "read_grants" , cl_read_grants );
97  cl_autoexec = cnf->ival(appname , "autoexec" , cl_autoexec );
98  cl_use_ssl = cnf->ival(appname , "use_ssl" , cl_use_ssl );
99  cl_chunksize = cnf->ival(appname , "chunksize" , cl_chunksize );
100 }
101 
103 {
104  Conf* cnf;
105 
106  cnf = new Conf(inipath);
107 
108  cnf->setval(appname, "APPVERS" , appvers );
109  cnf->setval(appname, "PATH" , path );
110  cnf->setval(appname, "INTERFACE" , interface );
111  cnf->setval(appname, "PORT" , port );
112  cnf->setval(appname, "TIMEOUT" , (int) timeout );
113  cnf->setval(appname, "MAX_USERS" , max_users );
114  cnf->setval(appname, "DB_USER" , db_user );
115  cnf->setval(appname, "DB_PASS" , db_pass );
116  cnf->setval(appname, "DB_NAME" , db_name );
117  cnf->setval(appname, "local_kills_mcs", cl_local_kills_mcs);
118  cnf->setval(appname, "have_db" , cl_have_db );
119  cnf->setval(appname, "custom_auth" , cl_custom_auth );
120  cnf->setval(appname, "logfile" , cl_logfile );
121  cnf->setval(appname, "work" , cl_work );
122  cnf->setval(appname, "work_cid" , cl_work_cid );
123  cnf->setval(appname, "logstdout" , cl_logstdout );
124  cnf->setval(appname, "createlocal" , cl_createlocal );
125  cnf->setval(appname, "restartlocal" , cl_restartlocal );
126  cnf->setval(appname, "clean_logout" , cl_clean_logout );
127  cnf->setval(appname, "read_grants" , cl_read_grants );
128  cnf->setval(appname, "autoexec" , cl_autoexec );
129  cnf->setval(appname, "use_ssl" , cl_use_ssl );
130  cnf->setval(appname, "chunksize" , cl_chunksize );
131  cnf->save();
132  delete cnf;
133 }
134 
135 
136 mcs::Env::Env(string app, string inipath) {
137  char* p;
138  string s;
139 
140  flog=NULL;
141  cnf = NULL;
142  appname=app;
143  server = NULL;
144  local=NULL;
145  srand(time(0));
146 
147  ssl_ctx = NULL;
148  sslcert = "";
149  sslpriv = "";
150 
151 
152  //Getting host name
153  localhost="localhost";
154  if ((p=getenv("HOSTNAME")))
155  localhost=p;
156 
157  this->inipath = inipath;
158  cnf = new Conf(inipath);
159  LoadConf();
160 
161  //Remove trailing slash
162  remTrailing(path, "/");
163  chkTree();
164 
165  //Eventually open log file
166  if (cl_logfile) {
167  char ts[20];
168  const char *format="%Y%m%d%H%M%S"; //Format time string
169  const int lenfmt=strlen(format)+3; //Three more for century and NULL
170  time_t tp=time(NULL);
171  strftime(ts, lenfmt, format, localtime(&tp)); //Time-stamp
172 
173  s=path;
174  s+=string("/mcs-") + appname + string("-");
175  s+=ts;
176  s+=".log";
177 
178  flog=new ofstream(s.csz, ios::app);
179  if (!(flog->is_open()))
180  throw MCS_ERROR(MSG_CANT_OPEN_FILE, s.csz);
181  }
182 
183 #ifdef HAVE_OPENSSL_SSL_H
184  if (cl_use_ssl) {
185  unsigned int ui;
186  string s;
187  sslcert = path + "/" + string(MCS_SSLCERT);
188  sslpriv = path + "/" + string(MCS_SSLPRIV);
189 
190  if ((! File_Dir_Exist(sslcert, ui)) ||
191  (! File_Dir_Exist(sslpriv, ui)) )
192  throw MCS_ERROR(MSG_ERROR, "Can't found certificate and private key files, run \"mcscreatekey\"");
193  }
194 #endif //HAVE_OPENSSL_SSL_H
195 
196  //DumpConf();
197 }
198 
199 
200 
202 {
203 #if ENABLE_MYSQL
204  if (cl_have_db)
205  db.close();
206 #endif
207 
208  if (cnf)
209  delete cnf;
210 
211  if (flog)
212  if (flog->is_open())
213  flog->close();
214 
215  //if (x509)
216  // delete x509;
217  //DumpConf();
218 }
219 
#define MCS_DEFAULT_PORT
Default port on which the server is listening.
Definition: mcs.hh:189
#define MCS_DEFAULT_MAX_USERS
Default max number of clients connected.
Definition: mcs.hh:202
bool File_Dir_Exist(string fn, unsigned int &size)
Check if a file or directory exists.
Definition: Utils.cc:39
string Pwd()
Return the current working dir.
Definition: Utils.cc:66
void save(string filename="")
Save all the values in a configuration file.
Definition: Conf.cc:227
#define MCS_DEFAULTCHUNKSIZE
File chunk size.
Definition: mcs.hh:198
#define MCS_ERROR(A, rest...)
Facility to easily pass all necessary parameter to an Event constructor.
Definition: mcs.hh:964
void setval(string section, string key, string val, string comment="")
Set a new value for the specified section/key pair.
Definition: Conf.cc:191
Read and write configuration files.
Definition: mcs.hh:4843
string remTrailing(string &s, const char *p)
Remove any trailing character "p".
Definition: Utils.cc:185
Env(string app="mcs", string inipath="mcs.conf")
Env constructor.
Definition: Env.cc:136
Main include file for all MCS based applications.
void LoadConf()
Load configuration entries.
Definition: Env.cc:43
~Env()
Destructor.
Definition: Env.cc:201
void chkTree()
Check the entire MCS tree.
Definition: Env.cc:29
bool mkDir(string path, mode_t perm=0, enum ThrowExceptions throwexc=THROW)
Create a directory.
Definition: Utils.cc:259
#define MCS_DEFAULT_CLIENT_TIMEOUT
Default client connection timeout, in milliseconds: 10 minutes.
Definition: mcs.hh:206
void DumpConf()
Dump environment to configuration file.
Definition: Env.cc:102
Namespace for MCS library.

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha7
Documentation generated on Mon May 28 07:39:41 UTC 2018