From cd0e640970d569712077219d90a705bfa4ccd2d4 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Sat, 9 Feb 2013 21:17:29 +0100 Subject: Preserve (instead of collapse) arguments in su_monkeysphere_user(). It is a healthy coding practice to keep each argument separate when executing system calls, i.e. quote each variable separately instead of relying on whitespace to indicate argument separation. Quoting shell-inside-shell is tricky to do right, but not impossible: Bourne-derived shells treat single-quoting literally, which means that shell command arguments (i.e. an array of strings) should be safe to serialize (dual-quote) using these simple rules: b) each single-quote inside each string is escaped as '\'' a) each string is surrounded by single-quotes This patch applies above single-quote serialization for su_monkeysphere_user(), and adapts ma/update_users $STRICT_MODES on line 82 to match. If serialization routine should turn out to be broken, it will therefore only affect $STRICT_MODES in ma/update_users badly. All other variables passed through su_monkeysphere_user() should not possibly be treated any worse than previous non-quoting. --- src/share/common | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/share/common') diff --git a/src/share/common b/src/share/common index f9be05a..4dd46c8 100644 --- a/src/share/common +++ b/src/share/common @@ -100,15 +100,19 @@ su_monkeysphere_user() { # introduce an extra dependency just for this. This may be a # candidate for re-factoring if we switch implementation languages. + # singlequote-escape strings - like this bashism: + # printf -v CMDLINE "%q " "$@" + local CMDLINE="$(perl -0 -e "foreach (@ARGV) {s/'/'\\\\''/g; print \"'\$_' \"}" "$@")" + case $(id -un) in # if monkeysphere user, run the command under bash "$MONKEYSPHERE_USER") - bash -c "$*" + bash -c "$CMDLINE" ;; # if root, su command as monkeysphere user 'root') - su "$MONKEYSPHERE_USER" -c "$*" + su "$MONKEYSPHERE_USER" -c "$CMDLINE" ;; # otherwise, fail -- cgit v1.2.3