From: "cron2 (Code Review)" Date: Wed, 14 May 2025 17:30:43 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., heiko@...515..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="izk9PtA/vPY="; 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: d12fk , plaisthos , flichtenheld Cc: openvpn-devel --izk9PtA/vPY= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#26) to the change originally created b= y d12fk. ( http://gerrit.openvpn.net/c/openvpn/+/838?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 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 Acked-by: Gert Doering Message-Id: <20250514135334.14377-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg31= 639.html Signed-off-by: Gert Doering --- 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/26 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 =20 +/* 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 @@ 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 3c1a875..a6f2dc4 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 55b923c..3c1632f 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" @@ -918,6 +920,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 @@ -8046,6 +8052,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: 26 Gerrit-Owner: d12fk Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-MessageType: newpatchset --izk9PtA/vPY= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 uploaded patch set #26 to the change originally c= reated by d12fk.

View Change

The following approvals got outdated an= d were removed: Code-Review+2 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...>
Acked-by: Gert Doering <gert@...1296...>=
Message-Id: <20250514135334.14377-1-gert@...1296...>
URL: http= s://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg31639.html<= br>Signed-off-by: Gert Doering <gert@...1296...>
---
M .gitigno= re
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/ha= ikuos_file-dns-updown.sh
A distro/dns-scripts/openresolv-dns-updown.shA distro/dns-scripts/resolvconf_file-dns-updown.sh
A distro/dns-script= s/systemd-dns-updown.sh
M doc/man-sections/script-options.rst
M src/o= penvpn/Makefile.am
M src/openvpn/dns.c
M src/openvpn/dns.h
M src/o= penvpn/options.c
15 files changed, 707 insertions(+), 10 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/38/838=
/26
diff --git a/.gitignore b/.gitignore
index db8b= b73..04523af 100644
--- a/.gitignore
+++ b/= .gitignore
@@ -49,6 +49,7 @@
/doc/doxygen/= latex/
/doc/doxygen/openvpn.doxyfile
dist= ro/systemd/*.service
= +distro/dns-scripts/dns-updown
sample/sample-keys/sample-c= a/
vendor/cmocka_build
vendor/dist=
diff --git a/CMakeLists.txt b/CMakeLists.txt
inde= x 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 u= se actual cross compile/Android cmake to build for simple compile checks on= Linux")

-set(PLUGIN_DIR /usr/local/lib/openvpn/plugins CACHE FILEPATH &quo= t;Location of the plugin directory")
+option(ENABLE_DNS_UPDOWN_BY_DEFAULT "Run --dns-u= pdown hook by default" ON)
+set(DNS_UPDOWN_PATH "${CMAKE_INSTALL_PREFIX}/libexec/openv= pn/dns-updown" CACHE STRING "Default location for the DNS up/down= script")
+
+set(PLUGIN_DIR "${C= MAKE_INSTALL_PREFIX}/lib/openvpn/plugins" CACHE FILEPATH "Locatio= n of the plugin directory")

# Creat= e machine readable compile commands
option(ENABLE_COMPILE_= COMMANDS "Generate compile_commands.json and a symlink for clangd to f= ind it" OFF)
@@ -601,6 +604,8 @@

add_library_deps(openvpn)

+target_compile_options(openvpn PRIVAT= E -DDEFAULT_DNS_UPDOWN=3D\"${DNS_UPDOWN_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
i= ndex 2f7b43d..5164ce3 100644
--- a/config.h.cmake.in=
+++ b/config.h.cmake.in
@@ -35,6 +35,9 @@<= br> /* Enable LZO compression library */
#cmakedefin= e ENABLE_LZO

