2 * Copyright (C) 2010-2011 Robert Ancell.
3 * Author: Robert Ancell <robert.ancell@canonical.com>
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
17 #include "configuration.h"
18 #include "privileges.h"
19 #include "user-list.h"
22 dmrc_load (CommonUser *user)
26 gboolean have_dmrc, drop_privileges;
28 dmrc_file = g_key_file_new ();
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);
34 /* Guard against privilege escalation through symlinks, etc. */
35 drop_privileges = geteuid () == 0;
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);
40 privileges_reclaim ();
43 /* If no ~/.dmrc, then load from the cache */
46 gchar *filename, *cache_dir;
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);
54 g_key_file_load_from_file (dmrc_file, path, G_KEY_FILE_KEEP_COMMENTS, NULL);
62 dmrc_save (GKeyFile *dmrc_file, CommonUser *user)
64 gchar *path, *filename, *cache_dir, *dmrc_cache_dir;
67 gboolean drop_privileges;
69 data = g_key_file_to_data (dmrc_file, &length, NULL);
71 /* Update the users .dmrc */
72 path = g_build_filename (common_user_get_home_directory (user), ".dmrc", NULL);
74 /* Guard against privilege escalation through symlinks, etc. */
75 drop_privileges = geteuid () == 0;
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);
81 privileges_reclaim ();
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);
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));
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);
99 g_free (dmrc_cache_dir);