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.
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.
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
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
23 #include "stg/common.h"
28 #include <strings.h> // strncasecmp
35 struct ParserError : public std::runtime_error
37 ParserError(size_t pos, const std::string& message)
38 : runtime_error("Parsing error at position " + x2str(pos) + ". " + message),
42 virtual ~ParserError() throw() {}
48 size_t skipSpaces(const std::string& value, size_t start)
50 while (start < value.length() && std::isspace(value[start]))
55 size_t checkChar(const std::string& value, size_t start, char ch)
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]) + "'.");
64 std::pair<size_t, std::string> readString(const std::string& value, size_t start)
67 while (start < value.length() && !std::isspace(value[start]) &&
68 value[start] != ',' && value[start] != '(' && value[start] != ')')
69 dest.push_back(value[start++]);
71 if (start == value.length())
72 throw ParserError(start, "Unexpected end of string. Expected string.");
74 throw ParserError(start, "Unexpected whitespace. Expected string.");
76 return std::make_pair(start, dest);
79 Config::Pairs toPairs(const std::vector<std::string>& values)
82 return Config::Pairs();
83 std::string value(values[0]);
86 while (start < value.size()) {
88 start = skipSpaces(value, start);
91 start = checkChar(value, start, ',');
92 start = skipSpaces(value, start);
94 size_t pairStart = start;
95 start = checkChar(value, start, '(');
96 const std::pair<size_t, std::string> key = readString(value, start);
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);
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.");
114 bool toBool(const std::vector<std::string>& values)
118 std::string value(values[0]);
119 return strncasecmp(value.c_str(), "yes", 3) == 0;
122 std::string toString(const std::vector<std::string>& values)
129 uid_t toUID(const std::vector<std::string>& values)
133 uid_t res = str2uid(values[0]);
134 if (res == static_cast<uid_t>(-1))
135 throw ParserError(0, "Invalid user name: '" + values[0] + "'");
139 gid_t toGID(const std::vector<std::string>& values)
143 gid_t res = str2gid(values[0]);
144 if (res == static_cast<gid_t>(-1))
145 throw ParserError(0, "Invalid group name: '" + values[0] + "'");
149 mode_t toMode(const std::vector<std::string>& values)
153 mode_t res = str2mode(values[0]);
154 if (res == static_cast<mode_t>(-1))
155 throw ParserError(0, "Invalid mode: '" + values[0] + "'");
159 template <typename T>
160 T toInt(const std::vector<std::string>& values)
165 if (str2x(values[0], res) == 0)
170 uint16_t toPort(const std::string& value)
173 if (str2x(value, res) == 0)
175 throw ParserError(0, "'" + value + "' is not a valid port number.");
178 typedef std::map<std::string, Config::ReturnCode> Codes;
180 // One-time call to initialize the list of codes.
184 res["reject"] = Config::REJECT;
185 res["fail"] = Config::FAIL;
186 res["ok"] = Config::OK;
187 res["handled"] = Config::HANDLED;
188 res["invalid"] = Config::INVALID;
189 res["userlock"] = Config::USERLOCK;
190 res["notfound"] = Config::NOTFOUND;
191 res["noop"] = Config::NOOP;
192 res["updated"] = Config::UPDATED;
196 Config::ReturnCode toReturnCode(const std::vector<std::string>& values)
198 static Codes codes(getCodes());
200 return Config::REJECT;
201 std::string code = ToLower(values[0]);
202 const Codes::const_iterator it = codes.find(code);
203 if (it == codes.end())
204 return Config::REJECT;
208 Config::Pairs parseVector(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
210 for (size_t i = 0; i < params.size(); ++i)
211 if (params[i].param == paramName)
212 return toPairs(params[i].value);
213 return Config::Pairs();
216 Config::ReturnCode parseReturnCode(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
218 for (size_t i = 0; i < params.size(); ++i)
219 if (params[i].param == paramName)
220 return toReturnCode(params[i].value);
221 return Config::REJECT;
224 bool parseBool(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
226 for (size_t i = 0; i < params.size(); ++i)
227 if (params[i].param == paramName)
228 return toBool(params[i].value);
232 std::string parseString(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
234 for (size_t i = 0; i < params.size(); ++i)
235 if (params[i].param == paramName)
236 return toString(params[i].value);
240 std::string parseAddress(Config::Type connectionType, const std::string& value)
242 size_t pos = value.find_first_of(':');
243 if (pos == std::string::npos)
244 throw ParserError(0, "Connection type is not specified. Should be either 'unix' or 'tcp'.");
245 if (connectionType == Config::UNIX)
246 return value.substr(pos + 1);
247 std::string address(value.substr(pos + 1));
248 pos = address.find_first_of(':', pos + 1);
249 if (pos == std::string::npos)
250 throw ParserError(0, "Port is not specified.");
251 return address.substr(0, pos - 1);
254 std::string parsePort(Config::Type connectionType, const std::string& value)
256 size_t pos = value.find_first_of(':');
257 if (pos == std::string::npos)
258 throw ParserError(0, "Connection type is not specified. Should be either 'unix' or 'tcp'.");
259 if (connectionType == Config::UNIX)
260 return value.substr(pos + 1);
261 std::string address(value.substr(pos + 1));
262 pos = address.find_first_of(':', pos + 1);
263 if (pos == std::string::npos)
264 throw ParserError(0, "Port is not specified.");
265 return address.substr(pos + 1);
268 Config::Type parseConnectionType(const std::string& address)
270 size_t pos = address.find_first_of(':');
271 if (pos == std::string::npos)
272 throw ParserError(0, "Connection type is not specified. Should be either 'unix' or 'tcp'.");
273 std::string type = ToLower(address.substr(0, pos));
276 else if (type == "tcp")
278 throw ParserError(0, "Invalid connection type. Should be either 'unix' or 'tcp', got '" + type + "'");
281 Config::Section parseSection(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
283 for (size_t i = 0; i < params.size(); ++i)
284 if (params[i].param == paramName)
285 return Config::Section(parseVector("match", params[i].sections),
286 parseVector("modify", params[i].sections),
287 parseVector("reply", params[i].sections),
288 parseReturnCode("no_match", params[i].sections));
289 return Config::Section();
292 uid_t parseUID(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
294 for (size_t i = 0; i < params.size(); ++i)
295 if (params[i].param == paramName)
296 return toUID(params[i].value);
300 gid_t parseGID(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
302 for (size_t i = 0; i < params.size(); ++i)
303 if (params[i].param == paramName)
304 return toGID(params[i].value);
308 mode_t parseMode(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
310 for (size_t i = 0; i < params.size(); ++i)
311 if (params[i].param == paramName)
312 return toMode(params[i].value);
316 } // namespace anonymous
318 Config::Config(const MODULE_SETTINGS& settings)
319 : autz(parseSection("autz", settings.moduleParams)),
320 auth(parseSection("auth", settings.moduleParams)),
321 postauth(parseSection("postauth", settings.moduleParams)),
322 preacct(parseSection("preacct", settings.moduleParams)),
323 acct(parseSection("acct", settings.moduleParams)),
324 verbose(parseBool("verbose", settings.moduleParams)),
325 address(parseString("bind_address", settings.moduleParams)),
326 connectionType(parseConnectionType(address)),
327 bindAddress(parseAddress(connectionType, address)),
328 portStr(parsePort(connectionType, address)),
329 port(toPort(portStr)),
330 key(parseString("key", settings.moduleParams)),
331 sockUID(parseUID("sock_owner", settings.moduleParams)),
332 sockGID(parseGID("sock_group", settings.moduleParams)),
333 sockMode(parseMode("sock_mode", settings.moduleParams))