HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

test/units/TEST-75-RESOLVED.sh

75,481 bytesGit mode 100755Download original fileLGPL-2.1-or-later

Git blob: 814427c07207a55e8aa50a1cd38087aad68e1302

Lines 1001–1811 of 1,811

testcase_10_resolvectl_json() {
    local status_json
 
    # Cleanup
    # shellcheck disable=SC2317,SC2329
    cleanup() {
        rm -f /run/systemd/resolved.conf.d/90-fallback.conf
        systemctl reload systemd-resolved.service
        resolvectl revert dns0
    }
 
    trap cleanup RETURN ERR
 
    # Issue: https://github.com/systemd/systemd/issues/29580 (part #1)
    dig @127.0.0.54 signed.test
 
    systemctl stop resolvectl-monitor.service
    systemctl stop resolvectl-monitor-json.service
 
    # Issue: https://github.com/systemd/systemd/issues/29580 (part #2)
    #
    # Check for any warnings regarding malformed messages
    (! journalctl -u resolvectl-monitor.service -u reseolvectl-monitor-json.service -p warning --grep malformed)
    # Verify that all queries recorded by `resolvectl monitor --json` produced a valid JSON
    # with expected fields
    journalctl -p info -o cat _SYSTEMD_UNIT="resolvectl-monitor-json.service" | while read -r line; do
        # Check that both "question" and "answer" fields are arrays
        #
        # The expression is slightly more complicated due to the fact that the "answer" field is optional,
        # so we need to select it only if it's present, otherwise the type == "array" check would fail
        echo "$line" | jq -e '[. | .question, (select(has("answer")) | .answer) | type == "array"] | all'
    done
 
 
    # Test some global-only settings.
    mkdir -p /run/systemd/resolved.conf.d
    {
        echo "[Resolve]"
        echo "FallbackDNS=10.0.0.1 10.0.0.2"
    } >/run/systemd/resolved.conf.d/90-fallback.conf
    systemctl reload systemd-resolved
 
    status_json="$(mktemp)"
    resolvectl --json=short >"$status_json"
 
    # Delegates field should be empty when no delegates are configured.
    (! jq -rce '.[] | select(.delegate != null)' "$status_json")
 
    # Test that some links are present.
    jq -rce '.[] | select(.ifname == "dns0")' "$status_json"
 
    # Test some global-specific configuration.
    assert_eq \
        "$(jq -rc '.[] | select(.ifindex == null and .delegate == null) | [ .fallbackServers[] | .addressString ]' "$status_json")" \
        '["10.0.0.1","10.0.0.2"]'
    assert_eq \
        "$(jq -rc '.[] | select(.ifindex == null and .delegate == null) | .resolvConfMode' "$status_json")" \
        'stub'
 
    # Test link status.
    resolvectl dns dns0 '1.2.3.4'
    resolvectl domain dns0 'foo'
    resolvectl default-route dns0 'false'
    resolvectl llmnr dns0 'no'
    resolvectl mdns dns0 'no'
    resolvectl dnsovertls dns0 'opportunistic'
    resolvectl dnssec dns0 'yes'
    resolvectl nta dns0 'bar'
 
    resolvectl --json=short status dns0  >"$status_json"
 
    assert_eq "$(resolvectl --json=short dns dns0 | jq -rc '.[0].servers | .[0].addressString')" '1.2.3.4'
    assert_eq "$(jq -rc '.[0].servers | .[0].addressString' "$status_json")" '1.2.3.4'
 
    assert_eq "$(resolvectl --json=short domain dns0 | jq -rc '.[0].searchDomains| .[0].name')" 'foo'
    assert_eq "$(jq -rc '.[0].searchDomains | .[0].name' "$status_json")" 'foo'
 
    assert_eq "$(resolvectl --json=short default-route dns0 | jq -rc '.[0].defaultRoute')" 'false'
    assert_eq "$(jq -rc '.[0].defaultRoute' "$status_json")" 'false'
 
    assert_eq "$(resolvectl --json=short llmnr dns0 | jq -rc '.[0].llmnr')" 'no'
    assert_eq "$(jq -rc '.[0].llmnr' "$status_json")" 'no'
 
    assert_eq "$(resolvectl --json=short mdns dns0 | jq -rc '.[0].mDNS')" 'no'
    assert_eq "$(jq -rc '.[0].mDNS' "$status_json")" 'no'
 
    assert_eq "$(resolvectl --json=short dnsovertls dns0 | jq -rc '.[0].dnsOverTLS')" 'opportunistic'
    assert_eq "$(jq -rc '.[0].dnsOverTLS' "$status_json")" 'opportunistic'
 
    assert_eq "$(resolvectl --json=short dnssec dns0 | jq -rc '.[0].dnssec')" 'yes'
    assert_eq "$(jq -rc '.[0].dnssec' "$status_json")" 'yes'
 
    assert_eq "$(resolvectl --json=short nta dns0 | jq -rc '.[0].negativeTrustAnchors | .[0]')" 'bar'
    assert_eq "$(jq -rc '.[0].negativeTrustAnchors | .[0]' "$status_json")" 'bar'
 
    # Test that currentServer is non-empty.
    jq -rce '.[0].currentServer' "$status_json"
}
 
