From: "d12fk (Code Review)" Date: Wed, 5 Mar 2025 03:44:49 +0000 In-Reply-To: References: Reply-To: heiko@...515..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="MF3GbymtvWI="; 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 --MF3GbymtvWI= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos. Hello 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 (#9). 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-script and implements such a script for a few popular systems (and a default for the not so popular ones). Like the name suggests this script is soleley for dealing with modifying how names are resolved when the VPN pushes some --dns settings. The default dns script is part of the distribution and is installed with openvpn. You can change the path the script is located at as a compile time option, defaults to libexecdir. There's also a new runtime option --dns-script, which can run a custom script. Change-Id: Ifbe4ffb44d3bfcaa50adb38cacb3436fcdc71b10 Signed-off-by: Heiko Hund --- M .gitignore M CMakeLists.txt 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 14 files changed, 610 insertions(+), 8 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/38/838/9 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..2696691 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,8 @@ 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") +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 +578,8 @@ add_library_deps(openvpn) +target_compile_options(openvpn PRIVATE -DDNS_UPDOWN_PATH=3D\"${DNS_UPDOWN_= PATH}\") + if(MINGW) target_compile_options(openvpn PRIVATE -municode -UUNICODE) target_link_options(openvpn PRIVATE -municode) diff --git a/configure.ac b/configure.ac index 9777e36..45d3ac1 100644 --- a/configure.ac +++ b/configure.ac @@ -315,37 +315,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_SCRIPT],[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_SCRIPT],[false]) + AC_SUBST([DNS_UPDOWN_TYPE], ["resolvconf_file"]) have_tap_header=3D"yes" ac_cv_type_struct_in_pktinfo=3Dno ;; @@ -353,6 +366,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_SCRIPT],[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 +375,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 +388,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 @@ -1505,6 +1524,7 @@ sampledir=3D"\$(docdir)/sample" AC_SUBST([plugindir]) +AC_SUBST([scriptdir]) AC_SUBST([sampledir]) AC_SUBST([systemdunitdir]) @@ -1541,6 +1561,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..b8fb85b 100644 --- a/distro/Makefile.am +++ b/distro/Makefile.am @@ -13,3 +13,7 @@ $(srcdir)/Makefile.in SUBDIRS =3D systemd + +if ENABLE_DNS_SCRIPT +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..4a68499 --- /dev/null +++ b/distro/dns-scripts/haikuos_file-dns-updown.sh @@ -0,0 +1,82 @@ +#!/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 + } + + 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..731f9b5 --- /dev/null +++ b/distro/dns-scripts/openresolv-dns-updown.sh @@ -0,0 +1,87 @@ +#!/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 + } + + 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..7ee66e9 --- /dev/null +++ b/distro/dns-scripts/resolvconf_file-dns-updown.sh @@ -0,0 +1,82 @@ +#!/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 + } + + 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..c8e455e --- /dev/null +++ b/distro/dns-scripts/systemd-dns-updown.sh @@ -0,0 +1,190 @@ +#!/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 + [[ "$list" =3D~ (^| )"${!domain_var}"( |$) ]] && continue + 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 + } + + 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 + } + + 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..0a93509 100644 --- a/doc/man-sections/script-options.rst +++ b/doc/man-sections/script-options.rst @@ -8,9 +8,13 @@ Script Order of Execution ------------------------- +#. ``--dns-script`` + + 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-scri= pt``. #. ``--tls-verify`` @@ -38,9 +42,13 @@ Executed in ``--mode server`` mode on client instance shutdown. +#. ``--dns-script`` + + 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-script``. #. ``--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,21 @@ The ``--client-disconnect`` command is not passed any extra arguments (only those arguments specified in cmd, if any). +--dns-script cmd + Run command ``cmd``, instead of the default DNS command that comes with + openvpn. If ``cmd`` is 'disable' no command is executed to set ``--dns`` + options. If you write your own command, please make sure to ignore ``--d= ns`` + 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 on the order they are listed in. + + Note that ``--dns-script`` is not supported on Windows. DNS will + always be set by the service there. Also note that no DNS related + ``--dhcp-option`` will cause this hook to run. Instead foreign_option + environment values will be passed to ``--up`` and ``--down`` if no ``--d= ns`` + option causes the old behavior to be overridden. + --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 +684,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-= script`` 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..6664aae 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}\" \ + -DDNS_UPDOWN_PATH=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 12f88ab..4e728e9 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.script =3D o->script; + clone.user_defined_script =3D o->user_defined_script; return clone; } @@ -519,6 +522,54 @@ send_msg_iservice(o->msg_channel, &nrpt, sizeof(nrpt), &ack, "DNS"); } +#else /* ifdef _WIN32 */ + +static void +script_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_script(bool up, const struct dns_options *o, const struct t= untap *tt) +{ + struct gc_arena gc =3D gc_new(); + struct argv argv =3D argv_new(); + struct env_set *es =3D env_set_create(&gc); + + script_env_set(up, o, tt, es); + + argv_printf(&argv, "%s", o->script); + argv_msg(M_INFO, &argv); + int res; + if (o->user_defined_script) + { + res =3D openvpn_run_script(&argv, es, S_EXITCODE, "dns script"); + } + else + { + res =3D openvpn_execve_check(&argv, es, S_EXITCODE, "WARNING: Fail= ed running dns script"); + } + argv_free(&argv); + gc_free(&gc); + return res; +} + +static void +run_up_down_script(bool up, struct options *o, const struct tuntap *tt) +{ + if (!o->dns_options.script) + { + return; + } + + int status; + status =3D do_run_up_down_script(up, &o->dns_options, tt); + msg(M_INFO, "dns script exited with status %d", status); +} + #endif /* _WIN32 */ void @@ -637,5 +688,7 @@ #ifdef _WIN32 run_up_down_service(up, o, tt); +#else + run_up_down_script(up, o, tt); #endif /* ifdef _WIN32 */ } diff --git a/src/openvpn/dns.h b/src/openvpn/dns.h index f24e30b..38ced79 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 *script; + bool user_defined_script; }; /** diff --git a/src/openvpn/options.c b/src/openvpn/options.c index dc8c880..8775ea3 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -528,6 +528,7 @@ " sni : DNS server name indication\n" "--dns search-domains [domain ...]:\n" " Add domains to DNS domain search list\n" + "--dns-script cmd : Run cmd as user defined dns configuration script\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" @@ -919,6 +920,8 @@ #ifndef ENABLE_DCO o->disable_dco =3D true; #endif /* ENABLE_DCO */ + + o->dns_options.script =3D DNS_UPDOWN_PATH; } void @@ -8148,6 +8151,28 @@ to->ip_win32_defined =3D true; } #endif /* ifdef _WIN32 */ + else if (streq(p[0], "dns-script") && p[1]) + { + VERIFY_PERMISSION(OPT_P_SCRIPT); + if (!no_more_than_n_args(msglevel, p, 2, NM_QUOTE_HINT)) + { + goto err; + } + if (streq(p[1], "disable")) + { + options->dns_options.script =3D NULL; + } + else + { + if (options->dns_options.user_defined_script =3D=3D false) + { + /* Unset the default script to prevent warnings */ + options->dns_options.script =3D NULL; + } + set_user_script(options, &options->dns_options.script, p[1], p= [0], false); + options->dns_options.user_defined_script =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: 9 Gerrit-Owner: d12fk Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --MF3GbymtvWI= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, plaisthos.

d12fk uploaded patch set #9 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-script and implement= s such a
script for a few popular systems (and a default for the not so = popular
ones). Like the name suggests this script is soleley for dealing= with
modifying how names are resolved when the VPN pushes some --dnssettings.

The default dns script is part of the distribution and is= installed with
openvpn. You can change the path the script is located a= t as a compile
time option, defaults to libexecdir. There's also a n= ew runtime option
--dns-script, which can run a custom script.

Ch= ange-Id: Ifbe4ffb44d3bfcaa50adb38cacb3436fcdc71b10
Signed-off-by: Heiko = Hund <heiko@...2662...>
---
M .gitignore
M CMakeLists.txtM 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-updow= n.sh
A distro/dns-scripts/systemd-dns-updown.sh
M doc/man-sections/sc= ript-options.rst
M src/openvpn/Makefile.am
M src/openvpn/dns.c
M s= rc/openvpn/dns.h
M src/openvpn/options.c
14 files changed, 610 insert= ions(+), 8 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/o=
penvpn refs/changes/38/838/9
diff --git a/.gitignore b/.gitignore
index db8bb73..04523af 100644
--- a/.gitigno= re
+++ b/.gitignore
@@ -49,6 +49,7 @@
/doc/doxygen/latex/
/doc/doxygen/openvpn.doxyf= ile
distro/systemd/*.service
+distro/dns-scripts/dns-updown
sa= mple/sample-keys/sample-ca/
vendor/cmocka_build
= vendor/dist
diff --git a/CMakeLists.txt b/CMakeLists= .txt
index b04adce..2696691 100644
--- a/CM= akeLists.txt
+++ b/CMakeLists.txt
@@ -41,7 = +41,8 @@
option(USE_WERROR "Treat compiler warnings a= s errors (-Werror)" ON)
option(FAKE_ANDROID "Tar= get Android but do not use actual cross compile/Android cmake to build for = simple compile checks on Linux")

-set(PLUGIN_DIR /usr/local/lib/openvpn/plu= gins CACHE FILEPATH "Location of the plugin directory")+set(DNS_UPDOWN_PATH "${C= MAKE_INSTALL_PREFIX}/libexec/openvpn/dns-updown" CACHE STRING "De= fault location for the DNS up/down script")
+set(PLUGIN_DIR "${CMAKE_INSTALL_PREFIX}/l= ib/openvpn/plugins" CACHE FILEPATH "Location of the plugin direct= ory")

# Create machine readable com= pile commands
option(ENABLE_COMPILE_COMMANDS "Generat= e compile_commands.json and a symlink for clangd to find it" OFF)
@@ -577,6 +578,8 @@

add_libr= ary_deps(openvpn)

+target_compile_options(openvpn PRIVATE -DDNS_UPDOWN_PATH=3D= \"${DNS_UPDOWN_PATH}\")
+
if(MINGW)
target_compi= le_options(openvpn PRIVATE -municode -UUNICODE)
target= _link_options(openvpn PRIVATE -municode)
diff --git a/confi= gure.ac b/configure.ac
index 9777e36..45d3ac1 100644=
--- a/configure.ac
+++ b/configure.ac
<= span>@@ -315,37 +315,50 @@

plugindir=3D"\${libdir}/o= penvpn/plugins"
fi

+AC_ARG_VAR([SCRIPTDIR], [Path of scr= ipt directory @<:@default=3DPKGLIBEXECDIR@:>@])
+if test -n "${SCRIPTDIR}"; then=
+ scriptdir=3D"= ${SCRIPTDIR}"
+e= lse
+ scriptdir=3D&qu= ot;\${pkglibexecdir}"
+fi
+ AC_DEFINE_UNQUOTED([TARGET_ALIAS], ["${host}"], [A string= representing our host])
+AM_CONDITIONAL([ENABLE_DNS_SCRIPT],[true]) case "$host" in
*-*-linux*)
<= span> AC_DEFINE([TARGET_LINUX], [1], [Are we running on Linux?])
- AM_CONDITIONAL([TARGET_LINUX= ], [true])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["L&= quot;], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["systemd"])
= have_sitnl=3D"yes"
pkg_config_required=3D&qu= ot;yes"
;;
*-*-solaris*) AC_DEFINE([TARGET_SOLARIS], [1], [Are we running on Solaris?])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["S"], [Tar= get prefix])
+ AC_SU= BST([DNS_UPDOWN_TYPE], ["resolvconf_file"])
CP= PFLAGS=3D"$CPPFLAGS -D_XPG4_2"
test -x /bin/ba= sh && 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_fil= e"])
;;
*-*-freebsd*)
<= span> AC_DEFINE([TARGET_FREEBSD], [1], [Are we running on FreeBSD?])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["F"], [Target= prefix])
+ AC_SUBST= ([DNS_UPDOWN_TYPE], ["openresolv"])
;;<= br> *-*-netbsd*)
AC_DEFINE([TARGET_NETBSD], [1], = [Are we running NetBSD?])
AC_DEFINE_UNQUOTED([TARGET_PRE= FIX], ["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])<= /span>
+ AM_CONDITIONAL([EN= ABLE_DNS_SCRIPT],[false])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["resolvconf_file"]) have_tap_header=3D"yes"
ac_cv_type_= struct_in_pktinfo=3Dno
;;
@@ -353,6 +366= ,8 @@
AC_DEFINE([TARGET_WIN32], [1], [Are we running WIN= 32?])
AC_DEFINE([ENABLE_DCO], [1], [DCO is always enable= d on Windows])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], [&quo= t;W"], [Target prefix])
+ AM_CONDITIONAL([ENABLE_DNS_SCRIPT],[false])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["win= dows"])
CPPFLAGS=3D"${CPPFLAGS} -DWIN32_LEAN_A= ND_MEAN"
CPPFLAGS=3D"${CPPFLAGS} -DNTDDI_VERSI= ON=3DNTDDI_VISTA -D_WIN32_WINNT=3D_WIN32_WINNT_VISTA"
= WIN32=3Dyes
@@ -360,10 +375,12 @@
*-*-= dragonfly*)
AC_DEFINE([TARGET_DRAGONFLY], [1], [Are we r= unning on DragonFlyBSD?])
AC_DEFINE_UNQUOTED([TARGET_PRE= FIX], ["D"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["openresolv"])=
;;
*-aix*)
AC_DEFI= NE([TARGET_AIX], [1], [Are we running AIX?])
AC_DEFINE_U= NQUOTED([TARGET_PREFIX], ["A"], [Target prefix])
+ AC_SUBST([DNS_UPDOWN_TYPE], ["= ;resolvconf_file"])
ROUTE=3D"/usr/sbin/route&q= uot;
have_tap_header=3D"yes"
= ac_cv_header_net_if_h=3D"no" # exists, but breaks things=
@@ -371,10 +388,12 @@
*-*-haiku*)
AC_DEFINE([TARGET_HAIKU], [1], [Are we running Haiku?])

AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["H"], [Target prefix])=

+ AC_SUBST([DNS_UPD= OWN_TYPE], ["haikuos_file"])
LIBS=3D"${LI= BS} -lnetwork"
;;
*)
AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["X"], [Target prefix]= )
+ AC_SUBST([DNS_UP= DOWN_TYPE], ["resolvconf_file"])
have_tap_head= er=3D"yes"
;;
esac
= @@ -1505,6 +1524,7 @@

sampledir=3D= "\$(docdir)/sample"
AC_SUBST([plugindir])=
+AC_SUBST([scriptdir])
AC_SUBST([sampledir])

AC_SU= BST([systemdunitdir])
@@ -1541,6 +1561,7 @@
Makefile

distro/Makefile
distro/syst= emd/Makefile
+ distro= /dns-scripts/Makefile
doc/Makefile
doc/= doxygen/Makefile
doc/doxygen/openvpn.doxyfile
<= span>diff --git a/distro/Makefile.am b/distro/Makefile.am
i= ndex 7a588da..b8fb85b 100644
--- a/distro/Makefile.am
+++ b/distro/Makefile.am
@@ -13,3 +13,7 @@
$(srcdir)/Makefile.in

SUBDI= RS =3D systemd
+
+if ENABLE_DNS_SCRIPT
+SUBDIRS +=3D dns-script= s
+endif
diff --git a/distro/dns-scripts/Makefile.am b/distro/dns-scripts/Makefi= le.am

new file mode 100644
index 0000000..e= c7bfde
--- /dev/null
+++ b/distro/dns-scrip= ts/Makefile.am
@@ -0,0 +1,28 @@
+#
+# OpenVPN -- An application to securely tunnel IP networks
+# over a sing= le UDP port, with support for SSL/TLS-based
+# session authentication and key exchan= ge,
+# pa= cket encryption, packet authentication, and
+# packet compression.
+#
+# Copyright (C) 2002-2024 OpenVPN Inc <sales@...2146...= ...>
+#
= +
+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)
+
+dn= s-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
new file mode 100644
in= dex 0000000..4a68499
--- /dev/null
+++ b/di= stro/dns-scripts/haikuos_file-dns-updown.sh
@@ -0,0 +1,82 @= @
+#!/bin/sh+#
+# Simple OpenVPN up/down script for modifying = Haiku OS resolv.conf
= +# (C) Copyright 2024 OpenVPN Inc <sales@...515...>
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#

+# Example env from openvpn (most are not applied):

+#
+# dev tun0
+# script-type dns-up
+# dns_search_domain_1 mycorp.in
= +# dns_search_domain_2 eu.myc= orp.com
+# dns_serv= er_1_address_1 192.168.99.254
+# dns_server_1_address_2 fd00::99:53
+# dns_server_1_port_1 53
+# dns_server_1_port_2 53
+# dns_server_1_resolve_domain_= 1 mycorp.in
+# dns_= server_1_resolve_domain_2 eu.mycorp.com
+# dns_server_1_dnssec true
+# dns_server_1_transport DoH
+# dns_server_1_sni dns.mycorp.in<= /span>
+#
+
+set -e +u
+
+conly_stand= ard_server_ports() {
= + i=3D1
+ while= true; do
+ ev= al addr=3D\"\$dns_server_${n}_address_${i}\"
+ [ -n "$addr" ] || retu= rn 0
+
+ eval port=3D\"\$dns_s= erver_${n}_port_${i}\"
+ [ -z "$port" -o "$port" =3D "53&qu= ot; ] || return 1
+
+ i=3D$(expr $i= + 1)
+ done

+}
+
+onf=3D/boot/system/settings/network/resolv.conf
+test -e "$conf" || exi= t 1
+case "${scr= ipt_type}" in
+d= ns-up)
+ n=3D1
+ while :; do<= br>+ eval addr=3D\"= \$dns_server_${n}_address_1\"
+ [ -n "$addr" ] || {
+ echo "setting DNS failed= , no compatible server profile"
+ exit 1
+ }
+
+ eval = dnssec=3D\"\$dns_server_${n}_dnssec\"
+ eval transport=3D\"\$dns_server_${= n}_transport\"
+= [ -z "$transport" -o "$transport" =3D "pla= in" ] \
+ = && [ -z "$dnssec" -o "$dnssec" =3D "n= o" ] \
+ = && only_standard_server_ports && break
+
+ n=3D$(expr $n + 1)
+ done
+
+ = eval addr1=3D\"\$dns_server_${n}_address_1\"
+ eval addr2=3D\"\$dns_server_${= n}_address_2\"
+= eval addr3=3D\"\$dns_server_${n}_address_3\"
+ text=3D"### openvpn ${dev} = begin ###\n"
+ = text=3D"${text}nameserver $addr1\n"
+ test -z "$addr2" || text=3D"= ${text}nameserver $addr2\n"
+ test -z "$addr3" || text=3D"${text}nameserv= er $addr3\n"
+
+ test -z "$dn= s_search_domain_1" || {
+ for i in $(seq 1 6); do
+ eval domains=3D\"$domains\$dns_search= _domain_${i} \" || break
+ done
+ }
+ text=3D"${text}### openvpn ${dev} end ###&quo= t;
+
+ sed -i "1i${text}" &qu= ot;$conf"
+ ;= ;
+dns-down)+ sed -i "/### openvp= n ${dev} begin ###/,/### openvpn ${dev} end ###/d" "$conf"
+ ;;
+esac

diff --git a/= distro/dns-scripts/openresolv-dns-updown.sh b/distro/dns-scripts/openresolv= -dns-updown.sh
new file mode 100644
index 0= 000000..731f9b5
--- /dev/null
+++ b/distro/= dns-scripts/openresolv-dns-updown.sh
@@ -0,0 +1,87 @@
+#!/bin/sh
+#

+# Simple OpenVPN up/down script for openresolv integr= ation
+# (C) Copyrigh= t 2016 Baptiste Daroussin
+# 2024 OpenVPN Inc <sales@...515...>
+#

+# SPDX-License-Identifier: BSD-2-Clause
= +#
+# Example env from openvpn (most are not applied= ):
+#
+# dev tun0

+# script-type dns-up
+# dns_search_domain_1 mycorp.in
+# dns_search_domain_2 = eu.mycorp.com
+# dn= s_server_1_address_1 192.168.99.254
+# dns_server_1_address_2 fd00::99:53
+# dns_server_1_port_1 53
+# dns_server_1_port_2 53
=
+# dns_server_1_resolve_d= omain_1 mycorp.in
+# = dns_server_1_resolve_domain_2 eu.mycorp.com
+# dns_server_1_dnssec true
+# dns_server_1_transport DoH+# dns_server_1_sni dns.myco= rp.in
+#
+
+set -e +u
+
+only_= standard_server_ports() {
+ i=3D1
+ = while true; do
+ = eval addr=3D\"\$dns_server_${n}_address_${i}\"
+ [ -n "$addr" ] ||= return 0
++ eval port=3D\"\$= dns_server_${n}_port_${i}\"
+ [ -z "$port" -o "$port" =3D "= 53" ] || return 1
+ i=3D$(ex= pr $i + 1)
+ done<= /span>
+}
+
+: ${script_type:=3Ddns-down}
+case "${script_type}" in
+dns-up)

+ n=3D1
+ while :; do
+ eval addr=3D\"\$dns_server_${n}_address_1\&quo= t;
+ [ -n &quo= t;$addr" ] || {
= + echo "setting DNS failed, no compatible server profile&qu= ot;
+ exit= 1
+ }<= br>+
+ 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.= .7ee66e9
--- /dev/null
+++ b/distro/dns-scr= ipts/resolvconf_file-dns-updown.sh
@@ -0,0 +1,82 @@<= 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%);">+

+ eval dnssec=3D\"\$dns_server_${n}_dns= sec\"
+ e= val transport=3D\"\$dns_server_${n}_transport\"
+ [ -z "$transport" -= o "$transport" =3D "plain" ] \
+ && [ -z "$dnssec&qu= ot; -o "$dnssec" =3D "no" ] \
+ && only_standard_server_p= orts && break
+
+ n=3D$(exp= r $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 $add= r1\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<= br>+ 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 ${d= ev} end ###/d" "$conf"
+ ;;
+esac
diff --git a/distro/dns-scripts/systemd-dns-updow= n.sh b/distro/dns-scripts/systemd-dns-updown.sh
new file mo= de 100644
index 0000000..c8e455e
--- /dev/n= ull
+++ b/distro/dns-scripts/systemd-dns-updown.sh@@ -0,0 +1,190 @@
+#!/bin/bash
+#<= /span>
+# dns-updown - add/r= emove openvpn provided DNS information
+#
= +# Copyright (C) 2024 OpenVPN Inc <sales@...515...>
+#
+# SPDX-License-Identifier: GPL-2.0
+#
+# Add/remove openvpn DNS settings from the env into/from
+# the system. Support= ed backends in this order:
+#
+# * syste= md-resolved
+# * re= solvconf
+#+# Example env from openvpn (n= ot all are always applied):
+#
+# dev tu= n0
+# script-type d= ns-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_s= erver_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 t= rue
+# dns_server_1= _transport DoH
+# d= ns_server_1_sni dns.mycorp.in
+#
+<= br>+function do_resolved_server= s {
+ local sni=3D= ""
+ loc= al transport_var=3Ddns_server_${n}_transport
+ local sni_var=3Ddns_server_${n}_sni
= + [ "${!transport_var}&= quot; =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]= "
+ f= i
+ addrs+= =3D"${addr}:${!port_var}${sni} "
+ else
+ addrs+=3D"${addr}${sni} "
<= span style=3D"color: hsl(120, 100%, 40%);">+ fi
+ i=3D$((i+1))
+ done
+
+ resolvectl dns "$dev" $addrs
+}
+
+function= do_resolved_domains {
+ for domain_var in ${!dns_search_domain_*}; do
+ list+=3D"${!domain_= var} "
+ done=
+ local domain_va= r=3Ddns_server_${n}_resolve_domain_1
+ if [ -z "${!domain_var}" ]; then
<= span style=3D"color: hsl(120, 100%, 40%);">+ resolvectl default-rout= e "$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
+ [[ "$list" = =3D~ (^| )"${!domain_var}"( |$) ]] && continue
= + list+=3D"~${!= domain_var} "
+ = i=3D$((i+1))
+ done
+ = fi
+
+ resolvectl domain "$dev&qu= ot; $list
+}+
+function do_resolved_dnssec {
+ local dnssec_var=3Ddns_server_${n= }_dnssec
+ if [ &q= uot;${!dnssec_var}" =3D "optional" ]; then
+ resolvectl dnssec "$dev&= quot; allow-downgrade
+ elif [ "${!dnssec_var}" =3D "yes" ]; then<= br>+ resolvectl dnssec &= quot;$dev" true
= + else
+ re= solvectl dnssec "$dev" false
+ fi
+}
+
<= span style=3D"color: hsl(120, 100%, 40%);">+function do_resolved_dnsovertls= {
+ local transpo= rt_var=3Ddns_server_${n}_transport
+ if [ "${!transport_var}" =3D "DoT" ]= ; then
+ resol= vectl 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<= br>+ [ -n "${!addr_= var}" ] || {
+ = echo "setting DNS failed, no compatible server profile"=
+ return = 1
+ }+
+ local transport_var=3Ddns_server_${n}_t= ransport
+ [ -= n "${!transport_var}" -a "${!transport_var}" =3D "= DoH" ] || break
= +
+ 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_dnsse= c
+ do_resolve= d_dnsovertls
+ els= e
+ echo "= ;unsetting DNS using resolvectl"
+ resolvectl revert "$dev"
+ fi
+
+ return 0
+}
+
+function only_standard_server_ports= {
+ local i=3D1
+ while :; do

+ local addr_var=3D= dns_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 ser= ver_addr_var=3Ddns_server_${n}_address_1
+ [ -n "${!server_addr_var}" ] || {

+ echo "= ;setting DNS failed, no compatible server profile"
+ return 1
+ }
+
+ local dnssec_var=3Ddns_server_${n}_dnssec
+ local transport_var=3Ddns_ser= ver_${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..0a93509 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-script``
++ 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-script``.

#. ``--tls-verify``

@@ -38,9 +42,13 @@
<= br> Executed in ``--mode server`` mode on client instance shutdown= .

+= #. ``--dns-script``
+=
+ 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-script``.

#. ``--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,21 @@
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 command that comes with
+ openvpn. If ``cmd``= is 'disable' no command is executed to set ``--dns``
+ options. If you write your own = command, please make sure to ignore ``--dns``

+ server profiles that cannot be applied. Port, D= NSSEC and secure transport
+ settings need to be adhered to. If split DNS is not possible a ful= l redirect
+ can be = used as a fallback. If not all of the server addresses or search domains
+ can be configured, a= pply them on the order they are listed in.
+
+ Note that ``--dns-script`` is not supported on Windows. DNS will
+ always be set by the s= ervice there. Also note that no DNS related
+ ``--dhcp-option`` will cause this hook to run. In= stead foreign_option
= + environment values will be passed to ``--up`` and ``--down`` if no ``--d= ns``
+ option causes= the old behavior to be overridden.
+
--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 +684,7 @@
names). Set prior to = ``--up`` or ``--down`` script execution.

= :code:`dns_*`
- The= ``--dns`` configuration options will be made available to script+ The ``--dns`` configurati= on options will be made available to ``--dns-script``
= 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 refe= r to the documentation for ``--dns``.
diff --git a/src/open= vpn/Makefile.am b/src/openvpn/Makefile.am
index 37af683..66= 64aae 100644
--- a/src/openvpn/Makefile.am
= +++ b/src/openvpn/Makefile.am
@@ -30,7 +30,8 @@
<= span> $(OPTIONAL_LZ4_CFLAGS) \

$(OPTIONAL_PKCS11_HELPER_= CFLAGS) \
$(OPTIONAL_INOTIFY_CFLAGS) \
- -DPLUGIN_LIBDIR=3D\"${plugindir}\&= quot;
+ -DPLUGIN_LIBD= IR=3D\"${plugindir}\" \
+ -DDNS_UPDOWN_PATH=3D\"${scriptdir}/dns-updown\"

if WIN32
# we want unicod= e entry point but not the macro
diff --git a/src/openvpn/dn= s.c b/src/openvpn/dns.c
index 12f88ab..4e728e9 100644
--- a/src/openvpn/dns.c
+++ b/src/openvpn/dns.c<= /span>
@@ -30,6 +30,7 @@
#include "dns.h&quo= t;
#include "socket.h"
#include= "options.h"
+ clone.script =3D o->script;
+ clone.user_defined_sc= ript =3D o->user_defined_script;

= return clone;
}
@@ -519,6 +522,54 @@
send_msg_iservice(o->msg_channel, &nrpt, sizeof(nrpt= ), &ack, "DNS");
}
<= br>+#else /* ifdef _WIN32 */
+
+static void
+script_env_set(bool up, const struct dns_options = *o, const struct tuntap *tt, struct env_set *es)
+{
+ setenv_str(es, "dev", tt->actual_name);<= br>+ setenv_str(es, "sc= ript_type", up ? "dns-up" : "dns-down");+ setenv_dns_options(o, es)= ;
+}
+
+static int
+do_run_up_down_script(bool up, const struct dns_options *o, cons= t struct tuntap *tt)
= +{
+ struct gc_are= na gc =3D gc_new();
+= struct argv argv =3D argv_new();
+ struct env_set *es =3D env_set_create(&gc);=
+
+ script_env_set(up, o, tt, es);
= +
+ argv_printf(&argv, "%s", o->= script);
+ argv_ms= g(M_INFO, &argv);
+ int res;
+ i= f (o->user_defined_script)
+ {
+ = res =3D openvpn_run_script(&argv, es, S_EXITCODE, "dns script&= quot;);
+ }=
+ else
+ {
+ res =3D openvpn_execve_check(&argv, es,= S_EXITCODE, "WARNING: Failed running dns script");
+ }

+ argv_free(&argv);
+ gc_free(&gc);
+ return res;
+}
+
+sta= tic void
+run_up_down= _script(bool up, struct options *o, const struct tuntap *tt)
+{

+ if (!o->dns_options.script)
+ {
+ return;
+ }
+
+ int statu= s;
+ status =3D do= _run_up_down_script(up, &o->dns_options, tt);
+ msg(M_INFO, "dns script exited w= ith status %d", status);
+}
+<= br> #endif /* _WIN32 */

void
@@ -637,5 +688,7 @@

#ifdef _W= IN32
run_up_down_service(up, o, tt);
+#else
+ run_up_down_script(up, o, tt);
= #endif /* ifdef _WIN32 */
}
diff --git a/= src/openvpn/dns.h b/src/openvpn/dns.h
index f24e30b..38ced7= 9 100644
--- a/src/openvpn/dns.h
+++ b/src/= openvpn/dns.h
@@ -73,6 +73,8 @@
struct= dns_server *servers_prepull;
struct dns_server *serve= rs;
struct gc_arena gc;
+ const char *script;
+ bool user_defined_script;
= };

