#!/bin/sh 
#
# AUTHOR:	Leonard Giambrone	09/30/96
#
# NAME: 	pppinst
#
# USAGE:	pppinst -locip <local_ip_addr> -remip <remote_ip_addr>
#                       -remhost <remote_hostname> -remmask <remote_netmask>
#                       -remphone <remote_phone_number> [-client]
#
# DESCRIPTION:
#
# This script must be executed by root.  It will create and edit the
# necessary text files to enable this host to receive or initiate a ppp
# connection to $REM_HOST and a dial-in only host (pcclient).  In
# addition, it creates the users $BIDIR_PPP_USER and $DIALIN_PPP_USER, and makes
# their passwords $PPP_PASS.  It also removes all currently configured
# port monitors and creates one new port on $DEVICE.  Lastly, it queries
# about routing policies and configures as appropriate.
#
#
# FILES ACCESSED:
#
# See Global Variables section for Variable reference.
# Files which have w access may be created or deleted.
#
# Filename					Access
# --------                      		-----
# $LOGFILE					  w
# $DEVICES					  rw
# $DIALERS					  rw
# $SYSTEMS					  rw
# $ASPPPCF					  rw
# $PASSWD					  rw
# $SHADOW					  rw
# $TTYDEFS					  rw
# $HOSTS					  rw
# $NETMASKS					  rw
# $GATEWAYS					  rw
#
#
# NON OPERATING SYSTEM PROGRAMS CALLED:
#
# EXIT CODES:
#
#		0	Successful execution
#		1	User is not root
#		2	Wrong number of args
#		3	Bad argument for -locip
#		4	Bad argument for -remip
#		5	Bad argument for -remhost
#		6	Bad argument for -remmask
#		7	Bad argument for -remphone
#		8	Bad argument
#		9	Invalid Remote Netmask
#		10	Invalid Local IP address
#		11	Invalid Remote IP address
#		12	Invalid phone number
#		13	$DIALERS not found
#		14	Could not create $BIDIR_PPP_USER or $DIALIN_PPP_USER
#		15	Could not create password for $BIDIR_PPP_USER
#			or $DIALIN_PPP_USER
#		16	Could not remove port monitor
#		17	Could not add port monitor
#		18	Could not add port
#
# HISTORY:
#
# 09/30/96 	ltg	Initial Version
# 10/18/96	ltg	added -w switch to greps
#			added hardcode path of /usr/ucb
# 10/27/96	ltg	hardcoded $BAUD
# 			Made Bad Netmask and Bad IP address errors distinct
# 10/31/96	ltg	Changed format of $ASPPPCF
# 11/01/96	ltg	Changed $PPP_USER to $BIDIR_PPP_USER
#			Changed $INTERFACE to $BIDIR_INTERFACE
#			Added $DIALIN_INTERFACE
#			Added $DIALIN_PPP_USER
#			Added dummy dialin vars:
#			  $DUMMY_IP, $DUMMY_NET, $DUMMY_NAME, $DUMMY_MASK
# 11/04/96	ltg	Added -client switch and associated code
#
# -----------------------------------------------------------------------------

# Global Variables

DEBUG=${DEBUG:-0}

CLIENT=0

LOGFILE=/dev/tty
DEVICES=/etc/uucp/Devices
DIALERS=/etc/uucp/Dialers
SYSTEMS=/etc/uucp/Systems
ASPPPCF=/etc/asppp.cf
PASSWD=/etc/passwd
SHADOW=/etc/shadow
TTYDEFS=/etc/ttydefs
HOSTS=/etc/hosts
NETMASKS=/etc/netmasks
GATEWAYS=/etc/gateways

BIDIR_INTERFACE=ipdptp0
DIALIN_INTERFACE=ipdptp1
BAUD=38400
BIDIR_PPP_USER=pppbi
DIALIN_PPP_USER=pppdin
PPP_PASS=ppp

# Parameters for dial-in client.  These are random, and can be changed.

DUMMY_IP="204.178.48.100"
DUMMY_NET=`echo $DUMMY_IP | sed 's/[^.]*$/0/'`
DUMMY_NAME=dialin
DUMMY_MASK="255.255.255.0"

DEVICE=/dev/term/b
P_TAG=zsmon
S_TAG=ttyb


