From: "d12fk (Code Review)" Date: Thu, 12 Dec 2024 07:47:46 +0000 References: Reply-To: heiko@...515..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <512869b830f8378ca8280553affef768e5dd840b-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="KTZrXPDD+Bw="; 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 --KTZrXPDD+Bw= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, plaisthos. Hello plaisthos, flichtenheld, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/838?usp=3Demail to review the following change. 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/freebsd-dns-updown.sh A distro/dns-scripts/linux-dns-updown.sh A distro/dns-scripts/other-dns-updown.sh M src/openvpn/Makefile.am M src/openvpn/dns.c M src/openvpn/dns.h M src/openvpn/options.c 12 files changed, 344 insertions(+), 3 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/38/838/1 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 ca58cd7..249f7bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,7 @@ option(ENABLE_PKCS11 "BUILD with pkcs11-helper" ON) option(USE_WERROR "Treat compiler warnings as errors (-Werror)" ON) +set(DNS_UPDOWN_PATH "/usr/libexec/openvpn/dns-updown" CACHE STRING "Defaul= t location for the DNS up/down script") set(PLUGIN_DIR /usr/local/lib/openvpn/plugins CACHE FILEPATH "Location of = the plugin directory") # Create machine readable compile commands @@ -565,6 +566,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..4bd9a69 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([TARGET_OS], ["linux"]) 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([TARGET_OS], ["solaris"]) 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([TARGET_OS], ["openbsd"]) ;; *-*-freebsd*) AC_DEFINE([TARGET_FREEBSD], [1], [Are we running on FreeBSD?]) AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["F"], [Target prefix]) + AC_SUBST([TARGET_OS], ["freebsd"]) ;; *-*-netbsd*) AC_DEFINE([TARGET_NETBSD], [1], [Are we running NetBSD?]) AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["N"], [Target prefix]) + AC_SUBST([TARGET_OS], ["netbsd"]) ;; *-*-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([TARGET_OS], ["darwin"]) 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([TARGET_OS], ["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([TARGET_OS], ["dragonfly"]) ;; *-aix*) AC_DEFINE([TARGET_AIX], [1], [Are we running AIX?]) AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["A"], [Target prefix]) + AC_SUBST([TARGET_OS], ["aix"]) ROUTE=3D"/usr/sbin/route" have_tap_header=3D"yes" ac_cv_header_net_if_h=3D"no" # exists, but breaks things @@ -375,6 +392,7 @@ ;; *) AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["X"], [Target prefix]) + AC_SUBST([TARGET_OS], ["other"]) have_tap_header=3D"yes" ;; esac @@ -1505,6 +1523,7 @@ sampledir=3D"\$(docdir)/sample" AC_SUBST([plugindir]) +AC_SUBST([scriptdir]) AC_SUBST([sampledir]) AC_SUBST([systemdunitdir]) @@ -1541,6 +1560,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..610bf18 --- /dev/null +++ b/distro/dns-scripts/Makefile.am @@ -0,0 +1,27 @@ +# +# 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 \ + linux-dns-updown.sh \ + freebsd-dns-updown.sh \ + other-dns-updown.sh + +script_SCRIPTS =3D \ + dns-updown + +CLEANFILES =3D $(script_SCRIPTS) + +dns-updown: @TARGET_OS@...2841... + cp ${srcdir}/@TARGET_OS@...2841... $@ + +all: $(script_SCRIPTS) diff --git a/distro/dns-scripts/freebsd-dns-updown.sh b/distro/dns-scripts/= freebsd-dns-updown.sh new file mode 100644 index 0000000..12d742d --- /dev/null +++ b/distro/dns-scripts/freebsd-dns-updown.sh @@ -0,0 +1,55 @@ +#!/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): +# +# 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 +[ -z "${dns_vars_file}" ] || . "${dns_vars_file}" +: ${script_type:=3Ddns-down} +case "${script_type}" in +dns-up) + { + i=3D1 + maxns=3D3 + while :; do + maxns=3D$((maxns - 1)) + [ $maxns -gt 0 ] || break + eval option=3D\"\$dns_server_1_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/linux-dns-updown.sh b/distro/dns-scripts/li= nux-dns-updown.sh new file mode 100644 index 0000000..4b459f5 --- /dev/null +++ b/distro/dns-scripts/linux-dns-updown.sh @@ -0,0 +1,128 @@ +#!/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): +# +# 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 +# + +[ -z "${dns_vars_file}" ] || . "${dns_vars_file}" + +function do_resolved_servers { + local sni=3D"" + [ "$dns_server_1_transport" =3D "DoT" ] && sni=3D"#$dns_server_1_sni" + + local addrs=3D"" + for addr_var in ${!dns_server_1_address_*}; do + local port_var=3D"${addr_var/address/port}" + local addr=3D"${!addr_var}" + 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 + 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 + if [ -z "${!dns_server_1_resolve_domain_*}" ]; then + resolvectl default-route "$dev" true + list+=3D"~." + else + resolvectl default-route "$dev" false + for domain_var in ${!dns_server_1_resolve_domain_*}; do + [[ "$list" =3D~ (^| )"${!domain_var}"( |$) ]] && continue + list+=3D"~${!domain_var} " + done + fi + + resolvectl domain "$dev" $list +} + +function do_resolved_dnssec { + if [ "$dns_server_1_dnssec" =3D "optional" ]; then + resolvectl dnssec "$dev" allow-downgrade + elif [ "$dns_server_1_dnssec" =3D "yes" ]; then + resolvectl dnssec "$dev" true + else + resolvectl dnssec "$dev" false + fi +} + +function do_resolved_dnsovertls { + if [ "$dns_server_1_transport" =3D "DoT" ]; then + resolvectl dnsovertls "$dev" true + else + resolvectl dnsovertls "$dev" false + fi +} + +function do_resolved { + [[ "$(readlink /etc/resolv.conf)" =3D~ systemd ]] || return 1 + + if [ "$script_type" =3D "dns-up" ]; then + do_resolved_servers + do_resolved_domains + do_resolved_dnssec + do_resolved_dnsovertls + else + resolvectl revert "$dev" + fi + + return 0 +} + +function do_resolvconf { + [ -x /sbin/resolvconf ] || return 1 + + if [ "$script_type" =3D "dns-up" ]; then + 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 + /sbin/resolvconf -d "$dev" + fi + + return 0 +} + +do_resolved || do_resolvconf diff --git a/distro/dns-scripts/other-dns-updown.sh b/distro/dns-scripts/ot= her-dns-updown.sh new file mode 100644 index 0000000..e3bcf0d --- /dev/null +++ b/distro/dns-scripts/other-dns-updown.sh @@ -0,0 +1,50 @@ +#!/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): +# +# 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 + +conf=3D/etc/resolv.conf +test -e "$conf" || exit 1 +test -z "${dns_vars_file}" || . "${dns_vars_file}" +case "${script_type}" in +dns-up) + text=3D"### openvpn ${dev} begin ###\n" + text=3D"${text}nameserver $dns_server_1_address_1\n" + test -z "$dns_server_1_address_2" || + text=3D"${text}nameserver $dns_server_1_address_2\n" + test -z "$dns_server_1_address_3" || + text=3D"${text}nameserver $dns_server_1_address_3\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/src/openvpn/Makefile.am b/src/openvpn/Makefile.am index ecb2bcf..d8beabd 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 4528a9c..2f21ed5 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,7 @@ 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; return clone; } @@ -519,6 +521,42 @@ 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 =3D openvpn_run_script(&argv, es, S_FATAL|S_EXITCODE, "dns scr= ipt"); + + argv_free(&argv); + gc_free(&gc); + return res; +} + +static void +run_up_down_script(bool up, struct options *o, const struct tuntap *tt) +{ + 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 +675,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..39a3393 100644 --- a/src/openvpn/dns.h +++ b/src/openvpn/dns.h @@ -73,6 +73,7 @@ struct dns_server *servers_prepull; struct dns_server *servers; struct gc_arena gc; + const char *script; }; /** diff --git a/src/openvpn/options.c b/src/openvpn/options.c index cc723ca..319f370 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -907,6 +907,8 @@ #ifndef ENABLE_DCO o->disable_dco =3D true; #endif /* ENABLE_DCO */ + + o->dns_options.script =3D DNS_UPDOWN_PATH; } void @@ -8073,6 +8075,15 @@ 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; + } + set_user_script(options, &options->dns_options.script, p[1], p[0],= false); + } 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: 1 Gerrit-Owner: d12fk Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-MessageType: newchange --KTZrXPDD+Bw= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, plaisthos.

