]> git.stg.codes - stg.git/blobdiff - projects/sgauth/main.cpp
Merge branch 'stg-2.409-radius'
[stg.git] / projects / sgauth / main.cpp
index c7b2bfdcaa49ff53f43a225f2e444f6d4cf56b52..3256ae52424c4e8d63e62abfb95772ca0428394a 100644 (file)
  $Author: faust $
  */
 
-
-#include <stdio.h>
-#include <stdlib.h>
-
-#ifndef WIN32
 #include <unistd.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
-#include <signal.h>
-#include "conffiles.h"
-#endif
 
-#include <string.h>
-#include <vector>
+#include <csignal>
+#include <cstdio>
+#include <cstring>
 #include <iostream>
+#include <vector>
 
-#include "ia_auth_c.h"
-#include "common.h"
-#include "common_settings.h"
+#include "stg/ia.h"
+#include "stg/common.h"
 #include "web.h"
+#include "settings_impl.h"
 
 int mes;
 char infoText[256];
@@ -55,269 +49,23 @@ const int winKOI = 0;
 IA_CLIENT_PROT * clnp;
 WEB * web = NULL;
 
-using namespace std;
-
 time_t stgTime;
 
 //-----------------------------------------------------------------------------
-class SETTINGS: public COMMON_SETTINGS
-{
-public:
-                    SETTINGS();
-    virtual         ~SETTINGS(){};
-    virtual int     Reload(){ return 0; };
-    void            SetConfFile(string confFile);
-    virtual int     ReadSettings();
-
-    virtual string  GetStrError() const;
-
-    string          GetServerName() const;
-    uint16_t        GetServerPort() const;
-    uint16_t        GetLocalPort() const;
-
-    string          GetLogin() const;
-    string          GetPassword() const;
-
-    bool            GetDaemon() const;
-    bool            GetShowPid() const;
-    bool            GetNoWeb() const;
-    bool            GetReconnect() const;
-    int             GetRefreshPeriod() const;
-    uint32_t        GetListenWebIP() const;
-
-    void            Print() const;
-
-private:
-    string          login;
-    string          password;
-    string          serverName;
-    int             port;
-    int             localPort;
-    uint32_t        listenWebIP;
-    int             refreshPeriod;
-
-    bool            daemon;
-    bool            noWeb;
-    bool            reconnect;
-    bool            showPid;
-
-    string          confFile;
-};
 //-----------------------------------------------------------------------------
