X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/1538d6276533140505fddb71c99a0bafe6ca9182..d6e4a058a37bdaea7df8c8d360978c0dc8848fff:/projects/stargazer/plugins/other/radius/config.cpp diff --git a/projects/stargazer/plugins/other/radius/config.cpp b/projects/stargazer/plugins/other/radius/config.cpp index 8a90567d..187620dd 100644 --- a/projects/stargazer/plugins/other/radius/config.cpp +++ b/projects/stargazer/plugins/other/radius/config.cpp @@ -39,6 +39,7 @@ struct ParserError : public std::runtime_error position(pos), error(message) {} + virtual ~ParserError() throw() {} size_t position; std::string error; @@ -51,19 +52,20 @@ size_t skipSpaces(const std::string& value, size_t start) return start; } -size_t checkChar(const std:string& value, size_t start, char ch) +size_t checkChar(const std::string& value, size_t start, char ch) { if (start >= value.length()) - throw ParserError(start, "Unexpected end of string. Expected '" + std::string(ch) + "'."); + throw ParserError(start, "Unexpected end of string. Expected '" + std::string(1, ch) + "'."); if (value[start] != ch) - throw ParserError(start, "Expected '" + std::string(ch) + "', got '" + std::string(value[start]) + "'."); + throw ParserError(start, "Expected '" + std::string(1, ch) + "', got '" + std::string(1, value[start]) + "'."); return start + 1; } std::pair readString(const std::string& value, size_t start) { std::string dest; - while (start < value.length() && !std::isspace(value[start])) + while (start < value.length() && !std::isspace(value[start]) && + value[start] != ',' && value[start] != '(' && value[start] != ')') dest.push_back(value[start++]); if (dest.empty()) { if (start == value.length()) @@ -71,7 +73,7 @@ std::pair readString(const std::string& value, size_t start else throw ParserError(start, "Unexpected whitespace. Expected string."); } - return dest; + return std::make_pair(start, dest); } Config::Pairs toPairs(const std::vector& values) @@ -84,17 +86,22 @@ Config::Pairs toPairs(const std::vector& values) while (start < value.size()) { Config::Pair pair; start = skipSpaces(value, start); + if (!res.empty()) + { + start = checkChar(value, start, ','); + start = skipSpaces(value, start); + } size_t pairStart = start; start = checkChar(value, start, '('); - std::pair key = readString(value, start); + const std::pair key = readString(value, start); start = key.first; pair.first = key.second; start = skipSpaces(value, start); - start = checkChar(value, start, ',') + start = checkChar(value, start, ','); start = skipSpaces(value, start); - std::pair value = readString(value, start); - start = key.first; - pair.second = value.second; + const std::pair val = readString(value, start); + start = val.first; + pair.second = val.second; start = skipSpaces(value, start); start = checkChar(value, start, ')'); if (res.find(pair.first) != res.end()) @@ -119,59 +126,141 @@ std::string toString(const std::vector& values) return values[0]; } +uid_t toUID(const std::vector& values) +{ + if (values.empty()) + return -1; + uid_t res = str2uid(values[0]); + if (res == static_cast(-1)) + throw ParserError(0, "Invalid user name: '" + values[0] + "'"); + return res; +} + +gid_t toGID(const std::vector& values) +{ + if (values.empty()) + return -1; + gid_t res = str2gid(values[0]); + if (res == static_cast(-1)) + throw ParserError(0, "Invalid group name: '" + values[0] + "'"); + return res; +} + +mode_t toMode(const std::vector& values) +{ + if (values.empty()) + return -1; + mode_t res = str2mode(values[0]); + if (res == static_cast(-1)) + throw ParserError(0, "Invalid mode: '" + values[0] + "'"); + return res; +} + template T toInt(const std::vector& values) { if (values.empty()) return 0; T res = 0; - if (srt2x(values[0], res) == 0) + if (str2x(values[0], res) == 0) return res; return 0; } -Config::Pairs parseVector(const std::string& paramName, const MODULE_SETTINGS& params) +Config::Pairs parseVector(const std::string& paramName, const std::vector& params) { - for (size_t i = 0; i < params.moduleParams.size(); ++i) - if (params.moduleParams[i].first == paramName) - return toPairs(params.moduleParams[i].second); + for (size_t i = 0; i < params.size(); ++i) + if (params[i].param == paramName) + return toPairs(params[i].value); return Config::Pairs(); } -bool parseBool(const std::string& paramName, const MODULE_SETTINGS& params) +bool parseBool(const std::string& paramName, const std::vector& params) { - for (size_t i = 0; i < params.moduleParams.size(); ++i) - if (params.moduleParams[i].first == paramName) - return toBool(params.moduleParams[i].second); + for (size_t i = 0; i < params.size(); ++i) + if (params[i].param == paramName) + return toBool(params[i].value); return false; } -std::string parseString(const std::string& paramName, const MODULE_SETTINGS& params) +std::string parseString(const std::string& paramName, const std::vector& params) { - for (size_t i = 0; i < params.moduleParams.size(); ++i) - if (params.moduleParams[i].first == paramName) - return toString(params.moduleParams[i].second); + for (size_t i = 0; i < params.size(); ++i) + if (params[i].param == paramName) + return toString(params[i].value); return ""; } -template -T parseInt(const std::string& paramName, const MODULE_SETTINGS& params) +std::string parseAddress(const std::string& address) { - for (size_t i = 0; i < params.moduleParams.size(); ++i) - if (params.moduleParams[i].first == paramName) - return toInt(params.moduleParams[i].second); - return 0; + size_t pos = address.find_first_of(':'); + if (pos == std::string::npos) + throw ParserError(0, "Connection type is not specified. Should be either 'unix' or 'tcp'."); + return address.substr(pos + 1); +} + +Config::Type parseConnectionType(const std::string& address) +{ + size_t pos = address.find_first_of(':'); + if (pos == std::string::npos) + throw ParserError(0, "Connection type is not specified. Should be either 'unix' or 'tcp'."); + std::string type = ToLower(address.substr(0, pos)); + if (type == "unix") + return Config::UNIX; + else if (type == "tcp") + return Config::TCP; + throw ParserError(0, "Invalid connection type. Should be either 'unix' or 'tcp', got '" + type + "'"); +} + +Config::Section parseSection(const std::string& paramName, const std::vector& params) +{ + for (size_t i = 0; i < params.size(); ++i) + if (params[i].param == paramName) + return Config::Section(parseVector("match", params[i].sections), + parseVector("modify", params[i].sections), + parseVector("reply", params[i].sections)); + return Config::Section(); +} + +uid_t parseUID(const std::string& paramName, const std::vector& params) +{ + for (size_t i = 0; i < params.size(); ++i) + if (params[i].param == paramName) + return toUID(params[i].value); + return -1; +} + +gid_t parseGID(const std::string& paramName, const std::vector& params) +{ + for (size_t i = 0; i < params.size(); ++i) + if (params[i].param == paramName) + return toGID(params[i].value); + return -1; +} + +mode_t parseMode(const std::string& paramName, const std::vector& params) +{ + for (size_t i = 0; i < params.size(); ++i) + if (params[i].param == paramName) + return toMode(params[i].value); + return -1; } } // namespace anonymous Config::Config(const MODULE_SETTINGS& settings) - : match(parseVector("match", settings)), - modify(parseVector("modify", settings)), - reply(parseVector("reply", settings)), - verbose(parseBool("verbose", settings)), - bindAddress(parseString("bind_address", settings)), - port(parseInt("port", settings)), - key(parseString("key", settings)) + : autz(parseSection("autz", settings.moduleParams)), + auth(parseSection("auth", settings.moduleParams)), + postauth(parseSection("postauth", settings.moduleParams)), + preacct(parseSection("preacct", settings.moduleParams)), + acct(parseSection("acct", settings.moduleParams)), + verbose(parseBool("verbose", settings.moduleParams)), + address(parseString("bind_address", settings.moduleParams)), + bindAddress(parseAddress(address)), + connectionType(parseConnectionType(address)), + key(parseString("key", settings.moduleParams)), + sockUID(parseUID("sock_owner", settings.moduleParams)), + sockGID(parseGID("sock_group", settings.moduleParams)), + sockMode(parseMode("sock_mode", settings.moduleParams)) { }