[daemon-fix][daemon-opt] Fixed owner id when setting kdbus policy
[platform/upstream/dbus.git] / bus / kdbus-d.c
index 789730e..4f37bc2 100644 (file)
@@ -1,10 +1,26 @@
-/*
- * kdbus-d.c
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+/* kdbus-d.c  kdbus related daemon functions
+ *
+ * Copyright (C) 2013  Samsung Electronics
+ *
+ * Licensed under the Academic Free License version 2.1
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version and under the terms of the GNU
+ * Lesser General Public License as published by the
+ * Free Software Foundation; either version 2.1 of the License, or (at
+ * your option) any later version.
  *
- *  Created on: Sep 4, 2013
- *      Author: r.pajak
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  *
- *  kdbus add-on to dbus daemon
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
 
@@ -27,6 +43,9 @@
 #include <unistd.h>
 #include <errno.h>
 
+/*
+ * Converts string with unique name into __u64 id number. If the name is not unique, sets error.
+ */
 __u64 sender_name_to_id(const char* name, DBusError* error)
 {
        __u64 sender_id = 0;
@@ -39,6 +58,9 @@ __u64 sender_name_to_id(const char* name, DBusError* error)
        return sender_id;
 }
 
+/*
+ * Creates kdbus bus of given type.
+ */
 char* make_kdbus_bus(DBusBusType type, DBusError *error)
 {
     struct {
@@ -51,8 +73,6 @@ char* make_kdbus_bus(DBusBusType type, DBusError *error)
     int fdc, ret;
     char *bus;
 
-    /*TODO Distinguish session and system bus make*/
-
     _dbus_verbose("Opening /dev/kdbus/control\n");
     fdc = open("/dev/kdbus/control", O_RDWR|O_CLOEXEC);
     if (fdc < 0)
@@ -68,8 +88,11 @@ char* make_kdbus_bus(DBusBusType type, DBusError *error)
 
     if(type == DBUS_BUS_SYSTEM)
         snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus-%s", getuid(), "system");
+    else if(type == DBUS_BUS_SESSION)
+        snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus", getuid());
     else
         snprintf(bus_make.name, sizeof(bus_make.name), "%u-kdbus-%u", getuid(), getpid());
+
     bus_make.n_type = KDBUS_MAKE_NAME;
     bus_make.n_size = KDBUS_PART_HEADER_SIZE + strlen(bus_make.name) + 1;
     bus_make.head.size = sizeof(struct kdbus_cmd_bus_make) + bus_make.n_size;
@@ -93,15 +116,22 @@ char* make_kdbus_bus(DBusBusType type, DBusError *error)
        return bus;
 }
 
+/*
+ * Minimal server init needed by context to go further.
+ */
 DBusServer* empty_server_init(char* address)
 {
        return dbus_server_init_mini(address);
 }
 
+/*
+ * Connects daemon to bus created by him and adds matches for "system" broadcasts.
+ * Do not requests org.freedesktop.DBus name, because it's to early
+ * (some structures of BusContext are not ready yet).
+ */
 DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *error)
 {
        DBusConnection* connection;
-       DBusString daemon_name;
 
        dbus_bus_set_bus_connection_address(type, address);
 
@@ -109,32 +139,25 @@ DBusConnection* daemon_as_client(DBusBusType type, char* address, DBusError *err
        if(connection == NULL)
                return NULL;
 
-       _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS);
-       if(!kdbus_register_policy (&daemon_name, connection))
-               goto failed;
-
-       if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
-               goto failed;
-
-    if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='IdRemoved'"))
+       if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='IdRemoved'"))
     {
           dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
-          return FALSE;
+          goto failed;
     }
     if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='NameChanged'"))
     {
           dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
-          return FALSE;
+          goto failed;
     }
     if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "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;
+          goto failed;
     }
     if(!add_match_kdbus (dbus_connection_get_transport(connection), 1, "member='NameAcquired'"))
     {
           dbus_set_error (error, _dbus_error_from_errno (errno), "Could not add match for id:1, %s", _dbus_strerror_from_errno ());
-          return FALSE;
+          goto failed;
     }
 
        if(dbus_error_is_set(error))
@@ -150,13 +173,40 @@ failed:
        return connection;
 }
 
