]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
driver: Factor out bus_driver_fill_connection_credentials
authorSimon McVittie <smcv@collabora.com>
Wed, 10 Jan 2018 15:36:55 +0000 (15:36 +0000)
committerSimon McVittie <smcv@collabora.com>
Mon, 15 Jan 2018 14:00:20 +0000 (14:00 +0000)
Signed-off-by: Simon McVittie <smcv@collabora.com>
Reviewed-by: Philip Withnall <withnall@endlessm.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=104610

bus/driver.c
bus/driver.h

index eabc7accdb998192707237738936f45bbef3b1b3..fab1a8bacd6c828aa78d6f209c1f64a6bfc69140 100644 (file)
@@ -1903,117 +1903,140 @@ bus_driver_handle_get_connection_selinux_security_context (DBusConnection *conne
   return FALSE;
 }
 
-static dbus_bool_t
-bus_driver_handle_get_connection_credentials (DBusConnection *connection,
-                                              BusTransaction *transaction,
-                                              DBusMessage    *message,
-                                              DBusError      *error)
+/*
+ * Write the credentials of connection @conn (or the bus daemon itself,
+ * if @conn is #NULL) into the a{sv} @asv_iter. Return #FALSE on OOM.
+ */
+dbus_bool_t
+bus_driver_fill_connection_credentials (DBusConnection  *conn,
+                                        DBusMessageIter *asv_iter)
 {
-  DBusConnection *conn;
-  DBusMessage *reply;
-  DBusMessageIter reply_iter;
-  DBusMessageIter array_iter;
   unsigned long ulong_uid, ulong_pid;
   char *s;
   const char *path;
-  const char *service;
-  BusDriverFound found;
-
-  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
-
-  reply = NULL;
 
-  found = bus_driver_get_conn_helper (connection, message, "credentials",
-                                      &service, &conn, error);
-
-  switch (found)
+  if (conn == NULL)
     {
-      case BUS_DRIVER_FOUND_SELF:
-        ulong_pid = _dbus_getpid ();
-        ulong_uid = _dbus_getuid ();
-        break;
-
-      case BUS_DRIVER_FOUND_PEER:
-        if (!dbus_connection_get_unix_process_id (conn, &ulong_pid))
-          ulong_pid = DBUS_PID_UNSET;
-        if (!dbus_connection_get_unix_user (conn, &ulong_uid))
-          ulong_uid = DBUS_UID_UNSET;
-        break;
-      case BUS_DRIVER_FOUND_ERROR:
-        /* fall through */
-      default:
-        goto failed;
+      ulong_pid = _dbus_getpid ();
+      ulong_uid = _dbus_getuid ();
     }
+  else
+    {
+      if (!dbus_connection_get_unix_process_id (conn, &ulong_pid))
+        ulong_pid = DBUS_PID_UNSET;
 
-  reply = _dbus_asv_new_method_return (message, &reply_iter, &array_iter);
-  if (reply == NULL)
-    goto oom;
+      if (!dbus_connection_get_unix_user (conn, &ulong_uid))
+        ulong_uid = DBUS_UID_UNSET;
+    }
 
   /* we can't represent > 32-bit pids; if your system needs them, please
    * add ProcessID64 to the spec or something */
   if (ulong_pid <= _DBUS_UINT32_MAX && ulong_pid != DBUS_PID_UNSET &&
-      !_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_pid))
-    goto oom;
+      !_dbus_asv_add_uint32 (asv_iter, "ProcessID", ulong_pid))
+    return FALSE;
 
   /* we can't represent > 32-bit uids; if your system needs them, please
    * add UnixUserID64 to the spec or something */
   if (ulong_uid <= _DBUS_UINT32_MAX && ulong_uid != DBUS_UID_UNSET &&
-      !_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_uid))
-    goto oom;
+      !_dbus_asv_add_uint32 (asv_iter, "UnixUserID", ulong_uid))
+    return FALSE;
 
   /* FIXME: Obtain the Windows user of the bus daemon itself */
