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