From e1b2962f0f2bf976c5f7d2bb8e9c53f896ef84b5 Mon Sep 17 00:00:00 2001 From: Darren 'Tadgy' Austin Date: Wed, 18 Nov 2020 16:11:07 +0000 Subject: Various updates to netconfig - see full log. * Rename -syntax_check_color function to syntax_check_v4 as it more accurately represents its IPv4 specific function now. * Some updates to text presented in dialogs. * Protect some variable expansions with ""s. * Initial support for IPADDRS/IP6ADDRS arrays. * Improve validation of user supplied input. * Refactoring for use of the IPADDRS array and multiple IP/mask entry. --- netconfig | 170 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 90 insertions(+), 80 deletions(-) (limited to 'netconfig') diff --git a/netconfig b/netconfig index bad070e..48b2d4d 100644 --- a/netconfig +++ b/netconfig @@ -366,7 +366,7 @@ fi # usage: syntax_check ADDRESS #-OF-EXPECTED-SEGMENTS (up to 4) # example: syntax_check 123.22.43.1 4 # returns: 0=found correct 1=too many fields 2=non numeric field found -syntax_check_color() { +syntax_check_v4() { RET_CODE=0 SCRATCH=$1 SCRATCH=`echo $SCRATCH | tr "." "/"` @@ -387,11 +387,13 @@ syntax_check_color() { fi if [ "$3" = "WARN" -a ! "$RET_CODE" = "0" ]; then cat << EOF > $TMP/tempmsg -The value you have entered seems to be non-standard. We were -expecting $2 groups of numbers seperated by dots, like: 127.0.0.1 -Please enter a valid value. +The IP address you entered, $1, is not valid. +There should be $2 groups of numbers seperated by dots, for example: + 192.168.1.1 + +Please correct any mistakes in the IP. EOF - dialog --title "INVALID VALUE" --defaultno --msgbox "`cat $TMP/tempmsg`" 7 72 + dialog --title "INVALID VALUE" --defaultno --msgbox "`cat $TMP/tempmsg`" 9 72 rm -r $TMP/tempmsg else if [ "$3" = "ECHO" ]; then @@ -418,12 +420,14 @@ USE_DHCP="" # Use DHCP? "" == no. USE_DHCP6="" DHCP_HOSTNAME="" # This is our DHCP hostname. NETWORK=127.0.0.0 +IPADDRS=() +IP6ADDRS=() +# FIXME: Now deprecated: IPADDR=127.0.0.1 NETMASK=24 # Use CIDR now. IPADDR6=::1 PREFIX6=64 -# Main loop: while [ 0 ]; do cat << EOF > $TMP/tempmsg First, we'll need the name you'd like to give your host. @@ -433,17 +437,19 @@ For example: darkstar Enter hostname: EOF dialog --title "ENTER HOSTNAME" --inputbox "`cat $TMP/tempmsg`" 12 64 \ - $HOSTNM 2> $TMP/SeThost + "$HOSTNM" 2> $TMP/SeThost if [ $? = 1 -o $? = 255 ]; then rm -f $TMP/SeThost $TMP/tempmsg exit fi HOSTNM="`cat $TMP/SeThost`" rm -f $TMP/SeThost $TMP/tempmsg - if [ "$HOSTNM" = "" ] || [[ ! "$HOSTNM" =~ ^[[:alnum:]]+(-)*[[:alnum:]]+$ ]]; then + if [ "$HOSTNM" = "" ] || [[ ! "$HOSTNM" =~ ^[[:alnum:]-]+$ ]] || \ + [ "${HOSTNM:0:1}" == "-" ] || [ "${HOSTNM: -1:1}" == "-" ]; then dialog --title "HOSTNAME" --ok-button "Back" --msgbox \ - "That doesn't appear to be a valid hostname.\n -Hostnames can only contain letters, numbers and hyphens." 6 60 + "That doesn't appear to be a valid hostname.\n +Hostnames can only contain letters, numbers and hyphens, +and may not begin or end with a hyphen." 7 60 if [ $? = 1 -o $? = 255 ]; then exit fi @@ -458,22 +464,23 @@ Now, we need the domain name for this machine, such as: example.org Do not supply a leading '.' -Enter domain name for $HOSTNM: +Enter domain name for $HOSTNM: EOF dialog --title "ENTER DOMAINNAME FOR '$HOSTNM'" --inputbox \ -"`cat $TMP/tempmsg`" \ -12 59 $DOMAIN 2> $TMP/SeTdom + "`cat $TMP/tempmsg`" 12 59 "$DOMAIN" 2> $TMP/SeTdom if [ $? = 1 -o $? = 255 ]; then rm -f $TMP/SeTdom $TMP/tempmsg exit fi DOMAIN="`cat $TMP/SeTdom`" rm -f $TMP/SeTdom $TMP/tempmsg - # This regex doesn't catch everything, just basic syntax mistakes. - if [ "$DOMAIN" = "" ] || [[ ! "$DOMAIN" =~ ^([[:alnum:]]+[-.]*[[:alnum:]]+)+$ ]]; then + # These regexes doon't catch everything, just basic syntax mistakes. + if [ "$DOMAIN" = "" ] || [[ ! "$DOMAIN" =~ ^([[:alnum:]]+[-.]*[[:alnum:]]+)+$ ]] || \ + [[ "${DOMAIN:0:1}" =~ ^[.-]$ ]] || [[ "${DOMAIN: -1:1}" == ^[.-]$ ]]; then dialog --title "DOMAINNAME" --ok-button "Back" --msgbox \ "That doesn't appear to be a valid domain name.\n -Domain names can only contain letters, numbers hyphens and dots." 6 68 +Domain names can only contain letters and numbers, hyphens +and dots, and may not begin or end with a hyphen or dot." 7 68 if [ $? = 1 -o $? = 255 ]; then exit fi @@ -570,12 +577,16 @@ pure IPv6 network, or 'Both' to try and set up both types of network." 13 73 3 \ USE_DHCP="yes" USE_DHCP6="yes" fi - dialog --title "SET DHCP HOSTNAME" --inputbox "Some network providers require \ + dialog --title "SET DHCP HOSTNAME" --cancel-label "Skip" \ + --inputbox "Some network providers require \ that the DHCP hostname be set in order to connect. If so, they'll have assigned \ a hostname to your machine, which may look something like CC-NUMBER-A (this \ depends on your ISP). If you were assigned a DHCP hostname, please enter it \ -below. If you do not have a DHCP hostname, just hit ENTER or Cancel." 13 62 \ -2> $TMP/SeTDHCPHOST +below. If you do not have a DHCP hostname, just hit ENTER or select 'Skip'." 13 62 \ + 2> $TMP/SeTDHCPHOST + if [ "$?" = "255" ]; then + exit + fi NEW_DHCPHOST="`cat $TMP/SeTDHCPHOST`" rm -f $TMP/SeTDHCPHOST # OK, if we actually got something, use it. @@ -595,96 +606,95 @@ fi if [ "$LOOPBACK" = "no" -a ! "$USE_DHCP" = "yes" -a ! "$USE_SLAAC" = "yes" ]; then while [ 0 ]; do while [ 0 ]; do - if [ -r $TMP/SeTIP ]; then - IPADDR=`cat $TMP/SeTIP` + if [ -r $TMP/SeTIPSv4 ]; then + IPADDRS=($(cat $TMP/SeTIPSv4)) fi - dialog --title "ENTER IPv4 ADDRESS FOR '$HOSTNM.$DOMAIN'" --inputbox \ -"Enter the IPv4 address for the local machine. Example: 111.112.113.114 -If you do not want to configure a static IPv4 address, just hit ENTER. " \ - 9 75 $IPADDR 2> $TMP/SeTlip - if [ $? = 1 -o $? = 255 ]; then + dialog --title "ENTER IPv4 ADDRESSES" --cancel-label "Skip" --inputbox \ +"Enter the IPv4 addresses for this host. You may list one or more, +space delimited, IPv4 addresses with net masks in the format: + / +For example: + 111.112.113.114/24 +If a net mask is not specified a default of 24 will be used. + +The first address entered will become the primary IPv4 address +of this host. If you do not want to configure a static IPv4 +address, just hit ENTER or select 'Skip' " \ + 17 69 "${IPADDRS[*]}" 2> $TMP/SeTlip + ERRNO="$?" + if [ "$ERRNO" = "1" ]; then + break + elif [ "$ERRNO" = "255" ]; then rm -f $TMP/SeTlip exit fi - IPADDR="`cat $TMP/SeTlip`" + IPADDRS=($(cat $TMP/SeTlip)) rm -f $TMP/SeTlip - if [ "$IPADDR" = "" ]; then - break - fi - syntax_check_color $IPADDR 4 WARN - if [ $? = 0 ]; then - echo $IPADDR > $TMP/SeTIP + if [ "${IPADDRS[*]}" = "" ]; then break fi - done - - if [ ! "$IPADDR" = "" ]; then - while [ 0 ]; do - if [ -r $TMP/SeTnetmask ]; then - NETMASK=`cat $TMP/SeTnetmask` - fi - dialog --title "ENTER NETMASK FOR IPv4 NETWORK" --inputbox \ -"Enter the netmask for your IPv4 network. -This will generally be a single number (eg, 24) -or look something like this: 255.255.255.0." 10 55 $NETMASK 2> $TMP/SeTnmask - if [ $? = 1 -o $? = 255 ]; then - rm -f $TMP/SeTnmask - exit - fi - NETMASK="`cat $TMP/SeTnmask`" - rm -f $TMP/SeTnmask - if [ "$NETMASK" = "" ]; then - dialog --title "NETMASK REQUIRED" --msgbox \ -"You have configured this host with a v4 -IP address - a netmask is required." 6 43 + for ((I=0; I < ${#IPADDRS[@]}; I++)); do + IP="${IPADDRS[$I]%%/*}" + MASK="${IPADDRS[$I]#*/}" + if [ "$IP" = "" ]; then + dialog --title "NO IPv4 ADDRESSES" --ok-button "Back" --msgbox \ + "No IP specified in entry: ${IPADDRS[$I]}." 5 46 continue fi - if (($NETMASK == 0)) 2>/dev/null || (($NETMASK > 32)) 2>/dev/null; then - dialog --title "NETMASK INVALID" --msgbox \ -"The netmask you entered is not valid. -Valid netmasks will be in the range 1-32." 6 45 - continue - elif (($NETMASK)) 2>/dev/null; then - echo $NETMASK > $TMP/SeTnetmask - break - else - syntax_check_color $NETMASK 4 WARN - if [ $? = 0 ]; then - echo $NETMASK > $TMP/SeTnetmask - break - fi + syntax_check_v4 "$IP" 4 WARN || continue 2 + if [ "$MASK" = "" ]; then + IPADDRS[$I]="$IP/24" + MASK="24" + fi + if [[ ! "$MASK" =~ ^[0-9]+$ ]] || (($MASK <= 0)) || (($MASK > 32)); then + dialog --title "INVALID NET MASK" --msgbox \ +"Invalid net mask specified for entry: ${IPADDRS[$I]}. +Valid net masks will be in the range 1-32." 6 70 + echo "${IPADDRS[@]}" >$TMP/SeTIPSv4 + continue 2 fi done + # echo "${IPADDRS[0]%%/*}" >$TMP/SeTIP + # echo "${IPADDRS[0]#*/}" >$TMP/SeTnetmask + echo "${IPADDRS[@]}" >$TMP/SeTIPSv4 + break + done + + if [ ! "${IPADDRS[*]}" = "" ]; then while [ 0 ]; do - if [ -r $TMP/SeTgateway ]; then - GATEWAY=`cat $TMP/SeTgateway` + if [ -r $TMP/SeTgateway4 ]; then + GATEWAY="`cat $TMP/SeTgateway4`" fi - dialog --title "ENTER IPv4 GATEWAY ADDRESS" --inputbox \ + dialog --title "ENTER IPv4 GATEWAY ADDRESS" --cancel-label "Skip" --inputbox \ "Enter the address for the IPv4 gateway on your network, such as: - `echo $IPADDR | cut -f 1-3 -d .`.1 + `echo ${IPADDRS[0]} | cut -f 1-3 -d .`.1 If you don't have an IPv4 gateway on your network just hit ENTER -without entering a gateway IP address. +without entering a gateway IP address, or select 'Skip'. -Enter IPv4 gateway address (aaa.bbb.ccc.ddd):" 14 68 $GATEWAY 2> $TMP/SeTgate +Enter IPv4 gateway address:" 14 68 "$GATEWAY" 2> $TMP/SeTgate4 if [ $? = 1 -o $? = 255 ]; then - rm -f $TMP/SeTgate + rm -f $TMP/SeTgate4 exit fi - GATEWAY="`cat $TMP/SeTgate`" - rm -f $TMP/SeTgate + GATEWAY="`cat $TMP/SeTgate4`" + rm -f $TMP/SeTgate4 if [ "$GATEWAY" = "" ]; then - echo > $TMP/SeTgateway + echo > $TMP/SeTgateway4 break + else + echo "$GATEWAY" >$TMP/SeTgateway4 fi - syntax_check_color $GATEWAY 4 WARN + syntax_check_v4 "$GATEWAY" 4 WARN if [ $? = 0 ]; then - echo $GATEWAY > $TMP/SeTgateway + echo $GATEWAY > $TMP/SeTgateway4 break fi done fi +exit + while [ 0 ]; do if [ -r $TMP/SeTIP6 ]; then IPADDR6=`cat $TMP/SeTIP6` -- cgit v1.2.3