# -----------------------------------------------------------------------------
# Function:	Usage
# Arguments:	none
# Files:	none
# FuncCalls:	none
# Description:  Outputs Usage error message
# -----------------------------------------------------------------------------

usage ()
{
echo "Usage:"
echo "        `basename $0` -locip <local_ip_addr> -remip <remote_ip_addr>"
echo "                -remhost <remote_hostname> -remmask <remote_netmask>"
echo "                -remphone <remote_phone_number> [-client]"
echo "\n        where:\n"
echo "        <local_ip_addr> is the IP address of the local machine"
echo "        <remote_ip_addr> is the IP address of the remote machine"
echo "        <remote_hostname> is the host name of the remote machine"
echo "        <remote_netmask> is the netmask of the remote machine's network"
echo "        <remote_phone_number> is the phone number of the remote machine"
echo "        -client specifies this is a client-only machine.  Only the"
echo "                bidirectional interface will be configured."
}


# -----------------------------------------------------------------------------
# Main Routine
# -----------------------------------------------------------------------------

if [ "$DEBUG" -eq 1 ]; then
   set -xv
fi


#
# Make sure the user is root
#

if [ `/usr/ucb/whoami` != "root" ]; then
   echo "ERROR:  You must be root to run this installation.  Exiting." 
   exit 1
fi

#
# Parse Arguments
#

if [ $# -lt 10 ]; then
   usage
   exit 2
fi

while [ $# -gt 0 ]; 
do

   case "$1" in

      "-locip")
         shift
         if [ `expr "X$1" : "X-.*"` -eq `expr "X$1" : "X.*"` ]; then
            echo "\nERROR:  You must supply the local IP address after the -locip switch\n"
            usage
            exit 3
         else
            LOC_IP=$1
         fi
         ;;

      "-remip")
         shift
         if [ `expr "X$1" : "X-.*"` -eq `expr "X$1" : "X.*"` ]; then
            echo "\nERROR:  You must supply the remote IP address after the -remip switch\n"
            usage
            exit 4
         else
            REM_IP=$1
         fi
         ;;

      "-remhost")
         shift
         if [ `expr "X$1" : "X-.*"` -eq `expr "X$1" : "X.*"` ]; then
            echo "\nERROR:  You must supply the remote hostname after the -remhost switch\n"
            usage
            exit 5
         else
            REM_HOST=$1
         fi
         ;;
         
      "-remmask")
         shift
         if [ `expr "X$1" : "X-.*"` -eq `expr "X$1" : "X.*"` ]; then
            echo "\nERROR:  You must supply the netmask of the remote host after the -remmask switch\n"
            usage
            exit 6
         else
            REM_MASK=$1
         fi
         ;;

      "-remphone")
         shift
         if [ `expr "X$1" : "X-.*"` -eq `expr "X$1" : "X.*"` ]; then
            echo "\nERROR:  You must supply the phone number of the remote host after the"
            echo "        -remphone switch\n"
            usage
            exit 7
         else
            REM_PHONE=$1
         fi
         ;;

      "-client")
         CLIENT=1
         DIALIN_PPP_USER=""
         ;;

      *)
         usage
         exit 8
         ;;

   esac
   shift
done

#
# Check validity of $LOC_IP, $REM_IP, $REM_MASK
#

for ip in $LOC_IP $REM_IP $REM_MASK
do
   length=`expr "$ip" : ".*"`
   if [ $length -gt 15 -o $length -lt 7 -o \
         `expr "$ip" : '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*$'` -ne "$length" ]; then
      if [ "$ip" == "$REM_MASK" ]; then
         echo "\nERROR:  Bad Netmask:  $ip\n"
         exitval=9
      else
         echo "\nERROR:  Bad IP address:  $ip\n"
         if [ "$ip" == "$LOC_IP" ]; then
            exitval=10
         else
            exitval=11
         endif
      endif
      usage
      exit $exitval
   fi
done

#
# Check validity of $REM_PHONE
# 

length=`expr "$REM_PHONE" : ".*"`
if [ `expr "$REM_PHONE" : '[0-9-=]*'` -ne "$length" ]; then
   echo "\nERROR:  Valid characters in phone number are 0-9, -, =.\n"
   usage
   exit 12
fi


# This must be set *after* parsing the args

REM_NET=`echo $REM_IP | sed 's/[^.]*$/0/'`


#
# Devices
#

