mirror of
https://github.com/troglobit/finit.git
synced 2026-11-23 20:11:44 +00:00
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:
4 files changed
+92
-13
No files matched your search
@@ -30,6 +30,8 @@ All relevant changes are documented in this file.
|
|||||||
chattr(1), `H` also on everything below the path
|
chattr(1), `H` also on everything below the path
|
||||||
- tmpfiles: add the `L?` entry, a link that is only made when what 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
|
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
|
- tmpfiles: add `--prefix=PATH`, to only run the entries for paths below
|
||||||
it. `--prefix` and `--exclude-prefix` match whole path components,
|
it. `--prefix` and `--exclude-prefix` match whole path components,
|
||||||
`--exclude-prefix=/dev` no longer skips `/devices`
|
`--exclude-prefix=/dev` no longer skips `/devices`
|
||||||
|
|||||||
@@ -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
|
argument). `-` (do not fail) has nothing to do, tmpfiles never exits
|
||||||
with an error for an entry, and an entry with `^` (credential as
|
with an error for an entry, and an entry with `^` (credential as
|
||||||
argument) is skipped, Finit has no credentials
|
argument) is skipped, Finit has no credentials
|
||||||
* Quoted paths, a path with a space in it cannot be written
|
|
||||||
|
|
||||||
|
|
||||||
2. Command line
|
2. Command line
|
||||||
|
|||||||
+74
-12
@@ -1349,6 +1349,71 @@ static char *expand(const char *str, char *buf, size_t len)
|
|||||||
return buf;
|
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,
|
* The configuration format is one line per path, containing type, path,
|
||||||
* mode, ownership, age, and argument fields. The lines are separated by
|
* 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];
|
char buf[1024], pathbuf[PATH_MAX], argbuf[1024];
|
||||||
glob_t gl;
|
glob_t gl;
|
||||||
|
|
||||||
type = strtok(line, "\t ");
|
type = field(&line);
|
||||||
if (!type || type[0] == '#')
|
if (!type || type[0] == '#')
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@@ -1394,7 +1459,7 @@ static void tmpfiles(char *line)
|
|||||||
if (strchr("vqQ", type[0]))
|
if (strchr("vqQ", type[0]))
|
||||||
type[0] = 'd';
|
type[0] = 'd';
|
||||||
|
|
||||||
path = strtok(NULL, "\t ");
|
path = field(&line);
|
||||||
if (!path)
|
if (!path)
|
||||||
return;
|
return;
|
||||||
if (strchr(path, '%')) {
|
if (strchr(path, '%')) {
|
||||||
@@ -1415,7 +1480,7 @@ static void tmpfiles(char *line)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
token = strtok(NULL, "\t ");
|
token = field(&line);
|
||||||
if (token) {
|
if (token) {
|
||||||
for (; *token == '~' || *token == ':'; token++) {
|
for (; *token == '~' || *token == ':'; token++) {
|
||||||
if (*token == '~')
|
if (*token == '~')
|
||||||
@@ -1430,14 +1495,14 @@ static void tmpfiles(char *line)
|
|||||||
perm.mode = 0;
|
perm.mode = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
user = strtok(NULL, "\t ");
|
user = field(&line);
|
||||||
if (user && user[0] == ':') {
|
if (user && user[0] == ':') {
|
||||||
perm.new_uid = 1;
|
perm.new_uid = 1;
|
||||||
user++;
|
user++;
|
||||||
}
|
}
|
||||||
if (user && !strcmp(user, "-"))
|
if (user && !strcmp(user, "-"))
|
||||||
user = NULL;
|
user = NULL;
|
||||||
group = strtok(NULL, "\t ");
|
group = field(&line);
|
||||||
if (group && group[0] == ':') {
|
if (group && group[0] == ':') {
|
||||||
perm.new_gid = 1;
|
perm.new_gid = 1;
|
||||||
group++;
|
group++;
|
||||||
@@ -1459,13 +1524,10 @@ static void tmpfiles(char *line)
|
|||||||
if (group && perm.gid < 0 && debug)
|
if (group && perm.gid < 0 && debug)
|
||||||
warnx("Unknown group %s, not setting the group of %s", group, path);
|
warnx("Unknown group %s, not setting the group of %s", group, path);
|
||||||
|
|
||||||
age = strtok(NULL, "\t ");
|
age = field(&line);
|
||||||
arg = strtok(NULL, "\n");
|
arg = line + strspn(line, "\t ");
|
||||||
if (arg) {
|
if (!arg[0] || !strcmp(arg, "-"))
|
||||||
arg += strspn(arg, "\t ");
|
arg = NULL;
|
||||||
if (!arg[0] || !strcmp(arg, "-"))
|
|
||||||
arg = NULL;
|
|
||||||
}
|
|
||||||
if (arg && strchr(arg, '%') && strchr("CfFlLtTw", type[0])) {
|
if (arg && strchr(arg, '%') && strchr("CfFlLtTw", type[0])) {
|
||||||
arg = expand(arg, argbuf, sizeof(argbuf));
|
arg = expand(arg, argbuf, sizeof(argbuf));
|
||||||
if (!arg) {
|
if (!arg) {
|
||||||
|
|||||||
@@ -86,3 +86,19 @@ CONF
|
|||||||
run "/libexec/finit/tmpfiles --create $CONF"
|
run "/libexec/finit/tmpfiles --create $CONF"
|
||||||
assert "All hex digits" "$(texec cat $DIR/hex)" = "9AJk"
|
assert "All hex digits" "$(texec cat $DIR/hex)" = "9AJk"
|
||||||
assert "No comment in line" "$(texec cat $DIR/hash)" = "a#b c"
|
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"
|
||||||
Reference in new issue
Block a user