[181] | 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 |
---|
[302] | 10 | import psycopg2 |
---|
| 11 | import sqlalchemy |
---|
| 12 | import time |
---|
[181] | 13 | |
---|
| 14 | class DatabaseAuthority(common.ResolverBase): |
---|
| 15 | """An Authority that is loaded from a file.""" |
---|
| 16 | |
---|
| 17 | soa = None |
---|
| 18 | |
---|
[505] | 19 | def __init__(self, domains, database=None): |
---|
[181] | 20 | common.ResolverBase.__init__(self) |
---|
| 21 | if database is not None: |
---|
| 22 | sipb_xen_database.connect(database) |
---|
[505] | 23 | self.domains = domains |
---|
[181] | 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) |
---|
[582] | 28 | self.ns = dns.Record_NS(name='ns1.xvm.mit.edu', ttl=3600) |
---|
| 29 | |
---|
[181] | 30 | def _lookup(self, name, cls, type, timeout = None): |
---|
[302] | 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): |
---|
[300] | 44 | sipb_xen_database.clear_cache() |
---|
[582] | 45 | |
---|
| 46 | ttl = 900 |
---|
[505] | 47 | if name.lower() in self.domains: |
---|
| 48 | domain = name.lower() |
---|
| 49 | else: |
---|
[582] | 50 | # This works because domain will remain bound after breaking out of the loop |
---|
[505] | 51 | for domain in self.domains: |
---|
| 52 | if name.lower().endswith('.'+domain): |
---|
| 53 | break |
---|
[508] | 54 | else: #Not us |
---|
[505] | 55 | return defer.fail(failure.Failure(dns.DomainError(name))) |
---|
[181] | 56 | results = [] |
---|
| 57 | authority = [] |
---|
[582] | 58 | additional = [] |
---|
[541] | 59 | authority.append(dns.RRHeader(domain, dns.NS, dns.IN, |
---|
[582] | 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_CNAME('sipb-xen-dev.mit.edu', ttl) |
---|
| 66 | results.append(dns.RRHeader(name, dns.CNAME, 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))) |
---|
[181] | 92 | |
---|
| 93 | if '__main__' == __name__: |
---|
[505] | 94 | resolver = DatabaseAuthority(['servers.csail.mit.edu', |
---|
| 95 | 'xvm.mit.edu'], |
---|
[181] | 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() |
---|