]> git.stg.codes - stg.git/commitdiff
Merge branch 'log-unauth-reasons'
authorMaxim Mamontov <faust.madf@gmail.com>
Thu, 14 Nov 2013 20:01:45 +0000 (22:01 +0200)
committerMaxim Mamontov <faust.madf@gmail.com>
Thu, 14 Nov 2013 20:01:45 +0000 (22:01 +0200)
Conflicts:
projects/stargazer/user_impl.cpp

1  2 
include/stg/users.h
projects/stargazer/main.cpp
projects/stargazer/user_impl.cpp
projects/stargazer/user_impl.h
projects/stargazer/users_impl.cpp
projects/stargazer/users_impl.h

diff --combined include/stg/users.h
index a1d5d295ced2f91adaac53ac95589e5c5c1ad42f,765fc14d2e505b757dcfdbf5125ea5a43d505bb0..1a8d7a4379bff76d74b685dea039fcf3057aa44e
@@@ -32,7 -32,6 +32,7 @@@ class USERS 
  public:
      virtual ~USERS() {}
      virtual int  FindByName(const std::string & login, USER_PTR * user) = 0;
 +    virtual int  FindByName(const std::string & login, CONST_USER_PTR * user) const = 0;
  
      virtual bool TariffInUse(const std::string & tariffName) const = 0;
  
@@@ -47,7 -46,9 +47,9 @@@
  
      virtual bool Authorize(const std::string & login, uint32_t ip,
                             uint32_t enabledDirs, const AUTH * auth) = 0;
-     virtual bool Unauthorize(const std::string & login, const AUTH * auth) = 0;
+     virtual bool Unauthorize(const std::string & login,
+                              const AUTH * auth,
+                              const std::string & reason = std::string()) = 0;
  
      virtual int  ReadUsers() = 0;
      virtual size_t Count() const = 0;
@@@ -62,7 -63,6 +64,7 @@@
  
      virtual int  Start() = 0;
      virtual int  Stop() = 0;
 +
  };
  
  #endif
index 7cc8eccc6e54c32f7ead49ad48bd01357b7d88bf,200fd2a664e15f46e1d2228b20ba6f4db509bdd3..1e4c677e255133503bbc25c61721874cf3891d95
@@@ -271,6 -271,7 +271,6 @@@ if (settings->ReadSettings()
      WriteServLog("ReadSettings error. %s", settings->GetStrError().c_str());
      exit(1);
      }
 -
  #ifndef NO_DAEMON
  std::string startFile(settings->GetConfDir() + START_FILE);
  #endif
@@@ -463,7 -464,7 +463,7 @@@ while (true
                  }
              break;
          default:
-             WriteServLog("Ignore signel %d", sig);
+             WriteServLog("Ignore signal %d", sig);
              break;
          }
      if (stop)
