From 456eeb47ee9874693d18df121eecd2d16187bd06 Mon Sep 17 00:00:00 2001
From: Ryan Lortie <desrt@desrt.ca>
Date: Wed, 17 Apr 2013 08:48:12 +0200
Subject: [PATCH 1/3] User: hold on to our keyfile

When updating a User object from keyfile, keep reference on the keyfile
object passed in and modify the Daemon not to destroy it.

When saving a User, instead of creating a new keyfile, reuse the one we
stored on the object.

We still record the properties from the User object into the keyfile in
the previous way (and include a modification to clear out the 'User'
group before doing this).

The intention here is to allow other groups stored in the keyfile to be
preserved.  These other groups are likely to correspond to extension
interfaces (which will be introduced in future patches).

An alternative approach would have been to only preserve groups for
extensions that we currently have loaded but this was abandoned as being
excessively brittle since the result of a temporarily missing extension
file would be the destruction of data.

https://bugs.freedesktop.org/show_bug.cgi?id=63733
---
 src/daemon.c |  2 +-
 src/user.c   | 17 ++++++++++++-----
 2 files changed, 13 insertions(+), 6 deletions(-)

Index: accountsservice-0.6.34/src/daemon.c
===================================================================
--- accountsservice-0.6.34.orig/src/daemon.c	2013-06-26 20:02:05.659804880 +0200
+++ accountsservice-0.6.34/src/daemon.c	2013-06-26 20:02:14.000000000 +0200
@@ -493,7 +493,7 @@
                 key_file = g_key_file_new ();
                 if (g_key_file_load_from_file (key_file, filename, 0, NULL))
                         user_update_from_keyfile (user, key_file);
-                g_key_file_free (key_file);
+                g_key_file_unref (key_file);
                 g_free (filename);
         }
 
Index: accountsservice-0.6.34/src/user.c
===================================================================
--- accountsservice-0.6.34.orig/src/user.c	2013-06-26 20:02:05.659804880 +0200
+++ accountsservice-0.6.34/src/user.c	2013-06-26 20:02:14.000000000 +0200
@@ -87,6 +87,8 @@
 
         GHashTable *secret_exchanges;
 
+        GKeyFile     *keyfile;
+
         uid_t         uid;
         gid_t         gid;
         gchar        *user_name;
@@ -404,6 +406,9 @@
             }
         }
 
+        g_clear_pointer (&user->keyfile, g_key_file_unref);
+        user->keyfile = g_key_file_ref (keyfile);
+
         g_object_thaw_notify (G_OBJECT (user));
 }
 
@@ -431,6 +436,8 @@
 user_save_to_keyfile (User     *user,
                       GKeyFile *keyfile)
 {
+        g_key_file_remove_group (keyfile, "User", NULL);
+
         if (user->email)
                 g_key_file_set_string (keyfile, "User", "Email", user->email);
 
@@ -465,15 +472,13 @@
 save_extra_data (User *user)
 {
         gchar *filename;
-        GKeyFile *keyfile;
         gchar *data;
         GError *error;
 
-        keyfile = g_key_file_new ();
-        user_save_to_keyfile (user, keyfile);
+        user_save_to_keyfile (user, user->keyfile);
 
         error = NULL;
-        data = g_key_file_to_data (keyfile, NULL, &error);
+        data = g_key_file_to_data (user->keyfile, NULL, &error);
         if (error == NULL) {
                 filename = g_build_filename (USERDIR,
                                              user->user_name,
@@ -486,7 +491,6 @@
                            user->user_name, error->message);
                 g_error_free (error);
         }
-        g_key_file_free (keyfile);
 }
 
 static void
@@ -2708,6 +2712,8 @@
 
         g_hash_table_unref (user->secret_exchanges);
 
+        g_clear_pointer (&user->keyfile, g_key_file_unref);
+
         g_free (user->object_path);
         g_free (user->user_name);
         g_free (user->real_name);
@@ -2961,4 +2967,5 @@
         user->automatic_login = FALSE;
         user->system_account = FALSE;
         user->login_history = NULL;
+        user->keyfile = g_key_file_new ();
 }
