Skip to content

Commit

Permalink
socket: getpeercreds cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
markokr committed Jun 12, 2014
1 parent 1ba2cf2 commit 235e046
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions usual/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,16 @@ const char *sa2str(const struct sockaddr *sa, char *dst, int dstlen)
}

#ifndef HAVE_GETPEEREID

/*
* Get other side's uid and git for UNIX socket.
* Get other side's uid and gid for UNIX socket.
*/
int getpeereid(int fd, uid_t *uid_p, gid_t *gid_p)
{
pid_t pid;
return getpeercreds(fd, uid_p, gid_p, &pid);
}

#endif

/*
Expand All @@ -204,14 +206,14 @@ int getpeercreds(int fd, uid_t *uid_p, gid_t *gid_p, pid_t *pid_p)
/* What a mess */

#if defined(SO_PEERCRED)

#ifdef HAVE_SYS_UCRED_H
/* linux and others */
#if defined(HAVE_SYS_UCRED_H)
struct sockpeercred cred; /* openbsd */
#else
struct ucred cred; /* linux */
#endif
socklen_t len = sizeof(cred);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len) >= 0) {
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &len) == 0) {
*uid_p = cred.uid;
*gid_p = cred.gid;
*pid_p = cred.pid;
Expand All @@ -221,7 +223,7 @@ int getpeercreds(int fd, uid_t *uid_p, gid_t *gid_p, pid_t *pid_p)
#elif defined(HAVE_GETPEERUCRED)
/* solaris */
ucred_t *cred = NULL;
if (getpeerucred(fd, &cred) >= 0) {
if (getpeerucred(fd, &cred) == 0) {
*uid_p = ucred_geteuid(cred);
*gid_p = ucred_getegid(cred);
*pid_p = ucred_getpid(cred);
Expand All @@ -235,7 +237,7 @@ int getpeercreds(int fd, uid_t *uid_p, gid_t *gid_p, pid_t *pid_p)
/* netbsd */
struct unpcbid cred;
socklen_t len = sizeof(cred);
if (getsockopt(fd, 0, LOCAL_PEEREID, &cred, &len) < 0)
if (getsockopt(fd, 0, LOCAL_PEEREID, &cred, &len) != 0)
return -1;
*uid_p = cred.unp_euid;
*gid_p = cred.unp_egid;
Expand All @@ -244,15 +246,15 @@ int getpeercreds(int fd, uid_t *uid_p, gid_t *gid_p, pid_t *pid_p)
#elif defined(HAVE_GETPEEREID)
/* generic bsd; no pid */
*pid_p = 0;
return getpeereid(fd, uid_p, gid_p);
return getpeereid(fd, uid_p, gid_p) == 0 ? 0 : -1;
#elif defined(LOCAL_PEERCRED)
/* old freebsd, osx; no pid */
/* freebsd, osx, dfly; no pid */
struct xucred cred;
socklen_t len = sizeof(cred);
if (getsockopt(fd, 0, LOCAL_PEERCRED, &cred, &len) < 0)
if (getsockopt(fd, 0, LOCAL_PEERCRED, &cred, &len) != 0)
return -1;
if (cred.cr_version != XUCRED_VERSION) {
errno = EIO;
errno = EINVAL;
return -1;
}
*uid_p = cred.cr_uid;
Expand Down

0 comments on commit 235e046

Please sign in to comment.