GETPEEREID(3)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1

🫢 getpeereid(3) β€” bestie, whose creds are these? 🫢

okay so this is the GETPEEREID(3) page from the Library Functions Manual, and it lives in the Standard C Library (libc, -lc). no cap. πŸ’…


✨ NAME ✨

getpeereid β€” get the effective credentials of a UNIX-domain peer

πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc)

🌸 SYNOPSIS 🌸

#include <unistd.h>

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

πŸ’– DESCRIPTION πŸ’–

okay bestie, here's the tea: getpeereid() hands you the effective user and group IDs of the peer connected to a Unix-domain socket. the argument s has to be a connected Unix-domain socket (unix(4)) of type SOCK_STREAM on which either connect(2) or listen(2) has been called. the effective user ID goes into euid, and the effective group ID goes into egid. πŸŽ€

now the important part: the credentials returned to the listen(2) caller are those of its peer at the time it called connect(2); the credentials returned to the connect(2) caller are those of its peer at the time it called listen(2). 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(2) or listen(2)) under different effective credentials. periodt. πŸ”₯

one common use of this routine is for a Unix-domain server to verify the credentials of its client. likewise, the client can verify the credentials of the server. mutual vibe check, but with uids and gids. ✨

🧚 IMPLEMENTATION NOTES 🧚

on FreeBSD, getpeereid() is implemented in terms of the LOCAL_PEERCRED unix(4) socket option. iykyk. πŸ’œ

🫑 RETURN VALUES 🫑

the getpeereid() function returns the value 0 if successful; otherwise the value -1 is returned and the global variable errno is set to indicate the error. classic. 😭

πŸ’€ ERRORS πŸ’€

the getpeereid() function fails if:

  • [EBADF] β€” the argument s is not a valid descriptor.
  • [ENOTSOCK] β€” the argument s is a file, not a socket.
  • [ENOTCONN] β€” the argument s does not refer to a socket on which connect(2) or listen(2) have been called.
  • [EINVAL] β€” the argument s does not refer to a socket of type SOCK_STREAM, or the kernel returned invalid data.
bestie, if you see EINVAL because the kernel returned invalid data, that's not a vibe. πŸ’€

πŸ‘€ SEE ALSO πŸ‘€

πŸ¦‹ HISTORY πŸ¦‹

the getpeereid() function appeared in FreeBSD 4.6. iconic behavior from the 4.6 era. πŸ‘‘


and that's the whole page, bestie: this version is from Debian, dated November 16, 2023, and the manual page is GETPEEREID(3). mwah, go check those peer creds πŸ’‹βœ¨

πŸ“¦ where this page comes from