dns-check.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # DNSSEC verification probe for unbound-sec.
  2. #
  3. # Builds a correct EDNS0 query. The DO (DNSSEC OK) bit lives in the OPT
  4. # record's 4-byte TTL/EXTFLAGS field (0x8000); the header must carry
  5. # arcount=1 when the OPT record is present. We then assert on the AD
  6. # (authenticated data) bit in the response header.
  7. #
  8. # Checks (all deterministic, no dependency on third-party zone signing):
  9. # 1. signed zone + DO -> NOERROR, answers, AD=1 (validation works)
  10. # 2. signed zone, no DO -> NOERROR, answers, AD=0 (AD only when requested)
  11. # 3. signed zone + DO -> NOERROR, answers, AD=1 (repeat, stability)
  12. #
  13. # Exit codes (see run-tests.sh):
  14. # 0 = all checks passed
  15. # 1 = at least one CONTENT deviation (e.g. AD bit wrong, rcode wrong)
  16. # 2 = no content deviation, but at least one check TIMED OUT (infrastructure)
  17. #
  18. # A separate restart-persistence + negative (SERVFAIL) check live in run-tests.sh.
  19. import socket
  20. import struct
  21. import sys
  22. import time
  23. server = sys.argv[1] if len(sys.argv) > 1 else "unbound"
  24. port = int(sys.argv[2]) if len(sys.argv) > 2 else 5053
  25. TRIES = 5
  26. BACKOFF = 2 # seconds
  27. class Timeout(Exception):
  28. pass
  29. def enc(name):
  30. out = b""
  31. for p in name.rstrip(".").split("."):
  32. out += bytes([len(p)]) + p.encode()
  33. return out + b"\x00"
  34. def query(name, qtype, do_bit):
  35. tid = 0x1234
  36. flags = 0x0100 # RD
  37. arcount = 1 if do_bit else 0
  38. header = struct.pack(">HHHHHH", tid, flags, 1, 0, 0, arcount)
  39. q = header + enc(name) + struct.pack(">HH", qtype, 1) # type, IN
  40. if do_bit:
  41. # OPT: root name, type=41, class=0, TTL/EXTFLAGS=DO(0x8000), rdlen=0
  42. q += b"\x00" + struct.pack(">HHIH", 41, 0, 0x8000, 0)
  43. for attempt in range(TRIES):
  44. if attempt:
  45. time.sleep(BACKOFF) # backoff between retries (WSL net hiccups)
  46. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  47. s.settimeout(15)
  48. try:
  49. s.sendto(q, (server, port))
  50. data, _ = s.recvfrom(4096)
  51. except socket.timeout:
  52. continue
  53. finally:
  54. s.close()
  55. rf = struct.unpack(">H", data[2:4])[0]
  56. an = struct.unpack(">H", data[6:8])[0]
  57. ad = (rf >> 5) & 1
  58. rc = rf & 0xF
  59. return an, ad, rc
  60. raise Timeout(name)
  61. def check(label, name, qtype, do_bit, want_ad, want_rc=0):
  62. """Return 'pass' | 'fail' | 'timeout'."""
  63. try:
  64. an, ad, rc = query(name, qtype, do_bit)
  65. except Timeout:
  66. print(f"{label}: TIMEOUT (no response after {TRIES} tries)")
  67. return "timeout"
  68. good = (rc == want_rc) and (an > 0) and (ad == want_ad)
  69. print(f"{label}: answers={an} AD={ad} rcode={rc} -> {'PASS' if good else 'FAIL'}")
  70. return "pass" if good else "fail"
  71. results = []
  72. # 1) signed zone, DO -> AD=1
  73. results.append(check("signed example.com A (DO) ", "example.com", 1, True, want_ad=1))
  74. # 2) signed zone, no DO -> AD=0
  75. results.append(check("signed example.com A (no DO)", "example.com", 1, False, want_ad=0))
  76. # 3) repeat signed+DO for stability -> AD=1
  77. results.append(check("signed example.com A (DO) ", "example.com", 1, True, want_ad=1))
  78. if any(r == "fail" for r in results):
  79. sys.exit(1)
  80. if any(r == "timeout" for r in results):
  81. sys.exit(2)
  82. sys.exit(0)