tmpfiles: read quotes and escapes in the fields before the argument

There is no way to write an entry for a path with a space in it, the
fields are split at every space.  In tmpfiles.d(5) a field can be
quoted, and have C-style escapes:

    d "/srv/my files"
    d /srv/my\sfiles

Split the fields with a function that resolves both.  The argument is
still the rest of the line as it is, quotes and all, like in systemd.
An entry with a quote left open is skipped.

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

No files matched your search

+74 -12
View File
@@ -1349,6 +1349,71 @@ static char *expand(const char *str, char *buf, size_t len)
return buf;
}
/*
* Next field of @line, NULL when there are no more, a quote is left
* open, or the field is empty or has a NUL. Quotes and backslash
* escapes are resolved in place, so a path can have a space.
*/
static char *field(char **line)
{
static const char from[] = "abfnrtvs", to[] = "\a\b\f\n\r\t\v ";
char *in = *line, *out, *start;
char quote = 0;
int nul = 0;
in += strspn(in, "\t ");
if (!*in)
return NULL;
for (start = out = in; *in; in++) {
const char *ptr;
if (quote) {
if (*in == quote) {
quote = 0;
continue;
}
} else if (*in == '"' || *in == '\'') {
quote = *in;
continue;
} else if (*in == ' ' || *in == '\t') {
in++;
break;
}
if (*in != '\\' || !in[1]) {
*out++ = *in;
continue;
}
in++;
if (*in == 'x' && isxdigit((unsigned char)in[1]) && isxdigit((unsigned char)in[2])) {
char hex[3] = { in[1], in[2], 0 };
*out = strtoul(hex, NULL, 16);
nul |= !*out++;
in += 2;
} else if (*in >= '0' && *in <= '3' && in[1] >= '0' && in[1] <= '7' && in[2] >= '0' && in[2] <= '7') {
*out = (in[0] - '0') << 6 | (in[1] - '0') << 3 | (in[2] - '0');
nul |= !*out++;
in += 2;
} else if ((ptr = strchr(from, *in))) {
*out++ = to[ptr - from];
} else {
*out++ = *in;
}
}
*out = 0;
*line = in;
if (quote || nul || out == start) {
if (!quiet)
warnx("Missing end quote, or an empty field, skipping %s", start);
return NULL;
}
return start;
}
/*
* The configuration format is one line per path, containing type, path,
* mode, ownership, age, and argument fields. The lines are separated by
@@ -1373,7 +1438,7 @@ static void tmpfiles(char *line)
char buf[1024], pathbuf[PATH_MAX], argbuf[1024];
glob_t gl;
type = strtok(line, "\t ");
type = field(&line);
if (!type || type[0] == '#')
return;
@@ -1394,7 +1459,7 @@ static void tmpfiles(char *line)
if (strchr("vqQ", type[0]))
type[0] = 'd';
path = strtok(NULL, "\t ");
path = field(&line);
if (!path)
return;
if (strchr(path, '%')) {
@@ -1415,7 +1480,7 @@ static void tmpfiles(char *line)
return;
}
token = strtok(NULL, "\t ");
token = field(&line);
if (token) {
for (; *token == '~' || *token == ':'; token++) {
if (*token == '~')
@@ -1430,14 +1495,14 @@ static void tmpfiles(char *line)
perm.mode = 0;
}
user = strtok(NULL, "\t ");
user = field(&line);
if (user && user[0] == ':') {
perm.new_uid = 1;
user++;
}
if (user && !strcmp(user, "-"))
user = NULL;
group = strtok(NULL, "\t ");
group = field(&line);
if (group && group[0] == ':') {
perm.new_gid = 1;
group++;
@@ -1459,13 +1524,10 @@ static void tmpfiles(char *line)
if (group && perm.gid < 0 && debug)
warnx("Unknown group %s, not setting the group of %s", group, path);
age = strtok(NULL, "\t ");
arg = strtok(NULL, "\n");
if (arg) {
arg += strspn(arg, "\t ");
if (!arg[0] || !strcmp(arg, "-"))
arg = NULL;
}
age = field(&line);
arg = line + strspn(line, "\t ");
if (!arg[0] || !strcmp(arg, "-"))
arg = NULL;
if (arg && strchr(arg, '%') && strchr("CfFlLtTw", type[0])) {
arg = expand(arg, argbuf, sizeof(argbuf));
if (!arg) {