id.c revision 144840
1/*-
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1991, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)id.c	8.2 (Berkeley) 2/16/94";
43#endif
44#endif /* not lint */
45#include <sys/cdefs.h>
46__FBSDID("$FreeBSD: head/usr.bin/id/id.c 144840 2005-04-09 14:31:41Z stefanf $");
47
48#include <sys/param.h>
49#include <sys/mac.h>
50
51#include <err.h>
52#include <errno.h>
53#include <grp.h>
54#include <pwd.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58#include <unistd.h>
59
60void	current(void);
61void	pline(struct passwd *);
62void	pretty(struct passwd *);
63void	group(struct passwd *, int);
64void	maclabel(void);
65void	usage(void);
66void	user(struct passwd *);
67struct passwd *
68	who(char *);
69
70int isgroups, iswhoami;
71
72int
73main(int argc, char *argv[])
74{
75	struct group *gr;
76	struct passwd *pw;
77	int Gflag, Mflag, Pflag, ch, gflag, id, nflag, pflag, rflag, uflag;
78	const char *myname;
79
80	Gflag = Mflag = Pflag = gflag = nflag = pflag = rflag = uflag = 0;
81
82	myname = strrchr(argv[0], '/');
83	myname = (myname != NULL) ? myname + 1 : argv[0];
84	if (strcmp(myname, "groups") == 0) {
85		isgroups = 1;
86		Gflag = nflag = 1;
87	}
88	else if (strcmp(myname, "whoami") == 0) {
89		iswhoami = 1;
90		uflag = nflag = 1;
91	}
92
93	while ((ch = getopt(argc, argv,
94	    (isgroups || iswhoami) ? "" : "PGMgnpru")) != -1)
95		switch(ch) {
96		case 'G':
97			Gflag = 1;
98			break;
99		case 'M':
100			Mflag = 1;
101			break;
102		case 'P':
103			Pflag = 1;
104			break;
105		case 'g':
106			gflag = 1;
107			break;
108		case 'n':
109			nflag = 1;
110			break;
111		case 'p':
112			pflag = 1;
113			break;
114		case 'r':
115			rflag = 1;
116			break;
117		case 'u':
118			uflag = 1;
119			break;
120		case '?':
121		default:
122			usage();
123		}
124	argc -= optind;
125	argv += optind;
126
127	if (iswhoami && argc > 0)
128		usage();
129
130	switch(Gflag + Pflag + gflag + pflag + uflag) {
131	case 1:
132		break;
133	case 0:
134		if (!nflag && !rflag)
135			break;
136		/* FALLTHROUGH */
137	default:
138		usage();
139	}
140
141	pw = *argv ? who(*argv) : NULL;
142
143	if (Mflag && pw != NULL)
144		usage();
145
146	if (gflag) {
147		id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
148		if (nflag && (gr = getgrgid(id)))
149			(void)printf("%s\n", gr->gr_name);
150		else
151			(void)printf("%u\n", id);
152		exit(0);
153	}
154
155	if (uflag) {
156		id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
157		if (nflag && (pw = getpwuid(id)))
158			(void)printf("%s\n", pw->pw_name);
159		else
160			(void)printf("%u\n", id);
161		exit(0);
162	}
163
164	if (Gflag) {
165		group(pw, nflag);
166		exit(0);
167	}
168
169	if (Mflag) {
170		maclabel();
171		exit(0);
172	}
173
174	if (Pflag) {
175		pline(pw);
176		exit(0);
177	}
178
179	if (pflag) {
180		pretty(pw);
181		exit(0);
182	}
183
184	if (pw)
185		user(pw);
186	else
187		current();
188	exit(0);
189}
190
191void
192pretty(struct passwd *pw)
193{
194	struct group *gr;
195	u_int eid, rid;
196	char *login;
197
198	if (pw) {
199		(void)printf("uid\t%s\n", pw->pw_name);
200		(void)printf("groups\t");
201		group(pw, 1);
202	} else {
203		if ((login = getlogin()) == NULL)
204			err(1, "getlogin");
205
206		pw = getpwuid(rid = getuid());
207		if (pw == NULL || strcmp(login, pw->pw_name))
208			(void)printf("login\t%s\n", login);
209		if (pw)
210			(void)printf("uid\t%s\n", pw->pw_name);
211		else
212			(void)printf("uid\t%u\n", rid);
213
214		if ((eid = geteuid()) != rid) {
215			if ((pw = getpwuid(eid)))
216				(void)printf("euid\t%s\n", pw->pw_name);
217			else
218				(void)printf("euid\t%u\n", eid);
219		}
220		if ((rid = getgid()) != (eid = getegid())) {
221			if ((gr = getgrgid(rid)))
222				(void)printf("rgid\t%s\n", gr->gr_name);
223			else
224				(void)printf("rgid\t%u\n", rid);
225		}
226		(void)printf("groups\t");
227		group(NULL, 1);
228	}
229}
230
231void
232current(void)
233{
234	struct group *gr;
235	struct passwd *pw;
236	int cnt, id, eid, lastid, ngroups;
237	gid_t groups[NGROUPS];
238	const char *fmt;
239
240	id = getuid();
241	(void)printf("uid=%u", id);
242	if ((pw = getpwuid(id)))
243		(void)printf("(%s)", pw->pw_name);
244	if ((eid = geteuid()) != id) {
245		(void)printf(" euid=%u", eid);
246		if ((pw = getpwuid(eid)))
247			(void)printf("(%s)", pw->pw_name);
248	}
249	id = getgid();
250	(void)printf(" gid=%u", id);
251	if ((gr = getgrgid(id)))
252		(void)printf("(%s)", gr->gr_name);
253	if ((eid = getegid()) != id) {
254		(void)printf(" egid=%u", eid);
255		if ((gr = getgrgid(eid)))
256			(void)printf("(%s)", gr->gr_name);
257	}
258	if ((ngroups = getgroups(NGROUPS, groups))) {
259		for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
260		    fmt = ", %u", lastid = id) {
261			id = groups[cnt++];
262			if (lastid == id)
263				continue;
264			(void)printf(fmt, id);
265			if ((gr = getgrgid(id)))
266				(void)printf("(%s)", gr->gr_name);
267		}
268	}
269	(void)printf("\n");
270}
271
272void
273user(struct passwd *pw)
274{
275	struct group *gr;
276	const char *fmt;
277	gid_t gid, lastgid, groups[NGROUPS + 1];
278	int cnt, ngroups;
279
280	(void)printf("uid=%u(%s)", pw->pw_uid, pw->pw_name);
281	gid = pw->pw_gid;
282	(void)printf(" gid=%u", gid);
283	if ((gr = getgrgid(gid)))
284		(void)printf("(%s)", gr->gr_name);
285	ngroups = NGROUPS + 1;
286	(void) getgrouplist(pw->pw_name, gid, groups, &ngroups);
287	fmt = " groups=%u";
288	for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) {
289		if (lastgid == (gid = groups[cnt]))
290			continue;
291		(void)printf(fmt, gid);
292		fmt = ", %u";
293		if ((gr = getgrgid(gid)))
294			(void)printf("(%s)", gr->gr_name);
295		lastgid = gid;
296	}
297	(void)printf("\n");
298}
299
300void
301group(struct passwd *pw, int nflag)
302{
303	struct group *gr;
304	int cnt, id, lastid, ngroups;
305	gid_t groups[NGROUPS + 1];
306	const char *fmt;
307
308	if (pw) {
309		ngroups = NGROUPS + 1;
310		(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
311	} else {
312		groups[0] = getgid();
313		ngroups = getgroups(NGROUPS, groups + 1) + 1;
314	}
315	fmt = nflag ? "%s" : "%u";
316	for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
317		if (lastid == (id = groups[cnt]))
318			continue;
319		if (nflag) {
320			if ((gr = getgrgid(id)))
321				(void)printf(fmt, gr->gr_name);
322			else
323				(void)printf(*fmt == ' ' ? " %u" : "%u",
324				    id);
325			fmt = " %s";
326		} else {
327			(void)printf(fmt, id);
328			fmt = " %u";
329		}
330		lastid = id;
331	}
332	(void)printf("\n");
333}
334
335void
336maclabel(void)
337{
338	char *string;
339	mac_t label;
340	int error;
341
342	error = mac_prepare_process_label(&label);
343	if (error == -1)
344		errx(1, "mac_prepare_type: %s", strerror(errno));
345
346	error = mac_get_proc(label);
347	if (error == -1)
348		errx(1, "mac_get_proc: %s", strerror(errno));
349
350	error = mac_to_text(label, &string);
351	if (error == -1)
352		errx(1, "mac_to_text: %s", strerror(errno));
353
354	(void)printf("%s\n", string);
355	mac_free(label);
356	free(string);
357}
358
359struct passwd *
360who(char *u)
361{
362	struct passwd *pw;
363	long id;
364	char *ep;
365
366	/*
367	 * Translate user argument into a pw pointer.  First, try to
368	 * get it as specified.  If that fails, try it as a number.
369	 */
370	if ((pw = getpwnam(u)))
371		return(pw);
372	id = strtol(u, &ep, 10);
373	if (*u && !*ep && (pw = getpwuid(id)))
374		return(pw);
375	errx(1, "%s: no such user", u);
376	/* NOTREACHED */
377}
378
379void
380pline(struct passwd *pw)
381{
382
383	if (!pw) {
384		if ((pw = getpwuid(getuid())) == NULL)
385			err(1, "getpwuid");
386	}
387
388	(void)printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw->pw_name,
389			pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
390			(long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos,
391			pw->pw_dir, pw->pw_shell);
392}
393
394
395void
396usage(void)
397{
398
399	if (isgroups)
400		(void)fprintf(stderr, "usage: groups [user]\n");
401	else if (iswhoami)
402		(void)fprintf(stderr, "usage: whoami\n");
403	else
404		(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
405		    "usage: id [user]",
406		    "       id -G [-n] [user]",
407		    "       id -M",
408		    "       id -P [user]",
409		    "       id -g [-nr] [user]",
410		    "       id -p [user]",
411		    "       id -u [-nr] [user]");
412	exit(1);
413}
414