From 2f7d215539637d8190ed57868980988086883433 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 28 Feb 2021 22:12:42 -0800 Subject: Move sys_reset_uid_gid to library MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- lib/rtapelib.c | 23 +++++++++++++++++++++++ lib/system.h | 18 ------------------ 2 files changed, 23 insertions(+), 18 deletions(-) (limited to 'lib') diff --git a/lib/rtapelib.c b/lib/rtapelib.c index 5190cd4..1ff2eb6 100644 --- a/lib/rtapelib.c +++ b/lib/rtapelib.c @@ -353,6 +353,29 @@ encode_oflag (char *buf, int oflag) if (oflag & O_TRUNC) 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 file (a magnetic tape device?) 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 diff --git a/lib/system.h b/lib/system.h index 92b2462..0690363 100644 --- a/lib/system.h +++ b/lib/system.h @@ -465,28 +465,10 @@ char *getenv (); # define SET_BINARY_MODE(arc) setmode(arc, O_BINARY) # define mkdir(file, mode) (mkdir) (file) # define TTY_NAME "con" -# define sys_reset_uid_gid() #else # define SET_BINARY_MODE(arc) # define TTY_NAME "/dev/tty" # include -static char const * -sys_reset_uid_gid (void) -{ - uid_t uid = getuid (); - gid_t gid = getgid (); - struct passwd *pw = getpwuid (uid); - - if (!pw) - return "getpwuid"; - if (initgroups (pw->pw_name, gid)) - return "initgroups"; - if (gid != getegid () && setgid (gid) && errno != EPERM) - return "setgid"; - if (uid != geteuid () && setuid (uid) && errno != EPERM) - return "setuid"; - return NULL; -} #endif #if XENIX -- cgit v1.2.1