diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2018-06-14 11:26:29 +0900 |
---|---|---|
committer | Sven Eden <yamakuzure@gmx.net> | 2018-08-24 16:47:08 +0200 |
commit | fc7c1e170db761d2675a3b1c72a16856b8dfb868 (patch) | |
tree | 230f2a0eadc55c5f5621e6f9dc4c1fdb05589c31 /src | |
parent | 7bde1b2188a60dc70a99fc2047b3b877e5f8349d (diff) |
fs-util: introduce fchmod_and_chown()
The new function fchmod_and_chown() is almost same as chmod_and_chown()
except it takes file descriptor instead of file path.
Diffstat (limited to 'src')
-rw-r--r-- | src/basic/fs-util.c | 16 | ||||
-rw-r--r-- | src/basic/fs-util.h | 1 |
2 files changed, 17 insertions, 0 deletions
diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c index 81d51bb66..1f2765737 100644 --- a/src/basic/fs-util.c +++ b/src/basic/fs-util.c @@ -239,6 +239,22 @@ int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid) { return 0; } +int fchmod_and_chown(int fd, mode_t mode, uid_t uid, gid_t gid) { + /* Under the assumption that we are running privileged we + * first change the access mode and only then hand out + * ownership to avoid a window where access is too open. */ + + if (mode != MODE_INVALID) + if (fchmod(fd, mode) < 0) + return -errno; + + if (uid != UID_INVALID || gid != GID_INVALID) + if (fchown(fd, uid, gid) < 0) + return -errno; + + return 0; +} + int fchmod_umask(int fd, mode_t m) { mode_t u; int r; diff --git a/src/basic/fs-util.h b/src/basic/fs-util.h index 4b490078d..4e65fd238 100644 --- a/src/basic/fs-util.h +++ b/src/basic/fs-util.h @@ -37,6 +37,7 @@ int readlink_and_make_absolute(const char *p, char **r); #endif // 0 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid); +int fchmod_and_chown(int fd, mode_t mode, uid_t uid, gid_t gid); int fchmod_umask(int fd, mode_t mode); int fchmod_opath(int fd, mode_t m); |