echo "\nChecking $DEVICES:"

if [ ! -f $DEVICES ]; then
   touch $DEVICES
   chown uucp:uucp $DEVICES
   chmod 644 $DEVICES
fi
if [ `egrep -c "^ACU cua/b .*hayes$" $DEVICES` -eq 0 ]; then
   echo "Adding correct entry to $DEVICES...\c"
   echo "ACU cua/b - Any hayes" >> $DEVICES  2>$LOGFILE
   echo "done."
else
   echo "Correct entry found."
fi


#
# Dialers
#

echo "\nChecking $DIALERS:"

if [ ! -f $DIALERS ]; then
   echo "ERROR:  $DIALERS not found!  Exiting."
   exit 13
fi
if [ `egrep -c "^hayes.*STTY.*cs8,-parenb$" $DIALERS` -eq 0 ]; then
   echo "Editing $DIALERS...\c"
   ed - $DIALERS <<EOF 1>/dev/null 2>$LOGFILE
/^hayes/
t.
-1i
#
.
j
+1s/ *\(""\)/\1 P_ZERO ""/
a
 STTY=crtscts,cs8,-parenb
.
-1,.j
w
q
EOF
   echo "done."
else
   echo "Correct entry found."
fi


#
# Systems
#

echo "\nChecking $SYSTEMS:"

if [ ! -f $SYSTEMS ]; then
   touch $SYSTEMS
   chown uucp:uucp $SYSTEMS
   chmod 600 $SYSTEMS
fi
if [ `grep -cw "$BIDIR_PPP_USER" $SYSTEMS` -eq 0 ]; then
   echo "Adding correct entry to $SYSTEMS...\c"
   cat >>$SYSTEMS <<EOF 2>$LOGFILE
$BIDIR_PPP_USER Any ACU $BAUD $REM_PHONE "" P_ZERO "" \r\c ogin: $BIDIR_PPP_USER ssword: $PPP_PASS
EOF
   echo "done."
else
   echo "Correct entry found."
fi


#
# asppp.cf
#

echo "\nChecking $ASPPPCF:"

if [ ! -f "$ASPPPCF" ]; then
   touch $ASPPPCF
fi
if [ `egrep -c "^[^#].*$BIDIR_INTERFACE" $ASPPPCF` -eq 0 ]; then
   echo "Configuring $ASPPPCF...\c"

   echo "ifconfig $BIDIR_INTERFACE plumb $LOC_IP $REM_IP up" >>$ASPPPCF 2>$LOGFILE
      if [ $CLIENT -eq 0 ]; then
         echo "ifconfig $DIALIN_INTERFACE plumb $LOC_IP $DUMMY_IP up" >>$ASPPPCF 2>$LOGFILE
      fi
   cat <<EOF >>$ASPPPCF 2>$LOGFILE

defaults
	inactivity_timeout 300	   # Approx. 5 minutes
	ipcp_async_map 0	   # More efficient
#	debug_level 9

path
	interface $BIDIR_INTERFACE
	peer_system_name $BIDIR_PPP_USER   
EOF
   if [ $CLIENT -eq 0 ]; then
   cat <<EOF >>$ASPPPCF 2>$LOGFILE
path
	interface $DIALIN_INTERFACE
	peer_system_name $DIALIN_PPP_USER
EOF
   echo "done."
else
   echo "$ASPPPCF already configured."
fi


#
# Add users $BIDIR_PPP_USER and $DIALIN_PPP_USER
#


for cur_user in $BIDIR_PPP_USER $DIALIN_PPP_USER
do
   echo "\nChecking for existence of user $cur_user:"
   if [ `grep -cw "$cur_user" $PASSWD` -eq 0 ]; then
      echo "Creating user $cur_user...\c"
      useradd -u 1002 -s /usr/sbin/aspppls -g 10 -d / -c "PPP login" $cur_user 2>$LOGFILE
      if [ $? -ne 0 ]; then
         echo "\nERROR:  Couldn't add user $cur_user!  Exiting."
         exit 14
      else
         echo "done."
      fi
      echo "Creating password for $cur_user...\c"
      key=`./mkpass $PPP_PASS` 2>$LOGFILE
      chmod 600 $SHADOW 2>$LOGFILE
      ed - $SHADOW <<EOF 2>$LOGFILE
