aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLiam Mitchell <[email protected]>2025-09-08 01:30:03 -0700
committerGitHub Enterprise <[email protected]>2025-09-08 10:30:03 +0200
commit74f349a91454c92f2020e7d9377bfd78d5036893 (patch)
tree81fe6bae34bc7215a25301a9d4b7677dd6dd2180 /src
parent5.7.1-pre0 (diff)
downloadzen-74f349a91454c92f2020e7d9377bfd78d5036893.tar.xz
zen-74f349a91454c92f2020e7d9377bfd78d5036893.zip
Set ownership of service executables to the service user on Linux (#489)
- Bugfix: Linux only, set ownership of installed files to specified user when using `zen service install --full --user`
Diffstat (limited to 'src')
-rw-r--r--src/zen/cmds/service_cmd.cpp44
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();