From: "d12fk (Code Review)" Date: Sat, 10 May 2025 12:36:29 +0000 In-Reply-To: References: Reply-To: heiko@...515..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., gert@...1296... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="EEVdg0x/OCo="; 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 --EEVdg0x/OCo= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: cron2, 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 (#23). 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, 705 insertions(+), 10 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/38/838/23 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 @@ 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..333a89a --- /dev/null +++ b/distro/dns-scripts/haikuos_file-dns-updown.sh @@ -0,0 +1,84 @@ +#!/bin/sh +# +# Simple OpenVPN up/down script for modifying Haiku OS resolv.conf +# (C) Copyright 2024 OpenVPN Inc +# +# 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 ###\n" + + sed -i'' -e "1s/^/${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..87b6641 --- /dev/null +++ b/distro/dns-scripts/resolvconf_file-dns-updown.sh @@ -0,0 +1,84 @@ +#!/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 ###\n" + + sed -i'' -e "1s/^/${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: 23 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-MessageType: newpatchset --EEVdg0x/OCo= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: cron2, flichtenheld, plaisthos. <= /p>

d12fk uploaded patch set #23 to this change.

View Change

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 h= ow names are resolved when the VPN pushes some --dns
settings.

Th= e default dns updown command is part of the distribution and is
installe= d with openvpn. You can change the path the command is located
at as a c= ompile time option, defaults to libexecdir.

You can compile-time dis= able that the default dns-updown hook is
run by passing --disable-dns-up= down-by-default to configure or
ccmake ENABLE_DNS_UPDOWN_BY_DEFAULT to O= FF.

There's also a new runtime option --dns-updown, which can ru= n a custom
command, force running the default when disabled or disable e= xecution
of the dns-updown altogether.

Change-Id: Ifbe4ffb44d3bfc= aa50adb38cacb3436fcdc71b10
Signed-off-by: Heiko Hund <heiko@...2866...= .>
---
M .gitignore
M CMakeLists.txt
M config.h.cmake.in
= M configure.ac
M distro/Makefile.am
A distro/dns-scripts/Makefile.am<= br>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/scr= ipt-options.rst
M src/openvpn/Makefile.am
M src/openvpn/dns.c
M sr= c/openvpn/dns.h
M src/openvpn/options.c
15 files changed, 705 inserti= ons(+), 10 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/o=
penvpn refs/changes/38/838/23
diff --git a/.gitignore b/.gitignore<=
/span>
index db8bb73..04523af 100644
--- a/.gitign= ore
+++ b/.gitignore
@@ -49,6 +49,7 @@
/doc/doxygen/latex/
/doc/doxygen/openvpn.doxy= file
distro/systemd/*.service
+distro/dns-scripts/dns-updown
s= ample/sample-keys/sample-ca/
vendor/cmocka_build vendor/dist
diff --git a/CMakeLists.txt b/CMakeList= s.txt
index ae818c3..40bffd4 100644
--- a/C= MakeLists.txt
+++ b/CMakeLists.txt
@@ -41,7= +41,10 @@
option(USE_WERROR "Treat compiler warnings= as errors (-Werror)" ON)
option(FAKE_ANDROID "T= arget Android but do not use actual cross compile/Android cmake to build fo= r simple compile checks on Linux")

-set(PLUGIN_DIR /usr/local/lib/openvpn/p= lugins CACHE FILEPATH "Location of the plugin directory")<= br>+option(ENABLE_DNS_UPDOWN_BY= _DEFAULT "Run --dns-updown hook by default" ON)
+set(DNS_UPDOWN_PATH "${CMAKE_INS= TALL_PREFIX}/libexec/openvpn/dns-updown" CACHE STRING "Default lo= cation for the DNS up/down script")
+
+set(PLUGIN_DIR "${CMAKE_INSTALL_PREFIX}/lib/openvpn/plugins" CA= CHE FILEPATH "Location of the plugin directory")
=
# Create machine readable compile commands
option(ENABLE_COMPILE_COMMANDS "Generate compile_commands.json and= a symlink for clangd to find it" OFF)

@@ -601,6 +604,= 8 @@

add_library_deps(openvpn)
+target_comp= ile_options(openvpn PRIVATE -DDEFAULT_DNS_UPDOWN=3D\"${DNS_UPDOWN_PATH= }\")
+ if(MINGW)
target_compile_options(openvpn PRIVA= TE -municode -UUNICODE)
target_link_options(openvpn PR= IVATE -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_DN= S_UPDOWN
+
= /* Enable NTLMv2 proxy support */
#define ENABLE_NT= LM 1

diff --git a/configure.ac b/configur= e.ac
index 9777e36..75367e8 100644
--- a/co= nfigure.ac
+++ b/configure.ac
@@ -96,6 +96,= 13 @@
)

