Back to index

lightdm  1.3.2
dmrc.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 <errno.h>
00013 #include <string.h>
00014 #include <unistd.h>
00015 
00016 #include "dmrc.h"
00017 #include "configuration.h"
00018 #include "accounts.h"
00019 #include "privileges.h"
00020 
00021 GKeyFile *
00022 dmrc_load (const gchar *username)
00023 {
00024     User *user;
00025     GKeyFile *dmrc_file;
00026     gchar *path;
00027     gboolean have_dmrc, drop_privileges;
00028 
00029     dmrc_file = g_key_file_new ();
00030 
00031     user = accounts_get_user_by_name (username);
00032     if (!user)
00033     {
00034         g_warning ("Cannot load .dmrc file, unable to get information on user %s", username);      
00035         return dmrc_file;
00036     }
00037 
00038     /* Load from the user directory, if this fails (e.g. the user directory
00039      * is not yet mounted) then load from the cache */
00040     path = g_build_filename (user_get_home_directory (user), ".dmrc", NULL);
00041 
00042     /* Guard against privilege escalation through symlinks, etc. */
00043     drop_privileges = geteuid () == 0;
00044     if (drop_privileges)
00045         privileges_drop (user);
00046     have_dmrc = g_key_file_load_from_file (dmrc_file, path, G_KEY_FILE_KEEP_COMMENTS, NULL);
00047     if (drop_privileges)
00048         privileges_reclaim ();
00049     g_free (path);
00050 
00051     /* If no ~/.dmrc, then load from the cache */  
00052     if (!have_dmrc)
00053     {
00054         gchar *filename, *cache_dir;
00055 
00056         filename = g_strdup_printf ("%s.dmrc", user_get_name (user));
00057         cache_dir = config_get_string (config_get_instance (), "LightDM", "cache-directory");
00058         path = g_build_filename (cache_dir, "dmrc", filename, NULL);
00059         g_free (filename);
00060         g_free (cache_dir);
00061 
00062         g_key_file_load_from_file (dmrc_file, path, G_KEY_FILE_KEEP_COMMENTS, NULL);
00063         g_free (path);
00064     }
00065 
00066     g_object_unref (user);
00067 
00068     return dmrc_file;
00069 }
00070 
00071 void
00072 dmrc_save (GKeyFile *dmrc_file, const gchar *username)
00073 {
00074     User *user;
00075     gchar *path, *filename, *cache_dir, *dmrc_cache_dir;
00076     gchar *data;
00077     gsize length;
00078 
00079     user = accounts_get_user_by_name (username);
00080     if (!user)
00081     {
00082         g_warning ("Not saving DMRC file - unable to get information on user %s", username);
00083         return;
00084     }
00085 
00086     data = g_key_file_to_data (dmrc_file, &length, NULL);
00087 
00088     /* Update the users .dmrc */
00089     if (user)
00090     {
00091         gboolean drop_privileges;
00092 
00093         path = g_build_filename (user_get_home_directory (user), ".dmrc", NULL);
00094 
00095         /* Guard against privilege escalation through symlinks, etc. */
00096         drop_privileges = geteuid () == 0;
00097         if (drop_privileges)
00098             privileges_drop (user);
00099         g_debug ("Writing %s", path);
00100         g_file_set_contents (path, data, length, NULL);
00101         if (drop_privileges)
00102             privileges_reclaim ();
00103 
00104         g_free (path);
00105     }
00106 
00107     /* Update the .dmrc cache */
00108     cache_dir = config_get_string (config_get_instance (), "LightDM", "cache-directory");
00109     dmrc_cache_dir = g_build_filename (cache_dir, "dmrc", NULL);
00110     g_mkdir_with_parents (dmrc_cache_dir, 0700);
00111 
00112     filename = g_strdup_printf ("%s.dmrc", username);
00113     path = g_build_filename (dmrc_cache_dir, filename, NULL);
00114     g_file_set_contents (path, data, length, NULL);
00115 
00116     g_free (dmrc_cache_dir);
00117     g_free (path);
00118     g_free (filename);
00119     g_object_unref (user);
00120 }