aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Twisted/py3/twisted/names/secondary.py
blob: 0b9e184b0264bb985d2f10150c3bb275ea89f6a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# -*- test-case-name: twisted.names.test.test_names -*-
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.


__all__ = ["SecondaryAuthority", "SecondaryAuthorityService"]

from twisted.application import service
from twisted.internet import defer, task
from twisted.names import client, common, dns, resolve
from twisted.names.authority import FileAuthority
from twisted.python import failure, log
from twisted.python.compat import nativeString


class SecondaryAuthorityService(service.Service):
    """
    A service that keeps one or more authorities up to date by doing hourly
    zone transfers from a master.

    @ivar primary: IP address of the master.
    @type primary: L{str}

    @ivar domains: An authority for each domain mirrored from the master.
    @type domains: L{list} of L{SecondaryAuthority}
    """

    calls = None

    _port = 53

    def __init__(self, primary, domains):
        """
        @param primary: The IP address of the server from which to perform
        zone transfers.
        @type primary: L{str}

        @param domains: A sequence of domain names for which to perform
        zone transfers.
        @type domains: L{list} of L{bytes}
        """
        self.primary = nativeString(primary)
        self.domains = [SecondaryAuthority(primary, d) for d in domains]

    @classmethod
    def fromServerAddressAndDomains(cls, serverAddress, domains):
        """
        Construct a new L{SecondaryAuthorityService} from a tuple giving a
        server address and a C{str} giving the name of a domain for which this
        is an authority.

        @param serverAddress: A two-tuple, the first element of which is a
            C{str} giving an IP address and the second element of which is a
            C{int} giving a port number.  Together, these define where zone
            transfers will be attempted from.

        @param domains: Domain names for which to perform zone transfers.
        @type domains: sequence of L{bytes}

        @return: A new instance of L{SecondaryAuthorityService}.
        """
        primary, port = serverAddress
        service = cls(primary, [])
        service._port = port
        service.domains = [
            SecondaryAuthority.fromServerAddressAndDomain(serverAddress, d)
            for d in domains
        ]
        return service

    def getAuthority(self):
        """
        Get a resolver for the transferred domains.

        @rtype: L{ResolverChain}
        """
        return resolve.ResolverChain(self.domains)

    def startService(self):
        service.Service.startService(self)
        self.calls = [task.LoopingCall(d.transfer) for d in self.domains]
        i = 0
        from twisted.internet import reactor

        for c in self.calls:
            # XXX Add errbacks, respect proper timeouts
            reactor.callLater(i, c.start, 60 * 60)
            i += 1

    def stopService(self):
        service.Service.stopService(self)
        for c in self.calls:
            c.stop()


class SecondaryAuthority(FileAuthority):
    """
    An Authority that keeps itself updated by performing zone transfers.

    @ivar primary: The IP address of the server from which zone transfers will
    be attempted.
    @type primary: L{str}

    @ivar _port: The port number of the server from which zone transfers will
    be attempted.
    @type _port: L{int}

    @ivar domain: The domain for which this is the secondary authority.
    @type domain: L{bytes}

    @ivar _reactor: The reactor to use to perform the zone transfers, or
    L{None} to use the global reactor.
    """

    transferring = False
    soa = records = None
    _port = 53
    _reactor = None

    def __init__(self, primaryIP, domain):
        """
        @param domain: The domain for which this will be the secondary
            authority.
        @type domain: L{bytes} or L{str}
        """
        # Yep.  Skip over FileAuthority.__init__.  This is a hack until we have
        # a good composition-based API for the complicated DNS record lookup
        # logic we want to share.
        common.ResolverBase.__init__(self)
        self.primary = nativeString(primaryIP)
        self.domain = dns.domainString(domain)

    @classmethod
    def fromServerAddressAndDomain(cls, serverAddress, domain):
        """
        Construct a new L{SecondaryAuthority} from a tuple giving a server
        address and a C{bytes} giving the name of a domain for which this is an
        authority.

        @param serverAddress: A two-tuple, the first element of which is a
            C{str} giving an IP address and the second element of which is a
            C{int} giving a port number.  Together, these define where zone
            transfers will be attempted from.

        @param domain: A C{bytes} giving the domain to transfer.
        @type domain: L{bytes}

        @return: A new instance of L{SecondaryAuthority}.
        """
        primary, port = serverAddress
        secondary = cls(primary, domain)
        secondary._port = port
        return secondary

    def transfer(self):
        """
        Attempt a zone transfer.

        @returns: A L{Deferred} that fires with L{None} when attempted zone
            transfer has completed.
        """
        # FIXME: This logic doesn't avoid duplicate transfers
        # https://twistedmatrix.com/trac/ticket/9754
        if self.transferring:  # <-- never true
            return
        self.transfering = True  # <-- speling

        reactor = self._reactor
        if reactor is None:
            from twisted.internet import reactor

        resolver = client.Resolver(
            servers=[(self.primary, self._port)], reactor=reactor
        )
        return (
            resolver.lookupZone(self.domain)
            .addCallback(self._cbZone)
            .addErrback(self._ebZone)
        )

    def _lookup(self, name, cls, type, timeout=None):
        if not self.soa or not self.records:
            # No transfer has occurred yet. Fail non-authoritatively so that
            # the caller can try elsewhere.
            return defer.fail(failure.Failure(dns.DomainError(name)))
        return FileAuthority._lookup(self, name, cls, type, timeout)

    def _cbZone(self, zone):
        ans, _, _ = zone
        self.records = r = {}
        for rec in ans:
            if not self.soa and rec.type == dns.SOA:
                self.soa = (rec.name.name.lower(), rec.payload)
            else:
                r.setdefault(rec.name.name.lower(), []).append(rec.payload)

    def _ebZone(self, failure):
        log.msg(
            "Updating %s from %s failed during zone transfer"
            % (self.domain, self.primary)
        )
        log.err(failure)

    def update(self):
        self.transfer().addCallbacks(self._cbTransferred, self._ebTransferred)

    def _cbTransferred(self, result):
        self.transferring = False

    def _ebTransferred(self, failure):
        self.transferred = False
        log.msg(
            "Transferring %s from %s failed after zone transfer"
            % (self.domain, self.primary)
        )
        log.err(failure)