]> git.stg.codes - stg.git/commitdiff
Radius. (#8)
authorHelenMamontova <44774239+HelenMamontova@users.noreply.github.com>
Mon, 10 Feb 2025 14:46:27 +0000 (16:46 +0200)
committerGitHub <noreply@github.com>
Mon, 10 Feb 2025 14:46:27 +0000 (16:46 +0200)
* Radius. Header file <mutex>, <jthread.hpp> 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.

projects/stargazer/inst/linux/etc/stargazer/conf-available.d/mod_radius.conf
projects/stargazer/plugins/other/radius/radius.cpp
projects/stargazer/plugins/other/radius/radius.h

index edc94d50888e58040e9b40ee4c9be6b20d8da394..d4cf6c4c41a621dab57e9f1af4fc24ec825c3e99 100644 (file)
@@ -1,34 +1,20 @@
-# Enable the interaction module for FreeRADIUS "mod_radius.so"
+# Enable the interaction module for RADIUS "mod_radius.so"
 <Module radius>
-    # FreeRADIUS password
+    # RADIUS shared secret
     # Parameter: required
-    # Values: any, supported by software
-    # Default: 123456
-    Password = 123456
+    # Values: any
+    Secret = sec
 
-    # FreeRADIUS server
-    # Parameter: required
-    # Values: IP address or DNS name
-    # Default: 127.0.0.1
-    ServerIP = 127.0.0.1
+    # Path to RADIUS dictionaries file
+    # Parameter: optional
+    # Values: file path
+    # Default: /usr/share/freeradius/dictionary
+    # Dictionaries = /usr/share/freeradius/dictionary
 
-    # FreeRADIUS port
-    # Parameter: required
+    # RADIUS port number
+    # Parameter: optional
     # Value: 1 ... 65535
-    # Default: 6666
-    Port = 6666
+    # Default: 1812
+    # Port = 1812
 
-    # List of services for which will be carried out FreeRADIUS authentication 
-    # Note: Parameter can be blank
-    # Parameter: required
-    # Value: any, supported by software
-    # Default: Login-User
-    AuthServices = Login-User
-
-    # List of services for which will be carried out FreeRADIUS Accounting
-    # Note: Parameter can be blank
-    # Parameter: required
-    # Value: any, supported by software
-    # Default: Framed-User
-    AcctServices = Framed-User
-</Module>
\ No newline at end of file
+</Module>
index 78c80b4df9e32a2d832038f2da5acf6711d7a494..e517c959ec81882907a96ff349a812c261ca8542 100644 (file)
@@ -6,10 +6,11 @@
 
 #include <boost/asio.hpp>
 #include <string>
+#include <iterator>
 #include <iostream>
-#include <cstdint> //uint8_t, uint32_t
 
 using STG::RADIUS;
+using STG::RAD_SETTINGS;
 
 extern "C" STG::Plugin* GetPlugin()
 {
@@ -17,15 +18,88 @@ extern "C" STG::Plugin* GetPlugin()
     return &plugin;
 }
 
-std::string RADIUS::GetVersion() const
+RAD_SETTINGS::RAD_SETTINGS()
+    : m_port(1812),
+      m_dictionaries("/usr/share/freeradius/dictionary")
+{}
+
+int RAD_SETTINGS::ParseSettings(const ModuleSettings & s)
 {
-    return "Radius v.1.0";
+    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<uint16_t>(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];
+    return 0;
 }
 
 RADIUS::RADIUS()
-    : m_logger(PluginLogger::get("radius")),
-      m_running(false)
+    : m_running(false),
+      m_logger(PluginLogger::get("radius"))
+{
+}
+
+int RADIUS::Run(std::stop_token token)
+{
+    SetRunning(true);
+
+    try
+    {
+        boost::asio::io_service ioService;
+        Server server(ioService, m_radSettings.GetSecret(), m_radSettings.GetPort(), m_radSettings.GetDictionaries());
+        ioService.run();
+    }
+    catch (const std::exception& e)
+    {
+        m_errorStr = "Exception in RADIUS::Run(): " + std::string(e.what());
+        m_logger("Exception in RADIUS:: Run(): %s", e.what());
+        printfd(__FILE__, "Exception in RADIUS:: Run(). Message: '%s'\n", e.what());
+    }
+
+    SetRunning(false);
+    return 0;
+}
+
+int RADIUS::ParseSettings()
 {
+    auto ret = m_radSettings.ParseSettings(m_settings);
+    if (ret != 0)
+        m_errorStr = m_radSettings.GetStrError();
+
+    return ret;
+}
+
+std::string RADIUS::GetVersion() const
+{
+    return "Radius v.1.0";
 }
 
 int RADIUS::Start()
@@ -45,35 +119,14 @@ int RADIUS::Stop()
     return 0;
 }
 
