query.cc 7.49 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Copyright (C) 2010  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

Michal Vaner's avatar
Michal Vaner committed
15
16
17
#include <vector>
#include <boost/foreach.hpp>

18
19
#include <dns/message.h>
#include <dns/rcode.h>
20
#include <dns/rdataclass.h>
21

22
#include <datasrc/memory_datasrc.h>
23
24
25
26
27

#include <auth/query.h>

using namespace isc::dns;
using namespace isc::datasrc;
28
using namespace isc::dns::rdata;
29
30
31

namespace isc {
namespace auth {
32

33
void
Jerry's avatar
Jerry committed
34
Query::getAdditional(const Zone& zone, const RRset& rrset) const {
chenzhengzhang's avatar
chenzhengzhang committed
35
36
37
38
39
40
41
42
43
44
    RdataIteratorPtr rdata_iterator(rrset.getRdataIterator());
    for (; !rdata_iterator->isLast(); rdata_iterator->next()) {
        const Rdata& rdata(rdata_iterator->getCurrent());
        if (rrset.getType() == RRType::NS()) {
            // Need to perform the search in the "GLUE OK" mode.
            const generic::NS& ns = dynamic_cast<const generic::NS&>(rdata);
            findAddrs(zone, ns.getNSName(), Zone::FIND_GLUE_OK);
        } else if (rrset.getType() == RRType::MX()) {
            const generic::MX& mx(dynamic_cast<const generic::MX&>(rdata));
            findAddrs(zone, mx.getMXName());
45
        }
46
47
48
49
    }
}

void
50
51
Query::findAddrs(const Zone& zone, const Name& qname,
                 const Zone::FindOptions options) const
52
{
53
54
55
56
57
58
    // Out of zone name
    NameComparisonResult result = zone.getOrigin().compare(qname);
    if ((result.getRelation() != NameComparisonResult::SUPERDOMAIN) &&
        (result.getRelation() != NameComparisonResult::EQUAL))
        return;

59
60
61
62
63
64
65
66
    // Omit additional data which has already been provided in the answer
    // section from the additional.
    //
    // All the address rrset with the owner name of qname have been inserted
    // into ANSWER section.
    if (qname_ == qname && qtype_ == RRType::ANY())
        return;

67
    // Find A rrset
68
    if (qname_ != qname || qtype_ != RRType::A()) {
69
70
        Zone::FindResult a_result = zone.find(qname, RRType::A(), NULL,
                                              options);
71
72
73
74
        if (a_result.code == Zone::SUCCESS) {
            response_.addRRset(Message::SECTION_ADDITIONAL,
                    boost::const_pointer_cast<RRset>(a_result.rrset));
        }
75
    }
76

77
    // Find AAAA rrset
78
    if (qname_ != qname || qtype_ != RRType::AAAA()) {
Jerry's avatar
Jerry committed
79
        Zone::FindResult aaaa_result =
80
            zone.find(qname, RRType::AAAA(), NULL, options);
81
82
83
84
        if (aaaa_result.code == Zone::SUCCESS) {
            response_.addRRset(Message::SECTION_ADDITIONAL,
                    boost::const_pointer_cast<RRset>(aaaa_result.rrset));
        }
85
86
87
    }
}

Michal Vaner's avatar
Michal Vaner committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
void
Query::putSOA(const Zone& zone) const {
    Zone::FindResult soa_result(zone.find(zone.getOrigin(),
        RRType::SOA()));
    if (soa_result.code != Zone::SUCCESS) {
        isc_throw(NoSOA, "There's no SOA record in zone " <<
            zone.getOrigin().toText());
    } else {
        /*
         * FIXME:
         * The const-cast is wrong, but the Message interface seems
         * to insist.
         */
        response_.addRRset(Message::SECTION_AUTHORITY,
            boost::const_pointer_cast<RRset>(soa_result.rrset));
    }
}

106
107
108
109
110
void
Query::getAuthAdditional(const Zone& zone) const {
    // Fill in authority and addtional sections.
    Zone::FindResult ns_result = zone.find(zone.getOrigin(), RRType::NS());
    // zone origin name should have NS records
Jerry's avatar
Jerry committed
111
112
113
114
115
    if (ns_result.code != Zone::SUCCESS) {
        isc_throw(NoApexNS, "There's no apex NS records in zone " <<
                zone.getOrigin().toText());
    } else {
        response_.addRRset(Message::SECTION_AUTHORITY,
116
117
            boost::const_pointer_cast<RRset>(ns_result.rrset));
        // Handle additional for authority section
Jerry's avatar
Jerry committed
118
119
        getAdditional(zone, *ns_result.rrset);
    }
120
121
}

122
123
void
Query::process() const {
Jerry's avatar
Jerry committed
124
125
    bool keep_doing = true;
    response_.setHeaderFlag(Message::HEADERFLAG_AA, false);
126
    const MemoryDataSrc::FindResult result =
127
        memory_datasrc_.findZone(qname_);
128

129
130
131
132
133
    // If we have no matching authoritative zone for the query name, return
    // REFUSED.  In short, this is to be compatible with BIND 9, but the
    // background discussion is not that simple.  See the relevant topic
    // at the BIND 10 developers's ML:
    // https://lists.isc.org/mailman/htdig/bind10-dev/2010-December/001633.html
134
135
    if (result.code != result::SUCCESS &&
        result.code != result::PARTIALMATCH) {
136
        response_.setRcode(Rcode::REFUSED());
137
138
139
        return;
    }

Jerry's avatar
Jerry committed
140
141
    // Found a zone which is the nearest ancestor to QNAME, set the AA bit
    response_.setHeaderFlag(Message::HEADERFLAG_AA);
142
143
    while (keep_doing) {
        keep_doing = false;
144
        Zone::FindResult db_result = result.zone->find(qname_, qtype_);
145
        switch (db_result.code) {
146
147
148
149
150
151
152
153
154
            case Zone::CNAME:
                /*
                 * We don't do chaining yet. Therefore handling a CNAME is
                 * mostly the same as handling SUCCESS, but we didn't get
                 * what we expected. It means no exceptions in ANY or NS
                 * on the origin (though CNAME in origin is probably
                 * forbidden anyway).
                 */
                // No break; here, fall trough.
155
            case Zone::SUCCESS:
156
157
                response_.setRcode(Rcode::NOERROR());
                response_.addRRset(Message::SECTION_ANSWER,
158
                    boost::const_pointer_cast<RRset>(db_result.rrset));
159
                // Handle additional for answer section
160
                getAdditional(*result.zone, *db_result.rrset);
161
162
163
164
165
                // If apex NS records haven't been provided in the answer
                // section, insert apex NS records into the authority section
                // and AAAA/A RRS of each of the NS RDATA into the additional
                // section.
                if (qname_ != result.zone->getOrigin() ||
166
                    db_result.code != Zone::SUCCESS ||
167
168
169
170
                    (qtype_ != RRType::NS() && qtype_ != RRType::ANY()))
                {
                    getAuthAdditional(*result.zone);
                }
171
172
                break;
            case Zone::DELEGATION:
Jerry's avatar
Jerry committed
173
                response_.setHeaderFlag(Message::HEADERFLAG_AA, false);
174
175
                response_.setRcode(Rcode::NOERROR());
                response_.addRRset(Message::SECTION_AUTHORITY,
176
                    boost::const_pointer_cast<RRset>(db_result.rrset));
177
                getAdditional(*result.zone, *db_result.rrset);
178
179
                break;
            case Zone::NXDOMAIN:
Michal Vaner's avatar
Michal Vaner committed
180
                // Just empty answer with SOA in authority section
181
                response_.setRcode(Rcode::NXDOMAIN());
Michal Vaner's avatar
Michal Vaner committed
182
                putSOA(*result.zone);
183
184
                break;
            case Zone::NXRRSET:
Michal Vaner's avatar
Michal Vaner committed
185
                // Just empty answer with SOA in authority section
186
                response_.setRcode(Rcode::NOERROR());
Michal Vaner's avatar
Michal Vaner committed
187
                putSOA(*result.zone);
188
189
                break;
            case Zone::DNAME:
Jerry's avatar
Jerry committed
190
                // TODO : replace qname, continue lookup
191
192
193
                break;
        }
    }
194
}
Michal Vaner's avatar
Michal Vaner committed
195

196
197
}
}