From: "cron2 (Code Review)" Date: Mon, 14 Jul 2025 16:18:26 +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="SQ1iM45t5vo="; 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 , plaisthos , flichtenheld Cc: openvpn-devel --SQ1iM45t5vo= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#4) to the change originally created by= d12fk. ( http://gerrit.openvpn.net/c/openvpn/+/1073?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by plaisthos 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(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/73/1073/4 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: newpatchset --SQ1iM45t5vo= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 uploaded patch set #4 to the change originally cr= eated by d12fk.

View Change

The following approvals got outdated an= d were removed: Code-Review+2 by plaisthos

prevent search domain races with macOS dns-updow=
n

When connections go up and down there are situations where search<= br>domains of a split DNS connection are either lost, or survive the
lif= etime of the connction. This can happen when there is also a
connection = that modifies the global DNS setting. When it backs-up the
global settin= gs before modifying them, or when it restores the backup,
the search dom= ains could contain or miss VPN domains from other
connections, leading t= o misconfiguration.

The fix is to also update the backed-up search d= omains when a split DNS
connection comes up or goes down. That way the b= ackup is always up to
date and restoring it will keep the global search = domains as expected.

Change-Id: Ide2cddad193c636eb440c9752751176dae0= a6897
Signed-off-by: Heiko Hund <heiko@...2662...>
Acked-by: Ar= ne Schwabe <arne-openvpn@...1227...>
Message-Id: <2025071416090= 3.7479-1-gert@...1296...>
URL: https://www.mail-archive.com/openvpn-d= evel@lists.sourceforge.net/msg32127.html
Signed-off-by: Gert Doering <= ;gert@...1296...>
---
M distro/dns-scripts/macos-dns-updown.sh
= 1 file changed, 67 insertions(+), 20 deletions(-)

git pull ssh:=
//gerrit.openvpn.net:29418/openvpn refs/changes/73/1073/4
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 @@
= [ -z "${dns_vars_file}" ] || . "${dns_vars_file}"= ;

itf_dns_key=3D"State:/Network/Ser= vice/openvpn-${dev}/DNS"
-dns_backup_key=3D"State:/Network/Service/openvpn-${dev}/DnsBac= kup"
-dns_backup_k= ey_pattern=3D"State:/Network/Service/openvpn-.*/DnsBackup"=

function primary_dns_key {
= local uuid=3D$(echo "show State:/Network/Global/IPv4" | /usr/sb= in/scutil | 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)&quo= t;
+ if [[ "$= {key}" =3D~ no\ key ]]; then
+ echo "State:/Network/Service/openvpn-${dev}/DnsB= ackup"
+ else=
+ echo "= ${key}"
+ fi<= /span>
+}
+
+function property_value {
+ local key=3D"$1"
+ local prop=3D"$2"+
+ [ -n "${key}" -a -n "${prop}= " ] || return
+<= /span>
+ local match_prop= =3D"${prop} : (.*)"
+ local match_array_start=3D"${prop} : <array>"=
+ local match_arr= ay_elem=3D"[0-9]* : (.*)"
+ local match_array_end=3D"}"
+ local in_array=3Dfalse
= + local values=3D""= ;
+
+ echo "show ${key}" | /u= sr/sbin/scutil | while read line; do
+ if [ "${in_array}" =3D false ] &&= ; [[ "${line}" =3D~ "${match_array_start}" ]]; then
+ in_array=3D= true
+ elif [ = "${in_array}" =3D true ] && [[ "${line}" =3D~ $= {match_array_elem} ]]; then
+ values+=3D"${BASH_REMATCH[1]} "
+ elif [ "${in_array}&= quot; =3D true ] && [[ "${line}" =3D~ "${match_array= _end}" ]]; then

= + echo "${values}"
+ break
+ elif [[ "${line}" =3D~ ${match_prop} ]]= ; then
+ e= cho "${BASH_REMATCH[1]}"
+ break
+ fi
+ done
+}
+
functio= n only_standard_server_ports {
local i=3D1
<= span> while :; do

@@ -73,42 +108,52 @@
= }

function get_search_domains {<= br>- local search_domains=3D&q= uot;"
- local r= esolver=3D0
- /usr/s= bin/scutil --dns | while read line; do
- if [[ "$line" =3D~ resolver.# ]]; then
- resolver=3D= $((resolver+1))
- = elif [ "$resolver" =3D 1 ] && [[ "$line" =3D~= search.domain ]]; then
- search_domains+=3D"$(echo $line | cut -d: -f2 | xargs) &= quot;
- elif [ &= quot;$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_k= ey)"
+ local = search_domains=3D"${1}$(get_search_domains)"

local cmds=3D""
cmds+=3D&= quot;get ${dns_key}\n"
cmds+=3D"d.add Search= Domains * ${search_domains}\n"
cmds+=3D"set = ${dns_key}\n"
+<= /span>
+ 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} SearchDoma= ins)"
+ c= mds+=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}&qu= ot; | /usr/sbin/scutil
}

function unset_search_domains {

[ -n "$1" = ] || return
- dns_ke= y=3D$(primary_dns_key)
= - search_domains=3D"$(get_search_domains)"
+ local dns_key=3D$(primary_dns_key)<= /span>
+ local dns_backup= _key=3D"$(dns_backup_key)"
+ local search_domains=3D"$(get_search_domains)&quo= t;
search_domains=3D$(echo $search_domains | sed -e &q= uot;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
+ # Rem= ove 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.ad= d SearchDomains * ${search_domains}\n"
+ cmds+=3D"set ${dns_backup_key}\n"=
+ fi
+

echo -e &q= uot;${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"$(dn= s_backup_key)"
+= [[ "${dns_backup_key}" =3D~ ${dev}/ ]] || {
echo "setting DNS failed, already redirecting to anoth= er tunnel"

exit 1
= }
@@ -207,7 +253,8 @@
unset_sear= ch_domains "$search_domains"
else
= # Do not unset if this tunnel did not set/backup DNS before<= /span>
- echo list ${dn= s_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"&quo= t;
cmds+=3D"get ${dns_backup_key}\n"

To view, visit change 1073. To 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: newpatchset
--SQ1iM45t5vo=--