#!/bin/sh
# A script to do basic network configuration.
# Mostly written by Patrick Volkerding <volkerdi@slackware.com>.

TMP=/var/log/setup/tmp
if [ ! -d $TMP ]; then
 mkdir -p $TMP
 chmod 700 $TMP
fi

# This checks IP address syntax.
# 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() {
  RET_CODE=0 
  SCRATCH=$1
  SCRATCH=`echo $SCRATCH | tr "." "/"`
  INDEX=$2
  while [ ! "$INDEX" = "0" ]; do
    # OK, so I'm a LISP-head :^)
    FIELD=`basename $SCRATCH`
    SCRATCH=`dirname $SCRATCH`
    if expr $FIELD + 1 1> /dev/null 2> /dev/null; then
      true
    else
      RET_CODE=2; # non-numeric field
    fi
    INDEX=`expr $INDEX - 1`
  done
  if [ ! "$SCRATCH" = "." ]; then
    RET_CODE=1; # too many arguments
  fi
  if [ "$3" = "WARN" -a ! "$RET_CODE" = "0" ]; then
    cat << EOF > $TMP/tempmsg

The address you have entered seems to be non-standard. We were expecting
$2 groups of numbers seperated by dots, like: 127.0.0.1
Are you absolutely sure you want to use the address $1?

EOF
    dialog --title "WARNING" --yesno "`cat $TMP/tempmsg`" 9 72
    if [ $? = 0 ]; then
      RET_CODE = 0;
    fi
    rm -r $TMP/tempmsg
  else
    if [ "$3" = "ECHO" ]; then
      echo $RET_CODE;
    fi
  fi
  return $RET_CODE;
}

if [ ! -d proc -a ! -d bin -a ! -d tmp ]; then # cheap, but it works :^)
 cd /
fi;

# IMPORTANT!!! NO LEADING '/' in the paths below, or this script will not
# function from the bootdisk.
RC=etc/rc.d/rc.inet1			# Where rc.inet1 file is.
RESOLV=etc/resolv.conf			# Where resolv.conf file is.
HOSTS=etc/hosts			 	# Where hosts file is.
ETCNETWORKS=etc/networks		# Where networks file is.
DHCP="#"                                # Use DHCP?  '#' == no.
USE_DHCP_HOSTNAME="#"                   # Does the provider need a DHCP hostname?  '#' == no.
DHCP_HOSTNAME="CCHOSTNUM-A"             # This is our fake example DHCP hostname.
#
# defaults:
NETWORK=127.0.0.0
IPADDR=127.0.0.1
NETMASK=255.255.255.0
#
############################################################################
#			 Question and answer.
############################################################################
#
cat << EOF > $TMP/tempmsg
Now we will attempt to configure your network (TCP/IP) settings.
This process doesn't cover every possible network configuration,
but will give you a basic working setup to start with.  You will
be able to reconfigure your system at any time by typing:
  
netconfig
EOF
dialog --title "NETWORK CONFIGURATION" --msgbox "`cat $TMP/tempmsg`" 10 69
if [ $? = 1 -o $? = 255 ]; then
 rm -f $TMP/SeThost $TMP/tempmsg
 exit
fi

while [ 0 ]; do
cat << EOF > $TMP/tempmsg
First, we'll need the name you'd like to give your host.
Only the base hostname is needed right now. (not the domain)

Enter hostname:
EOF
 dialog --title "ENTER HOSTNAME" --inputbox "`cat $TMP/tempmsg`" 11 65 \
 $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" = "" ]; then
  break;
 fi
done

while [ 0 ]; do
cat << EOF > $TMP/tempmsg
Now, we need the domain name for this machine, such as:

example.org

Do not supply a leading '.'

Enter domain name for $HOSTNM: 
EOF
 dialog --title "ENTER DOMAINNAME FOR '$HOSTNM'" --inputbox \
"`cat $TMP/tempmsg`" \
14 64 $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
 if [ ! "$DOMAIN" = "" ]; then
  break;
 fi
done

echo $HOSTNM.$DOMAIN > etc/HOSTNAME

