Changeset 2208


Ignore:
Timestamp:
Feb 27, 2009, 4:38:26 AM (15 years ago)
Author:
price
Message:

refactor DNS logic; fix some bugs in reverse-resolution

Namely, stop returning answers to A and NS queries on *.in-addr.arpa.

I found these bugs because I was simplifying the logic, after the
author had said it was correct and he and another developer had
tested it. This is why it matters to get the code clear.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/invirt-dns/invirt-dns

    r2207 r2208  
    8383
    8484        if cls == dns.IN:
    85             host = name[:-len(domain)-1]
    86             if not host and type != dns.PTR: # Request for the domain itself.
     85            if name.endswith(".in-addr.arpa"):
     86                if type in (dns.PTR, dns.ALL_RECORDS):
     87                    ip = '.'.join(reversed(name.split('.')[:-2]))
     88                    value = invirt.database.NIC.query.filter_by(ip=ip).first()
     89                    if value and value.hostname:
     90                        hostname = value.hostname
     91                        if '.' not in hostname:
     92                            hostname = hostname + "." + config.dns.domains[0]
     93                        record = dns.Record_PTR(hostname, ttl)
     94                        results.append(dns.RRHeader(name, dns.PTR, dns.IN,
     95                                                    ttl, record, auth=True))
     96                    else: # IP address doesn't point to an active host
     97                        return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name)))
     98                elif type == dns.SOA:
     99                    results.append(dns.RRHeader(domain, dns.SOA, dns.IN,
     100                                                ttl, self.soa, auth=True))
     101                # FIXME: Should only return success with no records if the name actually exists
     102            elif name == domain or name == '.'+domain:
    87103                if type in (dns.A, dns.ALL_RECORDS):
    88104                    record = dns.Record_A(config.dns.nameservers[0].ip, ttl)
    89                     results.append(dns.RRHeader(name, dns.A, dns.IN, 
     105                    results.append(dns.RRHeader(name, dns.A, dns.IN,
    90106                                                ttl, record, auth=True))
    91107                elif type == dns.NS:
     
    96112                    results.append(dns.RRHeader(domain, dns.SOA, dns.IN,
    97113                                                ttl, self.soa, auth=True))
    98             else: # Request for a subdomain.
    99                 if name.endswith(".in-addr.arpa"): # Reverse resolution here
    100                     if type in (dns.PTR, dns.ALL_RECORDS):
    101                         ip = '.'.join(reversed(name.split('.')[:-2]))
    102                         value = invirt.database.NIC.query.filter_by(ip=ip).first()
    103                         if value and value.hostname:
    104                             hostname = value.hostname
    105                             if '.' not in hostname:
    106                                 hostname = hostname + "." + config.dns.domains[0]
    107                             record = dns.Record_PTR(hostname, ttl)
    108                             results.append(dns.RRHeader(name, dns.PTR, dns.IN,
    109                                                         ttl, record, auth=True))
    110                         else: # IP address doesn't point to an active host
    111                             return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name)))
    112                     # FIXME: Should only return success with no records if the name actually exists
    113                 else: # Forward resolution here
    114                     value = invirt.database.NIC.query.filter_by(hostname=host).first()
     114            else:
     115                host = name[:-len(domain)-1]
     116                value = invirt.database.NIC.query.filter_by(hostname=host).first()
     117                if value:
     118                    ip = value.ip
     119                else:
     120                    value = invirt.database.Machine.query().filter_by(name=host).first()
    115121                    if value:
    116                         ip = value.ip
     122                        ip = value.nics[0].ip
    117123                    else:
    118                         value = invirt.database.Machine.query().filter_by(name=host).first()
    119                         if value:
    120                             ip = value.nics[0].ip
    121                         else:
    122                             return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name)))
    123                
    124                     if ip is None:
    125124                        return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name)))
    126125
    127                     if type in (dns.A, dns.ALL_RECORDS):
    128                         record = dns.Record_A(ip, ttl)
    129                         results.append(dns.RRHeader(name, dns.A, dns.IN,
    130                                                     ttl, record, auth=True))
    131                     elif type == dns.SOA:
    132                         results.append(dns.RRHeader(domain, dns.SOA, dns.IN,
    133                                                     ttl, self.soa, auth=True))
     126                if ip is None:
     127                    return defer.fail(failure.Failure(dns.AuthoritativeDomainError(name)))
     128
     129                if type in (dns.A, dns.ALL_RECORDS):
     130                    record = dns.Record_A(ip, ttl)
     131                    results.append(dns.RRHeader(name, dns.A, dns.IN,
     132                                                ttl, record, auth=True))
     133                elif type == dns.SOA:
     134                    results.append(dns.RRHeader(domain, dns.SOA, dns.IN,
     135                                                ttl, self.soa, auth=True))
    134136            if len(results) == 0:
    135137                authority = []
Note: See TracChangeset for help on using the changeset viewer.