#!/bin/sh set -eu export LC_ALL=C MEDIA=/Volumes/REIMSTOOLS APP_DIR=/private/etc/reims-host-dns PLIST=/Library/LaunchDaemons/com.reimsgo.dns-proxy.plist SERVICE=system/com.reimsgo.dns-proxy CONF="$APP_DIR/dns-proxy.conf" BACKUP="$APP_DIR/dns-proxy.previous-dns" proxy_pid() { sudo launchctl print "$SERVICE" 2>/dev/null | awk '$1 == "pid" && $2 == "=" {print $3; exit}' } dns_listener_pids() { sudo /usr/sbin/lsof -nP -Fpn "$@" | awk ' /^p/ {pid = substr($0, 2)} /^n(127[.]0[.]0[.]1|\*|\[::\]):53(->|$)/ {print pid} ' } sudo -v port=$(sudo sed -n "s/^REIMS_HOST_PORT='\([0-9][0-9]*\)'$/\1/p" "$MEDIA/config") token=$(sudo sed -n "s/^REIMS_TOKEN='\([0-9a-f][0-9a-f]*\)'$/\1/p" "$MEDIA/config") if ! printf '%s\n' "$port" | grep -Eq '^[0-9]{1,5}$' || [ "$port" -lt 1 ] || [ "$port" -gt 65535 ] || ! printf '%s\n' "$token" | grep -Eq '^[0-9a-f]{64}$'; then echo "Invalid DNS settings on REIMSTOOLS." >&2 exit 1 fi settings=$(curl -fsS -m 5 -H "Authorization: Bearer $token" "http://10.0.2.2:$port/v1/config") if ! printf '%s\n' "$settings" | grep -q '"host_dns_enabled"[[:space:]]*:[[:space:]]*true'; then echo "Enable Host DNS for this VM in Reims Go first." >&2 exit 1 fi pid=$(proxy_pid) for owner in $(dns_listener_pids -iUDP:53) $(dns_listener_pids -iTCP:53 -sTCP:LISTEN); do if [ "$owner" != "$pid" ]; then echo "Another process (PID $owner) is using 127.0.0.1:53." >&2 exit 1 fi done if sudo test -L "$APP_DIR" || { sudo test -e "$APP_DIR" && [ "$(sudo stat -f '%u:%Lp' "$APP_DIR")" != 0:755 ]; }; then echo "Host DNS requires a root-owned directory at $APP_DIR." >&2 exit 1 fi sudo install -d -o root -g wheel -m 0755 "$APP_DIR" sudo install -o root -g wheel -m 0755 "$MEDIA/Disable-Host-DNS.command" "$APP_DIR/Disable-Host-DNS.command" rollback() { result=$? trap - EXIT HUP INT TERM if [ "$result" -ne 0 ]; then if ! "$APP_DIR/Disable-Host-DNS.command"; then echo "Could not restore DNS. Rerun $APP_DIR/Disable-Host-DNS.command to retry." >&2 fi fi exit "$result" } trap rollback EXIT trap 'exit 1' HUP INT TERM sudo install -o root -g wheel -m 0755 "$MEDIA/dns-proxy.pl" "$APP_DIR/dns-proxy.pl" sudo install -o root -g wheel -m 0600 /dev/null "$CONF.new" printf 'REIMS_HOST_PORT=%s\nREIMS_TOKEN=%s\n' "$port" "$token" | sudo tee "$CONF.new" >/dev/null sudo chmod 600 "$CONF.new" sudo mv -f "$CONF.new" "$CONF" if sudo launchctl print "$SERVICE" >/dev/null 2>&1; then sudo launchctl bootout "$SERVICE" fi token_sha256=$(printf '%s' "$token" | /usr/bin/shasum -a 256 | awk '{print $1}') sudo install -o root -g wheel -m 0644 /dev/null "$PLIST" sudo tee "$PLIST" >/dev/null < Labelcom.reimsgo.dns-proxy ProgramArguments /usr/bin/perl$APP_DIR/dns-proxy.pl EnvironmentVariables REIMS_HOST_PORT$port REIMS_TOKEN_SHA256$token_sha256 RunAtLoad KeepAlive EOF sudo launchctl bootstrap system "$PLIST" # Check that the responding resolver owns both sockets before changing system DNS. addresses=$(dig @127.0.0.1 +tries=3 +time=7 +short example.com A) || addresses= pid=$(proxy_pid) if ! printf '%s\n' "$addresses" | grep -Eq '^[0-9]+(\.[0-9]+){3}$' || [ -z "$pid" ] || ! dns_listener_pids -iUDP:53 | grep -Fxq "$pid" || ! dns_listener_pids -iTCP:53 -sTCP:LISTEN | grep -Fxq "$pid"; then echo "The Host DNS proxy did not return an address for example.com." >&2 exit 1 fi if ! sudo test -f "$BACKUP"; then sudo install -o root -g wheel -m 0600 /dev/null "$BACKUP" fi snapshots=$(sudo cat "$BACKUP") services=$(networksetup -listallnetworkservices) printf '%s\n' "$services" | sed '1d; /^\*/d; /^$/d' | while IFS= read -r svc; do previous=$(networksetup -getdnsservers "$svc") if [ "$previous" = "There aren't any DNS Servers set on $svc." ]; then previous=Empty fi if ! printf '%s\n' "$snapshots" | REIMS_DNS_SERVICE="$svc" awk ' BEGIN {RS = ""; FS = "\n"} $1 == ENVIRON["REIMS_DNS_SERVICE"] {found = 1; exit} END {exit !found} '; then printf '%s\n%s\n\n' "$svc" "$previous" | sudo tee -a "$BACKUP" >/dev/null fi [ "$previous" != 127.0.0.1 ] || continue sudo networksetup -setdnsservers "$svc" 127.0.0.1 done sudo dscacheutil -flushcache echo "Host DNS is active."