Blame SOURCES/0001-user-Introduce-user-templates-for-setting-default-se.patch

285da9
From 72427bd4fcae931298c670093f9cbd34ad58f59a Mon Sep 17 00:00:00 2001
285da9
From: Ray Strode <rstrode@redhat.com>
285da9
Date: Wed, 4 Aug 2021 19:54:59 -0400
285da9
Subject: [PATCH] user: Introduce user templates for setting default session
285da9
 etc
285da9
285da9
At the moment there's no easy way to set a default session, or
285da9
face icon or whatever for all users.  If a user has never logged in
285da9
before, we just generate their cache file from hardcoded defaults.
285da9
285da9
This commit introduces a template system to make it possible for
285da9
admins to set up defaults on their own.
285da9
285da9
Admins can write either
285da9
/etc/accountsservice/user-templates/administrator
285da9
or
285da9
/etc/accountsservice/user-templates/standard
285da9
285da9
files.  These files follow the same format as
285da9
285da9
/var/lib/AccountsService/users/username
285da9
285da9
files, but will support substituting $HOME and $USER to the appropriate
285da9
user specific values.
285da9
285da9
User templates also support an additional group [Template] that
285da9
have an additional key EnvironmentFiles that specify a list
285da9
of environment files to load (files with KEY=VALUE pairs in them).
285da9
Any keys listed in those environment files will also get substituted.
285da9
---
285da9
 data/administrator |   6 +
285da9
 data/meson.build   |  10 ++
285da9
 data/standard      |   6 +
285da9
 src/daemon.c       |   8 +-
285da9
 src/meson.build    |   1 +
285da9
 src/user.c         | 284 ++++++++++++++++++++++++++++++++++++++++++++-
285da9
 src/user.h         |   3 +-
285da9
 7 files changed, 305 insertions(+), 13 deletions(-)
285da9
 create mode 100644 data/administrator
285da9
 create mode 100644 data/standard
285da9
285da9
diff --git a/data/administrator b/data/administrator
285da9
new file mode 100644
285da9
index 0000000..ea043c9
285da9
--- /dev/null
285da9
+++ b/data/administrator
285da9
@@ -0,0 +1,6 @@
285da9
+[Template]
285da9
+#EnvironmentFiles=/etc/os-release;
285da9
+
285da9
+[User]
285da9
+Session=
285da9
+Icon=${HOME}/.face
285da9
diff --git a/data/meson.build b/data/meson.build
285da9
index 2dc57c2..7d9bdcd 100644
285da9
--- a/data/meson.build
285da9
+++ b/data/meson.build
285da9
@@ -22,30 +22,40 @@ service = act_namespace + '.service'
285da9
 configure_file(
285da9
   input: service + '.in',
285da9
   output: service,
285da9
   configuration: service_conf,
285da9
   install: true,
285da9
   install_dir: dbus_sys_dir,
285da9
 )
285da9
 
285da9
 policy = act_namespace.to_lower() + '.policy'
285da9
 
285da9
 i18n.merge_file(
285da9
   policy,
285da9
   input: policy + '.in',
285da9
   output: policy,
285da9
   po_dir: po_dir,
285da9
   install: true,
285da9
   install_dir: policy_dir,
285da9
 )
285da9
 
285da9
 if install_systemd_unit_dir
285da9
   service = 'accounts-daemon.service'
285da9
 
285da9
   configure_file(
285da9
     input: service + '.in',
285da9
     output: service,
285da9
     configuration: service_conf,
285da9
     install: true,
285da9
     install_dir: systemd_system_unit_dir,
285da9
   )
285da9
 endif
285da9
+
285da9
+install_data(
285da9
+  'administrator',
285da9
+  install_dir: join_paths(act_datadir, 'accountsservice', 'user-templates'),
285da9
+)
285da9
+
285da9
+install_data(
285da9
+  'standard',
285da9
+  install_dir: join_paths(act_datadir, 'accountsservice', 'user-templates'),
285da9
+)
285da9
diff --git a/data/standard b/data/standard
285da9
new file mode 100644
285da9
index 0000000..ea043c9
285da9
--- /dev/null
285da9
+++ b/data/standard
285da9
@@ -0,0 +1,6 @@
285da9
+[Template]
285da9
+#EnvironmentFiles=/etc/os-release;
285da9
+
285da9
+[User]
285da9
+Session=
285da9
+Icon=${HOME}/.face
285da9
diff --git a/src/daemon.c b/src/daemon.c
285da9
index 5ce0216..66ac7ba 100644
285da9
--- a/src/daemon.c
285da9
+++ b/src/daemon.c
285da9
@@ -298,69 +298,63 @@ entry_generator_cachedir (Daemon       *daemon,
285da9
                         break;
285da9
 
285da9
                 /* Only load files in this directory */
285da9
                 filename = g_build_filename (USERDIR, name, NULL);
285da9
                 regular = g_file_test (filename, G_FILE_TEST_IS_REGULAR);
285da9
 
285da9
                 if (regular) {
285da9
                         errno = 0;
285da9
                         pwent = getpwnam (name);
285da9
                         if (pwent != NULL) {
285da9
                                 *shadow_entry = getspnam (pwent->pw_name);
285da9
 
285da9
                                 return pwent;
285da9
                         } else if (errno == 0) {
285da9
                                 g_debug ("user '%s' in cache dir but not present on system, removing", name);
285da9
                                 remove_cache_files (name);
285da9
                         }
285da9
                         else {
285da9
                                 g_warning ("failed to check if user '%s' in cache dir is present on system: %s",
285da9
                                   name, g_strerror (errno));
285da9
                         }
285da9
                 }
285da9
         }
