#!/bin/bash

# Connection script for Stevens Inst. of Tech. VPN (Campus Domain Auth)
# http://www.coglib.com/~jcordasc/onsite/stevensvpn.sh

# Copyright (c) 2008 Jared Cordasco (jcordasc@coglib.com)
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''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 THE AUTHOR 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.

#
# Interface created by pppd for vpn traffic
#
VPN_INTF=ppp0

#
# IP address of the vpn server
#
VPN_SERVER=155.246.151.11

#
# First three octets of subnet on which the client is assigned an address
# (NOTE: This is used for determining the internal IP address assigned to
#        the client. Due to this, it must be a string of the form:
#        xxx.yyy.zzz in order to work.)
#
INT_SUBNET=155.246.152

#
# Network and netmask describing which traffic should be routed over the
# vpn connection.
#
VPN_NETWORK=155.246.0.0
VPN_NETMASK=255.255.0.0

#
# pppd profile correctly configured for vpn (found in /etc/ppp/peers/)
#
PPPD_PROFILE=stevensvpn

#
# Amount of time to sleep waiting for vpn connection to complete
#
SLEEP=5

### Do not edit below this line #########################################

PATH=$PATH:/sbin:/usr/sbin

ROUTE_INFO=$(route -n | egrep -e '^0.0.0.0' | awk '{print $2 ":" $8}')
DFLT_GWY=${ROUTE_INFO%%:*}
EXT_INTF=${ROUTE_INFO##*:}


start_vpn() {
	ROUTE_INFO=$(route -n | egrep -e "^$VPN_SERVER" | \
					awk '{print $2 ":" $3 ":" $8}')

	if [ "$ROUTE_INFO" != "$DFLT_GWY:255.255.255.255:$EXT_INTF" ] ; then
		echo -n "Adding route to VPN server ... "
		route add -host $VPN_SERVER gw $DFLT_GWY dev $EXT_INTF
		echo "Done."
	fi

	echo -n "Starting pppd ... "
	pppd call $PPPD_PROFILE
	echo "Done."

	echo -n "Waiting $SLEEP seconds for connection ... "
	sleep $SLEEP
	echo "Done."
	ifconfig $VPN_INTF > /dev/null 2>&1
	EXIT="$?"

	while [ "$EXIT" != "0" ] ; do
		echo "  Connection not up."
		echo -n "  Waiting $SLEEP seconds for connection ... "
		sleep $SLEEP
		echo "Done."
		ifconfig $VPN_INTF > /dev/null 2>&1
		EXIT="$?"
	done

	INT_IPADDR=$(ifconfig $VPN_INTF | \
				sed -ne "/P-t-P/ { s/^.*\\($INT_SUBNET.[^ ]*\\).*$/\\1/ ; p }")

	echo -n "Deleting loop route ..."
	route del -host $VPN_SERVER dev $VPN_INTF
	echo "Done."

	echo -n "Adding route to $VPN_NETWORK/$VPN_NETMASK ..."
	route add -net $VPN_NETWORK netmask $VPN_NETMASK \
			gw $INT_IPADDR dev $VPN_INTF
	echo "Done."
}


stop_vpn() {
	echo -n "Killing pptp ... "
	killall -15 pptp
	echo "Done."

	ROUTE_INFO=$(route -n | egrep -e "^$VPN_SERVER" | \
					awk '{print $2 ":" $3 ":" $8}')

	if [ "$ROUTE_INFO" == "$DFLT_GWY:255.255.255.255:$EXT_INTF" ] ; then
		echo -n "Removing route to VPN server ... "
		route del -host $VPN_SERVER gw $DFLT_GWY dev $EXT_INTF
		echo "Done."
	fi
}


usage() {
	echo -e "Usage:\n\t$(basename $0) <start|stop>\n"
	exit 1
}


if [ "$(id -u)" != 0 ] ; then
	echo -e "\n*** Script must be run as root ***\n"
	exit 2
fi

if [ -z "$1" ] ; then
	usage
fi

case "$1" in
	start)
		start_vpn
		;;
	stop)
		stop_vpn
		;;
	*)
		usage
		;;
esac
