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