update.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. # Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
  2. # Copyright (C) 2003-2007, 2009-2011 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 Dynamic Update Support"""
  17. import dns.message
  18. import dns.name
  19. import dns.opcode
  20. import dns.rdata
  21. import dns.rdataclass
  22. import dns.rdataset
  23. import dns.tsig
  24. class UpdateSection(dns.enum.IntEnum):
  25. """Update sections"""
  26. ZONE = 0
  27. PREREQ = 1
  28. UPDATE = 2
  29. ADDITIONAL = 3
  30. @classmethod
  31. def _maximum(cls):
  32. return 3
  33. class UpdateMessage(dns.message.Message):
  34. _section_enum = UpdateSection
  35. def __init__(self, zone=None, rdclass=dns.rdataclass.IN, keyring=None,
  36. keyname=None, keyalgorithm=dns.tsig.default_algorithm,
  37. id=None):
  38. """Initialize a new DNS Update object.
  39. See the documentation of the Message class for a complete
  40. description of the keyring dictionary.
  41. *zone*, a ``dns.name.Name``, ``str``, or ``None``, the zone
  42. which is being updated. ``None`` should only be used by dnspython's
  43. message constructors, as a zone is required for the convenience
  44. methods like ``add()``, ``replace()``, etc.
  45. *rdclass*, an ``int`` or ``str``, the class of the zone.
  46. The *keyring*, *keyname*, and *keyalgorithm* parameters are passed to
  47. ``use_tsig()``; see its documentation for details.
  48. """
  49. super().__init__(id=id)
  50. self.flags |= dns.opcode.to_flags(dns.opcode.UPDATE)
  51. if isinstance(zone, str):
  52. zone = dns.name.from_text(zone)
  53. self.origin = zone
  54. rdclass = dns.rdataclass.RdataClass.make(rdclass)
  55. self.zone_rdclass = rdclass
  56. if self.origin:
  57. self.find_rrset(self.zone, self.origin, rdclass, dns.rdatatype.SOA,
  58. create=True, force_unique=True)
  59. if keyring is not None:
  60. self.use_tsig(keyring, keyname, algorithm=keyalgorithm)
  61. @property
  62. def zone(self):
  63. """The zone section."""
  64. return self.sections[0]
  65. @zone.setter
  66. def zone(self, v):
  67. self.sections[0] = v
  68. @property
  69. def prerequisite(self):
  70. """The prerequisite section."""
  71. return self.sections[1]
  72. @prerequisite.setter
  73. def prerequisite(self, v):
  74. self.sections[1] = v
  75. @property
  76. def update(self):
  77. """The update section."""
  78. return self.sections[2]
  79. @update.setter
  80. def update(self, v):
  81. self.sections[2] = v
  82. def _add_rr(self, name, ttl, rd, deleting=None, section=None):
  83. """Add a single RR to the update section."""
  84. if section is None:
  85. section = self.update
  86. covers = rd.covers()
  87. rrset = self.find_rrset(section, name, self.zone_rdclass, rd.rdtype,
  88. covers, deleting, True, True)
  89. rrset.add(rd, ttl)
  90. def _add(self, replace, section, name, *args):
  91. """Add records.
  92. *replace* is the replacement mode. If ``False``,
  93. RRs are added to an existing RRset; if ``True``, the RRset
  94. is replaced with the specified contents. The second
  95. argument is the section to add to. The third argument
  96. is always a name. The other arguments can be:
  97. - rdataset...
  98. - ttl, rdata...
  99. - ttl, rdtype, string...
  100. """
  101. if isinstance(name, str):
  102. name = dns.name.from_text(name, None)
  103. if isinstance(args[0], dns.rdataset.Rdataset):
  104. for rds in args:
  105. if replace:
  106. self.delete(name, rds.rdtype)
  107. for rd in rds:
  108. self._add_rr(name, rds.ttl, rd, section=section)
  109. else:
  110. args = list(args)
  111. ttl = int(args.pop(0))
  112. if isinstance(args[0], dns.rdata.Rdata):
  113. if replace:
  114. self.delete(name, args[0].rdtype)
  115. for rd in args:
  116. self._add_rr(name, ttl, rd, section=section)
  117. else:
  118. rdtype = dns.rdatatype.RdataType.make(args.pop(0))
  119. if replace:
  120. self.delete(name, rdtype)
  121. for s in args:
  122. rd = dns.rdata.from_text(self.zone_rdclass, rdtype, s,
  123. self.origin)
  124. self._add_rr(name, ttl, rd, section=section)
  125. def add(self, name, *args):
  126. """Add records.
  127. The first argument is always a name. The other
  128. arguments can be:
  129. - rdataset...
  130. - ttl, rdata...
  131. - ttl, rdtype, string...
  132. """
  133. self._add(False, self.update, name, *args)
  134. def delete(self, name, *args):
  135. """Delete records.
  136. The first argument is always a name. The other
  137. arguments can be:
  138. - *empty*
  139. - rdataset...
  140. - rdata...
  141. - rdtype, [string...]
  142. """
  143. if isinstance(name, str):
  144. name = dns.name.from_text(name, None)
  145. if len(args) == 0:
  146. self.find_rrset(self.update, name, dns.rdataclass.ANY,
  147. dns.rdatatype.ANY, dns.rdatatype.NONE,
  148. dns.rdatatype.ANY, True, True)
  149. elif isinstance(args[0], dns.rdataset.Rdataset):
  150. for rds in args:
  151. for rd in rds:
  152. self._add_rr(name, 0, rd, dns.rdataclass.NONE)
  153. else:
  154. args = list(args)
  155. if isinstance(args[0], dns.rdata.Rdata):
  156. for rd in args:
  157. self._add_rr(name, 0, rd, dns.rdataclass.NONE)
  158. else:
  159. rdtype = dns.rdatatype.RdataType.make(args.pop(0))
  160. if len(args) == 0:
  161. self.find_rrset(self.update, name,
  162. self.zone_rdclass, rdtype,
  163. dns.rdatatype.NONE,
  164. dns.rdataclass.ANY,
  165. True, True)
  166. else:
  167. for s in args:
  168. rd = dns.rdata.from_text(self.zone_rdclass, rdtype, s,
  169. self.origin)
  170. self._add_rr(name, 0, rd, dns.rdataclass.NONE)
  171. def replace(self, name, *args):
  172. """Replace records.
  173. The first argument is always a name. The other
  174. arguments can be:
  175. - rdataset...
  176. - ttl, rdata...
  177. - ttl, rdtype, string...
  178. Note that if you want to replace the entire node, you should do
  179. a delete of the name followed by one or more calls to add.
  180. """
  181. self._add(True, self.update, name, *args)
  182. def present(self, name, *args):
  183. """Require that an owner name (and optionally an rdata type,
  184. or specific rdataset) exists as a prerequisite to the
  185. execution of the update.
  186. The first argument is always a name.
  187. The other arguments can be:
  188. - rdataset...
  189. - rdata...
  190. - rdtype, string...
  191. """
  192. if isinstance(name, str):
  193. name = dns.name.from_text(name, None)
  194. if len(args) == 0:
  195. self.find_rrset(self.prerequisite, name,
  196. dns.rdataclass.ANY, dns.rdatatype.ANY,
  197. dns.rdatatype.NONE, None,
  198. True, True)
  199. elif isinstance(args[0], dns.rdataset.Rdataset) or \
  200. isinstance(args[0], dns.rdata.Rdata) or \
  201. len(args) > 1:
  202. if not isinstance(args[0], dns.rdataset.Rdataset):
  203. # Add a 0 TTL
  204. args = list(args)
  205. args.insert(0, 0)
  206. self._add(False, self.prerequisite, name, *args)
  207. else:
  208. rdtype = dns.rdatatype.RdataType.make(args[0])
  209. self.find_rrset(self.prerequisite, name,
  210. dns.rdataclass.ANY, rdtype,
  211. dns.rdatatype.NONE, None,
  212. True, True)
  213. def absent(self, name, rdtype=None):
  214. """Require that an owner name (and optionally an rdata type) does
  215. not exist as a prerequisite to the execution of the update."""
  216. if isinstance(name, str):
  217. name = dns.name.from_text(name, None)
  218. if rdtype is None:
  219. self.find_rrset(self.prerequisite, name,
  220. dns.rdataclass.NONE, dns.rdatatype.ANY,
  221. dns.rdatatype.NONE, None,
  222. True, True)
  223. else:
  224. rdtype = dns.rdatatype.RdataType.make(rdtype)
  225. self.find_rrset(self.prerequisite, name,
  226. dns.rdataclass.NONE, rdtype,
  227. dns.rdatatype.NONE, None,
  228. True, True)
  229. def _get_one_rr_per_rrset(self, value):
  230. # Updates are always one_rr_per_rrset
  231. return True
  232. def _parse_rr_header(self, section, name, rdclass, rdtype):
  233. deleting = None
  234. empty = False
  235. if section == UpdateSection.ZONE:
  236. if dns.rdataclass.is_metaclass(rdclass) or \
  237. rdtype != dns.rdatatype.SOA or \
  238. self.zone:
  239. raise dns.exception.FormError
  240. else:
  241. if not self.zone:
  242. raise dns.exception.FormError
  243. if rdclass in (dns.rdataclass.ANY, dns.rdataclass.NONE):
  244. deleting = rdclass
  245. rdclass = self.zone[0].rdclass
  246. empty = (deleting == dns.rdataclass.ANY or
  247. section == UpdateSection.PREREQ)
  248. return (rdclass, rdtype, deleting, empty)
  249. # backwards compatibility
  250. Update = UpdateMessage
  251. ### BEGIN generated UpdateSection constants
  252. ZONE = UpdateSection.ZONE
  253. PREREQ = UpdateSection.PREREQ
  254. UPDATE = UpdateSection.UPDATE
  255. ADDITIONAL = UpdateSection.ADDITIONAL
  256. ### END generated UpdateSection constants