X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/e59123a3a13f9ffb3af28e85295b6a14c5c8aaca..a5709854a8f0dc833598a2e1aefbd3000aeefb41:/projects/stargazer/plugins/other/radius/radius.cpp diff --git a/projects/stargazer/plugins/other/radius/radius.cpp b/projects/stargazer/plugins/other/radius/radius.cpp index c0b70e72..efa99570 100644 --- a/projects/stargazer/plugins/other/radius/radius.cpp +++ b/projects/stargazer/plugins/other/radius/radius.cpp @@ -27,89 +27,56 @@ */ #include +#include #include +#include "stg/store.h" +#include "stg/common.h" +#include "stg/user_conf.h" +#include "stg/user_property.h" +#include "stg/plugin_creator.h" #include "radius.h" -#include "common.h" extern volatile const time_t stgTime; -void InitEncrypt(BLOWFISH_CTX * ctx, const string & password); +void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password); void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8); void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8); //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -class RAD_CREATOR -{ -private: - RADIUS * rad; - -public: - RAD_CREATOR() - : rad(new RADIUS()) - { - }; - ~RAD_CREATOR() - { - delete rad; - }; - - RADIUS * GetPlugin() - { - return rad; - }; -}; -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- +PLUGIN_CREATOR radc; //----------------------------------------------------------------------------- -RAD_CREATOR radc; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -BASE_PLUGIN * GetPlugin() +PLUGIN * GetPlugin() { return radc.GetPlugin(); } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -int RAD_SETTINGS::ParseIntInRange(const string & str, int min, int max, int * val) -{ -if (str2x(str.c_str(), *val)) - { - errorStr = "Incorrect value \'" + str + "\'."; - return -1; - } -if (*val < min || *val > max) - { - errorStr = "Value \'" + str + "\' out of range."; - return -1; - } -return 0; -} -//----------------------------------------------------------------------------- -int RAD_SETTINGS::ParseServices(const vector & str, list * lst) +int RAD_SETTINGS::ParseServices(const std::vector & str, std::list * lst) { - copy(str.begin(), str.end(), back_inserter(*lst)); - list::iterator it(find(lst->begin(), - lst->end(), - "empty")); - if (it != lst->end()) - *it = ""; +std::copy(str.begin(), str.end(), std::back_inserter(*lst)); +std::list::iterator it(std::find(lst->begin(), + lst->end(), + "empty")); +if (it != lst->end()) + *it = ""; - return 0; +return 0; } //----------------------------------------------------------------------------- int RAD_SETTINGS::ParseSettings(const MODULE_SETTINGS & s) { int p; PARAM_VALUE pv; -vector::const_iterator pvi; +std::vector::const_iterator pvi; /////////////////////////// pv.param = "Port"; -pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); +pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv); if (pvi == s.moduleParams.end()) { errorStr = "Parameter \'Port\' not found."; @@ -125,7 +92,7 @@ if (ParseIntInRange(pvi->value[0], 2, 65535, &p)) port = p; /////////////////////////// pv.param = "Password"; -pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); +pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv); if (pvi == s.moduleParams.end()) { errorStr = "Parameter \'Password\' not found."; @@ -135,14 +102,14 @@ if (pvi == s.moduleParams.end()) password = pvi->value[0]; /////////////////////////// pv.param = "AuthServices"; -pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); +pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv); if (pvi != s.moduleParams.end()) { ParseServices(pvi->value, &authServices); } /////////////////////////// pv.param = "AcctServices"; -pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); +pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv); if (pvi != s.moduleParams.end()) { ParseServices(pvi->value, &acctServices); @@ -161,6 +128,7 @@ RADIUS::RADIUS() store(NULL), sock(-1) { +InitEncrypt(&ctx, ""); } //----------------------------------------------------------------------------- void RADIUS::SetUsers(USERS * u) @@ -178,7 +146,7 @@ void RADIUS::SetSettings(const MODULE_SETTINGS & s) settings = s; } //----------------------------------------------------------------------------- -void RADIUS::SetStore(BASE_STORE * s) +void RADIUS::SetStore(STORE * s) { store = s; } @@ -196,7 +164,7 @@ bool RADIUS::IsRunning() return isRunning; } //----------------------------------------------------------------------------- -const string RADIUS::GetVersion() const +const std::string RADIUS::GetVersion() const { return "RADIUS data access plugin v 0.6"; } @@ -246,7 +214,7 @@ return 0; //----------------------------------------------------------------------------- int RADIUS::Start() { -string password(radSettings.GetPassword()); +std::string password(radSettings.GetPassword()); authServices = radSettings.GetAuthServices(); acctServices = radSettings.GetAcctServices(); @@ -281,13 +249,13 @@ if (!IsRunning()) nonstop = false; -map::iterator it; +std::map::iterator it; for (it = sessions.begin(); it != sessions.end(); ++it) { - user_iter ui; + USER_PTR ui; if (users->FindByName(it->second.userName, &ui)) { - ui->Unauthorize(this); + users->Unauthorize(ui->GetLogin(), this); } } sessions.erase(sessions.begin(), sessions.end()); @@ -327,7 +295,7 @@ rad->isRunning = true; while (rad->nonstop) { - if (!rad->WaitPackets(rad->sock)) + if (!WaitPackets(rad->sock)) { continue; } @@ -435,7 +403,7 @@ return 0; //----------------------------------------------------------------------------- int RADIUS::ProcessAuthPacket(RAD_PACKET * packet) { -user_iter ui; +USER_PTR ui; if (!CanAcctService((char *)packet->service)) { @@ -473,7 +441,7 @@ return 0; //----------------------------------------------------------------------------- int RADIUS::ProcessPostAuthPacket(RAD_PACKET * packet) { -user_iter ui; +USER_PTR ui; if (!CanAcctService((char *)packet->service)) { @@ -495,7 +463,7 @@ if (!FindUser(&ui, (char *)packet->login)) // I think that only Framed-User services has sense to be accountable // So we have to supply a Framed-IP -USER_IPS ips = ui->property.ips; +USER_IPS ips = ui->GetProperty().ips; packet->packetType = RAD_ACCEPT_PACKET; // Additional checking for Framed-User service @@ -510,7 +478,7 @@ return 0; //----------------------------------------------------------------------------- int RADIUS::ProcessAcctStartPacket(RAD_PACKET * packet) { -user_iter ui; +USER_PTR ui; if (!FindUser(&ui, (char *)packet->login)) { @@ -529,8 +497,8 @@ if (CanAcctService((char *)packet->service)) packet->packetType = RAD_REJECT_PACKET; return -1; } - USER_IPS ips = ui->property.ips; - if (ui->Authorize(ips[0].ip, "", 0xffFFffFF, this)) + USER_IPS ips = ui->GetProperty().ips; + if (users->Authorize(ui->GetLogin(), ips[0].ip, 0xffFFffFF, this)) { printfd(__FILE__, "RADIUS::ProcessAcctStartPacket cannot authorize user '%s'\n", packet->login); packet->packetType = RAD_REJECT_PACKET; @@ -551,7 +519,7 @@ return 0; //----------------------------------------------------------------------------- int RADIUS::ProcessAcctStopPacket(RAD_PACKET * packet) { -map::iterator sid; +std::map::iterator sid; if ((sid = sessions.find((const char *)packet->sessid)) == sessions.end()) { @@ -560,7 +528,7 @@ if ((sid = sessions.find((const char *)packet->sessid)) == sessions.end()) return -1; } -user_iter ui; +USER_PTR ui; if (!FindUser(&ui, sid->second.userName)) { @@ -571,7 +539,7 @@ if (!FindUser(&ui, sid->second.userName)) sessions.erase(sid); -ui->Unauthorize(this); +users->Unauthorize(ui->GetLogin(), this); packet->packetType = RAD_ACCEPT_PACKET; return 0; @@ -591,12 +559,12 @@ packet->packetType = RAD_ACCEPT_PACKET; return 0; } //----------------------------------------------------------------------------- -void RADIUS::PrintServices(const list & svcs) +void RADIUS::PrintServices(const std::list & svcs) { - for_each(svcs.begin(), svcs.end(), Printer()); +for_each(svcs.begin(), svcs.end(), Printer()); } //----------------------------------------------------------------------------- -bool RADIUS::FindUser(user_iter * ui, const std::string & login) const +bool RADIUS::FindUser(USER_PTR * ui, const std::string & login) const { if (users->FindByName(login, ui)) { @@ -607,49 +575,21 @@ return true; //----------------------------------------------------------------------------- bool RADIUS::CanAuthService(const std::string & svc) const { - return find(authServices.begin(), authServices.end(), svc) != authServices.end(); +return find(authServices.begin(), authServices.end(), svc) != authServices.end(); } //----------------------------------------------------------------------------- bool RADIUS::CanAcctService(const std::string & svc) const { - return find(acctServices.begin(), acctServices.end(), svc) != acctServices.end(); +return find(acctServices.begin(), acctServices.end(), svc) != acctServices.end(); } //----------------------------------------------------------------------------- bool RADIUS::IsAllowedService(const std::string & svc) const { - return CanAuthService(svc) || CanAcctService(svc); -} -//----------------------------------------------------------------------------- -bool RADIUS::WaitPackets(int sd) const -{ -fd_set rfds; -FD_ZERO(&rfds); -FD_SET(sd, &rfds); - -struct timeval tv; -tv.tv_sec = 0; -tv.tv_usec = 500000; - -int res = select(sd + 1, &rfds, NULL, NULL, &tv); -if (res == -1) // Error - { - if (errno != EINTR) - { - printfd(__FILE__, "Error on select: '%s'\n", strerror(errno)); - } - return false; - } - -if (res == 0) // Timeout - { - return false; - } - -return true; +return CanAuthService(svc) || CanAcctService(svc); } //----------------------------------------------------------------------------- inline -void InitEncrypt(BLOWFISH_CTX * ctx, const string & password) +void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password) { unsigned char keyL[RAD_PASSWORD_LEN]; // Пароль для шифровки memset(keyL, 0, RAD_PASSWORD_LEN);