From: "d12fk (Code Review)" Date: Thu, 3 Jul 2025 03:56:29 +0000 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="ph3dSpt4PSY="; 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: plaisthos , flichtenheld Cc: openvpn-devel --ph3dSpt4PSY= 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/+/1073?usp=3Demail to review the following change. 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 --- 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/1 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 @@ } =20 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: 1 Gerrit-Owner: d12fk Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: flichtenheld Gerrit-MessageType: newchange --ph3dSpt4PSY= 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.

V= iew Change

prevent search domain races with macOS dns-updown

Whe= n connections go up and down there are situations where search
domains o= f a split DNS connection are either lost, or survive the
lifetime of the= connction. This can happen when there is also a
connection that modifie= s the global DNS setting. When it backs-up the
global settings before mo= difying them, or when it restores the backup,
the search domains could c= ontain or miss VPN domains from other
connections, leading to misconfigu= ration.

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 alw= ays up to
date and restoring it will keep the global search domains as e= xpected.

Change-Id: Ide2cddad193c636eb440c9752751176dae0a6897
Sig= ned-off-by: Heiko Hund <heiko@...2662...>
---
M distro/dns-scri= pts/macos-dns-updown.sh
1 file changed, 67 insertions(+), 20 deletions(-= )

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/73=
/1073/1
diff --git a/distro/dns-scripts/macos-dns-updown.sh b/distr=
o/dns-scripts/macos-dns-updown.sh
index c15abaa..f0640ee 10= 0644
--- a/distro/dns-scripts/macos-dns-updown.sh+++ b/distro/dns-scripts/macos-dns-updown.sh
@@ -29,= 14 +29,49 @@
[ -z "${dns_vars_file}" ] || . &quo= t;${dns_vars_file}"

itf_dns_key=3D&= quot;State:/Network/Service/openvpn-${dev}/DNS"
-dns_backup_key=3D"State:/Network/Servi= ce/openvpn-${dev}/DnsBackup"
-dns_backup_key_pattern=3D"State:/Network/Service/openvpn-.= */DnsBackup"

function primary_dns_k= ey {
local uuid=3D$(echo "show State:/Network/Glo= bal/IPv4" | /usr/sbin/scutil | grep "PrimaryService" | cut -= d: -f2 | xargs)
echo "Setup:/Network/Service/${uu= id}/DNS"
}

+function dns_backup_key {
+ local key=3D"$(echo "lis= t State:/Network/Service/openvpn-.*/DnsBackup" | /usr/sbin/scutil | cu= t -d=3D -f2 | xargs)"
+ if [[ "${key}" =3D~ no\ key ]]; then
+ echo "State:/Network/Ser= vice/openvpn-${dev}/DnsBackup"
+ else
+ echo "${key}"
+ fi
+}
+
+function property_value {<= br>+ local key=3D"$1&qu= ot;
+ local prop= =3D"$2"
+
+ [ -n "${key}= " -a -n "${prop}" ] || return
+
+ local match_prop=3D"${prop} : (.*)"
+ local match_array_start=3D"${p= rop} : <array>"
+ local match_array_elem=3D"[0-9]* : (.*)"
+ local match_array_end=3D"= ;}"

+ local i= n_array=3Dfalse
+ = local values=3D""
+
+ echo &q= uot;show ${key}" | /usr/sbin/scutil | while read line; do
+ if [ "${in_array}&q= uot; =3D false ] && [[ "${line}" =3D~ "${match_array= _start}" ]]; then
+ elif [ "${in_array}" =3D true ] && [[ = "${line}" =3D~ ${match_array_elem} ]]; then
+ values+=3D"${BASH_REMATC= H[1]} "
+ = elif [ "${in_array}" =3D true ] && [[ "${line}"= ; =3D~ "${match_array_end}" ]]; then
+ echo "${values}"+ break
<= span style=3D"color: hsl(120, 100%, 40%);">+ elif [[ "${line}&q= uot; =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 ge= t_search_domains {
- = local search_domains=3D""
- local resolver=3D0
- /usr/sbin/scutil --dns | while read line; do
= - if [[ "$line" = =3D~ resolver.# ]]; then
- elif [ "$resolver" =3D 1 ] &&= [[ "$line" =3D~ search.domain ]]; then
- search_domains+=3D"$(echo $lin= e | cut -d: -f2 | xargs) "
- elif [ "$resolver" -gt 1 ]; then
- echo "$search_domai= ns"
- b= reak
- fi=
- done
+ property_value State:/Network/Globa= l/DNS SearchDomains
}

f= unction 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)&= quot;

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.ad= d SearchDomains * ${search_domains} ${existing}\n"
+ cmds+=3D"set ${dns_backup_= key}\n"
+ fi<= /span>
+
= echo -e "${cmds}" | /usr/sbin/scutil
}<= br>
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"
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ if ! [[ "${dns_backup_= key}" =3D~ ${dev}/ ]]; then
+ # Remove the domains from the backup for when the defa= ult goes down
+ = search_domains=3D"$(property_value ${dns_backup_key} SearchDomains)&= quot;
+ 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 lis= t ${dns_backup_key_pattern} | /usr/sbin/scutil | grep -q 'no key' |= | {
+ local dn= s_backup_key=3D"$(dns_backup_key)"
+ [[ "${dns_backup_key}" =3D~ ${de= v}/ ]] || {
echo "setting DNS failed, alr= eady redirecting to another tunnel"
exit = 1
}
@@ -207,7 +253,8 @@
= unset_search_domains "$search_domains"
<= span> else
# Do not unset if this tunnel did n= ot set/backup DNS before
= + local dns_backup_key=3D"$(dns_backup_key)"
+ [[ "${dns_backup_key= }" =3D~ ${dev}/ ]] || return

= local cmds=3D""
cmds+=3D"get ${dn= s_backup_key}\n"

To view, visit change 107= 3. 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: 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
--ph3dSpt4PSY=--