xref: /csrg-svn/lib/libc/gen/getgrouplist.c (revision 61111)
151661Smckusick /*
2*61111Sbostic  * Copyright (c) 1991, 1993
3*61111Sbostic  *	The Regents of the University of California.  All rights reserved.
451661Smckusick  *
551661Smckusick  * %sccs.include.redist.c%
651661Smckusick  */
751661Smckusick 
851661Smckusick #if defined(LIBC_SCCS) && !defined(lint)
9*61111Sbostic static char sccsid[] = "@(#)getgrouplist.c	8.1 (Berkeley) 06/04/93";
1051661Smckusick #endif /* LIBC_SCCS and not lint */
1151661Smckusick 
1251661Smckusick /*
1351661Smckusick  * get credential
1451661Smckusick  */
1551661Smckusick #include <sys/types.h>
1651661Smckusick #include <string.h>
1751661Smckusick #include <grp.h>
1851661Smckusick 
1951661Smckusick int
2051661Smckusick getgrouplist(uname, agroup, groups, grpcnt)
2151661Smckusick 	const char *uname;
2251661Smckusick 	int agroup;
2351661Smckusick 	register int *groups;
2451661Smckusick 	int *grpcnt;
2551661Smckusick {
2651661Smckusick 	register struct group *grp;
2751661Smckusick 	register struct passwd *pw;
2851661Smckusick 	register int i, ngroups;
2951661Smckusick 	int ret, maxgroups;
3051661Smckusick 
3151661Smckusick 	ret = 0;
3251661Smckusick 	ngroups = 0;
3351661Smckusick 	maxgroups = *grpcnt;
3451661Smckusick 	/*
3551661Smckusick 	 * When installing primary group, duplicate it;
3651661Smckusick 	 * the first element of groups is the effective gid
3751661Smckusick 	 * and will be overwritten when a setgid file is executed.
3851661Smckusick 	 */
3951661Smckusick 	groups[ngroups++] = agroup;
4051661Smckusick 	if (maxgroups > 1)
4151661Smckusick 		groups[ngroups++] = agroup;
4251661Smckusick 	/*
4351661Smckusick 	 * Scan the group file to find additional groups.
4451661Smckusick 	 */
4551661Smckusick 	setgrent();
4651661Smckusick 	while (grp = getgrent()) {
4751661Smckusick 		if (grp->gr_gid == agroup)
4851661Smckusick 			continue;
4951661Smckusick 		if (ngroups >= maxgroups) {
5051661Smckusick 			ret = -1;
5151661Smckusick 			break;
5251661Smckusick 		}
5351661Smckusick 		for (i = 0; grp->gr_mem[i]; i++) {
5451661Smckusick 			if (!strcmp(grp->gr_mem[i], uname)) {
5551661Smckusick 				groups[ngroups++] = grp->gr_gid;
5651661Smckusick 				break;
5751661Smckusick 			}
5851661Smckusick 		}
5951661Smckusick 	}
6051661Smckusick 	endgrent();
6151661Smckusick 	*grpcnt = ngroups;
6251661Smckusick 	return (ret);
6351661Smckusick }
64