X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/913b7a4ab586482c853b679fcd8415cf88524c31..7e2a4802f98a1e22841be0ed47f3db773a5d6070:/projects/stargazer/plugins/other/radius/radius.cpp?ds=inline diff --git a/projects/stargazer/plugins/other/radius/radius.cpp b/projects/stargazer/plugins/other/radius/radius.cpp index 8c7d53d1..937d853c 100644 --- a/projects/stargazer/plugins/other/radius/radius.cpp +++ b/projects/stargazer/plugins/other/radius/radius.cpp @@ -26,19 +26,26 @@ * */ +#include +#include #include -#include +#include "stg/store.h" +#include "stg/common.h" +#include "stg/user_conf.h" +#include "stg/user_property.h" #include "radius.h" -#include "common.h" extern volatile const time_t stgTime; +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 -{ +class RAD_CREATOR { private: RADIUS * rad; @@ -46,16 +53,16 @@ public: RAD_CREATOR() : rad(new RADIUS()) { - }; + } ~RAD_CREATOR() { delete rad; - }; + } RADIUS * GetPlugin() { return rad; - }; + } }; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- @@ -64,43 +71,14 @@ RAD_CREATOR radc; //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -BASE_PLUGIN * GetPlugin() +PLUGIN * GetPlugin() { return radc.GetPlugin(); } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -uint16_t RAD_SETTINGS::GetPort() const -{ -return port; -} -//----------------------------------------------------------------------------- -int RAD_SETTINGS::GetPassword(string * password) const -{ -*password = RAD_SETTINGS::password; -return 0; -} -//----------------------------------------------------------------------------- -int RAD_SETTINGS::GetAuthServices(list * svcs) const -{ -*svcs = authServices; -return 0; -} -//----------------------------------------------------------------------------- -int RAD_SETTINGS::GetAcctServices(list * svcs) const -{ -*svcs = acctServices; -return 0; -} -//----------------------------------------------------------------------------- -int RAD_SETTINGS::ParseIP(const string & str, uint32_t * IP) -{ -*IP = inet_addr(str.c_str()); -return *IP == INADDR_NONE ? -1 : 0; -} -//----------------------------------------------------------------------------- -int RAD_SETTINGS::ParseIntInRange(const string & str, int min, int max, int * val) +int RAD_SETTINGS::ParseIntInRange(const std::string & str, int min, int max, int * val) { if (str2x(str.c_str(), *val)) { @@ -115,26 +93,26 @@ if (*val < min || *val > max) 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."; @@ -150,7 +128,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."; @@ -160,14 +138,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); @@ -186,6 +164,7 @@ RADIUS::RADIUS() store(NULL), sock(-1) { +InitEncrypt(&ctx, ""); } //----------------------------------------------------------------------------- void RADIUS::SetUsers(USERS * u) @@ -203,7 +182,7 @@ void RADIUS::SetSettings(const MODULE_SETTINGS & s) settings = s; } //----------------------------------------------------------------------------- -void RADIUS::SetStore(BASE_STORE * s) +void RADIUS::SetStore(STORE * s) { store = s; } @@ -221,7 +200,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"; } @@ -237,14 +216,6 @@ uint16_t RADIUS::GetStopPosition() const return 20; } //----------------------------------------------------------------------------- -void RADIUS::SetUserNotifier(user_iter) -{ -} -//----------------------------------------------------------------------------- -void RADIUS::UnSetUserNotifier(user_iter) -{ -} -//----------------------------------------------------------------------------- int RADIUS::PrepareNet() { sock = socket(AF_INET, SOCK_DGRAM, 0); @@ -279,11 +250,10 @@ return 0; //----------------------------------------------------------------------------- int RADIUS::Start() { -string password; +std::string password(radSettings.GetPassword()); -radSettings.GetPassword(&password); -radSettings.GetAuthServices(&authServices); -radSettings.GetAcctServices(&acctServices); +authServices = radSettings.GetAuthServices(); +acctServices = radSettings.GetAcctServices(); InitEncrypt(&ctx, password); @@ -315,10 +285,10 @@ 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); @@ -469,7 +439,7 @@ return 0; //----------------------------------------------------------------------------- int RADIUS::ProcessAuthPacket(RAD_PACKET * packet) { -user_iter ui; +USER_PTR ui; if (!CanAcctService((char *)packet->service)) { @@ -507,7 +477,7 @@ return 0; //----------------------------------------------------------------------------- int RADIUS::ProcessPostAuthPacket(RAD_PACKET * packet) { -user_iter ui; +USER_PTR ui; if (!CanAcctService((char *)packet->service)) { @@ -529,7 +499,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 @@ -544,7 +514,7 @@ return 0; //----------------------------------------------------------------------------- int RADIUS::ProcessAcctStartPacket(RAD_PACKET * packet) { -user_iter ui; +USER_PTR ui; if (!FindUser(&ui, (char *)packet->login)) { @@ -563,8 +533,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 (ui->Authorize(ips[0].ip, 0xffFFffFF, this)) { printfd(__FILE__, "RADIUS::ProcessAcctStartPacket cannot authorize user '%s'\n", packet->login); packet->packetType = RAD_REJECT_PACKET; @@ -585,7 +555,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()) { @@ -594,7 +564,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)) { @@ -625,40 +595,12 @@ packet->packetType = RAD_ACCEPT_PACKET; return 0; } //----------------------------------------------------------------------------- -void RADIUS::InitEncrypt(BLOWFISH_CTX * ctx, const string & password) +void RADIUS::PrintServices(const std::list & svcs) { -unsigned char keyL[RAD_PASSWORD_LEN]; // Пароль для шифровки -memset(keyL, 0, RAD_PASSWORD_LEN); -strncpy((char *)keyL, password.c_str(), RAD_PASSWORD_LEN); -Blowfish_Init(ctx, keyL, RAD_PASSWORD_LEN); +for_each(svcs.begin(), svcs.end(), Printer()); } //----------------------------------------------------------------------------- -void RADIUS::Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8) -{ -// len8 - длина в 8-ми байтовых блоках -if (dst != src) - memcpy(dst, src, len8 * 8); - -for (int i = 0; i < len8; i++) - Blowfish_Encrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4)); -} -//----------------------------------------------------------------------------- -void RADIUS::Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8) -{ -// len8 - длина в 8-ми байтовых блоках -if (dst != src) - memcpy(dst, src, len8 * 8); - -for (int i = 0; i < len8; i++) - Blowfish_Decrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4)); -} -//----------------------------------------------------------------------------- -void RADIUS::PrintServices(const list & svcs) -{ - 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)) { @@ -669,17 +611,17 @@ 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); +return CanAuthService(svc) || CanAcctService(svc); } //----------------------------------------------------------------------------- bool RADIUS::WaitPackets(int sd) const @@ -709,3 +651,34 @@ if (res == 0) // Timeout return true; } +//----------------------------------------------------------------------------- +inline +void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password) +{ +unsigned char keyL[RAD_PASSWORD_LEN]; // Пароль для шифровки +memset(keyL, 0, RAD_PASSWORD_LEN); +strncpy((char *)keyL, password.c_str(), RAD_PASSWORD_LEN); +Blowfish_Init(ctx, keyL, RAD_PASSWORD_LEN); +} +//----------------------------------------------------------------------------- +inline +void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8) +{ +// len8 - длина в 8-ми байтовых блоках +if (dst != src) + memcpy(dst, src, len8 * 8); + +for (int i = 0; i < len8; i++) + Blowfish_Encrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4)); +} +//----------------------------------------------------------------------------- +inline +void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8) +{ +// len8 - длина в 8-ми байтовых блоках +if (dst != src) + memcpy(dst, src, len8 * 8); + +for (int i = 0; i < len8; i++) + Blowfish_Decrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4)); +}