From e83923554ab4cd370b7a00e6906976fc8f9eb72f Mon Sep 17 00:00:00 2001 From: Brad Warren Date: Tue, 25 Jul 2017 14:21:08 -0700 Subject: [PATCH] Preserve select environment variables with sudo. --- letsencrypt-auto-source/letsencrypt-auto | 10 ++++++++-- letsencrypt-auto-source/letsencrypt-auto.template | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/letsencrypt-auto-source/letsencrypt-auto b/letsencrypt-auto-source/letsencrypt-auto index 7a6ac36c3..3d2f5df59 100755 --- a/letsencrypt-auto-source/letsencrypt-auto +++ b/letsencrypt-auto-source/letsencrypt-auto @@ -159,6 +159,12 @@ su_sudo() { su root -c "$args" } +# Run the specified command with sudo but preserve select environment +# variables. +sudo_with_env() { + sudo LE_AUTO_DIR_TEMPLATE="$LE_AUTO_DIR_TEMPLATE" LE_AUTO_JSON_URL="$LE_AUTO_JSON_URL" LE_AUTO_PUBLIC_KEY="$LE_AUTO_PUBLIC_KEY" LE_PYTHON="$LE_PYTHON" OLD_VENV_PATH="$OLD_VENV_PATH" VENV_PATH="$VENV_PATH" "$@" +} + if [ "$1" = "--cb-auto-has-root" ]; then shift 1 elif [ "$1" != "--le-auto-phase2" ]; then @@ -170,7 +176,7 @@ elif [ "$1" != "--le-auto-phase2" ]; then SUDO=su_sudo ;; sudo) - SUDO=sudo + SUDO=sudo_with_env ;; '') ;; # Nothing to do for plain root method. *) @@ -181,7 +187,7 @@ elif [ "$1" != "--le-auto-phase2" ]; then else if test "`id -u`" -ne "0" ; then if $EXISTS sudo 1>/dev/null 2>&1; then - SUDO=sudo + SUDO=sudo_with_env else say \"sudo\" is not available, will use \"su\" for installation steps... SUDO=su_sudo diff --git a/letsencrypt-auto-source/letsencrypt-auto.template b/letsencrypt-auto-source/letsencrypt-auto.template index cc578ff1b..500275b62 100755 --- a/letsencrypt-auto-source/letsencrypt-auto.template +++ b/letsencrypt-auto-source/letsencrypt-auto.template @@ -159,6 +159,12 @@ su_sudo() { su root -c "$args" } +# Run the specified command with sudo but preserve select environment +# variables. +sudo_with_env() { + sudo LE_AUTO_DIR_TEMPLATE="$LE_AUTO_DIR_TEMPLATE" LE_AUTO_JSON_URL="$LE_AUTO_JSON_URL" LE_AUTO_PUBLIC_KEY="$LE_AUTO_PUBLIC_KEY" LE_PYTHON="$LE_PYTHON" OLD_VENV_PATH="$OLD_VENV_PATH" VENV_PATH="$VENV_PATH" "$@" +} + if [ "$1" = "--cb-auto-has-root" ]; then shift 1 elif [ "$1" != "--le-auto-phase2" ]; then @@ -170,7 +176,7 @@ elif [ "$1" != "--le-auto-phase2" ]; then SUDO=su_sudo ;; sudo) - SUDO=sudo + SUDO=sudo_with_env ;; '') ;; # Nothing to do for plain root method. *) @@ -181,7 +187,7 @@ elif [ "$1" != "--le-auto-phase2" ]; then else if test "`id -u`" -ne "0" ; then if $EXISTS sudo 1>/dev/null 2>&1; then - SUDO=sudo + SUDO=sudo_with_env else say \"sudo\" is not available, will use \"su\" for installation steps... SUDO=su_sudo