diff options
| -rw-r--r-- | CHANGELOG.md | 3 | ||||
| -rw-r--r-- | src/zen/cmds/service_cmd.cpp | 44 |
2 files changed, 46 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 58b1b594b..4f26fab3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,7 +33,8 @@ kHttpServiceUnavailable = 69, // ServiceUnavailable(503) kHttpBadGateway = 92, // BadGateway(502) kHttpGatewayTimeout = 93, // GatewayTimeout(504) - +- Bugfix: Linux only, set ownership of installed files to specified user when using `zen service install --full --user` + ## 5.7.0 - **EXPERIMENTAL** Feature: Added command line tool `zen service` to do maintenance operations for running zenserver as a local service - `zen service status` - Check the status of zenservice as local service, indidicating if it is installed, running and the options it was installed with 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(); |