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

+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);