initgroups.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
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 
9 #include "squid.h"
10 #include "compat/initgroups.h"
11 
12 #if HAVE_GRP_H
13 #include <grp.h>
14 #endif
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 
28 int initgroups(const char *name, gid_t basegid)
29 {
30 #if HAVE_SETGROUPS
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)) {
44  if (g->gr_gid != basegid) {
45  char **names;
46 
47  for (names = g->gr_mem; *names != NULL; ++names) {
48 
49  if (!strcmp(*names, name))
50  groups[index++] = g->gr_gid;
51 
52  }
53  }
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 
int initgroups(const char *name, gid_t basegid)
Definition: initgroups.c:28
#define NULL
Definition: types.h:166

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors