]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/other/radius/config.cpp
Parse TCP port correctly.
[stg.git] / projects / stargazer / plugins / other / radius / config.cpp
index 8e9f27a9fab9d99121235efb67627b040b69e5dc..6742f8111e1be21a732818908a2093a5a5fe63ef 100644 (file)
@@ -126,6 +126,36 @@ std::string toString(const std::vector<std::string>& values)
     return values[0];
 }
 
+uid_t toUID(const std::vector<std::string>& values)
+{
+    if (values.empty())
+        return -1;
+    uid_t res = str2uid(values[0]);
+    if (res == static_cast<uid_t>(-1))
+        throw ParserError(0, "Invalid user name: '" + values[0] + "'");
+    return res;
+}
+
+gid_t toGID(const std::vector<std::string>& values)
+{
+    if (values.empty())
+        return -1;
+    gid_t res = str2gid(values[0]);
+    if (res == static_cast<gid_t>(-1))
+        throw ParserError(0, "Invalid group name: '" + values[0] + "'");
+    return res;
+}
+
+mode_t toMode(const std::vector<std::string>& values)
+{
+    if (values.empty())
+        return -1;
+    mode_t res = str2mode(values[0]);
+    if (res == static_cast<mode_t>(-1))
+        throw ParserError(0, "Invalid mode: '" + values[0] + "'");
+    return res;
+}
+
 template <typename T>
 T toInt(const std::vector<std::string>& values)
 {
@@ -137,6 +167,44 @@ T toInt(const std::vector<std::string>& values)
     return 0;
 }
 
+uint16_t toPort(const std::string& value)
+{
+    uint16_t res = 0;
+    if (str2x(value, res) == 0)
+        return res;
+    throw ParserError(0, "'" + value + "' is not a valid port number.");
+}
+
+typedef std::map<std::string, Config::ReturnCode> Codes;
+
+// One-time call to initialize the list of codes.
+Codes getCodes()
+{
+    Codes res;
+    res["reject"]   = Config::REJECT;
+    res["fail"]     = Config::FAIL;
+    res["ok"]       = Config::OK;
+    res["handled"]  = Config::HANDLED;
+    res["invalid"]  = Config::INVALID;
+    res["userlock"] = Config::USERLOCK;
+    res["notfound"] = Config::NOTFOUND;
+    res["noop"]     = Config::NOOP;
+    res["updated"]  = Config::UPDATED;
+    return res;
+}
+
+Config::ReturnCode toReturnCode(const std::vector<std::string>& values)
+{
+    static Codes codes(getCodes());
+    if (values.empty())
+        return Config::REJECT;
+    std::string code = ToLower(values[0]);
+    const Codes::const_iterator it = codes.find(code);
+    if (it == codes.end())
+        return Config::REJECT;
+    return it->second;
+}
+
 Config::Pairs parseVector(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
 {
     for (size_t i = 0; i < params.size(); ++i)
@@ -145,6 +213,14 @@ Config::Pairs parseVector(const std::string& paramName, const std::vector<PARAM_
     return Config::Pairs();
 }
 
+Config::ReturnCode parseReturnCode(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
+{
+    for (size_t i = 0; i < params.size(); ++i)
+        if (params[i].param == paramName)
+            return toReturnCode(params[i].value);
+    return Config::REJECT;
+}
+
 bool parseBool(const std::string& paramName, const std::vector<PARAM_VALUE>& params)
 {
     for (size_t i = 0; i < params.size(); ++i)
@@ -161,11 +237,31 @@ std::string parseString(const std::string& paramName, const std::vector<PARAM_VA
     return "";
 }
 
-std::string parseAddress(const std::string& address)
+std::string parseAddress(Config::Type connectionType, const std::string& value)
 {
-    size_t pos = address.find_first_of(':');
+    size_t pos = value.find_first_of(':');
+    if (pos == std::string::npos)
+        throw ParserError(0, "Connection type is not specified. Should be either 'unix' or 'tcp'.");
+    if (connectionType == Config::UNIX)
+        return value.substr(pos + 1);
+    std::string address(value.substr(pos + 1));
+    pos = address.find_first_of(':', pos + 1);
+    if (pos == std::string::npos)
+        throw ParserError(0, "Port is not specified.");
+    return address.substr(0, pos - 1);
+}
+
+std::string parsePort(Config::Type connectionType, const std::string& value)
+{
+    size_t pos = value.find_first_of(':');
     if (pos == std::string::npos)
         throw ParserError(0, "Connection type is not specified. Should be either 'unix' or 'tcp'.");
+    if (connectionType == Config::UNIX)
+        return value.substr(pos + 1);
+    std::string address(value.substr(pos + 1));
+    pos = address.find_first_of(':', pos + 1);
+    if (pos == std::string::npos)
+        throw ParserError(0, "Port is not specified.");
     return address.substr(pos + 1);
 }
 
@@ -188,10 +284,35 @@ Config::Section parseSection(const std::string& paramName, const std::vector<PAR
         if (params[i].param == paramName)
             return Config::Section(parseVector("match", params[i].sections),
                                    parseVector("modify", params[i].sections),
-                                   parseVector("reply", params[i].sections));
+                                   parseVector("reply", params[i].sections),
+                                   parseReturnCode("no_match", params[i].sections));
     return Config::Section();
 }
 
+uid_t parseUID(const std::string& paramName, const std::vector<PARAM_VALUE>& 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<PARAM_VALUE>& 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<PARAM_VALUE>& 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)
@@ -202,8 +323,13 @@ Config::Config(const MODULE_SETTINGS& settings)
       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))
+      bindAddress(parseAddress(connectionType, address)),
+      portStr(parsePort(connectionType, address)),
+      port(toPort(portStr)),
+      key(parseString("key", settings.moduleParams)),
+      sockUID(parseUID("sock_owner", settings.moduleParams)),
+      sockGID(parseGID("sock_group", settings.moduleParams)),
+      sockMode(parseMode("sock_mode", settings.moduleParams))
 {
 }