10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
23*108Sbasabi * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate * The Regents of the University of California
330Sstevel@tonic-gate * All Rights Reserved
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate * contributors.
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
410Sstevel@tonic-gate
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate * groups - show group memberships
440Sstevel@tonic-gate */
450Sstevel@tonic-gate /* LINTLIBRARY PROTOLIB1 */
460Sstevel@tonic-gate
470Sstevel@tonic-gate #include <grp.h>
480Sstevel@tonic-gate #include <pwd.h>
490Sstevel@tonic-gate #include <stdio.h>
500Sstevel@tonic-gate #include <unistd.h>
510Sstevel@tonic-gate #include <stdlib.h>
520Sstevel@tonic-gate
530Sstevel@tonic-gate extern struct group *getgrgid();
540Sstevel@tonic-gate extern struct passwd *getpwnam();
550Sstevel@tonic-gate extern int _getgroupsbymember(const char *, gid_t[], int, int);
560Sstevel@tonic-gate
570Sstevel@tonic-gate static void showgroups();
580Sstevel@tonic-gate
590Sstevel@tonic-gate static int ngroups_max;
600Sstevel@tonic-gate
61*108Sbasabi int
main(int argc,char * argv[])62*108Sbasabi main(int argc, char *argv[])
630Sstevel@tonic-gate {
64*108Sbasabi int xval = 0;
65*108Sbasabi struct passwd *pw;
660Sstevel@tonic-gate
670Sstevel@tonic-gate ngroups_max = sysconf(_SC_NGROUPS_MAX);
680Sstevel@tonic-gate
690Sstevel@tonic-gate if (ngroups_max < 0) {
700Sstevel@tonic-gate (void) fprintf(stderr,
710Sstevel@tonic-gate "groups: could not get configuration info\n");
720Sstevel@tonic-gate exit(1);
730Sstevel@tonic-gate }
740Sstevel@tonic-gate
750Sstevel@tonic-gate if (ngroups_max == 0)
760Sstevel@tonic-gate exit(0);
770Sstevel@tonic-gate
780Sstevel@tonic-gate if (argc == 1) {
790Sstevel@tonic-gate
800Sstevel@tonic-gate if ((pw = getpwuid(getuid())) == NULL) {
810Sstevel@tonic-gate (void) fprintf(stderr, "groups: No passwd entry\n");
820Sstevel@tonic-gate xval = 1;
830Sstevel@tonic-gate } else
840Sstevel@tonic-gate showgroups(pw);
850Sstevel@tonic-gate
860Sstevel@tonic-gate } else while (*++argv) {
870Sstevel@tonic-gate
880Sstevel@tonic-gate if ((pw = getpwnam(*argv)) == NULL) {
890Sstevel@tonic-gate (void) fprintf(stderr,
900Sstevel@tonic-gate "groups: %s : No such user\n", *argv);
910Sstevel@tonic-gate xval = 1;
920Sstevel@tonic-gate } else {
930Sstevel@tonic-gate if (argc > 2)
940Sstevel@tonic-gate (void) printf("%s : ", *argv);
950Sstevel@tonic-gate showgroups(pw);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate }
980Sstevel@tonic-gate
99*108Sbasabi return (xval);
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate static void
showgroups(struct passwd * pw)104*108Sbasabi showgroups(struct passwd *pw)
1050Sstevel@tonic-gate {
106*108Sbasabi struct group *gr;
1070Sstevel@tonic-gate static gid_t *groups = NULL;
1080Sstevel@tonic-gate int ngroups;
1090Sstevel@tonic-gate int i;
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate if (groups == NULL) {
1120Sstevel@tonic-gate if ((groups = (gid_t *)calloc((uint_t)ngroups_max,
1130Sstevel@tonic-gate sizeof (gid_t))) == 0) {
1140Sstevel@tonic-gate (void) fprintf(stderr,
1150Sstevel@tonic-gate "allocation of %d bytes failed\n",
1160Sstevel@tonic-gate ngroups_max * sizeof (gid_t));
1170Sstevel@tonic-gate exit(1);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate groups[0] = pw->pw_gid;
1210Sstevel@tonic-gate
1220Sstevel@tonic-gate ngroups = _getgroupsbymember(pw->pw_name, groups, ngroups_max, 1);
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate if (gr = getgrgid(groups[0]))
1250Sstevel@tonic-gate (void) printf("%s", gr->gr_name);
1260Sstevel@tonic-gate else
1270Sstevel@tonic-gate (void) printf("%d", (int)pw->pw_gid);
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate for (i = 1; i < ngroups; i++) {
1300Sstevel@tonic-gate if ((gr = getgrgid(groups[i])))
1310Sstevel@tonic-gate (void) printf(" %s", gr->gr_name);
1320Sstevel@tonic-gate else
1330Sstevel@tonic-gate (void) printf(" %d", (int)groups[i]);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate (void) printf("\n");
1370Sstevel@tonic-gate }
138