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 * 4. 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
31 static const char copyright[] =
32 "@(#) Copyright (c) 1991, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)id.c 8.2 (Berkeley) 2/16/94";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
48 #include <bsm/audit.h>
60 static void id_print(struct passwd *, int, int, int);
61 static void pline(struct passwd *);
62 static void pretty(struct passwd *);
64 static void auditid(void);
66 static void group(struct passwd *, int);
67 static void maclabel(void);
68 static void usage(void);
69 static struct passwd *who(char *);
71 static int isgroups, iswhoami;
74 main(int argc, char *argv[])
78 int Gflag, Mflag, Pflag, ch, gflag, id, nflag, pflag, rflag, uflag;
82 char loginclass[MAXLOGNAME];
84 Gflag = Mflag = Pflag = gflag = nflag = pflag = rflag = uflag = 0;
87 myname = strrchr(argv[0], '/');
88 myname = (myname != NULL) ? myname + 1 : argv[0];
89 if (strcmp(myname, "groups") == 0) {
93 else if (strcmp(myname, "whoami") == 0) {
98 while ((ch = getopt(argc, argv,
99 (isgroups || iswhoami) ? "" : "APGMacgnpru")) != -1)
142 if (iswhoami && argc > 0)
145 switch(Aflag + Gflag + Mflag + Pflag + gflag + pflag + uflag) {
149 if (!nflag && !rflag)
156 pw = *argv ? who(*argv) : NULL;
158 if (Mflag && pw != NULL)
169 error = getloginclass(loginclass, sizeof(loginclass));
171 err(1, "loginclass");
172 (void)printf("%s\n", loginclass);
177 id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
178 if (nflag && (gr = getgrgid(id)))
179 (void)printf("%s\n", gr->gr_name);
181 (void)printf("%u\n", id);
186 id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
187 if (nflag && (pw = getpwuid(id)))
188 (void)printf("%s\n", pw->pw_name);
190 (void)printf("%u\n", id);
215 id_print(pw, 1, 0, 0);
220 id_print(pw, 0, 1, 1);
226 pretty(struct passwd *pw)
233 (void)printf("uid\t%s\n", pw->pw_name);
234 (void)printf("groups\t");
237 if ((login = getlogin()) == NULL)
240 pw = getpwuid(rid = getuid());
241 if (pw == NULL || strcmp(login, pw->pw_name))
242 (void)printf("login\t%s\n", login);
244 (void)printf("uid\t%s\n", pw->pw_name);
246 (void)printf("uid\t%u\n", rid);
248 if ((eid = geteuid()) != rid) {
249 if ((pw = getpwuid(eid)))
250 (void)printf("euid\t%s\n", pw->pw_name);
252 (void)printf("euid\t%u\n", eid);
254 if ((rid = getgid()) != (eid = getegid())) {
255 if ((gr = getgrgid(rid)))
256 (void)printf("rgid\t%s\n", gr->gr_name);
258 (void)printf("rgid\t%u\n", rid);
260 (void)printf("groups\t");
266 id_print(struct passwd *pw, int use_ggl, int p_euid, int p_egid)
269 gid_t gid, egid, lastgid;
285 ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
286 if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL)
289 if (use_ggl && pw != NULL) {
290 ngroups = ngroups_max;
291 getgrouplist(pw->pw_name, gid, groups, &ngroups);
294 ngroups = getgroups(ngroups_max, groups);
298 printf("uid=%u(%s)", uid, pw->pw_name);
300 printf("uid=%u", getuid());
301 printf(" gid=%u", gid);
302 if ((gr = getgrgid(gid)))
303 (void)printf("(%s)", gr->gr_name);
304 if (p_euid && (euid = geteuid()) != uid) {
305 (void)printf(" euid=%u", euid);
306 if ((pw = getpwuid(euid)))
307 (void)printf("(%s)", pw->pw_name);
309 if (p_egid && (egid = getegid()) != gid) {
310 (void)printf(" egid=%u", egid);
311 if ((gr = getgrgid(egid)))
312 (void)printf("(%s)", gr->gr_name);
315 for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) {
316 if (lastgid == (gid = groups[cnt]))
320 if ((gr = getgrgid(gid)))
321 printf("(%s)", gr->gr_name);
332 auditinfo_t auditinfo;
333 auditinfo_addr_t ainfo_addr;
337 ret = getaudit(&auditinfo);
338 if (ret < 0 && errno == E2BIG) {
339 if (getaudit_addr(&ainfo_addr, sizeof(ainfo_addr)) < 0)
340 err(1, "getaudit_addr");
345 (void) printf("auid=%d\n"
346 "mask.success=0x%08x\n"
347 "mask.failure=0x%08x\n"
349 "termid_addr.port=0x%08x\n"
350 "termid_addr.addr[0]=0x%08x\n"
351 "termid_addr.addr[1]=0x%08x\n"
352 "termid_addr.addr[2]=0x%08x\n"
353 "termid_addr.addr[3]=0x%08x\n",
354 ainfo_addr.ai_auid, ainfo_addr.ai_mask.am_success,
355 ainfo_addr.ai_mask.am_failure, ainfo_addr.ai_asid,
356 ainfo_addr.ai_termid.at_port,
357 ainfo_addr.ai_termid.at_addr[0],
358 ainfo_addr.ai_termid.at_addr[1],
359 ainfo_addr.ai_termid.at_addr[2],
360 ainfo_addr.ai_termid.at_addr[3]);
362 (void) printf("auid=%d\n"
363 "mask.success=0x%08x\n"
364 "mask.failure=0x%08x\n"
366 "termid.port=0x%08x\n"
367 "termid.machine=0x%08x\n",
368 auditinfo.ai_auid, auditinfo.ai_mask.am_success,
369 auditinfo.ai_mask.am_failure,
370 auditinfo.ai_asid, auditinfo.ai_termid.port,
371 auditinfo.ai_termid.machine);
377 group(struct passwd *pw, int nflag)
380 int cnt, id, lastid, ngroups;
385 ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
386 if ((groups = malloc(sizeof(gid_t) * (ngroups_max))) == NULL)
390 ngroups = ngroups_max;
391 (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
393 ngroups = getgroups(ngroups_max, groups);
395 fmt = nflag ? "%s" : "%u";
396 for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
397 if (lastid == (id = groups[cnt]))
400 if ((gr = getgrgid(id)))
401 (void)printf(fmt, gr->gr_name);
403 (void)printf(*fmt == ' ' ? " %u" : "%u",
407 (void)printf(fmt, id);
423 error = mac_prepare_process_label(&label);
425 errx(1, "mac_prepare_type: %s", strerror(errno));
427 error = mac_get_proc(label);
429 errx(1, "mac_get_proc: %s", strerror(errno));
431 error = mac_to_text(label, &string);
433 errx(1, "mac_to_text: %s", strerror(errno));
435 (void)printf("%s\n", string);
440 static struct passwd *
448 * Translate user argument into a pw pointer. First, try to
449 * get it as specified. If that fails, try it as a number.
451 if ((pw = getpwnam(u)))
453 id = strtol(u, &ep, 10);
454 if (*u && !*ep && (pw = getpwuid(id)))
456 errx(1, "%s: no such user", u);
461 pline(struct passwd *pw)
465 if ((pw = getpwuid(getuid())) == NULL)
469 (void)printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw->pw_name,
470 pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
471 (long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos,
472 pw->pw_dir, pw->pw_shell);
481 (void)fprintf(stderr, "usage: groups [user]\n");
483 (void)fprintf(stderr, "usage: whoami\n");
485 (void)fprintf(stderr, "%s\n%s%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
492 " id -G [-n] [user]",
496 " id -g [-nr] [user]",
498 " id -u [-nr] [user]");