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