summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2011-11-15 13:23:24 -0800
committerPaul Eggert <eggert@cs.ucla.edu>2011-11-15 13:24:02 -0800
commit8f512f9bdb2cf436e4ec63c31410d8033d75779a (patch)
tree802f4b4d2a5737774286cfca31689323e4314686
parente36e6f2c6e95d6e23be805f7bab6c596b1818d22 (diff)
downloadcoreutils-8f512f9bdb2cf436e4ec63c31410d8033d75779a.tar.xz
id: fix bug when euid != ruid
* src/id.c (main): Report an error if no args are given and getuid fails, because print_full_info needs ruid. Redo code so that getuid and friends are invoked only when needed; this makes the code easier to follow, and is how I found the above bug.
-rw-r--r--src/id.c51
1 files changed, 30 insertions, 21 deletions
diff --git a/src/id.c b/src/id.c
index 8f7ce9e5a..10471491c 100644
--- a/src/id.c
+++ b/src/id.c
@@ -207,27 +207,36 @@ main (int argc, char **argv)
uid_t NO_UID = -1;
gid_t NO_GID = -1;
- errno = 0;
- euid = geteuid ();
- if (euid == NO_UID && errno && !use_real
- && !just_group && !just_group_list && !just_context)
- error (EXIT_FAILURE, errno, _("cannot get effective UID"));
-
- errno = 0;
- ruid = getuid ();
- if (ruid == NO_UID && errno && use_real
- && !just_group && !just_group_list && !just_context)
- error (EXIT_FAILURE, errno, _("cannot get real UID"));
-
- errno = 0;
- egid = getegid ();
- if (egid == NO_GID && errno && !use_real && !just_user)
- error (EXIT_FAILURE, errno, _("cannot get effective GID"));
-
- errno = 0;
- rgid = getgid ();
- if (rgid == NO_GID && errno && use_real && !just_user)
- error (EXIT_FAILURE, errno, _("cannot get real GID"));
+ if (just_user ? !use_real
+ : !just_group && !just_group_list && !just_context)
+ {
+ errno = 0;
+ euid = geteuid ();
+ if (euid == NO_UID && errno)
+ error (EXIT_FAILURE, errno, _("cannot get effective UID"));
+ }
+
+ if (just_user ? use_real
+ : !just_group && (just_group_list || !just_context))
+ {
+ errno = 0;
+ ruid = getuid ();
+ if (ruid == NO_UID && errno)
+ error (EXIT_FAILURE, errno, _("cannot get real UID"));
+ }
+
+ if (!just_user && (just_group || just_group_list || !just_context))
+ {
+ errno = 0;
+ egid = getegid ();
+ if (egid == NO_GID && errno)
+ error (EXIT_FAILURE, errno, _("cannot get effective GID"));
+
+ errno = 0;
+ rgid = getgid ();
+ if (rgid == NO_GID && errno)
+ error (EXIT_FAILURE, errno, _("cannot get real GID"));
+ }
}
if (just_user)