diff options
Diffstat (limited to 'libgdm')
-rw-r--r-- | libgdm/gdm-client.c | 1424 | ||||
-rw-r--r-- | libgdm/gdm-client.h | 108 | ||||
-rw-r--r-- | libgdm/gdm-sessions.c | 435 | ||||
-rw-r--r-- | libgdm/gdm-sessions.h | 37 | ||||
-rw-r--r-- | libgdm/gdm-user-switching.c | 258 | ||||
-rw-r--r-- | libgdm/gdm-user-switching.h | 31 | ||||
-rw-r--r-- | libgdm/gdm.pc.in | 11 | ||||
-rw-r--r-- | libgdm/libgdm.map | 6 | ||||
-rw-r--r-- | libgdm/meson.build | 104 |
9 files changed, 2414 insertions, 0 deletions
diff --git a/libgdm/gdm-client.c b/libgdm/gdm-client.c new file mode 100644 index 0000000..fb58f1a --- /dev/null +++ b/libgdm/gdm-client.c @@ -0,0 +1,1424 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2012 Red Hat, Inc. + * Copyright (C) 2012 Giovanni Campagna <scampa.giovanni@gmail.com> + * + * 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. + * + * 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. + * + * 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. + * + */ + +#include "config.h" + +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <string.h> + +#include <glib.h> +#include <glib/gi18n.h> +#include <glib-object.h> + +#include "gdm-client.h" +#include "gdm-client-glue.h" +#include "gdm-manager-glue.h" + +#define SESSION_DBUS_PATH "/org/gnome/DisplayManager/Session" + +struct _GdmClient +{ + GObject parent; + + GdmUserVerifier *user_verifier; + GdmUserVerifier *user_verifier_for_reauth; + GHashTable *user_verifier_extensions; + + GdmGreeter *greeter; + GdmRemoteGreeter *remote_greeter; + GdmChooser *chooser; + + char **enabled_extensions; +}; + +static void gdm_client_finalize (GObject *object); + +G_DEFINE_TYPE (GdmClient, gdm_client, G_TYPE_OBJECT); + +static gpointer client_object = NULL; + +GQuark +gdm_client_error_quark (void) +{ + static GQuark error_quark = 0; + + if (error_quark == 0) + error_quark = g_quark_from_static_string ("gdm-client"); + + return error_quark; +} + +static GDBusConnection * +gdm_client_get_open_connection (GdmClient *client) +{ + GDBusProxy *proxy = NULL; + + if (client->user_verifier != NULL) { + proxy = G_DBUS_PROXY (client->user_verifier); + } else if (client->greeter != NULL) { + proxy = G_DBUS_PROXY (client->greeter); + } else if (client->remote_greeter != NULL) { + proxy = G_DBUS_PROXY (client->remote_greeter); + } else if (client->chooser != NULL) { + proxy = G_DBUS_PROXY (client->chooser); + } + + if (proxy != NULL) { + return g_dbus_proxy_get_connection (proxy); + } + + return NULL; +} + +static void +on_got_manager (GObject *object, + GAsyncResult *result, + gpointer user_data) +{ + g_autoptr(GTask) task = user_data; + g_autoptr(GdmClient) client = NULL; + g_autoptr(GdmManager) manager = NULL; + g_autoptr(GError) error = NULL; + + client = GDM_CLIENT (g_async_result_get_source_object (G_ASYNC_RESULT (task))); + manager = gdm_manager_proxy_new_finish (result, &error); + + if (error != NULL) { + g_task_return_error (task, g_steal_pointer (&error)); + } else { + g_task_return_pointer (task, + g_steal_pointer (&manager), + (GDestroyNotify) g_object_unref); + } +} + +static void +get_manager (GdmClient *client, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GTask *task; + + task = g_task_new (G_OBJECT (client), + cancellable, + callback, + user_data); + + gdm_manager_proxy_new_for_bus (G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + "org.gnome.DisplayManager", + "/org/gnome/DisplayManager/Manager", + cancellable, + on_got_manager, + task); +} + +typedef struct { + GTask *task; + GdmUserVerifier *user_verifier; +} UserVerifierData; + +static UserVerifierData * +user_verifier_data_new (GTask *task, GdmUserVerifier *user_verifier) +{ + UserVerifierData *data; + + data = g_slice_new (UserVerifierData); + data->task = g_object_ref (task); + data->user_verifier = g_object_ref (user_verifier); + + return data; +} + +static void +user_verifier_data_free (UserVerifierData *data) +{ + g_object_unref (data->task); + g_object_unref (data->user_verifier); + g_slice_free (UserVerifierData, data); +} + +static void +complete_user_verifier_proxy_operation (GdmClient *client, + UserVerifierData *data) +{ + g_task_return_pointer (data->task, + g_object_ref (data->user_verifier), + (GDestroyNotify) g_object_unref); + user_verifier_data_free (data); +} + +static void +maybe_complete_user_verifier_proxy_operation (GdmClient *client, + UserVerifierData *data) +{ + GHashTable *user_verifier_extensions; + GHashTableIter iter; + gpointer key, value; + + user_verifier_extensions = g_object_get_data (G_OBJECT (data->user_verifier), "gdm-client-user-verifier-extensions"); + if (user_verifier_extensions != NULL) { + g_hash_table_iter_init (&iter, user_verifier_extensions); + while (g_hash_table_iter_next (&iter, &key, &value)) { + if (value == NULL) + return; + } + } + + complete_user_verifier_proxy_operation (client, data); +} + +static void +on_user_verifier_choice_list_proxy_created (GObject *source, + GAsyncResult *result, + UserVerifierData *data) +{ + GHashTable *user_verifier_extensions; + g_autoptr(GdmClient) client = NULL; + GdmUserVerifierChoiceList *choice_list; + g_autoptr(GError) error = NULL; + + client = GDM_CLIENT (g_async_result_get_source_object (G_ASYNC_RESULT (data->task))); + + user_verifier_extensions = g_object_get_data (G_OBJECT (data->user_verifier), "gdm-client-user-verifier-extensions"); + choice_list = gdm_user_verifier_choice_list_proxy_new_finish (result, &error); + + if (choice_list == NULL) { + g_debug ("Couldn't create UserVerifier ChoiceList proxy: %s", error->message); + g_hash_table_remove (user_verifier_extensions, gdm_user_verifier_choice_list_interface_info ()->name); + } else { + g_hash_table_replace (user_verifier_extensions, gdm_user_verifier_choice_list_interface_info ()->name, choice_list); + } + + maybe_complete_user_verifier_proxy_operation (client, data); +} + +static void +on_user_verifier_extensions_enabled (GdmUserVerifier *user_verifier, + GAsyncResult *result, + UserVerifierData *data) +{ + g_autoptr(GdmClient) client = NULL; + GHashTable *user_verifier_extensions; + GCancellable *cancellable; + GDBusConnection *connection; + g_autoptr(GError) error = NULL; + size_t i; + + client = GDM_CLIENT (g_async_result_get_source_object (G_ASYNC_RESULT (data->task))); + cancellable = g_task_get_cancellable (data->task); + user_verifier_extensions = g_object_get_data (G_OBJECT (user_verifier), "gdm-client-user-verifier-extensions"); + + gdm_user_verifier_call_enable_extensions_finish (user_verifier, result, &error); + + if (error != NULL) { + g_debug ("Couldn't enable user verifier extensions: %s", + error->message); + complete_user_verifier_proxy_operation (client, data); + return; + } + + connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (user_verifier)); + + for (i = 0; client->enabled_extensions[i] != NULL; i++) { + g_debug ("Enabled extensions[%lu] = %s", i, client->enabled_extensions[i]); + g_hash_table_insert (user_verifier_extensions, client->enabled_extensions[i], NULL); + + if (strcmp (client->enabled_extensions[i], + gdm_user_verifier_choice_list_interface_info ()->name) == 0) { + g_hash_table_insert (user_verifier_extensions, client->enabled_extensions[i], NULL); + gdm_user_verifier_choice_list_proxy_new (connection, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + SESSION_DBUS_PATH, + cancellable, + (GAsyncReadyCallback) + on_user_verifier_choice_list_proxy_created, + data); + } else { + g_debug ("User verifier extension %s is unsupported", client->enabled_extensions[i]); + g_hash_table_remove (user_verifier_extensions, + client->enabled_extensions[i]); + } + } + + if (g_hash_table_size (user_verifier_extensions) == 0) { + g_debug ("No supported user verifier extensions"); + complete_user_verifier_proxy_operation (client, data); + } + +} + +static void +free_interface_skeleton (GDBusInterfaceSkeleton *interface) +{ + if (interface == NULL) + return; + + g_object_unref (interface); +} + +static void +on_user_verifier_proxy_created (GObject *source, + GAsyncResult *result, + gpointer user_data) +{ + g_autoptr(GdmClient) self = NULL; + GHashTable *user_verifier_extensions; + GCancellable *cancellable = NULL; + g_autoptr(GdmUserVerifier) user_verifier = NULL; + g_autoptr(GTask) task = user_data; + g_autoptr(GError) error = NULL; + + user_verifier = gdm_user_verifier_proxy_new_finish (result, &error); + if (user_verifier == NULL) { + g_task_return_error (task, g_steal_pointer (&error)); + return; + } + + g_debug ("UserVerifier %p created", user_verifier); + + self = GDM_CLIENT (g_async_result_get_source_object (G_ASYNC_RESULT (task))); + if (self->enabled_extensions == NULL) { + g_debug ("no enabled extensions"); + g_task_return_pointer (task, + g_steal_pointer (&user_verifier), + (GDestroyNotify) g_object_unref); + return; + } + + user_verifier_extensions = g_hash_table_new_full (g_str_hash, + g_str_equal, + NULL, + (GDestroyNotify) + free_interface_skeleton); + g_object_set_data_full (G_OBJECT (user_verifier), + "gdm-client-user-verifier-extensions", + user_verifier_extensions, + (GDestroyNotify) g_hash_table_unref); + cancellable = g_task_get_cancellable (task); + gdm_user_verifier_call_enable_extensions (user_verifier, + (const char * const *) + self->enabled_extensions, + cancellable, + (GAsyncReadyCallback) + on_user_verifier_extensions_enabled, + user_verifier_data_new (task, user_verifier)); +} + +static void +on_reauthentication_channel_connected (GObject *source_object, + GAsyncResult *result, + gpointer user_data) +{ + GCancellable *cancellable; + g_autoptr(GTask) task = user_data; + g_autoptr(GDBusConnection) connection = NULL; + g_autoptr(GError) error = NULL; + + connection = g_dbus_connection_new_for_address_finish (result, &error); + if (!connection) { + g_task_return_error (task, g_steal_pointer (&error)); + return; + } + + cancellable = g_task_get_cancellable (task); + gdm_user_verifier_proxy_new (connection, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + SESSION_DBUS_PATH, + cancellable, + on_user_verifier_proxy_created, + g_steal_pointer (&task)); +} + +static void +on_reauthentication_channel_opened (GdmManager *manager, + GAsyncResult *result, + gpointer user_data) +{ + GCancellable *cancellable; + g_autoptr(GTask) task = user_data; + g_autoptr(GError) error = NULL; + g_autofree char *address = NULL; + + if (!gdm_manager_call_open_reauthentication_channel_finish (manager, + &address, + result, + &error)) { + g_task_return_error (task, g_steal_pointer (&error)); + return; + } + + cancellable = g_task_get_cancellable (task); + g_dbus_connection_new_for_address (address, + G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT, + NULL, + cancellable, + on_reauthentication_channel_connected, + g_steal_pointer (&task)); +} + +static void +on_got_manager_for_reauthentication (GdmClient *client, + GAsyncResult *result, + gpointer user_data) +{ + GCancellable *cancellable; + const char *username; + g_autoptr(GTask) task = user_data; + g_autoptr(GdmManager) manager = NULL; + g_autoptr(GError) error = NULL; + + manager = g_task_propagate_pointer (G_TASK (result), &error); + if (manager == NULL) { + g_task_return_error (task, g_steal_pointer (&error)); + return; + } + + cancellable = g_task_get_cancellable (task); + username = g_object_get_data (G_OBJECT (task), "username"); + gdm_manager_call_open_reauthentication_channel (manager, + username, + cancellable, + (GAsyncReadyCallback) + on_reauthentication_channel_opened, + g_steal_pointer (&task)); +} + +static GDBusConnection * +gdm_client_get_connection_sync (GdmClient *client, + GCancellable *cancellable, + GError **error) +{ + g_autoptr(GdmManager) manager = NULL; + g_autofree char *address = NULL; + GDBusConnection *connection; + gboolean ret; + + g_return_val_if_fail (GDM_IS_CLIENT (client), NULL); + + connection = gdm_client_get_open_connection (client); + + if (connection != NULL) { + return g_object_ref (connection); + } + + manager = gdm_manager_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + "org.gnome.DisplayManager", + "/org/gnome/DisplayManager/Manager", + cancellable, + error); + + if (manager == NULL) { + return NULL; + } + + ret = gdm_manager_call_open_session_sync (manager, + &address, + cancellable, + error); + + if (!ret) { + return NULL; + } + + g_debug ("GdmClient: connecting to address: %s", address); + + connection = g_dbus_connection_new_for_address_sync (address, + G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT, + NULL, + cancellable, + error); + + return connection; +} + +static GDBusConnection * +gdm_client_get_connection_finish (GdmClient *client, + GAsyncResult *result, + GError **error) +{ + GDBusConnection *connection; + + g_return_val_if_fail (GDM_IS_CLIENT (client), NULL); + + connection = g_task_propagate_pointer (G_TASK (result), error); + if (connection == NULL) { + return NULL; + } + + return connection; +} + +static void +gdm_client_get_connection (GdmClient *client, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + g_autoptr(GTask) task = NULL; + g_autoptr(GError) error = NULL; + GDBusConnection *connection; + + g_return_if_fail (GDM_IS_CLIENT (client)); + + task = g_task_new (G_OBJECT (client), + cancellable, + callback, + user_data); + + connection = gdm_client_get_connection_sync (client, + cancellable, + &error); + if (connection != NULL) { + g_task_return_pointer (task, + g_object_ref (connection), + (GDestroyNotify) g_object_unref); + } else { + g_task_return_error (task, g_steal_pointer (&error)); + } +} + +/** + * gdm_client_open_reauthentication_channel_sync: + * @client: a #GdmClient + * @username: user to reauthenticate + * @cancellable: a #GCancellable + * @error: a #GError + * + * Gets a #GdmUserVerifier object that can be used to + * reauthenticate an already logged in user. Free with + * g_object_unref to close reauthentication channel. + * + * Returns: (transfer full): #GdmUserVerifier or %NULL if @username is not + * already logged in. + */ +GdmUserVerifier * +gdm_client_open_reauthentication_channel_sync (GdmClient *client, + const char *username, + GCancellable *cancellable, + GError **error) +{ + g_autoptr(GDBusConnection) connection = NULL; + g_autoptr(GdmManager) manager = NULL; + g_autofree char *address = NULL; + GdmUserVerifier *user_verifier = NULL; + gboolean ret; + + g_return_val_if_fail (GDM_IS_CLIENT (client), NULL); + + manager = gdm_manager_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + "org.gnome.DisplayManager", + "/org/gnome/DisplayManager/Manager", + cancellable, + error); + + if (manager == NULL) { + return NULL; + } + + ret = gdm_manager_call_open_reauthentication_channel_sync (manager, + username, + &address, + cancellable, + error); + + if (!ret) { + return NULL; + } + + g_debug ("GdmClient: connecting to address: %s", address); + + connection = g_dbus_connection_new_for_address_sync (address, + G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT, + NULL, + cancellable, + error); + + if (connection == NULL) { + return NULL; + } + + user_verifier = gdm_user_verifier_proxy_new_sync (connection, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + SESSION_DBUS_PATH, + cancellable, + error); + + g_set_weak_pointer (&client->user_verifier_for_reauth, user_verifier); + + return user_verifier; +} + +/** + * gdm_client_open_reauthentication_channel: + * @client: a #GdmClient + * @username: user to reauthenticate + * @callback: a #GAsyncReadyCallback to call when the request is satisfied + * @user_data: The data to pass to @callback + * @cancellable: a #GCancellable + * + * Gets a #GdmUserVerifier object that can be used to + * reauthenticate an already logged in user. + */ +void +gdm_client_open_reauthentication_channel (GdmClient *client, + const char *username, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + GTask *task; + + g_return_if_fail (GDM_IS_CLIENT (client)); + + task = g_task_new (G_OBJECT (client), + cancellable, + callback, + user_data); + + g_object_set_data_full (G_OBJECT (task), + "username", + g_strdup (username), + (GDestroyNotify) + g_free); + + get_manager (client, + cancellable, + (GAsyncReadyCallback) + on_got_manager_for_reauthentication, + task); +} + +/** + * gdm_client_open_reauthentication_channel_finish: + * @client: a #GdmClient + * @result: The #GAsyncResult from the callback + * @error: a #GError + * + * Finishes an operation started with + * gdm_client_open_reauthentication_channel(). + * + * Returns: (transfer full): a #GdmUserVerifier + */ +GdmUserVerifier * +gdm_client_open_reauthentication_channel_finish (GdmClient *client, + GAsyncResult *result, + GError **error) +{ + GdmUserVerifier *user_verifier; + + g_return_val_if_fail (GDM_IS_CLIENT (client), NULL); + + user_verifier = g_task_propagate_pointer (G_TASK (result), error); + + g_set_weak_pointer (&client->user_verifier_for_reauth, user_verifier); + + return user_verifier; +} + +/** + * gdm_client_get_user_verifier_sync: + * @client: a #GdmClient + * @cancellable: a #GCancellable + * @error: a #GError + * + * Gets a #GdmUserVerifier object that can be used to + * verify a user's local account. + * + * Returns: (transfer full): #GdmUserVerifier or %NULL if not connected + */ +GdmUserVerifier * +gdm_client_get_user_verifier_sync (GdmClient *client, + GCancellable *cancellable, + GError **error) +{ + g_autoptr(GDBusConnection) connection = NULL; + GdmUserVerifier *user_verifier; + + if (client->user_verifier != NULL) { + return g_object_ref (client->user_verifier); + } + + connection = gdm_client_get_connection_sync (client, cancellable, error); + + if (connection == NULL) { + return NULL; + } + + user_verifier = gdm_user_verifier_proxy_new_sync (connection, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + SESSION_DBUS_PATH, + cancellable, + error); + + g_set_weak_pointer (&client->user_verifier, user_verifier); + + if (client->user_verifier != NULL) { + if (client->enabled_extensions != NULL) { + GHashTable *user_verifier_extensions; + gboolean res; + + user_verifier_extensions = g_hash_table_new_full (g_str_hash, + g_str_equal, + NULL, + (GDestroyNotify) + free_interface_skeleton); + g_object_set_data_full (G_OBJECT (client->user_verifier), + "gdm-client-user-verifier-extensions", + user_verifier_extensions, + (GDestroyNotify) g_hash_table_unref); + + res = gdm_user_verifier_call_enable_extensions_sync (client->user_verifier, + (const char * const *) + client->enabled_extensions, + cancellable, + NULL); + + if (res) { + size_t i; + for (i = 0; client->enabled_extensions[i] != NULL; i++) { + if (strcmp (client->enabled_extensions[i], + gdm_user_verifier_choice_list_interface_info ()->name) == 0) { + GdmUserVerifierChoiceList *choice_list_interface; + choice_list_interface = gdm_user_verifier_choice_list_proxy_new_sync (connection, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + SESSION_DBUS_PATH, + cancellable, + NULL); + if (choice_list_interface != NULL) + g_hash_table_insert (user_verifier_extensions, client->enabled_extensions[i], choice_list_interface); + } + } + } + } + } + + return client->user_verifier; +} + +static void +on_connection_for_user_verifier (GdmClient *client, + GAsyncResult *result, + gpointer user_data) +{ + GCancellable *cancellable; + g_autoptr(GTask) task = user_data; + g_autoptr(GDBusConnection) connection = NULL; + g_autoptr(GError) error = NULL; + + connection = gdm_client_get_connection_finish (client, result, &error); + if (connection == NULL) { + g_task_return_error (task, g_steal_pointer (&error)); + return; + } + + cancellable = g_task_get_cancellable (task); + gdm_user_verifier_proxy_new (connection, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + SESSION_DBUS_PATH, + cancellable, + on_user_verifier_proxy_created, + g_steal_pointer (&task)); +} + +/** + * gdm_client_get_user_verifier: + * @client: a #GdmClient + * @callback: a #GAsyncReadyCallback to call when the request is satisfied + * @user_data: The data to pass to @callback + * @cancellable: a #GCancellable + * + * Gets a #GdmUserVerifier object that can be used to + * verify a user's local account. + */ +void +gdm_client_get_user_verifier (GdmClient *client, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + g_autoptr(GTask) task = NULL; + + g_return_if_fail (GDM_IS_CLIENT (client)); + + task = g_task_new (G_OBJECT (client), + cancellable, + callback, + user_data); + + if (client->user_verifier != NULL) { + g_task_return_pointer (task, + g_object_ref (client->user_verifier), + (GDestroyNotify) g_object_unref); + return; + } + + gdm_client_get_connection (client, + cancellable, + (GAsyncReadyCallback) + on_connection_for_user_verifier, + g_steal_pointer (&task)); +} + +/** + * gdm_client_get_user_verifier_finish: + * @client: a #GdmClient + * @result: The #GAsyncResult from the callback + * @error: a #GError + * + * Finishes an operation started with + * gdm_client_get_user_verifier(). + * + * Returns: (transfer full): a #GdmUserVerifier + */ +GdmUserVerifier * +gdm_client_get_user_verifier_finish (GdmClient *client, + GAsyncResult *result, + GError **error) +{ + GdmUserVerifier *user_verifier; + + g_return_val_if_fail (GDM_IS_CLIENT (client), NULL); + + if (client->user_verifier != NULL) + return g_object_ref (client->user_verifier); + + user_verifier = g_task_propagate_pointer (G_TASK (result), error); + if (user_verifier == NULL) + return NULL; + + g_set_weak_pointer (&client->user_verifier, user_verifier); + + return user_verifier; +} + +/** + * gdm_client_get_user_verifier_choice_list: + * @client: a #GdmClient + * + * Gets a #GdmUserVerifierChoiceList object that can be used to + * verify a user's local account. + * + * Returns: (transfer none): #GdmUserVerifierChoiceList or %NULL if user + * verifier isn't yet fetched, or daemon doesn't support choice lists + */ +GdmUserVerifierChoiceList * +gdm_client_get_user_verifier_choice_list (GdmClient *client) +{ + GHashTable *user_verifier_extensions = NULL; + + if (client->user_verifier_for_reauth != NULL) + user_verifier_extensions = g_object_get_data (G_OBJECT (client->user_verifier_for_reauth), "gdm-client-user-verifier-extensions"); + + if (user_verifier_extensions == NULL && client->user_verifier != NULL) + user_verifier_extensions = g_object_get_data (G_OBJECT (client->user_verifier), "gdm-client-user-verifier-extensions"); + + if (user_verifier_extensions == NULL) + return NULL; + + return g_hash_table_lookup (user_verifier_extensions, + gdm_user_verifier_choice_list_interface_info ()->name); +} + +static void +on_timed_login_details_got (GdmGreeter *greeter, + GAsyncResult *result) +{ + gdm_greeter_call_get_timed_login_details_finish (greeter, NULL, NULL, NULL, result, NULL); +} + +static void +query_for_timed_login_requested_signal (GdmGreeter *greeter) +{ + /* This just makes sure a timed-login-requested signal gets fired + * off if appropriate. + */ + gdm_greeter_call_get_timed_login_details (greeter, + NULL, + (GAsyncReadyCallback) + on_timed_login_details_got, + NULL); +} + +static void +on_greeter_proxy_created (GObject *source, + GAsyncResult *result, + gpointer user_data) +{ + g_autoptr(GTask) task = user_data; + g_autoptr(GError) error = NULL; + GdmGreeter *greeter; + + greeter = gdm_greeter_proxy_new_finish (result, &error); + if (greeter == NULL) { + g_task_return_error (task, g_steal_pointer (&error)); + return; + } + + g_task_return_pointer (task, + greeter, + (GDestroyNotify) g_object_unref); + + query_for_timed_login_requested_signal (greeter); +} + +static void +on_connection_for_greeter (GdmClient *client, + GAsyncResult *result, + gpointer user_data) +{ + GCancellable *cancellable; + g_autoptr(GTask) task = user_data; + g_autoptr(GDBusConnection) connection = NULL; + g_autoptr(GError) error = NULL; + + connection = gdm_client_get_connection_finish (client, result, &error); + + if (connection == NULL) { + g_task_return_error (task, g_steal_pointer (&error)); + return; + } + + cancellable = g_task_get_cancellable (task); + gdm_greeter_proxy_new (connection, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + SESSION_DBUS_PATH, + cancellable, + on_greeter_proxy_created, + g_steal_pointer (&task)); +} + +/** + * gdm_client_get_greeter: + * @client: a #GdmClient + * @callback: a #GAsyncReadyCallback to call when the request is satisfied + * @user_data: The data to pass to @callback + * @cancellable: a #GCancellable + * + * Gets a #GdmGreeter object that can be used to + * verify a user's local account. + */ +void +gdm_client_get_greeter (GdmClient *client, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + g_autoptr(GTask) task = NULL; + + g_return_if_fail (GDM_IS_CLIENT (client)); + + task = g_task_new (G_OBJECT (client), + cancellable, + callback, + user_data); + + if (client->greeter != NULL) { + g_task_return_pointer (task, + g_object_ref (client->greeter), + (GDestroyNotify) g_object_unref); + return; + } + + gdm_client_get_connection (client, + cancellable, + (GAsyncReadyCallback) + on_connection_for_greeter, + g_steal_pointer (&task)); +} + +/** + * gdm_client_get_greeter_finish: + * @client: a #GdmClient + * @result: The #GAsyncResult from the callback + * @error: a #GError + * + * Finishes an operation started with + * gdm_client_get_greeter(). + * + * Returns: (transfer full): a #GdmGreeter + */ +GdmGreeter * +gdm_client_get_greeter_finish (GdmClient *client, + GAsyncResult *result, + GError **error) +{ + GdmGreeter *greeter; + + g_return_val_if_fail (GDM_IS_CLIENT (client), NULL); + + if (client->greeter != NULL) + return g_object_ref (client->greeter); + + greeter = g_task_propagate_pointer (G_TASK (result), error); + if (greeter == NULL) + return NULL; + + g_set_weak_pointer (&client->greeter, greeter); + + return greeter; +} + +/** + * gdm_client_get_greeter_sync: + * @client: a #GdmClient + * @cancellable: a #GCancellable + * @error: a #GError + * + * Gets a #GdmGreeter object that can be used + * to do do various login screen related tasks, such + * as selecting a users session, and starting that + * session. + * + * Returns: (transfer full): #GdmGreeter or %NULL if caller is not a greeter + */ +GdmGreeter * +gdm_client_get_greeter_sync (GdmClient *client, + GCancellable *cancellable, + GError **error) +{ + g_autoptr(GDBusConnection) connection = NULL; + GdmGreeter *greeter; + + if (client->greeter != NULL) { + return g_object_ref (client->greeter); + } + + connection = gdm_client_get_connection_sync (client, cancellable, error); + + if (connection == NULL) { + return NULL; + } + + greeter = gdm_greeter_proxy_new_sync (connection, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + SESSION_DBUS_PATH, + cancellable, + error); + + g_set_weak_pointer (&client->greeter, greeter); + + if (client->greeter != NULL) { + query_for_timed_login_requested_signal (client->greeter); + } + + return client->greeter; +} + +static void +on_remote_greeter_proxy_created (GObject *object, + GAsyncResult *result, + gpointer user_data) +{ + g_autoptr(GTask) task = user_data; + g_autoptr(GError) error = NULL; + GdmRemoteGreeter *remote_greeter; + + remote_greeter = gdm_remote_greeter_proxy_new_finish (result, &error); + if (remote_greeter == NULL) { + g_task_return_error (task, g_steal_pointer (&error)); + return; + } + + g_task_return_pointer (task, + remote_greeter, + (GDestroyNotify) g_object_unref); +} + +static void +on_connection_for_remote_greeter (GdmClient *client, + GAsyncResult *result, + gpointer user_data) +{ + GCancellable *cancellable; + g_autoptr(GTask) task = user_data; + g_autoptr(GDBusConnection) connection = NULL; + g_autoptr(GError) error = NULL; + + connection = gdm_client_get_connection_finish (client, result, &error); + + if (connection == NULL) { + g_task_return_error (task, g_steal_pointer (&error)); + return; + } + + cancellable = g_task_get_cancellable (task); + gdm_remote_greeter_proxy_new (connection, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + SESSION_DBUS_PATH, + cancellable, + on_remote_greeter_proxy_created, + g_steal_pointer (&task)); +} + +/** + * gdm_client_get_remote_greeter: + * @client: a #GdmClient + * @callback: a #GAsyncReadyCallback to call when the request is satisfied + * @user_data: The data to pass to @callback + * @cancellable: a #GCancellable + * + * Gets a #GdmRemoteGreeter object that can be used to + * verify a user's local account. + */ +void +gdm_client_get_remote_greeter (GdmClient *client, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + g_autoptr (GTask) task = NULL; + + g_return_if_fail (GDM_IS_CLIENT (client)); + + task = g_task_new (G_OBJECT (client), + cancellable, + callback, + user_data); + + if (client->remote_greeter != NULL) { + g_task_return_pointer (task, + g_object_ref (client->remote_greeter), + (GDestroyNotify) g_object_unref); + return; + } + + gdm_client_get_connection (client, + cancellable, + (GAsyncReadyCallback) + on_connection_for_remote_greeter, + g_steal_pointer (&task)); +} + +/** + * gdm_client_get_remote_greeter_finish: + * @client: a #GdmClient + * @result: The #GAsyncResult from the callback + * @error: a #GError + * + * Finishes an operation started with + * gdm_client_get_remote_greeter(). + * + * Returns: (transfer full): a #GdmRemoteGreeter + */ +GdmRemoteGreeter * +gdm_client_get_remote_greeter_finish (GdmClient *client, + GAsyncResult *result, + GError **error) +{ + GdmRemoteGreeter *remote_greeter; + + g_return_val_if_fail (GDM_IS_CLIENT (client), NULL); + + if (client->remote_greeter != NULL) + return g_object_ref (client->remote_greeter); + + remote_greeter = g_task_propagate_pointer (G_TASK (result), error); + if (remote_greeter == NULL) + return NULL; + + g_set_weak_pointer (&client->remote_greeter, remote_greeter); + + return remote_greeter; +} + +/** + * gdm_client_get_remote_greeter_sync: + * @client: a #GdmClient + * @cancellable: a #GCancellable + * @error: a #GError + * + * Gets a #GdmRemoteGreeter object that can be used + * to do do various remote login screen related tasks, + * such as disconnecting. + * + * Returns: (transfer full): #GdmRemoteGreeter or %NULL if caller is not remote + */ +GdmRemoteGreeter * +gdm_client_get_remote_greeter_sync (GdmClient *client, + GCancellable *cancellable, + GError **error) +{ + g_autoptr(GDBusConnection) connection = NULL; + GdmRemoteGreeter *remote_greeter; + + if (client->remote_greeter != NULL) { + return g_object_ref (client->remote_greeter); + } + + connection = gdm_client_get_connection_sync (client, cancellable, error); + + if (connection == NULL) { + return NULL; + } + + remote_greeter = gdm_remote_greeter_proxy_new_sync (connection, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + SESSION_DBUS_PATH, + cancellable, + error); + + g_set_weak_pointer (&client->remote_greeter, remote_greeter); + + return client->remote_greeter; +} + +static void +on_chooser_proxy_created (GObject *source, + GAsyncResult *result, + gpointer user_data) +{ + GdmChooser *chooser; + g_autoptr(GTask) task = user_data; + g_autoptr(GError) error = NULL; + + chooser = gdm_chooser_proxy_new_finish (result, &error); + if (chooser == NULL) { + g_task_return_error (task, g_steal_pointer (&error)); + return; + } + + g_task_return_pointer (task, + chooser, + (GDestroyNotify) g_object_unref); +} + +static void +on_connection_for_chooser (GdmClient *client, + GAsyncResult *result, + gpointer user_data) +{ + GCancellable *cancellable; + g_autoptr(GTask) task = user_data; + g_autoptr(GDBusConnection) connection = NULL; + g_autoptr(GError) error = NULL; + + connection = gdm_client_get_connection_finish (client, result, &error); + + if (connection == NULL) { + g_task_return_error (task, g_steal_pointer (&error)); + return; + } + + cancellable = g_task_get_cancellable (task); + gdm_chooser_proxy_new (connection, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + SESSION_DBUS_PATH, + cancellable, + (GAsyncReadyCallback) + on_chooser_proxy_created, + g_steal_pointer (&task)); +} + +/** + * gdm_client_get_chooser: + * @client: a #GdmClient + * @callback: a #GAsyncReadyCallback to call when the request is satisfied + * @user_data: The data to pass to @callback + * @cancellable: a #GCancellable + * + * Gets a #GdmChooser object that can be used to + * verify a user's local account. + */ +void +gdm_client_get_chooser (GdmClient *client, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data) +{ + g_autoptr(GTask) task = NULL; + + g_return_if_fail (GDM_IS_CLIENT (client)); + + task = g_task_new (G_OBJECT (client), + cancellable, + callback, + user_data); + + if (client->chooser != NULL) { + g_task_return_pointer (task, + g_object_ref (client->chooser), + (GDestroyNotify) g_object_unref); + return; + } + + gdm_client_get_connection (client, + cancellable, + (GAsyncReadyCallback) + on_connection_for_chooser, + g_steal_pointer (&task)); +} + +/** + * gdm_client_get_chooser_finish: + * @client: a #GdmClient + * @result: The #GAsyncResult from the callback + * @error: a #GError + * + * Finishes an operation started with + * gdm_client_get_chooser(). + * + * Returns: (transfer full): a #GdmChooser + */ +GdmChooser * +gdm_client_get_chooser_finish (GdmClient *client, + GAsyncResult *result, + GError **error) +{ + GdmChooser *chooser; + + g_return_val_if_fail (GDM_IS_CLIENT (client), NULL); + + if (client->chooser != NULL) + return g_object_ref (client->chooser); + + chooser = g_task_propagate_pointer (G_TASK (result), error); + if (chooser == NULL) + return NULL; + + g_set_weak_pointer (&client->chooser, chooser); + + return chooser; +} + +/** + * gdm_client_get_chooser_sync: + * @client: a #GdmClient + * @cancellable: a #GCancellable + * @error: a #GError + * + * Gets a #GdmChooser object that can be used + * to do do various XDMCP chooser related tasks, such + * as selecting a host or disconnecting. + * + * Returns: (transfer full): #GdmChooser or %NULL if caller is not a chooser + */ +GdmChooser * +gdm_client_get_chooser_sync (GdmClient *client, + GCancellable *cancellable, + GError **error) +{ + g_autoptr(GDBusConnection) connection = NULL; + GdmChooser *chooser; + + if (client->chooser != NULL) { + return g_object_ref (client->chooser); + } + + connection = gdm_client_get_connection_sync (client, cancellable, error); + + if (connection == NULL) { + return NULL; + } + + chooser = gdm_chooser_proxy_new_sync (connection, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + SESSION_DBUS_PATH, + cancellable, + error); + + g_set_weak_pointer (&client->chooser, chooser); + + return client->chooser; +} + +static void +gdm_client_class_init (GdmClientClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = gdm_client_finalize; +} + +static void +gdm_client_init (GdmClient *client) +{ +} + +static void +gdm_client_finalize (GObject *object) +{ + GdmClient *client; + + g_return_if_fail (object != NULL); + g_return_if_fail (GDM_IS_CLIENT (object)); + + client = GDM_CLIENT (object); + + g_return_if_fail (client != NULL); + + g_clear_weak_pointer (&client->user_verifier); + g_clear_weak_pointer (&client->user_verifier_for_reauth); + g_clear_weak_pointer (&client->greeter); + g_clear_weak_pointer (&client->remote_greeter); + g_clear_weak_pointer (&client->chooser); + + g_strfreev (client->enabled_extensions); + + G_OBJECT_CLASS (gdm_client_parent_class)->finalize (object); +} + +GdmClient * +gdm_client_new (void) +{ + if (client_object != NULL) { + g_object_ref (client_object); + } else { + client_object = g_object_new (GDM_TYPE_CLIENT, NULL); + g_object_add_weak_pointer (client_object, + (gpointer *) &client_object); + } + + return GDM_CLIENT (client_object); +} + + +/** + * gdm_client_set_enabled_extensions: + * @client: a #GdmClient + * @extensions: (array zero-terminated=1) (element-type utf8): a list of extensions + * + * Enables GDM's pam extensions. Currently, only + * org.gnome.DisplayManager.UserVerifier.ChoiceList is supported. + */ +void +gdm_client_set_enabled_extensions (GdmClient *client, + const char * const *extensions) +{ + client->enabled_extensions = g_strdupv ((char **) extensions); +} diff --git a/libgdm/gdm-client.h b/libgdm/gdm-client.h new file mode 100644 index 0000000..f73b555 --- /dev/null +++ b/libgdm/gdm-client.h @@ -0,0 +1,108 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2012 Red Hat, Inc. + * Copyright (C) 2012 Giovanni Campagna <scampa.giovanni@gmail.com> + * + * 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. + * + * 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. + * + * 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. + * + */ + +#ifndef __GDM_CLIENT_H +#define __GDM_CLIENT_H + +#include <glib-object.h> +#include "gdm-client-glue.h" + +G_BEGIN_DECLS + +#define GDM_TYPE_CLIENT (gdm_client_get_type ()) +G_DECLARE_FINAL_TYPE (GdmClient, gdm_client, GDM, CLIENT, GObject) + +#define GDM_CLIENT_ERROR (gdm_client_error_quark ()) + +typedef enum _GdmClientError { + GDM_CLIENT_ERROR_GENERIC = 0, +} GdmClientError; + +GQuark gdm_client_error_quark (void); + +GdmClient *gdm_client_new (void); +void gdm_client_set_enabled_extensions (GdmClient *client, + const char * const * extensions); + +void gdm_client_open_reauthentication_channel (GdmClient *client, + const char *username, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); + +GdmUserVerifier *gdm_client_open_reauthentication_channel_finish (GdmClient *client, + GAsyncResult *result, + GError **error); + +GdmUserVerifier *gdm_client_open_reauthentication_channel_sync (GdmClient *client, + const char *username, + GCancellable *cancellable, + GError **error); + +void gdm_client_get_user_verifier (GdmClient *client, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); +GdmUserVerifier *gdm_client_get_user_verifier_finish (GdmClient *client, + GAsyncResult *result, + GError **error); +GdmUserVerifier *gdm_client_get_user_verifier_sync (GdmClient *client, + GCancellable *cancellable, + GError **error); + +GdmUserVerifierChoiceList *gdm_client_get_user_verifier_choice_list (GdmClient *client); + +void gdm_client_get_greeter (GdmClient *client, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); +GdmGreeter *gdm_client_get_greeter_finish (GdmClient *client, + GAsyncResult *result, + GError **error); +GdmGreeter *gdm_client_get_greeter_sync (GdmClient *client, + GCancellable *cancellable, + GError **error); + +void gdm_client_get_remote_greeter (GdmClient *client, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); +GdmRemoteGreeter *gdm_client_get_remote_greeter_finish (GdmClient *client, + GAsyncResult *result, + GError **error); +GdmRemoteGreeter *gdm_client_get_remote_greeter_sync (GdmClient *client, + GCancellable *cancellable, + GError **error); + +void gdm_client_get_chooser (GdmClient *client, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); +GdmChooser *gdm_client_get_chooser_finish (GdmClient *client, + GAsyncResult *result, + GError **error); +GdmChooser *gdm_client_get_chooser_sync (GdmClient *client, + GCancellable *cancellable, + GError **error); + +G_END_DECLS + +#endif /* __GDM_CLIENT_H */ diff --git a/libgdm/gdm-sessions.c b/libgdm/gdm-sessions.c new file mode 100644 index 0000000..d8b4d5c --- /dev/null +++ b/libgdm/gdm-sessions.c @@ -0,0 +1,435 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright 2008 Red Hat, Inc, + * 2007 William Jon McCann <mccann@jhu.edu> + * + * 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. + * + * 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. + * + * 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. + * + * Written by : William Jon McCann <mccann@jhu.edu> + * Ray Strode <rstrode@redhat.com> + */ + +#include "config.h" + +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> +#include <dirent.h> +#include <sys/stat.h> + +#include <glib.h> +#include <glib/gi18n.h> +#include <glib/gstdio.h> + +#include "gdm-sessions.h" + +typedef struct _GdmSessionFile { + char *id; + char *path; + char *translated_name; + char *translated_comment; +} GdmSessionFile; + +static GHashTable *gdm_available_sessions_map; + +static gboolean gdm_sessions_map_is_initialized = FALSE; + +static void +gdm_session_file_free (GdmSessionFile *session) +{ + g_free (session->id); + g_free (session->path); + g_free (session->translated_name); + g_free (session->translated_comment); + g_free (session); +} + +/* adapted from gnome-menus desktop-entries.c */ +static gboolean +key_file_is_relevant (GKeyFile *key_file) +{ + GError *error; + gboolean no_display; + gboolean hidden; + gboolean tryexec_failed; + char *tryexec; + + error = NULL; + no_display = g_key_file_get_boolean (key_file, + G_KEY_FILE_DESKTOP_GROUP, + "NoDisplay", + &error); + if (error) { + no_display = FALSE; + g_error_free (error); + } + + error = NULL; + hidden = g_key_file_get_boolean (key_file, + G_KEY_FILE_DESKTOP_GROUP, + "Hidden", + &error); + if (error) { + hidden = FALSE; + g_error_free (error); + } + + tryexec_failed = FALSE; + tryexec = g_key_file_get_string (key_file, + G_KEY_FILE_DESKTOP_GROUP, + "TryExec", + NULL); + if (tryexec) { + char *path; + + path = g_find_program_in_path (g_strstrip (tryexec)); + + tryexec_failed = (path == NULL); + + g_free (path); + g_free (tryexec); + } + + if (no_display || hidden || tryexec_failed) { + return FALSE; + } + + return TRUE; +} + +static void +load_session_file (const char *id, + const char *path) +{ + GKeyFile *key_file; + GError *error; + gboolean res; + GdmSessionFile *session; + + key_file = g_key_file_new (); + + error = NULL; + res = g_key_file_load_from_file (key_file, path, 0, &error); + + if (!res) { + g_debug ("Failed to load \"%s\": %s\n", path, error->message); + g_error_free (error); + goto out; + } + + if (! g_key_file_has_group (key_file, G_KEY_FILE_DESKTOP_GROUP)) { + goto out; + } + + res = g_key_file_has_key (key_file, G_KEY_FILE_DESKTOP_GROUP, "Name", NULL); + if (! res) { + g_debug ("\"%s\" contains no \"Name\" key\n", path); + goto out; + } + + if (!key_file_is_relevant (key_file)) { + g_debug ("\"%s\" is hidden or contains non-executable TryExec program\n", path); + goto out; + } + + session = g_new0 (GdmSessionFile, 1); + + session->id = g_strdup (id); + session->path = g_strdup (path); + + session->translated_name = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, "Name", NULL, NULL); + session->translated_comment = g_key_file_get_locale_string (key_file, G_KEY_FILE_DESKTOP_GROUP, "Comment", NULL, NULL); + + g_hash_table_insert (gdm_available_sessions_map, + g_strdup (id), + session); + out: + g_key_file_free (key_file); +} + +static gboolean +remove_duplicate_sessions (gpointer key, + gpointer value, + gpointer user_data) +{ + gboolean already_known; + GHashTable *names_seen_before; + GdmSessionFile *session; + + names_seen_before = (GHashTable *) user_data; + session = (GdmSessionFile *) value; + already_known = !g_hash_table_add (names_seen_before, session->translated_name); + + if (already_known) + g_debug ("GdmSession: Removing %s (%s) as we already have a session by this name", + session->id, + session->path); + + return already_known; +} + +static void +collect_sessions_from_directory (const char *dirname) +{ + GDir *dir; + const char *filename; + + gboolean is_x11 = g_getenv ("WAYLAND_DISPLAY") == NULL && + g_getenv ("RUNNING_UNDER_GDM") != NULL; + gboolean is_wayland = g_getenv ("WAYLAND_DISPLAY") != NULL && + g_getenv ("RUNNING_UNDER_GDM") != NULL; + + /* FIXME: add file monitor to directory */ + + dir = g_dir_open (dirname, 0, NULL); + if (dir == NULL) { + return; + } + + while ((filename = g_dir_read_name (dir))) { + char *id; + char *full_path; + + if (! g_str_has_suffix (filename, ".desktop")) { + continue; + } + + if (is_wayland) { + if (g_str_has_suffix (filename, "-wayland.desktop")) { + g_autofree char *base_name = g_strndup (filename, strlen (filename) - strlen ("-wayland.desktop")); + g_autofree char *other_name = g_strconcat (base_name, ".desktop", NULL); + g_autofree char *other_path = g_build_filename (dirname, other_name, NULL); + + if (g_file_test (other_path, G_FILE_TEST_EXISTS)) { + g_debug ("Running under Wayland, ignoring %s", filename); + continue; + } + } else { + g_autofree char *base_name = g_strndup (filename, strlen (filename) - strlen (".desktop")); + g_autofree char *other_name = g_strdup_printf ("%s-xorg.desktop", base_name); + g_autofree char *other_path = g_build_filename (dirname, other_name, NULL); + + if (g_file_test (other_path, G_FILE_TEST_EXISTS)) { + g_debug ("Running under Wayland, ignoring %s", filename); + continue; + } + } + } else if (is_x11) { + if (g_str_has_suffix (filename, "-xorg.desktop")) { + g_autofree char *base_name = g_strndup (filename, strlen (filename) - strlen ("-xorg.desktop")); + g_autofree char *other_name = g_strconcat (base_name, ".desktop", NULL); + g_autofree char *other_path = g_build_filename (dirname, other_name, NULL); + + if (g_file_test (other_path, G_FILE_TEST_EXISTS)) { + g_debug ("Running under X11, ignoring %s", filename); + continue; + } + } else { + g_autofree char *base_name = g_strndup (filename, strlen (filename) - strlen (".desktop")); + g_autofree char *other_name = g_strdup_printf ("%s-wayland.desktop", base_name); + g_autofree char *other_path = g_build_filename (dirname, other_name, NULL); + + if (g_file_test (other_path, G_FILE_TEST_EXISTS)) { + g_debug ("Running under X11, ignoring %s", filename); + continue; + } + } + } + + id = g_strndup (filename, strlen (filename) - strlen (".desktop")); + + full_path = g_build_filename (dirname, filename, NULL); + + load_session_file (id, full_path); + + g_free (id); + g_free (full_path); + } + + g_dir_close (dir); +} + +static void +collect_sessions (void) +{ + g_autoptr(GHashTable) names_seen_before = NULL; + g_autoptr(GPtrArray) xorg_search_array = NULL; + g_autoptr(GPtrArray) wayland_search_array = NULL; + gchar *session_dir = NULL; + int i; + const char *xorg_search_dirs[] = { + "/etc/X11/sessions/", + DMCONFDIR "/Sessions/", + DATADIR "/gdm/BuiltInSessions/", + DATADIR "/xsessions/", + }; + const gchar *supported_session_types_env = NULL; + g_auto (GStrv) supported_session_types = NULL; + + supported_session_types_env = g_getenv ("GDM_SUPPORTED_SESSION_TYPES"); + if (supported_session_types_env != NULL) { + supported_session_types = g_strsplit (supported_session_types_env, ":", -1); + } + + names_seen_before = g_hash_table_new (g_str_hash, g_str_equal); + xorg_search_array = g_ptr_array_new_with_free_func (g_free); + + const gchar * const *system_data_dirs = g_get_system_data_dirs (); + + for (i = 0; system_data_dirs[i]; i++) { + session_dir = g_build_filename (system_data_dirs[i], "xsessions", NULL); + g_ptr_array_add (xorg_search_array, session_dir); + } + + for (i = 0; i < G_N_ELEMENTS (xorg_search_dirs); i++) { + g_ptr_array_add (xorg_search_array, g_strdup (xorg_search_dirs[i])); + } + +#ifdef ENABLE_WAYLAND_SUPPORT + const char *wayland_search_dirs[] = { + DATADIR "/wayland-sessions/", + }; + + wayland_search_array = g_ptr_array_new_with_free_func (g_free); + + for (i = 0; system_data_dirs[i]; i++) { + session_dir = g_build_filename (system_data_dirs[i], "wayland-sessions", NULL); + g_ptr_array_add (wayland_search_array, session_dir); + } + + for (i = 0; i < G_N_ELEMENTS (wayland_search_dirs); i++) { + g_ptr_array_add (wayland_search_array, g_strdup (wayland_search_dirs[i])); + } +#endif + + if (gdm_available_sessions_map == NULL) { + gdm_available_sessions_map = g_hash_table_new_full (g_str_hash, g_str_equal, + g_free, (GDestroyNotify)gdm_session_file_free); + } + + if (!supported_session_types || g_strv_contains ((const char * const *) supported_session_types, "x11")) { + for (i = 0; i < xorg_search_array->len; i++) { + collect_sessions_from_directory (g_ptr_array_index (xorg_search_array, i)); + } + } + +#ifdef ENABLE_WAYLAND_SUPPORT +#ifdef ENABLE_USER_DISPLAY_SERVER + if (!supported_session_types || g_strv_contains ((const char * const *) supported_session_types, "wayland")) { + for (i = 0; i < wayland_search_array->len; i++) { + collect_sessions_from_directory (g_ptr_array_index (wayland_search_array, i)); + } + } +#endif +#endif + + g_hash_table_foreach_remove (gdm_available_sessions_map, + remove_duplicate_sessions, + names_seen_before); +} + +static gint +compare_session_ids (gconstpointer a, + gconstpointer b) +{ + GdmSessionFile *session_a, *session_b; + session_a = (GdmSessionFile *) g_hash_table_lookup (gdm_available_sessions_map, a); + session_b = (GdmSessionFile *) g_hash_table_lookup (gdm_available_sessions_map, b); + + if (session_a == NULL) + return -1; + + if (session_b == NULL) + return 1; + + return g_strcmp0 (session_a->translated_name, session_b->translated_name); +} + +/** + * gdm_get_session_ids: + * + * Reads /usr/share/xsessions and other relevant places for possible sessions + * to log into and returns the complete list. + * + * Returns: (transfer full): a %NULL terminated list of session ids + */ +char ** +gdm_get_session_ids (void) +{ + GHashTableIter iter; + gpointer key, value; + GPtrArray *array; + + if (!gdm_sessions_map_is_initialized) { + collect_sessions (); + + gdm_sessions_map_is_initialized = TRUE; + } + + array = g_ptr_array_new (); + g_hash_table_iter_init (&iter, gdm_available_sessions_map); + while (g_hash_table_iter_next (&iter, &key, &value)) { + GdmSessionFile *session; + + session = (GdmSessionFile *) value; + + g_ptr_array_add (array, g_strdup (session->id)); + } + g_ptr_array_add (array, NULL); + + g_ptr_array_sort (array, compare_session_ids); + + return (char **) g_ptr_array_free (array, FALSE); +} + +/** + * gdm_get_session_name_and_description: + * @id: an id from gdm_get_session_ids() + * @description: (out): optional returned session description + * + * Takes an xsession id and returns the name and comment about it. + * + * Returns: The session name if found, or %NULL otherwise + */ +char * +gdm_get_session_name_and_description (const char *id, + char **description) +{ + GdmSessionFile *session; + char *name; + + if (!gdm_sessions_map_is_initialized) { + collect_sessions (); + + gdm_sessions_map_is_initialized = TRUE; + } + + session = (GdmSessionFile *) g_hash_table_lookup (gdm_available_sessions_map, + id); + + if (session == NULL) { + return NULL; + } + + name = g_strdup (session->translated_name); + + if (description != NULL) { + *description = g_strdup (session->translated_comment); + } + + return name; +} diff --git a/libgdm/gdm-sessions.h b/libgdm/gdm-sessions.h new file mode 100644 index 0000000..66e99fd --- /dev/null +++ b/libgdm/gdm-sessions.h @@ -0,0 +1,37 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright 2008 Red Hat, Inc. + * Copyright 2007 William Jon McCann <mccann@jhu.edu> + * + * 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. + * + * 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. + * + * 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. + * + * Written by: Ray Strode + * William Jon McCann + */ + +#ifndef __GDM_GREETER_SESSIONS_H +#define __GDM_GREETER_SESSIONS_H + +#include <glib.h> + +G_BEGIN_DECLS + +char ** gdm_get_session_ids (void); +char * gdm_get_session_name_and_description (const char *id, + char **description); + +G_END_DECLS + +#endif /* __GDM_SESSION_H */ diff --git a/libgdm/gdm-user-switching.c b/libgdm/gdm-user-switching.c new file mode 100644 index 0000000..b39d21d --- /dev/null +++ b/libgdm/gdm-user-switching.c @@ -0,0 +1,258 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2012 Red Hat, Inc. + * Copyright (C) 2012 Giovanni Campagna <scampa.giovanni@gmail.com> + * + * 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. + * + * 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. + * + * 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. + * + */ + +#include "config.h" + +#include <stdlib.h> +#include <stdio.h> +#include <unistd.h> +#include <string.h> + +#include <glib.h> +#include <glib/gi18n.h> +#include <glib-object.h> +#include <gio/gio.h> + +#include <systemd/sd-login.h> + +#include "common/gdm-common.h" +#include "gdm-user-switching.h" +#include "gdm-client.h" + +static gboolean +create_transient_display (GDBusConnection *connection, + GCancellable *cancellable, + GError **error) +{ + GVariant *reply; + const char *value; + + reply = g_dbus_connection_call_sync (connection, + "org.gnome.DisplayManager", + "/org/gnome/DisplayManager/LocalDisplayFactory", + "org.gnome.DisplayManager.LocalDisplayFactory", + "CreateTransientDisplay", + NULL, /* parameters */ + G_VARIANT_TYPE ("(o)"), + G_DBUS_CALL_FLAGS_NONE, + -1, + cancellable, error); + if (reply == NULL) { + g_prefix_error (error, _("Unable to create transient display: ")); + return FALSE; + } + + g_variant_get (reply, "(&o)", &value); + g_debug ("Started %s", value); + + g_variant_unref (reply); + return TRUE; +} + +static gboolean +activate_session_id (GDBusConnection *connection, + GCancellable *cancellable, + const char *seat_id, + const char *session_id, + GError **error) +{ + GVariant *reply; + + reply = g_dbus_connection_call_sync (connection, + "org.freedesktop.login1", + "/org/freedesktop/login1", + "org.freedesktop.login1.Manager", + "ActivateSessionOnSeat", + g_variant_new ("(ss)", session_id, seat_id), + NULL, + G_DBUS_CALL_FLAGS_NONE, + -1, + cancellable, error); + if (reply == NULL) { + g_prefix_error (error, _("Unable to activate session: ")); + return FALSE; + } + + g_variant_unref (reply); + + return TRUE; +} + +static gboolean +get_login_window_session_id (const char *seat_id, + char **session_id) +{ + gboolean ret; + int res, i; + char **sessions; + char *service_id; + char *service_class; + char *state; + + res = sd_seat_get_sessions (seat_id, &sessions, NULL, NULL); + if (res < 0) { + g_debug ("Failed to determine sessions: %s", strerror (-res)); + return FALSE; + } + + if (sessions == NULL || sessions[0] == NULL) { + *session_id = NULL; + ret = TRUE; + goto out; + } + + for (i = 0; sessions[i]; i ++) { + + res = sd_session_get_class (sessions[i], &service_class); + if (res < 0) { + g_debug ("failed to determine class of session %s: %s", sessions[i], strerror (-res)); + ret = FALSE; + goto out; + } + + if (strcmp (service_class, "greeter") != 0) { + free (service_class); + continue; + } + + free (service_class); + + ret = sd_session_get_state (sessions[i], &state); + if (ret < 0) { + g_debug ("failed to determine state of session %s: %s", sessions[i], strerror (-res)); + ret = FALSE; + goto out; + } + + if (g_strcmp0 (state, "closing") == 0) { + free (state); + continue; + } + free (state); + + res = sd_session_get_service (sessions[i], &service_id); + if (res < 0) { + g_debug ("failed to determine service of session %s: %s", sessions[i], strerror (-res)); + ret = FALSE; + goto out; + } + + if (strcmp (service_id, "gdm-launch-environment") == 0) { + *session_id = g_strdup (sessions[i]); + ret = TRUE; + + free (service_id); + goto out; + } + + free (service_id); + } + + *session_id = NULL; + ret = TRUE; + +out: + if (sessions) { + for (i = 0; sessions[i]; i ++) { + free (sessions[i]); + } + + free (sessions); + } + + return ret; +} + +static gboolean +goto_login_session (GDBusConnection *connection, + GCancellable *cancellable, + GError **error) +{ + gboolean ret; + int res; + char *our_session; + char *session_id; + char *seat_id; + GError *local_error = NULL; + + ret = FALSE; + session_id = NULL; + seat_id = NULL; + + /* First look for any existing LoginWindow sessions on the seat. + If none are found, create a new one. */ + + /* Note that we mostly use free () here, instead of g_free () + * since the data allocated is from libsystemd-logind, which + * does not use GLib's g_malloc (). */ + if (!gdm_find_display_session (0, getuid (), &our_session, &local_error)) { + g_propagate_prefixed_error (error, local_error, _("Could not identify the current session: ")); + + return FALSE; + } + + res = sd_session_get_seat (our_session, &seat_id); + free (our_session); + if (res < 0) { + g_debug ("failed to determine own seat: %s", strerror (-res)); + g_set_error (error, GDM_CLIENT_ERROR, 0, _("Could not identify the current seat.")); + + return FALSE; + } + + res = get_login_window_session_id (seat_id, &session_id); + if (res && session_id != NULL) { + res = activate_session_id (connection, cancellable, seat_id, session_id, error); + + if (res) { + ret = TRUE; + } + } + + if (! ret && g_strcmp0 (seat_id, "seat0") == 0) { + res = create_transient_display (connection, cancellable, error); + if (res) { + ret = TRUE; + } + } + + free (seat_id); + g_free (session_id); + + return ret; +} + +gboolean +gdm_goto_login_session_sync (GCancellable *cancellable, + GError **error) +{ + GDBusConnection *connection; + gboolean retval; + + connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, cancellable, error); + if (!connection) + return FALSE; + + retval = goto_login_session (connection, cancellable, error); + + g_object_unref (connection); + return retval; +} diff --git a/libgdm/gdm-user-switching.h b/libgdm/gdm-user-switching.h new file mode 100644 index 0000000..2886d50 --- /dev/null +++ b/libgdm/gdm-user-switching.h @@ -0,0 +1,31 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2012 Red Hat, Inc. + * Copyright (C) 2012 Giovanni Campagna <scampa.giovanni@gmail.com> + * + * 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. + * + * 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. + * + * 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. + * + */ + +#ifndef __GDM_USER_SWITCHING_H +#define __GDM_USER_SWITCHING_H + +#include <glib-object.h> +#include <gio/gio.h> + +gboolean gdm_goto_login_session_sync (GCancellable *cancellable, + GError **error); + +#endif diff --git a/libgdm/gdm.pc.in b/libgdm/gdm.pc.in new file mode 100644 index 0000000..65ee6d3 --- /dev/null +++ b/libgdm/gdm.pc.in @@ -0,0 +1,11 @@ +prefix=@prefix@ +exec_prefix=@exec_prefix@ +libdir=@libdir@ +includedir=@includedir@ + +Name: GDM Client Library +Description: Client Library for communicating with GDM daemon +Version: @VERSION@ +Libs: -L${libdir} -lgdm +Cflags: -I${includedir}/gdm +Requires: gobject-2.0 diff --git a/libgdm/libgdm.map b/libgdm/libgdm.map new file mode 100644 index 0000000..6a1c7fa --- /dev/null +++ b/libgdm/libgdm.map @@ -0,0 +1,6 @@ +{ +global: + *; +local: + _*; +}; diff --git a/libgdm/meson.build b/libgdm/meson.build new file mode 100644 index 0000000..3f8cafb --- /dev/null +++ b/libgdm/meson.build @@ -0,0 +1,104 @@ +libgdm_manager_dbus_gen = gnome.gdbus_codegen('gdm-manager-glue', + sources: '..' / 'daemon' / 'gdm-manager.xml', + namespace: 'Gdm', + interface_prefix: 'org.gnome.DisplayManager', + autocleanup: 'all', + annotations: [ + [ 'org.gnome.DisplayManager.Manager', 'org.gtk.GDBus.C.Name', 'Manager' ], + ], + build_by_default: true, +) +libgdm_client_dbus_gen = gnome.gdbus_codegen('gdm-client-glue', + sources: '..' / 'daemon' / 'gdm-session.xml', + namespace: 'Gdm', + interface_prefix: 'org.gnome.DisplayManager', + autocleanup: 'all', + annotations: [ + [ 'org.gnome.DisplayManager.UserVerifier', 'org.gtk.GDBus.C.Name', 'UserVerifier' ], + [ 'org.gnome.DisplayManager.Greeter', 'org.gtk.GDBus.C.Name', 'Greeter' ], + [ 'org.gnome.DisplayManager.RemoteGreeter', 'org.gtk.GDBus.C.Name', 'RemoteGreeter' ], + [ 'org.gnome.DisplayManager.Chooser', 'org.gtk.GDBus.C.Name', 'Chooser' ], + ], + build_by_default: true, + install_header: true, + install_dir: get_option('includedir') / meson.project_name() +) + +libgdm_built_sources = [ + libgdm_manager_dbus_gen, + libgdm_client_dbus_gen, +] + +libgdm_sources = files( + 'gdm-client.c', + 'gdm-sessions.c', + 'gdm-user-switching.c', +) + +libgdm_public_headers = [ + 'gdm-client.h', + 'gdm-sessions.h', + 'gdm-user-switching.h', + # gdm-client-glue.h is automatically installed +] + +install_headers(libgdm_public_headers, + subdir: meson.project_name(), +) + +libgdm_headers = [ + libgdm_public_headers, + libgdm_client_dbus_gen[1], + libgdm_manager_dbus_gen[1], +] + +libgdm_deps = [ + glib_dep, + gio_dep, + gio_unix_dep, + libsystemd_dep, + libgdmcommon_dep, +] + +libgdm_link_flags = [ + '-Wl,--version-script,@0@/libgdm.map'.format(meson.current_source_dir()), +] + +libgdm = library('gdm', + libgdm_sources + libgdm_built_sources, + version: '1.0.0', + dependencies: libgdm_deps, + include_directories: config_h_dir, + link_args: libgdm_link_flags, + install: true, +) + +libgdm_gir_includes = [ + 'GLib-2.0', + 'GObject-2.0', + 'Gio-2.0', +] + +libgdm_dep = declare_dependency( + link_with: libgdm, + dependencies: libgdm_deps, + include_directories: include_directories('.'), + sources: libgdm_built_sources, +) + +libgdm_gir = gnome.generate_gir(libgdm, + sources: libgdm_headers + libgdm_sources, + namespace: 'Gdm', + nsversion: '1.0', + identifier_prefix: 'Gdm', + includes: libgdm_gir_includes, + install: true, +) + +# Pkg-config file +pkgconfig.generate(libgdm, + name: 'GDM Client Library', + description: 'Client Library for communicating with GDM daemon', + filebase: meson.project_name(), + requires: gobject_dep, +) |