kdbus: move dbuspolicy init to bus_register_kdbus 71/201471/2
authorAdrian Szyndela <adrian.s@samsung.com>
Fri, 8 Mar 2019 11:33:02 +0000 (12:33 +0100)
committerAdrian Szyndela <adrian.s@samsung.com>
Thu, 14 Mar 2019 10:34:37 +0000 (11:34 +0100)
This changes the moment of checking if user is allowed to connect to
the bus. Now, it reflects standard process a bit more.

The standard process is:
- open fd (e.g. socket);
- check authentication, if needed and possible;
- connect to the bus (say hello).

In kdbus, we have only:
- open kdbus fd;
- connect to the bus (ioctl KDBUS_CMD_HELLO).
Calling libdbuspolicy for authentication fits between the two.

Additionally, and most importantly, this is required to share
the connection between libdbus and libdbuspolicy in the future.

Change-Id: Id6fe1dbc1cdc6ec774316e13fe5d60d862949476

dbus/dbus-transport-kdbus.c
dbus/kdbus-common.c
dbus/kdbus-common.h

index 626c2a7..c0088fc 100755 (executable)
@@ -1225,6 +1225,19 @@ bus_register_kdbus (DBusTransportKdbus *transport,
   int ret;
   dbus_uint64_t flags;
 
+#ifdef LIBDBUSPOLICY
+  void *policy = dbuspolicy1_init (_kdbus_get_path(transport->kdbus));
+  if (NULL == policy)
+    {
+      dbus_set_error (error,
+                      DBUS_ERROR_NO_REPLY,
+                      "Did not receive a reply. Possible causes include: couldn't load dbus policy for kdbus transport or the message bus security policy blocked the reply.");
+      return FALSE;
+    }
+
+  transport->policy = policy;
+#endif
+
   flags = KDBUS_HELLO_ACCEPT_FD;
   if (registration_flags & REGISTER_FLAG_MONITOR)
     flags |= KDBUS_HELLO_MONITOR;
@@ -1239,6 +1252,9 @@ bus_register_kdbus (DBusTransportKdbus *transport,
   if (ret != 0)
     {
       dbus_set_error (error, DBUS_ERROR_FAILED, "Hello failed: %d", -ret);
+#ifdef LIBDBUSPOLICY
+      dbuspolicy1_free (policy);
+#endif
       return FALSE;
     }
 
@@ -1246,6 +1262,9 @@ bus_register_kdbus (DBusTransportKdbus *transport,
   if (NULL == transport->my_DBus_unique_name)
     {
       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, "Hello post failed: %d", -ret);
+#ifdef LIBDBUSPOLICY
+      dbuspolicy1_free (policy);
+#endif
       return FALSE;
     }
 
@@ -3408,6 +3427,7 @@ kdbus_read_message (DBusTransportKdbus *kdbus_transport,
   return ret_size;
 }
 
+#ifdef ENABLE_KDBUS_SYNC_CALLS
 static DBusMessage *
 kdbus_send_sync_call (DBusTransportKdbus  *transport,
                       DBusMessage         *message)
@@ -3457,6 +3477,7 @@ out:
 
   return reply;
 }
+#endif
 
 /**
  * Copy-paste from socket transport. Only renames done.
@@ -4308,9 +4329,6 @@ _dbus_transport_new_for_kdbus (const char *path,
   DBusTransportKdbus *transport;
   DBusString address;
   kdbus_t *kdbus;
-#ifdef LIBDBUSPOLICY
-  void *policy;
-#endif
 
 #ifdef DBUS_ENABLE_VERBOSE_MODE
   const char *dbgenv = _dbus_getenv ("G_DBUS_DEBUG");
@@ -4337,25 +4355,14 @@ _dbus_transport_new_for_kdbus (const char *path,
       goto failed_0;
     }
 
-  kdbus = _kdbus_new ();
+  kdbus = _kdbus_new (path);
   if (NULL == kdbus)
     {
       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
       goto failed_0;
     }
 
-#ifdef LIBDBUSPOLICY
-  policy = dbuspolicy1_init (path);
-  if (NULL == policy)
-    {
-      dbus_set_error (error,
-                      DBUS_ERROR_NO_REPLY,
-                      "Did not receive a reply. Possible causes include: couldn't load dbus policy for kdbus transport or the message bus security policy blocked the reply.");
-      goto failed_0_with_kdbus;
-    }
-#endif
-
-  ret = _kdbus_open (kdbus, path);
+  ret = _kdbus_open (kdbus);
   if (ret < 0)
     {
       dbus_set_error (error,
@@ -4363,7 +4370,7 @@ _dbus_transport_new_for_kdbus (const char *path,
                       "Failed to open file descriptor: %s: %s",
                       path,
                       _dbus_strerror (-ret));
-      goto failed_0_with_kdbus_and_policy;
+      goto failed_0_with_kdbus;
     }
 
   _dbus_verbose ("Successfully connected to kdbus bus %s\n", path);
@@ -4374,9 +4381,6 @@ _dbus_transport_new_for_kdbus (const char *path,
       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
       goto failed_1;
     }
-#ifdef LIBDBUSPOLICY
-  transport->policy = policy;
-#endif
 
   _dbus_string_free (&address);
 
@@ -4384,10 +4388,6 @@ _dbus_transport_new_for_kdbus (const char *path,
 
 failed_1:
   _kdbus_close (kdbus);
-failed_0_with_kdbus_and_policy:
-#ifdef LIBDBUSPOLICY
-  dbuspolicy1_free (policy);
-#endif
 failed_0_with_kdbus:
   _kdbus_free (kdbus);
 failed_0:
index 008f701..fec3855 100755 (executable)
@@ -45,6 +45,7 @@
 
 struct kdbus_t
 {
+  const char *path;                 /**< Path to kdbus file */
   int fd;                                                     /**< File descriptor */
   void *mmap_ptr;                   /**< Mapped memory where kdbus (kernel) writes
                                      *   messages incoming to us.
@@ -252,14 +253,25 @@ get_from_offset (kdbus_t *kdbus,
 }
 
 kdbus_t *
-_kdbus_new ()
+_kdbus_new (const char *path)
 {
-  return dbus_new (kdbus_t, 1);
+  kdbus_t *kdbus = dbus_new (kdbus_t, 1);
+  if (NULL != kdbus)
+    {
+      kdbus->path = _dbus_strdup(path);
+      if (NULL == kdbus->path)
+        {
+          dbus_free (kdbus);
+          kdbus = NULL;
+        }
+    }
+  return kdbus;
 }
 
 void
 _kdbus_free (kdbus_t *kdbus)
 {
+  dbus_free ((char*)kdbus->path);
   dbus_free (kdbus);
 }
 
@@ -267,13 +279,12 @@ _kdbus_free (kdbus_t *kdbus)
  * Opens a connection to the kdbus bus
  *
  * @param kdbus kdbus object
- * @param path the path to kdbus bus
  * @returns 0 on success, -errno on failure
  */
 int
-_kdbus_open (kdbus_t *kdbus, const char *path)
+_kdbus_open (kdbus_t *kdbus)
 {
-  int fd = open (path, O_RDWR|O_CLOEXEC|O_NONBLOCK);
+  int fd = open (kdbus->path, O_RDWR|O_CLOEXEC|O_NONBLOCK);
   if (-1 == fd)
     return -errno;
 
@@ -306,6 +317,12 @@ _kdbus_close (kdbus_t *kdbus)
   return 0;
 }
 
+const char *
+_kdbus_get_path (kdbus_t *kdbus)
+{
+  return kdbus->path;
+}
+
 int
 _kdbus_get_fd (kdbus_t *kdbus)
 {
index e1a652d..e37b606 100755 (executable)
@@ -61,12 +61,13 @@ typedef struct kdbus_t kdbus_t;
 
 typedef __u64 kdbus_bloom_data_t;
 
-kdbus_t *   _kdbus_new                             (void);
+kdbus_t *   _kdbus_new                             (const char *path);
 void        _kdbus_free                            (kdbus_t *kdbus);
 
-int         _kdbus_open                            (kdbus_t *kdbus, const char *path);
+int         _kdbus_open                            (kdbus_t *kdbus);
 int         _kdbus_close                           (kdbus_t *kdbus);
 
+const char *_kdbus_get_path                        (kdbus_t *kdbus);
 int         _kdbus_get_fd                          (kdbus_t *kdbus);
 __u64       _kdbus_get_id                          (kdbus_t *kdbus);
 char       *_kdbus_get_bus_id                      (kdbus_t *kdbus);