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