Fix home dir detection when using sudo as root

Close https://github.com/angristan/openvpn-install/issues/780
This commit is contained in:
Stanislas Lange 2021-03-10 22:16:16 +01:00
parent d1de5c64fe
commit e965518dc7
No known key found for this signature in database
GPG Key ID: 710D9597C7EAD8CF

View File

@ -1084,12 +1084,20 @@ function newClient() {
echo "Client $CLIENT added."
fi
# Home directory of the user, where the client configuration (.ovpn) will be written
if [ -e "/home/$CLIENT" ]; then # if $1 is a user name
homeDir="/home/$CLIENT"
elif [ "${SUDO_USER}" ]; then # if not, use SUDO_USER
# Home directory of the user, where the client configuration will be written
if [ -e "/home/${CLIENT_NAME}" ]; then
# if $1 is a user name
homeDir="/home/${CLIENT_NAME}"
elif [ "${SUDO_USER}" ]; then
# if not, use SUDO_USER
if [ "${SUDO_USER}" == "root" ]; then
# If running sudo as root
homeDir="/root"
else
homeDir="/home/${SUDO_USER}"
else # if not SUDO_USER, use /root
fi
else
# if not SUDO_USER, use /root
homeDir="/root"
fi