forked from NevermoreSSH/Blueblue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstunnel5.init
240 lines (222 loc) · 5.27 KB
/
stunnel5.init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#! /bin/sh -e
### BEGIN INIT INFO
# Provides: stunnel
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $syslog
# Should-Stop: $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start or stop stunnel 4.x (TLS tunnel for network daemons)
# Description: Starts or stops all configured TLS network tunnels. Each *.conf file in
# /usr/local/etc/stunnel/ will spawn a separate stunnel process. The list of files
# can be overridden in /usr/local/etc/default/stunnel, and that same file can be used
# to completely disable *all* tunnels.
### END INIT INFO
. /lib/lsb/init-functions
DEFAULTPIDFILE="/var/run/stunnel.pid"
DAEMON=/usr/local/lamvpn/stunnel5
NAME=stunnel5
DESC="Stunnel5 Service"
OPTIONS=""
get_opt() {
sed -e "s;^[[:space:]]*;;" -e "s;[[:space:]]*$;;" \
-e "s;[[:space:]]*=[[:space:]]*;=;" "$1" |
grep -i "^$2=" | sed -e "s;^[^=]*=;;"
}
get_pidfile() {
local file=$1
if [ -f $file ]; then
CHROOT=`get_opt $file chroot`
PIDFILE=`get_opt $file pid`
if [ "$PIDFILE" = "" ]; then
PIDFILE=$DEFAULTPIDFILE
fi
echo "$CHROOT/$PIDFILE"
fi
}
startdaemons() {
local res file args pidfile warn status
if ! [ -d /var/run/stunnel ]; then
rm -rf /var/run/stunnel
install -d -o stunnel -g stunnel /var/run/stunnel
fi
if [ -n "$RLIMITS" ]; then
ulimit $RLIMITS
fi
res=0
for file in $FILES; do
if [ -f $file ]; then
echo -n " $file: "
args="$file $OPTIONS"
pidfile=`get_pidfile $file`
if egrep -qe '^pid[[:space:]]*=' "$file"; then
warn=''
else
warn=' (no pid=pidfile specified!)'
fi
status=0
start_daemon -p "$pidfile" "$DAEMON" $args || status=$?
if [ "$status" -eq 0 ]; then
echo -n "started$warn"
else
echo "failed$warn"
echo "You should check that you have specified the pid= in you configuration file"
res=1
fi
fi
done;
echo ''
return "$res"
}
killdaemons()
{
local sig file pidfile status
sig=$1
res=0
for file in $FILES; do
echo -n " $file: "
pidfile=`get_pidfile $file`
if [ ! -e "$pidfile" ]; then
echo -n "no pid file"
else
status=0
killproc -p "$pidfile" "$DAEMON" ${sig:+"$sig"} || status=$?
if [ "$status" -eq 0 ]; then
echo -n 'stopped'
else
echo -n 'failed'
res=1
fi
fi
done
echo ''
return "$res"
}
querydaemons()
{
local res file pidfile status
res=0
for file in $FILES; do
echo -n " $file: "
pidfile=`get_pidfile "$file"`
if [ ! -e "$pidfile" ]; then
echo -n 'no pid file'
res=1
else
status=0
pidofproc -p "$pidfile" "$DAEMON" >/dev/null || status="$?"
if [ "$status" = 0 ]; then
echo -n 'running'
elif [ "$status" = 4 ]; then
echo "cannot access the pid file $pidfile"
res=1
else
echo -n 'stopped'
res=1
fi
fi
done
echo ''
exit "$res"
}
restartrunningdaemons()
{
local res file pidfile status args
res=0
for file in $FILES; do
echo -n " $file: "
pidfile=`get_pidfile "$file"`
if [ ! -e "$pidfile" ]; then
echo -n 'no pid file'
else
status=0
pidofproc -p "$pidfile" "$DAEMON" >/dev/null || status="$?"
if [ "$status" = 0 ]; then
echo -n 'stopping'
killproc -p "$pidfile" "$DAEMON" "$sig" || status="$?"
if [ "$status" -eq 0 ]; then
echo -n ' starting'
args="$file $OPTIONS"
start_daemon -p "$pidfile" "$DAEMON" $args || status="$?"
if [ "$status" -eq 0 ]; then
echo -n ' started'
else
echo ' failed'
res=1
fi
else
echo -n ' failed'
res=1
fi
elif [ "$status" = 4 ]; then
echo "cannot access the pid file $pidfile"
else
echo -n 'stopped'
fi
fi
done
echo ''
exit "$res"
}
if [ "x$OPTIONS" != "x" ]; then
OPTIONS="-- $OPTIONS"
fi
# If the user want to manage a single tunnel, the conf file's name
# is in $2. Otherwise, respect /usr/local/etc/default/stunnel4 setting.
# If no setting there, use /usr/local/etc/stunnel/*.conf.
if [ -n "${2:-}" ]; then
if [ -e "/etc/stunnel5/stunnel5.conf" ]; then
FILES="/etc/stunnel5/stunnel5.conf"
fi
else
if [ -z "$FILES" ]; then
FILES="/etc/stunnel5/*.conf"
fi
fi
[ -x $DAEMON ] || exit 0
set -e
res=0
case "$1" in
start)
echo -n "Starting $DESC:"
startdaemons
res=$?
;;
stop)
echo -n "Stopping $DESC:"
killdaemons
res=$?
;;
reopen-logs)
echo -n "Reopening log files $DESC:"
killdaemons USR1
res=$?
;;
force-reload|reload)
echo -n "Reloading configuration $DESC:"
killdaemons HUP
res=$?
;;
restart)
echo -n "Restarting $DESC:"
killdaemons && startdaemons
res=$?
;;
try-restart)
echo -n "Restarting $DESC if running:"
restartrunningdaemons
res=$?
;;
status)
echo -n "$DESC status:"
querydaemons
res=$?
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|status|reload|reopen-logs|restart|try-restart} [<stunnel instance>]" >&2
res=1
;;
esac
exit "$res"