diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 4a441085..594738b2 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -30,6 +30,8 @@ All relevant changes are documented in this file. chattr(1), `H` also on everything below the path - 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: 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 `--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` diff --git a/doc/tmpfiles-todo.md b/doc/tmpfiles-todo.md index a2617dcc..abd0b226 100644 --- a/doc/tmpfiles-todo.md +++ b/doc/tmpfiles-todo.md @@ -17,7 +17,6 @@ files or common packages already rely on, last what few will miss. 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 -* Quoted paths, a path with a space in it cannot be written 2. Command line diff --git a/src/tmpfiles.c b/src/tmpfiles.c index 08a5d748..1b52cd70 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -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) { diff --git a/test/tmpfiles-arg.sh b/test/tmpfiles-arg.sh index f2b9b3d8..14a3c33d 100755 --- a/test/tmpfiles-arg.sh +++ b/test/tmpfiles-arg.sh @@ -86,3 +86,19 @@ CONF run "/libexec/finit/tmpfiles --create $CONF" assert "All hex digits" "$(texec cat $DIR/hex)" = "9AJk" assert "No comment in line" "$(texec cat $DIR/hash)" = "a#b c" + +sep +say 'A path can be quoted, and have escapes' +run "cat > $CONF" <<'CONF' +f "/tmp/arg/with space" - - - - double +f '/tmp/arg/single quote' - - - - single +f /tmp/arg/mid"dle sp"ace - - - - middle +f /tmp/arg/esc\x41\sped - - - - "kept as is" +f "/tmp/arg/open - - - - quote +CONF +run "/libexec/finit/tmpfiles --create $CONF" +assert "Double quotes" "$(texec cat "$DIR/with space")" = "double" +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"