Description: Support setting/reading LightDM autologin setings
Author: Michael Terry <michael.terry@canonical.com>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=40020
Bug-Ubuntu: https://launchpad.net/bugs/798962
Reviewed-by: Alessio Treglia <alessio@debian.org>
Last-Update: 2012-04-30
---
 src/daemon.c |  143 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 132 insertions(+), 11 deletions(-)

Index: accountsservice-0.6.35/src/daemon.c
===================================================================
--- accountsservice-0.6.35.orig/src/daemon.c	2014-01-17 18:57:33.000000000 +0100
+++ accountsservice-0.6.35/src/daemon.c	2014-01-17 18:58:31.480569091 +0100
@@ -51,10 +51,10 @@
 #define PATH_SHADOW "/etc/shadow"
 #define PATH_GROUP "/etc/group"
 #define PATH_GDM_CUSTOM "/etc/gdm/custom.conf"
+#define PATH_LIGHTDM_CONFIG "/etc/lightdm/lightdm.conf"
 #ifdef HAVE_UTMPX_H
 #define PATH_WTMP _PATH_WTMPX
 #endif
-
 enum {
         PROP_0,
         PROP_DAEMON_VERSION
@@ -72,6 +72,7 @@
         GFileMonitor *shadow_monitor;
         GFileMonitor *group_monitor;
         GFileMonitor *gdm_monitor;
+        GFileMonitor *lightdm_monitor;
 #ifdef HAVE_UTMPX_H
         GFileMonitor *wtmp_monitor;
 #endif
@@ -703,9 +704,19 @@
                                                     on_users_monitor_changed);
 #endif
 
-        daemon->priv->gdm_monitor = setup_monitor (daemon,
-                                                   PATH_GDM_CUSTOM,
-                                                   on_gdm_monitor_changed);
+        if (g_file_test ("/etc/gdm", G_FILE_TEST_EXISTS))
+        {
+                daemon->priv->gdm_monitor = setup_monitor (daemon,
+                                                           PATH_GDM_CUSTOM,
+                                                           on_gdm_monitor_changed);
+        }
+        
+        if (g_file_test ("/etc/lightdm", G_FILE_TEST_EXISTS))
+        {
+                daemon->priv->lightdm_monitor = setup_monitor (daemon,
+                                                           PATH_LIGHTDM_CONFIG,
+                                                           on_gdm_monitor_changed);
+        }
 
         queue_reload_users (daemon);
         queue_reload_autologin (daemon);
@@ -1435,10 +1446,10 @@
 }
 
 gboolean
-load_autologin (Daemon      *daemon,
-                gchar      **name,
-                gboolean    *enabled,
-                GError     **error)
+load_autologin_gdm (Daemon      *daemon,
+                    gchar      **name,
+                    gboolean    *enabled,
+                    GError     **error)
 {
         GKeyFile *keyfile;
         GError *local_error;
@@ -1481,23 +1492,90 @@
         return TRUE;
 }
 
