]> 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 SetDirName(const vector<string> & dn, void *)
307 {
308 for (int j = 0; j < DIR_NUM; j++)
309     {
310     if (winKOI)
311         {
312         string dir;
313         KOIToWin(dn[j], &dir);
314         if (web)
315             web->SetDirName(dir, j);
316         }
317     else
318         {
319         if (web)
320             web->SetDirName(dn[j], j);
321         }
322     }
323 }
324 //-----------------------------------------------------------------------------
325 void StatUpdate(const LOADSTAT & ls, void *)
326 {
327 if (web)
328     web->UpdateStat(ls);
329 }
330 //-----------------------------------------------------------------------------
331 void StatusChanged(int, void *)
332 {
333
334 }
335 //-----------------------------------------------------------------------------
336 void ShowMessage(const string & message, int i, int, int, void *)
337 {
338 if (web)
339     web->AddMessage(message, i);
340 }
341 //-----------------------------------------------------------------------------
342 void ShowError(const string & message, int, void *)
343 {
344 if (web)
345      web->AddMessage(message, 0);
346 }
347 //-----------------------------------------------------------------------------
348 #ifndef WIN32
349 void CatchUSR1(int)
350 {
351 if (clnp->GetAuthorized())
352     {
353     cout << "Connect" << endl;
354     clnp->Connect();
355         }
356 }
357 //-----------------------------------------------------------------------------
358 void CatchUSR2(int)
359 {
360 cout << "Disconnect" << endl;
361 clnp->Disconnect();
362 }
363 //-----------------------------------------------------------------------------
364 void CatchTERM(int)
365 {
366 cout << "Terminated" << endl;
367 clnp->Disconnect();
368 sleep(2);
369 exit(0);
370 }
371 //-----------------------------------------------------------------------------
372 static void SetSignalHandlers()
373 {
374 struct sigaction newsa, oldsa;
375 sigset_t sigmask;
376
377 sigemptyset(&sigmask);
378 sigaddset(&sigmask, SIGTERM);
379 newsa.sa_handler = CatchTERM;
380 newsa.sa_mask = sigmask;
381 newsa.sa_flags = 0;
382 sigaction(SIGTERM, &newsa, &oldsa);
383
384 sigemptyset(&sigmask);
385 sigaddset(&sigmask, SIGINT);
386 newsa.sa_handler = CatchTERM;
387 newsa.sa_mask = sigmask;
388 newsa.sa_flags = 0;
389 sigaction(SIGINT, &newsa, &oldsa);
390
391 sigemptyset(&sigmask);
392 sigaddset(&sigmask, SIGUSR1);
393 newsa.sa_handler = CatchUSR1;
394 newsa.sa_mask = sigmask;
395 newsa.sa_flags = 0;
396 sigaction(SIGUSR1, &newsa, &oldsa);
397
398 sigemptyset(&sigmask);
399 sigaddset(&sigmask, SIGUSR2);
400 newsa.sa_handler = CatchUSR2;
401 newsa.sa_mask = sigmask;
402 newsa.sa_flags = 0;
403 sigaction(SIGUSR2, &newsa, &oldsa);
404
405 return;
406 }
407 #endif
408 //-----------------------------------------------------------------------------
409 int main(int argc, char *argv[])
410 {
411 //int port;
412 //char *endptr;
413
414 SETTINGS settings;
415
416 #ifndef WIN32
417 if (argc == 2)
418 #else
419 if(0)
420 #endif
421     {
422     settings.SetConfFile(argv[1]);
423     if (settings.ReadSettings())
424         {
425         printf("ReadSettingsError\n");
426         printf("%s\n", settings.GetStrError().c_str());
427         exit(-1);
428         }
429     settings.Print();
430     }
431 else
432     {
433     /*if (argc != 5)
434         {
435         Usage();
436         exit(1);
437         }
438     else
439         {
440         string serverName(argv[1]);
441         port = strtol(argv[2], &endptr, 10);
442         if (*endptr != 0)
443             {
444             printf("Invalid port!\n");
445             exit(1);
446             }
447         login = argv[3];
448         passwd = argv[4];
449         }*/
450     }
451
452 //settings.Print();
453
454 #ifndef WIN32
455 if (settings.GetDaemon())
456     {
457     /*close(0);
458     close(1);
459     close(2);*/
460
461     switch (fork())
462         {
463         case -1:    // ìÁÖÁ
464             exit(1);
465             break;
466
467         case 0:    // ðÏÔÏÍÏË
468             setsid();
469             break;
470
471         default:    // ïÓÎÏ×ÎÏÊ ÐÒÏÃÅÓÓ
472             exit(0);
473             break;
474         }
475     }
476
477
478
479 #endif
480
481 clnp = new IA_CLIENT_PROT(settings.GetServerName(), settings.GetServerPort(), settings.GetLocalPort());
482
483 if (!settings.GetNoWeb())
484     {
485     web = new WEB();
486     web->SetRefreshPagePeriod(settings.GetRefreshPeriod());
487     web->SetListenAddr(settings.GetListenWebIP());
488     web->Start();
489     }
490
491 clnp->SetLogin(settings.GetLogin());
492 clnp->SetPassword(settings.GetPassword());
493
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());
500
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 //-----------------------------------------------------------------------------
563
564