-  if (found == BUS_DRIVER_FOUND_PEER &&
+  if (conn != NULL &&
       dbus_connection_get_windows_user (conn, &s))
     {
       DBusString str;
       dbus_bool_t result;
 
       if (s == NULL)
-        goto oom;
+        return FALSE;
 
       _dbus_string_init_const (&str, s);
       result = _dbus_validate_utf8 (&str, 0, _dbus_string_get_length (&str));
       _dbus_string_free (&str);
       if (result)
         {
-          if (!_dbus_asv_add_string (&array_iter, "WindowsSID", s))
+          if (!_dbus_asv_add_string (asv_iter, "WindowsSID", s))
             {
               dbus_free (s);
-              goto oom;
+              return FALSE;
             }
         }
       dbus_free (s);
     }
 
   /* FIXME: Obtain the security label for the bus daemon itself */
-  if (found == BUS_DRIVER_FOUND_PEER &&
+  if (conn != NULL &&
       _dbus_connection_get_linux_security_label (conn, &s))
     {
       if (s == NULL)
-        goto oom;
+        return FALSE;
 
       /* use the GVariant bytestring convention for strings of unknown
        * encoding: include the \0 in the payload, for zero-copy reading */
-      if (!_dbus_asv_add_byte_array (&array_iter, "LinuxSecurityLabel",
+      if (!_dbus_asv_add_byte_array (asv_iter, "LinuxSecurityLabel",
                                      s, strlen (s) + 1))
         {
           dbus_free (s);
-          goto oom;
+          return FALSE;
         }
 
       dbus_free (s);
     }
 
-  if (found == BUS_DRIVER_FOUND_PEER &&
+  if (conn != NULL &&
       bus_containers_connection_is_contained (conn, &path, NULL, NULL))
     {
-      if (!_dbus_asv_add_object_path (&array_iter,
+      if (!_dbus_asv_add_object_path (asv_iter,
                                       DBUS_INTERFACE_CONTAINERS1 ".Instance",
                                       path))
-        goto oom;
+        return FALSE;
     }
 
-  if (!_dbus_asv_close (&reply_iter, &array_iter))
+  return TRUE;
+}
+
+static dbus_bool_t
+bus_driver_handle_get_connection_credentials (DBusConnection *connection,
+                                              BusTransaction *transaction,
+                                              DBusMessage    *message,
+                                              DBusError      *error)
+{
+  DBusConnection *conn;
+  DBusMessage *reply;
+  DBusMessageIter reply_iter;
+  DBusMessageIter array_iter;
+  const char *service;
+  BusDriverFound found;
+
+  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
+
+  reply = NULL;
+
+  found = bus_driver_get_conn_helper (connection, message, "credentials",
+                                      &service, &conn, error);
+
+  switch (found)
+    {
+      case BUS_DRIVER_FOUND_SELF:
+        conn = NULL;
+        break;
+
+      case BUS_DRIVER_FOUND_PEER:
+        _dbus_assert (conn != NULL);
+        break;
+
+      case BUS_DRIVER_FOUND_ERROR:
+        /* fall through */
+      default:
+        goto failed;
+    }
+
+  reply = _dbus_asv_new_method_return (message, &reply_iter, &array_iter);
+
+  if (reply == NULL ||
+      !bus_driver_fill_connection_credentials (conn, &array_iter) ||
+      !_dbus_asv_close (&reply_iter, &array_iter))
     goto oom;
 
   if (! bus_transaction_send_from_driver (transaction, connection, reply))
index ac1289da41e3f78eab09b141e0327859a961a95e..d7508bf0619ccc763a55a676680f467b12ba23f0 100644 (file)
@@ -55,6 +55,8 @@ dbus_bool_t bus_driver_send_service_owner_changed  (const char     *service_name
 dbus_bool_t bus_driver_generate_introspect_string  (DBusString *xml,
                                                     dbus_bool_t canonical_path,
                                                     DBusMessage *message);
+dbus_bool_t bus_driver_fill_connection_credentials (DBusConnection  *conn,
+                                                    DBusMessageIter *asv_iter);
 
 BusDriverFound bus_driver_get_conn_helper (DBusConnection  *connection,
                                            DBusMessage     *message,