| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
set -eu |
| 27 |
|
| 28 |
DROP_IN=/etc/systemd/resolved.conf.d/20-alloy-resolver.conf |
| 29 |
SHIPPED=/etc/systemd/resolved.conf.d/10-alloy-dns.conf |
| 30 |
REPORT=0 |
| 31 |
|
| 32 |
die() { printf 'alloy-dns: %s\n' "$*" >&2; exit 1; } |
| 33 |
|
| 34 |
[ $# -ge 1 ] || die "usage: alloy-dns [--report] <id|network> [server...]" |
| 35 |
case "$1" in |
| 36 |
--report|-n|--dry-run) REPORT=1; shift ;; |
| 37 |
esac |
| 38 |
[ $# -ge 1 ] || die "no resolver named" |
| 39 |
|
| 40 |
id="$1"; shift |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
case "$id" in |
| 45 |
*[!a-z0-9-]*|'') die "not a resolver id: $id" ;; |
| 46 |
esac |
| 47 |
|
| 48 |
if [ "$id" = network ]; then |
| 49 |
[ $# -eq 0 ] || die "the network's own resolver takes no servers" |
| 50 |
if [ "$REPORT" = 1 ]; then |
| 51 |
if [ -e "$DROP_IN" ]; then |
| 52 |
echo "would remove $DROP_IN, leaving the shipped $SHIPPED" |
| 53 |
echo "would restart systemd-resolved" |
| 54 |
else |
| 55 |
echo "nothing to do: no resolver is set, so the network's own is in use" |
| 56 |
fi |
| 57 |
exit 0 |
| 58 |
fi |
| 59 |
if [ -e "$DROP_IN" ]; then |
| 60 |
rm -f "$DROP_IN" |
| 61 |
echo "removed $DROP_IN" |
| 62 |
else |
| 63 |
echo "no resolver was set; the network's own was already in use" |
| 64 |
fi |
| 65 |
systemctl restart systemd-resolved |
| 66 |
echo "restarted systemd-resolved" |
| 67 |
exit 0 |
| 68 |
fi |
| 69 |
|
| 70 |
[ $# -ge 1 ] || die "$id needs at least one server, as address#hostname" |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
for server in "$@"; do |
| 76 |
case "$server" in |
| 77 |
*'#'*) ;; |
| 78 |
*) die "$server has no #hostname, so there is no certificate name to check" ;; |
| 79 |
esac |
| 80 |
case "$server" in |
| 81 |
*[!0-9a-fA-F.:#a-z-]*) die "$server is not an address#hostname" ;; |
| 82 |
esac |
| 83 |
done |
| 84 |
|
| 85 |
if [ "$REPORT" = 1 ]; then |
| 86 |
echo "would write $DROP_IN naming $id:" |
| 87 |
for server in "$@"; do echo " DNS=$server"; done |
| 88 |
echo " DNSOverTLS=yes" |
| 89 |
echo "would restart systemd-resolved" |
| 90 |
exit 0 |
| 91 |
fi |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
tmp="$(mktemp "${DROP_IN}.XXXXXX")" |
| 96 |
trap 'rm -f "$tmp"' EXIT |
| 97 |
|
| 98 |
{ |
| 99 |
printf '# Written by alloy-dns. Chosen in `alloy settings`, System tab.\n' |
| 100 |
printf '#\n' |
| 101 |
printf '# The line below is how the choice is read back; it is a comment because\n' |
| 102 |
printf '# resolved would reject an unknown key.\n' |
| 103 |
printf '# alloy-resolver: %s\n' "$id" |
| 104 |
printf '#\n' |
| 105 |
printf '# DNSOverTLS=yes rather than opportunistic, and only because a resolver\n' |
| 106 |
printf '# was named: these servers are known to speak it, so falling back to\n' |
| 107 |
printf '# plaintext would give away the whole point of choosing one. The shipped\n' |
| 108 |
printf '# %s stays opportunistic for the network default,\n' "$SHIPPED" |
| 109 |
printf '# where the uplink usually cannot.\n' |
| 110 |
printf '[Resolve]\n' |
| 111 |
for server in "$@"; do printf 'DNS=%s\n' "$server"; done |
| 112 |
printf 'DNSOverTLS=yes\n' |
| 113 |
} > "$tmp" |
| 114 |
|
| 115 |
chmod 0644 "$tmp" |
| 116 |
mv -f "$tmp" "$DROP_IN" |
| 117 |
trap - EXIT |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
if command -v restorecon >/dev/null 2>&1; then |
| 123 |
restorecon -F "$DROP_IN" 2>/dev/null || true |
| 124 |
fi |
| 125 |
|
| 126 |
echo "wrote $DROP_IN naming $id" |
| 127 |
systemctl restart systemd-resolved |
| 128 |
echo "restarted systemd-resolved" |
| 129 |
|