d12fk would like plaisthos and flichtenheld to review t= his change.

Vi= ew Change

dns: apply settings via script on unixoid systems

This= introduces a new script hook, the dns-script and implements such a
scri= pt 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
modifyi= ng how names are resolved when the VPN pushes some --dns
settings.
The default dns script is part of the distribution and is installed with<= br>openvpn. You can change the path the script is located at as a compiletime option, defaults to libexecdir. There's also a new runtime optio= n
--dns-script, which can run a custom script.

Change-Id: Ifbe4ff= b44d3bfcaa50adb38cacb3436fcdc71b10
Signed-off-by: Heiko Hund <heiko@...220...= ..2662...>
---
M .gitignore
M CMakeLists.txt
M configure.ac<= br>M distro/Makefile.am
A distro/dns-scripts/Makefile.am
A distro/dns= -scripts/freebsd-dns-updown.sh
A distro/dns-scripts/linux-dns-updown.sh<= br>A distro/dns-scripts/other-dns-updown.sh
M src/openvpn/Makefile.amM src/openvpn/dns.c
M src/openvpn/dns.h
M src/openvpn/options.c
1= 2 files changed, 344 insertions(+), 3 deletions(-)

git pull ssh:=
//gerrit.openvpn.net:29418/openvpn refs/changes/38/838/1
diff --git=
 a/.gitignore b/.gitignore
