diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2021-02-28 22:12:42 -0800 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2021-02-28 22:13:34 -0800 |
commit | 2f7d215539637d8190ed57868980988086883433 (patch) | |
tree | e6436d20d02048104c1dc3f4f9a18aca8deb7f8f /paxlib | |
parent | c30dcfedd3c616f706b4f5db166793de4ac1f64d (diff) | |
download | paxutils-2f7d215539637d8190ed57868980988086883433.tar.gz |
Move sys_reset_uid_gid to library
This fixes a problem where ‘gcc -fanalyzer -flto’ (GCC 10)
complained that sys_reset_uid_gid was defined but not used in some
modules. This function belonged in a .c file anyway.
* lib/rtapelib.c (sys_reset_uid_gid):
* paxlib/rtape.c (sys_reset_uid_gid):
Move here from system.h. The code is now duplicated, but so is
most of the rest of this file anyway.
* lib/system.h (sys_reset_uid_gid): Remove.
Diffstat (limited to 'paxlib')
-rw-r--r-- | paxlib/rtape.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/paxlib/rtape.c b/paxlib/rtape.c index 2364594..1089d92 100644 --- a/paxlib/rtape.c +++ b/paxlib/rtape.c @@ -358,6 +358,29 @@ encode_oflag (char *buf, int oflag) strcat (buf, "|O_TRUNC"); } +/* Reset user and group IDs to be those of the real user. + Return NULL on success, a failing syscall name (setting errno) on error. */ +static char const * +sys_reset_uid_gid (void) +{ +#if !MSDOS + uid_t uid = getuid (); + gid_t gid = getgid (); + struct passwd *pw = getpwuid (uid); + + if (!pw) + return "getpwuid"; + if (initgroups (pw->pw_name, gid) != 0) + return "initgroups"; + if (gid != getegid () && setgid (gid) != 0 && errno != EPERM) + return "setgid"; + if (uid != geteuid () && setuid (uid) != 0 && errno != EPERM) + return "setuid"; +#endif + + return NULL; +} + /* Open a remote file on the system specified in FILE_NAME, as the given user. FILE_NAME has the form `[USER@]HOST:FILE'. OPEN_MODE is O_RDONLY, O_WRONLY, etc. If successful, return the |