update.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # Copyright (C) 2003-2007, 2009-2011 Nominum, Inc.
  2. #
  3. # Permission to use, copy, modify, and distribute this software and its
  4. # documentation for any purpose with or without fee is hereby granted,
  5. # provided that the above copyright notice and this permission notice
  6. # appear in all copies.
  7. #
  8. # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
  9. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
  11. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  14. # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. """DNS Dynamic Update Support"""
  16. import dns.message
  17. import dns.name
  18. import dns.opcode
  19. import dns.rdata
  20. import dns.rdataclass
  21. import dns.rdataset
  22. import dns.tsig
  23. from ._compat import string_types
  24. class Update(dns.message.Message):
  25. def __init__(self, zone, rdclass=dns.rdataclass.IN, keyring=None,
  26. keyname=None, keyalgorithm=dns.tsig.default_algorithm):
  27. """Initialize a new DNS Update object.
  28. @param zone: The zone which is being updated.
  29. @type zone: A dns.name.Name or string
  30. @param rdclass: The class of the zone; defaults to dns.rdataclass.IN.
  31. @type rdclass: An int designating the class, or a string whose value
  32. is the name of a class.
  33. @param keyring: The TSIG keyring to use; defaults to None.
  34. @type keyring: dict
  35. @param keyname: The name of the TSIG key to use; defaults to None.
  36. The key must be defined in the keyring. If a keyring is specified
  37. but a keyname is not, then the key used will be the first key in the
  38. keyring. Note that the order of keys in a dictionary is not defined,
  39. so applications should supply a keyname when a keyring is used, unless
  40. they know the keyring contains only one key.
  41. @type keyname: dns.name.Name or string
  42. @param keyalgorithm: The TSIG algorithm to use; defaults to
  43. dns.tsig.default_algorithm. Constants for TSIG algorithms are defined
  44. in dns.tsig, and the currently implemented algorithms are
  45. HMAC_MD5, HMAC_SHA1, HMAC_SHA224, HMAC_SHA256, HMAC_SHA384, and
  46. HMAC_SHA512.
  47. @type keyalgorithm: string
  48. """
  49. super(Update, self).__init__()
  50. self.flags |= dns.opcode.to_flags(dns.opcode.UPDATE)
  51. if isinstance(zone, string_types):
  52. zone = dns.name.from_text(zone)
  53. self.origin = zone
  54. if isinstance(rdclass, string_types):
  55. rdclass = dns.rdataclass.from_text(rdclass)
  56. self.zone_rdclass = rdclass
  57. self.find_rrset(self.question, 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. def _add_rr(self, name, ttl, rd, deleting=None, section=None):
  62. """Add a single RR to the update section."""
  63. if section is None:
  64. section = self.authority
  65. covers = rd.covers()
  66. rrset = self.find_rrset(section, name, self.zone_rdclass, rd.rdtype,
  67. covers, deleting, True, True)
  68. rrset.add(rd, ttl)
  69. def _add(self, replace, section, name, *args):
  70. """Add records. The first argument is the replace mode. If
  71. false, RRs are added to an existing RRset; if true, the RRset
  72. is replaced with the specified contents. The second
  73. argument is the section to add to. The third argument
  74. is always a name. The other arguments can be:
  75. - rdataset...
  76. - ttl, rdata...
  77. - ttl, rdtype, string..."""
  78. if isinstance(name, string_types):
  79. name = dns.name.from_text(name, None)
  80. if isinstance(args[0], dns.rdataset.Rdataset):
  81. for rds in args:
  82. if replace:
  83. self.delete(name, rds.rdtype)
  84. for rd in rds:
  85. self._add_rr(name, rds.ttl, rd, section=section)
  86. else:
  87. args = list(args)
  88. ttl = int(args.pop(0))
  89. if isinstance(args[0], dns.rdata.Rdata):
  90. if replace:
  91. self.delete(name, args[0].rdtype)
  92. for rd in args:
  93. self._add_rr(name, ttl, rd, section=section)
  94. else:
  95. rdtype = args.pop(0)
  96. if isinstance(rdtype, string_types):
  97. rdtype = dns.rdatatype.from_text(rdtype)
  98. if replace:
  99. self.delete(name, rdtype)
  100. for s in args:
  101. rd = dns.rdata.from_text(self.zone_rdclass, rdtype, s,
  102. self.origin)
  103. self._add_rr(name, ttl, rd, section=section)
  104. def add(self, name, *args):
  105. """Add records. The first argument is always a name. The other
  106. arguments can be:
  107. - rdataset...
  108. - ttl, rdata...
  109. - ttl, rdtype, string..."""
  110. self._add(False, self.authority, name, *args)
  111. def delete(self, name, *args):
  112. """Delete records. The first argument is always a name. The other
  113. arguments can be:
  114. - I{nothing}
  115. - rdataset...
  116. - rdata...
  117. - rdtype, [string...]"""
  118. if isinstance(name, string_types):
  119. name = dns.name.from_text(name, None)
  120. if len(args) == 0:
  121. self.find_rrset(self.authority, name, dns.rdataclass.ANY,
  122. dns.rdatatype.ANY, dns.rdatatype.NONE,
  123. dns.rdatatype.ANY, True, True)
  124. elif isinstance(args[0], dns.rdataset.Rdataset):
  125. for rds in args:
  126. for rd in rds:
  127. self._add_rr(name, 0, rd, dns.rdataclass.NONE)
  128. else:
  129. args = list(args)
  130. if isinstance(args[0], dns.rdata.Rdata):
  131. for rd in args:
  132. self._add_rr(name, 0, rd, dns.rdataclass.NONE)
  133. else:
  134. rdtype = args.pop(0)
  135. if isinstance(rdtype, string_types):
  136. rdtype = dns.rdatatype.from_text(rdtype)
  137. if len(args) == 0:
  138. self.find_rrset(self.authority, name,
  139. self.zone_rdclass, rdtype,
  140. dns.rdatatype.NONE,
  141. dns.rdataclass.ANY,
  142. True, True)
  143. else:
  144. for s in args:
  145. rd = dns.rdata.from_text(self.zone_rdclass, rdtype, s,
  146. self.origin)
  147. self._add_rr(name, 0, rd, dns.rdataclass.NONE)
  148. def replace(self, name, *args):
  149. """Replace records. The first argument is always a name. The other
  150. arguments can be:
  151. - rdataset...
  152. - ttl, rdata...
  153. - ttl, rdtype, string...
  154. Note that if you want to replace the entire node, you should do
  155. a delete of the name followed by one or more calls to add."""
  156. self._add(True, self.authority, name, *args)
  157. def present(self, name, *args):
  158. """Require that an owner name (and optionally an rdata type,
  159. or specific rdataset) exists as a prerequisite to the
  160. execution of the update. The first argument is always a name.
  161. The other arguments can be:
  162. - rdataset...
  163. - rdata...
  164. - rdtype, string..."""
  165. if isinstance(name, string_types):
  166. name = dns.name.from_text(name, None)
  167. if len(args) == 0:
  168. self.find_rrset(self.answer, name,
  169. dns.rdataclass.ANY, dns.rdatatype.ANY,
  170. dns.rdatatype.NONE, None,
  171. True, True)
  172. elif isinstance(args[0], dns.rdataset.Rdataset) or \
  173. isinstance(args[0], dns.rdata.Rdata) or \
  174. len(args) > 1:
  175. if not isinstance(args[0], dns.rdataset.Rdataset):
  176. # Add a 0 TTL
  177. args = list(args)
  178. args.insert(0, 0)
  179. self._add(False, self.answer, name, *args)
  180. else:
  181. rdtype = args[0]
  182. if isinstance(rdtype, string_types):
  183. rdtype = dns.rdatatype.from_text(rdtype)
  184. self.find_rrset(self.answer, name,
  185. dns.rdataclass.ANY, rdtype,
  186. dns.rdatatype.NONE, None,
  187. True, True)
  188. def absent(self, name, rdtype=None):
  189. """Require that an owner name (and optionally an rdata type) does
  190. not exist as a prerequisite to the execution of the update."""
  191. if isinstance(name, string_types):
  192. name = dns.name.from_text(name, None)
  193. if rdtype is None:
  194. self.find_rrset(self.answer, name,
  195. dns.rdataclass.NONE, dns.rdatatype.ANY,
  196. dns.rdatatype.NONE, None,
  197. True, True)
  198. else:
  199. if isinstance(rdtype, string_types):
  200. rdtype = dns.rdatatype.from_text(rdtype)
  201. self.find_rrset(self.answer, name,
  202. dns.rdataclass.NONE, rdtype,
  203. dns.rdatatype.NONE, None,
  204. True, True)
  205. def to_wire(self, origin=None, max_size=65535):
  206. """Return a string containing the update in DNS compressed wire
  207. format.
  208. @rtype: string"""
  209. if origin is None:
  210. origin = self.origin
  211. return super(Update, self).to_wire(origin, max_size)