Commit | Line | Data |
---|---|---|
bb0cfa24 DF |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
eabc610b KB |
3 | * All rights reserved. |
4 | * | |
269a7923 | 5 | * %sccs.include.redist.c% |
bb0cfa24 DF |
6 | */ |
7 | ||
2ce81398 | 8 | #if defined(LIBC_SCCS) && !defined(lint) |
c5980113 | 9 | static char sccsid[] = "@(#)initgroups.c 5.7 (Berkeley) %G%"; |
eabc610b | 10 | #endif /* LIBC_SCCS and not lint */ |
a4f42d0e SL |
11 | |
12 | /* | |
13 | * initgroups | |
14 | */ | |
a4f42d0e | 15 | #include <sys/param.h> |
c5980113 DS |
16 | #include <stdio.h> |
17 | #include <string.h> | |
18 | #include <unistd.h> | |
a4f42d0e SL |
19 | #include <grp.h> |
20 | ||
21 | struct group *getgrent(); | |
22 | ||
c5980113 | 23 | int |
a4f42d0e | 24 | initgroups(uname, agroup) |
c5980113 | 25 | const char *uname; |
a4f42d0e SL |
26 | int agroup; |
27 | { | |
28 | int groups[NGROUPS], ngroups = 0; | |
29 | register struct group *grp; | |
30 | register int i; | |
31 | ||
60d94477 MK |
32 | /* |
33 | * If installing primary group, duplicate it; | |
34 | * the first element of groups is the effective gid | |
35 | * and will be overwritten when a setgid file is executed. | |
36 | */ | |
37 | if (agroup >= 0) { | |
a4f42d0e | 38 | groups[ngroups++] = agroup; |
60d94477 MK |
39 | groups[ngroups++] = agroup; |
40 | } | |
a4f42d0e | 41 | setgrent(); |
0fb3f429 SL |
42 | while (grp = getgrent()) { |
43 | if (grp->gr_gid == agroup) | |
44 | continue; | |
a4f42d0e SL |
45 | for (i = 0; grp->gr_mem[i]; i++) |
46 | if (!strcmp(grp->gr_mem[i], uname)) { | |
a4f42d0e | 47 | if (ngroups == NGROUPS) { |
0fb3f429 | 48 | fprintf(stderr, "initgroups: %s is in too many groups\n", uname); |
a4f42d0e SL |
49 | goto toomany; |
50 | } | |
7479b5d0 | 51 | groups[ngroups++] = grp->gr_gid; |
a4f42d0e | 52 | } |
0fb3f429 | 53 | } |
a4f42d0e | 54 | toomany: |
4ce247a6 | 55 | endgrent(); |
a4f42d0e | 56 | if (setgroups(ngroups, groups) < 0) { |
0fb3f429 | 57 | perror("setgroups"); |
b3b3b313 | 58 | return (-1); |
a4f42d0e SL |
59 | } |
60 | return (0); | |
61 | } |