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