xref: /freebsd/usr.bin/id/id.c (revision 195ebc7e9e4b129de810833791a19dfb4349d6a9)
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 	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
312 void
313 auditid(void)
314 {
315 	auditinfo_t auditinfo;
316 	auditinfo_addr_t ainfo_addr;
317 	int ret, extended;
318 
319 	extended = 0;
320 	ret = getaudit(&auditinfo);
321 	if (ret < 0 && errno == E2BIG) {
322 		if (getaudit_addr(&ainfo_addr, sizeof(ainfo_addr)) < 0)
323 			err(1, "getaudit_addr");
324 		extended = 1;
325 	} else if (ret < 0)
326 		err(1, "getaudit");
327 	if (extended != 0) {
328 		(void) printf("auid=%d\n"
329 		    "mask.success=0x%08x\n"
330 		    "mask.failure=0x%08x\n"
331 		    "asid=%d\n"
332 		    "termid_addr.port=0x%08x\n"
333 		    "termid_addr.addr[0]=0x%08x\n"
334 		    "termid_addr.addr[1]=0x%08x\n"
335 		    "termid_addr.addr[2]=0x%08x\n"
336 		    "termid_addr.addr[3]=0x%08x\n",
337 			ainfo_addr.ai_auid, ainfo_addr.ai_mask.am_success,
338 			ainfo_addr.ai_mask.am_failure, ainfo_addr.ai_asid,
339 			ainfo_addr.ai_termid.at_port,
340 			ainfo_addr.ai_termid.at_addr[0],
341 			ainfo_addr.ai_termid.at_addr[1],
342 			ainfo_addr.ai_termid.at_addr[2],
343 			ainfo_addr.ai_termid.at_addr[3]);
344 	} else {
345 		(void) printf("auid=%d\n"
346 		    "mask.success=0x%08x\n"
347 		    "mask.failure=0x%08x\n"
348 		    "asid=%d\n"
349 		    "termid.port=0x%08x\n"
350 		    "termid.machine=0x%08x\n",
351 			auditinfo.ai_auid, auditinfo.ai_mask.am_success,
352 			auditinfo.ai_mask.am_failure,
353 			auditinfo.ai_asid, auditinfo.ai_termid.port,
354 			auditinfo.ai_termid.machine);
355 	}
356 }
357 #endif
358 
359 void
360 group(struct passwd *pw, int nflag)
361 {
362 	struct group *gr;
363 	int cnt, id, lastid, ngroups;
364 	gid_t groups[NGROUPS + 1];
365 	const char *fmt;
366 
367 	if (pw) {
368 		ngroups = NGROUPS + 1;
369 		(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
370 	} else {
371 		groups[0] = getgid();
372 		ngroups = getgroups(NGROUPS, groups + 1) + 1;
373 	}
374 	fmt = nflag ? "%s" : "%u";
375 	for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
376 		if (lastid == (id = groups[cnt]))
377 			continue;
378 		if (nflag) {
379 			if ((gr = getgrgid(id)))
380 				(void)printf(fmt, gr->gr_name);
381 			else
382 				(void)printf(*fmt == ' ' ? " %u" : "%u",
383 				    id);
384 			fmt = " %s";
385 		} else {
386 			(void)printf(fmt, id);
387 			fmt = " %u";
388 		}
389 		lastid = id;
390 	}
391 	(void)printf("\n");
392 }
393 
394 void
395 maclabel(void)
396 {
397 	char *string;
398 	mac_t label;
399 	int error;
400 
401 	error = mac_prepare_process_label(&label);
402 	if (error == -1)
403 		errx(1, "mac_prepare_type: %s", strerror(errno));
404 
405 	error = mac_get_proc(label);
406 	if (error == -1)
407 		errx(1, "mac_get_proc: %s", strerror(errno));
408 
409 	error = mac_to_text(label, &string);
410 	if (error == -1)
411 		errx(1, "mac_to_text: %s", strerror(errno));
412 
413 	(void)printf("%s\n", string);
414 	mac_free(label);
415 	free(string);
416 }
417 
418 struct passwd *
419 who(char *u)
420 {
421 	struct passwd *pw;
422 	long id;
423 	char *ep;
424 
425 	/*
426 	 * Translate user argument into a pw pointer.  First, try to
427 	 * get it as specified.  If that fails, try it as a number.
428 	 */
429 	if ((pw = getpwnam(u)))
430 		return(pw);
431 	id = strtol(u, &ep, 10);
432 	if (*u && !*ep && (pw = getpwuid(id)))
433 		return(pw);
434 	errx(1, "%s: no such user", u);
435 	/* NOTREACHED */
436 }
437 
438 void
439 pline(struct passwd *pw)
440 {
441 
442 	if (!pw) {
443 		if ((pw = getpwuid(getuid())) == NULL)
444 			err(1, "getpwuid");
445 	}
446 
447 	(void)printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw->pw_name,
448 			pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
449 			(long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos,
450 			pw->pw_dir, pw->pw_shell);
451 }
452 
453 
454 void
455 usage(void)
456 {
457 
458 	if (isgroups)
459 		(void)fprintf(stderr, "usage: groups [user]\n");
460 	else if (iswhoami)
461 		(void)fprintf(stderr, "usage: whoami\n");
462 	else
463 		(void)fprintf(stderr, "%s\n%s%s\n%s\n%s\n%s\n%s\n%s\n",
464 		    "usage: id [user]",
465 #ifdef USE_BSM_AUDIT
466 		    "       id -A\n",
467 #else
468 		    "",
469 #endif
470 		    "       id -G [-n] [user]",
471 		    "       id -M",
472 		    "       id -P [user]",
473 		    "       id -g [-nr] [user]",
474 		    "       id -p [user]",
475 		    "       id -u [-nr] [user]");
476 	exit(1);
477 }
478