# Test serve stale feature and NFTSet= if nftables is installed
testcase_11_nft() {
    if ! command -v nft >/dev/null; then
        echo "nftables is not installed. Skipped serve stale feature tests."
        return 0
    fi
 
    ### Test without serve stale feature ###
    NFT_FILTER_NAME=dns_port_filter
 
    drop_dns_outbound_traffic() {
        nft add table inet $NFT_FILTER_NAME
        nft add chain inet $NFT_FILTER_NAME output \{ type filter hook output priority 0 \; \}
        nft add rule inet $NFT_FILTER_NAME output ip daddr 10.0.0.1 udp dport 53 drop
        nft add rule inet $NFT_FILTER_NAME output ip daddr 10.0.0.1 tcp dport 53 drop
        nft add rule inet $NFT_FILTER_NAME output ip6 daddr fd00:dead:beef:cafe::1 udp dport 53 drop
        nft add rule inet $NFT_FILTER_NAME output ip6 daddr fd00:dead:beef:cafe::1 tcp dport 53 drop
    }
 
    run dig stale1.unsigned.test -t A
    grep -qE "NOERROR" "$RUN_OUT"
    sleep 2
    drop_dns_outbound_traffic
    set +e
    # Make sure we give sd-resolved enough time to timeout (5-10s) before giving up
    # See: https://github.com/systemd/systemd/issues/31639#issuecomment-2009152617
    run dig +tries=1 +timeout=15 stale1.unsigned.test -t A
    set -eux
    grep -qE "no servers could be reached" "$RUN_OUT"
    nft flush ruleset
 
    . /etc/os-release
    if [[ "${ID_LIKE:-}" == alpine ]]; then
        # FIXME: For some reasons, the following tests will fail on Alpine/Nura.
        return 0
    fi
 
    ### Test TIMEOUT with serve stale feature ###
 
    mkdir -p /run/systemd/resolved.conf.d
    {
        echo "[Resolve]"
        echo "StaleRetentionSec=1d"
    } >/run/systemd/resolved.conf.d/10-test.conf
    systemctl reload systemd-resolved.service
 
    run dig stale1.unsigned.test -t A
    grep -qE "NOERROR" "$RUN_OUT"
    sleep 2
    drop_dns_outbound_traffic
    # Make sure we give sd-resolved enough time to timeout (5-10s) and serve the stale data (see above)
    run dig +tries=1 +timeout=15 stale1.unsigned.test -t A
    grep -qE "NOERROR" "$RUN_OUT"
    grep -qE "10.0.0.112" "$RUN_OUT"
 
    nft flush ruleset
 
    ### Test NXDOMAIN with serve stale feature ###
    # NXDOMAIN response should replace the cache with NXDOMAIN response
    run dig stale1.unsigned.test -t A
    grep -qE "NOERROR" "$RUN_OUT"
    # Delete stale1 record from zone
    knotc zone-begin unsigned.test
    knotc zone-unset unsigned.test stale1 A
    knotc zone-commit unsigned.test
    knotc reload
    sleep 2
    run dig stale1.unsigned.test -t A
    grep -qE "NXDOMAIN" "$RUN_OUT"
 
    nft flush ruleset
}
 
