]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/radius/config.cpp
Merge branch 'stg-2.409' into stg-2.409-radius
[stg.git] / projects / stargazer / plugins / other / radius / config.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #include "config.h"
22
23 #include "stg/common.h"
24
25 #include <vector>
26 #include <stdexcept>
27
28 #include <strings.h> // strncasecmp
29
30 using STG::Config;
31
32 namespace
33 {
34
35 struct ParserError : public std::runtime_error
36 {
37     ParserError(size_t pos, const std::string& message)
38         : runtime_error("Parsing error at position " + x2str(pos) + ". " + message),
39           position(pos),
40           error(message)
41     {}
42     virtual ~ParserError() throw() {}
43
44     size_t position;
45     std::string error;
46 };
47
48 size_t skipSpaces(const std::string& value, size_t start)
49 {
50     while (start < value.length() && std::isspace(value[start]))
51         ++start;
52     return start;
53 }
54
55 size_t checkChar(const std::string& value, size_t start, char ch)
56 {
57     if (start >= value.length())
58         throw ParserError(start, "Unexpected end of string. Expected '" + std::string(1, ch) + "'.");
59     if (value[start] != ch)
60         throw ParserError(start, "Expected '" + std::string(1, ch) + "', got '" + std::string(1, value[start]) + "'.");
61     return start + 1;
62 }
63
64 std::pair<size_t, std::string> readString(const std::string& value, size_t start)
65 {
66     std::string dest;
67     while (start < value.length() && !std::isspace(value[start]) &&
68            value[start] != ',' && value[start] != '(' && value[start] != ')')
69         dest.push_back(value[start++]);
70     if (dest.empty()) {
71         if (start == value.length())
72             throw ParserError(start, "Unexpected end of string. Expected string.");
73         else
74             throw ParserError(start, "Unexpected whitespace. Expected string.");
75     }
76     return std::make_pair(start, dest);
77 }
78
79 Config::Pairs toPairs(const std::vector<std::string>& values)
80 {
81     if (values.empty())
82         return Config::Pairs();
83     std::string value(values[0]);
84     Config::Pairs res;
85     size_t start = 0;
86     while (start < value.size()) {
87         Config::Pair pair;
88         start = skipSpaces(value, start);
89         if (!res.empty())
90         {
91             start = checkChar(value, start, ',');
92             start = skipSpaces(value, start);
93         }
94         size_t pairStart = start;
95         start = checkChar(value, start, '(');
96         const std::pair<size_t, std::string> key = readString(value, start);
97         start = key.first;
98         pair.first = key.second;
99         start = skipSpaces(value, start);
100         start = checkChar(value, start, ',');
101         start = skipSpaces(value, start);
102         const std::pair<size_t, std::string> val = readString(value, start);
103         start = val.first;
104         pair.second = val.second;
105         start = skipSpaces(value, start);
106         start = checkChar(value, start, ')');
107         if (res.find(pair.first) != res.end())
108             throw ParserError(pairStart, "Duplicate field.");
109         res.insert(pair);
110     }
111     return res;
112 }
113
114 bool toBool(const std::vector<std::string>& values)
115 {
116     if (values.empty())
117         return false;
118     std::string value(values[0]);
119     return strncasecmp(value.c_str(), "yes", 3) == 0;
120 }
121
122 std::string toString(const std::vector<std::string>& values)
123 {
124     if (values.empty())
125         return "";
126     return values[0];
127 }
128
129 uid_t toUID(const std::vector<std::string>& values)
130 {
131     if (values.empty())
132         return -1;
133     uid_t res = str2uid(values[0]);
134     if (res == static_cast<uid_t>(-1))
135         throw ParserError(0, "Invalid user name: '" + values[0] + "'");
136     return res;
137 }
138
139 gid_t toGID(const std::vector<std::string>& values)
140 {
141     if (values.empty())
142         return -1;
143     gid_t res = str2gid(values[0]);
144     if (res == static_cast<gid_t>(-1))
145         throw ParserError(0, "Invalid group name: '" + values[0] + "'");
146     return res;
147 }
148
149 mode_t toMode(const std::vector<std::string>& values)
150 {
151     if (values.empty())
152         return -1;
153     mode_t res = str2mode(values[0]);
154     if (res == static_cast<mode_t>(-1))
155         throw ParserError(0, "Invalid mode: '" + values[0] + "'");
156     return res;
157 }
158
159 template <typename T>
160 T toInt(const std::vector<std::string>& values)
161 {
162     if (values.empty())
163         return 0;
164     T res = 0;
165     if (str2x(values[0], res) == 0)
166         return res;
167     return 0;
168 }
169
170 uint16_t toPort(const std::string& value)
171 {
172     if (value.empty())
173         return 0;
174     uint16_t res = 0;
175     if (str2x(value, res) == 0)
176         return res;
177     throw ParserError(0, "'" + value + "' is not a valid port number.");
178 }
179
180 typedef std::map<std::string, Config::ReturnCode> Codes;
181
182 // One-time call to initialize the list of codes.
183 Codes getCodes()
184 {
185     Codes res;
186     res["reject"]   = Config::REJECT;
187     res["fail"]     = Config::FAIL;
188     res["ok"]       = Config::OK;
189     res["handled"]  = Config::HANDLED;
190     res["invalid"]  = Config::INVALID;
191     res["userlock"] = Config::USERLOCK;
192     res["notfound"] = Config::NOTFOUND;
193     res["noop"]     = Config::NOOP;
194     res["updated"]  = Config::UPDATED;
195     return res;
196 }
197
198 Config::ReturnCode toReturnCode(const std::vector<std::string>& values)
199 {
200     static Codes codes(getCodes());
201     if (values.empty())
202         return Config::REJECT;
203     std::string code = ToLower(values[0]);
204     const Codes::const_iterator it = codes.find(code);
205     if (it == codes.end())
206         return Config::REJECT;
207     return it->second;
208 }
209
210 Config::Pairs parseVector(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
211 {
212     for (size_t i = 0; i < params.size(); ++i)
213         if (params[i].param == paramName)
214             return toPairs(params[i].value);
215     return Config::Pairs();
216 }
217
218 Config::ReturnCode parseReturnCode(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
219 {
220     for (size_t i = 0; i < params.size(); ++i)
221         if (params[i].param == paramName)
222             return toReturnCode(params[i].value);
223     return Config::REJECT;
224 }
225
226 bool parseBool(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
227 {
228     for (size_t i = 0; i < params.size(); ++i)
229         if (params[i].param == paramName)
230             return toBool(params[i].value);
231     return false;
232 }
233
234 std::string parseString(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
235 {
236     for (size_t i = 0; i < params.size(); ++i)
237         if (params[i].param == paramName)
238             return toString(params[i].value);
239     return "";
240 }
241
242 std::string parseAddress(Config::Type connectionType, const std::string& value)
243 {
244     size_t pos = value.find_first_of(':');
245     if (pos == std::string::npos)
246         throw ParserError(0, "Connection type is not specified. Should be either 'unix' or 'tcp'.");
247     if (connectionType == Config::UNIX)
248         return value.substr(pos + 1);
249     std::string address(value.substr(pos + 1));
250     pos = address.find_first_of(':', pos + 1);
251     if (pos == std::string::npos)
252         throw ParserError(0, "Port is not specified.");
253     return address.substr(0, pos - 1);
254 }
255
256 std::string parsePort(Config::Type connectionType, const std::string& value)
257 {
258     size_t pos = value.find_first_of(':');
259     if (pos == std::string::npos)
260         throw ParserError(0, "Connection type is not specified. Should be either 'unix' or 'tcp'.");
261     if (connectionType == Config::UNIX)
262         return "";
263     std::string address(value.substr(pos + 1));
264     pos = address.find_first_of(':', pos + 1);
265     if (pos == std::string::npos)
266         throw ParserError(0, "Port is not specified.");
267     return address.substr(pos + 1);
268 }
269
270 Config::Type parseConnectionType(const std::string& address)
271 {
272     size_t pos = address.find_first_of(':');
273     if (pos == std::string::npos)
274         throw ParserError(0, "Connection type is not specified. Should be either 'unix' or 'tcp'.");
275     std::string type = ToLower(address.substr(0, pos));
276     if (type == "unix")
277         return Config::UNIX;
278     else if (type == "tcp")
279         return Config::TCP;
280     throw ParserError(0, "Invalid connection type. Should be either 'unix' or 'tcp', got '" + type + "'");
281 }
282
283 Config::Section parseSection(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
284 {
285     for (size_t i = 0; i < params.size(); ++i)
286         if (params[i].param == paramName)
287             return Config::Section(parseVector("match", params[i].sections),
288                                    parseVector("modify", params[i].sections),
289                                    parseVector("reply", params[i].sections),
290                                    parseReturnCode("no_match", params[i].sections));
291     return Config::Section();
292 }
293
294 uid_t parseUID(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
295 {
296     for (size_t i = 0; i < params.size(); ++i)
297         if (params[i].param == paramName)
298             return toUID(params[i].value);
299     return -1;
300 }
301
302 gid_t parseGID(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
303 {
304     for (size_t i = 0; i < params.size(); ++i)
305         if (params[i].param == paramName)
306             return toGID(params[i].value);
307     return -1;
308 }
309
310 mode_t parseMode(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
311 {
312     for (size_t i = 0; i < params.size(); ++i)
313         if (params[i].param == paramName)
314             return toMode(params[i].value);
315     return -1;
316 }
317
318 } // namespace anonymous
319
320 Config::Config(const MODULE_SETTINGS& settings)
321     : autz(parseSection("autz", settings.moduleParams)),
322       auth(parseSection("auth", settings.moduleParams)),
323       postauth(parseSection("postauth", settings.moduleParams)),
324       preacct(parseSection("preacct", settings.moduleParams)),
325       acct(parseSection("acct", settings.moduleParams)),
326       verbose(parseBool("verbose", settings.moduleParams)),
327       address(parseString("bind_address", settings.moduleParams)),
328       connectionType(parseConnectionType(address)),
329       bindAddress(parseAddress(connectionType, address)),
330       portStr(parsePort(connectionType, address)),
331       port(toPort(portStr)),
332       key(parseString("key", settings.moduleParams)),
333       sockUID(parseUID("sock_owner", settings.moduleParams)),
334       sockGID(parseGID("sock_group", settings.moduleParams)),
335       sockMode(parseMode("sock_mode", settings.moduleParams))
336 {
337 }