19b50d902SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
49b50d902SRodney W. Grimes * Copyright (c) 1991, 1993
59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
69b50d902SRodney W. Grimes *
79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes * are met:
109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes * without specific prior written permission.
189b50d902SRodney W. Grimes *
199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes * SUCH DAMAGE.
309b50d902SRodney W. Grimes */
319b50d902SRodney W. Grimes
329b50d902SRodney W. Grimes #include <sys/param.h>
33300b40afSRobert Watson #include <sys/mac.h>
349b50d902SRodney W. Grimes
355bae3124SRobert Watson #ifdef USE_BSM_AUDIT
365bae3124SRobert Watson #include <bsm/audit.h>
375bae3124SRobert Watson #endif
385bae3124SRobert Watson
39d48dbc78SPhilippe Charnier #include <err.h>
40300b40afSRobert Watson #include <errno.h>
419b50d902SRodney W. Grimes #include <grp.h>
429b50d902SRodney W. Grimes #include <pwd.h>
439b337054SGleb Kurtsou #include <stdint.h>
449b50d902SRodney W. Grimes #include <stdio.h>
459b50d902SRodney W. Grimes #include <stdlib.h>
469b50d902SRodney W. Grimes #include <string.h>
479b50d902SRodney W. Grimes #include <unistd.h>
489b50d902SRodney W. Grimes
49f82c82dfSEd Schouten static void id_print(struct passwd *, int, int, int);
50f82c82dfSEd Schouten static void pline(struct passwd *);
51f82c82dfSEd Schouten static void pretty(struct passwd *);
526291cd02SEd Schouten #ifdef USE_BSM_AUDIT
53f82c82dfSEd Schouten static void auditid(void);
546291cd02SEd Schouten #endif
55f82c82dfSEd Schouten static void group(struct passwd *, int);
56f82c82dfSEd Schouten static void maclabel(void);
57f82c82dfSEd Schouten static void usage(void);
58f82c82dfSEd Schouten static struct passwd *who(char *);
599b50d902SRodney W. Grimes
60f82c82dfSEd Schouten static int isgroups, iswhoami;
614bf12cb9SRuslan Ermilov
629b50d902SRodney W. Grimes int
main(int argc,char * argv[])63f4ac32deSDavid Malone main(int argc, char *argv[])
649b50d902SRodney W. Grimes {
659b50d902SRodney W. Grimes struct group *gr;
669b50d902SRodney W. Grimes struct passwd *pw;
67300b40afSRobert Watson int Gflag, Mflag, Pflag, ch, gflag, id, nflag, pflag, rflag, uflag;
682bfc50bcSEdward Tomasz Napierala int Aflag, cflag;
692bfc50bcSEdward Tomasz Napierala int error;
704bf12cb9SRuslan Ermilov const char *myname;
712bfc50bcSEdward Tomasz Napierala char loginclass[MAXLOGNAME];
729b50d902SRodney W. Grimes
73300b40afSRobert Watson Gflag = Mflag = Pflag = gflag = nflag = pflag = rflag = uflag = 0;
742bfc50bcSEdward Tomasz Napierala Aflag = cflag = 0;
754bf12cb9SRuslan Ermilov
764bf12cb9SRuslan Ermilov myname = strrchr(argv[0], '/');
774bf12cb9SRuslan Ermilov myname = (myname != NULL) ? myname + 1 : argv[0];
784bf12cb9SRuslan Ermilov if (strcmp(myname, "groups") == 0) {
794bf12cb9SRuslan Ermilov isgroups = 1;
804bf12cb9SRuslan Ermilov Gflag = nflag = 1;
814bf12cb9SRuslan Ermilov }
824bf12cb9SRuslan Ermilov else if (strcmp(myname, "whoami") == 0) {
834bf12cb9SRuslan Ermilov iswhoami = 1;
844bf12cb9SRuslan Ermilov uflag = nflag = 1;
854bf12cb9SRuslan Ermilov }
864bf12cb9SRuslan Ermilov
874bf12cb9SRuslan Ermilov while ((ch = getopt(argc, argv,
882bfc50bcSEdward Tomasz Napierala (isgroups || iswhoami) ? "" : "APGMacgnpru")) != -1)
899b50d902SRodney W. Grimes switch(ch) {
90f4882b24SRobert Watson #ifdef USE_BSM_AUDIT
91f4882b24SRobert Watson case 'A':
92f4882b24SRobert Watson Aflag = 1;
93f4882b24SRobert Watson break;
94f4882b24SRobert Watson #endif
959b50d902SRodney W. Grimes case 'G':
969b50d902SRodney W. Grimes Gflag = 1;
979b50d902SRodney W. Grimes break;
98300b40afSRobert Watson case 'M':
99300b40afSRobert Watson Mflag = 1;
100300b40afSRobert Watson break;
101bd706105SDavid E. O'Brien case 'P':
102bd706105SDavid E. O'Brien Pflag = 1;
103bd706105SDavid E. O'Brien break;
104de5e5e6eSCeri Davies case 'a':
105de5e5e6eSCeri Davies break;
1062bfc50bcSEdward Tomasz Napierala case 'c':
1072bfc50bcSEdward Tomasz Napierala cflag = 1;
1082bfc50bcSEdward Tomasz Napierala break;
1099b50d902SRodney W. Grimes case 'g':
1109b50d902SRodney W. Grimes gflag = 1;
1119b50d902SRodney W. Grimes break;
1129b50d902SRodney W. Grimes case 'n':
1139b50d902SRodney W. Grimes nflag = 1;
1149b50d902SRodney W. Grimes break;
1159b50d902SRodney W. Grimes case 'p':
1169b50d902SRodney W. Grimes pflag = 1;
1179b50d902SRodney W. Grimes break;
1189b50d902SRodney W. Grimes case 'r':
1199b50d902SRodney W. Grimes rflag = 1;
1209b50d902SRodney W. Grimes break;
1219b50d902SRodney W. Grimes case 'u':
1229b50d902SRodney W. Grimes uflag = 1;
1239b50d902SRodney W. Grimes break;
1249b50d902SRodney W. Grimes case '?':
1259b50d902SRodney W. Grimes default:
1269b50d902SRodney W. Grimes usage();
1279b50d902SRodney W. Grimes }
1289b50d902SRodney W. Grimes argc -= optind;
1299b50d902SRodney W. Grimes argv += optind;
1309b50d902SRodney W. Grimes
1314bf12cb9SRuslan Ermilov if (iswhoami && argc > 0)
1324bf12cb9SRuslan Ermilov usage();
133*4a06e937SAlan Somers if ((cflag || Aflag || Mflag) && argc > 0)
134*4a06e937SAlan Somers usage();
1354bf12cb9SRuslan Ermilov
136f4882b24SRobert Watson switch(Aflag + Gflag + Mflag + Pflag + gflag + pflag + uflag) {
1379b50d902SRodney W. Grimes case 1:
1389b50d902SRodney W. Grimes break;
1399b50d902SRodney W. Grimes case 0:
1409b50d902SRodney W. Grimes if (!nflag && !rflag)
1419b50d902SRodney W. Grimes break;
1429b50d902SRodney W. Grimes /* FALLTHROUGH */
1439b50d902SRodney W. Grimes default:
1449b50d902SRodney W. Grimes usage();
1459b50d902SRodney W. Grimes }
1469b50d902SRodney W. Grimes
1479b50d902SRodney W. Grimes pw = *argv ? who(*argv) : NULL;
1489b50d902SRodney W. Grimes
149300b40afSRobert Watson if (Mflag && pw != NULL)
150300b40afSRobert Watson usage();
151300b40afSRobert Watson
1525bae3124SRobert Watson #ifdef USE_BSM_AUDIT
153f4882b24SRobert Watson if (Aflag) {
1545bae3124SRobert Watson auditid();
1555bae3124SRobert Watson exit(0);
1565bae3124SRobert Watson }
1575bae3124SRobert Watson #endif
1585bae3124SRobert Watson
1592bfc50bcSEdward Tomasz Napierala if (cflag) {
1602bfc50bcSEdward Tomasz Napierala error = getloginclass(loginclass, sizeof(loginclass));
1612bfc50bcSEdward Tomasz Napierala if (error != 0)
1622bfc50bcSEdward Tomasz Napierala err(1, "loginclass");
1632bfc50bcSEdward Tomasz Napierala (void)printf("%s\n", loginclass);
1642bfc50bcSEdward Tomasz Napierala exit(0);
1652bfc50bcSEdward Tomasz Napierala }
1662bfc50bcSEdward Tomasz Napierala
1679b50d902SRodney W. Grimes if (gflag) {
1689b50d902SRodney W. Grimes id = pw ? pw->pw_gid : rflag ? getgid() : getegid();
1699b50d902SRodney W. Grimes if (nflag && (gr = getgrgid(id)))
1709b50d902SRodney W. Grimes (void)printf("%s\n", gr->gr_name);
1719b50d902SRodney W. Grimes else
1729b50d902SRodney W. Grimes (void)printf("%u\n", id);
1739b50d902SRodney W. Grimes exit(0);
1749b50d902SRodney W. Grimes }
1759b50d902SRodney W. Grimes
1769b50d902SRodney W. Grimes if (uflag) {
1779b50d902SRodney W. Grimes id = pw ? pw->pw_uid : rflag ? getuid() : geteuid();
1789b50d902SRodney W. Grimes if (nflag && (pw = getpwuid(id)))
1799b50d902SRodney W. Grimes (void)printf("%s\n", pw->pw_name);
1809b50d902SRodney W. Grimes else
1819b50d902SRodney W. Grimes (void)printf("%u\n", id);
1829b50d902SRodney W. Grimes exit(0);
1839b50d902SRodney W. Grimes }
1849b50d902SRodney W. Grimes
1859b50d902SRodney W. Grimes if (Gflag) {
1869b50d902SRodney W. Grimes group(pw, nflag);
1879b50d902SRodney W. Grimes exit(0);
1889b50d902SRodney W. Grimes }
1899b50d902SRodney W. Grimes
190300b40afSRobert Watson if (Mflag) {
191300b40afSRobert Watson maclabel();
192300b40afSRobert Watson exit(0);
193300b40afSRobert Watson }
194300b40afSRobert Watson
195bd706105SDavid E. O'Brien if (Pflag) {
196bd706105SDavid E. O'Brien pline(pw);
197bd706105SDavid E. O'Brien exit(0);
198bd706105SDavid E. O'Brien }
199bd706105SDavid E. O'Brien
2009b50d902SRodney W. Grimes if (pflag) {
2019b50d902SRodney W. Grimes pretty(pw);
2029b50d902SRodney W. Grimes exit(0);
2039b50d902SRodney W. Grimes }
2049b50d902SRodney W. Grimes
20568b9b81eSRobert Drehmel if (pw) {
2067f313541SRobert Drehmel id_print(pw, 1, 0, 0);
20768b9b81eSRobert Drehmel }
20868b9b81eSRobert Drehmel else {
20968b9b81eSRobert Drehmel id = getuid();
210f2588359SMike Pritchard pw = getpwuid(id);
2117f313541SRobert Drehmel id_print(pw, 0, 1, 1);
21268b9b81eSRobert Drehmel }
2139b50d902SRodney W. Grimes exit(0);
2149b50d902SRodney W. Grimes }
2159b50d902SRodney W. Grimes
216f82c82dfSEd Schouten static void
pretty(struct passwd * pw)217f4ac32deSDavid Malone pretty(struct passwd *pw)
2189b50d902SRodney W. Grimes {
2199b50d902SRodney W. Grimes struct group *gr;
2209b50d902SRodney W. Grimes u_int eid, rid;
2219b50d902SRodney W. Grimes char *login;
2229b50d902SRodney W. Grimes
2239b50d902SRodney W. Grimes if (pw) {
2249b50d902SRodney W. Grimes (void)printf("uid\t%s\n", pw->pw_name);
2259b50d902SRodney W. Grimes (void)printf("groups\t");
2269b50d902SRodney W. Grimes group(pw, 1);
2279b50d902SRodney W. Grimes } else {
2289b50d902SRodney W. Grimes if ((login = getlogin()) == NULL)
229d48dbc78SPhilippe Charnier err(1, "getlogin");
2309b50d902SRodney W. Grimes
2319b50d902SRodney W. Grimes pw = getpwuid(rid = getuid());
2329b50d902SRodney W. Grimes if (pw == NULL || strcmp(login, pw->pw_name))
2339b50d902SRodney W. Grimes (void)printf("login\t%s\n", login);
2349b50d902SRodney W. Grimes if (pw)
2359b50d902SRodney W. Grimes (void)printf("uid\t%s\n", pw->pw_name);
2369b50d902SRodney W. Grimes else
2379b50d902SRodney W. Grimes (void)printf("uid\t%u\n", rid);
2389b50d902SRodney W. Grimes
2399ef5c48bSBill Fumerola if ((eid = geteuid()) != rid) {
240d48dbc78SPhilippe Charnier if ((pw = getpwuid(eid)))
241a81b76b3SSteve Price (void)printf("euid\t%s\n", pw->pw_name);
2429b50d902SRodney W. Grimes else
243a81b76b3SSteve Price (void)printf("euid\t%u\n", eid);
2449ef5c48bSBill Fumerola }
2459ef5c48bSBill Fumerola if ((rid = getgid()) != (eid = getegid())) {
246d48dbc78SPhilippe Charnier if ((gr = getgrgid(rid)))
2479b50d902SRodney W. Grimes (void)printf("rgid\t%s\n", gr->gr_name);
2489b50d902SRodney W. Grimes else
2499b50d902SRodney W. Grimes (void)printf("rgid\t%u\n", rid);
2509ef5c48bSBill Fumerola }
2519b50d902SRodney W. Grimes (void)printf("groups\t");
2529b50d902SRodney W. Grimes group(NULL, 1);
2539b50d902SRodney W. Grimes }
2549b50d902SRodney W. Grimes }
2559b50d902SRodney W. Grimes
256f82c82dfSEd Schouten static void
id_print(struct passwd * pw,int use_ggl,int p_euid,int p_egid)2577f313541SRobert Drehmel id_print(struct passwd *pw, int use_ggl, int p_euid, int p_egid)
2589b50d902SRodney W. Grimes {
2599b50d902SRodney W. Grimes struct group *gr;
26068b9b81eSRobert Drehmel gid_t gid, egid, lastgid;
26168b9b81eSRobert Drehmel uid_t uid, euid;
262950cc395SStefan Farfeleder int cnt, ngroups;
26354404cfbSBrooks Davis long ngroups_max;
26454404cfbSBrooks Davis gid_t *groups;
26568b9b81eSRobert Drehmel const char *fmt;
2669b50d902SRodney W. Grimes
267f2588359SMike Pritchard if (pw != NULL) {
26868b9b81eSRobert Drehmel uid = pw->pw_uid;
269b5203ad0SDavid Greenman gid = pw->pw_gid;
270f2588359SMike Pritchard }
271f2588359SMike Pritchard else {
272f2588359SMike Pritchard uid = getuid();
273f2588359SMike Pritchard gid = getgid();
274f2588359SMike Pritchard }
27568b9b81eSRobert Drehmel
27654404cfbSBrooks Davis ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
27754404cfbSBrooks Davis if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL)
27854404cfbSBrooks Davis err(1, "malloc");
27954404cfbSBrooks Davis
280f2588359SMike Pritchard if (use_ggl && pw != NULL) {
28154404cfbSBrooks Davis ngroups = ngroups_max;
28268b9b81eSRobert Drehmel getgrouplist(pw->pw_name, gid, groups, &ngroups);
2837f313541SRobert Drehmel }
2847f313541SRobert Drehmel else {
28554404cfbSBrooks Davis ngroups = getgroups(ngroups_max, groups);
2867f313541SRobert Drehmel }
28768b9b81eSRobert Drehmel
288f2588359SMike Pritchard if (pw != NULL)
28968b9b81eSRobert Drehmel printf("uid=%u(%s)", uid, pw->pw_name);
290f2588359SMike Pritchard else
291f2588359SMike Pritchard printf("uid=%u", getuid());
292311e80b1SStefan Farfeleder printf(" gid=%u", gid);
293311e80b1SStefan Farfeleder if ((gr = getgrgid(gid)))
294311e80b1SStefan Farfeleder (void)printf("(%s)", gr->gr_name);
29568b9b81eSRobert Drehmel if (p_euid && (euid = geteuid()) != uid) {
29668b9b81eSRobert Drehmel (void)printf(" euid=%u", euid);
29768b9b81eSRobert Drehmel if ((pw = getpwuid(euid)))
29868b9b81eSRobert Drehmel (void)printf("(%s)", pw->pw_name);
29968b9b81eSRobert Drehmel }
30068b9b81eSRobert Drehmel if (p_egid && (egid = getegid()) != gid) {
30168b9b81eSRobert Drehmel (void)printf(" egid=%u", egid);
30268b9b81eSRobert Drehmel if ((gr = getgrgid(egid)))
30368b9b81eSRobert Drehmel (void)printf("(%s)", gr->gr_name);
30468b9b81eSRobert Drehmel }
3059b50d902SRodney W. Grimes fmt = " groups=%u";
306b5203ad0SDavid Greenman for (lastgid = -1, cnt = 0; cnt < ngroups; ++cnt) {
307b5203ad0SDavid Greenman if (lastgid == (gid = groups[cnt]))
3089b50d902SRodney W. Grimes continue;
30968b9b81eSRobert Drehmel printf(fmt, gid);
310387acdd9SDima Dorfman fmt = ",%u";
311d48dbc78SPhilippe Charnier if ((gr = getgrgid(gid)))
31268b9b81eSRobert Drehmel printf("(%s)", gr->gr_name);
313b5203ad0SDavid Greenman lastgid = gid;
3149b50d902SRodney W. Grimes }
31568b9b81eSRobert Drehmel printf("\n");
31654404cfbSBrooks Davis free(groups);
3179b50d902SRodney W. Grimes }
3189b50d902SRodney W. Grimes
3195bae3124SRobert Watson #ifdef USE_BSM_AUDIT
320f82c82dfSEd Schouten static void
auditid(void)3215bae3124SRobert Watson auditid(void)
3225bae3124SRobert Watson {
3235bae3124SRobert Watson auditinfo_t auditinfo;
3242e61a3d0SChristian S.J. Peron auditinfo_addr_t ainfo_addr;
3252e61a3d0SChristian S.J. Peron int ret, extended;
3265bae3124SRobert Watson
3272e61a3d0SChristian S.J. Peron extended = 0;
3282e61a3d0SChristian S.J. Peron ret = getaudit(&auditinfo);
3292e61a3d0SChristian S.J. Peron if (ret < 0 && errno == E2BIG) {
3302e61a3d0SChristian S.J. Peron if (getaudit_addr(&ainfo_addr, sizeof(ainfo_addr)) < 0)
3312e61a3d0SChristian S.J. Peron err(1, "getaudit_addr");
3322e61a3d0SChristian S.J. Peron extended = 1;
3332e61a3d0SChristian S.J. Peron } else if (ret < 0)
334acc17cb5SRuslan Ermilov err(1, "getaudit");
3352e61a3d0SChristian S.J. Peron if (extended != 0) {
3362e61a3d0SChristian S.J. Peron (void) printf("auid=%d\n"
3372e61a3d0SChristian S.J. Peron "mask.success=0x%08x\n"
3382e61a3d0SChristian S.J. Peron "mask.failure=0x%08x\n"
3392e61a3d0SChristian S.J. Peron "asid=%d\n"
3409b337054SGleb Kurtsou "termid_addr.port=0x%08jx\n"
3412e61a3d0SChristian S.J. Peron "termid_addr.addr[0]=0x%08x\n"
3422e61a3d0SChristian S.J. Peron "termid_addr.addr[1]=0x%08x\n"
3432e61a3d0SChristian S.J. Peron "termid_addr.addr[2]=0x%08x\n"
3442e61a3d0SChristian S.J. Peron "termid_addr.addr[3]=0x%08x\n",
3452e61a3d0SChristian S.J. Peron ainfo_addr.ai_auid, ainfo_addr.ai_mask.am_success,
3462e61a3d0SChristian S.J. Peron ainfo_addr.ai_mask.am_failure, ainfo_addr.ai_asid,
3479b337054SGleb Kurtsou (uintmax_t)ainfo_addr.ai_termid.at_port,
3482e61a3d0SChristian S.J. Peron ainfo_addr.ai_termid.at_addr[0],
3492e61a3d0SChristian S.J. Peron ainfo_addr.ai_termid.at_addr[1],
3502e61a3d0SChristian S.J. Peron ainfo_addr.ai_termid.at_addr[2],
3512e61a3d0SChristian S.J. Peron ainfo_addr.ai_termid.at_addr[3]);
3522e61a3d0SChristian S.J. Peron } else {
3532e61a3d0SChristian S.J. Peron (void) printf("auid=%d\n"
3542e61a3d0SChristian S.J. Peron "mask.success=0x%08x\n"
3552e61a3d0SChristian S.J. Peron "mask.failure=0x%08x\n"
3562e61a3d0SChristian S.J. Peron "asid=%d\n"
3579b337054SGleb Kurtsou "termid.port=0x%08jx\n"
3582e61a3d0SChristian S.J. Peron "termid.machine=0x%08x\n",
3592e61a3d0SChristian S.J. Peron auditinfo.ai_auid, auditinfo.ai_mask.am_success,
3602e61a3d0SChristian S.J. Peron auditinfo.ai_mask.am_failure,
3619b337054SGleb Kurtsou auditinfo.ai_asid, (uintmax_t)auditinfo.ai_termid.port,
3622e61a3d0SChristian S.J. Peron auditinfo.ai_termid.machine);
3632e61a3d0SChristian S.J. Peron }
3645bae3124SRobert Watson }
3655bae3124SRobert Watson #endif
3665bae3124SRobert Watson
367f82c82dfSEd Schouten static void
group(struct passwd * pw,int nflag)368f4ac32deSDavid Malone group(struct passwd *pw, int nflag)
3699b50d902SRodney W. Grimes {
3709b50d902SRodney W. Grimes struct group *gr;
3719b50d902SRodney W. Grimes int cnt, id, lastid, ngroups;
37254404cfbSBrooks Davis long ngroups_max;
37354404cfbSBrooks Davis gid_t *groups;
374da3082d4SDima Dorfman const char *fmt;
3759b50d902SRodney W. Grimes
37654404cfbSBrooks Davis ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1;
37754404cfbSBrooks Davis if ((groups = malloc(sizeof(gid_t) * (ngroups_max))) == NULL)
37854404cfbSBrooks Davis err(1, "malloc");
37954404cfbSBrooks Davis
3809b50d902SRodney W. Grimes if (pw) {
38154404cfbSBrooks Davis ngroups = ngroups_max;
3829b50d902SRodney W. Grimes (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
3839b50d902SRodney W. Grimes } else {
38454404cfbSBrooks Davis ngroups = getgroups(ngroups_max, groups);
3859b50d902SRodney W. Grimes }
3869b50d902SRodney W. Grimes fmt = nflag ? "%s" : "%u";
3879b50d902SRodney W. Grimes for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) {
3889b50d902SRodney W. Grimes if (lastid == (id = groups[cnt]))
3899b50d902SRodney W. Grimes continue;
3909b50d902SRodney W. Grimes if (nflag) {
391d48dbc78SPhilippe Charnier if ((gr = getgrgid(id)))
3929b50d902SRodney W. Grimes (void)printf(fmt, gr->gr_name);
3939b50d902SRodney W. Grimes else
3949b50d902SRodney W. Grimes (void)printf(*fmt == ' ' ? " %u" : "%u",
3959b50d902SRodney W. Grimes id);
3969b50d902SRodney W. Grimes fmt = " %s";
3979b50d902SRodney W. Grimes } else {
3989b50d902SRodney W. Grimes (void)printf(fmt, id);
3999b50d902SRodney W. Grimes fmt = " %u";
4009b50d902SRodney W. Grimes }
4019b50d902SRodney W. Grimes lastid = id;
4029b50d902SRodney W. Grimes }
4039b50d902SRodney W. Grimes (void)printf("\n");
40454404cfbSBrooks Davis free(groups);
4059b50d902SRodney W. Grimes }
4069b50d902SRodney W. Grimes
407f82c82dfSEd Schouten static void
maclabel(void)408300b40afSRobert Watson maclabel(void)
409300b40afSRobert Watson {
410300b40afSRobert Watson char *string;
411300b40afSRobert Watson mac_t label;
412300b40afSRobert Watson int error;
413300b40afSRobert Watson
414300b40afSRobert Watson error = mac_prepare_process_label(&label);
415300b40afSRobert Watson if (error == -1)
416300b40afSRobert Watson errx(1, "mac_prepare_type: %s", strerror(errno));
417300b40afSRobert Watson
418300b40afSRobert Watson error = mac_get_proc(label);
419300b40afSRobert Watson if (error == -1)
420300b40afSRobert Watson errx(1, "mac_get_proc: %s", strerror(errno));
421300b40afSRobert Watson
422300b40afSRobert Watson error = mac_to_text(label, &string);
423300b40afSRobert Watson if (error == -1)
424300b40afSRobert Watson errx(1, "mac_to_text: %s", strerror(errno));
425300b40afSRobert Watson
426300b40afSRobert Watson (void)printf("%s\n", string);
427300b40afSRobert Watson mac_free(label);
428300b40afSRobert Watson free(string);
429300b40afSRobert Watson }
430300b40afSRobert Watson
431f82c82dfSEd Schouten static struct passwd *
who(char * u)432f4ac32deSDavid Malone who(char *u)
4339b50d902SRodney W. Grimes {
4349b50d902SRodney W. Grimes struct passwd *pw;
4359b50d902SRodney W. Grimes long id;
4369b50d902SRodney W. Grimes char *ep;
4379b50d902SRodney W. Grimes
4389b50d902SRodney W. Grimes /*
4399b50d902SRodney W. Grimes * Translate user argument into a pw pointer. First, try to
4409b50d902SRodney W. Grimes * get it as specified. If that fails, try it as a number.
4419b50d902SRodney W. Grimes */
442d48dbc78SPhilippe Charnier if ((pw = getpwnam(u)))
4439b50d902SRodney W. Grimes return(pw);
4449b50d902SRodney W. Grimes id = strtol(u, &ep, 10);
4459b50d902SRodney W. Grimes if (*u && !*ep && (pw = getpwuid(id)))
4469b50d902SRodney W. Grimes return(pw);
447d48dbc78SPhilippe Charnier errx(1, "%s: no such user", u);
4489b50d902SRodney W. Grimes /* NOTREACHED */
4499b50d902SRodney W. Grimes }
4509b50d902SRodney W. Grimes
451f82c82dfSEd Schouten static void
pline(struct passwd * pw)452f4ac32deSDavid Malone pline(struct passwd *pw)
453bd706105SDavid E. O'Brien {
454bd706105SDavid E. O'Brien
455bd706105SDavid E. O'Brien if (!pw) {
4566c97c3d1SStefan Farfeleder if ((pw = getpwuid(getuid())) == NULL)
457bd706105SDavid E. O'Brien err(1, "getpwuid");
458bd706105SDavid E. O'Brien }
459bd706105SDavid E. O'Brien
46015b09e65SBill Fumerola (void)printf("%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n", pw->pw_name,
461bd706105SDavid E. O'Brien pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
46215b09e65SBill Fumerola (long)pw->pw_change, (long)pw->pw_expire, pw->pw_gecos,
463bd706105SDavid E. O'Brien pw->pw_dir, pw->pw_shell);
464bd706105SDavid E. O'Brien }
465bd706105SDavid E. O'Brien
466bd706105SDavid E. O'Brien
467f82c82dfSEd Schouten static void
usage(void)468f4ac32deSDavid Malone usage(void)
4699b50d902SRodney W. Grimes {
4704bf12cb9SRuslan Ermilov
4714bf12cb9SRuslan Ermilov if (isgroups)
4724bf12cb9SRuslan Ermilov (void)fprintf(stderr, "usage: groups [user]\n");
4734bf12cb9SRuslan Ermilov else if (iswhoami)
4744bf12cb9SRuslan Ermilov (void)fprintf(stderr, "usage: whoami\n");
4754bf12cb9SRuslan Ermilov else
4762bfc50bcSEdward Tomasz Napierala (void)fprintf(stderr, "%s\n%s%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
477d48dbc78SPhilippe Charnier "usage: id [user]",
4785bae3124SRobert Watson #ifdef USE_BSM_AUDIT
479f4882b24SRobert Watson " id -A\n",
4805bae3124SRobert Watson #else
4815bae3124SRobert Watson "",
4825bae3124SRobert Watson #endif
483f4882b24SRobert Watson " id -G [-n] [user]",
484f4882b24SRobert Watson " id -M",
485f4882b24SRobert Watson " id -P [user]",
4862bfc50bcSEdward Tomasz Napierala " id -c",
487d48dbc78SPhilippe Charnier " id -g [-nr] [user]",
488f72286afSChris Costello " id -p [user]",
489d48dbc78SPhilippe Charnier " id -u [-nr] [user]");
4909b50d902SRodney W. Grimes exit(1);
4919b50d902SRodney W. Grimes }
492