diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 8db61a47..f14ea515 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -22,6 +22,8 @@ All relevant changes are documented in this file. `%w`, `%W`, `%B`, `%M`, and `%A`. Before, an entry with a `%` in the path was skipped. **Note:** a literal `%` in such an argument, e.g. in a `w` entry for `core_pattern`, must now be written `%%` +- tmpfiles: add the `t` and `T` entries, to set extended attributes on + a path, `T` also on everything below it - tmpfiles: add the `L?` entry, a link that is only made when what it points to exists, the same as Finit's own `l` entry - tmpfiles: add `--prefix=PATH`, to only run the entries for paths below diff --git a/doc/tmpfiles-todo.md b/doc/tmpfiles-todo.md index 6ea495eb..3c2f5abb 100644 --- a/doc/tmpfiles-todo.md +++ b/doc/tmpfiles-todo.md @@ -13,7 +13,7 @@ files or common packages already rely on, last what few will miss. * `C+`, which is read as `C` * `z` and `Z` only set the SELinux label when `restorecon` is installed -* `t`, `T` for extended attributes and `h`, `H` for file attributes +* `h`, `H` for file attributes * Modifiers `-` (do not fail), `=` (remove what is of another type), `~` (base64 argument), and `^` (credential as argument) * Quoted paths, a path with a space in it cannot be written diff --git a/src/tmpfiles.c b/src/tmpfiles.c index 9ae80855..9cc9f4b4 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -964,6 +964,118 @@ static int acl_entry(const char *pattern, char *arg, int recursive, int modify) return rc; } +/* + * For the entries that take a glob and have a recursive form. @fn + * and @what are global, nftw() cannot pass user data. + */ +static int (*tree_fn)(const char *path); +static const char *tree_what; + +static int tree_cb(const char *path, const struct stat *st, int tflag, struct FTW *ftw) +{ + (void)tflag; (void)ftw; + + /* None of this is for a link, and what it points to is not ours to change */ + if (!S_ISLNK(st->st_mode) && tree_fn(path)) + warn("Failed setting %s on %s", tree_what, path); + + return 0; +} + +static int tree_do(const char *pattern, int (*fn)(const char *), const char *what, int recursive) +{ + glob_t gl; + + if (glob(pattern, GLOB_NOESCAPE, NULL, &gl)) + return 0; + + tree_fn = fn; + tree_what = what; + for (size_t i = 0; i < gl.gl_pathc; i++) { + struct stat st; + + if (recursive) + nftw(gl.gl_pathv[i], tree_cb, 20, FTW_PHYS); + else if (!lstat(gl.gl_pathv[i], &st)) + tree_cb(gl.gl_pathv[i], &st, 0, NULL); + } + globfree(&gl); + + return 0; +} + +/* Extended attributes of a t or T entry, see xattr_parse() */ +#define XATTR_MAX 16 + +static struct { + char *name; + char *value; +} xattrs[XATTR_MAX]; +static int num_xattrs; + +/* + * NAME=VALUE pairs with space between, cut up where they are in @arg. + * A value may be in quotes, and a backslash takes the next character + * as it is. + */ +static int xattr_parse(char *arg) +{ + num_xattrs = 0; + + while (arg && *arg) { + char quote = 0, *out; + + arg += strspn(arg, "\t "); + if (!*arg) + break; + if (num_xattrs == XATTR_MAX) + goto inval; + + xattrs[num_xattrs].name = arg; + arg = strchr(arg, '='); + if (!arg || arg == xattrs[num_xattrs].name) + goto inval; + *arg++ = 0; + if (strpbrk(xattrs[num_xattrs].name, "\t ")) + goto inval; + + xattrs[num_xattrs++].value = out = arg; + for (; *arg; arg++) { + if (*arg == '\\' && arg[1]) + *out++ = *++arg; + else if (quote && *arg == quote) + quote = 0; + else if (!quote && (*arg == '"' || *arg == '\'')) + quote = *arg; + else if (!quote && (*arg == ' ' || *arg == '\t')) + break; + else + *out++ = *arg; + } + if (*arg) + arg++; + *out = 0; + if (quote) + goto inval; + } + + if (num_xattrs) + return 0; +inval: + errno = EINVAL; + return -1; +} + +static int xattr_path(const char *path) +{ + for (int i = 0; i < num_xattrs; i++) { + if (lsetxattr(path, xattrs[i].name, xattrs[i].value, strlen(xattrs[i].value), 0)) + return -1; + } + + return 0; +} + /* First line of @file, without @strip characters, empty when it cannot be read */ static char *specifier_file(const char *file, const char *strip, char *buf, size_t len) { @@ -1260,7 +1372,7 @@ static void tmpfiles(char *line) if (!arg[0] || !strcmp(arg, "-")) arg = NULL; } - if (arg && strchr(arg, '%') && strchr("CfFlLw", type[0])) { + if (arg && strchr(arg, '%') && strchr("CfFlLtTw", type[0])) { arg = expand(arg, argbuf, sizeof(argbuf)); if (!arg) { if (!quiet) @@ -1280,6 +1392,8 @@ static void tmpfiles(char *line) case 'c': case 'C': case 'd': + case 't': + case 'T': break; case 'D': rmcontents(path); @@ -1455,6 +1569,12 @@ static void tmpfiles(char *line) rc = fclose(fp); break; } + case 't': + case 'T': + rc = xattr_parse(arg); + if (!rc) + rc = tree_do(path, xattr_path, "extended attributes", type[0] == 'T'); + break; case 'l': /* Finit extension, like 'L' but only if target exists */ if (!arg) { paste(buf, sizeof(buf), "/usr/share/factory", path); diff --git a/test/Makefile.am b/test/Makefile.am index d6b0e097..0d643faa 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -83,6 +83,7 @@ EXTRA_DIST += signal-service.sh EXTRA_DIST += testserv.sh EXTRA_DIST += tmpfiles-acl.sh EXTRA_DIST += tmpfiles-arg.sh +EXTRA_DIST += tmpfiles-attr.sh EXTRA_DIST += tmpfiles-perm.sh EXTRA_DIST += tmpfiles-boot.sh EXTRA_DIST += tmpfiles-clean.sh @@ -168,6 +169,7 @@ if TESTSERV TESTS += testserv.sh TESTS += tmpfiles-acl.sh TESTS += tmpfiles-arg.sh +TESTS += tmpfiles-attr.sh TESTS += tmpfiles-perm.sh TESTS += tmpfiles-boot.sh TESTS += tmpfiles-clean.sh diff --git a/test/tmpfiles-attr.sh b/test/tmpfiles-attr.sh new file mode 100755 index 00000000..ffe60446 --- /dev/null +++ b/test/tmpfiles-attr.sh @@ -0,0 +1,44 @@ +#!/bin/sh +# Verify the t and T entries of tmpfiles, extended attributes. On the +# file system of the sysroot, not all of this works on tmpfs. +set -eu + +TEST_DIR=$(dirname "$0") + +DIR=/attr-test +CONF=/tmp/attr.conf + +tmpfiles() +{ + run "cat > $CONF" + run "/libexec/finit/tmpfiles --create $CONF" +} + +# The sysroot has setfattr but no getfattr, removing one that is not +# set fails +assert_xattr() +{ + assert "$1 has $2" "$(texec sh -c "setfattr -x $2 $1 && echo yes || echo no")" = "yes" +} + +test_teardown() +{ + say "Running test teardown." + run "rm -rf $DIR $CONF" +} + +# shellcheck source=/dev/null +. "$TEST_DIR/lib/setup.sh" + +run "mkdir -p $DIR/tree/sub; touch $DIR/file $DIR/tree/sub/file" + +sep +say 't sets extended attributes, T on everything below the path too' +tmpfiles <