1 | #!/usr/bin/python |
---|
2 | from twisted.internet import reactor |
---|
3 | from twisted.names import server |
---|
4 | from twisted.names import dns |
---|
5 | from twisted.names import common |
---|
6 | from twisted.internet import defer |
---|
7 | from twisted.python import failure |
---|
8 | |
---|
9 | import sipb_xen_database |
---|
10 | import psycopg2 |
---|
11 | import sqlalchemy |
---|
12 | import time |
---|
13 | |
---|
14 | class DatabaseAuthority(common.ResolverBase): |
---|
15 | """An Authority that is loaded from a file.""" |
---|
16 | |
---|
17 | soa = None |
---|
18 | |
---|
19 | def __init__(self, domains, database=None): |
---|
20 | common.ResolverBase.__init__(self) |
---|
21 | if database is not None: |
---|
22 | sipb_xen_database.connect(database) |
---|
23 | self.domains = domains |
---|
24 | self.soa = dns.Record_SOA(mname='sipb-xen-dev.mit.edu', |
---|
25 | rname='sipb-xen.mit.edu', |
---|
26 | serial=1, refresh=3600, retry=900, |
---|
27 | expire=3600000, minimum=21600, ttl=3600) |
---|
28 | self.ns = dns.Record_NS(name='ns1.xvm.mit.edu', ttl=3600) |
---|
29 | |
---|
30 | def _lookup(self, name, cls, type, timeout = None): |
---|
31 | for i in range(3): |
---|
32 | try: |
---|
33 | value = self._lookup_unsafe(name, cls, type, timeout = None) |
---|
34 | except (psycopg2.OperationalError, sqlalchemy.exceptions.SQLError): |
---|
35 | if i == 2: |
---|
36 | raise |
---|
37 | print "Reloading database" |
---|
38 | time.sleep(0.5) |
---|
39 | continue |
---|
40 | else: |
---|
41 | return value |
---|
42 | |
---|
43 | def _lookup_unsafe(self, name, cls, type, timeout): |
---|
44 | sipb_xen_database.clear_cache() |
---|
45 | |
---|
46 | ttl = 900 |
---|
47 | if name.lower() in self.domains: |
---|
48 | domain = name.lower() |
---|
49 | else: |
---|
50 | # This works because domain will remain bound after breaking out of the loop |
---|
51 | for domain in self.domains: |
---|
52 | if name.lower().endswith('.'+domain): |
---|
53 | break |
---|
54 | else: #Not us |
---|
55 | return defer.fail(failure.Failure(dns.DomainError(name))) |
---|
56 | results = [] |
---|
57 | authority = [] |
---|
58 | additional = [] |
---|
59 | authority.append(dns.RRHeader(domain, dns.NS, dns.IN, |
---|
60 | 3600, self.ns, auth=True)) |
---|
61 | if cls == dns.IN: |
---|
62 | if type in (dns.A, dns.ALL_RECORDS): |
---|
63 | host = name[:-len(domain)-1] |
---|
64 | if not host: |
---|
65 | record = dns.Record_A('18.181.0.62', ttl) |
---|
66 | results.append(dns.RRHeader(name, dns.A, dns.IN, |
---|
67 | ttl, record, auth=True)) |
---|
68 | else: |
---|
69 | value = sipb_xen_database.Machine.get_by(name=host) |
---|
70 | if value is None or not value.nics: |
---|
71 | return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name))) |
---|
72 | ip = value.nics[0].ip |
---|
73 | if ip is None: #Deactivated? |
---|
74 | return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name))) |
---|
75 | record = dns.Record_A(ip, ttl) |
---|
76 | results.append(dns.RRHeader(name, dns.A, dns.IN, |
---|
77 | ttl, record, auth=True)) |
---|
78 | elif type == dns.NS: |
---|
79 | results.append(dns.RRHeader(domain, dns.NS, dns.IN, |
---|
80 | ttl, self.ns, auth=True)) |
---|
81 | authority = [] |
---|
82 | record = dns.Record_A(address='18.181.0.62', ttl=ttl) |
---|
83 | additional.append(dns.RRHeader('ns1.xvm.mit.edu', dns.A, dns.IN, |
---|
84 | ttl, record, auth=True)) |
---|
85 | elif type == dns.SOA: |
---|
86 | results.append(dns.RRHeader(domain, dns.SOA, dns.IN, |
---|
87 | ttl, self.soa, auth=True)) |
---|
88 | return defer.succeed((results, authority, additional)) |
---|
89 | else: |
---|
90 | #Doesn't exist |
---|
91 | return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name))) |
---|
92 | |
---|
93 | if '__main__' == __name__: |
---|
94 | resolver = DatabaseAuthority(['servers.csail.mit.edu', |
---|
95 | 'xvm.mit.edu'], |
---|
96 | 'postgres://sipb-xen@sipb-xen-dev/sipb_xen') |
---|
97 | |
---|
98 | verbosity = 0 |
---|
99 | f = server.DNSServerFactory(authorities=[resolver], verbose=verbosity) |
---|
100 | p = dns.DNSDatagramProtocol(f) |
---|
101 | f.noisy = p.noisy = verbosity |
---|
102 | |
---|
103 | reactor.listenUDP(53, p) |
---|
104 | reactor.listenTCP(53, f) |
---|
105 | reactor.run() |
---|