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
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static const char copyright[] =
36 "@(#) Copyright (c) 1991, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)id.c 8.2 (Berkeley) 2/16/94";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
61 void pline(struct passwd *);
62 void pretty(struct passwd *);
63 void group(struct passwd *, int);
66 void user(struct passwd *);
70 int isgroups, iswhoami;
73 main(int argc, char *argv[])
77 int Gflag, Mflag, Pflag, ch, gflag, id, nflag, pflag, rflag, uflag;
80 Gflag = Mflag = Pflag = gflag = nflag = pflag = rflag = uflag = 0;
82 myname = strrchr(argv[0], '/');
83 myname = (myname != NULL) ? myname + 1 : argv[0];
84 if (strcmp(myname, "groups") == 0) {
88 else if (strcmp(myname, "whoami") == 0) {
93 while ((ch = getopt(argc, argv,
94 (isgroups || iswhoami) ? "" : "PGMgnpru")) != -1)
127 if (iswhoami && argc > 0)
130 switch(Gflag + Pflag + gflag + pflag + uflag) {
134 if (!nflag && !rflag)
141 pw = *argv ? who(*argv) : NULL;
143 if (Mflag && pw != NULL)
147 id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
148 if (nflag && (gr = getgrgid(id)))
149 (void)printf("%s\n", gr->gr_name);
151 (void)printf("%u\n", id);
156 id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
157 if (nflag && (pw = getpwuid(id)))
158 (void)printf("%s\n", pw->pw_name);
160 (void)printf("%u\n", id);
192 pretty(struct passwd *pw)
199 (void)printf("uid\t%s\n", pw->pw_name);
200 (void)printf("groups\t");
203 if ((login = getlogin()) == NULL)
206 pw = getpwuid(rid = getuid());
207 if (pw == NULL || strcmp(login, pw->pw_name))
208 (void)printf("login\t%s\n", login);
210 (void)printf("uid\t%s\n", pw->pw_name);
212 (void)printf("uid\t%u\n", rid);
214 if ((eid = geteuid()) != rid) {
215 if ((pw = getpwuid(eid)))
216 (void)printf("euid\t%s\n", pw->pw_name);
218 (void)printf("euid\t%u\n", eid);
220 if ((rid = getgid()) != (eid = getegid())) {
221 if ((gr = getgrgid(rid)))
222 (void)printf("rgid\t%s\n", gr->gr_name);
224 (void)printf("rgid\t%u\n", rid);
226 (void)printf("groups\t");
236 int cnt, id, eid, lastid, ngroups;
237 gid_t groups[NGROUPS];
241 (void)printf("uid=%u", id);
242 if ((pw = getpwuid(id)))
243 (void)printf("(%s)", pw->pw_name);
244 if ((eid = geteuid()) != id) {
245 (void)printf(" euid=%u", eid);
246 if ((pw = getpwuid(eid)))
247 (void)printf("(%s)", pw->pw_name);
250 (void)printf(" gid=%u", id);
251 if ((gr = getgrgid(id)))
252 (void)printf("(%s)", gr->gr_name);
253 if ((eid = getegid()) != id) {
254 (void)printf(" egid=%u", eid);
255 if ((gr = getgrgid(eid)))
256 (void)printf("(%s)", gr->gr_name);
258 if ((ngroups = getgroups(NGROUPS, groups))) {
259 for (fmt = " groups=%u", lastid = -1, cnt = 0; cnt < ngroups;
260 fmt = ", %u", lastid = id) {
264 (void)printf(fmt, id);
265 if ((gr = getgrgid(id)))
266 (void)printf("(%s)", gr->gr_name);
273 user(struct passwd *pw)
277 int cnt, gid, lastgid, ngroups, groups[NGROUPS + 1];
279 (void)printf("uid=%u(%s)", pw->pw_uid, pw->pw_name);
281 (void)printf(" gid=%u", gid);
282 if ((gr = getgrgid(gid)))
283 (void)printf("(%s)", gr->gr_name);
284 ngroups = NGROUPS + 1;
285 (void) getgrouplist(pw->pw_name, gid, groups, &ngroups);
287 for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) {
288 if (lastgid == (gid = groups[cnt]))
290 (void)printf(fmt, gid);
292 if ((gr = getgrgid(gid)))
293 (void)printf("(%s)", gr->gr_name);
300 group(struct passwd *pw, int nflag)
303 int cnt, id, lastid, ngroups;
304 gid_t groups[NGROUPS + 1];
308 ngroups = NGROUPS + 1;
309 (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
311 groups[0] = getgid();
312 ngroups = getgroups(NGROUPS, groups + 1) + 1;
314 fmt = nflag ? "%s" : "%u";
315 for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
316 if (lastid == (id = groups[cnt]))
319 if ((gr = getgrgid(id)))
320 (void)printf(fmt, gr->gr_name);
322 (void)printf(*fmt == ' ' ? " %u" : "%u",
326 (void)printf(fmt, id);
341 error = mac_prepare_process_label(&label);
343 errx(1, "mac_prepare_type: %s", strerror(errno));
345 error = mac_get_proc(label);
347 errx(1, "mac_get_proc: %s", strerror(errno));
349 error = mac_to_text(label, &string);
351 errx(1, "mac_to_text: %s", strerror(errno));
353 (void)printf("%s\n", string);
366 * Translate user argument into a pw pointer. First, try to
367 * get it as specified. If that fails, try it as a number.
369 if ((pw = getpwnam(u)))
371 id = strtol(u, &ep, 10);
372 if (*u && !*ep && (pw = getpwuid(id)))
374 errx(1, "%s: no such user", u);
379 pline(struct passwd *pw)
384 if ((pw = getpwuid(rid = getuid())) == NULL)
388 (void)printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw->pw_name,
389 pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
390 (long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos,
391 pw->pw_dir, pw->pw_shell);
400 (void)fprintf(stderr, "usage: groups [user]\n");
402 (void)fprintf(stderr, "usage: whoami\n");
404 (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
406 " id -G [-n] [user]",
409 " id -g [-nr] [user]",
411 " id -u [-nr] [user]");