Back to index

lightdm  1.3.2
guest-account.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010-2011 Robert Ancell.
00003  * Author: Robert Ancell <robert.ancell@canonical.com>
00004  * 
00005  * This program is free software: you can redistribute it and/or modify it under
00006  * the terms of the GNU General Public License as published by the Free Software
00007  * Foundation, either version 3 of the License, or (at your option) any later
00008  * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
00009  * license.
00010  */
00011 
00012 #include <string.h>
00013 #include <ctype.h>
00014 
00015 #include "guest-account.h"
00016 #include "configuration.h"
00017 
00018 static gchar *
00019 get_setup_script (void)
00020 {
00021     gchar *script;
00022     static gchar *setup_script = NULL;
00023   
00024     if (setup_script)
00025         return setup_script;
00026 
00027     script = config_get_string (config_get_instance (), "LightDM", "guest-account-script");
00028     if (!script)
00029         return NULL;
00030 
00031     setup_script = g_find_program_in_path (script);
00032     g_free (script);
00033   
00034     return setup_script;
00035 }
00036 
00037 gboolean
00038 guest_account_is_installed (void)
00039 {
00040     return get_setup_script () != NULL;
00041 }
00042 
00043 static gboolean
00044 run_script (const gchar *script, gchar **stdout_text, gint *exit_status, GError **error)
00045 {
00046     gint argc;
00047     gchar **argv;
00048     gboolean result;
00049 
00050     if (!g_shell_parse_argv (script, &argc, &argv, error))
00051         return FALSE;
00052 
00053     result = g_spawn_sync (NULL, argv, NULL,
00054                            G_SPAWN_SEARCH_PATH,
00055                            NULL, NULL,
00056                            stdout_text, NULL, exit_status, error);
00057     g_strfreev (argv);
00058   
00059     return result;
00060 }
00061 
00062 gchar *
00063 guest_account_setup (void)
00064 {
00065     gchar *command, *stdout_text, *username, **lines;
00066     gint exit_status;
00067     gboolean result;
00068     GError *error = NULL;
00069 
00070     command = g_strdup_printf ("%s add", get_setup_script ());
00071     g_debug ("Opening guest account with command '%s'", command);
00072     result = run_script (command, &stdout_text, &exit_status, &error);
00073     g_free (command);
00074     if (error)
00075         g_warning ("Error running guest account setup script '%s': %s", get_setup_script (), error->message);
00076     g_clear_error (&error);
00077     if (!result)
00078         return NULL;
00079 
00080     if (exit_status != 0)
00081     {
00082         g_debug ("Guest account setup script returns %d: %s", exit_status, stdout_text);
00083         g_free (stdout_text);
00084         return NULL;
00085     }
00086 
00087     /* Use the last line and trim whitespace */
00088     lines = g_strsplit (g_strstrip (stdout_text), "\n", -1);
00089     if (lines)
00090         username = g_strdup (g_strstrip (lines[g_strv_length (lines) - 1]));
00091     else
00092         username = g_strdup ("");
00093     g_free (stdout_text);
00094 
00095     if (strcmp (username, "") == 0)
00096     {
00097         g_free (username);
00098         g_debug ("Guest account setup script didn't return a username");
00099         return NULL;
00100     }
00101   
00102     g_debug ("Guest account %s setup", username);
00103 
00104     return username;
00105 }
00106 
00107 void
00108 guest_account_cleanup (const gchar *username)
00109 {
00110     gchar *command;
00111     gboolean result;
00112     gint exit_status;
00113     GError *error = NULL;
00114 
00115     command = g_strdup_printf ("%s remove %s", get_setup_script (), username);
00116     g_debug ("Closing guest account %s with command '%s'", username, command);
00117 
00118     result = run_script (command, NULL, &exit_status, &error);
00119     g_free (command);
00120 
00121     if (error)
00122         g_warning ("Error running guest account cleanup script '%s': %s", get_setup_script (), error->message);
00123     g_clear_error (&error);
00124 
00125     if (result && exit_status != 0)
00126         g_debug ("Guest account cleanup script returns %d", exit_status);
00127 }