index 13cf0b77dc87cd4c1337c6cefc530db7e728572a,37178dd696d0bb3f77ad8776c17f7b490426fddb..7b0348bc57e75928936640cbed90d90bd9b28481
  #define _GNU_SOURCE
  #endif
  
 -#include <pthread.h>
 -#include <unistd.h> // access
 -
 -#include <cassert>
 -#include <cstdlib>
 -#include <cmath>
 -#include <algorithm>
 -#include <functional>
 +#include "user_impl.h"
 +#include "settings_impl.h"
 +#include "stg_timer.h"
  
  #include "stg/users.h"
  #include "stg/common.h"
  #include "stg/tariff.h"
  #include "stg/tariffs.h"
  #include "stg/admin.h"
 -#include "user_impl.h"
 -#include "settings_impl.h"
 -#include "stg_timer.h"
 +
 +#include <algorithm>
 +#include <functional>
 +
 +#include <cassert>
 +#include <cstdlib>
 +#include <cmath>
 +
 +#include <pthread.h>
 +#include <unistd.h> // access
  
  #ifdef USE_ABSTRACT_SETTINGS
  USER_IMPL::USER_IMPL(const SETTINGS * s,
        property(s->GetScriptsDir()),
        WriteServLog(GetStgLogger()),
        lastScanMessages(0),
 -      login(),
        id(0),
        __connected(0),
        connected(__connected),
 -      enabledDirs(),
 -      userIDGenerator(),
        __currIP(0),
        currIP(__currIP),
        lastIPForDisconnect(0),
        store(st),
        tariffs(t),
        tariff(NULL),
 -      traffStat(),
 -      traffStatSaved(),
        settings(s),
 -      authorizedBy(),
 -      messages(),
 +      authorizedModificationTime(0),
        deleted(false),
        lastWriteStat(0),
        lastWriteDetailedStat(0),
        userdata7(property.userdata7),
        userdata8(property.userdata8),
        userdata9(property.userdata9),
 -      sessionUpload(),
 -      sessionDownload(),
        passiveNotifier(this),
        tariffNotifier(this),
        cashNotifier(this),
 -      ipNotifier(this),
 -      mutex(),
 -      errorStr()
 +      ipNotifier(this)
  {
  password = "*_EMPTY_PASSWORD_*";
  tariffName = NO_TARIFF_NAME;
@@@ -147,9 -155,12 +147,9 @@@ USER_IMPL::USER_IMPL(const SETTINGS_IMP
        property(s->GetScriptsDir()),
        WriteServLog(GetStgLogger()),
        lastScanMessages(0),
 -      login(),
        id(0),
        __connected(0),
        connected(__connected),
 -      enabledDirs(),
 -      userIDGenerator(),
        __currIP(0),
        currIP(__currIP),
        lastIPForDisconnect(0),
        store(st),
        tariffs(t),
        tariff(NULL),
 -      traffStat(),
 -      traffStatSaved(),
        settings(s),
 -      authorizedBy(),
 -      messages(),
 +      authorizedModificationTime(0),
        deleted(false),
        lastWriteStat(0),
        lastWriteDetailedStat(0),
        userdata7(property.userdata7),
        userdata8(property.userdata8),
        userdata9(property.userdata9),
 -      sessionUpload(),
 -      sessionDownload(),
        passiveNotifier(this),
        disabledNotifier(this),
        tariffNotifier(this),
        cashNotifier(this),
 -      ipNotifier(this),
 -      mutex(),
 -      errorStr()
 +      ipNotifier(this)
  {
  password = "*_EMPTY_PASSWORD_*";
  tariffName = NO_TARIFF_NAME;
@@@ -232,6 -250,7 +232,6 @@@ USER_IMPL::USER_IMPL(const USER_IMPL & 
        id(u.id),
        __connected(0),
        connected(__connected),
 -      enabledDirs(),
        userIDGenerator(u.userIDGenerator),
        __currIP(u.__currIP),
        currIP(__currIP),
        traffStat(u.traffStat),
        traffStatSaved(u.traffStatSaved),
        settings(u.settings),
 -      authorizedBy(),
 +      authorizedModificationTime(u.authorizedModificationTime),
        messages(u.messages),
        deleted(u.deleted),
        lastWriteStat(u.lastWriteStat),
        disabledNotifier(this),
        tariffNotifier(this),
        cashNotifier(this),
 -      ipNotifier(this),
 -      mutex(),
 -      errorStr()
 +      ipNotifier(this)
  {
  if (&u == this)
      return;
@@@ -516,8 -537,6 +516,8 @@@ els
          }
      }
  
 +if (authorizedBy.empty())
 +    authorizedModificationTime = stgTime;
  authorizedBy.insert(auth);
  
  ScanMessage();
  return 0;
  }
  //-----------------------------------------------------------------------------
- void USER_IMPL::Unauthorize(const AUTH * auth)
+ void USER_IMPL::Unauthorize(const AUTH * auth, const std::string & reason)
  {
  STG_LOCKER lock(&mutex, __FILE__, __LINE__);
  /*
@@@ -536,7 -555,7 +536,8 @@@ if (!authorizedBy.erase(auth)
  
  if (authorizedBy.empty())
      {
 +    authorizedModificationTime = stgTime;
+     lastDisconnectReason = reason;
      lastIPForDisconnect = currIP;
      currIP = 0; // DelUser in traffcounter
      return;
@@@ -579,7 -598,6 +580,7 @@@ if (!fakeConnect
              }
  
          std::string scriptOnConnectParams;
 +
          strprintf(&scriptOnConnectParams,
                  "%s \"%s\" \"%s\" \"%f\" \"%d\" \"%s\"",
                  scriptOnConnect.c_str(),
                  id,
                  dirsStr);
  
 +        std::vector<std::string>::const_iterator it(settings->GetScriptParams().begin());
 +        while (it != settings->GetScriptParams().end())
 +            {
 +            scriptOnConnectParams += " \"" + GetParamValue(it->c_str()) + "\"";
 +            ++it;
 +            }
 +
          ScriptExec(scriptOnConnectParams.c_str());
          }
      else
@@@ -632,7 -643,6 +633,7 @@@ if (!lastIPForDisconnect
  
  if (!fakeDisconnect)
      {
 +    lastDisconnectReason = reason;
      std::string scriptOnDisonnect = settings->GetScriptsDir() + "/OnDisconnect";
  
      if (access(scriptOnDisonnect.c_str(), X_OK) == 0)
                  id,
                  dirsStr);
  
 +        std::vector<std::string>::const_iterator it(settings->GetScriptParams().begin());
 +        while (it != settings->GetScriptParams().end())
 +            {
 +            scriptOnDisonnectParams += " \"" + GetParamValue(it->c_str()) + "\"";
 +            ++it;
 +            }
 +
          ScriptExec(scriptOnDisonnectParams.c_str());
          }
      else
      connected = false;
      }
  
- if (store->WriteUserDisconnect(login, up, down, sessionUpload, sessionDownload, cash, freeMb, reason))
+ std::string reasonMessage(reason);
+ if (!lastDisconnectReason.empty())
+     reasonMessage += ": " + lastDisconnectReason;
+ if (store->WriteUserDisconnect(login, up, down, sessionUpload, sessionDownload,
+                                cash, freeMb, reasonMessage))
      {
      WriteServLog("Cannot write disconnect for user %s.", login.c_str());
      WriteServLog("%s", store->GetStrError().c_str());
@@@ -1464,43 -1472,6 +1470,43 @@@ while (it != messages.end()
      }
  }
  //-----------------------------------------------------------------------------
 +std::string USER_IMPL::GetParamValue(const std::string & name) const
 +{
 +if (name == "freeMb")       return property.freeMb.ToString();
 +if (name == "passive")      return property.passive.ToString();
 +if (name == "disabled")     return property.disabled.ToString();
 +if (name == "alwaysOnline") return property.alwaysOnline.ToString();
 +if (name == "tariffName")   return property.tariffName;
 +if (name == "nextTariff")   return property.nextTariff;
 +if (name == "address")      return property.address;
 +if (name == "note")         return property.note;
 +if (name == "group")        return property.group;
 +if (name == "email")        return property.email;
 +if (name == "phone")        return property.phone;
 +if (name == "realName")     return property.realName;
 +if (name == "credit")       return property.credit.ToString();
 +if (name == "userdata0")    return property.userdata0;
 +if (name == "userdata1")    return property.userdata1;
 +if (name == "userdata2")    return property.userdata2;
 +if (name == "userdata3")    return property.userdata3;
 +if (name == "userdata4")    return property.userdata4;
 +if (name == "userdata5")    return property.userdata5;
 +if (name == "userdata6")    return property.userdata6;
 +if (name == "userdata7")    return property.userdata7;
 +if (name == "userdata8")    return property.userdata8;
 +if (name == "userdata9")    return property.userdata9;
 +if (name == "cash")         return property.cash.ToString();
 +if (name == "id")
 +    {
 +    std::stringstream stream;
 +    stream << id;
 +    return stream.str();;
 +    }
 +if (name == "login")        return login;
 +if (name == "ip")           return currIP.ToString();
 +return "";
 +}
 +//-----------------------------------------------------------------------------
  //-----------------------------------------------------------------------------
  //-----------------------------------------------------------------------------
  void CHG_PASSIVE_NOTIFIER::Notify(const int & oldPassive, const int & newPassive)
index 26ff58fc2b8d902f093739e4cc867f1670a43794,6f6030ca40d02474af7f554aa676aee30b7e349e..68945d2c65547efc9ef230ce3d9ef9810aaf4bcc
  #ifndef USER_IMPL_H
  #define USER_IMPL_H
  
 -#include <ctime>
 -#include <list>
 -#include <vector>
 -#include <string>
 -#include <set>
 -
  #include "stg/user.h"
 -#include "stg/os_int.h"
 -#include "stg/const.h"
  #include "stg/user_stat.h"
  #include "stg/user_conf.h"
  #include "stg/user_ips.h"
  #include "stg/auth.h"
  #include "stg/message.h"
  #include "stg/noncopyable.h"
 +#include "stg/os_int.h"
 +#include "stg/const.h"
 +
 +#include <list>
 +#include <vector>
 +#include <string>
 +#include <set>
 +
 +#include <ctime>
  
  //-----------------------------------------------------------------------------
  class TARIFF;
@@@ -184,11 -183,10 +184,12 @@@ public
  
      bool            GetConnected() const { return connected; }
      time_t          GetConnectedModificationTime() const { return connected.ModificationTime(); }
 +    const std::string & GetLastDisconnectReason() const { return lastDisconnectReason; }
      int             GetAuthorized() const { return static_cast<int>(authorizedBy.size()); }
 +    time_t          GetAuthorizedModificationTime() const { return authorizedModificationTime; }
      int             Authorize(uint32_t ip, uint32_t enabledDirs, const AUTH * auth);
-     void            Unauthorize(const AUTH * auth);
+     void            Unauthorize(const AUTH * auth,
+                                 const std::string & reason = std::string());
      bool            IsAuthorizedBy(const AUTH * auth) const;
      std::vector<std::string> GetAuthorizers() const;
  
      void            OnAdd();
      void            OnDelete();
  
 +    virtual std::string GetParamValue(const std::string & name) const;
 +
  private:
      USER_IMPL & operator=(const USER_IMPL & rvalue);
  
      int             id;
      bool            __connected;
      USER_PROPERTY<bool> connected;
 +    std::string     lastDisconnectReason;
  
      bool            enabledDirs[DIR_NUM];
  
      USER_PROPERTY<uint32_t> currIP;
  
      uint32_t        lastIPForDisconnect; // User's ip after unauth but before disconnect
+     std::string     lastDisconnectReason;
  
      time_t          pingTime;
  
  #endif
  
      std::set<const AUTH *> authorizedBy;
 +    time_t          authorizedModificationTime;
  
      std::list<STG_MSG> messages;
  
index 5b88e685904a1d2ff0f509ea729f6491a5472247,97b4bbbce1eed913ef2c50e6bbe012c48e24215b..4c4d4ce03e4c0dcb9f9c360ea0676f492f49a357
@@@ -88,39 -88,23 +88,39 @@@ pthread_mutex_destroy(&mutex)
  //-----------------------------------------------------------------------------
  int USERS_IMPL::FindByNameNonLock(const std::string & login, user_iter * user)
  {
 -std::map<std::string, user_iter>::iterator iter;
 -iter = loginIndex.find(login);
 -if (iter != loginIndex.end())
 -    {
 -    if (user)
 -        *user = iter->second;
 -    return 0;
 -    }
 -return -1;
 +const std::map<std::string, user_iter>::const_iterator iter(loginIndex.find(login));
 +if (iter == loginIndex.end())
 +    return -1;
 +if (user)
 +    *user = iter->second;
 +return 0;
 +}
 +//-----------------------------------------------------------------------------
 +int USERS_IMPL::FindByNameNonLock(const std::string & login, const_user_iter * user) const
 +{
 +const std::map<std::string, user_iter>::const_iterator iter(loginIndex.find(login));
 +if (iter == loginIndex.end())
 +    return -1;
 +if (user)
 +    *user = iter->second;
 +return 0;
  }
  //-----------------------------------------------------------------------------
  int USERS_IMPL::FindByName(const std::string & login, USER_PTR * user)
  {
  STG_LOCKER lock(&mutex, __FILE__, __LINE__);
  user_iter u;
 -int res = FindByNameNonLock(login, &u);
 -if (res)
 +if (FindByNameNonLock(login, &u))
 +    return -1;
 +*user = &(*u);
 +return 0;
 +}
 +//-----------------------------------------------------------------------------
 +int USERS_IMPL::FindByName(const std::string & login, CONST_USER_PTR * user) const
 +{
 +STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 +const_user_iter u;
 +if (FindByNameNonLock(login, &u))
      return -1;
  *user = &(*u);
  return 0;
@@@ -314,7 -298,9 +314,9 @@@ AddToIPIdx(iter)
  return true;
  }
  //-----------------------------------------------------------------------------
- bool USERS_IMPL::Unauthorize(const std::string & login, const AUTH * auth)
+ bool USERS_IMPL::Unauthorize(const std::string & login,
+                              const AUTH * auth,
+                              const std::string & reason)
  {
  user_iter iter;
  STG_LOCKER lock(&mutex, __FILE__, __LINE__);
@@@ -326,7 -312,7 +328,7 @@@ if (FindByNameNonLock(login, &iter)
  
  uint32_t ip = iter->GetCurrIP();
  
- iter->Unauthorize(auth);
+ iter->Unauthorize(auth, reason);
  
  if (!iter->GetAuthorized())
      DelFromIPIdx(ip);
index b9d2b22d34e894f123862de2d089dbedc8e489ee,70a5b9bdf9fe9f5d4f2f473fedb2e2d29aa69ab8..43065bdeb18753f3ab9bbeeb1c68491892cc2b48
@@@ -75,7 -75,6 +75,7 @@@ public
      virtual ~USERS_IMPL();
  
      int             FindByName(const std::string & login, USER_PTR * user);
 +    int             FindByName(const std::string & login, CONST_USER_PTR * user) const;
  
      bool            TariffInUse(const std::string & tariffName) const;
  
@@@ -96,7 -95,9 +96,9 @@@
  
      bool            Authorize(const std::string & login, uint32_t ip,
                                uint32_t enabledDirs, const AUTH * auth);
-     bool            Unauthorize(const std::string & login, const AUTH * auth);
+     bool            Unauthorize(const std::string & login,
+                                 const AUTH * auth,
+                                 const std::string & reason = std::string());
  
      int             ReadUsers();
      size_t          Count() const { return users.size(); }
@@@ -123,7 -124,6 +125,7 @@@ private
      bool            FindByIPIdx(uint32_t ip, user_iter & iter) const;
  
      int             FindByNameNonLock(const std::string & login, user_iter * user);
 +    int             FindByNameNonLock(const std::string & login, const_user_iter * user) const;
  
      void            RealDelUser();
      void            ProcessActions();