1 /*
2  * Copyright (C) 2010-2011 Robert Ancell.
3  * Author: Robert Ancell <robert.ancell@canonical.com>
4  *
5  * This program is free software: you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License as published by the Free Software
7  * Foundation, either version 3 of the License, or (at your option) any later
8  * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
9  * license.
10  */
11 
12 #include <string.h>
13 #include <ctype.h>
14 
15 #include "guest-account.h"
16 #include "configuration.h"
17 
18 static gchar *
get_setup_script(void)19 get_setup_script (void)
20 {
21     static gchar *setup_script = NULL;
22 
23     if (setup_script)
24         return setup_script;
25 
26     g_autofree gchar *script = config_get_string (config_get_instance (), "LightDM", "guest-account-script");
27     if (!script)
28         return NULL;
29 
30     setup_script = g_find_program_in_path (script);
31 
32     return setup_script;
33 }
34 
35 gboolean
guest_account_is_installed(void)36 guest_account_is_installed (void)
37 {
38     return get_setup_script () != NULL;
39 }
40 
41 static gboolean
run_script(const gchar * script,gchar ** stdout_text,gint * exit_status,GError ** error)42 run_script (const gchar *script, gchar **stdout_text, gint *exit_status, GError **error)
43 {
44     gint argc;
45     g_auto(GStrv) argv = NULL;
46     if (!g_shell_parse_argv (script, &argc, &argv, error))
47         return FALSE;
48 
49     gboolean result = g_spawn_sync (NULL, argv, NULL,
50                                     G_SPAWN_SEARCH_PATH,
51                                     NULL, NULL,
52                                     stdout_text, NULL, exit_status, error);
53 
54     return result;
55 }
56 
57 gchar *
guest_account_setup(void)58 guest_account_setup (void)
59 {
60     g_autofree gchar *command = g_strdup_printf ("%s add", get_setup_script ());
61     g_debug ("Opening guest account with command '%s'", command);
62     g_autofree gchar *stdout_text = NULL;
63     gint exit_status;
64     g_autoptr(GError) error = NULL;
65     gboolean result = run_script (command, &stdout_text, &exit_status, &error);
66     if (error)
67         g_warning ("Error running guest account setup script '%s': %s", get_setup_script (), error->message);
68     if (!result)
69         return NULL;
70 
71     if (exit_status != 0)
72     {
73         g_debug ("Guest account setup script returns %d: %s", exit_status, stdout_text);
74         return NULL;
75     }
76 
77     /* Use the last line and trim whitespace */
78     g_auto(GStrv) lines = g_strsplit (g_strstrip (stdout_text), "\n", -1);
79     g_autofree gchar *username = NULL;
80     if (lines)
81         username = g_strdup (g_strstrip (lines[g_strv_length (lines) - 1]));
82     else
83         username = g_strdup ("");
84 
85     if (strcmp (username, "") == 0)
86     {
87         g_debug ("Guest account setup script didn't return a username");
88         return NULL;
89     }
90 
91     g_debug ("Guest account %s setup", username);
92 
93     return g_steal_pointer (&username);
94 }
95 
96 void
guest_account_cleanup(const gchar * username)97 guest_account_cleanup (const gchar *username)
98 {
99     g_autofree gchar *command = g_strdup_printf ("%s remove %s", get_setup_script (), username);
100     g_debug ("Closing guest account %s with command '%s'", username, command);
101 
102     gint exit_status;
103     g_autoptr(GError) error = NULL;
104     gboolean result = run_script (command, NULL, &exit_status, &error);
105 
106     if (error)
107         g_warning ("Error running guest account cleanup script '%s': %s", get_setup_script (), error->message);
108 
109     if (result && exit_status != 0)
110         g_debug ("Guest account cleanup script returns %d", exit_status);
111 }
112