From: "d12fk (Code Review)" Attention is currently required from: cron2, flichtenheld, plaisthos. <=
/p>
d12fk uploaded patch set #22 to this change. The following approvals got outdated and were removed:
Code-Review+2 by flichtenheld, Code-Review-1 by cron2 The change is n=
o longer submittable: Code-Review and checks~ChecksSubmitRule are unsatisfi=
ed now.dns: apply settings via script on unixoid systems
This intro=
duces a new script hook, the dns-updown, and implements such a
command s=
cript for a few popular systems (and a default for the not so
popular on=
es). Like the name suggests this hook is soleley for dealing
with modify=
ing how names are resolved when the VPN pushes some --dns
settings.
<=
br>The default dns updown command is part of the distribution and is
ins=
talled with openvpn. You can change the path the command is located
at a=
s a compile time option, defaults to libexecdir.
You can compile-tim=
e disable that the default dns-updown hook is
run by passing --disable-d=
ns-updown-by-default to configure or
ccmake ENABLE_DNS_UPDOWN_BY_DEFAULT=
to OFF.
There's also a new runtime option --dns-updown, which c=
an run a custom
command, force running the default when disabled or disa=
ble execution
of the dns-updown altogether.
Change-Id: Ifbe4ffb44=
d3bfcaa50adb38cacb3436fcdc71b10
Signed-off-by: Heiko Hund <heiko@...1665...=
662...>
---
M .gitignore
M CMakeLists.txt
M config.h.cmake.i=
n
M configure.ac
M distro/Makefile.am
A distro/dns-scripts/Makefil=
e.am
A distro/dns-scripts/haikuos_file-dns-updown.sh
A distro/dns-scr=
ipts/openresolv-dns-updown.sh
A distro/dns-scripts/resolvconf_file-dns-u=
pdown.sh
A distro/dns-scripts/systemd-dns-updown.sh
M doc/man-section=
s/script-options.rst
M src/openvpn/Makefile.am
M src/openvpn/dns.c
M src/openvpn/dns.h
M src/openvpn/options.c
15 files changed, 701 in=
sertions(+), 10 deletions(-)git pull ssh://gerrit.openvpn.net:29=
418/openvpn refs/changes/38/838/22
diff --git a/.gitignore b/.gitig=
nore
<=
p>To view, visit change 838. To unsubscribe, or for help writing mail filters,=
visit settings.
index db8bb73..04523af 100644
--- a/.g=
itignore
+++ b/.gitignore
@@ -49,6 +49,7 @@=
/doc/doxygen/latex/
/doc/doxygen/openvpn=
.doxyfile
distro/systemd/*.service
+distro/dns-scripts/dns-updown
<=
span> sample/sample-keys/sample-ca/
vendor/cmocka_build
vendor/dist
diff --git a/CMakeLists.txt b/CM=
akeLists.txt
index a8fb64b..c057d86 100644
=
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@=
@ -41,7 +41,10 @@
option(USE_WERROR "Treat compiler w=
arnings as errors (-Werror)" ON)
option(FAKE_ANDROID =
"Target Android but do not use actual cross compile/Android cmake to b=
uild for simple compile checks on Linux")
=
-set(PLUGIN_DIR /usr/local/lib/op=
envpn/plugins CACHE FILEPATH "Location of the plugin directory")<=
/span>
+option(ENABLE_DNS_UP=
DOWN_BY_DEFAULT "Run --dns-updown hook by default" ON)
=
+set(DNS_UPDOWN_PATH "${CM=
AKE_INSTALL_PREFIX}/libexec/openvpn/dns-updown" CACHE STRING "Def=
ault location for the DNS up/down script")
+
+set(PLUGIN_DIR "${CMAKE_INSTALL_PREFIX}/lib/openvpn/plugins&q=
uot; CACHE FILEPATH "Location of the plugin directory")
# Create machine readable compile commands=
option(ENABLE_COMPILE_COMMANDS "Generate compile_commands.j=
son and a symlink for clangd to find it" OFF)
@@ -578,=
6 +581,8 @@
add_library_deps(openvpn)
+targ=
et_compile_options(openvpn PRIVATE -DDEFAULT_DNS_UPDOWN=3D\"${DNS_UPDO=
WN_PATH}\")
+
if(MINGW)
target_compile_options(openvp=
n PRIVATE -municode -UUNICODE)
target_link_options(ope=
nvpn PRIVATE -municode)
diff --git a/config.h.cmake.in b/co=
nfig.h.cmake.in
index 2f7b43d..5164ce3 100644
+++ b/config.h.cmake.in@@ -35,6 +35,9 @@
/* Enable LZO compression librar=
y */
#cmakedefine ENABLE_LZO
<=
span style=3D"color: hsl(120, 100%, 40%);">+/* Enable dns-updown script hoo=
k */
+#cmakedefine EN=
ABLE_DNS_UPDOWN
+
/* Enable NTLMv2 proxy support */
#define EN=
ABLE_NTLM 1
diff --git a/configure.ac b/c=
onfigure.ac
index 9777e36..75367e8 100644
-=
-- a/configure.ac
+++ b/configure.ac
@@ -96=
,6 +96,13 @@
)
AC_ARG_E=
NABLE(
+ [dns-updown-=
by-default],
+ [AS_HE=
LP_STRING([--disable-dns-updown-by-default], [disable running --dns-updown =
by default @<:@default=3Dyes@:>@])],
+ ,
+ [enable_dns_updown_by_default=3D"yes"]
+)
+
+A=
C_ARG_ENABLE(
[ntlm],
[AS_HELP_STRING([=
--disable-ntlm], [disable NTLMv2 proxy support @<:@default=3Dyes@:>@]=
)],
,
@@ -315,37 +322,50 @@
fi=
+A=
C_ARG_VAR([SCRIPTDIR], [Path of script directory @<:@default=3DPKGLIBEXE=
CDIR@:>@])
+if tes=
t -n "${SCRIPTDIR}"; then
+ scriptdir=3D"${SCRIPTDIR}"
+else
+ scriptdir=3D"\${pkglibexecdir}"
+
AC_DEFINE_UNQUOTED([TARGET_ALIAS=
], ["${host}"], [A string representing our host])
-AM_CONDITIONAL([TARGET_LINUX], [false=
])
+AM_CONDITIONAL([E=
NABLE_DNS_UPDOWN],[true])
case "$host" in=
*-*-linux*)
AC_DEFINE([TARGET_LINUX], [1], [=
Are we running on Linux?])
- AM_CONDITIONAL([TARGET_LINUX], [true])
AC_DEFINE_=
UNQUOTED([TARGET_PREFIX], ["L"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], [&quo=
t;systemd"])
have_sitnl=3D"yes"
pkg_config_required=3D"yes"
;;
*-*-solaris*)
AC_DEFINE([TARGET_SOLARIS], [=
1], [Are we running on Solaris?])
AC_DEFINE_UNQUOTED([TA=
RGET_PREFIX], ["S"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["resolvconf=
_file"])
CPPFLAGS=3D"$CPPFLAGS -D_XPG4_2"=
test -x /bin/bash && SHELL=3D"/bin/bash&qu=
ot;
;;
*-*-openbsd*)
=
AC_DEFINE([TARGET_OPENBSD], [1], [Are we running on OpenBSD?])
=
AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["O"], [Target prefi=
x])
+ AC_SUBST([DNS_=
UPDOWN_TYPE], ["resolvconf_file"])
;; *-*-freebsd*)
AC_DEFINE([TARGET_FREEBSD], [1],=
[Are we running on FreeBSD?])
AC_DEFINE_UNQUOTED([TARGE=
T_PREFIX], ["F"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["openresolv&qu=
ot;])
;;
*-*-netbsd*)
=
AC_DEFINE([TARGET_NETBSD], [1], [Are we running NetBSD?])
+ AC_SUBST([DNS_UPDO=
WN_TYPE], ["openresolv"])
;;
=
*-*-darwin*)
AC_DEFINE([TARGET_DARWIN], [1], [Are we ru=
nning on Mac OS X?])
AC_DEFINE_UNQUOTED([TARGET_PREFIX],=
["M"], [Target prefix])
+ AM_CONDITIONAL([ENABLE_DNS_UPDOWN], [false])
have_tap_header=3D"yes&=
quot;
ac_cv_type_struct_in_pktinfo=3Dno
=
;;
@@ -353,6 +373,8 @@
AC_DEFINE([TAR=
GET_WIN32], [1], [Are we running WIN32?])
AC_DEFINE([ENA=
BLE_DCO], [1], [DCO is always enabled on Windows])
AC_DE=
FINE_UNQUOTED([TARGET_PREFIX], ["W"], [Target prefix])
=
+ AM_CONDITIONAL([ENABLE_DNS_U=
PDOWN], [false])
+ A=
C_SUBST([DNS_UPDOWN_TYPE], ["windows"])
CPPFLA=
GS=3D"${CPPFLAGS} -DWIN32_LEAN_AND_MEAN"
CPPFL=
AGS=3D"${CPPFLAGS} -DNTDDI_VERSION=3DNTDDI_VISTA -D_WIN32_WINNT=3D_WIN=
32_WINNT_VISTA"
WIN32=3Dyes
@@ -360=
,10 +382,12 @@
*-*-dragonfly*)
AC_DEFI=
NE([TARGET_DRAGONFLY], [1], [Are we running on DragonFlyBSD?]) AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["D"], [Target prefix]=
)
+ AC_SUBST([DNS_UP=
DOWN_TYPE], ["openresolv"])
;;
*-aix*)
AC_DEFINE([TARGET_AIX], [1], [Are we running =
AIX?])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["A"=
;], [Target prefix])
=
+ AC_SUBST([DNS_UPDOWN_TYPE], ["resolvconf_file"])
have_tap_heade=
r=3D"yes"
ac_cv_header_net_if_h=3D"no&quo=
t; # exists, but breaks things
@@ -371,10 +395,12 @@=
*-*-haiku*)
AC_DEFINE([TARGET_HAIKU], [1], [=
Are we running Haiku?])
AC_DEFINE_UNQUOTED([TARGET_PREFI=
X], ["H"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["haikuos_file"])=
LIBS=3D"${LIBS} -lnetwork"
=
;;
*)
AC_DEFINE_UNQUOTED([TARGET_PREF=
IX], ["X"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["resolvconf_file&quo=
t;])
have_tap_header=3D"yes"
=
;;
esac
@@ -1317,7 +1343,7 @@
=
test "${enable_small}" =3D "yes" && AC_D=
EFINE([ENABLE_SMALL], [1], [Enable smaller executable size])
test "${enable_port_share}" =3D "yes" &=
& AC_DEFINE([ENABLE_PORT_SHARE], [1], [Enable TCP Server port sharing])=
-
+test "${enable_dns_updown_by_defaul=
t}" =3D "yes" && AC_DEFINE([ENABLE_DNS_UPDOWN_BY_DEF=
AULT], [1], [Enable dns-updown hook by default])
test &quo=
t;${enable_ntlm}" =3D "yes" && AC_DEFINE([ENABLE_NTL=
M], [1], [Enable NTLMv2 proxy support])
test "${enabl=
e_crypto_ofb_cfb}" =3D "yes" && AC_DEFINE([ENABLE_OF=
B_CFB_MODE], [1], [Enable OFB and CFB cipher modes])
if te=
st "${have_export_keying_material}" =3D "yes"; then
@@ -1505,6 +1531,7 @@
sampl=
edir=3D"\$(docdir)/sample"
AC_SUBST([plugindir])=
+AC_SUBST([scriptdir=
])
AC_SUBST([sampledir])
AC_SUBST([systemdunitdir])
@@ -1541,6 +1568,7 @@ Makefile
distro/Makefile
dist=
ro/systemd/Makefile
+=
distro/dns-scripts/Makefile
doc/Makefile
doc/doxygen/Makefile
doc/doxygen/openvpn.doxyfile
diff --git a/distro/Makefile.am b/distro/Makefile.am
=
index 7a588da..26f577b 100644
--- a/distro/Makefile.a=
m
+++ b/distro/Makefile.am
@@ -13,3 +13,7 @=
@
$(srcdir)/Makefile.in
SUBDIRS =3D systemd
+
+if ENABLE_DNS_UPD=
OWN
+SUBDIRS +=3D dns=
-scripts
+endif
diff --git a/distro/dns-scripts/Makefile.am b/distro/dns-scripts=
/Makefile.am
new file mode 100644
index 000=
0000..fc2db08
--- /dev/null
+++ b/distro/dn=
s-scripts/Makefile.am
@@ -0,0 +1,29 @@
+#
+# OpenVPN -- An application to securely tunnel IP network=
s
+# over=
a single UDP port, with support for SSL/TLS-based
+# session authentication and key=
exchange,
+# =
packet encryption, packet authentication, and
+# packet compression.
=
+#
+# Copyright (C) 2002-2024 OpenVPN Inc <sales=
@...515...>
+#
+
+MAINTAINERCLEANFILES =3D \
<=
span style=3D"color: hsl(120, 100%, 40%);">+
+EXTRA_DIST =3D \
+ systemd-dns-updown.sh \
+ openresolv-dns-updown.sh \
+ haikuos_file-dns-updown.sh \
=
+ resolvconf_file-dns-updown.sh=
+
+script_SCRIPTS =3D \
+ dns-updown
+
+CLEANFILES =3D $(script_SCRIPTS)
+
+dns-updown: @DNS_UPDOWN_TYPE@...2841...
+ cp ${srcdir}/@DNS_UPDOWN_TYPE@...2841... $@
+ chmod +x $@+
+all: $(script_SCRIPTS)
diff --git a=
/distro/dns-scripts/haikuos_file-dns-updown.sh b/distro/dns-scripts/haikuos=
_file-dns-updown.sh
new file mode 100644
in=
dex 0000000..1b03e9c
--- /dev/null
+++ b/di=
stro/dns-scripts/haikuos_file-dns-updown.sh
@@ -0,0 +1,84 @=
@
+#!/bin/sh+#
+# Simple OpenVPN up/down script for modifying =
Haiku OS resolv.conf
=
+# (C) Copyright 2024 OpenVPN Inc <sales@...515...>
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Example env from openvpn (most are not applied):
+#
+# dev tun0
+# script-type dns-up
+# dns_search_domain_1 mycorp.in
=
+# dns_search_domain_2 eu.myc=
orp.com
+# dns_serv=
er_1_address_1 192.168.99.254
+# dns_server_1_address_2 fd00::99:53
+# dns_server_1_port_1 53
+# dns_server_1_port_2 53+# dns_server_1_resolve_domain_=
1 mycorp.in
+# dns_=
server_1_resolve_domain_2 eu.mycorp.com
+# dns_server_1_dnssec true
+# dns_server_1_transport DoH
+# dns_server_1_sni dns.mycorp.in<=
/span>
+#
+
+set -e +u
+
+conly_stand=
ard_server_ports() {
=
+ i=3D1
+ while=
true; do
+ ev=
al addr=3D\"\$dns_server_${n}_address_${i}\"
+ [ -n "$addr" ] || retu=
rn 0
+
+ [ -z "$port" -o "$port" =3D "53&qu=
ot; ] || return 1
+=
span>
+ i=3D$(expr $i=
+ 1)
+ done
+}
+
+onf=3D/boot/system/settings/network/resolv.conf+test -e "$conf" || exi=
t 1
+case "${scr=
ipt_type}" in
+d=
ns-up)
+ n=3D1
+ while :; do<=
br>+ eval addr=3D\"=
\$dns_server_${n}_address_1\"
+ [ -n "$addr" ] || {
+ echo "setting DNS failed=
, no compatible server profile"
+ exit 1
+ }
+
+ # Ski=
p server profiles which require DNSSEC,
+ # secure transport or use a custom port<=
br>+ eval dnssec=3D\&quo=
t;\$dns_server_${n}_dnssec\"
+ eval transport=3D\"\$dns_server_${n}_transport\&=
quot;
+ [ -z &=
quot;$transport" -o "$transport" =3D "plain" ] \=
span>
+ &&=
; [ -z "$dnssec" -o "$dnssec" =3D "no" ] \
+ &&=
only_standard_server_ports && break
+
+ n=3D$(expr $n + 1)
+ done
+
+ eval addr1=3D=
\"\$dns_server_${n}_address_1\"
+ eval addr2=3D\"\$dns_server_${n}_address_2\&=
quot;
+ eval addr3=
=3D\"\$dns_server_${n}_address_3\"
+ text=3D"### openvpn ${dev} begin ###\n&qu=
ot;
+ text=3D"=
;${text}nameserver $addr1\n"
+ test -z "$addr2" || text=3D"${text}nameser=
ver $addr2\n"
+ =
test -z "$addr3" || text=3D"${text}nameserver $addr3\n&qu=
ot;
+
+ test -z "$dns_search_domai=
n_1" || {
+ =
for i in $(seq 1 6); do
+ eval domains=3D\"$domains\$dns_search_domain_${i} \=
" || break
+ =
done
+ tex=
t=3D"${text}search $domains\n"
+ }
+ text=3D"${text}### openvpn ${dev} end ###"
<=
span style=3D"color: hsl(120, 100%, 40%);">+
+ sed -i "1i${text}" "$conf"=
+ ;;+dns-down)
+ sed -i "/### openvpn ${dev} begi=
n ###/,/### openvpn ${dev} end ###/d" "$conf"
+esac
diff --git a/distro/dns-sc=
ripts/openresolv-dns-updown.sh b/distro/dns-scripts/openresolv-dns-updown.s=
h
new file mode 100644
index 0000000..1b218=
f5
--- /dev/null
+++ b/distro/dns-scripts/o=
penresolv-dns-updown.sh
@@ -0,0 +1,89 @@
+#!/bin/sh
+#
+# Simple OpenVPN up/down script for openresolv integration<=
br>+# (C) Copyright 2016 Baptis=
te Daroussin
+# =
2024 OpenVPN Inc <sales@...515...>
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Example env from openvpn (most are not applied):+#
+# dev tun0
+# script-type dns-up
+# dns_search_domain_1 mycorp.in
+# dns_search_domain_2 eu.mycorp.co=
m
+# dns_server_1_a=
ddress_1 192.168.99.254
+# dns_server_1_address_2 fd00::99:53
+# dns_server_1_port_1 53
+# dns_server_1_port_2 53
+# dns_server_1_resolve_domain_1 myco=
rp.in
+# dns_server=
_1_resolve_domain_2 eu.mycorp.com
+# dns_server_1_dnssec true
+# dns_server_1_transport DoH
+# dns_server_1_sni dns.mycorp.in=
+#
+
+set -e +u
+
+only_standard_ser=
ver_ports() {
+ i=
=3D1
+ while true;=
do
+ eval add=
r=3D\"\$dns_server_${n}_address_${i}\"
+ [ -n "$addr" ] || return 0=
span>
+
+ eval port=3D\"\$dns_server_=
${n}_port_${i}\"
+ [ -z "$port" -o "$port" =3D "53" ] =
|| return 1
+<=
br>+ i=3D$(expr $i + 1)<=
/span>
+ done
<=
span style=3D"color: hsl(120, 100%, 40%);">+}
+
+: ${script_type:=3Ddns-down}
+case "${script_type}" in
+dns-up)
+ n=3D1
+ while :; do
+ eval addr=3D\"\$dns_server_${n}_address_1\"+ [ -n "$addr&quo=
t; ] || {
+ =
echo "setting DNS failed, no compatible server profile"<=
br>+ exit 1+ }
+
+ # Skip server profiles which require DNSSEC,
+ # secure transpo=
rt or use a custom port
+ eval dnssec=3D\"\$dns_server_${n}_dnssec\"
=
+ eval transport=3D\&quo=
t;\$dns_server_${n}_transport\"
+ [ -z "$transport" -o "$transport&qu=
ot; =3D "plain" ] \
+ && [ -z "$dnssec" -o "$dnssec&=
quot; =3D "no" ] \
+ && only_standard_server_ports && break=
+
+ n=3D$(expr $n + 1)
<=
span style=3D"color: hsl(120, 100%, 40%);">+ done
+
+ {
=
+ i=3D1
+ =
maxns=3D3
+ =
while :; do
+ =
maxns=3D$((maxns - 1))
+ [ $maxns -gt 0 ] || break
+ eval option=3D\"\$dns_server_${n=
}_address_${i}\" || break
+ [ "${option}" ] || break
+ i=3D$((i + 1))
=
+ echo "nameser=
ver ${option}"
+=
done
+ =
i=3D1
+ maxdo=
m=3D6
+ while =
:; do
+ ma=
xdom=3D$((maxdom - 1))
+ [ $maxdom -gt 0 ] || break
+ eval option=3D\"\$dns_search_domain_=
${i}\" || break
=
+ [ "${option}" ] || break
+ i=3D$((i + 1))
+ echo "search ${option}&=
quot;
+ done=
span>
+ } | /sbin/resolvc=
onf -a "${dev}"
+ ;;
+dns-dow=
n)
+ /sbin/resolvc=
onf -d "${dev}" -f
+ ;;
+esac=
diff --git a/distro/dns-scripts/resolvconf_file-dns-updown=
.sh b/distro/dns-scripts/resolvconf_file-dns-updown.sh
new =
file mode 100644
index 0000000..c469490
---=
/dev/null
+++ b/distro/dns-scripts/resolvconf_file-dns-upd=
own.sh
@@ -0,0 +1,84 @@
+#!/bin/sh
+#
+# Simple=
OpenVPN up/down script for modifying /etc/resolv.conf
+# (C) Copyright 2024 OpenVPN Inc <sal=
es@...515...>
+#=
span>
+# SPDX-License-Identi=
fier: BSD-2-Clause
+#=
+# Example env from =
openvpn (most are not applied):
+#
+# de=
v tun0
+# script-ty=
pe dns-up
+# dns_se=
arch_domain_1 mycorp.in
+# dns_search_domain_2 eu.mycorp.com
+# dns_server_1_address_1 192.168.99.254
<=
span style=3D"color: hsl(120, 100%, 40%);">+# dns_server_1_address_2 fd00=
::99:53
+# dns_serv=
er_1_port_1 53
+# d=
ns_server_1_port_2 53
+# dns_server_1_resolve_domain_1 mycorp.in
+# dns_server_1_resolve_domain_2 eu.mycorp.com=
span>
+# dns_server_1_dnss=
ec true
+# dns_serv=
er_1_transport DoH
+#=
dns_server_1_sni dns.mycorp.in
+#
+
+set -e +u
+only_standard_server_ports() {
+ i=3D1
+ while true; do
+ eval addr=3D\"\$dns_server_${n}_address=
_${i}\"
+ =
[ -n "$addr" ] || return 0
+
+ =
eval port=3D\"\$dns_server_${n}_port_${i}\"
+
+ i=3D$(expr $i + 1)
+ done
+}
+
+conf=3D/etc/resolv.conf
+test -e "$conf"=
|| exit 1
+case &quo=
t;${script_type}" in
+dns-up)
+ n=
=3D1
+ while :; do=
+ eval addr=
=3D\"\$dns_server_${n}_address_1\"
+ [ -n "$addr" ] || {+ echo "setting D=
NS failed, no compatible server profile"
+ exit 1
+ }
+
+ =
# Skip server profiles which require DNSSEC,
+ # secure transport or use a custom por=
t
+ eval dnsse=
c=3D\"\$dns_server_${n}_dnssec\"
+ eval transport=3D\"\$dns_server_${n}_tr=
ansport\"
+ =
[ -z "$transport" -o "$transport" =3D "plain&qu=
ot; ] \
+ =
&& [ -z "$dnssec" -o "$dnssec" =3D "no&quo=
t; ] \
+ &=
amp;& only_standard_server_ports && break
+
+ n=3D$(expr $n + 1)
+ done
+
+ eval=
addr1=3D\"\$dns_server_${n}_address_1\"
+ eval addr2=3D\"\$dns_server_${n}_ad=
dress_2\"
+ e=
val addr3=3D\"\$dns_server_${n}_address_3\"
+ text=3D"### openvpn ${dev} begin=
###\n"
+ tex=
t=3D"${text}nameserver $addr1\n"
+ test -z "$addr2" || text=3D"${tex=
t}nameserver $addr2\n"
+ test -z "$addr3" || text=3D"${text}nameserver $a=
ddr3\n"
+=
+ test -z "$dns_sea=
rch_domain_1" || {
+ for i in $(seq 1 6); do
+ eval domains=3D\"$domains\$dns_search_doma=
in_${i} \" || break
+ done
+ =
text=3D"${text}search $domains\n"
+ }
+ text=3D"${text}### openvpn ${dev} end ###"
+
+ sed -i "1i${text}" "$c=
onf"
+ ;;
+dns-down)
+ ;;
+esac
diff --git a/distr=
o/dns-scripts/systemd-dns-updown.sh b/distro/dns-scripts/systemd-dns-updown=
.sh
new file mode 100644
index 0000000..43c=
31b9
--- /dev/null
+++ b/distro/dns-scripts=
/systemd-dns-updown.sh
@@ -0,0 +1,236 @@
+#!/bin/bash
+#
+# dns-updown - add/remove openvpn provided DNS information
+#
+# Copyright (C) 2024 OpenVPN Inc <sale=
s@...515...>
+#
+# SPDX-License-Identif=
ier: GPL-2.0
+#
+# Add/remove openvpn DNS =
settings from the env into/from
+# the system. Supported backends in this order:
+#
+# * systemd-resolved
+# * resolvconf
+#
+# Example env from openvpn (not all are always applied):
+# dev tun0
+# script-type dns-up
+# dns_search_domain_1 mycorp.in
+# dns_search_domain_2 eu.mycorp.com
+# dns_server_1_addres=
s_1 192.168.99.254
+#=
dns_server_1_address_2 fd00::99:53
+# dns_server_1_port_1 53
+# dns_server_1_port_2 53
+# dns_server_1_resolve_domain_1 mycorp.=
in
+# dns_server_1_=
resolve_domain_2 eu.mycorp.com
+# dns_server_1_dnssec true
+# dns_server_1_transport DoH
+# dns_server_1_sni dns.mycorp.in
+#
+
+function do_resolved_servers {
+ local sni=3D""
+ local transport_var=3Ddns_server_${n}_tran=
sport
+ local sni_=
var=3Ddns_server_${n}_sni
+ [ "${!transport_var}" =3D "DoT" ] && =
sni=3D"#${!sni_var}"
+
+ loca=
l i=3D1
+ local ad=
drs=3D""
+ =
while :; do
+ =
local addr_var=3Ddns_server_${n}_address_${i}
+ local addr=3D"${!addr_var}"<=
/span>
+ [ -n "$=
addr" ] || break
+
+ local por=
t_var=3Ddns_server_${n}_port_${i}
+ if [ -n "${!port_var}" ]; then+ if [[ "$addr&qu=
ot; =3D~ : ]]; then
+=
addr=3D"[$addr]"
+ fi
+ addrs+=3D"${addr}:${!port_var}${sni} &qu=
ot;
+ else
+ addrs+=3D&q=
uot;${addr}${sni} "
+ fi
+ =
i=3D$((i+1))
+ =
done
+
+}+
+function do_resolved_domains {
+ local list=3D""=
+ for domain_var in ${!d=
ns_search_domain_*}; do
+ list+=3D"${!domain_var} "
+ done
+ local domain_var=3Ddns_server_${n}_resolve_domain_1
+ if [ -z "${!d=
omain_var}" ]; then
+ resolvectl default-route "$dev" true
+ list+=3D"~."
+ else
+ resolvectl default-route &q=
uot;$dev" false
=
+ local i=3D1
=
+ while :; do
=
+ domain_var=3Ddns_server_${n}_resolve_domain_${i}
+ # Add as split domain (~ prefix), if it doesn't already e=
xist
+ [[ =
"$list" =3D~ (^| )"${!domain_var}"( |$) ]] \
=
+ || list+=3D&qu=
ot;~${!domain_var} "
+ i=3D$((i+1))
+ done
+ fi
++ resolvectl domain "=
$dev" $list
+}=
span>
+
+function do_resolved_dnssec {
=
+ local dnssec_var=3Ddns_ser=
ver_${n}_dnssec
+ =
if [ "${!dnssec_var}" =3D "optional" ]; then
=
+ resolvectl dnssec &quo=
t;$dev" allow-downgrade
+ elif [ "${!dnssec_var}" =3D "yes" ]; then<=
/span>
+ resolvectl d=
nssec "$dev" true
+ else
+ =
resolvectl dnssec "$dev" false
+ fi
+}
+
+function do_resolved_dns=
overtls {
+ local =
transport_var=3Ddns_server_${n}_transport
+ if [ "${!transport_var}" =3D "DoT&=
quot; ]; then
+ =
resolvectl dnsovertls "$dev" true
+ else
+ resolvectl dnsovertls "$dev" false<=
br>+ fi
+}
+
+fu=
nction do_resolved {
=
+ [[ "$(readlink /etc/resolv.conf)" =3D~ systemd ]] || return =
1
+
+ n=3D1
+ while :; do
+ local addr_var=3Ddns_server_${n}_address_1<=
/span>
+ [ -n "$=
{!addr_var}" ] || {
+ echo "setting DNS failed, no compatible server profil=
e"
+ =
return 1
+ }=
span>
+
+ # Skip server profiles which req=
uire DNS-over-HTTPS
+=
local transport_var=3Ddns_server_${n}_transport
+ [ -n "${!transport_var}&qu=
ot; -a "${!transport_var}" =3D "DoH" ] || break<=
br>+
+ n=3D$((n+1))
+ done
+
+=
if [ "$script_type" =3D "dns-up" ]; then
=
+ echo "setting DNS=
using resolvectl"
+ do_resolved_servers
+ do_resolved_domains
+ do_resolved_dnssec
+ do_resolved_dnsovertls
+ else
+ echo "unsetting DNS using resolvect=
l"
+ reso=
lvectl revert "$dev"
+ fi
+=
span>
+ return 0+}
+
+function only_standard_server_ports {
+ local i=3D1
+ while :; do
+ local addr_var=3Ddns_server_${n}_address_${i}=
span>
+ [ -n "${=
!addr_var}" ] || return 0
+
+ =
local port_var=3Ddns_server_${n}_port_${i}
+ [ -z "${!port_var}" -o "${!po=
rt_var}" =3D "53" ] || return 1
+
+ i=3D$((i+1))
+ done
+}<=
/span>
+
+function resolv_conf_compat_profile {=
span>
+ n=3D1
<=
span style=3D"color: hsl(120, 100%, 40%);">+ while :; do
+ local server_addr_var=3Ddns=
_server_${n}_address_1
+ [ -n "${!server_addr_var}" ] || {
+ echo "setting DNS faile=
d, no compatible server profile"
+ exit 1
+ }
+
+ # Sk=
ip server profiles which require DNSSEC,
+ # secure transport or use a custom port=
+ local dnssec_var=
=3Ddns_server_${n}_dnssec
+ local transport_var=3Ddns_server_${n}_transport
+ && [ -=
z "${!dnssec_var}" -o "${!dnssec_var}" =3D "no&quo=
t; ] \
+ &=
amp;& only_standard_server_ports && break
+
+ n=3D$((n+1))
+ done
+}
++function do_resolvconf {<=
br>+ [ -x /sbin/resolvconf ]=
|| return 1
+=
+ n=3D$(resolv_conf_comp=
at_profile)
+<=
br>+ if [ "$script_type=
" =3D "dns-up" ]; then
+ echo "setting DNS using resolvconf"
+ local domains=
=3D""
+ =
for domain_var in ${!dns_search_domain_*}; do
+ domains+=3D"${!domain_var} &=
quot;
+ done=
span>
+ {
<=
span style=3D"color: hsl(120, 100%, 40%);">+ local maxns=3D3
+ local serve=
r_var=3Ddns_server_${n}_address_*
+ for addr_var in ${!server_var}; do
+ =
echo "nameserver ${!addr_var}"
+ done
+ [ -z "$domains" ] || echo=
"search $domains"
+ } | /sbin/resolvconf -a "$dev"
+ else
+ echo "unsetting DNS using resolvcon=
f"
+ /sbi=
n/resolvconf -d "$dev"
+ fi
+=
+ return 0=
+}
+
+function do_resolv_conf_file {
+ conf=3D/etc/resolv.conf
+ test -e "$conf" || exit 1
+
+ n=3D$(resolv_conf_compat_profile)
+
+ if [ "$script_type" =3D &qu=
ot;dns-up" ]; then
+ echo "setting DNS using resolv.conf file"
<=
span style=3D"color: hsl(120, 100%, 40%);">+
+ local addr1_var=3Ddns_server_${n}_address_=
1
+ local addr=
2_var=3Ddns_server_${n}_address_2
+ local addr3_var=3Ddns_server_${n}_address_3
+ text=3D"### open=
vpn ${dev} begin ###\n"
+ text=3D"${text}nameserver ${!addr1_var}\n"
+ test -z "${!=
addr2_var}" || text=3D"${text}nameserver ${!addr2_var}\n"
+ test -z "=
${!addr3_var}" || text=3D"${text}nameserver ${!addr3_var}\n"=
+
+ test -z "$dns_search_doma=
in_1" || {
+ =
for i in $(seq 1 6); do
+ eval domains=3D\"$domains\$dns_search_doma=
in_${i} \" || break
+ done
+ text=3D"${text}search $domains\n"
+ }
+ text=3D"${text}### openvpn ${dev} =
end ###"
+
+ sed -i "1i${=
text}" "$conf"
+ else
+ =
echo "unsetting DNS using resolv.conf file"
+ fi
+
+ return 0
+}
+
+do_resolved || do_resolv=
conf || do_resolv_conf_file || {
+ echo "setting DNS failed, no method succeeded"=
span>
+ exit 1
=
+}
diff --git a=
/doc/man-sections/script-options.rst b/doc/man-sections/script-options.rst<=
/span>
index 0d1f9ae..de3842b 100644
--- a/doc/man=
-sections/script-options.rst
+++ b/doc/man-sections/script-=
options.rst
@@ -8,9 +8,13 @@
Script Order =
of Execution
-------------------------
=
span>
+#. ``--dns-updown``=
span>
+
+ Executed after TCP/UDP socket bind an=
d TUN/TAP open, before ``--up``.
+
#. ``--up``
+=
Executed after TCP/UDP socket bind and TUN/TAP open, after ``--dns-updow=
n``.
#. ``--tls-verify``
@@ -38,9 +42,13 @@
=
Executed in ``--mode server`` mode on client instance shutdown.
=
+#. ``--dns-u=
pdown``
+
<=
span style=3D"color: hsl(120, 100%, 40%);">+ Executed before TCP/UDP and =
TUN/TAP close, before ``--down``.
+
#. ``--down``
<=
span style=3D"color: hsl(0, 100%, 40%);">- Executed after TCP/UDP and TUN=
/TAP close.
+ Execu=
ted after TCP/UDP and TUN/TAP close, after ``--dns-updown``.
#. ``--learn-address``
client-crresponse cmd
OpenVPN will write the response of the clie=
nt into a temporary file.
- The filename will be passed as an argument to ``cmd``, and the file w=
ill be
+ The filenam=
e will be passed as an argument to ``cmd``, and the file will
=
The response is passed as is from the client. Th=
e script needs to check
@@ -235,6 +243,31 @@
(only those arguments specified in cmd, if any).<=
br>
+--dns-upd=
own cmd
+ Run comman=
d ``cmd``, instead of the default DNS up/down command that comes
=
+ with openvpn. If ``cmd`` is =
``disable`` the ``--dns-updown`` command is not run.
+
+ If you write your own command, please make sure to ignore =
``--dns``
+ server p=
rofiles that cannot be applied. Port, DNSSEC and secure transport
+ settings need to be adhered=
to. If split DNS is not possible a full redirect
+ can be used as a fallback. If not all of th=
e server addresses or search domains
+ can be configured, apply them in the order they are list=
ed in.
++ Note that ``--dns-updown`` is =
not supported on all platforms. On Windows DNS
+ will always be set by the service. On Android =
DNS will be passed via management
+ interface.
+
+ Note that =
DNS-related ``--dhcp-option``\ s might be converted so that they are=
+ available to this hook i=
f no ``--dns`` options exist. If any ``--dns server``
+ option is present, DNS-related ``--dhcp=
-option``\ s will always be ignored.
+ If an ``--up`` script is defined, foreign_option env var=
s will be generated
+=
from ``--dns`` options and passed to the script. The default ``--dns-updo=
wn``
+ command is no=
t run if an ``--up`` script is defined. Both is done for backward
+ compatibility. In case you =
want to run the ``--dns-updown`` command even if
+ there is an ``--up`` defined, you can define=
a custom command or use ``force``
+ as ``cmd`` to run the default command. No DNS env vars wil=
l be passed to ``--up``
+ in this case.
+=
--down cmd
Run command ``cmd`` after T=
UN/TAP device close (post ``--user`` UID
change and/or `=
`--chroot`` ). ``cmd`` consists of a path to script (or
@@ =
-661,7 +694,7 @@
names). Set prior to ``--up`` or ``--=
down`` script execution.
:code:`dns_*`=
span>
- The ``--dns`` confi=
guration options will be made available to script
+ The ``--dns`` configuration options will =
be made available to ``--dns-updown``
execution throug=
h this set of environment variables. Variables appear
=
only if the corresponding option has a value assigned. For the semantics
of each individual variable, please refer to the documen=
tation for ``--dns``.
diff --git a/src/openvpn/Makefile.am =
b/src/openvpn/Makefile.am
index 37af683..2e93ebb 100644
--- a/src/openvpn/Makefile.am
+++ b/src/openvp=
n/Makefile.am
@@ -30,7 +30,8 @@
$(OPTIONA=
L_LZ4_CFLAGS) \
$(OPTIONAL_PKCS11_HELPER_CFLAGS) \=
$(OPTIONAL_INOTIFY_CFLAGS) \
- -DPLUGIN_LIBDIR=3D\"${plugindir}\"
=
+ -DPLUGIN_LIBDIR=3D\"${pl=
ugindir}\" \
+ -=
DDEFAULT_DNS_UPDOWN=3D\"${scriptdir}/dns-updown\"
if WIN32
# we want unicode entry point=
but not the macro
diff --git a/src/openvpn/dns.c b/src/ope=
nvpn/dns.c
index b6e524f..4da0747 100644
--=
- a/src/openvpn/dns.c
+++ b/src/openvpn/dns.c
#include "dns.h"
=
#include "socket.h"
#include "option=
s.h"
+#include &=
quot;run_command.h"
#ifdef _WIN32=
span>
#include "win32.h"
@@ -262,6 +263=
,8 @@
clone.search_domains =3D clone_dns_domains(o->=
;search_domains, gc);
clone.servers =3D clone_dns_serv=
ers(o->servers, gc);
clone.servers_prepull =3D clon=
e_dns_servers(o->servers_prepull, gc);
+ clone.updown =3D o->updown;
+ clone.user_set_updown =3D o->us=
er_set_updown;
return clone;<=
br> }
@@ -548,6 +551,54 @@
send_=
msg_iservice(o->msg_channel, &nrpt, sizeof(nrpt), &ack, "DN=
S");
}
+#else /* ifdef _WIN32 */
+
+static void
+updown_env_set(bool up, const struct dns_options *o, const struct tu=
ntap *tt, struct env_set *es)
+{
+ sete=
nv_str(es, "dev", tt->actual_name);
+ setenv_str(es, "script_type", up=
? "dns-up" : "dns-down");
+ setenv_dns_options(o, es);
+}
+
=
+static int
+do_run_u=
p_down_command(bool up, const struct dns_options *o, const struct tuntap *t=
t)
+{
+ struct gc_arena gc =3D gc_new()=
;
+ struct argv ar=
gv =3D argv_new();
+ =
struct env_set *es =3D env_set_create(&gc);
+
+ updown_env_set(up, o, tt, es);
+
+ argv_printf(&argv, "%s", o->updown);
+ argv_msg(M_INFO, &arg=
v);
+ int res;
+ if (o->user_set_=
updown)
+ {=
+ res =3D openvpn_ru=
n_script(&argv, es, S_EXITCODE, "dns updown");
+ else
+ {
+ res =3D openvpn_execve_check(&argv, es, S_EXITCODE, "WAR=
NING: Failed running dns updown");
+ }
+ argv_free(&argv);
+ gc_free(&gc);
+ return res;
+}
+<=
br>+static void
+run_up_down_command(bool up, struct=
options *o, const struct tuntap *tt)
+{
+=
if (!o->dns_options.updown)
+ {
=
+ return;
+ =
}
+
+ int status;
+ status =3D do_run_up_down_command(up=
, &o->dns_options, tt);
+ msg(M_INFO, "dns %s command exited with status %d"=
, up ? "up" : "down", status);
+}
+
#endif /* _WIN32 */
=
void
@@ -666,5 +717,7 @@
#ifdef _WIN32
run_up_down_service(up, o, t=
t);
+#else
=
+ run_up_down_command(up, o,=
tt);
#endif /* ifdef _WIN32 */
}<=
br>diff --git a/src/openvpn/dns.h b/src/openvpn/dns.h
index f24e30b..c4d19ff 100644
--- a/src/openvpn/dns.h
+++ b/src/openvpn/dns.h
@@ -73,6 +73,8 @@
struct dns_server *servers_prepull;
st=
ruct dns_server *servers;
struct gc_arena gc;+ const char *updown;
+ bool user_set_updown=
;
};
/**
--- a/src/openvpn/options.c=
+++ b/src/openvpn/options.c
@@ -526,10 +52=
6,12 @@
" address <addr[:port=
]> [addr[:port] ...] : server addresses 4/6\n"
=
" resolve-domains <domain> [domain ...] : spli=
t domains\n"
" dnssec <y=
es|no|optional> : option to use DNSSEC\n"
- " type <DoH|DoT>=
; : query server over HTTPS / TLS\n"
+ " transport <DoH|DoT>=
: query server over HTTPS / TLS\n"
" =
sni <domain> : DNS server name indication\n" "--dns search-domains <domain> [domain ...]:\n"=
;
" Add domains to DNS domain se=
arch list\n"
+ =
"--dns-updown cmd|force|disable : Run cmd as user defined dns config=
command,\n"
+ =
" force running the default script or disable runni=
ng it.\n"
"--auth-retry t : How to handle a=
uth failures. Set t to\n"
" =
none (default), interact, or nointeract.\n"
&qu=
ot;--static-challenge t e [<scrv1|concat>]: Enable static challenge/r=
esponse protocol using\n"
@@ -921,6 +923,10 @@<=
br> #ifndef ENABLE_DCO
o->disable_dco =3D tru=
e;
#endif /* ENABLE_DCO */
+
+#ifdef ENABLE_DNS_UPDOWN_BY_DEFAULT
+ o->dns_options.updown =3D DEFAULT_DNS_UPDOWN;=
span>
+#endif /* ENABLE_DNS_=
UPDOWN_BY_DEFAULT */
}
=
void
@@ -8087,6 +8093,39 @@
to->=
;ip_win32_defined =3D true;
}
#endif =
/* ifdef _WIN32 */
+ =
else if (streq(p[0], "dns-updown") && p[1])
=
+ {
+ VERIFY_PERMISSION(OPT_P_SCRIPT);
+ if (!no_more_th=
an_n_args(msglevel, p, 2, NM_QUOTE_HINT))
+ {
+ goto err;
+ }
+ struct dns_options *dns =3D &options->dns_options;
+ if (streq(p[1], &=
quot;disable"))
=
+ {
+ =
dns->updown =3D NULL;
+ dns->user_set_updown =3D false;
+ }
+ else if (streq(p[1], "force"))=
span>
+ {
<=
span style=3D"color: hsl(120, 100%, 40%);">+ /* force dns-updown=
run, even if a --up script is defined */
+ if (dns->user_set_updown =3D=3D false)=
+ {
+ dns->u=
pdown =3D DEFAULT_DNS_UPDOWN;
+ dns->user_set_updown =3D true;
+ }
+ }
+ else
+ {
+ if (streq(dns->updown, DEFAULT_DNS_UPDOWN))
+ {
+ /* Unset the defaul=
t command to prevent warnings */
+ dns->updown =3D NULL;
+ }
+ set_user_script(options, &dns-&=
gt;updown, p[1], p[0], false);
+ dns->user_set_updown =3D true;
+ }
+ }
else if (streq(p[0], =
"dns") && p[1])
{
=
VERIFY_PERMISSION(OPT_P_DHCPDNS);