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