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
|
2003-10-21 06:17:45 +04:00
|
|
|
__RCSID("$NetBSD: id.c,v 1.20 2003/10/21 02:17:45 fvdl 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
|
|
|
|
1997-10-19 06:45:44 +04:00
|
|
|
int main __P((int, char **));
|
1998-12-19 19:55:21 +03:00
|
|
|
|
|
|
|
static void current __P((void));
|
|
|
|
static void pretty __P((struct passwd *));
|
|
|
|
static void group __P((struct passwd *, int));
|
|
|
|
static void usage __P((void));
|
|
|
|
static void user __P((struct passwd *));
|
|
|
|
static struct passwd *who __P((char *));
|
|
|
|
|
|
|
|
static int maxgroups;
|
|
|
|
static gid_t *groups;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-06 03:24:26 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
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
|
|
|
|
2001-02-20 02:03:42 +03:00
|
|
|
if (!strcmp(getprogname(), "groups")) {
|
1998-10-15 01:56:57 +04:00
|
|
|
Gflag = 1;
|
|
|
|
nflag = 1;
|
2001-02-20 02:03:42 +03:00
|
|
|
} else if (!strcmp(getprogname(), "whoami")) {
|
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)
|
1993-03-21 12:45:37 +03:00
|
|
|
switch(ch) {
|
|
|
|
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;
|
|
|
|
|
1994-05-06 03:24:26 +04:00
|
|
|
switch(Gflag + gflag + pflag + uflag) {
|
|
|
|
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);
|
|
|
|
return(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1998-12-19 19:55:21 +03:00
|
|
|
static void
|
1994-05-06 03:24:26 +04:00
|
|
|
pretty(pw)
|
|
|
|
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
|
1993-03-21 12:45:37 +03:00
|
|
|
current()
|
|
|
|
{
|
1994-05-06 03:24:26 +04:00
|
|
|
struct group *gr;
|
|
|
|
struct passwd *pw;
|
|
|
|
int cnt, id, eid, lastid, ngroups;
|
1993-03-21 12:45:37 +03:00
|
|
|
char *fmt;
|
|
|
|
|
|
|
|
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
|
1993-03-21 12:45:37 +03:00
|
|
|
user(pw)
|
1997-10-19 06:45:44 +04:00
|
|
|
struct passwd *pw;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-19 06:45:44 +04:00
|
|
|
struct group *gr;
|
|
|
|
char *fmt;
|
1998-12-19 19:55:21 +03:00
|
|
|
int cnt, id, lastid, ngroups;
|
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;
|
1994-05-06 03:24:26 +04:00
|
|
|
(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
|
1999-08-17 21:37:32 +04:00
|
|
|
for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
|
|
|
|
fmt=",%u", lastid = id, cnt++) {
|
|
|
|
id = groups[cnt];
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
1998-12-19 19:55:21 +03:00
|
|
|
static void
|
1994-05-06 03:24:26 +04:00
|
|
|
group(pw, nflag)
|
|
|
|
struct passwd *pw;
|
|
|
|
int nflag;
|
|
|
|
{
|
|
|
|
struct group *gr;
|
|
|
|
int cnt, id, lastid, ngroups;
|
|
|
|
char *fmt;
|
|
|
|
|
|
|
|
if (pw) {
|
1998-12-19 19:55:21 +03:00
|
|
|
ngroups = maxgroups;
|
1994-05-06 03:24:26 +04:00
|
|
|
(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
|
|
|
|
} else {
|
|
|
|
groups[0] = getgid();
|
1998-12-19 19:55:21 +03:00
|
|
|
ngroups = getgroups(maxgroups, groups + 1) + 1;
|
1994-05-06 03:24:26 +04:00
|
|
|
}
|
|
|
|
fmt = nflag ? "%s" : "%u";
|
|
|
|
for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
|
|
|
|
if (lastid == (id = groups[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");
|
|
|
|
}
|
|
|
|
|
1994-05-06 03:24:26 +04:00
|
|
|
struct passwd *
|
1993-03-21 12:45:37 +03:00
|
|
|
who(u)
|
|
|
|
char *u;
|
|
|
|
{
|
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)
|
1993-03-21 12:45:37 +03:00
|
|
|
return(pw);
|
|
|
|
id = strtol(u, &ep, 10);
|
|
|
|
if (*u && !*ep && (pw = getpwuid(id)))
|
|
|
|
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 */
|
1997-10-19 06:45:44 +04:00
|
|
|
return (NULL);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
(void)fprintf(stderr, "usage: id [user]\n");
|
|
|
|
(void)fprintf(stderr, " id -G [-n] [user]\n");
|
|
|
|
(void)fprintf(stderr, " id -g [-nr] [user]\n");
|
1998-10-13 00:33:52 +04:00
|
|
|
(void)fprintf(stderr, " id -p\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)fprintf(stderr, " id -u [-nr] [user]\n");
|
|
|
|
exit(1);
|
|
|
|
}
|