extended input validation

This commit is contained in:
2025-11-03 09:15:14 +01:00
parent 567932f03c
commit bc06a2a279

View File

@@ -67,30 +67,11 @@ usage() {
printf -- " -h | -help | --help Show this help screen\n\n"
}
#
# Function inputcheck
#
inputcheck () {
[[ -z "$hostname" ]] && { usage; echo "ERROR: DOMAIN CANNOT BE EMPTY!"; exit 1; }
[[ "$hostname" =~ [[:space:]/] ]] && { echo "ERROR: DOMAIN INVALID (no spaces or slashes allowed)"; exit 1; }
[[ -z "$dbpass" ]] && { usage; echo "ERROR: DATABASE PASSWORD CANNOT BE EMPTY!"; exit 1; }
}
#
# Let's go
#
clear
#
# Try to determine the latest Zabbix version
#
zabbix_version=$(curl -s https://repo.zabbix.com/zabbix/ | grep -oP '(?<=href=")[0-9]+\.[0]' | sort -V | tail -1)
# Use fallback version if fetch failed
if [[ -z "$zabbix_version" ]]; then printf "Could not determine latest Zabbix version. Falling back to version %s\n\n" "$fallbackversion" ; zabbix_version="$fallbackversion" ; fi
printf "Using Zabbix version: %s\n\n" "$zabbix_version"
#
# Configure command line options
#
@@ -108,7 +89,54 @@ while getopts ":n:p:m:a:h" option; do
\?) usage; echo ""; echo "Error! Invalid option: -$OPTARG"; echo ""; exit 1;;
esac
done
inputcheck "$hostname" "$dbpass"
#
# Validate hostname and DB password
#
if [[ -z "$hostname" || -z "$dbpass" ]]; then
usage
printf "\nERROR: Both -n (domain) and -p (database password) are required.\n\n"
exit 1
fi
# Convert to lowercase (domains are case-insensitive)
hostname=$(echo "$hostname" | tr '[:upper:]' '[:lower:]')
# Disallow leading hyphen
if [[ "$hostname" =~ ^- ]]; then
printf "\nERROR: Domain cannot start with a hyphen.\n"
printf "Example of valid input: zabbix.example.com\n\n"
exit 1
fi
# Disallow spaces, slashes, underscores
if [[ "$hostname" =~ [[:space:]/_] ]]; then
printf "\nERROR: Domain cannot contain spaces, slashes, or underscores.\n\n"
exit 1
fi
# RFC 1123 validation
if [[ ! "$hostname" =~ ^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$ ]]; then
printf "\nERROR: Invalid domain format.\n"
printf "Example of valid input: zabbix.example.com\n\n"
exit 1
fi
# DB password sanity check
if [[ "$dbpass" =~ [[:space:]] ]]; then
printf "\nERROR: Database password cannot contain spaces.\n\n"
exit 1
fi
#
# Try to determine the latest Zabbix version
#
zabbix_version=$(curl -s https://repo.zabbix.com/zabbix/ | grep -oP '(?<=href=")[0-9]+\.[0]' | sort -V | tail -1)
# Use fallback version if fetch failed
if [[ -z "$zabbix_version" ]]; then printf "Could not determine latest Zabbix version. Falling back to version %s\n\n" "$fallbackversion" ; zabbix_version="$fallbackversion" ; fi
printf "Using Zabbix version: %s\n\n" "$zabbix_version"
#
# Download and install the Zabbix repository