2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2012-05-07 18:55:45 +02:00
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <dirent.h>
|
2012-05-07 18:55:45 +02:00
|
|
|
#include <errno.h>
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <stdarg.h>
|
2012-05-07 18:55:45 +02:00
|
|
|
#include <stdio.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-05-07 18:55:45 +02:00
|
|
|
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "conf-files.h"
|
2018-04-26 13:49:50 +02:00
|
|
|
#include "def.h"
|
2015-10-26 20:07:55 +01:00
|
|
|
#include "dirent-util.h"
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "fd-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "hashmap.h"
|
|
|
|
#include "log.h"
|
2012-05-07 18:55:45 +02:00
|
|
|
#include "macro.h"
|
|
|
|
#include "missing.h"
|
2012-05-07 21:36:12 +02:00
|
|
|
#include "path-util.h"
|
2018-04-16 21:24:13 +02:00
|
|
|
#include "set.h"
|
2017-09-12 16:57:33 +02:00
|
|
|
#include "stat-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
|
|
|
#include "strv.h"
|
2018-04-26 13:49:50 +02:00
|
|
|
#include "terminal-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "util.h"
|
2012-05-07 18:55:45 +02:00
|
|
|
|
2018-04-16 21:24:13 +02:00
|
|
|
static int files_add(
|
|
|
|
Hashmap *h,
|
|
|
|
Set *masked,
|
|
|
|
const char *suffix,
|
|
|
|
const char *root,
|
|
|
|
unsigned flags,
|
|
|
|
const char *path) {
|
|
|
|
|
2013-02-11 23:48:36 +01:00
|
|
|
_cleanup_closedir_ DIR *dir = NULL;
|
2015-05-13 17:42:10 +02:00
|
|
|
const char *dirpath;
|
2016-01-02 21:32:45 +01:00
|
|
|
struct dirent *de;
|
2015-05-13 17:42:10 +02:00
|
|
|
int r;
|
2013-02-08 10:45:30 +01:00
|
|
|
|
2018-04-16 21:24:13 +02:00
|
|
|
assert(h);
|
|
|
|
assert((flags & CONF_FILES_FILTER_MASKED) == 0 || masked);
|
2014-06-20 04:07:04 +02:00
|
|
|
assert(path);
|
2014-06-17 01:56:44 +02:00
|
|
|
|
2015-05-13 17:42:10 +02:00
|
|
|
dirpath = prefix_roota(root, path);
|
2014-06-17 01:56:44 +02:00
|
|
|
|
2014-06-20 04:07:04 +02:00
|
|
|
dir = opendir(dirpath);
|
2012-05-07 18:55:45 +02:00
|
|
|
if (!dir) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return 0;
|
2018-04-16 21:24:13 +02:00
|
|
|
|
|
|
|
return log_debug_errno(errno, "Failed to open directory '%s': %m", dirpath);
|
2012-05-07 18:55:45 +02:00
|
|
|
}
|
|
|
|
|
2016-01-02 21:32:45 +01:00
|
|
|
FOREACH_DIRENT(de, dir, return -errno) {
|
2018-04-16 21:24:13 +02:00
|
|
|
struct stat st;
|
|
|
|
char *p, *key;
|
|
|
|
|
|
|
|
/* Does this match the suffix? */
|
|
|
|
if (suffix && !endswith(de->d_name, suffix))
|
|
|
|
continue;
|
2012-05-07 18:55:45 +02:00
|
|
|
|
2018-04-16 21:24:13 +02:00
|
|
|
/* Has this file already been found in an earlier directory? */
|
|
|
|
if (hashmap_contains(h, de->d_name)) {
|
|
|
|
log_debug("Skipping overridden file '%s/%s'.", dirpath, de->d_name);
|
2012-05-07 18:55:45 +02:00
|
|
|
continue;
|
2017-09-13 11:41:41 +02:00
|
|
|
}
|
2012-05-07 18:55:45 +02:00
|
|
|
|
2018-04-16 21:24:13 +02:00
|
|
|
/* Has this been masked in an earlier directory? */
|
|
|
|
if ((flags & CONF_FILES_FILTER_MASKED) && set_contains(masked, de->d_name)) {
|
|
|
|
log_debug("File '%s/%s' is masked by previous entry.", dirpath, de->d_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read file metadata if we shall validate the check for file masks, for node types or whether the node is marked executable. */
|
|
|
|
if (flags & (CONF_FILES_FILTER_MASKED|CONF_FILES_REGULAR|CONF_FILES_DIRECTORY|CONF_FILES_EXECUTABLE))
|
|
|
|
if (fstatat(dirfd(dir), de->d_name, &st, 0) < 0) {
|
|
|
|
log_debug_errno(errno, "Failed to stat '%s/%s', ignoring: %m", dirpath, de->d_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is this a masking entry? */
|
|
|
|
if ((flags & CONF_FILES_FILTER_MASKED))
|
|
|
|
if (null_or_empty(&st)) {
|
|
|
|
/* Mark this one as masked */
|
|
|
|
r = set_put_strdup(masked, de->d_name);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
log_debug("File '%s/%s' is a mask.", dirpath, de->d_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does this node have the right type? */
|
|
|
|
if (flags & (CONF_FILES_REGULAR|CONF_FILES_DIRECTORY))
|
|
|
|
if (!((flags & CONF_FILES_DIRECTORY) && S_ISDIR(st.st_mode)) &&
|
|
|
|
!((flags & CONF_FILES_REGULAR) && S_ISREG(st.st_mode))) {
|
|
|
|
log_debug("Ignoring '%s/%s', as it is not a of the right type.", dirpath, de->d_name);
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-12 16:57:33 +02:00
|
|
|
|
2018-04-16 21:24:13 +02:00
|
|
|
/* Does this node have the executable bit set? */
|
|
|
|
if (flags & CONF_FILES_EXECUTABLE)
|
2017-09-12 16:57:33 +02:00
|
|
|
/* As requested: check if the file is marked exectuable. Note that we don't check access(X_OK)
|
|
|
|
* here, as we care about whether the file is marked executable at all, and not whether it is
|
2018-04-16 21:24:13 +02:00
|
|
|
* executable for us, because if so, such errors are stuff we should log about. */
|
2017-09-12 16:57:33 +02:00
|
|
|
|
2018-04-16 21:24:13 +02:00
|
|
|
if ((st.st_mode & 0111) == 0) { /* not executable */
|
|
|
|
log_debug("Ignoring '%s/%s', as it is not marked executable.", dirpath, de->d_name);
|
2017-09-12 16:57:33 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-16 21:24:13 +02:00
|
|
|
if (flags & CONF_FILES_BASENAME) {
|
|
|
|
p = strdup(de->d_name);
|
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
2017-09-12 16:57:33 +02:00
|
|
|
|
2018-04-16 21:24:13 +02:00
|
|
|
key = p;
|
|
|
|
} else {
|
|
|
|
p = strjoin(dirpath, "/", de->d_name);
|
|
|
|
if (!p)
|
|
|
|
return -ENOMEM;
|
2012-05-07 18:55:45 +02:00
|
|
|
|
2018-04-16 21:24:13 +02:00
|
|
|
key = basename(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
r = hashmap_put(h, key, p);
|
|
|
|
if (r < 0) {
|
2012-05-07 18:55:45 +02:00
|
|
|
free(p);
|
2018-04-16 21:24:13 +02:00
|
|
|
return log_debug_errno(r, "Failed to add item to hashmap: %m");
|
2012-05-07 18:55:45 +02:00
|
|
|
}
|
2018-04-16 21:24:13 +02:00
|
|
|
|
|
|
|
assert(r > 0);
|
2012-05-07 18:55:45 +02:00
|
|
|
}
|
|
|
|
|
2013-02-08 10:45:30 +01:00
|
|
|
return 0;
|
2012-05-07 18:55:45 +02:00
|
|
|
}
|
|
|
|
|
2018-09-18 01:39:24 +02:00
|
|
|
static int base_cmp(char * const *a, char * const *b) {
|
|
|
|
return strcmp(basename(*a), basename(*b));
|
2012-05-07 18:55:45 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 16:57:33 +02:00
|
|
|
static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, unsigned flags, char **dirs) {
|
2014-06-24 19:00:32 +02:00
|
|
|
_cleanup_hashmap_free_ Hashmap *fh = NULL;
|
2018-04-16 21:24:13 +02:00
|
|
|
_cleanup_set_free_free_ Set *masked = NULL;
|
2013-02-11 23:48:36 +01:00
|
|
|
char **files, **p;
|
2012-07-24 23:18:25 +02:00
|
|
|
int r;
|
2012-05-07 18:55:45 +02:00
|
|
|
|
2013-02-11 23:48:36 +01:00
|
|
|
assert(strv);
|
|
|
|
|
|
|
|
/* This alters the dirs string array */
|
2014-06-20 04:07:02 +02:00
|
|
|
if (!path_strv_resolve_uniq(dirs, root))
|
2013-02-11 23:48:36 +01:00
|
|
|
return -ENOMEM;
|
2012-05-07 18:55:45 +02:00
|
|
|
|
2018-04-16 21:24:13 +02:00
|
|
|
fh = hashmap_new(&path_hash_ops);
|
2013-02-11 23:48:36 +01:00
|
|
|
if (!fh)
|
|
|
|
return -ENOMEM;
|
2012-05-07 18:55:45 +02:00
|
|
|
|
2018-04-16 21:24:13 +02:00
|
|
|
if (flags & CONF_FILES_FILTER_MASKED) {
|
|
|
|
masked = set_new(&path_hash_ops);
|
|
|
|
if (!masked)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2012-05-07 18:55:45 +02:00
|
|
|
STRV_FOREACH(p, dirs) {
|
2018-04-16 21:24:13 +02:00
|
|
|
r = files_add(fh, masked, suffix, root, flags, *p);
|
2016-01-02 21:32:45 +01:00
|
|
|
if (r == -ENOMEM)
|
2013-02-11 23:48:36 +01:00
|
|
|
return r;
|
2016-01-02 21:32:45 +01:00
|
|
|
if (r < 0)
|
|
|
|
log_debug_errno(r, "Failed to search for files in %s, ignoring: %m", *p);
|
2012-05-07 18:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
files = hashmap_get_strv(fh);
|
2016-01-02 21:32:45 +01:00
|
|
|
if (!files)
|
2013-02-11 23:48:36 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2018-09-18 01:39:24 +02:00
|
|
|
typesafe_qsort(files, hashmap_size(fh), base_cmp);
|
2013-02-11 23:48:36 +01:00
|
|
|
*strv = files;
|
2012-05-07 18:55:45 +02:00
|
|
|
|
2013-02-11 23:48:36 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-05 14:53:11 +01:00
|
|
|
int conf_files_insert(char ***strv, const char *root, char **dirs, const char *path) {
|
sysusers: allow admin/runtime overrides to command-line config
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
2018-01-31 15:37:02 +01:00
|
|
|
/* 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.
|
|
|
|
*/
|
tree-wide: be more careful with the type of array sizes
Previously we were a bit sloppy with the index and size types of arrays,
we'd regularly use unsigned. While I don't think this ever resulted in
real issues I think we should be more careful there and follow a
stricter regime: unless there's a strong reason not to use size_t for
array sizes and indexes, size_t it should be. Any allocations we do
ultimately will use size_t anyway, and converting forth and back between
unsigned and size_t will always be a source of problems.
Note that on 32bit machines "unsigned" and "size_t" are equivalent, and
on 64bit machines our arrays shouldn't grow that large anyway, and if
they do we have a problem, however that kind of overly large allocation
we have protections for usually, but for overflows we do not have that
so much, hence let's add it.
So yeah, it's a story of the current code being already "good enough",
but I think some extra type hygiene is better.
This patch tries to be comprehensive, but it probably isn't and I missed
a few cases. But I guess we can cover that later as we notice it. Among
smaller fixes, this changes:
1. strv_length()' return type becomes size_t
2. the unit file changes array size becomes size_t
3. DNS answer and query array sizes become size_t
Fixes: https://bugs.freedesktop.org/show_bug.cgi?id=76745
2018-04-27 14:09:31 +02:00
|
|
|
size_t i;
|
sysusers: allow admin/runtime overrides to command-line config
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
2018-01-31 15:37:02 +01:00
|
|
|
char *t;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
for (i = 0; i < strv_length(*strv); i++) {
|
|
|
|
int c;
|
|
|
|
|
2018-09-18 01:39:24 +02:00
|
|
|
c = base_cmp((char* const*) *strv + i, (char* const*) &path);
|
sysusers: allow admin/runtime overrides to command-line config
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
2018-01-31 15:37:02 +01:00
|
|
|
if (c == 0) {
|
2018-02-05 14:53:11 +01:00
|
|
|
char **dir;
|
sysusers: allow admin/runtime overrides to command-line config
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
2018-01-31 15:37:02 +01:00
|
|
|
|
|
|
|
/* Oh, we found our spot and it already contains something. */
|
2018-02-05 14:53:11 +01:00
|
|
|
STRV_FOREACH(dir, dirs) {
|
sysusers: allow admin/runtime overrides to command-line config
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
2018-01-31 15:37:02 +01:00
|
|
|
char *p1, *p2;
|
|
|
|
|
|
|
|
p1 = path_startswith((*strv)[i], root);
|
|
|
|
if (p1)
|
2018-02-05 14:53:11 +01:00
|
|
|
/* Skip "/" in *dir, because p1 is without "/" too */
|
|
|
|
p1 = path_startswith(p1, *dir + 1);
|
sysusers: allow admin/runtime overrides to command-line config
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
2018-01-31 15:37:02 +01:00
|
|
|
if (p1)
|
|
|
|
/* Existing entry with higher priority
|
|
|
|
* or same priority, no need to do anything. */
|
|
|
|
return 0;
|
|
|
|
|
2018-02-05 14:53:11 +01:00
|
|
|
p2 = path_startswith(path, *dir);
|
sysusers: allow admin/runtime overrides to command-line config
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
2018-01-31 15:37:02 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-05 14:53:11 +01:00
|
|
|
int conf_files_insert_nulstr(char ***strv, const char *root, const char *dirs, const char *path) {
|
|
|
|
_cleanup_strv_free_ char **d = NULL;
|
|
|
|
|
|
|
|
assert(strv);
|
|
|
|
|
|
|
|
d = strv_split_nulstr(dirs);
|
|
|
|
if (!d)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
return conf_files_insert(strv, root, d, path);
|
|
|
|
}
|
|
|
|
|
2017-09-12 16:57:33 +02:00
|
|
|
int conf_files_list_strv(char ***strv, const char *suffix, const char *root, unsigned flags, const char* const* dirs) {
|
2013-02-11 23:48:36 +01:00
|
|
|
_cleanup_strv_free_ char **copy = NULL;
|
|
|
|
|
|
|
|
assert(strv);
|
|
|
|
|
|
|
|
copy = strv_copy((char**) dirs);
|
|
|
|
if (!copy)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2017-09-12 16:57:33 +02:00
|
|
|
return conf_files_list_strv_internal(strv, suffix, root, flags, copy);
|
2012-05-07 18:55:45 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 16:57:33 +02:00
|
|
|
int conf_files_list(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dir, ...) {
|
2013-10-30 18:15:38 +01:00
|
|
|
_cleanup_strv_free_ char **dirs = NULL;
|
|
|
|
va_list ap;
|
2013-02-11 23:48:36 +01:00
|
|
|
|
|
|
|
assert(strv);
|
2012-05-07 18:55:45 +02:00
|
|
|
|
2013-10-30 18:15:38 +01:00
|
|
|
va_start(ap, dir);
|
|
|
|
dirs = strv_new_ap(dir, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (!dirs)
|
|
|
|
return -ENOMEM;
|
2013-02-11 23:48:36 +01:00
|
|
|
|
2017-09-12 16:57:33 +02:00
|
|
|
return conf_files_list_strv_internal(strv, suffix, root, flags, dirs);
|
2013-02-11 23:48:36 +01:00
|
|
|
}
|
2012-05-07 18:55:45 +02:00
|
|
|
|
2018-02-05 14:53:11 +01:00
|
|
|
int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, unsigned flags, const char *dirs) {
|
|
|
|
_cleanup_strv_free_ char **d = NULL;
|
2013-02-11 23:48:36 +01:00
|
|
|
|
|
|
|
assert(strv);
|
|
|
|
|
2018-02-05 14:53:11 +01:00
|
|
|
d = strv_split_nulstr(dirs);
|
|
|
|
if (!d)
|
2013-02-11 23:48:36 +01:00
|
|
|
return -ENOMEM;
|
2012-05-07 18:55:45 +02:00
|
|
|
|
2018-02-05 14:53:11 +01:00
|
|
|
return conf_files_list_strv_internal(strv, suffix, root, flags, d);
|
2012-05-07 18:55:45 +02:00
|
|
|
}
|
2018-04-26 13:49:50 +02:00
|
|
|
|
2018-04-26 19:07:54 +02:00
|
|
|
int conf_files_list_with_replacement(
|
|
|
|
const char *root,
|
|
|
|
char **config_dirs,
|
|
|
|
const char *replacement,
|
|
|
|
char ***files,
|
|
|
|
char **replace_file) {
|
|
|
|
|
|
|
|
_cleanup_strv_free_ char **f = NULL;
|
|
|
|
_cleanup_free_ char *p = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(config_dirs);
|
|
|
|
assert(files);
|
|
|
|
assert(replace_file || !replacement);
|
|
|
|
|
|
|
|
r = conf_files_list_strv(&f, ".conf", root, 0, (const char* const*) config_dirs);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to enumerate config files: %m");
|
|
|
|
|
|
|
|
if (replacement) {
|
|
|
|
r = conf_files_insert(&f, root, config_dirs, replacement);
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to extend config file list: %m");
|
|
|
|
|
|
|
|
p = path_join(root, replacement, NULL);
|
|
|
|
if (!p)
|
|
|
|
return log_oom();
|
|
|
|
}
|
|
|
|
|
|
|
|
*files = TAKE_PTR(f);
|
|
|
|
if (replace_file)
|
|
|
|
*replace_file = TAKE_PTR(p);
|
|
|
|
return 0;
|
|
|
|
}
|