daemon fix and development
authorRadoslaw Pajak <r.pajak@samsung.com>
Thu, 19 Sep 2013 06:43:55 +0000 (08:43 +0200)
committerRadoslaw Pajak <r.pajak@samsung.com>
Thu, 19 Sep 2013 06:43:55 +0000 (08:43 +0200)
 - 2 fixes in original dbus daemon - iterations when error in bus_activation_list_services and
bus_registry_list_services
 - daemon on kdbus development, status:

Hello method must be implemented in library (not posssible to be done by daemon)

NOT WORKING org.freedesktop.DBus methods:
- ListQueuedOwners - corresponding functionality not present in kdbus
- GetAuditSessionData - to be investigated
- StartServiceByName - work in progress

WORKING org.freedesktop.DBus methods:
- RequestName
- ReleaseName
- UpdateActivationEnvironment
- NameHasOwner
- ListNames
- ListActivatableNames
- AddMatch
- RemoveMatch
- GetNameOwner
- GetConnectionUnixUser
- GetConnectionUnixProcessID
- GetConnectionSELinuxSecurityContext
- ReloadConfig
- GetId
- GetConnectionCredentials

Change-Id: Ife35a51f7bc934fda12a647011d9eeeaf6285f70

16 files changed:
bus/activation.c
bus/bus.c
bus/bus.h
bus/connection.c
bus/dispatch.c
bus/driver.c
bus/kdbus-d.c
bus/kdbus-d.h
bus/services.c
bus/services.h
dbus/Makefile.am
dbus/dbus-connection.c
dbus/dbus-transport-kdbus.c
dbus/dbus-transport-kdbus.h
dbus/kdbus-common.c [new file with mode: 0644]
dbus/kdbus-common.h [new file with mode: 0644]

index e03b6fe..124f91b 100644 (file)
@@ -2179,7 +2179,7 @@ bus_activation_list_services (BusActivation *activation,
 
  error:
   for (j = 0; j < i; j++)
-    dbus_free (retval[i]);
+    dbus_free (retval[j]);
   dbus_free (retval);
 
   return FALSE;
index 8f8674a..774952d 100644 (file)
--- a/bus/bus.c
+++ b/bus/bus.c
@@ -70,8 +70,7 @@ struct BusContext
   unsigned int keep_umask : 1;
   unsigned int allow_anonymous : 1;
   unsigned int systemd_activation : 1;
-  unsigned int is_kdbus : 1;
-  DBusConnection *myConnection;
+  DBusConnection *myKdbusConnection;  //todo maybe can be rafctored and removed
 };
 
 static dbus_int32_t server_data_slot = -1;
@@ -433,8 +432,6 @@ process_config_first_time_only (BusContext       *context,
          DBusServer* server;
          char* bus_address;
 
-         context->is_kdbus = TRUE;
-
          if(!strcmp (context->type, "system"))
                  type = DBUS_BUS_SYSTEM;
          else if(!strcmp (context->type, "session"))
@@ -459,8 +456,8 @@ process_config_first_time_only (BusContext       *context,
                  goto oom;
          }
 
-         context->myConnection = daemon_as_client(type, bus_address, error);
-         if(context->myConnection == NULL)
+         context->myKdbusConnection = daemon_as_client(type, bus_address, error);
+         if(context->myKdbusConnection == NULL)
                  goto failed;
       }
       else
@@ -773,6 +770,7 @@ bus_context_new (const DBusString *config_file,
       goto failed;
     }
   context->refcount = 1;
+  context->myKdbusConnection = NULL;
 
   _dbus_generate_uuid (&context->uuid);
 
@@ -971,16 +969,20 @@ bus_context_new (const DBusString *config_file,
 
   dbus_server_free_data_slot (&server_data_slot);
 
-  if(context->myConnection)
+  if(context->myKdbusConnection)
   {
          DBusString unique_name;
 
-         bus_connections_setup_connection(context->connections, context->myConnection);
-         _dbus_string_init_const(&unique_name, ":1.1");
-         if(!bus_connection_complete(context->myConnection, &unique_name, error))
+         bus_connections_setup_connection(context->connections, context->myKdbusConnection);
+         dbus_connection_set_route_peer_messages (context->myKdbusConnection, FALSE);
+         _dbus_string_init_const(&unique_name, ":1.1"); //dbus_bus_get_unique_name(context->myConnection)); this is without :1.
+         if(!bus_connection_complete(context->myKdbusConnection, &unique_name, error))
          {
-                 _dbus_verbose ("bus connection complete failed\n");
+                 _dbus_verbose ("bus connection complete failed for kdbus\n");
+                 _dbus_string_free(&unique_name);
+                 goto failed;
          }
+         _dbus_string_free(&unique_name);
   }
 
   return context;
@@ -1232,7 +1234,7 @@ bus_context_get_loop (BusContext *context)
 
 DBusConnection* bus_context_get_myConnection(BusContext *context)
 {
-  return context->myConnection;
+  return context->myKdbusConnection;
 }
 
 dbus_bool_t
@@ -1332,6 +1334,11 @@ bus_context_get_reply_timeout (BusContext *context)
   return context->limits.reply_timeout;
 }
 
+dbus_bool_t bus_context_is_kdbus(BusContext* context)
+{
+       return context->myKdbusConnection != NULL;
+}
+
 void
 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...) _DBUS_GNUC_PRINTF (3, 4);
 
