#!/bin/ksh
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident  "@(#)se6000configure 1.20     03/07/24 SMI"
#

#####
# validateAddress()
# Check to make sure a string is formatted correctly
# to be and IP address.
#####

validateAddress()
{

  p1=$(echo $1|cut -f1 -d.)
  p2=$(echo $1|cut -f2 -d.)
  p3=$(echo $1|cut -f3 -d.)
  p4=$(echo $1|cut -f4 -d.)
  p5=$(echo $1|cut -f5 -d.)

#
# expr $x + 1000 is used throughout the script 
# to make sure that the field being tested is 
# a number. The only time this would fail is
# if the number being tested is -1000, and for
# all cases where this is used, -1000 is never
# valid.
#
  if ! expr $p1 + 1000 1>/dev/null 2>&1 ||
     ! expr $p2 + 1000 1>/dev/null 2>&1 ||
     ! expr $p3 + 1000 1>/dev/null 2>&1 ||
     ! expr $p4 + 1000 1>/dev/null 2>&1 
  then
    return 1
  fi

  if [ -n "$p1" ] && [ $p1 -ge 1 ] && [ $p1 -le 255 ] &&
     [ -n "$p2" ] && [ $p2 -ge 0 ] && [ $p2 -le 255 ] &&
     [ -n "$p3" ] && [ $p3 -ge 0 ] && [ $p3 -le 255 ] &&
     [ -n "$p4" ] && [ $p4 -ge 0 ] && [ $p4 -le 255 ] &&
     [ -z "$p5" ]
  then
    return 0
  fi
  return 1
}

#####
#findExistingSps()
#####

findExistingSps()
{
  if [ alreadyFoundSps -eq 0 ]
  then
    alreadyFoundSps=1
  else
    return 0
  fi

  myecho "$YOUR_CURRENT_SP_IS $currentSpName."
  myecho "$LOOK_FOR_EXISTING_SP"
  for i in 0 1 2 3 4 5 6 7
  do
    if [ "$currentSpName" != "sp$i" ]
    then
      myecho "$PING_SP sp$i ..."
      if ping sp$i 5 1>/dev/null 2>&1
      then
        if [ -z "$existingSps" ]
        then
          existingSps="$i"
          existingSpNames="sp$i"
        else
          existingSps="$existingSps,$i"
          existingSpNames="$existingSpNames,sp$i"
        fi
      fi
    fi
  done
  return 0
}

#####
# testNetwork()
#####

testNetwork()
{
  echoheader "$TEST_HOST_HEADER"
  gotit=0
  while [ $gotit -eq 0 ]
  do
    myprintf "$TEST_HOST_PROMPT "
    ans=$(myread)
    if validateAddress $ans
    then
      testHost=$(echo $ans | cut -f1,2,3,4 -d.)

      myecho "$TEST_HOST_WARNING"
      if ping $testHost 1>/dev/null 2>&1
      then
        myecho "$TEST_HOST_SUCCESS"
        return 0
      else
        myecho "$TEST_HOST_FAILURE" $testHost
        myprintf "$TEST_HOST_AGAIN"
        ans=$(myread)
        if [ "$ans" != "Y" ] && [ "$ans" != "y" ]
        then
	  return 1
        fi
      fi
    else
      myecho "$INVALID_VALUE"
    fi
  done
}


#####
# restoreConfig()
#####

restoreConfig()
{
  # NO LONGER SUPPORTED, BUT NEED TO LEAVE NUMBER IN DUE 
  # TO DOCUMENTATION.

  myecho "$RESTORE_NOT_SUPPORTED"
  myprintf "\n$PRESS_ENTER"
  ans=$(myread)
  return 0
}

#####
# unConfig()
#####