dialog --title "SETUP IP FOR '$HOSTNM.$DOMAIN'" --menu \
"Now we need to know how your machine connects to the network.  If you \
have an internal network card and an assigned IP address, gateway, \
and DNS, use the 'static IP' choice to enter these values.  If your IP \
address is assigned by a DHCP server (commonly used by cable modem and DSL \
services), select 'DHCP'.  If you do not have a network card, select \
the 'loopback' choice.  'loopback' is also the correct choice if your \
only connection to the network will be through a serial modem \
(with SLIP or PPP), or if you are using a laptop network card (these \
are configured in /etc/pcmcia/). What type of network connection \
best describes your machine?" 20 69 3 \
"static IP" "Use a static IP address to configure ethernet" \
"DHCP" "Use a DHCP server to configure ethernet" \
"loopback" "Set up a loopback connection (modem or no net)" 2> $TMP/reply
if [ $? = 1 -o $? = 255 ]; then
  rm -f $TMP/reply
  exit
fi
REPLY=`cat $TMP/reply`
rm -f $TMP/reply

if [ "$REPLY" = "DHCP" ]; then
  DHCP=""
  dialog --title "SET DHCP HOSTNAME"  --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
  NEW_DHCPHOST="`cat $TMP/SeTDHCPHOST`"
  rm -f $TMP/SeTDHCPHOST
  # OK, if we actually got something, use it.
  if [ ! "$NEW_DHCPHOST" = "" ]; then
    DHCP_HOSTNAME="$NEW_DHCPHOST"
    USE_DHCP_HOSTNAME=""
  fi
elif [ "$REPLY" = "loopback" ]; then
  LOOPBACK="yes"
else
  LOOPBACK="no"
fi

if [ "$LOOPBACK" = "no" -a "$DHCP" = "#" ]; then

 while [ 0 ]; do
  if [ -r $TMP/SeTIP ]; then
   IPADDR=`cat $TMP/SeTIP`
  fi
  cat << EOF > $TMP/tempmsg
Enter your IP address for the local machine.  Example: 
111.112.113.114
Enter IP address for $HOSTNM (aaa.bbb.ccc.ddd): 
EOF
  dialog --title "ENTER IP ADDRESS FOR '$HOSTNM.$DOMAIN'" --inputbox \
"`cat $TMP/tempmsg`" \
10 68 $IPADDR 2> $TMP/SeTlip
  if [ $? = 1 -o $? = 255 ]; then
   rm -f $TMP/SeTlip $TMP/tempmsg
   exit
  fi
  IPADDR="`cat $TMP/SeTlip`"
  rm -f $TMP/SeTlip $TMP/tempmsg
  if [ "$IPADDR" = "" ]; then
   continue;
  fi
  syntax_check_color $IPADDR 4 WARN
  if [ $? = 0 ]; then
   echo $IPADDR > $TMP/SeTIP
   break;
  fi
 done

 while [ 0 ]; do
  if [ -r $TMP/SeTnetmask ]; then
   NETMASK=`cat $TMP/SeTnetmask`
  fi
  cat << EOF > $TMP/tempmsg
Enter your netmask.  This will generally look something
like this: 255.255.255.0
Enter netmask (aaa.bbb.ccc.ddd):
EOF
  dialog --title "ENTER NETMASK FOR LOCAL NETWORK" --inputbox \
"`cat $TMP/tempmsg`" \
10 65 $NETMASK 2> $TMP/SeTnmask
  if [ $? = 1 -o $? = 255 ]; then
   rm -f $TMP/SeTnmask $TMP/tempmsg
   exit
  fi
  NETMASK="`cat $TMP/SeTnmask`"
  rm -f $TMP/SeTnmask $TMP/tempmsg
  if [ "$NETMASK" = "" ]; then
   continue;
  fi
  syntax_check_color $NETMASK 4 WARN
  if [ $? = 0 ]; then
   echo $NETMASK > $TMP/SeTnetmask
   break;
  fi
 done
 
 # Set broadcast/network addresses automatically:
 BROADCAST=`ipmask $NETMASK $IPADDR | cut -f 1 -d ' '`
 NETWORK=`ipmask $NETMASK $IPADDR | cut -f 2 -d ' '`

 while [ 0 ]; do
  if [ -r $TMP/SeTgateway ]; then
   GATEWAY=`cat $TMP/SeTgateway`
  fi
  cat << EOF > $TMP/tempmsg