# Test resolvectl show-server-state
testcase_12_resolvectl2() {
    # Cleanup
    # shellcheck disable=SC2317,SC2329
    cleanup() {
        rm -f /run/systemd/resolved.conf.d/90-reload.conf
        systemctl reload systemd-resolved.service
        resolvectl revert dns0
    }
 
    trap cleanup RETURN
 
    run resolvectl show-server-state
    grep -qF "10.0.0.1" "$RUN_OUT"
    grep -qF "Interface" "$RUN_OUT"
 
    run resolvectl show-server-state --json=short
    grep -qF "10.0.0.1" "$RUN_OUT"
    grep -qF "Interface" "$RUN_OUT"
 
    run resolvectl show-server-state --json=pretty
    grep -qF "10.0.0.1" "$RUN_OUT"
    grep -qF "Interface" "$RUN_OUT"
 
    ### Test resolvectl statistics ###
    run resolvectl statistics
    grep -qF "Transactions" "$RUN_OUT"
    grep -qF "Cache" "$RUN_OUT"
    grep -qF "Failure Transactions" "$RUN_OUT"
    grep -qF "DNSSEC Verdicts" "$RUN_OUT"
 
    run resolvectl statistics --json=short
    grep -qF "transactions" "$RUN_OUT"
    grep -qF "cache" "$RUN_OUT"
    grep -qF "dnssec" "$RUN_OUT"
 
    run resolvectl statistics --json=pretty
    grep -qF "transactions" "$RUN_OUT"
    grep -qF "cache" "$RUN_OUT"
    grep -qF "dnssec" "$RUN_OUT"
 
    ### Test resolvectl reset-statistics ###
    run resolvectl reset-statistics
 
    run resolvectl reset-statistics --json=pretty
 
    run resolvectl reset-statistics --json=short
 
    test "$(resolvectl --json=short query -t AAAA localhost)" == '{"key":{"class":1,"type":28,"name":"localhost"},"address":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]}'
    test "$(resolvectl --json=short query -t A localhost)" == '{"key":{"class":1,"type":1,"name":"localhost"},"address":[127,0,0,1]}'
 
    # Test ResolveRecord RR resolving via Varlink
    test "$(varlinkctl call /run/systemd/resolve/io.systemd.Resolve io.systemd.Resolve.ResolveRecord '{"name":"localhost","type":1}' --json=short | jq -rc 'del(.rrs | .[] | .ifindex)')" == '{"rrs":[{"rr":{"key":{"class":1,"type":1,"name":"localhost"},"address":[127,0,0,1]},"raw":"CWxvY2FsaG9zdAAAAQABAAAAAAAEfwAAAQ=="}],"flags":786945}'
    test "$(varlinkctl call /run/systemd/resolve/io.systemd.Resolve io.systemd.Resolve.ResolveRecord '{"name":"localhost","type":28}' --json=short | jq -rc 'del(.rrs | .[] | .ifindex)')" == '{"rrs":[{"rr":{"key":{"class":1,"type":28,"name":"localhost"},"address":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]},"raw":"CWxvY2FsaG9zdAAAHAABAAAAAAAQAAAAAAAAAAAAAAAAAAAAAQ=="}],"flags":786945}'
 
    # Ensure that reloading keeps the manually configured address
    {
        echo "[Resolve]"
        echo "DNS=8.8.8.8"
        echo "DNSStubListenerExtra=127.0.0.153"
    } >/run/systemd/resolved.conf.d/90-reload.conf
    resolvectl dns dns0 1.1.1.1
    systemctl reload systemd-resolved.service
    resolvectl status
 
    run resolvectl dns dns0
    grep -qF "1.1.1.1" "$RUN_OUT"
 
    run resolvectl dns
    grep -qF "8.8.8.8" "$RUN_OUT"
 
    run ss -4nl
    grep -qF '127.0.0.153' "$RUN_OUT"
 
    {
        echo "[Resolve]"
        echo "DNS=8.8.4.4"
        echo "DNSStubListenerExtra=127.0.0.154"
    } >/run/systemd/resolved.conf.d/90-reload.conf
    systemctl reload systemd-resolved.service
    resolvectl status
 
    run resolvectl dns dns0
    grep -qF "1.1.1.1" "$RUN_OUT"
 
    run resolvectl dns
    (! grep -qF "8.8.8.8" "$RUN_OUT")
    grep -qF "8.8.4.4" "$RUN_OUT"
 
    run ss -4nl
    (! grep -qF '127.0.0.153' "$RUN_OUT")
    grep -qF '127.0.0.154' "$RUN_OUT"
 
    # Check if resolved exits cleanly.
    restart_resolved
}
 
