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