+ {
+ _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;
+}
+
+/*
+ * 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;
+ 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) || (inter_ret == -ENXIO)) //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;
+}
+
+/**
+ * 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);
+
+ if(bus_context_is_kdbus(bus_connection_get_context (connection)))
+ return kdbus_get_unix_user(connection, bus_connection_get_name(connection), uid, NULL);
+
+ return dbus_connection_get_unix_user_dbus(connection, uid);
+}
+
+/**
+ * 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);
+
+ if(bus_context_is_kdbus(bus_connection_get_context (connection)))
+ return kdbus_get_connection_unix_process_id(connection, bus_connection_get_name(connection), pid, NULL);
+
+ return dbus_connection_get_unix_process_id_dbus(connection, pid);
+}
+
+/*
+ * 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 Sname;
+
+ _dbus_string_init_const(&Sname, name);
+
+ phantom_connection = _dbus_connection_new_for_used_transport (dbus_connection_get_transport(connection));
+ if(phantom_connection == NULL)
+ return FALSE;
+ if(!bus_connections_setup_connection(bus_connection_get_connections(connection), phantom_connection))
+ {
+ dbus_connection_unref_phantom(phantom_connection);
+ phantom_connection = NULL;
+ dbus_set_error (error, DBUS_ERROR_FAILED , "Name \"%s\" could not be acquired", name);
+ goto out;
+ }
+ if(!bus_connection_complete(phantom_connection, &Sname, error))
+ {
+ bus_connection_disconnected(phantom_connection);
+ phantom_connection = NULL;
+ goto out;
+ }
+
+ _dbus_verbose ("Created phantom connection for %s\n", bus_connection_get_name(phantom_connection));
+
+out:
+ return phantom_connection;
+}
+
+/*
+ * Registers activatable services as kdbus starters.
+ */
+dbus_bool_t register_kdbus_starters(DBusConnection* connection)
+{
+ int i,j, len;
+ char **services;
+ dbus_bool_t retval = FALSE;
+ int fd;
+ BusTransaction *transaction;
+ DBusString name;
+ DBusTransport* transport;
+
+ transaction = bus_transaction_new (bus_connection_get_context(connection));
+ if (transaction == NULL)
+ return FALSE;
+
+ if (!bus_activation_list_services (bus_connection_get_activation (connection), &services, &len))
+ return FALSE;
+
+ transport = dbus_connection_get_transport(connection);
+
+ if(!_dbus_transport_get_socket_fd (transport, &fd))
+ return FALSE;
+
+ _dbus_string_init(&name);
+
+ for(i=0; i<len; i++)
+ {
+#ifdef POLICY_TO_KDBUS
+ if(!register_kdbus_policy(services[i], transport, geteuid()))
+ goto out;
+#endif
+
+ if (request_kdbus_name(fd, services[i], (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER_NAME) , 0) < 0)
+ goto out;
+
+ if(!_dbus_string_append(&name, services[i]))
+ goto out;
+ if(!bus_registry_ensure (bus_connection_get_registry (connection), &name, connection,
+ (DBUS_NAME_FLAG_ALLOW_REPLACEMENT | KDBUS_NAME_STARTER_NAME), transaction, NULL))
+ goto out;
+ if(!_dbus_string_set_length(&name, 0))
+ goto out;
+ }
+ retval = TRUE;
+
+out:
+ if(retval == FALSE)
+ {
+ for(j=0; j<i; j++)
+ release_kdbus_name(fd, services[j], 0);
+ }
+ dbus_free_string_array (services);
+ _dbus_string_free(&name);
+ if(retval)
+ bus_transaction_execute_and_free(transaction);
+ else
+ 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;
+ DBusList **services_old;
+ DBusList *link;
+ BusService *service = NULL;
+ BusTransaction *transaction;
+ int fd;
+
+ transaction = bus_transaction_new (bus_connection_get_context(connection));
+ if (transaction == NULL)
+ return FALSE;
+
+ if(!_dbus_transport_get_socket_fd(dbus_connection_get_transport(connection), &fd))
+ goto out;
+
+ 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)
+ {
+ int ret;
+
+ service = (BusService*) link->data;
+ if(service == NULL)
+ goto out;
+
+ ret = release_kdbus_name(fd, bus_service_get_name(service), 0);
+
+ if (ret == DBUS_RELEASE_NAME_REPLY_RELEASED)
+ {
+ if(!bus_service_remove_owner(service, connection, transaction, NULL))
+ _dbus_verbose ("Unable to remove\n");
+ }
+ else if(ret < 0)
+ goto out;
+
+ link = _dbus_list_get_next_link (services_old, link);
+ }
+
+ if(!register_kdbus_starters(connection))
+ {
+ _dbus_verbose ("Registering kdbus starters for dbus activatable names failed!\n");
+ goto out;
+ }
+ retval = TRUE;
+
+out:
+ if(retval)
+ bus_transaction_execute_and_free(transaction);
+ else
+ bus_transaction_cancel_and_free(transaction);
+
+ return retval;
+}
+
+/*
+ * 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;
+
+ if(!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID))
+ {
+ _dbus_verbose ("Couldn't get args of NameOwnerChanged signal.\n");//, error.message);
+ return;
+ }
+
+ _dbus_verbose ("Got NameOwnerChanged signal:\nName: %s\nOld: %s\nNew: %s\n", name, old, new);
+
+ if(!strncmp(name, ":1.", 3))/*if it starts from :1. it is unique name - this might be IdRemoved info*/
+ {
+ if(!strcmp(name, old)) //yes it is - someone has disconnected
+ {
+ DBusConnection* conn;
+
+ conn = bus_connections_find_conn_by_name(bus_connection_get_connections(connection), name);
+ if(conn)
+ bus_connection_disconnected(conn);
+ }
+ }
+ else //it is well-known name
+ {
+ if((*old != 0) && (strcmp(old, bus_connection_get_name(connection))))
+ {
+ DBusMessage *message;
+
+ if(bus_connections_find_conn_by_name(bus_connection_get_connections(connection), old) == NULL)
+ goto next;
+
+ _dbus_verbose ("Owner '%s' lost name '%s'. Sending NameLost.\n", old, name);
+
+ message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameLost");
+ if (message == NULL)
+ goto next;
+
+ if (!dbus_message_set_destination (message, old) || !dbus_message_append_args (message,
+ DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_INVALID))
+ {
+ dbus_message_unref (message);
+ goto next;
+ }
+
+ bus_transaction_send_from_driver (transaction, connection, message);
+ dbus_message_unref (message);
+ }
+ next:
+ if((*new != 0) && (strcmp(new, bus_connection_get_name(connection))))
+ {
+ DBusMessage *message;
+
+ _dbus_verbose ("Owner '%s' acquired name '%s'. Sending NameAcquired.\n", new, name);
+
+ message = dbus_message_new_signal (DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, "NameAcquired");
+ if (message == NULL)
+ return;
+
+ if (!dbus_message_set_destination (message, new) || !dbus_message_append_args (message,
+ DBUS_TYPE_STRING, &name,
+ DBUS_TYPE_INVALID))
+ {
+ dbus_message_unref (message);
+ return;
+ }
+
+ bus_transaction_send_from_driver (transaction, connection, message);
+ dbus_message_unref (message);
+ }
+ }