|
|
43ff24c |
From b91985eca0db632daecf6e20b1d3718f0f884389 Mon Sep 17 00:00:00 2001
|
|
|
43ff24c |
From: =?UTF-8?q?Mantas=20Mikul=C4=97nas?= <grawity@gmail.com>
|
|
|
43ff24c |
Date: Sun, 13 Jul 2014 18:49:00 +0300
|
|
|
43ff24c |
Subject: [PATCH] fileio: quote more shell characters in envfiles
|
|
|
43ff24c |
|
|
|
43ff24c |
Turns out, making strings shell-proof is harder than expected:
|
|
|
43ff24c |
|
|
|
43ff24c |
# machinectl set-hostname "foo|poweroff" && . /etc/machine-info
|
|
|
43ff24c |
|
|
|
43ff24c |
(This could be simplified by quoting *and* escaping all characters,
|
|
|
43ff24c |
which is harmless in shell but unnecessary.)
|
|
|
43ff24c |
|
|
|
43ff24c |
(cherry picked from commit 0ce5a80601597fe4d1a715a8f70ce8d5ccaa2d86)
|
|
|
43ff24c |
---
|
|
|
43ff24c |
src/shared/fileio.c | 4 ++--
|
|
|
43ff24c |
src/shared/util.h | 6 ++++++
|
|
|
43ff24c |
2 files changed, 8 insertions(+), 2 deletions(-)
|
|
|
43ff24c |
|
|
|
43ff24c |
diff --git a/src/shared/fileio.c b/src/shared/fileio.c
|
|
|
43ff24c |
index b1de5908c5..d22770b772 100644
|
|
|
43ff24c |
--- a/src/shared/fileio.c
|
|
|
43ff24c |
+++ b/src/shared/fileio.c
|
|
|
43ff24c |
@@ -738,11 +738,11 @@ static void write_env_var(FILE *f, const char *v) {
|
|
|
43ff24c |
p++;
|
|
|
43ff24c |
fwrite(v, 1, p-v, f);
|
|
|
43ff24c |
|
|
|
43ff24c |
- if (string_has_cc(p, NULL) || chars_intersect(p, WHITESPACE "\'\"\\`$")) {
|
|
|
43ff24c |
+ if (string_has_cc(p, NULL) || chars_intersect(p, WHITESPACE SHELL_NEED_QUOTES)) {
|
|
|
43ff24c |
fputc('\"', f);
|
|
|
43ff24c |
|
|
|
43ff24c |
for (; *p; p++) {
|
|
|
43ff24c |
- if (strchr("\'\"\\`$", *p))
|
|
|
43ff24c |
+ if (strchr(SHELL_NEED_ESCAPE, *p))
|
|
|
43ff24c |
fputc('\\', f);
|
|
|
43ff24c |
|
|
|
43ff24c |
fputc(*p, f);
|
|
|
43ff24c |
diff --git a/src/shared/util.h b/src/shared/util.h
|
|
|
43ff24c |
index 7124e51d90..64b9fc6884 100644
|
|
|
43ff24c |
--- a/src/shared/util.h
|
|
|
43ff24c |
+++ b/src/shared/util.h
|
|
|
43ff24c |
@@ -93,6 +93,12 @@
|
|
|
43ff24c |
#define COMMENTS "#;"
|
|
|
43ff24c |
#define GLOB_CHARS "*?["
|
|
|
43ff24c |
|
|
|
43ff24c |
+/* What characters are special in the shell? */
|
|
|
43ff24c |
+/* must be escaped outside and inside double-quotes */
|
|
|
43ff24c |
+#define SHELL_NEED_ESCAPE "\"\\`$"
|
|
|
43ff24c |
+/* can be escaped or double-quoted */
|
|
|
43ff24c |
+#define SHELL_NEED_QUOTES SHELL_NEED_ESCAPE GLOB_CHARS "'()<>|&;"
|
|
|
43ff24c |
+
|
|
|
43ff24c |
#define FORMAT_BYTES_MAX 8
|
|
|
43ff24c |
|
|
|
43ff24c |
#define ANSI_HIGHLIGHT_ON "\x1B[1;39m"
|