-dbus_bool_t kdbus_register_policy (const DBusString *service_name, DBusConnection* connection)
+/*
+ * Asks bus for org.freedesktop.DBus well-known name.
+ */
+dbus_bool_t register_daemon_name(DBusConnection* connection)
 {
-       int fd;
+    DBusString daemon_name;
+    dbus_bool_t retval = FALSE;
+    BusTransaction *transaction;
 
-       _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
+    _dbus_string_init_const(&daemon_name, DBUS_SERVICE_DBUS);
+    if(!register_kdbus_policy(DBUS_SERVICE_DBUS, dbus_connection_get_transport(connection), geteuid()))
+      return FALSE;
+
+    if(kdbus_request_name(connection, &daemon_name, 0, 0) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
+       return FALSE;
+
+    transaction = bus_transaction_new (bus_connection_get_context(connection));
+    if (transaction == NULL)
+    {
+        kdbus_release_name(connection, &daemon_name, 0);
+        goto out;
+    }
+
+    if(!bus_registry_ensure (bus_connection_get_registry (connection), &daemon_name, connection, 0, transaction, NULL))
+    {
+        kdbus_release_name(connection, &daemon_name, 0);
+        goto out;
+    }
+
+    retval = TRUE;
 
-       return register_kdbus_policy(_dbus_string_get_const_data(service_name), fd);
+out:
+       bus_transaction_cancel_and_free(transaction);
+    return retval;
 }
 
 dbus_uint32_t kdbus_request_name(DBusConnection* connection, const DBusString *service_name, dbus_uint32_t flags, __u64 sender_id)
@@ -177,15 +227,89 @@ dbus_uint32_t kdbus_release_name(DBusConnection* connection, const DBusString *s
        return release_kdbus_name(fd, _dbus_string_get_const_data(service_name), sender_id);
 }
 
+/*
+ * Asks kdbus for well-known names registered on the bus
+ */
 dbus_bool_t kdbus_list_services (DBusConnection* connection, char ***listp, int *array_len)
 {
        int fd;
+       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;
 
        _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
 
-       return list_kdbus_names(fd, listp, array_len);
+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;
 }
 
+/*
+ *  Register match rule in kdbus on behalf of sender of the message
+ */
 dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* message, const char* text, DBusError* error)
 {
        __u64 sender_id;
@@ -204,6 +328,9 @@ dbus_bool_t kdbus_add_match_rule (DBusConnection* connection, DBusMessage* messa
        return TRUE;
 }
 
+/*
+ *  Removes match rule in kdbus on behalf of sender of the message
+ */
 dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message, DBusError* error)
 {
        __u64 sender_id;
@@ -221,40 +348,63 @@ dbus_bool_t kdbus_remove_match (DBusConnection* connection, DBusMessage* message
        return TRUE;
 }
 
-dbus_bool_t kdbus_get_connection_unix_user(DBusConnection* connection, DBusMessage* message, unsigned long* uid, DBusError* error)
+int kdbus_get_name_owner(DBusConnection* connection, const char* name, char* owner)
 {
-       char* name = NULL;
-       struct nameInfo info;
-       int inter_ret;
-       dbus_bool_t ret = FALSE;
+  int ret;
+  struct nameInfo info;
+
+  ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
+  if(ret == 0) //unique id of the name
+  {
+    sprintf(owner, ":1.%llu", (unsigned long long int)info.uniqueId);
+    _dbus_verbose("Unique name discovered:%s\n", owner);
+  }
+  else if(ret != -ENOENT)
+    _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
+
+  return ret;
+}
 
-       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);
-       }
+/*
+ *  Asks kdbus for uid of the owner of the name given in the message
+ */
+dbus_bool_t kdbus_get_unix_user(DBusConnection* connection, const char* name, unsigned long* uid, DBusError* error)
+{
+  struct nameInfo info;
+  int inter_ret;
+  dbus_bool_t ret = FALSE;
+
+  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_verbose ("Name %s has no owner.\n", name);
+      dbus_set_error (error, DBUS_ERROR_FAILED, "Could not get UID of name '%s': no such name", name);
+    }
 
