X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/641204dfbdb9fc870cdd2e7f9e3169a44693e7bf..ae8b02d528163c599a1238e1d14adc65ee847eea:/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 591f4b63..376e4278 100644 --- a/projects/stargazer/plugins/other/radius/radius.cpp +++ b/projects/stargazer/plugins/other/radius/radius.cpp @@ -18,711 +18,346 @@ * Author : Maxim Mamontov */ -/* - * This file contains a realization of radius data access plugin for Stargazer - * - * $Revision: 1.14 $ - * $Date: 2009/12/13 14:17:13 $ - * - */ +#include "radius.h" + +#include "stg/store.h" +#include "stg/users.h" +#include "stg/plugin_creator.h" +#include "stg/common.h" #include -#include +#include +#include +#include +#include -#include "radius.h" -#include "common.h" +#include +#include +#include // UNIX +#include // IP +#include // TCP +#include -extern volatile const time_t stgTime; +using STG::Config; +using STG::Conn; -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -class RAD_CREATOR +namespace { -private: - RADIUS * rad; -public: - RAD_CREATOR() - : rad(new RADIUS()) - { - }; - ~RAD_CREATOR() - { - delete rad; - }; +PLUGIN_CREATOR creator; - RADIUS * GetPlugin() - { - return rad; - }; -}; -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -RAD_CREATOR radc; -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -BASE_PLUGIN * GetPlugin() -{ -return radc.GetPlugin(); -} -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -uint16_t RAD_SETTINGS::GetPort() const -{ -return port; -} -//----------------------------------------------------------------------------- -uint32_t RAD_SETTINGS::GetServerIP() const -{ -return serverIP; -} -//----------------------------------------------------------------------------- -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) -{ -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) -{ - copy(str.begin(), str.end(), back_inserter(*lst)); - list::iterator it(find(lst->begin(), - lst->end(), - "empty")); - if (it != lst->end()) - *it = ""; - return 0; -} -//----------------------------------------------------------------------------- -int RAD_SETTINGS::ParseSettings(const MODULE_SETTINGS & s) +extern "C" PLUGIN * GetPlugin() { -int p; -PARAM_VALUE pv; -vector::const_iterator pvi; -/////////////////////////// -pv.param = "Port"; -pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); -if (pvi == s.moduleParams.end()) - { - errorStr = "Parameter \'Port\' not found."; - printfd(__FILE__, "Parameter 'Port' not found\n"); - return -1; - } -if (ParseIntInRange(pvi->value[0], 2, 65535, &p)) - { - errorStr = "Cannot parse parameter \'Port\': " + errorStr; - printfd(__FILE__, "Cannot parse parameter 'Port'\n"); - return -1; - } -port = p; -/////////////////////////// -pv.param = "ServerIP"; -pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); -if (pvi == s.moduleParams.end()) - { - serverIP = 0; - } -else - { - if (ParseIP(pvi->value[0], &serverIP)) - { - serverIP = 0; - } - } -/////////////////////////// -pv.param = "Password"; -pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); -if (pvi == s.moduleParams.end()) - { - errorStr = "Parameter \'Password\' not found."; - printfd(__FILE__, "Parameter 'Password' not found\n"); - return -1; - } -password = pvi->value[0]; -/////////////////////////// -pv.param = "AuthServices"; -pvi = 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); -if (pvi != s.moduleParams.end()) - { - ParseServices(pvi->value, &acctServices); - } - -return 0; + return creator.GetPlugin(); } -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- -//----------------------------------------------------------------------------- + RADIUS::RADIUS() + : m_config(), + m_running(false), + m_stopped(true), + m_users(NULL), + m_store(NULL), + m_listenSocket(0), + m_logger(GetPluginLogger(GetStgLogger(), "radius")) { -isRunning = false; -} -//----------------------------------------------------------------------------- -void RADIUS::SetUsers(USERS * u) -{ -users = u; -} -//----------------------------------------------------------------------------- -void RADIUS::SetStgSettings(const SETTINGS * s) -{ -stgSettings = s; -} -//----------------------------------------------------------------------------- -void RADIUS::SetSettings(const MODULE_SETTINGS & s) -{ -settings = s; -} -//----------------------------------------------------------------------------- -void RADIUS::SetStore(BASE_STORE * s) -{ -store = s; } -//----------------------------------------------------------------------------- + int RADIUS::ParseSettings() { -int ret = radSettings.ParseSettings(settings); -if (ret) - errorStr = radSettings.GetStrError(); -return ret; -} -//----------------------------------------------------------------------------- -bool RADIUS::IsRunning() -{ -return isRunning; -} -//----------------------------------------------------------------------------- -const string RADIUS::GetVersion() const -{ -return "RADIUS data access plugin v 0.6"; -} -//----------------------------------------------------------------------------- -uint16_t RADIUS::GetStartPosition() const -{ -// Start before any authorizers!!! -return 20; -} -//----------------------------------------------------------------------------- -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); - -if (sock < 0) - { - errorStr = "Cannot create socket."; - printfd(__FILE__, "Cannot create socket\n"); - return -1; - } - -inAddr.sin_family = AF_INET; -inAddr.sin_port = htons(port); -inAddr.sin_addr.s_addr = inet_addr("0.0.0.0"); - -if (bind(sock, (struct sockaddr*)&inAddr, sizeof(inAddr)) < 0) - { - errorStr = "RADIUS: Bind failed."; - printfd(__FILE__, "Cannot bind socket\n"); - return -1; + try { + m_config = STG::Config(m_settings); + return reconnect() ? 0 : -1; + } catch (const std::runtime_error& ex) { + m_logger("Failed to parse settings. %s", ex.what()); + return -1; } - -return 0; -} -//----------------------------------------------------------------------------- -int RADIUS::FinalizeNet() -{ -close(sock); -return 0; } -//----------------------------------------------------------------------------- + int RADIUS::Start() { -string password; - -radSettings.GetPassword(&password); -port = radSettings.GetPort(); -serverIP = radSettings.GetServerIP(); -radSettings.GetAuthServices(&authServices); -radSettings.GetAcctServices(&acctServices); + if (m_running) + return 0; -InitEncrypt(&ctx, password); + int res = pthread_create(&m_thread, NULL, run, this); + if (res == 0) + return 0; -nonstop = true; - -if (PrepareNet()) - { + m_error = strerror(res); + m_logger("Failed to create thread: '" + m_error + "'."); return -1; - } - -if (!isRunning) - { - if (pthread_create(&thread, NULL, Run, this)) - { - errorStr = "Cannot create thread."; - printfd(__FILE__, "Cannot create thread\n"); - return -1; - } - } - -errorStr = ""; -return 0; } -//----------------------------------------------------------------------------- -int RADIUS::Stop() -{ -if (!IsRunning()) - return 0; -nonstop = false; - -map::iterator it; -for (it = sessions.begin(); it != sessions.end(); ++it) - { - user_iter ui; - if (users->FindByName(it->second.userName, &ui)) - { - ui->Unauthorize(this); - } - } -sessions.erase(sessions.begin(), sessions.end()); - -FinalizeNet(); - -if (isRunning) - { - //5 seconds to thread stops itself - for (int i = 0; i < 25 && isRunning; i++) - { - usleep(200000); - } - - //after 5 seconds waiting thread still running. now killing it - if (isRunning) - { - if (pthread_kill(thread, SIGINT)) - { - errorStr = "Cannot kill thread."; - printfd(__FILE__, "Cannot kill thread\n"); - return -1; - } - printfd(__FILE__, "RADIUS::Stop killed Run\n"); - } - } - -return 0; -} -//----------------------------------------------------------------------------- -void * RADIUS::Run(void * d) +int RADIUS::Stop() { -RADIUS * rad = (RADIUS *)d; -RAD_PACKET packet; + if (m_stopped) + return 0; -rad->isRunning = true; + m_running = false; -while (rad->nonstop) - { - if (!rad->WaitPackets(rad->sock)) - { - continue; - } - if (rad->RecvData(&packet)) - { - printfd(__FILE__, "RADIUS::Run Error on RecvData\n"); - } - else - { - if (rad->ProcessData(&packet)) - { - packet.packetType = RAD_REJECT_PACKET; - } - rad->Send(packet); - } + for (size_t i = 0; i < 25 && !m_stopped; i++) { + struct timespec ts = {0, 200000000}; + nanosleep(&ts, NULL); } -rad->isRunning = false; - -return NULL; -} -//----------------------------------------------------------------------------- -int RADIUS::RecvData(RAD_PACKET * packet) -{ - int8_t buf[RAD_MAX_PACKET_LEN]; - outerAddrLen = sizeof(struct sockaddr_in); - int dataLen = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, (struct sockaddr *)&outerAddr, &outerAddrLen); - if (dataLen > 0) { - Decrypt(&ctx, (char *)packet, (const char *)buf, dataLen / 8); + if (m_stopped) { + pthread_join(m_thread, NULL); + return 0; } - if (strncmp((char *)packet->magic, RAD_ID, RAD_MAGIC_LEN)) - { - printfd(__FILE__, "RADIUS::RecvData Error magic. Wanted: '%s', got: '%s'\n", RAD_ID, packet->magic); - return -1; - } - return 0; -} -//----------------------------------------------------------------------------- -int RADIUS::Send(const RAD_PACKET & packet) -{ -int res, len = sizeof(RAD_PACKET); -char buf[1032]; -Encrypt(&ctx, buf, (char *)&packet, len / 8); -res = sendto(sock, buf, len, 0, (struct sockaddr *)&outerAddr, outerAddrLen); + if (m_config.connectionType == Config::UNIX) + unlink(m_config.bindAddress.c_str()); -return 0; -} -//----------------------------------------------------------------------------- -int RADIUS::ProcessData(RAD_PACKET * packet) -{ -//struct in_addr addr = {packet->ip}; -if (strncmp((const char *)packet->protoVer, "01", 2)) - { - printfd(__FILE__, "RADIUS::ProcessData packet.protoVer incorrect\n"); + m_error = "Failed to stop thread."; + m_logger(m_error); return -1; - } -switch (packet->packetType) - { - case RAD_AUTZ_PACKET: - return ProcessAutzPacket(packet); - case RAD_AUTH_PACKET: - return ProcessAuthPacket(packet); - case RAD_POST_AUTH_PACKET: - return ProcessPostAuthPacket(packet); - case RAD_ACCT_START_PACKET: - return ProcessAcctStartPacket(packet); - case RAD_ACCT_STOP_PACKET: - return ProcessAcctStopPacket(packet); - case RAD_ACCT_UPDATE_PACKET: - return ProcessAcctUpdatePacket(packet); - case RAD_ACCT_OTHER_PACKET: - return ProcessAcctOtherPacket(packet); - default: - printfd(__FILE__, "RADIUS::ProcessData Unsupported packet type: %d\n", packet->packetType); - return -1; - }; -return 0; } //----------------------------------------------------------------------------- -int RADIUS::ProcessAutzPacket(RAD_PACKET * packet) +void* RADIUS::run(void* d) { -USER_CONF conf; - -if (!IsAllowedService((char *)packet->service)) - { - printfd(__FILE__, "RADIUS::ProcessAutzPacket service '%s' is not allowed to authorize\n", packet->service); - packet->packetType = RAD_REJECT_PACKET; - return 0; - } + sigset_t signalSet; + sigfillset(&signalSet); + pthread_sigmask(SIG_BLOCK, &signalSet, NULL); -if (store->RestoreUserConf(&conf, (char *)packet->login)) - { - packet->packetType = RAD_REJECT_PACKET; - printfd(__FILE__, "RADIUS::ProcessAutzPacket cannot restore conf for user '%s'\n", packet->login); - return 0; - } - -// At this point service can be authorized at least -// So we send a plain-text password + static_cast(d)->runImpl(); -packet->packetType = RAD_ACCEPT_PACKET; -strncpy((char *)packet->password, conf.password.c_str(), RAD_PASSWORD_LEN); - -return 0; + return NULL; } -//----------------------------------------------------------------------------- -int RADIUS::ProcessAuthPacket(RAD_PACKET * packet) -{ -user_iter ui; -if (!CanAcctService((char *)packet->service)) +bool RADIUS::reconnect() +{ + if (!m_conns.empty()) { - - // There are no sense to check for allowed service - // It has allready checked at previous stage (authorization) - - printfd(__FILE__, "RADIUS::ProcessAuthPacket service '%s' neednot stargazer authentication\n", (char *)packet->service); - packet->packetType = RAD_ACCEPT_PACKET; - return 0; + std::deque::const_iterator it; + for (it = m_conns.begin(); it != m_conns.end(); ++it) + delete(*it); + m_conns.clear(); } - -// At this point we have an accountable service -// All other services got a password if allowed or rejected - -if (!FindUser(&ui, (char *)packet->login)) + if (m_listenSocket != 0) { - packet->packetType = RAD_REJECT_PACKET; - printfd(__FILE__, "RADIUS::ProcessAuthPacket user '%s' not found\n", (char *)packet->login); - return 0; + shutdown(m_listenSocket, SHUT_RDWR); + close(m_listenSocket); } - -if (ui->IsInetable()) - { - packet->packetType = RAD_ACCEPT_PACKET; - } -else + if (m_config.connectionType == Config::UNIX) + m_listenSocket = createUNIX(); + else + m_listenSocket = createTCP(); + if (m_listenSocket == 0) + return false; + if (listen(m_listenSocket, 100) == -1) { - packet->packetType = RAD_REJECT_PACKET; + m_error = std::string("Error starting to listen socket: ") + strerror(errno); + m_logger(m_error); + return false; } - -packet->packetType = RAD_ACCEPT_PACKET; -return 0; + return true; } -//----------------------------------------------------------------------------- -int RADIUS::ProcessPostAuthPacket(RAD_PACKET * packet) -{ -user_iter ui; -if (!CanAcctService((char *)packet->service)) +int RADIUS::createUNIX() const +{ + int fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd == -1) { - - // There are no sense to check for allowed service - // It has allready checked at previous stage (authorization) - - packet->packetType = RAD_ACCEPT_PACKET; - return 0; + m_error = std::string("Error creating UNIX socket: ") + strerror(errno); + m_logger(m_error); + return 0; } - -if (!FindUser(&ui, (char *)packet->login)) + struct sockaddr_un addr; + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, m_config.bindAddress.c_str(), m_config.bindAddress.length()); + unlink(m_config.bindAddress.c_str()); + if (bind(fd, reinterpret_cast(&addr), sizeof(addr)) == -1) { - packet->packetType = RAD_REJECT_PACKET; - printfd(__FILE__, "RADIUS::ProcessPostAuthPacket user '%s' not found\n", (char *)packet->login); - return 0; + shutdown(fd, SHUT_RDWR); + close(fd); + m_error = std::string("Error binding UNIX socket: ") + strerror(errno); + m_logger(m_error); + return 0; } - -// 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; -packet->packetType = RAD_ACCEPT_PACKET; - -// Additional checking for Framed-User service - -if (!strncmp((char *)packet->service, "Framed-User", RAD_SERVICE_LEN)) - packet->ip = ips[0].ip; -else - packet->ip = 0; - -return 0; + chown(m_config.bindAddress.c_str(), m_config.sockUID, m_config.sockGID); + if (m_config.sockMode != static_cast(-1)) + chmod(m_config.bindAddress.c_str(), m_config.sockMode); + return fd; } -//----------------------------------------------------------------------------- -int RADIUS::ProcessAcctStartPacket(RAD_PACKET * packet) + +int RADIUS::createTCP() const { -user_iter ui; + addrinfo hints; + memset(&hints, 0, sizeof(addrinfo)); + + hints.ai_family = AF_INET; /* Allow IPv4 */ + hints.ai_socktype = SOCK_STREAM; /* Stream socket */ + hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */ + hints.ai_protocol = 0; /* Any protocol */ + hints.ai_canonname = NULL; + hints.ai_addr = NULL; + hints.ai_next = NULL; -if (!FindUser(&ui, (char *)packet->login)) + addrinfo* ais = NULL; + int res = getaddrinfo(m_config.bindAddress.c_str(), m_config.portStr.c_str(), &hints, &ais); + if (res != 0) { - packet->packetType = RAD_REJECT_PACKET; - printfd(__FILE__, "RADIUS::ProcessAcctStartPacket user '%s' not found\n", (char *)packet->login); - return 0; + m_error = "Error resolving address '" + m_config.bindAddress + "': " + gai_strerror(res); + m_logger(m_error); + return 0; } -// At this point we have to unauthorize user only if it is an accountable service - -if (CanAcctService((char *)packet->service)) + for (addrinfo* ai = ais; ai != NULL; ai = ai->ai_next) { - if (sessions.find((const char *)packet->sessid) != sessions.end()) + int fd = socket(AF_INET, SOCK_STREAM, 0); + if (fd == -1) { - printfd(__FILE__, "RADIUS::ProcessAcctStartPacket session already started!\n"); - packet->packetType = RAD_REJECT_PACKET; - return -1; + m_error = std::string("Error creating TCP socket: ") + strerror(errno); + m_logger(m_error); + freeaddrinfo(ais); + return 0; } - USER_IPS ips = ui->property.ips; - if (ui->Authorize(ips[0].ip, "", 0xffFFffFF, this)) + if (bind(fd, ai->ai_addr, ai->ai_addrlen) == -1) { - printfd(__FILE__, "RADIUS::ProcessAcctStartPacket cannot authorize user '%s'\n", packet->login); - packet->packetType = RAD_REJECT_PACKET; - return -1; + shutdown(fd, SHUT_RDWR); + close(fd); + m_error = std::string("Error binding TCP socket: ") + strerror(errno); + m_logger(m_error); + continue; } - sessions[(const char *)packet->sessid].userName = (const char *)packet->login; - sessions[(const char *)packet->sessid].serviceType = (const char *)packet->service; - for_each(sessions.begin(), sessions.end(), SPrinter()); - } -else - { - printfd(__FILE__, "RADIUS::ProcessAcctStartPacket service '%s' can not be accounted\n", (char *)packet->service); + freeaddrinfo(ais); + return fd; } -packet->packetType = RAD_ACCEPT_PACKET; -return 0; + m_error = "Failed to resolve '" + m_config.bindAddress; + m_logger(m_error); + + freeaddrinfo(ais); + return 0; } -//----------------------------------------------------------------------------- -int RADIUS::ProcessAcctStopPacket(RAD_PACKET * packet) + +void RADIUS::runImpl() { -map::iterator sid; + m_running = true; + m_stopped = false; -if ((sid = sessions.find((const char *)packet->sessid)) == sessions.end()) - { - printfd(__FILE__, "RADIUS::ProcessAcctStopPacket session had not started yet\n"); - packet->packetType = RAD_REJECT_PACKET; - return -1; - } + while (m_running) { + fd_set fds; -user_iter ui; + buildFDSet(fds); -if (!FindUser(&ui, sid->second.userName)) - { - packet->packetType = RAD_REJECT_PACKET; - printfd(__FILE__, "RADIUS::ProcessPostAuthPacket user '%s' not found\n", sid->second.userName.c_str()); - return 0; - } + struct timeval tv; + tv.tv_sec = 0; + tv.tv_usec = 500000; -sessions.erase(sid); + int res = select(maxFD() + 1, &fds, NULL, NULL, &tv); + if (res < 0) + { + if (errno == EINTR) + continue; + m_error = std::string("'select' is failed: '") + strerror(errno) + "'."; + m_logger(m_error); + break; + } -ui->Unauthorize(this); + if (!m_running) + break; -packet->packetType = RAD_ACCEPT_PACKET; -return 0; -} -//----------------------------------------------------------------------------- -int RADIUS::ProcessAcctUpdatePacket(RAD_PACKET * packet) -{ -// Fake. May be used later -packet->packetType = RAD_ACCEPT_PACKET; -return 0; + if (res > 0) + handleEvents(fds); + else + { + for (std::deque::iterator it = m_conns.begin(); it != m_conns.end(); ++it) + (*it)->tick(); + } + + cleanupConns(); + } + + m_stopped = true; } -//----------------------------------------------------------------------------- -int RADIUS::ProcessAcctOtherPacket(RAD_PACKET * packet) + +int RADIUS::maxFD() const { -// Fake. May be used later -packet->packetType = RAD_ACCEPT_PACKET; -return 0; + int maxFD = m_listenSocket; + std::deque::const_iterator it; + for (it = m_conns.begin(); it != m_conns.end(); ++it) + if (maxFD < (*it)->sock()) + maxFD = (*it)->sock(); + return maxFD; } -//----------------------------------------------------------------------------- -void RADIUS::InitEncrypt(BLOWFISH_CTX * ctx, const string & password) + +void RADIUS::buildFDSet(fd_set & fds) const { -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); + FD_ZERO(&fds); + FD_SET(m_listenSocket, &fds); + std::deque::const_iterator it; + for (it = m_conns.begin(); it != m_conns.end(); ++it) + FD_SET((*it)->sock(), &fds); } -//----------------------------------------------------------------------------- -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) +void RADIUS::cleanupConns() { -// len8 - длина в 8-ми байтовых блоках -if (dst != src) - memcpy(dst, src, len8 * 8); + std::deque::iterator pos; + for (pos = m_conns.begin(); pos != m_conns.end(); ++pos) + if (!(*pos)->isOk()) { + delete *pos; + *pos = NULL; + } -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()); + pos = std::remove(m_conns.begin(), m_conns.end(), static_cast(NULL)); + m_conns.erase(pos, m_conns.end()); } -//----------------------------------------------------------------------------- -bool RADIUS::FindUser(user_iter * ui, const std::string & login) const + +void RADIUS::handleEvents(const fd_set & fds) { -if (users->FindByName(login, ui)) + if (FD_ISSET(m_listenSocket, &fds)) + acceptConnection(); + else { - return false; + std::deque::iterator it; + for (it = m_conns.begin(); it != m_conns.end(); ++it) + if (FD_ISSET((*it)->sock(), &fds)) + (*it)->read(); + else + (*it)->tick(); } -return true; } -//----------------------------------------------------------------------------- -bool RADIUS::CanAuthService(const std::string & svc) const -{ - 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(); -} -//----------------------------------------------------------------------------- -bool RADIUS::IsAllowedService(const std::string & svc) const + +void RADIUS::acceptConnection() { - return CanAuthService(svc) || CanAcctService(svc); + if (m_config.connectionType == Config::UNIX) + acceptUNIX(); + else + acceptTCP(); } -//----------------------------------------------------------------------------- -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 +void RADIUS::acceptUNIX() +{ + struct sockaddr_un addr; + memset(&addr, 0, sizeof(addr)); + socklen_t size = sizeof(addr); + int res = accept(m_listenSocket, reinterpret_cast(&addr), &size); + if (res == -1) { - if (errno != EINTR) - { - printfd(__FILE__, "Error on select: '%s'\n", strerror(errno)); - } - return false; + m_error = std::string("Failed to accept UNIX connection: ") + strerror(errno); + m_logger(m_error); + return; } + printfd(__FILE__, "New UNIX connection: '%s'\n", addr.sun_path); + m_conns.push_back(new Conn(*m_users, m_logger, m_config, res, addr.sun_path)); +} -if (res == 0) // Timeout +void RADIUS::acceptTCP() +{ + struct sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + socklen_t size = sizeof(addr); + int res = accept(m_listenSocket, reinterpret_cast(&addr), &size); + if (res == -1) { - return false; + m_error = std::string("Failed to accept TCP connection: ") + strerror(errno); + m_logger(m_error); + return; } - -return true; + std::string remote = inet_ntostring(addr.sin_addr.s_addr) + ":" + x2str(ntohs(addr.sin_port)); + printfd(__FILE__, "New TCP connection: '%s'\n", remote.c_str()); + m_conns.push_back(new Conn(*m_users, m_logger, m_config, res, remote)); }