id.c revision 165028
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 165028 2006-12-09 12:58:14Z mpp $");
47
48#include <sys/param.h>
49#include <sys/mac.h>
50
51#ifdef USE_BSM_AUDIT
52#include <bsm/audit.h>
53#endif
54
55#include <err.h>
56#include <errno.h>
57#include <grp.h>
58#include <pwd.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#include <unistd.h>
63
64void	id_print(struct passwd *, int, int, int);
65void	pline(struct passwd *);
66void	pretty(struct passwd *);
67void	auditid(void);
68void	group(struct passwd *, int);
69void	maclabel(void);
70void	usage(void);
71struct passwd *who(char *);
72
73int isgroups, iswhoami;
74
75int
76main(int argc, char *argv[])
77{
78	struct group *gr;
79	struct passwd *pw;
80	int Gflag, Mflag, Pflag, ch, gflag, id, nflag, pflag, rflag, uflag;
81	int Aflag;
82	const char *myname;
83
84	Gflag = Mflag = Pflag = gflag = nflag = pflag = rflag = uflag = 0;
85	Aflag = 0;
86
87	myname = strrchr(argv[0], '/');
88	myname = (myname != NULL) ? myname + 1 : argv[0];
89	if (strcmp(myname, "groups") == 0) {
90		isgroups = 1;
91		Gflag = nflag = 1;
92	}
93	else if (strcmp(myname, "whoami") == 0) {
94		iswhoami = 1;
95		uflag = nflag = 1;
96	}
97
98	while ((ch = getopt(argc, argv,
99	    (isgroups || iswhoami) ? "" : "APGMagnpru")) != -1)
100		switch(ch) {
101#ifdef USE_BSM_AUDIT
102		case 'A':
103			Aflag = 1;
104			break;
105#endif
106		case 'G':
107			Gflag = 1;
108			break;
109		case 'M':
110			Mflag = 1;
111			break;
112		case 'P':
113			Pflag = 1;
114			break;
115		case 'a':
116			break;
117		case 'g':
118			gflag = 1;
119			break;
120		case 'n':
121			nflag = 1;
122			break;
123		case 'p':
124			pflag = 1;
125			break;
126		case 'r':
127			rflag = 1;
128			break;
129		case 'u':
130			uflag = 1;
131			break;
132		case '?':
133		default:
134			usage();
135		}
136	argc -= optind;
137	argv += optind;
138
139	if (iswhoami && argc > 0)
140		usage();
141
142	switch(Aflag + Gflag + Mflag + Pflag + gflag + pflag + uflag) {
143	case 1:
144		break;
145	case 0:
146		if (!nflag && !rflag)
147			break;
148		/* FALLTHROUGH */
149	default:
150		usage();
151	}
152
153	pw = *argv ? who(*argv) : NULL;
154
155	if (Mflag && pw != NULL)
156		usage();
157
158#ifdef USE_BSM_AUDIT
159	if (Aflag) {
160		auditid();
161		exit(0);
162	}
163#endif
164
165	if (gflag) {
166		id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
167		if (nflag && (gr = getgrgid(id)))
168			(void)printf("%s\n", gr->gr_name);
169		else
170			(void)printf("%u\n", id);
171		exit(0);
172	}
173
174	if (uflag) {
175		id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
176		if (nflag && (pw = getpwuid(id)))
177			(void)printf("%s\n", pw->pw_name);
178		else
179			(void)printf("%u\n", id);
180		exit(0);
181	}
182
183	if (Gflag) {
184		group(pw, nflag);
185		exit(0);
186	}
187
188	if (Mflag) {
189		maclabel();
190		exit(0);
191	}
192
193	if (Pflag) {
194		pline(pw);
195		exit(0);
196	}
197
198	if (pflag) {
199		pretty(pw);
200		exit(0);
201	}
202
203	if (pw) {
204		id_print(pw, 1, 0, 0);
205	}
206	else {
207		id = getuid();
208		pw = getpwuid(id);
209		id_print(pw, 0, 1, 1);
210	}
211	exit(0);
212}
213
214void
215pretty(struct passwd *pw)
216{
217	struct group *gr;
218	u_int eid, rid;
219	char *login;
220
221	if (pw) {
222		(void)printf("uid\t%s\n", pw->pw_name);
223		(void)printf("groups\t");
224		group(pw, 1);
225	} else {
226		if ((login = getlogin()) == NULL)
227			err(1, "getlogin");
228
229		pw = getpwuid(rid = getuid());
230		if (pw == NULL || strcmp(login, pw->pw_name))
231			(void)printf("login\t%s\n", login);
232		if (pw)
233			(void)printf("uid\t%s\n", pw->pw_name);
234		else
235			(void)printf("uid\t%u\n", rid);
236
237		if ((eid = geteuid()) != rid) {
238			if ((pw = getpwuid(eid)))
239				(void)printf("euid\t%s\n", pw->pw_name);
240			else
241				(void)printf("euid\t%u\n", eid);
242		}
243		if ((rid = getgid()) != (eid = getegid())) {
244			if ((gr = getgrgid(rid)))
245				(void)printf("rgid\t%s\n", gr->gr_name);
246			else
247				(void)printf("rgid\t%u\n", rid);
248		}
249		(void)printf("groups\t");
250		group(NULL, 1);
251	}
252}
253
254void
255id_print(struct passwd *pw, int use_ggl, int p_euid, int p_egid)
256{
257	struct group *gr;
258	gid_t gid, egid, lastgid;
259	uid_t uid, euid;
260	int cnt, ngroups;
261	gid_t groups[NGROUPS + 1];
262	const char *fmt;
263
264	if (pw != NULL) {
265		uid = pw->pw_uid;
266		gid = pw->pw_gid;
267	}
268	else {
269		uid = getuid();
270		gid = getgid();
271	}
272
273	if (use_ggl && pw != NULL) {
274		ngroups = NGROUPS + 1;
275		getgrouplist(pw->pw_name, gid, groups, &ngroups);
276	}
277	else {
278		ngroups = getgroups(NGROUPS + 1, groups);
279	}
280
281	if (pw != NULL)
282		printf("uid=%u(%s)", uid, pw->pw_name);
283	else
284		printf("uid=%u", getuid());
285	printf(" gid=%u", gid);
286	if ((gr = getgrgid(gid)))
287		(void)printf("(%s)", gr->gr_name);
288	if (p_euid && (euid = geteuid()) != uid) {
289		(void)printf(" euid=%u", euid);
290		if ((pw = getpwuid(euid)))
291			(void)printf("(%s)", pw->pw_name);
292	}
293	if (p_egid && (egid = getegid()) != gid) {
294		(void)printf(" egid=%u", egid);
295		if ((gr = getgrgid(egid)))
296			(void)printf("(%s)", gr->gr_name);
297	}
298	fmt = " groups=%u";
299	for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) {
300		if (lastgid == (gid = groups[cnt]))
301			continue;
302		printf(fmt, gid);
303		fmt = ", %u";
304		if ((gr = getgrgid(gid)))
305			printf("(%s)", gr->gr_name);
306		lastgid = gid;
307	}
308	printf("\n");
309}
310
311#ifdef USE_BSM_AUDIT
312void
313auditid(void)
314{
315	auditinfo_t auditinfo;
316
317	if (getaudit(&auditinfo) < 0)
318		err(1, "getaudit");
319	printf("auid=%d\n", auditinfo.ai_auid);
320	printf("mask.success=0x%08x\n", auditinfo.ai_mask.am_success);
321	printf("mask.failure=0x%08x\n", auditinfo.ai_mask.am_failure);
322	printf("termid.port=0x%08x\n", auditinfo.ai_termid.port);
323	printf("asid=%d\n", auditinfo.ai_asid);
324}
325#endif
326
327void
328group(struct passwd *pw, int nflag)
329{
330	struct group *gr;
331	int cnt, id, lastid, ngroups;
332	gid_t groups[NGROUPS + 1];
333	const char *fmt;
334
335	if (pw) {
336		ngroups = NGROUPS + 1;
337		(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
338	} else {
339		groups[0] = getgid();
340		ngroups = getgroups(NGROUPS, groups + 1) + 1;
341	}
342	fmt = nflag ? "%s" : "%u";
343	for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
344		if (lastid == (id = groups[cnt]))
345			continue;
346		if (nflag) {
347			if ((gr = getgrgid(id)))
348				(void)printf(fmt, gr->gr_name);
349			else
350				(void)printf(*fmt == ' ' ? " %u" : "%u",
351				    id);
352			fmt = " %s";
353		} else {
354			(void)printf(fmt, id);
355			fmt = " %u";
356		}
357		lastid = id;
358	}
359	(void)printf("\n");
360}
361
362void
363maclabel(void)
364{
365	char *string;
366	mac_t label;
367	int error;
368
369	error = mac_prepare_process_label(&label);
370	if (error == -1)
371		errx(1, "mac_prepare_type: %s", strerror(errno));
372
373	error = mac_get_proc(label);
374	if (error == -1)
375		errx(1, "mac_get_proc: %s", strerror(errno));
376
377	error = mac_to_text(label, &string);
378	if (error == -1)
379		errx(1, "mac_to_text: %s", strerror(errno));
380
381	(void)printf("%s\n", string);
382	mac_free(label);
383	free(string);
384}
385
386struct passwd *
387who(char *u)
388{
389	struct passwd *pw;
390	long id;
391	char *ep;
392
393	/*
394	 * Translate user argument into a pw pointer.  First, try to
395	 * get it as specified.  If that fails, try it as a number.
396	 */
397	if ((pw = getpwnam(u)))
398		return(pw);
399	id = strtol(u, &ep, 10);
400	if (*u && !*ep && (pw = getpwuid(id)))
401		return(pw);
402	errx(1, "%s: no such user", u);
403	/* NOTREACHED */
404}
405
406void
407pline(struct passwd *pw)
408{
409
410	if (!pw) {
411		if ((pw = getpwuid(getuid())) == NULL)
412			err(1, "getpwuid");
413	}
414
415	(void)printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw->pw_name,
416			pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
417			(long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos,
418			pw->pw_dir, pw->pw_shell);
419}
420
421
422void
423usage(void)
424{
425
426	if (isgroups)
427		(void)fprintf(stderr, "usage: groups [user]\n");
428	else if (iswhoami)
429		(void)fprintf(stderr, "usage: whoami\n");
430	else
431		(void)fprintf(stderr, "%s\n%s%s\n%s\n%s\n%s\n%s\n%s\n",
432		    "usage: id [user]",
433#ifdef USE_BSM_AUDIT
434		    "       id -A\n",
435#else
436		    "",
437#endif
438		    "       id -G [-n] [user]",
439		    "       id -M",
440		    "       id -P [user]",
441		    "       id -g [-nr] [user]",
442		    "       id -p [user]",
443		    "       id -u [-nr] [user]");
444	exit(1);
445}
446