1 /*-------------------------------------------------------------------------
4 * get peer userid for UNIX-domain socket connection
6 * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
10 * src/port/getpeereid.c
12 *-------------------------------------------------------------------------
17 #include <sys/param.h>
18 #include <sys/socket.h>
26 #ifdef HAVE_SYS_UCRED_H
27 #include <sys/ucred.h>
32 * BSD-style getpeereid() for platforms that lack it.
35 getpeereid(int sock
, uid_t
*uid
, gid_t
*gid
)
37 #if defined(SO_PEERCRED)
38 /* Linux: use getsockopt(SO_PEERCRED) */
39 struct ucred peercred
;
40 ACCEPT_TYPE_ARG3 so_len
= sizeof(peercred
);
42 if (getsockopt(sock
, SOL_SOCKET
, SO_PEERCRED
, &peercred
, &so_len
) != 0 ||
43 so_len
!= sizeof(peercred
))
48 #elif defined(LOCAL_PEERCRED)
49 /* Debian with FreeBSD kernel: use getsockopt(LOCAL_PEERCRED) */
50 struct xucred peercred
;
51 ACCEPT_TYPE_ARG3 so_len
= sizeof(peercred
);
53 if (getsockopt(sock
, 0, LOCAL_PEERCRED
, &peercred
, &so_len
) != 0 ||
54 so_len
!= sizeof(peercred
) ||
55 peercred
.cr_version
!= XUCRED_VERSION
)
57 *uid
= peercred
.cr_uid
;
58 *gid
= peercred
.cr_gid
;
60 #elif defined(HAVE_GETPEERUCRED)
61 /* Solaris: use getpeerucred() */
64 ucred
= NULL
; /* must be initialized to NULL */
65 if (getpeerucred(sock
, &ucred
) == -1)
68 *uid
= ucred_geteuid(ucred
);
69 *gid
= ucred_getegid(ucred
);
72 if (*uid
== (uid_t
) (-1) || *gid
== (gid_t
) (-1))
76 /* No implementation available on this platform */