unConfig()
{
  echoheader "$UNCONFIG_HEADER"
  gotit=0
  while [ $gotit -eq 0 ]
  do
    myecho "$UNCONFIG_WARNING"
    myprintf "$CONFIRM_OPERATION_PROMPT"
    ans=$(myread)
    if [ "$ans" = "Y" ]  || [ "$ans" = "y" ]
    then
      storEdgeId=-1
      networkSetting="Fixed"
      newSpName="new_sp"
      newSpAddress=$(grep $newSpName /etc/hosts | grep -v "-" | cut -f1 -d' ')
      newRouterAddress=$(grep new_firewall /etc/hosts | cut -f1 -d' ')
      newNTCAddress=$(grep new_ntc /etc/hosts | cut -f1 -d' ')
      ipAddress="127.0.0.1"
      gatewayAddress="127.0.0.1"
      netmaskAddress="255.0.0.0"
      nameServerAddress=""
      domainNameAddress=""
      gotit=1
    elif [ "$ans" = "N" ] || [ "$ans" = "n" ]
    then 
      return 1
    else
      myecho "$INVALID_VALUE"
    fi
  done
  return 0
  
}

#####
# configPower()
#####

configPower()
{
  storEdgeId=-1
  echoheader "$CONFIG_POWER_HEADER"
  gotit=0
  while [ $gotit -eq 0 ]
  do
    if [ -f $POWER_SEQUENCER_FILE ]
    then
       myecho "$DISABLE_POWER_WARNING"
    else
       myecho "$ENABLE_POWER_WARNING"
    fi
    myprintf "$CONFIRM_OPERATION_PROMPT"
    ans=$(myread)
    if [ "$ans" = "Y" ]  || [ "$ans" = "y" ]
    then
      if [ -f $POWER_SEQUENCER_FILE ]
      then
         $VMCFG unconfigure -t ps
      else
         $VMCFG configure -t ps
      fi
      rc=$?
      gotit=1
    elif [ "$ans" = "N" ] || [ "$ans" = "n" ]
    then 
      return 1
    else
      myecho "$INVALID_VALUE"
    fi
  done
  return $rc
  
}


#####
# initialConfig()
#####

