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

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