# Test io.systemd.Resolve.Monitor.SubscribeDNSConfiguration
testcase_13_varlink_subscribe_dns_configuration() {
    # FIXME: for some reasons, the test case unexpectedly fail when running on sanitizers.
    if [[ -v ASAN_OPTIONS ]]; then
        return 0
    fi
 
    # Cleanup
    # shellcheck disable=SC2317,SC2329
    cleanup() {
        echo "===== io.systemd.Resolve.Monitor.SubscribeDNSConfiguration output: ====="
        cat "$tmpfile"
        echo "=========="
        rm -f /run/systemd/resolved.conf.d/90-global-dns.conf
        restart_resolved
        resolvectl revert dns0
    }
 
    trap cleanup RETURN ERR
 
    local unit
    local tmpfile
 
    unit="subscribe-dns-configuration-$(systemd-id128 new -u).service"
    tmpfile=$(mktemp)
 
    # Clear global and per-interface DNS before monitoring the configuration change.
    mkdir -p /run/systemd/resolved.conf.d/
    {
        echo "[Resolve]"
        echo "DNS="
    } >/run/systemd/resolved.conf.d/90-global-dns.conf
    systemctl reload systemd-resolved.service
    resolvectl dns dns0 ""
    resolvectl domain dns0 ""
 
    # Start the call to io.systemd.Resolve.Monitor.SubscribeDNSConfiguration
    systemd-run -u "$unit" -p "Type=exec" -p "StandardOutput=truncate:$tmpfile" \
        varlinkctl call --more --timeout=5 --graceful=io.systemd.TimedOut /run/systemd/resolve/io.systemd.Resolve.Monitor io.systemd.Resolve.Monitor.SubscribeDNSConfiguration '{}'
 
    # Wait until the initial configuration has been received.
    timeout 5 bash -c "until [[ -s $tmpfile ]]; do sleep 0.1; done"
 
    # Update the global configuration.
    mkdir -p /run/systemd/resolved.conf.d/
    {
        echo "[Resolve]"
        echo "DNS=8.8.8.8"
        echo "Domains=lan"
    } >/run/systemd/resolved.conf.d/90-global-dns.conf
    systemctl reload systemd-resolved.service
 
    # Update a link configuration.
    resolvectl dns dns0 8.8.4.4 1.1.1.1
    resolvectl domain dns0 ~.
 
    # Wait for the monitor to exit gracefully.
    while systemctl --quiet is-active "$unit"; do
        sleep 0.5
    done
 
    # Hack to remove the "Method call returned expected error" line from the output.
    sed -i '/^Method call.*returned expected error/d' "$tmpfile"
 
    # Check that an initial reply was given with the settings applied BEFORE the monitor started.
    grep -qF \
        '{"global":{"servers":null,"domains":null}}' \
        <(jq -cr --seq  '.configuration[] | select(.ifname == null) | {"global": {servers: .servers, domains: .searchDomains}}' "$tmpfile")
    grep -qF \
        '{"dns0":{"servers":null,"domains":null}}' \
        <(jq -cr --seq  '.configuration[] | select(.ifname == "dns0") | {"dns0": {servers: .servers, domains: .searchDomains}}' "$tmpfile")
 
    # Check that the global configuration change was reflected.
    grep -qF \
        '{"global":{"servers":[[8,8,8,8]],"domains":["lan"]}}' \
        <(jq -cr --seq  '.configuration[] | select(.ifname == null and .servers != null and .searchDomains != null) | {"global":{servers: [.servers[] | .address], domains: [.searchDomains[] | .name]}}' "$tmpfile")
 
    # Check that the link configuration change was reflected.
    grep -qF \
        '{"dns0":{"servers":[[8,8,4,4],[1,1,1,1]],"domains":["."]}}' \
        <(jq -cr --seq  '.configuration[] | select(.ifname == "dns0" and .servers != null and .searchDomains != null) | {"dns0":{servers: [.servers[] | .address], domains: [.searchDomains[] | .name]}}' "$tmpfile")
}
 
