getpeereid.c (1147B)
1 /* ISC license. */ 2 3 #include <skalibs/sysdeps.h> 4 5 #ifdef SKALIBS_HASGETPEEREID 6 /* syscall exists - do nothing */ 7 8 #else 9 10 #ifdef SKALIBS_HASSOPEERCRED 11 /* implementation with SO_PEERCRED */ 12 13 #include <skalibs/nonposix.h> 14 #include <sys/socket.h> 15 #include <sys/un.h> 16 #include <skalibs/posixplz.h> 17 18 int getpeereid (int s, uid_t *u, gid_t *g) 19 { 20 struct ucred blah ; 21 socklen_t len = sizeof(blah) ; 22 23 if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &blah, &len) == -1) 24 return -1 ; 25 *u = blah.uid ; 26 *g = blah.gid ; 27 return 0 ; 28 } 29 30 #else 31 32 #ifdef SKALIBS_HASGETPEERUCRED 33 /* implementation with getpeerucred() */ 34 35 #include <skalibs/nonposix.h> 36 #include <ucred.h> 37 #include <skalibs/posixplz.h> 38 39 int getpeereid (int s, uid_t *u, gid_t *g) 40 { 41 ucred_t *cred = 0 ; 42 if (getpeerucred(s, &cred) == -1) return -1 ; 43 *u = ucred_geteuid(cred) ; 44 *g = ucred_getegid(cred) ; 45 ucred_free(cred) ; 46 return 0 ; 47 } 48 49 #else 50 51 /* can't find a real implementation, make a stub */ 52 53 #include <errno.h> 54 #include <skalibs/posixplz.h> 55 56 int getpeereid (int s, uid_t *uid, gid_t *gid) 57 { 58 (void)s ; 59 *uid = *gid = -1 ; 60 errno = ENOSYS ; 61 return -1 ; 62 } 63 64 #endif 65 #endif 66 #endif