AC_ARG_ENABLE(<= /span>
+ [dns-updown-by-defa= ult],
+ [AS_HELP_STRI= NG([--disable-dns-updown-by-default], [disable running --dns-updown by defa= ult @<:@default=3Dyes@:>@])],
+ ,
+ = [enable_dns_updown_by_default=3D"yes"]
+)
+
+AC_ARG_E= NABLE(
[ntlm],
[AS_HELP_STRING([--disab= le-ntlm], [disable NTLMv2 proxy support @<:@default=3Dyes@:>@])],
,
@@ -315,37 +322,50 @@
pl= ugindir=3D"\${libdir}/openvpn/plugins"
fi=

+AC_ARG_V= AR([SCRIPTDIR], [Path of script directory @<:@default=3DPKGLIBEXECDIR@:&= gt;@])
+if test -n &q= uot;${SCRIPTDIR}"; then
+ scriptdir=3D"${SCRIPTDIR}"
+else
+ scriptdir=3D"\${pkglibexecdir}"
+fi
+
AC_DEFINE_UNQUOTED([TARGET_ALIAS], [&qu= ot;${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_UNQUOT= ED([TARGET_PREFIX], ["L"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["syst= emd"])
have_sitnl=3D"yes"
pkg_config_required=3D"yes"

;;
<= span> *-*-solaris*)

AC_DEFINE([TARGET_SOLARIS], [1], [A= re we running on Solaris?])
AC_DEFINE_UNQUOTED([TARGET_P= REFIX], ["S"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["resolvconf_file&= quot;])
CPPFLAGS=3D"$CPPFLAGS -D_XPG4_2"
test -x /bin/bash && SHELL=3D"/bin/bash"
;;
*-*-openbsd*)
AC_D= EFINE([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_PREF= IX], ["F"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["openresolv"])<= /span>
;;
*-*-netbsd*)
AC_= DEFINE([TARGET_NETBSD], [1], [Are we running NetBSD?])
A= C_DEFINE_UNQUOTED([TARGET_PREFIX], ["N"], [Target prefix])=
+ AC_SUBST([DNS_UPDOWN_TYP= E], ["openresolv"])
;;
*-*-d= arwin*)
AC_DEFINE([TARGET_DARWIN], [1], [Are we running = on Mac OS X?])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], [&quo= t;M"], [Target prefix])
+ AM_CONDITIONAL([ENABLE_DNS_UPDOWN], [false])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["re= solvconf_file"])
have_tap_header=3D"yes"<= /span>
ac_cv_type_struct_in_pktinfo=3Dno
;;<= /span>
@@ -353,6 +373,8 @@
AC_DEFINE([TARGET_WI= N32], [1], [Are we running WIN32?])
AC_DEFINE([ENABLE_DC= O], [1], [DCO is always enabled on Windows])
AC_DEFINE_U= NQUOTED([TARGET_PREFIX], ["W"], [Target prefix])
+ AM_CONDITIONAL([ENABLE_DNS_UPDOWN]= , [false])
+ AC_SUBS= T([DNS_UPDOWN_TYPE], ["windows"])
CPPFLAGS=3D&= quot;${CPPFLAGS} -DWIN32_LEAN_AND_MEAN"
CPPFLAGS=3D= "${CPPFLAGS} -DNTDDI_VERSION=3DNTDDI_VISTA -D_WIN32_WINNT=3D_WIN32_WIN= NT_VISTA"
WIN32=3Dyes
@@ -360,10 +3= 82,12 @@
*-*-dragonfly*)
AC_DEFINE([TA= RGET_DRAGONFLY], [1], [Are we running on DragonFlyBSD?])
= AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["D"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_T= YPE], ["openresolv"])
;;
*-a= ix*)
AC_DEFINE([TARGET_AIX], [1], [Are we running AIX?])=
AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["A"], [T= arget prefix])
+ AC_= SUBST([DNS_UPDOWN_TYPE], ["resolvconf_file"])
= ROUTE=3D"/usr/sbin/route"
have_tap_header=3D&q= uot;yes"
ac_cv_header_net_if_h=3D"no" # e= xists, but breaks things
@@ -371,10 +395,12 @@
*-*-haiku*)

