forked from Freetz/freetz
-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
rc.openvpn
executable file
·167 lines (149 loc) · 4.09 KB
/
rc.openvpn
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
#!/bin/sh
scriptname=$0
DAEMON=${scriptname##*/rc.}
. /etc/init.d/modlibrc
mypidof() {
ps -w | sed -n "/\/m[o]d\/etc\/${1}.conf/ s/^[ ]*\([0-9]*\)[ ].*/\1/ p"
}
config(){
if [ "$OPENVPN_LOADTUN" == "yes" ]; then
modprobe tun 2>/dev/null
modprobe yf_patchkernel 2>/dev/null
fi
if [ -r "/tmp/flash/openvpn/own_${DAEMON}.conf" ]; then
cp /tmp/flash/openvpn/own_${DAEMON}.conf ${DAEMON_CONFIG}
else
modlib_config ${DAEMON} ${DAEMON_CONFIG}
fi
}
start() {
cp -f /etc/resolv.conf /tmp/openvpn/etc
cp -f /etc/TZ /tmp/openvpn/etc
mknod -m 444 /tmp/openvpn/dev/urandom c 1 9 2>/dev/null
for file in $(echo ${OPENVPN_FILES2CP#*#} | tr ';#' '\n\n'); do
if [ ! -r $file ]; then # not a full path? Try to find a binary
fullpath=$(which $file);
[ ! x = x$fullpath ] && file=$fullpath
fi
if [ -r $file ]; then
mkdir -p /tmp/openvpn${file%/*}
cp -a $file /tmp/openvpn${file%/*}
else
echo "Copying files to chroot. File $file not found. Skipped"
fi
done
trap 'echo -e "You may get some more hints by starting ${DAEMON} w/o \"--daemon\":\n\t$DAEMON --config ${DAEMON_CONFIG}"' EXIT
modlib_startdaemon ${DAEMON} --config ${DAEMON_CONFIG} --writepid /var/run/${DAEMON}.pid --daemon
trap - EXIT
# if we have brctl, then try to add tap to "lan" if requestet (ignore errors)
if [ $(which brctl) ] && TAP=$(grep "#Helperline" ${DAEMON_CONFIG} | grep -o tap[0-9] ); then
brctl addif lan $TAP 2> /dev/null
fi
}
stop() {
echo -n "Stopping ${DAEMON} ... "
PID=$(mypidof $DAEMON)
if [ -z "$PID" ]; then
echo "not running."
return 1
fi
kill $PID > /dev/null 2>&1
exitval=$?
if [ "$exitval" -eq 0 ]; then
echo 'done.'
else
echo 'failed.'
exit $exitval
fi
rm -f /var/run/${DAEMON}.pid
}
if [ ! -e /var/tmp/.openvpnfirstrun ]; then
touch /var/tmp/.openvpnfirstrun
/mod/etc/default.openvpn/openvpn_dynamic_conf
fi
[ -x /mod/sbin/$DAEMON ] || ln -s /usr/sbin/openvpn /mod/sbin/$DAEMON
case $1 in
""|load)
if [ "$DAEMON" = "openvpn" ]; then
modreg cgi 'openvpn' 'OpenVPN'
modreg daemon openvpn
modreg extra openvpn "$(lang de:"Verbundene Clients" en:"Connected clients")" 2 "openvpn_clients"
modreg file openvpn 'box_crt' 'Box Cert' 0 "box_crt"
modreg file openvpn 'box_key' 'Private Key' 0 "box_key"
modreg file openvpn 'ca_crt' 'CA Cert' 0 "ca_crt"
modreg file openvpn 'crl_pem' 'CRL' 0 "crl_pem"
modreg file openvpn 'dh_pem' 'DH Param' 0 "dh_pem"
modreg file openvpn 'static_key' 'Static Key' 0 "static_key"
modlib_add_user_and_group openvpn
mkdir -p /tmp/openvpn/etc
mkdir -p /tmp/openvpn/dev
mkdir -p /tmp/openvpn/tmp
mkdir -p /tmp/flash/openvpn
[ "$OPENVPN_CONFIG_COUNT" -eq 1 -a "$OPENVPN_ENABLED" = inetd ] && /mod/etc/default.openvpn/openvpn_conf > $DAEMON_CONFIG
fi
if [ ! -e "/tmp/flash/openvpn/static.key" ]; then
echo -n "creating static.key ... "
openvpn --genkey --secret /tmp/flash/openvpn/static.key
/usr/bin/modsave flash >/dev/null
fi
d_BU="$DAEMON"
as="${OPENVPN_AUTOSTART#*#}"
for d in $(echo "openvpn${OPENVPN_CONFIG_NAMES#*DEFAULT#}" | sed 's/#/ openvpn_/g' ); do
TMP_ENABLE=${as%%#*}
[ -z "$TMP_ENABLE" ] && TMP_ENABLE="no"
DAEMON="$d"
DAEMON_LONG_NAME=$DAEMON
DAEMON_BIN=$DAEMON
DAEMON_CONFIG=/mod/etc/${DAEMON}.conf
PID_FILE=/var/run/${DAEMON_BIN}.pid
modlib_start $TMP_ENABLE
as=${as#*#}
done
DAEMON="$d_BU"
DAEMON_LONG_NAME=$DAEMON
;;
unload)
stop
if [ "$DAEMON" = "openvpn" ]; then
for d in $(echo "${OPENVPN_CONFIG_NAMES#*DEFAULT#}" | sed 's/#/ openvpn_/g' ); do
DAEMON="$d"
DAEMON_LONG_NAME=$DAEMON
stop
done
modunreg file openvpn
modunreg daemon openvpn
modunreg cgi openvpn
modunreg extra openvpn
fi
;;
start)
modlib_start
;;
stop)
stop
;;
restart)
stop
sleep 1
start
;;
reload)
if [ -r "/var/run/${DAEMON}.pid" ]; then
kill -HUP $(cat /var/run/${DAEMON}.pid)
fi
;;
status)
if [ "$OPENVPN_ENABLED" = inetd ]; then
echo "inetd"
elif [ -z "$(mypidof $DAEMON)" ]; then
echo 'stopped'
else
echo 'running'
fi
;;
*)
echo "Usage: $0 [load|unload|start|stop|restart|reload|status]" 1>&2
exit 1
;;
esac
exit 0