xref: /freebsd/usr.bin/id/id.c (revision 2546665afcaf0d53dc2c7058fee96354b3680f5a)
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 #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 
60 void	current(void);
61 void	pline(struct passwd *);
62 void	pretty(struct passwd *);
63 void	group(struct passwd *, int);
64 void	maclabel(void);
65 void	usage(void);
66 void	user(struct passwd *);
67 struct passwd *
68 	who(char *);
69 
70 int isgroups, iswhoami;
71 
72 int
73 main(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 
191 void
192 pretty(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 
231 void
232 current(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 
272 void
273 user(struct passwd *pw)
274 {
275 	struct group *gr;
276 	const char *fmt;
277 	int cnt, gid, lastgid, ngroups, groups[NGROUPS + 1];
278 
279 	(void)printf("uid=%u(%s)", pw->pw_uid, pw->pw_name);
280 	gid = pw->pw_gid;
281 	(void)printf(" gid=%u", gid);
282 	if ((gr = getgrgid(gid)))
283 		(void)printf("(%s)", gr->gr_name);
284 	ngroups = NGROUPS + 1;
285 	(void) getgrouplist(pw->pw_name, gid, groups, &ngroups);
286 	fmt = " groups=%u";
287 	for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) {
288 		if (lastgid == (gid = groups[cnt]))
289 			continue;
290 		(void)printf(fmt, gid);
291 		fmt = ", %u";
292 		if ((gr = getgrgid(gid)))
293 			(void)printf("(%s)", gr->gr_name);
294 		lastgid = gid;
295 	}
296 	(void)printf("\n");
297 }
298 
299 void
300 group(struct passwd *pw, int nflag)
301 {
302 	struct group *gr;
303 	int cnt, id, lastid, ngroups;
304 	gid_t groups[NGROUPS + 1];
305 	const char *fmt;
306 
307 	if (pw) {
308 		ngroups = NGROUPS + 1;
309 		(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
310 	} else {
311 		groups[0] = getgid();
312 		ngroups = getgroups(NGROUPS, groups + 1) + 1;
313 	}
314 	fmt = nflag ? "%s" : "%u";
315 	for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
316 		if (lastid == (id = groups[cnt]))
317 			continue;
318 		if (nflag) {
319 			if ((gr = getgrgid(id)))
320 				(void)printf(fmt, gr->gr_name);
321 			else
322 				(void)printf(*fmt == ' ' ? " %u" : "%u",
323 				    id);
324 			fmt = " %s";
325 		} else {
326 			(void)printf(fmt, id);
327 			fmt = " %u";
328 		}
329 		lastid = id;
330 	}
331 	(void)printf("\n");
332 }
333 
334 void
335 maclabel(void)
336 {
337 	char *string;
338 	mac_t label;
339 	int error;
340 
341 	error = mac_prepare_process_label(&label);
342 	if (error == -1)
343 		errx(1, "mac_prepare_type: %s", strerror(errno));
344 
345 	error = mac_get_proc(label);
346 	if (error == -1)
347 		errx(1, "mac_get_proc: %s", strerror(errno));
348 
349 	error = mac_to_text(label, &string);
350 	if (error == -1)
351 		errx(1, "mac_to_text: %s", strerror(errno));
352 
353 	(void)printf("%s\n", string);
354 	mac_free(label);
355 	free(string);
356 }
357 
358 struct passwd *
359 who(char *u)
360 {
361 	struct passwd *pw;
362 	long id;
363 	char *ep;
364 
365 	/*
366 	 * Translate user argument into a pw pointer.  First, try to
367 	 * get it as specified.  If that fails, try it as a number.
368 	 */
369 	if ((pw = getpwnam(u)))
370 		return(pw);
371 	id = strtol(u, &ep, 10);
372 	if (*u && !*ep && (pw = getpwuid(id)))
373 		return(pw);
374 	errx(1, "%s: no such user", u);
375 	/* NOTREACHED */
376 }
377 
378 void
379 pline(struct passwd *pw)
380 {
381 	u_int rid;
382 
383 	if (!pw) {
384 		if ((pw = getpwuid(rid = 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 
395 void
396 usage(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