From: "cron2 (Code Review)" Date: Mon, 14 Jul 2025 16:18:28 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., heiko@...515..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="ausPM86f1c0="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: prevent search domain races with macOS dns-updown List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: d12fk Cc: plaisthos , flichtenheld , openvpn-devel --ausPM86f1c0= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/10= 73?usp=3Demail ) Change subject: prevent search domain races with macOS dns-updown ...................................................................... prevent search domain races with macOS dns-updown When connections go up and down there are situations where search domains of a split DNS connection are either lost, or survive the lifetime of the connction. This can happen when there is also a connection that modifies the global DNS setting. When it backs-up the global settings before modifying them, or when it restores the backup, the search domains could contain or miss VPN domains from other connections, leading to misconfiguration. The fix is to also update the backed-up search domains when a split DNS connection comes up or goes down. That way the backup is always up to date and restoring it will keep the global search domains as expected. Change-Id: Ide2cddad193c636eb440c9752751176dae0a6897 Signed-off-by: Heiko Hund Acked-by: Arne Schwabe Message-Id: <20250714160903.7479-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg32= 127.html Signed-off-by: Gert Doering --- M distro/dns-scripts/macos-dns-updown.sh 1 file changed, 67 insertions(+), 20 deletions(-) diff --git a/distro/dns-scripts/macos-dns-updown.sh b/distro/dns-scripts/ma= cos-dns-updown.sh index c15abaa..f0640ee 100644 --- a/distro/dns-scripts/macos-dns-updown.sh +++ b/distro/dns-scripts/macos-dns-updown.sh @@ -29,14 +29,49 @@ [ -z "${dns_vars_file}" ] || . "${dns_vars_file}" itf_dns_key=3D"State:/Network/Service/openvpn-${dev}/DNS" -dns_backup_key=3D"State:/Network/Service/openvpn-${dev}/DnsBackup" -dns_backup_key_pattern=3D"State:/Network/Service/openvpn-.*/DnsBackup" function primary_dns_key { local uuid=3D$(echo "show State:/Network/Global/IPv4" | /usr/sbin/scut= il | grep "PrimaryService" | cut -d: -f2 | xargs) echo "Setup:/Network/Service/${uuid}/DNS" } +function dns_backup_key { + local key=3D"$(echo "list State:/Network/Service/openvpn-.*/DnsBackup"= | /usr/sbin/scutil | cut -d=3D -f2 | xargs)" + if [[ "${key}" =3D~ no\ key ]]; then + echo "State:/Network/Service/openvpn-${dev}/DnsBackup" + else + echo "${key}" + fi +} + +function property_value { + local key=3D"$1" + local prop=3D"$2" + + [ -n "${key}" -a -n "${prop}" ] || return + + local match_prop=3D"${prop} : (.*)" + local match_array_start=3D"${prop} : " + local match_array_elem=3D"[0-9]* : (.*)" + local match_array_end=3D"}" + local in_array=3Dfalse + local values=3D"" + + echo "show ${key}" | /usr/sbin/scutil | while read line; do + if [ "${in_array}" =3D false ] && [[ "${line}" =3D~ "${match_array= _start}" ]]; then + in_array=3Dtrue + elif [ "${in_array}" =3D true ] && [[ "${line}" =3D~ ${match_array= _elem} ]]; then + values+=3D"${BASH_REMATCH[1]} " + elif [ "${in_array}" =3D true ] && [[ "${line}" =3D~ "${match_arra= y_end}" ]]; then + echo "${values}" + break + elif [[ "${line}" =3D~ ${match_prop} ]]; then + echo "${BASH_REMATCH[1]}" + break + fi + done +} + function only_standard_server_ports { local i=3D1 while :; do @@ -73,42 +108,52 @@ } function get_search_domains { - local search_domains=3D"" - local resolver=3D0 - /usr/sbin/scutil --dns | while read line; do - if [[ "$line" =3D~ resolver.# ]]; then - resolver=3D$((resolver+1)) - elif [ "$resolver" =3D 1 ] && [[ "$line" =3D~ search.domain ]]; th= en - search_domains+=3D"$(echo $line | cut -d: -f2 | xargs) " - elif [ "$resolver" -gt 1 ]; then - echo "$search_domains" - break - fi - done + property_value State:/Network/Global/DNS SearchDomains } function set_search_domains { [ -n "$1" ] || return - dns_key=3D$(primary_dns_key) - search_domains=3D"${1}$(get_search_domains)" + local dns_key=3D$(primary_dns_key) + local dns_backup_key=3D"$(dns_backup_key)" + local search_domains=3D"${1}$(get_search_domains)" local cmds=3D"" cmds+=3D"get ${dns_key}\n" cmds+=3D"d.add SearchDomains * ${search_domains}\n" cmds+=3D"set ${dns_key}\n" + + if ! [[ "${dns_backup_key}" =3D~ ${dev}/ ]]; then + # Add the domains to the backup in case the default goes down + local existing=3D"$(property_value ${dns_backup_key} SearchDomains= )" + cmds+=3D"get ${dns_backup_key}\n" + cmds+=3D"d.add SearchDomains * ${search_domains} ${existing}\n" + cmds+=3D"set ${dns_backup_key}\n" + fi + echo -e "${cmds}" | /usr/sbin/scutil } function unset_search_domains { [ -n "$1" ] || return - dns_key=3D$(primary_dns_key) - search_domains=3D"$(get_search_domains)" + local dns_key=3D$(primary_dns_key) + local dns_backup_key=3D"$(dns_backup_key)" + local search_domains=3D"$(get_search_domains)" search_domains=3D$(echo $search_domains | sed -e "s/$1//") local cmds=3D"" cmds+=3D"get ${dns_key}\n" cmds+=3D"d.add SearchDomains * ${search_domains}\n" cmds+=3D"set ${dns_key}\n" + + if ! [[ "${dns_backup_key}" =3D~ ${dev}/ ]]; then + # Remove the domains from the backup for when the default goes down + search_domains=3D"$(property_value ${dns_backup_key} SearchDomains= )" + search_domains=3D$(echo $search_domains | sed -e "s/$1//") + cmds+=3D"get ${dns_backup_key}\n" + cmds+=3D"d.add SearchDomains * ${search_domains}\n" + cmds+=3D"set ${dns_backup_key}\n" + fi + echo -e "${cmds}" | /usr/sbin/scutil } @@ -167,7 +212,8 @@ echo -e "${cmds}" | /usr/sbin/scutil set_search_domains "$search_domains" else - echo list ${dns_backup_key_pattern} | /usr/sbin/scutil | grep -q '= no key' || { + local dns_backup_key=3D"$(dns_backup_key)" + [[ "${dns_backup_key}" =3D~ ${dev}/ ]] || { echo "setting DNS failed, already redirecting to another tunne= l" exit 1 } @@ -207,7 +253,8 @@ unset_search_domains "$search_domains" else # Do not unset if this tunnel did not set/backup DNS before - echo list ${dns_backup_key} | /usr/sbin/scutil | grep -qv 'no key'= || return + local dns_backup_key=3D"$(dns_backup_key)" + [[ "${dns_backup_key}" =3D~ ${dev}/ ]] || return local cmds=3D"" cmds+=3D"get ${dns_backup_key}\n" -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1073?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: Ide2cddad193c636eb440c9752751176dae0a6897 Gerrit-Change-Number: 1073 Gerrit-PatchSet: 4 Gerrit-Owner: d12fk Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-MessageType: merged --ausPM86f1c0= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 submitted this change.

View Change

prevent search domain races with macOS dns-up=
down

When connections go up and down there are situations where sear= ch
domains of a split DNS connection are either lost, or survive the
= lifetime of the connction. This can happen when there is also a
connecti= on that modifies the global DNS setting. When it backs-up the
global set= tings before modifying them, or when it restores the backup,
the search = domains could contain or miss VPN domains from other
connections, leadin= g to misconfiguration.

The fix is to also update the backed-up searc= h domains when a split DNS
connection comes up or goes down. That way th= e backup is always up to
date and restoring it will keep the global sear= ch domains as expected.

Change-Id: Ide2cddad193c636eb440c9752751176d= ae0a6897
Signed-off-by: Heiko Hund <heiko@...2662...>
Acked-by:= Arne Schwabe <arne-openvpn@...1227...>
Message-Id: <2025071416= 0903.7479-1-gert@...1296...>
URL: https://www.mail-archive.com/openvp= n-devel@lists.sourceforge.net/msg32127.html
Signed-off-by: Gert Doering = <gert@...1296...>
---
M distro/dns-scripts/macos-dns-updown.sh<= br>1 file changed, 67 insertions(+), 20 deletions(-)

diff --git a/distro/dns-scripts/macos-dns-updown.sh b/distro/dns-scripts=
/macos-dns-updown.sh
index c15abaa..f0640ee 100644--- a/distro/dns-scripts/macos-dns-updown.sh
+++ b/= distro/dns-scripts/macos-dns-updown.sh
@@ -29,14 +29,49 @@<= /span>
[ -z "${dns_vars_file}" ] || . "${dns_vars_= file}"

itf_dns_key=3D"State:/N= etwork/Service/openvpn-${dev}/DNS"
-dns_backup_key=3D"State:/Network/Service/openvpn-${d= ev}/DnsBackup"
-dn= s_backup_key_pattern=3D"State:/Network/Service/openvpn-.*/DnsBackup&qu= ot;

function primary_dns_key { local uuid=3D$(echo "show State:/Network/Global/IPv4"= | /usr/sbin/scutil | grep "PrimaryService" | cut -d: -f2 | xargs= )
echo "Setup:/Network/Service/${uuid}/DNS"<= /span>
}

+function dns_backup_key {
+ local key=3D"$(echo "list State:/Netwo= rk/Service/openvpn-.*/DnsBackup" | /usr/sbin/scutil | cut -d=3D -f2 | = xargs)"
+ if = [[ "${key}" =3D~ no\ key ]]; then
+ echo "State:/Network/Service/openvpn-$= {dev}/DnsBackup"
+ else
+ e= cho "${key}"
+}+
+function property_value {
+ local key=3D"$1"
= + local prop=3D"$2"= ;
+
+ [ -n "${key}" -a -n &qu= ot;${prop}" ] || return
+
+ local = match_prop=3D"${prop} : (.*)"
+ local match_array_start=3D"${prop} : <array= >"
+ local= match_array_elem=3D"[0-9]* : (.*)"
+ local match_array_end=3D"}"<= br>+ local in_array=3Dfalse<= /span>
+ local values=3D&= quot;"
+<= br>+ echo "show ${key}&= quot; | /usr/sbin/scutil | while read line; do
+ if [ "${in_array}" =3D false ]= && [[ "${line}" =3D~ "${match_array_start}" ]]= ; then
+ i= n_array=3Dtrue
+ = elif [ "${in_array}" =3D true ] && [[ "${line}&qu= ot; =3D~ ${match_array_elem} ]]; then
+ values+=3D"${BASH_REMATCH[1]} "

+ elif [ "${= in_array}" =3D true ] && [[ "${line}" =3D~ "${m= atch_array_end}" ]]; then
+ echo "${values}"
+ break
+ elif [[ "${line}" =3D~ ${matc= h_prop} ]]; then
+ = echo "${BASH_REMATCH[1]}"
+ break
+ fi
+ done
+
function only_standard_server_ports {
local i=3D1
while :; do
@@ -73,42 +108,52 @@=
}

function get_search_domains= {
- local search_do= mains=3D""
- = local resolver=3D0
-= /usr/sbin/scutil --dns | while read line; do
- if [[ "$line" =3D~ resolver.# = ]]; then
- r= esolver=3D$((resolver+1))
- elif [ "$resolver" =3D 1 ] && [[ "$line&= quot; =3D~ search.domain ]]; then
- search_domains+=3D"$(echo $line | cut -d: -f2 = | xargs) "
- = elif [ "$resolver" -gt 1 ]; then
- echo "$search_domains"<= br>- break
<= span style=3D"color: hsl(0, 100%, 40%);">- fi

- done
+ property_value State:/Network/Global/DNS SearchDom= ains
}

function set_sea= rch_domains {
[ -n "$1" ] || return- dns_key=3D$(primary_dns_ke= y)
- search_domains= =3D"${1}$(get_search_domains)"
+ local dns_key=3D$(primary_dns_key)
+ local dns_backup_key=3D"$(= dns_backup_key)"
+ local search_domains=3D"${1}$(get_search_domains)"
local cmds=3D""
= cmds+=3D"get ${dns_key}\n"
cmds+=3D"= d.add SearchDomains * ${search_domains}\n"
cmds+= =3D"set ${dns_key}\n"
+
+ if = ! [[ "${dns_backup_key}" =3D~ ${dev}/ ]]; then
+ # Add the domains to the backu= p in case the default goes down
+ local existing=3D"$(property_value ${dns_backup_ke= y} SearchDomains)"
+ cmds+=3D"get ${dns_backup_key}\n"
+ cmds+=3D"d.add SearchDomai= ns * ${search_domains} ${existing}\n"
+ cmds+=3D"set ${dns_backup_key}\n"<= /span>
+ fi
+

echo -e &qu= ot;${cmds}" | /usr/sbin/scutil
}

function unset_search_domains {
[ -n &q= uot;$1" ] || return
- search_domains=3D"$(get_search_domains)"=
+ local dns_key=3D$(prim= ary_dns_key)
+ loc= al dns_backup_key=3D"$(dns_backup_key)"
+ local search_domains=3D"$(get_search= _domains)"
search_domains=3D$(echo $search_domain= s | sed -e "s/$1//")

local= cmds=3D""
cmds+=3D"get ${dns_key}\n&qu= ot;
cmds+=3D"d.add SearchDomains * ${search_domai= ns}\n"
cmds+=3D"set ${dns_key}\n"

+
+ if ! [[ "${dns_backup_key}" = =3D~ ${dev}/ ]]; then
+ # Remove the domains from the backup for when the default goes do= wn
+ search_do= mains=3D"$(property_value ${dns_backup_key} SearchDomains)"
+ search_domains=3D= $(echo $search_domains | sed -e "s/$1//")
+ cmds+=3D"get ${dns_backup_ke= y}\n"
+ c= mds+=3D"d.add SearchDomains * ${search_domains}\n"
+ cmds+=3D"set ${dns_ba= ckup_key}\n"

+ = fi
+
echo -e "${cmds}" | /usr/sbin/scutil
}

@@ -167,7 +212,8 @@
= echo -e "${cmds}" | /usr/sbin/scutil
s= et_search_domains "$search_domains"
else
- echo list ${dns_b= ackup_key_pattern} | /usr/sbin/scutil | grep -q 'no key' || {
+ local dns_backup_= key=3D"$(dns_backup_key)"
+ [[ "${dns_backup_key}" =3D~ ${dev}/ ]] ||= {
echo "setting DNS failed, already redi= recting to another tunnel"
exit 1<= br> }
@@ -207,7 +253,8 @@
= unset_search_domains "$search_domains"
= else
# Do not unset if this tunnel did not set/ba= ckup DNS before
- = echo list ${dns_backup_key} | /usr/sbin/scutil | grep -qv 'no key'= ; || return
+ = local dns_backup_key=3D"$(dns_backup_key)"
+ [[ "${dns_backup_key}" = =3D~ ${dev}/ ]] || return

local = cmds=3D""
cmds+=3D"get ${dns_backup= _key}\n"

To view, visit change 1073. T= o unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ide2cddad193c636eb440c975275= 1176dae0a6897
Gerrit-Change-Number: 1073
Gerrit-PatchSet: 4
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-MessageType: merged
--ausPM86f1c0=--