node.py 12 KB

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