1993-03-21 12:45:37 +03:00
|
|
|
/*-
|
1994-05-06 03:24:26 +04:00
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-19 06:45:44 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-10-19 06:45:44 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-10-19 06:45:44 +04:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)id.c 8.3 (Berkeley) 4/28/95";
|
|
|
|
#else
|
2005-08-30 20:47:47 +04:00
|
|
|
__RCSID("$NetBSD: id.c,v 1.25 2005/08/30 16:47:47 drochner Exp $");
|
1997-10-19 06:45:44 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
1994-05-06 03:24:26 +04:00
|
|
|
|
1997-10-19 06:45:44 +04:00
|
|
|
#include <err.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <errno.h>
|
1994-05-06 03:24:26 +04:00
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdio.h>
|
1994-05-06 03:24:26 +04:00
|
|
|
#include <stdlib.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <string.h>
|
1994-05-06 03:24:26 +04:00
|
|
|
#include <unistd.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2004-11-22 20:28:58 +03:00
|
|
|
static void current(void);
|
|
|
|
static void pretty(struct passwd *);
|
|
|
|
static void group(struct passwd *, int);
|
|
|
|
static void usage(void);
|
|
|
|
static void user(struct passwd *);
|
|
|
|
static struct passwd *who(char *);
|
1998-12-19 19:55:21 +03:00
|
|
|
|
|
|
|
static int maxgroups;
|
|
|
|
static gid_t *groups;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-06 03:24:26 +04:00
|
|
|
int
|
2004-11-22 20:28:58 +03:00
|
|
|
main(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-05-06 03:24:26 +04:00
|
|
|
struct group *gr;
|
|
|
|
struct passwd *pw;
|
1998-10-14 04:55:03 +04:00
|
|
|
int ch, id;
|
|
|
|
int Gflag, gflag, nflag, pflag, rflag, uflag;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-06 03:24:26 +04:00
|
|
|
Gflag = gflag = nflag = pflag = rflag = uflag = 0;
|
1998-10-14 04:55:03 +04:00
|
|
|
|
2004-11-22 20:28:58 +03:00
|
|
|
if (strcmp(getprogname(), "groups") == 0) {
|
1998-10-15 01:56:57 +04:00
|
|
|
Gflag = 1;
|
|
|
|
nflag = 1;
|
2004-11-22 20:28:58 +03:00
|
|
|
} else if (strcmp(getprogname(), "whoami") == 0) {
|
1998-10-15 01:56:57 +04:00
|
|
|
uflag = 1;
|
|
|
|
nflag = 1;
|
|
|
|
}
|
1998-10-14 04:55:03 +04:00
|
|
|
|
1997-10-19 07:18:54 +04:00
|
|
|
while ((ch = getopt(argc, argv, "Ggnpru")) != -1)
|
2004-11-22 20:28:58 +03:00
|
|
|
switch (ch) {
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'G':
|
|
|
|
Gflag = 1;
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
gflag = 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
nflag = 1;
|
|
|
|
break;
|
1994-05-06 03:24:26 +04:00
|
|
|
case 'p':
|
|
|
|
pflag = 1;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 'r':
|
|
|
|
rflag = 1;
|
|
|
|
break;
|
|
|
|
case 'u':
|
|
|
|
uflag = 1;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
2004-11-22 20:28:58 +03:00
|
|
|
switch (Gflag + gflag + pflag + uflag) {
|
1994-05-06 03:24:26 +04:00
|
|
|
case 1:
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
if (!nflag && !rflag)
|
|
|
|
break;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
default:
|
1993-03-21 12:45:37 +03:00
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
1994-05-06 03:24:26 +04:00
|
|
|
pw = *argv ? who(*argv) : NULL;
|
|
|
|
|
1998-12-19 19:55:21 +03:00
|
|
|
maxgroups = sysconf(_SC_NGROUPS_MAX);
|
|
|
|
if ((groups = malloc((maxgroups + 1) * sizeof(gid_t))) == NULL)
|
1999-11-09 18:06:30 +03:00
|
|
|
err(1, NULL);
|
1998-12-19 19:55:21 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (gflag) {
|
|
|
|
id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
|
1994-05-06 03:24:26 +04:00
|
|
|
if (nflag && (gr = getgrgid(id)))
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)printf("%s\n", gr->gr_name);
|
1994-05-06 03:24:26 +04:00
|
|
|
else
|
|
|
|
(void)printf("%u\n", id);
|
1998-12-19 19:55:21 +03:00
|
|
|
goto done;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (uflag) {
|
|
|
|
id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
|
1994-05-06 03:24:26 +04:00
|
|
|
if (nflag && (pw = getpwuid(id)))
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)printf("%s\n", pw->pw_name);
|
1994-05-06 03:24:26 +04:00
|
|
|
else
|
|
|
|
(void)printf("%u\n", id);
|
1998-12-19 19:55:21 +03:00
|
|
|
goto done;
|
1994-05-06 03:24:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Gflag) {
|
|
|
|
group(pw, nflag);
|
1998-12-19 19:55:21 +03:00
|
|
|
goto done;
|
1994-05-06 03:24:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pflag) {
|
|
|
|
pretty(pw);
|
1998-12-19 19:55:21 +03:00
|
|
|
goto done;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pw)
|
|
|
|
user(pw);
|
|
|
|
else
|
|
|
|
current();
|
1998-12-19 19:55:21 +03:00
|
|
|
done:
|
|
|
|
free(groups);
|
2004-11-22 20:28:58 +03:00
|
|
|
|
|
|
|
return 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1998-12-19 19:55:21 +03:00
|
|
|
static void
|
2004-11-22 20:28:58 +03:00
|
|
|
pretty(struct passwd *pw)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-05-06 03:24:26 +04:00
|
|
|
struct group *gr;
|
|
|
|
u_int eid, rid;
|
|
|
|
char *login;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (pw) {
|
1994-05-06 03:24:26 +04:00
|
|
|
(void)printf("uid\t%s\n", pw->pw_name);
|
|
|
|
(void)printf("groups\t");
|
|
|
|
group(pw, 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
} else {
|
1994-05-06 03:24:26 +04:00
|
|
|
if ((login = getlogin()) == NULL)
|
1997-10-19 06:45:44 +04:00
|
|
|
err(1, "getlogin");
|
1994-05-06 03:24:26 +04:00
|
|
|
|
|
|
|
pw = getpwuid(rid = getuid());
|
|
|
|
if (pw == NULL || strcmp(login, pw->pw_name))
|
|
|
|
(void)printf("login\t%s\n", login);
|
|
|
|
if (pw)
|
|
|
|
(void)printf("uid\t%s\n", pw->pw_name);
|
|
|
|
else
|
|
|
|
(void)printf("uid\t%u\n", rid);
|
|
|
|
|
1998-08-26 00:59:36 +04:00
|
|
|
if ((eid = geteuid()) != rid) {
|
1997-10-19 06:45:44 +04:00
|
|
|
if ((pw = getpwuid(eid)) != NULL)
|
2001-01-05 06:08:23 +03:00
|
|
|
(void)printf("euid\t%s\n", pw->pw_name);
|
1993-03-21 12:45:37 +03:00
|
|
|
else
|
2001-01-05 06:08:23 +03:00
|
|
|
(void)printf("euid\t%u\n", eid);
|
1998-08-26 00:59:36 +04:00
|
|
|
}
|
|
|
|
if ((rid = getgid()) != (eid = getegid())) {
|
1997-10-19 06:45:44 +04:00
|
|
|
if ((gr = getgrgid(rid)) != NULL)
|
1994-05-06 03:24:26 +04:00
|
|
|
(void)printf("rgid\t%s\n", gr->gr_name);
|
|
|
|
else
|
|
|
|
(void)printf("rgid\t%u\n", rid);
|
1998-08-26 00:59:36 +04:00
|
|
|
}
|
1994-05-06 03:24:26 +04:00
|
|
|
(void)printf("groups\t");
|
|
|
|
group(NULL, 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-12-19 19:55:21 +03:00
|
|
|
static void
|
2004-11-22 20:28:58 +03:00
|
|
|
current(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-05-06 03:24:26 +04:00
|
|
|
struct group *gr;
|
|
|
|
struct passwd *pw;
|
|
|
|
int cnt, id, eid, lastid, ngroups;
|
2004-11-22 20:31:38 +03:00
|
|
|
const char *fmt;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
id = getuid();
|
|
|
|
(void)printf("uid=%u", id);
|
1997-10-19 06:45:44 +04:00
|
|
|
if ((pw = getpwuid(id)) != NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)printf("(%s)", pw->pw_name);
|
|
|
|
if ((eid = geteuid()) != id) {
|
|
|
|
(void)printf(" euid=%u", eid);
|
1997-10-19 06:45:44 +04:00
|
|
|
if ((pw = getpwuid(eid)) != NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)printf("(%s)", pw->pw_name);
|
|
|
|
}
|
|
|
|
id = getgid();
|
|
|
|
(void)printf(" gid=%u", id);
|
1997-10-19 06:45:44 +04:00
|
|
|
if ((gr = getgrgid(id)) != NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)printf("(%s)", gr->gr_name);
|
|
|
|
if ((eid = getegid()) != id) {
|
|
|
|
(void)printf(" egid=%u", eid);
|
1997-10-19 06:45:44 +04:00
|
|
|
if ((gr = getgrgid(eid)) != NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)printf("(%s)", gr->gr_name);
|
|
|
|
}
|
2003-10-21 06:17:45 +04:00
|
|
|
if ((ngroups = getgroups(maxgroups, groups)) != 0) {
|
1994-05-06 03:24:26 +04:00
|
|
|
for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
|
1999-08-17 21:37:32 +04:00
|
|
|
fmt = ",%u", lastid = id, cnt++) {
|
|
|
|
id = groups[cnt];
|
1993-03-21 12:45:37 +03:00
|
|
|
if (lastid == id)
|
|
|
|
continue;
|
|
|
|
(void)printf(fmt, id);
|
1997-10-19 06:45:44 +04:00
|
|
|
if ((gr = getgrgid(id)) != NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)printf("(%s)", gr->gr_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(void)printf("\n");
|
|
|
|
}
|
|
|
|
|
1998-12-19 19:55:21 +03:00
|
|
|
static void
|
2004-11-22 20:28:58 +03:00
|
|
|
user(struct passwd *pw)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-19 06:45:44 +04:00
|
|
|
struct group *gr;
|
2004-11-22 20:31:38 +03:00
|
|
|
const char *fmt;
|
1998-12-19 19:55:21 +03:00
|
|
|
int cnt, id, lastid, ngroups;
|
2005-08-30 20:47:47 +04:00
|
|
|
gid_t *glist = groups;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
id = pw->pw_uid;
|
|
|
|
(void)printf("uid=%u(%s)", id, pw->pw_name);
|
1998-12-19 19:55:21 +03:00
|
|
|
(void)printf(" gid=%lu", (u_long)pw->pw_gid);
|
1997-10-19 06:45:44 +04:00
|
|
|
if ((gr = getgrgid(pw->pw_gid)) != NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)printf("(%s)", gr->gr_name);
|
1998-12-19 19:55:21 +03:00
|
|
|
ngroups = maxgroups + 1;
|
2005-08-30 20:47:47 +04:00
|
|
|
if (getgrouplist(pw->pw_name, pw->pw_gid, glist, &ngroups) == -1) {
|
|
|
|
glist = malloc(ngroups * sizeof(gid_t));
|
|
|
|
(void) getgrouplist(pw->pw_name, pw->pw_gid, glist, &ngroups);
|
|
|
|
}
|
1999-08-17 21:37:32 +04:00
|
|
|
for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
|
|
|
|
fmt=",%u", lastid = id, cnt++) {
|
2005-08-30 20:47:47 +04:00
|
|
|
id = glist[cnt];
|
1999-08-17 21:37:32 +04:00
|
|
|
if (lastid == id)
|
1994-05-06 03:24:26 +04:00
|
|
|
continue;
|
|
|
|
(void)printf(fmt, id);
|
1997-10-19 06:45:44 +04:00
|
|
|
if ((gr = getgrgid(id)) != NULL)
|
1994-05-06 03:24:26 +04:00
|
|
|
(void)printf("(%s)", gr->gr_name);
|
|
|
|
}
|
|
|
|
(void)printf("\n");
|
2005-08-30 20:47:47 +04:00
|
|
|
if (glist != groups)
|
|
|
|
free(glist);
|
1994-05-06 03:24:26 +04:00
|
|
|
}
|
|
|
|
|
1998-12-19 19:55:21 +03:00
|
|
|
static void
|
2004-11-22 20:28:58 +03:00
|
|
|
group(struct passwd *pw, int nflag)
|
1994-05-06 03:24:26 +04:00
|
|
|
{
|
|
|
|
struct group *gr;
|
|
|
|
int cnt, id, lastid, ngroups;
|
2004-11-22 20:31:38 +03:00
|
|
|
const char *fmt;
|
2005-08-30 20:47:47 +04:00
|
|
|
gid_t *glist = groups;
|
1994-05-06 03:24:26 +04:00
|
|
|
|
|
|
|
if (pw) {
|
1998-12-19 19:55:21 +03:00
|
|
|
ngroups = maxgroups;
|
2005-08-30 20:47:47 +04:00
|
|
|
if (getgrouplist(pw->pw_name, pw->pw_gid, glist, &ngroups)
|
|
|
|
== -1) {
|
|
|
|
glist = malloc(ngroups * sizeof(gid_t));
|
|
|
|
(void) getgrouplist(pw->pw_name, pw->pw_gid, glist,
|
|
|
|
&ngroups);
|
|
|
|
}
|
1994-05-06 03:24:26 +04:00
|
|
|
} else {
|
2005-08-30 20:47:47 +04:00
|
|
|
glist[0] = getgid();
|
|
|
|
ngroups = getgroups(maxgroups, glist + 1) + 1;
|
1994-05-06 03:24:26 +04:00
|
|
|
}
|
|
|
|
fmt = nflag ? "%s" : "%u";
|
|
|
|
for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
|
2005-08-30 20:47:47 +04:00
|
|
|
if (lastid == (id = glist[cnt]))
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
1994-05-06 03:24:26 +04:00
|
|
|
if (nflag) {
|
1997-10-19 06:45:44 +04:00
|
|
|
if ((gr = getgrgid(id)) != NULL)
|
1994-05-06 03:24:26 +04:00
|
|
|
(void)printf(fmt, gr->gr_name);
|
|
|
|
else
|
|
|
|
(void)printf(*fmt == ' ' ? " %u" : "%u",
|
|
|
|
id);
|
|
|
|
fmt = " %s";
|
|
|
|
} else {
|
|
|
|
(void)printf(fmt, id);
|
|
|
|
fmt = " %u";
|
|
|
|
}
|
|
|
|
lastid = id;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
(void)printf("\n");
|
2005-08-30 20:47:47 +04:00
|
|
|
if (glist != groups)
|
|
|
|
free(glist);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2004-11-22 20:31:38 +03:00
|
|
|
static struct passwd *
|
2004-11-22 20:28:58 +03:00
|
|
|
who(char *u)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-05-06 03:24:26 +04:00
|
|
|
struct passwd *pw;
|
1993-03-21 12:45:37 +03:00
|
|
|
long id;
|
|
|
|
char *ep;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Translate user argument into a pw pointer. First, try to
|
|
|
|
* get it as specified. If that fails, try it as a number.
|
|
|
|
*/
|
1997-10-19 06:45:44 +04:00
|
|
|
if ((pw = getpwnam(u)) != NULL)
|
2004-11-22 20:28:58 +03:00
|
|
|
return pw;
|
1993-03-21 12:45:37 +03:00
|
|
|
id = strtol(u, &ep, 10);
|
|
|
|
if (*u && !*ep && (pw = getpwuid(id)))
|
2004-11-22 20:28:58 +03:00
|
|
|
return pw;
|
1997-10-19 06:45:44 +04:00
|
|
|
errx(1, "%s: No such user", u);
|
1993-03-21 12:45:37 +03:00
|
|
|
/* NOTREACHED */
|
2004-11-22 20:28:58 +03:00
|
|
|
return NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2004-11-22 20:31:38 +03:00
|
|
|
static void
|
2004-11-22 20:28:58 +03:00
|
|
|
usage(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2004-11-22 20:21:36 +03:00
|
|
|
|
|
|
|
if (strcmp(getprogname(), "groups") == 0) {
|
|
|
|
(void)fprintf(stderr, "usage: groups [user]\n");
|
|
|
|
} else if (strcmp(getprogname(), "whoami") == 0) {
|
|
|
|
(void)fprintf(stderr, "usage: whoami\n");
|
|
|
|
} else {
|
|
|
|
(void)fprintf(stderr, "usage: id [user]\n");
|
|
|
|
(void)fprintf(stderr, " id -G [-n] [user]\n");
|
|
|
|
(void)fprintf(stderr, " id -g [-nr] [user]\n");
|
|
|
|
(void)fprintf(stderr, " id -p [user]\n");
|
|
|
|
(void)fprintf(stderr, " id -u [-nr] [user]\n");
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|