From 3534b428629be185e096be99e3bd5fdfe32d5544 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Thu, 18 Sep 2014 18:55:44 +0000 Subject: initial commit with rc for skalibs-2.0.0.0 --- src/libstddjb/getpeereid.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/libstddjb/getpeereid.c (limited to 'src/libstddjb/getpeereid.c') diff --git a/src/libstddjb/getpeereid.c b/src/libstddjb/getpeereid.c new file mode 100644 index 0000000..bf70aca --- /dev/null +++ b/src/libstddjb/getpeereid.c @@ -0,0 +1,68 @@ +/* ISC license. */ + +#include + +#ifdef SKALIBS_HASGETPEEREID +/* syscall exists - do nothing */ + +#else + +#ifdef SKALIBS_HASSOPEERCRED +/* implementation with SO_PEERCRED */ + +#include +#include +#include +#include +#include + +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 +#include +#include + +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 +#include +#include + +int getpeereid (int s, uid_t *uid, gid_t *gid) +{ + (void)s ; + *uid = *gid = -1 ; + errno = ENOSYS ; + return -1 ; +} + +#endif +#endif +#endif -- cgit v1.2.3