285da9
 
285da9
         /* Last iteration */
285da9
         g_dir_close (dir);
285da9
 
285da9
         /* Update all the users from the files in the cache dir */
285da9
         g_hash_table_iter_init (&iter, users);
285da9
         while (g_hash_table_iter_next (&iter, &key, &value)) {
285da9
-                const gchar *name = key;
285da9
                 User *user = value;
285da9
-                g_autofree gchar *filename = NULL;
285da9
-                g_autoptr(GKeyFile) key_file = NULL;
285da9
 
285da9
-                filename = g_build_filename (USERDIR, name, NULL);
285da9
-                key_file = g_key_file_new ();
285da9
-                if (g_key_file_load_from_file (key_file, filename, 0, NULL))
285da9
-                        user_update_from_keyfile (user, key_file);
285da9
+                user_update_from_cache (user);
285da9
         }
285da9
 
285da9
         *state = NULL;
285da9
         return NULL;
285da9
 }
285da9
 
285da9
 static struct passwd *
285da9
 entry_generator_requested_users (Daemon       *daemon,
285da9
                                  GHashTable   *users,
285da9
                                  gpointer     *state,
285da9
                                  struct spwd **shadow_entry)
285da9
 {
285da9
         DaemonPrivate *priv = daemon_get_instance_private (daemon);
285da9
         struct passwd *pwent;
285da9
         GList *node;
285da9
 
285da9
         /* First iteration */
285da9
         if (*state == NULL) {
285da9
                 *state = priv->explicitly_requested_users;
285da9
         }
285da9
 
285da9
         /* Every iteration */
285da9
 
285da9
         if (g_hash_table_size (users) < MAX_LOCAL_USERS) {
285da9
                 node = *state;
285da9
                 while (node != NULL) {
285da9
                         const char *name;
285da9
 
285da9
                         name = node->data;
285da9
                         node = node->next;
285da9
diff --git a/src/meson.build b/src/meson.build
285da9
index 3970749..d3b0cb9 100644
285da9
--- a/src/meson.build
285da9
+++ b/src/meson.build
285da9
@@ -1,59 +1,60 @@
285da9
 sources = []
285da9
 
285da9
 gdbus_headers = []
285da9
 
285da9
 ifaces = [
285da9
   ['accounts-generated', 'org.freedesktop.', 'Accounts'],
285da9
   ['accounts-user-generated', act_namespace + '.', 'User'],
285da9
   ['realmd-generated', 'org.freedesktop.', 'realmd'],
285da9
 ]
285da9
 
285da9
 foreach iface: ifaces
285da9
   gdbus_sources = gnome.gdbus_codegen(
285da9
     iface[0],
285da9
     join_paths(data_dir, iface[1] + iface[2] + '.xml'),
285da9
     interface_prefix: iface[1],
285da9
     namespace: 'Accounts',
285da9
   )
285da9
   sources += gdbus_sources
285da9
   gdbus_headers += gdbus_sources[1]
285da9
 endforeach
285da9
 
285da9
 deps = [
285da9
   gio_dep,
285da9
   gio_unix_dep,
285da9
 ]
285da9
 
285da9
 cflags = [
285da9
   '-DLOCALSTATEDIR="@0@"'.format(act_localstatedir),
285da9
   '-DDATADIR="@0@"'.format(act_datadir),
285da9
+  '-DSYSCONFDIR="@0@"'.format(act_sysconfdir),
285da9
   '-DICONDIR="@0@"'.format(join_paths(act_localstatedir, 'lib', 'AccountsService', 'icons')),
285da9
   '-DUSERDIR="@0@"'.format(join_paths(act_localstatedir, 'lib', 'AccountsService', 'users')),
285da9
 ]
285da9
 
285da9
 libaccounts_generated = static_library(
285da9
   'accounts-generated',
285da9
   sources: sources,
285da9
   include_directories: top_inc,
285da9
   dependencies: deps,
285da9
   c_args: cflags,
285da9
 )
285da9
 
285da9
 libaccounts_generated_dep = declare_dependency(
285da9
   sources: gdbus_headers,
285da9
   include_directories: include_directories('.'),
285da9
   dependencies: gio_dep,
285da9
   link_with: libaccounts_generated,
285da9
 )
285da9
 
285da9
 sources = files(
285da9
   'daemon.c',
285da9
   'extensions.c',
285da9
   'main.c',
285da9
   'user.c',
285da9
   'user-classify.c',
285da9
   'util.c',
285da9
   'wtmp-helper.c',
285da9
 )
285da9
 
285da9
 deps = [
285da9
diff --git a/src/user.c b/src/user.c
285da9
index 9f57af5..16c7721 100644
285da9
--- a/src/user.c
285da9
+++ b/src/user.c
285da9
@@ -43,127 +43,384 @@
285da9
 #include <polkit/polkit.h>
285da9
 
285da9
 #include "user-classify.h"
285da9
 #include "daemon.h"
285da9
 #include "user.h"
285da9
 #include "accounts-user-generated.h"
285da9
 #include "util.h"
285da9
 
285da9
 struct User {
285da9
         AccountsUserSkeleton parent;
285da9
 
285da9
         GDBusConnection *system_bus_connection;
285da9
         gchar *object_path;
285da9
 
285da9
         Daemon       *daemon;
285da9
 
285da9
         GKeyFile     *keyfile;
285da9
 
285da9
         gid_t         gid;
285da9
         gint64        expiration_time;
285da9
         gint64        last_change_time;
285da9
         gint64        min_days_between_changes;
285da9
         gint64        max_days_between_changes;
285da9
         gint64        days_to_warn;
285da9
         gint64        days_after_expiration_until_lock;
285da9
         GVariant     *login_history;
285da9
         gchar        *icon_file;
285da9
         gchar        *default_icon_file;
285da9
         gboolean      account_expiration_policy_known;
285da9
         gboolean      cached;
285da9
+        gboolean      template_loaded;
285da9
 
285da9
         guint        *extension_ids;
285da9
         guint         n_extension_ids;
285da9
 
285da9
         guint         changed_timeout_id;
285da9
 };
285da9
 
285da9
 typedef struct UserClass
285da9
 {
285da9
         AccountsUserSkeletonClass parent_class;
285da9
 } UserClass;
285da9
 
285da9
 static void user_accounts_user_iface_init (AccountsUserIface *iface);
285da9
+static void user_update_from_keyfile (User *user, GKeyFile *keyfile);
285da9
 
285da9
 G_DEFINE_TYPE_WITH_CODE (User, user, ACCOUNTS_TYPE_USER_SKELETON, G_IMPLEMENT_INTERFACE (ACCOUNTS_TYPE_USER, user_accounts_user_iface_init));
285da9
 
285da9
 static gint
285da9
 account_type_from_pwent (struct passwd *pwent)
285da9
 {
285da9
         struct group *grp;
285da9
         gint i;
285da9
 
285da9
         if (pwent->pw_uid == 0) {
285da9
                 g_debug ("user is root so account type is administrator");
285da9
                 return ACCOUNT_TYPE_ADMINISTRATOR;
285da9
         }
285da9
 
285da9
         grp = getgrnam (ADMIN_GROUP);
285da9
         if (grp == NULL) {
285da9
                 g_debug (ADMIN_GROUP " group not found");
285da9
                 return ACCOUNT_TYPE_STANDARD;
285da9
         }
285da9
 
285da9
         for (i = 0; grp->gr_mem[i] != NULL; i++) {
285da9
                 if (g_strcmp0 (grp->gr_mem[i], pwent->pw_name) == 0) {
285da9
                         return ACCOUNT_TYPE_ADMINISTRATOR;
285da9
                 }
285da9
         }
285da9
 
285da9
         return ACCOUNT_TYPE_STANDARD;
285da9
 }
285da9
 
285da9
 static void
285da9
 user_reset_icon_file (User *user)
285da9
 {
285da9
         const char *icon_file;
285da9
         gboolean    icon_is_default;
285da9
         const char *home_dir;
285da9
 
285da9
         icon_file = accounts_user_get_icon_file (ACCOUNTS_USER (user));
285da9
 
285da9
         if (icon_file == NULL || g_strcmp0 (icon_file, user->default_icon_file) == 0) {
285da9
                 icon_is_default = TRUE;
285da9
         } else {
285da9
                 icon_is_default = FALSE;
285da9
         }
285da9
 
285da9
         g_free (user->default_icon_file);
285da9
         home_dir = accounts_user_get_home_directory (ACCOUNTS_USER (user));
285da9
 
285da9
         user->default_icon_file = g_build_filename (home_dir, ".face", NULL);
285da9
 
285da9
         if (icon_is_default) {
285da9
                 accounts_user_set_icon_file (ACCOUNTS_USER (user), user->default_icon_file);
285da9
         }
285da9
 }
285da9
 
285da9
+static gboolean
285da9
+user_has_cache_file (User *user)
285da9
+{
285da9
+        g_autofree char *filename = NULL;
285da9
+
285da9
+        filename = g_build_filename (USERDIR, user_get_user_name (user), NULL);
285da9
+
285da9
+        return g_file_test (filename, G_FILE_TEST_EXISTS);
285da9
+}
285da9
+
285da9
+static gboolean
285da9
+is_valid_shell_identifier_character (char     c,
285da9
+                                     gboolean first)
285da9
+{
285da9
+        return (!first && g_ascii_isdigit (c)) ||
285da9
+                c == '_' ||
285da9
+                g_ascii_isalpha (c);
285da9
+}
285da9
+
285da9
+static char *
285da9
+expand_template_variables (User       *user,
285da9
+                           GHashTable *template_variables,
285da9
+                           const char *str)
285da9
+{
285da9
+        GString *s = g_string_new ("");
285da9
+        const char *p, *start;
285da9
+        char c;
285da9
+
285da9
+        p = str;
285da9
+        while (*p) {
285da9
+                c = *p;
285da9
+                if (c == '\\') {
285da9
+                        p++;
285da9
+                        c = *p;
285da9
+                        if (c != '\0') {
285da9
+                                p++;
285da9
+                                switch (c) {
285da9
+                                case '\\':
285da9
+                                        g_string_append_c (s, '\\');
285da9
+                                        break;
285da9
+                                case '$':
285da9
+                                        g_string_append_c (s, '$');
285da9
+                                        break;
285da9
+                                default:
285da9
+                                        g_string_append_c (s, '\\');
285da9
+                                        g_string_append_c (s, c);
285da9
+                                        break;
285da9
+                                }
285da9
+                        }
285da9
+                } else if (c == '$') {
285da9
+                        gboolean brackets = FALSE;
285da9
+                        p++;
285da9
+                        if (*p == '{') {
285da9
+                                brackets = TRUE;
285da9
+                                p++;
285da9
+                        }
285da9
+                        start = p;
285da9
+                        while (*p != '\0' &&
285da9
+                               is_valid_shell_identifier_character (*p, p == start))
285da9
+                                p++;
285da9
+                        if (p == start || (brackets && *p != '}')) {
285da9
+                                g_string_append_c (s, '$');
285da9
+                                if (brackets)
285da9
+                                        g_string_append_c (s, '{');
285da9
+                                g_string_append_len (s, start, p - start);
285da9
+                        } else {
285da9
+                                g_autofree char *variable = NULL;
285da9
+                                const char *value;
285da9
+
285da9
+                                if (brackets && *p == '}')
285da9
+                                        p++;
285da9
+
285da9
+                                variable = g_strndup (start, p - start - 1);
285da9
+
285da9
+                                value = g_hash_table_lookup (template_variables, variable);
285da9
+                                if (value) {
285da9
+                                        g_string_append (s, value);
285da9
+                                }
285da9
+                        }
285da9
+                } else {
285da9
+                        p++;
285da9
+                        g_string_append_c (s, c);
285da9
+                }
285da9
+        }
285da9
+        return g_string_free (s, FALSE);
285da9
+}
285da9
+
285da9
+static void
285da9
+load_template_environment_file (User       *user,
285da9
+                                GHashTable *variables,
285da9
+                                const char *file)
285da9
+{
285da9
+        g_autofree char *contents = NULL;
285da9
+        g_auto (GStrv) lines = NULL;
285da9
+        g_autoptr (GError) error = NULL;
285da9
+        gboolean file_loaded;
285da9
+        size_t i;
285da9
+
285da9
+        file_loaded = g_file_get_contents (file, &contents, NULL, &error);
285da9
+
285da9
+        if (!file_loaded) {
285da9
+                g_debug ("Couldn't load template environment file %s: %s",
285da9
+                         file, error->message);
285da9
+                return;
285da9
+        }
285da9
+
285da9
+        lines = g_strsplit (contents, "\n", -1);
285da9
+
285da9
+        for (i = 0; lines[i] != NULL; i++) {
285da9
+                char *p;
285da9
+                char *variable_end;
285da9
+                const char *variable;
285da9
+                const char *value;
285da9
+
285da9
+                p = lines[i];
285da9
+                while (g_ascii_isspace (*p))
285da9
+                        p++;
285da9
+                if (*p == '#' || *p == '\0')
285da9
+                        continue;
285da9
+                variable = p;
285da9
+                while (is_valid_shell_identifier_character (*p, p == variable))
285da9
+                        p++;
285da9
+                variable_end = p;
285da9
+                while (g_ascii_isspace (*p))
285da9
+                        p++;
285da9
+                if (variable_end == variable || *p != '=') {
285da9
+                        g_debug ("template environment file %s has invalid line '%s'\n", file, lines[i]);
285da9
+                        continue;
285da9
+                }
285da9
+                *variable_end = '\0';
285da9
+                p++;
285da9
+                while (g_ascii_isspace (*p))
285da9
+                        p++;
285da9
+                value = p;
285da9
+
285da9
+                if (g_hash_table_lookup (variables, variable) == NULL) {
285da9
+                        g_hash_table_insert (variables,
285da9
+                                             g_strdup (variable),
285da9
+                                             g_strdup (value));
285da9
+                }
285da9
+
285da9
+        }
285da9
+}
285da9
+
285da9
+static void
285da9
+initialize_template_environment (User               *user,
285da9
+                                 GHashTable         *variables,
285da9
+                                 const char * const *files)
285da9
+{
285da9
+        size_t i;
285da9
+
285da9
+        g_hash_table_insert (variables, g_strdup ("HOME"), g_strdup (accounts_user_get_home_directory (ACCOUNTS_USER (user))));
285da9
+        g_hash_table_insert (variables, g_strdup ("USER"), g_strdup (user_get_user_name (user)));
285da9
+
285da9
+        if (files == NULL)
285da9
+                return;
285da9
+
285da9
+        for (i = 0; files[i] != NULL; i++) {
285da9
+                load_template_environment_file (user, variables, files[i]);
285da9
+        }
285da9
+}
285da9
+
285da9
+static void
285da9
+user_update_from_template (User *user)
285da9
+{
285da9
+        g_autofree char *filename = NULL;
285da9
+        g_autoptr (GKeyFile) key_file = NULL;
285da9
+        g_autoptr (GError) error = NULL;
285da9
+        g_autoptr (GHashTable) template_variables = NULL;
285da9
+        g_auto (GStrv) template_environment_files = NULL;
285da9
+        gboolean key_file_loaded = FALSE;
285da9
+        const char * const *system_dirs[] = {
285da9
+                (const char *[]) { "/run", SYSCONFDIR, NULL },
285da9
+                g_get_system_data_dirs (),
285da9
+                NULL
285da9
+        };
285da9
+        g_autoptr (GPtrArray) dirs = NULL;
285da9
+        AccountType account_type;
285da9
+        const char *account_type_string;
285da9
+        size_t i, j;
285da9
+        g_autofree char *contents = NULL;
285da9
+        g_autofree char *expanded = NULL;
285da9
+        g_auto (GStrv) lines = NULL;
285da9
+
285da9
+        if (user->template_loaded)
285da9
+                return;
285da9
+
285da9
+        filename = g_build_filename (USERDIR,
285da9
+                                     accounts_user_get_user_name (ACCOUNTS_USER (user)),
285da9
+                                     NULL);
285da9
+
285da9
+        account_type = accounts_user_get_account_type (ACCOUNTS_USER (user));
285da9
+        if (account_type == ACCOUNT_TYPE_ADMINISTRATOR)
285da9
+                account_type_string = "administrator";
285da9
+        else
285da9
+                account_type_string = "standard";
285da9
+
285da9
+        dirs = g_ptr_array_new ();
285da9
+        for (i = 0; system_dirs[i] != NULL; i++) {
285da9
+                for (j = 0; system_dirs[i][j] != NULL; j++) {
285da9
+                        char *dir;
285da9
+
285da9
+                        dir = g_build_filename (system_dirs[i][j],
285da9
+                                                "accountsservice",
285da9
+                                                "user-templates",
285da9
+                                                NULL);
285da9
+                        g_ptr_array_add (dirs, dir);
285da9
+                }
285da9
+        }
285da9
+        g_ptr_array_add (dirs, NULL);
285da9
+
285da9
+        key_file = g_key_file_new ();
285da9
+        key_file_loaded = g_key_file_load_from_dirs (key_file,
285da9
+                                                     account_type_string,
285da9
+                                                     (const char **) dirs->pdata,
285da9
+                                                     NULL,
285da9
+                                                     G_KEY_FILE_KEEP_COMMENTS,
285da9
+                                                     &error);
285da9
+
285da9
+        if (!key_file_loaded) {
285da9
+                g_debug ("failed to load user template: %s", error->message);
285da9
+                return;
285da9
+        }
285da9
+
285da9
+        template_variables = g_hash_table_new_full (g_str_hash,
285da9
+                                                    g_str_equal,
285da9
+                                                    g_free,
285da9
+                                                    g_free);
285da9
+
285da9
+        template_environment_files = g_key_file_get_string_list (key_file,
285da9
+                                                                 "Template",
285da9
+                                                                 "EnvironmentFiles",
285da9
+                                                                 NULL,
285da9
+                                                                 NULL);
285da9
+
285da9
+        initialize_template_environment (user, template_variables, (const char * const *) template_environment_files);
285da9
+
285da9
+        g_key_file_remove_group (key_file, "Template", NULL);
285da9
+        contents = g_key_file_to_data (key_file, NULL, NULL);
285da9
+        lines = g_strsplit (contents, "\n", -1);
285da9
+
285da9
+        expanded = expand_template_variables (user, template_variables, contents);
285da9
+
285da9
+        key_file_loaded = g_key_file_load_from_data (key_file,
285da9
+                                                     expanded,
285da9
+                                                     strlen (expanded),
285da9
+                                                     G_KEY_FILE_KEEP_COMMENTS,
285da9
+                                                     &error);
285da9
+
285da9
+        if (key_file_loaded)
285da9
+                user_update_from_keyfile (user, key_file);
285da9
+
285da9
+        user->template_loaded = key_file_loaded;
285da9
+}
285da9
+
285da9
 void
285da9
 user_update_from_pwent (User          *user,
285da9
                         struct passwd *pwent,
285da9
                         struct spwd   *spent)
285da9
 {
285da9
         g_autofree gchar *real_name = NULL;
285da9
         gboolean is_system_account;
285da9
         const gchar *passwd;
285da9
         gboolean locked;
285da9
         PasswordMode mode;
285da9
         AccountType account_type;
285da9
 
285da9
         g_object_freeze_notify (G_OBJECT (user));
285da9
 
285da9
         if (pwent->pw_gecos && pwent->pw_gecos[0] != '\0') {
285da9
                 gchar *first_comma = NULL;
285da9
                 gchar *valid_utf8_name = NULL;
285da9
 
285da9
                 if (g_utf8_validate (pwent->pw_gecos, -1, NULL)) {
285da9
                         valid_utf8_name = pwent->pw_gecos;
285da9
                         first_comma = g_utf8_strchr (valid_utf8_name, -1, ',');
285da9
                 }
285da9
                 else {
285da9
                         g_warning ("User %s has invalid UTF-8 in GECOS field. "
285da9
                                    "It would be a good thing to check /etc/passwd.",
285da9
                                    pwent->pw_name ? pwent->pw_name : "");
285da9
                 }
285da9
 
285da9
                 if (first_comma) {
285da9
                         real_name = g_strndup (valid_utf8_name,
285da9
@@ -212,134 +469,150 @@ user_update_from_pwent (User          *user,
285da9
         accounts_user_set_locked (ACCOUNTS_USER (user), locked);
285da9
 
285da9
         if (passwd == NULL || passwd[0] != 0) {
285da9
                 mode = PASSWORD_MODE_REGULAR;
285da9
         }
285da9
         else {
285da9
                 mode = PASSWORD_MODE_NONE;
285da9
         }
285da9
 
285da9
         if (spent) {
285da9
                 if (spent->sp_lstchg == 0) {
285da9
                         mode = PASSWORD_MODE_SET_AT_LOGIN;
285da9
                 }
285da9
 
285da9
                 user->expiration_time = spent->sp_expire;
285da9
                 user->last_change_time  = spent->sp_lstchg;
285da9
                 user->min_days_between_changes = spent->sp_min;
285da9
                 user->max_days_between_changes = spent->sp_max;
285da9
                 user->days_to_warn  = spent->sp_warn;
285da9
                 user->days_after_expiration_until_lock = spent->sp_inact;
285da9
                 user->account_expiration_policy_known = TRUE;
285da9
         }
285da9
 
285da9
         accounts_user_set_password_mode (ACCOUNTS_USER (user), mode);
285da9
         is_system_account = !user_classify_is_human (accounts_user_get_uid (ACCOUNTS_USER (user)),
285da9
                                                      accounts_user_get_user_name (ACCOUNTS_USER (user)),
285da9
                                                      accounts_user_get_shell (ACCOUNTS_USER (user)),
285da9
                                                      passwd);
285da9
         accounts_user_set_system_account (ACCOUNTS_USER (user), is_system_account);
285da9
 
285da9
+        if (!user_has_cache_file (user))
285da9
+                user_update_from_template (user);
285da9
         g_object_thaw_notify (G_OBJECT (user));
285da9
 }
285da9
 
285da9
-void
285da9
+static void
285da9
 user_update_from_keyfile (User     *user,
285da9
                           GKeyFile *keyfile)
285da9
 {
285da9
         gchar *s;
285da9
 
285da9
-        g_object_freeze_notify (G_OBJECT (user));
285da9
-
285da9
         s = g_key_file_get_string (keyfile, "User", "Language", NULL);
285da9
         if (s != NULL) {
285da9
                 accounts_user_set_language (ACCOUNTS_USER (user), s);
285da9
                 g_clear_pointer (&s, g_free);
285da9
         }
285da9
 
285da9
         s = g_key_file_get_string (keyfile, "User", "XSession", NULL);
285da9
         if (s != NULL) {
285da9
                 accounts_user_set_xsession (ACCOUNTS_USER (user), s);
285da9
 
285da9
                 /* for backward compat */
285da9
                 accounts_user_set_session (ACCOUNTS_USER (user), s);
285da9
                 g_clear_pointer (&s, g_free);
285da9
         }
285da9
 
285da9
         s = g_key_file_get_string (keyfile, "User", "Session", NULL);
285da9
         if (s != NULL) {
285da9
                 accounts_user_set_session (ACCOUNTS_USER (user), s);
285da9
                 g_clear_pointer (&s, g_free);
285da9
         }
285da9
 
285da9
         s = g_key_file_get_string (keyfile, "User", "SessionType", NULL);
285da9
         if (s != NULL) {
285da9
                 accounts_user_set_session_type (ACCOUNTS_USER (user), s);
285da9
                 g_clear_pointer (&s, g_free);
285da9
         }
285da9
 
285da9
         s = g_key_file_get_string (keyfile, "User", "Email", NULL);
285da9
         if (s != NULL) {
285da9
                 accounts_user_set_email (ACCOUNTS_USER (user), s);
285da9
                 g_clear_pointer (&s, g_free);
285da9
         }
285da9
 
285da9
         s = g_key_file_get_string (keyfile, "User", "Location", NULL);
285da9
         if (s != NULL) {
285da9
                 accounts_user_set_location (ACCOUNTS_USER (user), s);
285da9
                 g_clear_pointer (&s, g_free);
285da9
         }
285da9
 
285da9
         s = g_key_file_get_string (keyfile, "User", "PasswordHint", NULL);
285da9
         if (s != NULL) {
285da9
                 accounts_user_set_password_hint (ACCOUNTS_USER (user), s);
285da9
                 g_clear_pointer (&s, g_free);
285da9
         }
285da9
 
285da9
         s = g_key_file_get_string (keyfile, "User", "Icon", NULL);
285da9
         if (s != NULL) {
285da9
                 accounts_user_set_icon_file (ACCOUNTS_USER (user), s);
285da9
                 g_clear_pointer (&s, g_free);
285da9
         }
285da9
 
285da9
         if (g_key_file_has_key (keyfile, "User", "SystemAccount", NULL)) {
285da9
             gboolean system_account;
285da9
 
285da9
             system_account = g_key_file_get_boolean (keyfile, "User", "SystemAccount", NULL);
285da9
             accounts_user_set_system_account (ACCOUNTS_USER (user), system_account);
285da9
         }
285da9
 
285da9
         g_clear_pointer (&user->keyfile, g_key_file_unref);
285da9
         user->keyfile = g_key_file_ref (keyfile);
285da9
+}
285da9
+
285da9
+void
285da9
+user_update_from_cache (User *user)
285da9
+{
285da9
+        g_autofree gchar *filename = NULL;
285da9
+        g_autoptr(GKeyFile) key_file = NULL;
285da9
+
285da9
+        filename = g_build_filename (USERDIR, accounts_user_get_user_name (ACCOUNTS_USER (user)), NULL);
285da9
+
285da9
+        key_file = g_key_file_new ();
285da9
+
285da9
+        if (!g_key_file_load_from_file (key_file, filename, 0, NULL))
285da9
+                return;
285da9
+
285da9
+        g_object_freeze_notify (G_OBJECT (user));
285da9
+        user_update_from_keyfile (user, key_file);
285da9
         user_set_cached (user, TRUE);
285da9
         user_set_saved (user, TRUE);
285da9
-
285da9
         g_object_thaw_notify (G_OBJECT (user));
285da9
 }
285da9
 
285da9
 void
285da9
 user_update_local_account_property (User          *user,
285da9
                                     gboolean       local)
285da9
 {
285da9
         accounts_user_set_local_account (ACCOUNTS_USER (user), local);
285da9
 }
285da9
 
285da9
 void
285da9
 user_update_system_account_property (User          *user,
285da9
                                      gboolean       system)
285da9
 {
285da9
         accounts_user_set_system_account (ACCOUNTS_USER (user), system);
285da9
 }
285da9
 
285da9
 static void
285da9
 user_save_to_keyfile (User     *user,
285da9
                       GKeyFile *keyfile)
285da9
 {
285da9
         g_key_file_remove_group (keyfile, "User", NULL);
285da9
 
285da9
         if (accounts_user_get_email (ACCOUNTS_USER (user)))
285da9
                 g_key_file_set_string (keyfile, "User", "Email", accounts_user_get_email (ACCOUNTS_USER (user)));
285da9
 
285da9
         if (accounts_user_get_language (ACCOUNTS_USER (user)))
285da9
                 g_key_file_set_string (keyfile, "User", "Language", accounts_user_get_language (ACCOUNTS_USER (user)));
285da9
 
285da9
         if (accounts_user_get_session (ACCOUNTS_USER (user)))
285da9
@@ -509,60 +782,63 @@ user_extension_set_property (User                  *user,
285da9
         if (!prev || !g_str_equal (printed, prev)) {
285da9
                 g_key_file_set_value (user->keyfile, interface->name, property->name, printed);
285da9
 
285da9
                 /* Emit a change signal.  Use invalidation
285da9
                  * because the data may not be world-readable.
285da9
                  */
285da9
                 g_dbus_connection_emit_signal (g_dbus_method_invocation_get_connection (invocation),
285da9
                                                NULL, /* destination_bus_name */
285da9
                                                g_dbus_method_invocation_get_object_path (invocation),
285da9
                                                "org.freedesktop.DBus.Properties", "PropertiesChanged",
285da9
                                                g_variant_new_parsed ("( %s, %a{sv}, [ %s ] )",
285da9
                                                                      interface->name, NULL, property->name),
285da9
                                                NULL);
285da9
 
285da9
                 accounts_user_emit_changed (ACCOUNTS_USER (user));
285da9
                 save_extra_data (user);
285da9
         }
285da9
 
285da9
         g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
285da9
 }
285da9
 
285da9
 static void
285da9
 user_extension_authentication_done (Daemon                *daemon,
285da9
                                     User                  *user,
285da9
                                     GDBusMethodInvocation *invocation,
285da9
                                     gpointer               user_data)
285da9
 {
285da9
         GDBusInterfaceInfo *interface = user_data;
285da9
         const gchar *method_name;
285da9
 
285da9
+        if (!user_has_cache_file (user))
285da9
+                user_update_from_template (user);
285da9
+
285da9
         method_name = g_dbus_method_invocation_get_method_name (invocation);
285da9
 
285da9
         if (g_str_equal (method_name, "Get"))
285da9
                 user_extension_get_property (user, daemon, interface, invocation);
285da9
         else if (g_str_equal (method_name, "GetAll"))
285da9
                 user_extension_get_all_properties (user, daemon, interface, invocation);
285da9
         else if (g_str_equal (method_name, "Set"))
285da9
                 user_extension_set_property (user, daemon, interface, invocation);
285da9
         else
285da9
                 g_assert_not_reached ();
285da9
 }
285da9
 
285da9
 static void
285da9
 user_extension_method_call (GDBusConnection       *connection,
285da9
                             const gchar           *sender,
285da9
                             const gchar           *object_path,
285da9
                             const gchar           *interface_name,
285da9
                             const gchar           *method_name,
285da9
                             GVariant              *parameters,
285da9
                             GDBusMethodInvocation *invocation,
285da9
                             gpointer               user_data)
285da9
 {
285da9
         User *user = user_data;
285da9
         GDBusInterfaceInfo *iface_info;
285da9
         const gchar *annotation_name;
285da9
         const gchar *action_id;
285da9
         gint uid;
285da9
         gint i;
285da9
 
285da9
         /* We don't allow method calls on extension interfaces, so we
285da9
diff --git a/src/user.h b/src/user.h
285da9
index b3b3380..eb81918 100644
285da9
--- a/src/user.h
285da9
+++ b/src/user.h
285da9
@@ -30,58 +30,57 @@
285da9
 #include "types.h"
285da9
 
285da9
 G_BEGIN_DECLS
285da9
 
285da9
 #define TYPE_USER (user_get_type ())
285da9
 #define USER(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), TYPE_USER, User))
285da9
 #define IS_USER(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), TYPE_USER))
285da9
 
285da9
 typedef enum {
285da9
         ACCOUNT_TYPE_STANDARD,
285da9
         ACCOUNT_TYPE_ADMINISTRATOR,
285da9
 #define ACCOUNT_TYPE_LAST ACCOUNT_TYPE_ADMINISTRATOR
285da9
 } AccountType;
285da9
 
285da9
 typedef enum {
285da9
         PASSWORD_MODE_REGULAR,
285da9
         PASSWORD_MODE_SET_AT_LOGIN,
285da9
         PASSWORD_MODE_NONE,
285da9
 #define PASSWORD_MODE_LAST PASSWORD_MODE_NONE
285da9
 } PasswordMode;
285da9
 
285da9
 /* local methods */
285da9
 
285da9
 GType          user_get_type                (void) G_GNUC_CONST;
285da9
 User *         user_new                     (Daemon        *daemon,
285da9
                                              uid_t          uid);
285da9
 
285da9
 void           user_update_from_pwent       (User          *user,
285da9
                                              struct passwd *pwent,
285da9
                                              struct spwd   *spent);
285da9
-void           user_update_from_keyfile     (User          *user,
285da9
-                                             GKeyFile      *keyfile);
285da9
+void           user_update_from_cache       (User *user);
285da9
 void           user_update_local_account_property (User          *user,
285da9
                                                    gboolean       local);
285da9
 void           user_update_system_account_property (User          *user,
285da9
                                                     gboolean       system);
285da9
 gboolean       user_get_cached              (User          *user);
285da9
 void           user_set_cached              (User          *user,
285da9
                                              gboolean       cached);
285da9
 void           user_set_saved               (User          *user,
285da9
                                              gboolean       saved);
285da9
 
285da9
 void           user_register                (User          *user);
285da9
 void           user_unregister              (User          *user);
285da9
 void           user_changed                 (User          *user);
285da9
 
285da9
 void           user_save                    (User          *user);
285da9
 
285da9
 const gchar *  user_get_user_name           (User          *user);
285da9
 gboolean       user_get_system_account      (User          *user);
285da9
 gboolean       user_get_local_account       (User          *user);
285da9
 const gchar *  user_get_object_path         (User          *user);
285da9
 uid_t          user_get_uid                 (User          *user);
285da9
 const gchar *  user_get_shell               (User          *user);
285da9
 
285da9
 G_END_DECLS
285da9
 
285da9
 #endif
285da9
-- 
285da9
2.27.0
285da9