]>
Commit | Line | Data |
---|---|---|
e3d74828 | 1 | #include "config.h" |
2 | ||
3 | #if HAVE_GRP_H | |
4 | #include <grp.h> | |
5 | #endif | |
6 | #if HAVE_SYS_TYPES_H | |
7 | #include <sys/types.h> | |
8 | #endif | |
9 | #if HAVE_UNISTD_H | |
10 | #include <unistd.h> | |
11 | #endif | |
12 | #if HAVE_STRING_H | |
13 | #include <string.h> | |
14 | #endif | |
15 | #if HAVE_STRINGS_H | |
16 | #include <strings.h> | |
17 | #endif | |
18 | #if HAVE_LIMITS_H | |
19 | #include <limits.h> | |
20 | #endif | |
f77f47bb | 21 | #if !HAVE_INITGROUPS |
22 | #include "initgroups.h" | |
23 | #endif | |
e3d74828 | 24 | |
25 | int initgroups(const char *name, gid_t basegid) | |
26 | { | |
27 | #ifdef HAVE_SETGROUPS | |
28 | #ifndef NGROUPS_MAX | |
29 | #define NGROUPS_MAX 16 | |
30 | #endif | |
31 | ||
32 | gid_t groups[NGROUPS_MAX]; | |
33 | struct group *g; | |
34 | int index = 0; | |
35 | ||
36 | setgrent(); | |
37 | ||
38 | groups[index++] = basegid; | |
39 | ||
40 | while (index < NGROUPS_MAX && ((g = getgrent()) != NULL)) { | |
41 | if (g->gr_gid != basegid) { | |
42 | char **names; | |
43 | ||
44 | for (names = g->gr_mem; *names != NULL; ++names) { | |
45 | ||
46 | if (!strcmp(*names, name)) | |
47 | groups[index++] = g->gr_gid; | |
48 | ||
49 | } | |
50 | } | |
51 | } | |
52 | ||
53 | endgrent(); | |
54 | ||
55 | return setgroups(index, groups); | |
56 | ||
57 | #else | |
58 | ||
59 | return 0; | |
60 | ||
61 | #endif /* def HAVE_SETGROUPS */ | |
62 | } | |
63 |