Enter the address for the gateway on your network, such as:
`echo $IPADDR | cut -f 1-3 -d .`.1

If you don't have a gateway on your network just hit ENTER
without entering a gateway IP address.

Enter gateway address (aaa.bbb.ccc.ddd):
EOF
  dialog --title "ENTER GATEWAY ADDRESS" --inputbox "`cat $TMP/tempmsg`" \
  14 64 $GATEWAY 2> $TMP/SeTgate
  if [ $? = 1 -o $? = 255 ]; then
   rm -f $TMP/SeTgate $TMP/tempmsg
   exit
  fi
  GATEWAY="`cat $TMP/SeTgate`"
  rm -f $TMP/SeTgate $TMP/tempmsg
  if [ "$GATEWAY" = "" ]; then
    break;
  fi
  syntax_check_color $GATEWAY 4 WARN
  if [ $? = 0 ]; then
    echo $GATEWAY > $TMP/SeTgateway
    break;
  fi
 done

else
 if [ ! -r bin/telnet ]; then
  cat << EOF > $TMP/tempmsg
You do not seem to have TCP/IP installed, so all I can really
set up for you is your hostname/domainname. This won't mean
much since you're not on the network, but it will let you have
the hostname you prefer shown at the login prompt.
EOF
  dialog --title "SKIPPING MOST OF THE CONFIG PROCESS" \
--msgbox "`cat $TMP/tempmsg`" 8 67
 fi
fi

#
############################################################################
#			  The rc.inet1 file.
############################################################################
#
# echo "Creating /$RC..."
/bin/cat << ENDFILE > $RC
#! /bin/sh
# /etc/rc.d/rc.inet1
# This script starts up the base networking system.
#
# Version:
# @(#)/etc/rc.d/rc.inet1 8.1 Tue May 28 15:27:39 PDT 2002 (pjv)

# Edit these values to set up your first Ethernet card (eth0):
IPADDR="$IPADDR"  # REPLACE with YOUR IP address!
NETMASK="$NETMASK"	# REPLACE with YOUR netmask!
# Or, uncomment the following lines to set up eth0 using DHCP:
${DHCP}USE_DHCP=yes
# If your provider requires a DHCP hostname, uncomment and edit below:
${USE_DHCP_HOSTNAME}DHCP_HOSTNAME="${DHCP_HOSTNAME}"

# Edit these values to set up your second Ethernet card (eth1),
# if you have one.  Otherwise leave it configured to 127.0.0.1,
# or comment it out, and it will be ignored at boot.
IPADDR2="127.0.0.1"  # REPLACE with YOUR IP address!
NETMASK2="255.255.255.0" # REPLACE with YOUR netmask!
# Or, uncomment the following lines to set up eth1 using DHCP:
#USE_DHCP2=yes
# If your provider requires a DHCP hostname, uncomment and edit below:
#DHCP_HOSTNAME2="CCHOSTNUM-A"

# Edit the next line to point to your gateway:
GATEWAY="$GATEWAY" # REPLACE with YOUR gateway!

# You shouldn't need to edit anything below here.

# Set up the loopback interface:
/sbin/ifconfig lo 127.0.0.1
/sbin/route add -net 127.0.0.0 netmask 255.0.0.0 lo

# Set up the eth0 interface:
if [ "\$USE_DHCP" = "yes" ]; then # use DHCP to set everything up:
  echo "Attempting to configure eth0 by contacting a DHCP server..."
  # Add the -h option to the DHCP hostname:
  if [ ! "\$DHCP_HOSTNAME" = "" ]; then
    DHCP_HOSTNAME="-h \$DHCP_HOSTNAME"
  fi
  /sbin/dhcpcd -t 10 \${DHCP_HOSTNAME} -d eth0
