#!/bin/sh # # Module name: gateway # # Copyright (c) 2001 Toshiba America Information Systems, Inc. All rights reserved. # # Description: a gateway controlling dhcpcd, dhcpd, and dns, and supporting apache, atalk, firewall, networking, samba, sendmail, squid # # Date Created: 01/03/01 # # Revision History # # Date Initials Comments # ---- -------- -------- # 01/03/01 DMM Initial release # 01/09/01 DMM gateway save|restore state # gateway state connect|disconnect # 01/11/01 DMM Allow diald time to come up # 01/29/01 DMM Sendmail restart on update # 02/06/01 DMM MyGWMode for chaining # # gateway # MYGWPATH is also in gateway.state MYVERS="1.0.10" MYGWPATH="/etc/rc.d/init.d" MYSYSLOG="/var/log/messages" cd $MYGWPATH # Start/Stop/Stay case "$1" in install) if [ ! -e /etc/rc.d/rc3.d/S34gateway ]; then echo "Installing gateway: " $MYVERS # Install start mkdir -p /etc/rc.d/init.d mkdir -p /etc/rc.d/rc3.d mkdir -p /etc/rc.d/rc4.d mkdir -p /etc/rc.d/rc5.d ln -s ../init.d/gateway /etc/rc.d/rc0.d/K08gateway ln -s ../init.d/gateway /etc/rc.d/rc1.d/K08gateway ln -s ../init.d/gateway /etc/rc.d/rc2.d/K08gateway ln -s ../init.d/gateway /etc/rc.d/rc3.d/S08gateway ln -s ../init.d/gateway /etc/rc.d/rc4.d/S08gateway ln -s ../init.d/gateway /etc/rc.d/rc5.d/S08gateway ln -s ../init.d/gateway /etc/rc.d/rc6.d/K08gateway ln -s ../init.d/gateway /etc/rc.d/rc0.d/K34gateway ln -s ../init.d/gateway /etc/rc.d/rc1.d/K34gateway ln -s ../init.d/gateway /etc/rc.d/rc2.d/K34gateway ln -s ../init.d/gateway /etc/rc.d/rc3.d/S34gateway ln -s ../init.d/gateway /etc/rc.d/rc4.d/S34gateway ln -s ../init.d/gateway /etc/rc.d/rc5.d/S34gateway ln -s ../init.d/gateway /etc/rc.d/rc6.d/K34gateway if [ -x /etc/rc.d/rc3.d/S57diald ]; then mv -f /etc/rc.d/rc3.d/S57diald /etc/rc.d/rc3.d/K57diald mv -f /etc/rc.d/rc4.d/S57diald /etc/rc.d/rc4.d/K57diald mv -f /etc/rc.d/rc5.d/S57diald /etc/rc.d/rc5.d/K57diald fi if [ -x /etc/rc.d/rc3.d/S65dhcpd ]; then mv -f /etc/rc.d/rc3.d/S65dhcpd /etc/rc.d/rc3.d/K65dhcpd mv -f /etc/rc.d/rc4.d/S65dhcpd /etc/rc.d/rc4.d/K65dhcpd mv -f /etc/rc.d/rc5.d/S65dhcpd /etc/rc.d/rc5.d/K65dhcpd fi cd $MYGWPATH cp -f gateway.state1 gateway.state fi ;; remove) if [ -e /etc/rc.d/rc3.d/S34gateway ]; then echo "Removing gateway: " $MYVERS # Remove start if [ -e /etc/rc.d/rc0.d/K08gateway ]; then rm -f /etc/rc.d/rc0.d/K08gateway rm -f /etc/rc.d/rc1.d/K08gateway rm -f /etc/rc.d/rc2.d/K08gateway rm -f /etc/rc.d/rc3.d/S08gateway rm -f /etc/rc.d/rc4.d/S08gateway rm -f /etc/rc.d/rc5.d/S08gateway rm -f /etc/rc.d/rc6.d/K08gateway fi rm -f /etc/rc.d/rc0.d/K34gateway rm -f /etc/rc.d/rc1.d/K34gateway rm -f /etc/rc.d/rc2.d/K34gateway rm -f /etc/rc.d/rc3.d/S34gateway rm -f /etc/rc.d/rc4.d/S34gateway rm -f /etc/rc.d/rc5.d/S34gateway rm -f /etc/rc.d/rc6.d/K34gateway cd $MYGWPATH fi ;; save) echo "Saving gateway: " $MYVERS if [ $# -lt 2 ]; then tar -czf gw.default.tgz ./gateway* chmod 700 gw.default.tgz else if [ "$2" = "state" ]; then tar -czf gw.${2}.tgz ./gateway.state else tar -czf gw.${2}.tgz ./gateway* chmod 700 gw.${2}.tgz fi fi ;; restore) echo "Restoring gateway: " $MYVERS if [ $# -lt 2 ]; then tar -xzf gw.default.tgz else tar -xzf gw.${2}.tgz fi ;; init) # gateway init . gateway.state # Configure first time if not configured INS=`echo $MYPUBLICNAME | grep -c "sa[[:xdigit:]]\{4\}"` if [ $? -eq 0 ]; then MYPUBLICNAME="sa" fi if [ "$MYPUBLICNAME" = "sa" ]; then #MYPUBLICNAME=${MYPUBLICNAME}`ifconfig $MYPRIVATEINTERFACE | grep HWaddr | sed -e 's/.*://'` MYPUBLICNAME=${MYPUBLICNAME}`ifconfig $MYPRIVATEINTERFACE | grep HWaddr | sed -e 's/.*HWaddr ..:..:..:..:\(..\):\(..\).*$/\1\2/'` $0 state MYPUBLICNAME $MYPUBLICNAME . gateway.state fi # Create gateway files . gateway.init . gateway.update $0 state state ;; update) # gateway update echo "Updating gateway: " $MYVERS if [ -e /var/lock/subsys/gateway ]; then exit 1; else touch /var/lock/subsys/gateway fi if [ $# -eq 2 ]; then if [ "$2" = "test" ]; then $0 state MYGWINIT "test" fi fi . gateway.state . gateway.update if [ -f /var/lock/subsys/sendmail ]; then SNDMSTART=1 /etc/rc.d/init.d/sendmail stop >/dev/null 2>&1 fi if [ -f /var/run/adsl.pid ]; then /etc/rc.d/init.d/adsl stop >/dev/null 2>&1 fi if [ -x /etc/rc.d/init.d/atalk ]; then /etc/rc.d/init.d/atalk stop >/dev/null 2>&1 fi #. gateway stop INS=`ps ax | grep pppd | grep -v grep` if [ $? -eq 0 ]; then killall pppd fi if [ -x /sa/bin/dial_in ]; then MYDIALIN=`/sa/bin/dial_in -status` if [ "$MYDIALIN" = "enabled" ]; then /sa/bin/dial_in -disable fi fi if [ -x /etc/rc.d/init.d/diald ]; then if [ -f /var/lock/subsys/diald ]; then /etc/rc.d/init.d/diald stop fi fi /etc/rc.d/init.d/network stop /etc/rc.d/init.d/network start . gateway.start if [ -x /sa/bin/dial_in ]; then if [ "$MYDIALIN" = "enabled" ]; then /sa/bin/dial_in -enable fi fi if [ -x /etc/rc.d/init.d/atalk ]; then /etc/rc.d/init.d/atalk start >/dev/null 2>&1 fi if [ $SNDMSTART ]; then SNDMSTART=0 /etc/rc.d/init.d/sendmail start >/dev/null 2>&1 fi if [ $# -eq 2 ]; then if [ "$2" = "test" ]; then # # Update test # The previous state will be restored # if the new state is not saved # within two minutes of update # Save the current state gateway save # Change the state gateway state param value # Update the state gateway update test & # Save the state gateway save sleep 120 $0 restore $0 state MYGWINIT "no" fi fi if [ -e /var/lock/subsys/gateway ]; then rm -f /var/lock/subsys/gateway fi ;; start) # gateway start echo "Starting gateway: " $MYVERS if [ -e /var/lock/subsys/gateway ]; then exit 1; else touch /var/lock/subsys/gateway fi # Autorecovery if [ ! -x ./gateway.state ]; then cp -f gateway.state0 gateway.state fi . gateway.state if [ -z "$MYGATEWAY" ]; then cp -f gateway.state0 gateway.state . gateway.state if [ -z "$MYGATEWAY" ]; then cp -f gateway.state1 gateway.state . gateway.state fi fi if [ "$MYGWINIT" = "yes" ]; then $0 state MYGWINIT "no" $0 init elif [ "$MYGWINIT" = "update" ]; then $0 state MYGWINIT "no" . gateway.update . gateway stop /etc/rc.d/init.d/network start . gateway.start else # # Configure first time if not configured if [ -e /etc/rc.d/rc0.d/K08gateway ]; then rm -f /etc/rc.d/rc0.d/K08gateway rm -f /etc/rc.d/rc1.d/K08gateway rm -f /etc/rc.d/rc2.d/K08gateway rm -f /etc/rc.d/rc3.d/S08gateway rm -f /etc/rc.d/rc4.d/S08gateway rm -f /etc/rc.d/rc5.d/S08gateway rm -f /etc/rc.d/rc6.d/K08gateway fi . gateway.start fi if [ -e /var/lock/subsys/gateway ]; then rm -f /var/lock/subsys/gateway fi ;; newstate|state|oldstate) # gateway newstate|state|oldstate [newstate|state|oldstate|param [value]] if [ $# -lt 2 ]; then . gateway.${1} echo $MYPUBLICADDR elif [ $# -lt 3 ]; then case "$2" in oldstate) #cp -f gateway.state gateway.newstate cp -f gateway.oldstate gateway.state ;; newstate) cp -f gateway.state gateway.oldstate cp -f gateway.newstate gateway.state ;; state) cp -f gateway.state gateway.oldstate cp -f gateway.state gateway.newstate ;; connect) . gateway.${1} if [ "$MYDIALUPCONNECT" = "yes" ]; then #echo " Connecting $MYGATEWAY" # Restart diald in case system went offline /etc/rc.d/init.d/diald restart >/dev/null 2>&1 sleep 2 ping -c 1 $MYGATEWAY >/dev/null 2>&1 & MYINS=$! sleep 1 kill $MYINS fi ;; disconnect) . gateway.${1} if [ "$MYDIALUPCONNECT" = "yes" ]; then #echo " Disconnecting" INS=`ps ax | grep pppd | grep -v grep` if [ $? -eq 0 ]; then killall pppd fi /etc/rc.d/init.d/diald restart >/dev/null 2>&1 /etc/diald/diald-ppp0.disconnect ppp0 $MYDIALUPMASK $MYDIALUPADDR $MYDIALUPBCAST 1 >/dev/null 2>&1 fi ;; *) sed -n -e "/$2=/p" <./gateway.${1} | sed -e "s/.*=//" ;; esac else sed -e "s;${2}=.*$;${2}=\"${3}\";" <./gateway.${1} >./gateway.${1}0 if [ $? != 0 ]; then echo "gateway $1 error " ${2}"="${3} exit 1 else chmod 700 gateway.${1}0 cp -f gateway.${1}0 gateway.${1} fi fi ;; status) # gateway status [log [n [pat]]] MYGWMESS="-e gateway -e named -e dhcpd -e dhcpcd -e pumpd -e diald -e connect -e pppd -e rrlogin -e adsl" # -e atalk -e papd -e httpd -e samba -e sendmail -e squid if [ $# -lt 2 ]; then $0 state elif [ $# -lt 3 ]; then grep -c $MYGWMESS $MYSYSLOG elif [ $# -lt 4 ]; then if [ "$3" -eq 0 ]; then grep $MYGWMESS $MYSYSLOG else grep $MYGWMESS $MYSYSLOG | tail -${3} fi else if [ "$3" -eq 0 ]; then grep $MYGWMESS $MYSYSLOG | grep $4 else grep $MYGWMESS $MYSYSLOG | tail -${3} | grep $4 fi fi ;; online) # gateway online cp -f /var/named/named.ra /var/named/named.ca if [ -x /usr/sbin/ndc ]; then /usr/sbin/ndc restart #/usr/sbin/ndc start fi $0 state MYGWSTATE "on" ;; offline) # gateway offline cp -f /var/named/named.na /var/named/named.ca if [ -x /usr/sbin/ndc ]; then /usr/sbin/ndc restart #/usr/sbin/ndc stop fi $0 state MYGWSTATE "off" ;; restart|reload) # gateway restart|reload #echo "Starting gateway: " $MYVERS # Autorecovery if [ ! -x ./gateway.state ]; then cp -f gateway.state0 gateway.state fi . gateway.state if [ -z "$MYGATEWAY" ]; then cp -f gateway.state0 gateway.state . gateway.state if [ -z "$MYGATEWAY" ]; then cp -f gateway.state1 gateway.state . gateway.state fi fi # Dhcpcd has changed, has ip? if [ "$MYDEFAULTINTERFACE" = "$MYPUBLICINTERFACE" ]; then if [ "$PUBLICADDR" = "0.0.0.0" ]; then if [ -e /var/run/dhcpcd-${MYPUBLICINTERFACE}.pid ]; then INSID=`cat /var/run/dhcpcd-${MYPUBLICINTERFACE}.pid` /bin/ps $INSID | grep -c $INSID > /dev/null if [ $? -eq 0 ]; then IPADDR=`/sbin/ifconfig $MYPUBLICINTERFACE 2>/dev/null | grep 'inet addr' | awk '{print $2}' | sed -e 's/.*://'` if [ "$IPADDR" ]; then eval `/bin/ipcalc --network $IPADDR $MYPRIVATEMASK` if [ "$NETWORK" = "$MYPRIVATENET" ]; then if [ "$MYGWMODE" = "yes" ]; then $0 update else # Ouch, can't accept that address /sbin/ifdown $MYPUBLICINTERFACE $0 state MYPUBLICCONNECTIONTYPE "none" $0 offline $0 start fi else # Interface may have come up but not set host INSID=`/sbin/route -n | grep -c "$IPADDR .* UH .* $MYPUBLICINTERFACE"` if [ $? -ne 0 ]; then /sbin/route add -host $IPADDR dev $MYPUBLICINTERFACE>/dev/null 2>&1 fi if [ "$IPADDR" != "$MYPUBLICADDR" -o "$MYCURRENTINTERFACE" != "$MYDEFAULTINTERFACE" ]; then $0 online $0 start fi fi fi fi fi fi fi ;; stop) # gateway stop . gateway.state if [ -f /var/lock/subsys/sendmail ]; then /etc/rc.d/init.d/sendmail stop >/dev/null 2>&1 fi if [ -f /var/run/adsl.pid ]; then /etc/rc.d/init.d/adsl stop fi /usr/sbin/ndc stop if [ -x /etc/rc.d/init.d/atalk ]; then /etc/rc.d/init.d/atalk stop >/dev/null 2>&1 fi #/etc/rc.d/init.d/firewall stop /etc/rc.d/init.d/diald stop /etc/rc.d/init.d/dhcpcd stop /etc/rc.d/init.d/dhcpd stop /etc/rc.d/init.d/network stop if [ -e /var/lock/subsys/gateway ]; then rm -f /var/lock/subsys/gateway fi ;; *) # start or stay - open, stand - filter, stop - close # remove and install will reset to original configuration # init and update will reset to current configuration #echo "Usage: gateway (install|remove|save|restore)" #echo "Usage: gateway (save|restore [file])" #echo "Usage: gateway (init|update)" #echo "Usage: gateway (state [param [value]]|status [log [n [pat]]])" echo "Usage: gateway (start|state|status|restart|init|update)" exit 1 esac