AC_DEFINE([TARGET_HAIKU], [1], [Are we= running Haiku?])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], [&= quot;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])
tes= t "${enable_fragment}" =3D "yes" && AC_DEFINE([= ENABLE_FRAGMENT], [1], [Enable internal fragmentation support])
<= span> test "${enable_port_share}" =3D "yes" && = AC_DEFINE([ENABLE_PORT_SHARE], [1], [Enable TCP Server port sharing])
-
+test "${enable_dns_updown_by_default}&quo= t; =3D "yes" && AC_DEFINE([ENABLE_DNS_UPDOWN_BY_DEFAULT],= [1], [Enable dns-updown hook by default])
test "${en= able_ntlm}" =3D "yes" && AC_DEFINE([ENABLE_NTLM], [1= ], [Enable NTLMv2 proxy support])
test "${enable_cryp= to_ofb_cfb}" =3D "yes" && AC_DEFINE([ENABLE_OFB_CFB_= MODE], [1], [Enable OFB and CFB cipher modes])
if test &qu= ot;${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/s= ystemd/Makefile
+ dis= tro/dns-scripts/Makefile
doc/Makefile
d= oc/doxygen/Makefile
doc/doxygen/openvpn.doxyfilediff --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

SU= BDIRS =3D systemd
+
+if ENABLE_DNS_UPDOWN<= /span>
+SUBDIRS +=3D dns-scr= ipts
+endifdiff --git a/distro/dns-scripts/Makefile.am b/distro/dns-scripts/Mak= efile.am
new file mode 100644
index 0000000= ..fc2db08
--- /dev/null
+++ b/distro/dns-sc= ripts/Makefile.am
@@ -0,0 +1,29 @@
+#
+# OpenVPN -- An application to securely tunnel IP networks
+# over a s= ingle UDP port, with support for SSL/TLS-based
+# session authentication and key exc= hange,
+# = packet encryption, packet authentication, and
+# packet compression.
+#

+# Copyright (C) 2002-2024 OpenVPN Inc <sales@...493...= 515...>
+#<= br>+
+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/di= stro/dns-scripts/haikuos_file-dns-updown.sh b/distro/dns-scripts/haikuos_fi= le-dns-updown.sh
new file mode 100644
index= 0000000..333a89a
--- /dev/null
+++ b/distr= o/dns-scripts/haikuos_file-dns-updown.sh
@@ -0,0 +1,84 @@
+#!/bin/sh
<= span style=3D"color: hsl(120, 100%, 40%);">+#

+# Simple OpenVPN up/down script for modifying Hai= ku OS resolv.conf
+# = (C) Copyright 2024 OpenVPN Inc <sales@...515...>
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Example env from openvpn (most are not applied):
+#
+# dev tun0
+# script_type dns-up
+# dns_search_domain_1 mycorp.in
<= span style=3D"color: hsl(120, 100%, 40%);">+# dns_search_domain_2 eu.myco= rp.com

+# dns_serve= r_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_s= erver_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_standa= rd_server_ports() {
+= i=3D1
+ while = true; do
+ eva= l addr=3D\"\$dns_server_${n}_address_${i}\"
+ [ -n "$addr" ] || retur= n 0
+
+ eval port=3D\"\$dns_se= rver_${n}_port_${i}\"

+ [ -z "$port" -o "$port" =3D "53&quo= t; ] || 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\"\$d= ns_server_${n}_address_1\"
+ [ -n "$addr" ] || {
+ echo "setting DNS failed, no= compatible server profile"
+ exit 1
+ }
+
+ # Skip se= rver profiles which require DNSSEC,
+ # secure transport or use a custom port
<= span style=3D"color: hsl(120, 100%, 40%);">+ 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" ] \=
+ && onl= y_standard_server_ports && break
+
+ n=3D$(expr $n + 1)
+ done
+
+ eval addr1=3D\&qu= ot;\$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"<= /span>
+ text=3D"${t= ext}nameserver $addr1\n"
+ test -z "$addr2" || text=3D"${text}nameserver = $addr2\n"
+ t= est -z "$addr3" || text=3D"${text}nameserver $addr3\n"<= /span>
+
+ test -z "$dns_search_domain_1&= quot; || {
+ f= or i in $(seq 1 6); do
+ = done
+ text=3D= "${text}search $domains\n"
+ }
+
+ sed -i'' -e "1s/^/${text}/" &q= uot;$conf"
+ = ;;
+dns-down)<= br>+ sed -i'' -e &qu= ot;/### openvpn ${dev} begin ###/,/### openvpn ${dev} end ###/d" "= ;$conf"
+ ;;<= /span>
+esac
diff --git a/distro/dns-scripts/openresolv-dns-updown.sh b/distro/dns-scri= pts/openresolv-dns-updown.sh

