diff --git a/salt/common/tools/sbin/so-common b/salt/common/tools/sbin/so-common index 5198ebeb0..7273a84c2 100755 --- a/salt/common/tools/sbin/so-common +++ b/salt/common/tools/sbin/so-common @@ -451,6 +451,23 @@ valid_ip4() { echo "$ip" | grep -qP '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$' && return 0 || return 1 } +valid_ip4_cidr_mask() { + # Verify there is a backslash in the string + echo "$1" | grep -qP "^[^/]+/[^/]+$" || return 1 + + local cidr + local ip + + cidr=$(echo "$1" | sed 's/.*\///') + ip=$(echo "$1" | sed 's/\/.*//' ) + + if valid_ip4 "$ip"; then + [[ $cidr =~ ^([0-9]|[1-2][0-9]|3[0-2])$ ]] && return 0 || return 1 + else + return 1 + fi +} + valid_int() { local num=$1 local min=${2:-1} diff --git a/setup/so-functions b/setup/so-functions index 627ec934f..58cf91f55 100755 --- a/setup/so-functions +++ b/setup/so-functions @@ -449,7 +449,7 @@ collect_hostname_validate() { collect_int_ip_mask() { whiptail_management_interface_ip_mask - while ! valid_cidr "$manager_ip_mask"; do + while ! valid_ip4_cidr_mask "$manager_ip_mask"; do whiptail_invalid_input whiptail_management_interface_ip_mask "$manager_ip_mask" done diff --git a/setup/so-whiptail b/setup/so-whiptail index 0cb69404f..ba7adee40 100755 --- a/setup/so-whiptail +++ b/setup/so-whiptail @@ -1008,7 +1008,7 @@ whiptail_management_interface_ip_mask() { manager_ip_mask=$(whiptail --title "$whiptail_title" --inputbox "$msg" 12 60 "$1" 3>&1 1>&2 2>&3) local exitstatus=$? - # whiptail_check_exitstatus $exitstatus + whiptail_check_exitstatus $exitstatus } whiptail_management_nic() { diff --git a/tests/validation.sh b/tests/validation.sh index cfec11198..6f396141e 100644 --- a/tests/validation.sh +++ b/tests/validation.sh @@ -80,6 +80,22 @@ test_fun 1 valid_cidr "" sleep 0.15 +header "ip4 with CIDR mask" + +test_fun 0 valid_ip4_cidr_mask "192.168.1.12/24" + +test_fun 0 valid_ip4_cidr_mask "192.168.1.12/16" + +test_fun 1 valid_ip4_cidr_mask "192.168.9.12/54" + +test_fun 1 valid_cidr "192.168.1.0" + +test_fun 1 valid_ip4 "192.168.1.0/" + +test_fun 1 valid_ip4 "/24" + +test_fun 1 valid_cidr "" + header "CIDR list" test_fun 0 valid_cidr_list "10.0.0.0/8,192.168.0.0/16,172.16.0.0/12"