]> git.stg.codes - stg.git/blob - projects/sgauth/main.cpp
Добавление исходников
[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 {
109 confFile = "/etc/sgauth.conf";
110 }
111 //-----------------------------------------------------------------------------
112 void SETTINGS::SetConfFile(string confFile)
113 {
114 SETTINGS::confFile = confFile;
115 }
116 //-----------------------------------------------------------------------------
117 int SETTINGS::ReadSettings()
118 {
119 CONFIGFILE * cf;
120
121 cf = new CONFIGFILE(confFile);
122 string tmp;
123 int e = cf->Error();
124
125 if (e)
126     {
127     printf("Cannot read file.\n");
128     delete cf;
129     return -1;
130     }
131
132 cf->ReadString("Login", &login, "/?--?--?*");
133 if (login == "/?--?--?*")
134     {
135     strError = "Parameter \'Login\' not found.";
136     delete cf;
137     return -1;
138     }
139
140 cf->ReadString("Password", &password, "/?--?--?*");
141 if (login == "/?--?--?*")
142     {
143     strError = "Parameter \'Password\' not found.";
144     delete cf;
145     return -1;
146     }
147
148 cf->ReadString("ServerName", &serverName, "?*?*?");
149 if (serverName == "?*?*?")
150     {
151     strError = "Parameter \'ServerName\' not found.";
152     delete cf;
153     return -1;
154     }
155
156 cf->ReadString("ListenWebIP", &tmp, "127.0.0.1");
157 listenWebIP = inet_addr(tmp.c_str());
158 if (listenWebIP == INADDR_NONE)
159     {
160     strError = "Parameter \'ListenWebIP\' is not valid.";
161     delete cf;
162     return -1;
163     }
164
165 cf->ReadString("ServerPort", &tmp, "5555");
166 if (ParseIntInRange(tmp, 1, 65535, &port))
167     {
168     strError = "Parameter \'ServerPort\' is not valid.";
169     delete cf;
170     return -1;
171     }
172
173 cf->ReadString("LocalPort", &tmp, "0");
174 if (ParseIntInRange(tmp, 0, 65535, &localPort))
175     {
176     strError = "Parameter \'LocalPort\' is not valid.";
177     delete cf;
178     return -1;
179     }
180
181 printf("LocalPort=%d\n", localPort);
182
183 cf->ReadString("RefreshPeriod", &tmp, "5");
184 if (ParseIntInRange(tmp, 1, 24*3600, &refreshPeriod))
185     {
186     strError = "Parameter \'RefreshPeriod\' is not valid.";
187     delete cf;
188     return -1;
189     }
190
191 cf->ReadString("Reconnect", &tmp, "yes");
192 if (ParseYesNo(tmp, &reconnect))
193     {
194     strError = "Parameter \'Reconnect\' is not valid.";
195     delete cf;
196     return -1;
197     }
198
199 cf->ReadString("Daemon", &tmp, "yes");
200 if (ParseYesNo(tmp, &daemon))
201     {
202     strError = "Parameter \'Daemon\' is not valid.";
203     delete cf;
204     return -1;
205     }
206
207 cf->ReadString("ShowPid", &tmp, "no");
208 if (ParseYesNo(tmp, &showPid))
209     {
210     strError = "Parameter \'ShowPid\' is not valid.";
211     delete cf;
212     return -1;
213     }
214
215 cf->ReadString("DisableWeb", &tmp, "no");
216 if (ParseYesNo(tmp, &noWeb))
217     {
218     strError = "Parameter \'DisableWeb\' is not valid.";
219     delete cf;
220     return -1;
221     }
222
223 delete cf;
224 return 0;
225 }
226 //-----------------------------------------------------------------------------
227 string SETTINGS::GetStrError() const
228 {
229 return strError;
230 }
231 //-----------------------------------------------------------------------------
232 string SETTINGS::GetLogin() const
233 {
234 return login;
235 }
236 //-----------------------------------------------------------------------------
237 string SETTINGS::GetPassword() const
238 {
239 return password;
240 }
241 //-----------------------------------------------------------------------------
242 string SETTINGS::GetServerName() const
243 {
244 return serverName;
245 }
246 //-----------------------------------------------------------------------------
247 uint16_t SETTINGS::GetServerPort() const
248 {
249 return port;
250 }
251 //-----------------------------------------------------------------------------
252 uint16_t SETTINGS::GetLocalPort() const
253 {
254 return localPort;
255 }
256 //-----------------------------------------------------------------------------
257 int SETTINGS::GetRefreshPeriod() const
258 {
259 return refreshPeriod;
260 }
261 //-----------------------------------------------------------------------------
262 bool SETTINGS::GetDaemon() const
263 {
264 return daemon;
265 }
266 //-----------------------------------------------------------------------------
267 bool SETTINGS::GetNoWeb() const
268 {
269 return noWeb;
270 }
271 //-----------------------------------------------------------------------------
272 bool SETTINGS::GetShowPid() const
273 {
274 return showPid;
275 }
276 //-----------------------------------------------------------------------------
277 bool SETTINGS::GetReconnect() const
278 {
279 return reconnect;
280 }
281 //-----------------------------------------------------------------------------
282 uint32_t SETTINGS::GetListenWebIP() const
283 {
284 return listenWebIP;
285 }
286 //-----------------------------------------------------------------------------
287 void SETTINGS::Print() const
288 {
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;
299 }
300 //-----------------------------------------------------------------------------
301 void Usage()
302 {
303 printf("sgauth <server> <port> <login> <password>\n"); //TODO change to correct
304 }
305 //-----------------------------------------------------------------------------
306 void EventsFn(int)
307 {
308 LOADSTAT ls;
309 clnp->GetStat(&ls);
310 }
311 //-----------------------------------------------------------------------------
312 void SetDirName(const vector<string> & dn, void *)
313 {
314 for (int j = 0; j < DIR_NUM; j++)
315     {
316     if (winKOI)
317         {
318         string dir;
319         KOIToWin(dn[j], &dir);
320         if (web)
321             web->SetDirName(dir, j);
322         }
323     else
324         {
325         if (web)
326             web->SetDirName(dn[j], j);
327         }
328     }
329 }
330 //-----------------------------------------------------------------------------
331 void StatUpdate(const LOADSTAT & ls, void *)
332 {
333 if (web)
334     web->UpdateStat(ls);
335 }
336 //-----------------------------------------------------------------------------
337 void StatusChanged(int, void *)
338 {
339
340 }
341 //-----------------------------------------------------------------------------
342 void ShowMessage(const string & message, int i, int, int, void *)
343 {
344 if (web)
345     web->AddMessage(message, i);
346 }
347 //-----------------------------------------------------------------------------
348 void ShowError(const string & message, int, void *)
349 {
350 if (web)
351      web->AddMessage(message, 0);
352 }
353 //-----------------------------------------------------------------------------
354 #ifndef WIN32
355 void CatchUSR1(int)
356 {
357 if (clnp->GetAuthorized())
358     {
359     cout << "Connect" << endl;
360     clnp->Connect();
361         }
362 }
363 //-----------------------------------------------------------------------------
364 void CatchUSR2(int)
365 {
366 cout << "Disconnect" << endl;
367 clnp->Disconnect();
368 }
369 //-----------------------------------------------------------------------------
370 void CatchTERM(int)
371 {
372 cout << "Terminated" << endl;
373 clnp->Disconnect();
374 sleep(2);
375 exit(0);
376 }
377 //-----------------------------------------------------------------------------
378 static void SetSignalHandlers()
379 {
380 struct sigaction newsa, oldsa;
381 sigset_t sigmask;
382
383 sigemptyset(&sigmask);
384 sigaddset(&sigmask, SIGTERM);
385 newsa.sa_handler = CatchTERM;
386 newsa.sa_mask = sigmask;
387 newsa.sa_flags = 0;
388 sigaction(SIGTERM, &newsa, &oldsa);
389
390 sigemptyset(&sigmask);
391 sigaddset(&sigmask, SIGINT);
392 newsa.sa_handler = CatchTERM;
393 newsa.sa_mask = sigmask;
394 newsa.sa_flags = 0;
395 sigaction(SIGINT, &newsa, &oldsa);
396
397 sigemptyset(&sigmask);
398 sigaddset(&sigmask, SIGUSR1);
399 newsa.sa_handler = CatchUSR1;
400 newsa.sa_mask = sigmask;
401 newsa.sa_flags = 0;
402 sigaction(SIGUSR1, &newsa, &oldsa);
403
404 sigemptyset(&sigmask);
405 sigaddset(&sigmask, SIGUSR2);
406 newsa.sa_handler = CatchUSR2;
407 newsa.sa_mask = sigmask;
408 newsa.sa_flags = 0;
409 sigaction(SIGUSR2, &newsa, &oldsa);
410
411 return;
412 }
413 #endif
414 //-----------------------------------------------------------------------------
415 int main(int argc, char *argv[])
416 {
417 //int port;
418 //char *endptr;
419
420 SETTINGS settings;
421
422 #ifndef WIN32
423 if (argc == 2)
424 #else
425 if(0)
426 #endif
427     {
428     settings.SetConfFile(argv[1]);
429     if (settings.ReadSettings())
430         {
431         printf("ReadSettingsError\n");
432         printf("%s\n", settings.GetStrError().c_str());
433         exit(-1);
434         }
435     settings.Print();
436     }
437 else
438     {
439     /*if (argc != 5)
440         {
441         Usage();
442         exit(1);
443         }
444     else
445         {
446         string serverName(argv[1]);
447         port = strtol(argv[2], &endptr, 10);
448         if (*endptr != 0)
449             {
450             printf("Invalid port!\n");
451             exit(1);
452             }
453         login = argv[3];
454         passwd = argv[4];
455         }*/
456     }
457
458 //settings.Print();
459
460 #ifndef WIN32
461 if (settings.GetDaemon())
462     {
463     /*close(0);
464     close(1);
465     close(2);*/
466
467     switch (fork())
468         {
469         case -1:    // ìÁÖÁ
470             exit(1);
471             break;
472
473         case 0:    // ðÏÔÏÍÏË
474             setsid();
475             break;
476
477         default:    // ïÓÎÏ×ÎÏÊ ÐÒÏÃÅÓÓ
478             exit(0);
479             break;
480         }
481     }
482
483
484
485 #endif
486
487 clnp = new IA_CLIENT_PROT(settings.GetServerName(), settings.GetServerPort(), settings.GetLocalPort());
488
489 if (!settings.GetNoWeb())
490     {
491     web = new WEB();
492     web->SetRefreshPagePeriod(settings.GetRefreshPeriod());
493     web->SetListenAddr(settings.GetListenWebIP());
494     web->Start();
495     }
496
497 clnp->SetLogin(settings.GetLogin());
498 clnp->SetPassword(settings.GetPassword());
499
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());
506
507
508 clnp->Start();
509
510 SetSignalHandlers();
511
512 #ifdef LINUX
513 for (int i = 1; i < argc; i++)
514     memset(argv[i], 0, strlen(argv[i]));
515
516 if(argc > 1)
517     strcpy(argv[1], "Connecting...");
518 #endif
519
520 #ifdef FREEBSD
521 setproctitle("Connecting...");
522 #endif
523 clnp->Connect();
524
525 while (1)
526     {
527     #ifdef WIN32
528     Sleep(200);
529     #else
530     usleep(200000);
531     #endif
532
533     char state[20];
534
535     if (clnp->GetAuthorized())
536         {
537         if (settings.GetShowPid())
538             sprintf(state, "On %d", getpid());
539         else
540             strcpy(state, "Online");
541         }
542     else
543         {
544         if (settings.GetShowPid())
545             sprintf(state, "Off %d", getpid());
546         else
547             strcpy(state, "Offline");
548         }
549
550     #ifdef LINUX
551     for (int i = 1; i < argc; i++)
552         memset(argv[i], 0, strlen(argv[i]));
553     if(argc > 1)
554         strcpy(argv[1], state);
555     #endif
556
557     #ifdef FREEBSD
558     setproctitle(state);
559     #endif
560
561     #ifdef FREEBSD_5
562     setproctitle(state);
563     #endif
564     }
565
566 return 0;
567 }
568 //-----------------------------------------------------------------------------
569
570