#!/bin/sh
# $Id: netconfig,v 1.3 2002/07/16 22:25:56 tom Exp $

#
# Written by: Mike Glover, March 1997
# E-mail    : vexus@home.com, glover@credit.erin.utoronto.ca
#
# Purpose:
#    This is a graphical network configuration program.
#    It was inspired by the cdialog interface that Linux
#    uses. The widgets are the CDK command line widgets
#    provided by the CDK distribution. If you have any
#    questions about CDK, or the command line widgets
#    please feel free to mail me at one of the two above
#    addresses.
#

#
# Copyright 2000, Mike Glover
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgment:
#	This product includes software developed by Mike Glover
#	and contributors.
# 4. Neither the name of Mike Glover nor the names of contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY MIKE GLOVER AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL MIKE GLOVER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

#
# Set up the root directory for the system files.
#
if [ "$1" = "" ]; then
   ROOTDIR=""
else
   ROOTDIR=$1;
fi

#
# This copies a file to it's destination and
# sets the file mode.
#
copyFile()
{
   source=$1;
   dest=$2;
   mode=$3;

   #
   # If the original exists, make a copy.
   #
   if [ -f "${dest}" ]; then
      cp ${dest} ${dest}.old
   fi
   mv ${source} ${dest}
   chmod ${mode} ${dest}
}

#
# Set up some variables.
#
CDK_DIALOG="../cdkdialog"
CDK_ENTRY="../cdkentry"
CDK_TEMPLATE="../cdktemplate"
NETWORKS="${ROOTDIR}/etc/networks"
HOSTS="${ROOTDIR}/etc/hosts"
HOSTNAME_FILE="${ROOTDIR}/etc/HOSTNAME"
RESOLV="${ROOTDIR}/etc/resolv.conf"
INET="${ROOTDIR}/etc/rc.d/rc.inet1"
IFCONFIG="${ROOTDIR}/sbin/ifconfig"
ROUTE="${ROOTDIR}/sbin/route"
tmpFile="/tmp/tmp.$$"

#
# Define some global variables.
#
IP=""
HOST=""
DOMAIN=""
GATEWAY=""
NETMASK=""
NETWORK=""
BROADCAST=""
NAMESERVER=""

#
# Set the screen and widget colors.
#
CDK_WIDGET_COLOR="</5>"; export CDK_WIDGET_COLOR
CDK_SCREEN_COLOR="</7>"; export CDK_SCREEN_COLOR

#
#
#
exitMessage()
{
   exit;
}

#
# Create the opening window.
#
intro()
{
   program=`basename $0`
   buttons="</R>< OK >"
   message="<C></B>Network Configuration

We will now attempt to configure your mail and TCP/IP. This
process probably won't work on all possible network
configurations; but should give you a good start. You will
be able to reconfigure your network at any time by typing
in the command:
<C>${program}"

   ${CDK_DIALOG} -m "${message}" -B "${buttons}" -S 2> /dev/null
}

#
# Get the hostname of the machine.
#
getHostname()
{
   label="</24/B>Enter Hostname: "
   title="<C></B>Enter Hostname

We need to give the machine a name. We only need the base
hostname of the machine; the domain will be queried for
next.
"

   ${CDK_ENTRY} -i "${HOST}" -f 20 -F "</56/B>_" -T "${title}" -L "${label}" -S 2> ${tmpFile}
   HOST=`cat ${tmpFile}`
}

#
# Get the domain name of the machine.
#
getDomainName()
{
   label="</24/B>Enter Domain Name: "
   title="<C></B>Enter Domain Name

Now enter in the domain in which this machine will
reside. Do </K>NOT<!K> supply a leading '.'

"

   ${CDK_ENTRY} -i "${DOMAIN}" -f 20 -F "</56/B>_" -T "${title}" -L "${label}" -S 2> ${tmpFile}
   DOMAIN=`cat ${tmpFile}`
}

#
# Ask them if they plan to use loopback only.
#
loopback()
{
   buttons="< Yes >
< No >"
   message="<C></B>Loopback

Do you plan to only use loopback?  If so then your address will
be 127.0.0.1 and most of the following questions can be skipped.

<C>Do you plan to ONLY use loopback?
"

   ${CDK_DIALOG} -m "${message}" -B "${buttons}" -S 2> /dev/null
   answer=$?
   if [ $answer -eq 0 ]; then
      exitMessage;
   fi
}

#
# Get the IP Address.
#
getIPAddress()
{
   title="<C>         </B>Enter Machine IP Address

<C>Enter the IP address you plan to assign to this machine.
"
   plate="###.###.###.###"
   overlay="</56/B>___.___.___.___"
   ${CDK_TEMPLATE} -i "${IP}" -p "${plate}" -o "${overlay}" -T "${title}" -m 10 -P -S 2> ${tmpFile}
   IP=`cat ${tmpFile}`
}

