| 1 | #!/bin/sh |
| 2 | ## Sourced, not exec'ed, but helps syntax highlighting |
| 3 | # |
| 4 | # Script fragment to make dhclient supply nameserver information to resolved |
| 5 | # |
| 6 | |
| 7 | # Tips: |
| 8 | # * Be careful about changing the environment since this is sourced |
| 9 | # * This script fragment uses bash features |
| 10 | # * As of isc-dhcp-client 4.2 the "reason" (for running the script) can be one of the following. |
| 11 | # (Listed on man page:) MEDIUM(0) PREINIT(0) BOUND(M) RENEW(M) REBIND(M) REBOOT(M) EXPIRE(D) FAIL(D) RELEASE(D) STOP(D) NBI(-) TIMEOUT(M) |
| 12 | # (Also used in master script:) ARPCHECK(0), ARPSEND(0) |
| 13 | # (Also used in master script:) PREINIT6(0) BOUND6(M) RENEW6(M) REBIND6(M) DEPREF6(0) EXPIRE6(D) RELEASE6(D) STOP6(D) |
| 14 | # (0) = master script does not run make_resolv_conf |
| 15 | # (M) = master script runs make_resolv_conf |
| 16 | # (D) = master script downs interface |
| 17 | # (-) = master script does nothing with this |
| 18 | |
| 19 | if systemctl is-enabled systemd-resolved > /dev/null 2>&1; then |
| 20 | local mystatedir statedir ifindex |
| 21 | |
| 22 | if [ ! "$interface" ] ; then |
| 23 | return |
| 24 | fi |
| 25 | ifindex=$(cat "/sys/class/net/$interface/ifindex") |
| 26 | if [ ! "$ifindex" ]; then |
| 27 | return |
| 28 | fi |
| 29 | mystatedir=/run/network |
| 30 | mkdir -p $mystatedir |
| 31 | |
| 32 | statedir=/run/systemd/resolve/netif |
| 33 | mkdir -p $statedir |
| 34 | chown systemd-resolve:systemd-resolve $statedir |
| 35 | |
| 36 | local oldstate="$(mktemp)" |
| 37 | # ignore errors due to nonexistent file |
| 38 | md5sum "$mystatedir/isc-dhcp-v4-$interface" "$mystatedir/isc-dhcp-v6-$interface" "$mystatedir/ifupdown-inet-$interface" "$mystatedir/ifupdown-inet6-$interface" > "$oldstate" 2> /dev/null || true |
| 39 | |
| 40 | case "$reason" in |
| 41 | BOUND|RENEW|REBIND|REBOOT|TIMEOUT|BOUND6|RENEW6|REBIND6) |
| 42 | if [ -n "$new_domain_name_servers" ] ; then |
| 43 | cat <<EOF >"$mystatedir/isc-dhcp-v4-$interface" |
| 44 | DNS="$new_domain_name_servers" |
| 45 | EOF |
| 46 | if [ -n "$new_domain_name" ] || [ -n "$new_domain_search" ] ; then |
| 47 | cat <<EOF >>"$mystatedir/isc-dhcp-v4-$interface" |
| 48 | DOMAINS="$new_domain_search $new_domain_name" |
| 49 | EOF |
| 50 | fi |
| 51 | fi |
| 52 | if [ -n "$new_dhcp6_name_servers" ] ; then |
| 53 | cat <<EOF >"$mystatedir/isc-dhcp-v6-$interface" |
| 54 | DNS6="$new_dhcp6_name_servers" |
| 55 | EOF |
| 56 | if [ -n "$new_dhcp6_domain_search" ] ; then |
| 57 | cat <<EOF >>"$mystatedir/isc-dhcp-v6-$interface" |
| 58 | DOMAINS6="$new_dhcp6_domain_search" |
| 59 | EOF |
| 60 | fi |
| 61 | fi |
| 62 | ;; |
| 63 | |
| 64 | EXPIRE|FAIL|RELEASE|STOP) |
| 65 | rm -f "/run/network/isc-dhcp-v4-$interface" |
| 66 | ;; |
| 67 | EXPIRE6|RELEASE6|STOP6) |
| 68 | rm -f "/run/network/isc-dhcp-v6-$interface" |
| 69 | ;; |
| 70 | esac |
| 71 | |
| 72 | local newstate="$(mktemp)" |
| 73 | # ignore errors due to nonexistent file |
| 74 | md5sum "$mystatedir/isc-dhcp-v4-$interface" "$mystatedir/isc-dhcp-v6-$interface" "$mystatedir/ifupdown-inet-$interface" "$mystatedir/ifupdown-inet6-$interface" > "$newstate" 2> /dev/null || true |
| 75 | if ! cmp --silent "$oldstate" "$newstate" 2>/dev/null; then |
| 76 | local DNS DNS6 DOMAINS DOMAINS6 DEFAULT_ROUTE |
| 77 | # v4 first |
| 78 | if [ -e "$mystatedir/isc-dhcp-v4-$interface" ]; then |
| 79 | . "$mystatedir/isc-dhcp-v4-$interface" |
| 80 | fi |
| 81 | # v4 manual config overrides |
| 82 | if [ -e "$mystatedir/ifupdown-inet-$interface" ]; then |
| 83 | . "$mystatedir/ifupdown-inet-$interface" |
| 84 | fi |
| 85 | # v6 preffered |
| 86 | if [ -e "$mystatedir/isc-dhcp-v6-$interface" ]; then |
| 87 | . "$mystatedir/isc-dhcp-v6-$interface" |
| 88 | fi |
| 89 | # v6 manual config overrides |
| 90 | if [ -e "$mystatedir/ifupdown-inet6-$interface" ]; then |
| 91 | . "$mystatedir/ifupdown-inet6-$interface" |
| 92 | fi |
| 93 | local resolvectl_failed= |
| 94 | if [ "$DNS" ] || [ "$DNS6" ] ; then |
| 95 | cat <<EOF >"$statedir/$ifindex" |
| 96 | # This is private data. Do not parse. |
| 97 | LLMNR=yes |
| 98 | MDNS=no |
| 99 | SERVERS=$(echo $DNS6 $DNS) |
| 100 | DOMAINS=$(echo $DOMAINS6 $DOMAINS) |
| 101 | EOF |
| 102 | if [ -n "$DEFAULT_ROUTE" ]; then |
| 103 | cat <<EOF >>"$statedir/$ifindex" |
| 104 | DEFAULT_ROUTE=$DEFAULT_ROUTE |
| 105 | EOF |
| 106 | fi |
| 107 | chown systemd-resolve:systemd-resolve "$statedir/$ifindex" |
| 108 | # In addition to creating the state file (needed if we run before |
| 109 | # resolved is started), also feed the information directly to |
| 110 | # resolved. |
| 111 | if systemctl --quiet is-active systemd-resolved; then |
| 112 | resolvectl llmnr "$ifindex" yes || resolvectl_failed=$? |
| 113 | resolvectl mdns "$ifindex" no || resolvectl_failed=$? |
| 114 | if [ "$DOMAINS6" ] || [ "$DOMAINS" ]; then |
| 115 | resolvectl domain "$ifindex" $DOMAINS6 $DOMAINS || resolvectl_failed=$? |
| 116 | else |
| 117 | resolvectl domain "$ifindex" "" || resolvectl_failed=$? |
| 118 | fi |
| 119 | resolvectl dns "$ifindex" $DNS6 $DNS || resolvectl_failed=$? |
| 120 | if [ "$DEFAULT_ROUTE" ]; then |
| 121 | resolvectl default-route "$ifindex" $DEFAULT_ROUTE || resolvectl_failed=$? |
| 122 | fi |
| 123 | fi |
| 124 | else |
| 125 | rm -f "$statedir/$ifindex" |
| 126 | if systemctl --quiet is-active systemd-resolved; then |
| 127 | resolvectl revert "$ifindex" || resolvectl_failed=$? |
| 128 | fi |
| 129 | fi |
| 130 | |
| 131 | # resolved was running, but without dbus, it means state files |
| 132 | # will not be read & resolvectl commands failed, restart it |
| 133 | if [ "$resolvectl_failed" ]; then |
| 134 | systemctl try-restart systemd-resolved |
| 135 | fi |
| 136 | fi |
| 137 | rm -f "$oldstate" "$newstate" |
| 138 | fi |