elif [ ! "\$IPADDR" = "127.0.0.1" -a ! "\$IPADDR" = "" ]; then # set up IP statically:
  # Determine broadcast and network addresses from the IP address and netmask:
  BROADCAST=\`/bin/ipmask \$NETMASK \$IPADDR | cut -f 1 -d ' '\`
  NETWORK=\`/bin/ipmask \$NETMASK \$IPADDR | cut -f 2 -d ' '\`
  # Set up the ethernet card:
  echo "Configuring eth0:"
  echo "ifconfig eth0 \${IPADDR} broadcast \${BROADCAST} netmask \${NETMASK}"
  /sbin/ifconfig eth0 \${IPADDR} broadcast \${BROADCAST} netmask \${NETMASK}
  # If that didn't succeed, give the system administrator some hints:
  if [ ! \$? = 0 ]; then
    echo "Your eth0 card was not initialized properly.  Here are some reasons why this"
    echo "may have happened, and the solutions:"
    echo "1. Your kernel does not contain support for your card.  Including all the"
    echo "   network drivers in a Linux kernel can make it too large to even boot, and"
    echo "   sometimes including extra drivers can cause system hangs.  To support your"
    echo "   ethernet, either edit /etc/rc.d/rc.modules to load the support at boot time,"
    echo "   or compile and install a kernel that contains support."
    echo "2. You don't have an ethernet card, in which case you should run netconfig"
    echo "   and configure your machine for loopback. (Unless you don't mind seeing this"
    echo "   error...)"
  fi
fi # set up eth0

# Set up the eth1 interface:
if [ "\$USE_DHCP2" = "yes" ]; then # use DHCP to set everything up:
  echo "Attempting to configure eth1 by contacting a DHCP server..."
  # Add the -h option to the DHCP hostname:
  if [ ! "\$DHCP_HOSTNAME2" = "" ]; then
    DHCP_HOSTNAME2="-h \$DHCP_HOSTNAME2"
  fi
  /sbin/dhcpcd -t 10 \${DHCP_HOSTNAME2} -d eth1
elif [ ! "\$IPADDR2" = "127.0.0.1" -a ! "\$IPADDR2" = "" ]; then # set up IP statically:
  # Determine broadcast and network addresses from the IP address and netmask:
  BROADCAST2=\`/bin/ipmask \$NETMASK2 \$IPADDR2 | cut -f 1 -d ' '\`
  NETWORK2=\`/bin/ipmask \$NETMASK2 \$IPADDR2 | cut -f 2 -d ' '\`
  # Set up the ethernet card:
  echo "Configuring eth1:"
  echo "ifconfig eth1 \${IPADDR2} broadcast \${BROADCAST2} netmask \${NETMASK2}"
  /sbin/ifconfig eth1 \${IPADDR2} broadcast \${BROADCAST2} netmask \${NETMASK2}
  # If that didn't succeed, give the system administrator some hints:
  if [ ! \$? = 0 ]; then
    echo "Your eth1 card was not initialized properly.  Here are some reasons why this"
    echo "may have happened, and the solutions:"
    echo "1. Your kernel does not contain support for your card.  Including all the"
    echo "   network drivers in a Linux kernel can make it too large to even boot, and"
    echo "   sometimes including extra drivers can cause system hangs.  To support your"
    echo "   ethernet, either edit /etc/rc.d/rc.modules to load the support at boot time,"
    echo "   or compile and install a kernel that contains support."
    echo "2. You don't have an ethernet card, in which case you should fix"
    echo "   /etc/rc.d/rc.inet1 to stop trying to configure eth1. (Unless you don't mind"
    echo "   seeing this error...)"
  fi
fi # set up eth1

# Set up the gateway:
if [ ! "\$GATEWAY" = "127.0.0.1" -a ! "\$GATEWAY" = "" ]; then
  /sbin/route add default gw \${GATEWAY} metric 1
fi

# End of /etc/rc.d/rc.inet1
ENDFILE
chmod 755 $RC
#
############################################################################
#			  The networks file.
############################################################################
#
#echo "Creating /$ETCNETWORKS..."
/bin/cat <<EOF >$ETCNETWORKS
#
# networks	This file describes a number of netname-to-address
#		mappings for the TCP/IP subsystem.  It is mostly
#		used at boot time, when no name servers are running.
#

loopback	127.0.0.0
localnet	$NETWORK

# End of networks.
EOF
chmod 644 $ETCNETWORKS
#
############################################################################
#			   The hosts file.
############################################################################
#
#echo "Creating /$HOSTS..."
/bin/cat << EOF > $HOSTS
#
# hosts		This file describes a number of hostname-to-address
#		mappings for the TCP/IP subsystem.  It is mostly
#		used at boot time, when no name servers are running.
#		On small systems, this file can be used instead of a
#		"named" name server.  Just add the names, addresses
#		and any aliases to this file...
#
# By the way, Arnt Gulbrandsen <agulbra@nvg.unit.no> says that 127.0.0.1
# should NEVER be named with the name of the machine.  It causes problems
# for some (stupid) programs, irc and reputedly talk. :^)
#

# For loopbacking.
127.0.0.1	localhost
$IPADDR	 	$HOSTNM.$DOMAIN $HOSTNM

# End of hosts.

EOF
chmod 644 $HOSTS
#
############################################################################
#			The resolv.conf file.
############################################################################
#
if [ "$LOOPBACK" = "no" ]; then
 dialog --title "USE A NAMESERVER?" --yesno "Will you be accessing a \
nameserver?" 5 42
 if [ $? = 0 ]; then
  if [ ! "`cat $TMP/SeTns 2> /dev/null`" = "" ]; then
    DNSSAMPLE="`cat $TMP/SeTns 2> /dev/null`"
  elif [ "$GATEWAY" = "" ]; then
    DNSSAMPLE=`echo $IPADDR | cut -f 1-3 -d .`
  else
    DNSSAMPLE=$GATEWAY
  fi
  while [ "$NAMESERVER" = "" ]; do
   cat << EOF > $TMP/tempmsg
Here is your current IP address, full hostname, and base hostname:
$IPADDR       $HOSTNM.$DOMAIN    $HOSTNM

Please give the IP address of the name server to use,
such as $DNSSAMPLE.

You can add more Domain Name Servers later by editing /$RESOLV.

Primary name server to use (aaa.bbb.ccc.ddd): 
EOF
   dialog --title "SELECT NAMESERVER" --inputbox \
"`cat $TMP/tempmsg`" 17 72 $DNSSAMPLE 2> $TMP/SeTns
   if [ $? = 1 -o $? = 255 ]; then
    rm -f $TMP/tempmsg $TMP/SeTns 
    break
   fi
   NAMESERVER="`cat $TMP/SeTns`"
   rm -f $TMP/tempmsg $TMP/SeTns 
  done
  echo "search $DOMAIN" >$RESOLV
  echo "nameserver $NAMESERVER" >>$RESOLV
 else
  echo "search $DOMAIN" >$RESOLV
 fi
fi
if [ "$LOOPBACK" = "no" ]; then chmod 644 $RESOLV ;fi
#
############################################################################
#		     Change permissions and exit.
############################################################################
#

# Check for existing network driver:
if cat /proc/net/dev | fgrep eth0 1> /dev/null 2> /dev/null ; then
  # Skip probe by setting an empty cardfound file that will be ignored:
  touch /cardfound
fi

if [ -d lib/modules/`uname -r` \
     -a ! "$IPADDR" = "127.0.0.1" \
     -a ! -r /cardfound \
     -o "$DHCP" = "" ]; then
  dialog --title "PROBE FOR NETWORK CARD?" --menu "If you like, we \
can look to see what kind of network card you have in your machine, and \
if we find one create an /etc/rc.d/rc.netdevice script to load the module \
for it at boot time.  There's a slight bit of danger that the probing \
can cause problems, but it almost always works.  If you'd rather configure \
your system manually, you can skip the probing process and edit \
/etc/rc.d/rc.modules or /etc/modules.conf later to have it load the right module." \
16 68 2 \
"probe" "look for network cards to set up" \
"skip" "skip probe;  edit /etc/rc.d/rc.modules later" 2> $TMP/reply
  if [ $? = 1 -o $? = 255 ]; then
    rm -f $TMP/reply
    exit
  fi
  REPLY=`cat $TMP/reply`
  rm -f $TMP/reply
  if [ ! "$REPLY" = "skip" ]; then
    for card in 3c59x acenic de4x5 dgrs eepro100 epic100 hp100 lance \
    ne2k-pci olympic pcnet32 rcpci rtl8139 8139too 8139cp sktr tulip via-rhine \
    yellowfin natsemi ; do
      chroot . /sbin/modprobe $card 2> /dev/null
      if [ $? = 0 ]; then
        dialog --title "CARD DETECTED" --msgbox "A networking card using \
the $card.o module has been detected." 5 72
        echo "$card" > /cardfound
        break;
      fi
     done
     if [ ! -r /cardfound ]; then
       # Don't probe for com20020, because it doesn't check and will always load.
       # Don't probe for arlan, because it needs irq= to work.
       # Don't probe for com90io or com90xx because they taint the kernel.
       for card in depca ibmtr 3c501 3c503 3c505 3c507 3c509 3c515 ac3200 \
         acenic at1700 cosa cs89x0 de4x5 de600 \
         de620 e2100 eepro eexpress es3210 eth16i ewrk3 fmv18x hostess_sv11 \
         hp-plus hp lne390 ne3210 ni5010 ni52 ni65 sb1000 sealevel smc-ultra \
         smc-ultra32 smc9194 wd ; do
         chroot . /sbin/modprobe $card 2> /dev/null
         if [ $? = 0 ]; then
           dialog --title "CARD DETECTED" --msgbox "A networking card using \
the $card.o module has been detected." 5 72
           echo "$card" > /cardfound
           break
         fi
       done
     fi
     if [ ! -r /cardfound ]; then
       dialog --title "NO CARD DETECTED" --msgbox "Sorry, but no network \
card could be probed for on your system.  Some cards (like non-PCI NE2000s) \
must be supplied with the I/O address to use and can't be probed for safely. \
You'll have to try to configure the card later by editing \
/etc/rc.d/rc.modules or recompiling your kernel." 9 70
     fi
  fi
fi

if [ -r /cardfound ]; then
  if [ ! "`cat /cardfound`" = "" ]; then
    cat << EOF > etc/rc.d/rc.netdevice
# Load module for network device.
# This script is automatically generated during the installation.

/sbin/modprobe `cat /cardfound`

EOF
    chmod 755 etc/rc.d/rc.netdevice
  fi
fi
rm -f $TMP/tempmsg /cardfound

if [ "$LOOPBACK" = "yes" ]; then
  dialog --title "NETWORK SETUP COMPLETE" --yesno "Your networking \
system is now configured to use loopback:

IP address: 127.0.0.1
Netmask: 255.255.255.0

Is this correct?  Press 'Yes' to continue, or 'No' to reconfigure." 0 0
elif [ "$DHCP" = "#" ]; then
dialog --title "NETWORK SETUP COMPLETE" --yesno "Your networking \
system is now configured with these settings:

IP address: $IPADDR
Netmask: $NETMASK
Gateway: $GATEWAY
Nameserver: $NAMESERVER

Is this correct?  Press 'Yes' to continue, or 'No' to reconfigure." 0 0
else # DHCP was used
  if [ ! "$DHCP_HOSTNAME" = "CCHOSTNUM-A" ]; then
    WITH_HOST=" with DHCP hostname $DHCP_HOSTNAME"
  fi
  dialog --title "NETWORK SETUP COMPLETE" --yesno "Your networking \
system is now configured to use DHCP${WITH_HOST}:

IP address: (use DHCP)
Netmask: (use DHCP)
Gateway: (use DHCP)
Nameserver: (use DHCP)

Is this correct?  Press 'Yes' to continue, or 'No' to reconfigure." 0 0
fi

exit $?
