Skip to content

Commit b97c8cd

Browse files
da-xDimitriPapadopoulos
authored andcommitted
Add a script to be used for updating systemd-resolved
1 parent 282cb1f commit b97c8cd

1 file changed

Lines changed: 119 additions & 0 deletions

File tree

contrib/ifup-systemd-resolved.sh

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/bin/bash
2+
3+
# -------------------------------------------------------------------------------
4+
# LICENSE:
5+
#
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
# -------------------------------------------------------------------------------
19+
20+
# This is an `ifup` script to be used for integrating openfortivpn and
21+
# systemd-resolved. When the network interface goes up, the DNS server information
22+
# will be added to `systemd-resolved` without modifying /etc/resolve.conf.
23+
#
24+
# This script is largely based on the main script from the `update-systemd-resolved`
25+
# package, see: https://github.com/jonathanio/update-systemd-resolved
26+
27+
DBUS_DEST="org.freedesktop.resolve1"
28+
DBUS_NODE="/org/freedesktop/resolve1"
29+
30+
SCRIPT_NAME="${BASH_SOURCE[0]##*/}"
31+
32+
log() {
33+
logger -s -t "$SCRIPT_NAME" "$@"
34+
}
35+
36+
for level in emerg err warning info debug; do
37+
printf -v functext -- '%s() { log -p user.%s -- "$@" ; }' "$level" "$level"
38+
eval "$functext"
39+
done
40+
41+
get_link_info() {
42+
dev="$1"
43+
shift
44+
45+
link=''
46+
link="$(ip link show dev "$dev")" || return $?
47+
48+
echo "$dev" "${link%%:*}"
49+
}
50+
51+
busctl_call() {
52+
# Preserve busctl's exit status
53+
busctl call "$DBUS_DEST" "$DBUS_NODE" "${DBUS_DEST}.Manager" "$@" || {
54+
local -i status=$?
55+
emerg "'busctl' exited with status $status"
56+
return $status
57+
}
58+
}
59+
60+
up() {
61+
local link="$1"
62+
shift
63+
local if_index="$1"
64+
shift
65+
66+
local -a dns_servers=() dns_domain=() dns_search=() dns_routed=()
67+
local -i dns_server_count=0 dns_domain_count=0 dns_search_count=0 dns_routed_count=0
68+
local dns_sec=""
69+
70+
for address in ${DNS_SERVERS}; do
71+
(( dns_server_count += 1 ))
72+
dns_servers+=(2 4 ${address//./ })
73+
done
74+
75+
for domain in ${DNS_SUFFIX}; do
76+
(( dns_search_count += 1 ))
77+
dns_search+=("${domain}" false)
78+
done
79+
80+
if [[ "${#dns_servers[*]}" -gt 0 ]]; then
81+
busctl_params=("$if_index" "$dns_server_count" "${dns_servers[@]}")
82+
info "SetLinkDNS(${busctl_params[*]})"
83+
busctl_call SetLinkDNS 'ia(iay)' "${busctl_params[@]}" || return $?
84+
fi
85+
86+
if [[ "${#dns_domain[*]}" -gt 0 \
87+
|| "${#dns_search[*]}" -gt 0 \
88+
|| "${#dns_routed[*]}" -gt 0 ]]; then
89+
dns_count=$((dns_domain_count+dns_search_count+dns_routed_count))
90+
busctl_params=("$if_index" "$dns_count")
91+
if [[ "${#dns_domain[*]}" -gt 0 ]]; then
92+
busctl_params+=("${dns_domain[@]}")
93+
fi
94+
if [[ "${#dns_search[*]}" -gt 0 ]]; then
95+
busctl_params+=("${dns_search[@]}")
96+
fi
97+
if [[ "${#dns_routed[*]}" -gt 0 ]]; then
98+
busctl_params+=("${dns_routed[@]}")
99+
fi
100+
info "SetLinkDomains(${busctl_params[*]})"
101+
busctl_call SetLinkDomains 'ia(sb)' "${busctl_params[@]}" || return $?
102+
fi
103+
104+
if [[ -n "${dns_sec}" ]]; then
105+
if [[ "${dns_sec}" == "default" ]]; then
106+
# We need to provide an empty string to use the default settings
107+
info "SetLinkDNSSEC($if_index '')"
108+
busctl_call SetLinkDNSSEC 'is' "$if_index" "" || return $?
109+
else
110+
info "SetLinkDNSSEC($if_index ${dns_sec})"
111+
busctl_call SetLinkDNSSEC 'is' "$if_index" "${dns_sec}" || return $?
112+
fi
113+
fi
114+
}
115+
116+
dev=${NET_DEVICE}
117+
read -r link if_index _ < <(get_link_info "$dev")
118+
up "$link" "$if_index"
119+
systemd-resolve --flush-caches

0 commit comments

Comments
 (0)