9fce417433cccf9aab500f7e90a6091121e7ea43
[tinc] / redhat / tinc
1 #!/bin/sh
2 #
3 # tinc          tincd VPN setup script
4 #
5 # chkconfig:    2345 46 54
6 #
7 # version:      1.0.8
8 # authors:      Lubomir Bulej <pallas@kadan.cz>
9 #               Mads Kiilerich <mads@kiilerich.com>
10 #
11 # description:  This script parses tinc configuration files for networks given \
12 #               in /etc/tinc/nets.boot and for each of the networks it sets up \
13 #               the interface and static routes and starts the tinc daemon.
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 & sanity checks
28
29 TINCD=/usr/sbin/tincd
30 TCONF=/etc/tinc
31 TPIDS=/var/run
32 #DEBUG=-dddd
33
34 NETSFILE=$TCONF/nets.boot
35
36 # Check the daemon
37 if [ ! -x $TINCD ]; then
38     echo "**tinc: $TINCD does not exist or is not executable!" >&2
39     exit
40 fi
41
42 # Check the configuration directory
43 if [ ! -d $TCONF ]; then
44     echo "**tinc: configuration directory ($TCONF) not found!" >&2
45     exit
46 fi
47
48 # Check nets.boot
49 if [ ! -f $NETSFILE ]; then
50     echo "**tinc: file with list of VPNs to start ($NETSFILE) not found!" >&2
51     exit
52 fi
53
54 # Load names of networks to be started
55 NETS="$(sed -e 's/#.*//; s/[[:space:]]//g; /^$/ d' $NETSFILE)"
56
57
58 ##############################################################################
59 # vpn_start ()          starts specified VPN
60
61 # $1 ... VPN to start
62
63 vpn_start () {    
64     # start tincd
65     $TINCD --net="$1" $DEBUG || \
66         { MSG="could not start daemon for network $1"; return 3; }
67     return 0
68 } # vpn_start
69
70
71 ##############################################################################
72 # vpn_stop ()           Stops specified VPN
73 #
74 # $1 ... VPN to stop
75
76 vpn_stop () {
77     # kill the tincd daemon
78     PID="$TPIDS/tinc.$1.pid"
79     if [ -f $PID ]; then
80         $TINCD --net="$1" --kill &> /dev/null
81         RET=$?
82     
83         if [ $RET -eq 0 ]; then
84             dly=0
85             while [ $dly -le 5 ]; do
86                 [ -f $PID ] || break
87                 sleep 1; dly=$((dly + 1))
88             done
89         fi
90         
91         # remove stale PID file
92         [ -f $PID ] && rm -f $PID
93     fi
94     return 0
95 } # vpn_stop
96
97
98 # Check if there is anything to start
99 if [ ! -z "$1" -a "$1" != "status" -a -z "$NETS" ]; then
100     echo "**tinc: no networks found in $NETSFILE!" >&2
101     exit
102 fi
103
104
105 # See how we were called.
106 case "$1" in
107     start)
108         for vpn in $NETS; do
109             echo -n "Bringing up TINC network $vpn: "
110             vpn_start $vpn && \
111                 success "startup of network $vpn" || \
112                 failure "startup of network $vpn"
113             echo
114             
115             if [ ! -z "$MSG" ]; then
116                 [ ! -z "$ERR" ] && echo "$ERR" >&2
117                 echo "**tinc: $MSG" >&2
118             fi
119         done
120         
121         touch /var/lock/subsys/tinc
122         ;;
123         
124     stop)
125         for vpn in $NETS; do
126             echo -n "Shutting down TINC network $vpn: "
127             vpn_stop $vpn && \
128                 success "shutdown of network $vpn" || \
129                 failure "shutdown of network $vpn"
130             echo
131             
132             if [ ! -z "$MSG" ]; then
133                 [ ! -z "$ERR" ] && echo "$ERR" >&2
134                 echo "**tinc: $MSG" >&2
135             fi
136         done
137         
138         rm -f /var/lock/subsys/tinc
139         ;;
140         
141     status)
142         echo -n "Configured VPNs: "
143         for vpn in $NETS; do
144             PID="$TPIDS/tinc.$vpn.pid"
145             
146             [ -f $PID ] && PID="$(cat $PID)" || PID="-dead-"
147             ps ax | grep "^[[:space:]]*$PID" && STS="OK" || STS="DEAD"
148             echo -n "$vpn:$STS "
149         done
150         echo             
151         ;;      
152         
153     restart)
154         $0 stop
155         $0 start
156         ;;
157                 
158     *)
159         echo "Usage: tinc {start|stop|status|restart}"
160         exit 1
161 esac
162
163 exit 0