-SETTINGS::SETTINGS()
-    : port(0),
-      localPort(0),
-      listenWebIP(0),
-      refreshPeriod(0),
-      daemon(false),
-      noWeb(false),
-      reconnect(false),
-      showPid(false)
-{
-confFile = "/etc/sgauth.conf";
-}
-//-----------------------------------------------------------------------------
-void SETTINGS::SetConfFile(string confFile)
-{
-SETTINGS::confFile = confFile;
-}
-//-----------------------------------------------------------------------------
-int SETTINGS::ReadSettings()
-{
-CONFIGFILE * cf;
-
-cf = new CONFIGFILE(confFile);
-string tmp;
-int e = cf->Error();
-
-if (e)
-    {
-    printf("Cannot read file.\n");
-    delete cf;
-    return -1;
-    }
-
-cf->ReadString("Login", &login, "/?--?--?*");
-if (login == "/?--?--?*")
-    {
-    strError = "Parameter \'Login\' not found.";
-    delete cf;
-    return -1;
-    }
-
-cf->ReadString("Password", &password, "/?--?--?*");
-if (login == "/?--?--?*")
-    {
-    strError = "Parameter \'Password\' not found.";
-    delete cf;
-    return -1;
-    }
-
-cf->ReadString("ServerName", &serverName, "?*?*?");
-if (serverName == "?*?*?")
-    {
-    strError = "Parameter \'ServerName\' not found.";
-    delete cf;
-    return -1;
-    }
-
-cf->ReadString("ListenWebIP", &tmp, "127.0.0.1");
-listenWebIP = inet_addr(tmp.c_str());
-if (listenWebIP == INADDR_NONE)
-    {
-    strError = "Parameter \'ListenWebIP\' is not valid.";
-    delete cf;
-    return -1;
-    }
-
-cf->ReadString("ServerPort", &tmp, "5555");
-if (ParseIntInRange(tmp, 1, 65535, &port))
-    {
-    strError = "Parameter \'ServerPort\' is not valid.";
-    delete cf;
-    return -1;
-    }
-
-cf->ReadString("LocalPort", &tmp, "0");
-if (ParseIntInRange(tmp, 0, 65535, &localPort))
-    {
-    strError = "Parameter \'LocalPort\' is not valid.";
-    delete cf;
-    return -1;
-    }
-
-printf("LocalPort=%d\n", localPort);
-
-cf->ReadString("RefreshPeriod", &tmp, "5");
-if (ParseIntInRange(tmp, 1, 24*3600, &refreshPeriod))
-    {
-    strError = "Parameter \'RefreshPeriod\' is not valid.";
-    delete cf;
-    return -1;
-    }
-
-cf->ReadString("Reconnect", &tmp, "yes");
-if (ParseYesNo(tmp, &reconnect))
-    {
-    strError = "Parameter \'Reconnect\' is not valid.";
-    delete cf;
-    return -1;
-    }
-
-cf->ReadString("Daemon", &tmp, "yes");
-if (ParseYesNo(tmp, &daemon))
-    {
-    strError = "Parameter \'Daemon\' is not valid.";
-    delete cf;
-    return -1;
-    }
-
-cf->ReadString("ShowPid", &tmp, "no");
-if (ParseYesNo(tmp, &showPid))
-    {
-    strError = "Parameter \'ShowPid\' is not valid.";
-    delete cf;
-    return -1;
-    }
-
-cf->ReadString("DisableWeb", &tmp, "no");
-if (ParseYesNo(tmp, &noWeb))
-    {
-    strError = "Parameter \'DisableWeb\' is not valid.";
-    delete cf;
-    return -1;
-    }
-
-delete cf;
-return 0;
-}
-//-----------------------------------------------------------------------------
-string SETTINGS::GetStrError() const
-{
-return strError;
-}
-//-----------------------------------------------------------------------------
-string SETTINGS::GetLogin() const
-{
-return login;
-}
-//-----------------------------------------------------------------------------
-string SETTINGS::GetPassword() const
-{
-return password;
-}
-//-----------------------------------------------------------------------------
-string SETTINGS::GetServerName() const
-{
-return serverName;
-}
-//-----------------------------------------------------------------------------
-uint16_t SETTINGS::GetServerPort() const
-{
-return port;
-}
-//-----------------------------------------------------------------------------
-uint16_t SETTINGS::GetLocalPort() const
-{
-return localPort;
-}
-//-----------------------------------------------------------------------------
-int SETTINGS::GetRefreshPeriod() const
-{
-return refreshPeriod;
-}
-//-----------------------------------------------------------------------------
-bool SETTINGS::GetDaemon() const
-{
-return daemon;
-}
-//-----------------------------------------------------------------------------
-bool SETTINGS::GetNoWeb() const
-{
-return noWeb;
-}
-//-----------------------------------------------------------------------------
-bool SETTINGS::GetShowPid() const
-{
-return showPid;
-}
-//-----------------------------------------------------------------------------
-bool SETTINGS::GetReconnect() const
-{
-return reconnect;
-}
-//-----------------------------------------------------------------------------
-uint32_t SETTINGS::GetListenWebIP() const
-{
-return listenWebIP;
-}
-//-----------------------------------------------------------------------------
-void SETTINGS::Print() const
-{
-cout << "login = " << login << endl;
-cout << "password = " << password << endl;
-cout << "ip = " << serverName << endl;
-cout << "port = " << port << endl;
-cout << "localPort = " << localPort << endl;
-cout << "listenWebIP = " << inet_ntostring(listenWebIP) << endl;
-cout << "DisableWeb = " << noWeb << endl;
-cout << "refreshPeriod = " << refreshPeriod << endl;
-cout << "daemon = " << daemon << endl;
-cout << "reconnect = " << reconnect << endl;
-}
 //-----------------------------------------------------------------------------
 void Usage()
 {
-printf("sgauth <server> <port> <login> <password>\n"); //TODO change to correct
+printf("sgauth <path_to_config>\n");
 }
 //-----------------------------------------------------------------------------
