From: "d12fk (Code Review)" Date: Mon, 31 Mar 2025 07:52:11 +0000 In-Reply-To: References: Reply-To: heiko@...515..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., gert@...1296... Message-ID: <0494c43cb3305fffdd07dadc23da8ec32ad12458-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="+dQGQYnLysw="; 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 --+dQGQYnLysw= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: cron2, d12fk, flichtenheld, plaisthos. Hello cron2, flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/838?usp=3Demail to look at the new patch set (#19). The following approvals got outdated and were removed: Code-Review-1 by flichtenheld 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, 658 insertions(+), 10 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/38/838/19 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 b04adce..ef4a0f2 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) @@ -577,6 +580,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..ec7bfde --- /dev/null +++ b/distro/dns-scripts/Makefile.am @@ -0,0 +1,28 @@ +# +# 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... $@ + +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..1b03e9c --- /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 ###" + + sed -i "1i${text}" "$conf" + ;; +dns-down) + sed -i "/### 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..1b218f5 --- /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..c469490 --- /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 ###" + + sed -i "1i${text}" "$conf" + ;; +dns-down) + sed -i "/### 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..69bbebf --- /dev/null +++ b/distro/dns-scripts/systemd-dns-updown.sh @@ -0,0 +1,194 @@ +#!/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 +# +# 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 do_resolvconf { + [ -x /sbin/resolvconf ] || return 1 + + 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" + return 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 + + 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 + for addr_var in ${!dns_server_1_address_*}; 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 +} + +do_resolved || do_resolvconf diff --git a/doc/man-sections/script-options.rst b/doc/man-sections/script-= options.rst index 0d1f9ae..de3842b 100644 --- a/doc/man-sections/script-options.rst +++ b/doc/man-sections/script-options.rst @@ -8,9 +8,13 @@ Script Order of Execution ------------------------- +#. ``--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`` @@ -173,7 +181,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 @@ -235,6 +243,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 @@ -661,7 +694,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 99dd60a..b116740 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" @@ -921,6 +923,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 @@ -8087,6 +8093,39 @@ to->ip_win32_defined =3D true; } #endif /* ifdef _WIN32 */ + else if (streq(p[0], "dns-updown") && p[1]) + { + VERIFY_PERMISSION(OPT_P_SCRIPT); + if (!no_more_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: 19 Gerrit-Owner: d12fk Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: cron2 Gerrit-Attention: flichtenheld Gerrit-Attention: d12fk Gerrit-MessageType: newpatchset --+dQGQYnLysw= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

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

d12fk uploaded patch set #19 to this change.

View Change

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

dns: apply settings via script on unixoid sys=
tems

This introduces a new script hook, the dns-updown, and implemen= ts such a
command script for a few popular systems (and a default for th= e 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 dist= ribution and is
installed with openvpn. You can change the path the comm= and is located
at as a compile time option, defaults to libexecdir.
<= br>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: Hei= ko Hund <heiko@...2662...>
---
M .gitignore
M CMakeLists.txt=
M config.h.cmake.in
M configure.ac
M distro/Makefile.am
A dist= ro/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.s= h
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, 658 insertions(+), 10 deletions(-)

git pull ssh:=
//gerrit.openvpn.net:29418/openvpn refs/changes/38/838/19
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/*.servic= e
+distro/dns-scripts= /dns-updown
sample/sample-keys/sample-ca/
= vendor/cmocka_build
vendor/dist
diff --gi= t a/CMakeLists.txt b/CMakeLists.txt
index b04adce..ef4a0f2 = 100644
--- a/CMakeLists.txt
+++ b/CMakeList= s.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 com= pile/Android cmake to build for simple compile checks on Linux")

-set(PLUGI= N_DIR /usr/local/lib/openvpn/plugins CACHE FILEPATH "Location of the p= lugin directory")
+set(DNS= _UPDOWN_PATH "${CMAKE_INSTALL_PREFIX}/libexec/openvpn/dns-updown"= CACHE STRING "Default location for the DNS up/down script")

+
+set(PLUGIN_DIR "${CMAKE_INSTALL_PREF= IX}/lib/openvpn/plugins" CACHE FILEPATH "Location of the plugin d= irectory")

# Create machine readabl= e compile commands
option(ENABLE_COMPILE_COMMANDS "Ge= nerate compile_commands.json and a symlink for clangd to find it" OFF)=
@@ -577,6 +580,8 @@

add= _library_deps(openvpn)

+target_compile_options(openvpn PRIVATE -DDEFAULT_DNS_U= PDOWN=3D\"${DNS_UPDOWN_PATH}\")
+
if(MINGW)
targ= et_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..516= 4ce3 100644
--- a/config.h.cmake.in
+++ b/c= onfig.h.cmake.in
@@ -35,6 +35,9 @@
/* Enab= le LZO compression library */
#cmakedefine ENABLE_LZO

+/* Ena= ble 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 @@
)
<= /span>
AC_ARG_ENABLE(
+ [dns-updown-by-default],
+ [AS_HELP_STRING([--disable-dns-updown-by-default], [disa= ble running --dns-updown by default @<:@default=3Dyes@:>@])],<= br>+ ,
+ [enable_dns_updown_by_default=3D"yes&q= uot;]
+)
+

+AC_ARG_ENABLE(
[ntlm],
= [AS_HELP_STRING([--disable-ntlm], [disable NTLMv2 proxy support @&l= t;:@default=3Dyes@:>@])],
,
@@ -315,37= +322,50 @@
plugindir=3D"\${libdir}/openvpn/plugins&= quot;
fi

+AC_ARG_VAR([SCRIPTDIR], [Path of script directory @= <:@default=3DPKGLIBEXECDIR@:>@])
+if test -n "${SCRIPTDIR}"; then
+ scriptdir=3D"${SCRIPTDIR}&qu= ot;
+else
<= span style=3D"color: hsl(120, 100%, 40%);">+ scriptdir=3D"\${pkglibexe= cdir}"
+fi
+
AC_DEFI= NE_UNQUOTED([TARGET_ALIAS], ["${host}"], [A string representing o= ur host])
-AM_CONDITION= AL([TARGET_LINUX], [false])
+AM_CONDITIONAL([ENABLE_DNS_UPDOWN],[true])
case &q= uot;$host" in
*-*-linux*)
AC_DEFI= NE([TARGET_LINUX], [1], [Are we running on Linux?])
- AM_CONDITIONAL([TARGET_LINUX], [true])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["L"], [Targ= et prefix])
+ AC_SUB= ST([DNS_UPDOWN_TYPE], ["systemd"])
have_sitnl= =3D"yes"
pkg_config_required=3D"yes"=
;;
*-*-solaris*)
A= C_DEFINE([TARGET_SOLARIS], [1], [Are we running on Solaris?])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["S"], [Target prefix])=

+ AC_SUBST([DNS_UPD= OWN_TYPE], ["resolvconf_file"])
CPPFLAGS=3D&qu= ot;$CPPFLAGS -D_XPG4_2"
test -x /bin/bash &&= ; SHELL=3D"/bin/bash"
;;
*-*= -openbsd*)
AC_DEFINE([TARGET_OPENBSD], [1], [Are we runn= ing on OpenBSD?])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], [&= quot;O"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["resolvconf_file"])
;;
*-*-freebsd*)
AC_D= EFINE([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 runn= ing NetBSD?])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["= ;N"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["openresolv"])
<= span> ;;

*-*-darwin*)
AC_DEFINE([TAR= GET_DARWIN], [1], [Are we running on Mac OS X?])
AC_DEFI= NE_UNQUOTED([TARGET_PREFIX], ["M"], [Target prefix])
+ AM_CONDITIONAL([ENABLE_DNS_UPD= OWN], [false])

+ AC_= SUBST([DNS_UPDOWN_TYPE], ["resolvconf_file"])
= have_tap_header=3D"yes"
ac_cv_type_struct_in_p= ktinfo=3Dno
;;
@@ -353,6 +373,8 @@
AC_DEFINE([TARGET_WIN32], [1], [Are we running WIN32?])
AC_DEFINE([ENABLE_DCO], [1], [DCO is always enabled on Window= s])
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&qu= ot;
CPPFLAGS=3D"${CPPFLAGS} -DNTDDI_VERSION=3DNTDDI= _VISTA -D_WIN32_WINNT=3D_WIN32_WINNT_VISTA"
WIN32= =3Dyes
@@ -360,10 +382,12 @@
*-*-dragonfl= y*)
AC_DEFINE([TARGET_DRAGONFLY], [1], [Are we running o= n DragonFlyBSD?])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], [&= quot;D"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["openresolv"])<= br> ;;
*-aix*)
AC_DEFINE([TARG= ET_AIX], [1], [Are we running AIX?])
AC_DEFINE_UNQUOTED(= [TARGET_PREFIX], ["A"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["resolvc= onf_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])<= br>+ AC_SUBST([DNS_UPDOWN_TYPE= ], ["haikuos_file"])
LIBS=3D"${LIBS} -lne= twork"
;;
*)
A= C_DEFINE_UNQUOTED([TARGET_PREFIX], ["X"], [Target prefix])=
+ AC_SUBST([DNS_UPDOWN_TYP= E], ["resolvconf_file"])
have_tap_header=3D&qu= ot;yes"
;;
esac
@@= -1317,7 +1343,7 @@
test "${enable_small}" =3D &= quot;yes" && AC_DEFINE([ENABLE_SMALL], [1], [Enable smaller ex= ecutable size])
test "${enable_fragment}" =3D &q= uot;yes" && AC_DEFINE([ENABLE_FRAGMENT], [1], [Enable internal= fragmentation support])
test "${enable_port_share}&q= uot; =3D "yes" && AC_DEFINE([ENABLE_PORT_SHARE], [1], [En= able TCP Server port sharing])
-
+test "= ;${enable_dns_updown_by_default}" =3D "yes" && AC_DE= FINE([ENABLE_DNS_UPDOWN_BY_DEFAULT], [1], [Enable dns-updown hook by defaul= t])
test "${enable_ntlm}" =3D "yes" &a= mp;& AC_DEFINE([ENABLE_NTLM], [1], [Enable NTLMv2 proxy support])
test "${enable_crypto_ofb_cfb}" =3D "yes" &= amp;& AC_DEFINE([ENABLE_OFB_CFB_MODE], [1], [Enable OFB and CFB cipher = modes])
if test "${have_export_keying_material}"= =3D "yes"; then
@@ -1505,6 +1531,7 @@
=
sampledir=3D"\$(docdir)/sample" AC_SUBST([plugindir])
+AC_SUBST([scriptdir])
AC_SUBST([sampledir])

AC_SUBST([systemdunitdir])
= @@ -1541,6 +1568,7 @@
Makefile
distro/M= akefile
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..ec7bfde
--- /dev/null=
+++ b/distro/dns-scripts/Makefile.am
@@ -0= ,0 +1,28 @@
+#=
+# OpenVPN -- An applicati= on to securely tunnel IP networks
+# over a single UDP port, with support for SSL/TL= S-based
+# = session authentication and key exchange,
+# packet encryption, packet authenticati= on, and
+# = packet compression.
+#
+# Copyright (C)= 2002-2024 OpenVPN Inc <sales@...515...>
+#
+
+MAINTAINER= CLEANFILES =3D \
+ $(= srcdir)/Makefile.in
+=
+EXTRA_DIST =3D \
+ systemd-dns-updown.sh= \
+ openresolv-dns-u= pdown.sh \
+ haikuos_= file-dns-updown.sh \
= + resolvconf_file-dns-updown.sh
+
+script_= SCRIPTS =3D \
+ dns-u= pdown
+
+CLEANFILES =3D $(script_SCRIPTS)<= /span>
+
+dns-updown: @DNS_UPDOWN_TYPE@...2841...=
+ cp ${srcdir}/@DNS_= UPDOWN_TYPE@...2841... $@
+
+all: $(script= _SCRIPTS)
diff --git a/distro/dns-scripts/haikuos_file-dns-= updown.sh b/distro/dns-scripts/haikuos_file-dns-updown.sh
n= ew file mode 100644
index 0000000..1b03e9c
= --- /dev/null
+++ b/distro/dns-scripts/haikuos_file-dns-upd= own.sh
@@ -0,0 +1,84 @@
+#!/bin/sh
+#
+# Simple= OpenVPN up/down script for modifying Haiku OS resolv.conf
+# (C) Copyright 2024 OpenVPN Inc <= ;sales@...515...>
= +#
+# SPDX-License-Id= entifier: BSD-2-Clause
+# Example env f= rom openvpn (most are not applied):
+#
+# = dev tun0
+# scrip= t-type dns-up
+# dn= s_search_domain_1 mycorp.in
+# dns_search_domain_2 eu.mycorp.com
+# dns_server_1_address_1 192.168.99.254<= br>+# 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.c= om
+# dns_server_1_= dnssec true
+# dns_= server_1_transport DoH
+#
+=
+set -e +u+
+conly_standard_server_ports() {
+ i=3D1
+ while true; do
+ eval addr=3D\"\$dns_server_${n}_ad= dress_${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/setti= ngs/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,<= /span>
+ # secure tra= nsport or use a custom port
+ eval dnssec=3D\"\$dns_server_${n}_dnssec\"=
+ eval transport=3D\= "\$dns_server_${n}_transport\"
+ [ -z "$transport" -o "$transpor= t" =3D "plain" ] \
+ && [ -z "$dnssec" -o "$dns= sec" =3D "no" ] \
+ && only_standard_server_ports && b= reak
+
+ n=3D$(expr $n + 1)
<= br>+ 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\&q= uot;
+ text=3D&quo= t;### openvpn ${dev} begin ###\n"
+ text=3D"${text}nameserver $addr1\n"=
+ test -z "$addr2&q= uot; || 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}### openv= pn ${dev} end ###"

+
+ sed -i &quo= t;1i${text}" "$conf"
+ ;;
+ sed= -i "/### openvpn ${dev} begin ###/,/### openvpn ${dev} end ###/d"= ; "$conf"
+= ;;
+esacdiff --git a/distro/dns-scripts/openresolv-dns-updown.sh b/distro/d= ns-scripts/openresolv-dns-updown.sh
new file mode 100644
index 0000000..1b218f5
--- /dev/null+++ b/distro/dns-scripts/openresolv-dns-updown.sh
@= @ -0,0 +1,89 @@
+#!/b= in/sh
+#
+# Simple OpenVPN up/down script = for openresolv integration

+# (C) Copyright 2016 Baptiste Daroussin
+# 2024 OpenVPN Inc <sales@...1387...= 5...>
+#+# SPDX-License-Identifier: BS= D-2-Clause
+#<= br>+# Example env from openvpn = (most are not applied):
+#
+# dev tun0
+# script-type dns-u= p
+# dns_search_dom= ain_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<= /span>
+# dns_server_1_por= t_1 53
+# dns_serve= r_1_port_2 53
+# dn= s_server_1_resolve_domain_1 mycorp.in
+# dns_server_1_resolve_domain_2 eu.mycorp.com+# dns_server_1_dnssec true<= /span>
+# dns_server_1_tra= nsport DoH
+# dns_s= erver_1_sni dns.mycorp.in
+#
+
<= span style=3D"color: hsl(120, 100%, 40%);">+set -e +u

+
+only_standard_server_ports() {
+ i=3D1
+ while true; do
+ eval addr=3D\"\$dns_server_${n}_address_${i}\&q= uot;
+ [ -n &q= uot;$addr" ] || return 0
+
+ e= val 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}<= br>+case "${script_type}&q= uot; in
+dns-up)
+ n=3D1
+ while :; do

+ eval addr=3D\"\$dns_serve= r_${n}_address_1\"
+ [ -n "$addr" ] || {
+ echo "setting DNS failed, no compati= ble server profile"
+ exit 1
+ }
+
+ # Skip server pro= files which require DNSSEC,
+ # secure transport or use a custom port
+ eval dnssec=3D\"\$dns_serv= er_${n}_dnssec\"
+ eval transport=3D\"\$dns_server_${n}_transport\"=
+ [ -z "$transp= ort" -o "$transport" =3D "plain" ] \
+ && [ -z "= ;$dnssec" -o "$dnssec" =3D "no" ] \

+ && only_standa= rd_server_ports && break

+
+ = n=3D$(expr $n + 1)
= + done
++ {
+ i=3D1
+ maxns=3D3
+ while :; do
+ maxns=3D$((maxns - 1))
+ [ $maxns -gt 0 ] || br= eak

+ eval= option=3D\"\$dns_server_${n}_address_${i}\" || break
<= span style=3D"color: hsl(120, 100%, 40%);">+ [ "${option}&q= uot; ] || break
+ = i=3D$((i + 1))
+ done
+ i=3D1
+ maxdom=3D6
+ while :; do
+ maxdom=3D$((maxdom - 1))
+ [ $maxdom -gt 0 ] || break=
+ eval op= tion=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 00= 00000..c469490
--- /dev/null
+++ b/distro/d= ns-scripts/resolvconf_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 /et= c/resolv.conf
+# (C) = Copyright 2024 OpenVPN Inc <sales@...515...>
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Example env from openvpn (most are not applied):+#
+# dev tun0
+# script-type dns-up
+# dns_search_domain_1 mycorp.in
+# dns_search_domain_2 eu.mycorp.co= m
+# dns_server_1_a= ddress_1 192.168.99.254
+# dns_server_1_address_2 fd00::99:53
+# dns_server_1_port_1 53
+# dns_server_1_port_2 53
+# dns_server_1_resolve_domain_1 myco= rp.in
+# dns_server= _1_resolve_domain_2 eu.mycorp.com
+# dns_server_1_dnssec true
+# dns_server_1_transport DoH
+# dns_server_1_sni dns.mycorp.in=
+#
+
+set -e +u
+
+only_standard_ser= ver_ports() {
+ i= =3D1
+ while true;= do
+ eval add= r=3D\"\$dns_server_${n}_address_${i}\"
+ [ -n "$addr" ] || return 0
+
+ eval port=3D\"\$dns_server_= ${n}_port_${i}\"
+ [ -z "$port" -o "$port" =3D "53" ] = || return 1
+<= br>+ i=3D$(expr $i + 1)<= /span>
+ done
<= span style=3D"color: hsl(120, 100%, 40%);">+}

+
+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\&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%);">+

+ eval addr1=3D\"\$dns_server_${n}_address_= 1\"
+ eval ad= dr2=3D\"\$dns_server_${n}_address_2\"
+ eval addr3=3D\"\$dns_server_${n}_addre= ss_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 ###"
+
+ s= ed -i "1i${text}" "$conf"
+ ;;
+dns-down)
+ ;;
+esa= c
diff --git a/distro/dns-scripts/systemd-dns-updown.sh b/d= istro/dns-scripts/systemd-dns-updown.sh
new file mode 10064= 4
index 0000000..69bbebf
--- /dev/null

+++ b/distro/dns-scripts/systemd-dns-updown.sh
= @@ -0,0 +1,194 @@
+#!= /bin/bash
+#+# dns-updown - add/remove op= envpn provided DNS information
+#
+# Copyr= ight (C) 2024 OpenVPN Inc <sales@...515...>
+#
+# SPDX-License-Identifier: GPL-2.0
+#
+# Add/remove openvpn DNS settings from the env into/from+# the system. Supported backe= nds in this order:
+#=
+# * systemd-resol= ved
+# * resolvconf=
+#
+# Example env from openvpn (not all a= re 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
<= span style=3D"color: hsl(120, 100%, 40%);">+# dns_server_1_dnssec true
+# dns_server_1_trans= port DoH
+# dns_ser= ver_1_sni dns.mycorp.in
+#
+
+function do_resolved_servers {
+ local sni=3D"= "
+ local tra= nsport_var=3Ddns_server_${n}_transport
+ local sni_var=3Ddns_server_${n}_sni
+ [ "${!transport_var}" = =3D "DoT" ] && sni=3D"#${!sni_var}"
<= span style=3D"color: hsl(120, 100%, 40%);">+

+ local i=3D1
+ local addrs=3D""
+ while :; do
+ local addr_var=3Ddns_server_${n}_addr= ess_${i}
+ loc= al addr=3D"${!addr_var}"
+ [ -n "$addr" ] || break
+
+ local port_var=3Ddns_server_${n}_port_${i}=
+ if [ -n "${!p= ort_var}" ]; then
+ addr=3D"[$addr]&quo= t;
+ fi
+ addrs+=3D&= quot;${addr}:${!port_var}${sni} "
+ else
+ addrs+=3D"${addr}${sni} "
+ fi

+ i=3D$((i+1))
+ done
+
+ resolvectl dns "$dev" $addrs
+}
+
+function d= o_resolved_domains {
= + local list=3D""
+ for domain_var in ${!dns_search_domain_*}; do
+ list+=3D"${!domain_va= r} "

+ done
+ local domain_var= =3Ddns_server_${n}_resolve_domain_1
+ if [ -z "${!domain_var}" ]; then
+ resolvectl default-route= "$dev" true

+ else
+ 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} "
<= span style=3D"color: hsl(120, 100%, 40%);">+ i=3D$((i+1))=
+ done
+ fi
+
+ resolvectl domain "$dev" $list
+}
+
+fu= nction do_resolved_dnssec {
+ local dnssec_var=3Ddns_server_${n}_dnssec
+ if [ "${!dnssec_var}" =3D &= quot;optional" ]; then
+ resolvectl dnssec "$dev" allow-downgrade+ elif [ "${!dnssec_v= ar}" =3D "yes" ]; then
+ resolvectl dnssec "$dev" true+ else
+ resolvectl dnssec "$dev&quo= t; false
+ fi
+}
+
+function do_resolved_dnsovertls {
+ local transport_var=3Ddns_server_${n}_tran= sport
+ if [ "= ;${!transport_var}" =3D "DoT" ]; then
+ resolvectl dnsovertls "$dev&= quot; true
+ else<= /span>
+ resolvectl d= nsovertls "$dev" false
+ fi
+= }
+
+function do_resolved {
+ [[ "$(readlink /etc/resol= v.conf)" =3D~ systemd ]] || return 1

+
+ whi= le :; do
+ loc= al 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_s= erver_${n}_transport

= + [ -n "${!transport_var}" -a "${!transport_var}"= ; =3D "DoH" ] || break
+
+ = n=3D$((n+1))
+ d= one
+
+ if [ "$script_type" = =3D "dns-up" ]; then
+ echo "setting DNS using resolvectl"
= + do_resolved_servers
+ do_resolved_do= mains
+ do_res= olved_dnssec
+ = do_resolved_dnsovertls
+ else
+ = echo "unsetting DNS using resolvectl"
+ resolvectl revert "$dev"

+ fi
+
+ return 0
+}
+
+function only_standard_s= erver_ports {
+ lo= cal 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
+}

+
+ [ -x /sbin/resolvconf ] || return 1
+
+ n=3D1
+ = while :; do
+ = local server_addr_var=3Ddns_server_${n}_address_1
+ [ -n "${!server_addr_var}&qu= ot; ] || {
+ = echo "setting DNS failed, no compatible server profile"=
+ return 1
+ }
+

+ # Skip server profiles which require DNSSEC,
+ # secure tran= sport or use a custom port
+ local dnssec_var=3Ddns_server_${n}_dnssec
+ local transport_var=3Ddns_serv= er_${n}_transport
+ = [ -z "${!transport_var}" -o "${!transport_var}" = =3D "plain" ] \
+ && [ -z "${!dnssec_var}" -o "${!dn= ssec_var}" =3D "no" ] \
+ && only_standard_server_ports &&= amp; break
++ n=3D$((n+1))<= br>+ done
+
+ if [ "$script_type" =3D "dns-up" = ]; then
+ echo= "setting DNS using resolvconf"
+ local domains=3D""
+ for domain_var in ${!dns_sea= rch_domain_*}; do
+ = domains+=3D"${!domain_var} "
+ done
+ {
+ local maxns=3D3
+ for addr_var in ${!dns_server_1_address_*};= do
+ = [ $((maxns--)) -gt 0 ] || break
+ echo "nameserver ${!addr_var}"=
+ done+ [ -z "$domai= ns" ] || echo "search $domains"
+ } | /sbin/resolvconf -a "$dev"=
+ else
= + echo "unsetting D= NS using resolvconf"
+ /sbin/resolvconf -d "$dev"
+ fi
+
+ = return 0
+}=
+
+do_resolved || do_resolvconf
= diff --git a/doc/man-sections/script-options.rst b/doc/man-sections/script-= options.rst
index 0d1f9ae..de3842b 100644
-= -- a/doc/man-sections/script-options.rst
+++ b/doc/man-sect= ions/script-options.rst
@@ -8,9 +8,13 @@
S= cript Order of Execution
-------------------------<= br>
+#. ``--dn= s-updown``
++ Executed after TCP/UDP so= cket bind and TUN/TAP open, before ``--up``.
+
#. ``--up``

- Executed after TCP/UD= P socket bind and TUN/TAP open.
+ Executed after TCP/UDP socket bind and TUN/TAP open, after `= `--dns-updown``.

#. ``--tls-verify``

@@ -38,9 +42,13 @@
<= br> 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``

@@ -173,7 +181,7 @@
client-crrespon= se cmd

OpenVPN will write the response= of the client into a temporary file.
- The filename will be passed as an argument to ``cmd``, an= d 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 th= e client. The script needs to check
@@ -235,6 +243,31 @@
The ``--client-disconnect`` command is not passed any extr= a arguments
(only those arguments specified in cmd, if a= ny).

+= Run command ``cmd``, instead of the default DNS up/down command that come= s
+ 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 transpor= t
+ settings need to= be adhered to. If split DNS is not possible a full redirect
+ can be used as a fallback. If no= t all of the server addresses or search domains

+ can be configured, apply them in the order th= ey are listed in.
+
+ Note that ``--dns-u= pdown`` is not supported on all platforms. On Windows DNS
+ will always be set by the service. = On Android DNS will be passed via management
+ interface.
+
+ = Note that DNS-related ``--dhcp-option``\ s might be converted so that they= are
+ available to = this hook if no ``--dns`` options exist. If any ``--dns server``
= + option is present, DNS-relat= ed ``--dhcp-option``\ s will always be ignored.
+ If an ``--up`` script is defined, foreign_opt= ion env vars will be generated
+ from ``--dns`` options and passed to the script. The default `= `--dns-updown``
+ co= mmand is not run if an ``--up`` script is defined. Both is done for backwar= d
+ compatibility. I= n case you want to run the ``--dns-updown`` command even if
+ there is an ``--up`` defined, you= can define a custom command or use ``force``

+ as ``cmd`` to run the default command. No DNS e= nv vars will be passed to ``--up``
+ in this case.
+
--down cmd
Run command ``cm= d`` after TUN/TAP device close (post ``--user`` UID
chan= ge and/or ``--chroot`` ). ``cmd`` consists of a path to script (or@@ -661,7 +694,7 @@
names). Set prior to ``--u= p`` or ``--down`` script execution.

:cod= e:`dns_*`
- The ``--= dns`` configuration options will be made available to script
+ The ``--dns`` configuration op= tions will be made available to ``--dns-updown``

execu= tion through this set of environment variables. Variables appear
= only if the corresponding option has a value assigned. For the s= emantics
of each individual variable, please refer to = the documentation for ``--dns``.
diff --git a/src/openvpn/M= akefile.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_CFLAG= S) \
$(OPTIONAL_INOTIFY_CFLAGS) \
- -DPLUGIN_LIBDIR=3D\"${plugindir}\&quo= t;
+ -DPLUGIN_LIBDIR= =3D\"${plugindir}\" \
+ -DDEFAULT_DNS_UPDOWN=3D\"${scriptdir}/dns-updown\"

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

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

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

}

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

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

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

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

/*= *
diff --git a/src/openvpn/options.c b/src/openvpn/options.= c
index 99dd60a..b116740 100644
--- a/src/o= penvpn/options.c
+++ b/src/openvpn/options.c
@@ -526,10 +526,12 @@

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

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

void
@@ -8087,6 +8093,39 @@
to->ip_win32_defined =3D true;

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

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

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

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

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

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