index db8bb73..04523af 100644
--- a/.gitignore
+++ b/.gitignore
<= span>@@ -49,6 +49,7 @@

/doc/doxygen/latex/
/doc/doxygen/openvpn.doxyfile
distro/systemd/*.service
+distro/dns-scripts/dn= s-updown
sample/sample-keys/sample-ca/
ve= ndor/cmocka_build
vendor/dist
diff --git a= /CMakeLists.txt b/CMakeLists.txt
index ca58cd7..249f7bc 100= 644
--- a/CMakeLists.txt
+++ b/CMakeLists.t= xt
@@ -40,6 +40,7 @@
option(ENABLE_PKCS11 = "BUILD with pkcs11-helper" ON)
option(USE_WERROR= "Treat compiler warnings as errors (-Werror)" ON)

+set(DNS_UPDOWN_P= ATH "/usr/libexec/openvpn/dns-updown" CACHE STRING "Default = location for the DNS up/down script")
set(PLUGIN_DIR = /usr/local/lib/openvpn/plugins CACHE FILEPATH "Location of the plugin = directory")

# Create machine readab= le compile commands
@@ -565,6 +566,8 @@

add_library_deps(openvpn)

+target_compile_options(openvpn PRIV= ATE -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 9777e3= 6..4bd9a69 100644
--- a/configure.ac
+++ b/= configure.ac
@@ -315,37 +315,50 @@
plugin= dir=3D"\${libdir}/openvpn/plugins"
fi
=
+AC_ARG_VAR([= SCRIPTDIR], [Path of script directory @<:@default=3DPKGLIBEXECDIR@:>@= ])
+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_S= CRIPT],[true])
case "$host" in
= *-*-linux*)
AC_DEFINE([TARGET_LINUX], [1], [Are we runn= ing on Linux?])
- AM_C= ONDITIONAL([TARGET_LINUX], [true])
AC_DEFINE_UNQUOTED([T= ARGET_PREFIX], ["L"], [Target prefix])
+ AC_SUBST([TARGET_OS], ["linux"])
have_sitnl=3D"yes"
pkg_confi= g_required=3D"yes"
;;
*-*-so= laris*)
AC_DEFINE([TARGET_SOLARIS], [1], [Are we running= on Solaris?])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], [&quo= t;S"], [Target prefix])
+ AC_SUBST([TARGET_OS], ["solaris"])
= CPPFLAGS=3D"$CPPFLAGS -D_XPG4_2"
test -x /bin/= bash && SHELL=3D"/bin/bash"
;;<= br> *-*-openbsd*)
AC_DEFINE([TARGET_OPENBSD], [1]= , [Are we running on OpenBSD?])
AC_DEFINE_UNQUOTED([TARG= ET_PREFIX], ["O"], [Target prefix])
+ AC_SUBST([TARGET_OS], ["openbsd"])
;;
*-*-freebsd*)
AC_D= EFINE([TARGET_FREEBSD], [1], [Are we running on FreeBSD?])
= AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["F"], [Target prefix])
+ AC_SUBST([TARGET_OS]= , ["freebsd"])
;;
*-*-netbsd= *)
AC_DEFINE([TARGET_NETBSD], [1], [Are we running NetBS= D?])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["N"]= , [Target prefix])
+ = AC_SUBST([TARGET_OS], ["netbsd"])
;; *-*-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([TARGET_OS], [= "darwin"])
have_tap_header=3D"yes"
ac_cv_type_struct_in_pktinfo=3Dno
;;
@@ -353,6 +366,8 @@
AC_DEFINE([TARGET_WIN= 32], [1], [Are we running WIN32?])
AC_DEFINE([ENABLE_DCO= ], [1], [DCO is always enabled on Windows])
AC_DEFINE_UN= QUOTED([TARGET_PREFIX], ["W"], [Target prefix])
+ AM_CONDITIONAL([ENABLE_DNS_SCRIPT],= [false])
+ AC_SUBST(= [TARGET_OS], ["windows"])
CPPFLAGS=3D"${C= PPFLAGS} -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_DRA= GONFLY], [1], [Are we running on DragonFlyBSD?])
AC_DEFI= NE_UNQUOTED([TARGET_PREFIX], ["D"], [Target prefix])
+ AC_SUBST([TARGET_OS], ["d= ragonfly"])

;;
*-aix*)
= AC_DEFINE([TARGET_AIX], [1], [Are we running AIX?])
AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["A"], [Target prefix])<= /span>
+ AC_SUBST([TARGET_O= S], ["aix"])
ROUTE=3D"/usr/sbin/route&quo= t;
have_tap_header=3D"yes"
= ac_cv_header_net_if_h=3D"no" # exists, but breaks things@@ -375,6 +392,7 @@
;;
*)

AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["X"], [Target= prefix])
+ AC_SUBST= ([TARGET_OS], ["other"])
have_tap_header=3D&qu= ot;yes"
;;
esac
@@= -1505,6 +1523,7 @@

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

AC_SUBST([sampledir])

AC_SUBST([sys= temdunitdir])
@@ -1541,6 +1560,7 @@
Makef= ile
distro/Makefile
distro/systemd/Make= file
+ distro/dns-scr= ipts/Makefile
doc/Makefile
doc/doxygen/= Makefile
doc/doxygen/openvpn.doxyfile
dif= f --git a/distro/Makefile.am b/distro/Makefile.am
index 7a5= 88da..b8fb85b 100644
--- a/distro/Makefile.am
+++ b/distro/Makefile.am
@@ -13,3 +13,7 @@
$(srcdir)/Makefile.in

SUBDIRS =3D s= ystemd
+
+if ENABLE_DNS_SCRIPT
<= span style=3D"color: hsl(120, 100%, 40%);">+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..610bf18
--- /dev/null
+++ b/distro/dns-scripts/Makef= ile.am
@@ -0,0 +1,27 @@
+#
+# OpenVPN -- An application to securely tunnel IP networks
+# over a single UDP p= ort, with support for SSL/TLS-based

+# session authentication and key exchange,
+# packet enc= ryption, packet authentication, and
+# packet compression.
+#
+# Copyright (C) 2002-2024 OpenVPN Inc <sales@...515...><= /span>
+#
+
+MAINTAINERCLEANFILES =3D \
+ $(srcdir)/Makefile.in
+
+EXTRA_DIST =3D \
+ linux-dns-updown.sh \
+ freebsd-dns-updown.sh \
+ other-dns-updown.sh
+
+scri= pt_SCRIPTS =3D \
+ dn= s-updown
+
= +CLEANFILES =3D $(script_SCRIPT= S)
+
+dns-updown: @TARGET_OS@...2841...

+ cp ${srcdir}/@TARGET_= OS@...2841... $@
+
+all: $(script_SCRIPTS)=
diff --git a/distro/dns-scripts/freebsd-dns-updown.sh b/di= stro/dns-scripts/freebsd-dns-updown.sh
new file mode 100644=
index 0000000..12d742d
--- /dev/null
+++ b/distro/dns-scripts/freebsd-dns-updown.sh
@= @ -0,0 +1,55 @@
+#!/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):
+#
+# 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>+set -e +u
+[ -z "${dns_vars_file}" ] |= | . "${dns_vars_file}"
+: ${script_type:=3Ddns-down}
+case "${script_type}" in
+dns-up)
+ {
+ i=3D1
+ maxns=3D3
+ [ $maxns -gt 0 ] || break
+ eval option=3D\"\$dns_= server_1_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 ${o= ption}"

+ = done
+ } | /sbin/= resolvconf -a "${dev}"
+ ;;
+= dns-down)
+ /sbin/= resolvconf -d "${dev}" -f
+ ;;
+#!= /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):
+= #
+# 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%);">+[ -z "${dns_vars_file}&quo= t; ] || . "${dns_vars_file}"

+
+= function do_resolved_servers {
+ local sni=3D""
+ [ "$dns_server_1_transport" =3D "DoT= " ] && sni=3D"#$dns_server_1_sni"
+
+ local addrs=3D""
+ for addr_var in ${!dns_server_1_address_*}= ; do
+ local p= ort_var=3D"${addr_var/address/port}"
+ local addr=3D"${!addr_var}"
+ 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

+ done
+
+ resolvectl dns "$dev" $addrs
+}
+
+function = do_resolved_domains {
+ local list=3D""
+ for domain_var in ${!dns_search_domain_*}; do
+ list+=3D"${!domain_v= ar} "

+ done<= /span>
+ if [ -z "${= !dns_server_1_resolve_domain_*}" ]; then
+ resolvectl default-route "$dev"= true
+ list+= =3D"~."
+ = else
+ resol= vectl default-route "$dev" false
+ for domain_var in ${!dns_server_1_resolve_do= main_*}; do
+ = [[ "$list" =3D~ (^| )"${!domain_var}"( |$) ]] &= & continue
+ = list+=3D"~${!domain_var} "
+ done
+ fi
+
+ resolvectl = domain "$dev" $list
+}
+<= br>+function do_resolved_dnssec= {
+ if [ "$d= ns_server_1_dnssec" =3D "optional" ]; then
+ resolvectl dnssec "$dev&= quot; allow-downgrade
+ elif [ "$dns_server_1_dnssec" =3D "yes" ]; then
+ resolvectl dn= ssec "$dev" true
+ else
+ = resolvectl dnssec "$dev" false
+ fi
+}
+
+function do_resolved_dnso= vertls {
+ if [ &q= uot;$dns_server_1_transport" =3D "DoT" ]; then
+ resolvectl dnsovertls &qu= ot;$dev" true

+ = else
+ reso= lvectl dnsovertls "$dev" false
+ fi
+}
++function do_resolved {=
+ [[ "$(readlink /e= tc/resolv.conf)" =3D~ systemd ]] || return 1
+
+ if [ "$script_type" =3D "dns-up" ]; then=
+ do_resolved= _servers
+ do_= resolved_domains
+ = do_resolved_dnssec
+ do_resolved_dnsovertls
+ else
+ resolvectl revert "$dev"
+ fi
+
+ r= eturn 0
+}
= +
+function do_resolvconf {
+ [ -x /sbin/resolvconf ] || return 1
+
+ if [ "$script_type" =3D &qu= ot;dns-up" ]; then
+ local domains=3D""
+ for domain_var in ${!dns_search_domain_*}; do<= /span>
+ 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
+ /sbin/resolvconf -d "$dev"
+ fi
+
+ return 0
+}
+
+do_resolved || do_resolv= conf
diff --git a/distro/dns-scripts/other-dns-updown.sh b/= distro/dns-scripts/other-dns-updown.sh
new file mode 100644=
index 0000000..e3bcf0d
--- /dev/null
+++ b/distro/dns-scripts/other-dns-updown.sh
@@ = -0,0 +1,50 @@
+#!/bin= /sh
+#
+# Simple OpenVPN up/down script fo= r modifying /etc/resolv.conf

+# (C) Copyright 2024 OpenVPN Inc <sales@...515...>+#
+# SPDX-License-Identifier: BSD-2-Clause<= br>+#
+# Example env from openvpn (most are not appl= ied):
+#
+# dns_search_domain_1 mycorp.i= n

+# dns_search_dom= ain_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
<= br>+# dns_server_1_port_2 53<= /span>
+# dns_server_1_res= olve_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
+
+conf=3D/etc/resolv.conf
+test -e "$conf" || exit 1
+test -z "${dns_vars_file}" || . "${= dns_vars_file}"
= +case "${script_type}" in
+dns-up)
+ text=3D"### openvpn ${dev} begin ###\n"
+ text=3D"${text}nameserver $d= ns_server_1_address_1\n"
+ test -z "$dns_server_1_address_2" ||
+ text=3D"${text}namese= rver $dns_server_1_address_2\n"

+ test -z "$dns_server_1_address_3" ||<= br>+ text=3D"${text= }nameserver $dns_server_1_address_3\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&= quot;
+ }+ 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/src/openvpn/Makefile.am b/src/openvpn/Makefile.am=
index ecb2bcf..d8beabd 100644
--- a/src/op= envpn/Makefile.am
+++ b/src/openvpn/Makefile.am
<= span>@@ -30,7 +30,8 @@

$(OPTIONAL_LZ4_CFLAGS) \ $(OPTIONAL_PKCS11_HELPER_CFLAGS) \
$(OPTIONAL_IN= OTIFY_CFLAGS) \
- -DPLU= GIN_LIBDIR=3D\"${plugindir}\"
+ -DPLUGIN_LIBDIR=3D\"${plugindir}\" \<= br>+ -DDNS_UPDOWN_PATH=3D\"= ;${scriptdir}/dns-updown\"

if WIN32=
# we want unicode entry point but not the macrodiff --git a/src/openvpn/dns.c b/src/openvpn/dns.c
i= ndex 4528a9c..2f21ed5 100644
--- a/src/openvpn/dns.c=
+++ b/src/openvpn/dns.c
@@ -30,6 +30,7 @@<= br> #include "dns.h"
#include "socket= .h"
#include "options.h"
+#include "run_command.h"

#ifdef _WIN32
#include &qu= ot;win32.h"
@@ -262,6 +263,7 @@
c= lone.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);
+ cl= one.script =3D o->script;

return = clone;
}
@@ -519,6 +521,42 @@
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, 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_script(bool up, const struct dns_options *o, const str= uct tuntap *tt)
+{
+ struct gc_arena gc= =3D gc_new();
+ s= truct argv argv =3D argv_new();
+ struct env_set *es =3D env_set_create(&gc);
<= span style=3D"color: hsl(120, 100%, 40%);">+

+ script_env_set(up, o, tt, es);
+

+ argv_printf(&argv, "%s", o->scrip= t);
+ argv_msg(M_I= NFO, &argv);
+ = int res =3D openvpn_run_script(&argv, es, S_FATAL|S_EXITCODE, "dn= s script");
+
+ argv_free(&arg= v);
+ gc_free(&= ;gc);
+ return res= ;
+}
+
+static void
+run_up_down_script(bool up, struct options *o, const struct tun= tap *tt)
+{+ int status;
+ status =3D do_run_up_down_scr= ipt(up, &o->dns_options, tt);

+ msg(M_INFO, "dns script exited with status %d&quo= t;, status);
+}
+
#endif = /* _WIN32 */