#
#
#
getGatewayAddress()
{
   NETWORK=`echo ${IP} | awk 'BEGIN {FS="."} {printf ("%s.%s.%s.0",$1,$2,$3)}'`
   BROADCAST=`echo ${IP} | awk 'BEGIN {FS="."} {printf ("%s.%s.%s.255",$1,$2,$3)}'`

   #
   # Get the machine's gateway address.
   #
   title="<C>         </B>Enter Gateway Address

Enter the address of the gateway for this machine. If you do not
know, or have one, use the IP address of this machine. The IP
address you assigned to this machine should already be filled
in. If you want to erase it press control-x to erase the field
or hit the backspace or delete key to erase portions of the field.
"
   tmp=`echo ${IP} | sed 's/\.//g'`
   plate="###.###.###.###"
   overlay="</B/56>___.___.___.___"
   ${CDK_TEMPLATE} -d "${tmp}" -p "${plate}" -o "${overlay}" -T "${title}" -m 10 -P -S 2> ${tmpFile}
   GATEWAY=`cat ${tmpFile}`

   #
   # Get the machine's netmask.
   #
   title="<C>         </B>Enter Netmask

Enter your netmask.  More often than not this value will be
255.255.255.0  A defaulted value of 255.255.255.0 is already
filled in.  If you want to erase it press control-x to erase
the field or hit the backspace or delete key to erase
portions of the field.
"
   plate="###.###.###.###"
   overlay="</B/56>___.___.___.___"
   ${CDK_TEMPLATE} -d "2552552550" -p "${plate}" -o "${overlay}" -T "${title}" -m 10 -P -S 2> ${tmpFile}
   NETMASK=`cat ${tmpFile}`

   #
   # Find out if the user will be accessing a domain name server.
   #
   message="<C>         </B>Named Services

Will you be accessing a name server.
"
   buttons="< Yes >
< No >"
   ${CDK_DIALOG} -m "${message}" -B "${buttons}" -S 2> /dev/null
   answer=$?

   #
   # If they answered yes, get the IP address.
   #
   if [ "$answer" -eq 0 ]; then
      #
      # Get the machine's domain name server address.
      #
      title="<C>         </B>Enter DNS Address

Enter the address of the domain name server (DNS) which you want to
use.  The value of the DNS server has been filled in with the
gateway address you provided.  If you want to erase it press
control-x to erase the field or hit the backspace or delete key
to erase portions of the field.
"
      tmp=`echo ${GATEWAY} | sed 's/\.//g'`
      plate="###.###.###.###"
      overlay="</B/56>___.___.___.___"
      ${CDK_TEMPLATE} -d "${tmp}" -p "${plate}" -o "${overlay}" -T "${title}" -m 10 -P -S 2> ${tmpFile}
      NAMESERVER=`cat ${tmpFile}`
   fi
}

#
# Call the functions needed.
#


#########################################
# Create the hosts file.
#
cat << EOF > ${tmpFile}
#
# 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
$IP	${HOST}.${DOMAIN} ${HOST}
# End of hosts
EOF
copyFile ${tmpFile} "${HOSTS}" 644
echo "${HOST}.${DOMAIN}" > ${tmpFile}
copyFile ${tmpFile} ${HOSTNAME_FILE} 644

#########################################
# Create the resolv.conf file.
#
cat << EOF > ${tmpFile}
domain ${DOMAIN}
nameserver ${NAMESERVER}
EOF
copyFile ${tmpFile} "${RESOLV}" 644

#########################################
# Create the /etc/networks file.
#
cat << EOF > ${tmpFile}
#
# 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	${IP}

# End of networks.
EOF
copyFile ${tmpFile} "${NETWORKS}" 644

#########################################
# Create the /etc/rc.d/inet1 file.
#
cat << EOF > ${tmpFile}
#! /bin/sh
#
# rc.inet1	This shell script boots up the base INET system.
#
# Version:	@(#)/etc/rc.d/rc.inet1	1.01	05/27/93
#

HOSTNAME=\`cat /etc/HOSTNAME\`

# Attach the loopback device.
${IFCONFIG} lo 127.0.0.1
${ROUTE} add -net 127.0.0.0

# IF YOU HAVE AN ETHERNET CONNECTION, use these lines below to configure the
# eth0 interface. If you're only using loopback or SLIP, don't include the
# rest of the lines in this file.

# Edit for your setup.
IPADDR="$IP"	# REPLACE with YOUR IP address!
NETMASK="$NETMASK"	# REPLACE with YOUR netmask!
NETWORK="$NETWORK"	# REPLACE with YOUR network address!
BROADCAST="$BROADCAST"	# REPLACE with YOUR broadcast address, if you
			# have one. If not, leave blank and edit below.
GATEWAY="$GATEWAY"	# REPLACE with YOUR gateway address!

# Uncomment the line below to configure your ethernet card.
${IFCONFIG} eth0 \${IP} broadcast \${BROADCAST} netmask \${NETMASK}

# If the line above is uncommented, the code below can also be uncommented.
# It sees if the ethernet was properly initialized, and gives the admin some
# hints about what to do if it wasn't.
if [ ! \$? = 0 ]; then
  cat << END
Your ethernet card was not initialized properly.  Here are some reasons why this
may have happened, and the solutions:
1. Your kernel does not contain support for your card.  Including all the
   network drivers in a Linux kernel can make it too large to even boot, and
   sometimes including extra drivers can cause system hangs.  To support your
   ethernet, either edit /etc/rc.d/rc.modules to load the support at boottime,
   or compile and install a kernel that contains support.
2. You don't have an ethernet card, in which case you should comment out this
   section of /etc/rc.d/rc.inet1.  (Unless you don't mind seeing this error...)
END
fi

# Uncomment these to set up your IP routing table.
${ROUTE} add -net \${NETWORK} netmask \${NETMASK}
${ROUTE} add default gw \${GATEWAY} metric 1

# End of rc.inet1
EOF
copyFile ${tmpFile} "${INET}" 755

#
# Spit out the 'we are done' message.
#
message="<C></B>Network Setup Complete

Your network software has now been configured. You may
rerun this program to reconfigure or you may edit the
respective system files."
buttons="< OK >"
${CDK_DIALOG} -m "${message}" -B "${buttons}" -S 2> /dev/null

#
# Clean up
#
rm -f ${tmpFile}
