Use getpeerucred if available (not tested).

pull/3160/head
Nicholas Marriott 2022-03-08 11:35:06 +00:00
parent 759f949654
commit f97d784f17
2 changed files with 23 additions and 2 deletions

View File

@ -19,6 +19,10 @@
#include <stdio.h> #include <stdio.h>
#ifdef HAVE_UCRED_H
#include <ucred.h>
#endif
#include "compat.h" #include "compat.h"
int int
@ -33,6 +37,21 @@ getpeereid(int s, uid_t *uid, gid_t *gid)
*uid = uc.uid; *uid = uc.uid;
*gid = uc.gid; *gid = uc.gid;
return (0); return (0);
#elif defined(HAVE_GETPEERUCRED)
int
getpeereid(int s, uid_t *uid, gid_t *gid)
{
ucred_t *ucred = NULL;
if (getpeerucred(s, &ucred) == -1)
return (-1);
if ((*uid = ucred_geteuid(ucred)) == -1)
return (-1);
if ((*gid = ucred_getrgid(ucred)) == -1)
return (-1);
ucred_free(ucred);
return (0);
}
#else #else
errno = EOPNOTSUPP; errno = EOPNOTSUPP;
return (-1); return (-1);

View File

@ -128,6 +128,7 @@ AC_CHECK_HEADERS([ \
sys/dir.h \ sys/dir.h \
sys/ndir.h \ sys/ndir.h \
sys/tree.h \ sys/tree.h \
ucred.h \
util.h \ util.h \
]) ])
@ -146,7 +147,8 @@ AC_CHECK_FUNCS([ \
flock \ flock \
prctl \ prctl \
proc_pidinfo \ proc_pidinfo \
sysconf getpeerucred \
sysconf \
]) ])
# Check for functions with a compatibility implementation. # Check for functions with a compatibility implementation.
@ -170,7 +172,7 @@ AC_REPLACE_FUNCS([ \
strlcat \ strlcat \
strlcpy \ strlcpy \
strndup \ strndup \
strsep strsep \
]) ])
AC_FUNC_STRNLEN AC_FUNC_STRNLEN