/^$cur_user/s;\*LK\*;$key;
w
q
EOF
      if [ $? -ne 0 ]; then
         echo "\nERROR:  Could not create password for $cur_user!  Exiting."
         exit 15
      fi
      chmod 600 $SHADOW 2>$LOGFILE
      echo "done."
   else
      echo "User $cur_user found."
   fi
done

#
# ttydefs
#

echo "\nChecking $TTYDEFS:"

if [ ! -f $TTYDEFS ]; then
   touch $TTYDEFS
   chown root:sys $TTYDEFS
   chmod 644 $TTYDEFS
fi
if [ `grep -cw "^38400M" $TTYDEFS` -eq 0 ]; then
   echo "Adding correct entries to $TTYDEFS...\c"
   cat <<EOF >>$TTYDEFS 2>$LOGFILE
38400M:38400 hupcl:38400 hupcl crtscts::19200M
19200M:19200 hupcl:19200 hupcl crtscts::9600M
9600M:9600 hupcl:9600 hupcl crtscts::4800M
4800M:4800 hupcl:4800 hupcl crtscts::2400M
2400M:2400 hupcl:2400 hupcl crtscts::1200M
1200M:1200 hupcl:1200 hupcl crtscts::300M
300M:300 hupcl:300 hupcl crtscts::38400M

EOF
   echo "done."
else
   echo "Correct entries found."
fi


#
# Serial ports
#

echo "\nConfiguring serial ports:"

# First disable /etc/rc3.d/S19.spooler, since it's only purpose is
# to call config_modem, which is now obsolete

if [ -f /etc/rc3.d/S19.spooler ]; then
   echo "Renaming /etc/rc3.d/S19.spooler to /etc/rc3.d/oldS19.spooler...\c"
   mv /etc/rc3.d/S19.spooler /etc/rc3.d/oldS19.spooler 2>$LOGFILE
   echo "done."
fi

#
# Next, get rid of all previously defined serial ports, since they are
# incorrect.
#

echo "Removing previously defined port monitors:"

for ptag in `egrep "^[^# ]" /etc/saf/_sactab | awk -F: '{print $1}'` 
do
   echo "Removing port monitor $ptag...\c"
   sacadm -r -p $ptag 2>$LOGFILE
   if [ $? -ne 0 ]; then
      echo "\nERROR:  Could not remove port monitor $ptag.  Exiting."
      exit 16
   else
      echo "done."
   fi
done

#
# Create the port monitor
#

echo "Creating new port monitor $P_TAG...\c"
sacadm -a -p $P_TAG -t ttymon -c /usr/lib/saf/ttymon -v `ttyadm -V` 2>$LOGFILE
if [ $? -ne 0 ]; then
   echo "\nERROR:  Could not add new port monitor $P_TAG.  Exiting."
   exit 17
else
   echo "done."
fi

#
# Create the port 
#

echo "Creating new port $S_TAG...\c"
pmadm -a -p $P_TAG -s $S_TAG -i root -fu -v `ttyadm -V` -m "`ttyadm -b -d $DEVICE -l ${BAUD}M -m ldterm,ttcompat -s /usr/bin/login -S n`" -y "Modem - Bidirectional" 2>$LOGFILE
if [ $? -ne 0 ]; then
   echo "\nERROR:  Could not add new port $S_TAG.  Exiting."
   exit 18
else
   echo "done."
fi


#
# Add remote host to $HOSTS if needed
#

echo "\nChecking $HOSTS:"

if [ ! -f $HOSTS ]; then
   touch $HOSTS
   chown root:sys $HOSTS
   chmod 644 $HOSTS
fi
if [ `grep -cw "$REM_HOST" $HOSTS` -eq 0 ]; then
   echo "Adding entry for $REM_HOST to $HOSTS...\c"
   echo "$REM_IP\t$REM_HOST" >>$HOSTS 2>$LOGFILE
   if [ $CLIENT -eq 0 ]; then
      echo "$DUMMY_IP\t$DUMMY_NAME" >>$HOSTS 2>$LOGFILE
   fi
   echo "done."
else
   echo "Entry for $REM_HOST found."
fi


#
# Add remote netmask to $NETMASKS if needed
#

echo "\nChecking $NETMASKS:"

if [ ! -f $NETMASKS ]; then
   touch $NETMASKS
   chown root:sys $NETMASKS
   chmod 644 $HOSTS
