chiark / gitweb /
sysusers: allow admin/runtime overrides to command-line config
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 31 Jan 2018 14:37:02 +0000 (15:37 +0100)
committerSven Eden <yamakuzure@gmx.net>
Wed, 30 May 2018 05:58:48 +0000 (07:58 +0200)
When used in a package installation script, we want to invoke systemd-sysusers
before that package is installed (so it can contain files owned by the newly
created user), so the configuration to use is specified on the command
line. This should be a copy of the configuration that will be installed as
/usr/lib/sysusers.d/package.conf. We still want to obey any overrides in
/etc/sysusers.d or /run/sysusers.d in the usual fashion. Otherwise, we'd get a
different result when systemd-sysusers is run with a copy of the new config on
the command line and when systemd-sysusers is run at boot after package
instalation. In the second case any files in /etc or /run have higher priority,
so the same should happen when the configuration is given on the command line.
More generally, we want the behaviour in this special case to be as close to
the case where the file is finally on disk as possible, so we have to read all
configuration files, since they all might contain overrides and additional
configuration that matters. Even files that have lower priority might specify
additional groups for the user we are creating. Thus, we need to read all
configuration, but insert our new configuration somewhere with the right
priority.

If --target=/path/to/file.conf is given on the command line, we gather the list
of files, and pretend that the command-line config is read from
/path/to/file.conf (doesn't matter if the file on disk actually exists or
not). All package scripts should use this option to obtain consistent and
idempotent behaviour.

The corner case when --target= is specified and there are no positional
arguments is disallowed.

v1:
- version with --config-name=
v2:
- disallow --config-name= and no positional args
v3:
- remove --config-name=
v4:
- add --target= and rework the code completely
v5:
- fix argcounting bug and add example in man page
v6:
- rename --target to --replace

src/basic/conf-files.c
src/basic/conf-files.h

index c0ac202f57f698d68844512d941bdd0b5af18d38..08ede2c766a19e4ba92dfa10c3818395066bd5e5 100644 (file)
@@ -154,6 +154,70 @@ static int conf_files_list_strv_internal(char ***strv, const char *suffix, const
         return 0;
 }
 
+int conf_files_insert(char ***strv, const char *root, const char *dirs, const char *path) {
+        /* Insert a path into strv, at the place honouring the usual sorting rules:
+         * - we first compare by the basename
+         * - and then we compare by dirname, allowing just one file with the given
+         *   basename.
+         * This means that we will
+         * - add a new entry if basename(path) was not on the list,
+         * - do nothing if an entry with higher priority was already present,
+         * - do nothing if our new entry matches the existing entry,
+         * - replace the existing entry if our new entry has higher priority.
+         */
+        char *t;
+        unsigned i;
+        int r;
+
+        for (i = 0; i < strv_length(*strv); i++) {
+                int c;
+
+                c = base_cmp(*strv + i, &path);
+                if (c == 0) {
+                        const char *dir;
+
+                        /* Oh, we found our spot and it already contains something. */
+                        NULSTR_FOREACH(dir, dirs) {
+                                char *p1, *p2;
+
+                                p1 = path_startswith((*strv)[i], root);
+                                if (p1)
+                                        /* Skip "/" in dir, because p1 is without "/" too */
+                                        p1 = path_startswith(p1, dir + 1);
+                                if (p1)
+                                        /* Existing entry with higher priority
+                                         * or same priority, no need to do anything. */
+                                        return 0;
+
+                                p2 = path_startswith(path, dir);
+                                if (p2) {
+                                        /* Our new entry has higher priority */
+                                        t = path_join(root, path, NULL);
+                                        if (!t)
+                                                return log_oom();
+
+                                        return free_and_replace((*strv)[i], t);
+                                }
+                        }
+
+                } else if (c > 0)
+                        /* Following files have lower priority, let's go insert our
+                         * new entry. */
+                        break;
+
+                /* … we are not there yet, let's continue */
+        }
+
+        t = path_join(root, path, NULL);
+        if (!t)
+                return log_oom();
+
+        r = strv_insert(strv, i, t);
+        if (r < 0)
+                free(t);
+        return r;
+}
+
 int conf_files_list_strv(char ***strv, const char *suffix, const char *root, unsigned flags, const char* const* dirs) {
         _cleanup_strv_free_ char **copy = NULL;
 
index 75dfd05e7c1af2f7bb3ebb582adcbde870f2a1c1..ddee7278260a2e1100ae672970f3cf3fc1615f09 100644 (file)
@@ -28,3 +28,4 @@ enum {
 int conf_files_list(char ***ret, const char *suffix, const char *root, unsigned flags, const char *dir, ...);
 int conf_files_list_strv(char ***ret, const char *suffix, const char *root, unsigned flags, const char* const* dirs);
 int conf_files_list_nulstr(char ***ret, const char *suffix, const char *root, unsigned flags, const char *dirs);
+int conf_files_insert(char ***strv, const char *root, const char *dirs, const char *path);