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

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