-void RADIUS::SetRunning(bool val)
-{
-    const std::lock_guard lock(m_mutex);
-    m_running = val;
-}
-
 bool RADIUS::IsRunning()
 {
     const std::lock_guard lock(m_mutex);
     return m_running;
 }
 
-int RADIUS::Run(std::stop_token token)
+void RADIUS::SetRunning(bool val)
 {
-    SetRunning(true);
-
-    try
-    {
-        boost::asio::io_service ioService;
-        Server server(ioService, "secret", 1812, "/usr/share/freeradius/dictionary");
-        ioService.run();
-    }
-    catch (const std::exception& e)
-    {
-        m_errorStr = "Exception in RADIUS::Run(): " + std::string(e.what());
-        m_logger("Exception in RADIUS:: Run(): %s", e.what());
-        printfd(__FILE__, "Exception in RADIUS:: Run(). Message: '%s'\n", e.what());
-    }
-
-    SetRunning(false);
-    return 0;
+    const std::lock_guard lock(m_mutex);
+    m_running = val;
 }
index 4e06e463e581ec8ae91432a541f5a884b94a02ba..8eedace11e83cdb822674b4564b9416f920b1c1f 100644 (file)
@@ -1,27 +1,58 @@
 #pragma once
 
 #include "stg/auth.h"
+#include "stg/plugin.h"
+#include "stg/module_settings.h"
+#include "stg/subscriptions.h"
 #include "stg/logger.h"
 
 #include <string>
 #include <mutex>
 #include <jthread.hpp>
+#include <cstdint> //uint8_t, uint32_t
 
 namespace STG
 {
+    struct Settings;
+
+    class RAD_SETTINGS
+    {
+        public:
+            RAD_SETTINGS();
+            virtual ~RAD_SETTINGS() {}
+            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; }
+
+        private:
+            std::string m_errorStr;
+            uint16_t m_port;
+            std::string m_dictionaries;
+            std::string m_secret;
+    };
+
     class RADIUS : public Auth
     {
         public:
             RADIUS();
+            RADIUS(const RADIUS&) = delete;
+            RADIUS& operator=(const RADIUS&) = delete;
+
+            void SetSettings(const ModuleSettings & s) override { m_settings = s; }
+            int ParseSettings() override;
 
             int Start() override;
             int Stop() override;
             int Reload(const ModuleSettings & /*ms*/) override { return 0; }
             bool IsRunning() override;
             void SetRunning(bool val);
-            int ParseSettings() override { return 0; }
+
             const std::string & GetStrError() const override { return m_errorStr; }
             std::string GetVersion() const override;
+
             uint16_t GetStartPosition() const override { return 0; }
             uint16_t GetStopPosition() const override { return 0; }
 
@@ -29,11 +60,17 @@ namespace STG
 
         private:
             std::mutex m_mutex;
+
+            int Run(std::stop_token token);
+
             mutable std::string m_errorStr;
-            std::jthread m_thread;
+            RAD_SETTINGS m_radSettings;
+            ModuleSettings m_settings;
+
             bool m_running;
-            PluginLogger m_logger;
 
-            int Run(std::stop_token token);
+            std::jthread m_thread;
+
+            PluginLogger m_logger;
     };
 }