]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/other/radius/config.cpp
Implemented backend for rlm_stg.
[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         dest.push_back(value[start++]);
69     if (dest.empty()) {
70         if (start == value.length())
71             throw ParserError(start, "Unexpected end of string. Expected string.");
72         else
73             throw ParserError(start, "Unexpected whitespace. Expected string.");
74     }
75     return std::make_pair(start, dest);
76 }
77
78 Config::Pairs toPairs(const std::vector<std::string>& values)
79 {
80     if (values.empty())
81         return Config::Pairs();
82     std::string value(values[0]);
83     Config::Pairs res;
84     size_t start = 0;
85     while (start < value.size()) {
86         Config::Pair pair;
87         start = skipSpaces(value, start);
88         size_t pairStart = start;
89         start = checkChar(value, start, '(');
90         std::pair<size_t, std::string> key = readString(value, start);
91         start = key.first;
92         pair.first = key.second;
93         start = skipSpaces(value, start);
94         start = checkChar(value, start, ',');
95         start = skipSpaces(value, start);
96         std::pair<size_t, std::string> val = readString(value, start);
97         start = key.first;
98         pair.second = val.second;
99         start = skipSpaces(value, start);
100         start = checkChar(value, start, ')');
101         if (res.find(pair.first) != res.end())
102             throw ParserError(pairStart, "Duplicate field.");
103         res.insert(pair);
104     }
105     return res;
106 }
107
108 bool toBool(const std::vector<std::string>& values)
109 {
110     if (values.empty())
111         return false;
112     std::string value(values[0]);
113     return strncasecmp(value.c_str(), "yes", 3) == 0;
114 }
115
116 std::string toString(const std::vector<std::string>& values)
117 {
118     if (values.empty())
119         return "";
120     return values[0];
121 }
122
123 template <typename T>
124 T toInt(const std::vector<std::string>& values)
125 {
126     if (values.empty())
127         return 0;
128     T res = 0;
129     if (str2x(values[0], res) == 0)
130         return res;
131     return 0;
132 }
133
134 Config::Pairs parseVector(const std::string& paramName, const MODULE_SETTINGS& params)
135 {
136     for (size_t i = 0; i < params.moduleParams.size(); ++i)
137         if (params.moduleParams[i].param == paramName)
138             return toPairs(params.moduleParams[i].value);
139     return Config::Pairs();
140 }
141
142 bool parseBool(const std::string& paramName, const MODULE_SETTINGS& params)
143 {
144     for (size_t i = 0; i < params.moduleParams.size(); ++i)
145         if (params.moduleParams[i].param == paramName)
146             return toBool(params.moduleParams[i].value);
147     return false;
148 }
149
150 std::string parseString(const std::string& paramName, const MODULE_SETTINGS& params)
151 {
152     for (size_t i = 0; i < params.moduleParams.size(); ++i)
153         if (params.moduleParams[i].param == paramName)
154             return toString(params.moduleParams[i].value);
155     return "";
156 }
157
158 template <typename T>
159 T parseInt(const std::string& paramName, const MODULE_SETTINGS& params)
160 {
161     for (size_t i = 0; i < params.moduleParams.size(); ++i)
162         if (params.moduleParams[i].param == paramName)
163             return toInt<T>(params.moduleParams[i].value);
164     return 0;
165 }
166
167 } // namespace anonymous
168
169 Config::Config(const MODULE_SETTINGS& settings)
170     : match(parseVector("match", settings)),
171       modify(parseVector("modify", settings)),
172       reply(parseVector("reply", settings)),
173       verbose(parseBool("verbose", settings)),
174       bindAddress(parseString("bind_address", settings)),
175       portStr(parseString("port", settings)),
176       port(parseInt<uint16_t>("port", settings)),
177       key(parseString("key", settings))
178 {
179 }