#!/bin/zsh
# Filename:      grml-pptp-vcgraz
# Purpose:       connect via pptp in vc-graz (www.vc-graz.ac.at)
# Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2.
# Latest change: Sam Mai 27 15:12:52 CEST 2006 [mika]
################################################################################

################################################################################
# Notes:
# This script is based on 'knoppix-pptp' (versions 0.3.1 and 0.4.0-test2)
# by Wolfgang Scheicher.
################################################################################

LANG=C
LC_ALL=C

if [ "$UID" != 0 ];  then
  sudo $0
  exit
fi

runit(){
echo "name ${VPNUSERNAME}" > /etc/ppp/peers/vc-graz
cat >> /etc/ppp/peers/vc-graz << "EOF"

remotename PPTP
ipparam tunl
lock
noauth
nobsdcomp
nodeflate
refuse-pap
refuse-eap
noccp
mtu 1460
mru 1500
lcp-echo-failure 10
lcp-echo-interval 10
logfile /var/log/pptp.log
persist
maxfail 3
holdoff 15
noipdefault
defaultroute
EOF

# make sure it is not readable by any non-root users:
touch     /etc/ppp/chap-secrets
chmod 600 /etc/ppp/chap-secrets
# don't overwrite existing files - so just append:
echo "${VPNUSERNAME} PPTP ${VPNPASSWORD} *" >> /etc/ppp/chap-secrets

echo -e "#!/bin/sh\nLANG=C\nVPNSERVER=\"${VPNSERVER}\"\nDORMITORY=\"${DORMITORY}\"" > /etc/init.d/pptp-vcgraz
cat >> /etc/init.d/pptp-vcgraz << "EOF"
# connect to vc-graz via pptp
case "$1" in
  start)
    ifconfig | grep $VPNSERVER > /dev/null && echo "PPTP already started"
    ifconfig | grep $VPNSERVER > /dev/null && exit 0
    echo "Starting PPTP Tunnel"
    route del default
    route add -host $VPNSERVER gw 10.${DORMITORY}.0.1
    route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.${DORMITORY}.0.1
    pppd pty "pptp $VPNSERVER --nolaunchpppd" call vc-graz updetach || exit 1
    ;;

  stop)
    ifconfig | grep $VPNSERVER > /dev/null || echo "PPTP already stopped"
    ifconfig | grep $VPNSERVER > /dev/null || exit 0
    echo  "Stopping PPTP Tunnel"
    killall -HUP pppd
    killall -HUP pptp
    sleep 1
    killall pppd
    killall pptp
    route del -host $VPNSERVER gw 10.${DORMITORY}.0.1
    route del -net 10.0.0.0 netmask 255.0.0.0 gw 10.${DORMITORY}.0.1
    route add default gateway 10.${DORMITORY}.0.1
    ;;

  restart)
    /etc/init.d/pptp-vcgraz stop
    sleep 1
    /etc/init.d/pptp-vcgraz start
    ;;

  status)
    dialog --title "Status of /etc/init.d/pptp-vcgraz - PPTP Log" --no-cancel --tailbox /var/log/pptp.log 0 0
    ;;

  *)
    echo 'Usage: /etc/init.d/pptp-vcgraz {start|stop|restart}' >&2
    exit 1
    ;;
esac

exit 0
EOF

chmod +x /etc/init.d/pptp-vcgraz
touch /var/log/pptp.log
/etc/init.d/pptp-vcgraz start
dialog --title "PPTP Log" --no-cancel --tailbox /var/log/pptp.log 0 0
}

[ -n "$VPNSERVER" ] || VPNSERVER="10.0.0.3"
[ -n "$DORMITORY" ] || DORMITORY="$(ifconfig | grep "addr:10" | cut -d. -f2)"

if [ -z "$DORMITORY" ]; then
  dialog --stdout --title "VC-Graz"  --msgbox "No 10.x.x.x ip address found, sorry. grml-pptp-vcgraz does work only for VC-Graz in 10.x.x.x network. Make sure you requested an ip address via DHCP. Try running pump/dhclient otherwise." 0 0 || \
  echo "No 10.x.x.x ip address found, sorry. grml-pptp-vcgraz does work only for VC-Graz in 10.x.x.x network. Make sure you requested an ip address via DHCP. Try running pump/dhclient otherwise."
  exit 1
fi

if [ ! -x /usr/sbin/pppd ]; then
  dialog --stdout --title "VC-Graz"  --msgbox "/usr/sbin/pppd not found. Huh?!" 0 0 ||
  echo "Error: /usr/sbin/pppd not found. Huh?!"
  exit 1
fi

if [ ! -x /usr/sbin/pptp ]; then
  dialog --stdout --title "VC-Graz"  --msgbox "/usr/sbin/pptp not found. Huh?!" 0 0 ||
  echo "Error: /usr/sbin/pptp not found. Huh?!"
  exit 1
fi

#[ -n "$VPNUSERNAME" ] || VPNUSERNAME=$(cat /etc/ppp/chap-secrets | grep PPTP | head -1 | cut -d " " -f1)
#[ -n "$VPNPASSWORD" ] || VPNPASSWORD=$(cat /etc/ppp/chap-secrets | grep PPTP | head -1 | cut -d " " -f3)

if [ -z "$VPNUSERNAME" ] || [ -z "$VPNPASSWORD" ] ; then
  COMMAND1=$(dialog --stdout --title "Virtual Campus Graz" --inputbox    "Account number:" 0 0) || exit 0
  VPNUSERNAME=${COMMAND1%/*}
  if [ -z "$VPNUSERNAME" ] ; then
     dialog --stdout --title "Virtual Campus Graz" --msgbox "Sorry, please provide a valid username. Exiting." 0 0
     exit 1
  fi
  COMMAND2=$(dialog --stdout --title "Virtual Campus Graz" --passwordbox "Account password (hidden typing)" 0 40) || exit 0
  VPNPASSWORD=${COMMAND2#*/}
  if [ -z "$VPNPASSWORD" ] ; then
     dialog --stdout --title "Virtual Campus Graz" --msgbox "Sorry, please provide a valid password. Exiting." 0 0
     exit 1
  fi
  runit
else
  runit
fi

## END OF FILE #################################################################
