Debian Codegraph @

support/openvpn/up.sh

raw
1 #!/bin/sh
2 # Copyright (c) 2007-2015 The OpenRC Authors.
3 # See the Authors file at the top-level directory of this distribution and
4 # https://github.com/OpenRC/openrc/blob/HEAD/AUTHORS
5 #
6 # This file is part of OpenRC. It is subject to the license terms in
7 # the LICENSE file found in the top-level directory of this
8 # distribution and at https://github.com/OpenRC/openrc/blob/HEAD/LICENSE
9 # This file may not be copied, modified, propagated, or distributed
10 # except according to the terms contained in the LICENSE file.
11
12 # Setup our resolv.conf
13 # Vitally important that we use the domain entry in resolv.conf so we
14 # can setup the nameservers are for the domain ONLY in resolvconf if
15 # we're using a decent dns cache/forwarder like dnsmasq and NOT nscd/libc.
16 # nscd/libc users will get the VPN nameservers before their other ones
17 # and will use the first one that responds - maybe the LAN ones?
18 # non resolvconf users just the VPN resolv.conf
19
20 # FIXME:- if we have >1 domain, then we have to use search :/
21 # We need to add a flag to resolvconf to say
22 # "these nameservers should only be used for the listed search domains
23 # if other global nameservers are present on other interfaces"
24 # This however, will break compatibility with Debian's resolvconf
25 # A possible workaround would be to just list multiple domain lines
26 # and try and let resolvconf handle it
27
28 NS=
29 DOMAIN=
30 SEARCH=
31 i=1
32 while true; do
33 eval opt=\$foreign_option_${i}
34 [ -z "${opt}" ] && break
35 if [ "${opt}" != "${opt#dhcp-option DOMAIN *}" ]; then
36 if [ -z "${DOMAIN}" ]; then
37 DOMAIN="${opt#dhcp-option DOMAIN *}"
38 else
39 SEARCH="${SEARCH:+ }${opt#dhcp-option DOMAIN *}"
40 fi
41 elif [ "${opt}" != "${opt#dhcp-option DNS *}" ]; then
42 NS="${NS}nameserver ${opt#dhcp-option DNS *}\n"
43 fi
44 : $(( i += 1 ))
45 done
46
47 if [ -n "${NS}" ]; then
48 DNS="# Generated by openvpn for interface ${dev}\n"
49 if [ -n "${SEARCH}" ]; then
50 DNS="${DNS}search ${DOMAIN} ${SEARCH}\n"
51 else
52 DNS="${DNS}domain ${DOMAIN}\n"
53 fi
54 DNS="${DNS}${NS}"
55 if command -v resolvconf >/dev/null 2>&1; then
56 printf "${DNS}" | resolvconf -a "${dev}"
57 else
58 # Preserve the existing resolv.conf
59 if [ -e /etc/resolv.conf ]; then
60 cp -p /etc/resolv.conf /etc/resolv.conf-"${dev}".sv
61 fi
62 (umask 022; printf "${DNS}" > /etc/resolv.conf)
63 fi
64 fi
65
66 # Below section is OpenRC specific
67
68 # If we have a service specific script, run this now
69 [ -x "${RC_SVCNAME}"-up.sh ] && "${RC_SVCNAME}"-up.sh
70
71 # Re-enter the init script to start any dependent services
72 if [ -x "${RC_SERVICE}" ]; then
73 if ! "${RC_SERVICE}" --quiet status; then
74 IN_BACKGROUND=YES
75 export IN_BACKGROUND
76 "${RC_SERVICE}" --quiet start
77 fi
78 fi
79
80 exit 0