From: "d12fk (Code Review)" Date: Wed, 14 May 2025 12:58:39 +0000 In-Reply-To: References: Reply-To: heiko@...515..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., gert@...1296... Message-ID: <151e2107434bec6f3810c2fb78ff3e9272be5c55-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="V4q0xDr0Sgc="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: dns: apply settings via script on unixoid systems List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --V4q0xDr0Sgc= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: cron2, d12fk, flichtenheld, plaisthos. Hello cron2, flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/838?usp=3Demail to look at the new patch set (#25). The following approvals got outdated and were removed: Code-Review-1 by cron2 Change subject: dns: apply settings via script on unixoid systems ...................................................................... dns: apply settings via script on unixoid systems This introduces a new script hook, the dns-updown, and implements such a command script for a few popular systems (and a default for the not so popular ones). Like the name suggests this hook is soleley for dealing with modifying how names are resolved when the VPN pushes some --dns settings. The default dns updown command is part of the distribution and is installed with openvpn. You can change the path the command is located at as a compile time option, defaults to libexecdir. You can compile-time disable that the default dns-updown hook is run by passing --disable-dns-updown-by-default to configure or ccmake ENABLE_DNS_UPDOWN_BY_DEFAULT to OFF. There's also a new runtime option --dns-updown, which can run a custom command, force running the default when disabled or disable execution of the dns-updown altogether. Change-Id: Ifbe4ffb44d3bfcaa50adb38cacb3436fcdc71b10 Signed-off-by: Heiko Hund --- M .gitignore M CMakeLists.txt M config.h.cmake.in M configure.ac M distro/Makefile.am A distro/dns-scripts/Makefile.am A distro/dns-scripts/haikuos_file-dns-updown.sh A distro/dns-scripts/openresolv-dns-updown.sh A distro/dns-scripts/resolvconf_file-dns-updown.sh A distro/dns-scripts/systemd-dns-updown.sh M doc/man-sections/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, 707 insertions(+), 10 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/38/838/25 diff --git a/.gitignore b/.gitignore index db8bb73..04523af 100644 --- a/.gitignore +++ b/.gitignore @@ -49,6 +49,7 @@ /doc/doxygen/latex/ /doc/doxygen/openvpn.doxyfile distro/systemd/*.service +distro/dns-scripts/dns-updown sample/sample-keys/sample-ca/ vendor/cmocka_build vendor/dist diff --git a/CMakeLists.txt b/CMakeLists.txt index ae818c3..40bffd4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,10 @@ option(USE_WERROR "Treat compiler warnings as errors (-Werror)" ON) option(FAKE_ANDROID "Target Android but do not use actual cross compile/An= droid cmake to build for simple compile checks on Linux") -set(PLUGIN_DIR /usr/local/lib/openvpn/plugins CACHE FILEPATH "Location of = the plugin directory") +option(ENABLE_DNS_UPDOWN_BY_DEFAULT "Run --dns-updown hook by default" ON) +set(DNS_UPDOWN_PATH "${CMAKE_INSTALL_PREFIX}/libexec/openvpn/dns-updown" C= ACHE STRING "Default location for the DNS up/down script") + +set(PLUGIN_DIR "${CMAKE_INSTALL_PREFIX}/lib/openvpn/plugins" CACHE FILEPAT= H "Location of the plugin directory") # Create machine readable compile commands option(ENABLE_COMPILE_COMMANDS "Generate compile_commands.json and a symli= nk for clangd to find it" OFF) @@ -601,6 +604,8 @@ add_library_deps(openvpn) +target_compile_options(openvpn PRIVATE -DDEFAULT_DNS_UPDOWN=3D\"${DNS_UPDO= WN_PATH}\") + if(MINGW) target_compile_options(openvpn PRIVATE -municode -UUNICODE) target_link_options(openvpn PRIVATE -municode) diff --git a/config.h.cmake.in b/config.h.cmake.in index 2f7b43d..5164ce3 100644 --- a/config.h.cmake.in +++ b/config.h.cmake.in @@ -35,6 +35,9 @@ /* Enable LZO compression library */ #cmakedefine ENABLE_LZO +/* Enable dns-updown script hook */ +#cmakedefine ENABLE_DNS_UPDOWN + /* Enable NTLMv2 proxy support */ #define ENABLE_NTLM 1 diff --git a/configure.ac b/configure.ac index 9777e36..75367e8 100644 --- a/configure.ac +++ b/configure.ac @@ -96,6 +96,13 @@ ) AC_ARG_ENABLE( + [dns-updown-by-default], + [AS_HELP_STRING([--disable-dns-updown-by-default], [disable running --dns= -updown by default @<:@default=3Dyes@:>@])], + , + [enable_dns_updown_by_default=3D"yes"] +) + +AC_ARG_ENABLE( [ntlm], [AS_HELP_STRING([--disable-ntlm], [disable NTLMv2 proxy support @<:@defau= lt=3Dyes@:>@])], , @@ -315,37 +322,50 @@ plugindir=3D"\${libdir}/openvpn/plugins" fi +AC_ARG_VAR([SCRIPTDIR], [Path of script directory @<:@default=3DPKGLIBEXEC= DIR@:>@]) +if test -n "${SCRIPTDIR}"; then + scriptdir=3D"${SCRIPTDIR}" +else + scriptdir=3D"\${pkglibexecdir}" +fi + AC_DEFINE_UNQUOTED([TARGET_ALIAS], ["${host}"], [A string representing our= host]) -AM_CONDITIONAL([TARGET_LINUX], [false]) +AM_CONDITIONAL([ENABLE_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], ["systemd"]) have_sitnl=3D"yes" pkg_config_required=3D"yes" ;; *-*-solaris*) AC_DEFINE([TARGET_SOLARIS], [1], [Are we running on Solaris?]) AC_DEFINE_UNQUOTED([TARGET_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" ;; *-*-openbsd*) AC_DEFINE([TARGET_OPENBSD], [1], [Are we running on OpenBSD?]) AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["O"], [Target prefix]) + AC_SUBST([DNS_UPDOWN_TYPE], ["resolvconf_file"]) ;; *-*-freebsd*) AC_DEFINE([TARGET_FREEBSD], [1], [Are we running on FreeBSD?]) AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["F"], [Target prefix]) + AC_SUBST([DNS_UPDOWN_TYPE], ["openresolv"]) ;; *-*-netbsd*) AC_DEFINE([TARGET_NETBSD], [1], [Are we running NetBSD?]) AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["N"], [Target prefix]) + AC_SUBST([DNS_UPDOWN_TYPE], ["openresolv"]) ;; *-*-darwin*) AC_DEFINE([TARGET_DARWIN], [1], [Are we running on Mac OS X?]) AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["M"], [Target prefix]) + AM_CONDITIONAL([ENABLE_DNS_UPDOWN], [false]) + AC_SUBST([DNS_UPDOWN_TYPE], ["resolvconf_file"]) have_tap_header=3D"yes" ac_cv_type_struct_in_pktinfo=3Dno ;; @@ -353,6 +373,8 @@ AC_DEFINE([TARGET_WIN32], [1], [Are we running WIN32?]) AC_DEFINE([ENABLE_DCO], [1], [DCO is always enabled on Windows]) AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["W"], [Target prefix]) + AM_CONDITIONAL([ENABLE_DNS_UPDOWN], [false]) + AC_SUBST([DNS_UPDOWN_TYPE], ["windows"]) CPPFLAGS=3D"${CPPFLAGS} -DWIN32_LEAN_AND_MEAN" CPPFLAGS=3D"${CPPFLAGS} -DNTDDI_VERSION=3DNTDDI_VISTA -D_WIN32_WINNT=3D_= WIN32_WINNT_VISTA" WIN32=3Dyes @@ -360,10 +382,12 @@ *-*-dragonfly*) AC_DEFINE([TARGET_DRAGONFLY], [1], [Are we running on DragonFlyBSD?]) AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["D"], [Target prefix]) + AC_SUBST([DNS_UPDOWN_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"]) ROUTE=3D"/usr/sbin/route" have_tap_header=3D"yes" ac_cv_header_net_if_h=3D"no" # exists, but breaks things @@ -371,10 +395,12 @@ *-*-haiku*) AC_DEFINE([TARGET_HAIKU], [1], [Are we running Haiku?]) AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["H"], [Target prefix]) + AC_SUBST([DNS_UPDOWN_TYPE], ["haikuos_file"]) LIBS=3D"${LIBS} -lnetwork" ;; *) AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["X"], [Target prefix]) + AC_SUBST([DNS_UPDOWN_TYPE], ["resolvconf_file"]) have_tap_header=3D"yes" ;; esac @@ -1317,7 +1343,7 @@ test "${enable_small}" =3D "yes" && AC_DEFINE([ENABLE_SMALL], [1], [Enable= smaller executable size]) test "${enable_fragment}" =3D "yes" && AC_DEFINE([ENABLE_FRAGMENT], [1], [= Enable internal fragmentation support]) test "${enable_port_share}" =3D "yes" && AC_DEFINE([ENABLE_PORT_SHARE], [1= ], [Enable TCP Server port sharing]) - +test "${enable_dns_updown_by_default}" =3D "yes" && AC_DEFINE([ENABLE_DNS_= UPDOWN_BY_DEFAULT], [1], [Enable dns-updown hook by default]) test "${enable_ntlm}" =3D "yes" && AC_DEFINE([ENABLE_NTLM], [1], [Enable N= TLMv2 proxy support]) test "${enable_crypto_ofb_cfb}" =3D "yes" && AC_DEFINE([ENABLE_OFB_CFB_MOD= E], [1], [Enable OFB and CFB cipher modes]) if test "${have_export_keying_material}" =3D "yes"; then @@ -1505,6 +1531,7 @@ =20 sampledir=3D"\$(docdir)/sample" AC_SUBST([plugindir]) +AC_SUBST([scriptdir]) AC_SUBST([sampledir]) AC_SUBST([systemdunitdir]) @@ -1541,6 +1568,7 @@ Makefile distro/Makefile distro/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.am +++ b/distro/Makefile.am @@ -13,3 +13,7 @@ $(srcdir)/Makefile.in SUBDIRS =3D systemd + +if ENABLE_DNS_UPDOWN +SUBDIRS +=3D dns-scripts +endif diff --git a/distro/dns-scripts/Makefile.am b/distro/dns-scripts/Makefile.am new file mode 100644 index 0000000..fc2db08 --- /dev/null +++ b/distro/dns-scripts/Makefile.am @@ -0,0 +1,29 @@ +# +# OpenVPN -- An application to securely tunnel IP networks +# 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 +# + +MAINTAINERCLEANFILES =3D \ + $(srcdir)/Makefile.in + +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-scr= ipts/haikuos_file-dns-updown.sh new file mode 100644 index 0000000..748804e --- /dev/null +++ b/distro/dns-scripts/haikuos_file-dns-updown.sh @@ -0,0 +1,85 @@ +#!/bin/sh +# +# Simple OpenVPN up/down script for modifying Haiku OS resolv.conf +# (C) Copyright 2024 OpenVPN Inc +# +# 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.com +# dns_server_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 +# + +set -e +u + +conly_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}\" + [ -z "$port" -o "$port" =3D "53" ] || return 1 + + i=3D$(expr $i + 1) + done +} + +onf=3D/boot/system/settings/network/resolv.conf +test -e "$conf" || exit 1 +case "${script_type}" in +dns-up) + n=3D1 + while :; do + eval addr=3D\"\$dns_server_${n}_address_1\" + [ -n "$addr" ] || { + echo "setting DNS failed, no compatible server profile" + exit 1 + } + + # Skip server profiles which require DNSSEC, + # secure transport or use a custom port + eval dnssec=3D\"\$dns_server_${n}_dnssec\" + eval transport=3D\"\$dns_server_${n}_transport\" + [ -z "$transport" -o "$transport" =3D "plain" ] \ + && [ -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\" + eval addr3=3D\"\$dns_server_${n}_address_3\" + text=3D"### openvpn ${dev} begin ###\n" + text=3D"${text}nameserver $addr1\n" + test -z "$addr2" || text=3D"${text}nameserver $addr2\n" + test -z "$addr3" || text=3D"${text}nameserver $addr3\n" + + test -z "$dns_search_domain_1" || { + for i in $(seq 1 6); do + eval domains=3D\"$domains\$dns_search_domain_${i} \" || break + done + text=3D"${text}search $domains\n" + } + text=3D"${text}### openvpn ${dev} end ###" + text=3D"${text}\n$(cat ${conf})" + + echo "${text}" > "${conf}" + ;; +dns-down) + sed -i'' -e "/### openvpn ${dev} begin ###/,/### openvpn ${dev} end ##= #/d" "$conf" + ;; +esac diff --git a/distro/dns-scripts/openresolv-dns-updown.sh b/distro/dns-scrip= ts/openresolv-dns-updown.sh new file mode 100644 index 0000000..e50398c --- /dev/null +++ b/distro/dns-scripts/openresolv-dns-updown.sh @@ -0,0 +1,89 @@ +#!/bin/sh +# +# Simple OpenVPN up/down script for openresolv integration +# (C) Copyright 2016 Baptiste Daroussin +# 2024 OpenVPN Inc +# +# 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.com +# dns_server_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 +# + +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}\" + [ -z "$port" -o "$port" =3D "53" ] || return 1 + + i=3D$(expr $i + 1) + done +} + +: ${script_type:=3Ddns-down} +case "${script_type}" in +dns-up) + n=3D1 + while :; do + eval addr=3D\"\$dns_server_${n}_address_1\" + [ -n "$addr" ] || { + echo "setting DNS failed, no compatible server profile" + exit 1 + } + + # Skip server profiles which require DNSSEC, + # secure transport or use a custom port + eval dnssec=3D\"\$dns_server_${n}_dnssec\" + eval transport=3D\"\$dns_server_${n}_transport\" + [ -z "$transport" -o "$transport" =3D "plain" ] \ + && [ -z "$dnssec" -o "$dnssec" =3D "no" ] \ + && only_standard_server_ports && break + + n=3D$(expr $n + 1) + 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 "nameserver ${option}" + done + i=3D1 + maxdom=3D6 + while :; do + maxdom=3D$((maxdom - 1)) + [ $maxdom -gt 0 ] || break + eval option=3D\"\$dns_search_domain_${i}\" || break + [ "${option}" ] || break + i=3D$((i + 1)) + echo "search ${option}" + done + } | /sbin/resolvconf -a "${dev}" + ;; +dns-down) + /sbin/resolvconf -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..567b402 --- /dev/null +++ b/distro/dns-scripts/resolvconf_file-dns-updown.sh @@ -0,0 +1,85 @@ +#!/bin/sh +# +# Simple OpenVPN up/down script for modifying /etc/resolv.conf +# (C) Copyright 2024 OpenVPN Inc +# +# 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.com +# dns_server_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 +# + +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}\" + [ -z "$port" -o "$port" =3D "53" ] || return 1 + + i=3D$(expr $i + 1) + done +} + +conf=3D/etc/resolv.conf +test -e "$conf" || exit 1 +case "${script_type}" in +dns-up) + n=3D1 + while :; do + eval addr=3D\"\$dns_server_${n}_address_1\" + [ -n "$addr" ] || { + echo "setting DNS failed, no compatible server profile" + exit 1 + } + + # Skip server profiles which require DNSSEC, + # secure transport or use a custom port + eval dnssec=3D\"\$dns_server_${n}_dnssec\" + eval transport=3D\"\$dns_server_${n}_transport\" + [ -z "$transport" -o "$transport" =3D "plain" ] \ + && [ -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\" + eval addr3=3D\"\$dns_server_${n}_address_3\" + text=3D"### openvpn ${dev} begin ###\n" + text=3D"${text}nameserver $addr1\n" + test -z "$addr2" || text=3D"${text}nameserver $addr2\n" + test -z "$addr3" || text=3D"${text}nameserver $addr3\n" + + test -z "$dns_search_domain_1" || { + for i in $(seq 1 6); do + eval domains=3D\"$domains\$dns_search_domain_${i} \" || break + done + text=3D"${text}search $domains\n" + } + text=3D"${text}### openvpn ${dev} end ###" + text=3D"${text}\n$(cat ${conf})" + + echo "${text}" > "${conf}" + ;; +dns-down) + sed -i'' -e "/### openvpn ${dev} begin ###/,/### openvpn ${dev} end ##= #/d" "$conf" + ;; +esac diff --git a/distro/dns-scripts/systemd-dns-updown.sh b/distro/dns-scripts/= systemd-dns-updown.sh new file mode 100644 index 0000000..ecadd29 --- /dev/null +++ b/distro/dns-scripts/systemd-dns-updown.sh @@ -0,0 +1,240 @@ +#!/bin/bash +# +# dns-updown - add/remove openvpn provided DNS information +# +# Copyright (C) 2024 OpenVPN Inc +# +# SPDX-License-Identifier: GPL-2.0 +# +# Add/remove openvpn DNS settings from the env into/from +# the system. Supported backends in this order: +# +# * systemd-resolved +# * resolvconf +# * /etc/resolv.conf file +# +# 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_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 +# + +function do_resolved_servers { + local sni=3D"" + local transport_var=3Ddns_server_${n}_transport + local sni_var=3Ddns_server_${n}_sni + [ "${!transport_var}" =3D "DoT" ] && sni=3D"#${!sni_var}" + + local i=3D1 + local addrs=3D"" + while :; do + local addr_var=3Ddns_server_${n}_address_${i} + local addr=3D"${!addr_var}" + [ -n "$addr" ] || break + + local port_var=3Ddns_server_${n}_port_${i} + if [ -n "${!port_var}" ]; then + if [[ "$addr" =3D~ : ]]; then + addr=3D"[$addr]" + fi + addrs+=3D"${addr}:${!port_var}${sni} " + else + addrs+=3D"${addr}${sni} " + fi + i=3D$((i+1)) + done + + resolvectl dns "$dev" $addrs +} + +function do_resolved_domains { + local list=3D"" + for domain_var in ${!dns_search_domain_*}; do + list+=3D"${!domain_var} " + done + local domain_var=3Ddns_server_${n}_resolve_domain_1 + if [ -z "${!domain_var}" ]; then + resolvectl default-route "$dev" true + list+=3D"~." + else + resolvectl default-route "$dev" false + local i=3D1 + while :; do + domain_var=3Ddns_server_${n}_resolve_domain_${i} + [ -n "${!domain_var}" ] || break + # Add as split domain (~ prefix), if it doesn't already exist + [[ "$list" =3D~ (^| )"${!domain_var}"( |$) ]] \ + || list+=3D"~${!domain_var} " + i=3D$((i+1)) + done + fi + + resolvectl domain "$dev" $list +} + +function do_resolved_dnssec { + local dnssec_var=3Ddns_server_${n}_dnssec + if [ "${!dnssec_var}" =3D "optional" ]; then + resolvectl dnssec "$dev" allow-downgrade + elif [ "${!dnssec_var}" =3D "yes" ]; then + resolvectl dnssec "$dev" true + else + resolvectl dnssec "$dev" false + fi +} + +function do_resolved_dnsovertls { + local transport_var=3Ddns_server_${n}_transport + if [ "${!transport_var}" =3D "DoT" ]; then + resolvectl dnsovertls "$dev" true + else + resolvectl dnsovertls "$dev" false + fi +} + +function do_resolved { + [[ "$(readlink /etc/resolv.conf)" =3D~ systemd ]] || return 1 + + n=3D1 + while :; do + local addr_var=3Ddns_server_${n}_address_1 + [ -n "${!addr_var}" ] || { + echo "setting DNS failed, no compatible server profile" + return 1 + } + + # Skip server profiles which require DNS-over-HTTPS + local transport_var=3Ddns_server_${n}_transport + [ -n "${!transport_var}" -a "${!transport_var}" =3D "DoH" ] || bre= ak + + 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 resolvectl" + resolvectl revert "$dev" + fi + + return 0 +} + +function only_standard_server_ports { + local i=3D1 + while :; do + local addr_var=3Ddns_server_${n}_address_${i} + [ -n "${!addr_var}" ] || return 0 + + local port_var=3Ddns_server_${n}_port_${i} + [ -z "${!port_var}" -o "${!port_var}" =3D "53" ] || return 1 + + i=3D$((i+1)) + done +} + +function resolv_conf_compat_profile { + local n=3D1 + while :; do + local server_addr_var=3Ddns_server_${n}_address_1 + [ -n "${!server_addr_var}" ] || { + echo "setting DNS failed, no compatible server profile" + exit 1 + } + + # Skip 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 "${!transport_var}" -o "${!transport_var}" =3D "plain" ] \ + && [ -z "${!dnssec_var}" -o "${!dnssec_var}" =3D "no" ] \ + && only_standard_server_ports && break + + n=3D$((n+1)) + done + return $n +} + +function do_resolvconf { + [ -x /sbin/resolvconf ] || return 1 + + resolv_conf_compat_profile + local n=3D$? + + 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} " + done + { + local maxns=3D3 + local server_var=3Ddns_server_${n}_address_* + for addr_var in ${!server_var}; do + [ $((maxns--)) -gt 0 ] || break + echo "nameserver ${!addr_var}" + done + [ -z "$domains" ] || echo "search $domains" + } | /sbin/resolvconf -a "$dev" + else + echo "unsetting DNS using resolvconf" + /sbin/resolvconf -d "$dev" + fi + + return 0 +} + +function do_resolv_conf_file { + conf=3D/etc/resolv.conf + test -e "$conf" || exit 1 + + resolv_conf_compat_profile + local n=3D$? + + if [ "$script_type" =3D "dns-up" ]; then + echo "setting DNS using resolv.conf file" + + local addr1_var=3Ddns_server_${n}_address_1 + local addr2_var=3Ddns_server_${n}_address_2 + local addr3_var=3Ddns_server_${n}_address_3 + text=3D"### openvpn ${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_domain_1" || { + for i in $(seq 1 6); do + eval domains=3D\"$domains\$dns_search_domain_${i} \" || br= eak + 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" + sed -i "/### openvpn ${dev} begin ###/,/### openvpn ${dev} end ###= /d" "$conf" + fi + + return 0 +} + +do_resolved || do_resolvconf || do_resolv_conf_file || { + echo "setting DNS failed, no method succeeded" + exit 1 +} diff --git a/doc/man-sections/script-options.rst b/doc/man-sections/script-= options.rst index e48710b..bd5ecd4 100644 --- a/doc/man-sections/script-options.rst +++ b/doc/man-sections/script-options.rst @@ -8,9 +8,13 @@ Script Order of Execution ------------------------- +#. ``--dns-updown`` + + Executed after TCP/UDP socket bind and TUN/TAP open, before ``--up``. + #. ``--up`` - Executed after TCP/UDP socket bind and TUN/TAP open. + Executed after TCP/UDP socket bind and TUN/TAP open, after ``--dns-updo= wn``. #. ``--tls-verify`` @@ -38,9 +42,13 @@ Executed in ``--mode server`` mode on client instance shutdown. +#. ``--dns-updown`` + + Executed before TCP/UDP and TUN/TAP close, before ``--down``. + #. ``--down`` - Executed after TCP/UDP and TUN/TAP close. + Executed after TCP/UDP and TUN/TAP close, after ``--dns-updown``. #. ``--learn-address`` @@ -171,7 +179,7 @@ client-crresponse cmd OpenVPN will write the response of the client into a temporary file. - The filename will be passed as an argument to ``cmd``, and the file will= be + The filename will be passed as an argument to ``cmd``, and the file will automatically deleted by OpenVPN after the script returns. The response is passed as is from the client. The script needs to check @@ -233,6 +241,31 @@ The ``--client-disconnect`` command is not passed any extra arguments (only those arguments specified in cmd, if any). +--dns-updown cmd + Run command ``cmd``, instead of the default DNS up/down command that com= es + 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 profiles that cannot be applied. Port, DNSSEC and secure transport + settings need to be adhered to. If split DNS is not possible a full redi= rect + can be used as a fallback. If not all of the server addresses or search = domains + can be configured, apply them in the order they are listed 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 man= agement + interface. + + Note that DNS-related ``--dhcp-option``\ s might be converted so that th= ey are + available to this hook if no ``--dns`` options exist. If any ``--dns ser= ver`` + option is present, DNS-related ``--dhcp-option``\ s will always be ignor= ed. + If an ``--up`` script is defined, foreign_option env vars will be genera= ted + from ``--dns`` options and passed to the script. The default ``--dns-upd= own`` + command is not run if an ``--up`` script is defined. Both is done for ba= ckward + 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 ``f= orce`` + as ``cmd`` to run the default command. No DNS env vars will be passed to= ``--up`` + in this case. + --down cmd Run command ``cmd`` after TUN/TAP device close (post ``--user`` UID change and/or ``--chroot`` ). ``cmd`` consists of a path to script (or @@ -659,7 +692,7 @@ names). Set prior to ``--up`` or ``--down`` script execution. :code:`dns_*` - The ``--dns`` configuration options will be made available to script + The ``--dns`` configuration options will be made available to ``--dns-= updown`` execution through this set of environment variables. Variables appear only if the corresponding option has a value assigned. For the semanti= cs of each individual variable, please refer to the documentation 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/openvpn/Makefile.am @@ -30,7 +30,8 @@ $(OPTIONAL_LZ4_CFLAGS) \ $(OPTIONAL_PKCS11_HELPER_CFLAGS) \ $(OPTIONAL_INOTIFY_CFLAGS) \ - -DPLUGIN_LIBDIR=3D\"${plugindir}\" + -DPLUGIN_LIBDIR=3D\"${plugindir}\" \ + -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/openvpn/dns.c index b6e524f..4da0747 100644 --- a/src/openvpn/dns.c +++ b/src/openvpn/dns.c @@ -30,6 +30,7 @@ #include "dns.h" #include "socket.h" #include "options.h" +#include "run_command.h" #ifdef _WIN32 #include "win32.h" @@ -262,6 +263,8 @@ clone.search_domains =3D clone_dns_domains(o->search_domains, gc); clone.servers =3D clone_dns_servers(o->servers, gc); clone.servers_prepull =3D clone_dns_servers(o->servers_prepull, gc); + clone.updown =3D o->updown; + clone.user_set_updown =3D o->user_set_updown; return clone; } @@ -548,6 +551,54 @@ send_msg_iservice(o->msg_channel, &nrpt, sizeof(nrpt), &ack, "DNS"); } +#else /* ifdef _WIN32 */ + +static void +updown_env_set(bool up, const struct dns_options *o, const struct tuntap *= tt, struct env_set *es) +{ + setenv_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_up_down_command(bool up, const struct dns_options *o, const struct = tuntap *tt) +{ + struct gc_arena gc =3D gc_new(); + struct argv argv =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, &argv); + int res; + if (o->user_set_updown) + { + res =3D openvpn_run_script(&argv, es, S_EXITCODE, "dns updown"); + } + else + { + res =3D openvpn_execve_check(&argv, es, S_EXITCODE, "WARNING: Fail= ed running dns updown"); + } + argv_free(&argv); + gc_free(&gc); + return res; +} + +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, tt); +#else + run_up_down_command(up, o, tt); #endif /* ifdef _WIN32 */ } 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; struct dns_server *servers; struct gc_arena gc; + const char *updown; + bool user_set_updown; }; /** diff --git a/src/openvpn/options.c b/src/openvpn/options.c index 02970a7..1d1daea 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -526,10 +526,12 @@ " address [addr[:port] ...] : server ad= dresses 4/6\n" " resolve-domains [domain ...] : split domai= ns\n" " dnssec : option to use DNSSEC\n" - " type : query server over HTTPS / TLS\n" + " transport : query server over HTTPS / TLS= \n" " sni : DNS server name indication\n" "--dns search-domains [domain ...]:\n" " Add domains to DNS domain search list\n" + "--dns-updown cmd|force|disable : Run cmd as user defined dns config c= ommand,\n" + " force running the default script or disable running= it.\n" "--auth-retry t : How to handle auth failures. Set t to\n" " none (default), interact, or nointeract.\n" "--static-challenge t e []: Enable static challenge/resp= onse protocol using\n" @@ -922,6 +924,10 @@ #ifndef ENABLE_DCO o->disable_dco =3D true; #endif /* ENABLE_DCO */ + +#ifdef ENABLE_DNS_UPDOWN_BY_DEFAULT + o->dns_options.updown =3D DEFAULT_DNS_UPDOWN; +#endif /* ENABLE_DNS_UPDOWN_BY_DEFAULT */ } void @@ -8088,6 +8094,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_than_n_args(msglevel, p, 2, NM_QUOTE_HINT)) + { + goto err; + } + struct dns_options *dns =3D &options->dns_options; + if (streq(p[1], "disable")) + { + dns->updown =3D NULL; + dns->user_set_updown =3D false; + } + else if (streq(p[1], "force")) + { + /* force dns-updown run, even if a --up script is defined */ + if (dns->user_set_updown =3D=3D false) + { + dns->updown =3D DEFAULT_DNS_UPDOWN; + dns->user_set_updown =3D true; + } + } + else + { + if (streq(dns->updown, DEFAULT_DNS_UPDOWN)) + { + /* Unset the default command to prevent warnings */ + dns->updown =3D NULL; + } + set_user_script(options, &dns->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); -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/838?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Ifbe4ffb44d3bfcaa50adb38cacb3436fcdc71b10 Gerrit-Change-Number: 838 Gerrit-PatchSet: 25 Gerrit-Owner: d12fk Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: cron2 Gerrit-Attention: flichtenheld Gerrit-Attention: d12fk Gerrit-MessageType: newpatchset --V4q0xDr0Sgc= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: cron2, d12fk, flichtenheld, plais= thos.

d12fk uploaded patch set #25 to this change.

View Change

The following approvals got outdated and were removed: Code-Review-1 by cron2

dns: apply settings via script on unixoid systems
This introduces a new script hook, the dns-updown, and implements such= a
command script for a few popular systems (and a default for the not s= o
popular ones). Like the name suggests this hook is soleley for dealing=
with modifying how names are resolved when the VPN pushes some --dnssettings.

The default dns updown command is part of the distributio= n and is
installed with openvpn. You can change the path the command is = located
at as a compile time option, defaults to libexecdir.

You = can compile-time disable that the default dns-updown hook is
run by pass= ing --disable-dns-updown-by-default to configure or
ccmake ENABLE_DNS_UP= DOWN_BY_DEFAULT to OFF.

There's also a new runtime option --dns-= updown, which can run a custom
command, force running the default when d= isabled or disable execution
of the dns-updown altogether.

Change= -Id: Ifbe4ffb44d3bfcaa50adb38cacb3436fcdc71b10
Signed-off-by: Heiko Hund= <heiko@...2662...>
---
M .gitignore
M CMakeLists.txt
M c= onfig.h.cmake.in
M configure.ac
M distro/Makefile.am
A distro/dns-= scripts/Makefile.am
A distro/dns-scripts/haikuos_file-dns-updown.sh
A= distro/dns-scripts/openresolv-dns-updown.sh
A distro/dns-scripts/resolv= conf_file-dns-updown.sh
A distro/dns-scripts/systemd-dns-updown.sh
M = doc/man-sections/script-options.rst
M src/openvpn/Makefile.am
M src/o= penvpn/dns.c
M src/openvpn/dns.h
M src/openvpn/options.c
15 files = changed, 707 insertions(+), 10 deletions(-)

git pull ssh://gerrit=
.openvpn.net:29418/openvpn refs/changes/38/838/25
diff --git a/.git=
ignore b/.gitignore
index db8bb73..04523af 100644--- a/.gitignore
+++ b/.gitignore
@@= -49,6 +49,7 @@
/doc/doxygen/latex/
/doc/= doxygen/openvpn.doxyfile
distro/systemd/*.service+distro/dns-scripts/dns-updow= n
sample/sample-keys/sample-ca/
vendor/cm= ocka_build
vendor/dist
diff --git a/CMakeL= ists.txt b/CMakeLists.txt
index ae818c3..40bffd4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -41,7 +41,10 @@
option(USE_WERROR "Tre= at compiler warnings as errors (-Werror)" ON)
option(= FAKE_ANDROID "Target Android but do not use actual cross compile/Andro= id cmake to build for simple compile checks on Linux")

-set(PLUGIN_DIR /usr= /local/lib/openvpn/plugins CACHE FILEPATH "Location of the plugin dire= ctory")
+option(= ENABLE_DNS_UPDOWN_BY_DEFAULT "Run --dns-updown hook by default" O= N)
+set(DNS_UPDOWN_PA= TH "${CMAKE_INSTALL_PREFIX}/libexec/openvpn/dns-updown" CACHE STR= ING "Default location for the DNS up/down script")
+

+set(PLUGIN_DIR "${CMAKE_INSTALL_PREFIX}/lib/open= vpn/plugins" CACHE FILEPATH "Location of the plugin directory&quo= t;)

# Create machine readable compile co= mmands
option(ENABLE_COMPILE_COMMANDS "Generate compi= le_commands.json and a symlink for clangd to find it" OFF)
<= span>@@ -601,6 +604,8 @@

add_library_dep= s(openvpn)

+target_compile_options(openvpn PRIVATE -DDEFAULT_DNS_UPDOWN=3D\&qu= ot;${DNS_UPDOWN_PATH}\")
+
if(MINGW)
target_compile_o= ptions(openvpn PRIVATE -municode -UUNICODE)
target_lin= k_options(openvpn PRIVATE -municode)
diff --git a/config.h.= cmake.in b/config.h.cmake.in
index 2f7b43d..5164ce3 100644<= /span>
--- a/config.h.cmake.in
+++ b/config.h.cmak= e.in
@@ -35,6 +35,9 @@
/* Enable LZO compr= ession library */
#cmakedefine ENABLE_LZO
=
+/* Enable dns-updo= wn script hook */
+#c= makedefine ENABLE_DNS_UPDOWN
+
/* Enable NTLMv2 proxy support */
#define ENABLE_NTLM 1


diff --git a/con= figure.ac b/configure.ac
index 9777e36..75367e8 100644
--- a/configure.ac
+++ b/configure.ac@@ -96,6 +96,13 @@
)

