tmpfiles: add the ~ modifier, an argument in Base64

In tmpfiles.d(5) a ~ after the type of an f, F, or w entry means the
argument is Base64, for content that cannot be written on a line:

    f~ /root/.ssh/authorized_keys 0600 root root - c3NoLWVkMjU1MTkg...

tmpfiles ignores the ~ and writes the argument as it is.

Decode the argument.  As in systemd it is not looked at for specifiers
or escapes, and an entry with an argument that is not Base64 is
skipped.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-21 17:14:02 +01:00
1 parent ed96814fdc
commit 09dcf91877
4 files changed
+77 -10

No files matched your search

+2
View File
@@ -32,6 +32,8 @@ All relevant changes are documented in this file.
points to exists, the same as Finit's own `l` entry
- tmpfiles: the path of an entry can now be quoted and have backslash
escapes, for a path with a space in it: `d "/srv/my files"`
- tmpfiles: add the `~` modifier for `f`, `F`, and `w` entries, as in
`f~`, where the argument is Base64, for a file with binary content
- tmpfiles: add `--prefix=PATH`, to only run the entries for paths below
it. `--prefix` and `--exclude-prefix` match whole path components,
`--exclude-prefix=/dev` no longer skips `/devices`
+4 -4
View File
@@ -13,10 +13,10 @@ 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
* Modifiers `=` (remove what is of another type) and `~` (base64
argument). `-` (do not fail) has nothing to do, tmpfiles never exits
with an error for an entry, and an entry with `^` (credential as
argument) is skipped, Finit has no credentials
* Modifier `=` (remove what is of another type). `-` (do not fail) has
nothing to do, tmpfiles never exits with an error for an entry, and
an entry with `^` (credential as argument) is skipped, Finit has no
credentials
2. Command line
+56 -6
View File
@@ -242,12 +242,51 @@ end:
return &p[i];
}
static void write_arg(FILE *fp, char *arg)
/* Decode Base64 in place, returns the length or -1 when @str is not Base64 */
static ssize_t b64_decode(char *str)
{
static const char tbl[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
unsigned int acc = 0;
char *out = str;
int bits = 0;
for (char *in = str; *in; in++) {
const char *ptr;
if (isspace((unsigned char)*in))
continue;
if (*in == '=') {
if (in[strspn(in, "=\t ")])
return -1;
break;
}
ptr = strchr(tbl, *in);
if (!ptr)
return -1;
acc = acc << 6 | (ptr - tbl);
bits += 6;
if (bits >= 8) {
bits -= 8;
*out++ = acc >> bits;
}
}
return out - str;
}
/* Write @arg with its escapes resolved, or the @len bytes of it when @len is not -1 */
static void write_arg(FILE *fp, char *arg, ssize_t len)
{
char *p;
if (!arg)
return;
if (len >= 0) {
fwrite(arg, sizeof(char), len, fp);
return;
}
while (*arg && (p = strchr(arg, '\\'))) {
fwrite(arg, sizeof(char), p - arg, fp);
@@ -1433,7 +1472,8 @@ static void tmpfiles(char *line)
int major, minor;
int strc, rc = 0;
struct perm perm = { 0 };
int plus = 0, boot = 0;
int plus = 0, boot = 0, b64 = 0;
ssize_t arglen = -1;
FILE *fp = NULL;
char buf[1024], pathbuf[PATH_MAX], argbuf[1024];
glob_t gl;
@@ -1447,6 +1487,8 @@ static void tmpfiles(char *line)
plus = 1;
else if (*mod == '!')
boot = 1;
else if (*mod == '~')
b64 = 1;
else if (*mod == '?' && type[0] == 'L')
type[0] = 'l'; /* What Finit has had as l */
else if (*mod == '^')
@@ -1528,7 +1570,15 @@ static void tmpfiles(char *line)
arg = line + strspn(line, "\t ");
if (!arg[0] || !strcmp(arg, "-"))
arg = NULL;
if (arg && strchr(arg, '%') && strchr("CfFlLtTw", type[0])) {
if (b64) {
if (arg && strchr("fFw", type[0]))
arglen = b64_decode(arg);
if (arg && arglen < 0) {
if (!quiet)
warnx("Argument is not Base64, or not one to decode, skipping %s", path);
return;
}
} else if (arg && strchr(arg, '%') && strchr("CfFlLtTw", type[0])) {
arg = expand(arg, argbuf, sizeof(argbuf));
if (!arg) {
if (!quiet)
@@ -1549,7 +1599,7 @@ static void tmpfiles(char *line)
if (clean_flag && strchr("CdDe", type[0]) && !parse_age(age, &clean_age))
printf("clean %s %s, older than %s\n", type, path, age);
if (create_flag && !strchr("rRxX", type[0]))
printf("create %s %s%s%s\n", type, path, arg ? " " : "", arg ?: "");
printf("create %s %s%s%s\n", type, path, arg && !b64 ? " " : "", arg && !b64 ? arg : "");
return;
}
@@ -1737,7 +1787,7 @@ static void tmpfiles(char *line)
break;
}
write_arg(fp, arg);
write_arg(fp, arg, arglen);
if (strc && !perm.mode)
perm.mode = 0644;
@@ -1834,7 +1884,7 @@ static void tmpfiles(char *line)
close(fd);
continue;
}
write_arg(fp, arg);
write_arg(fp, arg, arglen);
rc = fclose(fp);
}
globfree(&gl);
+15
View File
@@ -102,3 +102,18 @@ assert "Single quotes" "$(texec cat "$DIR/single quote")" = "single"
assert "Quotes in path" "$(texec cat "$DIR/middle space")" = "middle"
assert "Escapes in path" "$(texec cat "$DIR/escA ped")" = '"kept as is"'
assert "Skipped with a quote left open" "$(texec sh -c "ls $DIR | grep -c open")" = "0"
sep
say 'With ~ the argument is Base64'
run "printf abcdef > $DIR/w1"
run "cat > $CONF" <<'CONF'
f~ /tmp/arg/b64 - - - - aGVsbG8gJXQK
f~ /tmp/arg/b64pad - - - - aGk=
f~ /tmp/arg/b64bad - - - - a!!!
w~ /tmp/arg/w1 - - - - WFk=
CONF
run "/libexec/finit/tmpfiles --create $CONF"
assert "Decoded, no specifiers" "$(texec cat $DIR/b64)" = "hello %t"
assert "Padded" "$(texec cat $DIR/b64pad)" = "hi"
assert "Written with w" "$(texec cat $DIR/w1)" = "XYcdef"
assert "Skipped when not Base64" "$(texec sh -c "ls $DIR | grep -c b64bad")" = "0"