-
Notifications
You must be signed in to change notification settings - Fork 2
/
peercred.cc
30 lines (28 loc) · 1.11 KB
/
peercred.cc
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
// Copyright (c) 2014 David Caldwell, All Rights Reserved.
#include <sys/un.h>
#include <sys/socket.h>
#if defined(LOCAL_PEERCRED)
# include <sys/ucred.h>
#endif
#include "peercred.h"
#include "strprintf.h"
uid_t get_peer_uid(int socket)
{
#if defined(SO_PEERCRED)
struct ucred cred;
socklen_t cred_len = sizeof(cred);
getsockopt(socket, SOCK_STREAM, SO_PEERCRED, &cred, &cred_len) == 0
|| throw_strerr("Couldn't determine user: getsockopt() failed");
if (cred_len != sizeof(cred)) throw_str("getsockopt returned %d but I wanted %zd", cred_len, sizeof(cred));
return cred.uid;
#elif defined(LOCAL_PEERCRED)
struct xucred cred;
socklen_t cred_len = sizeof(cred);
getsockopt(socket, SOCK_STREAM, LOCAL_PEERCRED, &cred, &cred_len) == 0
|| throw_strerr("Couldn't determine user: getsockopt() failed");
if (cred_len != sizeof(cred)) throw_str("getsockopt returned %d but I wanted %zd", cred_len, sizeof(cred));
return cred.cr_uid;
#else
# error "daemon-manager requires the PEERCRED socket option for security. If your system doesn't support this, I feel bad for you, son."
#endif
}