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

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