secondary.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # -*- test-case-name: twisted.names.test.test_names -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. from __future__ import absolute_import, division
  5. __all__ = ['SecondaryAuthority', 'SecondaryAuthorityService']
  6. from twisted.internet import task, defer
  7. from twisted.names import dns
  8. from twisted.names import common
  9. from twisted.names import client
  10. from twisted.names import resolve
  11. from twisted.names.authority import FileAuthority
  12. from twisted.python import log, failure
  13. from twisted.python.compat import nativeString
  14. from twisted.application import service
  15. class SecondaryAuthorityService(service.Service):
  16. """
  17. A service that keeps one or more authorities up to date by doing hourly
  18. zone transfers from a master.
  19. @ivar primary: IP address of the master.
  20. @type primary: L{str}
  21. @ivar domains: An authority for each domain mirrored from the master.
  22. @type domains: L{list} of L{SecondaryAuthority}
  23. """
  24. calls = None
  25. _port = 53
  26. def __init__(self, primary, domains):
  27. """
  28. @param primary: The IP address of the server from which to perform
  29. zone transfers.
  30. @type primary: L{str}
  31. @param domains: A sequence of domain names for which to perform
  32. zone transfers.
  33. @type domains: L{list} of L{bytes}
  34. """
  35. self.primary = nativeString(primary)
  36. self.domains = [SecondaryAuthority(primary, d) for d in domains]
  37. @classmethod
  38. def fromServerAddressAndDomains(cls, serverAddress, domains):
  39. """
  40. Construct a new L{SecondaryAuthorityService} from a tuple giving a
  41. server address and a C{str} giving the name of a domain for which this
  42. is an authority.
  43. @param serverAddress: A two-tuple, the first element of which is a
  44. C{str} giving an IP address and the second element of which is a
  45. C{int} giving a port number. Together, these define where zone
  46. transfers will be attempted from.
  47. @param domain: A C{bytes} giving the domain to transfer.
  48. @return: A new instance of L{SecondaryAuthorityService}.
  49. """
  50. primary, port = serverAddress
  51. service = cls(primary, [])
  52. service._port = port
  53. service.domains = [
  54. SecondaryAuthority.fromServerAddressAndDomain(serverAddress, d)
  55. for d in domains]
  56. return service
  57. def getAuthority(self):
  58. """
  59. Get a resolver for the transferred domains.
  60. @rtype: L{ResolverChain}
  61. """
  62. return resolve.ResolverChain(self.domains)
  63. def startService(self):
  64. service.Service.startService(self)
  65. self.calls = [task.LoopingCall(d.transfer) for d in self.domains]
  66. i = 0
  67. from twisted.internet import reactor
  68. for c in self.calls:
  69. # XXX Add errbacks, respect proper timeouts
  70. reactor.callLater(i, c.start, 60 * 60)
  71. i += 1
  72. def stopService(self):
  73. service.Service.stopService(self)
  74. for c in self.calls:
  75. c.stop()
  76. class SecondaryAuthority(FileAuthority):
  77. """
  78. An Authority that keeps itself updated by performing zone transfers.
  79. @ivar primary: The IP address of the server from which zone transfers will
  80. be attempted.
  81. @type primary: C{str}
  82. @ivar _port: The port number of the server from which zone transfers will
  83. be attempted.
  84. @type: C{int}
  85. @ivar domain: The domain for which this is the secondary authority.
  86. @type: C{bytes}
  87. @ivar _reactor: The reactor to use to perform the zone transfers, or
  88. L{None} to use the global reactor.
  89. """
  90. transferring = False
  91. soa = records = None
  92. _port = 53
  93. _reactor = None
  94. def __init__(self, primaryIP, domain):
  95. """
  96. @param domain: The domain for which this will be the secondary
  97. authority.
  98. @type domain: L{bytes} or L{str}
  99. """
  100. # Yep. Skip over FileAuthority.__init__. This is a hack until we have
  101. # a good composition-based API for the complicated DNS record lookup
  102. # logic we want to share.
  103. common.ResolverBase.__init__(self)
  104. self.primary = nativeString(primaryIP)
  105. self.domain = dns.domainString(domain)
  106. @classmethod
  107. def fromServerAddressAndDomain(cls, serverAddress, domain):
  108. """
  109. Construct a new L{SecondaryAuthority} from a tuple giving a server
  110. address and a C{bytes} giving the name of a domain for which this is an
  111. authority.
  112. @param serverAddress: A two-tuple, the first element of which is a
  113. C{str} giving an IP address and the second element of which is a
  114. C{int} giving a port number. Together, these define where zone
  115. transfers will be attempted from.
  116. @param domain: A C{bytes} giving the domain to transfer.
  117. @type domain: L{bytes}
  118. @return: A new instance of L{SecondaryAuthority}.
  119. """
  120. primary, port = serverAddress
  121. secondary = cls(primary, domain)
  122. secondary._port = port
  123. return secondary
  124. def transfer(self):
  125. """
  126. Attempt a zone transfer.
  127. @returns: A L{Deferred} that fires with L{None} when attempted zone
  128. transfer has completed.
  129. """
  130. # FIXME: This logic doesn't avoid duplicate transfers
  131. # https://twistedmatrix.com/trac/ticket/9754
  132. if self.transferring: # <-- never true
  133. return
  134. self.transfering = True # <-- speling
  135. reactor = self._reactor
  136. if reactor is None:
  137. from twisted.internet import reactor
  138. resolver = client.Resolver(
  139. servers=[(self.primary, self._port)], reactor=reactor)
  140. return resolver.lookupZone(self.domain
  141. ).addCallback(self._cbZone
  142. ).addErrback(self._ebZone
  143. )
  144. def _lookup(self, name, cls, type, timeout=None):
  145. if not self.soa or not self.records:
  146. # No transfer has occurred yet. Fail non-authoritatively so that
  147. # the caller can try elsewhere.
  148. return defer.fail(failure.Failure(dns.DomainError(name)))
  149. return FileAuthority._lookup(self, name, cls, type, timeout)
  150. def _cbZone(self, zone):
  151. ans, _, _ = zone
  152. self.records = r = {}
  153. for rec in ans:
  154. if not self.soa and rec.type == dns.SOA:
  155. self.soa = (rec.name.name.lower(), rec.payload)
  156. else:
  157. r.setdefault(rec.name.name.lower(), []).append(rec.payload)
  158. def _ebZone(self, failure):
  159. log.msg("Updating %s from %s failed during zone transfer" % (self.domain, self.primary))
  160. log.err(failure)
  161. def update(self):
  162. self.transfer().addCallbacks(self._cbTransferred, self._ebTransferred)
  163. def _cbTransferred(self, result):
  164. self.transferring = False
  165. def _ebTransferred(self, failure):
  166. self.transferred = False
  167. log.msg("Transferring %s from %s failed after zone transfer" % (self.domain, self.primary))
  168. log.err(failure)