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