fi

temp=`echo $REM_NET | sed 's/\./\\\./g'`
if [ `grep -c "$temp" $NETMASKS` -eq 0 ]; then
   echo "Adding entry for $REM_NET to $NETMASKS...\c"
   echo "$REM_NET\t$REM_MASK" >>$NETMASKS 2>$LOGFILE
   if [ $CLIENT -eq 0 ]; then
      echo "$DUMMY_NET\t$DUMMY_MASK" >>$NETMASKS 2>$LOGFILE
   fi
   echo "done."
else
   echo "Entry for $REM_NET found."
fi


#
# Routing
#

echo "\nChecking routing methods:\n"

if [ -f /etc/defaultrouter ]; then
   echo "This machine is currently configured to have a default router."
   echo ""
   echo "In the current configuration, this machine (`uname -n`) will be able"
   echo "to communicate with the other side of the PPP link ($REMOTEHOST),"
   echo "but not with other machines on $REMOTEHOST's network, and vice versa."
   echo ""
   echo "One way to enable this communication is to add static routes"
   echo "pointing to $REMOTENET on all machines in this network."
   echo ""
   echo "The Alternative is to configure this machine as a router, which"
   echo "will only be effective if other machines in each network are also"
   echo "configured as routers."
   echo ""
   echo "     1.  Keep default router"
   echo "     2.  Configure as a router"
   echo ""
   echo "Enter your choice:  \c"
   read choice
   if [ "$choice" -eq 2 ]; then
      rm -f /etc/defaultrouter 2>$LOGFILE
   fi
   echo ""

elif [ -f /etc/notrouter -o \
	\( `ifconfig -au | grep -c inet` -lt 3 -a ! -f /etc/gateways \) ]; then

   echo "This machine currently does not have a default router and is not"
   echo "configured as a router.  This installation will configure this"
   echo "machine as a router.  You may override this if you wish and prevent"
   echo "this machine from acting as a router."
   echo ""
   echo "     1.  Configure as a router"
   echo "     2.  Prevent this machine from acting as a router"
   echo ""
   echo "Enter your choice:  \c"
   read choice
   if [ "$choice" -eq 2 ]; then
      touch /etc/notrouter 2>$LOGFILE
   fi

fi


#
# gateways 
#

if [ ! -f "$GATEWAYS" ]; then
   echo "Creating $GATEWAYS...\c"
   cat <<EOF >$GATEWAYS 2>$LOGFILE
norip $BIDIR_INTERFACE
net $REM_NET gateway $REM_IP metric 1 passive
EOF
   if [ $CLIENT -eq 0 ]; then
      cat <<EOF >$GATEWAYS 2>$LOGFILE
norip $DIALIN_INTERFACE
net $DUMMY_NET gateway $DUMMY_IP metric 1 passive
EOF
   fi
   echo "done."
elif [ `grep -c $BIDIR_INTERFACE $GATEWAYS` -eq 0 ]; then
   echo "Creating new entry for $GATEWAYS...\c"
   cat <<EOF >>$GATEWAYS 2>$LOGFILE
norip $BIDIR_INTERFACE
net $REM_NET gateway $REM_IP metric 1 passive
EOF
   if [ $CLIENT -eq 0 ]; then
      cat <<EOF >$GATEWAYS 2>$LOGFILE
norip $DIALIN_INTERFACE
net $DUMMY_NET gateway $DUMMY_IP metric 1 passive
EOF
   fi
   echo "done."
fi


#
# Router Discovery (in.rdisc)
#

if [ -f /usr/sbin/in.rdisc ]; then

   echo ""
   echo "This machine currently uses router discovery (in.rdisc)."
   echo "This installation disables router discovery by default.  If"
   echo "your network requires router discovery, you may override this step."
   echo "However, this will cause the PPP link to come up every ten minutes"
   echo "instead of only when a connection is initiated."
   echo ""
   echo "     1.  Disable router discovery"
   echo "     2.  Use router discovery"
   echo ""
   echo "Enter your choice:  \c"
   read choice
   if [ "$choice" -ne 2 ]; then
      echo "Disabling router discovery...\c"
      mv /usr/sbin/in.rdisc /usr/sbin/in.rdisc.save 2>$LOGFILE
      echo "done."
   fi
fi
