]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/radius/radius.cpp
Radius (#9)
[stg.git] / projects / stargazer / plugins / other / radius / radius.cpp
1 #include "radius.h"
2 #include "radproto/error.h"
3 #include "stg/common.h"
4
5 #include <iterator>
6 #include <iostream>
7
8 using STG::RADIUS;
9 using STG::RAD_SETTINGS;
10
11 extern "C" STG::Plugin* GetPlugin()
12 {
13     static RADIUS plugin;
14     return &plugin;
15 }
16
17 RAD_SETTINGS::RAD_SETTINGS()
18     : m_port(1812),
19       m_dictionaries("/usr/share/freeradius/dictionary")
20 {}
21
22 int RAD_SETTINGS::ParseSettings(const ModuleSettings & s)
23 {
24     ParamValue pv;
25     int p;
26
27     pv.param = "Port";
28     auto pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
29     if (pvi != s.moduleParams.end() && !pvi->value.empty())
30     {
31         if (ParseIntInRange(pvi->value[0], 2, 65535, &p) != 0)
32         {
33             m_errorStr = "Cannot parse parameter \'Port\': " + m_errorStr;
34             printfd(__FILE__, "Cannot parse parameter 'Port'\n");
35             return -1;
36         }
37         m_port = static_cast<uint16_t>(p);
38     }
39
40     pv.param = "Secret";
41     pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
42     if (pvi == s.moduleParams.end() || pvi->value.empty())
43     {
44         m_errorStr = "Parameter \'Secret\' not found.";
45         printfd(__FILE__, "Parameter 'Secret' not found\n");
46         m_secret = "";
47     }
48     else
49     {
50         m_secret = pvi->value[0];
51     }
52
53     pv.param = "Dictionaries";
54     pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
55     if (pvi != s.moduleParams.end() && !pvi->value.empty())
56         m_dictionaries = pvi->value[0];
57     return 0;
58 }
59
60 RADIUS::RADIUS()
61     : m_running(false),
62       m_logger(PluginLogger::get("radius"))
63 {
64 }
65
66 int RADIUS::ParseSettings()
67 {
68     auto ret = m_radSettings.ParseSettings(m_settings);
69     if (ret != 0)
70         m_errorStr = m_radSettings.GetStrError();
71
72     return ret;
73 }
74
75 std::string RADIUS::GetVersion() const
76 {
77     return "Radius v.1.0";
78 }
79
80 int RADIUS::Start()
81 {
82     m_thread = std::jthread([this](auto token){ Run(std::move(token)); });
83     return 0;
84 }
85
86 int RADIUS::Stop()
87 {
88     if (!m_thread.joinable())
89         return 0;
90
91     m_thread.request_stop();
92
93     if (m_server)
94         m_server->stop();
95
96     m_thread.join();
97     return 0;
98 }
99
100 bool RADIUS::IsRunning()
101 {
102     const std::lock_guard lock(m_mutex);
103     return m_running;
104 }
105
106 void RADIUS::SetRunning(bool val)
107 {
108     const std::lock_guard lock(m_mutex);
109     m_running = val;
110 }
111
112 int RADIUS::Run(std::stop_token token)
113 {
114     SetRunning(true);
115
116     try
117     {
118         if (!m_server)
119            m_server = std::make_unique<Server>(m_ioService, m_radSettings.GetSecret(), m_radSettings.GetPort(), m_radSettings.GetDictionaries(), std::move(token), m_logger);
120         m_ioService.run();
121     }
122     catch (const std::exception& e)
123     {
124         m_errorStr = "Exception in RADIUS::Run(): " + std::string(e.what());
125         m_logger("Exception in RADIUS:: Run(): %s", e.what());
126         printfd(__FILE__, "Exception in RADIUS:: Run(). Message: '%s'\n", e.what());
127     }
128
129     SetRunning(false);
130     return 0;
131 }