xref: /openbsd-src/usr.bin/id/id.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: id.c,v 1.19 2009/10/27 23:59:39 deraadt Exp $	*/
2 
3 /*-
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/param.h>
33 
34 #include <errno.h>
35 #include <grp.h>
36 #include <pwd.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <err.h>
42 
43 void	current(void);
44 void	pretty(struct passwd *);
45 void	group(struct passwd *, int);
46 void	usage(void);
47 void	user(struct passwd *);
48 struct passwd *
49 	who(char *);
50 
51 int
52 main(int argc, char *argv[])
53 {
54 	struct group *gr;
55 	struct passwd *pw;
56 	int Gflag, ch, gflag, nflag, pflag, rflag, uflag;
57 	uid_t uid;
58 	gid_t gid;
59 
60 	Gflag = gflag = nflag = pflag = rflag = uflag = 0;
61 	while ((ch = getopt(argc, argv, "Ggnpru")) != -1)
62 		switch(ch) {
63 		case 'G':
64 			Gflag = 1;
65 			break;
66 		case 'g':
67 			gflag = 1;
68 			break;
69 		case 'n':
70 			nflag = 1;
71 			break;
72 		case 'p':
73 			pflag = 1;
74 			break;
75 		case 'r':
76 			rflag = 1;
77 			break;
78 		case 'u':
79 			uflag = 1;
80 			break;
81 		case '?':
82 		default:
83 			usage();
84 		}
85 	argc -= optind;
86 	argv += optind;
87 
88 	switch(Gflag + gflag + pflag + uflag) {
89 	case 1:
90 		break;
91 	case 0:
92 		if (!nflag && !rflag)
93 			break;
94 		/* FALLTHROUGH */
95 	default:
96 		usage();
97 	}
98 
99 	pw = *argv ? who(*argv) : NULL;
100 
101 	if (gflag) {
102 		gid = pw ? pw->pw_gid : rflag ? getgid() : getegid();
103 		if (nflag && (gr = getgrgid(gid)))
104 			(void)printf("%s\n", gr->gr_name);
105 		else
106 			(void)printf("%u\n", gid);
107 		exit(0);
108 	}
109 
110 	if (uflag) {
111 		uid = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
112 		if (nflag && (pw = getpwuid(uid)))
113 			(void)printf("%s\n", pw->pw_name);
114 		else
115 			(void)printf("%u\n", uid);
116 		exit(0);
117 	}
118 
119 	if (Gflag) {
120 		group(pw, nflag);
121 		exit(0);
122 	}
123 
124 	if (pflag) {
125 		pretty(pw);
126 		exit(0);
127 	}
128 
129 	if (pw)
130 		user(pw);
131 	else
132 		current();
133 	exit(0);
134 }
135 
136 void
137 pretty(struct passwd *pw)
138 {
139 	struct group *gr;
140 	uid_t eid, rid;
141 	char *login;
142 
143 	if (pw) {
144 		(void)printf("uid\t%s\n", pw->pw_name);
145 		(void)printf("groups\t");
146 		group(pw, 1);
147 	} else {
148 		if ((login = getlogin()) == NULL)
149 			err(1, "getlogin");
150 
151 		pw = getpwuid(rid = getuid());
152 		if (pw == NULL || strcmp(login, pw->pw_name))
153 			(void)printf("login\t%s\n", login);
154 		if (pw)
155 			(void)printf("uid\t%s\n", pw->pw_name);
156 		else
157 			(void)printf("uid\t%u\n", rid);
158 
159 		if ((eid = geteuid()) != rid) {
160 			if ((pw = getpwuid(eid)))
161 				(void)printf("euid\t%s\n", pw->pw_name);
162 			else
163 				(void)printf("euid\t%u\n", eid);
164 		}
165 		if ((rid = getgid()) != (eid = getegid())) {
166 			if ((gr = getgrgid(rid)))
167 				(void)printf("rgid\t%s\n", gr->gr_name);
168 			else
169 				(void)printf("rgid\t%u\n", rid);
170 		}
171 		(void)printf("groups\t");
172 		group(NULL, 1);
173 	}
174 }
175 
176 void
177 current(void)
178 {
179 	struct group *gr;
180 	struct passwd *pw;
181 	int cnt, ngroups;
182 	uid_t uid, euid;
183 	gid_t groups[NGROUPS], gid, egid, lastgid;
184 	char *fmt;
185 
186 	uid = getuid();
187 	(void)printf("uid=%u", uid);
188 	if ((pw = getpwuid(uid)))
189 		(void)printf("(%s)", pw->pw_name);
190 	if ((euid = geteuid()) != uid) {
191 		(void)printf(" euid=%u", euid);
192 		if ((pw = getpwuid(euid)))
193 			(void)printf("(%s)", pw->pw_name);
194 	}
195 	gid = getgid();
196 	(void)printf(" gid=%u", gid);
197 	if ((gr = getgrgid(gid)))
198 		(void)printf("(%s)", gr->gr_name);
199 	if ((egid = getegid()) != gid) {
200 		(void)printf(" egid=%u", egid);
201 		if ((gr = getgrgid(egid)))
202 			(void)printf("(%s)", gr->gr_name);
203 	}
204 	if ((ngroups = getgroups(NGROUPS, groups))) {
205 		for (fmt = " groups=%u", lastgid = (gid_t)-1, cnt = 0;
206 		    cnt < ngroups; fmt = ", %u", lastgid = gid) {
207 			gid = groups[cnt++];
208 			if (lastgid == gid)
209 				continue;
210 			(void)printf(fmt, gid);
211 			if ((gr = getgrgid(gid)))
212 				(void)printf("(%s)", gr->gr_name);
213 		}
214 	}
215 	(void)printf("\n");
216 }
217 
218 void
219 user(struct passwd *pw)
220 {
221 	gid_t gid, groups[NGROUPS + 1];
222 	int cnt, ngroups;
223 	uid_t uid;
224 	struct group *gr;
225 	char *fmt;
226 
227 	uid = pw->pw_uid;
228 	(void)printf("uid=%u(%s)", uid, pw->pw_name);
229 	(void)printf(" gid=%u", pw->pw_gid);
230 	if ((gr = getgrgid(pw->pw_gid)))
231 		(void)printf("(%s)", gr->gr_name);
232 	ngroups = NGROUPS + 1;
233 	(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
234 	fmt = " groups=%u";
235 	for (cnt = 0; cnt < ngroups;) {
236 		gid = groups[cnt];
237 		(void)printf(fmt, gid);
238 		fmt = ", %u";
239 		if ((gr = getgrgid(gid)))
240 			(void)printf("(%s)", gr->gr_name);
241 		/* Skip same gid entries. */
242 		while (++cnt < ngroups && gid == groups[cnt]);
243 	}
244 	(void)printf("\n");
245 }
246 
247 void
248 group(struct passwd *pw, int nflag)
249 {
250 	int cnt, ngroups;
251 	gid_t gid, groups[NGROUPS + 1];
252 	struct group *gr;
253 	char *fmt;
254 
255 	if (pw) {
256 		ngroups = NGROUPS + 1;
257 		(void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
258 	} else {
259 		groups[0] = getgid();
260 		ngroups = getgroups(NGROUPS, groups + 1) + 1;
261 	}
262 	fmt = nflag ? "%s" : "%u";
263 	for (cnt = 0; cnt < ngroups;) {
264 		gid = groups[cnt];
265 		if (nflag) {
266 			if ((gr = getgrgid(gid)))
267 				(void)printf(fmt, gr->gr_name);
268 			else
269 				(void)printf(*fmt == ' ' ? " %u" : "%u",
270 				    gid);
271 			fmt = " %s";
272 		} else {
273 			(void)printf(fmt, gid);
274 			fmt = " %u";
275 		}
276 		/* Skip same gid entries. */
277 		while (++cnt < ngroups && gid == groups[cnt]);
278 	}
279 	(void)printf("\n");
280 }
281 
282 struct passwd *
283 who(char *u)
284 {
285 	struct passwd *pw;
286 	uid_t uid;
287 	const char *errstr;
288 
289 	/*
290 	 * Translate user argument into a pw pointer.  First, try to
291 	 * get it as specified.  If that fails, try it as a number.
292 	 */
293 	if ((pw = getpwnam(u)))
294 		return(pw);
295 	uid = strtonum(u, 0, UID_MAX, &errstr);
296 	if (!errstr && (pw = getpwuid(uid)))
297 		return(pw);
298 	errx(1, "%s: No such user", u);
299 	/* NOTREACHED */
300 }
301 
302 void
303 usage(void)
304 {
305 	(void)fprintf(stderr, "usage: id [user]\n"
306 			      "       id -G [-n] [user]\n"
307 			      "       id -g [-nr] [user]\n"
308 			      "       id -p [user]\n"
309 			      "       id -u [-nr] [user]\n");
310 	exit(1);
311 }
312