summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2021-02-28 22:12:42 -0800
committerPaul Eggert <eggert@cs.ucla.edu>2021-02-28 22:13:34 -0800
commit2f7d215539637d8190ed57868980988086883433 (patch)
treee6436d20d02048104c1dc3f4f9a18aca8deb7f8f
parentc30dcfedd3c616f706b4f5db166793de4ac1f64d (diff)
downloadpaxutils-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.
-rw-r--r--lib/rtapelib.c23
-rw-r--r--lib/system.h18
-rw-r--r--paxlib/rtape.c23
3 files changed, 46 insertions, 18 deletions
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 <paxlib.h>
-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
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