From: Maxim Mamontov Date: Thu, 14 Nov 2013 20:01:45 +0000 (+0200) Subject: Merge branch 'log-unauth-reasons' X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/64b687f8ec36a103865044613cb0f42f98aeda3a?ds=inline;hp=-c Merge branch 'log-unauth-reasons' Conflicts: projects/stargazer/user_impl.cpp --- 64b687f8ec36a103865044613cb0f42f98aeda3a diff --combined include/stg/users.h index a1d5d295,765fc14d..1a8d7a43 --- a/include/stg/users.h +++ b/include/stg/users.h @@@ -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 diff --combined projects/stargazer/main.cpp index 7cc8eccc,200fd2a6..1e4c677e --- a/projects/stargazer/main.cpp +++ b/projects/stargazer/main.cpp @@@ -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) diff --combined projects/stargazer/user_impl.cpp index 13cf0b77,37178dd6..7b0348bc --- a/projects/stargazer/user_impl.cpp +++ b/projects/stargazer/user_impl.cpp @@@ -32,9 -32,14 +32,9 @@@ #define _GNU_SOURCE #endif -#include -#include // access - -#include -#include -#include -#include -#include +#include "user_impl.h" +#include "settings_impl.h" +#include "stg_timer.h" #include "stg/users.h" #include "stg/common.h" @@@ -42,16 -47,9 +42,16 @@@ #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 +#include + +#include +#include +#include + +#include +#include // access #ifdef USE_ABSTRACT_SETTINGS USER_IMPL::USER_IMPL(const SETTINGS * s, @@@ -64,9 -62,12 +64,9 @@@ property(s->GetScriptsDir()), WriteServLog(GetStgLogger()), lastScanMessages(0), - login(), id(0), __connected(0), connected(__connected), - enabledDirs(), - userIDGenerator(), __currIP(0), currIP(__currIP), lastIPForDisconnect(0), @@@ -75,8 -76,11 +75,8 @@@ store(st), tariffs(t), tariff(NULL), - traffStat(), - traffStatSaved(), settings(s), - authorizedBy(), - messages(), + authorizedModificationTime(0), deleted(false), lastWriteStat(0), lastWriteDetailedStat(0), @@@ -114,10 -118,14 +114,10 @@@ 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), @@@ -158,8 -169,11 +158,8 @@@ store(st), tariffs(t), tariff(NULL), - traffStat(), - traffStatSaved(), settings(s), - authorizedBy(), - messages(), + authorizedModificationTime(0), deleted(false), lastWriteStat(0), lastWriteDetailedStat(0), @@@ -197,11 -211,15 +197,11 @@@ 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), @@@ -244,7 -263,7 +244,7 @@@ traffStat(u.traffStat), traffStatSaved(u.traffStatSaved), settings(u.settings), - authorizedBy(), + authorizedModificationTime(u.authorizedModificationTime), messages(u.messages), deleted(u.deleted), lastWriteStat(u.lastWriteStat), @@@ -289,7 -308,9 +289,7 @@@ 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(); @@@ -525,7 -544,7 +525,7 @@@ 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(), @@@ -589,13 -607,6 +590,13 @@@ id, dirsStr); + std::vector::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) @@@ -654,13 -664,6 +655,13 @@@ id, dirsStr); + std::vector::const_iterator it(settings->GetScriptParams().begin()); + while (it != settings->GetScriptParams().end()) + { + scriptOnDisonnectParams += " \"" + GetParamValue(it->c_str()) + "\""; + ++it; + } + ScriptExec(scriptOnDisonnectParams.c_str()); } else @@@ -671,7 -674,12 +672,12 @@@ 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) diff --combined projects/stargazer/user_impl.h index 26ff58fc,6f6030ca..68945d2c --- a/projects/stargazer/user_impl.h +++ b/projects/stargazer/user_impl.h @@@ -27,7 -27,15 +27,7 @@@ #ifndef USER_IMPL_H #define USER_IMPL_H -#include -#include -#include -#include -#include - #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" @@@ -35,15 -43,6 +35,15 @@@ #include "stg/auth.h" #include "stg/message.h" #include "stg/noncopyable.h" +#include "stg/os_int.h" +#include "stg/const.h" + +#include +#include +#include +#include + +#include //----------------------------------------------------------------------------- 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(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 GetAuthorizers() const; @@@ -221,8 -219,6 +222,8 @@@ void OnAdd(); void OnDelete(); + virtual std::string GetParamValue(const std::string & name) const; + private: USER_IMPL & operator=(const USER_IMPL & rvalue); @@@ -245,7 -241,6 +246,7 @@@ int id; bool __connected; USER_PROPERTY connected; + std::string lastDisconnectReason; bool enabledDirs[DIR_NUM]; @@@ -255,6 -250,7 +256,7 @@@ USER_PROPERTY currIP; uint32_t lastIPForDisconnect; // User's ip after unauth but before disconnect + std::string lastDisconnectReason; time_t pingTime; @@@ -274,7 -270,6 +276,7 @@@ #endif std::set authorizedBy; + time_t authorizedModificationTime; std::list messages; diff --combined projects/stargazer/users_impl.cpp index 5b88e685,97b4bbbc..4c4d4ce0 --- a/projects/stargazer/users_impl.cpp +++ b/projects/stargazer/users_impl.cpp @@@ -88,39 -88,23 +88,39 @@@ pthread_mutex_destroy(&mutex) //----------------------------------------------------------------------------- int USERS_IMPL::FindByNameNonLock(const std::string & login, user_iter * user) { -std::map::iterator iter; -iter = loginIndex.find(login); -if (iter != loginIndex.end()) - { - if (user) - *user = iter->second; - return 0; - } -return -1; +const std::map::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::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); diff --combined projects/stargazer/users_impl.h index b9d2b22d,70a5b9bd..43065bde --- a/projects/stargazer/users_impl.h +++ b/projects/stargazer/users_impl.h @@@ -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();