initialConfig()
{
  echoheader "$INITIAL_CONFIG_HEADER"
  myecho "$INITIAL_CONFIG_WARNING"

  findExistingSps

  gotit=0
  while [ $gotit -eq 0 ]
  do
    if [ -z "$existingSps" ]
    then
      myecho "$NO_SPS_FOUND"
      myecho "$STOREDGE_ID"
    else
      myecho "$FOLLOWING_SPS_FOUND $existingSpNames"
      myecho "$STOREDGE_ID_EXCLUDE $existingSps"
    fi
    
    myprintf "$YOUR_CHOICE"
    ans=$(myread)
    if expr $ans + 1000 1>/dev/null 2>&1 &&
       [ $ans -ge 0 ] && [ $ans -le 7 ] &&
       [ -z "$(echo $existingSps | grep $ans)" ]
    then
      gotit=1
    else
      myecho "$INVALID_VALUE"
    fi
  done
  storEdgeId=$ans
  newSpName="sp"$ans
  newSpAddress=$(grep $newSpName /etc/hosts | grep -v "-" | cut -f1 -d' ')
  newRouterAddress=$(grep $newSpName-firewall /etc/hosts | cut -f1 -d' ')
  newNTCAddress=$(grep $newSpName-ntc /etc/hosts | cut -f1 -d' ')
   
  echosubheader "$NETWORK_SETTINGS :"
  
  gotit=0
  while [ $gotit -eq 0 ]
  do
    myecho "$DHCP_OR_FIXED"
    myecho "$DHCP_PROMPT"
    myecho "$FIXED_PROMPT"
    myprintf "$YOUR_CHOICE"
    ans=$(myread)
    if expr $ans + 1000 1>/dev/null 2>&1 &&
       [ $ans -ge 1 ] && [ $ans -le 2 ]
    then
      gotit=1
    else
      myecho "$INVALID_VALUE"
    fi
  done
  if [ $ans -eq 1 ] 
  then
    networkSetting="DHCP"
    netmaskAddress="255.255.255.0"
  else
    networkSetting="Fixed"
  fi
   
  
  if [ "$networkSetting" = "Fixed" ]
  then
    gotit=0
    while [ $gotit -eq 0 ]
    do
      myprintf "$IP_ADDRESS_PROMPT"
      ans=$(myread)
      if validateAddress $ans
      then
        gotit=1
      else
        myecho "$INVALID_VALUE"
      fi
    done
    ipAddress=$(echo $ans | cut -f1,2,3,4 -d.)
   
    gotit=0
    while [ $gotit -eq 0 ]
    do
      myprintf "$GATEWAY_PROMPT"
      ans=$(myread)
      if validateAddress $ans
      then
        gotit=1
      else
        myecho "$INVALID_VALUE"
      fi
    done
    gatewayAddress=$(echo $ans | cut -f1,2,3,4 -d.)
   
    gotit=0
    while [ $gotit -eq 0 ]
    do
      myprintf "$MASK_PROMPT"
      ans=$(myread)
      if validateAddress $ans
      then
        gotit=1
      else
        myecho "$INVALID_VALUE"
      fi
    done
    netmaskAddress=$(echo $ans | cut -f1,2,3,4 -d.)
   
  fi

  gotit=0
  while [ $gotit -eq 0 ]
  do
    myprintf "$NAME_SERVER_PROMPT"
    ans=$(myread)
    if validateAddress $ans
    then
      gotit=1
    else
      myecho "$INVALID_VALUE"
    fi
  done
  nameServerAddress=$(echo $ans | cut -f1,2,3,4 -d.)


  gotit=0
  while [ $gotit -eq 0 ]
  do
    myprintf "$DOMAIN_NAME_PROMPT"
    ans=$(myread)
    gotit=1
  done
  domainNameAddress=$ans

  myecho "\n*************************************************************"
  echosubheader "$SYSTEM_SETTINGS"
  myecho "$DISPLAY_ID $storEdgeId"
  echosubheader "$NETWORK_SETTINGS"
  myecho "$DISPLAY_NETWORK_SETTING $networkSetting"

  if [ "$networkSetting" = "Fixed" ]
  then
    myecho "$DISPLAY_ADDRESS $ipAddress"
    myecho "$DISPLAY_GATEWAY $gatewayAddress"
    myecho "$DISPLAY_MASK $netmaskAddress"
  fi
  myecho "$DISPLAY_NAME_SERVER $nameServerAddress"
  myecho "$DISPLAY_DOMAIN_NAME $domainNameAddress"
  myecho "\n*************************************************************\n"

  gotit=0
  while [ $gotit -eq 0 ]
  do
    myprintf "$CONFIRM_SETTINGS_PROMPT"
    ans=$(myread)
    if [ "$ans" = "Y" ]  || [ "$ans" = "y" ]
    then
      return 0
    elif [ "$ans" = "N" ] || [ "$ans" = "n" ]
    then 
      return 1
    else
      myecho "$INVALID_VALUE"
    fi
  done

  return 0
}

#####
# setFirewall()
#####

setFirewall()
{
  touch /tmp/sleeping
  while [ -f /tmp/sleeping ]
  do
    myprintf '.';
    sleep 10
  done &

  cmd="$FW_CONFIG -d"

  if [ "$storEdgeId" = "-1" ]
  then
    cmd="$cmd --unconfigure"
  else
    if [ "$storEdgeId" = "0" ]
    then
      cmd="$cmd --dhcp_server en"
      cmd="$cmd --dhcp_dns_server $nameServerAddress"
    else
      cmd="$cmd --dhcp_server dis"
    fi

    cmd="$cmd --lan_ip_addr $newRouterAddress"
    cmd="$cmd --port_fwd_addr $newSpAddress"
  
    if [ "$networkSetting" = "Fixed" ]
    then
      cmd="$cmd --wan_ip_addr $ipAddress"
      cmd="$cmd --wan_gateway $gatewayAddress"
      cmd="$cmd --wan_subnet_mask $netmaskAddress"
      cmd="$cmd --wan_addr_type static"
    else
      cmd="$cmd --wan_addr_type dynamic"
    fi
  fi

  echo "Executing $FW_CONFIG with the following parameters" >> $CONFIGURE_LOG
  echo "$cmd" >> $CONFIGURE_LOG
  $cmd >> $CONFIGURE_LOG 2>&1
  rc=$?
  rm -f /tmp/sleeping

  myecho "\n$RETURN_CODE (${rc}) : ${FW_ERROR[$rc]}"

  if [ $rc -eq 0 ]
  then
    return 0

  elif [ $rc -eq 41 -o \
         $rc -eq 42 -o \
         $rc -eq 101 -o \
         $rc -eq 102 -o \
         $rc -eq 103 -o \
         $rc -eq 104 -o \
         $rc -eq 105 -o \
         $rc -eq 110 -o \
         $rc -eq 130 -o \
         $rc -eq 134 -o \
         $rc -eq 150 ]
  then
    myecho "$FW_WARNING_ESM"
    rc=0
  elif [ $rc -eq 16 ] 
  then
    myecho "$FW_WARNING_DHCP"
    rc=0
  else
    myecho "$FW_ERROR_FAIL"
  fi
  return $rc
}


