- Merge two functions for printing `id' output.

Showing the ids of both an user given by an argument to `id',
   and the current user, is now handled in a single function.
   Displaying the current user's ids was inaccurate because
   getgroups(2) had been used.  getgroups(2) returns the current
   kernel state of a user's groups, which may not always be
   correct if /etc/group was recently changed.
 - Fix a few style bugs.

PR:		bin/78085
This commit is contained in:
Robert Drehmel 2005-04-28 15:55:54 +00:00
parent 48c49ace00
commit 68b9b81e79
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=145628

View File

@ -57,15 +57,13 @@ __FBSDID("$FreeBSD$");
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
void current(void); void id_print(struct passwd *, int, int);
void pline(struct passwd *); void pline(struct passwd *);
void pretty(struct passwd *); void pretty(struct passwd *);
void group(struct passwd *, int); void group(struct passwd *, int);
void maclabel(void); void maclabel(void);
void usage(void); void usage(void);
void user(struct passwd *); struct passwd *who(char *);
struct passwd *
who(char *);
int isgroups, iswhoami; int isgroups, iswhoami;
@ -181,10 +179,14 @@ main(int argc, char *argv[])
exit(0); exit(0);
} }
if (pw) if (pw) {
user(pw); id_print(pw, 0, 0);
else }
current(); else {
id = getuid();
if ((pw = getpwuid(id)) != NULL)
id_print(pw, 1, 1);
}
exit(0); exit(0);
} }
@ -229,72 +231,46 @@ pretty(struct passwd *pw)
} }
void void
current(void) id_print(struct passwd *pw, int p_euid, int p_egid)
{ {
struct group *gr; struct group *gr;
struct passwd *pw; gid_t gid, egid, lastgid;
int cnt, id, eid, lastid, ngroups; uid_t uid, euid;
gid_t groups[NGROUPS]; int cnt, ngroups;
gid_t groups[NGROUPS + 1];
const char *fmt; const char *fmt;
id = getuid(); uid = pw->pw_uid;
(void)printf("uid=%u", id); gid = pw->pw_gid;
if ((pw = getpwuid(id)))
(void)printf("(%s)", pw->pw_name); ngroups = NGROUPS;
if ((eid = geteuid()) != id) { getgrouplist(pw->pw_name, gid, groups, &ngroups);
(void)printf(" euid=%u", eid);
if ((pw = getpwuid(eid))) printf("uid=%u(%s)", uid, pw->pw_name);
if (p_euid && (euid = geteuid()) != uid) {
(void)printf(" euid=%u", euid);
if ((pw = getpwuid(euid)))
(void)printf("(%s)", pw->pw_name); (void)printf("(%s)", pw->pw_name);
} }
id = getgid(); printf(" gid=%u", gid);
(void)printf(" gid=%u", id);
if ((gr = getgrgid(id)))
(void)printf("(%s)", gr->gr_name);
if ((eid = getegid()) != id) {
(void)printf(" egid=%u", eid);
if ((gr = getgrgid(eid)))
(void)printf("(%s)", gr->gr_name);
}
if ((ngroups = getgroups(NGROUPS, groups))) {
for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
fmt = ", %u", lastid = id) {
id = groups[cnt++];
if (lastid == id)
continue;
(void)printf(fmt, id);
if ((gr = getgrgid(id)))
(void)printf("(%s)", gr->gr_name);
}
}
(void)printf("\n");
}
void
user(struct passwd *pw)
{
struct group *gr;
const char *fmt;
gid_t gid, lastgid, groups[NGROUPS + 1];
int cnt, ngroups;
(void)printf("uid=%u(%s)", pw->pw_uid, pw->pw_name);
gid = pw->pw_gid;
(void)printf(" gid=%u", gid);
if ((gr = getgrgid(gid))) if ((gr = getgrgid(gid)))
(void)printf("(%s)", gr->gr_name); (void)printf("(%s)", gr->gr_name);
ngroups = NGROUPS + 1; if (p_egid && (egid = getegid()) != gid) {
(void) getgrouplist(pw->pw_name, gid, groups, &ngroups); (void)printf(" egid=%u", egid);
if ((gr = getgrgid(egid)))
(void)printf("(%s)", gr->gr_name);
}
fmt = " groups=%u"; fmt = " groups=%u";
for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) { for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) {
if (lastgid == (gid = groups[cnt])) if (lastgid == (gid = groups[cnt]))
continue; continue;
(void)printf(fmt, gid); printf(fmt, gid);
fmt = ", %u"; fmt = ", %u";
if ((gr = getgrgid(gid))) if ((gr = getgrgid(gid)))
(void)printf("(%s)", gr->gr_name); printf("(%s)", gr->gr_name);
lastgid = gid; lastgid = gid;
} }
(void)printf("\n"); printf("\n");
} }
void void