Skip to content

chown/chmod client file when name match system user (#961) #962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions openvpn-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1093,21 +1093,27 @@ function newClient() {
echo "Client $CLIENT added."
fi

# Home directory of the user, where the client configuration 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
if [ "${SUDO_USER}" == "root" ]; then
# If running sudo as root
homeDir="/root"
if [[ -z "$CLIENT_FILEPATH" ]]; then
# Home directory of the user, where the client configuration will be written
if [ -e "/home/${CLIENT}" ]; then
# if $1 is a user name
homeDir="/home/${CLIENT}"
CLIENT_OWNER="$CLIENT"
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}"
fi
CLIENT_OWNER="$SUDO_USER"
else
homeDir="/home/${SUDO_USER}"
# if not SUDO_USER, use /root
homeDir="/root"
fi
else
# if not SUDO_USER, use /root
homeDir="/root"

CLIENT_FILEPATH="$homeDir/$CLIENT.ovpn"
fi

# Determine if we use tls-auth or tls-crypt
Expand All @@ -1118,7 +1124,7 @@ function newClient() {
fi

# Generates the custom client.ovpn
cp /etc/openvpn/client-template.txt "$homeDir/$CLIENT.ovpn"
cp /etc/openvpn/client-template.txt "$CLIENT_FILEPATH"
{
echo "<ca>"
cat "/etc/openvpn/easy-rsa/pki/ca.crt"
Expand All @@ -1145,10 +1151,18 @@ function newClient() {
echo "</tls-auth>"
;;
esac
} >>"$homeDir/$CLIENT.ovpn"
} >>"$CLIENT_FILEPATH"

if [[ -n "$CLIENT_OWNER" ]]; then
echo "Setting owner permission for $CLIENT_FILEPATH"
CLIENT_OWNER_GROUP=$(id -gn "$CLIENT_OWNER")

chmod go-rw "$CLIENT_FILEPATH"
chown "$CLIENT_OWNER:$CLIENT_OWNER_GROUP" "$CLIENT_FILEPATH"
fi

echo ""
echo "The configuration file has been written to $homeDir/$CLIENT.ovpn."
echo "The configuration file has been written to $CLIENT_FILEPATH."
echo "Download the .ovpn file and import it in your OpenVPN client."

exit 0
Expand Down