-void SetDirName(const vector<string> & dn, void *)
+void SetDirName(const std::vector<std::string> & dn, void *)
 {
 for (int j = 0; j < DIR_NUM; j++)
     {
     if (winKOI)
         {
-        string dir;
+        std::string dir;
         KOIToWin(dn[j], &dir);
         if (web)
             web->SetDirName(dir, j);
@@ -338,40 +86,38 @@ if (web)
 //-----------------------------------------------------------------------------
 void StatusChanged(int, void *)
 {
-
 }
 //-----------------------------------------------------------------------------
-void ShowMessage(const string & message, int i, int, int, void *)
+void ShowMessage(const std::string & message, int i, int, int, void *)
 {
 if (web)
     web->AddMessage(message, i);
 }
 //-----------------------------------------------------------------------------
-void ShowError(const string & message, int, void *)
+void ShowError(const std::string & message, int, void *)
 {
 if (web)
      web->AddMessage(message, 0);
 }
 //-----------------------------------------------------------------------------
-#ifndef WIN32
 void CatchUSR1(int)
 {
 if (clnp->GetAuthorized())
     {
-    cout << "Connect" << endl;
+    std::cout << "Connect" << std::endl;
     clnp->Connect();
-       }
+    }
 }
 //-----------------------------------------------------------------------------
 void CatchUSR2(int)
 {
-cout << "Disconnect" << endl;
+std::cout << "Disconnect" << std::endl;
 clnp->Disconnect();
 }
 //-----------------------------------------------------------------------------
 void CatchTERM(int)
 {
-cout << "Terminated" << endl;
+std::cout << "Terminated" << std::endl;
 clnp->Disconnect();
 sleep(2);
 exit(0);
@@ -412,81 +158,47 @@ sigaction(SIGUSR2, &newsa, &oldsa);
 
 return;
 }
-#endif
 //-----------------------------------------------------------------------------
 int main(int argc, char *argv[])
 {
-//int port;
-//char *endptr;
-
-SETTINGS settings;
+SETTINGS_IMPL settings;
 
-#ifndef WIN32
 if (argc == 2)
-#else
-if(0)
-#endif
     {
     settings.SetConfFile(argv[1]);
-    if (settings.ReadSettings())
-        {
-        printf("ReadSettingsError\n");
-        printf("%s\n", settings.GetStrError().c_str());
-        exit(-1);
-        }
-    settings.Print();
     }
 else
     {
-    /*if (argc != 5)
-        {
-        Usage();
-        exit(1);
-        }
-    else
-        {
-        string serverName(argv[1]);
-        port = strtol(argv[2], &endptr, 10);
-        if (*endptr != 0)
-            {
-            printf("Invalid port!\n");
-            exit(1);
-            }
-        login = argv[3];
-        passwd = argv[4];
-        }*/
+    // Usage
     }
 
-//settings.Print();
+if (settings.ReadSettings())
+    {
+    printf("ReadSettingsError\n");
+    printf("%s\n", settings.GetStrError().c_str());
+    exit(-1);
+    }
+settings.Print();
 
-#ifndef WIN32
 if (settings.GetDaemon())
     {
-    /*close(0);
-    close(1);
-    close(2);*/
-
     switch (fork())
         {
-        case -1:    // ìÁÖÁ
+        case -1:
             exit(1);
             break;
 
-        case 0:    // ðÏÔÏÍÏË
+        case 0:
             setsid();
             break;
 
-        default:    // ïÓÎÏ×ÎÏÊ ÐÒÏÃÅÓÓ
+        default:
             exit(0);
             break;
         }
     }
 
-
-
-#endif
-
-clnp = new IA_CLIENT_PROT(settings.GetServerName(), settings.GetServerPort(), settings.GetLocalPort());
+clnp = new IA_CLIENT_PROT(settings.GetServerName(), settings.GetServerPort(), settings.GetLocalName(), settings.GetLocalPort());
 
 if (!settings.GetNoWeb())
     {
@@ -506,7 +218,6 @@ clnp->SetDirNameCb(SetDirName, NULL);
 clnp->SetStatChangedCb(StatUpdate, NULL);
 clnp->SetReconnect(settings.GetReconnect());
 
-
 clnp->Start();
 
 SetSignalHandlers();
@@ -526,11 +237,8 @@ clnp->Connect();
 
 while (1)
     {
-    #ifdef WIN32
-    Sleep(200);
-    #else
-    usleep(200000);
-    #endif
+    struct timespec ts = {0, 200000000};
+    nanosleep(&ts, NULL);
 
     char state[20];
 
@@ -568,5 +276,3 @@ while (1)
 return 0;
 }
 //-----------------------------------------------------------------------------
-
-