You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
10 KiB

4 years ago
  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. from ._compat import string_types
  25. class Update(dns.message.Message):
  26. def __init__(self, zone, rdclass=dns.rdataclass.IN, keyring=None,
  27. keyname=None, keyalgorithm=dns.tsig.default_algorithm):
  28. """Initialize a new DNS Update object.
  29. See the documentation of the Message class for a complete
  30. description of the keyring dictionary.
  31. *zone*, a ``dns.name.Name`` or ``text``, the zone which is being
  32. updated.
  33. *rdclass*, an ``int`` or ``text``, the class of the zone.
  34. *keyring*, a ``dict``, the TSIG keyring to use. If a
  35. *keyring* is specified but a *keyname* is not, then the key
  36. used will be the first key in the *keyring*. Note that the
  37. order of keys in a dictionary is not defined, so applications
  38. should supply a keyname when a keyring is used, unless they
  39. know the keyring contains only one key.
  40. *keyname*, a ``dns.name.Name`` or ``None``, the name of the TSIG key
  41. to use; defaults to ``None``. The key must be defined in the keyring.
  42. *keyalgorithm*, a ``dns.name.Name``, the TSIG algorithm to use.
  43. """
  44. super(Update, self).__init__()
  45. self.flags |= dns.opcode.to_flags(dns.opcode.UPDATE)
  46. if isinstance(zone, string_types):
  47. zone = dns.name.from_text(zone)
  48. self.origin = zone
  49. if isinstance(rdclass, string_types):
  50. rdclass = dns.rdataclass.from_text(rdclass)
  51. self.zone_rdclass = rdclass
  52. self.find_rrset(self.question, self.origin, rdclass, dns.rdatatype.SOA,
  53. create=True, force_unique=True)
  54. if keyring is not None:
  55. self.use_tsig(keyring, keyname, algorithm=keyalgorithm)
  56. def _add_rr(self, name, ttl, rd, deleting=None, section=None):
  57. """Add a single RR to the update section."""
  58. if section is None:
  59. section = self.authority
  60. covers = rd.covers()
  61. rrset = self.find_rrset(section, name, self.zone_rdclass, rd.rdtype,
  62. covers, deleting, True, True)
  63. rrset.add(rd, ttl)
  64. def _add(self, replace, section, name, *args):
  65. """Add records.
  66. *replace* is the replacement mode. If ``False``,
  67. RRs are added to an existing RRset; if ``True``, the RRset
  68. is replaced with the specified contents. The second
  69. argument is the section to add to. The third argument
  70. is always a name. The other arguments can be:
  71. - rdataset...
  72. - ttl, rdata...
  73. - ttl, rdtype, string...
  74. """
  75. if isinstance(name, string_types):
  76. name = dns.name.from_text(name, None)
  77. if isinstance(args[0], dns.rdataset.Rdataset):
  78. for rds in args:
  79. if replace:
  80. self.delete(name, rds.rdtype)
  81. for rd in rds:
  82. self._add_rr(name, rds.ttl, rd, section=section)
  83. else:
  84. args = list(args)
  85. ttl = int(args.pop(0))
  86. if isinstance(args[0], dns.rdata.Rdata):
  87. if replace:
  88. self.delete(name, args[0].rdtype)
  89. for rd in args:
  90. self._add_rr(name, ttl, rd, section=section)
  91. else:
  92. rdtype = args.pop(0)
  93. if isinstance(rdtype, string_types):
  94. rdtype = dns.rdatatype.from_text(rdtype)
  95. if replace:
  96. self.delete(name, rdtype)
  97. for s in args:
  98. rd = dns.rdata.from_text(self.zone_rdclass, rdtype, s,
  99. self.origin)
  100. self._add_rr(name, ttl, rd, section=section)
  101. def add(self, name, *args):
  102. """Add records.
  103. The first argument is always a name. The other
  104. arguments can be:
  105. - rdataset...
  106. - ttl, rdata...
  107. - ttl, rdtype, string...
  108. """
  109. self._add(False, self.authority, name, *args)
  110. def delete(self, name, *args):
  111. """Delete records.
  112. The first argument is always a name. The other
  113. arguments can be:
  114. - *empty*
  115. - rdataset...
  116. - rdata...
  117. - rdtype, [string...]
  118. """
  119. if isinstance(name, string_types):
  120. name = dns.name.from_text(name, None)
  121. if len(args) == 0:
  122. self.find_rrset(self.authority, name, dns.rdataclass.ANY,
  123. dns.rdatatype.ANY, dns.rdatatype.NONE,
  124. dns.rdatatype.ANY, True, True)
  125. elif isinstance(args[0], dns.rdataset.Rdataset):
  126. for rds in args:
  127. for rd in rds:
  128. self._add_rr(name, 0, rd, dns.rdataclass.NONE)
  129. else:
  130. args = list(args)
  131. if isinstance(args[0], dns.rdata.Rdata):
  132. for rd in args:
  133. self._add_rr(name, 0, rd, dns.rdataclass.NONE)
  134. else:
  135. rdtype = args.pop(0)
  136. if isinstance(rdtype, string_types):
  137. rdtype = dns.rdatatype.from_text(rdtype)
  138. if len(args) == 0:
  139. self.find_rrset(self.authority, name,
  140. self.zone_rdclass, rdtype,
  141. dns.rdatatype.NONE,
  142. dns.rdataclass.ANY,
  143. True, True)
  144. else:
  145. for s in args:
  146. rd = dns.rdata.from_text(self.zone_rdclass, rdtype, s,
  147. self.origin)
  148. self._add_rr(name, 0, rd, dns.rdataclass.NONE)
  149. def replace(self, name, *args):
  150. """Replace records.
  151. The first argument is always a name. The other
  152. arguments can be:
  153. - rdataset...
  154. - ttl, rdata...
  155. - ttl, rdtype, string...
  156. Note that if you want to replace the entire node, you should do
  157. a delete of the name followed by one or more calls to add.
  158. """
  159. self._add(True, self.authority, name, *args)
  160. def present(self, name, *args):
  161. """Require that an owner name (and optionally an rdata type,
  162. or specific rdataset) exists as a prerequisite to the
  163. execution of the update.
  164. The first argument is always a name.
  165. The other arguments can be:
  166. - rdataset...
  167. - rdata...
  168. - rdtype, string...
  169. """
  170. if isinstance(name, string_types):
  171. name = dns.name.from_text(name, None)
  172. if len(args) == 0:
  173. self.find_rrset(self.answer, name,
  174. dns.rdataclass.ANY, dns.rdatatype.ANY,
  175. dns.rdatatype.NONE, None,
  176. True, True)
  177. elif isinstance(args[0], dns.rdataset.Rdataset) or \
  178. isinstance(args[0], dns.rdata.Rdata) or \
  179. len(args) > 1:
  180. if not isinstance(args[0], dns.rdataset.Rdataset):
  181. # Add a 0 TTL
  182. args = list(args)
  183. args.insert(0, 0)
  184. self._add(False, self.answer, name, *args)
  185. else:
  186. rdtype = args[0]
  187. if isinstance(rdtype, string_types):
  188. rdtype = dns.rdatatype.from_text(rdtype)
  189. self.find_rrset(self.answer, name,
  190. dns.rdataclass.ANY, rdtype,
  191. dns.rdatatype.NONE, None,
  192. True, True)
  193. def absent(self, name, rdtype=None):
  194. """Require that an owner name (and optionally an rdata type) does
  195. not exist as a prerequisite to the execution of the update."""
  196. if isinstance(name, string_types):
  197. name = dns.name.from_text(name, None)
  198. if rdtype is None:
  199. self.find_rrset(self.answer, name,
  200. dns.rdataclass.NONE, dns.rdatatype.ANY,
  201. dns.rdatatype.NONE, None,
  202. True, True)
  203. else:
  204. if isinstance(rdtype, string_types):
  205. rdtype = dns.rdatatype.from_text(rdtype)
  206. self.find_rrset(self.answer, name,
  207. dns.rdataclass.NONE, rdtype,
  208. dns.rdatatype.NONE, None,
  209. True, True)
  210. def to_wire(self, origin=None, max_size=65535):
  211. """Return a string containing the update in DNS compressed wire
  212. format.
  213. *origin*, a ``dns.name.Name`` or ``None``, the origin to be
  214. appended to any relative names. If *origin* is ``None``, then
  215. the origin of the ``dns.update.Update`` message object is used
  216. (i.e. the *zone* parameter passed when the Update object was
  217. created).
  218. *max_size*, an ``int``, the maximum size of the wire format
  219. output; default is 0, which means "the message's request
  220. payload, if nonzero, or 65535".
  221. Returns a ``binary``.
  222. """
  223. if origin is None:
  224. origin = self.origin
  225. return super(Update, self).to_wire(origin, max_size)