| 1 |
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license |
| 2 |
|
| 3 |
# Copyright (C) 2001-2017 Nominum, Inc. |
| 4 |
# |
| 5 |
# Permission to use, copy, modify, and distribute this software and its |
| 6 |
# documentation for any purpose with or without fee is hereby granted, |
| 7 |
# provided that the above copyright notice and this permission notice |
| 8 |
# appear in all copies. |
| 9 |
# |
| 10 |
# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES |
| 11 |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR |
| 13 |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 16 |
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 |
|
| 18 |
"""DNS nodes. A node is a set of rdatasets.""" |
| 19 |
|
| 20 |
import enum |
| 21 |
import io |
| 22 |
from typing import Any, Dict |
| 23 |
|
| 24 |
import dns.immutable |
| 25 |
import dns.name |
| 26 |
import dns.rdataclass |
| 27 |
import dns.rdataset |
| 28 |
import dns.rdatatype |
| 29 |
import dns.rrset |
| 30 |
|
| 31 |
_cname_types = { |
| 32 |
dns.rdatatype.CNAME, |
| 33 |
} |
| 34 |
|
| 35 |
# "neutral" types can coexist with a CNAME and thus are not "other data" |
| 36 |
_neutral_types = { |
| 37 |
dns.rdatatype.NSEC, # RFC 4035 section 2.5 |
| 38 |
dns.rdatatype.NSEC3, # This is not likely to happen, but not impossible! |
| 39 |
dns.rdatatype.KEY, # RFC 4035 section 2.5, RFC 3007 |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
def _matches_type_or_its_signature(rdtypes, rdtype, covers): |
| 44 |
return rdtype in rdtypes or (rdtype == dns.rdatatype.RRSIG and covers in rdtypes) |
| 45 |
|
| 46 |
|
| 47 |
@enum.unique |
| 48 |
class NodeKind(enum.Enum): |
| 49 |
"""Rdatasets in nodes""" |
| 50 |
|
| 51 |
REGULAR = 0 # a.k.a "other data" |
| 52 |
NEUTRAL = 1 |
| 53 |
CNAME = 2 |
| 54 |
|
| 55 |
@classmethod |
| 56 |
def classify( |
| 57 |
cls, rdtype: dns.rdatatype.RdataType, covers: dns.rdatatype.RdataType |
| 58 |
) -> "NodeKind": |
| 59 |
if _matches_type_or_its_signature(_cname_types, rdtype, covers): |
| 60 |
return NodeKind.CNAME |
| 61 |
elif _matches_type_or_its_signature(_neutral_types, rdtype, covers): |
| 62 |
return NodeKind.NEUTRAL |
| 63 |
else: |
| 64 |
return NodeKind.REGULAR |
| 65 |
|
| 66 |
@classmethod |
| 67 |
def classify_rdataset(cls, rdataset: dns.rdataset.Rdataset) -> "NodeKind": |
| 68 |
return cls.classify(rdataset.rdtype, rdataset.covers) |
| 69 |
|
| 70 |
|
| 71 |
class Node: |
| 72 |
"""A Node is a set of rdatasets. |
| 73 |
|
| 74 |
A node is either a CNAME node or an "other data" node. A CNAME |
| 75 |
node contains only CNAME, KEY, NSEC, and NSEC3 rdatasets along with their |
| 76 |
covering RRSIG rdatasets. An "other data" node contains any |
| 77 |
rdataset other than a CNAME or RRSIG(CNAME) rdataset. When |
| 78 |
changes are made to a node, the CNAME or "other data" state is |
| 79 |
always consistent with the update, i.e. the most recent change |
| 80 |
wins. For example, if you have a node which contains a CNAME |
| 81 |
rdataset, and then add an MX rdataset to it, then the CNAME |
| 82 |
rdataset will be deleted. Likewise if you have a node containing |
| 83 |
an MX rdataset and add a CNAME rdataset, the MX rdataset will be |
| 84 |
deleted. |
| 85 |
""" |
| 86 |
|
| 87 |
__slots__ = ["rdatasets"] |
| 88 |
|
| 89 |
def __init__(self): |
| 90 |
# the set of rdatasets, represented as a list. |
| 91 |
self.rdatasets = [] |
| 92 |
|
| 93 |
def to_text(self, name: dns.name.Name, **kw: Dict[str, Any]) -> str: |
| 94 |
"""Convert a node to text format. |
| 95 |
|
| 96 |
Each rdataset at the node is printed. Any keyword arguments |
| 97 |
to this method are passed on to the rdataset's to_text() method. |
| 98 |
|
| 99 |
*name*, a ``dns.name.Name``, the owner name of the |
| 100 |
rdatasets. |
| 101 |
|
| 102 |
Returns a ``str``. |
| 103 |
|
| 104 |
""" |
| 105 |
|
| 106 |
s = io.StringIO() |
| 107 |
for rds in self.rdatasets: |
| 108 |
if len(rds) > 0: |
| 109 |
s.write(rds.to_text(name, **kw)) # type: ignore[arg-type] |
| 110 |
s.write("\n") |
| 111 |
return s.getvalue()[:-1] |
| 112 |
|
| 113 |
def __repr__(self): |
| 114 |
return "<DNS node " + str(id(self)) + ">" |
| 115 |
|
| 116 |
def __eq__(self, other): |
| 117 |
# |
| 118 |
# This is inefficient. Good thing we don't need to do it much. |
| 119 |
# |
| 120 |
for rd in self.rdatasets: |
| 121 |
if rd not in other.rdatasets: |
| 122 |
return False |
| 123 |
for rd in other.rdatasets: |
| 124 |
if rd not in self.rdatasets: |
| 125 |
return False |
| 126 |
return True |
| 127 |
|
| 128 |
def __ne__(self, other): |
| 129 |
return not self.__eq__(other) |
| 130 |
|
| 131 |
def __len__(self): |
| 132 |
return len(self.rdatasets) |
| 133 |
|
| 134 |
def __iter__(self): |
| 135 |
return iter(self.rdatasets) |
| 136 |
|
| 137 |
def _append_rdataset(self, rdataset): |
| 138 |
"""Append rdataset to the node with special handling for CNAME and |
| 139 |
other data conditions. |
| 140 |
|
| 141 |
Specifically, if the rdataset being appended has ``NodeKind.CNAME``, |
| 142 |
then all rdatasets other than KEY, NSEC, NSEC3, and their covering |
| 143 |
RRSIGs are deleted. If the rdataset being appended has |
| 144 |
``NodeKind.REGULAR`` then CNAME and RRSIG(CNAME) are deleted. |
| 145 |
""" |
| 146 |
# Make having just one rdataset at the node fast. |
| 147 |
if len(self.rdatasets) > 0: |
| 148 |
kind = NodeKind.classify_rdataset(rdataset) |
| 149 |
if kind == NodeKind.CNAME: |
| 150 |
self.rdatasets = [ |
| 151 |
rds |
| 152 |
for rds in self.rdatasets |
| 153 |
if NodeKind.classify_rdataset(rds) != NodeKind.REGULAR |
| 154 |
] |
| 155 |
elif kind == NodeKind.REGULAR: |
| 156 |
self.rdatasets = [ |
| 157 |
rds |
| 158 |
for rds in self.rdatasets |
| 159 |
if NodeKind.classify_rdataset(rds) != NodeKind.CNAME |
| 160 |
] |
| 161 |
# Otherwise the rdataset is NodeKind.NEUTRAL and we do not need to |
| 162 |
# edit self.rdatasets. |
| 163 |
self.rdatasets.append(rdataset) |
| 164 |
|
| 165 |
def find_rdataset( |
| 166 |
self, |
| 167 |
rdclass: dns.rdataclass.RdataClass, |
| 168 |
rdtype: dns.rdatatype.RdataType, |
| 169 |
covers: dns.rdatatype.RdataType = dns.rdatatype.NONE, |
| 170 |
create: bool = False, |
| 171 |
) -> dns.rdataset.Rdataset: |
| 172 |
"""Find an rdataset matching the specified properties in the |
| 173 |
current node. |
| 174 |
|
| 175 |
*rdclass*, a ``dns.rdataclass.RdataClass``, the class of the rdataset. |
| 176 |
|
| 177 |
*rdtype*, a ``dns.rdatatype.RdataType``, the type of the rdataset. |
| 178 |
|
| 179 |
*covers*, a ``dns.rdatatype.RdataType``, the covered type. |
| 180 |
Usually this value is ``dns.rdatatype.NONE``, but if the |
| 181 |
rdtype is ``dns.rdatatype.SIG`` or ``dns.rdatatype.RRSIG``, |
| 182 |
then the covers value will be the rdata type the SIG/RRSIG |
| 183 |
covers. The library treats the SIG and RRSIG types as if they |
| 184 |
were a family of types, e.g. RRSIG(A), RRSIG(NS), RRSIG(SOA). |
| 185 |
This makes RRSIGs much easier to work with than if RRSIGs |
| 186 |
covering different rdata types were aggregated into a single |
| 187 |
RRSIG rdataset. |
| 188 |
|
| 189 |
*create*, a ``bool``. If True, create the rdataset if it is not found. |
| 190 |
|
| 191 |
Raises ``KeyError`` if an rdataset of the desired type and class does |
| 192 |
not exist and *create* is not ``True``. |
| 193 |
|
| 194 |
Returns a ``dns.rdataset.Rdataset``. |
| 195 |
""" |
| 196 |
|
| 197 |
for rds in self.rdatasets: |
| 198 |
if rds.match(rdclass, rdtype, covers): |
| 199 |
return rds |
| 200 |
if not create: |
| 201 |
raise KeyError |
| 202 |
rds = dns.rdataset.Rdataset(rdclass, rdtype, covers) |
| 203 |
self._append_rdataset(rds) |
| 204 |
return rds |
| 205 |
|
| 206 |
def get_rdataset( |
| 207 |
self, |
| 208 |
rdclass: dns.rdataclass.RdataClass, |
| 209 |
rdtype: dns.rdatatype.RdataType, |
| 210 |
covers: dns.rdatatype.RdataType = dns.rdatatype.NONE, |
| 211 |
create: bool = False, |
| 212 |
) -> dns.rdataset.Rdataset | None: |
| 213 |
"""Get an rdataset matching the specified properties in the |
| 214 |
current node. |
| 215 |
|
| 216 |
None is returned if an rdataset of the specified type and |
| 217 |
class does not exist and *create* is not ``True``. |
| 218 |
|
| 219 |
*rdclass*, an ``int``, the class of the rdataset. |
| 220 |
|
| 221 |
*rdtype*, an ``int``, the type of the rdataset. |
| 222 |
|
| 223 |
*covers*, an ``int``, the covered type. Usually this value is |
| 224 |
dns.rdatatype.NONE, but if the rdtype is dns.rdatatype.SIG or |
| 225 |
dns.rdatatype.RRSIG, then the covers value will be the rdata |
| 226 |
type the SIG/RRSIG covers. The library treats the SIG and RRSIG |
| 227 |
types as if they were a family of |
| 228 |
types, e.g. RRSIG(A), RRSIG(NS), RRSIG(SOA). This makes RRSIGs much |
| 229 |
easier to work with than if RRSIGs covering different rdata |
| 230 |
types were aggregated into a single RRSIG rdataset. |
| 231 |
|
| 232 |
*create*, a ``bool``. If True, create the rdataset if it is not found. |
| 233 |
|
| 234 |
Returns a ``dns.rdataset.Rdataset`` or ``None``. |
| 235 |
""" |
| 236 |
|
| 237 |
try: |
| 238 |
rds = self.find_rdataset(rdclass, rdtype, covers, create) |
| 239 |
except KeyError: |
| 240 |
rds = None |
| 241 |
return rds |
| 242 |
|
| 243 |
def delete_rdataset( |
| 244 |
self, |
| 245 |
rdclass: dns.rdataclass.RdataClass, |
| 246 |
rdtype: dns.rdatatype.RdataType, |
| 247 |
covers: dns.rdatatype.RdataType = dns.rdatatype.NONE, |
| 248 |
) -> None: |
| 249 |
"""Delete the rdataset matching the specified properties in the |
| 250 |
current node. |
| 251 |
|
| 252 |
If a matching rdataset does not exist, it is not an error. |
| 253 |
|
| 254 |
*rdclass*, an ``int``, the class of the rdataset. |
| 255 |
|
| 256 |
*rdtype*, an ``int``, the type of the rdataset. |
| 257 |
|
| 258 |
*covers*, an ``int``, the covered type. |
| 259 |
""" |
| 260 |
|
| 261 |
rds = self.get_rdataset(rdclass, rdtype, covers) |
| 262 |
if rds is not None: |
| 263 |
self.rdatasets.remove(rds) |
| 264 |
|
| 265 |
def replace_rdataset(self, replacement: dns.rdataset.Rdataset) -> None: |
| 266 |
"""Replace an rdataset. |
| 267 |
|
| 268 |
It is not an error if there is no rdataset matching *replacement*. |
| 269 |
|
| 270 |
Ownership of the *replacement* object is transferred to the node; |
| 271 |
in other words, this method does not store a copy of *replacement* |
| 272 |
at the node, it stores *replacement* itself. |
| 273 |
|
| 274 |
*replacement*, a ``dns.rdataset.Rdataset``. |
| 275 |
|
| 276 |
Raises ``ValueError`` if *replacement* is not a |
| 277 |
``dns.rdataset.Rdataset``. |
| 278 |
""" |
| 279 |
|
| 280 |
if not isinstance(replacement, dns.rdataset.Rdataset): |
| 281 |
raise ValueError("replacement is not an rdataset") |
| 282 |
if isinstance(replacement, dns.rrset.RRset): |
| 283 |
# RRsets are not good replacements as the match() method |
| 284 |
# is not compatible. |
| 285 |
replacement = replacement.to_rdataset() |
| 286 |
self.delete_rdataset( |
| 287 |
replacement.rdclass, replacement.rdtype, replacement.covers |
| 288 |
) |
| 289 |
self._append_rdataset(replacement) |
| 290 |
|
| 291 |
def classify(self) -> NodeKind: |
| 292 |
"""Classify a node. |
| 293 |
|
| 294 |
A node which contains a CNAME or RRSIG(CNAME) is a |
| 295 |
``NodeKind.CNAME`` node. |
| 296 |
|
| 297 |
A node which contains only "neutral" types, i.e. types allowed to |
| 298 |
co-exist with a CNAME, is a ``NodeKind.NEUTRAL`` node. The neutral |
| 299 |
types are NSEC, NSEC3, KEY, and their associated RRSIGS. An empty node |
| 300 |
is also considered neutral. |
| 301 |
|
| 302 |
A node which contains some rdataset which is not a CNAME, RRSIG(CNAME), |
| 303 |
or a neutral type is a a ``NodeKind.REGULAR`` node. Regular nodes are |
| 304 |
also commonly referred to as "other data". |
| 305 |
""" |
| 306 |
for rdataset in self.rdatasets: |
| 307 |
kind = NodeKind.classify(rdataset.rdtype, rdataset.covers) |
| 308 |
if kind != NodeKind.NEUTRAL: |
| 309 |
return kind |
| 310 |
return NodeKind.NEUTRAL |
| 311 |
|
| 312 |
def is_immutable(self) -> bool: |
| 313 |
return False |
| 314 |
|
| 315 |
|
| 316 |
@dns.immutable.immutable |
| 317 |
class ImmutableNode(Node): |
| 318 |
def __init__(self, node): |
| 319 |
super().__init__() |
| 320 |
self.rdatasets = tuple( |
| 321 |
[dns.rdataset.ImmutableRdataset(rds) for rds in node.rdatasets] |
| 322 |
) |
| 323 |
|
| 324 |
def find_rdataset( |
| 325 |
self, |
| 326 |
rdclass: dns.rdataclass.RdataClass, |
| 327 |
rdtype: dns.rdatatype.RdataType, |
| 328 |
covers: dns.rdatatype.RdataType = dns.rdatatype.NONE, |
| 329 |
create: bool = False, |
| 330 |
) -> dns.rdataset.Rdataset: |
| 331 |
if create: |
| 332 |
raise TypeError("immutable") |
| 333 |
return super().find_rdataset(rdclass, rdtype, covers, False) |
| 334 |
|
| 335 |
def get_rdataset( |
| 336 |
self, |
| 337 |
rdclass: dns.rdataclass.RdataClass, |
| 338 |
rdtype: dns.rdatatype.RdataType, |
| 339 |
covers: dns.rdatatype.RdataType = dns.rdatatype.NONE, |
| 340 |
create: bool = False, |
| 341 |
) -> dns.rdataset.Rdataset | None: |
| 342 |
if create: |
| 343 |
raise TypeError("immutable") |
| 344 |
return super().get_rdataset(rdclass, rdtype, covers, False) |
| 345 |
|
| 346 |
def delete_rdataset( |
| 347 |
self, |
| 348 |
rdclass: dns.rdataclass.RdataClass, |
| 349 |
rdtype: dns.rdatatype.RdataType, |
| 350 |
covers: dns.rdatatype.RdataType = dns.rdatatype.NONE, |
| 351 |
) -> None: |
| 352 |
raise TypeError("immutable") |
| 353 |
|
| 354 |
def replace_rdataset(self, replacement: dns.rdataset.Rdataset) -> None: |
| 355 |
raise TypeError("immutable") |
| 356 |
|
| 357 |
def is_immutable(self) -> bool: |
| 358 |
return True |