From 28e96db99fd1589e60ac1e7a71f4cfaff4d44147 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Wed, 20 Apr 2016 15:36:02 +0100 Subject: [PATCH] Restore environment variables when becoming. Environment variables listed in .become/environment/user will be passed to the sudo shell. --- opt/bin/become | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/opt/bin/become b/opt/bin/become index d958646..4b1c0dc 100755 --- a/opt/bin/become +++ b/opt/bin/become @@ -59,6 +59,29 @@ for candidate in "$HOME" "$PROFILE_HOME"; do [ -f "$userprofile" ] || userprofile= done +# Preserve environment variables. +ignore_environment_user=0 +for candidate in "$HOME" "$PROFILE_HOME"; do + [ -n "$candidate" ] || continue + BECOME="$candidate/.become/environment" + [ -d "$BECOME" ] || continue + + # Script to run when becoming any user. + [ -z "$allusersenvironment" ] && allusersenvironment="$BECOME/all" + [ -f "$allusersenvironment" ] || allusersenvironment= + + # Set $HOME/all sticky to ignore $PROFILE_HOME/$user. + if [ "$candidate" = "$HOME" ]; then + [ -k "$BECOME/all" ] && ignore_environment_user=1 + else + [ $ignore_environment_user = 1 ] && continue + fi + + # Environment variables to preserve when becomeing this user. + [ -z "$userenvironment" ] && userenvironment="$BECOME/$user" + [ -f "$userenvironment" ] || userenvironment= +done + file="${TMPDIR:-/tmp}/$USER.become.$user.$RANDOM.$$" umask=$(builtin umask -p) builtin umask 077 @@ -93,8 +116,16 @@ else fi echo >&3 ". ${PROFILE_HOME:-$HOME}/.bash_profile" -[ -f "$allusersprofile" ] && cat >&3 2>/dev/null "$allusersprofile" -[ -f "$userprofile" ] && cat >&3 2>/dev/null "$userprofile" +for profile in "$allusersprofile" "$userprofile"; do + [ -f "$profile" ] && cat >&3 2>/dev/null "$profile" +done +for environment in "$allusersenvironment" "$userenvironment"; do + [ -f "$environment" ] || continue + while read -d $'\n' variable; do + variable=${variable%%=*} + eval "[ -z \${$variable:+set} ] || echo >&3 \"$variable='\$$variable'\"" + done < "$environment" +done [ $chdir = 1 ] && echo >&3 2>/dev/null "cd - &>/dev/null" exec 3>&- -- 2.7.4