net/PeerCredentials: add getpeereid() support
This commit is contained in:
parent
c7621ec0e4
commit
a0825e6ce0
@ -105,19 +105,11 @@ static constexpr Domain server_socket_domain("server_socket");
|
||||
static int
|
||||
get_remote_uid(SocketDescriptor s) noexcept
|
||||
{
|
||||
#ifdef HAVE_GETPEEREID
|
||||
uid_t euid;
|
||||
gid_t egid;
|
||||
|
||||
if (getpeereid(s.Get(), &euid, &egid) == 0)
|
||||
return euid;
|
||||
#else
|
||||
const auto cred = s.GetPeerCredentials();
|
||||
if (!cred.IsDefined())
|
||||
return -1;
|
||||
|
||||
return cred.GetUid();
|
||||
#endif
|
||||
}
|
||||
|
||||
inline void
|
||||
|
@ -20,6 +20,9 @@ class SocketPeerCredentials {
|
||||
|
||||
#ifdef HAVE_STRUCT_UCRED
|
||||
struct ucred cred;
|
||||
#elif defined(HAVE_GETPEEREID)
|
||||
uid_t uid;
|
||||
gid_t gid;
|
||||
#endif
|
||||
|
||||
public:
|
||||
@ -31,6 +34,9 @@ public:
|
||||
c.cred.pid = 0;
|
||||
c.cred.uid = -1;
|
||||
c.cred.gid = -1;
|
||||
#elif defined(HAVE_GETPEEREID)
|
||||
c.uid = static_cast<uid_t>(-1);
|
||||
c.gid = static_cast<gid_t>(-1);
|
||||
#endif
|
||||
return c;
|
||||
}
|
||||
@ -38,6 +44,9 @@ public:
|
||||
constexpr bool IsDefined() const noexcept {
|
||||
#ifdef HAVE_STRUCT_UCRED
|
||||
return cred.pid > 0;
|
||||
#elif defined(HAVE_GETPEEREID)
|
||||
return uid != static_cast<uid_t>(-1) ||
|
||||
gid != static_cast<gid_t>(-1);
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
@ -54,6 +63,8 @@ public:
|
||||
constexpr auto GetUid() const noexcept {
|
||||
#ifdef HAVE_STRUCT_UCRED
|
||||
return cred.uid;
|
||||
#elif defined(HAVE_GETPEEREID)
|
||||
return uid;
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
@ -62,6 +73,8 @@ public:
|
||||
constexpr auto GetGid() const noexcept {
|
||||
#ifdef HAVE_STRUCT_UCRED
|
||||
return cred.gid;
|
||||
#elif defined(HAVE_GETPEEREID)
|
||||
return gid;
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
|
@ -13,6 +13,10 @@
|
||||
#include "io/UniqueFileDescriptor.hxx"
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_GETPEEREID
|
||||
#include <unistd.h> // for getpeereid()
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
@ -235,6 +239,11 @@ SocketDescriptor::GetPeerCredentials() const noexcept
|
||||
&cred.cred, sizeof(cred.cred)) < sizeof(cred.cred))
|
||||
return SocketPeerCredentials::Undefined();
|
||||
return cred;
|
||||
#elif defined(HAVE_GETPEEREID)
|
||||
SocketPeerCredentials cred;
|
||||
return getpeereid(Get(), &cred.uid, &cred.gid) == 0
|
||||
? cred
|
||||
: SocketPeerCredentials::Undefined();
|
||||
#else
|
||||
return SocketPeerCredentials::Undefined();
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user