# Test RefuseRecordTypes
testcase_14_refuse_record_types() {
    # shellcheck disable=SC2317,SC2329
    cleanup() {
        rm -f /run/systemd/resolved.conf.d/90-refuserecords.conf
        restart_resolved
    }
    trap cleanup RETURN ERR
 
    mkdir -p /run/systemd/resolved.conf.d
    {
        echo "[Resolve]"
        echo "RefuseRecordTypes=AAAA SRV TXT"
    } >/run/systemd/resolved.conf.d/90-refuserecords.conf
    systemctl reload systemd-resolved.service
 
    run dig localhost -t AAAA
    grep -qF "status: REFUSED" "$RUN_OUT"
 
    run dig localhost @127.0.0.54 -t AAAA
    grep -qF "status: REFUSED" "$RUN_OUT"
 
    run dig localhost -t SRV
    grep -qF "status: REFUSED" "$RUN_OUT"
 
    run dig localhost @127.0.0.54 -t SRV
    grep -qF "status: REFUSED" "$RUN_OUT"
 
    run dig localhost -t TXT
    grep -qF "status: REFUSED" "$RUN_OUT"
 
    run dig localhost @127.0.0.54 -t TXT
    grep -qF "status: REFUSED" "$RUN_OUT"
 
    run dig localhost -t A
    grep -qF "status: NOERROR" "$RUN_OUT"
 
    run dig localhost @127.0.0.54 -t A
    grep -qF "status: NOERROR" "$RUN_OUT"
 
    run resolvectl query localhost5
    grep -qF "127.128.0.5" "$RUN_OUT"
 
    (! run resolvectl query localhost5 --type=SRV)
    grep -qF "DNS query type refused" "$RUN_OUT"
 
    (! run resolvectl query localhost5 --type=TXT)
    grep -qF "DNS query type refused" "$RUN_OUT"
 
    (! run resolvectl query localhost5 --type=AAAA)
    grep -qF "DNS query type refused" "$RUN_OUT"
 
    run resolvectl query localhost5 --type=A
    grep -qF "127.128.0.5" "$RUN_OUT"
 
    (! run resolvectl service _mysvc._tcp signed.test)
    (! run varlinkctl call /run/systemd/resolve/io.systemd.Resolve io.systemd.Resolve.ResolveService '{"name":"","type":"_mysvc._tcp","domain":"signed.test"}')
 
    # Filter only AAAA
    {
        echo "[Resolve]"
        echo "RefuseRecordTypes=AAAA"
    } >/run/systemd/resolved.conf.d/90-refuserecords.conf
    systemctl reload systemd-resolved.service
 
    run dig localhost -t SRV
    grep -qF "status: NOERROR" "$RUN_OUT"
 
    run dig localhost @127.0.0.54 -t SRV
    grep -qF "status: NOERROR" "$RUN_OUT"
 
    run dig localhost -t TXT
    grep -qF "status: NOERROR" "$RUN_OUT"
 
    run dig localhost @127.0.0.54 -t TXT
    grep -qF "status: NOERROR" "$RUN_OUT"
 
    run dig localhost -t AAAA
    grep -qF "status: REFUSED" "$RUN_OUT"
 
    run dig localhost @127.0.0.54 -t AAAA
    grep -qF "status: REFUSED" "$RUN_OUT"
 
    (! run resolvectl query localhost5 --type=SRV)
    grep -qF "does not have any RR of the requested type" "$RUN_OUT"
 
    (! run resolvectl query localhost5 --type=TXT)
    grep -qF "does not have any RR of the requested type" "$RUN_OUT"
 
    (! run resolvectl query localhost5 --type=AAAA)
    grep -qF "DNS query type refused" "$RUN_OUT"
 
    run resolvectl service _mysvc._tcp signed.test
    grep -qF "myservice.signed.test:1234" "$RUN_OUT"
    (! grep -qF "This is TXT for myservice" "$RUN_OUT")
    grep -qF "10.0.0.20" "$RUN_OUT"
    (! grep -qF "fd00:dead:beef:cafe::17" "$RUN_OUT")
    grep -qF "authenticated: yes" "$RUN_OUT"
 
    run varlinkctl call /run/systemd/resolve/io.systemd.Resolve io.systemd.Resolve.ResolveService '{"name":"","type":"_mysvc._tcp","domain":"signed.test"}'
    grep -qF '"services":[{"priority":10,"weight":5,"port":1234,"hostname":"myservice.signed.test","canonicalName":"myservice.signed.test"' "$RUN_OUT"
    grep -qF '"addresses":[{"ifindex":' "$RUN_OUT"
    grep -qF '"family":2,"address":[10,0,0,20]' "$RUN_OUT"
    (! grep -qF '"family":10,"address":[253,0,222,173,190,239,202,254,0,0,0,0,0,0,0,23]' "$RUN_OUT")
    grep -qF '"txt":["This is TXT for myservice"]' "$RUN_OUT"
    grep -qF '"canonical":{"name":null,"type":"_mysvc._tcp","domain":"signed.test"}' "$RUN_OUT"
 
    # Filter both A and AAAA
    {
        echo "[Resolve]"
        echo "RefuseRecordTypes=A AAAA"
    } >/run/systemd/resolved.conf.d/90-refuserecords.conf
    systemctl reload systemd-resolved.service
 
    run resolvectl service _mysvc._tcp signed.test
    grep -qF "myservice.signed.test:1234" "$RUN_OUT"
    (! grep -qF "This is TXT for myservice" "$RUN_OUT")
    (! grep -qF "10.0.0.20" "$RUN_OUT")
    (! grep -qF "fd00:dead:beef:cafe::17" "$RUN_OUT")
    grep -qF "authenticated: yes" "$RUN_OUT"
 
    run varlinkctl call /run/systemd/resolve/io.systemd.Resolve io.systemd.Resolve.ResolveService '{"name":"","type":"_mysvc._tcp","domain":"signed.test"}'
    grep -qF '"services":[{"priority":10,"weight":5,"port":1234,"hostname":"myservice.signed.test"}]' "$RUN_OUT"
    (! grep -qF '"addresses":[{"ifindex":' "$RUN_OUT")
    (! grep -qF '"family":2,"address":[10,0,0,20]' "$RUN_OUT")
    (! grep -qF '"family":10,"address":[253,0,222,173,190,239,202,254,0,0,0,0,0,0,0,23]' "$RUN_OUT")
    grep -qF '"txt":["This is TXT for myservice"]' "$RUN_OUT"
    grep -qF '"canonical":{"name":null,"type":"_mysvc._tcp","domain":"signed.test"}' "$RUN_OUT"
 
    # Filter AAAA and TXT
    {
        echo "[Resolve]"
        echo "RefuseRecordTypes=AAAA TXT"
    } >/run/systemd/resolved.conf.d/90-refuserecords.conf
    systemctl reload systemd-resolved.service
 
    run resolvectl service _mysvc._tcp signed.test
    grep -qF "myservice.signed.test:1234" "$RUN_OUT"
    grep -qF "10.0.0.20" "$RUN_OUT"
    (! grep -qF "fd00:dead:beef:cafe::17" "$RUN_OUT")
    grep -qF "authenticated: yes" "$RUN_OUT"
 
    run varlinkctl call /run/systemd/resolve/io.systemd.Resolve io.systemd.Resolve.ResolveService '{"name":"","type":"_mysvc._tcp","domain":"signed.test"}'
    grep -qF '"services":[{"priority":10,"weight":5,"port":1234,"hostname":"myservice.signed.test","canonicalName":"myservice.signed.test"' "$RUN_OUT"
    grep -qF '"addresses":[{"ifindex":' "$RUN_OUT"
    grep -qF '"family":2,"address":[10,0,0,20]' "$RUN_OUT"
    (! grep -qF '"family":10,"address":[253,0,222,173,190,239,202,254,0,0,0,0,0,0,0,23]' "$RUN_OUT")
    (! grep -qF '"txt":["This is TXT for myservice"]' "$RUN_OUT")
    grep -qF '"canonical":{"name":null,"type":"_mysvc._tcp","domain":"signed.test"}' "$RUN_OUT"
 
    # Filter SRV
    {
        echo "[Resolve]"
        echo "RefuseRecordTypes=SRV"
    } >/run/systemd/resolved.conf.d/90-refuserecords.conf
    systemctl reload systemd-resolved.service
 
    (! run resolvectl service _mysvc._tcp signed.test)
    (! run varlinkctl call /run/systemd/resolve/io.systemd.Resolve io.systemd.Resolve.ResolveService '{"name":"","type":"_mysvc._tcp","domain":"signed.test"}')
}
 