new file mode 100644index 0000000..e50398c
--- /dev/null
+++ b/distro/dns-scripts/openresolv-dns-updown.sh

@@ -0,0 = +1,89 @@
+#!/bin/sh
+#
+# Simple OpenVPN up/down script for ope= nresolv integration
+= # (C) Copyright 2016 Baptiste Daroussin
+# 2024 OpenVPN Inc <sales@...515...>= ;
+#
+# SPDX-License-Identifier: BSD-2-Cla= use
+#
+# Example env from openvpn (most a= re not applied):

+#
+# dev tun0+# script_type dns-up
+# dns_search_domain_1 m= ycorp.in
+# dns_sea= rch_domain_2 eu.mycorp.com
+# dns_server_1_address_1 192.168.99.254
+# dns_server_1_address_2 fd00::99:53<= br>+# dns_server_1_port_1 53<= /span>
+# dns_server_1_por= t_2 53
+# dns_serve= r_1_resolve_domain_1 mycorp.in
+# dns_server_1_resolve_domain_2 eu.mycorp.com
+# dns_server_1_dnssec true<= br>+# 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 "$ad= dr" ] || return 0
+ eval por= t=3D\"\$dns_server_${n}_port_${i}\"
+ [ -z "$port" -o "$port&quo= t; =3D "53" ] || return 1
+
+ = i=3D$(expr $i + 1)
+ done
+}
+
+: ${script_type:=3Ddns-down}
+case "${script_type}" in<= /span>
+dns-up)
+ n=3D1

+ while :; do
+ eval addr=3D\"\$dns_server_${n}_= address_1\"
+ = [ -n "$addr" ] || {
+ echo "setting DNS failed, no compatible ser= ver profile"
+ = exit 1
+ = }
+
+ # Skip server profiles w= hich 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&quo= t; -o "$transport" =3D "plain" ] \

