xref: /titanic_53/usr/src/cmd/id/id.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
30*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <locale.h>
34*7c478bd9Sstevel@tonic-gate #include <stdio.h>
35*7c478bd9Sstevel@tonic-gate #include <pwd.h>
36*7c478bd9Sstevel@tonic-gate #include <grp.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
38*7c478bd9Sstevel@tonic-gate #include <unistd.h>
39*7c478bd9Sstevel@tonic-gate #include <string.h>
40*7c478bd9Sstevel@tonic-gate #include <project.h>
41*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #define	PWNULL  ((struct passwd *)0)
44*7c478bd9Sstevel@tonic-gate #define	GRNULL  ((struct group *)0)
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate typedef enum TYPE {
47*7c478bd9Sstevel@tonic-gate 	UID, EUID, GID, EGID, SGID
48*7c478bd9Sstevel@tonic-gate }	TYPE;
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate typedef enum PRINT {
51*7c478bd9Sstevel@tonic-gate 	CURR,		/* Print uid/gid only */
52*7c478bd9Sstevel@tonic-gate 	ALLGROUPS,	/* Print all groups */
53*7c478bd9Sstevel@tonic-gate 	GROUP,		/* Print only group */
54*7c478bd9Sstevel@tonic-gate 	USER		/* Print only uid */
55*7c478bd9Sstevel@tonic-gate }	PRINT;
56*7c478bd9Sstevel@tonic-gate static PRINT mode = CURR;
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate static int usage(void);
59*7c478bd9Sstevel@tonic-gate static void puid(uid_t);
60*7c478bd9Sstevel@tonic-gate static void pgid(gid_t);
61*7c478bd9Sstevel@tonic-gate static void prid(TYPE, uid_t);
62*7c478bd9Sstevel@tonic-gate static int getusergroups(int, gid_t *, char *, gid_t);
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate static int nflag = 0;		/* Output names, not numbers */
65*7c478bd9Sstevel@tonic-gate static int rflag = 0;		/* Output real, not effective IDs */
66*7c478bd9Sstevel@tonic-gate static char stdbuf[BUFSIZ];
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate int
69*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	gid_t *idp;
72*7c478bd9Sstevel@tonic-gate 	uid_t uid, euid;
73*7c478bd9Sstevel@tonic-gate 	gid_t gid, egid, prgid;
74*7c478bd9Sstevel@tonic-gate 	int c, aflag = 0, project_flag = 0;
75*7c478bd9Sstevel@tonic-gate 	struct passwd *pwp;
76*7c478bd9Sstevel@tonic-gate 	int i, j;
77*7c478bd9Sstevel@tonic-gate 	gid_t groupids[NGROUPS_UMAX];
78*7c478bd9Sstevel@tonic-gate 	struct group *gr;
79*7c478bd9Sstevel@tonic-gate 	char *user = NULL;
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
84*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
85*7c478bd9Sstevel@tonic-gate #endif
86*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
87*7c478bd9Sstevel@tonic-gate #ifdef XPG4
88*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "Ggunrp")) != EOF) {
89*7c478bd9Sstevel@tonic-gate #else
90*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "ap")) != EOF) {
91*7c478bd9Sstevel@tonic-gate #endif
92*7c478bd9Sstevel@tonic-gate 		switch (c) {
93*7c478bd9Sstevel@tonic-gate 			case 'G':
94*7c478bd9Sstevel@tonic-gate 				if (mode != CURR)
95*7c478bd9Sstevel@tonic-gate 					return (usage());
96*7c478bd9Sstevel@tonic-gate 				mode = ALLGROUPS;
97*7c478bd9Sstevel@tonic-gate 				break;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 			case 'g':
100*7c478bd9Sstevel@tonic-gate 				if (mode != CURR)
101*7c478bd9Sstevel@tonic-gate 					return (usage());
102*7c478bd9Sstevel@tonic-gate 				mode = GROUP;
103*7c478bd9Sstevel@tonic-gate 				break;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate #ifndef XPG4
106*7c478bd9Sstevel@tonic-gate 			case 'a':
107*7c478bd9Sstevel@tonic-gate 				aflag++;
108*7c478bd9Sstevel@tonic-gate 				break;
109*7c478bd9Sstevel@tonic-gate #endif
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 			case 'n':
112*7c478bd9Sstevel@tonic-gate 				nflag++;
113*7c478bd9Sstevel@tonic-gate 				break;
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 			case 'r':
116*7c478bd9Sstevel@tonic-gate 				rflag++;
117*7c478bd9Sstevel@tonic-gate 				break;
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 			case 'u':
120*7c478bd9Sstevel@tonic-gate 				if (mode != CURR)
121*7c478bd9Sstevel@tonic-gate 					return (usage());
122*7c478bd9Sstevel@tonic-gate 				mode = USER;
123*7c478bd9Sstevel@tonic-gate 				break;
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 			case 'p':
126*7c478bd9Sstevel@tonic-gate 				if (mode != CURR)
127*7c478bd9Sstevel@tonic-gate 					return (usage());
128*7c478bd9Sstevel@tonic-gate 				project_flag++;
129*7c478bd9Sstevel@tonic-gate 				break;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 			case '?':
132*7c478bd9Sstevel@tonic-gate 				return (usage());
133*7c478bd9Sstevel@tonic-gate 		}
134*7c478bd9Sstevel@tonic-gate 	}
135*7c478bd9Sstevel@tonic-gate 	setbuf(stdout, stdbuf);
136*7c478bd9Sstevel@tonic-gate 	argc -= optind-1;
137*7c478bd9Sstevel@tonic-gate 	argv += optind-1;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	/* -n and -r must be combined with one of -[Ggu] */
140*7c478bd9Sstevel@tonic-gate 	/* -r cannot be combined with -G */
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	if ((mode == CURR && (nflag || rflag)) ||
143*7c478bd9Sstevel@tonic-gate 		(mode == ALLGROUPS && rflag) ||
144*7c478bd9Sstevel@tonic-gate 		(argc != 1 && argc != 2))
145*7c478bd9Sstevel@tonic-gate 		return (usage());
146*7c478bd9Sstevel@tonic-gate 	if (argc == 2) {
147*7c478bd9Sstevel@tonic-gate 		if ((pwp = getpwnam(argv[1])) == PWNULL) {
148*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
149*7c478bd9Sstevel@tonic-gate 				gettext("id: invalid user name: \"%s\"\n"),
150*7c478bd9Sstevel@tonic-gate 					argv[1]);
151*7c478bd9Sstevel@tonic-gate 			return (1);
152*7c478bd9Sstevel@tonic-gate 		}
153*7c478bd9Sstevel@tonic-gate 		user = argv[1];
154*7c478bd9Sstevel@tonic-gate 		uid = euid = pwp->pw_uid;
155*7c478bd9Sstevel@tonic-gate 		prgid = gid = egid = pwp->pw_gid;
156*7c478bd9Sstevel@tonic-gate 	} else {
157*7c478bd9Sstevel@tonic-gate 		uid = getuid();
158*7c478bd9Sstevel@tonic-gate 		gid = getgid();
159*7c478bd9Sstevel@tonic-gate 		euid = geteuid();
160*7c478bd9Sstevel@tonic-gate 		egid = getegid();
161*7c478bd9Sstevel@tonic-gate 	}
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	if (mode != CURR) {
164*7c478bd9Sstevel@tonic-gate 		if (!rflag) {
165*7c478bd9Sstevel@tonic-gate 			uid = euid;
166*7c478bd9Sstevel@tonic-gate 			gid = egid;
167*7c478bd9Sstevel@tonic-gate 		}
168*7c478bd9Sstevel@tonic-gate 		if (mode == USER)
169*7c478bd9Sstevel@tonic-gate 			puid(uid);
170*7c478bd9Sstevel@tonic-gate 		else if (mode == GROUP)
171*7c478bd9Sstevel@tonic-gate 			pgid(gid);
172*7c478bd9Sstevel@tonic-gate 		else if (mode == ALLGROUPS) {
173*7c478bd9Sstevel@tonic-gate 			pgid(gid);
174*7c478bd9Sstevel@tonic-gate 			if (user)
175*7c478bd9Sstevel@tonic-gate 				i = getusergroups(NGROUPS_UMAX, groupids, user,
176*7c478bd9Sstevel@tonic-gate 				    prgid);
177*7c478bd9Sstevel@tonic-gate 			else
178*7c478bd9Sstevel@tonic-gate 				i = getgroups(NGROUPS_UMAX, groupids);
179*7c478bd9Sstevel@tonic-gate 			if (i == -1)
180*7c478bd9Sstevel@tonic-gate 				perror("getgroups");
181*7c478bd9Sstevel@tonic-gate 			else if (i > 0) {
182*7c478bd9Sstevel@tonic-gate 				for (j = 0; j < i; ++j) {
183*7c478bd9Sstevel@tonic-gate 					if ((gid = groupids[j]) == egid)
184*7c478bd9Sstevel@tonic-gate 						continue;
185*7c478bd9Sstevel@tonic-gate 					(void) putchar(' ');
186*7c478bd9Sstevel@tonic-gate 					pgid(gid);
187*7c478bd9Sstevel@tonic-gate 				}
188*7c478bd9Sstevel@tonic-gate 			}
189*7c478bd9Sstevel@tonic-gate 		}
190*7c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
191*7c478bd9Sstevel@tonic-gate 	} else {
192*7c478bd9Sstevel@tonic-gate 		prid(UID, uid);
193*7c478bd9Sstevel@tonic-gate 		prid(GID, gid);
194*7c478bd9Sstevel@tonic-gate 		if (uid != euid)
195*7c478bd9Sstevel@tonic-gate 			prid(EUID, euid);
196*7c478bd9Sstevel@tonic-gate 		if (gid != egid)
197*7c478bd9Sstevel@tonic-gate 			prid(EGID, egid);
198*7c478bd9Sstevel@tonic-gate #ifndef XPG4
199*7c478bd9Sstevel@tonic-gate 		if (aflag) {
200*7c478bd9Sstevel@tonic-gate 			if (user)
201*7c478bd9Sstevel@tonic-gate 				i = getusergroups(NGROUPS_UMAX, groupids, user,
202*7c478bd9Sstevel@tonic-gate 				    prgid);
203*7c478bd9Sstevel@tonic-gate 			else
204*7c478bd9Sstevel@tonic-gate 				i = getgroups(NGROUPS_UMAX, groupids);
205*7c478bd9Sstevel@tonic-gate 			if (i == -1)
206*7c478bd9Sstevel@tonic-gate 				perror("getgroups");
207*7c478bd9Sstevel@tonic-gate 			else if (i > 0) {
208*7c478bd9Sstevel@tonic-gate 				(void) printf(" groups=");
209*7c478bd9Sstevel@tonic-gate 				for (idp = groupids; i--; idp++) {
210*7c478bd9Sstevel@tonic-gate 					(void) printf("%d", (int)*idp);
211*7c478bd9Sstevel@tonic-gate 					if (gr = getgrgid(*idp))
212*7c478bd9Sstevel@tonic-gate 						(void) printf("(%s)",
213*7c478bd9Sstevel@tonic-gate 							gr->gr_name);
214*7c478bd9Sstevel@tonic-gate 					if (i)
215*7c478bd9Sstevel@tonic-gate 						(void) putchar(',');
216*7c478bd9Sstevel@tonic-gate 				}
217*7c478bd9Sstevel@tonic-gate 			}
218*7c478bd9Sstevel@tonic-gate 		}
219*7c478bd9Sstevel@tonic-gate #else
220*7c478bd9Sstevel@tonic-gate 		if (user)
221*7c478bd9Sstevel@tonic-gate 			i = getusergroups(NGROUPS_UMAX, groupids, user, prgid);
222*7c478bd9Sstevel@tonic-gate 		else
223*7c478bd9Sstevel@tonic-gate 			i = getgroups(NGROUPS_UMAX, groupids);
224*7c478bd9Sstevel@tonic-gate 		if (i == -1)
225*7c478bd9Sstevel@tonic-gate 			perror("getgroups");
226*7c478bd9Sstevel@tonic-gate 		else if (i > 1) {
227*7c478bd9Sstevel@tonic-gate 			(void) printf(" groups=");
228*7c478bd9Sstevel@tonic-gate 			for (idp = groupids; i--; idp++) {
229*7c478bd9Sstevel@tonic-gate 				if (*idp == egid)
230*7c478bd9Sstevel@tonic-gate 					continue;
231*7c478bd9Sstevel@tonic-gate 				(void) printf("%d", (int)*idp);
232*7c478bd9Sstevel@tonic-gate 				if (gr = getgrgid(*idp))
233*7c478bd9Sstevel@tonic-gate 					(void) printf("(%s)", gr->gr_name);
234*7c478bd9Sstevel@tonic-gate 				if (i)
235*7c478bd9Sstevel@tonic-gate 					(void) putchar(',');
236*7c478bd9Sstevel@tonic-gate 			}
237*7c478bd9Sstevel@tonic-gate 		}
238*7c478bd9Sstevel@tonic-gate #endif
239*7c478bd9Sstevel@tonic-gate 		if (project_flag) {
240*7c478bd9Sstevel@tonic-gate 			struct project proj;
241*7c478bd9Sstevel@tonic-gate 			void *projbuf;
242*7c478bd9Sstevel@tonic-gate 			projid_t curprojid = getprojid();
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 			if ((projbuf = malloc(PROJECT_BUFSZ)) == NULL) {
245*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "unable to allocate "
246*7c478bd9Sstevel@tonic-gate 				    "memory\n");
247*7c478bd9Sstevel@tonic-gate 				return (2);
248*7c478bd9Sstevel@tonic-gate 			}
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 			if (user) {
251*7c478bd9Sstevel@tonic-gate 				if (getdefaultproj(user, &proj, projbuf,
252*7c478bd9Sstevel@tonic-gate 				    PROJECT_BUFSZ) != NULL)
253*7c478bd9Sstevel@tonic-gate 					(void) printf(" projid=%d(%s)",
254*7c478bd9Sstevel@tonic-gate 					    (int)proj.pj_projid, proj.pj_name);
255*7c478bd9Sstevel@tonic-gate 				else
256*7c478bd9Sstevel@tonic-gate 					/*
257*7c478bd9Sstevel@tonic-gate 					 * This can only happen if project
258*7c478bd9Sstevel@tonic-gate 					 * "default" has been removed from
259*7c478bd9Sstevel@tonic-gate 					 * /etc/project file or the whole
260*7c478bd9Sstevel@tonic-gate 					 * project database file was removed.
261*7c478bd9Sstevel@tonic-gate 					 */
262*7c478bd9Sstevel@tonic-gate 					(void) printf(" projid=(NONE)");
263*7c478bd9Sstevel@tonic-gate 			} else {
264*7c478bd9Sstevel@tonic-gate 				if (getprojbyid(curprojid, &proj, projbuf,
265*7c478bd9Sstevel@tonic-gate 				    PROJECT_BUFSZ) == NULL)
266*7c478bd9Sstevel@tonic-gate 					(void) printf(" projid=%d",
267*7c478bd9Sstevel@tonic-gate 					    (int)curprojid);
268*7c478bd9Sstevel@tonic-gate 				else
269*7c478bd9Sstevel@tonic-gate 					(void) printf(" projid=%d(%s)",
270*7c478bd9Sstevel@tonic-gate 					    (int)curprojid, proj.pj_name);
271*7c478bd9Sstevel@tonic-gate 			}
272*7c478bd9Sstevel@tonic-gate 			free(projbuf);
273*7c478bd9Sstevel@tonic-gate 		}
274*7c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
275*7c478bd9Sstevel@tonic-gate 	}
276*7c478bd9Sstevel@tonic-gate 	return (0);
277*7c478bd9Sstevel@tonic-gate }
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate static int
280*7c478bd9Sstevel@tonic-gate usage()
281*7c478bd9Sstevel@tonic-gate {
282*7c478bd9Sstevel@tonic-gate #ifdef XPG4
283*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
284*7c478bd9Sstevel@tonic-gate 	    "Usage: id [-p] [user]\n"
285*7c478bd9Sstevel@tonic-gate 	    "       id -G [-n] [user]\n"
286*7c478bd9Sstevel@tonic-gate 	    "       id -g [-nr] [user]\n"
287*7c478bd9Sstevel@tonic-gate 	    "       id -u [-nr] [user]\n"));
288*7c478bd9Sstevel@tonic-gate #else
289*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Usage: id [-ap] [user]\n"));
290*7c478bd9Sstevel@tonic-gate #endif
291*7c478bd9Sstevel@tonic-gate 	return (2);
292*7c478bd9Sstevel@tonic-gate }
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate static void
295*7c478bd9Sstevel@tonic-gate puid(uid_t uid)
296*7c478bd9Sstevel@tonic-gate {
297*7c478bd9Sstevel@tonic-gate 	struct passwd *pw;
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate 	if (nflag && (pw = getpwuid(uid)) != PWNULL)
300*7c478bd9Sstevel@tonic-gate 		(void) printf("%s", pw->pw_name);
301*7c478bd9Sstevel@tonic-gate 	else
302*7c478bd9Sstevel@tonic-gate 		(void) printf("%u", (int)uid);
303*7c478bd9Sstevel@tonic-gate }
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate static void
306*7c478bd9Sstevel@tonic-gate pgid(gid_t gid)
307*7c478bd9Sstevel@tonic-gate {
308*7c478bd9Sstevel@tonic-gate 	struct group *gr;
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 	if (nflag && (gr = getgrgid(gid)) != GRNULL)
311*7c478bd9Sstevel@tonic-gate 		(void) printf("%s", gr->gr_name);
312*7c478bd9Sstevel@tonic-gate 	else
313*7c478bd9Sstevel@tonic-gate 		(void) printf("%u", (int)gid);
314*7c478bd9Sstevel@tonic-gate }
315*7c478bd9Sstevel@tonic-gate 
316*7c478bd9Sstevel@tonic-gate static void
317*7c478bd9Sstevel@tonic-gate prid(TYPE how, uid_t id)
318*7c478bd9Sstevel@tonic-gate {
319*7c478bd9Sstevel@tonic-gate 	char *s;
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 	switch ((int)how) {
322*7c478bd9Sstevel@tonic-gate 		case UID:
323*7c478bd9Sstevel@tonic-gate 			s = "uid";
324*7c478bd9Sstevel@tonic-gate 			break;
325*7c478bd9Sstevel@tonic-gate 
326*7c478bd9Sstevel@tonic-gate 		case EUID:
327*7c478bd9Sstevel@tonic-gate 			s = " euid";
328*7c478bd9Sstevel@tonic-gate 			break;
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate 		case GID:
331*7c478bd9Sstevel@tonic-gate 			s = " gid";
332*7c478bd9Sstevel@tonic-gate 			break;
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate 		case EGID:
335*7c478bd9Sstevel@tonic-gate 			s = " egid";
336*7c478bd9Sstevel@tonic-gate 			break;
337*7c478bd9Sstevel@tonic-gate 
338*7c478bd9Sstevel@tonic-gate 	}
339*7c478bd9Sstevel@tonic-gate 	if (s != NULL)
340*7c478bd9Sstevel@tonic-gate 		(void) printf("%s=", s);
341*7c478bd9Sstevel@tonic-gate 	(void) printf("%u", (int)id);
342*7c478bd9Sstevel@tonic-gate 	switch ((int)how) {
343*7c478bd9Sstevel@tonic-gate 	case UID:
344*7c478bd9Sstevel@tonic-gate 	case EUID:
345*7c478bd9Sstevel@tonic-gate 		{
346*7c478bd9Sstevel@tonic-gate 			struct passwd *pwp;
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 			if ((pwp = getpwuid(id)) != PWNULL)
349*7c478bd9Sstevel@tonic-gate 				(void) printf("(%s)", pwp->pw_name);
350*7c478bd9Sstevel@tonic-gate 
351*7c478bd9Sstevel@tonic-gate 		}
352*7c478bd9Sstevel@tonic-gate 		break;
353*7c478bd9Sstevel@tonic-gate 	case GID:
354*7c478bd9Sstevel@tonic-gate 	case EGID:
355*7c478bd9Sstevel@tonic-gate 		{
356*7c478bd9Sstevel@tonic-gate 			struct group *grp;
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 			if ((grp = getgrgid(id)) != GRNULL)
359*7c478bd9Sstevel@tonic-gate 				(void) printf("(%s)", grp->gr_name);
360*7c478bd9Sstevel@tonic-gate 		}
361*7c478bd9Sstevel@tonic-gate 		break;
362*7c478bd9Sstevel@tonic-gate 	}
363*7c478bd9Sstevel@tonic-gate }
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate /*
366*7c478bd9Sstevel@tonic-gate  * Get the supplementary group affiliation for the user
367*7c478bd9Sstevel@tonic-gate  */
368*7c478bd9Sstevel@tonic-gate static int getusergroups(gidsetsize, grouplist, user, prgid)
369*7c478bd9Sstevel@tonic-gate int	gidsetsize;
370*7c478bd9Sstevel@tonic-gate gid_t	*grouplist;
371*7c478bd9Sstevel@tonic-gate char	*user;
372*7c478bd9Sstevel@tonic-gate gid_t	prgid;
373*7c478bd9Sstevel@tonic-gate {
374*7c478bd9Sstevel@tonic-gate 	struct group *group;
375*7c478bd9Sstevel@tonic-gate 	char **gr_mem;
376*7c478bd9Sstevel@tonic-gate 	int ngroups = 0;
377*7c478bd9Sstevel@tonic-gate 
378*7c478bd9Sstevel@tonic-gate 	setgrent();
379*7c478bd9Sstevel@tonic-gate 	while ((ngroups < gidsetsize) && ((group = getgrent()) != NULL))
380*7c478bd9Sstevel@tonic-gate 		for (gr_mem = group->gr_mem; *gr_mem; gr_mem++)
381*7c478bd9Sstevel@tonic-gate 			if (strcmp(user, *gr_mem) == 0) {
382*7c478bd9Sstevel@tonic-gate 				if (gidsetsize)
383*7c478bd9Sstevel@tonic-gate 					grouplist[ngroups] = group->gr_gid;
384*7c478bd9Sstevel@tonic-gate 				ngroups++;
385*7c478bd9Sstevel@tonic-gate 			}
386*7c478bd9Sstevel@tonic-gate 	endgrent();
387*7c478bd9Sstevel@tonic-gate 	if (gidsetsize && !ngroups)
388*7c478bd9Sstevel@tonic-gate 		grouplist[ngroups++] = prgid;
389*7c478bd9Sstevel@tonic-gate 	return (ngroups);
390*7c478bd9Sstevel@tonic-gate }
391