# Test systemd-networkd-wait-online interactions with systemd-resolved
testcase_15_wait_online_dns() {
    # Cleanup
    # shellcheck disable=SC2317,SC2329
    cleanup() {
        echo "===== journalctl -u $unit ====="
        journalctl -b --no-pager --no-hostname --full -u "$unit"
        echo "=========="
        rm -f "$override" "$cursor_file"
        restart_resolved
        resolvectl revert dns0
    }
 
    trap cleanup RETURN ERR
 
    local unit
    local override
    local cursor_file
 
    unit="wait-online-dns-$(systemd-id128 new -u).service"
    override="/run/systemd/resolved.conf.d/90-global-dns.conf"
 
    # Clear global and per-interface DNS before monitoring the configuration change.
    mkdir -p "$(dirname "$override")"
    {
        echo "[Resolve]"
        echo "DNS="
        echo "FallbackDNS="
    } >"$override"
    systemctl reload systemd-resolved.service
    resolvectl dns dns0 ""
    resolvectl domain dns0 ""
 
    # Stop systemd-resolved before calling systemd-networkd-wait-online. It should retry connections.
    systemctl stop systemd-resolved-monitor.socket systemd-resolved-varlink.socket
    systemctl stop systemd-resolved.service
    systemctl start systemd-resolved-monitor.socket systemd-resolved-varlink.socket
 
    # Capture a journal cursor before starting the unit so we can match only on
    # log messages emitted afterwards. We deliberately do not filter on
    # _SYSTEMD_UNIT= because journald may attach stale cgroup metadata
    # (e.g. _SYSTEMD_UNIT=init.scope) to the very first messages emitted by a
    # freshly-spawned process, before its cgroup migration into the new service
    # is observed. Filtering by SYSLOG_IDENTIFIER and a cursor is not affected
    # by that race.
    cursor_file=$(mktemp)
    journalctl -n 0 --cursor-file="$cursor_file"
 
    # Begin systemd-networkd-wait-online --dns
    systemd-run -u "$unit" -p "Environment=SYSTEMD_LOG_LEVEL=debug" -p "Environment=SYSTEMD_LOG_TARGET=journal" --service-type=exec \
        /usr/lib/systemd/systemd-networkd-wait-online --timeout=0 --dns --interface=dns0
 
    # Wait until it blocks waiting for updated DNS config.
    # Note: don't use 'journalctl -f | grep -m1 ...' here. Once grep exits on
    # match, journalctl -f will only notice the closed pipe on its next write
    # attempt, which may never come for an otherwise idle unit, causing the
    # pipeline to hang.
    timeout 30 bash -c "until journalctl --after-cursor=\"\$(cat \"$cursor_file\")\" SYSLOG_IDENTIFIER=systemd-networkd-wait-online --grep 'dns0: No.*DNS server is accessible' >/dev/null 2>&1; do sleep 0.5; done"
 
    # Update the global configuration. Restart rather than reload systemd-resolved so that
    # systemd-networkd-wait-online has to re-connect to the varlink service.
    {
        echo "[Resolve]"
        echo "DNS=10.0.0.1"
    } >"$override"
    systemctl restart systemd-resolved.service
 
    # Wait for the monitor to exit gracefully.
    timeout 30 bash -c "while systemctl --quiet is-active $unit; do sleep 0.5; done"
    journalctl --sync
 
    # Check that a disconnect happened, and was handled.
    journalctl --after-cursor="$(cat "$cursor_file")" SYSLOG_IDENTIFIER=systemd-networkd-wait-online --grep="DNS configuration monitor disconnected, reconnecting..." >/dev/null
 
    # Check that dns0 was found to be online.
    journalctl --after-cursor="$(cat "$cursor_file")" SYSLOG_IDENTIFIER=systemd-networkd-wait-online --grep="dns0: link is configured by networkd and online." >/dev/null
}
 
