9a26a6b9c984e6dafd297268f7f2823ab2eaa7d5
[grml-network.git] / sbin / grml-pptp-xdsl-students
1 #!/bin/sh
2 # Filename:      grml-pptp-xdsl-students
3 # Purpose:       connect via pptp to inode ["students-setup"] (www.inode.at)
4 # Authors:       grml-team (grml.org), (c) Martin Hecher <hecka@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' (version 0.3.1)
12 # by Wolfgang Scheicher
13 # and based on grml-pptp-inode
14 # by Andreas 'Jimmy' Gredler
15 ################################################################################
16
17 export LANG=C
18 DIALOG="dialog"
19
20 if [ "$UID" != 0 ];  then
21   sudo $0
22   exit
23 fi
24
25 setroute()
26 {
27 set $(grep "option routers" /var/lib/dhcp3/dhclient.leases | tail -1 | cut -f5 -d' ' | tr -d ";")
28
29 ROUTER="$1"
30 ANT="10.0.0.138"
31
32 if route -n | grep $ANT >>/dev/null
33   then
34     /sbin/route del $ANT
35   fi
36
37 /sbin/route add $ANT gw $ROUTER
38
39 if ifconfig | grep ppp0 >>/dev/null
40    then
41       set $(ifconfig ppp0 | grep P-t-P | tr -s [:blank:] | cut -f4 -d' ' | cut -b7-)
42       GATEWAY="$1"
43       /sbin/route del default
44       /sbin/route add default gw $GATEWAY
45       exit 0
46    fi
47 }
48
49 runit(){
50
51 /sbin/dhclient ${NET_DEVICE}
52
53 echo "name ${VPNUSERNAME}" > /etc/ppp/peers/inode
54 cat >> /etc/ppp/peers/inode << "EOF"
55 remotename XDSL
56 defaultroute
57 noipdefault
58 noauth
59 persist
60 logfile /var/log/pptp.log
61
62 EOF
63
64 echo "${VPNUSERNAME} XDSL ${VPNPASSWORD} *" >> /etc/ppp/pap-secrets
65 chmod 0600 /etc/ppp/pap-secrets
66
67 echo -e "#!/bin/sh\nLANG=C\nVPNSERVER=\"${VPNSERVER}\"\n" > /etc/init.d/pptp
68
69 cat >> /etc/init.d/pptp << "EOF"
70
71 PPTP="/usr/sbin/pptp $VPNSERVER call inode"
72
73 case "$1" in
74   start)
75       echo "Starting up XDSL: pptp"
76       start-stop-daemon --start --exec $PPTP
77       if [ $? == 0 ]; then
78         echo "."
79       fi
80     ;;
81   stop)
82       echo "Shutting down XDSL: pptp"
83       start-stop-daemon --stop --exec $PPTP
84       if [ $? == 0 ]; then
85         /sbin/route del -host $VPNSERVER
86         /sbin/route add default gw $GATEWAY
87         echo "."
88       fi
89     ;;
90   reconnect)
91     echo -n "Reconnecting XDSL: pptd"
92     start-stop-daemon --start --exec $PPTP
93     echo "."
94     ;;
95   *)
96       echo "Usage: /etc/init.d/pptp {start|stop|reconnect}"
97       exit 1
98     ;;
99 esac
100
101 exit 0
102 EOF
103
104 chmod +x /etc/init.d/pptp
105 touch /var/log/pptp.log
106 /etc/init.d/pptp start
107 $DIALOG --title "Information" --no-cancel --msgbox "Please click OK and wait until remote IP is assigned." 0 0
108 $DIALOG --title "PPTP Log" --no-cancel --tailbox /var/log/pptp.log 0 0
109 }
110
111 [ -n "$VPNSERVER"   ] || VPNSERVER="10.0.0.138"
112 [ -n "$VPNUSERNAME" ] || VPNUSERNAME="$(cat /etc/ppp/pap-secrets | grep XDSL | cut -d " " -f1)"
113 [ -n "$VPNPASSWORD" ] || VPNPASSWORD="$(cat /etc/ppp/pap-secrets | grep XDSL | cut -d " " -f3)"
114
115 if [ -z "$VPNUSERNAME" ] || [ -z "$VPNPASSWORD" ] ; then
116
117   COMMAND1=$($DIALOG --stdout --title "Inode XDSL Graz" --inputbox \
118   "Account name (e.g. grml@tug):" 0 35) || exit 0
119   COMMAND2=$($DIALOG --stdout --title "Inode XDSL Graz" --passwordbox "Account password (hidden typing):" 0 40) || exit 0
120
121   TMP=$(mktemp)
122
123   bailout(){
124   rm -f "$TMP"
125   exit $1
126   }
127
128   NETDEVICES="$(grep -ve 'lo:' -ve 'Inter-|' -ve 'face |bytes' /proc/net/dev | awk -F: '{print $1}')"
129
130   wireless(){
131    [ -d "/sys/class/net/${1}/wireless" ] && return 0
132    # Card exists but is not configured yet
133    [ -n "$(iwconfig $1 2>/dev/null | head -1)" ] && return 0
134    return 1
135   }
136
137   if [ -z "$NETDEVICES" ]; then
138     $DIALOG --msgbox "No network devices found." 15 45
139     bailout
140   fi
141
142   count="$(echo "$NETDEVICES" | wc -w)"
143
144   if [ "$count" -gt 1 ]; then
145     DEVICELIST=""
146     for DEVICE in $NETDEVICES; do
147       wireless "$DEVICE" && DEVICELIST="$DEVICELIST ${DEVICE} Wireless" || DEVICELIST="$DEVICELIST ${DEVICE} LAN"
148     done
149     rm -f "$TMP"
150     $DIALOG --menu "Network device to connect from:" 18 45 12 $DEVICELIST 2>"$TMP" || bailout
151     read NET_DEVICE <"$TMP" ; rm -f "$TMP"
152     else
153     # Remove additional spaces
154     NET_DEVICE="$(echo $NETDEVICES)"
155   fi
156
157   VPNUSERNAME=${COMMAND1%/*}
158   VPNPASSWORD=${COMMAND2#*/}
159   [ ! -z "$VPNUSERNAME" ] || exit 1
160   [ ! -z "$VPNPASSWORD" ] || exit 1
161   [ ! -z "$NET_DEVICE" ] || exit 1
162   runit
163   setroute
164 else
165   $DIALOG --title "Inode XDSL Graz" --msgbox "Found already configured
166   account.\nIf the settings are wrong, delete the entries in
167   /etc/ppp/pap-secrets" 10 45
168   runit
169 fi
170
171 ## END OF FILE #################################################################