/**
diff --git a/src= /openvpn/options.c b/src/openvpn/options.c
index dc8c880..8= 775ea3 100644
--- a/src/openvpn/options.c
+= ++ b/src/openvpn/options.c
@@ -528,6 +528,7 @@
" sni <domain> : DNS server name indic= ation\n"

"--dns search-domains <domain>= ; [domain ...]:\n"
" Add do= mains to DNS domain search list\n"
+ "--dns-script cmd : Run cmd as user defined dn= s configuration script\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"
@@ -919,6 +9= 20,8 @@
#ifndef ENABLE_DCO
o->disa= ble_dco =3D true;
#endif /* ENABLE_DCO */
+
+ o->dns_options.script =3D DNS_UPDOWN_PATH;
}

void
@@ -8= 148,6 +8151,28 @@
to->ip_win32_defined =3D true= ;
}
#endif /* ifdef _WIN32 */<= br>+ else if (streq(p[0], &q= uot;dns-script") && p[1])
+ {
+ VERIFY_PERMISSION(OPT_P_SCRIPT);
+ if (!no_more_than_n_args(msglevel, p, 2, = NM_QUOTE_HINT))
+ = {
+ go= to err;
+ }
+ if (streq(p[1]= , "disable"))
+ {
+ = options->dns_options.script =3D NULL;
+ }
+ else
+ {
+= if (options->dns_options.user_defined_script =3D=3D false)
+ {<= br>+ /* Unset th= e default script to prevent warnings */
+ options->dns_options.script =3D NULL= ;
+ }
+ set_user_scr= ipt(options, &options->dns_options.script, p[1], p[0], false);
+ options->d= ns_options.user_defined_script =3D true;
+ }
+ }
else if (streq(p[0], "dns")= && p[1])
{
VERIFY_PE= RMISSION(OPT_P_DHCPDNS);

To view, visit= change 8= 38. To unsubscribe, 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: 9
Gerrit-Owner: d12fk <heiko@...515...>
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: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newpatchset
--MF3GbymtvWI=--