]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - common/dmrc.c
Don't show guest session dialog in MATE
[sojka/lightdm.git] / common / dmrc.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 <errno.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 #include "dmrc.h"
17 #include "configuration.h"
18 #include "privileges.h"
19 #include "user-list.h"
20
21 GKeyFile *
22 dmrc_load (CommonUser *user)
23 {
24     GKeyFile *dmrc_file;
25     gchar *path;
26     gboolean have_dmrc, drop_privileges;
27
28     dmrc_file = g_key_file_new ();
29
30     /* Load from the user directory, if this fails (e.g. the user directory
31      * is not yet mounted) then load from the cache */
32     path = g_build_filename (common_user_get_home_directory (user), ".dmrc", NULL);
33
34     /* Guard against privilege escalation through symlinks, etc. */
35     drop_privileges = geteuid () == 0;
36     if (drop_privileges)
37         privileges_drop (common_user_get_uid (user), common_user_get_gid (user));
38     have_dmrc = g_key_file_load_from_file (dmrc_file, path, G_KEY_FILE_KEEP_COMMENTS, NULL);
39     if (drop_privileges)
40         privileges_reclaim ();
41     g_free (path);
42
43     /* If no ~/.dmrc, then load from the cache */  
44     if (!have_dmrc)
45     {
46         gchar *filename, *cache_dir;
47
48         filename = g_strdup_printf ("%s.dmrc", common_user_get_name (user));
49         cache_dir = config_get_string (config_get_instance (), "LightDM", "cache-directory");
50         path = g_build_filename (cache_dir, "dmrc", filename, NULL);
51         g_free (filename);
52         g_free (cache_dir);
53
54         g_key_file_load_from_file (dmrc_file, path, G_KEY_FILE_KEEP_COMMENTS, NULL);
55         g_free (path);
56     }
57
58     return dmrc_file;
59 }
60
61 void
62 dmrc_save (GKeyFile *dmrc_file, CommonUser *user)
63 {
64     gchar *path, *filename, *cache_dir, *dmrc_cache_dir;
65     gchar *data;
66     gsize length;
67     gboolean drop_privileges;
68
69     data = g_key_file_to_data (dmrc_file, &length, NULL);
70
71     /* Update the users .dmrc */
72     path = g_build_filename (common_user_get_home_directory (user), ".dmrc", NULL);
73
74     /* Guard against privilege escalation through symlinks, etc. */
75     drop_privileges = geteuid () == 0;
76     if (drop_privileges)
77         privileges_drop (common_user_get_uid (user), common_user_get_gid (user));
78     g_debug ("Writing %s", path);
79     g_file_set_contents (path, data, length, NULL);
80     if (drop_privileges)
81         privileges_reclaim ();
82
83     g_free (path);
84
85     /* Update the .dmrc cache */
86     cache_dir = config_get_string (config_get_instance (), "LightDM", "cache-directory");
87     dmrc_cache_dir = g_build_filename (cache_dir, "dmrc", NULL);
88     g_free (cache_dir);
89     if (g_mkdir_with_parents (dmrc_cache_dir, 0700) < 0)
90         g_warning ("Failed to make DMRC cache directory %s: %s", dmrc_cache_dir, strerror (errno));
91
92     filename = g_strdup_printf ("%s.dmrc", common_user_get_name (user));
93     path = g_build_filename (dmrc_cache_dir, filename, NULL);
94     g_file_set_contents (path, data, length, NULL);
95
96     g_free (data);
97     g_free (path);
98     g_free (filename);
99     g_free (dmrc_cache_dir);
100 }