2  *    This program is free software; you can redistribute it and/or modify
 
   3  *    it under the terms of the GNU General Public License as published by
 
   4  *    the Free Software Foundation; either version 2 of the License, or
 
   5  *    (at your option) any later version.
 
   7  *    This program is distributed in the hope that it will be useful,
 
   8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
   9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  10  *    GNU General Public License for more details.
 
  12  *    You should have received a copy of the GNU General Public License
 
  13  *    along with this program; if not, write to the Free Software
 
  14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
  18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
 
  23  $Date: 2010/04/14 09:01:29 $
 
  33 #include <sys/socket.h>
 
  34 #include <netinet/in.h>
 
  35 #include <arpa/inet.h>
 
  37 #include "conffiles.h"
 
  44 #include "ia_auth_c.h"
 
  46 #include "common_settings.h"
 
  51 char messageText[256];
 
  55 IA_CLIENT_PROT * clnp;
 
  62 //-----------------------------------------------------------------------------
 
  63 class SETTINGS: public COMMON_SETTINGS
 
  67     virtual         ~SETTINGS(){};
 
  68     virtual int     Reload(){ return 0; };
 
  69     void            SetConfFile(string confFile);
 
  70     virtual int     ReadSettings();
 
  72     virtual string  GetStrError() const;
 
  74     string          GetServerName() const;
 
  75     uint16_t        GetServerPort() const;
 
  76     uint16_t        GetLocalPort() const;
 
  78     string          GetLogin() const;
 
  79     string          GetPassword() const;
 
  81     bool            GetDaemon() const;
 
  82     bool            GetShowPid() const;
 
  83     bool            GetNoWeb() const;
 
  84     bool            GetReconnect() const;
 
  85     int             GetRefreshPeriod() const;
 
  86     uint32_t        GetListenWebIP() const;
 
 106 //-----------------------------------------------------------------------------
 
 117 confFile = "/etc/sgauth.conf";
 
 119 //-----------------------------------------------------------------------------
 
 120 void SETTINGS::SetConfFile(string confFile)
 
 122 SETTINGS::confFile = confFile;
 
 124 //-----------------------------------------------------------------------------
 
 125 int SETTINGS::ReadSettings()
 
 129 cf = new CONFIGFILE(confFile);
 
 135     printf("Cannot read file.\n");
 
 140 cf->ReadString("Login", &login, "/?--?--?*");
 
 141 if (login == "/?--?--?*")
 
 143     strError = "Parameter \'Login\' not found.";
 
 148 cf->ReadString("Password", &password, "/?--?--?*");
 
 149 if (login == "/?--?--?*")
 
 151     strError = "Parameter \'Password\' not found.";
 
 156 cf->ReadString("ServerName", &serverName, "?*?*?");
 
 157 if (serverName == "?*?*?")
 
 159     strError = "Parameter \'ServerName\' not found.";
 
 164 cf->ReadString("ListenWebIP", &tmp, "127.0.0.1");
 
 165 listenWebIP = inet_addr(tmp.c_str());
 
 166 if (listenWebIP == INADDR_NONE)
 
 168     strError = "Parameter \'ListenWebIP\' is not valid.";
 
 173 cf->ReadString("ServerPort", &tmp, "5555");
 
 174 if (ParseIntInRange(tmp, 1, 65535, &port))
 
 176     strError = "Parameter \'ServerPort\' is not valid.";
 
 181 cf->ReadString("LocalPort", &tmp, "0");
 
 182 if (ParseIntInRange(tmp, 0, 65535, &localPort))
 
 184     strError = "Parameter \'LocalPort\' is not valid.";
 
 189 printf("LocalPort=%d\n", localPort);
 
 191 cf->ReadString("RefreshPeriod", &tmp, "5");
 
 192 if (ParseIntInRange(tmp, 1, 24*3600, &refreshPeriod))
 
 194     strError = "Parameter \'RefreshPeriod\' is not valid.";
 
 199 cf->ReadString("Reconnect", &tmp, "yes");
 
 200 if (ParseYesNo(tmp, &reconnect))
 
 202     strError = "Parameter \'Reconnect\' is not valid.";
 
 207 cf->ReadString("Daemon", &tmp, "yes");
 
 208 if (ParseYesNo(tmp, &daemon))
 
 210     strError = "Parameter \'Daemon\' is not valid.";
 
 215 cf->ReadString("ShowPid", &tmp, "no");
 
 216 if (ParseYesNo(tmp, &showPid))
 
 218     strError = "Parameter \'ShowPid\' is not valid.";
 
 223 cf->ReadString("DisableWeb", &tmp, "no");
 
 224 if (ParseYesNo(tmp, &noWeb))
 
 226     strError = "Parameter \'DisableWeb\' is not valid.";
 
 234 //-----------------------------------------------------------------------------
 
 235 string SETTINGS::GetStrError() const
 
 239 //-----------------------------------------------------------------------------
 
 240 string SETTINGS::GetLogin() const
 
 244 //-----------------------------------------------------------------------------
 
 245 string SETTINGS::GetPassword() const
 
 249 //-----------------------------------------------------------------------------
 
 250 string SETTINGS::GetServerName() const
 
 254 //-----------------------------------------------------------------------------
 
 255 uint16_t SETTINGS::GetServerPort() const
 
 259 //-----------------------------------------------------------------------------
 
 260 uint16_t SETTINGS::GetLocalPort() const
 
 264 //-----------------------------------------------------------------------------
 
 265 int SETTINGS::GetRefreshPeriod() const
 
 267 return refreshPeriod;
 
 269 //-----------------------------------------------------------------------------
 
 270 bool SETTINGS::GetDaemon() const
 
 274 //-----------------------------------------------------------------------------
 
 275 bool SETTINGS::GetNoWeb() const
 
 279 //-----------------------------------------------------------------------------
 
 280 bool SETTINGS::GetShowPid() const
 
 284 //-----------------------------------------------------------------------------
 
 285 bool SETTINGS::GetReconnect() const
 
 289 //-----------------------------------------------------------------------------
 
 290 uint32_t SETTINGS::GetListenWebIP() const
 
 294 //-----------------------------------------------------------------------------
 
 295 void SETTINGS::Print() const
 
 297 cout << "login = " << login << endl;
 
 298 cout << "password = " << password << endl;
 
 299 cout << "ip = " << serverName << endl;
 
 300 cout << "port = " << port << endl;
 
 301 cout << "localPort = " << localPort << endl;
 
 302 cout << "listenWebIP = " << inet_ntostring(listenWebIP) << endl;
 
 303 cout << "DisableWeb = " << noWeb << endl;
 
 304 cout << "refreshPeriod = " << refreshPeriod << endl;
 
 305 cout << "daemon = " << daemon << endl;
 
 306 cout << "reconnect = " << reconnect << endl;
 
 308 //-----------------------------------------------------------------------------
 
 311 printf("sgauth <server> <port> <login> <password>\n"); //TODO change to correct
 
 313 //-----------------------------------------------------------------------------
 
 314 void SetDirName(const vector<string> & dn, void *)
 
 316 for (int j = 0; j < DIR_NUM; j++)
 
 321         KOIToWin(dn[j], &dir);
 
 323             web->SetDirName(dir, j);
 
 328             web->SetDirName(dn[j], j);
 
 332 //-----------------------------------------------------------------------------
 
 333 void StatUpdate(const LOADSTAT & ls, void *)
 
 338 //-----------------------------------------------------------------------------
 
 339 void StatusChanged(int, void *)
 
 343 //-----------------------------------------------------------------------------
 
 344 void ShowMessage(const string & message, int i, int, int, void *)
 
 347     web->AddMessage(message, i);
 
 349 //-----------------------------------------------------------------------------
 
 350 void ShowError(const string & message, int, void *)
 
 353      web->AddMessage(message, 0);
 
 355 //-----------------------------------------------------------------------------
 
 359 if (clnp->GetAuthorized())
 
 361     cout << "Connect" << endl;
 
 365 //-----------------------------------------------------------------------------
 
 368 cout << "Disconnect" << endl;
 
 371 //-----------------------------------------------------------------------------
 
 374 cout << "Terminated" << endl;
 
 379 //-----------------------------------------------------------------------------
 
 380 static void SetSignalHandlers()
 
 382 struct sigaction newsa, oldsa;
 
 385 sigemptyset(&sigmask);
 
 386 sigaddset(&sigmask, SIGTERM);
 
 387 newsa.sa_handler = CatchTERM;
 
 388 newsa.sa_mask = sigmask;
 
 390 sigaction(SIGTERM, &newsa, &oldsa);
 
 392 sigemptyset(&sigmask);
 
 393 sigaddset(&sigmask, SIGINT);
 
 394 newsa.sa_handler = CatchTERM;
 
 395 newsa.sa_mask = sigmask;
 
 397 sigaction(SIGINT, &newsa, &oldsa);
 
 399 sigemptyset(&sigmask);
 
 400 sigaddset(&sigmask, SIGUSR1);
 
 401 newsa.sa_handler = CatchUSR1;
 
 402 newsa.sa_mask = sigmask;
 
 404 sigaction(SIGUSR1, &newsa, &oldsa);
 
 406 sigemptyset(&sigmask);
 
 407 sigaddset(&sigmask, SIGUSR2);
 
 408 newsa.sa_handler = CatchUSR2;
 
 409 newsa.sa_mask = sigmask;
 
 411 sigaction(SIGUSR2, &newsa, &oldsa);
 
 416 //-----------------------------------------------------------------------------
 
 417 int main(int argc, char *argv[])
 
 427     settings.SetConfFile(argv[1]);
 
 438         string serverName(argv[1]);
 
 439         port = strtol(argv[2], &endptr, 10);
 
 442             printf("Invalid port!\n");
 
 450 if (settings.ReadSettings())
 
 452     printf("ReadSettingsError\n");
 
 453     printf("%s\n", settings.GetStrError().c_str());
 
 459 if (settings.GetDaemon())
 
 482 clnp = new IA_CLIENT_PROT(settings.GetServerName(), settings.GetServerPort(), settings.GetLocalPort());
 
 484 if (!settings.GetNoWeb())
 
 487     web->SetRefreshPagePeriod(settings.GetRefreshPeriod());
 
 488     web->SetListenAddr(settings.GetListenWebIP());
 
 492 clnp->SetLogin(settings.GetLogin());
 
 493 clnp->SetPassword(settings.GetPassword());
 
 495 clnp->SetStatusChangedCb(StatusChanged, NULL);
 
 496 clnp->SetInfoCb(ShowMessage, NULL);
 
 497 clnp->SetErrorCb(ShowError, NULL);
 
 498 clnp->SetDirNameCb(SetDirName, NULL);
 
 499 clnp->SetStatChangedCb(StatUpdate, NULL);
 
 500 clnp->SetReconnect(settings.GetReconnect());
 
 507 for (int i = 1; i < argc; i++)
 
 508     memset(argv[i], 0, strlen(argv[i]));
 
 511     strcpy(argv[1], "Connecting...");
 
 515 setproctitle("Connecting...");
 
 529     if (clnp->GetAuthorized())
 
 531         if (settings.GetShowPid())
 
 532             sprintf(state, "On %d", getpid());
 
 534             strcpy(state, "Online");
 
 538         if (settings.GetShowPid())
 
 539             sprintf(state, "Off %d", getpid());
 
 541             strcpy(state, "Offline");
 
 545     for (int i = 1; i < argc; i++)
 
 546         memset(argv[i], 0, strlen(argv[i]));
 
 548         strcpy(argv[1], state);
 
 562 //-----------------------------------------------------------------------------