Remove check for bigendianness.
[tinc] / redhat / tinc
1 #!/bin/sh
2 #
3 # tinc          tincd VPN setup script
4 #
5 # chkconfig:    2345 15 85
6 #
7 # author:       Lubomir Bulej <pallas@kadan.cz>
8 #               Modified for RPM by Mads Kiilerich <mads@kiilerich.com>
9 # version:      1.0.3
10 #
11 # description:  this script takes care of starting and setting up of VPNs \
12 #               provided by tincd daemon. It parses the configuration files \
13 #               in all VPN directories and sets up the interfaces and routing
14 #
15 # processname:  tincd
16
17 # Source function library.
18 . /etc/rc.d/init.d/functions
19
20 # Source networking configuration.
21 . /etc/sysconfig/network
22
23 # Check that networking is up.
24 [ ${NETWORKING} = "no" ] && exit 0
25
26 #############################################################################
27 # configuration
28
29 TINCD=/usr/sbin/tincd
30 TCONF=/etc/tinc
31 TPIDS=/var/run
32 #DEBUG_OPT=-dddd
33
34 # Check if ip-route is installed
35 if [ ! -f /sbin/ip ]; then
36     echo "**tinc: ip-route utilities not installed!"
37     exit
38 fi
39                  
40
41 ##############################################################################
42 # vpn_load ()           Loads VPN configuration
43
44 # $1 ... VPN to load
45
46 vpn_load () {
47     CFG="$TCONF/$1/tincd.conf"
48     [ -f $CFG ] || { echo "Error: $CFG does not exist" >&2 ; return 1 }
49     
50     # load TINCD config
51     DEV=`grep -i -e '^TapDevice' $CFG | sed 's/[[:space:]]//g;s/^.*=//g'`
52     VPN=`grep -i -e '^(MyOwnVPNIP|MyVirtualIP)' -E $CFG | head -1 | sed 's/[[:space:]]//g;s/^.*=//g'`
53     
54     # discourage empty and multiple entries
55     [ -z "$DEV" ] && \
56                 { echo "Error: TapDevice needed" >&2 ; return 2 }
57     echo $DEV | grep -q '^/dev/tap' ||
58                 { echo "Error: TapDevice needs /dev/tapX" >&2 ; return 2 }
59     [ `echo $DEV | wc -l` -gt 1 ] && \
60                 { echo "Error in TapDevice" >&2 ; return 3 }
61     [ -z "$VPN" ] && \
62                 { echo "Error: MyOwnVPNIP/MyVirtualIP needed" >&2 ; return 2 }
63     [ `echo $VPN | wc -l` -gt 1 ] && \
64                 { echo "Error in MyOwnVPNIP/MyVirtualIP" >&2 ; return 3 }
65     echo $VPN | grep -q -x  \
66         '\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\}/[[:digit:]]\{1,2\}' ||
67                 { echo "Error in MyOwnVPNIP/MyVirtualIP address $VPN" ; 
68                         return 3 }
69     
70     # network device
71     TAP=`echo $DEV | cut -d"/" -f3`
72     NUM=`echo $TAP | sed 's/tap//'`
73
74     # IP address, netmask length
75     ADR=`echo $VPN | cut -d"/" -f1`
76     LEN=`echo $VPN | cut -d"/" -f2`
77
78     # Expand bitlength to netmask    
79     MSK=""; len=$LEN
80     for cnt in 1 1 1 0; do
81         if [ $len -ge 8 ]; then
82             msk=8
83         else
84             msk=$len
85         fi
86             
87         MSK="$MSK$((255 & (255 << (8 - msk))))"
88         [ $cnt -ne 0 ] && MSK="$MSK."
89         len=$((len-msk))
90     done
91
92     # Network & broadcast
93     BRD=`ipcalc --broadcast $ADR $MSK | cut -d"=" -f2`
94     NET=`ipcalc --network $ADR $MSK | cut -d"=" -f2`
95         
96     # MAC address
97     MAC=`printf "fe:fd:%0.2x:%0.2x:%0.2x:%0.2x" $(echo $ADR | sed 's/\./ /g')`
98     # echo "TAP $TAP NUM $NUM ADR $ADR LEN $LEN MSK $MSK BRD $BRD NET $NET MAC $MAC" >&2
99     return 0
100 }
101
102
103 ##############################################################################
104 # vpn_start ()          starts specified VPN
105
106 # $1 ... VPN to start
107
108 vpn_start () {    
109
110     vpn_load $1 || { echo "Error: Could not vpn_load $1" >&2 ; return 1 }
111             
112     # create device file
113     if [ ! -c $DEV ]; then
114         [ -e $DEV ] && rm -f $DEV
115         mknod --mode=0600 $DEV c 36 $((16 + NUM))
116     fi
117     
118     # load device module
119     { insmod ethertap --name="ethertap$NUM" unit="$NUM" 2>&1  || \
120                 { echo "Error: cannot insmod ethertap$NUM" >&2 ; return 2 }
121     } | grep -v '^Us'
122     
123     # configure the interface
124     ip link set $TAP address $MAC #&> /dev/null
125     ip link set $TAP up #&> /dev/null
126     ip addr flush dev $TAP 2>&1 | grep -v -x '^Nothing to flush.' #&> /dev/null
127     ip addr add $VPN brd $BRD dev $TAP #&> /dev/null
128     
129     # start tincd
130     $TINCD --net="$1" $DEBUG_OPT || { echo "Error: Cannot start $TINCD" >&2; 
131                 return 3 }
132
133     # default interface route
134     ip route add $NET/$LEN dev $TAP #&> /dev/null
135
136     # setup routes
137     /etc/sysconfig/network-scripts/ifup-routes $TAP
138
139     return 0
140 } # vpn_start
141
142
143 ##############################################################################
144 # vpn_stop ()           Stops specified VPN
145 #
146 # $1 ... VPN to stop
147
148 vpn_stop () {
149
150     vpn_load $1 || return 1
151     
152     # flush the routing table
153     ip route flush dev $TAP &> /dev/null
154     
155     # kill the tincd daemon
156     PID="$TPIDS/tincd.$1.pid"
157     if [ -f $PID ]; then
158         $TINCD --net="$1" --kill &> /dev/null
159         RET=$?
160     
161         if [ $RET -eq 0 ]; then
162             dly=0
163             while [ $dly -le 5 ]; do
164                 [ -f $PID ] || break
165                 sleep 1; dly=$((dly+1))
166             done
167         else
168             rm -f $PID &> /dev/null
169         fi
170     fi
171     
172     # bring the interface down
173     ip link set $TAP down &> /dev/null
174     
175     # remove kernel module
176     rmmod "ethertap$NUM" &> /dev/null
177     
178     return 0
179 } # vpn_stop
180
181
182 # See how we were called.
183 case "$1" in
184     start)
185         echo -n "Bringing up VPNs: "
186         for vpn in `ls -1 $TCONF`; do
187             vpn_start $vpn && echo -n "$vpn "
188         done
189         
190         touch /var/lock/subsys/tinc
191         action "" /bin/true
192         ;;
193         
194     stop)
195         echo -n "Shutting down VPNs: "
196         for vpn in `ls -1 $TCONF`; do
197             vpn_stop $vpn && echo -n "$vpn "
198         done
199         
200         rm -f /var/lock/susbsys/tinc
201         action "" /bin/true
202         ;;
203         
204     status)
205         echo -n "Currently running VPNs: "
206         for vpn in `ls -1 $TCONF`; do
207             PID="$TPIDS/tincd.$vpn.pid"
208             echo -n "$vpn "
209             if [ -f $PID -a `ps ax | grep "^ *$(cat $PID)" | wc -l` -eq 1 ] 
210             then
211                 echo -n "OK "
212             else
213                 echo -n "Dead "
214             fi
215         done
216         echo             
217         ;;      
218         
219     restart)
220         $0 stop
221         $0 start
222         ;;
223                 
224     *)
225         echo "Usage: tinc {start|stop|status|restart}"
226         exit 1
227 esac
228
229 exit 0