phunix/lib/libc/net/minix/getpeereid.c
Ben Gras 01624e6f86 <sys/socket.h>, <netinet/{in,tcp,udp,udp_var}.h>
. add sa_len to sockaddr, sin_len to sockaddr_in
	. rename SCM_CREDENTIALS to SCM_CREDS
	. retire PF_FILE (same as PF_UNIX)

Change-Id: Id3ec63fe2556fc7ceb48de8ad4e80736df3ddfc7
2014-03-03 20:47:03 +01:00

38 lines
915 B
C

#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ucred.h>
/*
* get the effective user ID and effective group ID of a peer
* connected through a Unix domain socket.
*/
int getpeereid(int sd, uid_t *euid, gid_t *egid) {
int rc;
struct uucred cred;
socklen_t ucred_length;
/* Initialize Data Structures */
ucred_length = sizeof(struct uucred);
memset(&cred, '\0', ucred_length);
/* Validate Input Parameters */
if (euid == NULL || egid == NULL) {
errno = EFAULT;
return -1;
} /* getsockopt will handle validating 'sd' */
/* Get the credentials of the peer at the other end of 'sd' */
rc = getsockopt(sd, SOL_SOCKET, SO_PEERCRED, &cred, &ucred_length);
if (rc == 0) {
/* Success - return the results */
*euid = cred.cr_uid;
*egid = cred.cr_gid;
return 0;
} else {
/* Failure - getsockopt takes care of setting errno */
return -1;
}
}