]> git.ipfire.org Git - thirdparty/squid.git/blame - compat/initgroups.c
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / compat / initgroups.c
CommitLineData
f7f3304a 1#include "squid.h"
27bc2077 2#include "compat/initgroups.h"
e3d74828 3
4#if HAVE_GRP_H
5#include <grp.h>
6#endif
e3d74828 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
20int initgroups(const char *name, gid_t basegid)
21{
32d002cb 22#if HAVE_SETGROUPS
e3d74828 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)) {
26ac0430
AJ
36 if (g->gr_gid != basegid) {
37 char **names;
e3d74828 38
26ac0430 39 for (names = g->gr_mem; *names != NULL; ++names) {
e3d74828 40
26ac0430
AJ
41 if (!strcmp(*names, name))
42 groups[index++] = g->gr_gid;
e3d74828 43
26ac0430
AJ
44 }
45 }
e3d74828 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