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