| 1 |
""" |
| 2 |
Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
|
| 4 |
SPDX-License-Identifier: MPL-2.0 |
| 5 |
|
| 6 |
This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
|
| 10 |
See the COPYRIGHT file distributed with this work for additional |
| 11 |
information regarding copyright ownership. |
| 12 |
""" |
| 13 |
|
| 14 |
from collections.abc import AsyncGenerator, Collection, Iterable |
| 15 |
|
| 16 |
import abc |
| 17 |
|
| 18 |
import dns.rcode |
| 19 |
import dns.rdataclass |
| 20 |
import dns.rdatatype |
| 21 |
import dns.rrset |
| 22 |
|
| 23 |
from isctest.asyncserver import ( |
| 24 |
ControllableAsyncDnsServer, |
| 25 |
DnsResponseSend, |
| 26 |
QueryContext, |
| 27 |
ResponseHandler, |
| 28 |
SwitchControlCommand, |
| 29 |
) |
| 30 |
|
| 31 |
|
| 32 |
def rrset(owner: str, rdtype: dns.rdatatype.RdataType, rdata: str) -> dns.rrset.RRset: |
| 33 |
return dns.rrset.from_text( |
| 34 |
owner, |
| 35 |
300, |
| 36 |
dns.rdataclass.IN, |
| 37 |
rdtype, |
| 38 |
rdata, |
| 39 |
) |
| 40 |
|
| 41 |
|
| 42 |
def soa(serial: int, *, owner: str = "nil.") -> dns.rrset.RRset: |
| 43 |
return rrset( |
| 44 |
owner, |
| 45 |
dns.rdatatype.SOA, |
| 46 |
f"ns.nil. root.nil. {serial} 300 300 604800 300", |
| 47 |
) |
| 48 |
|
| 49 |
|
| 50 |
def ns() -> dns.rrset.RRset: |
| 51 |
return rrset( |
| 52 |
"nil.", |
| 53 |
dns.rdatatype.NS, |
| 54 |
"ns.nil.", |
| 55 |
) |
| 56 |
|
| 57 |
|
| 58 |
def a(address: str, *, owner: str) -> dns.rrset.RRset: |
| 59 |
return rrset( |
| 60 |
owner, |
| 61 |
dns.rdatatype.A, |
| 62 |
address, |
| 63 |
) |
| 64 |
|
| 65 |
|
| 66 |
def txt(data: str, *, owner: str = "nil.") -> dns.rrset.RRset: |
| 67 |
return rrset( |
| 68 |
owner, |
| 69 |
dns.rdatatype.TXT, |
| 70 |
f'"{data}"', |
| 71 |
) |
| 72 |
|
| 73 |
|
| 74 |
class SoaHandler(ResponseHandler): |
| 75 |
def __init__(self, serial: int): |
| 76 |
self._serial = serial |
| 77 |
|
| 78 |
def match(self, qctx: QueryContext) -> bool: |
| 79 |
return qctx.qtype == dns.rdatatype.SOA |
| 80 |
|
| 81 |
async def get_responses( |
| 82 |
self, qctx: QueryContext |
| 83 |
) -> AsyncGenerator[DnsResponseSend, None]: |
| 84 |
qctx.response.answer.append(soa(self._serial)) |
| 85 |
yield DnsResponseSend(qctx.response) |
| 86 |
|
| 87 |
|
| 88 |
class AxfrHandler(ResponseHandler): |
| 89 |
@property |
| 90 |
@abc.abstractmethod |
| 91 |
def answers(self) -> Iterable[Collection[dns.rrset.RRset]]: |
| 92 |
""" |
| 93 |
Answer sections of response packets sent in response to |
| 94 |
AXFR queries. |
| 95 |
""" |
| 96 |
raise NotImplementedError |
| 97 |
|
| 98 |
def match(self, qctx: QueryContext) -> bool: |
| 99 |
return qctx.qtype == dns.rdatatype.AXFR |
| 100 |
|
| 101 |
async def get_responses( |
| 102 |
self, qctx: QueryContext |
| 103 |
) -> AsyncGenerator[DnsResponseSend, None]: |
| 104 |
for answer in self.answers: |
| 105 |
response = qctx.prepare_new_response() |
| 106 |
for rrset_ in answer: |
| 107 |
response.answer.append(rrset_) |
| 108 |
yield DnsResponseSend(response) |
| 109 |
|
| 110 |
|
| 111 |
class IxfrHandler(ResponseHandler): |
| 112 |
@property |
| 113 |
@abc.abstractmethod |
| 114 |
def answer(self) -> Collection[dns.rrset.RRset]: |
| 115 |
""" |
| 116 |
Answer section of a response packet sent in response to |
| 117 |
IXFR queries. |
| 118 |
""" |
| 119 |
raise NotImplementedError |
| 120 |
|
| 121 |
def match(self, qctx: QueryContext) -> bool: |
| 122 |
return qctx.qtype == dns.rdatatype.IXFR |
| 123 |
|
| 124 |
async def get_responses( |
| 125 |
self, qctx: QueryContext |
| 126 |
) -> AsyncGenerator[DnsResponseSend, None]: |
| 127 |
for rrset_ in self.answer: |
| 128 |
qctx.response.answer.append(rrset_) |
| 129 |
yield DnsResponseSend(qctx.response) |
| 130 |
|
| 131 |
|
| 132 |
class InitialAxfrHandler(AxfrHandler): |
| 133 |
answers = ( |
| 134 |
(soa(1),), |
| 135 |
( |
| 136 |
ns(), |
| 137 |
txt("initial AXFR"), |
| 138 |
a("10.0.0.62", owner="b.nil."), |
| 139 |
), |
| 140 |
(soa(1),), |
| 141 |
) |
| 142 |
|
| 143 |
|
| 144 |
class InitialIxfrHandler(IxfrHandler): |
| 145 |
answer = ( |
| 146 |
soa(1), |
| 147 |
ns(), |
| 148 |
txt("initial AXFR"), |
| 149 |
a("10.0.0.62", owner="b.nil."), |
| 150 |
soa(1), |
| 151 |
) |
| 152 |
|
| 153 |
|
| 154 |
class NxrrsetIxfrHandler(IxfrHandler): |
| 155 |
""" |
| 156 |
IXFR from serial 1 -> 2. |
| 157 |
|
| 158 |
Deletes the only A record at b.nil. (10.0.0.62). Since this is |
| 159 |
the last record in that rdataset, subtractrdataset() returns |
| 160 |
DNS_R_NXRRSET. |
| 161 |
|
| 162 |
Also adds a TXT record so the zone has a real change beyond the |
| 163 |
SOA serial bump. |
| 164 |
""" |
| 165 |
|
| 166 |
answer = ( |
| 167 |
soa(2), |
| 168 |
soa(1), |
| 169 |
a("10.0.0.62", owner="b.nil."), |
| 170 |
txt("initial AXFR"), |
| 171 |
soa(2), |
| 172 |
txt("nxrrset ixfr test"), |
| 173 |
soa(2), |
| 174 |
) |
| 175 |
|
| 176 |
|
| 177 |
def main() -> None: |
| 178 |
server = ControllableAsyncDnsServer( |
| 179 |
default_aa=True, default_rcode=dns.rcode.NOERROR |
| 180 |
) |
| 181 |
switch_command = SwitchControlCommand( |
| 182 |
{ |
| 183 |
"initial_axfr": ( |
| 184 |
SoaHandler(1), |
| 185 |
InitialIxfrHandler(), |
| 186 |
InitialAxfrHandler(), |
| 187 |
), |
| 188 |
"nxrrset_ixfr": ( |
| 189 |
SoaHandler(2), |
| 190 |
NxrrsetIxfrHandler(), |
| 191 |
), |
| 192 |
} |
| 193 |
) |
| 194 |
server.install_control_command(switch_command) |
| 195 |
server.run() |
| 196 |
|
| 197 |
|
| 198 |
if __name__ == "__main__": |
| 199 |
main() |