#####
# setNTC()
#####

setNTC()
{
  touch /tmp/sleeping
  while [ -f /tmp/sleeping ]
  do
    myprintf '.';
    sleep 10
  done &

  cmd="$NTC_CONFIG -d"

  if [ "$storEdgeId" = "-1" ]
  then
    cmd="$cmd --unconfigure"
  else
    cmd="$cmd --new_ip $newNTCAddress"
  fi

  cmd="$cmd --curr_ip $currentNTCAddress"
  cmd="$cmd --sp_ip $newSpAddress"

  echo "Executing $NTC_CONFIG with the following parameters" >> $CONFIGURE_LOG
  echo "$cmd" >> $CONFIGURE_LOG
  $cmd >> $CONFIGURE_LOG 2>&1
  rc=$?

  rm -f /tmp/sleeping

  myecho "\n$RETURN_CODE (${rc}) : ${NTC_ERROR[$rc]}"

  if [ $rc -eq 0 ]
  then
    myecho "$NTC_GOOD"
    rc=0
  else
    myecho "$NTC_ERROR_FAIL"
  fi
  return $rc
}


#####
# setDefaultRoute()
#####

setDefaultRoute()
{
  currentRoute=$(netstat -rn | grep ^default)
  if [ -n "$currentRoute" ]
  then
    currentRouterAddress=$(echo $currentRoute | cut -f2 -d' ')
    route delete default $currentRouterAddress 1>/dev/null
  fi
  if route add default $newRouterAddress 1>/dev/null
  then
    echo "$newRouterAddress" > /etc/defaultrouter
  else
    return 1
  fi
  return $?
}

#####
# setHostsFiles()
#####

