From 302e193b44660563d2b296a937714c52ec23976b Mon Sep 17 00:00:00 2001 From: Patrick Welche Date: Tue, 20 May 2014 10:11:14 +0100 Subject: [PATCH] Implement NetBSD credentials-passing with LOCAL_PEEREID --- bus/dispatch.c | 1 + configure.ac | 5 +++++ dbus/dbus-sysdeps-unix.c | 37 +++++++++++++++++++++++++++++++------ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/bus/dispatch.c b/bus/dispatch.c index 7a61953..02b84ed 100644 --- a/bus/dispatch.c +++ b/bus/dispatch.c @@ -1309,6 +1309,7 @@ check_get_connection_unix_process_id (BusContext *context, { #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \ defined(__linux__) || \ + defined(__NetBSD__) || \ defined(__OpenBSD__) warn_unexpected (connection, message, "not this error"); diff --git a/configure.ac b/configure.ac index 13d1c87..85f8001 100644 --- a/configure.ac +++ b/configure.ac @@ -811,6 +811,11 @@ if test x$dbus_have_struct_cmsgcred = xyes; then AC_DEFINE(HAVE_CMSGCRED,1,[Have cmsgcred structure]) fi +AC_CHECK_MEMBER([struct unpcbid.unp_pid], + [AC_DEFINE([HAVE_UNPCBID], 1, [Have unpcbid structure])], + [], + [[#include ]]) + AC_CHECK_FUNCS(getpeerucred getpeereid) AC_CHECK_FUNCS(pipe2 accept4) diff --git a/dbus/dbus-sysdeps-unix.c b/dbus/dbus-sysdeps-unix.c index 17b4a8d..ab2141e 100644 --- a/dbus/dbus-sysdeps-unix.c +++ b/dbus/dbus-sysdeps-unix.c @@ -1741,16 +1741,41 @@ _dbus_read_credentials_socket (int client_fd, #endif int cr_len = sizeof (cr); - if (getsockopt (client_fd, SOL_SOCKET, SO_PEERCRED, &cr, &cr_len) == 0 && - cr_len == sizeof (cr)) + if (getsockopt (client_fd, SOL_SOCKET, SO_PEERCRED, &cr, &cr_len) != 0) { - pid_read = cr.pid; - uid_read = cr.uid; + _dbus_verbose ("Failed to getsockopt(SO_PEERCRED): %s\n", + _dbus_strerror (errno)); + } + else if (cr_len != sizeof (cr)) + { + _dbus_verbose ("Failed to getsockopt(SO_PEERCRED), returned %d bytes, expected %d\n", + cr_len, (int) sizeof (cr)); + } + else + { + pid_read = cr.pid; + uid_read = cr.uid; + } +#elif defined(HAVE_UNPCBID) && defined(LOCAL_PEEREID) + /* Another variant of the above - used on NetBSD + */ + struct unpcbid cr; + socklen_t cr_len = sizeof (cr); + + if (getsockopt (client_fd, 0, LOCAL_PEEREID, &cr, &cr_len) != 0) + { + _dbus_verbose ("Failed to getsockopt(LOCAL_PEEREID): %s\n", + _dbus_strerror (errno)); + } + else if (cr_len != sizeof (cr)) + { + _dbus_verbose ("Failed to getsockopt(LOCAL_PEEREID), returned %d bytes, expected %d\n", + cr_len, (int) sizeof (cr)); } else { - _dbus_verbose ("Failed to getsockopt() credentials, returned len %d/%d: %s\n", - cr_len, (int) sizeof (cr), _dbus_strerror (errno)); + pid_read = cr.unp_pid; + uid_read = cr.unp_euid; } #elif defined(HAVE_CMSGCRED) /* We only check for HAVE_CMSGCRED, but we're really assuming that the -- 1.9.3