added -n and -p options to pivpn add

::: Create a client ovpn profile, optional nopass
:::
::: Usage: pivpn <-a|add> [-n|--name <arg>] [-p|--password
<arg>]|[nopass] [-h|--help]
:::
::: Commands:
:::  nopass               Create a client without a password
:::  -n,--name            Name for the Client (default: 'raspberrypi')
:::  -p,--password        Password for the Client (no default)
:::  -h,--help            Show this help dialog
This commit is contained in:
EWouters 2017-03-15 00:49:25 +13:00
parent 277a212a8f
commit 7a65f083c4

View file

@ -10,6 +10,59 @@ TA="ta.key"
INDEX="/etc/openvpn/easy-rsa/pki/index.txt" INDEX="/etc/openvpn/easy-rsa/pki/index.txt"
INSTALL_USER=$(cat /etc/pivpn/INSTALL_USER) INSTALL_USER=$(cat /etc/pivpn/INSTALL_USER)
helpFunc() {
echo "::: Create a client ovpn profile, optional nopass"
echo ":::"
echo "::: Usage: pivpn <-a|add> [-n|--name <arg>] [-p|--password <arg>]|[nopass] [-h|--help]"
echo ":::"
echo "::: Commands:"
echo "::: nopass Create a client without a password"
echo "::: -n,--name Name for the Client (default: '"$(hostname)"')"
echo "::: -p,--password Password for the Client (no default)"
echo "::: -h,--help Show this help dialog"
}
# Parse input arguments
while test $# -gt 0
do
_key="$1"
case "$_key" in
-n|--name|--name=*)
_val="${_key##--name=}"
if test "$_val" = "$_key"
then
test $# -lt 2 && echo "Missing value for the optional argument '$_key'." && exit 1
_val="$2"
shift
fi
NAME="$_val"
;;
-p|--password|--password=*)
_val="${_key##--password=}"
if test "$_val" = "$_key"
then
test $# -lt 2 && echo "Missing value for the optional argument '$_key'." && exit 1
_val="$2"
shift
fi
PASSWD="$_val"
;;
-h|--help)
helpFunc
exit 0
;;
nopass)
NO_PASS="1"
;;
*)
echo "Error: Got an unexpected argument '$1'"
helpFunc
exit 1
;;
esac
shift
done
# Functions def # Functions def
function keynoPASS() { function keynoPASS() {
@ -27,6 +80,7 @@ EOF
function keyPASS() { function keyPASS() {
if [[ -z "${PASSWD}" ]]; then
stty -echo stty -echo
while true while true
do do
@ -46,6 +100,7 @@ function keyPASS() {
echo "pivpn add nopass" echo "pivpn add nopass"
exit 1 exit 1
fi fi
fi
if [ ${#PASSWD} -lt 4 ] || [ ${#PASSWD} -gt 1024 ] if [ ${#PASSWD} -lt 4 ] || [ ${#PASSWD} -gt 1024 ]
then then
echo "Password must be between from 4 to 1024 characters" echo "Password must be between from 4 to 1024 characters"
@ -69,8 +124,10 @@ EOF
} }
if [ -z "${NAME}" ]; then
printf "Enter a Name for the Client: " printf "Enter a Name for the Client: "
read -r NAME read -r NAME
fi
if [[ "${NAME}" =~ [^a-zA-Z0-9] ]]; then if [[ "${NAME}" =~ [^a-zA-Z0-9] ]]; then
echo "Name can only contain alphanumeric characters." echo "Name can only contain alphanumeric characters."
@ -109,8 +166,11 @@ fi
cd /etc/openvpn/easy-rsa || exit cd /etc/openvpn/easy-rsa || exit
if [[ "$@" =~ "nopass" ]]; then if [[ "${NO_PASS}" =~ "1" ]]; then
keynoPASS keynoPASS
elif [[ -n "${PASSWD}" ]]; then
echo "Both nopass and password arguments passed to the script. Please use either one."
exit 1
else else
keyPASS keyPASS
fi fi