getpeereid()

Updated: April 19, 2023

Get the effective credentials of a UNIX-domain peer

Synopsis:

#include <sys/types.h>
#include <unistd.h>

int getpeereid( int s,
                uid_t *euid,
                gid_t *egid );

Arguments:

s
A UNIX-domain socket (see the UNIX protocol) of type SOCK_STREAM on which either you've called connect(), or one returned from accept() after you've called bind() and listen().
euid
NULL, or a pointer to a location where the function can store the effective user ID.
egid
NULL, or a pointer to a location where the function can store the effective group ID.

Library:

libsocket

Use the -l socket option to qcc to link against this library.

Description:

The getpeereid() function gets the effective user and group IDs of the peer connected to a UNIX-domain socket. If euid and egid are non-NULL, the function stores the IDs in the locations they point to.

If you got the socket by calling connect(), the credentials are those of your peer when you called bind(). If the socket was one returned from accept(), the credentials are those of your peer when you called connect(). This mechanism is reliable; there is no way for either side to influence the credentials returned to its peer except by calling the appropriate system call (i.e., either connect() or bind()) under different effective credentials.

UNIX-domain servers and clients commonly use this function to verify each other's credentials.

Returns:

0
Success.
-1
An error occurred; errno is set.

Errors:

EBADF
The argument s isn't a valid descriptor.
ENOTSOCK
The argument s is a file, not a socket.
ENOTCONN
The argument s doesn't refer to a socket on which you've called connect(), or isn't one returned by listen().
EINVAL
The argument s doesn't refer to a socket of type SOCK_STREAM, or the system returned invalid data.

Classification:

NetBSD

Safety:  
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes