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