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 //-----------------------------------------------------------------------------
306 void SetDirName(const vector<string> & dn, void *)
308 for (int j = 0; j < DIR_NUM; j++)
313 KOIToWin(dn[j], &dir);
315 web->SetDirName(dir, j);
320 web->SetDirName(dn[j], j);
324 //-----------------------------------------------------------------------------
325 void StatUpdate(const LOADSTAT & ls, void *)
330 //-----------------------------------------------------------------------------
331 void StatusChanged(int, void *)
335 //-----------------------------------------------------------------------------
336 void ShowMessage(const string & message, int i, int, int, void *)
339 web->AddMessage(message, i);
341 //-----------------------------------------------------------------------------
342 void ShowError(const string & message, int, void *)
345 web->AddMessage(message, 0);
347 //-----------------------------------------------------------------------------
351 if (clnp->GetAuthorized())
353 cout << "Connect" << endl;
357 //-----------------------------------------------------------------------------
360 cout << "Disconnect" << endl;
363 //-----------------------------------------------------------------------------
366 cout << "Terminated" << endl;
371 //-----------------------------------------------------------------------------
372 static void SetSignalHandlers()
374 struct sigaction newsa, oldsa;
377 sigemptyset(&sigmask);
378 sigaddset(&sigmask, SIGTERM);
379 newsa.sa_handler = CatchTERM;
380 newsa.sa_mask = sigmask;
382 sigaction(SIGTERM, &newsa, &oldsa);
384 sigemptyset(&sigmask);
385 sigaddset(&sigmask, SIGINT);
386 newsa.sa_handler = CatchTERM;
387 newsa.sa_mask = sigmask;
389 sigaction(SIGINT, &newsa, &oldsa);
391 sigemptyset(&sigmask);
392 sigaddset(&sigmask, SIGUSR1);
393 newsa.sa_handler = CatchUSR1;
394 newsa.sa_mask = sigmask;
396 sigaction(SIGUSR1, &newsa, &oldsa);
398 sigemptyset(&sigmask);
399 sigaddset(&sigmask, SIGUSR2);
400 newsa.sa_handler = CatchUSR2;
401 newsa.sa_mask = sigmask;
403 sigaction(SIGUSR2, &newsa, &oldsa);
408 //-----------------------------------------------------------------------------
409 int main(int argc, char *argv[])
422 settings.SetConfFile(argv[1]);
423 if (settings.ReadSettings())
425 printf("ReadSettingsError\n");
426 printf("%s\n", settings.GetStrError().c_str());
440 string serverName(argv[1]);
441 port = strtol(argv[2], &endptr, 10);
444 printf("Invalid port!\n");
455 if (settings.GetDaemon())
471 default: // ïÓÎÏ×ÎÏÊ ÐÒÏÃÅÓÓ
481 clnp = new IA_CLIENT_PROT(settings.GetServerName(), settings.GetServerPort(), settings.GetLocalPort());
483 if (!settings.GetNoWeb())
486 web->SetRefreshPagePeriod(settings.GetRefreshPeriod());
487 web->SetListenAddr(settings.GetListenWebIP());
491 clnp->SetLogin(settings.GetLogin());
492 clnp->SetPassword(settings.GetPassword());
494 clnp->SetStatusChangedCb(StatusChanged, NULL);
495 clnp->SetInfoCb(ShowMessage, NULL);
496 clnp->SetErrorCb(ShowError, NULL);
497 clnp->SetDirNameCb(SetDirName, NULL);
498 clnp->SetStatChangedCb(StatUpdate, NULL);
499 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 //-----------------------------------------------------------------------------