+/* Enable dns-updown script hook */
+#cmakedefine ENABLE_DNS_UPDOWN
+
/* Enable NTLMv2 pr= oxy 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-b= y-default], [disable running --dns-updown by default @<:@default=3Dyes@:= >@])],
+ ,<= br>+ [enable_dns_updown_by_defa= ult=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 s= cript directory @<:@default=3DPKGLIBEXECDIR@:>@])
+if test -n "${SCRIPTDIR}"; th= en
+ scriptdir=3D&quo= t;${SCRIPTDIR}"
= +else
+ scriptdir=3D&= quot;\${pkglibexecdir}"
+fi
+<= br> AC_DEFINE_UNQUOTED([TARGET_ALIAS], ["${host}"], [A stri= ng representing our host])
-AM_CONDITIONAL([TARGET_LINUX], [false])
+AM_CONDITIONAL([ENABLE_DNS_UPDOWN],[true])<= br> case "$host" in
*-*-linux*) AC_DEFINE([TARGET_LINUX], [1], [Are we running on Linux?])=
- AM_CONDITIONAL([TARGET_LIN= UX], [true])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["= L"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["systemd"])
have_sitnl=3D"yes"
pkg_config_required=3D&= quot;yes"
;;
*-*-solaris*)=
AC_DEFINE([TARGET_SOLARIS], [1], [Are we running on Solaris?])=
AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["S"], [T= arget prefix])
+ AC_= SUBST([DNS_UPDOWN_TYPE], ["resolvconf_file"])
= CPPFLAGS=3D"$CPPFLAGS -D_XPG4_2"
test -x /bin/= bash && SHELL=3D"/bin/bash"
;;<= br> *-*-openbsd*)
AC_DEFINE([TARGET_OPENBSD], [1]= , [Are we running on OpenBSD?])
AC_DEFINE_UNQUOTED([TARG= ET_PREFIX], ["O"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["resolvconf_f= ile"])
;;
*-*-freebsd*) AC_DEFINE([TARGET_FREEBSD], [1], [Are we running on FreeBSD?])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["F"], [Targ= et prefix])
+ AC_SUB= ST([DNS_UPDOWN_TYPE], ["openresolv"])
;;

*-*-netbsd*)
AC_DEFINE([TARGET_NETBSD], [1]= , [Are we running NetBSD?])
AC_DEFINE_UNQUOTED([TARGET_P= REFIX], ["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_ty= pe_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 ena= bled on Windows])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], [&= quot;W"], [Target prefix])
+ AM_CONDITIONAL([ENABLE_DNS_UPDOWN], [false])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["= ;windows"])
CPPFLAGS=3D"${CPPFLAGS} -DWIN32_LE= AN_AND_MEAN"
CPPFLAGS=3D"${CPPFLAGS} -DNTDDI_V= ERSION=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&quo= t;])
;;
*-aix*)
AC_= DEFINE([TARGET_AIX], [1], [Are we running AIX?])
AC_DEFI= NE_UNQUOTED([TARGET_PREFIX], ["A"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], [&= quot;resolvconf_file"])
ROUTE=3D"/usr/sbin/rou= te"
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 pref= ix])
+ AC_SUBST([DNS= _UPDOWN_TYPE], ["haikuos_file"])
LIBS=3D"= ${LIBS} -lnetwork"
;;
*) AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["X"], [Target pre= fix])
+ AC_SUBST([DN= S_UPDOWN_TYPE], ["resolvconf_file"])
have_tap_= header=3D"yes"
;;
esac=
@@ -1317,7 +1343,7 @@
test "${enable_small}= " =3D "yes" && AC_DEFINE([ENABLE_SMALL], [1], [Enabl= e smaller executable size])
test "${enable_fragment}&= quot; =3D "yes" && AC_DEFINE([ENABLE_FRAGMENT], [1], [Ena= ble internal fragmentation support])
test "${enable_p= ort_share}" =3D "yes" && AC_DEFINE([ENABLE_PORT_SHAR= E], [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 ho= ok by default])
test "${enable_ntlm}" =3D "= yes" && AC_DEFINE([ENABLE_NTLM], [1], [Enable NTLMv2 proxy sup= port])
test "${enable_crypto_ofb_cfb}" =3D "= ;yes" && AC_DEFINE([ENABLE_OFB_CFB_MODE], [1], [Enable OFB and= CFB cipher modes])
if test "${have_export_keying_mat= erial}" =3D "yes"; then
@@ -1505,6 +1531,7 @= @

sampledir=3D"\$(docdir)/sample&qu= ot;
AC_SUBST([plugindir])
+AC_SUBST([scriptdir])
AC_SUBST([sam= pledir])

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/M= akefile.am b/distro/Makefile.am
index 7a588da..26f577b 1006= 44
--- a/distro/Makefile.am
+++ b/distro/Ma= kefile.am
@@ -13,3 +13,7 @@
$(srcdir)/Mak= efile.in

SUBDIRS =3D systemd
<= span style=3D"color: hsl(120, 100%, 40%);">+
+if ENABLE_DNS_UPDOWN
+SUBDIRS +=3D dns-scripts
+endif
diff --git a/distro/dn= s-scripts/Makefile.am b/distro/dns-scripts/Makefile.am
new = file mode 100644
index 0000000..fc2db08
---= /dev/null
+++ b/distro/dns-scripts/Makefile.am
<= span>@@ -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 a= uthentication, and
+#= packet compression.
+#
+# Co= pyright (C) 2002-2024 OpenVPN Inc <sales@...515...>
+#
+
= +MAINTAINERCLEANFILES =3D \
+ $(srcdir)/Makefile.in
+
+EXTRA_DI= ST =3D \
+ systemd-dn= s-updown.sh \
+ openr= esolv-dns-updown.sh \
+ haikuos_file-dns-updown.sh \
+ resolvconf_file-dns-updown.sh
+
+script_SCRIPTS =3D \
+ dns-updown
+
+CLEANFILES =3D $(scrip= t_SCRIPTS)
++dns-updown: @DNS_UPDOWN_TYPE= @...2841...
+ cp ${sr= cdir}/@DNS_UPDOWN_TYPE@...2841... $@
+ chmod +x $@
+
+all: $(sc= ript_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
+#
+# Si= mple OpenVPN up/down script for modifying Haiku OS resolv.conf
+# (C) Copyright 2024 OpenVPN Inc= <sales@...515...>
+#
+# SPDX-Licens= e-Identifier: BSD-2-Clause
+#
+# Example e= nv from openvpn (most are not applied):
+#
+# dev tun0
+# s= cript_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_addres= s_2 fd00::99:53
+# = dns_server_1_port_1 53
+# dns_server_1_resolve_domain_1 mycorp.in
+# dns_server_1_resolve_domain_2 eu.myco= rp.com
+# dns_serve= r_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 "$por= t" -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&quo= t;
+ exit = 1
+ }+
+ # Skip server profiles which require DN= SSEC,
+ # secu= re transport or use a custom port
+ eval dnssec=3D\"\$dns_server_${n}_dnssec\"<= /span>
+ eval transpo= rt=3D\"\$dns_server_${n}_transport\"
+ [ -z "$transport" -o "$tr= ansport" =3D "plain" ] \
+ && [ -z "$dnssec" -o &quo= t;$dnssec" =3D "no" ] \
+ && only_standard_server_ports &&= amp; break
++ n=3D$(expr $n + 1)
+ done
+

+ eval addr1=3D\"\$dns_server_${n}_address_1= \"
+ eval add= r2=3D\"\$dns_server_${n}_address_2\"
+ eval addr3=3D\"\$dns_server_${n}_addres= s_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 $domai= ns\n"
+ }
+ text=3D"${text= }### openvpn ${dev} end ###"
+ text=3D"${text}\n$(cat ${conf})"
+

+ echo "${text}" > "${conf}"=
+ ;;
+dns-down)
+ sed -i'' -e "/### openvp= n ${dev} begin ###/,/### openvpn ${dev} end ###/d" "$conf"
+ ;;
+esac

diff --git a/= distro/dns-scripts/openresolv-dns-updown.sh b/distro/dns-scripts/openresolv= -dns-updown.sh
new file mode 100644
index 0= 000000..e50398c
--- /dev/null
+++ b/distro/= dns-scripts/openresolv-dns-updown.sh
@@ -0,0 +1,89 @@

+#!/bin/sh
+#
+# Simple OpenVPN up/down script for openresolv integr= ation
+# (C) Copyrigh= t 2016 Baptiste 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.com
+# dn= s_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_d= omain_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.myco= rp.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$(ex= pr $i + 1)
+ done<= /span>
+}
+
+: ${script_type:=3Ddns-down}
+case "${script_type}" in
+dns-up)

+ n=3D1
+ while :; do
+ eval addr=3D\"\$dns_server_${n}_address_1\&quo= t;
+ [ -n &quo= t;$addr" ] || {
= + echo "setting DNS failed, no compatible server profile&qu= ot;
+ exit= 1
+ }<= br>+
+ # Skip server profiles which require D= NSSEC,
+ # sec= ure transport or use a custom port
+ eval dnssec=3D\"\$dns_server_${n}_dnssec\"=
+ eval transp= ort=3D\"\$dns_server_${n}_transport\"
+ [ -z "$transport" -o "$t= ransport" =3D "plain" ] \
+ && [ -z "$dnssec" -o &qu= ot;$dnssec" =3D "no" ] \
+ && only_standard_server_ports &= & break
+<= br>+ n=3D$(expr $n + 1)<= /span>
+ done
<= span style=3D"color: hsl(120, 100%, 40%);">+

+ {
+ i=3D1
+ maxns=3D3
+ [ $maxns -gt 0 ] || break
+ eval option=3D\"\$dns_= server_${n}_address_${i}\" || break
+ [ "${option}" ] || break<= br>+ i=3D$((i + 1))<= /span>
+ echo &qu= ot;nameserver ${option}"
+ done
+ = maxdom=3D6
+ = while :; do
+ = maxdom=3D$((maxdom - 1))
+ [ $maxdom -gt 0 ] || break
+ eval option=3D\"\$dns_se= arch_domain_${i}\" || break
+ [ "${option}" ] || break
+ i=3D$((i + 1))
+ echo "searc= h ${option}"
+ = done
+ } | /= sbin/resolvconf -a "${dev}"
+ ;;
+dns-down)
+ /= sbin/resolvconf -d "${dev}" -f
+ ;;
+esac
diff --git a/distro/dns-scripts/resolvconf_fil= e-dns-updown.sh b/distro/dns-scripts/resolvconf_file-dns-updown.shnew file mode 100644
index 0000000..567b402<= br>--- /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 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
+}
+
+conf=3D/etc/r= esolv.conf
+test -e &= quot;$conf" || exit 1
+case "${script_type}" in
+dns-up)
+ n=3D1
+= while :; do
+ = eval addr=3D\"\$dns_server_${n}_address_1\"
+ [ -n "$addr" ] || {=
+ echo &q= uot;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_s= erver_${n}_transport\"
+ [ -z "$transport" -o "$transport" =3D &= quot;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\"
<= span style=3D"color: hsl(120, 100%, 40%);">+ eval addr2=3D\"\$dns_s= erver_${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\$d= ns_search_domain_${i} \" || break
+ done
+ text=3D"${text}search $domains\n"
<= span style=3D"color: hsl(120, 100%, 40%);">+ }
+ text=3D"${text}### openvpn ${dev} en= d ###"
+ 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/syst= emd-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-updow= n.sh
@@ -0,0 +1,240 @@
+#!/bin/bash
+#
+# dns-u= pdown - add/remove openvpn provided DNS information
+#
+# Copyright (C) 2024 OpenVPN Inc <sales@...515...>

+#
+# SPDX-License-Identifier: GPL-2.0

+#
+# Add/remove openvpn DNS settings from th= e env into/from
+# th= e system. Supported backends in this order:
+#
+# * systemd-resolved
+# * resolvconf
+# * /etc/resolv.conf file
+#
+# Ex= ample 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
<= span style=3D"color: hsl(120, 100%, 40%);">+# dns_server_1_address_1 192.= 168.99.254
+# dns_s= erver_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<= br>+# dns_server_1_resolve_do= main_2 eu.mycorp.com
= +# dns_server_1_dnssec true
+# dns_server_1_transport DoH
+# dns_server_1_sni dns.mycorp.in
+#
+
+f= unction 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&quo= t;#${!sni_var}"
= +
+ local i=3D1

+ local addrs=3D&quo= t;"
+ while := ; do
+ local a= ddr_var=3Ddns_server_${n}_address_${i}
+ local addr=3D"${!addr_var}"
= + [ -n "$addr"= ] || break
+<= br>+ local port_var=3Ddn= s_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"${add= r}${sni} "
+ = fi
+ i=3D$= ((i+1))
+ done
+
+ resolvectl dns "$dev" $addr= s
+}
+
+function do_resolved_domains {
+ local list=3D""
+ for domain_var in ${!dns_searc= h_domain_*}; do

+ = list+=3D"${!domain_var} "
+ done
+ local domain_var=3Ddns_server_${n}_resolve_domain_1
= + if [ -z "${!domain_va= r}" ]; then
+ = resolvectl default-route "$dev" true
+ list+=3D"~."
= + else
+ resolvectl default-route "$d= ev" false
+ = local i=3D1
+ = while :; do
+ = domain_var=3Ddns_server_${n}_resolve_domain_${i}
+ [ -n "${!domain_var}&q= uot; ] || 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&q= uot; $list

+}<= br>+
+function do_resolved_dnssec {
+ local dnssec_var=3Ddns_server_${= n}_dnssec
+ if [ &= quot;${!dnssec_var}" =3D "optional" ]; then
+ resolvectl dnssec "$dev= " allow-downgrade
+ resolvectl dnssec = "$dev" true
+ else
+ r= esolvectl dnssec "$dev" false
+ fi
+}
+
= +function do_resolved_dnsovertl= s {
+ local transp= ort_var=3Ddns_server_${n}_transport
+ if [ "${!transport_var}" =3D "DoT" = ]; then
+ reso= lvectl 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 "${!ad= dr_var}" ] || {
= + echo "setting DNS failed, no compatible server profile&qu= ot;
+ retu= rn 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 "dns-up" ]; then
+ echo "setting DNS u= sing resolvectl"

+ do_resolved_servers
+ do_resolved_domains
+ do_resolved_dnssec
+ do_resolved_dnsovertls
+ else
+ echo "unsetting DNS using resolvectl&= quot;
+ resolv= ectl revert "$dev"
+ fi
+

+ return 0
= +}
+
+function only_standard_server_ports {
+ local i=3D1
+ while :; do
+ local addr_var=3Ddns_server_${n}_address_${i}
+ [ -n "${!a= ddr_var}" ] || return 0
+
+ lo= cal 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<= br>+ while :; do
<= span style=3D"color: hsl(120, 100%, 40%);">+ 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<= /span>
+ local dnssec= _var=3Ddns_server_${n}_dnssec
+ local transport_var=3Ddns_server_${n}_transport+ [ -z "${!transpo= rt_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_profi= le
+ local n=3D$?<= /span>
+
+ if [ "$script_type" =3D &= quot;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_se= rver_${n}_address_*
+= for addr_var in ${!server_var}; do
+ [ $((maxns--)) -gt 0 ] || break<= /span>
+ 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

+}
+
+fu= nction 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 "sett= ing DNS using resolv.conf file"
+
+ = local addr1_var=3Ddns_server_${n}_address_1
+ local addr2_var=3Ddns_server_${n}_add= ress_2
+ local= addr3_var=3Ddns_server_${n}_address_3
+ text=3D"### openvpn ${dev} begin ###\n"= ;
+ text=3D&qu= ot;${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" || {
<= span style=3D"color: hsl(120, 100%, 40%);">+ 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 ###"
+

+ sed -i "1i${text}" "$conf&quo= t;
+ else+ echo "unsetting= DNS using resolv.conf file"
+ sed -i "/### openvpn ${dev} begin ###/,/### open= vpn ${dev} end ###/d" "$conf"
+ fi
+
+ ret= urn 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-op= tions.rst b/doc/man-sections/script-options.rst
index e4871= 0b..bd5ecd4 100644
--- a/doc/man-sections/script-options.rs= t
+++ 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.<= br>+ Executed after TCP/UDP s= ocket bind and TUN/TAP open, after ``--dns-updown``.

#. ``--tls-verify``

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

Executed in ``--mode serve= r`` mode on client instance shutdown.

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

- Executed after TCP/UDP and TUN/TAP close.
+ Executed after TCP/UDP and TUN/T= AP 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 argu= ment 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-disconn= ect`` command is not passed any extra arguments
(only th= ose arguments specified in cmd, if any).

+--dns-updown cmd
+ Run command ``cmd``, instead of the d= efault DNS up/down command that comes
+ with openvpn. If ``cmd`` is ``disable`` the ``--dns-upd= own`` command is not run.
+
+ If you writ= e your own command, please make sure to ignore ``--dns``
+ server profiles that cannot be appli= ed. Port, DNSSEC and secure transport
+ settings need to be adhered to. If split DNS is not pos= sible a full redirect
+ can be used as a fallback. If not all of the server addresses or search= domains
+ can be co= nfigured, apply them in the order they are listed in.
+
+ Note that ``--dns-updown`` is not supported on all platfor= ms. On Windows DNS
+ = will always be set by the service. On Android DNS will be passed via manag= ement
+ interface.
+
+ Note that DNS-related ``--dhcp-option`= `\ s might be converted so that they are
+ available to this hook if 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 vars will be generated+ from ``--dns`` options and= passed to the script. The default ``--dns-updown``
+ command is not run if an ``--up`` scrip= t is defined. Both is done for backward
+ compatibility. In case you want to run the ``--dns-up= down`` command even if
+ as ``cmd`= ` to run the default command. No DNS env vars will be passed to ``--up``

+ in this case.=
+
--down c= md
Run command ``cmd`` after TUN/TAP device close (post = ``--user`` UID
change and/or ``--chroot`` ). ``cmd`` con= sists 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 m= ade available to script
+ The ``--dns`` configuration options will be made available to ``--d= ns-updown``
execution 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 to the documentation for ``--dns``.
diff --git a/src/openvpn/Makefile.am b/src/openvpn/Makefile.am<= /span>
index 3c1a875..a6f2dc4 100644
--- a/src/ope= nvpn/Makefile.am
+++ b/src/openvpn/Makefile.am
@@ -30,7 +30,8 @@

$(OPTIONAL_LZ4_CFLAGS) \
= $(OPTIONAL_PKCS11_HELPER_CFLAGS) \
$(OPTIONAL_INO= TIFY_CFLAGS) \
- -DPLUG= IN_LIBDIR=3D\"${plugindir}\"
+ -DPLUGIN_LIBDIR=3D\"${plugindir}\" \+ -DDEFAULT_DNS_UPDOWN=3D\&qu= ot;${scriptdir}/dns-updown\"

if WIN= 32
# we want unicode entry point but not the macro<= br>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 "sock= et.h"
#include "options.h"
+#include "run_command.h"

#ifdef _WIN32
#include &= quot;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->server= s_prepull, gc);
+ = clone.updown =3D o->updown;
+ clone.user_set_updown =3D o->user_set_updown;
<= span>
return clone;
}
@@ -548,6 +551,54 @@
send_msg_iservice(o->msg_cha= nnel, &nrpt, sizeof(nrpt), &ack, "DNS");
= }

= +#else /* ifdef _WIN32 */
+
+static void
+updown_env_set(bool u= p, 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" : &quo= t;dns-down");
+ = setenv_dns_options(o, es);
+}
+<= br>+static int
+do_run_up_down_command(bool up, cons= t 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 en= v_set_create(&gc);
+ updown_env_s= et(up, o, tt, es);
+<= /span>
+ 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_EX= ITCODE, "dns updown");
+ }
+ = else
+ {=
+ res =3D openvpn_ex= ecve_check(&argv, es, S_EXITCODE, "WARNING: Failed running dns upd= own");
+ }
+ argv_free(&arg= v);
+ gc_free(&= ;gc);
+ return res= ;
+}
+
+static void
+run_up_down_command(bool up, struct options *o, const struct tu= ntap *tt)
+{+ if (!o->dns_options.u= pdown)
+ {<= br>+ return;
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+
+ int status;
+ status =3D do_run_up_down_command(up, &o->dns_options, tt= );
+ msg(M_INFO, &= quot;dns %s command exited with status %d", up ? "up" : &quo= t;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);
#endi= f /* ifdef _WIN32 */
}
diff --git a/src/op= envpn/dns.h b/src/openvpn/dns.h
index f24e30b..c4d19ff 1006= 44
--- a/src/openvpn/dns.h
+++ b/src/openvp= n/dns.h
@@ -73,6 +73,8 @@
struct dns_s= erver *servers_prepull;
struct dns_server *servers;
struct gc_arena gc;
+ const char *updown;
+ bool user_set_updown;
};=

/**
diff --git a/src/openvpn/o= ptions.c b/src/openvpn/options.c
index 55b923c..3c1632f 100= 644
--- a/src/openvpn/options.c
+++ b/src/o= penvpn/options.c
@@ -526,10 +526,12 @@
= " address <addr[:port]> [addr[:port] ...] : se= rver addresses 4/6\n"
" res= olve-domains <domain> [domain ...] : split domains\n"
= " dnssec <yes|no|optional> : option = to use DNSSEC\n"
-= " type <DoH|DoT> : query server over HTTPS = / TLS\n"
+ &q= uot; transport <DoH|DoT> : query server over HTTPS /= TLS\n"
" sni <domain>= ; : DNS server name indication\n"
"--dns sea= rch-domains <domain> [domain ...]:\n"
"= ; Add domains to DNS domain search list\n"
= + "--dns-updown cmd|for= ce|disable : Run cmd as user defined dns config command,\n"
= + " fo= rce running the default script or disable running it.\n"
"--auth-retry t : How to handle auth failures. Set t to\n&qu= ot;

" none (default), interact, = or nointeract.\n"
"--static-challenge t e [&= lt;scrv1|concat>]: Enable static challenge/response protocol using\n&quo= t;
@@ -918,6 +920,10 @@
#ifndef ENABLE_DCO=
o->disable_dco =3D true;
#endif /= * ENABLE_DCO */
+
+#ifdef ENABLE_DNS_UPDOW= N_BY_DEFAULT
+ o-&= gt;dns_options.updown =3D DEFAULT_DNS_UPDOWN;
+#endif /* ENABLE_DNS_UPDOWN_BY_DEFAULT */<= br> }

void
@@ -804= 6,6 +8052,39 @@
to->ip_win32_defined =3D true;<= /span>
}
#endif /* ifdef _WIN32 */+ else if (streq(p[0], &quo= t;dns-updown") && p[1])
+ {
+ if (!no_more_than_n_args(msglevel, p, 2, NM= _QUOTE_HINT))
+ = {
+ goto= err;
+ }

+ struct dns_optio= ns *dns =3D &options->dns_options;
+ if (streq(p[1], "disable"))<= br>+ {
+ dns->updown =3D NULL;<= /span>
+ 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= ;
+ dn= s->user_set_updown =3D true;
+ }
+ }
+ = else
+ {
+ if (streq= (dns->updown, DEFAULT_DNS_UPDOWN))
+ {
+ /* Unset the default command to prevent warning= s */
+ = dns->updown =3D NULL;
+ }
+= set_user_script(options, &dns->updown, p[1], p[0], false= );
+ dns-&= gt;user_set_updown =3D true;
+ }
+ = }
else if (streq(p[0], "dns") && = p[1])
{
VERIFY_PERMISSION(OPT= _P_DHCPDNS);

To view, visit change 838. To u= nsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifbe4ffb44d3bfcaa50adb38cacb= 3436fcdc71b10
Gerrit-Change-Number: 838
Gerrit-PatchSet: 26
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-MessageType: newpatchset
--izk9PtA/vPY=--