-static gboolean
-save_autologin (Daemon      *daemon,
-                const gchar *name,
-                gboolean     enabled,
+gboolean
+load_autologin_lightdm (Daemon      *daemon,
+                        gchar      **name,
+                        gboolean    *enabled,
+                        GError     **error)
+{
+        GKeyFile *keyfile;
+
+        keyfile = g_key_file_new ();
+        if (!g_key_file_load_from_file (keyfile,
+                                        PATH_LIGHTDM_CONFIG,
+                                        G_KEY_FILE_KEEP_COMMENTS,
+                                        error)) {
+                g_key_file_free (keyfile);
+                return FALSE;
+        }
+
+        *name = g_key_file_get_string (keyfile, "SeatDefaults", "autologin-user", error);
+        *enabled = (*error == NULL && (*name) && (*name)[0] != 0);
+
+        g_key_file_free (keyfile);
+
+        return (*error == NULL);
+}
+
+gboolean
+load_autologin (Daemon      *daemon,
+                gchar      **name,
+                gboolean    *enabled,
                 GError     **error)
 {
+        GDBusConnection *connection;
+        GVariant *reply;
+
+        /* First, determine whether we should load GDM or LightDM values by
+           checking if GDM is running. */
+        connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
+        reply = g_dbus_connection_call_sync (connection, 
+                                             "org.freedesktop.DBus", 
+                                              "/", 
+                                              "org.freedesktop.DBus",
+                                              "GetNameOwner", 
+                                              g_variant_new ("(s)", "org.gnome.DisplayManager"),
+                                              G_VARIANT_TYPE ("(s)"), 
+                                              G_DBUS_CALL_FLAGS_NO_AUTO_START,
+                                              -1, 
+                                              NULL, 
+                                              NULL);
+        g_object_unref (connection);
+
+        if (reply != NULL) {
+                g_variant_unref (reply);
+                return load_autologin_gdm (daemon, name, enabled, error);
+        } else {
+                return load_autologin_lightdm (daemon, name, enabled, error);
+        }
+}
+
+static gboolean
+save_autologin_gdm (Daemon      *daemon,
+                    const gchar *name,
+                    gboolean     enabled,
+                    GError     **error)
+{
         GKeyFile *keyfile;
         gchar *data;
         gboolean result;
+        GError *local_error = NULL;
+
+        if (!g_file_test ("/etc/gdm", G_FILE_TEST_EXISTS))
+                return TRUE; /* not an error, just not installed */
 
         keyfile = g_key_file_new ();
         if (!g_key_file_load_from_file (keyfile,
                                         PATH_GDM_CUSTOM,
                                         G_KEY_FILE_KEEP_COMMENTS,
-                                        error)) {
-                g_key_file_free (keyfile);
-                return FALSE;
+                                        &local_error)) {
+                /* It's OK for custom.conf to not exist, we will make it */
+                if (!g_error_matches (local_error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) {
+                        g_propagate_error (error, local_error);
+                        g_key_file_free (keyfile);
+                        return FALSE;
+                }
+                g_error_free (local_error);
         }
 
         g_key_file_set_string (keyfile, "daemon", "AutomaticLoginEnable", enabled ? "True" : "False");
@@ -1512,6 +1590,58 @@
         return result;
 }
 
+static gboolean
+save_autologin_lightdm (Daemon      *daemon,
+                        const gchar *name,
+                        gboolean     enabled,
+                        GError     **error)
+{
+        GKeyFile *keyfile;
+        gchar *data;
+        gboolean result;
+        GError *local_error = NULL;
+
+        if (!g_file_test ("/etc/lightdm", G_FILE_TEST_EXISTS))
+                return TRUE; /* not an error, just not installed */
+
+        keyfile = g_key_file_new ();
+        if (!g_key_file_load_from_file (keyfile,
+                                        PATH_LIGHTDM_CONFIG,
+                                        G_KEY_FILE_KEEP_COMMENTS,
+                                        &local_error)) {
+                /* It's OK for custom.conf to not exist, we will make it */
+                if (!g_error_matches (local_error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) {
+                        g_propagate_error (error, local_error);
+                        g_key_file_free (keyfile);
+                        return FALSE;
+                }
+                g_error_free (local_error);
+        }
+
+        g_key_file_set_string (keyfile, "SeatDefaults", "autologin-user", enabled ? name : "");
+
+        data = g_key_file_to_data (keyfile, NULL, NULL);
+        result = g_file_set_contents (PATH_LIGHTDM_CONFIG, data, -1, error);
+
+        g_key_file_free (keyfile);
+        g_free (data);
+
+        return result;
+}
+
+static gboolean
+save_autologin (Daemon      *daemon,
+                const gchar *name,
+                gboolean     enabled,
+                GError     **error)
+{
+        if (!save_autologin_gdm (daemon, name, enabled, error))
+                return FALSE;
+        if (!save_autologin_lightdm (daemon, name, enabled, error))
+                return FALSE;
+        return TRUE;
+}
+
 gboolean
 daemon_local_set_automatic_login (Daemon    *daemon,
                                   User      *user,
