chiark / gitweb /
contrib/: Add copyright notices to contributed scripts.
[tripe] / contrib / tripe-upstart.in
1 ### -*-conf-*-
2 ###
3 ### Upstart control script for tripe.
4 ###
5 ### (c) 2012 Mark Wooding
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This file is part of Trivial IP Encryption (TrIPE).
11 ###
12 ### TrIPE is free software: you can redistribute it and/or modify it under
13 ### the terms of the GNU General Public License as published by the Free
14 ### Software Foundation; either version 3 of the License, or (at your
15 ### option) any later version.
16 ###
17 ### TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 ### ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 ### FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 ### for more details.
21 ###
22 ### You should have received a copy of the GNU General Public License
23 ### along with TrIPE.  If not, see <https://www.gnu.org/licenses/>.
24
25 ###--------------------------------------------------------------------------
26 ### Identification.
27
28 description "TrIPE virtual private network server"
29 author "Mark Wooding <mdw@distorted.org.uk>"
30
31 ###--------------------------------------------------------------------------
32 ### Trigger conditions.
33
34 start on stopped pymaemo-optify
35 stop on starting shutdown
36
37 respawn
38
39 ###--------------------------------------------------------------------------
40 ### Process environment.
41
42 console none
43
44 ###--------------------------------------------------------------------------
45 ### Pre-flight check.
46
47 pre-start script
48
49         [ -f @initconfig@ ] && . @initconfig@
50
51         : ${prefix=@prefix@} ${exec_prefix=@exec_prefix@}
52         : ${bindir=@bindir@} ${sbindir=@sbindir@}
53         : ${TRIPEDIR=@configdir@} ${TRIPESOCK=@socketdir@/tripesock}
54         : ${pidfile=@pidfile@}
55         : ${tripe=$sbindir/tripe} ${tripectl=$bindir/tripectl}
56
57         PATH=/usr/bin:/usr/sbin:/bin:/sbin:$bindir
58         export PATH TRIPEDIR TRIPESOCK TRIPE_SLIPIF
59
60         ## Give up if there's no binary.
61         if test ! -x "$tripe" || test ! -x "$tripectl"; then
62           echo >&2 "Not starting/stopping TrIPE: binary files missing"
63           exit 1
64         fi
65
66         ## Give up if there's no key.
67         if test ! -f "$TRIPEDIR/keyring" ||
68            test ! -f "$TRIPEDIR/keyring.pub"; then
69           echo >&2 "Not starting/stopping TrIPE: keyring files missing"
70           exit 1
71         fi
72
73         ## Check it will work, or at least stands a fighting chance.
74         ##
75         ## (Having loads of different tunnel types doesn't help any.)
76         case ${tunnel-`$tripe --tunnels | head -1`},`uname -s` in
77
78           ## Linux TUN/TAP.
79           linux,Linux)
80             if { test -f /proc/misc && grep -q tun /proc/misc; } ||
81                modprobe -q tun; then
82               : good
83             else
84               echo >&2 "$tripe needs the Linux TUN/TAP driver to run."
85               exit 1
86             fi
87             if test -c /dev/net/tun; then
88               : good
89             else
90               echo >&2 "$tripe needs /dev/net/tun, which is missing."
91               exit 1
92             fi
93             ;;
94
95           ## Linux Unet (obsolete).
96           unet,Linux)
97             if { test -f /proc/devices && grep -q unet /proc/devices; } ||
98                modprobe -q unet; then
99               : good
100             else
101               echo >&2 "$tripe needs the Linux UNET driver to run."
102               exit 1
103             fi
104             if test -c /dev/unet; then
105               : good
106             else
107               echo >&2 "$tripe needs /dev/unet, which is missing."
108               exit 1
109             fi
110             ;;
111
112           ## BSD tun.
113           bsd,*BSD)
114             ## Don't know how to check the device is working.  Check the
115             ## device file exists and hope for the best.
116             if test -c /dev/tun0; then
117               : good
118             else
119               echo >&2 "$tripe needs /dev/tun0, which is missing."
120               exit 1
121             fi
122             ;;
123
124           ## SLIP.
125           slip,*)
126             if test "$TRIPE_SLIPIF" = ""; then
127               echo >&2 "$tripe needs SLIP interfaces set up!"
128               exit 1
129             fi
130             ;;
131
132           ## Various kinds of misconfiguration.
133           linux,* | unet,*)
134             echo >&2 "CONFIGURATION ERROR"
135             echo >&2 "  $tripe is compiled to use a Linux tunnel device, but"
136             echo >&2 "  this system is `uname -s`"
137             exit 1
138             ;;
139           bsd,*)
140             echo >&2 "CONFIGURATION ERROR"
141             echo >&2 "  $tripe is compiled to use a BSD tunnel device, but"
142             echo >&2 "  this system is `uname -s`"
143             exit 1
144             ;;
145         esac
146
147 end script
148
149 ###--------------------------------------------------------------------------
150 ### Main startup.
151
152 script
153
154         [ -f @initconfig@ ] && . @initconfig@
155
156         : ${prefix=@prefix@} ${exec_prefix=@exec_prefix@}
157         : ${bindir=@bindir@} ${sbindir=@sbindir@}
158         : ${TRIPEDIR=@configdir@} ${TRIPESOCK=@socketdir@/tripesock}
159         : ${pidfile=@pidfile@}
160         : ${tripe=$sbindir/tripe} ${tripectl=$bindir/tripectl}
161
162         PATH=/usr/bin:/usr/sbin:/bin:/sbin:$bindir
163         export PATH TRIPEDIR TRIPESOCK TRIPE_SLIPIF
164
165         ## Start the server, passing lots of arguments.
166         logopt=
167         case ${syslogp-nil} in nil) ;; *) logopt="$logopt -l" ;; esac
168         case ${logfile+t},${syslogp-nil} in
169           t,*) logopt="$logopt -f$logfile" ;;
170           ,nil) logopt="$logopt -f@logfile@" ;;
171         esac
172         $tripectl -s -p$tripe \
173           -P$pidfile \
174           $logopt \
175           ${keytag+-S-t$keytag} \
176           ${addr+-S-b$addr} ${port+-S-p$port} \
177           ${user+-U$user} ${group+-G$group} \
178           ${sockmode+-S-m$sockmode} \
179           ${trace+-S-T$trace} \
180           ${tunnel+-S-n$tunnel} \
181           $miscopts
182
183 end script
184
185 post-start script
186
187         [ -f @initconfig@ ] && . @initconfig@
188
189         : ${prefix=@prefix@} ${exec_prefix=@exec_prefix@}
190         : ${bindir=@bindir@} ${sbindir=@sbindir@}
191         : ${TRIPEDIR=@configdir@} ${TRIPESOCK=@socketdir@/tripesock}
192         : ${pidfile=@pidfile@}
193         : ${tripe=$sbindir/tripe} ${tripectl=$bindir/tripectl}
194
195         PATH=/usr/bin:/usr/sbin:/bin:/sbin:$bindir
196         export PATH TRIPEDIR TRIPESOCK TRIPE_SLIPIF
197
198         ## Wait for the server to start up.  This doesn't usually take long.
199         for i in 1 2 3 4 give-up; do
200           $tripectl help >/dev/null 2>/dev/null && break
201           sleep 1
202         done
203
204         if [ $i = give-up ]; then
205           echo >&2 "Tripe server wouldn't start"
206           exit 1
207         fi
208
209         ## Start up the ancillary services.
210         [ -d $TRIPEDIR/services ] && for i in $TRIPEDIR/services/*; do
211           [ -x $i ] || continue
212           name=`basename $i`
213           case $name in *~|\#*) continue;; esac
214           $i --daemon --startup || :
215         done
216
217         ## Start up the statically configured peers.
218         [ -d $TRIPEDIR/peers ] && for i in $TRIPEDIR/peers/*; do
219           [ -x $i ] || continue
220           name=`basename $i`
221           case $name in *~|\#*) continue;; esac
222           $i || :
223         done
224
225 end script
226
227 ###--------------------------------------------------------------------------
228 ### Shutdown.
229
230 pre-stop script
231
232         [ -f /etc/default/tripe ] && . /etc/default/tripe
233
234         : ${prefix=/usr} ${exec_prefix=/usr}
235         : ${bindir=/usr/bin} ${sbindir=/usr/sbin}
236         : ${TRIPEDIR=/etc/tripe} ${TRIPESOCK=/var/run/tripesock}
237         : ${pidfile=/var/run/tripectl.pid}
238         : ${tripe=$sbindir/tripe} ${tripectl=$bindir/tripectl}
239
240         PATH=/usr/bin:/usr/sbin:/bin:/sbin:$bindir
241         export PATH TRIPEDIR TRIPESOCK TRIPE_SLIPIF
242
243         ## If there's no socket, it must have quit (probably nonviolently).
244         if test ! -S $TRIPESOCK; then
245           :
246
247         ## Ask it to die nicely.
248         elif $tripectl quit >/dev/null 2>&1; then
249           :
250
251         ## If there's no pidfile then tripectl presumably deleted it.
252         elif test ! -f $pidfile; then
253           rm -f $TRIPESOCK
254
255         ## Otherwise kill the process unpleasantly.
256         elif kill `cat $pidfile`; then
257           :
258         else
259           exit 1
260         fi
261
262 end script
263
264 ###------ That's all, folks -------------------------------------------------