+ && [ -z "$dnsse= c" -o "$dnssec" =3D "no" ] \
+ && only_standard_serv= er_ports && break
+
+ n=3D$= (expr $n + 1)
+ do= ne
+
+ {
+ 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<= br>+ eval option=3D\= "\$dns_search_domain_${i}\" || break
+ [ "${option}" ] || break
+ i=3D$((i = + 1))
+ ec= ho "search ${option}"
+ done
+ } | /sbin/resolvconf -a "${dev}"
+ ;;
+dns-down)
+ /sbin/resolvconf -d "${dev}" -f
+ ;;
+esac
diff --git a/distro/dns-script= s/resolvconf_file-dns-updown.sh b/distro/dns-scripts/resolvconf_file-dns-up= down.sh
new file mode 100644
index 0000000.= .87b6641
--- /dev/null
+++ b/distro/dns-scr= ipts/resolvconf_file-dns-updown.sh
@@ -0,0 +1,84 @@<= br>+#!/bin/sh
+#
+# Simple OpenVPN up/down script for modifying /etc/reso= lv.conf
+# (C) Copyri= ght 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
+# dns_server_1_addres= s_1 192.168.99.254
+#= dns_server_1_address_2 fd00::99:53
+# dns_server_1_port_1 53
+# dns_server_1_port_2 53
+# dns_server_1_resolve_domain_1 mycorp.= in
+# dns_server_1_= resolve_domain_2 eu.mycorp.com
+# dns_server_1_dnssec true
+# dns_server_1_transport DoH
+# dns_server_1_sni dns.mycorp.in+#
+
+set -e +u
+<= /span>
+only_standard_server= _ports() {
+ i=3D1=
+ while true; do<= /span>
+ 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" ] || r= eturn 1
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ i=3D$(expr $i + 1)
+ done
+}

+
+test -e "$conf" || exit 1
+case "${script_type}" in
+dns-up)
+ n=3D1
+ while :; do
+ eval addr=3D\"\$dns_server_${n}_address_1\"
+ [ -n "$a= ddr" ] || {
+ = echo "setting DNS failed, no compatible server profile"<= /span>
+ exit 1
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+

+ # Skip server profiles which require DNSSE= C,
+ # secure = transport or use a custom port
+ eval dnssec=3D\"\$dns_server_${n}_dnssec\"
+ eval transport= =3D\"\$dns_server_${n}_transport\"
+ [ -z "$transport" -o "$tran= sport" =3D "plain" ] \
+ && [ -z "$dnssec" -o "= $dnssec" =3D "no" ] \
+ && only_standard_server_ports &&am= p; break
+
= + n=3D$(expr $n + 1)
+ done
+

+ eval addr1=3D\"\$dns_server_${n}_address_1\&= quot;
+ 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 "$add= r2" || text=3D"${text}nameserver $addr2\n"
+ test -z "$addr3" || tex= t=3D"${text}nameserver $addr3\n"
+
+ test -z "$dns_search_domain_1" || {
+ for i in $(seq 1 6); do+ eval domains=3D\&= quot;$domains\$dns_search_domain_${i} \" || break
+ done
+ text=3D"${text}search $domains\n&q= uot;
+ }+ text=3D"${text}### o= penvpn ${dev} end ###\n"
+
+ sed -= i'' -e "1s/^/${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.shnew 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 <sales@...515...>=
+#
+# SPDX-License-Identifier: GPL-2.0
+#
+# Add/remove openvpn DNS settings from t= he env into/from
+# t= he system. Supported backends in this order:
+#
+# * systemd-resolved
+# * resolvconf
+# * /etc/resolv.conf file
+#
+# E= xample 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 37af683..2e93ebb 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 02970a7..1d1daea 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;
@@ -922,6 +924,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
@@ -808= 8,6 +8094,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: 23
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-MessageType: newpatchset
--EEVdg0x/OCo=--