fix(scripts): Evaluate client names correctly

Fix issue #1639
Add extra check for empty spaces
Fix client names not being checked when using pivpn add -n
This commit is contained in:
4s3ti 2022-10-07 23:59:57 +02:00
parent 469a765916
commit 79f7caf4d3
No known key found for this signature in database
GPG key ID: AC2D3B898F96BC51
2 changed files with 64 additions and 43 deletions

View file

@ -11,11 +11,6 @@ CA="ca.crt"
TA="ta.key" TA="ta.key"
INDEX="/etc/openvpn/easy-rsa/pki/index.txt" INDEX="/etc/openvpn/easy-rsa/pki/index.txt"
if [[ ! -f "${setupVars}" ]]; then
err "::: Missing setup vars file!"
exit 1
fi
# shellcheck disable=SC1090 # shellcheck disable=SC1090
source "${setupVars}" source "${setupVars}"
@ -23,6 +18,11 @@ err() {
echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2
} }
if [[ ! -f "${setupVars}" ]]; then
err "::: Missing setup vars file!"
exit 1
fi
helpFunc() { helpFunc() {
echo "::: Create a client ovpn profile, optional nopass" echo "::: Create a client ovpn profile, optional nopass"
echo ":::" echo ":::"
@ -46,6 +46,29 @@ helpFunc() {
echo "::: -h,--help Show this help dialog" echo "::: -h,--help Show this help dialog"
} }
checkName() {
# check name
if [[ "${NAME}" =~ [^a-zA-Z0-9.@_-] ]]; then
err "Name can only contain alphanumeric characters and these symbols (.-@_)."
exit 1
elif [[ "${NAME}" =~ ^[0-9]+$ ]]; then
err "Names cannot be integers."
exit 1
elif [[ "${NAME}" =~ \ |\' ]]; then
err "Names cannot contain spaces."
exit 1
elif [[ "${NAME:0:1}" == "-" ]]; then
err "Name cannot start with - (dash)"
exit 1
elif [[ "${NAME::1}" == "." ]]; then
err "Names cannot start with a . (dot)."
exit 1
elif [[ -z "${NAME}" ]]; then
err "::: You cannot leave the name blank."
exit 1
fi
}
if [[ -z "${HELP_SHOWN}" ]]; then if [[ -z "${HELP_SHOWN}" ]]; then
helpFunc helpFunc
echo echo
@ -70,6 +93,7 @@ while [[ "$#" -gt 0 ]]; do
fi fi
NAME="${_val}" NAME="${_val}"
checkName
;; ;;
-p | --password | --password=*) -p | --password | --password=*)
_val="${_key##--password=}" _val="${_key##--password=}"
@ -175,17 +199,8 @@ useBitwarden() {
printf "Enter the username: " printf "Enter the username: "
read -r NAME read -r NAME
# check name #check name
until [[ "${NAME}" =~ ^[a-zA-Z0-9.@_-]+$ ]] \ checkName
&& [[ "${NAME::1}" != "." ]] \
&& [[ "${NAME::1}" != "-" ]]; do
echo -n "Name can only contain alphanumeric characters and these "
echo -n "characters (.-@_). The name also cannot start with a dot (.)"
echo " or a dash (-). Please try again."
# ask user for username again
printf "Enter the username: "
read -r NAME
done
# ask user for length of password # ask user for length of password
printf "Please enter the length of characters you want your password to be " printf "Please enter the length of characters you want your password to be "
@ -273,18 +288,9 @@ fi
if [[ -z "${NAME}" ]]; then if [[ -z "${NAME}" ]]; then
printf "Enter a Name for the Client: " printf "Enter a Name for the Client: "
read -r NAME read -r NAME
elif [[ "${NAME::1}" == "." ]] || [[ "${NAME::1}" == "-" ]]; then checkName
err "Names cannot start with a dot (.) or a dash (-)." else
exit 1 checkName
elif [[ "${NAME}" =~ [^a-zA-Z0-9.@_-] ]]; then
err "Name can only contain alphanumeric characters and these symbols (.-@_)."
exit 1
elif [[ "${NAME}" =~ ^[0-9]+$ ]]; then
err "Names cannot be integers."
exit 1
elif [[ -z "${NAME}" ]]; then
err "You cannot leave the name blank."
exit 1
fi fi
if [[ "${GENOVPNONLY}" == 1 ]]; then if [[ "${GENOVPNONLY}" == 1 ]]; then

View file

@ -31,6 +31,32 @@ helpFunc() {
echo "::: -h,--help Show this help dialog" echo "::: -h,--help Show this help dialog"
} }
checkName() {
# check name
if [[ "${CLIENT_NAME}" =~ [^a-zA-Z0-9.@_-] ]]; then
err "Name can only contain alphanumeric characters and these symbols (.-@_)."
exit 1
elif [[ "${CLIENT_NAME}" =~ ^[0-9]+$ ]]; then
err "Names cannot be integers."
exit 1
elif [[ "${CLIENT_NAME}" =~ \ |\' ]]; then
err "Names cannot contain spaces."
exit 1
elif [[ "${CLIENT_NAME:0:1}" == "-" ]]; then
err "Name cannot start with - (dash)"
exit 1
elif [[ "${CLIENT_NAME::1}" == "." ]]; then
err "Names cannot start with a . (dot)."
exit 1
elif [[ -z "${CLIENT_NAME}" ]]; then
err "::: You cannot leave the name blank."
exit 1
elif [[ -f "configs/${CLIENT_NAME}.conf" ]]; then
err "::: A client with this name already exists"
exit 1
fi
}
# Parse input arguments # Parse input arguments
while [[ "$#" -gt 0 ]]; do while [[ "$#" -gt 0 ]]; do
_key="${1}" _key="${1}"
@ -49,6 +75,7 @@ while [[ "$#" -gt 0 ]]; do
fi fi
CLIENT_NAME="${_val}" CLIENT_NAME="${_val}"
checkName
;; ;;
-h | --help) -h | --help)
helpFunc helpFunc
@ -77,21 +104,9 @@ cd /etc/wireguard || exit
if [[ -z "${CLIENT_NAME}" ]]; then if [[ -z "${CLIENT_NAME}" ]]; then
read -r -p "Enter a Name for the Client: " CLIENT_NAME read -r -p "Enter a Name for the Client: " CLIENT_NAME
elif [[ "${CLIENT_NAME}" =~ [^a-zA-Z0-9.@_-] ]]; then checkName
err "Name can only contain alphanumeric characters and these symbols (.-@_)." else
exit 1 checkName
elif [[ "${CLIENT_NAME:0:1}" == "-" ]]; then
err "Name cannot start with -"
exit 1
elif [[ "${CLIENT_NAME}" =~ ^[0-9]+$ ]]; then
err "Names cannot be integers."
exit 1
elif [[ -z "${CLIENT_NAME}" ]]; then
err "::: You cannot leave the name blank."
exit 1
elif [[ -f "configs/${CLIENT_NAME}.conf" ]]; then
err "::: A client with this name already exists"
exit 1
fi fi
wg genkey \ wg genkey \