c62b8e
From bac7488e06e379628653fb2f3ece0a30414ff84e Mon Sep 17 00:00:00 2001
c62b8e
From: Lennart Poettering <lennart@poettering.net>
c62b8e
Date: Mon, 22 Jan 2018 21:03:53 +0100
c62b8e
Subject: [PATCH] tmpfiles: change ownership of symlinks too
c62b8e
c62b8e
Ownership is supported for symlinks, too, only file modes are not.
c62b8e
Support that too.
c62b8e
c62b8e
Fixes: #7509
c62b8e
(cherry picked from commit 51207ca134716a0dee5fd763a6c39204be849eb1)
c62b8e
c62b8e
Resolves: #1620110
c62b8e
---
c62b8e
 src/shared/macro.h      |  7 +++++++
c62b8e
 src/tmpfiles/tmpfiles.c | 41 +++++++++++++++++++++--------------------
c62b8e
 2 files changed, 28 insertions(+), 20 deletions(-)
c62b8e
c62b8e
diff --git a/src/shared/macro.h b/src/shared/macro.h
c62b8e
index 7a57f4e5b1..26df270d51 100644
c62b8e
--- a/src/shared/macro.h
c62b8e
+++ b/src/shared/macro.h
c62b8e
@@ -125,6 +125,13 @@ static inline unsigned long ALIGN_POWER2(unsigned long u) {
c62b8e
 
c62b8e
 #define ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
c62b8e
 
c62b8e
+/*
c62b8e
+ * STRLEN - return the length of a string literal, minus the trailing NUL byte.
c62b8e
+ *          Contrary to strlen(), this is a constant expression.
c62b8e
+ * @x: a string literal.
c62b8e
+ */
c62b8e
+#define STRLEN(x) (sizeof(""x"") - 1)
c62b8e
+
c62b8e
 /*
c62b8e
  * container_of - cast a member of a structure out to the containing structure
c62b8e
  * @ptr: the pointer to the member.
c62b8e
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
c62b8e
index 0b17b5908e..663f6c8b2d 100644
c62b8e
--- a/src/tmpfiles/tmpfiles.c
c62b8e
+++ b/src/tmpfiles/tmpfiles.c
c62b8e
@@ -591,6 +591,7 @@ finish:
c62b8e
 }
c62b8e
 
c62b8e
 static int path_set_perms(Item *i, const char *path) {
c62b8e
+        char fn[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
c62b8e
         _cleanup_close_ int fd = -1;
c62b8e
         struct stat st;
c62b8e
 
c62b8e
@@ -624,14 +625,12 @@ static int path_set_perms(Item *i, const char *path) {
c62b8e
         if (i->type == EMPTY_DIRECTORY && !S_ISDIR(st.st_mode))
c62b8e
                 return log_error_errno(EEXIST, "'%s' already exists and is not a directory. ", path);
c62b8e
 
c62b8e
-        if (S_ISLNK(st.st_mode))
c62b8e
-                log_debug("Skipping mode an owner fix for symlink %s.", path);
c62b8e
-        else {
c62b8e
-                char fn[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
c62b8e
-                xsprintf(fn, "/proc/self/fd/%i", fd);
c62b8e
+        xsprintf(fn, "/proc/self/fd/%i", fd);
c62b8e
 
c62b8e
-                /* not using i->path directly because it may be a glob */
c62b8e
-                if (i->mode_set) {
c62b8e
+        if (i->mode_set) {
c62b8e
+                if (S_ISLNK(st.st_mode))
c62b8e
+                        log_debug("Skipping mode fix for symlink %s.", path);
c62b8e
+                else {
c62b8e
                         mode_t m = i->mode;
c62b8e
 
c62b8e
                         if (i->mask_perms) {
c62b8e
@@ -646,25 +645,27 @@ static int path_set_perms(Item *i, const char *path) {
c62b8e
                         }
c62b8e
 
c62b8e
                         if (m == (st.st_mode & 07777))
c62b8e
-                                log_debug("\"%s\" has right mode %o", path, st.st_mode);
c62b8e
+                                log_debug("\"%s\" has correct mode %o already.", path, st.st_mode);
c62b8e
                         else {
c62b8e
-                                log_debug("chmod \"%s\" to mode %o", path, m);
c62b8e
+                                log_debug("Changing \"%s\" to mode %o.", path, m);
c62b8e
+
c62b8e
                                 if (chmod(fn, m) < 0)
c62b8e
                                         return log_error_errno(errno, "chmod(%s) failed: %m", path);
c62b8e
                         }
c62b8e
                 }
c62b8e
+        }
c62b8e
 
c62b8e
-                if ((i->uid != st.st_uid || i->gid != st.st_gid) &&
c62b8e
-                    (i->uid_set || i->gid_set)) {
c62b8e
-                        log_debug("chown \"%s\" to "UID_FMT"."GID_FMT,
c62b8e
-                                  path,
c62b8e
-                                  i->uid_set ? i->uid : UID_INVALID,
c62b8e
-                                  i->gid_set ? i->gid : GID_INVALID);
c62b8e
-                        if (chown(fn,
c62b8e
-                                  i->uid_set ? i->uid : UID_INVALID,
c62b8e
-                                  i->gid_set ? i->gid : GID_INVALID) < 0)
c62b8e
-                        return log_error_errno(errno, "chown(%s) failed: %m", path);
c62b8e
-                }
c62b8e
+        if ((i->uid != st.st_uid || i->gid != st.st_gid) &&
c62b8e
+            (i->uid_set || i->gid_set)) {
c62b8e
+                log_debug("Changing \"%s\" to owner "UID_FMT":"GID_FMT,
c62b8e
+                          path,
c62b8e
+                          i->uid_set ? i->uid : UID_INVALID,
c62b8e
+                          i->gid_set ? i->gid : GID_INVALID);
c62b8e
+
c62b8e
+                if (chown(fn,
c62b8e
+                          i->uid_set ? i->uid : UID_INVALID,
c62b8e
+                          i->gid_set ? i->gid : GID_INVALID) < 0)
c62b8e
+                        return log_error_errno(errno, "chown() of %s via %s failed: %m", path, fn);
c62b8e
         }
c62b8e
 
c62b8e
         fd = safe_close(fd);