mirror of
https://github.com/troglobit/finit.git
synced 2026-11-23 20:11:44 +00:00
tmpfiles: add the : and ~ prefixes for mode, user, and group
A mode with a prefix was not read as a number, so the entry was taken
to have no mode, and a user with one was looked up with the colon in
the name, which skipped the entry:
d /var/lib/app :0750 :app :app
With : the mode, user, or group is only set on a path the entry
creates, an existing one keeps what it has. With ~ the mode loses
read, write, or execute when no one has that on the existing path,
and setuid, setgid, and sticky when it is not a directory.
Mode and owner now go through perm_set() in tmpfiles, which knows the
entry created the path or not.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
4 files changed
+132
-32
No files matched your search
+109
-15
@@ -349,6 +349,86 @@ static int parse_gid(const char *group)
|
||||
return getgroup(group);
|
||||
}
|
||||
|
||||
/* Mode and owner of an entry, mode 0 and id -1 when not given */
|
||||
struct perm {
|
||||
mode_t mode;
|
||||
int uid, gid;
|
||||
int mask; /* ~, see perm_set() */
|
||||
int new_mode, new_uid, new_gid; /* :, only for a path the entry creates */
|
||||
};
|
||||
|
||||
/* Also for an fd opened with O_PATH, which fchmod() does not take */
|
||||
static int fd_chmod(int fd, mode_t mode)
|
||||
{
|
||||
char proc[32];
|
||||
|
||||
if (!fchmod(fd, mode))
|
||||
return 0;
|
||||
if (errno != EBADF)
|
||||
return -1;
|
||||
|
||||
snprintf(proc, sizeof(proc), "/proc/self/fd/%d", fd);
|
||||
|
||||
return chmod(proc, mode);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set mode and owner on an open fd, only what differs is touched, the
|
||||
* path may be immutable. @created tells if the entry made the path.
|
||||
*/
|
||||
static int perm_set(int fd, const struct perm *p, int created)
|
||||
{
|
||||
mode_t mode = p->mode;
|
||||
int uid = p->uid, gid = p->gid;
|
||||
struct stat st;
|
||||
|
||||
if (fstat(fd, &st))
|
||||
return -1;
|
||||
if (S_ISLNK(st.st_mode))
|
||||
return 0;
|
||||
|
||||
if (!created) {
|
||||
if (p->new_mode)
|
||||
mode = 0;
|
||||
if (p->new_uid)
|
||||
uid = -1;
|
||||
if (p->new_gid)
|
||||
gid = -1;
|
||||
|
||||
/* Keep read, write, and execute only where the path has it for someone */
|
||||
if (mode && p->mask) {
|
||||
for (mode_t bits = 0111; bits <= 0444; bits <<= 1) {
|
||||
if (!(st.st_mode & bits))
|
||||
mode &= ~bits;
|
||||
}
|
||||
if (!S_ISDIR(st.st_mode))
|
||||
mode &= ~07000;
|
||||
}
|
||||
}
|
||||
|
||||
if (mode && (st.st_mode & 07777) != mode && fd_chmod(fd, mode))
|
||||
return -1;
|
||||
if ((uid != -1 && st.st_uid != (uid_t)uid) || (gid != -1 && st.st_gid != (gid_t)gid))
|
||||
return fchownat(fd, "", uid, gid, AT_EMPTY_PATH);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Never follows a link at @path, see also dirperm() */
|
||||
static int perm_path(const char *path, const struct perm *p, int created, int flags)
|
||||
{
|
||||
int fd, rc;
|
||||
|
||||
fd = open(path, flags | O_NOFOLLOW | O_CLOEXEC);
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
|
||||
rc = perm_set(fd, p, created);
|
||||
close(fd);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* What the x and X entries keep from being cleaned */
|
||||
struct keep {
|
||||
char *pattern;
|
||||
@@ -765,9 +845,8 @@ static void tmpfiles(char *line)
|
||||
struct stat st, ast;
|
||||
int major, minor;
|
||||
int strc, rc = 0;
|
||||
struct perm perm = { 0 };
|
||||
int plus = 0, boot = 0;
|
||||
int uid, gid;
|
||||
mode_t mode = 0;
|
||||
FILE *fp = NULL;
|
||||
char buf[1024];
|
||||
glob_t gl;
|
||||
@@ -803,16 +882,31 @@ static void tmpfiles(char *line)
|
||||
|
||||
token = strtok(NULL, "\t ");
|
||||
if (token) {
|
||||
for (; *token == '~' || *token == ':'; token++) {
|
||||
if (*token == '~')
|
||||
perm.mask = 1;
|
||||
else
|
||||
perm.new_mode = 1;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
mode = strtoul(token, &arg, 8);
|
||||
perm.mode = strtoul(token, &arg, 8);
|
||||
if (errno || arg == token)
|
||||
mode = 0;
|
||||
perm.mode = 0;
|
||||
}
|
||||
|
||||
user = strtok(NULL, "\t ");
|
||||
if (user && user[0] == ':') {
|
||||
perm.new_uid = 1;
|
||||
user++;
|
||||
}
|
||||
if (user && !strcmp(user, "-"))
|
||||
user = NULL;
|
||||
group = strtok(NULL, "\t ");
|
||||
if (group && group[0] == ':') {
|
||||
perm.new_gid = 1;
|
||||
group++;
|
||||
}
|
||||
if (group && !strcmp(group, "-"))
|
||||
group = NULL;
|
||||
|
||||
@@ -820,14 +914,14 @@ static void tmpfiles(char *line)
|
||||
* Quiet about it, Finit's own files have entries for users of
|
||||
* services that may not be installed, and for the utmp group
|
||||
*/
|
||||
uid = parse_uid(user);
|
||||
if (user && uid < 0) {
|
||||
perm.uid = parse_uid(user);
|
||||
if (user && perm.uid < 0) {
|
||||
if (debug)
|
||||
warnx("Unknown user %s, skipping %s", user, path);
|
||||
return;
|
||||
}
|
||||
gid = parse_gid(group);
|
||||
if (group && gid < 0 && debug)
|
||||
perm.gid = parse_gid(group);
|
||||
if (group && perm.gid < 0 && debug)
|
||||
warnx("Unknown group %s, not setting the group of %s", group, path);
|
||||
|
||||
age = strtok(NULL, "\t ");
|
||||
@@ -923,7 +1017,7 @@ static void tmpfiles(char *line)
|
||||
erase(path);
|
||||
}
|
||||
mkparent(path, 0755);
|
||||
rc = blkdev(path, mode ?: 0644, major, minor);
|
||||
rc = blkdev(path, perm.mode ?: 0644, major, minor);
|
||||
break;
|
||||
case 'c':
|
||||
rc = parse_mm(arg, &major, &minor);
|
||||
@@ -935,7 +1029,7 @@ static void tmpfiles(char *line)
|
||||
erase(path);
|
||||
}
|
||||
mkparent(path, 0755);
|
||||
rc = chardev(path, mode ?: 0644, major, minor);
|
||||
rc = chardev(path, perm.mode ?: 0644, major, minor);
|
||||
break;
|
||||
case 'C':
|
||||
if (!arg) {
|
||||
@@ -964,8 +1058,8 @@ static void tmpfiles(char *line)
|
||||
|
||||
mkparent(path, 0755);
|
||||
omask = umask(0);
|
||||
rc = makedir(path, mode ?: 0755);
|
||||
if (!rc && dirperm(path, mode, uid, gid))
|
||||
rc = makedir(path, perm.mode ?: 0755);
|
||||
if (!rc && perm_path(path, &perm, strc, O_RDONLY | O_DIRECTORY))
|
||||
warn("Failed setting mode/owner on %s", path);
|
||||
umask(omask);
|
||||
break;
|
||||
@@ -978,7 +1072,7 @@ static void tmpfiles(char *line)
|
||||
char *p = gl.gl_pathv[i];
|
||||
|
||||
/* skip non-directories */
|
||||
if (dirperm(p, mode, uid, gid) && errno != ENOTDIR)
|
||||
if (perm_path(p, &perm, 0, O_RDONLY | O_DIRECTORY) && errno != ENOTDIR)
|
||||
warn("Failed setting mode/owner on %s", p);
|
||||
}
|
||||
globfree(&gl);
|
||||
@@ -1012,7 +1106,7 @@ static void tmpfiles(char *line)
|
||||
|
||||
write_arg(fp, arg);
|
||||
|
||||
if (fdperm(fd, mode, uid, gid))
|
||||
if (perm_set(fd, &perm, strc))
|
||||
warn("Failed setting mode/owner on %s", path);
|
||||
|
||||
rc = fclose(fp);
|
||||
@@ -1063,7 +1157,7 @@ static void tmpfiles(char *line)
|
||||
erase(path);
|
||||
}
|
||||
mkparent(path, 0755);
|
||||
rc = mkfifo(path, mode ?: 0644);
|
||||
rc = mkfifo(path, perm.mode ?: 0644);
|
||||
break;
|
||||
case 'r':
|
||||
case 'R':
|
||||
|
||||
Reference in new issue
Block a user