From 2763a908f80ee68d1e9788f70d645ab6015365b9 Mon Sep 17 00:00:00 2001 From: Gilles Chehade Date: Sun, 10 Nov 2019 19:40:11 +0100 Subject: conditional getpeereid() --- configure.ac | 1 + openbsd-compat/Makefile.am | 5 ++- openbsd-compat/bsd-getpeereid.c | 73 ----------------------------------------- openbsd-compat/getpeereid.c | 69 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 74 deletions(-) delete mode 100644 openbsd-compat/bsd-getpeereid.c create mode 100644 openbsd-compat/getpeereid.c diff --git a/configure.ac b/configure.ac index b02eb37c..7912abc6 100644 --- a/configure.ac +++ b/configure.ac @@ -2002,6 +2002,7 @@ AM_CONDITIONAL([NEED_FPARSELN], [test "x$ac_cv_have_fparseln" != "xyes"]) AM_CONDITIONAL([NEED_FREEZERO], [test "x$ac_cv_have_freezero" != "xyes"]) #AM_CONDITIONAL([NEED_GETLINE], [test "x$ac_cv_have_getline" != "xyes"]) +AM_CONDITIONAL([NEED_GETPEEREID], [test "x$ac_cv_have_getpeereid" != "xyes"]) AM_CONDITIONAL([NEED_IMSG], [test "x$ac_cv_have_imsg_init" != "xyes"]) AM_CONDITIONAL([NEED_INET_NET_PTON], [test "x$ac_cv_have_inet_net_pton" != "xyes"]) diff --git a/openbsd-compat/Makefile.am b/openbsd-compat/Makefile.am index 2f5b8d14..d2a423c7 100644 --- a/openbsd-compat/Makefile.am +++ b/openbsd-compat/Makefile.am @@ -5,7 +5,6 @@ libopenbsd_compat_a_SOURCES = empty.c libopenbsd_compat_a_SOURCES += arc4random.c libopenbsd_compat_a_SOURCES += SSL_CTX_use_certificate_chain_mem.c libopenbsd_compat_a_SOURCES += base64.c -libopenbsd_compat_a_SOURCES += bsd-getpeereid.c libopenbsd_compat_a_SOURCES += bsd-misc.c libopenbsd_compat_a_SOURCES += bsd-waitpid.c libopenbsd_compat_a_SOURCES += entropy.c @@ -71,6 +70,10 @@ endif #libopenbsd_compat_a_SOURCES += getline.c #endif +#if NEED_GETPEEREID +libopenbsd_compat_a_SOURCES += getpeereid.c +#endif + if NEED_IMSG libopenbsd_compat_a_SOURCES += imsg.c libopenbsd_compat_a_SOURCES += imsg-buffer.c diff --git a/openbsd-compat/bsd-getpeereid.c b/openbsd-compat/bsd-getpeereid.c deleted file mode 100644 index 5f7e677e..00000000 --- a/openbsd-compat/bsd-getpeereid.c +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (c) 2002,2004 Damien Miller - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include "includes.h" - -#if !defined(HAVE_GETPEEREID) - -#include -#include - -#include - -#if defined(SO_PEERCRED) -int -getpeereid(int s, uid_t *euid, gid_t *gid) -{ - struct ucred cred; - socklen_t len = sizeof(cred); - - if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cred, &len) < 0) - return (-1); - *euid = cred.uid; - *gid = cred.gid; - - return (0); -} -#elif defined(HAVE_GETPEERUCRED) - -#ifdef HAVE_UCRED_H -# include -#endif - -int -getpeereid(int s, uid_t *euid, gid_t *gid) -{ - ucred_t *ucred = NULL; - - if (getpeerucred(s, &ucred) == -1) - return (-1); - if ((*euid = ucred_geteuid(ucred)) == -1) - return (-1); - if ((*gid = ucred_getrgid(ucred)) == -1) - return (-1); - - ucred_free(ucred); - - return (0); -} -#else -int -getpeereid(int s, uid_t *euid, gid_t *gid) -{ - *euid = geteuid(); - *gid = getgid(); - - return (0); -} -#endif /* defined(SO_PEERCRED) */ - -#endif /* !defined(HAVE_GETPEEREID) */ diff --git a/openbsd-compat/getpeereid.c b/openbsd-compat/getpeereid.c new file mode 100644 index 00000000..c8ce808f --- /dev/null +++ b/openbsd-compat/getpeereid.c @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2002,2004 Damien Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include "includes.h" + +#include +#include + +#include + +#if defined(SO_PEERCRED) +int +getpeereid(int s, uid_t *euid, gid_t *gid) +{ + struct ucred cred; + socklen_t len = sizeof(cred); + + if (getsockopt(s, SOL_SOCKET, SO_PEERCRED, &cred, &len) < 0) + return (-1); + *euid = cred.uid; + *gid = cred.gid; + + return (0); +} +#elif defined(HAVE_GETPEERUCRED) + +#ifdef HAVE_UCRED_H +# include +#endif + +int +getpeereid(int s, uid_t *euid, gid_t *gid) +{ + ucred_t *ucred = NULL; + + if (getpeerucred(s, &ucred) == -1) + return (-1); + if ((*euid = ucred_geteuid(ucred)) == -1) + return (-1); + if ((*gid = ucred_getrgid(ucred)) == -1) + return (-1); + + ucred_free(ucred); + + return (0); +} +#else +int +getpeereid(int s, uid_t *euid, gid_t *gid) +{ + *euid = geteuid(); + *gid = getgid(); + + return (0); +} +#endif /* defined(SO_PEERCRED) */ -- cgit v1.2.3-59-g8ed1b