diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 78f2a263..a7c08f08 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -240,6 +240,9 @@ All relevant changes are documented in this file. `mksubsysd()` creates, are now set through an open fd and only when they differ, so an immutable directory like `/var/empty` no longer warns on every boot. By Sam Brkopac +- Fix owner of existing directories and files in tmpfiles, an entry with no + user or group, or `-`, reset them to `root`. As in tmpfiles.d(5), the + owner and the group are now each left alone unless the entry gives them - Fix a lost reload for a service paused mid-reload by a condition in flux. A second reload arriving meanwhile re-parsed the unchanged .conf and cleared the pending mark, so the service resumed without ever being diff --git a/src/service.c b/src/service.c index 0644ff48..aa168121 100644 --- a/src/service.c +++ b/src/service.c @@ -651,7 +651,7 @@ static void service_mkdirs(svc_t *svc, uid_t uid, gid_t gid) for (i = 0; i < NELEMS(svcdirs); i++) { uid_t u = svcdirs[i].chown ? uid : (uid_t)-1; - gid_t g = svcdirs[i].chown ? gid : 0; + gid_t g = svcdirs[i].chown ? gid : (gid_t)-1; struct stat st; char *ptr; diff --git a/src/tmpfiles.c b/src/tmpfiles.c index ab3a20ef..34fbb162 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -300,14 +300,14 @@ static time_t parse_age(const char *age) /** * Parse user string - supports both names and numeric UIDs. - * Returns UID on success, -1 on failure. + * Returns UID on success, -1 on failure or when no user is given. */ static int parse_uid(const char *user) { long val; - if (!user || !user[0]) - return 0; + if (!user) + return -1; /* Check if it's a numeric UID */ val = atonum(user); @@ -320,14 +320,15 @@ static int parse_uid(const char *user) /** * Parse group string - supports both names and numeric GIDs. - * Returns GID on success, -1 on failure. + * Returns GID on success, 0 for an unknown group, and -1 when no + * group is given. */ static int parse_gid(const char *group) { long val; - if (!group || !group[0]) - return 0; + if (!group) + return -1; /* Check if it's a numeric GID */ val = atonum(group); @@ -335,7 +336,9 @@ static int parse_gid(const char *group) return val; /* Not numeric, look up by name */ - return getgroup(group); + val = getgroup(group); + + return val < 0 ? 0 : val; } /* Globals for do_clean() callback - nftw doesn't support user data */ @@ -419,11 +422,11 @@ static void tmpfiles(char *line) } user = strtok(NULL, "\t "); - if (!user || !strcmp(user, "-")) - user = "root"; + if (user && !strcmp(user, "-")) + user = NULL; group = strtok(NULL, "\t "); - if (!group || !strcmp(group, "-")) - group = "root"; + if (group && !strcmp(group, "-")) + group = NULL; age = strtok(NULL, "\t "); arg = strtok(NULL, "\n"); @@ -553,10 +556,8 @@ static void tmpfiles(char *line) mkparent(path, 0755); omask = umask(0); uid = parse_uid(user); - if (uid >= 0) { + if (!user || uid >= 0) { gid = parse_gid(group); - if (gid < 0) - gid = 0; rc = makedir(path, mode ?: 0755); if (!rc && dirperm(path, mode, uid, gid)) @@ -573,8 +574,6 @@ static void tmpfiles(char *line) uid = parse_uid(user); gid = parse_gid(group); - if (gid < 0) - gid = 0; for (size_t i = 0; i < gl.gl_pathc; i++) { char *p = gl.gl_pathv[i]; @@ -617,8 +616,6 @@ static void tmpfiles(char *line) uid = parse_uid(user); gid = parse_gid(group); - if (gid < 0) - gid = 0; if (fdperm(fd, mode, uid, gid)) warn("Failed setting mode/owner on %s", path); diff --git a/src/util.c b/src/util.c index 78dc48c4..c1b4d89d 100644 --- a/src/util.c +++ b/src/util.c @@ -377,7 +377,8 @@ int rmrf(const char *path) /* * Set mode and owner on an open fd, only what differs is touched, the - * file may be immutable. mode 0 skips the chmod, uid -1 the chown. + * file may be immutable. mode 0 skips the chmod, uid and gid -1 keep + * the owner and the group. */ int fdperm(int fd, mode_t mode, uid_t uid, gid_t gid) { @@ -387,7 +388,8 @@ int fdperm(int fd, mode_t mode, uid_t uid, gid_t gid) rc = fstat(fd, &st); if (!rc && mode && (st.st_mode & 07777) != mode) rc = fchmod(fd, mode); - if (!rc && uid != (uid_t)-1 && (st.st_uid != uid || st.st_gid != gid)) + if (!rc && ((uid != (uid_t)-1 && st.st_uid != uid) || + (gid != (gid_t)-1 && st.st_gid != gid))) rc = fchown(fd, uid, gid); return rc; @@ -413,8 +415,8 @@ int dirperm(const char *path, mode_t mode, uid_t uid, gid_t gid) } /* - * Like mksubsys() but with the ids already resolved, uid -1 skips the - * chown. Parents are created 0755, only the leaf gets @mode. + * Like mksubsys() but with the ids already resolved, -1 keeps the id + * the directory has. Parents are created 0755, only the leaf gets @mode. */ int mksubsysd(const char *dir, mode_t mode, uid_t uid, gid_t gid) { @@ -438,12 +440,14 @@ int mksubsys(const char *dir, mode_t mode, char *user, char *group) uid = getuser(user, NULL); gid = getgroup(group); - if (gid < 0) - gid = 0; - if (uid < 0) + if (uid < 0) { warnx("Cannot find user %s, %s is owned by root", user, dir); + gid = -1; + } else if (gid < 0) { + gid = 0; + } - return mksubsysd(dir, mode, uid < 0 ? (uid_t)-1 : (uid_t)uid, (gid_t)gid); + return mksubsysd(dir, mode, (uid_t)uid, (gid_t)gid); } /* diff --git a/test/tmpfiles-perm.sh b/test/tmpfiles-perm.sh index 60244c88..d6c5ff57 100755 --- a/test/tmpfiles-perm.sh +++ b/test/tmpfiles-perm.sh @@ -1,6 +1,6 @@ #!/bin/sh -# Verify that tmpfiles leaves the mode of an existing directory alone -# when the entry does not give one, issue #523 +# Verify that tmpfiles leaves the mode and owner of an existing +# directory alone when the entry does not give them, issue #523 set -eu TEST_DIR=$(dirname "$0") @@ -30,10 +30,12 @@ say 'A new directory gets the default mode and owner' assert_perm "d $DIR" 755 root:root sep -say 'An existing directory keeps its mode when the entry gives none' -run "chmod 1777 $DIR" -assert_perm "d $DIR" 1777 root:root -assert_perm "d $DIR - - -" 1777 root:root -assert_perm "d $DIR 0750" 750 root:root -assert_perm "e $DIR" 750 root:root +say 'An existing directory keeps what the entry does not give' +run "chmod 1777 $DIR; chown daemon:daemon $DIR" +assert_perm "d $DIR" 1777 daemon:daemon +assert_perm "d $DIR - - -" 1777 daemon:daemon +assert_perm "d $DIR 0750" 750 daemon:daemon +assert_perm "d $DIR - bin" 750 bin:daemon +assert_perm "d $DIR - - sys" 750 bin:sys +assert_perm "e $DIR" 750 bin:sys assert_perm "d $DIR 1777 root root" 1777 root:root