-       return ret;
+  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)
+/*
+ *  Asks kdbus for pid of the owner of the name given in the message
+ */
+dbus_bool_t kdbus_get_connection_unix_process_id(DBusConnection* connection, const char* name, 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
        {
@@ -273,6 +423,9 @@ dbus_bool_t kdbus_get_connection_unix_process_id(DBusConnection* connection, DBu
        return ret;
 }
 
+/*
+ *  Asks kdbus for selinux_security_context of the owner of the name given in the message
+ */
 dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* connection, DBusMessage* message, DBusMessage* reply, DBusError* error)
 {
        char* name = NULL;
@@ -302,12 +455,62 @@ dbus_bool_t kdbus_get_connection_unix_selinux_security_context(DBusConnection* c
        return ret;
 }
 
-DBusConnection* create_phantom_connection(DBusConnection* connection, const char* unique_name, DBusError* error)
+/**
+ * Gets the UNIX user ID of the connection from kdbus, if known. Returns #TRUE if
+ * the uid is filled in.  Always returns #FALSE on non-UNIX platforms
+ * for now., though in theory someone could hook Windows to NIS or
+ * something.  Always returns #FALSE prior to authenticating the
+ * connection.
+ *
+ * The UID of is only read by bus daemon from kdbus. You can not
+ * call this function from client side of the connection.
+ *
+ * You can ask the bus to tell you the UID of another connection though
+ * if you like; this is done with dbus_bus_get_unix_user().
+ *
+ * @param connection the connection
+ * @param uid return location for the user ID
+ * @returns #TRUE if uid is filled in with a valid user ID
+ */
+dbus_bool_t
+dbus_connection_get_unix_user (DBusConnection *connection,
+                               unsigned long  *uid)
+{
+  _dbus_return_val_if_fail (connection != NULL, FALSE);
+  _dbus_return_val_if_fail (uid != NULL, FALSE);
+
+  return kdbus_get_unix_user(connection, bus_connection_get_name(connection), uid, NULL);
+}
+
+/**
+ * Gets the process ID of the connection if any.
+ * Returns #TRUE if the pid is filled in.
+ *
+ * @param connection the connection
+ * @param pid return location for the process ID
+ * @returns #TRUE if uid is filled in with a valid process ID
+ */
+dbus_bool_t
+dbus_connection_get_unix_process_id (DBusConnection *connection,
+             unsigned long  *pid)
+{
+  _dbus_return_val_if_fail (connection != NULL, FALSE);
+  _dbus_return_val_if_fail (pid != NULL, FALSE);
+
+  return kdbus_get_connection_unix_process_id(connection, bus_connection_get_name(connection), pid, NULL);
+}
+
+/*
+ * Create connection structure for given name. It is needed to control starters - activatable services
+ * and for ListQueued method (as long as kdbus is not supporting it). This connections don't have it's own
+ * fd so it is set up on the basis of daemon's transport. Functionality of such connection is limited.
+ */
+DBusConnection* create_phantom_connection(DBusConnection* connection, const char* name, DBusError* error)
 {
     DBusConnection *phantom_connection;
-    DBusString name;
+    DBusString Sname;
 
-    _dbus_string_init_const(&name, unique_name);
+    _dbus_string_init_const(&Sname, name);
 
     phantom_connection = _dbus_connection_new_for_used_transport (dbus_connection_get_transport(connection));
     if(phantom_connection == NULL)
@@ -316,10 +519,10 @@ DBusConnection* create_phantom_connection(DBusConnection* connection, const char
     {
         dbus_connection_unref_phantom(phantom_connection);
         phantom_connection = NULL;
-        dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", unique_name);
+        dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", name);
         goto out;
     }
-    if(!bus_connection_complete(phantom_connection, &name, error))
+    if(!bus_connection_complete(phantom_connection, &Sname, error))
     {
         bus_connection_disconnected(phantom_connection);
         phantom_connection = NULL;
@@ -332,6 +535,9 @@ out:
     return phantom_connection;
 }
 
+/*
+ * Registers activatable services as kdbus starters.
+ */
 dbus_bool_t register_kdbus_starters(DBusConnection* connection)
 {
     int i,j, len;
@@ -340,6 +546,8 @@ dbus_bool_t register_kdbus_starters(DBusConnection* connection)
     int fd;
     BusTransaction *transaction;
     DBusString name;
+    DBusTransport* transport;
+    unsigned long int euid;
 
     transaction = bus_transaction_new (bus_connection_get_context(connection));
     if (transaction == NULL)
@@ -348,13 +556,18 @@ dbus_bool_t register_kdbus_starters(DBusConnection* connection)
     if (!bus_activation_list_services (bus_connection_get_activation (connection), &services, &len))
         return FALSE;
 
-    _dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd);
+    transport = dbus_connection_get_transport(connection);
+    euid = geteuid();
+
+    if(!_dbus_transport_get_socket_fd (transport, &fd))
+      return FALSE;
+
     _dbus_string_init(&name);
 
     for(i=0; i<len; i++)
     {
-        if(!register_kdbus_policy(services[i], fd))
-            goto out;
+        if(!register_kdbus_policy(services[i], transport, euid))
+          goto out;
 
         if (request_kdbus_name(fd, services[i], (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER) , 0) < 0)
             goto out;
@@ -377,10 +590,14 @@ out:
     }
     dbus_free_string_array (services);
     _dbus_string_free(&name);
-    bus_transaction_free(transaction);
+    bus_transaction_cancel_and_free(transaction);
     return retval;
 }
 
+/*
+ * Updates kdbus starters (activatable services) after configuration was reloaded.
+ * It releases all previous starters and registers all new.
+ */
 dbus_bool_t update_kdbus_starters(DBusConnection* connection)
 {
     dbus_bool_t retval = FALSE;
@@ -399,6 +616,7 @@ dbus_bool_t update_kdbus_starters(DBusConnection* connection)
 
     services_old = bus_connection_get_services_owned(connection);
     link = _dbus_list_get_first_link(services_old);
+    link = _dbus_list_get_next_link (services_old, link); //skip org.freedesktop.DBus which is not starter
 
     while (link != NULL)
     {
@@ -429,21 +647,14 @@ dbus_bool_t update_kdbus_starters(DBusConnection* connection)
     retval = TRUE;
 
 out:
-    bus_transaction_free(transaction);
+       bus_transaction_cancel_and_free(transaction);
     return retval;
 }
 
 /*
-static dbus_bool_t remove_conn_if_name_match (DBusConnection *connection, void *data)
-{
-    if(!strcmp(bus_connection_get_name(connection), (char*)data))
-    {
-        bus_connection_disconnected(connection);
-        return FALSE; //this is to break foreach function
-    }
-    return TRUE;
-}*/
-
+ * Analyzes system broadcasts about id and name changes.
+ * Basing on this it sends NameAcquired and NameLost signals and clear phantom connections.
+ */
 void handleNameOwnerChanged(DBusMessage *msg, BusTransaction *transaction, DBusConnection *connection)
 {
     const char *name, *old, *new;