82caa26f049ea77092c78a1aadeb0eef6691d5e5
[10.03/packages.git] / net / quicktun / files / quicktun.init
1 #!/bin/sh /etc/rc.common
2 # Quicktun init script
3 # Partly taken the the OpenVPN init script (Copyright (C) 2008 Jo-Philipp Wich)
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
6
7 START=95
8 BIN=/usr/sbin/quicktun
9 SSD=start-stop-daemon
10 EXTRA_COMMANDS="up down"
11
12 LIST_SEP="
13 "
14
15 append_opt() {
16         local p="$1"; local v="$2"; local p_uc
17         
18         p_uc=$(echo "$p" | tr '[a-z]' '[A-Z]')
19         OPTS="$OPTS \"$p_uc=$v\""
20 }
21
22 append_opts() {
23         local p; local v; local s="$1"; shift
24         for p in $*; do
25                 config_get v "$s" "$p"
26                 [ -n "$v" ] && append_opt "$p" "$v"
27         done
28 }
29
30 start_service() {
31         local s="$1"
32         local enable=0
33
34         # disabled?
35         config_get_bool enable "$s" enable 0
36         [ "$enable" == 0 ] && return 0
37
38         PID="/var/run/quicktun-$s.pid"
39         OPTS=""
40
41         config_get interface "$s" interface
42         if [ -z "$interface" ]; then
43                 echo "$s: interface not set"
44                 return 1
45         fi
46
47         if ifconfig "$interface" >/dev/null 2>&1; then
48                 echo "$s: interface $interface is already in use"
49                 return 1
50         fi
51
52         append_opts "$s" interface local_address local_port remote_address remote_port \
53                 protocol private_key public_key time_window
54
55         config_get_bool tun_mode "$s" tun_mode 0
56         [ "$tun_mode" == 1 ] && append_opt tun_mode 1
57
58         config_get_bool remote_float "$s" remote_float 0
59         [ "$remote_float" == 1 ] && append_opt remote_float 1
60
61         eval env $OPTS "$SSD" -q -b -p "$PID" -m -x "$BIN" -S
62
63         while ! ifconfig "$interface" >/dev/null 2>&1; do
64                 if ! $SSD -t -q -p $PID -x $BIN -K; then
65                         echo "$s: daemon startup failed"
66                         return 1
67                 fi
68
69                 sleep 1
70         done
71
72         config_get up "$s" up
73         [ -n "$up" ] && sh -c "$up" - "$interface"
74 }
75
76 stop_service() {
77         local s="$1"
78         local enable=0
79
80         # disabled?
81         config_get_bool enable "$s" enable 0
82         [ "$enable" == 0 ] && return 0
83
84         config_get interface "$s" interface
85         if [ -z "$interface" ]; then
86                 echo "$s: interface not set"
87                 return 1
88         fi
89
90         if ! ifconfig "$interface" >/dev/null 2>&1; then
91                 echo "$s: interface $interface does not exist"
92                 return 1
93         fi
94
95         config_get down "$s" down
96         [ -n "$down" ] && sh -c "$down" - "$interface"
97
98         PID="/var/run/quicktun-$s.pid"
99
100         $SSD -q -p $PID -x $BIN -K
101         rm -f "$PID"
102 }
103
104 start() {
105         config_load quicktun
106         config_foreach start_service quicktun
107 }
108
109 stop() {
110         config_load quicktun
111         config_foreach stop_service quicktun
112 }
113
114 restart() {
115         stop; start
116 }
117
118 up() {
119         local exists
120         local INSTANCE
121         config_load quicktun
122         for INSTANCE in "$@"; do
123                 config_get exists "$INSTANCE" TYPE
124                 if [ "$exists" == "quicktun" ]; then
125                         start_service "$INSTANCE"
126                 fi
127         done
128 }
129
130 down() {
131         local exists
132         local INSTANCE
133         config_load quicktun
134         for INSTANCE in "$@"; do
135                 config_get exists "$INSTANCE" TYPE
136                 if [ "$exists" == "quicktun" ]; then
137                         stop_service "$INSTANCE"
138                 fi
139         done
140 }