1*51661Smckusick /* 2*51661Smckusick * Copyright (c) 1991 Regents of the University of California. 3*51661Smckusick * All rights reserved. 4*51661Smckusick * 5*51661Smckusick * %sccs.include.redist.c% 6*51661Smckusick */ 7*51661Smckusick 8*51661Smckusick #if defined(LIBC_SCCS) && !defined(lint) 9*51661Smckusick static char sccsid[] = "@(#)getgrouplist.c 5.1 (Berkeley) 11/12/91"; 10*51661Smckusick #endif /* LIBC_SCCS and not lint */ 11*51661Smckusick 12*51661Smckusick /* 13*51661Smckusick * get credential 14*51661Smckusick */ 15*51661Smckusick #include <sys/types.h> 16*51661Smckusick #include <string.h> 17*51661Smckusick #include <grp.h> 18*51661Smckusick 19*51661Smckusick int 20*51661Smckusick getgrouplist(uname, agroup, groups, grpcnt) 21*51661Smckusick const char *uname; 22*51661Smckusick int agroup; 23*51661Smckusick register int *groups; 24*51661Smckusick int *grpcnt; 25*51661Smckusick { 26*51661Smckusick register struct group *grp; 27*51661Smckusick register struct passwd *pw; 28*51661Smckusick register int i, ngroups; 29*51661Smckusick int ret, maxgroups; 30*51661Smckusick 31*51661Smckusick ret = 0; 32*51661Smckusick ngroups = 0; 33*51661Smckusick maxgroups = *grpcnt; 34*51661Smckusick /* 35*51661Smckusick * When installing primary group, duplicate it; 36*51661Smckusick * the first element of groups is the effective gid 37*51661Smckusick * and will be overwritten when a setgid file is executed. 38*51661Smckusick */ 39*51661Smckusick groups[ngroups++] = agroup; 40*51661Smckusick if (maxgroups > 1) 41*51661Smckusick groups[ngroups++] = agroup; 42*51661Smckusick /* 43*51661Smckusick * Scan the group file to find additional groups. 44*51661Smckusick */ 45*51661Smckusick setgrent(); 46*51661Smckusick while (grp = getgrent()) { 47*51661Smckusick if (grp->gr_gid == agroup) 48*51661Smckusick continue; 49*51661Smckusick if (ngroups >= maxgroups) { 50*51661Smckusick ret = -1; 51*51661Smckusick break; 52*51661Smckusick } 53*51661Smckusick for (i = 0; grp->gr_mem[i]; i++) { 54*51661Smckusick if (!strcmp(grp->gr_mem[i], uname)) { 55*51661Smckusick groups[ngroups++] = grp->gr_gid; 56*51661Smckusick break; 57*51661Smckusick } 58*51661Smckusick } 59*51661Smckusick } 60*51661Smckusick endgrent(); 61*51661Smckusick *grpcnt = ngroups; 62*51661Smckusick return (ret); 63*51661Smckusick } 64