AC_ARG_ENABLE(

+= [dns-updown-by-default],
+ [AS_HELP_STRING([--disable-dns-updown-by-default], [disable running = --dns-updown by default @<:@default=3Dyes@:>@])],
+ ,
+ [enable_dns_updown_by_default=3D"yes"]=
+)
+
+AC_ARG_ENABLE(
[ntlm],
[AS_= HELP_STRING([--disable-ntlm], [disable NTLMv2 proxy support @<:@default= =3Dyes@:>@])],
,
@@ -315,37 +322,50 @@=
plugindir=3D"\${libdir}/openvpn/plugins"
fi

+AC_ARG_VAR([SCRIPTDIR], [Path of script directory @<:@defau= lt=3DPKGLIBEXECDIR@:>@])
+if test -n "${SCRIPTDIR}"; then
+ scriptdir=3D"${SCRIPTDIR}"<= br>+else
+ scriptdir=3D"\${pkglibexecdir}"= ;
+fi
+

AC_DEFINE_UNQUOTE= D([TARGET_ALIAS], ["${host}"], [A string representing our host])<= /span>
-AM_CONDITIONAL([TARGET= _LINUX], [false])
+AM= _CONDITIONAL([ENABLE_DNS_UPDOWN],[true])
case "$host&= quot; 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])<= /span>
+ AC_SUBST([DNS_UPDO= WN_TYPE], ["systemd"])
have_sitnl=3D"yes&= quot;
pkg_config_required=3D"yes"
;;

*-*-solaris*)
AC_DEFINE([TAR= GET_SOLARIS], [1], [Are we running on Solaris?])
AC_DEFI= NE_UNQUOTED([TARGET_PREFIX], ["S"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], [&= quot;resolvconf_file"])
CPPFLAGS=3D"$CPPFLAGS = -D_XPG4_2"
test -x /bin/bash && SHELL=3D&qu= ot;/bin/bash"
;;
*-*-openbsd*)

AC_DEFINE([TARGET_OPENBSD], [1], [Are we running on OpenBS= D?])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["O"]= , [Target prefix])
+ = AC_SUBST([DNS_UPDOWN_TYPE], ["resolvconf_file"])
;;
*-*-freebsd*)
AC_DEFINE([TARGET= _FREEBSD], [1], [Are we running on FreeBSD?])
AC_DEFINE_= UNQUOTED([TARGET_PREFIX], ["F"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], [&quo= t;openresolv"])
;;
*-*-netbsd*)
AC_DEFINE([TARGET_NETBSD], [1], [Are we running NetBSD?])=
AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["N"], [T= arget prefix])
+ AC_= SUBST([DNS_UPDOWN_TYPE], ["openresolv"])
;;
*-*-darwin*)
AC_DEFINE([TARGET_DARWIN], = [1], [Are we running on Mac OS X?])
AC_DEFINE_UNQUOTED([= TARGET_PREFIX], ["M"], [Target prefix])
+ AM_CONDITIONAL([ENABLE_DNS_UPDOWN], [false]= )
+ AC_SUBST([DNS_UP= DOWN_TYPE], ["resolvconf_file"])
have_tap_head= er=3D"yes"
ac_cv_type_struct_in_pktinfo=3Dno
;;
@@ -353,6 +373,8 @@
= AC_DEFINE([TARGET_WIN32], [1], [Are we running WIN32?])
= AC_DEFINE([ENABLE_DCO], [1], [DCO is always enabled on Windows]) AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["W"], [Target pref= ix])
+ AM_CONDITIONA= L([ENABLE_DNS_UPDOWN], [false])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["windows"])
= CPPFLAGS=3D"${CPPFLAGS} -DWIN32_LEAN_AND_MEAN" CPPFLAGS=3D"${CPPFLAGS} -DNTDDI_VERSION=3DNTDDI_VISTA -D_WIN= 32_WINNT=3D_WIN32_WINNT_VISTA"
WIN32=3Dyes@@ -360,10 +382,12 @@
*-*-dragonfly*)
<= span> AC_DEFINE([TARGET_DRAGONFLY], [1], [Are we running on DragonFlyBSD?= ])

AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["D"], = [Target prefix])
+ A= C_SUBST([DNS_UPDOWN_TYPE], ["openresolv"])
;;<= /span>
*-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"]= )
ROUTE=3D"/usr/sbin/route"
= have_tap_header=3D"yes"
ac_cv_header_net_if_h= =3D"no" # exists, but breaks things
@@ -371,10 +3= 95,12 @@
*-*-haiku*)
AC_DEFINE([TARGET= _HAIKU], [1], [Are we running Haiku?])
AC_DEFINE_UNQUOTE= D([TARGET_PREFIX], ["H"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["haik= uos_file"])
LIBS=3D"${LIBS} -lnetwork"
;;
*)
AC_DEFINE_UNQUO= TED([TARGET_PREFIX], ["X"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["res= olvconf_file"])
have_tap_header=3D"yes"
;;
esac
@@ -1317,7 +1343= ,7 @@
test "${enable_small}" =3D "yes"= && AC_DEFINE([ENABLE_SMALL], [1], [Enable smaller executable size]= )
test "${enable_fragment}" =3D "yes" = && AC_DEFINE([ENABLE_FRAGMENT], [1], [Enable internal fragmentation= support])
test "${enable_port_share}" =3D "= ;yes" && AC_DEFINE([ENABLE_PORT_SHARE], [1], [Enable TCP Serve= r port sharing])
-
+test "${enable_dns_= updown_by_default}" =3D "yes" && AC_DEFINE([ENABLE_D= NS_UPDOWN_BY_DEFAULT], [1], [Enable dns-updown hook by default])
= test "${enable_ntlm}" =3D "yes" && AC_DE= FINE([ENABLE_NTLM], [1], [Enable NTLMv2 proxy support])
te= st "${enable_crypto_ofb_cfb}" =3D "yes" && AC_D= EFINE([ENABLE_OFB_CFB_MODE], [1], [Enable OFB and CFB cipher modes])=
if test "${have_export_keying_material}" =3D "yes= "; then
@@ -1505,6 +1531,7 @@
=
sampledir=3D"\$(docdir)/sample"
AC_SU= BST([plugindir])
+AC_= SUBST([scriptdir])
AC_SUBST([sampledir])
=
AC_SUBST([systemdunitdir])
@@ -1541,6 +15= 68,7 @@
Makefile
distro/Makefile=
distro/systemd/Makefile
+ distro/dns-scripts/Makefile
doc/Makefile=
doc/doxygen/Makefile
doc/doxygen/openv= pn.doxyfile
diff --git a/distro/Makefile.am b/distro/Makefi= le.am
index 7a588da..26f577b 100644
--- a/d= istro/Makefile.am
+++ b/distro/Makefile.am
= @@ -13,3 +13,7 @@
$(srcdir)/Makefile.in
=
SUBDIRS =3D systemd
+
+i= f ENABLE_DNS_UPDOWN
+= SUBDIRS +=3D dns-scripts
+endif
diff --git a/distro/dns-scripts/Makefile.am b/di= stro/dns-scripts/Makefile.am
new file mode 100644index 0000000..fc2db08
--- /dev/null
+++ b/distro/dns-scripts/Makefile.am

@@ -0,0 +1,29 @@
+#
+# OpenVPN -- An application to securely = tunnel IP networks
+#= over a single UDP port, with support for SSL/TLS-based<= br>+# session authe= ntication and key exchange,
+# packet encryption, packet authentication, and<= br>+# packet compre= ssion.
+#
<= span style=3D"color: hsl(120, 100%, 40%);">+# Copyright (C) 2002-2024 Open= VPN Inc <sales@...515...>

+#
+

+MAINTAINERCLEANFILES =3D = \
+ $(srcdir)/Makefil= e.in
+
+EXTRA_DIST =3D \
+ systemd-dns-updown.sh \
+ openresolv-dns-updown.sh \

+ haikuos_file-dns-updown= .sh \
+ resolvconf_fi= le-dns-updown.sh
+
+script_SCRIPTS =3D \
+ dns-updown+
+CLEANFILES =3D $(script_SCRIPTS)
+

+dns-updown: @DNS_UPDOWN_TYPE@...2841...
+ cp ${srcdir}/@DNS_UPDOWN_TYPE@...493...= 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

index 0000000..748804e
--- /dev/null+++ b/distro/dns-scripts/haikuos_file-dns-updown.sh
@@ -0,0 +1,85 @@
+#!= /bin/sh
+#
= +# Simple OpenVPN up/down scrip= t 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
<= span style=3D"color: hsl(120, 100%, 40%);">+# script_type dns-up
+# dns_search_domain_1 myco= rp.in
+# dns_search= _domain_2 eu.mycorp.com
+# dns_server_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_sn= i dns.mycorp.in
+#
+
+set -e +u
+
+conly_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}\"
+ [ -z "$port" -o "$port"= ; =3D "53" ] || return 1
+
+ = i=3D$(expr $i + 1)
+}=
+
+onf=3D/boot/system/settings/network/resolv.co= nf
+test -e "$co= nf" || exit 1
+c= ase "${script_type}" in
+dns-up)
+ n=3D1
+ whil= e :; do
+ eval= addr=3D\"\$dns_server_${n}_address_1\"
+ [ -n "$addr" ] || {<= br>+ echo "sett= ing DNS failed, no compatible server profile"
+ exit 1
+ }
+
= + # Skip server profiles which require DNSSEC,
+ # secure transport or use a custo= m port
+ eval = dnssec=3D\"\$dns_server_${n}_dnssec\"
+ eval transport=3D\"\$dns_server_${= n}_transport\"
+= [ -z "$transport" -o "$transport" =3D "pla= in" ] \
+ = && [ -z "$dnssec" -o "$dnssec" =3D "n= o" ] \
+ = && 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\"
+= eval addr3=3D\"\$dns_server_${n}_address_3\"
+ text=3D"### openvpn ${dev} = begin ###\n"
+ = text=3D"${text}nameserver $addr1\n"
+ test -z "$addr2" || text=3D"= ${text}nameserver $addr2\n"
+ test -z "$addr3" || text=3D"${text}nameserv= er $addr3\n"
+
+ test -z "$dn= s_search_domain_1" || {
+ for i in $(seq 1 6); do
+ eval domains=3D\"$domains\$dns_search= _domain_${i} \" || break
+ done
+ }
+ text=3D"${text}### openvpn ${dev} end ###&quo= t;
+ text=3D"= ${text}\n$(cat ${conf})"
+
+ echo = "${text}" > "${conf}"
+ ;;
+dns-down)
+ sed -i'' -e "/### openvpn ${dev} begin ###/,/### openvpn= ${dev} end ###/d" "$conf"
+ ;;
+esac
diff --git a/distro/dns-scripts/openresolv-dn= s-updown.sh b/distro/dns-scripts/openresolv-dns-updown.sh
n= ew file mode 100644
index 0000000..e50398c
= --- /dev/null
+++ b/distro/dns-scripts/openresolv-dns-updow= n.sh
@@ -0,0 +1,89 @@
+#!/bin/sh
+#
+# Simple O= penVPN up/down script for openresolv integration
+# (C) Copyright 2016 Baptiste Daroussin=
+# 2024 OpenV= PN Inc <sales@...515...>
+#
+# SPDX-= License-Identifier: BSD-2-Clause
+#
+# Exa= mple 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.com
+# dns_server_1_address_1 192.168.99.2= 54
+# 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 e= u.mycorp.com