testcase_delegate() {
    # Before we install the delegation file the DNS name should be directly resolvable via our DNS server
    run resolvectl query delegation.exercise.test
    grep -qF "1.2.3.4" "$RUN_OUT"
 
    mkdir -p /run/systemd/dns-delegate.d/
    cat >/run/systemd/dns-delegate.d/testcase.dns-delegate <<EOF
[Delegate]
DNS=192.168.77.78 192.168.77.78 192.168.77.79
Domains=exercise.test
FirewallMark=42
EOF
    systemctl reload systemd-resolved
    resolvectl status
 
    assert_eq "$(resolvectl --json=short | jq -rc '.[] | select(.delegate == "testcase") | .servers | .[0].addressString')" '192.168.77.78'
    assert_eq "$(resolvectl --json=short | jq -rc '.[] | select(.delegate == "testcase") | .servers | .[1].addressString')" '192.168.77.79'
    assert_eq "$(resolvectl --json=short | jq -rc '.[] | select(.delegate == "testcase") | .searchDomains | .[0].name')" 'exercise.test'
 
    # Now that we installed the delegation the resolution should fail, because nothing is listening on that IP address
    (! resolvectl query delegation.exercise.test)
 
    # Now make that IP address connectible
    ip link add delegate0 type dummy
    ip addr add 192.168.77.78 dev delegate0
 
    # This should work now
    run resolvectl query delegation.exercise.test
    grep -qF "1.2.3.4" "$RUN_OUT"
 
    ip link del delegate0
 
    # Let's restart here, as a way to ensure the rtnetlink delete is definitely processed.
    systemctl restart systemd-resolved
 
    # Should no longer work
    (! resolvectl query delegation.exercise.test)
 
    rm /run/systemd/dns-delegate.d/testcase.dns-delegate
    systemctl reload systemd-resolved
 
    # Should work again without delegation in the mix
    run resolvectl query delegation.exercise.test
    grep -qF "1.2.3.4" "$RUN_OUT"
}
 
