]> git.stg.codes - stg.git/blob - projects/sgauth/main.cpp
Додано патч від Alexey Osipov <lion-simba@pridelands.ru>
[stg.git] / projects / sgauth / main.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21  /*
22  $Revision: 1.13 $
23  $Date: 2010/04/14 09:01:29 $
24  $Author: faust $
25  */
26
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #ifndef WIN32
32 #include <unistd.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <signal.h>
37 #include "conffiles.h"
38 #endif
39
40 #include <string.h>
41 #include <vector>
42 #include <iostream>
43
44 #include "ia_auth_c.h"
45 #include "common.h"
46 #include "common_settings.h"
47 #include "web.h"
48
49 int mes;
50 char infoText[256];
51 char messageText[256];
52
53 const int winKOI = 0;
54
55 IA_CLIENT_PROT * clnp;
56 WEB * web = NULL;
57
58 using namespace std;
59
60 time_t stgTime;
61
62 //-----------------------------------------------------------------------------
63 class SETTINGS: public COMMON_SETTINGS
64 {
65 public:
66                     SETTINGS();
67     virtual         ~SETTINGS(){};
68     virtual int     Reload(){ return 0; };
69     void            SetConfFile(string confFile);
70     virtual int     ReadSettings();
71
72     virtual string  GetStrError() const;
73
74     string          GetServerName() const;
75     uint16_t        GetServerPort() const;
76     uint16_t        GetLocalPort() const;
77
78     string          GetLogin() const;
79     string          GetPassword() const;
80
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;
87
88     void            Print() const;
89
90 private:
91     string          login;
92     string          password;
93     string          serverName;
94     int             port;
95     int             localPort;
96     uint32_t        listenWebIP;
97     int             refreshPeriod;
98
99     bool            daemon;
100     bool            noWeb;
101     bool            reconnect;
102     bool            showPid;
103
104     string          confFile;
105 };
106 //-----------------------------------------------------------------------------
107 SETTINGS::SETTINGS()
108     : port(0),
109       localPort(0),
110       listenWebIP(0),
111       refreshPeriod(0),
112       daemon(false),
113       noWeb(false),
114       reconnect(false),
115       showPid(false)
116 {
117 confFile = "/etc/sgauth.conf";
118 }
119 //-----------------------------------------------------------------------------
120 void SETTINGS::SetConfFile(string confFile)
121 {
122 SETTINGS::confFile = confFile;
123 }
124 //-----------------------------------------------------------------------------
125 int SETTINGS::ReadSettings()
126 {
127 CONFIGFILE * cf;
128
129 cf = new CONFIGFILE(confFile);
130 string tmp;
131 int e = cf->Error();
132
133 if (e)
134     {
135     printf("Cannot read file.\n");
136     delete cf;
137     return -1;
138     }
139
140 cf->ReadString("Login", &login, "/?--?--?*");
141 if (login == "/?--?--?*")
142     {
143     strError = "Parameter \'Login\' not found.";
144     delete cf;
145     return -1;
146     }
147
148 cf->ReadString("Password", &password, "/?--?--?*");
149 if (login == "/?--?--?*")
150     {
151     strError = "Parameter \'Password\' not found.";
152     delete cf;
153     return -1;
154     }
155
156 cf->ReadString("ServerName", &serverName, "?*?*?");
157 if (serverName == "?*?*?")
158     {
159     strError = "Parameter \'ServerName\' not found.";
160     delete cf;
161     return -1;
162     }
163
164 cf->ReadString("ListenWebIP", &tmp, "127.0.0.1");
165 listenWebIP = inet_addr(tmp.c_str());
166 if (listenWebIP == INADDR_NONE)
167     {
168     strError = "Parameter \'ListenWebIP\' is not valid.";
169     delete cf;
170     return -1;
171     }
172
173 cf->ReadString("ServerPort", &tmp, "5555");
174 if (ParseIntInRange(tmp, 1, 65535, &port))
175     {
176     strError = "Parameter \'ServerPort\' is not valid.";
177     delete cf;
178     return -1;
179     }
180
181 cf->ReadString("LocalPort", &tmp, "0");
182 if (ParseIntInRange(tmp, 0, 65535, &localPort))
183     {
184     strError = "Parameter \'LocalPort\' is not valid.";
185     delete cf;
186     return -1;
187     }
188
189 printf("LocalPort=%d\n", localPort);
190
191 cf->ReadString("RefreshPeriod", &tmp, "5");
192 if (ParseIntInRange(tmp, 1, 24*3600, &refreshPeriod))
193     {
194     strError = "Parameter \'RefreshPeriod\' is not valid.";
195     delete cf;
196     return -1;
197     }
198
199 cf->ReadString("Reconnect", &tmp, "yes");
200 if (ParseYesNo(tmp, &reconnect))
201     {
202     strError = "Parameter \'Reconnect\' is not valid.";
203     delete cf;
204     return -1;
205     }
206
207 cf->ReadString("Daemon", &tmp, "yes");
208 if (ParseYesNo(tmp, &daemon))
209     {
210     strError = "Parameter \'Daemon\' is not valid.";
211     delete cf;
212     return -1;
213     }
214
215 cf->ReadString("ShowPid", &tmp, "no");
216 if (ParseYesNo(tmp, &showPid))
217     {
218     strError = "Parameter \'ShowPid\' is not valid.";
219     delete cf;
220     return -1;
221     }
222
223 cf->ReadString("DisableWeb", &tmp, "no");
224 if (ParseYesNo(tmp, &noWeb))
225     {
226     strError = "Parameter \'DisableWeb\' is not valid.";
227     delete cf;
228     return -1;
229     }
230
231 delete cf;
232 return 0;
233 }
234 //-----------------------------------------------------------------------------
235 string SETTINGS::GetStrError() const
236 {
237 return strError;
238 }
239 //-----------------------------------------------------------------------------
240 string SETTINGS::GetLogin() const
241 {
242 return login;
243 }
244 //-----------------------------------------------------------------------------
245 string SETTINGS::GetPassword() const
246 {
247 return password;
248 }
249 //-----------------------------------------------------------------------------
250 string SETTINGS::GetServerName() const
251 {
252 return serverName;
253 }
254 //-----------------------------------------------------------------------------
255 uint16_t SETTINGS::GetServerPort() const
256 {
257 return port;
258 }
259 //-----------------------------------------------------------------------------
260 uint16_t SETTINGS::GetLocalPort() const
261 {
262 return localPort;
263 }
264 //-----------------------------------------------------------------------------
265 int SETTINGS::GetRefreshPeriod() const
266 {
267 return refreshPeriod;
268 }
269 //-----------------------------------------------------------------------------
270 bool SETTINGS::GetDaemon() const
271 {
272 return daemon;
273 }
274 //-----------------------------------------------------------------------------
275 bool SETTINGS::GetNoWeb() const
276 {
277 return noWeb;
278 }
279 //-----------------------------------------------------------------------------
280 bool SETTINGS::GetShowPid() const
281 {
282 return showPid;
283 }
284 //-----------------------------------------------------------------------------
285 bool SETTINGS::GetReconnect() const
286 {
287 return reconnect;
288 }
289 //-----------------------------------------------------------------------------
290 uint32_t SETTINGS::GetListenWebIP() const
291 {
292 return listenWebIP;
293 }
294 //-----------------------------------------------------------------------------
295 void SETTINGS::Print() const
296 {
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;
307 }
308 //-----------------------------------------------------------------------------
309 void Usage()
310 {
311 printf("sgauth <server> <port> <login> <password>\n"); //TODO change to correct
312 }
313 //-----------------------------------------------------------------------------
314 void SetDirName(const vector<string> & dn, void *)
315 {
316 for (int j = 0; j < DIR_NUM; j++)
317     {
318     if (winKOI)
319         {
320         string dir;
321         KOIToWin(dn[j], &dir);
322         if (web)
323             web->SetDirName(dir, j);
324         }
325     else
326         {
327         if (web)
328             web->SetDirName(dn[j], j);
329         }
330     }
331 }
332 //-----------------------------------------------------------------------------
333 void StatUpdate(const LOADSTAT & ls, void *)
334 {
335 if (web)
336     web->UpdateStat(ls);
337 }
338 //-----------------------------------------------------------------------------
339 void StatusChanged(int, void *)
340 {
341
342 }
343 //-----------------------------------------------------------------------------
344 void ShowMessage(const string & message, int i, int, int, void *)
345 {
346 if (web)
347     web->AddMessage(message, i);
348 }
349 //-----------------------------------------------------------------------------
350 void ShowError(const string & message, int, void *)
351 {
352 if (web)
353      web->AddMessage(message, 0);
354 }
355 //-----------------------------------------------------------------------------
356 #ifndef WIN32
357 void CatchUSR1(int)
358 {
359 if (clnp->GetAuthorized())
360     {
361     cout << "Connect" << endl;
362     clnp->Connect();
363     }
364 }
365 //-----------------------------------------------------------------------------
366 void CatchUSR2(int)
367 {
368 cout << "Disconnect" << endl;
369 clnp->Disconnect();
370 }
371 //-----------------------------------------------------------------------------
372 void CatchTERM(int)
373 {
374 cout << "Terminated" << endl;
375 clnp->Disconnect();
376 sleep(2);
377 exit(0);
378 }
379 //-----------------------------------------------------------------------------
380 static void SetSignalHandlers()
381 {
382 struct sigaction newsa, oldsa;
383 sigset_t sigmask;
384
385 sigemptyset(&sigmask);
386 sigaddset(&sigmask, SIGTERM);
387 newsa.sa_handler = CatchTERM;
388 newsa.sa_mask = sigmask;
389 newsa.sa_flags = 0;
390 sigaction(SIGTERM, &newsa, &oldsa);
391
392 sigemptyset(&sigmask);
393 sigaddset(&sigmask, SIGINT);
394 newsa.sa_handler = CatchTERM;
395 newsa.sa_mask = sigmask;
396 newsa.sa_flags = 0;
397 sigaction(SIGINT, &newsa, &oldsa);
398
399 sigemptyset(&sigmask);
400 sigaddset(&sigmask, SIGUSR1);
401 newsa.sa_handler = CatchUSR1;
402 newsa.sa_mask = sigmask;
403 newsa.sa_flags = 0;
404 sigaction(SIGUSR1, &newsa, &oldsa);
405
406 sigemptyset(&sigmask);
407 sigaddset(&sigmask, SIGUSR2);
408 newsa.sa_handler = CatchUSR2;
409 newsa.sa_mask = sigmask;
410 newsa.sa_flags = 0;
411 sigaction(SIGUSR2, &newsa, &oldsa);
412
413 return;
414 }
415 #endif
416 //-----------------------------------------------------------------------------
417 int main(int argc, char *argv[])
418 {
419 SETTINGS settings;
420
421 #ifndef WIN32
422 if (argc == 2)
423 #else
424 if(0)
425 #endif
426     {
427     settings.SetConfFile(argv[1]);
428     }
429 else
430     {
431     /*if (argc != 5)
432         {
433         Usage();
434         exit(1);
435         }
436     else
437         {
438         string serverName(argv[1]);
439         port = strtol(argv[2], &endptr, 10);
440         if (*endptr != 0)
441             {
442             printf("Invalid port!\n");
443             exit(1);
444             }
445         login = argv[3];
446         passwd = argv[4];
447         }*/
448     }
449
450 if (settings.ReadSettings())
451     {
452     printf("ReadSettingsError\n");
453     printf("%s\n", settings.GetStrError().c_str());
454     exit(-1);
455     }
456 settings.Print();
457
458 #ifndef WIN32
459 if (settings.GetDaemon())
460     {
461     /*close(0);
462     close(1);
463     close(2);*/
464
465     switch (fork())
466         {
467         case -1:
468             exit(1);
469             break;
470
471         case 0:
472             setsid();
473             break;
474
475         default:
476             exit(0);
477             break;
478         }
479     }
480 #endif
481
482 clnp = new IA_CLIENT_PROT(settings.GetServerName(), settings.GetServerPort(), settings.GetLocalPort());
483
484 if (!settings.GetNoWeb())
485     {
486     web = new WEB();
487     web->SetRefreshPagePeriod(settings.GetRefreshPeriod());
488     web->SetListenAddr(settings.GetListenWebIP());
489     web->Start();
490     }
491
492 clnp->SetLogin(settings.GetLogin());
493 clnp->SetPassword(settings.GetPassword());
494
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());
501
502 clnp->Start();
503
504 SetSignalHandlers();
505
506 #ifdef LINUX
507 for (int i = 1; i < argc; i++)
508     memset(argv[i], 0, strlen(argv[i]));
509
510 if(argc > 1)
511     strcpy(argv[1], "Connecting...");
512 #endif
513
514 #ifdef FREEBSD
515 setproctitle("Connecting...");
516 #endif
517 clnp->Connect();
518
519 while (1)
520     {
521     #ifdef WIN32
522     Sleep(200);
523     #else
524     usleep(200000);
525     #endif
526
527     char state[20];
528
529     if (clnp->GetAuthorized())
530         {
531         if (settings.GetShowPid())
532             sprintf(state, "On %d", getpid());
533         else
534             strcpy(state, "Online");
535         }
536     else
537         {
538         if (settings.GetShowPid())
539             sprintf(state, "Off %d", getpid());
540         else
541             strcpy(state, "Offline");
542         }
543
544     #ifdef LINUX
545     for (int i = 1; i < argc; i++)
546         memset(argv[i], 0, strlen(argv[i]));
547     if(argc > 1)
548         strcpy(argv[1], state);
549     #endif
550
551     #ifdef FREEBSD
552     setproctitle(state);
553     #endif
554
555     #ifdef FREEBSD_5
556     setproctitle(state);
557     #endif
558     }
559
560 return 0;
561 }
562 //-----------------------------------------------------------------------------