+# dns= _server_1_dnssec true
+# dns_server_1_transport DoH
+# dns_server_1_sni dns.mycorp.in
+#
+
+set -e +u=
+
+only_standard_server_ports() {<= br>+ i=3D1
+ while true; do
+ eval addr=3D\"\$dns_serve= r_${n}_address_${i}\"
+ [ -n "$addr" ] || return 0
+
+ eval port=3D\"\$dns_server_${n}_port_${i}\"= ;
+ [ -z "= ;$port" -o "$port" =3D "53" ] || return 1+
+ i=3D$(expr $i + 1)
+ done
+}
+
+: ${script_ty= pe:=3Ddns-down}
+case= "${script_type}" in
+dns-up)
+ = n=3D1
+ while := ; do
+ eval ad= dr=3D\"\$dns_server_${n}_address_1\"
+ [ -n "$addr" ] || {
= + echo "setting= DNS failed, no compatible server profile"
+ exit 1
+ }
+
+ = # Skip server profiles which require DNSSEC,
+ # secure transport or use a custom p= ort
+ eval dns= sec=3D\"\$dns_server_${n}_dnssec\"
+ eval transport=3D\"\$dns_server_${n}_= transport\"
+ = [ -z "$transport" -o "$transport" =3D "plain&= quot; ] \
+ = && [ -z "$dnssec" -o "$dnssec" =3D "no&q= uot; ] \
+ = && only_standard_server_ports && break
+
+ n=3D$(expr $n + 1)
+ done
+
+ {<= /span>
+ 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 "nameserver ${option}"
+ done+ i=3D1
+ maxdom=3D6

+ while :; do

+ maxdom=3D$((maxdom - 1)= )

+ [ $max= dom -gt 0 ] || break
= + eval option=3D\"\$dns_search_domain_${i}\" || break<= /span>
+ [ "= ${option}" ] || break
+ i=3D$((i + 1))
+ echo "search ${option}"
+ done

+ } | /sbin/resolvconf -a "${dev}&= quot;
+ ;;<= br>+dns-down)
+ /sbin/resolvconf -d "${dev}&= quot; -f
+ ;;
+esac
dif= f --git a/distro/dns-scripts/resolvconf_file-dns-updown.sh b/distro/dns-scr= ipts/resolvconf_file-dns-updown.sh
new file mode 100644
index 0000000..567b402
--- /dev/null+++ b/distro/dns-scripts/resolvconf_file-dns-updown.sh
@@ -0,0 +1,85 @@