# Regression test for CVE/strict-DoT verification: when the global
# DNSOverTLS= is "no" or "opportunistic" but a specific link is configured
# with DNSOverTLS=yes (per-link strict mode), resolved must still enable
# OpenSSL peer certificate verification and host/IP name checking on that
# link. Previously dnstls_stream_connect_tls() looked at the manager-wide
# field directly instead of dns_server_get_dns_over_tls_mode(), so a
# self-signed/untrusted DoT server would be silently accepted on a strict
# link, allowing trivial MITM.
testcase_dot_strict_per_link_verify() {
    if ! command -v openssl >/dev/null; then
        echo "openssl not found, skipping per-link strict DoT test"
        return 0
    fi
 
    local certdir s_server_pid="" link_addr="10.123.99.1"
    local journal_cursor
 
    certdir="$(mktemp -d)"
 
    # shellcheck disable=SC2317,SC2329
    cleanup() {
        if [[ -n "$s_server_pid" ]] && kill -0 "$s_server_pid" 2>/dev/null; then
            kill "$s_server_pid" || :
            wait "$s_server_pid" 2>/dev/null || :
        fi
        rm -f /run/systemd/resolved.conf.d/95-strict-dot-test.conf
        rm -f /run/systemd/network/10-dns3-strict-dot.netdev
        rm -f /run/systemd/network/10-dns3-strict-dot.network
        networkctl reload || :
        ip link del dns3-sdot 2>/dev/null || :
        systemctl reload systemd-resolved.service || :
        rm -rf "$certdir" "$journal_cursor"
    }
 
    trap cleanup RETURN ERR
 
    # Self-signed cert for CN=strict-dot.test, with a SAN matching the link
    # IP. It is intentionally NOT in the system CA store, so peer
    # verification (when actually enabled) MUST reject it.
    openssl req -x509 -newkey rsa:2048 -nodes \
        -keyout "$certdir/key.pem" -out "$certdir/cert.pem" \
        -subj "/CN=strict-dot.test" \
        -addext "subjectAltName=IP:${link_addr},DNS:strict-dot.test" \
        -days 1
 
    # Dummy interface that hosts both the fake DoT server and the strict
    # client-side configuration.
    {
        echo "[NetDev]"
        echo "Name=dns3-sdot"
        echo "Kind=dummy"
    } >/run/systemd/network/10-dns3-strict-dot.netdev
    {
        echo "[Match]"
        echo "Name=dns3-sdot"
        echo ""
        echo "[Network]"
        echo "IPv6AcceptRA=no"
        echo "Address=${link_addr}/24"
        echo "DNS=${link_addr}"
        echo "DNSOverTLS=yes"
        echo "DNSSEC=no"
    } >/run/systemd/network/10-dns3-strict-dot.network
 
    # Force the manager-wide DoT mode to "no". Before the fix this would
    # cause SSL_VERIFY_PEER to NOT be set, even though the link is strict.
    {
        echo "[Resolve]"
        echo "DNSOverTLS=no"
    } >/run/systemd/resolved.conf.d/95-strict-dot-test.conf
 
    networkctl reload
    networkctl reconfigure dns3-sdot
    /usr/lib/systemd/systemd-networkd-wait-online --timeout=30 --interface=dns3-sdot --dns
 
    systemctl reload systemd-resolved.service
 
    assert_eq "$(resolvectl --json=short dnsovertls dns3-sdot | jq -rc '.[0].dnsOverTLS')" 'yes'
 
    # Start an unauthenticated TLS listener using the self-signed cert.
    # -naccept lets resolved reconnect a few times during retries.
    openssl s_server \
        -accept "${link_addr}:853" \
        -cert "$certdir/cert.pem" -key "$certdir/key.pem" \
        -quiet -naccept 8 </dev/null >/dev/null 2>&1 &
    s_server_pid=$!
 
    timeout 10s bash -c "until ss -lnt 'sport = :853' | grep -F ${link_addr} >/dev/null; do sleep 0.2; done"
 
    journal_cursor="$(mktemp)"
    journalctl -n0 -q --cursor-file="$journal_cursor"
 
    # The query must fail: a strict-DoT link must refuse an untrusted cert.
    set +e
    timeout 15s resolvectl query -i dns3-sdot strict-dot.test
    local rc=$?
    set -e
    if [[ $rc -eq 0 ]]; then
        echo "resolvectl query against untrusted DoT server on strict link succeeded, should have failed"
        return 1
    fi
 
    # Ensure the test failed for the right reason
    journalctl --sync
    journalctl -u systemd-resolved.service --cursor-file="$journal_cursor" \
        --grep "Failed to invoke SSL_do_handshake.*(certificate verify failed|self[- ]signed certificate|unable to get local issuer)"
}
 
testcase_static_record() {
    mkdir -p /run/systemd/resolve/static.d/
    cat >/run/systemd/resolve/static.d/statictest.rr <<EOF
{
        "key": { "name" : "statictest.waldo", "type" : 1 },
        "address" : [ 5, 7, 9, 11 ]
}
EOF
    cat >/run/systemd/resolve/static.d/statictest2.rr <<EOF
[
        {
                "key": { "name" : "statictest2.waldo", "type" : 1 },
                "address" : [ 5, 7, 9, 12 ]
        },
        {
                "key": { "name" : "statictest2.waldo", "type" : 28 },
                "address" : [ 10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 11, 10, 12 ]
        }
]
EOF
    cat >/run/systemd/resolve/static.d/garbage.rr <<EOF
[
        {
                "key": { "name" : "invalid...domain", "type" : 1 },
                "address" : [ 5, 7, 9, 12 ]
        },
]
EOF
    cat >/run/systemd/resolve/static.d/garbage2.rr <<EOF
[
        {
                "key": { "name" : "piff", "type" : 9 },
        },
]
EOF
 
    systemctl reload systemd-resolved
 
    run resolvectl query statictest.waldo
    grep -qF 5.7.9.11 "$RUN_OUT"
 
    run resolvectl query statictest2.waldo
    grep -qF 5.7.9.12 "$RUN_OUT"
    grep -qF a0b:a0b:a0b:a0b:a0b:a0b:a0b:a0c "$RUN_OUT"
 
    rm /run/systemd/resolve/static.d/statictest*.rr /run/systemd/resolve/static.d/garbage*.rr
    systemctl reload systemd-resolved
}
 
# PRE-SETUP
systemctl unmask systemd-resolved.service
systemctl enable --now systemd-resolved.service
 
# Need to be run before SETUP, otherwise things will break
manual_testcase_01_resolvectl
manual_testcase_02_mdns_llmnr
manual_testcase_03_register_service
 
# Run setup
setup
 
# Run tests
run_testcases
 
touch /testok