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