node.py 11 KB

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