index b1e2967..f913586 100644 (file)
--- a/bus/bus.h
+++ b/bus/bus.h
@@ -115,6 +115,7 @@ int               bus_context_get_max_services_per_connection    (BusContext
 int               bus_context_get_max_match_rules_per_connection (BusContext       *context);
 int               bus_context_get_max_replies_per_connection     (BusContext       *context);
 int               bus_context_get_reply_timeout                  (BusContext       *context);
+dbus_bool_t      bus_context_is_kdbus                                                   (BusContext       *context);
 void              bus_context_log                                (BusContext       *context,
                                                                   DBusSystemLogSeverity severity,
                                                                   const char       *msg,
index d69758c..129c4f6 100644 (file)
@@ -2016,7 +2016,8 @@ bus_transaction_send_from_driver (BusTransaction *transaction,
   if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
     return FALSE;
 
-  if (bus_connection_is_active (connection))
+  if(!bus_context_is_kdbus(bus_transaction_get_context (transaction))) /* todo kdbus inclusion*/
+    if (bus_connection_is_active (connection))
     {
       if (!dbus_message_set_destination (message,
                                          bus_connection_get_name (connection)))
index 35a4b72..45bc634 100644 (file)
@@ -251,7 +251,8 @@ bus_dispatch (DBusConnection *connection,
     }
 
   /* Assign a sender to the message */
-  if (bus_connection_is_active (connection))
+  if(!bus_context_is_kdbus(context))  /* todo kdbus inclusion - if not kdbus daemon */
+    if (bus_connection_is_active (connection))
     {
       sender = bus_connection_get_name (connection);
       _dbus_assert (sender != NULL);
index e95a79d..7861a52 100644 (file)
 #include <dbus/dbus-marshal-recursive.h>
 #include <string.h>
 
+#include "kdbus-d.h"
+#include <stdio.h>
+#include <errno.h>
+#include <limits.h>
+
 static DBusConnection *
 bus_driver_get_conn_helper (DBusConnection  *connection,
                             DBusMessage     *message,
@@ -98,6 +103,9 @@ bus_driver_send_service_owner_changed (const char     *service_name,
   dbus_bool_t retval;
   const char *null_service;
 
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+         return TRUE;
+
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
   null_service = "";
@@ -147,6 +155,9 @@ bus_driver_send_service_lost (DBusConnection *connection,
 {
   DBusMessage *message;
 
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+         return TRUE;
+
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
   message = dbus_message_new_signal (DBUS_PATH_DBUS,
@@ -190,6 +201,9 @@ bus_driver_send_service_acquired (DBusConnection *connection,
 {
   DBusMessage *message;
 
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+         return TRUE;
+
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
   message = dbus_message_new_signal (DBUS_PATH_DBUS,
@@ -437,6 +451,17 @@ bus_driver_handle_list_services (DBusConnection *connection,
       return FALSE;
     }
 
+
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+  {
+         if(!kdbus_list_services (connection, &services, &len))
+           {
+             dbus_message_unref (reply);
+             BUS_SET_OOM (error);
+             return FALSE;
+           }
+  }
+  else
   if (!bus_registry_list_services (registry, &services, &len))
     {
       dbus_message_unref (reply);
@@ -456,6 +481,7 @@ bus_driver_handle_list_services (DBusConnection *connection,
       return FALSE;
     }
 
+  if(!bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
   {
     /* Include the bus driver in the list */
     const char *v_STRING = DBUS_SERVICE_DBUS;
@@ -627,13 +653,25 @@ bus_driver_handle_acquire_service (DBusConnection *connection,
   retval = FALSE;
   reply = NULL;
 
-  _dbus_string_init_const (&service_name, name);
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+  {
+         if (!bus_registry_acquire_kdbus_service (registry, connection,
+                                                                                message,
+                                            &service_reply, transaction,
+                                            error))
+           goto out;
+  }
+  else
+  {
+         _dbus_string_init_const (&service_name, name);
 
-  if (!bus_registry_acquire_service (registry, connection,
-                                     &service_name, flags,
-                                     &service_reply, transaction,
-                                     error))
-    goto out;
+         if (!bus_registry_acquire_service (registry, connection,
+                                            &service_name, flags,
+                                            &service_reply, transaction,
+                                            error))
+           goto out;
+
+  }
 
   reply = dbus_message_new_method_return (message);
   if (reply == NULL)
@@ -648,6 +686,8 @@ bus_driver_handle_acquire_service (DBusConnection *connection,
       goto out;
     }
 
+ _dbus_verbose ("Reply sender: %s, destination: %s\n", dbus_message_get_sender(reply), dbus_message_get_destination(reply)); //todo kdbus incl
+
   if (!bus_transaction_send_from_driver (transaction, connection, reply))
     {
       BUS_SET_OOM (error);
@@ -691,6 +731,15 @@ bus_driver_handle_release_service (DBusConnection *connection,
 
   _dbus_string_init_const (&service_name, name);
 
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+  {
+         registry = (BusRegistry*) message;
+         /* looks like hack? Yes.
+          * But how to pass message to bus_registry_release_service in other way?
+          * In kdbus mode we don't need registry, though.
+          */
+  }
+
   if (!bus_registry_release_service (registry, connection,
                                      &service_name, &service_reply,
                                      transaction, error))
@@ -754,9 +803,27 @@ bus_driver_handle_service_exists (DBusConnection *connection,
     }
   else
     {
-      _dbus_string_init_const (&service_name, name);
-      service = bus_registry_lookup (registry, &service_name);
-      service_exists = service != NULL;
+         if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+         {
+                 int inter_ret;
+                 struct nameInfo info;
+
+                 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
+                       if((inter_ret == 0) || (inter_ret == -ENOENT))
+                               service_exists = (inter_ret == 0) ? TRUE : FALSE;
+                       else
+                       {
+                               _dbus_verbose("kdbus error checking if name exists: err %d (%m)\n", errno);
+                               dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine whether name '%s' exists", name);
+                               service_exists = FALSE;
+                       }
+         }
+         else
+         {
+             _dbus_string_init_const (&service_name, name);
+             service = bus_registry_lookup (registry, &service_name);
+             service_exists = service != NULL;
+         }
     }
 
   reply = dbus_message_new_method_return (message);
@@ -1026,20 +1093,33 @@ bus_driver_handle_add_match (DBusConnection *connection,
   if (rule == NULL)
     goto failed;
 
-  matchmaker = bus_connection_get_matchmaker (connection);
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+  {
 
-  if (!bus_matchmaker_add_rule (matchmaker, rule))
-    {
-      BUS_SET_OOM (error);
-      goto failed;
-    }
+         if (!kdbus_add_match_rule (connection, message, text, error))
+             goto failed;
 
-  if (!send_ack_reply (connection, transaction,
-                       message, error))
-    {
-      bus_matchmaker_remove_rule (matchmaker, rule);
-      goto failed;
-    }
+         if (!send_ack_reply (connection, transaction,
+                              message, error))
+             goto failed;
+  }
+  else
+  {
+         matchmaker = bus_connection_get_matchmaker (connection);
+
+         if (!bus_matchmaker_add_rule (matchmaker, rule))
+           {
+             BUS_SET_OOM (error);
+             goto failed;
+           }
+
+         if (!send_ack_reply (connection, transaction,
+                              message, error))
+           {
+             bus_matchmaker_remove_rule (matchmaker, rule);
+             goto failed;
+           }
+  }
 
   bus_match_rule_unref (rule);
 
@@ -1082,6 +1162,12 @@ bus_driver_handle_remove_match (DBusConnection *connection,
   if (rule == NULL)
     goto failed;
 
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+  {
+         if(!kdbus_remove_match(connection, message, error))
+                 goto failed;
+  }
+
   /* Send the ack before we remove the rule, since the ack is undone
    * on transaction cancel, but rule removal isn't.
    */
@@ -1089,10 +1175,13 @@ bus_driver_handle_remove_match (DBusConnection *connection,
                        message, error))
     goto failed;
 
-  matchmaker = bus_connection_get_matchmaker (connection);
+  if(!bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+  {
+         matchmaker = bus_connection_get_matchmaker (connection);
 
-  if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
-    goto failed;
+         if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
+               goto failed;
+  }
 
   bus_match_rule_unref (rule);
 
@@ -1117,6 +1206,7 @@ bus_driver_handle_get_service_owner (DBusConnection *connection,
   BusRegistry *registry;
   BusService *service;
   DBusMessage *reply;
+  char unique_name[(unsigned int)(snprintf((char*)base_name, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
@@ -1130,35 +1220,63 @@ bus_driver_handle_get_service_owner (DBusConnection *connection,
                               DBUS_TYPE_INVALID))
       goto failed;
 
-  _dbus_string_init_const (&str, text);
-  service = bus_registry_lookup (registry, &str);
-  if (service == NULL &&
-      _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
-    {
-      /* ORG_FREEDESKTOP_DBUS owns itself */
-      base_name = DBUS_SERVICE_DBUS;
-    }
-  else if (service == NULL)
-    {
-      dbus_set_error (error,
-                      DBUS_ERROR_NAME_HAS_NO_OWNER,
-                      "Could not get owner of name '%s': no such name", text);
-      goto failed;
-    }
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+  {
+         int ret;
+         struct nameInfo info;
+
+         ret = kdbus_NameQuery(text, dbus_connection_get_transport(connection), &info);
+               if(ret == 0) //unique id of the name
+               {
+                       sprintf(unique_name, ":1.%llu", (unsigned long long int)info.uniqueId);
+                       _dbus_verbose("Unique name discovered:%s\n", unique_name);
+                       base_name = unique_name;
+               }
+               else if(ret == -ENOENT)  //name has no owner
+               {
+                         dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
+                                                         "Could not get owner of name '%s': no such name", text);
+                         goto failed;
+               }
+               else
+               {
+                       _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
+                       dbus_set_error (error, DBUS_ERROR_FAILED,
+                                                         "Could not determine unique name for '%s'", text);
+                       goto failed;
+               }
+  }
   else
-    {
-      base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
-      if (base_name == NULL)
-        {
-          /* FIXME - how is this error possible? */
-          dbus_set_error (error,
-                          DBUS_ERROR_FAILED,
-                          "Could not determine unique name for '%s'", text);
-          goto failed;
-        }
-      _dbus_assert (*base_name == ':');
-    }
-
+  {
+         _dbus_string_init_const (&str, text);
+         service = bus_registry_lookup (registry, &str);
+         if (service == NULL &&
+                 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
+               {
+                 /* ORG_FREEDESKTOP_DBUS owns itself */
+                 base_name = DBUS_SERVICE_DBUS;
+               }
+         else if (service == NULL)
+               {
+                 dbus_set_error (error,
+                                                 DBUS_ERROR_NAME_HAS_NO_OWNER,
+                                                 "Could not get owner of name '%s': no such name", text);
+                 goto failed;
+               }
+         else
+               {
+                 base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
+                 if (base_name == NULL)
+                       {
+                         /* FIXME - how is this error possible? */
+                         dbus_set_error (error,
+                                                         DBUS_ERROR_FAILED,
+                                                         "Could not determine unique name for '%s'", text);
+                         goto failed;
+                       }
+                 _dbus_assert (*base_name == ':');
+               }
+  }
   _dbus_assert (base_name != NULL);
 
   reply = dbus_message_new_method_return (message);
@@ -1308,25 +1426,31 @@ bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
-  reply = NULL;
-
-  conn = bus_driver_get_conn_helper (connection, message, "UID", &service,
-                                     error);
-
-  if (conn == NULL)
-    goto failed;
-
   reply = dbus_message_new_method_return (message);
   if (reply == NULL)
     goto oom;
 
-  if (!dbus_connection_get_unix_user (conn, &uid))
-    {
-      dbus_set_error (error,
-                      DBUS_ERROR_FAILED,
-                      "Could not determine UID for '%s'", service);
-      goto failed;
-    }
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+  {
+         if(!kdbus_get_connection_unix_user(connection, message, &uid, error))
+                 goto failed;
+  }
+  else
+  {
+         conn = bus_driver_get_conn_helper (connection, message, "UID", &service,
+                                     error);
+
+         if (conn == NULL)
+                 goto failed;
+
+         if (!dbus_connection_get_unix_user (conn, &uid))
+               {
+                 dbus_set_error (error,
+                                                 DBUS_ERROR_FAILED,
+                                                 "Could not determine UID for '%s'", service);
+                 goto failed;
+               }
+  }
 
   uid32 = uid;
   if (! dbus_message_append_args (reply,
@@ -1365,25 +1489,33 @@ bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
-  reply = NULL;
-
-  conn = bus_driver_get_conn_helper (connection, message, "PID", &service,
-                                     error);
-
-  if (conn == NULL)
-    goto failed;
-
   reply = dbus_message_new_method_return (message);
   if (reply == NULL)
     goto oom;
 
-  if (!dbus_connection_get_unix_process_id (conn, &pid))
-    {
-      dbus_set_error (error,
-                      DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
-                      "Could not determine PID for '%s'", service);
-      goto failed;
-    }
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+  {
+         if(!kdbus_get_connection_unix_process_id(connection, message, &pid, error))
+                 goto failed;
+  }
+  else
+  {
+         conn = bus_driver_get_conn_helper (connection, message, "PID", &service,
+                                                                                error);
+
+         if (conn == NULL)
+               goto failed;
+
+
+
+         if (!dbus_connection_get_unix_process_id (conn, &pid))
+               {
+                 dbus_set_error (error,
+                                                 DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
+                                                 "Could not determine PID for '%s'", service);
+                 goto failed;
+               }
+  }
 
   pid32 = pid;
   if (! dbus_message_append_args (reply,
@@ -1477,29 +1609,35 @@ bus_driver_handle_get_connection_selinux_security_context (DBusConnection *conne
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
-  reply = NULL;
-
-  conn = bus_driver_get_conn_helper (connection, message, "security context",
-                                     &service, error);
-
-  if (conn == NULL)
-    goto failed;
-
   reply = dbus_message_new_method_return (message);
   if (reply == NULL)
     goto oom;
 
-  context = bus_connection_get_selinux_id (conn);
-  if (!context)
-    {
-      dbus_set_error (error,
-                      DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
-                      "Could not determine security context for '%s'", service);
-      goto failed;
-    }
-
-  if (! bus_selinux_append_context (reply, context, error))
-    goto failed;
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+  {
+         if(!kdbus_get_connection_unix_selinux_security_context(connection, message, reply, error))
+                 goto failed;
+  }
+  else
+  {
+         conn = bus_driver_get_conn_helper (connection, message, "security context",
+                                                                                &service, error);
+
+         if (conn == NULL)
+               goto failed;
+
+         context = bus_connection_get_selinux_id (conn);
+         if (!context)
+               {
+                 dbus_set_error (error,
+                                                 DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
+                                                 "Could not determine security context for '%s'", service);
+                 goto failed;
+               }
+
+         if (! bus_selinux_append_context (reply, context, error))
+               goto failed;
+  }
 
   if (! bus_transaction_send_from_driver (transaction, connection, reply))
     goto oom;
@@ -1533,35 +1671,54 @@ bus_driver_handle_get_connection_credentials (DBusConnection *connection,
 
   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
 
-  reply = NULL;
-
-  conn = bus_driver_get_conn_helper (connection, message, "credentials",
-                                     &service, error);
-
-  if (conn == NULL)
-    goto failed;
-
   reply = _dbus_asv_new_method_return (message, &reply_iter, &array_iter);
   if (reply == NULL)
     goto oom;
 
-  /* we can't represent > 32-bit pids; if your system needs them, please
-   * add ProcessID64 to the spec or something */
-  if (dbus_connection_get_unix_process_id (conn, &ulong_val) &&
-      ulong_val <= _DBUS_UINT32_MAX)
-    {
-      if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
-        goto oom;
-    }
-
-  /* we can't represent > 32-bit uids; if your system needs them, please
-   * add UnixUserID64 to the spec or something */
-  if (dbus_connection_get_unix_user (conn, &ulong_val) &&
-      ulong_val <= _DBUS_UINT32_MAX)
-    {
-      if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
-        goto oom;
-    }
+  if(!bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+  {
+         conn = bus_driver_get_conn_helper (connection, message, "credentials",
+                                                                                &service, error);
+
+         if (conn == NULL)
+               goto failed;
+
+         /* we can't represent > 32-bit pids; if your system needs them, please
+          * add ProcessID64 to the spec or something */
+         if (dbus_connection_get_unix_process_id (conn, &ulong_val) &&
+                 ulong_val <= _DBUS_UINT32_MAX)
+               {
+                 if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
+                       goto oom;
+               }
+
+         /* we can't represent > 32-bit uids; if your system needs them, please
+          * add UnixUserID64 to the spec or something */
+         if (dbus_connection_get_unix_user (conn, &ulong_val) &&
+                 ulong_val <= _DBUS_UINT32_MAX)
+               {
+                 if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
+                       goto oom;
+               }
+  }
+  else
+  {
+         if(kdbus_get_connection_unix_process_id(connection, message, &ulong_val, error))
+         {
+                 if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
+                       goto oom;
+         }
+         else
+                 goto failed;
+
+         if(kdbus_get_connection_unix_user(connection, message, &ulong_val, error))
+         {
+                 if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
+                       goto oom;
+         }
+         else
+                 goto failed;
+  }
 
   if (!_dbus_asv_close (&reply_iter, &array_iter))
     goto oom;
index 2153781..385d32f 100644 (file)
 #include <dbus/dbus-connection-internal.h>
 #include <dbus/dbus-bus.h>
 #include "dispatch.h"
+#include <dbus/kdbus-common.h>
+#include <dbus/dbus-transport.h>
+#include <dbus/dbus-transport-kdbus.h>
 
 #include <utils.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <errno.h>
+
+//todo there should be no include below - needed functions should be moved to kdbus-common
+#include <dbus/dbus-transport-kdbus.h>
+
+__u64 sender_name_to_id(const char* name, DBusError* error)
+{
+       __u64 sender_id = 0;
+
+       if(!strncmp(name, ":1.", 3)) /*if name is unique name it must be converted to unique id*/
+               sender_id = strtoull(&name[3], NULL, 10);
+       else
+               dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Could not convert sender of the message into kdbus unique id");
+
+       return sender_id;
+}
 
 char* make_kdbus_bus(DBusBusType type, DBusError *error)
 {
@@ -78,6 +97,7 @@ DBusServer* empty_server_init(char* address)
 DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error)
 {
        DBusConnection* connection;
+       DBusString daemon_name;
 
        dbus_bus_set_bus_connection_address(type, address);
 
@@ -85,11 +105,21 @@ DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *err
        if(connection == NULL)
                return NULL;
 
-       if(dbus_bus_request_name(connection, DBUS_SERVICE_DBUS, 0, error) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
+       _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS);
+       if(!kdbus_register_policy (&daemon_name, connection))
                goto failed;
 
-       dbus_bus_add_match(connection, "type='signal', member='NameAcquired'", error);
-       dbus_bus_add_match(connection, "type='signal', member='NameLost'", error);
+       if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
+               goto failed;
+
+//     dbus_bus_add_match(connection, "type='signal', member='NameAcquired'", error);  //not needed if request name ioctled  by daemon not libdbus
+//     dbus_bus_add_match(connection, "type='signal', member='NameLost'", error);  //todo dispatch and digest this  or ioctl about name where daemon checks name presence
+       if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "type='signal', member='NameLost'"))
+       {
+             dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
+             return FALSE;
+       }
+
        if(dbus_error_is_set(error))
        {
 failed:
@@ -102,3 +132,155 @@ failed:
 
        return connection;
 }
+
+dbus_bool_t kdbus_register_policy (const DBusString *service_name, DBusConnection* connection)
+{
+       int fd;
+
+       _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
+
+       return register_kdbus_policy(_dbus_string_get_const_data(service_name), fd);
+}
+
+dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id)
+{
+       int fd;
+
+       _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
+
+       return request_kdbus_name(fd, _dbus_string_get_const_data(service_name), flags, sender_id);
+}
+
+dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *service_name, __u64 sender_id)
+{
+       int fd;
+
+       _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
+
+       return release_kdbus_name(fd, _dbus_string_get_const_data(service_name), sender_id);
+}
+
+dbus_bool_t kdbus_list_services (DBusConnection* connection, char ***listp, int *array_len)
+{
+       int fd;
+
+       _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
+
+       return list_kdbus_names(fd, listp, array_len);
+}
+
+dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* message, const char* text, DBusError* error)
+{
+       __u64 sender_id;
+
+       sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
+       if(dbus_error_is_set(error))
+               return FALSE;
+
+       if(!add_match_kdbus (dbus_connection_get_transport(connection), sender_id, text))
+       {
+             dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:%d, %s",
+                             sender_id, _dbus_strerror_from_errno ());
+             return FALSE;
+       }
+
+       return TRUE;
+}
+
+dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message, DBusError* error)
+{
+       __u64 sender_id;
+
+       sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
+       if(dbus_error_is_set(error))
+               return FALSE;
+
+       if(!remove_match_kdbus (dbus_connection_get_transport(connection), sender_id))
+       {
+             dbus_set_error (error, _dbus_error_from_errno (errno), "Could not remove match rules for id:%d", sender_id);
+             return FALSE;
+       }
+
+       return TRUE;
+}
+
+dbus_bool_t kdbus_get_connection_unix_user(DBusConnection* connection, DBusMessage* message, unsigned long* uid, DBusError* error)
+{
+       char* name = NULL;
+       struct nameInfo info;
+       int inter_ret;
+       dbus_bool_t ret = FALSE;
+
+       dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
+       inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
+       if(inter_ret == 0) //name found
+       {
+               _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
+               *uid = info.userId;
+               return TRUE;
+       }
+       else if(inter_ret == -ENOENT)  //name has no owner
+               dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get UID of name '%s': no such name", name);
+       else
+       {
+               _dbus_verbose("kdbus error determining UID: err %d (%m)\n", errno);
+               dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine UID for '%s'", name);
+       }
+
+       return ret;
+}
+
+dbus_bool_t kdbus_get_connection_unix_process_id(DBusConnection* connection, DBusMessage* message, unsigned long* pid, DBusError* error)
+{
+       char* name = NULL;
+       struct nameInfo info;
+       int inter_ret;
+       dbus_bool_t ret = FALSE;
+
+       dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
+       inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
+       if(inter_ret == 0) //name found
+       {
+               _dbus_verbose("Process id:%llu\n", (unsigned long long) info.processId);
+               *pid = info.processId;
+               return TRUE;
+       }
+       else if(inter_ret == -ENOENT)  //name has no owner
+               dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get PID of name '%s': no such name", name);
+       else
+       {
+               _dbus_verbose("kdbus error determining PID: err %d (%m)\n", errno);
+               dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine PID for '%s'", name);
+       }
+
+       return ret;
+}
+
+dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* connection, DBusMessage* message, DBusMessage* reply, DBusError* error)
+{
+       char* name = NULL;
+       struct nameInfo info;
+       int inter_ret;
+       dbus_bool_t ret = FALSE;
+
+       dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
+       inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
+       if(inter_ret == -ENOENT)  //name has no owner
+               dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get security context of name '%s': no such name", name);
+       else if(inter_ret < 0)
+       {
+               _dbus_verbose("kdbus error determining security context: err %d (%m)\n", errno);
+               dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine security context for '%s'", name);
+       }
+       else
+       {
+               if (!dbus_message_append_args (reply, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &info.sec_label, info.sec_label_len, DBUS_TYPE_INVALID))
+               {
+                     _DBUS_SET_OOM (error);
+                     return FALSE;
+               }
+               ret = TRUE;
+       }
+
+       return ret;
+}
index c8874a7..57acfe0 100644 (file)
 #include <dbus/dbus-bus.h>
 #include "bus.h"
 #include <dbus/dbus-server.h>
+#include <linux/types.h>
+#include <dbus/dbus-transport-kdbus.h>
 
+__u64 sender_name_to_id(const char* name, DBusError* error);
 char* make_kdbus_bus(DBusBusType type, DBusError *error);
 DBusServer* empty_server_init(char* address);
 DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error);
+dbus_bool_t kdbus_register_policy (const DBusString *service_name, DBusConnection* connection);
+dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id);
+dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *service_name, __u64 sender_id);
+dbus_bool_t kdbus_list_services (DBusConnection* connection, char ***listp, int *array_len);
+dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* message, const char* text, DBusError* error);
+dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message, DBusError* error);
+dbus_bool_t kdbus_get_connection_unix_user(DBusConnection* connection, DBusMessage* message, unsigned long* uid, DBusError* error);
+dbus_bool_t kdbus_get_connection_unix_process_id(DBusConnection* connection, DBusMessage* message, unsigned long* pid, DBusError* error);
+dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* connection, DBusMessage* message, DBusMessage* reply, DBusError* error);
 
 #endif /* KDBUS_H_ */
index 6f380fa..322ffcf 100644 (file)
 #include <dbus/dbus-list.h>
 #include <dbus/dbus-mempool.h>
 #include <dbus/dbus-marshal-validate.h>
+#include <linux/types.h>
+#include <errno.h>
+#include <stdlib.h>
+
 
 #include "driver.h"
 #include "services.h"
@@ -36,6 +40,7 @@
 #include "policy.h"
 #include "bus.h"
 #include "selinux.h"
+#include "kdbus-d.h"
 
 struct BusService
 {
@@ -368,7 +373,7 @@ bus_registry_list_services (BusRegistry *registry,
   
  error:
   for (j = 0; j < i; j++)
-    dbus_free (retval[i]);
+    dbus_free (retval[j]);
   dbus_free (retval);
 
   return FALSE;
@@ -437,153 +442,263 @@ bus_registry_acquire_service (BusRegistry      *registry,
    * in bus_connection_selinux_allows_acquire_service()
    */
   sid = bus_selinux_id_table_lookup (registry->service_sid_table,
-                                     service_name);
+                                                                        service_name);
 
   if (!bus_selinux_allows_acquire_service (connection, sid,
                                           _dbus_string_get_const_data (service_name), error))
-    {
+       {
 
-      if (dbus_error_is_set (error) &&
+         if (dbus_error_is_set (error) &&
          dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
        {
          goto out;
        }
 
-      dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
-                      "Connection \"%s\" is not allowed to own the service \"%s\" due "
-                      "to SELinux policy",
-                      bus_connection_is_active (connection) ?
-                      bus_connection_get_name (connection) :
-                      "(inactive)",
-                      _dbus_string_get_const_data (service_name));
-      goto out;
-    }
-  
+         dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
+                                         "Connection \"%s\" is not allowed to own the service \"%s\" due "
+                                         "to SELinux policy",
+                                         bus_connection_is_active (connection) ?
+                                         bus_connection_get_name (connection) :
+                                         "(inactive)",
+                                         _dbus_string_get_const_data (service_name));
+         goto out;
+       }
+
   if (!bus_client_policy_check_can_own (policy, service_name))
-    {
-      dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
-                      "Connection \"%s\" is not allowed to own the service \"%s\" due "
-                      "to security policies in the configuration file",
-                      bus_connection_is_active (connection) ?
-                      bus_connection_get_name (connection) :
-                      "(inactive)",
-                      _dbus_string_get_const_data (service_name));
-      goto out;
-    }
+       {
+         dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
+                                         "Connection \"%s\" is not allowed to own the service \"%s\" due "
+                                         "to security policies in the configuration file",
+                                         bus_connection_is_active (connection) ?
+                                         bus_connection_get_name (connection) :
+                                         "(inactive)",
+                                         _dbus_string_get_const_data (service_name));
+         goto out;
+       }
 
   if (bus_connection_get_n_services_owned (connection) >=
-      bus_context_get_max_services_per_connection (registry->context))
-    {
-      dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
-                      "Connection \"%s\" is not allowed to own more services "
-                      "(increase limits in configuration file if required)",
-                      bus_connection_is_active (connection) ?
-                      bus_connection_get_name (connection) :
-                      "(inactive)");
-      goto out;
-    }
-  
+         bus_context_get_max_services_per_connection (registry->context))
+       {
+         dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
+                                         "Connection \"%s\" is not allowed to own more services "
+                                         "(increase limits in configuration file if required)",
+                                         bus_connection_is_active (connection) ?
+                                         bus_connection_get_name (connection) :
+                                         "(inactive)");
+         goto out;
+       }
+
   service = bus_registry_lookup (registry, service_name);
 
   if (service != NULL)
-    {
-      primary_owner = bus_service_get_primary_owner (service);
-      if (primary_owner != NULL)
-        old_owner_conn = primary_owner->conn;
-      else
-        old_owner_conn = NULL;
-    }
+       {
+         primary_owner = bus_service_get_primary_owner (service);
+         if (primary_owner != NULL)
+               old_owner_conn = primary_owner->conn;
+         else
+               old_owner_conn = NULL;
+       }
   else
-    old_owner_conn = NULL;
-      
+       old_owner_conn = NULL;
+
   if (service == NULL)
-    {
-      service = bus_registry_ensure (registry,
-                                     service_name, connection, flags,
-                                     transaction, error);
-      if (service == NULL)
-        goto out;
-    }
+       {
+         service = bus_registry_ensure (registry,
+                                                                        service_name, connection, flags,
+                                                                        transaction, error);
+         if (service == NULL)
+               goto out;
+       }
 
   primary_owner = bus_service_get_primary_owner (service);
   if (primary_owner == NULL)
-    goto out;
+       goto out;
 
   if (old_owner_conn == NULL)
-    {
-      _dbus_assert (primary_owner->conn == connection);
+       {
+         _dbus_assert (primary_owner->conn == connection);
 
-      *result = DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;      
-    }
+         *result = DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
+       }
   else if (old_owner_conn == connection)
-    {
-      bus_owner_set_flags (primary_owner, flags);
-      *result = DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER;
-    }
+       {
+         bus_owner_set_flags (primary_owner, flags);
+         *result = DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER;
+       }
   else if (((flags & DBUS_NAME_FLAG_DO_NOT_QUEUE) &&
-           !(bus_service_get_allow_replacement (service))) ||
+                  !(bus_service_get_allow_replacement (service))) ||
           ((flags & DBUS_NAME_FLAG_DO_NOT_QUEUE) &&
-           !(flags & DBUS_NAME_FLAG_REPLACE_EXISTING))) 
+                  !(flags & DBUS_NAME_FLAG_REPLACE_EXISTING)))
+       {
+         DBusList *link;
+         BusOwner *temp_owner;
+       /* Since we can't be queued if we are already in the queue
+          remove us */
+
+         link = _bus_service_find_owner_link (service, connection);
+         if (link != NULL)
+               {
+                 _dbus_list_unlink (&service->owners, link);
+                 temp_owner = (BusOwner *)link->data;
+                 bus_owner_unref (temp_owner);
+                 _dbus_list_free_link (link);
+               }
+
+         *result = DBUS_REQUEST_NAME_REPLY_EXISTS;
+       }
+  else if (!(flags & DBUS_NAME_FLAG_DO_NOT_QUEUE) &&
+                  (!(flags & DBUS_NAME_FLAG_REPLACE_EXISTING) ||
+               !(bus_service_get_allow_replacement (service))))
+       {
+         /* Queue the connection */
+         if (!bus_service_add_owner (service, connection,
+                                                                 flags,
+                                                                 transaction, error))
+               goto out;
+
+         *result = DBUS_REQUEST_NAME_REPLY_IN_QUEUE;
+       }
+  else
+       {
+         /* Replace the current owner */
+
+         /* We enqueue the new owner and remove the first one because
+          * that will cause NameAcquired and NameLost messages to
+          * be sent.
+          */
+
+         if (!bus_service_add_owner (service, connection,
+                                                                 flags,
+                                                                 transaction, error))
+               goto out;
+
+         if (primary_owner->do_not_queue)
+               {
+                 if (!bus_service_remove_owner (service, old_owner_conn,
+                                                                                transaction, error))
+                       goto out;
+               }
+         else
+               {
+                 if (!bus_service_swap_owner (service, old_owner_conn,
+                                                                          transaction, error))
+                       goto out;
+               }
+
+
+         _dbus_assert (connection == bus_service_get_primary_owner (service)->conn);
+         *result = DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
+       }
+
+  activation = bus_context_get_activation (registry->context);
+  retval = bus_activation_send_pending_auto_activation_messages (activation,
+                                                                service,
+                                                                transaction,
+                                                                error);
+
+ out:
+  return retval;
+}
+
+dbus_bool_t
+bus_registry_acquire_kdbus_service (BusRegistry      *registry,
+                              DBusConnection   *connection,
+                              DBusMessage *message,
+                              dbus_uint32_t    *result,
+                              BusTransaction   *transaction,
+                              DBusError        *error)
+{
+  dbus_bool_t retval;
+  BusService *service;
+  BusActivation  *activation;
+
+  DBusString service_name_real;
+  const DBusString *service_name = &service_name_real;
+  char* name;
+  dbus_uint32_t flags;
+  __u64 sender_id;
+
+  if (!dbus_message_get_args (message, error,
+                              DBUS_TYPE_STRING, &name,
+                              DBUS_TYPE_UINT32, &flags,
+                              DBUS_TYPE_INVALID))
+    return FALSE;
+
+  retval = FALSE;
+
+  _dbus_string_init_const (&service_name_real, name);
+
+  if (!_dbus_validate_bus_name (service_name, 0,
+                                _dbus_string_get_length (service_name)))
     {
-      DBusList *link;
-      BusOwner *temp_owner;
-    /* Since we can't be queued if we are already in the queue
-       remove us */
+      dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
+                      "Requested bus name \"%s\" is not valid",
+                      _dbus_string_get_const_data (service_name));
 
-      link = _bus_service_find_owner_link (service, connection);
-      if (link != NULL)
-        {
-          _dbus_list_unlink (&service->owners, link);
-          temp_owner = (BusOwner *)link->data;
-          bus_owner_unref (temp_owner); 
-          _dbus_list_free_link (link);
-        }
-      
-      *result = DBUS_REQUEST_NAME_REPLY_EXISTS;
+      _dbus_verbose ("Attempt to acquire invalid service name\n");
+
+      goto out;
     }
-  else if (!(flags & DBUS_NAME_FLAG_DO_NOT_QUEUE) &&
-           (!(flags & DBUS_NAME_FLAG_REPLACE_EXISTING) ||
-           !(bus_service_get_allow_replacement (service))))
+
+  if (_dbus_string_get_byte (service_name, 0) == ':')
     {
-      /* Queue the connection */
-      if (!bus_service_add_owner (service, connection, 
-                                  flags,
-                                  transaction, error))
-        goto out;
-      
-      *result = DBUS_REQUEST_NAME_REPLY_IN_QUEUE;
+      /* Not allowed; only base services can start with ':' */
+      dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
+                      "Cannot acquire a service starting with ':' such as \"%s\"",
+                      _dbus_string_get_const_data (service_name));
+
+      _dbus_verbose ("Attempt to acquire invalid base service name \"%s\"",
+                     _dbus_string_get_const_data (service_name));
+
+      goto out;
     }
-  else
+
+  if (_dbus_string_equal_c_str (service_name, DBUS_SERVICE_DBUS))
     {
-      /* Replace the current owner */
+      dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
+                      "Connection \"%s\" is not allowed to own the service \"%s\"because "
+                      "it is reserved for D-Bus' use only",
+                      bus_connection_is_active (connection) ?
+                      bus_connection_get_name (connection) :
+                      "(inactive)",
+                      DBUS_SERVICE_DBUS);
+      goto out;
+    }
 
-      /* We enqueue the new owner and remove the first one because
-       * that will cause NameAcquired and NameLost messages to
-       * be sent.
-       */
-      
-      if (!bus_service_add_owner (service, connection,
-                                  flags,
-                                  transaction, error))
-        goto out;
+       service = bus_registry_lookup (registry, service_name);
+       if (service == NULL)
+       {
+               service = bus_registry_ensure (registry, service_name, connection, flags,
+                                                                        transaction, error);  //todo need correction because it will send NameOwnerChangedSignal
+               if (service == NULL)
+                 goto out;
+
+               if(!kdbus_register_policy(service_name, connection))
+               {
+                       dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
+                                                 "Connection is not allowed to own the service \"%s\" due to security policies in the configuration file",
+                                                 _dbus_string_get_const_data (service_name));
+                       goto out;
+               }
+       }
 
-      if (primary_owner->do_not_queue)
-        {
-          if (!bus_service_remove_owner (service, old_owner_conn,
-                                         transaction, error))
-            goto out;
-        }
-      else
-        {
-          if (!bus_service_swap_owner (service, old_owner_conn,
-                                       transaction, error))
-            goto out;
-        }
-        
-    
-      _dbus_assert (connection == bus_service_get_primary_owner (service)->conn);
-      *result = DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
-    }
+       sender_id = sender_name_to_id(dbus_message_get_sender(message), error);
+       if(dbus_error_is_set(error))
+               return FALSE;
+
+       *result = kdbus_request_name(connection, service_name, flags, sender_id);
+       if(*result == -EPERM)
+       {
+               dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
+                                         "Connection is not allowed to own the service \"%s\" due to security policies in the configuration file",
+                                         _dbus_string_get_const_data (service_name));
+               goto out;
+       }
+       else if(*result < 0)
+       {
+               dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", name);
+               goto out;
+       }
 
   activation = bus_context_get_activation (registry->context);
   retval = bus_activation_send_pending_auto_activation_messages (activation,
@@ -646,25 +761,38 @@ bus_registry_release_service (BusRegistry      *registry,
       goto out;
     }
 
-  service = bus_registry_lookup (registry, service_name);
+  if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))  //todo kdbus incl
+  {
+       __u64 sender_id;
 
-  if (service == NULL)
-    {
-      *result = DBUS_RELEASE_NAME_REPLY_NON_EXISTENT;
-    }
-  else if (!bus_service_has_owner (service, connection))
-    {
-      *result = DBUS_RELEASE_NAME_REPLY_NOT_OWNER;
-    }
-  else
-    {
-      if (!bus_service_remove_owner (service, connection,
-                                     transaction, error))
-        goto out;
+       sender_id = sender_name_to_id(dbus_message_get_sender((DBusMessage*)registry), error);
+       if(dbus_error_is_set(error))
+               return FALSE;
 
-      _dbus_assert (!bus_service_has_owner (service, connection));
-      *result = DBUS_RELEASE_NAME_REPLY_RELEASED;
-    }
+       *result = kdbus_release_name(connection, service_name, sender_id);
+  }
+  else
+  {
+         service = bus_registry_lookup (registry, service_name);
+
+         if (service == NULL)
+               {
+                 *result = DBUS_RELEASE_NAME_REPLY_NON_EXISTENT;
+               }
+         else if (!bus_service_has_owner (service, connection))
+               {
+                 *result = DBUS_RELEASE_NAME_REPLY_NOT_OWNER;
+               }
+         else
+               {
+                 if (!bus_service_remove_owner (service, connection,
+                                                                                transaction, error))
+                       goto out;
+
+                 _dbus_assert (!bus_service_has_owner (service, connection));
+                 *result = DBUS_RELEASE_NAME_REPLY_RELEASED;
+               }
+  }
 
   retval = TRUE;
 
index 056dd9f..732f6c5 100644 (file)
@@ -57,6 +57,12 @@ dbus_bool_t  bus_registry_acquire_service (BusRegistry                 *registry
                                            dbus_uint32_t               *result,
                                            BusTransaction              *transaction,
                                            DBusError                   *error);
+dbus_bool_t bus_registry_acquire_kdbus_service (BusRegistry      *registry,
+                                                 DBusConnection   *connection,
+                                                 DBusMessage *message,
+                                                 dbus_uint32_t    *result,
+                                                 BusTransaction   *transaction,
+                                                 DBusError        *error);
 dbus_bool_t  bus_registry_release_service (BusRegistry                 *registry,
                                            DBusConnection              *connection,
                                            const DBusString            *service_name,
index 629b898..4ee22bd 100644 (file)
@@ -108,6 +108,8 @@ DBUS_SHARED_arch_sources =                  \
        dbus-transport-unix.h                   \
        dbus-transport-kdbus.c                  \
        dbus-transport-kdbus.h                  \
+       kdbus-common.c                  \
+       kdbus-common.h                  \
        dbus-userdb.c                           \
        dbus-userdb.h                           \
        sd-daemon.c                             \
index 851dc90..77b0a36 100644 (file)
@@ -3238,7 +3238,7 @@ dbus_connection_send_preallocated (DBusConnection       *connection,
     }
 
 #endif
-
+  _dbus_verbose ("Reply sender: %s, destination: %s\n", dbus_message_get_sender(message), dbus_message_get_destination(message));  //todo kdbus temp addon
   _dbus_connection_send_preallocated_and_unlock (connection,
                                                 preallocated,
                                                 message, client_serial);
index ba08e12..cf19608 100644 (file)
 
 #include "dbus-transport.h"
 #include "dbus-transport-kdbus.h"
-#include <dbus/dbus-transport-protected.h>
+//#include <dbus/dbus-transport-protected.h>
+#include "dbus-transport-protected.h"
 #include "dbus-connection-internal.h"
 #include "kdbus.h"
 #include "dbus-watch.h"
 #include "dbus-errors.h"
 #include "dbus-bus.h"
+#include "kdbus-common.h"
 #include <linux/types.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <sys/stat.h>
 #include <openssl/md5.h>
 
-#define KDBUS_ALIGN8(l) (((l) + 7) & ~7)
-#define KDBUS_ITEM_SIZE(s) KDBUS_ALIGN8((s) + KDBUS_PART_HEADER_SIZE)
+//todo documentation need to be corrected
 
-#define KDBUS_PART_NEXT(part) \
-       (typeof(part))(((uint8_t *)part) + KDBUS_ALIGN8((part)->size))
 #define KDBUS_PART_FOREACH(part, head, first)                          \
        for (part = (head)->first;                                      \
             (uint8_t *)(part) < (uint8_t *)(head) + (head)->size;      \
@@ -172,6 +171,7 @@ static int reply_1_data(DBusMessage *message, int data_type, void* pData, DBusCo
     return -1;
 }
 
+/*todo uncomment if needed
 static int reply_ack(DBusMessage *message, DBusConnection* connection)
 {
        DBusMessage *reply;
@@ -182,7 +182,7 @@ static int reply_ack(DBusMessage *message, DBusConnection* connection)
     if(add_message_to_received(reply, connection))
        return 0;
     return -1;
-}
+}*/
 
 /**
  * Retrieves file descriptor to memory pool from kdbus module.
@@ -432,15 +432,6 @@ out:
     return ret_size;
 }
 
-struct nameInfo
-{
-       __u64 uniqueId;
-       __u64 userId;
-       __u64 processId;
-       __u32 sec_label_len;
-       char *sec_label;
-};
-
 /**
  * Performs kdbus query of id of the given name
  *
@@ -449,7 +440,7 @@ struct nameInfo
  * @param ownerID place to store id of the name
  * @return 0 on success, -errno if failed
  */
-static int kdbus_NameQuery(char* name, int fd, struct nameInfo* pInfo)
+int kdbus_NameQuery(const char* name, DBusTransport* transport, struct nameInfo* pInfo)
 {
        struct kdbus_cmd_name_info *msg;
        struct kdbus_item *item;
@@ -484,11 +475,13 @@ static int kdbus_NameQuery(char* name, int fd, struct nameInfo* pInfo)
        strcpy(item->str, name);
 
        again:
-       ret = ioctl(fd, KDBUS_CMD_NAME_QUERY, msg);
+       ret = ioctl(((DBusTransportSocket*)transport)->fd, KDBUS_CMD_NAME_QUERY, msg);
        if (ret < 0)
        {
                if(errno == EINTR)
                        goto again;
+               if(errno == EAGAIN)
+                               goto again;
                else if(ret == -ENOBUFS)
                {
                        msg = realloc(msg, msg->size);  //prepare memory
@@ -503,7 +496,7 @@ static int kdbus_NameQuery(char* name, int fd, struct nameInfo* pInfo)
                pInfo->uniqueId = msg->id;
                pInfo->userId = msg->creds.uid;
                pInfo->processId = msg->creds.pid;
-_dbus_verbose ("I'm alive 1\n");
+
                item = msg->items;
                while((uint8_t *)(item) < (uint8_t *)(msg) + msg->size)
                {
@@ -511,11 +504,13 @@ _dbus_verbose ("I'm alive 1\n");
                        {
                                pInfo->sec_label_len = item->size - KDBUS_PART_HEADER_SIZE - 1;
                                if(pInfo->sec_label_len != 0)
+                               {
                                        pInfo->sec_label = malloc(pInfo->sec_label_len);
-                               if(pInfo->sec_label == NULL)
-                                       ret = -1;
-                               else
-                                       memcpy(pInfo->sec_label, item->data, pInfo->sec_label_len);
+                                       if(pInfo->sec_label == NULL)
+                                               ret = -1;
+                                       else
+                                               memcpy(pInfo->sec_label, item->data, pInfo->sec_label_len);
+                               }
                                        
                                break;
                        }
@@ -527,104 +522,6 @@ _dbus_verbose ("I'm alive 1\n");
        return ret;
 }
 
-static struct kdbus_policy *make_policy_name(const char *name)
-{
-       struct kdbus_policy *p;
-       __u64 size;
-
-       size = offsetof(struct kdbus_policy, name) + strlen(name) + 1;
-       p = malloc(size);
-       if (!p)
-               return NULL;
-       memset(p, 0, size);
-       p->size = size;
-       p->type = KDBUS_POLICY_NAME;
-       strcpy(p->name, name);
-
-       return p;
-}
-
-static struct kdbus_policy *make_policy_access(__u64 type, __u64 bits, __u64 id)
-{
-       struct kdbus_policy *p;
-       __u64 size = sizeof(*p);
-
-       p = malloc(size);
-       if (!p)
-               return NULL;
-
-       memset(p, 0, size);
-       p->size = size;
-       p->type = KDBUS_POLICY_ACCESS;
-       p->access.type = type;
-       p->access.bits = bits;
-       p->access.id = id;
-
-       return p;
-}
-
-static void append_policy(struct kdbus_cmd_policy *cmd_policy, struct kdbus_policy *policy, __u64 max_size)
-{
-       struct kdbus_policy *dst = (struct kdbus_policy *) ((char *) cmd_policy + cmd_policy->size);
-
-       if (cmd_policy->size + policy->size > max_size)
-               return;
-
-       memcpy(dst, policy, policy->size);
-       cmd_policy->size += KDBUS_ALIGN8(policy->size);
-       free(policy);
-}
-
-/**
- * Registers kdbus policy for given connection.
- *
- * Policy sets rights of the name (unique or well known) on the bus. Without policy it is
- * not possible to send or receive messages. It must be set separately for unique id and
- * well known name of the connection. It is set after registering on the bus, but before
- * requesting for name. The policy is valid for the given name, not for the connection.
- *
- * Name of the policy equals name on the bus.
- *
- * @param name name of the policy = name of the connection
- * @param connection the connection
- * @param error place to store errors
- *
- * @returns #TRUE on success
- */
-static dbus_bool_t bus_register_policy_kdbus(const char* name, int fd)
-{
-       struct kdbus_cmd_policy *cmd_policy;
-       struct kdbus_policy *policy;
-       int size = 0xffff;
-
-       cmd_policy = alloca(size);
-       memset(cmd_policy, 0, size);
-
-       policy = (struct kdbus_policy *) cmd_policy->policies;
-       cmd_policy->size = offsetof(struct kdbus_cmd_policy, policies);
-
-       policy = make_policy_name(name);
-       append_policy(cmd_policy, policy, size);
-
-       policy = make_policy_access(KDBUS_POLICY_ACCESS_USER, KDBUS_POLICY_OWN, getuid());
-       append_policy(cmd_policy, policy, size);
-
-       policy = make_policy_access(KDBUS_POLICY_ACCESS_WORLD, KDBUS_POLICY_RECV, 0);
-       append_policy(cmd_policy, policy, size);
-
-       policy = make_policy_access(KDBUS_POLICY_ACCESS_WORLD, KDBUS_POLICY_SEND, 0);
-       append_policy(cmd_policy, policy, size);
-
-       if (ioctl(fd, KDBUS_CMD_EP_POLICY_SET, cmd_policy) < 0)
-       {
-               _dbus_verbose ("Error setting policy: %m, %d", errno);
-               return FALSE;
-       }
-
-       _dbus_verbose("Policy %s set correctly\n", name);
-       return TRUE;
-}
-
 /**
  * Kdbus part of dbus_bus_register.
  * Shouldn't be used separately because it needs to be surrounded
@@ -671,65 +568,6 @@ static dbus_bool_t bus_register_kdbus(char* name, DBusTransportSocket* transport
 }
 
 /**
- * kdbus version of dbus_bus_request_name.
- *
- * Asks the bus to assign the given name to this connection.
- *
- * Use same flags as original dbus version with one exception below.
- * Result flag #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER is currently
- * never returned by kdbus, instead DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
- * is returned by kdbus.
- *
- * @param connection the connection
- * @param name the name to request
- * @param flags flags
- * @param error location to store the error
- * @returns a result code, -1 if error is set
- */
-static int bus_request_name_kdbus(int fd, const char *name, const uint64_t flags)
-{
-       struct kdbus_cmd_name *cmd_name;
-
-       uint64_t size = sizeof(*cmd_name) + strlen(name) + 1;
-       uint64_t flags_kdbus = 0;
-
-       cmd_name = alloca(size);
-
-       memset(cmd_name, 0, size);
-       strcpy(cmd_name->name, name);
-       cmd_name->size = size;
-
-       if(flags & DBUS_NAME_FLAG_ALLOW_REPLACEMENT)
-               flags_kdbus |= KDBUS_NAME_ALLOW_REPLACEMENT;
-       if(!(flags & DBUS_NAME_FLAG_DO_NOT_QUEUE))
-               flags_kdbus |= KDBUS_NAME_QUEUE;
-       if(flags & DBUS_NAME_FLAG_REPLACE_EXISTING)
-               flags_kdbus |= KDBUS_NAME_REPLACE_EXISTING;
-
-       cmd_name->conn_flags = flags_kdbus;
-
-       _dbus_verbose("Request name - flags sent: 0x%llx       !!!!!!!!!\n", cmd_name->conn_flags);
-
-       if (ioctl(fd, KDBUS_CMD_NAME_ACQUIRE, cmd_name))
-       {
-               _dbus_verbose ("error acquiring name '%s': %m, %d", name, errno);
-               if(errno == EEXIST)
-                       return DBUS_REQUEST_NAME_REPLY_EXISTS;
-               return -1;
-       }
-
-       _dbus_verbose("Request name - received flag: 0x%llx       !!!!!!!!!\n", cmd_name->conn_flags);
-
-       if(cmd_name->conn_flags & KDBUS_NAME_IN_QUEUE)
-               return DBUS_REQUEST_NAME_REPLY_IN_QUEUE;
-       else
-               return DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
-       /*todo now 1 code is never returned -  DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER
-        * because kdbus never returns it now
-        */
-}
-
-/**
  * Seeks key in rule string, and duplicates value of the key into pValue.
  * If value is "org.freedesktop.DBus" it is indicated by returning -1, because it
  * needs to be handled in different manner.
@@ -788,7 +626,7 @@ static int parse_match_key(const char *rule, const char* key, char** pValue)
  * @param rule textual form of match rule
  * @param error location to store any errors - may be NULL
  */
-static dbus_bool_t dbus_bus_add_match_kdbus (DBusTransportSocket* transportS, const char *rule)
+dbus_bool_t add_match_kdbus (DBusTransport* transport, __u64 id, const char *rule)
 {
        struct kdbus_cmd_match* pCmd_match;
        struct kdbus_item *pItem;
@@ -799,6 +637,7 @@ static dbus_bool_t dbus_bus_add_match_kdbus (DBusTransportSocket* transportS, co
        char* pName = NULL;
        char* pInterface = NULL;
        dbus_bool_t ret_value = FALSE;
+       DBusTransportSocket* transportS = (DBusTransportSocket*)transport;
 
        /*parsing rule and calculating size of command*/
        size = sizeof(struct kdbus_cmd_match);
@@ -855,9 +694,9 @@ static dbus_bool_t dbus_bus_add_match_kdbus (DBusTransportSocket* transportS, co
        if(pCmd_match == NULL)
                goto out;
 
-       pCmd_match->id = 0;
+       pCmd_match->id = id;
+       pCmd_match->cookie = id;
        pCmd_match->size = size;
-       pCmd_match->cookie = strtoull(dbus_bus_get_unique_name(transportS->base.connection), NULL , 10);
 
        pItem = pCmd_match->items;
        if(kernel_item == ~0)  //all signals from kernel
@@ -907,7 +746,7 @@ static dbus_bool_t dbus_bus_add_match_kdbus (DBusTransportSocket* transportS, co
                _dbus_verbose("Failed adding match bus rule %s,\nerror: %d, %m\n", rule, errno);
        else
        {
-               _dbus_verbose("Added match bus rule %s\n", rule);
+               _dbus_verbose("Added match bus rule %s for id:%llu\n", rule, (unsigned long long)id);
                ret_value = TRUE;
        }
 
@@ -928,17 +767,18 @@ out:
  * @param connection connection to the message bus
  * @param error location to store any errors - may be NULL
  */
-static dbus_bool_t dbus_bus_remove_match_kdbus (DBusTransportSocket* transportS)
+dbus_bool_t remove_match_kdbus (DBusTransport* transport, __u64 id)
 {
        struct kdbus_cmd_match __attribute__ ((__aligned__(8))) cmd;
+       DBusTransportSocket* transportS = (DBusTransportSocket*) transport;
 
-       cmd.cookie = strtoull(dbus_bus_get_unique_name(transportS->base.connection), NULL , 10);
-       cmd.id = cmd.cookie;
+       cmd.cookie = id;
+       cmd.id = id;
        cmd.size = sizeof(struct kdbus_cmd_match);
 
-       if(ioctl(transportS->fd, KDBUS_CMD_MATCH_ADD, &cmd))
+       if(ioctl(transportS->fd, KDBUS_CMD_MATCH_REMOVE, &cmd))
        {
-               _dbus_verbose("Failed removing match rule; error: %d, %m\n", errno);
+               _dbus_verbose("Failed removing match rule for id: %llu; error: %d, %m\n", (unsigned long long)id, errno);
                return FALSE;
        }
        else
@@ -969,8 +809,10 @@ static dbus_bool_t dbus_bus_remove_match_kdbus (DBusTransportSocket* transportS)
  */
 static int emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *message)
 {
+#ifdef DBUS_SERVICES_IN_LIB
        int inter_ret;
        struct nameInfo info;
+#endif
        int ret_value = -1;
 
        if(!strcmp(dbus_message_get_member(message), "Hello"))
@@ -983,7 +825,7 @@ static int emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *mess
                        return -1;
                if(!bus_register_kdbus(name, (DBusTransportSocket*)transport))
                        goto outH1;
-               if(!bus_register_policy_kdbus(name, ((DBusTransportSocket*)transport)->fd))
+               if(!register_kdbus_policy(name, ((DBusTransportSocket*)transport)->fd))
                        goto outH1;
 
                sender = malloc (strlen(name) + 4);
@@ -1000,6 +842,7 @@ static int emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *mess
        outH1:
                free(name);
        }
+#ifdef DBUS_SERVICES_IN_LIB
        else if(!strcmp(dbus_message_get_member(message), "RequestName"))
        {
                char* name;
@@ -1008,11 +851,19 @@ static int emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *mess
 
                if(!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_UINT32, &flags, DBUS_TYPE_INVALID))
                        return -1;
-               if(!bus_register_policy_kdbus(name, ((DBusTransportSocket*)transport)->fd))
+
+               if(!register_kdbus_policy(name, ((DBusTransportSocket*)transport)->fd))
                        return -1;
 
-               result = bus_request_name_kdbus(((DBusTransportSocket*)transport)->fd, name, flags);
-               return reply_1_data(message, DBUS_TYPE_UINT32, &result, transport->connection);
+               result = request_kdbus_name(((DBusTransportSocket*)transport)->fd, name, flags, 0);
+               if(result == -EPERM)
+                       return reply_with_error(DBUS_ERROR_ACCESS_DENIED,
+                                         "Connection is not allowed to own the service \"%s\" due to security policies in the configuration file",
+                                         name, message, transport->connection);
+               else if(result < 0)
+                       return reply_with_error(DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", name, message, transport->connection);
+               else
+                       return reply_1_data(message, DBUS_TYPE_UINT32, &result, transport->connection);
        }
        else if(!strcmp(dbus_message_get_member(message), "AddMatch"))
        {
@@ -1021,14 +872,14 @@ static int emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *mess
                if(!dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &rule, DBUS_TYPE_INVALID))
                        return -1;
 
-               if(!dbus_bus_add_match_kdbus((DBusTransportSocket*)transport, rule))
+               if(!add_match_kdbus(transport, 0, rule))
                        return -1;
 
                return reply_ack(message,transport->connection);
        }
        else if(!strcmp(dbus_message_get_member(message), "RemoveMatch"))
        {
-               if(!dbus_bus_remove_match_kdbus((DBusTransportSocket*)transport))
+               if(!remove_match_kdbus(transport, 0))
                        return -1;
                return reply_ack(message, transport->connection);
        }
@@ -1037,7 +888,7 @@ static int emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *mess
                char* name = NULL;
 
                dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
-               inter_ret = kdbus_NameQuery(name, ((DBusTransportSocket*)transport)->fd, &info);
+               inter_ret = kdbus_NameQuery(name, transport, &info);
                if(inter_ret == 0) //unique id of the name
                {
                        char unique_name[(unsigned int)(snprintf(name, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
@@ -1061,7 +912,7 @@ static int emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *mess
                dbus_bool_t result;
 
                dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
-               inter_ret = kdbus_NameQuery(name, ((DBusTransportSocket*)transport)->fd, &info);
+               inter_ret = kdbus_NameQuery(name, transport, &info);
                if((inter_ret == 0) || (inter_ret == -ENOENT))
                {
                        result = (inter_ret == 0) ? TRUE : FALSE;
@@ -1078,7 +929,7 @@ static int emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *mess
                char* name = NULL;
 
                dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
-               inter_ret = kdbus_NameQuery(name, ((DBusTransportSocket*)transport)->fd, &info);
+               inter_ret = kdbus_NameQuery(name, transport, &info);
                if(inter_ret == 0) //name found
                {
                        _dbus_verbose("User id:%llu\n", (unsigned long long) info.userId);
@@ -1097,7 +948,7 @@ static int emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *mess
                char* name = NULL;
 
                dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
-               inter_ret = kdbus_NameQuery(name, ((DBusTransportSocket*)transport)->fd, &info);
+               inter_ret = kdbus_NameQuery(name, transport, &info);
                if(inter_ret == 0) //name found
                        ret_value = reply_1_data(message, DBUS_TYPE_UINT32, &info.processId, transport->connection);
                else if(inter_ret == -ENOENT)  //name has no owner
@@ -1215,19 +1066,21 @@ out:
        outgid:
                return ret_value;
        }
-       else if(!strcmp(dbus_message_get_member(message), "GetAdtAuditSessionData"))
+#endif
+       else if(!strcmp(dbus_message_get_member(message), "GetAdtAuditSessionData"))  //todo to be implemented - now can not be simply passed to daemon
        {
                char* name = NULL;
 
                dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
                return reply_with_error(DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN, "Could not determine audit session data for '%s'", name, message, transport->connection);
        }
+#ifdef DBUS_SERVICES_IN_LIB
        else if(!strcmp(dbus_message_get_member(message), "GetConnectionSELinuxSecurityContext"))
        {
                char* name = NULL;
 
                dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID);
-               inter_ret = kdbus_NameQuery(name, ((DBusTransportSocket*)transport)->fd, &info);
+               inter_ret = kdbus_NameQuery(name, transport, &info);
                if(inter_ret == -ENOENT)  //name has no owner
                        return reply_with_error(DBUS_ERROR_NAME_HAS_NO_OWNER, "Could not get security context of name '%s': no such name", name, message, transport->connection);
                else if(inter_ret < 0)
@@ -1247,29 +1100,15 @@ out:
                        }
                }
        }
+#endif
        else
                return 1;  //send to daemon
 //             return reply_with_error(DBUS_ERROR_UNKNOWN_METHOD, NULL, (char*)dbus_message_get_member(message), message, transport->connection);
-/*     else if(!strcmp(dbus_message_get_member(message), "ListActivatableNames"))  //todo
-       {
-
-       }
-       else if(!strcmp(dbus_message_get_member(message), "StartServiceByName"))
-       {
-
-       }
-       else if(!strcmp(dbus_message_get_member(message), "UpdateActivationEnvironment"))
-       {
-
-       }
-       else if(!strcmp(dbus_message_get_member(message), "ReloadConfig"))
-       {
-
-       }
-       */
 
+#ifdef DBUS_SERVICES_IN_LIB
        if(info.sec_label)
                free(info.sec_label);
+#endif
        return ret_value;
 }
 
index a6b5c38..098ceaa 100644 (file)
@@ -1,13 +1,26 @@
 
-#include "dbus-transport.h"
+#ifndef DBUS_TRANSPORT_KDBUS_H_
+#define DBUS_TRANSPORT_KDBUS_H_
+
+//#include "dbus-transport.h"
 #include "dbus-transport-protected.h"
-#include "dbus-address.h"
-#include "dbus-errors.h"
+//#include "dbus-address.h"
+//#include "dbus-errors.h"
 #include "dbus-types.h"
+#include <linux/types.h>
+
+struct nameInfo
+{
+       __u64 uniqueId;
+       __u64 userId;
+       __u64 processId;
+       __u32 sec_label_len;
+       char *sec_label;
+};
 
 DBusTransportOpenResult _dbus_transport_open_kdbus(DBusAddressEntry *entry, DBusTransport **transport_p, DBusError *error);
-//dbus_bool_t bus_register_kdbus(char* name, DBusConnection *connection, DBusError *error);
-//dbus_bool_t bus_register_policy_kdbus(const char* name, DBusConnection *connection, DBusError *error);
-//int bus_request_name_kdbus(DBusConnection *connection, const char *name, const uint64_t flags, DBusError *error);
-//void dbus_bus_add_match_kdbus (DBusConnection *connection, const char *rule, DBusError *error);
-//void dbus_bus_remove_match_kdbus (DBusConnection *connection, DBusError *error);
+dbus_bool_t add_match_kdbus (DBusTransport* transport, __u64 id, const char *rule);
+dbus_bool_t remove_match_kdbus (DBusTransport* transport, __u64 id);
+int kdbus_NameQuery(const char* name, DBusTransport* transport, struct nameInfo* pInfo);
+
+#endif
diff --git a/dbus/kdbus-common.c b/dbus/kdbus-common.c
new file mode 100644 (file)
index 0000000..0c96749
--- /dev/null
@@ -0,0 +1,277 @@
+/*
+ * kdbus_common.c
+ *
+ *  Created on: Sep 13, 2013
+ *      Author: r.pajak
+ *
+ *  Kdbus internal util functions used by daemon and libdbus
+ */
+
+#include "kdbus.h"
+#include "kdbus-common.h"
+#include <string.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <dbus/dbus-internals.h>
+#include <dbus/dbus-shared.h>
+
+static struct kdbus_policy *make_policy_name(const char *name)
+{
+       struct kdbus_policy *p;
+       __u64 size;
+
+       size = offsetof(struct kdbus_policy, name) + strlen(name) + 1;
+       p = malloc(size);
+       if (!p)
+               return NULL;
+       memset(p, 0, size);
+       p->size = size;
+       p->type = KDBUS_POLICY_NAME;
+       strcpy(p->name, name);
+
+       return p;
+}
+
+static struct kdbus_policy *make_policy_access(__u64 type, __u64 bits, __u64 id)
+{
+       struct kdbus_policy *p;
+       __u64 size = sizeof(*p);
+
+       p = malloc(size);
+       if (!p)
+               return NULL;
+
+       memset(p, 0, size);
+       p->size = size;
+       p->type = KDBUS_POLICY_ACCESS;
+       p->access.type = type;
+       p->access.bits = bits;
+       p->access.id = id;
+
+       return p;
+}
+
+static void append_policy(struct kdbus_cmd_policy *cmd_policy, struct kdbus_policy *policy, __u64 max_size)
+{
+       struct kdbus_policy *dst = (struct kdbus_policy *) ((char *) cmd_policy + cmd_policy->size);
+
+       if (cmd_policy->size + policy->size > max_size)
+               return;
+
+       memcpy(dst, policy, policy->size);
+       cmd_policy->size += KDBUS_ALIGN8(policy->size);
+       free(policy);
+}
+
+/**
+ * Registers kdbus policy for given connection.
+ *
+ * Policy sets rights of the name (unique or well known) on the bus. Without policy it is
+ * not possible to send or receive messages. It must be set separately for unique id and
+ * well known name of the connection. It is set after registering on the bus, but before
+ * requesting for name. The policy is valid for the given name, not for the connection.
+ *
+ * Name of the policy equals name on the bus.
+ *
+ * @param name name of the policy = name of the connection
+ * @param connection the connection
+ * @param error place to store errors
+ *
+ * @returns #TRUE on success
+ */
+dbus_bool_t register_kdbus_policy(const char* name, int fd)
+{
+       struct kdbus_cmd_policy *cmd_policy;
+       struct kdbus_policy *policy;
+       int size = 0xffff;
+
+       cmd_policy = alloca(size);
+       memset(cmd_policy, 0, size);
+
+       policy = (struct kdbus_policy *) cmd_policy->policies;
+       cmd_policy->size = offsetof(struct kdbus_cmd_policy, policies);
+
+       policy = make_policy_name(name);
+       append_policy(cmd_policy, policy, size);
+
+       policy = make_policy_access(KDBUS_POLICY_ACCESS_USER, KDBUS_POLICY_OWN, getuid());
+       append_policy(cmd_policy, policy, size);
+
+       policy = make_policy_access(KDBUS_POLICY_ACCESS_WORLD, KDBUS_POLICY_RECV, 0);
+       append_policy(cmd_policy, policy, size);
+
+       policy = make_policy_access(KDBUS_POLICY_ACCESS_WORLD, KDBUS_POLICY_SEND, 0);
+       append_policy(cmd_policy, policy, size);
+
+       if (ioctl(fd, KDBUS_CMD_EP_POLICY_SET, cmd_policy) < 0)
+       {
+               _dbus_verbose ("Error setting policy: %m, %d", errno);
+               return FALSE;
+       }
+
+       _dbus_verbose("Policy %s set correctly\n", name);
+       return TRUE;
+}
+
+dbus_bool_t list_kdbus_names(int fd, char ***listp, int *array_len)
+{
+       struct kdbus_cmd_names* pCmd;
+       __u64 cmd_size;
+       dbus_bool_t ret_val = FALSE;
+       char** list;
+       int list_len = 0;
+       int i = 0;
+       int j;
+
+       cmd_size = sizeof(struct kdbus_cmd_names) + KDBUS_ITEM_SIZE(1);
+       pCmd = malloc(cmd_size);
+       if(pCmd == NULL)
+               goto out;
+       pCmd->size = cmd_size;
+
+again:
+       cmd_size = 0;
+       if(ioctl(fd, KDBUS_CMD_NAME_LIST, pCmd))
+       {
+               if(errno == EINTR)
+                       goto again;
+               if(errno == ENOBUFS)                    //buffer to small to put all names into it
+                       cmd_size = pCmd->size;          //here kernel tells how much memory it needs
+               else
+               {
+                       _dbus_verbose("kdbus error asking for name list: err %d (%m)\n",errno);
+                       goto out;
+               }
+       }
+       if(cmd_size)  //kernel needs more memory
+       {
+               pCmd = realloc(pCmd, cmd_size);  //prepare memory
+               if(pCmd == NULL)
+                       return FALSE;
+               goto again;                                             //and try again
+       }
+       else
+       {
+               struct kdbus_cmd_name* pCmd_name;
+
+               for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
+                       list_len++;
+
+               list = malloc(sizeof(char*) * (list_len + 1));
+               if(list == NULL)
+                       goto out;
+
+               for (pCmd_name = pCmd->names; (uint8_t *)(pCmd_name) < (uint8_t *)(pCmd) + pCmd->size; pCmd_name = KDBUS_PART_NEXT(pCmd_name))
+               {
+                       list[i] = strdup(pCmd_name->name);
+                       if(list[i] == NULL)
+                       {
+                               for(j=0; j<i; j++)
+                                       free(list[j]);
+                               free(list);
+                               goto out;
+                       }
+                       _dbus_verbose ("Name %d: %s\n", i, list[i]);
+                       ++i;
+               }
+               list[i] = NULL;
+       }
+
+       *array_len = list_len;
+       *listp = list;
+       ret_val = TRUE;
+
+out:
+       if(pCmd)
+               free(pCmd);
+       return ret_val;
+}
+
+/**
+ * kdbus version of dbus_bus_request_name.
+ *
+ * Asks the bus to assign the given name to this connection.
+ *
+ * Use same flags as original dbus version with one exception below.
+ * Result flag #DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER is currently
+ * never returned by kdbus, instead DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
+ * is returned by kdbus.
+ *
+ * @param connection the connection
+ * @param name the name to request
+ * @param flags flags
+ * @param error location to store the error
+ * @returns a result code, -1 if error is set
+ */
+int request_kdbus_name(int fd, const char *name, const __u64 flags, __u64 id)
+{
+       struct kdbus_cmd_name *cmd_name;
+
+       __u64 size = sizeof(*cmd_name) + strlen(name) + 1;
+       __u64 flags_kdbus = 0;
+
+       cmd_name = alloca(size);
+
+//     memset(cmd_name, 0, size);
+       strcpy(cmd_name->name, name);
+       cmd_name->size = size;
+
+       if(flags & DBUS_NAME_FLAG_ALLOW_REPLACEMENT)
+               flags_kdbus |= KDBUS_NAME_ALLOW_REPLACEMENT;
+       if(!(flags & DBUS_NAME_FLAG_DO_NOT_QUEUE))
+               flags_kdbus |= KDBUS_NAME_QUEUE;
+       if(flags & DBUS_NAME_FLAG_REPLACE_EXISTING)
+               flags_kdbus |= KDBUS_NAME_REPLACE_EXISTING;
+
+       cmd_name->flags = flags_kdbus;
+       cmd_name->id = id;
+//     cmd_name->conn_flags = 0;
+
+       _dbus_verbose("Request name - flags sent: 0x%llx       !!!!!!!!!\n", cmd_name->flags);
+
+       if (ioctl(fd, KDBUS_CMD_NAME_ACQUIRE, cmd_name))
+       {
+               _dbus_verbose ("error acquiring name '%s': %m, %d\n", name, errno);
+               if(errno == EEXIST)
+                       return DBUS_REQUEST_NAME_REPLY_EXISTS;
+               return -errno;
+       }
+
+       _dbus_verbose("Request name - received flag: 0x%llx       !!!!!!!!!\n", cmd_name->flags);
+
+       if(cmd_name->flags & KDBUS_NAME_IN_QUEUE)
+               return DBUS_REQUEST_NAME_REPLY_IN_QUEUE;
+       else
+               return DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER;
+       /*todo now 1 code is never returned -  DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER
+        * because kdbus never returns it now
+        */
+}
+
+int release_kdbus_name(int fd, const char *name, __u64 id)
+{
+       struct kdbus_cmd_name *cmd_name;
+
+       __u64 size = sizeof(*cmd_name) + strlen(name) + 1;
+
+       cmd_name = alloca(size);
+       cmd_name->id = id;
+       strcpy(cmd_name->name, name);
+       cmd_name->size = size;
+
+       if (ioctl(fd, KDBUS_CMD_NAME_RELEASE, cmd_name))
+       {
+               if(errno == ESRCH)
+                       return DBUS_RELEASE_NAME_REPLY_NON_EXISTENT;
+               else if (errno == EPERM)
+                       return DBUS_RELEASE_NAME_REPLY_NOT_OWNER;
+               _dbus_verbose ("error releasing name '%s' for id:%llu. Error: %m, %d\n", name, (unsigned long long)id, errno);
+               return -errno;
+       }
+
+       _dbus_verbose("Name '%s' released\n", name);
+
+       return DBUS_RELEASE_NAME_REPLY_RELEASED;
+}
diff --git a/dbus/kdbus-common.h b/dbus/kdbus-common.h
new file mode 100644 (file)
index 0000000..93cd726
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * kdbus_common.h
+ *
+ *  Created on: Sep 13, 2013
+ *      Author: r.pajak
+ *
+ *  Kdbus internal util functions used by daemon and libdbus
+ */
+
+#ifndef KDBUS_COMMON_H_
+#define KDBUS_COMMON_H_
+
+#include <dbus/dbus-types.h>
+#include <dbus/dbus-transport.h>
+
+#define KDBUS_ALIGN8(l) (((l) + 7) & ~7)
+#define KDBUS_PART_NEXT(part) \
+       (typeof(part))(((uint8_t *)part) + KDBUS_ALIGN8((part)->size))
+#define KDBUS_ITEM_SIZE(s) KDBUS_ALIGN8((s) + KDBUS_PART_HEADER_SIZE)
+
+/*struct kdbus_policy *make_policy_name(const char *name);
+struct kdbus_policy *make_policy_access(__u64 type, __u64 bits, __u64 id);
+void append_policy(struct kdbus_cmd_policy *cmd_policy, struct kdbus_policy *policy, __u64 max_size);*/
+dbus_bool_t register_kdbus_policy(const char* name, int fd);
+dbus_bool_t list_kdbus_names(int fd, char ***listp, int *array_len);
+int request_kdbus_name(int fd, const char *name, const __u64 flags, __u64 id);
+int release_kdbus_name(int fd, const char *name, __u64 id);
+
+#endif /* KDBUS_COMMON_H_ */