2 #include "radproto/error.h"
3 #include "stg/common.h"
4 #include <boost/tokenizer.hpp>
5 #include <boost/algorithm/string.hpp>
14 using AttrValue = Config::AttrValue;
15 using ASection = Config::ASection;
19 std::string ShowRules(const std::vector<std::pair<std::string, AttrValue>>& rules)
22 for (const auto& at : rules)
27 if (at.second.type == AttrValue::Type::PARAM_NAME)
28 result.append(at.first + " = " + at.second.value);
30 result.append(at.first + " = '" + at.second.value + "'");
36 std::vector<std::pair<std::string, AttrValue>> Config::ParseRules(const std::string& value, const std::string& paramName)
38 using tokenizer = boost::tokenizer<boost::char_separator<char>>;
39 const boost::char_separator<char> sep(",");
41 const tokenizer tokens(value, sep);
43 std::vector<std::pair<std::string, AttrValue>> res;
45 for (const auto& token : tokens)
47 std::vector<std::string> keyValue;
49 split(keyValue, boost::algorithm::trim_copy_if(token, boost::is_any_of(" \t")), boost::is_any_of(" ="), boost::token_compress_on);
51 if (keyValue.size() != 2)
53 m_logger("The '%s' attribute specification has an incorrect format: '%s'.", paramName.c_str(), token.c_str());
54 printfd(__FILE__, "The '%s' attribute specification has an incorrect format: '%s'.", paramName.c_str(), token.c_str());
58 auto type = AttrValue::Type::PARAM_NAME;
59 std::string valueName = keyValue[1];
60 if (valueName.front() == '\'' && valueName.back() == '\'')
62 type = AttrValue::Type::VALUE;
63 valueName.erase(0, 1);
64 valueName.erase(valueName.length() - 1, 1);
66 else if ((valueName.front() == '\'' && valueName.back() != '\'') || (valueName.front() != '\'' && valueName.back() == '\''))
68 m_logger("Error ParseRules: '%s' attribute parameter value is invalid.\n", paramName.c_str());
69 printfd(__FILE__, "Error ParseRules: '%s' attribute parameter value is invalid.\n", paramName.c_str());
72 res.emplace_back(keyValue[0], AttrValue{valueName, type});
77 ASection Config::parseASection(const std::vector<ParamValue>& conf)
80 const auto mit = std::find(conf.begin(), conf.end(), ParamValue("match", {}));
81 if (mit != conf.end())
82 res.match = ParseRules(mit->value[0], mit->param);
84 const auto sit = std::find(conf.begin(), conf.end(), ParamValue("send", {}));
85 if (sit != conf.end())
86 res.send = ParseRules(sit->value[0], sit->param);
93 m_dictionaries("/usr/share/freeradius/dictionary"),
94 m_logger(PluginLogger::get("radius"))
97 int Config::ParseSettings(const ModuleSettings & s)
103 auto pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
104 if (pvi != s.moduleParams.end() && !pvi->value.empty())
106 if (ParseIntInRange(pvi->value[0], 2, 65535, &p) != 0)
108 m_errorStr = "Cannot parse parameter \'Port\': " + m_errorStr;
109 printfd(__FILE__, "Cannot parse parameter 'Port'\n");
112 m_port = static_cast<uint16_t>(p);
116 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
117 if (pvi == s.moduleParams.end() || pvi->value.empty())
119 m_errorStr = "Parameter \'Secret\' not found.";
120 printfd(__FILE__, "Parameter 'Secret' not found\n");
124 m_secret = pvi->value[0];
126 pv.param = "Dictionaries";
127 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
128 if (pvi != s.moduleParams.end() && !pvi->value.empty())
129 m_dictionaries = pvi->value[0];
131 const auto authIt = std::find(s.moduleParams.begin(), s.moduleParams.end(), ParamValue("auth", {}));
132 if (authIt != s.moduleParams.end())
133 m_auth = parseASection(authIt->sections);
135 const auto autzIt = std::find(s.moduleParams.begin(), s.moduleParams.end(), ParamValue("autz", {}));
136 if (autzIt != s.moduleParams.end())
137 m_autz = parseASection(autzIt->sections);
139 printfd(__FILE__, " auth.match = \"%s\"\n", ShowRules(m_auth.match).c_str());
140 printfd(__FILE__, " auth.send = \"%s\"\n", ShowRules(m_auth.send).c_str());
141 printfd(__FILE__, " autz.match = \"%s\"\n", ShowRules(m_autz.match).c_str());
142 printfd(__FILE__, " autz.send = \"%s\"\n", ShowRules(m_autz.send).c_str());