summaryrefslogtreecommitdiff
path: root/src/libstddjb/getpeereid.c
blob: bf70aca353acfb1828db7dd35882d3de9b18dc7c (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* ISC license. */

#include <skalibs/sysdeps.h>

#ifdef SKALIBS_HASGETPEEREID
/* syscall exists - do nothing */

#else

#ifdef SKALIBS_HASSOPEERCRED
/* implementation with SO_PEERCRED */

#include <skalibs/nonposix.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <skalibs/getpeereid.h>

int getpeereid (int s, uid_t *u, gid_t *g)
{
  struct ucred blah ;
  unsigned int len = sizeof(blah) ;

  if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &blah, &len) == -1)
    return -1 ;
  *u = blah.uid ;
  *g = blah.gid ;
  return 0 ;
}

#else

#ifdef SKALIBS_HASGETPEERUCRED
/* implementation with getpeerucred() */

#include <skalibs/nonposix.h>
#include <sys/types.h>
#include <ucred.h>

int getpeereid (int s, uid_t *u, gid_t *g)
{
  ucred_t *cred ;
  if (getpeerucred(s, &cred) == -1) return -1 ;
  *u = ucred_geteuid(cred) ;
  *g = ucred_getegid(cred) ;
  ucred_free(cred) ;
  return 0 ;
}

#else

/* can't find a real implementation, make a stub */

#include <sys/types.h>
#include <errno.h>
#include <skalibs/getpeereid.h>

int getpeereid (int s, uid_t *uid, gid_t *gid)
{
  (void)s ;
  *uid = *gid = -1 ;
  errno = ENOSYS ;
  return -1 ;
}

#endif
#endif
#endif