void
@@ -63= 7,5 +675,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..39a3393 100644--- a/src/openvpn/dns.h
+++ b/src/openvpn/dns.h
@@ -73,6 +73,7 @@
struct dns_server *serv= ers_prepull;
struct dns_server *servers;
struct gc_arena gc;

+ const char *script;
};

/**
diff --git a/src/openvpn/options.c b/src/op= envpn/options.c
index cc723ca..319f370 100644
--- a/src/openvpn/options.c

+++ b/src/openvpn/options.c<= /span>
@@ -907,6 +907,8 @@
#ifndef ENABLE_DCO
o->disable_dco =3D true;
#endif /* EN= ABLE_DCO */
+<= br>+ o->dns_options.scrip= t =3D DNS_UPDOWN_PATH;
}

void

@@ -8073,6 +8075,15 @@
to-&= gt;ip_win32_defined =3D true;
}
#endi= f /* ifdef _WIN32 */
= + else if (streq(p[0], "dns-script") && p[1])+ {
+ VERIFY_PERMISSION(OPT_P_SCRIPT);<= /span>
+ if (!no_more= _than_n_args(msglevel, p, 2, NM_QUOTE_HINT))
+ {
+ goto err;
+ }
+ set_user_script(options, &options->dns_options.script= , p[1], p[0], false);
+ }
else if (streq(p[0], "dns") &&am= p; p[1])
{
VERIFY_PERMISSION(= OPT_P_DHCPDNS);

To view, visit change 838. = 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: 1
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: newchange
--KTZrXPDD+Bw=--