setHostsFiles()
{
  for hostsFile in $(ls /etc/net/*/hosts)
  do
    grep -v "$currentSpName" $hostsFile > $hostsFile.bak
    echo "$newSpName\t$newSpName" >> $hostsFile.bak
    mv $hostsFile.bak $hostsFile
  done

  echo "$newSpName" > /etc/hostname.dmfe0 
  echo "$newSpName" > /etc/nodename 
  hostname "$newSpName"
  mkdir /var/crash/$newSpName 2>/dev/null
  dumpadm -s /var/crash/$newSpName >/dev/null
  return 0
}

#####
# setConfig()
#####

setConfig()
{
  echoheader "$SET_CONFIG_HEADER"
  total_steps=8

  step=1
  myecho "\n$RUNNING_STEP $step $STEP_OF $total_steps..."
  myecho "$UPDATE_IFCONFIG"

  if ifconfig dmfe0 set $newSpAddress netmask 255.255.255.0
  then
    myecho "$SUCCESS"
  else
    myecho "$FAILURE"
    return 1
  fi

  step=2
  myecho "\n$RUNNING_STEP $step $STEP_OF $total_steps..."
  myecho "$UPDATE_HOSTNAME"

  if setHostsFiles 
  then
    myecho "$SUCCESS"
  else
    myecho "$FAILURE"
    return 1
  fi

  step=3
  myecho "\n$RUNNING_STEP $step $STEP_OF $total_steps..."
  myecho "$UPDATE_FIREWALL_CONFIG"

  if ! setFirewall 
  then
    myprintf "$CONTINUE_ON_ERROR" 
    ans=$(myread)
    if [ "$ans" = "N" ]  || [ "$ans" = "n" ]
    then
      return 1
    elif [ "$ans" != "Y" ]  && [ "$ans" != "y" ]
    then
      myecho "$INVALID_VALUE"
    fi
  fi

  step=4
  myecho "\n$RUNNING_STEP $step $STEP_OF $total_steps..."
  myecho "$UPDATE_DEFAULT_ROUTE"

  if setDefaultRoute
  then
    myecho "$SUCCESS"
  else
    myecho "$FAILURE"
    return 1
  fi

  step=5
  myecho "\n$RUNNING_STEP $step $STEP_OF $total_steps..."
  myecho "$UPDATE_RESOLV_CONF"

  rm -f /etc/resolv.conf
  if [ -n "$nameServerAddress" ]
  then
    echo "nameserver\t$nameServerAddress" >> /etc/resolv.conf
  fi
  if [ -n "$domainNameAddress" ]
  then
    echo "domain\t\t$domainNameAddress" >> /etc/resolv.conf
  fi

  #
  # Refresh Namserver Daemon Cache
  #
  if /etc/init.d/nscd stop
  then
    if /etc/init.d/nscd start
    then
      myecho "$SUCCESS"
    else
      myecho "$FAILURE"
    fi
  else
    myecho "$FAILURE"
  fi

  step=6
  myecho "\n$RUNNING_STEP $step $STEP_OF $total_steps..."
  myecho "$RESTART_SENDMAIL"

  if /etc/init.d/sendmail restart 
  then
    myecho "$SUCCESS"
  else
    myecho "$FAILURE"
  fi

  step=7
  myecho "\n$RUNNING_STEP $step $STEP_OF $total_steps..."
  myecho "$RESTART_SE6000"
  rm -f /opt/se6x20/watchdog_no_restart
  if /opt/se6x20/bin/restart_se6000
  then
    myecho "$SUCCESS"
  else
    myecho "$FAILURE"
  fi

  step=8
  myecho "\n$RUNNING_STEP $step $STEP_OF $total_steps..."
  myecho "$UPDATE_NTC_CONFIG"

  if setNTC
  then
    myecho "$SUCCESS"
  else
    myecho "$FAILURE"
    return 1
  fi

  myprintf "\n$PRESS_ENTER"
  ans=$(myread)
  return 0
}

#####
# myread()
#####

myread()
{
  read readVal
  echo "$readVal"
  echo "$readVal" >> $CONFIGURE_LOG
  return 0
}


#####
# myecho()
#####

myecho()
{
  echo "$*" >> $CONFIGURE_LOG
  echo "$*"
}


#####
# myprintf()
#####

myprintf()
{
  printf "$*" >> $CONFIGURE_LOG
  printf "$*"
}

#####
# echoheader()
#####

echoheader()
{
  STARS=$(echo "$1" | sed -e "s/./*/g")
  myecho "\n**$STARS**"
  myecho "* $1 *"
  myecho "**$STARS**\n"
}

#####
# echosubheader()
#####

echosubheader()
{
  DASHES=$(echo "$1" | sed -e "s/./-/g")
  myecho "\n $1 "
  myecho "-$DASHES-"
}

#####
# setMessages()
#####

setMessages()
{
  if [ ! -f $(dirname $0)/locale/C/messages.properties ]
  then
    myecho "FATAL ERROR : No message files, $0 unable to continue."
    myecho "Exiting."
    exit 1
  fi

  . $(dirname $0)/locale/C/messages.properties 2>/dev/null
  . $(dirname $0)/locale/C/FW_config_errors.properties 2>/dev/null
  . $(dirname $0)/locale/C/NTC_config_errors.properties 2>/dev/null
  if [ "$LANG" != "C" ]
  then
    if [ -f $(dirname $0)/locale/C/messages.properties ]
    then
      . $(dirname $0)/locale/C/messages.properties 2>/dev/null
      . $(dirname $0)/locale/C/FW_config_errors.properties 2>/dev/null
      . $(dirname $0)/locale/C/NTC_config_errors.properties 2>/dev/null
    fi
  fi
}

#############
#############
##
## Main
##
#############
#############

clear

FW_CONFIG=/usr/local/bin/FW_config
NTC_CONFIG=/usr/local/bin/ntc_config
CONFIGURE_LOG=/var/tmp/se6000configure.log
VMCFG=/opt/se6x20/bin/vmcfg 
POWER_SEQUENCER_FILE=/opt/se6x20/PSDATA/PS 

setMessages

if [ `tty` != "/dev/console" ]
then
    myecho "You must be on the console to perform Firewall configuration."
    exit 1
fi

if [ `id | cut -f1 -d\(` != "uid=0" ] ; then
    myecho "You must be 'setup' user to run this command"
    exit 1
fi

alreadyFoundSps=0
currentSpName=$(hostname)
if [ "$currentSpName" = "new_sp" ]
then
  currentNTCAddress=$(grep new_ntc /etc/hosts | cut -f1 -d' ')
else
  currentNTCAddress=$(grep $currentSpName-ntc /etc/hosts | cut -f1 -d' ')
fi


myecho $(date)
echoheader "$HEADER"

rc=1
allDone=0
while [ $allDone -eq 0 ]
do
  gotit=0
  while [ $gotit -eq 0 ]
  do
    myecho "$RESTORE_INIT_OR_UNCONFIG"
    myecho "$INITIAL_PROMPT"
    myecho "$RESTORE_PROMPT"
    myecho "$UNCONFIG_PROMPT"
    if [ -f $POWER_SEQUENCER_FILE ]
    then
      myecho "$DISABLE_POWER_PROMPT"
    else
      myecho "$ENABLE_POWER_PROMPT"
    fi
    myprintf "$YOUR_CHOICE"
    ans=$(myread)
#
# Make sure $ans is an integer before using in a test
#
    if ! expr $ans + 1000 1>/dev/null 2>&1 
    then
      myecho "$INVALID_VALUE"
    elif [ $ans -eq 1 ] 
    then
      parmsOK=1
      while [ $parmsOK -eq 1 ] 
      do
        initialConfig
        parmsOK=$?
      done
      setConfig
      rc=$?
      gotit=1
    elif [ $ans -eq 2 ]
    then 
      restoreConfig
      gotit=1
    elif [ $ans -eq 3 ]
    then 
      if unConfig
      then
        setConfig
        rc=$?
      fi
      gotit=1
    elif [ $ans -eq 4 ]
    then 
      configPower
    elif [ $ans -eq 911 ]
    then 
      gotit=1
      myecho "$RESTART_SE6000"
      myprintf "$CONFIRM_OPERATION_PROMPT"
      ans=$(myread)
      if [ "$ans" = "Y" ]  || [ "$ans" = "y" ]
      then
        /opt/se6x20/bin/restart_se6000
      fi
    else
      myecho "$INVALID_VALUE"
    fi
  done

  # 
  # Only call this if we configured 
  # successfully. Do not test the network in the case
  # of a failure or unconfigure
  #
  if [ $rc -eq 0 -a "$storEdgeId" != "-1" ]
  then
    testNetwork 
    value1=$?
    if [ $value1 -eq 0 ]
    then
      myecho "\n$(basename $0) $SUCCESS\n\n\n"
      allDone=1
    else
      myecho "\n$(basename $0) $FAILURE.\n"
      myprintf "$TRY_AGAIN"
      ans=$(myread ans)
      if [ "$ans" != "Y" ]  && [ "$ans" != "y" ]
      then
        allDone=1
      fi
    fi
  else
    allDone=1
  fi
done

exit 0
