summaryrefslogtreecommitdiff
path: root/src/guest-account.c
diff options
context:
space:
mode:
authorRobert Ancell <robert.ancell@canonical.com>2011-06-26 23:54:57 +1000
committerRobert Ancell <robert.ancell@canonical.com>2011-06-26 23:54:57 +1000
commitde4b691115622a5a3361ac8d19855d43edc8bc20 (patch)
treeb390960d0bd73df733e8c10d0f239ba7ebb290e9 /src/guest-account.c
parent754a9af1192d2f63de79a947181bda5b0d1bc779 (diff)
downloadlightdm-de4b691115622a5a3361ac8d19855d43edc8bc20.tar.gz
More work getting guest mode working
Diffstat (limited to 'src/guest-account.c')
-rw-r--r--src/guest-account.c158
1 files changed, 158 insertions, 0 deletions
diff --git a/src/guest-account.c b/src/guest-account.c
new file mode 100644
index 00000000..f98c71f0
--- /dev/null
+++ b/src/guest-account.c
@@ -0,0 +1,158 @@
+/*
+ * Copyright (C) 2010-2011 Robert Ancell.
+ * Author: Robert Ancell <robert.ancell@canonical.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 3 of the License, or (at your option) any later
+ * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
+ * license.
+ */
+
+#include "guest-account.h"
+#include "configuration.h"
+
+struct GuestAccountPrivate
+{
+ gchar *username;
+};
+
+G_DEFINE_TYPE (GuestAccount, guest_account, G_TYPE_OBJECT);
+
+static GuestAccount *guest_account_instance = NULL;
+
+GuestAccount *
+guest_account_get_instance ()
+{
+ if (!guest_account_instance)
+ guest_account_instance = g_object_new (GUEST_ACCOUNT_TYPE, NULL);
+ return guest_account_instance;
+}
+
+gboolean
+guest_account_get_is_enabled (GuestAccount *account)
+{
+ return config_get_boolean (config_get_instance (), "GuestAccount", "enabled");
+}
+
+const gchar *
+guest_account_get_username (GuestAccount *account)
+{
+ return account->priv->username;
+}
+
+static gboolean
+run_script (const gchar *script, gchar **stdout_text, gint *exit_status, GError **error)
+{
+ gint argc;
+ gchar **argv;
+ gboolean result;
+
+ if (!g_shell_parse_argv (script, &argc, &argv, error))
+ return FALSE;
+
+ result = g_spawn_sync (NULL, argv, NULL,
+ G_SPAWN_SEARCH_PATH,
+ NULL, NULL,
+ stdout_text, NULL, exit_status, error);
+ g_strfreev (argv);
+
+ return result;
+}
+
+gboolean
+guest_account_setup (GuestAccount *account)
+{
+ gchar *setup_script;
+ gchar *stdout_text = NULL;
+ gint exit_status;
+ gboolean result;
+ GError *error = NULL;
+
+ if (!guest_account_get_is_enabled (account))
+ return FALSE;
+
+ setup_script = config_get_string (config_get_instance (), "GuestAccount", "setup-script");
+ if (!setup_script)
+ return FALSE;
+
+ g_debug ("Opening guest account with script %s", setup_script);
+
+ result = run_script (setup_script, &stdout_text, &exit_status, &error);
+ if (!result)
+ g_warning ("Error running guest account setup script '%s': %s", setup_script, error->message);
+ g_free (setup_script);
+ g_clear_error (&error);
+ if (!result)
+ return FALSE;
+
+ if (exit_status != 0)
+ {
+ g_warning ("Guest account setup script returns %d: %s", exit_status, stdout_text);
+ result = FALSE;
+ }
+ else
+ {
+ g_free (account->priv->username);
+ account->priv->username = g_strdup (g_strstrip (stdout_text));
+ g_debug ("Guest account setup with username '%s'", account->priv->username);
+ }
+
+ g_free (stdout_text);
+
+ return result;
+}
+
+void
+guest_account_cleanup (GuestAccount *account)
+{
+ gchar *cleanup_script;
+ gint exit_status;
+ GError *error = NULL;
+
+ if (!guest_account_get_is_enabled (account))
+ return;
+
+ cleanup_script = config_get_string (config_get_instance (), "GuestAccount", "cleanup-script");
+ if (!cleanup_script)
+ return;
+
+ g_debug ("Closing guest account with script %s", cleanup_script);
+
+ if (run_script (cleanup_script, NULL, &exit_status, &error))
+ {
+ if (exit_status != 0)
+ g_warning ("Guest account cleanup script returns %d", exit_status);
+ }
+ else
+ g_warning ("Error running guest account cleanup script '%s': %s", cleanup_script, error->message);
+ g_clear_error (&error);
+
+ g_free (cleanup_script);
+}
+
+static void
+guest_account_init (GuestAccount *account)
+{
+ account->priv = G_TYPE_INSTANCE_GET_PRIVATE (account, GUEST_ACCOUNT_TYPE, GuestAccountPrivate);
+}
+
+static void
+guest_account_finalize (GObject *object)
+{
+ GuestAccount *self = GUEST_ACCOUNT (object);
+
+ g_free (self->priv->username);
+
+ G_OBJECT_CLASS (guest_account_parent_class)->finalize (object);
+}
+
+static void
+guest_account_class_init (GuestAccountClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = guest_account_finalize;
+
+ g_type_class_add_private (klass, sizeof (GuestAccountPrivate));
+}