| 1 | #!/bin/bash |
| 2 | |
| 3 | # Author: Daniel Kahn Gillmor <[email protected]> |
| 4 | # 2018-08-30 |
| 5 | # License: GPLv3+ |
| 6 | |
| 7 | # error on exit |
| 8 | set -e |
| 9 | # for handling jobspecs: |
| 10 | set -m |
| 11 | |
| 12 | if [ -z "$AUTOPKGTEST_ARTIFACTS" ]; then |
| 13 | d="$(mktemp -d)" |
| 14 | remove="$d" |
| 15 | else |
| 16 | d="$AUTOPKGTEST_ARTIFACTS" |
| 17 | fi |
| 18 | ip="${TESTIP:-127.$(( $RANDOM % 256 )).$(( $RANDOM % 256 )).$(( $RANDOM % 256 ))}" |
| 19 | kresd="${KRESD:-/usr/sbin/kresd}" |
| 20 | kdig="${KDIG:-$(command -v kdig)}" |
| 21 | |
| 22 | declare -a kresd_args=(--addr="$ip@8053" --tls="$ip@8853" --noninteractive --config="$d/kresd.conf" --verbose --verbose --verbose) |
| 23 | if [ -n "$MODULE_DIR" ]; then |
| 24 | kresd_args+=(-m "$MODULE_DIR") |
| 25 | fi |
| 26 | |
| 27 | printf "%s + %s roundtrip tests\n------------\n workdir: %s\n IP addr: %s\n kresd args: %s\n" "$kresd" "$kdig" "$d" "$ip" "${kresd_args[*]}" |
| 28 | |
| 29 | section() { |
| 30 | printf "\n%s\n" "$1" |
| 31 | sed 's/./-/g' <<<"$1" |
| 32 | } |
| 33 | |
| 34 | cleanup () { |
| 35 | section "cleaning up" |
| 36 | find "$d" -ls |
| 37 | tail -n +1 -v "$d"/*.err |
| 38 | ctrl_socket=/run/knot-resolver/control/1 |
| 39 | if [ -S "$ctrl_socket" ]; then |
| 40 | echo 'quit()' | socat STDIO "UNIX-CONNECT:$ctrl_socket" |
| 41 | wait %1 |
| 42 | else |
| 43 | pkill -KILL kresd ; true |
| 44 | fi |
| 45 | |
| 46 | if [ "$remove" ]; then |
| 47 | printf "\ncleaning up working directory %s\n" "$remove" |
| 48 | rm -rf "$remove" |
| 49 | fi |
| 50 | } |
| 51 | trap cleanup EXIT |
| 52 | |
| 53 | section "make Certificate Authority key and certificate" |
| 54 | cat > "$d/ca.template" <<EOF |
| 55 | cn = "testing certificate authority (NOT FOR PRODUCTION)" |
| 56 | expiration_days = 12 |
| 57 | ca |
| 58 | path_len = 1 |
| 59 | nc_permit_dns = example |
| 60 | cert_signing_key |
| 61 | EOF |
| 62 | certtool --stdout-info --generate-privkey --outfile "$d/ca-key.pem" |
| 63 | certtool --stdout-info --generate-self-signed --template "$d/ca.template" --load-privkey "$d/ca-key.pem" --outfile "$d/ca-cert.pem" |
| 64 | |
| 65 | section "make Bogus Certificate Authority key and certificate" |
| 66 | certtool --stdout-info --generate-privkey --outfile "$d/bogus-key.pem" |
| 67 | certtool --stdout-info --generate-self-signed --template "$d/ca.template" --load-privkey "$d/bogus-key.pem" --outfile "$d/bogus-cert.pem" |
| 68 | |
| 69 | section "make End Entity key and certificate" |
| 70 | cat > "$d/ee.template" <<EOF |
| 71 | cn = "test.example" |
| 72 | dns_name = test.example |
| 73 | expiration_days = 10 |
| 74 | signing_key |
| 75 | tls_www_server |
| 76 | EOF |
| 77 | certtool --stdout-info --generate-privkey --outfile "$d/ee-key.pem" |
| 78 | certtool --stdout-info --pubkey-info --load-privkey "$d/ee-key.pem" --outfile "$d/ee-pubkey.pem" |
| 79 | certtool --stdout-info --generate-certificate --load-ca-privkey "$d/ca-key.pem" --load-ca-certificate "$d/ca-cert.pem" --template "$d/ee.template" --load-pubkey "$d/ee-pubkey.pem" --outfile "$d/ee-cert.pem" |
| 80 | |
| 81 | section "set up kresd daemon on $ip on ports 8053 (UDP, TCP) and 8853 (TLS)" |
| 82 | cat > "$d/kresd.conf" <<EOF |
| 83 | verbose(true) |
| 84 | modules = { 'hints > iterate' } |
| 85 | net.tls("$d/ee-cert.pem", "$d/ee-key.pem") |
| 86 | hints["monkeys.example"] = "127.15.23.5" |
| 87 | |
| 88 | local path = '/run/knot-resolver/control/1' |
| 89 | local ok, err = pcall(net.listen, path, nil, { kind = 'control' }) |
| 90 | if not ok then |
| 91 | warn('bind to '..path..' failed '..err) |
| 92 | end |
| 93 | EOF |
| 94 | "$kresd" "${kresd_args[@]}" "$d" 2> "$d/kresd.err" & |
| 95 | |
| 96 | sleep 1 |
| 97 | |
| 98 | section "test UDP with kdig" |
| 99 | x=$("$kdig" +short +time=2 +retry=0 @"$ip:8053" monkeys.example) |
| 100 | [ "$x" = "127.15.23.5" ] |
| 101 | echo "successful UDP request to $ip on port 8053" |
| 102 | |
| 103 | section "test TCP with kdig" |
| 104 | x=$("$kdig" +short +tcp @"$ip:8053" monkeys.example) |
| 105 | [ "$x" = "127.15.23.5" ] |
| 106 | echo "successful TCP request to $ip on port 8053" |
| 107 | |
| 108 | section "test opportunistic DNS-over-TLS with kdig" |
| 109 | x=$("$kdig" +short +tls @"$ip:8853" monkeys.example) |
| 110 | [ "$x" = "127.15.23.5" ] |
| 111 | echo "successful opportunistic DNS-over-TLS request to $ip on port 8853" |
| 112 | |
| 113 | section "test strict DNS-over-TLS with kdig" |
| 114 | x=$("$kdig" +short +tls +tls-ca="$d/ca-cert.pem" +tls-hostname=test.example @"$ip:8853" monkeys.example) |
| 115 | [ "$x" = "127.15.23.5" ] |
| 116 | echo "successful strict DNS-over-TLS request to $ip on port 8853" |
| 117 | |
| 118 | section "test invalid name with strict DNS-over-TLS with kdig" |
| 119 | # Kdig returns non-zero code if error since version 2.7.5 |
| 120 | x=$("$kdig" +tls +tls-ca="$d/ca-cert.pem" +tls-hostname=notright.example @"$ip:8853" monkeys.example 2>"$d/badname.err" || true) |
| 121 | if [ "$x" ]; then |
| 122 | printf >&2 "got: %s\nShould not have succeeded since name did not match!" "$x" |
| 123 | false |
| 124 | fi |
| 125 | echo "successful strict DNS-over-TLS request failure when name mismatch to $ip on port 8853" |
| 126 | |
| 127 | section "test bad authority with strict DNS-over-TLS with kdig" |
| 128 | # Kdig returns non-zero code if error since version 2.7.5 |
| 129 | x=$("$kdig" +tls-ca="$d/bogus-cert.pem" +tls-hostname=test.example @"$ip:8853" monkeys.example 2>"$d/badca.err" || true) |
| 130 | if [ "$x" ]; then |
| 131 | printf >&2 "got: %s\nShould not have succeeded since authority was wrong!" "$x" |
| 132 | false |
| 133 | fi |
| 134 | echo "successful strict DNS-over-TLS request failure to $ip on port 8853" |