]> git.ipfire.org Git - thirdparty/squid.git/blame - compat/initgroups.c
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / compat / initgroups.c
CommitLineData
37be9888 1/*
5b74111a 2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
37be9888
AJ
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
f7f3304a 9#include "squid.h"
27bc2077 10#include "compat/initgroups.h"
e3d74828 11
12#if HAVE_GRP_H
13#include <grp.h>
14#endif
e3d74828 15#if HAVE_UNISTD_H
16#include <unistd.h>
17#endif
18#if HAVE_STRING_H
19#include <string.h>
20#endif
21#if HAVE_STRINGS_H
22#include <strings.h>
23#endif
24#if HAVE_LIMITS_H
25#include <limits.h>
26#endif
27
28int initgroups(const char *name, gid_t basegid)
29{
32d002cb 30#if HAVE_SETGROUPS
e3d74828 31#ifndef NGROUPS_MAX
32#define NGROUPS_MAX 16
33#endif
34
35 gid_t groups[NGROUPS_MAX];
36 struct group *g;
37 int index = 0;
38
39 setgrent();
40
41 groups[index++] = basegid;
42
43 while (index < NGROUPS_MAX && ((g = getgrent()) != NULL)) {
26ac0430
AJ
44 if (g->gr_gid != basegid) {
45 char **names;
e3d74828 46
26ac0430 47 for (names = g->gr_mem; *names != NULL; ++names) {
e3d74828 48
26ac0430
AJ
49 if (!strcmp(*names, name))
50 groups[index++] = g->gr_gid;
e3d74828 51
26ac0430
AJ
52 }
53 }
e3d74828 54 }
55
56 endgrent();
57
58 return setgroups(index, groups);
59
60#else
61
62 return 0;
63
64#endif /* def HAVE_SETGROUPS */
65}
66