From: Maxim Mamontov <faust.madf@gmail.com>
Date: Tue, 22 Sep 2015 19:51:14 +0000 (+0300)
Subject: Apply owner/group/mode settings to a UNIX socket.
X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/d6e4a058a37bdaea7df8c8d360978c0dc8848fff?ds=inline;hp=-c

Apply owner/group/mode settings to a UNIX socket.
---

d6e4a058a37bdaea7df8c8d360978c0dc8848fff
diff --git a/projects/stargazer/plugins/other/radius/config.cpp b/projects/stargazer/plugins/other/radius/config.cpp
index 8e9f27a9..187620dd 100644
--- a/projects/stargazer/plugins/other/radius/config.cpp
+++ b/projects/stargazer/plugins/other/radius/config.cpp
@@ -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)
 {
@@ -192,6 +222,30 @@ Config::Section parseSection(const std::string& paramName, const std::vector<PAR
     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)
@@ -204,6 +258,9 @@ Config::Config(const MODULE_SETTINGS& settings)
       address(parseString("bind_address", settings.moduleParams)),
       bindAddress(parseAddress(address)),
       connectionType(parseConnectionType(address)),
-      key(parseString("key", settings.moduleParams))
+      key(parseString("key", settings.moduleParams)),
+      sockUID(parseUID("sock_owner", settings.moduleParams)),
+      sockGID(parseGID("sock_group", settings.moduleParams)),
+      sockMode(parseMode("sock_mode", settings.moduleParams))
 {
 }
diff --git a/projects/stargazer/plugins/other/radius/config.h b/projects/stargazer/plugins/other/radius/config.h
index 45ee521f..c70c7b0f 100644
--- a/projects/stargazer/plugins/other/radius/config.h
+++ b/projects/stargazer/plugins/other/radius/config.h
@@ -28,6 +28,9 @@
 #include <map>
 #include <string>
 
+#include <unistd.h> // uid_t, gid_t
+#include <sys/stat.h> // mode_t
+
 namespace STG
 {
 
@@ -64,6 +67,10 @@ struct Config
     std::string portStr;
     uint16_t port;
     std::string key;
+
+    uid_t sockUID;
+    gid_t sockGID;
+    mode_t sockMode;
 };
 
 } // namespace STG
diff --git a/projects/stargazer/plugins/other/radius/radius.cpp b/projects/stargazer/plugins/other/radius/radius.cpp
index 68a96d29..376e4278 100644
--- a/projects/stargazer/plugins/other/radius/radius.cpp
+++ b/projects/stargazer/plugins/other/radius/radius.cpp
@@ -176,6 +176,9 @@ int RADIUS::createUNIX() const
         m_logger(m_error);
         return 0;
     }
+    chown(m_config.bindAddress.c_str(), m_config.sockUID, m_config.sockGID);
+    if (m_config.sockMode != static_cast<mode_t>(-1))
+        chmod(m_config.bindAddress.c_str(), m_config.sockMode);
     return fd;
 }