]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
Support LOCAL_PEERCRED found on FreeBSD and MacOS in _dbus_read_credentials_socket()
authorAlex S <iwtcex@gmail.com>
Thu, 13 Mar 2025 08:27:21 +0000 (11:27 +0300)
committerGleb Popov <6yearold@gmail.com>
Fri, 14 Mar 2025 08:50:55 +0000 (11:50 +0300)
cmake/ConfigureChecks.cmake
cmake/config.h.cmake
dbus/dbus-sysdeps-unix.c
meson.build

index b7f37020bfb9bb98d484b2ff4ada6c52ff6258bd..786ba9ddd2c8a16948e0e4cb1af3dab72b0ee427 100644 (file)
@@ -31,6 +31,7 @@ check_include_file(sys/random.h     HAVE_SYS_RANDOM_H)
 check_include_file(sys/resource.h     HAVE_SYS_RESOURCE_H)
 check_include_file(sys/syscall.h HAVE_SYS_SYSCALL_H)
 check_include_file(sys/time.h   HAVE_SYS_TIME_H)# dbus-sysdeps-win.c
+check_include_file(sys/ucred.h  HAVE_SYS_UCRED_H)# dbus-sysdeps-unix.c
 check_include_file(sys/vfs.h    HAVE_SYS_VFS_H)
 check_include_file(syslog.h     HAVE_SYSLOG_H)
 check_include_file(unistd.h     HAVE_UNISTD_H)  # dbus-sysdeps-util-win.c
index 17e0def0f170e40805b99392573425469aad7f68..54625bdc67f6adce571c42ec4dc613ac45f2de12 100644 (file)
 /* Define to 1 if you have sys/time.h */
 #cmakedefine    HAVE_SYS_TIME_H 1
 
+/* Define to 1 if you have sys/ucred.h */
+#cmakedefine    HAVE_SYS_UCRED_H 1
+
 /* Define to 1 if you have unistd.h */
 #cmakedefine   HAVE_UNISTD_H 1
 
 # endif
 #else
 # define DBUS_UNIX
-#endif 
+#endif
 
 #if defined(_WIN32) || defined(_WIN64)
 // mingw mode_t
index 87ba2d86a82c350782f3b26baaa8b675f41beb8f..40b2af3b4b468a0426786ea93bb093c46ab1e509 100644 (file)
@@ -80,6 +80,9 @@
 #ifdef HAVE_GETPEERUCRED
 #include <ucred.h>
 #endif
+#ifdef HAVE_SYS_UCRED_H
+#include <sys/ucred.h>
+#endif
 #ifdef HAVE_ALLOCA_H
 #include <alloca.h>
 #endif
@@ -2373,6 +2376,29 @@ _dbus_read_credentials_socket  (DBusSocket       client_fd,
         pid_read = cr.unp_pid;
         uid_read = cr.unp_euid;
       }
+#elif defined(LOCAL_PEERCRED)
+    /* A variant of SO_PEERCRED found on FreeBSD 13+ and MacOS
+     * The structure returned by getsockopt in this case is called struct xucred
+     * and is documented in unix(4) page of the FreeBSD manual.
+     */
+    struct xucred cr;
+    socklen_t cr_len = sizeof (cr);
+
+    if (getsockopt (client_fd.fd, 0, LOCAL_PEERCRED, &cr, &cr_len) != 0)
+      {
+        _dbus_verbose ("Failed to getsockopt(LOCAL_PEERCRED): %s\n",
+                       _dbus_strerror (errno));
+      }
+    else if (cr_len != sizeof (cr))
+      {
+        _dbus_verbose ("Failed to getsockopt(LOCAL_PEERCRED), returned %d bytes, expected %d\n",
+                       cr_len, (int) sizeof (cr));
+      }
+    else
+      {
+        pid_read = cr.cr_pid;
+        uid_read = cr.cr_uid;
+      }
 #elif defined(HAVE_CMSGCRED)
     /* We only check for HAVE_CMSGCRED, but we're really assuming that the
      * presence of that struct implies SCM_CREDS. Supported by at least
@@ -2462,8 +2488,8 @@ _dbus_read_credentials_socket  (DBusSocket       client_fd,
      * - AIX?
      * - Blackberry?
      * - Cygwin
-     * - FreeBSD 4.6+ (but we prefer SCM_CREDS: it carries the pid)
-     * - Mac OS X
+     * - FreeBSD 4.6+ (but we prefer LOCAL_PEERCRED and fall back to SCM_CREDS: both methods carry the pid)
+     * - Mac OS X (but we prefer LOCAL_PEERCRED if available: it carries the pid)
      * - Minix 3.1.8+
      * - MirBSD?
      * - NetBSD 5.0+ (but LOCAL_PEEREID would be better: it carries the pid)
index 3f4ad31620d14703470d07ac92231fe7f84742a9..7a4067a0511634880eb18d67fc5da6c88eb9877b 100644 (file)
@@ -744,6 +744,7 @@ check_headers = [
     'sys/resource.h',
     'sys/syscall.h',
     'sys/time.h',
+    'sys/ucred.h',
     'sys/vfs.h',
     'unistd.h',
     'ws2tcpip.h',