AlaK4X
Linux lhjmq-records 5.15.0-118-generic #128-Ubuntu SMP Fri Jul 5 09:28:59 UTC 2024 x86_64



Your IP : 3.145.152.49


Current Path : /etc/dhcp/dhclient-exit-hooks.d/
Upload File :
Current File : //etc/dhcp/dhclient-exit-hooks.d/resolved

#!/bin/sh
## Sourced, not exec'ed, but helps syntax highlighting
#
# Script fragment to make dhclient supply nameserver information to resolved
#

# Tips:
# * Be careful about changing the environment since this is sourced
# * This script fragment uses bash features
# * As of isc-dhcp-client 4.2 the "reason" (for running the script) can be one of the following.
#   (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)
#   (Also used in master script:)                                                                                                                         ARPCHECK(0), ARPSEND(0)
#   (Also used in master script:)   PREINIT6(0) BOUND6(M) RENEW6(M) REBIND6(M)        DEPREF6(0) EXPIRE6(D)         RELEASE6(D) STOP6(D)
#   (0) = master script does not run make_resolv_conf
#   (M) = master script runs make_resolv_conf
#   (D) = master script downs interface
#   (-) = master script does nothing with this

if systemctl is-enabled systemd-resolved > /dev/null 2>&1; then
    local mystatedir statedir ifindex

    if [ ! "$interface" ] ; then
        return
    fi
    ifindex=$(cat "/sys/class/net/$interface/ifindex")
    if [ ! "$ifindex" ]; then
        return
    fi
    mystatedir=/run/network
    mkdir -p $mystatedir

    statedir=/run/systemd/resolve/netif
    mkdir -p $statedir

    local oldstate="$(mktemp)"
    # ignore errors due to nonexistent file
    md5sum "$mystatedir/isc-dhcp-v4-$interface" "$mystatedir/isc-dhcp-v6-$interface" "$mystatedir/ifupdown-inet-$interface" "$mystatedir/ifupdown-inet6-$interface" > "$oldstate" 2> /dev/null || true

    case "$reason" in
      BOUND|RENEW|REBIND|REBOOT|TIMEOUT|BOUND6|RENEW6|REBIND6)
        if [ -n "$new_domain_name_servers" ] ; then
            cat <<EOF >"$mystatedir/isc-dhcp-v4-$interface"
DNS="$new_domain_name_servers"
EOF
            if [ -n "$new_domain_name" ] || [ -n "$new_domain_search" ] ; then
                cat <<EOF >>"$mystatedir/isc-dhcp-v4-$interface"
DOMAINS="$new_domain_search $new_domain_name"
EOF
            fi
        fi
        if [ -n "$new_dhcp6_name_servers" ] ; then
            cat <<EOF >"$mystatedir/isc-dhcp-v6-$interface"
DNS6="$new_dhcp6_name_servers"
EOF
            if [ -n "$new_dhcp6_domain_search" ] ; then
                cat <<EOF >>"$mystatedir/isc-dhcp-v6-$interface"
DOMAINS6="$new_dhcp6_domain_search"
EOF
            fi
        fi
        ;;

      EXPIRE|FAIL|RELEASE|STOP)
        rm -f "/run/network/isc-dhcp-v4-$interface"
        ;;
      EXPIRE6|RELEASE6|STOP6)
        rm -f "/run/network/isc-dhcp-v6-$interface"
        ;;
    esac

    local newstate="$(mktemp)"
    # ignore errors due to nonexistent file
    md5sum "$mystatedir/isc-dhcp-v4-$interface" "$mystatedir/isc-dhcp-v6-$interface" "$mystatedir/ifupdown-inet-$interface" "$mystatedir/ifupdown-inet6-$interface" > "$newstate" 2> /dev/null || true
    if ! cmp --silent "$oldstate" "$newstate" 2>/dev/null; then
        local DNS DNS6 DOMAINS DOMAINS6 DEFAULT_ROUTE
        # v4 first
        if [ -e "$mystatedir/isc-dhcp-v4-$interface" ]; then
            . "$mystatedir/isc-dhcp-v4-$interface"
        fi
        # v4 manual config overrides
        if [ -e "$mystatedir/ifupdown-inet-$interface" ]; then
            . "$mystatedir/ifupdown-inet-$interface"
        fi
        # v6 preffered
        if [ -e "$mystatedir/isc-dhcp-v6-$interface" ]; then
            . "$mystatedir/isc-dhcp-v6-$interface"
        fi
        # v6 manual config overrides
        if [ -e "$mystatedir/ifupdown-inet6-$interface" ]; then
            . "$mystatedir/ifupdown-inet6-$interface"
        fi
        local resolvectl_failed=
        if [ "$DNS" ] || [ "$DNS6" ] ; then
            cat <<EOF >"$statedir/$ifindex"
# This is private data. Do not parse.
LLMNR=yes
MDNS=no
SERVERS=$(echo $DNS6 $DNS)
DOMAINS=$(echo $DOMAINS6 $DOMAINS)
EOF
            if [ -n "$DEFAULT_ROUTE" ]; then
                cat <<EOF >>"$statedir/$ifindex"
DEFAULT_ROUTE=$DEFAULT_ROUTE
EOF
            fi
            chown systemd-resolve:systemd-resolve "$statedir/$ifindex"
            # In addition to creating the state file (needed if we run before
            # resolved is started), also feed the information directly to
            # resolved.
            if systemctl --quiet is-active systemd-resolved; then
                resolvectl llmnr "$ifindex" yes || resolvectl_failed=$?
                resolvectl mdns "$ifindex" no || resolvectl_failed=$?
                if [ "$DOMAINS6" ] || [ "$DOMAINS" ]; then
                    resolvectl domain "$ifindex" $DOMAINS6 $DOMAINS || resolvectl_failed=$?
                else
                    resolvectl domain "$ifindex" "" || resolvectl_failed=$?
                fi
                resolvectl dns "$ifindex" $DNS6 $DNS || resolvectl_failed=$?
                if [ "$DEFAULT_ROUTE" ]; then
                    resolvectl default-route "$ifindex" $DEFAULT_ROUTE || resolvectl_failed=$?
                fi
            fi
        else
            rm -f "$statedir/$ifindex"
            if systemctl --quiet is-active systemd-resolved; then
                resolvectl revert "$ifindex" || resolvectl_failed=$?
            fi
        fi

        # resolved was running, but without dbus, it means state files
        # will not be read & resolvectl commands failed, restart it
        if [ "$resolvectl_failed" ]; then
                systemctl try-restart systemd-resolved
        fi
    fi
    rm -f "$oldstate" "$newstate"
fi