]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - src/guest-account.c
Load all users only when really needed
[sojka/lightdm.git] / src / guest-account.c
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 *
19 get_setup_script (void)
20 {
21     gchar *script;
22     static gchar *setup_script = NULL;
23
24     if (setup_script)
25         return setup_script;
26
27     script = config_get_string (config_get_instance (), "LightDM", "guest-account-script");
28     if (!script)
29         return NULL;
30
31     setup_script = g_find_program_in_path (script);
32     g_free (script);
33
34     return setup_script;
35 }
36
37 gboolean
38 guest_account_is_installed (void)
39 {
40     return get_setup_script () != NULL;
41 }
42
43 static gboolean
44 run_script (const gchar *script, gchar **stdout_text, gint *exit_status, GError **error)
45 {
46     gint argc;
47     gchar **argv;
48     gboolean result;
49
50     if (!g_shell_parse_argv (script, &argc, &argv, error))
51         return FALSE;
52
53     result = g_spawn_sync (NULL, argv, NULL,
54                            G_SPAWN_SEARCH_PATH,
55                            NULL, NULL,
56                            stdout_text, NULL, exit_status, error);
57     g_strfreev (argv);
58
59     return result;
60 }
61
62 gchar *
63 guest_account_setup (void)
64 {
65     gchar *command, *stdout_text, *username, **lines;
66     gint exit_status;
67     gboolean result;
68     GError *error = NULL;
69
70     command = g_strdup_printf ("%s add", get_setup_script ());
71     g_debug ("Opening guest account with command '%s'", command);
72     result = run_script (command, &stdout_text, &exit_status, &error);
73     g_free (command);
74     if (error)
75         g_warning ("Error running guest account setup script '%s': %s", get_setup_script (), error->message);
76     g_clear_error (&error);
77     if (!result)
78         return NULL;
79
80     if (exit_status != 0)
81     {
82         g_debug ("Guest account setup script returns %d: %s", exit_status, stdout_text);
83         g_free (stdout_text);
84         return NULL;
85     }
86
87     /* Use the last line and trim whitespace */
88     lines = g_strsplit (g_strstrip (stdout_text), "\n", -1);
89     if (lines)
90         username = g_strdup (g_strstrip (lines[g_strv_length (lines) - 1]));
91     else
92         username = g_strdup ("");
93     g_free (stdout_text);
94
95     if (strcmp (username, "") == 0)
96     {
97         g_free (username);
98         g_debug ("Guest account setup script didn't return a username");
99         return NULL;
100     }
101
102     g_debug ("Guest account %s setup", username);
103
104     return username;
105 }
106
107 void
108 guest_account_cleanup (const gchar *username)
109 {
110     gchar *command;
111     gboolean result;
112     gint exit_status;
113     GError *error = NULL;
114
115     command = g_strdup_printf ("%s remove %s", get_setup_script (), username);
116     g_debug ("Closing guest account %s with command '%s'", username, command);
117
118     result = run_script (command, NULL, &exit_status, &error);
119     g_free (command);
120
121     if (error)
122         g_warning ("Error running guest account cleanup script '%s': %s", get_setup_script (), error->message);
123     g_clear_error (&error);
124
125     if (result && exit_status != 0)
126         g_debug ("Guest account cleanup script returns %d", exit_status);
127 }