| 1 | /* |
| 2 | * Copyright (c) 1983 Regents of the University of California. |
| 3 | * All rights reserved. The Berkeley software License Agreement |
| 4 | * specifies the terms and conditions for redistribution. |
| 5 | */ |
| 6 | |
| 7 | #ifndef lint |
| 8 | static char sccsid[] = "@(#)initgroups.c 5.1 (Berkeley) %G%"; |
| 9 | #endif not lint |
| 10 | |
| 11 | /* |
| 12 | * initgroups |
| 13 | */ |
| 14 | #include <stdio.h> |
| 15 | #include <sys/param.h> |
| 16 | #include <grp.h> |
| 17 | |
| 18 | struct group *getgrent(); |
| 19 | |
| 20 | initgroups(uname, agroup) |
| 21 | char *uname; |
| 22 | int agroup; |
| 23 | { |
| 24 | int groups[NGROUPS], ngroups = 0; |
| 25 | register struct group *grp; |
| 26 | register int i; |
| 27 | |
| 28 | if (agroup >= 0) |
| 29 | groups[ngroups++] = agroup; |
| 30 | setgrent(); |
| 31 | while (grp = getgrent()) { |
| 32 | if (grp->gr_gid == agroup) |
| 33 | continue; |
| 34 | for (i = 0; grp->gr_mem[i]; i++) |
| 35 | if (!strcmp(grp->gr_mem[i], uname)) { |
| 36 | if (ngroups == NGROUPS) { |
| 37 | fprintf(stderr, "initgroups: %s is in too many groups\n", uname); |
| 38 | goto toomany; |
| 39 | } |
| 40 | groups[ngroups++] = grp->gr_gid; |
| 41 | } |
| 42 | } |
| 43 | toomany: |
| 44 | endgrent(); |
| 45 | if (setgroups(ngroups, groups) < 0) { |
| 46 | perror("setgroups"); |
| 47 | return (1); |
| 48 | } |
| 49 | return (0); |
| 50 | } |