diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/zen/cmds/service_cmd.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/zen/cmds/service_cmd.cpp b/src/zen/cmds/service_cmd.cpp index cf3a0bcd0..f64e6860d 100644 --- a/src/zen/cmds/service_cmd.cpp +++ b/src/zen/cmds/service_cmd.cpp @@ -19,6 +19,11 @@ # include <unistd.h> #endif +#if ZEN_PLATFORM_LINUX +# include <pwd.h> +# include <grp.h> +#endif + ZEN_THIRD_PARTY_INCLUDES_START #include <gsl/gsl-lite.hpp> ZEN_THIRD_PARTY_INCLUDES_END @@ -441,6 +446,34 @@ ServiceCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) throw std::runtime_error(fmt::format("Unable to create install directory '{}'", m_InstallPath)); } +#if ZEN_PLATFORM_LINUX + uid_t UserId = 0; + gid_t GroupId = 0; + if (!m_UserName.empty()) + { + struct passwd* Passwd = getpwnam(m_UserName.c_str()); + if (Passwd == NULL) + { + throw std::runtime_error(fmt::format("Unable to determine user ID for user '{}'", m_UserName)); + } + + UserId = Passwd->pw_uid; + + struct group* Grp = getgrnam(m_UserName.c_str()); + if (Grp == NULL) + { + throw std::runtime_error(fmt::format("Unable to determine group ID for user '{}'", m_UserName)); + } + + GroupId = Grp->gr_gid; + if (chown(m_InstallPath.c_str(), UserId, GroupId) != 0) + { + throw std::runtime_error( + fmt::format("Unable to set ownership of directory '{}' for user '{}'", m_InstallPath, m_UserName)); + } + } +#endif + for (const std::filesystem::path& File : FilesToCopy) { std::filesystem::path Destination = m_InstallPath / File.filename(); @@ -456,6 +489,17 @@ ServiceCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv) { std::filesystem::permissions(Destination, std::filesystem::perms::owner_exec, std::filesystem::perm_options::add); } + +#if ZEN_PLATFORM_LINUX + if (UserId != 0) + { + if (chown(Destination.c_str(), UserId, GroupId) != 0) + { + throw std::runtime_error( + fmt::format("Unable to set ownership of file '{}' for user '{}'", Destination, m_UserName)); + } + } +#endif } m_ServerExecutable = m_InstallPath / m_ServerExecutable.filename(); |