+= #!/bin/sh
+#+# Simple OpenVPN up/down scr= ipt for modifying /etc/resolv.conf
+# (C) Copyright 2024 OpenVPN Inc <sales@...515...>
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Example env from openvpn (most are no= t applied):
+#=
+# dev tun0
+# script_type dns-up

= +# dns_search_domain_1 mycorp= .in
+# dns_search_d= omain_2 eu.mycorp.com
+# dns_server_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 5= 3
+# dns_server_1_r= esolve_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
+#
+
+set -e +u
+
+only_standard_server_ports() {
+ i=3D1
+ while true; do
+ eval addr=3D\"\$dns_server_${n}_address_${i}\"

+ [ -n "$addr&= quot; ] || return 0
+=
+ eval port= =3D\"\$dns_server_${n}_port_${i}\"
+ [ -z "$port" -o "$port"= ; =3D "53" ] || return 1
+
+ = i=3D$(expr $i + 1)
+}=
+
+conf=3D/etc/resolv.conf
+test -e "$conf" || exit 1

+case "${script_type}= " in
+dns-up)
+ n=3D1
+ while :; do

+ eval addr=3D\"\$dns_ser= ver_${n}_address_1\"
+ [ -n "$addr" ] || {
+ echo "setting DNS failed, no compa= tible server profile"
+ exit 1
+ }
+
+ # Skip server p= rofiles which require DNSSEC,
+ # secure transport or use a custom port
+ eval dnssec=3D\"\$dns_se= rver_${n}_dnssec\"
+ eval transport=3D\"\$dns_server_${n}_transport\"
+ [ -z "$tran= sport" -o "$transport" =3D "plain" ] \
<= span style=3D"color: hsl(120, 100%, 40%);">+ && [ -z &qu= ot;$dnssec" -o "$dnssec" =3D "no" ] \

+ && only_stan= dard_server_ports && break

+
+ = n=3D$(expr $n + 1)
+<= br>+ eval addr1=3D\"\$d= ns_server_${n}_address_1\"
+ eval addr2=3D\"\$dns_server_${n}_address_2\"
+ eval addr3=3D\"= \$dns_server_${n}_address_3\"
+ text=3D"### openvpn ${dev} begin ###\n"=
+ text=3D"${text}na= meserver $addr1\n"
+ test -z "$addr2" || text=3D"${text}nameserver $addr2= \n"
+ test -z= "$addr3" || text=3D"${text}nameserver $addr3\n"=
+
+ test -z "$dns_search_domain_1" = || {
+ for i i= n $(seq 1 6); do
+ = eval domains=3D\"$domains\$dns_search_domain_${i} \" || = break
+ done
+ text=3D"= ${text}search $domains\n"
+ }
+ = text=3D"${text}### openvpn ${dev} end ###"
+ text=3D"${text}\n$(cat ${conf})&= quot;
+
+ echo "${text}" >= "${conf}"

= + ;;
+dns-down)
+ sed -i'' -= e "/### openvpn ${dev} begin ###/,/### openvpn ${dev} end ###/d" = "$conf"
+ = ;;
+esac
= diff --git a/distro/dns-scripts/systemd-dns-updown.sh b/distro/dns-sc= ripts/systemd-dns-updown.sh
new file mode 100644
= index 0000000..ecadd29
--- /dev/null
= +++ b/distro/dns-scripts/systemd-dns-updown.sh
@@ -0,0 +1,2= 40 @@
+#!/bin/bash
+#
+# dns-updown - add/remove openvpn provid= ed DNS information
+#=
+# Copyright (C) 202= 4 OpenVPN Inc <sales@...515...>
+#
+= # SPDX-License-Identifier: GPL-2.0
+#
+# A= dd/remove openvpn DNS settings from the env into/from
+# the system. Supported backends in this = order:
+#
<= span style=3D"color: hsl(120, 100%, 40%);">+# * systemd-resolved
+# * resolvconf
<= span style=3D"color: hsl(120, 100%, 40%);">+# * /etc/resolv.conf file
+#
+# Example env from openvpn (not all are a= lways applied):
+#
+# dev tun0+# script_type dns-up=
+# dns_search_domain_1 my= corp.in
+# dns_sear= ch_domain_2 eu.mycorp.com
+# dns_server_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 D= oH
+# dns_server_1_= sni dns.mycorp.in
+#<= /span>
+
+function do_resolved_servers {+ local sni=3D""=
+ local transport= _var=3Ddns_server_${n}_transport
+ local sni_var=3Ddns_server_${n}_sni
+ [ "${!transport_var}" =3D &= quot;DoT" ] && sni=3D"#${!sni_var}"
+
+ local i=3D1
+ local addrs=3D""
+ while :; do
+ local addr_var=3Ddns_server_${n}_address_${i= }
+ local addr= =3D"${!addr_var}"
+ [ -n "$addr" ] || break
+
+ local port_var=3Ddns_server_${n}_port_${i}
+ if [ -n "${!port_var= }" ]; then

+ = if [[ "$addr" =3D~ : ]]; then
+ addr=3D"[$addr]"
+ fi+ addrs+=3D"${= addr}:${!port_var}${sni} "
+ else
+ addrs+=3D"${addr}${sni} "
+ fi
+ i=3D$((i+1))
+ done
+
+ = resolvectl dns "$dev" $addrs
+}
= +
+function do_resolv= ed_domains {
+ loc= al list=3D""
+ list+=3D"${!domain_var} &quo= t;
+ done+ local domain_var=3Ddns_s= erver_${n}_resolve_domain_1
+ if [ -z "${!domain_var}" ]; then
+ resolvectl default-route "$= dev" true
+ = list+=3D"~."
+ else
+ = resolvectl default-route "$dev" false
+ local i=3D1
+ while :; do
+ domain_var=3Ddns_server_${n}_res= olve_domain_${i}
+ = [ -n "${!domain_var}" ] || break
+ # Add as split domain (~ pref= ix), if it doesn't already exist
+ [[ "$list" =3D~ (^| )"${!domain= _var}"( |$) ]] \
+ || list+=3D"~${!domain_var} "
+ i=3D$((i+1))
+ done

+ fi
+
+ resolvectl domain "$dev" $list
+}
+
+function= do_resolved_dnssec {
+ local dnssec_var=3Ddns_server_${n}_dnssec
+ if [ "${!dnssec_var}" =3D "o= ptional" ]; then
+ resolvectl dnssec "$dev" allow-downgrade
+ elif [ "${!dnssec_var}&qu= ot; =3D "yes" ]; then

+ resolvectl dnssec "$dev" true
+ else

+ resolvectl dnssec "$dev" fal= se
+ fi
= +}
+
+function do_resolved_dnsovertls {
+ local transport_var=3Ddns_server_${n}_transport
+ if [ "${!tra= nsport_var}" =3D "DoT" ]; then
+ resolvectl dnsovertls "$dev" tr= ue
+ else+ resolvectl dnsovertl= s "$dev" false
+ fi
+}=
+
+function do_resolved {
+ [[ "$(readlink /etc/resolv.conf)= " =3D~ systemd ]] || return 1
+
+ = n=3D1
+ while :; d= o
+ local addr= _var=3Ddns_server_${n}_address_1
+ [ -n "${!addr_var}" ] || {
+ echo "setting DNS fa= iled, no compatible server profile"
+ return 1
+ }
+
+ = # Skip server profiles which require DNS-over-HTTPS
+ local transport_var=3Ddns_server_= ${n}_transport
+ = [ -n "${!transport_var}" -a "${!transport_var}" =3D = "DoH" ] || break
+
+ n=3D= $((n+1))
+ done
+
+ if [ "$script_type" =3D &qu= ot;dns-up" ]; then
+ echo "setting DNS using resolvectl"
+ do_resolved_servers+ do_resolved_domains
+ do_resolved_d= nssec
+ do_res= olved_dnsovertls
+ = else
+ echo &= quot;unsetting DNS using resolvectl"
+ resolvectl revert "$dev"
= + fi
+
+ return 0
+}
++function only_standard_server= _ports {
+ local i= =3D1
+ while :; do=
+ local addr_= var=3Ddns_server_${n}_address_${i}
+ [ -n "${!addr_var}" ] || return 0+
+ local port_var=3Ddns_server_${n}_port_$= {i}
+ [ -z &qu= ot;${!port_var}" -o "${!port_var}" =3D "53" ] || r= eturn 1
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ i=3D$((i+1))

= + done
+}
+
+fun= ction resolv_conf_compat_profile {
+ local n=3D1
+ while :; do
+ local server_addr_var=3Ddns_server_${n}_address_1
= + [ -n "${!server_a= ddr_var}" ] || {
+ echo "setting DNS failed, no compatible server profile&q= uot;
+ exi= t 1
+ }=
+
+ # Skip server profiles which require = DNSSEC,
+ # se= cure transport or use a custom port
+ local dnssec_var=3Ddns_server_${n}_dnssec+ local transport_var= =3Ddns_server_${n}_transport
+ [ -z "${!transport_var}" -o "${!transport_v= ar}" =3D "plain" ] \
+ && [ -z "${!dnssec_var}" -o &= quot;${!dnssec_var}" =3D "no" ] \
+ && only_standard_server_po= rts && break
= +
+ n=3D$((n+1= ))
+ done+ return $n
+}

+
+function do_resolvconf {
+ [ -x /sbin/resolvconf ] || return 1
+
+ resolv_conf_compat_profile
+ local n=3D$?
+
+ = if [ "$script_type" =3D "dns-up" ]; then
<= span style=3D"color: hsl(120, 100%, 40%);">+ echo "setting DNS = using resolvconf"

+ for domain_var in ${!dns_search_domain_*}; do
+ domains+= =3D"${!domain_var} "
+ done
+ {
+ = local maxns=3D3
+ local server_var=3Ddns_server_${n}_address_*
+ for addr_var in ${!serve= r_var}; do
+ = [ $((maxns--)) -gt 0 ] || break
+ echo "nameserver ${!addr_var}"=
+ done
+ [ -z "= ;$domains" ] || echo "search $domains"
+ } | /sbin/resolvconf -a "$de= v"
+ else
+ echo "unse= tting DNS using resolvconf"
+ /sbin/resolvconf -d "$dev"
+ fi
+
+ return 0
+}=
+
+function do_resolv_conf_file {<= br>+ conf=3D/etc/resolv.conf=
+ test -e "$= conf" || exit 1
= +
+ resolv_conf_co= mpat_profile
+ loc= al n=3D$?
++ if [ "$script_type&q= uot; =3D "dns-up" ]; then
+ echo "setting DNS using resolv.conf file"=
+
+ local addr1_var=3Ddns_server_$= {n}_address_1
+ = local addr2_var=3Ddns_server_${n}_address_2
+ local addr3_var=3Ddns_server_${n}_address= _3
+ text=3D&q= uot;### openvpn ${dev} begin ###\n"
+ text=3D"${text}nameserver ${!addr1_var}\n= "
+ test = -z "${!addr2_var}" || text=3D"${text}nameserver ${!addr2_var= }\n"
+ te= st -z "${!addr3_var}" || text=3D"${text}nameserver ${!addr3_= var}\n"
+=
+ test -z "$dns= _search_domain_1" || {
+ for i in $(seq 1 6); do
+ eval domains=3D\"$domains\$dns= _search_domain_${i} \" || break
+ done
+ text=3D"${text}search $domains\n"
+ }
+ text=3D"${text}### ope= nvpn ${dev} end ###"

+
+ sed -= i "1i${text}" "$conf"
+ else
+ echo "unsetting DNS using resolv.conf file"
+ sed -i "/= ### openvpn ${dev} begin ###/,/### openvpn ${dev} end ###/d" "$co= nf"
+ fi
+
+ return 0
+}
+
+do_resolve= d || do_resolvconf || do_resolv_conf_file || {
+ echo "setting DNS failed, no method suc= ceeded"
+ exi= t 1
+}
diff --git a/doc/man-sections/script-options.rst b/doc/man-sections/scrip= t-options.rst

index e48710b..bd5ecd4 100644
--- a/doc/man-sections/script-options.rst

+++ b/doc/man-se= ctions/script-options.rst
@@ -8,9 +8,13 @@
= Script Order of Execution
-------------------------

+#. ``--= dns-updown``
+=
+ Executed after TCP/UDP = socket bind and TUN/TAP open, before ``--up``.
+
#. ``--up``
<= /span>
- Executed after TCP/= UDP socket bind and TUN/TAP open.
+ Executed after TCP/UDP socket bind and TUN/TAP open, after= ``--dns-updown``.

#. ``--tls-verify``

@@ -38,9 +42,13 @@

Executed in ``--mode server`` mode on client instance shutdo= wn.

+#. ``--dns-updown``
+
+ Executed befor= e TCP/UDP and TUN/TAP close, before ``--down``.
+
#. ``--down``

- Executed after T= CP/UDP and TUN/TAP close.
+ Executed after TCP/UDP and TUN/TAP close, after ``--dns-updown``.<= /span>

#. ``--learn-address``
=
@@ -171,7 +179,7 @@
client-crresp= onse cmd

OpenVPN will write the respon= se of the client into a temporary file.
- The filename will be passed as an argument to ``cmd``, = and the file will be
= + The filename will be passed as an argument to ``cmd``, and the file will=
automatically deleted by OpenVPN after the script retur= ns.

The response is passed as is from = the client. The script needs to check
@@ -233,6 +241,31 @@<= /span>
The ``--client-disconnect`` command is not passed any ex= tra arguments
(only those arguments specified in cmd, if= any).

+--dns-updown cmd
+ Run command ``cmd``, instead of the default DNS up/down command that co= mes
+ with openvpn. = If ``cmd`` is ``disable`` the ``--dns-updown`` command is not run.+
+ If you write your own command, please make su= re to ignore ``--dns``
+ 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 the server addresses or search domains

+ can be configured, apply them in the order = they are listed 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 th= ey are
+ available t= o this hook if no ``--dns`` options exist. If any ``--dns server``+ option is present, DNS-rel= ated ``--dhcp-option``\ s will always be ignored.
+ If an ``--up`` script is defined, foreign_o= ption env vars will be generated
+ from ``--dns`` options and passed to the script. The default= ``--dns-updown``
+ = command is not run if an ``--up`` script is defined. Both is done for backw= ard
+ compatibility.= In case you want to run the ``--dns-updown`` command even if
+ there is an ``--up`` defined, y= ou can define a custom command or use ``force``

+ as ``cmd`` to run the default command. No DNS= env vars will be passed to ``--up``
+ in this case.
+
--down cmd
Run command ``= cmd`` after TUN/TAP device close (post ``--user`` UID
ch= ange and/or ``--chroot`` ). ``cmd`` consists of a path to script (or=
@@ -659,7 +692,7 @@
names). Set prior to ``-= -up`` or ``--down`` script execution.

:c= ode:`dns_*`
- The ``= --dns`` configuration options will be made available to script
+ The ``--dns`` configuration = options will be made available to ``--dns-updown``

exe= cution through 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 t= o the documentation for ``--dns``.
diff --git a/src/openvpn= /Makefile.am b/src/openvpn/Makefile.am
index 37af683..2e93e= bb 100644
--- a/src/openvpn/Makefile.am
+++= b/src/openvpn/Makefile.am
@@ -30,7 +30,8 @@
$(OPTIONAL_LZ4_CFLAGS) \

$(OPTIONAL_PKCS11_HELPER_CFL= AGS) \
$(OPTIONAL_INOTIFY_CFLAGS) \
- -DPLUGIN_LIBDIR=3D\"${plugindir}\&quo= t;
+ -DPLUGIN_LIBDIR= =3D\"${plugindir}\" \
+ -DDEFAULT_DNS_UPDOWN=3D\"${scriptdir}/dns-updown\"

if WIN32
# we want unico= de entry point but not the macro
diff --git a/src/openvpn/d= ns.c b/src/openvpn/dns.c
index b6e524f..4da0747 100644
--- a/src/openvpn/dns.c
+++ b/src/openvpn/dns.c=
@@ -30,6 +30,7 @@
#include "dns.h&qu= ot;
#include "socket.h"
#includ= e "options.h"
+#include "run_command.h"

#= ifdef _WIN32
#include "win32.h"
= @@ -262,6 +263,8 @@
clone.search_domains =3D clone_dns= _domains(o->search_domains, gc);
clone.servers =3D = clone_dns_servers(o->servers, gc);
clone.servers_pr= epull =3D clone_dns_servers(o->servers_prepull, gc);
+ clone.updown =3D o->updown;
+ clone.user_set_updow= n =3D o->user_set_updown;

return = clone;
}
@@ -548,6 +551,54 @@
send_msg_iservice(o->msg_channel, &nrpt, sizeof(nrpt), &= ;ack, "DNS");

}

+#else /* ifdef _WIN32 */
+
+static void
+updown_env_set(bool up, const struct dns_options *o, con= st struct tuntap *tt, struct env_set *es)
+{
+ setenv_str(es, "dev", tt->actual_name);
+ setenv_str(es, "script_ty= pe", up ? "dns-up" : "dns-down");

+ setenv_dns_options(o, es);
+}
+
+static int
+do_run_up_down_command(bool up, const struct dns_options *o, const st= ruct tuntap *tt)
+{
+ struct gc_arena g= c =3D gc_new();
+ = struct argv argv =3D argv_new();
+ struct env_set *es =3D env_set_create(&gc);
= +
+ updown_env_set(up, o, tt, es);
+

+ argv_printf(&argv, "%s", o->updo= wn);
+ argv_msg(M_= INFO, &argv);
+ = int res;
+ if (o= ->user_set_updown)
+ {
+ res = =3D openvpn_run_script(&argv, es, S_EXITCODE, "dns updown");<= /span>
+ }
+ else

+ {
+ res =3D openvpn_execve_check(&argv, es, S_EXITC= ODE, "WARNING: Failed running dns updown");
+ }
+ argv_free(&argv);
+ gc_free(&gc);
+ return res;
+}
+
+static void
+run_up_down_command(b= ool up, struct options *o, const struct tuntap *tt)
+{
+ if (!o->dns_options.updown)
+ {
+ return;
+ }
+<= /span>
+ 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 @@<= br>
#ifdef _WIN32
run_up_down_= service(up, o, tt);
+= #else
+ run_up_dow= n_command(up, o, tt);
#endif /* ifdef _WIN32 */
= }
diff --git a/src/openvpn/dns.h b/src/openvpn/dns.h=
index f24e30b..c4d19ff 100644
--- a/src/op= envpn/dns.h
+++ b/src/openvpn/dns.h
@@ -73,= 6 +73,8 @@
struct dns_server *servers_prepull;<= br> struct dns_server *servers;
struct gc_ar= ena gc;
+ const ch= ar *updown;
+ bool= user_set_updown;
};

/*= *
diff --git a/src/openvpn/options.c b/src/openvpn/options.= c
index 02970a7..1d1daea 100644
--- a/src/o= penvpn/options.c
+++ b/src/openvpn/options.c
@@ -526,10 +526,12 @@

" addres= s <addr[:port]> [addr[:port] ...] : server addresses 4/6\n"
" resolve-domains <domain> [do= main ...] : split domains\n"
" = dnssec <yes|no|optional> : option to use DNSSEC\n"- " typ= e <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> [dom= ain ...]:\n"
" Add domains = to DNS domain search list\n"
+ "--dns-updown cmd|force|disable : Run cmd as user de= fined dns config command,\n"
+ " force running the default script = or disable running it.\n"
"--auth-retry t := How to handle auth failures. Set t to\n"
"= none (default), interact, or nointeract.\n" "--static-challenge t e [<scrv1|concat>]: Enable st= atic challenge/response protocol using\n"
@@ -922,6 +9= 24,10 @@
#ifndef ENABLE_DCO
o->dis= able_dco =3D true;
#endif /* ENABLE_DCO */
+

+#ifdef ENABLE_DNS_UPDOWN_BY_DEFAULT
+ o->dns_options.updown =3D DEFAU= LT_DNS_UPDOWN;
+#endi= f /* ENABLE_DNS_UPDOWN_BY_DEFAULT */
}

void
@@ -8088,6 +8094,39 @@
to->ip_win32_defined =3D true;

} #endif /* ifdef _WIN32 */
+ else if (streq(p[0], "dns-updown") && = p[1])
+ {+ VERIFY_PERMISSION(OP= T_P_SCRIPT);
+ = if (!no_more_than_n_args(msglevel, p, 2, NM_QUOTE_HINT))
+ {
+ goto err;
+ }
+ struct dns_options *dns =3D &options->dn= s_options;
+ i= f (streq(p[1], "disable"))
+ {
+ dns->updown =3D NULL;
+ dns->user_set_updown =3D false;
+ }
+ else if (streq(p[1], "= ;force"))

+ = {
+ /* = force dns-updown run, even if a --up script is defined */
+ if (dns->user_set_updo= wn =3D=3D false)
+ = {
+ = dns->updown =3D DEFAULT_DNS_UPDOWN;
+ dns->user_set_updown =3D true;=
+ }
+ }
+ else

+ {
+ if (streq(dns->updown, DEFAULT_DNS_U= PDOWN))
+ = {
+ /*= Unset the default command to prevent warnings */
+ dns->updown =3D NULL;
+ }
= + set_user_script(op= tions, &dns->updown, p[1], p[0], false);
+ dns->user_set_updown =3D true;
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

else= if (streq(p[0], "dns") && p[1])
{
VERIFY_PERMISSION(OPT_P_DHCPDNS);
<= /span>

To view, visit change 838. To unsubscribe, or for help writin= g mail filters, visit settin= gs.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifbe4ffb44d3bfcaa50adb38cacb= 3436fcdc71b10
Gerrit-Change-Number: 838
Gerrit-PatchSet: 25
Gerrit-Owner: d12fk <heiko@...515...>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: cron2 <gert@...1296...>= ;
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-Attention: d12fk <heiko@...515...>= ;
Gerrit-MessageType: newpatchset
--V4q0xDr0Sgc=--