source: trunk/packages/invirt-dns/invirt-dns @ 2971

Last change on this file since 2971 was 2209, checked in by price, 15 years ago

pull out dns.IN, comment the structure of the DNS logic

  • Property svn:executable set to *
File size: 8.5 KB
Line 
1#!/usr/bin/python
2from twisted.internet import reactor
3from twisted.names import server
4from twisted.names import dns
5from twisted.names import common
6from twisted.names import authority
7from twisted.internet import defer
8from twisted.python import failure
9
10from invirt.common import InvirtConfigError
11from invirt.config import structs as config
12import invirt.database
13import psycopg2
14import sqlalchemy
15import time
16import re
17
18class DatabaseAuthority(common.ResolverBase):
19    """An Authority that is loaded from a file."""
20
21    soa = None
22
23    def __init__(self, domains=None, database=None):
24        common.ResolverBase.__init__(self)
25        if database is not None:
26            invirt.database.connect(database)
27        else:
28            invirt.database.connect()
29        if domains is not None:
30            self.domains = domains
31        else:
32            self.domains = config.dns.domains
33        ns = config.dns.nameservers[0]
34        self.soa = dns.Record_SOA(mname=ns.hostname,
35                                  rname=config.dns.contact.replace('@','.',1),
36                                  serial=1, refresh=3600, retry=900,
37                                  expire=3600000, minimum=21600, ttl=3600)
38        self.ns = dns.Record_NS(name=ns.hostname, ttl=3600)
39        record = dns.Record_A(address=ns.ip, ttl=3600)
40        self.ns1 = dns.RRHeader(ns.hostname, dns.A, dns.IN,
41                                3600, record, auth=True)
42
43   
44    def _lookup(self, name, cls, type, timeout = None):
45        for i in range(3):
46            try:
47                value = self._lookup_unsafe(name, cls, type, timeout = None)
48            except (psycopg2.OperationalError, sqlalchemy.exceptions.SQLError):
49                if i == 2:
50                    raise
51                print "Reloading database"
52                time.sleep(0.5)
53                continue
54            else:
55                return value
56
57    def _lookup_unsafe(self, name, cls, type, timeout):
58        invirt.database.clear_cache()
59       
60        ttl = 900
61        name = name.lower()
62
63        if name in self.domains:
64            domain = name
65        else:
66            # Look for the longest-matching domain.
67            best_domain = ''
68            for domain in self.domains:
69                if name.endswith('.'+domain) and len(domain) > len(best_domain):
70                    best_domain = domain
71            if best_domain == '':
72                if name.endswith('.in-addr.arpa'):
73                    # Act authoritative for the IP address for reverse resolution requests
74                    best_domain = name
75                else:
76                    return defer.fail(failure.Failure(dns.DomainError(name)))
77            domain = best_domain
78        results = []
79        authority = []
80        additional = [self.ns1]
81        authority.append(dns.RRHeader(domain, dns.NS, dns.IN,
82                                      3600, self.ns, auth=True))
83
84        # The order of logic:
85        # - What class?
86        # - What domain: in-addr.arpa, domain root, or subdomain?
87        # - What query type: A, PTR, NS, ...?
88
89        if cls != dns.IN:
90            # Hahaha.  No.
91            return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name)))
92
93        if name.endswith(".in-addr.arpa"):
94            if type in (dns.PTR, dns.ALL_RECORDS):
95                ip = '.'.join(reversed(name.split('.')[:-2]))
96                value = invirt.database.NIC.query.filter_by(ip=ip).first()
97                if value and value.hostname:
98                    hostname = value.hostname
99                    if '.' not in hostname:
100                        hostname = hostname + "." + config.dns.domains[0]
101                    record = dns.Record_PTR(hostname, ttl)
102                    results.append(dns.RRHeader(name, dns.PTR, dns.IN,
103                                                ttl, record, auth=True))
104                else: # IP address doesn't point to an active host
105                    return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name)))
106            elif type == dns.SOA:
107                results.append(dns.RRHeader(domain, dns.SOA, dns.IN,
108                                            ttl, self.soa, auth=True))
109            # FIXME: Should only return success with no records if the name actually exists
110
111        elif name == domain or name == '.'+domain:
112            if type in (dns.A, dns.ALL_RECORDS):
113                record = dns.Record_A(config.dns.nameservers[0].ip, ttl)
114                results.append(dns.RRHeader(name, dns.A, dns.IN,
115                                            ttl, record, auth=True))
116            elif type == dns.NS:
117                results.append(dns.RRHeader(domain, dns.NS, dns.IN,
118                                            ttl, self.ns, auth=True))
119                authority = []
120            elif type == dns.SOA:
121                results.append(dns.RRHeader(domain, dns.SOA, dns.IN,
122                                            ttl, self.soa, auth=True))
123
124        else:
125            host = name[:-len(domain)-1]
126            value = invirt.database.NIC.query.filter_by(hostname=host).first()
127            if value:
128                ip = value.ip
129            else:
130                value = invirt.database.Machine.query().filter_by(name=host).first()
131                if value:
132                    ip = value.nics[0].ip
133                else:
134                    return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name)))
135            if ip is None:
136                return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name)))
137            if type in (dns.A, dns.ALL_RECORDS):
138                record = dns.Record_A(ip, ttl)
139                results.append(dns.RRHeader(name, dns.A, dns.IN,
140                                            ttl, record, auth=True))
141            elif type == dns.SOA:
142                results.append(dns.RRHeader(domain, dns.SOA, dns.IN,
143                                            ttl, self.soa, auth=True))
144
145        if len(results) == 0:
146            authority = []
147            additional = []
148        return defer.succeed((results, authority, additional))
149
150class QuotingBindAuthority(authority.BindAuthority):
151    """
152    A BindAuthority that (almost) deals with quoting correctly
153   
154    This will catch double quotes as marking the start or end of a
155    quoted phrase, unless the double quote is escaped by a backslash
156    """
157    # Match either a quoted or unquoted string literal followed by
158    # whitespace or the end of line.  This yields two groups, one of
159    # which has a match, and the other of which is None, depending on
160    # whether the string literal was quoted or unquoted; this is what
161    # necessitates the subsequent filtering out of groups that are
162    # None.
163    string_pat = \
164            re.compile(r'"((?:[^"\\]|\\.)*)"|((?:[^\\\s]|\\.)+)(?:\s+|\s*$)')
165
166    # For interpreting escapes.
167    escape_pat = re.compile(r'\\(.)')
168
169    def collapseContinuations(self, lines):
170        L = []
171        state = 0
172        for line in lines:
173            if state == 0:
174                if line.find('(') == -1:
175                    L.append(line)
176                else:
177                    L.append(line[:line.find('(')])
178                    state = 1
179            else:
180                if line.find(')') != -1:
181                    L[-1] += ' ' + line[:line.find(')')]
182                    state = 0
183                else:
184                    L[-1] += ' ' + line
185        lines = L
186        L = []
187
188        for line in lines:
189            in_quote = False
190            split_line = []
191            for m in self.string_pat.finditer(line):
192                [x] = [x for x in m.groups() if x is not None]
193                split_line.append(self.escape_pat.sub(r'\1', x))
194            L.append(split_line)
195        return filter(None, L)
196
197if '__main__' == __name__:
198    resolvers = []
199    try:
200        for zone in config.dns.zone_files:
201            for origin in config.dns.domains:
202                r = QuotingBindAuthority(zone)
203                # This sucks, but if I want a generic zone file, I have to
204                # reload the information by hand
205                r.origin = origin
206                lines = open(zone).readlines()
207                lines = r.collapseContinuations(r.stripComments(lines))
208                r.parseLines(lines)
209               
210                resolvers.append(r)
211    except InvirtConfigError:
212        # Don't care if zone_files isn't defined
213        pass
214    resolvers.append(DatabaseAuthority())
215
216    verbosity = 0
217    f = server.DNSServerFactory(authorities=resolvers, verbose=verbosity)
218    p = dns.DNSDatagramProtocol(f)
219    f.noisy = p.noisy = verbosity
220   
221    reactor.listenUDP(53, p)
222    reactor.listenTCP(53, f)
223    reactor.run()
Note: See TracBrowser for help on using the repository browser.