added LSBize an Init Script
[anytun.git] / etc / init.d / anytun
blob0bb5f5d970e6deb050b09d4d934393ebd74df0f9
1 #! /bin/sh
2 ### BEGIN INIT INFO
3 # Provides: anytun
4 # Required-Start: $network $named $syslog
5 # Required-Stop:
6 # Default-Start: 2 3 4 5
7 # Default-Stop: 0 1 6
8 # Short-Description: Start anycast tunneling daemon at boot time
9 # Description: Enables networking over vpn tunnel interfaces
10 ### END INIT INFO
11 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
12 DAEMON=/usr/sbin/anytun
13 ANYTUNCONFIG=/usr/bin/anytun-config
14 CONTROLDAEMON=/usr/bin/anytun-controld
15 CHROOTDIR='/var/run/anytun'
16 # DAEMONOPTS="--chroot --username nobody --chroot-dir $CHROOTDIR"
17 NAME=anytun
18 DESC=anytun
19 CONFIG_DIR=/etc/anytun
20 VARCONFIG_DIR=/var/run/anytun-controld
22 test -x $DAEMON || exit 0
24 # Include anytun defaults if available
25 if [ -f /etc/default/anytun ] ; then
26 . /etc/default/anytun
29 start_vpn () {
30 STATUS=OK
31 if [ -f $CONFIG_DIR/$NAME/config ] ; then
32 POSTUP=''
33 test -f $CONFIG_DIR/$NAME/post-up.sh && POSTUP="-x $CONFIG_DIR/$NAME/post-up.sh"
34 DAEMONARG=`sed 's/#.*//' < $CONFIG_DIR/$NAME/config | grep -e '\w' | sed 's/^/ --/' | xargs echo`
35 $DAEMON --write-pid /var/run/anytun.$NAME.pid $POSTUP \
36 $DAEMONOPTS $DAEMONARG || STATUS="FAILED"
37 else
38 STATUS="no config found"
40 start_configd
41 echo -n "($STATUS)"
43 stop_vpn () {
44 kill `cat $PIDFILE` || true
45 rm $PIDFILE
46 stop_configd
49 start_configd () {
50 if [ -d $CONFIG_DIR/$NAME/conf.d ] ; then
51 test -d $VARCONFIG_DIR || mkdir -p $VARCONFIG_DIR
52 chmod 700 $VARCONFIG_DIR
53 rm -f $VARCONFIG_DIR/$NAME 2>/dev/null
54 for CLIENTNAME in `ls $CONFIG_DIR/$NAME/conf.d`; do
55 echo -n " ($CLIENTNAME)"
56 DAEMONARG=`sed 's/#.*//' < $CONFIG_DIR/$NAME/conf.d/$CLIENTNAME | grep -e '\w' | sed 's/^/ --/' | xargs echo`
57 $ANYTUNCONFIG $DAEMONARG >> $VARCONFIG_DIR/$NAME
58 done
59 CONTROLHOST=`sed 's/#.*//' < $CONFIG_DIR/$NAME/config | grep -e 'control-host' | sed 's/^/ --/'`
60 $CONTROLDAEMON -f $VARCONFIG_DIR/$NAME $DAEMONOPTS $CONTROLHOST \
61 --write-pid $VARCONFIG_DIR/$NAME.pid
62 # rm -f $VARCONFIG_DIR/$NAME
65 stop_configd () {
66 if [ -d $CONFIG_DIR/$NAME/conf.d ] ; then
67 echo -n " ($NAME)"
68 kill `cat $VARCONFIG_DIR/$NAME.pid` || true
69 rm $VARCONFIG_DIR/$NAME.pid
70 fi
73 set -e
74 case "$1" in
75 start)
76 echo -n "Starting $DESC: "
77 test -d $CHROOTDIR || mkdir -p $CHROOTDIR
78 if test -z "$2" ; then
79 if [ -f $CONFIG_DIR/autostart ] ; then
80 for NAME in `sed 's/#.*//' < $CONFIG_DIR/autostart | grep -e '\w'`; do
81 echo -n "$NAME"
82 start_vpn
83 done
84 else
85 echo "no config found"
86 exit 1;
88 else
89 while shift ; do
90 if [ ! -z $1 ] ; then
91 NAME=$1
92 echo -n "$NAME"
93 start_vpn
95 done
97 echo "."
99 stop)
100 echo -n "Stoping $DESC: "
101 if test -z "$2" ; then
102 for PIDFILE in `ls /var/run/anytun.*.pid 2> /dev/null`; do
103 NAME=`echo $PIDFILE | cut -c17-`
104 NAME=${NAME%%.pid}
105 echo -n " $NAME"
106 stop_vpn
107 done
108 else
109 while shift ; do
110 [ -z "$1" ] && break
111 if test -e /var/run/anytun.$1.pid ; then
112 PIDFILE=`ls /var/run/anytun.$1.pid 2> /dev/null`
113 NAME=`echo $PIDFILE | cut -c17-`
114 NAME=${NAME%%.pid}
115 echo -n " $NAME"
116 stop_vpn
117 else
118 echo -n " (failure: No such tunnel is running: $1)"
120 done
122 echo .
124 reload)
125 if test -z "$2" ; then
126 for PIDFILE in `ls /var/run/anytun.*.pid 2> /dev/null`; do
127 NAME=`echo $PIDFILE | cut -c17-`
128 NAME=${NAME%%.pid}
129 if [ -d $CONFIG_DIR/$NAME/conf.d ] ; then
130 stop_vpn
131 start_vpn
132 else
133 stop_config
134 start_config
136 echo -n " $NAME"
137 done
138 else
139 while shift ; do
140 [ -z "$1" ] && break
141 if test -e /var/run/anytun.$1.pid ; then
142 PIDFILE=`ls /var/run/anytun.$1.pid 2> /dev/null`
143 NAME=`echo $PIDFILE | cut -c17-`
144 NAME=${NAME%%.pid}
145 echo -n " $NAME"
146 if [ -d $CONFIG_DIR/$NAME/conf.d ] ; then
147 stop_vpn
148 start_vpn
149 else
150 stop_config
151 start_config
153 else
154 echo -n " (failure: No such tunnel is running: $1)"
156 done
159 force-reload)
160 echo -n "Restarting $DESC: "
161 if test -z "$2" ; then
162 for PIDFILE in `ls /var/run/anytun.*.pid 2> /dev/null`; do
163 NAME=`echo $PIDFILE | cut -c17-`
164 NAME=${NAME%%.pid}
165 echo -n " $NAME"
166 stop_vpn
167 sleep 1
168 start_vpn
169 done
170 else
171 while shift ; do
172 [ -z "$1" ] && break
173 if test -e /var/run/anytun.$1.pid ; then
174 PIDFILE=`ls /var/run/anytun.$1.pid 2> /dev/null`
175 NAME=`echo $PIDFILE | cut -c17-`
176 NAME=${NAME%%.pid}
177 echo -n " $NAME"
178 stop_vpn
179 start_vpn
180 else
181 echo -n " (failure: No such tunnel is running: $1)"
183 done
185 echo .
187 restart)
188 SCRIPT=$0
189 shift
190 $SCRIPT stop $*
191 sleep 1
192 $SCRIPT start $*
195 N=/etc/init.d/$NAME
196 echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
197 exit 1
199 esac
201 exit 0