xref: /freebsd/usr.bin/id/id.c (revision 941e286383714ef25f1ffe9ba6ae5040afdd7060)
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
35 static 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
42 static char sccsid[] = "@(#)id.c	8.2 (Berkeley) 2/16/94";
43 #endif
44 #endif /* not lint */
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
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 
64 void	id_print(struct passwd *, int, int, int);
65 void	pline(struct passwd *);
66 void	pretty(struct passwd *);
67 void	auditid(void);
68 void	group(struct passwd *, int);
69 void	maclabel(void);
70 void	usage(void);
71 struct passwd *who(char *);
72 
73 int isgroups, iswhoami;
74 
75 int
76 main(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 
214 void
215 pretty(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 
254 void
255 id_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 	long ngroups_max;
262 	gid_t *groups;
263 	const char *fmt;
264 
265 	if (pw != NULL) {
266 		uid = pw->pw_uid;
267 		gid = pw->pw_gid;
268 	}
269 	else {
270 		uid = getuid();
271 		gid = getgid();
272 	}
273 
274 	ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
275 	if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL)
276 		err(1, "malloc");
277 
278 	if (use_ggl && pw != NULL) {
279 		ngroups = ngroups_max;
280 		getgrouplist(pw->pw_name, gid, groups, &ngroups);
281 	}
282 	else {
283 		ngroups = getgroups(ngroups_max, groups);
284 	}
285 
286 	if (pw != NULL)
287 		printf("uid=%u(%s)", uid, pw->pw_name);
288 	else
289 		printf("uid=%u", getuid());
290 	printf(" gid=%u", gid);
291 	if ((gr = getgrgid(gid)))
292 		(void)printf("(%s)", gr->gr_name);
293 	if (p_euid && (euid = geteuid()) != uid) {
294 		(void)printf(" euid=%u", euid);
295 		if ((pw = getpwuid(euid)))
296 			(void)printf("(%s)", pw->pw_name);
297 	}
298 	if (p_egid && (egid = getegid()) != gid) {
299 		(void)printf(" egid=%u", egid);
300 		if ((gr = getgrgid(egid)))
301 			(void)printf("(%s)", gr->gr_name);
302 	}
303 	fmt = " groups=%u";
304 	for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) {
305 		if (lastgid == (gid = groups[cnt]))
306 			continue;
307 		printf(fmt, gid);
308 		fmt = ",%u";
309 		if ((gr = getgrgid(gid)))
310 			printf("(%s)", gr->gr_name);
311 		lastgid = gid;
312 	}
313 	printf("\n");
314 	free(groups);
315 }
316 
317 #ifdef USE_BSM_AUDIT
318 void
319 auditid(void)
320 {
321 	auditinfo_t auditinfo;
322 	auditinfo_addr_t ainfo_addr;
323 	int ret, extended;
324 
325 	extended = 0;
326 	ret = getaudit(&auditinfo);
327 	if (ret < 0 && errno == E2BIG) {
328 		if (getaudit_addr(&ainfo_addr, sizeof(ainfo_addr)) < 0)
329 			err(1, "getaudit_addr");
330 		extended = 1;
331 	} else if (ret < 0)
332 		err(1, "getaudit");
333 	if (extended != 0) {
334 		(void) printf("auid=%d\n"
335 		    "mask.success=0x%08x\n"
336 		    "mask.failure=0x%08x\n"
337 		    "asid=%d\n"
338 		    "termid_addr.port=0x%08x\n"
339 		    "termid_addr.addr[0]=0x%08x\n"
340 		    "termid_addr.addr[1]=0x%08x\n"
341 		    "termid_addr.addr[2]=0x%08x\n"
342 		    "termid_addr.addr[3]=0x%08x\n",
343 			ainfo_addr.ai_auid, ainfo_addr.ai_mask.am_success,
344 			ainfo_addr.ai_mask.am_failure, ainfo_addr.ai_asid,
345 			ainfo_addr.ai_termid.at_port,
346 			ainfo_addr.ai_termid.at_addr[0],
347 			ainfo_addr.ai_termid.at_addr[1],
348 			ainfo_addr.ai_termid.at_addr[2],
349 			ainfo_addr.ai_termid.at_addr[3]);
350 	} else {
351 		(void) printf("auid=%d\n"
352 		    "mask.success=0x%08x\n"
353 		    "mask.failure=0x%08x\n"
354 		    "asid=%d\n"
355 		    "termid.port=0x%08x\n"
356 		    "termid.machine=0x%08x\n",
357 			auditinfo.ai_auid, auditinfo.ai_mask.am_success,
358 			auditinfo.ai_mask.am_failure,
359 			auditinfo.ai_asid, auditinfo.ai_termid.port,
360 			auditinfo.ai_termid.machine);
361 	}
362 }
363 #endif
364 
365 void
366 group(struct passwd *pw, int nflag)
367 {
368 	struct group *gr;
369 	int cnt, id, lastid, ngroups;
370 	long ngroups_max;
371 	gid_t *groups;
372 	const char *fmt;
373 
374 	ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
375 	if ((groups = malloc(sizeof(gid_t) * (ngroups_max))) == NULL)
376 		err(1, "malloc");
377 
378 	if (pw) {
379 		ngroups = ngroups_max;
380 		(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
381 	} else {
382 		ngroups = getgroups(ngroups_max, groups);
383 	}
384 	fmt = nflag ? "%s" : "%u";
385 	for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
386 		if (lastid == (id = groups[cnt]))
387 			continue;
388 		if (nflag) {
389 			if ((gr = getgrgid(id)))
390 				(void)printf(fmt, gr->gr_name);
391 			else
392 				(void)printf(*fmt == ' ' ? " %u" : "%u",
393 				    id);
394 			fmt = " %s";
395 		} else {
396 			(void)printf(fmt, id);
397 			fmt = " %u";
398 		}
399 		lastid = id;
400 	}
401 	(void)printf("\n");
402 	free(groups);
403 }
404 
405 void
406 maclabel(void)
407 {
408 	char *string;
409 	mac_t label;
410 	int error;
411 
412 	error = mac_prepare_process_label(&label);
413 	if (error == -1)
414 		errx(1, "mac_prepare_type: %s", strerror(errno));
415 
416 	error = mac_get_proc(label);
417 	if (error == -1)
418 		errx(1, "mac_get_proc: %s", strerror(errno));
419 
420 	error = mac_to_text(label, &string);
421 	if (error == -1)
422 		errx(1, "mac_to_text: %s", strerror(errno));
423 
424 	(void)printf("%s\n", string);
425 	mac_free(label);
426 	free(string);
427 }
428 
429 struct passwd *
430 who(char *u)
431 {
432 	struct passwd *pw;
433 	long id;
434 	char *ep;
435 
436 	/*
437 	 * Translate user argument into a pw pointer.  First, try to
438 	 * get it as specified.  If that fails, try it as a number.
439 	 */
440 	if ((pw = getpwnam(u)))
441 		return(pw);
442 	id = strtol(u, &ep, 10);
443 	if (*u && !*ep && (pw = getpwuid(id)))
444 		return(pw);
445 	errx(1, "%s: no such user", u);
446 	/* NOTREACHED */
447 }
448 
449 void
450 pline(struct passwd *pw)
451 {
452 
453 	if (!pw) {
454 		if ((pw = getpwuid(getuid())) == NULL)
455 			err(1, "getpwuid");
456 	}
457 
458 	(void)printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw->pw_name,
459 			pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
460 			(long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos,
461 			pw->pw_dir, pw->pw_shell);
462 }
463 
464 
465 void
466 usage(void)
467 {
468 
469 	if (isgroups)
470 		(void)fprintf(stderr, "usage: groups [user]\n");
471 	else if (iswhoami)
472 		(void)fprintf(stderr, "usage: whoami\n");
473 	else
474 		(void)fprintf(stderr, "%s\n%s%s\n%s\n%s\n%s\n%s\n%s\n",
475 		    "usage: id [user]",
476 #ifdef USE_BSM_AUDIT
477 		    "       id -A\n",
478 #else
479 		    "",
480 #endif
481 		    "       id -G [-n] [user]",
482 		    "       id -M",
483 		    "       id -P [user]",
484 		    "       id -g [-nr] [user]",
485 		    "       id -p [user]",
486 		    "       id -u [-nr] [user]");
487 	exit(1);
488 }
489