node.py 12 KB

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