#!/bin/sh # skeleton example file to build /etc/rc.d/init.d/ scripts. # # Written by Miquel van Smoorenburg . # Modified for Debian GNU/Linux # by Ian Murdock . # Modified for RedHat Linux # by Bart Trojanowski . # # Version: @(#)skeleton 1.6 11-Nov-1996 miquels@cistron.nl # vpn 1.7 20-Dec-1999 bart@jukie.net # # # setup a path... # PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/bin/X11/: # Source RedHat function library. . /etc/rc.d/init.d/functions # # required commands... # ROUTE=/sbin/route PPPD=/usr/sbin/pppd REDIR=/usr/local/bin/pty-redir SSH=/usr/bin/ssh test -f $PPPD || exit -1 test -f $ROUTE || exit -2 test -f $REDIR || exit -3 test -f $SSH || exit -4 # #describe the connection we will be making to the slave machine... # SLAVE_HOSTNAME=gus.jukie.net SLAVE_USERNAME=vpn #SLAVE=${SLAVE_USERNAME}@${SLAVE_HOSTNAME} SLAVE="${SLAVE_HOSTNAME} -l${SLAVE_USERNAME}" # # specify both ends of the ppp tunnel # PPPIP_LOCAL=192.168.0.1 PPPIP_REMOTE=192.168.0.2 # # here is a list of all networks and their subnets that are to be # exported and imported # NETS_LOCAL="192.168.10.0/24 192.168.11.0/24" NETS_REMOTE="192.168.5.0/24 192.168.6.0/24" # # globals... # TTYNAME="" PPPDEV="" # # setup some functions... # start_remote_session() { ${REDIR} ${SSH} -t ${SLAVE} -o 'Batchmode yes' sudo \ ${PPPD} nodetach noauth passive > /tmp/device 2>&1 TTYNAME=`cat /tmp/device` if [ "${TTYNAME}" = "" ]; then rturn 1 fi # allow for the connection to stabilize sleep 10s # test to make sure the is data on this device if [ -z ${TTYNAME} ]; then TTYNAME="" return 2 fi return 0 } start_local_session() { # store the before ppp device count PPPCNT=`/sbin/ifconfig | grep ppp | wc -l` # now launch the remote portion ${PPPD} ${TTYNAME} noauth ${PPPIP_LOCAL}:${PPPIP_REMOTE} # once again allow for stabilization sleep 5s # test to see if we got a ppp0 device if [ "${PPPCNT}" = `/sbin/ifconfig | grep ppp | wc -l` ]; then return 1 fi PPPDEV=`/sbin/ifconfig | grep ppp | tail -n 1 | awk '{print $1}'` return 0 } # # START OF SCRIPT # case "$1" in start) # launch remote session echo -n "VPN: Starting remote connection on... " start_remote_session action "${TTYNAME}" test "( ${TTYNAME} )" if [ "${TTYNAME}" = "" ] ; then exit 1 ; fi # launch local session echo -n "VPN: Starting local connection on... " start_local_session action "${PPPDEV}" test "( ${PPPDEV} )" if [ "${PPPDEV}" = "" ] ; then exit 1 ; fi # setup local routes to slave nets for line in ${NETS_REMOTE} ; do ${ROUTE} add -net ${line} gw ${PPPIP_LOCAL} done # setup routes on slave to local networks for line in ${NETS_LOCAL} ; do $SSH -o 'Batchmode yes' ${SLAVE} sudo \ ${ROUTE} add -net ${line} gw ${PPPIP_REMOTE} done ;; stop) PID=`ps -ax | grep "${SSH} -t ${SLAVE} -o" | grep -v 'grep ' | awk '{print $1}'` if [ "${PID}" != "" ]; then action "Shutting down vpn on pid '$PID':" kill $PID else action "Failed to find PID for the connection" /bin/false fi ;; *) echo "Usage: vpn {start|stop}" exit 1 ;; esac exit 0