16b09613eed1493d87e10e6ab75fca4801d0fb8b
[tinc] / debian / init.d
1 #! /bin/sh -x
2 #
3 # skeleton      example file to build /etc/init.d/ scripts.
4 #               This file should be used to construct scripts for /etc/init.d.
5 #
6 #               Written by Miquel van Smoorenburg <miquels@cistron.nl>.
7 #               Modified for Debian GNU/Linux
8 #               by Ian Murdock <imurdock@gnu.ai.mit.edu>.
9 #
10 # Version:      @(#)skeleton  1.8  03-Mar-1998  miquels@cistron.nl
11 #
12 # This file was automatically customized by dh-make on Fri, 21 Apr 2000 17:07:50 +0200
13
14 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
15 DAEMON=/usr/sbin/tincd
16 NAME=tinc
17 DESC="tinc daemons"
18 NETS="test2"
19
20 TCONF="/etc/tinc"
21
22 test -f $DAEMON || exit 0
23
24 set -e
25
26 # Check the daemon
27 if [ ! -x $TINCD ]; then
28     echo "**tinc: daemon $TINCD does not exist or is not executable!"
29     exit
30 fi
31
32 # Check the configuration directory
33 if [ ! -d $TCONF ]; then
34     echo "**tinc: configuration directory ($TCONF) not found!"
35     exit
36 fi
37
38
39 ##############################################################################
40 # vpn_load ()           Loads VPN configuration
41
42 # $1 ... VPN to load
43
44
45 vpn_load () {
46     CFG="$TCONF/$1/tinc.conf"
47     [ -f $CFG ] || { echo "tinc: $CFG does not exist" >&2 ; return 1; }
48
49     # load TINCD config
50     DEV=`grep -i -e '^[[:space:]]*TapDevice' $CFG | sed 's/[[:space:]]//g; s/^.*=//g'`
51     VPN=`grep -i -e '^[[:space:]]*(MyOwnVPNIP|MyVirtualIP)' -E $CFG | head -1 | sed 's/[[:space:]]//g; s/^.*=//g'`
52
53     # discourage empty and multiple entries
54     [ -z "$DEV" ] && \
55         { echo "tinc: TapDevice required" >&2 ; return 2; }
56     echo $DEV | grep -q '^/dev/tap' ||
57         { echo "tinc: TapDevice should be in form /dev/tapX" >&2 ; return 2; }
58     [ `echo $DEV | wc -l` -gt 1 ] &&
59         { echo "tinc: multiple TapDevice entries not allowed" >&2 ; return 3; }
60     [ -z "$VPN" ] && \
61         { echo "tinc: MyOwnVPNIP/MyVirtualIP required" >&2 ; return 2; }
62     [ `echo $VPN | wc -l` -gt 1 ] &&
63         { echo "tinc: multiple MyOwnVPNIP/MyVirtualIP entries not allowed" >&2 ; return 3; }
64     echo $VPN | grep -q -x \
65         '\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\}/[[:digit:]]\{1,2\}' || \
66         { echo "tinc: badly formed MyOwnVPNIP/MyVirtualIP address $VPN" ; return 3; }
67
68     # network device
69     TAP=`echo $DEV | cut -d"/" -f3`
70     NUM=`echo $TAP | sed 's/tap//'`
71             
72     # IP address, netmask length
73     ADR=`echo $VPN | cut -d"/" -f1`
74     LEN=`echo $VPN | cut -d"/" -f2`
75             
76     # Expand bitlength to netmask    
77     MSK=""; len=$LEN
78     for cnt in 1 1 1 0 ; do
79         if [ $len -ge 8 ]; then
80             msk=8
81         else
82             msk=$len
83         fi
84         
85         MSK="$MSK$((255 & (255 << (8 - msk))))"
86         [ $cnt -ne 0 ] && MSK="$MSK."
87         len=$((len-msk))
88     done
89
90     # Network & broadcast addresses
91 #    BRD=`ipcalc --broadcast $ADR $MSK | cut -d"=" -f2`
92 #    NET=`ipcalc --network $ADR $MSK | cut -d"=" -f2`
93             
94     # MAC address
95     MAC=`printf "fe:fd:%0.2x:%0.2x:%0.2x:%0.2x" $(echo $ADR | sed 's/\./ /g')`
96     echo
97     echo "TAP $TAP NUM $NUM ADR $ADR LEN $LEN MSK $MSK BRD $BRD NET $NET MAC $MAC" >&2
98     return 0
99 }
100
101
102 ##############################################################################
103 # vpn_start ()          starts specified VPN
104
105 # $1 ... VPN to start
106
107 vpn_start () {    
108
109     vpn_load $1 || { echo "**tinc: could not vpn_load $1" >&2 ; return 1; }
110             
111     # create device file
112     if [ ! -c $DEV ]; then
113         [ -e $DEV ] && rm -f $DEV
114         mknod --mode=0600 $DEV c 36 $((16 + NUM))
115     fi
116     
117     # load device module
118     { insmod ethertap --name="ethertap$NUM" unit="$NUM" 2>&1 || \
119         { echo "**tinc: cannot insmod ethertap$NUM" >&2 ; return 2; } ;
120     } | grep -v '^Us'
121     
122     # configure the interface
123     ip link set $TAP address $MAC
124     ip link set $TAP up
125     ip addr flush dev $TAP 2>&1 | grep -v -x '^Nothing to flush.'
126     ip addr add $VPN brd $BRD dev $TAP
127     
128     # start tincd
129     $TINCD --net="$1" $DEBUG || \
130         { echo "**tinc: could not start $TINCD" >&2; return 3; }
131
132     # default interface route
133     # ip route add $NET/$LEN dev $TAP
134
135     # setup routes
136     /etc/sysconfig/network-scripts/ifup-routes $TAP
137
138     return 0
139 } # vpn_start
140
141
142 ##############################################################################
143 # vpn_stop ()           Stops specified VPN
144 #
145 # $1 ... VPN to stop
146
147 vpn_stop () {
148
149     vpn_load $1 || return 1
150     
151     # flush the routing table
152     # ip route flush dev $TAP &> /dev/null
153     
154     # kill the tincd daemon
155     PID="$TPIDS/tinc.$1.pid"
156     if [ -f $PID ]; then
157         $TINCD --net="$1" --kill &> /dev/null
158         RET=$?
159     
160         if [ $RET -eq 0 ]; then
161             dly=0
162             while [ $dly -le 5 ]; do
163                 [ -f $PID ] || break
164                 sleep 1; dly=$((dly+1))
165             done
166         fi
167         
168         [ -f $PID ] && rm -f $PID
169     fi
170     
171     # bring the interface down
172     ip link set $TAP down &> /dev/null
173     
174     # remove ethertap module
175     rmmod "ethertap$NUM" &> /dev/null
176     
177     return 0
178 } # vpn_stop
179
180
181
182
183
184
185
186 case "$1" in
187   start)
188         echo -n "Starting $DESC:"
189         for net in $NETS ; do
190           echo -n " $net"
191           vpn_start $net
192           start-stop-daemon --start --quiet --pidfile /var/run/$NAME.$net.pid \
193                   --exec $DAEMON -- -n $net
194         done
195         echo "."
196         ;;
197   stop)
198         echo -n "Stopping $DESC:"
199         for net in $NETS ; do
200           echo -n " $net"
201           start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.$net.pid \
202                   --exec $DAEMON -- -n $net -k
203         done
204         echo "."
205         ;;
206   #reload)
207         #
208         #       If the daemon can reload its config files on the fly
209         #       for example by sending it SIGHUP, do it here.
210         #
211         #       If the daemon responds to changes in its config file
212         #       directly anyway, make this a do-nothing entry.
213         #
214         # echo "Reloading $DESC configuration files."
215         # start-stop-daemon --stop --signal 1 --quiet --pidfile \
216         #       /var/run/$NAME.pid --exec $DAEMON
217   #;;
218   restart|force-reload)
219         #
220         #       If the "reload" option is implemented, move the "force-reload"
221         #       option to the "reload" entry above. If not, "force-reload" is
222         #       just the same as "restart".
223         #
224         echo -n "Restarting $DESC:"
225         for net in $NETS ; do
226           start-stop-daemon --stop --quiet --pidfile \
227                   /var/run/$NAME.$net.pid --exec $DAEMON -- -n $net -k
228           sleep 1
229           start-stop-daemon --start --quiet --pidfile \
230                   /var/run/$NAME.$net.pid --exec $DAEMON -- -n $net
231           echo -n " $net"
232         done
233         echo "."
234         ;;
235   *)
236         N=/etc/init.d/$NAME
237         # echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
238         echo "Usage: $N {start|stop|restart|force-reload}" >&2
239         exit 1
240         ;;
241 esac
242
243 exit 0