902a2ab1df91b21b8a5a1bced5f0d274ee6eb6aa
[grml-scripts.git] / usr_sbin / grml-pptp-vcgraz
1 #!/bin/zsh
2 # Filename:      grml-pptp-vcgraz
3 # Purpose:       connect via pptp in vc-graz (www.vc-graz.ac.at)
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 # Latest change: Sam Mai 27 15:12:52 CEST 2006 [mika]
8 ################################################################################
9
10 ################################################################################
11 # Notes:
12 # This script is based on 'knoppix-pptp' (versions 0.3.1 and 0.4.0-test2)
13 # by Wolfgang Scheicher.
14 ################################################################################
15
16 LANG=C
17 LC_ALL=C
18
19 if [ "$UID" != 0 ];  then
20   sudo $0
21   exit
22 fi
23
24 runit(){
25 echo "name ${VPNUSERNAME}" > /etc/ppp/peers/vc-graz
26 cat >> /etc/ppp/peers/vc-graz << "EOF"
27
28 remotename PPTP
29 ipparam tunl
30 lock
31 noauth
32 nobsdcomp
33 nodeflate
34 refuse-pap
35 refuse-eap
36 noccp
37 mtu 1460
38 mru 1500
39 lcp-echo-failure 10
40 lcp-echo-interval 10
41 logfile /var/log/pptp.log
42 persist
43 maxfail 3
44 holdoff 15
45 noipdefault
46 defaultroute
47 EOF
48
49 # make sure it is not readable by any non-root users:
50 touch     /etc/ppp/chap-secrets
51 chmod 600 /etc/ppp/chap-secrets
52 # don't overwrite existing files - so just append:
53 echo "${VPNUSERNAME} PPTP ${VPNPASSWORD} *" >> /etc/ppp/chap-secrets
54
55 echo -e "#!/bin/sh\nLANG=C\nVPNSERVER=\"${VPNSERVER}\"\nDORMITORY=\"${DORMITORY}\"" > /etc/init.d/pptp-vcgraz
56 cat >> /etc/init.d/pptp-vcgraz << "EOF"
57 # connect to vc-graz via pptp
58 case "$1" in
59   start)
60     ifconfig | grep $VPNSERVER > /dev/null && echo "PPTP already started"
61     ifconfig | grep $VPNSERVER > /dev/null && exit 0
62     echo "Starting PPTP Tunnel"
63     route del default
64     route add -host $VPNSERVER gw 10.${DORMITORY}.0.1
65     route add -net 10.0.0.0 netmask 255.0.0.0 gw 10.${DORMITORY}.0.1
66     pppd pty "pptp $VPNSERVER --nolaunchpppd" call vc-graz updetach || exit 1
67     ;;
68
69   stop)
70     ifconfig | grep $VPNSERVER > /dev/null || echo "PPTP already stopped"
71     ifconfig | grep $VPNSERVER > /dev/null || exit 0
72     echo  "Stopping PPTP Tunnel"
73     killall -HUP pppd
74     killall -HUP pptp
75     sleep 1
76     killall pppd
77     killall pptp
78     route del -host $VPNSERVER gw 10.${DORMITORY}.0.1
79     route del -net 10.0.0.0 netmask 255.0.0.0 gw 10.${DORMITORY}.0.1
80     route add default gateway 10.${DORMITORY}.0.1
81     ;;
82
83   restart)
84     /etc/init.d/pptp-vcgraz stop
85     sleep 1
86     /etc/init.d/pptp-vcgraz start
87     ;;
88
89   status)
90     dialog --title "Status of /etc/init.d/pptp-vcgraz - PPTP Log" --no-cancel --tailbox /var/log/pptp.log 0 0
91     ;;
92
93   *)
94     echo 'Usage: /etc/init.d/pptp-vcgraz {start|stop|restart}' >&2
95     exit 1
96     ;;
97 esac
98
99 exit 0
100 EOF
101
102 chmod +x /etc/init.d/pptp-vcgraz
103 touch /var/log/pptp.log
104 /etc/init.d/pptp-vcgraz start
105 dialog --title "PPTP Log" --no-cancel --tailbox /var/log/pptp.log 0 0
106 }
107
108 [ -n "$VPNSERVER" ] || VPNSERVER="10.0.0.3"
109 [ -n "$DORMITORY" ] || DORMITORY="$(ifconfig | grep "addr:10" | cut -d. -f2)"
110
111 if [ -z "$DORMITORY" ]; then
112   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 || \
113   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."
114   exit 1
115 fi
116
117 if [ ! -x /usr/sbin/pppd ]; then
118   dialog --stdout --title "VC-Graz"  --msgbox "/usr/sbin/pppd not found. Huh?!" 0 0 ||
119   echo "Error: /usr/sbin/pppd not found. Huh?!"
120   exit 1
121 fi
122
123 if [ ! -x /usr/sbin/pptp ]; then
124   dialog --stdout --title "VC-Graz"  --msgbox "/usr/sbin/pptp not found. Huh?!" 0 0 ||
125   echo "Error: /usr/sbin/pptp not found. Huh?!"
126   exit 1
127 fi
128
129 #[ -n "$VPNUSERNAME" ] || VPNUSERNAME=$(cat /etc/ppp/chap-secrets | grep PPTP | head -1 | cut -d " " -f1)
130 #[ -n "$VPNPASSWORD" ] || VPNPASSWORD=$(cat /etc/ppp/chap-secrets | grep PPTP | head -1 | cut -d " " -f3)
131
132 if [ -z "$VPNUSERNAME" ] || [ -z "$VPNPASSWORD" ] ; then
133   COMMAND1=$(dialog --stdout --title "Virtual Campus Graz" --inputbox    "Account number:" 0 0) || exit 0
134   VPNUSERNAME=${COMMAND1%/*}
135   if [ -z "$VPNUSERNAME" ] ; then
136      dialog --stdout --title "Virtual Campus Graz" --msgbox "Sorry, please provide a valid username. Exiting." 0 0
137      exit 1
138   fi
139   COMMAND2=$(dialog --stdout --title "Virtual Campus Graz" --passwordbox "Account password (hidden typing)" 0 40) || exit 0
140   VPNPASSWORD=${COMMAND2#*/}
141   if [ -z "$VPNPASSWORD" ] ; then
142      dialog --stdout --title "Virtual Campus Graz" --msgbox "Sorry, please provide a valid password. Exiting." 0 0
143      exit 1
144   fi
145   runit
146 else
147   runit
148 fi
149
150 ## END OF FILE #################################################################