From: HelenMamontova <44774239+HelenMamontova@users.noreply.github.com> Date: Wed, 23 Apr 2025 12:47:51 +0000 (+0300) Subject: Radius. (#11) X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/38cfd057eaa3aa82b37659b5b587ef92cac3bfea Radius. (#11) * Radius. Header file , added. Functions start, stop declaration changed. Class members m_thread, m_mutex added. Function Run declaration added. * Radius. Header files added. Functions Start, Stop, Run definition added. * Radius. New files server.h, server.cpp added. * Radius. Definition of functions startReceive, makeResponse, handleSend, handleReceive added. * The Run function name fixed. Definition of secret variable sdded. * The file server.cpp added to add_library command, command find_package(OpenSSL 1.0.0 Required) added, OpenSSL::Crypto added to target_link_libraries command in the block if(BUILD_MOD_RADIUS). * Parameter token added to function Run() declaration. * Radius. Method Start: variable isRunning=true removed, m_thread definition changed. Method Stop: variable isRunning=false removed, m_thread.joinable check added, isRunning check added, request_stop call added. Method Run: parameter token added, variables secret, port removed, object lock and isRunning=true added before cycle while, cycle while added, isRunning=false added after cycle while. * Radius. Hold the mutex removed,extra conditions for m_thread.join() removed in the Stop function. * Radius. Cycle while removed in function Run. * Radius. The variables isRunning, errorStr replaced by m_running, m_errorStr in class Radius. * Radius. The variable isRunning replaced by m_running in function Run. * Radius. Object name io_service changed to ioService in the function Run. * Method SetRunning declaration added to the class RADIUS. * Method SetRunning definition added. Method SetRunning call added to function Run. * Radius. Namespace STG added. * Radius. Declaration using STG::Server added. * Radius. Header file "stg/logger.h" added. RADIUS class member m_loger added. * Radius. Header file "stg/common.h" added. Initialization of m_logger added to constructor RADIUS. Output cerr replaced by logger and printfd(). * Radius. Variable except added, messages fixed in the function Run. * Radius. Thread join logic fixed in the function Stop. * Keyword const added to std::lock_guard in the function SetRunning. * Radius. Class member m_mutex moved to the top of list. * Radius. Class member m_logger put after the m_running. * Radius. Unnecessary variable except removed in the function Run. * Radius. Extra whitespace removed in the function Stop. Function IsRunning definition added. * Radius. Function IsRunning declaration changed. * Radius. Formatting fixed. * Radius. Extra symbols '//' removed. * Radius. Class member m_running initialization added to constructor RADIUS. * Radius.Header file "stg/module_settings.h" added. Class members variable m_settings and method SetSettings added to class RADIUS. * Radius. The parameters Secret, Dictionaries added, parameter Port changed, other parameters removed. * Radius. Header files added. Class RAD_SETTINGS added. Methods SetSettings, copy constructor RADIUS, assignment operator added. Method ParseSettings changed. Formatting fixed. * Radius. Parameter default changed to optional and parameters value commented out for the parameters dictionaries, port. * Radius. Description of parameters secret, port, dictionaries changed. * Radius. Copy constructor RADIUS and assignment operator declaration changed and moved to public section. * Radius. Class member m_mutex moved to the top of section private. * Radius. The m_port variable value initialization changed to 1812, m_dictionaries variable initialization added to constructor RAD_SETTINGS. * Radius. Check for missing parameters port and dictionaries removed in function ParseSettings. * Radius. The dist directory added. * Radius. The methods declaration stop and start added. * Radius. The methods stop, start definition added. Method start call added to Server constructor. * Radius. The header files "server.h", , added. The class members m_ioService, m_server added. * Radius. The header files "server", , removed. The method m_server->stop() call added to method Stop. * Radius. Header files and added. Parameter token added to constructor Server. Class member m_token added. * Radius. Parameter token and m_token class member initialization added to Server constructor definition. The check m_token.stop_requested() added to method handleSend before startReceive method call. * Radius. The m_server class member initialization removed. * Radius. The m_server->stop() call adnd m_thread.request_stop() call reodered, the m_server check added before m_server->stop call in the method Stop. Parameter token added to constructor Server in the function Run. * Radius. Header file "stg/logger.h" added. Class member m_logger added to class Server. * Radius. Header file "stg/common.h" added, header file removed. Class member m_logger initialization added to constructor Server. Object std::cout replaced by function printfd and m_logger. * Radius. Class member m_logger initialization changed in the constructor Server. Check token moved before checking the error code in the handleSend method. * Radius. Check token added to handleReceive method. * Radius. Header file replaced by , header file removed. * Radius. Parameter m_logger added to constructor Server call in the method Run. * Radius. Parameter logger added to constructor Server declaration. Class member m_logger added to class Server. * Radius. Parameter logger added to constructor Server definition. Class member m_logger initialization changed in the constructor Server. * Radius. Extra whitespace removed ib the method Run. * Radius. The order of operations changed in the method handleSend. * Radius. The order of operations changed in the method handleReceive. * Radius. Operator return removed when checking ec, operator else removed when checking packet in the handleReceive method. * Radius. Parameter token replaced by std::move(token) in the constructor Server in method Run. * Radius. Class member m_token initialization value changed to std::move(token) in the constructor Server. * Radius. The c_str() function added to message when checking error in the handleSend and handleReceive methods. * Radius. Header file added. * Radius. Header files "stg/users.h", "stg/user.h", "stg/user_property.h" added. The using statements added. The findUser, SetUsers functions declaration added. Class member m_users added. * Radius. Header files "radproto/attribute_types.h", added. The m_users class member initialization added to constructor Server. The findUser function definition added. The findUser function call added to makeResponse function. * Radius. Getting the values of login and password fixed in the function findUser. Extra functions printfd call added. * Radius. Using statements added. Method SetUsers added, variable class member m_users added to class RADIUS. * Radius. The class member variable m_users initialization added to constructor Radius. Parameter m_users added to constructor Server call in the method Run. * Radius. Parameter users added to constructir Server declaration. Function SetUsers removed fron class Server. * Radius. Parameter users added to constructor Server definition. Variable class member m_users initialization added to constructor Server. * Radius. Function c_str added to login and password in m_logger and printfd function in the function findUser. * Radius. Extra functions printfd removed. * Radius. The using declaration for UserPtr, ConstUserPtr removed. * Radius. Looking for user moved after check request type in the makeResponse function. * Radius. The using declaration for UserPtr, ConstUserPtr removed. Header files "stg/user.h", "stg/users.h", "stg/user.property.h" removed. * Radius. Header files "stg/user.h", "stg/users.h" added. * Radius. Function findUser moved to private. * Radius. Return type of function findUser changed to bool. * Radius. Return type of function findUser definition and call changed to bool. * Radius. Variables attributesEmpty, vendorSpecificEmpty added for packet ACCESS_REJECT in the function makeResponse. * Radius. Type STG::Users* replaced by Users* in parameter users in the constructor Server definition. * Radius. Condition and code order changed when packet is returned in makeResponse function. * Radius. Unnecessary variables attributeEmpty, vendorSpecificEmpty replaced by {} when ACCESS_REJECT packet is returned in makeResponse function. * Radius. Prefix STG of user object removed, user is initialized by nullptr and moved to the point before call FindByName function in the findUser function. * Radius. Extra printfd function call removed in the function findUser. * Radius. The way of initializing the object user changed in the findUser function. Error log removed in the makeResponse function. * Radius. Prefix STG of class member m_users and of parameter u in SetUsers function removed. * Radius. Prefix STG of class member m_users and of parameter users in the constructor Server declaration removed. * Radius. The messages of m_logger and printfd changed in function findUser. --- diff --git a/projects/stargazer/plugins/other/radius/radius.cpp b/projects/stargazer/plugins/other/radius/radius.cpp index 8e5b37a8..c93c4ddf 100644 --- a/projects/stargazer/plugins/other/radius/radius.cpp +++ b/projects/stargazer/plugins/other/radius/radius.cpp @@ -59,6 +59,7 @@ int RAD_SETTINGS::ParseSettings(const ModuleSettings & s) RADIUS::RADIUS() : m_running(false), + m_users(NULL), m_logger(PluginLogger::get("radius")) { } @@ -116,7 +117,7 @@ int RADIUS::Run(std::stop_token token) try { if (!m_server) - m_server = std::make_unique(m_ioService, m_radSettings.GetSecret(), m_radSettings.GetPort(), m_radSettings.GetDictionaries(), std::move(token), m_logger); + m_server = std::make_unique(m_ioService, m_radSettings.GetSecret(), m_radSettings.GetPort(), m_radSettings.GetDictionaries(), std::move(token), m_logger, m_users); m_ioService.run(); } catch (const std::exception& e) diff --git a/projects/stargazer/plugins/other/radius/radius.h b/projects/stargazer/plugins/other/radius/radius.h index 6b742de5..17f8ea4e 100644 --- a/projects/stargazer/plugins/other/radius/radius.h +++ b/projects/stargazer/plugins/other/radius/radius.h @@ -18,6 +18,8 @@ namespace STG { struct Settings; + class Users; + class RAD_SETTINGS { public: @@ -44,6 +46,7 @@ namespace STG RADIUS(const RADIUS&) = delete; RADIUS& operator=(const RADIUS&) = delete; + void SetUsers(Users* u) { m_users = u; } void SetSettings(const ModuleSettings & s) override { m_settings = s; } int ParseSettings() override; @@ -74,7 +77,7 @@ namespace STG bool m_running; std::jthread m_thread; - + Users* m_users; PluginLogger m_logger; std::unique_ptr m_server; diff --git a/projects/stargazer/plugins/other/radius/server.cpp b/projects/stargazer/plugins/other/radius/server.cpp index 850847fa..375fcd69 100644 --- a/projects/stargazer/plugins/other/radius/server.cpp +++ b/projects/stargazer/plugins/other/radius/server.cpp @@ -1,15 +1,20 @@ #include "server.h" #include "radproto/packet_codes.h" +#include "radproto/attribute_types.h" +#include "stg/user.h" +#include "stg/users.h" #include "stg/common.h" #include #include +#include //uint8_t, uint32_t using STG::Server; using boost::system::error_code; -Server::Server(boost::asio::io_service& io_service, const std::string& secret, uint16_t port, const std::string& filePath, std::stop_token token, PluginLogger& logger) +Server::Server(boost::asio::io_service& io_service, const std::string& secret, uint16_t port, const std::string& filePath, std::stop_token token, PluginLogger& logger, Users* users) : m_radius(io_service, secret, port), m_dictionaries(filePath), + m_users(users), m_token(std::move(token)), m_logger(logger) { @@ -48,10 +53,14 @@ RadProto::Packet Server::makeResponse(const RadProto::Packet& request) std::vector vendorValue {0, 0, 0, 3}; vendorSpecific.push_back(RadProto::VendorSpecific(m_dictionaries.vendorCode("Dlink"), m_dictionaries.vendorAttributeCode("Dlink", "Dlink-User-Level"), vendorValue)); - if (request.type() == RadProto::ACCESS_REQUEST) + if (request.type() != RadProto::ACCESS_REQUEST) + return RadProto::Packet(RadProto::ACCESS_REJECT, request.id(), request.auth(), {}, {}); + + if (findUser(request)) return RadProto::Packet(RadProto::ACCESS_ACCEPT, request.id(), request.auth(), attributes, vendorSpecific); - return RadProto::Packet(RadProto::ACCESS_REJECT, request.id(), request.auth(), attributes, vendorSpecific); + printfd(__FILE__, "Error findUser\n"); + return RadProto::Packet(RadProto::ACCESS_REJECT, request.id(), request.auth(), {}, {}); } void Server::handleSend(const error_code& ec) @@ -84,5 +93,38 @@ void Server::handleReceive(const error_code& error, const std::optionaltype() == RadProto::USER_NAME) + login = attribute->toString(); + + if (attribute->type() == RadProto::USER_PASSWORD) + password = attribute->toString(); + } + + User* user = nullptr; + if (m_users->FindByName(login, &user)) + { + m_logger("User '%s' not found.", login.c_str()); + printfd(__FILE__, "User '%s' NOT found!\n", login.c_str()); + return false; + } + + printfd(__FILE__, "User '%s' FOUND!\n", user->GetLogin().c_str()); + + if (password != user->GetProperties().password.Get()) + { + m_logger("User's password is incorrect. %s", password.c_str()); + printfd(__FILE__, "User's password is incorrect.\n", password.c_str()); + return false; + } + return true; +} diff --git a/projects/stargazer/plugins/other/radius/server.h b/projects/stargazer/plugins/other/radius/server.h index e33a23b3..7abcff3d 100644 --- a/projects/stargazer/plugins/other/radius/server.h +++ b/projects/stargazer/plugins/other/radius/server.h @@ -11,13 +11,16 @@ namespace STG { + class Users; + class Server { public: - Server(boost::asio::io_service& io_service, const std::string& secret, uint16_t port, const std::string& filePath, std::stop_token token, PluginLogger& logger); + Server(boost::asio::io_service& io_service, const std::string& secret, uint16_t port, const std::string& filePath, std::stop_token token, PluginLogger& logger, Users* users); void stop(); private: RadProto::Packet makeResponse(const RadProto::Packet& request); + bool findUser(const RadProto::Packet& packet); void handleReceive(const boost::system::error_code& error, const std::optional& packet, const boost::asio::ip::udp::endpoint& source); void handleSend(const boost::system::error_code& ec); void start(); @@ -25,6 +28,7 @@ namespace STG RadProto::Socket m_radius; RadProto::Dictionaries m_dictionaries; + Users* m_users; std::stop_token m_token; PluginLogger& m_logger;