id.c revision 1.11 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 #include <sys/cdefs.h>
35 #ifndef lint
36 __COPYRIGHT("@(#) 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.3 (Berkeley) 4/28/95";
43 #else
44 __RCSID("$NetBSD: id.c,v 1.11 1998/10/12 20:33:52 erh Exp $");
45 #endif
46 #endif /* not lint */
47
48 #include <sys/param.h>
49
50 #include <err.h>
51 #include <errno.h>
52 #include <grp.h>
53 #include <pwd.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58
59 void current __P((void));
60 void pretty __P((struct passwd *));
61 int main __P((int, char **));
62 void group __P((struct passwd *, int));
63 void usage __P((void));
64 void user __P((struct passwd *));
65 struct passwd *
66 who __P((char *));
67
68 int
69 main(argc, argv)
70 int argc;
71 char *argv[];
72 {
73 struct group *gr;
74 struct passwd *pw;
75 int Gflag, ch, gflag, id, nflag, pflag, rflag, uflag;
76
77 Gflag = gflag = nflag = pflag = rflag = uflag = 0;
78 while ((ch = getopt(argc, argv, "Ggnpru")) != -1)
79 switch(ch) {
80 case 'G':
81 Gflag = 1;
82 break;
83 case 'g':
84 gflag = 1;
85 break;
86 case 'n':
87 nflag = 1;
88 break;
89 case 'p':
90 pflag = 1;
91 break;
92 case 'r':
93 rflag = 1;
94 break;
95 case 'u':
96 uflag = 1;
97 break;
98 case '?':
99 default:
100 usage();
101 }
102 argc -= optind;
103 argv += optind;
104
105 switch(Gflag + gflag + pflag + uflag) {
106 case 1:
107 break;
108 case 0:
109 if (!nflag && !rflag)
110 break;
111 /* FALLTHROUGH */
112 default:
113 usage();
114 }
115
116 pw = *argv ? who(*argv) : NULL;
117
118 if (gflag) {
119 id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
120 if (nflag && (gr = getgrgid(id)))
121 (void)printf("%s\n", gr->gr_name);
122 else
123 (void)printf("%u\n", id);
124 exit(0);
125 }
126
127 if (uflag) {
128 id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
129 if (nflag && (pw = getpwuid(id)))
130 (void)printf("%s\n", pw->pw_name);
131 else
132 (void)printf("%u\n", id);
133 exit(0);
134 }
135
136 if (Gflag) {
137 group(pw, nflag);
138 exit(0);
139 }
140
141 if (pflag) {
142 pretty(pw);
143 exit(0);
144 }
145
146 if (pw)
147 user(pw);
148 else
149 current();
150 exit(0);
151 }
152
153 void
154 pretty(pw)
155 struct passwd *pw;
156 {
157 struct group *gr;
158 u_int eid, rid;
159 char *login;
160
161 if (pw) {
162 (void)printf("uid\t%s\n", pw->pw_name);
163 (void)printf("groups\t");
164 group(pw, 1);
165 } else {
166 if ((login = getlogin()) == NULL)
167 err(1, "getlogin");
168
169 pw = getpwuid(rid = getuid());
170 if (pw == NULL || strcmp(login, pw->pw_name))
171 (void)printf("login\t%s\n", login);
172 if (pw)
173 (void)printf("uid\t%s\n", pw->pw_name);
174 else
175 (void)printf("uid\t%u\n", rid);
176
177 if ((eid = geteuid()) != rid) {
178 if ((pw = getpwuid(eid)) != NULL)
179 (void)printf("euid\t%s", pw->pw_name);
180 else
181 (void)printf("euid\t%u", eid);
182 }
183 if ((rid = getgid()) != (eid = getegid())) {
184 if ((gr = getgrgid(rid)) != NULL)
185 (void)printf("rgid\t%s\n", gr->gr_name);
186 else
187 (void)printf("rgid\t%u\n", rid);
188 }
189 (void)printf("groups\t");
190 group(NULL, 1);
191 }
192 }
193
194 void
195 current()
196 {
197 struct group *gr;
198 struct passwd *pw;
199 int cnt, id, eid, lastid, ngroups;
200 gid_t groups[NGROUPS];
201 char *fmt;
202
203 id = getuid();
204 (void)printf("uid=%u", id);
205 if ((pw = getpwuid(id)) != NULL)
206 (void)printf("(%s)", pw->pw_name);
207 if ((eid = geteuid()) != id) {
208 (void)printf(" euid=%u", eid);
209 if ((pw = getpwuid(eid)) != NULL)
210 (void)printf("(%s)", pw->pw_name);
211 }
212 id = getgid();
213 (void)printf(" gid=%u", id);
214 if ((gr = getgrgid(id)) != NULL)
215 (void)printf("(%s)", gr->gr_name);
216 if ((eid = getegid()) != id) {
217 (void)printf(" egid=%u", eid);
218 if ((gr = getgrgid(eid)) != NULL)
219 (void)printf("(%s)", gr->gr_name);
220 }
221 if ((ngroups = getgroups(NGROUPS, groups)) != NULL) {
222 for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
223 fmt = ", %u", lastid = id) {
224 id = groups[cnt++];
225 if (lastid == id)
226 continue;
227 (void)printf(fmt, id);
228 if ((gr = getgrgid(id)) != NULL)
229 (void)printf("(%s)", gr->gr_name);
230 }
231 }
232 (void)printf("\n");
233 }
234
235 void
236 user(pw)
237 struct passwd *pw;
238 {
239 struct group *gr;
240 char *fmt;
241 int cnt, id, lastid, ngroups, groups[NGROUPS + 1];
242
243 id = pw->pw_uid;
244 (void)printf("uid=%u(%s)", id, pw->pw_name);
245 (void)printf(" gid=%u", pw->pw_gid);
246 if ((gr = getgrgid(pw->pw_gid)) != NULL)
247 (void)printf("(%s)", gr->gr_name);
248 ngroups = NGROUPS + 1;
249 (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
250 fmt = " groups=%u";
251 for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
252 if (lastid == (id = groups[cnt]))
253 continue;
254 (void)printf(fmt, id);
255 fmt = " %u";
256 if ((gr = getgrgid(id)) != NULL)
257 (void)printf("(%s)", gr->gr_name);
258 lastid = id;
259 }
260 (void)printf("\n");
261 }
262
263 void
264 group(pw, nflag)
265 struct passwd *pw;
266 int nflag;
267 {
268 struct group *gr;
269 int cnt, id, lastid, ngroups;
270 gid_t groups[NGROUPS + 1];
271 char *fmt;
272
273 if (pw) {
274 ngroups = NGROUPS + 1;
275 (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
276 } else {
277 groups[0] = getgid();
278 ngroups = getgroups(NGROUPS, groups + 1) + 1;
279 }
280 fmt = nflag ? "%s" : "%u";
281 for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
282 if (lastid == (id = groups[cnt]))
283 continue;
284 if (nflag) {
285 if ((gr = getgrgid(id)) != NULL)
286 (void)printf(fmt, gr->gr_name);
287 else
288 (void)printf(*fmt == ' ' ? " %u" : "%u",
289 id);
290 fmt = " %s";
291 } else {
292 (void)printf(fmt, id);
293 fmt = " %u";
294 }
295 lastid = id;
296 }
297 (void)printf("\n");
298 }
299
300 struct passwd *
301 who(u)
302 char *u;
303 {
304 struct passwd *pw;
305 long id;
306 char *ep;
307
308 /*
309 * Translate user argument into a pw pointer. First, try to
310 * get it as specified. If that fails, try it as a number.
311 */
312 if ((pw = getpwnam(u)) != NULL)
313 return(pw);
314 id = strtol(u, &ep, 10);
315 if (*u && !*ep && (pw = getpwuid(id)))
316 return(pw);
317 errx(1, "%s: No such user", u);
318 /* NOTREACHED */
319 return (NULL);
320 }
321
322 void
323 usage()
324 {
325 (void)fprintf(stderr, "usage: id [user]\n");
326 (void)fprintf(stderr, " id -G [-n] [user]\n");
327 (void)fprintf(stderr, " id -g [-nr] [user]\n");
328 (void)fprintf(stderr, " id -p\n");
329 (void)fprintf(stderr, " id -u [-nr] [user]\n");
330 exit(1);
331 }
332