summaryrefslogtreecommitdiff
path: root/src/libstddjb/prot_grps.c
blob: c5ea35ba9acecc2df99b993e8b2ef0704405b485 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* ISC license. */

/* MT-unsafe */

#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <limits.h>
#include <errno.h>
#include <skalibs/setgroups.h>
#include <skalibs/djbunix.h>

int prot_grps (char const *name)
{
  gid_t tab[NGROUPS_MAX] ;
  struct passwd *pw ;
  int n = prot_readgroups(name, tab, NGROUPS_MAX) ;
  if (n < 0) return n ;
  errno = 0 ;
  pw = getpwnam(name) ;
  if (!pw)
  {
    if (!errno) errno = ENOENT ;
    return -1 ;
  }
  return setgroups_and_gid(pw->pw_gid, n, tab) ;
}