#!/bin/sh
##########################################################################
#           Copyright (c) 2001, Cisco Systems, All Rights Reserved
###########################################################################
#
#  File:    vpnclient_init
#  Date:    04/23/2001
#
###########################################################################
#
# chkconfig: 345 85 85
# description: Startup script for the vpn client. Version 4.8.02 (0030)
#
###########################################################################
# Source function library.
VPNCLIENT="/opt/cisco-vpnclient/bin/vpnclient"
VPNDEV="cipsec0"
VPNMOD=cisco_ipsec
case `uname -r` in
2.[56].*)
    VPNMOD_FILE="${VPNMOD}.ko"
    ;;
*)
    VPNMOD_FILE="$VPNMOD"
    ;;
esac    

WHOAMI=`id | sed -e 's/(.*//'`

# See how we were called.
case "$1" in
  start)
	echo -n "Starting ${VPNCLIENT}: "
	if [ "$WHOAMI" != "uid=0" ] ; then
		echo "Failed (super user access required)"
		exit 1
	fi
	/sbin/lsmod | grep -q "${VPNMOD}"
        if [ "$?" = "0" ] ; then 
		echo "module ${VPNMOD} is already running. Use restart instead."
		exit 1
	fi

	if [ -f /etc/resolv.conf.vpnbackup ]; then
		echo "restoring /etc/resolv.conf"
		mv /etc/resolv.conf.vpnbackup /etc/resolv.conf
	fi
	if [ -d /lib/modules/preferred ]; then
		PC=/lib/modules/preferred/CiscoVPN
	else
		PC=/lib/modules/`uname -r`/CiscoVPN
	fi
	if [ -d $PC ] ; then
		/sbin/insmod ${PC}/${VPNMOD_FILE}
		if [ "$?" != "0" ] ; then
			echo "Failed (insmod)"
		exit 1
        fi
	else
		echo "module directory $PC not found."
		exit 1
	fi
	case "`uname -r`" in
	2.6.*)
		;;
	2.5.*)
		;;
	2.4.*)
		;;
	2.2.*)
		;;
	2.0.*)
		#
		# This is only needed due to a bug in 2.0.x kernels that affects
		# arp lookups.
		#
		ifconfig $VPNDEV 222.222.222.222 ;
		if [ "$?" != "0" ] ; then
			echo "Failed (ifconfig)"
			/sbin/rmmod ${VPNMOD}
			exit 1
		fi
		;;
	*)
		echo "Failed (unsupported Linux version)"
		/sbin/rmmod ${VPNMOD}
		exit 1
		;;
	esac
	
	echo "Done"
	;;
  stop)
	echo -n "Shutting down ${VPNCLIENT}: "
	if [ "$WHOAMI" != "uid=0" ] ; then
		echo "Failed (super user access required)"
		exit 1
	fi
    killall cvpnd > /dev/null 2>&1

	/sbin/lsmod | grep -q "${VPNMOD}"
    if [ "$?" != "0" ] ; then 
		echo "module ${VPNMOD} is not running."
		exit 1
	fi
	/sbin/ifconfig $VPNDEV down
	if [ "$?" != "0" ] ; then
		echo "Failed (ifconfig)"
		exit 1
	fi
	/sbin/rmmod ${VPNMOD}
	if [ "$?" != "0" ] ; then
		echo "Failed (rmmod)"
		exit 1
	fi
	echo "Done"
	;;
  status)
	/sbin/lsmod | egrep 'Module'
	/sbin/lsmod | egrep "${VPNMOD}"
	if [ "$?" != "0" ] ; then
			echo
			echo "Status Failed (lsmod ${VPNMOD}) - The VPN module is not loaded."
    fi
	echo
	/sbin/ifconfig $VPNDEV
	if [ "$?" != "0" ] ; then
		echo
		echo "Status Failed (ifconfig ${VPNDEV}) - The virtual interface is not present."
		exit 1
	fi
	;;
  restart|reload)
	$0 stop
	$0 start
	;;
  *)
	echo "Usage: $0 {start|stop|restart|reload|status}"
	exit 1
esac

exit 0
