xref: /csrg-svn/lib/libc/gen/getgrouplist.c (revision 68063)
151661Smckusick /*
261111Sbostic  * Copyright (c) 1991, 1993
361111Sbostic  *	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*68063Smckusick static char sccsid[] = "@(#)getgrouplist.c	8.2 (Berkeley) 12/08/94";
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
getgrouplist(uname,agroup,groups,grpcnt)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 		for (i = 0; grp->gr_mem[i]; i++) {
5051661Smckusick 			if (!strcmp(grp->gr_mem[i], uname)) {
51*68063Smckusick 				if (ngroups >= maxgroups) {
52*68063Smckusick 					ret = -1;
53*68063Smckusick 					break;
54*68063Smckusick 				}
5551661Smckusick 				groups[ngroups++] = grp->gr_gid;
5651661Smckusick 				break;
5751661Smckusick 			}
5851661Smckusick 		}
5951661Smckusick 	}
6051661Smckusick 	endgrent();
6151661Smckusick 	*grpcnt = ngroups;
6251661Smckusick 	return (ret);
6351661Smckusick }
64