id.c revision 1.27 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.27 liamjfoy __RCSID("$NetBSD: id.c,v 1.27 2006/06/07 13:09:56 liamjfoy 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.27 liamjfoy gid_t gid, egid, lastid;
211 1.27 liamjfoy uid_t uid, euid;
212 1.27 liamjfoy int cnt, ngroups;
213 1.24 peter const char *fmt;
214 1.1 cgd
215 1.27 liamjfoy uid = getuid();
216 1.27 liamjfoy (void)printf("uid=%ju", (uintmax_t)uid);
217 1.27 liamjfoy if ((pw = getpwuid(uid)) != NULL)
218 1.1 cgd (void)printf("(%s)", pw->pw_name);
219 1.27 liamjfoy gid = getgid();
220 1.27 liamjfoy (void)printf(" gid=%ju", (uintmax_t)gid);
221 1.27 liamjfoy if ((gr = getgrgid(gid)) != NULL)
222 1.27 liamjfoy (void)printf("(%s)", gr->gr_name);
223 1.27 liamjfoy if ((euid = geteuid()) != uid) {
224 1.27 liamjfoy (void)printf(" euid=%ju", (uintmax_t)euid);
225 1.27 liamjfoy if ((pw = getpwuid(euid)) != NULL)
226 1.1 cgd (void)printf("(%s)", pw->pw_name);
227 1.1 cgd }
228 1.27 liamjfoy if ((egid = getegid()) != gid) {
229 1.27 liamjfoy (void)printf(" egid=%ju", (uintmax_t)egid);
230 1.27 liamjfoy if ((gr = getgrgid(egid)) != NULL)
231 1.1 cgd (void)printf("(%s)", gr->gr_name);
232 1.1 cgd }
233 1.20 fvdl if ((ngroups = getgroups(maxgroups, groups)) != 0) {
234 1.27 liamjfoy for (fmt = " groups=%ju", lastid = -1, cnt = 0; cnt < ngroups;
235 1.27 liamjfoy fmt = ",%ju", lastid = gid, cnt++) {
236 1.27 liamjfoy gid = groups[cnt];
237 1.27 liamjfoy if (lastid == gid)
238 1.1 cgd continue;
239 1.27 liamjfoy (void)printf(fmt, (uintmax_t)gid);
240 1.27 liamjfoy if ((gr = getgrgid(gid)) != NULL)
241 1.1 cgd (void)printf("(%s)", gr->gr_name);
242 1.1 cgd }
243 1.1 cgd }
244 1.1 cgd (void)printf("\n");
245 1.1 cgd }
246 1.1 cgd
247 1.14 christos static void
248 1.23 peter user(struct passwd *pw)
249 1.1 cgd {
250 1.8 lukem struct group *gr;
251 1.24 peter const char *fmt;
252 1.14 christos int cnt, id, lastid, ngroups;
253 1.25 drochner gid_t *glist = groups;
254 1.1 cgd
255 1.1 cgd id = pw->pw_uid;
256 1.1 cgd (void)printf("uid=%u(%s)", id, pw->pw_name);
257 1.14 christos (void)printf(" gid=%lu", (u_long)pw->pw_gid);
258 1.8 lukem if ((gr = getgrgid(pw->pw_gid)) != NULL)
259 1.1 cgd (void)printf("(%s)", gr->gr_name);
260 1.14 christos ngroups = maxgroups + 1;
261 1.25 drochner if (getgrouplist(pw->pw_name, pw->pw_gid, glist, &ngroups) == -1) {
262 1.25 drochner glist = malloc(ngroups * sizeof(gid_t));
263 1.25 drochner (void) getgrouplist(pw->pw_name, pw->pw_gid, glist, &ngroups);
264 1.25 drochner }
265 1.15 mycroft for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
266 1.15 mycroft fmt=",%u", lastid = id, cnt++) {
267 1.25 drochner id = glist[cnt];
268 1.15 mycroft if (lastid == id)
269 1.5 cgd continue;
270 1.5 cgd (void)printf(fmt, id);
271 1.8 lukem if ((gr = getgrgid(id)) != NULL)
272 1.5 cgd (void)printf("(%s)", gr->gr_name);
273 1.5 cgd }
274 1.5 cgd (void)printf("\n");
275 1.25 drochner if (glist != groups)
276 1.25 drochner free(glist);
277 1.5 cgd }
278 1.5 cgd
279 1.14 christos static void
280 1.23 peter group(struct passwd *pw, int nflag)
281 1.5 cgd {
282 1.5 cgd struct group *gr;
283 1.5 cgd int cnt, id, lastid, ngroups;
284 1.24 peter const char *fmt;
285 1.25 drochner gid_t *glist = groups;
286 1.5 cgd
287 1.5 cgd if (pw) {
288 1.14 christos ngroups = maxgroups;
289 1.25 drochner if (getgrouplist(pw->pw_name, pw->pw_gid, glist, &ngroups)
290 1.25 drochner == -1) {
291 1.25 drochner glist = malloc(ngroups * sizeof(gid_t));
292 1.25 drochner (void) getgrouplist(pw->pw_name, pw->pw_gid, glist,
293 1.25 drochner &ngroups);
294 1.25 drochner }
295 1.5 cgd } else {
296 1.25 drochner glist[0] = getgid();
297 1.25 drochner ngroups = getgroups(maxgroups, glist + 1) + 1;
298 1.5 cgd }
299 1.5 cgd fmt = nflag ? "%s" : "%u";
300 1.5 cgd for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
301 1.26 christos if (lastid == (id = glist[cnt]) || (cnt && id == glist[0]))
302 1.1 cgd continue;
303 1.5 cgd if (nflag) {
304 1.8 lukem if ((gr = getgrgid(id)) != NULL)
305 1.5 cgd (void)printf(fmt, gr->gr_name);
306 1.5 cgd else
307 1.5 cgd (void)printf(*fmt == ' ' ? " %u" : "%u",
308 1.5 cgd id);
309 1.5 cgd fmt = " %s";
310 1.5 cgd } else {
311 1.5 cgd (void)printf(fmt, id);
312 1.5 cgd fmt = " %u";
313 1.5 cgd }
314 1.5 cgd lastid = id;
315 1.1 cgd }
316 1.1 cgd (void)printf("\n");
317 1.25 drochner if (glist != groups)
318 1.25 drochner free(glist);
319 1.1 cgd }
320 1.1 cgd
321 1.24 peter static struct passwd *
322 1.23 peter who(char *u)
323 1.1 cgd {
324 1.5 cgd struct passwd *pw;
325 1.1 cgd long id;
326 1.1 cgd char *ep;
327 1.1 cgd
328 1.1 cgd /*
329 1.1 cgd * Translate user argument into a pw pointer. First, try to
330 1.1 cgd * get it as specified. If that fails, try it as a number.
331 1.1 cgd */
332 1.8 lukem if ((pw = getpwnam(u)) != NULL)
333 1.23 peter return pw;
334 1.1 cgd id = strtol(u, &ep, 10);
335 1.1 cgd if (*u && !*ep && (pw = getpwuid(id)))
336 1.23 peter return pw;
337 1.8 lukem errx(1, "%s: No such user", u);
338 1.1 cgd /* NOTREACHED */
339 1.23 peter return NULL;
340 1.1 cgd }
341 1.1 cgd
342 1.24 peter static void
343 1.23 peter usage(void)
344 1.1 cgd {
345 1.22 peter
346 1.22 peter if (strcmp(getprogname(), "groups") == 0) {
347 1.22 peter (void)fprintf(stderr, "usage: groups [user]\n");
348 1.22 peter } else if (strcmp(getprogname(), "whoami") == 0) {
349 1.22 peter (void)fprintf(stderr, "usage: whoami\n");
350 1.22 peter } else {
351 1.22 peter (void)fprintf(stderr, "usage: id [user]\n");
352 1.22 peter (void)fprintf(stderr, " id -G [-n] [user]\n");
353 1.22 peter (void)fprintf(stderr, " id -g [-nr] [user]\n");
354 1.22 peter (void)fprintf(stderr, " id -p [user]\n");
355 1.22 peter (void)fprintf(stderr, " id -u [-nr] [user]\n");
356 1.22 peter }
357 1.1 cgd exit(1);
358 1.1 cgd }
359