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 //-----------------------------------------------------------------------------
109 confFile = "/etc/sgauth.conf";
111 //-----------------------------------------------------------------------------
112 void SETTINGS::SetConfFile(string confFile)
114 SETTINGS::confFile = confFile;
116 //-----------------------------------------------------------------------------
117 int SETTINGS::ReadSettings()
121 cf = new CONFIGFILE(confFile);
127 printf("Cannot read file.\n");
132 cf->ReadString("Login", &login, "/?--?--?*");
133 if (login == "/?--?--?*")
135 strError = "Parameter \'Login\' not found.";
140 cf->ReadString("Password", &password, "/?--?--?*");
141 if (login == "/?--?--?*")
143 strError = "Parameter \'Password\' not found.";
148 cf->ReadString("ServerName", &serverName, "?*?*?");
149 if (serverName == "?*?*?")
151 strError = "Parameter \'ServerName\' not found.";
156 cf->ReadString("ListenWebIP", &tmp, "127.0.0.1");
157 listenWebIP = inet_addr(tmp.c_str());
158 if (listenWebIP == INADDR_NONE)
160 strError = "Parameter \'ListenWebIP\' is not valid.";
165 cf->ReadString("ServerPort", &tmp, "5555");
166 if (ParseIntInRange(tmp, 1, 65535, &port))
168 strError = "Parameter \'ServerPort\' is not valid.";
173 cf->ReadString("LocalPort", &tmp, "0");
174 if (ParseIntInRange(tmp, 0, 65535, &localPort))
176 strError = "Parameter \'LocalPort\' is not valid.";
181 printf("LocalPort=%d\n", localPort);
183 cf->ReadString("RefreshPeriod", &tmp, "5");
184 if (ParseIntInRange(tmp, 1, 24*3600, &refreshPeriod))
186 strError = "Parameter \'RefreshPeriod\' is not valid.";
191 cf->ReadString("Reconnect", &tmp, "yes");
192 if (ParseYesNo(tmp, &reconnect))
194 strError = "Parameter \'Reconnect\' is not valid.";
199 cf->ReadString("Daemon", &tmp, "yes");
200 if (ParseYesNo(tmp, &daemon))
202 strError = "Parameter \'Daemon\' is not valid.";
207 cf->ReadString("ShowPid", &tmp, "no");
208 if (ParseYesNo(tmp, &showPid))
210 strError = "Parameter \'ShowPid\' is not valid.";
215 cf->ReadString("DisableWeb", &tmp, "no");
216 if (ParseYesNo(tmp, &noWeb))
218 strError = "Parameter \'DisableWeb\' is not valid.";
226 //-----------------------------------------------------------------------------
227 string SETTINGS::GetStrError() const
231 //-----------------------------------------------------------------------------
232 string SETTINGS::GetLogin() const
236 //-----------------------------------------------------------------------------
237 string SETTINGS::GetPassword() const
241 //-----------------------------------------------------------------------------
242 string SETTINGS::GetServerName() const
246 //-----------------------------------------------------------------------------
247 uint16_t SETTINGS::GetServerPort() const
251 //-----------------------------------------------------------------------------
252 uint16_t SETTINGS::GetLocalPort() const
256 //-----------------------------------------------------------------------------
257 int SETTINGS::GetRefreshPeriod() const
259 return refreshPeriod;
261 //-----------------------------------------------------------------------------
262 bool SETTINGS::GetDaemon() const
266 //-----------------------------------------------------------------------------
267 bool SETTINGS::GetNoWeb() const
271 //-----------------------------------------------------------------------------
272 bool SETTINGS::GetShowPid() const
276 //-----------------------------------------------------------------------------
277 bool SETTINGS::GetReconnect() const
281 //-----------------------------------------------------------------------------
282 uint32_t SETTINGS::GetListenWebIP() const
286 //-----------------------------------------------------------------------------
287 void SETTINGS::Print() const
289 cout << "login = " << login << endl;
290 cout << "password = " << password << endl;
291 cout << "ip = " << serverName << endl;
292 cout << "port = " << port << endl;
293 cout << "localPort = " << localPort << endl;
294 cout << "listenWebIP = " << inet_ntostring(listenWebIP) << endl;
295 cout << "DisableWeb = " << noWeb << endl;
296 cout << "refreshPeriod = " << refreshPeriod << endl;
297 cout << "daemon = " << daemon << endl;
298 cout << "reconnect = " << reconnect << endl;
300 //-----------------------------------------------------------------------------
303 printf("sgauth <server> <port> <login> <password>\n"); //TODO change to correct
305 //-----------------------------------------------------------------------------
311 //-----------------------------------------------------------------------------
312 void SetDirName(const vector<string> & dn, void *)
314 for (int j = 0; j < DIR_NUM; j++)
319 KOIToWin(dn[j], &dir);
321 web->SetDirName(dir, j);
326 web->SetDirName(dn[j], j);
330 //-----------------------------------------------------------------------------
331 void StatUpdate(const LOADSTAT & ls, void *)
336 //-----------------------------------------------------------------------------
337 void StatusChanged(int, void *)
341 //-----------------------------------------------------------------------------
342 void ShowMessage(const string & message, int i, int, int, void *)
345 web->AddMessage(message, i);
347 //-----------------------------------------------------------------------------
348 void ShowError(const string & message, int, void *)
351 web->AddMessage(message, 0);
353 //-----------------------------------------------------------------------------
357 if (clnp->GetAuthorized())
359 cout << "Connect" << endl;
363 //-----------------------------------------------------------------------------
366 cout << "Disconnect" << endl;
369 //-----------------------------------------------------------------------------
372 cout << "Terminated" << endl;
377 //-----------------------------------------------------------------------------
378 static void SetSignalHandlers()
380 struct sigaction newsa, oldsa;
383 sigemptyset(&sigmask);
384 sigaddset(&sigmask, SIGTERM);
385 newsa.sa_handler = CatchTERM;
386 newsa.sa_mask = sigmask;
388 sigaction(SIGTERM, &newsa, &oldsa);
390 sigemptyset(&sigmask);
391 sigaddset(&sigmask, SIGINT);
392 newsa.sa_handler = CatchTERM;
393 newsa.sa_mask = sigmask;
395 sigaction(SIGINT, &newsa, &oldsa);
397 sigemptyset(&sigmask);
398 sigaddset(&sigmask, SIGUSR1);
399 newsa.sa_handler = CatchUSR1;
400 newsa.sa_mask = sigmask;
402 sigaction(SIGUSR1, &newsa, &oldsa);
404 sigemptyset(&sigmask);
405 sigaddset(&sigmask, SIGUSR2);
406 newsa.sa_handler = CatchUSR2;
407 newsa.sa_mask = sigmask;
409 sigaction(SIGUSR2, &newsa, &oldsa);
414 //-----------------------------------------------------------------------------
415 int main(int argc, char *argv[])
428 settings.SetConfFile(argv[1]);
429 if (settings.ReadSettings())
431 printf("ReadSettingsError\n");
432 printf("%s\n", settings.GetStrError().c_str());
446 string serverName(argv[1]);
447 port = strtol(argv[2], &endptr, 10);
450 printf("Invalid port!\n");
461 if (settings.GetDaemon())
477 default: // ïÓÎÏ×ÎÏÊ ÐÒÏÃÅÓÓ
487 clnp = new IA_CLIENT_PROT(settings.GetServerName(), settings.GetServerPort(), settings.GetLocalPort());
489 if (!settings.GetNoWeb())
492 web->SetRefreshPagePeriod(settings.GetRefreshPeriod());
493 web->SetListenAddr(settings.GetListenWebIP());
497 clnp->SetLogin(settings.GetLogin());
498 clnp->SetPassword(settings.GetPassword());
500 clnp->SetStatusChangedCb(StatusChanged, NULL);
501 clnp->SetInfoCb(ShowMessage, NULL);
502 clnp->SetErrorCb(ShowError, NULL);
503 clnp->SetDirNameCb(SetDirName, NULL);
504 clnp->SetStatChangedCb(StatUpdate, NULL);
505 clnp->SetReconnect(settings.GetReconnect());
513 for (int i = 1; i < argc; i++)
514 memset(argv[i], 0, strlen(argv[i]));
517 strcpy(argv[1], "Connecting...");
521 setproctitle("Connecting...");
535 if (clnp->GetAuthorized())
537 if (settings.GetShowPid())
538 sprintf(state, "On %d", getpid());
540 strcpy(state, "Online");
544 if (settings.GetShowPid())
545 sprintf(state, "Off %d", getpid());
547 strcpy(state, "Offline");
551 for (int i = 1; i < argc; i++)
552 memset(argv[i], 0, strlen(argv[i]));
554 strcpy(argv[1], state);
568 //-----------------------------------------------------------------------------