NetBSD/usr.bin/id/id.c

371 lines
8.6 KiB
C
Raw Normal View History

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.
* 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.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1991, 1993\
The Regents of the University of California. All rights reserved.");
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)id.c 8.3 (Berkeley) 4/28/95";
#else
2011-09-16 19:39:25 +04:00
__RCSID("$NetBSD: id.c,v 1.32 2011/09/16 15:39:26 joerg Exp $");
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
#include <sys/param.h>
1994-05-06 03:24:26 +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);
2011-09-16 19:39:25 +04:00
__dead static void usage(void);
2004-11-22 20:28:58 +03:00
static void user(struct passwd *);
static struct passwd *who(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
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;
int ch, id;
int Gflag, gflag, nflag, pflag, rflag, uflag;
const char *opts;
1993-03-21 12:45:37 +03:00
1994-05-06 03:24:26 +04:00
Gflag = gflag = nflag = pflag = rflag = uflag = 0;
2004-11-22 20:28:58 +03:00
if (strcmp(getprogname(), "groups") == 0) {
Gflag = 1;
nflag = 1;
opts = "";
if (argc > 2)
usage();
2004-11-22 20:28:58 +03:00
} else if (strcmp(getprogname(), "whoami") == 0) {
uflag = 1;
nflag = 1;
opts = "";
if (argc > 1)
usage();
} else
opts = "Ggnpru";
while ((ch = getopt(argc, argv, opts)) != -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();
}
if (strcmp(opts, "") != 0 && argc > 1)
usage();
1994-05-06 03:24:26 +04:00
pw = *argv ? who(*argv) : NULL;
maxgroups = sysconf(_SC_NGROUPS_MAX);
if ((groups = malloc((maxgroups + 1) * sizeof(gid_t))) == NULL)
err(1, NULL);
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);
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);
goto done;
1994-05-06 03:24:26 +04:00
}
if (Gflag) {
group(pw, nflag);
goto done;
1994-05-06 03:24:26 +04:00
}
if (pflag) {
pretty(pw);
goto done;
1993-03-21 12:45:37 +03:00
}
if (pw)
user(pw);
else
current();
done:
free(groups);
2004-11-22 20:28:58 +03:00
return 0;
1993-03-21 12:45:37 +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)
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);
if ((eid = geteuid()) != rid) {
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);
}
if ((rid = getgid()) != (eid = getegid())) {
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);
}
1994-05-06 03:24:26 +04:00
(void)printf("groups\t");
group(NULL, 1);
1993-03-21 12:45:37 +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;
gid_t gid, egid, lastid;
uid_t uid, euid;
int cnt, ngroups;
2004-11-22 20:31:38 +03:00
const char *fmt;
1993-03-21 12:45:37 +03:00
uid = getuid();
(void)printf("uid=%ju", (uintmax_t)uid);
if ((pw = getpwuid(uid)) != NULL)
1993-03-21 12:45:37 +03:00
(void)printf("(%s)", pw->pw_name);
gid = getgid();
(void)printf(" gid=%ju", (uintmax_t)gid);
if ((gr = getgrgid(gid)) != NULL)
(void)printf("(%s)", gr->gr_name);
if ((euid = geteuid()) != uid) {
(void)printf(" euid=%ju", (uintmax_t)euid);
if ((pw = getpwuid(euid)) != NULL)
1993-03-21 12:45:37 +03:00
(void)printf("(%s)", pw->pw_name);
}
if ((egid = getegid()) != gid) {
(void)printf(" egid=%ju", (uintmax_t)egid);
if ((gr = getgrgid(egid)) != 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) {
for (fmt = " groups=%ju", lastid = -1, cnt = 0; cnt < ngroups;
fmt = ",%ju", lastid = gid, cnt++) {
gid = groups[cnt];
if (lastid == gid)
1993-03-21 12:45:37 +03:00
continue;
(void)printf(fmt, (uintmax_t)gid);
if ((gr = getgrgid(gid)) != NULL)
1993-03-21 12:45:37 +03:00
(void)printf("(%s)", gr->gr_name);
}
}
(void)printf("\n");
}
static void
2004-11-22 20:28:58 +03:00
user(struct passwd *pw)
1993-03-21 12:45:37 +03:00
{
struct group *gr;
2004-11-22 20:31:38 +03:00
const char *fmt;
int cnt, id, lastid, ngroups;
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);
(void)printf(" gid=%lu", (u_long)pw->pw_gid);
if ((gr = getgrgid(pw->pw_gid)) != NULL)
1993-03-21 12:45:37 +03:00
(void)printf("(%s)", gr->gr_name);
ngroups = maxgroups + 1;
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);
}
for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
fmt=",%u", lastid = id, cnt++) {
id = glist[cnt];
if (lastid == id)
1994-05-06 03:24:26 +04:00
continue;
(void)printf(fmt, id);
if ((gr = getgrgid(id)) != NULL)
1994-05-06 03:24:26 +04:00
(void)printf("(%s)", gr->gr_name);
}
(void)printf("\n");
if (glist != groups)
free(glist);
1994-05-06 03:24:26 +04: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;
2009-04-12 14:51:38 +04:00
int cnt, ngroups;
gid_t id, lastid;
2004-11-22 20:31:38 +03:00
const char *fmt;
gid_t *glist = groups;
1994-05-06 03:24:26 +04:00
if (pw) {
ngroups = maxgroups;
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 {
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) {
if (lastid == (id = glist[cnt]) || (cnt && id == glist[0]))
1993-03-21 12:45:37 +03:00
continue;
1994-05-06 03:24:26 +04:00
if (nflag) {
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");
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.
*/
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;
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
{
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);
}