aboutsummaryrefslogtreecommitdiff
path: root/init.d.misc/openvpn.in
blob: 4f60b83aa956146f74361d6fe867b4ba34749c7a (plain)
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
#!@PREFIX@/sbin/runscript
# Copyright 2007-2008 Roy Marples <roy@marples.name>
# All rights reserved. Released under the 2-clause BSD license.

vpn=${RC_SVCNAME#*.}
name="OpenVPN"
[ "${vpn}" != "openvpn" ] && name="${name} (${vpn})"
command=@PKG_PREFIX@/sbin/openvpn

pidfile=@VARBASE@/run/"${RC_SVCNAME}".pid
openvpn_dir=${openvpn_dir:-@PKG_PREFIX@/etc/openvpn}
openvpn_config=${openvpn_config:-${openvpn_dir}/${vpn}.conf}
command_args="${openvpn_args} --daemon --config ${openvpn_config} --writepid ${pidfile}"
required_dirs="${openvpn_dir}"
required_files="${openvpn_config}"

# If we're an openvpn client, then supply a nice default config
# You can find sample up/down scripts in the OpenRC support/openvpn dir
if yesno "${openvpn_client}"; then
	openvpn_up=${openvpn_up:-${openvpn_dir}/up.sh}
	openvpn_down=${openvpn_down:-${openvpn_dir}/down.sh}
	command_args="${command_args} --nobind --up-delay --up-restart --down-pre"
	command_args="${command_args} --up ${openvpn_up}"
	command_args="${command_args} --down ${openvpn_down}"
	required_files="${required_files} ${openvpn_up} ${openvpn_down}"
	
	in_background_fake="start stop"
	start_inactive="YES"
fi

depend()
{
	need localmount net
	use dns
	after bootmisc
}

start_pre()
{
	# Linux has good dynamic tun/tap creation
	if [ "${RC_UNAME}" = "Linux" ]; then
		if [ ! -e /dev/net/tun ]; then
			if ! modprobe tun; then
				eerror "TUN/TAP support is not available in this kernel"
				return 1
			fi
		fi
		if [ -h /dev/net/tun -a -c /dev/misc/net/tun ]; then
			ebegin "Detected broken /dev/net/tun symlink, fixing..."
			rm -f /dev/net/tun
			ln -s /dev/misc/net/tun /dev/net/tun
			eend $?
		fi
	else
		if type kldload >/dev/null 2>&1; then
			# Hammer the modules home by default
			sysctl -a | grep -q '\.tun\.' || kldload if_tun
			sysctl -a | grep -q '\.tap\.' || kldload if_tap
		fi
	fi
	
	# If the config file does not specify the cd option, we do
	# But if we specify it, we override the config option which we do not want
	if ! grep -q "^[ \t]*cd[ \t].*" "${openvpn_config}"; then
		command_args="${command_args} --cd ${openvpn_dir}"
	fi
}