From: HelenMamontova <44774239+HelenMamontova@users.noreply.github.com> Date: Sun, 8 Mar 2026 14:29:59 +0000 (+0200) Subject: Sending attributes from /send section. (#13) X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/4fe07d7c76c7c9006780389a5babb524560b3ff0 Sending attributes from /send section. (#13) * 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. * Radius. Search for parameters auth and send added to the ParseSettings function. * Radius. Extra whitespace removed, unnecessary variables sendValue and sectionsParams removed in ParseSettings function. * Radius. The struct AttrValue added. * Radius. Header files , , added. Parsing attributes of field send in section auth of mod_radius.conf added to the function ParseSettings. * Radius. Filling vector of pairs attrSend added. Extra printfd function removed. * Radius. The Sign enumeration elements names changed in the struct AttrValue. * Radius. Using declaration for struct AttrValue added. Function ParseSendAttr definition added and call in the function ParseSettings added. * Radius. The enum class Sign renamed to Type, object sign renamed to type. * Radius. The enum class Sign renamed to Type, object sign renamed to type in ParseSendAttr function and ParseSettings function. * Radius. The enumerator names NOT_VALUE changed to PARAN_NAME, IS_VALUE changed to VALUE in enum Type. * Radius. Radius. The enumerator names NOT_VALUE changed to PARAM_NAME, IS_VALUE changed to VALUE in ParseSendAttr function. * Radius. The AttrValue struct moved into the RAD_SETTINGS class. * Radius. The using statement for struct AttrValue removed. The using alias for struct AttrValue added. * Radius. The vector parsedSendAttr check added to the function ParseSendAttr. * Radius. The ParseSendAttr method added, m_logger variable class member added to RAD_SETTINGS class. * Radius. ParseSendAttr function changed to method of RAD_SETTINGS class. Vector parsedSendAttr check for emptiness and check vector size added to the ParseSendAttr method. Class member m_logger initialization added to initialization list to RAD_SETTINGS constructor. * Radius. Quotes check for the ValueName added to the ParseSendAttr method. * Radius. Extra variables attrValue.value and attrValue.type removed in the ParseSendAttr method. * Radius. Extra variable attrValue removed in ParseSendAttr method. * Radius. Send attributes parsing changed in method ParseSendAttr. * Radius. Argument fieldSendAttr name changed to value and argument type changed to const std::string& in ParseSendAttr method. Vector keyValuePairs name changed to res. * Radius. Argument fieldSendAttr name changed to value and argument type changed to const std::string& in ParseSendAttr method. * Radius. Vector name parsedSendAttr changed to keyValue in ParseSendAttr method. * Radius. Functions front() and back() are used to get the first and last characters of string valueName in ParseSendAttr method. * Radius. Method declaration ParseSendAttr moved to private in class RAD_SETTINGS. * Radius. Formatting fixed. Class member m_res and public function GetRes added to class RAD_SETTINGS. * Radius. Extra variable res removed. Class member m_res definition added to ParseSendAttr function and ParseSettings function. * Radius. Vector res added, variable m_res replaced by res in ParseSendAttr function. * Radius. Function name GetRes changed to GetSendPairs, class member vaiable name m_res changed to m_sendPairs in class RAD_SETTINGS. * Radius. Class member vaiable name m_res changed to m_sendPairs in the ParseSettings function. * Radius. Parameter auth added to mod_radius.conf. * Radius. The ParseMatchAttr method declaration and m_matchPairs class member added to RAD_SETTINGS class. * Radius. The ParseMatchAttr function definition added. Match attributes parsing added to ParseSetting function. * Radius. The ParseMatchAttr method removed, the ParseSendAttr method renamed to ParseAuthAttr in the class RAD_SETTINGS. * Radius. The ParseSendAttr method definition and call renamed to ParseAuthAttr. Parameter paramName added to ParseAuthAttr method. The ParseMatchAttr method definition removed. ParseMatchAttr method call replaced by ParseAuthAttr. * Radius. Attribute for parameter auth/match changed. * Radius. Parameter autz added. * Radius. ParseAuthAttr function renamed to ParseAuthAutzAttr. * Radius. ParseAuthAttr function renamed to ParseAuthAutzAttr. The autz attributes parsing added, m_logger and printfd messages changed in the ParseAuthAutzAttr function. * Radius. ParseAuthAutzAttr function renamed to ParseSectionsAttr. The MakeKeyValuePairs function definition added. * Radius. ParseAuthAutzAttr function renamed to ParseSectionsAttr. The messages of m_logger and printfd changed in the ParseSectionsAttr function. The MakeKeyValuePairs function definition and call added. * Radius. Attributes of auth/send, autz/send, autz/match changed. * Radius. ParseSectionsAttr function name changed to ParseRyles. * Radius. ParseSectionsAttr function name definition and call changed to ParseRyles. * Radius. Extra printfd function call removed in MakeKeyValuePairs functions. * Radius. The ShowRules, GetSendPairsAuth, GetMatchPairsAuth, GetSendPairsAutz, GetMatchPairsAutz functions declaration added. The m_sendPairs, m_matchPairs class members removed. The m_sendPairsAuth, m_matchPairsAuth, m_sendPairsAutz, m_matchPairsAutz class members added. * Radius. The ShowRules function definition added and its call in MakeKeyValuePairs function added. * Radius. The strRules string creating fixed and strRules string renamed to result in the ShowRules function. * Radius. Parameter name changed to attributes and & added to parameter in the ParseRules function. * Radius. The ShowRules function definition changed. Parameter name changed to attributes and & added to parameter in the ShowRules function. * Radius. The struct ASection added. The getAuth, getAutz, parseASection function declaration added. Class member m_auth, m_autz added. Unnecessary code removed. * Radius. The using ASection for alias added. The parseASection function definition and call added, MakeKeyValuePairs function removed, ParseSettings function changed. Extra printfd function added. * Radius. The return added to parseASection function. Extra printfd functions removed. * Radius. GIT_TAG changed to 1.1.3 in the ExternalProject_Add for async-radius. * Radius. The m_ioService class member replaced by m_ioContext in class Radius. * Radius. The m_ioService class member replaced by m_ioContext in the function Run. * Radius. Parameter io_service replaced by io_context in the constructor Server. * Radius. Parameter io_service replaced by io_context in the constructor Server definition. * Radius. Parameters msg, ip commented out in the SendMessage function. * Radius. Variable 'sep' declared 'const' in ParseRules function. * Radius. Variables 'tokens', 'sp', 'tok' declared 'const' in ParseRules function. * Radius. ShowRules function declaration removed from class RAD_SETTINGS. * Radius. ShowRules function definition is made as a free function. * Radius. ShowRules function definition is placed in the anonymous namespace. * Radius. The code for determining the first element of vector changed in the function ShowRules. * Radius. Function SetUsers declaration is made as override in the class Radius. * Radius. Header file added. * Radius. The rad_settings.cpp file name added to add_library for MODULE radius. * Radius. Header file "rad_settings.h" added. RAD_SETTINGS class declaration removed, struct Settings forward declaration removed. * Radius. Header files , , , , removed. Extra using declaration removed. Functions definition of class RAD_SETTINGS removed. Parameter m_radSettings added to Server constructor call. * Radius. The new file rad_settings.h added. * Radius. The new file rad_settings.cpp added. * Radius. Header file "rad_settings.h" added. Parameter radSettings added to constructor Server declaration. Class memberm_radSettings added. * Radius. Parameter radSettings added to constructor Server definition. Class member m_radSettings initialization added in the constructor Server. * Radius. The attributes User-Name, Cacllback-Number added to section /send. * Radius. Class User forward declaration added. Parameter radSettings changed to const in constructor Server declaration. The bool findUser function declaration replaced by const User* findUser. * Radius. Header files added. The using statement for STG User added. Parameter radSettings changed to const in constructor Server definition. The bool findUser function definition replaced by const User* findUser. The makeAttributes function definition added. Function makeResponse definition changed. * Radius. Unnecessary attributes removed from /send section. * Radius. The rad_settings.h header file changed to settings.h. * Radius. The rad_settings.h header file changed to settings.h. * Radius. The rad_settings.h header file changed to settings.h. * Radius. The rad_settings.cpp filename changed to settings.cpp in add_library for BUILD_MOD_RADIUS. * Radius. The settings.cpp filename changed to config.cpp in add_library for BUILD_MOD_RADIUS. * Radius. The settings.h header file changed to config.h. * Radius. The settings.h header file changed to config.h. * Radius. The settings.h header file changed to config.h. * Radius. The RAD_SETTINGS class name changed to Config. * Radius. The RAD_SETTINGS class name changed to Config. * Radius. The RAD_SETTINGS class name changed to Config. The m_radSettings variable class member changed to m_config. * Radius. The m_radSettings variable class member changed to m_config. * Radius. The RAD_SETTINGS class name changed to Config. The m_radSettings variable class member changed to m_config. The radSettings parameter name changed to config. * Radius. The RAD_SETTINGS class name changed to Config. The m_radSettings variable class member changed to m_config. The radSettings parameter name changed to config in constructor Server. * GIT_TAG changed to 1.2.0. * GIT_TAG changed to 1.3.0. * Header file "rsdproto/attribute.h" added. Server class member variable m_attribute added. * Header file "radproto/attribute_types.h" replaced by "radproto/attribute_codes.h". The class member m_attribute initialization added to constructor Server. The attrType variable added. The function type() replaced by code(). The function call make() added to function makeAttributes instead of attributes vector fill code. * Header file "radproto/attribute.h" removed. Server class member variable m_attribute removed. * Header file "radproto/attribute.h" added. Server class member variable m_attribute initialization removed. Logic in cycle for is fixed in the makeAttributes function. * Parameter name attributes fixed to rules in ShowRules function. * Vector keyValue fill replaced with split in the function ParseRules. Formatting fixed. * The variables attrName, attrValue, attrType, attrCode declaration moved into the cycle in the function makeAttributes. * The header files , removed. * The variables attrName, attrCode, attrType declaration changed in function makeAttributes. * The predicate for boost::algorithm::trim_copy_if changed in the function ParseRules. * The output of the user's password in the log removed. * The m_secret variable value changed to -1 if parameter "Secret" not found in ParseSettings function. * The user variable declaration and definition moved in makeResponse function. * The return command added if error in the handleReceive function. * The output of the user's password in the function printfd removed. * The SetRunning function declaration moved to privat. * Header file removed. * Null changed to nullptr when initializing m_users in class Radius. * The body of the condition if parameter "Secret" not found fixed and return -1 added in ParseSettings function. --- diff --git a/projects/stargazer/plugins/CMakeLists.txt b/projects/stargazer/plugins/CMakeLists.txt index fc030b67..7d0e02e6 100644 --- a/projects/stargazer/plugins/CMakeLists.txt +++ b/projects/stargazer/plugins/CMakeLists.txt @@ -15,7 +15,8 @@ if ( BUILD_MOD_RADIUS ) find_package ( Boost REQUIRED ) add_library ( mod_radius MODULE other/radius/radius.cpp - other/radius/server.cpp) + other/radius/server.cpp + other/radius/config.cpp) target_link_libraries ( mod_radius PRIVATE scriptexecuter logger common ) set_target_properties ( mod_radius PROPERTIES PREFIX "" ) @@ -23,7 +24,7 @@ if ( BUILD_MOD_RADIUS ) ExternalProject_Add ( async-radius GIT_REPOSITORY https://github.com/madf/async-radius.git - GIT_TAG 1.1.3 + GIT_TAG 1.3.0 GIT_SHALLOW true INSTALL_COMMAND "" CMAKE_ARGS -DCMAKE_CXX_FLAGS=-fPIC ) diff --git a/projects/stargazer/plugins/other/radius/config.cpp b/projects/stargazer/plugins/other/radius/config.cpp new file mode 100644 index 00000000..108b61e6 --- /dev/null +++ b/projects/stargazer/plugins/other/radius/config.cpp @@ -0,0 +1,146 @@ +#include "config.h" +#include "radproto/error.h" +#include "stg/common.h" +#include +#include + +#include +#include +#include +#include +#include + +using STG::Config; +using AttrValue = Config::AttrValue; +using ASection = Config::ASection; + +namespace +{ + std::string ShowRules(const std::vector>& rules) + { + std::string result; + for (const auto& at : rules) + { + if (!result.empty()) + result += ", "; + + if (at.second.type == AttrValue::Type::PARAM_NAME) + result.append(at.first + " = " + at.second.value); + else + result.append(at.first + " = '" + at.second.value + "'"); + } + return result; + } +} + +std::vector> Config::ParseRules(const std::string& value, const std::string& paramName) +{ + using tokenizer = boost::tokenizer>; + const boost::char_separator sep(","); + + const tokenizer tokens(value, sep); + + std::vector> res; + + for (const auto& token : tokens) + { + std::vector keyValue; + + split(keyValue, boost::algorithm::trim_copy_if(token, boost::is_any_of(" \t")), boost::is_any_of(" ="), boost::token_compress_on); + + if (keyValue.size() != 2) + { + m_logger("The '%s' attribute specification has an incorrect format: '%s'.", paramName.c_str(), token.c_str()); + printfd(__FILE__, "The '%s' attribute specification has an incorrect format: '%s'.", paramName.c_str(), token.c_str()); + return {}; + } + + auto type = AttrValue::Type::PARAM_NAME; + std::string valueName = keyValue[1]; + if (valueName.front() == '\'' && valueName.back() == '\'') + { + type = AttrValue::Type::VALUE; + valueName.erase(0, 1); + valueName.erase(valueName.length() - 1, 1); + } + else if ((valueName.front() == '\'' && valueName.back() != '\'') || (valueName.front() != '\'' && valueName.back() == '\'')) + { + m_logger("Error ParseRules: '%s' attribute parameter value is invalid.\n", paramName.c_str()); + printfd(__FILE__, "Error ParseRules: '%s' attribute parameter value is invalid.\n", paramName.c_str()); + return {}; + } + res.emplace_back(keyValue[0], AttrValue{valueName, type}); + } + return res; +} + +ASection Config::parseASection(const std::vector& conf) +{ + ASection res; + const auto mit = std::find(conf.begin(), conf.end(), ParamValue("match", {})); + if (mit != conf.end()) + res.match = ParseRules(mit->value[0], mit->param); + + const auto sit = std::find(conf.begin(), conf.end(), ParamValue("send", {})); + if (sit != conf.end()) + res.send = ParseRules(sit->value[0], sit->param); + + return res; +} + +Config::Config() + : m_port(1812), + m_dictionaries("/usr/share/freeradius/dictionary"), + m_logger(PluginLogger::get("radius")) +{} + +int Config::ParseSettings(const ModuleSettings & s) +{ + ParamValue pv; + int p; + + pv.param = "Port"; + auto pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv); + if (pvi != s.moduleParams.end() && !pvi->value.empty()) + { + if (ParseIntInRange(pvi->value[0], 2, 65535, &p) != 0) + { + m_errorStr = "Cannot parse parameter \'Port\': " + m_errorStr; + printfd(__FILE__, "Cannot parse parameter 'Port'\n"); + return -1; + } + m_port = static_cast(p); + } + + pv.param = "Secret"; + pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv); + if (pvi == s.moduleParams.end() || pvi->value.empty()) + { + m_errorStr = "Parameter \'Secret\' not found."; + printfd(__FILE__, "Parameter 'Secret' not found\n"); + return -1; + } + else + m_secret = pvi->value[0]; + + pv.param = "Dictionaries"; + pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv); + if (pvi != s.moduleParams.end() && !pvi->value.empty()) + m_dictionaries = pvi->value[0]; + + const auto authIt = std::find(s.moduleParams.begin(), s.moduleParams.end(), ParamValue("auth", {})); + if (authIt != s.moduleParams.end()) + m_auth = parseASection(authIt->sections); + + const auto autzIt = std::find(s.moduleParams.begin(), s.moduleParams.end(), ParamValue("autz", {})); + if (autzIt != s.moduleParams.end()) + m_autz = parseASection(autzIt->sections); + + printfd(__FILE__, " auth.match = \"%s\"\n", ShowRules(m_auth.match).c_str()); + printfd(__FILE__, " auth.send = \"%s\"\n", ShowRules(m_auth.send).c_str()); + printfd(__FILE__, " autz.match = \"%s\"\n", ShowRules(m_autz.match).c_str()); + printfd(__FILE__, " autz.send = \"%s\"\n", ShowRules(m_autz.send).c_str()); + + return 0; +} + diff --git a/projects/stargazer/plugins/other/radius/config.h b/projects/stargazer/plugins/other/radius/config.h new file mode 100644 index 00000000..74e483a5 --- /dev/null +++ b/projects/stargazer/plugins/other/radius/config.h @@ -0,0 +1,60 @@ +#pragma once + +#include "stg/module_settings.h" +#include "stg/subscriptions.h" +#include "stg/logger.h" + +#include +#include //uint8_t, uint32_t + +namespace STG +{ + struct Settings; + + class Config + { + public: + Config(); + + struct AttrValue + { + enum class Type + { + PARAM_NAME, + VALUE + }; + std::string value; + Type type; + }; + + struct ASection + { + using Pairs = std::vector>; + Pairs match; + Pairs send; + }; + + const std::string& GetStrError() const { return m_errorStr; } + int ParseSettings(const ModuleSettings& s); + + uint16_t GetPort() const { return m_port; } + const std::string& GetDictionaries() const { return m_dictionaries; } + const std::string& GetSecret() const { return m_secret; } + const ASection& getAuth() const { return m_auth; } + const ASection& getAutz() const { return m_autz; } + + private: + std::vector> ParseRules(const std::string& value, const std::string& paramName); + ASection parseASection(const std::vector& conf); + + std::string m_errorStr; + uint16_t m_port; + std::string m_dictionaries; + std::string m_secret; + + ASection m_auth; + ASection m_autz; + + PluginLogger m_logger; + }; +} diff --git a/projects/stargazer/plugins/other/radius/radius.cpp b/projects/stargazer/plugins/other/radius/radius.cpp index 68291dcf..11fbcf7b 100644 --- a/projects/stargazer/plugins/other/radius/radius.cpp +++ b/projects/stargazer/plugins/other/radius/radius.cpp @@ -3,16 +3,9 @@ #include "stg/common.h" #include -#include #include -#include -#include -#include using STG::RADIUS; -using STG::RAD_SETTINGS; -using AttrValue = RAD_SETTINGS::AttrValue; -using ASection = RAD_SETTINGS::ASection; extern "C" STG::Plugin* GetPlugin() { @@ -20,150 +13,18 @@ extern "C" STG::Plugin* GetPlugin() return &plugin; } -namespace -{ - std::string ShowRules(const std::vector>& attributes) - { - std::string result; - for (const auto& at : attributes) - { - if (!result.empty()) - result += ", "; - - if (at.second.type == AttrValue::Type::PARAM_NAME) - result.append(at.first + " = " + at.second.value); - else - result.append(at.first + " = '" + at.second.value + "'"); - } - return result; - } -} - -std::vector> RAD_SETTINGS::ParseRules(const std::string& value, const std::string& paramName) -{ - using tokenizer = boost::tokenizer>; - const boost::char_separator sep(","); - - const tokenizer tokens(value, sep); - - std::vector> res; - for (const auto& token : tokens) - { - const boost::char_separator sp(" ="); - const tokenizer tok(token, sp); - - std::vector keyValue; - for (const auto& t : tok) - keyValue.push_back(t); - - if (keyValue.size() != 2) - { - m_logger("The '%s' attribute specification has an incorrect format: '%s'.", paramName.c_str(), token.c_str()); - printfd(__FILE__, "The '%s' attribute specification has an incorrect format: '%s'.", paramName.c_str(), token.c_str()); - return {}; - } - - auto type = AttrValue::Type::PARAM_NAME; - std::string valueName = keyValue[1]; - if (valueName.front() == '\'' && valueName.back() == '\'') - { - type = AttrValue::Type::VALUE; - valueName.erase(0, 1); - valueName.erase(valueName.length() - 1, 1); - } - else if ((valueName.front() == '\'' && valueName.back() != '\'') || (valueName.front() != '\'' && valueName.back() == '\'')) - { - m_logger("Error ParseRules: '%s' attribute parameter value is invalid.\n", paramName.c_str()); - printfd(__FILE__, "Error ParseRules: '%s' attribute parameter value is invalid.\n", paramName.c_str()); - return {}; - } - res.emplace_back(keyValue[0], AttrValue{valueName, type}); - } - return res; -} - -ASection RAD_SETTINGS::parseASection(const std::vector& conf) -{ - ASection res; - const auto mit = std::find(conf.begin(), conf.end(), ParamValue("match", {})); - if (mit != conf.end()) - res.match = ParseRules(mit->value[0], mit->param); - - const auto sit = std::find(conf.begin(), conf.end(), ParamValue("send", {})); - if (sit != conf.end()) - res.send = ParseRules(sit->value[0], sit->param); - - return res; -} - -RAD_SETTINGS::RAD_SETTINGS() - : m_port(1812), - m_dictionaries("/usr/share/freeradius/dictionary"), - m_logger(PluginLogger::get("radius")) -{} - -int RAD_SETTINGS::ParseSettings(const ModuleSettings & s) -{ - ParamValue pv; - int p; - - pv.param = "Port"; - auto pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv); - if (pvi != s.moduleParams.end() && !pvi->value.empty()) - { - if (ParseIntInRange(pvi->value[0], 2, 65535, &p) != 0) - { - m_errorStr = "Cannot parse parameter \'Port\': " + m_errorStr; - printfd(__FILE__, "Cannot parse parameter 'Port'\n"); - return -1; - } - m_port = static_cast(p); - } - - pv.param = "Secret"; - pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv); - if (pvi == s.moduleParams.end() || pvi->value.empty()) - { - m_errorStr = "Parameter \'Secret\' not found."; - printfd(__FILE__, "Parameter 'Secret' not found\n"); - m_secret = ""; - } - else - m_secret = pvi->value[0]; - - pv.param = "Dictionaries"; - pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv); - if (pvi != s.moduleParams.end() && !pvi->value.empty()) - m_dictionaries = pvi->value[0]; - - const auto authIt = std::find(s.moduleParams.begin(), s.moduleParams.end(), ParamValue("auth", {})); - if (authIt != s.moduleParams.end()) - m_auth = parseASection(authIt->sections); - - const auto autzIt = std::find(s.moduleParams.begin(), s.moduleParams.end(), ParamValue("autz", {})); - if (autzIt != s.moduleParams.end()) - m_autz = parseASection(autzIt->sections); - - printfd(__FILE__, " auth.match = \"%s\"\n", ShowRules(m_auth.match).c_str()); - printfd(__FILE__, " auth.send = \"%s\"\n", ShowRules(m_auth.send).c_str()); - printfd(__FILE__, " autz.match = \"%s\"\n", ShowRules(m_autz.match).c_str()); - printfd(__FILE__, " autz.send = \"%s\"\n", ShowRules(m_autz.send).c_str()); - - return 0; -} - RADIUS::RADIUS() : m_running(false), - m_users(NULL), + m_users(nullptr), m_logger(PluginLogger::get("radius")) { } int RADIUS::ParseSettings() { - auto ret = m_radSettings.ParseSettings(m_settings); + auto ret = m_config.ParseSettings(m_settings); if (ret != 0) - m_errorStr = m_radSettings.GetStrError(); + m_errorStr = m_config.GetStrError(); return ret; } @@ -212,7 +73,7 @@ int RADIUS::Run(std::stop_token token) try { if (!m_server) - m_server = std::make_unique(m_ioContext, m_radSettings.GetSecret(), m_radSettings.GetPort(), m_radSettings.GetDictionaries(), std::move(token), m_logger, m_users); + m_server = std::make_unique(m_ioContext, m_config.GetSecret(), m_config.GetPort(), m_config.GetDictionaries(), std::move(token), m_logger, m_users, m_config); m_ioContext.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 80e032a8..59cbf0d5 100644 --- a/projects/stargazer/plugins/other/radius/radius.h +++ b/projects/stargazer/plugins/other/radius/radius.h @@ -2,6 +2,7 @@ #include "stg/auth.h" #include "stg/plugin.h" +#include "config.h" #include "stg/module_settings.h" #include "stg/subscriptions.h" #include "stg/logger.h" @@ -16,58 +17,8 @@ namespace STG { - struct Settings; - class Users; - class RAD_SETTINGS - { - public: - RAD_SETTINGS(); - virtual ~RAD_SETTINGS() {} - - struct AttrValue - { - enum class Type - { - PARAM_NAME, - VALUE - }; - std::string value; - Type type; - }; - - struct ASection - { - using Pairs = std::vector>; - Pairs match; - Pairs send; - }; - - const std::string& GetStrError() const { return m_errorStr; } - int ParseSettings(const ModuleSettings& s); - - uint16_t GetPort() const { return m_port; } - const std::string& GetDictionaries() const { return m_dictionaries; } - const std::string& GetSecret() const { return m_secret; } - const ASection& getAuth() const { return m_auth; } - const ASection& getAutz() const { return m_autz; } - - private: - std::vector> ParseRules(const std::string& value, const std::string& paramName); - ASection parseASection(const std::vector& conf); - - std::string m_errorStr; - uint16_t m_port; - std::string m_dictionaries; - std::string m_secret; - - ASection m_auth; - ASection m_autz; - - PluginLogger m_logger; - }; - class RADIUS : public Auth { public: @@ -83,7 +34,6 @@ namespace STG int Stop() override; int Reload(const ModuleSettings& /*ms*/) override { return 0; } bool IsRunning() override; - void SetRunning(bool val); const std::string& GetStrError() const override { return m_errorStr; } std::string GetVersion() const override; @@ -97,10 +47,11 @@ namespace STG std::mutex m_mutex; boost::asio::io_context m_ioContext; + void SetRunning(bool val); int Run(std::stop_token token); mutable std::string m_errorStr; - RAD_SETTINGS m_radSettings; + Config m_config; ModuleSettings m_settings; bool m_running; diff --git a/projects/stargazer/plugins/other/radius/server.cpp b/projects/stargazer/plugins/other/radius/server.cpp index 9e6766d5..11aa9a79 100644 --- a/projects/stargazer/plugins/other/radius/server.cpp +++ b/projects/stargazer/plugins/other/radius/server.cpp @@ -1,20 +1,26 @@ #include "server.h" +#include "radproto/attribute.h" #include "radproto/packet_codes.h" -#include "radproto/attribute_types.h" +#include "radproto/attribute_codes.h" #include "stg/user.h" #include "stg/users.h" #include "stg/common.h" +#include +#include +#include #include #include #include //uint8_t, uint32_t using STG::Server; +using STG::User; using boost::system::error_code; -Server::Server(boost::asio::io_context& io_context, const std::string& secret, uint16_t port, const std::string& filePath, std::stop_token token, PluginLogger& logger, Users* users) +Server::Server(boost::asio::io_context& io_context, const std::string& secret, uint16_t port, const std::string& filePath, std::stop_token token, PluginLogger& logger, Users* users, const Config& config) : m_radius(io_context, secret, port), m_dictionaries(filePath), m_users(users), + m_config(config), m_token(std::move(token)), m_logger(logger) { @@ -37,27 +43,42 @@ void Server::startReceive() m_radius.asyncReceive([this](const auto& error, const auto& packet, const boost::asio::ip::udp::endpoint& source){ handleReceive(error, packet, source); }); } -RadProto::Packet Server::makeResponse(const RadProto::Packet& request) +std::vector Server::makeAttributes(const User* user) { std::vector attributes; - attributes.push_back(new RadProto::String(m_dictionaries.attributeCode("User-Name"), "test")); - attributes.push_back(new RadProto::Integer(m_dictionaries.attributeCode("NAS-Port"), 20)); - std::array address {127, 104, 22, 17}; - attributes.push_back(new RadProto::IpAddress(m_dictionaries.attributeCode("NAS-IP-Address"), address)); - std::vector bytes {'1', '2', '3', 'a', 'b', 'c'}; - attributes.push_back(new RadProto::Bytes(m_dictionaries.attributeCode("Callback-Number"), bytes)); - std::vector chapPassword {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g' }; - attributes.push_back(new RadProto::ChapPassword(m_dictionaries.attributeCode("CHAP-Password"), 1, chapPassword)); - - std::vector vendorSpecific; - 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) + + for (const auto& at : m_config.getAuth().send) + { + std::string attrValue; + + if (at.second.type == Config::AttrValue::Type::PARAM_NAME) + attrValue = user->GetParamValue(at.second.value); + else + attrValue = at.second.value; + + const auto attrName = at.first; + const auto attrCode = m_dictionaries.attributeCode(attrName); + const auto attrType = m_dictionaries.attributeType(attrCode); + + if ((attrType == "integer") && (m_dictionaries.attributeValueFindByName(attrName, attrValue))) + attributes.push_back(RadProto::Attribute::make(attrCode, attrType, std::to_string(m_dictionaries.attributeValueCode(attrName, attrValue)))); + else + attributes.push_back(RadProto::Attribute::make(attrCode, attrType, attrValue)); + } + return attributes; +} + +RadProto::Packet Server::makeResponse(const RadProto::Packet& request) +{ + if (request.code() != 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); + const User* user; + + user = findUser(request); + + if (user != nullptr) + return RadProto::Packet(RadProto::ACCESS_ACCEPT, request.id(), request.auth(), makeAttributes(user), {}); printfd(__FILE__, "Error findUser\n"); return RadProto::Packet(RadProto::ACCESS_REJECT, request.id(), request.auth(), {}, {}); @@ -85,6 +106,7 @@ void Server::handleReceive(const error_code& error, const std::optionaltype() == RadProto::USER_NAME) + if (attribute->code() == RadProto::USER_NAME) login = attribute->toString(); - if (attribute->type() == RadProto::USER_PASSWORD) + if (attribute->code() == RadProto::USER_PASSWORD) password = attribute->toString(); } @@ -115,16 +137,16 @@ bool Server::findUser(const RadProto::Packet& packet) { m_logger("User '%s' not found.", login.c_str()); printfd(__FILE__, "User '%s' NOT found!\n", login.c_str()); - return false; + return nullptr; } 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; + m_logger("User's password is incorrect."); + printfd(__FILE__, "User's password is incorrect.\n"); + return nullptr; } - return true; + return user; } diff --git a/projects/stargazer/plugins/other/radius/server.h b/projects/stargazer/plugins/other/radius/server.h index d4efef98..1523179e 100644 --- a/projects/stargazer/plugins/other/radius/server.h +++ b/projects/stargazer/plugins/other/radius/server.h @@ -2,6 +2,7 @@ #include "radproto/socket.h" #include "radproto/packet.h" +#include "config.h" #include "radproto/dictionaries.h" #include "stg/logger.h" #include @@ -12,15 +13,17 @@ namespace STG { class Users; + class User; class Server { public: - Server(boost::asio::io_context& io_context, const std::string& secret, uint16_t port, const std::string& filePath, std::stop_token token, PluginLogger& logger, Users* users); + Server(boost::asio::io_context& io_context, const std::string& secret, uint16_t port, const std::string& filePath, std::stop_token token, PluginLogger& logger, Users* users, const Config& config); void stop(); private: + std::vector makeAttributes(const User* user); RadProto::Packet makeResponse(const RadProto::Packet& request); - bool findUser(const RadProto::Packet& packet); + const User* 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(); @@ -29,6 +32,7 @@ namespace STG RadProto::Socket m_radius; RadProto::Dictionaries m_dictionaries; Users* m_users; + const Config& m_config; std::stop_token m_token; PluginLogger& m_logger;