query.cc 10.3 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
    bool keep_doing = true;
125
    const bool qtype_is_any = (qtype_ == RRType::ANY());
chenzhengzhang's avatar
chenzhengzhang committed
126

Jerry's avatar
Jerry committed
127
    response_.setHeaderFlag(Message::HEADERFLAG_AA, false);
128
    const MemoryDataSrc::FindResult result =
129
        memory_datasrc_.findZone(qname_);
130

131
132
133
134
135
    // 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
136
137
    if (result.code != result::SUCCESS &&
        result.code != result::PARTIALMATCH) {
138
        response_.setRcode(Rcode::REFUSED());
139
140
141
        return;
    }

Jerry's avatar
Jerry committed
142
143
    // Found a zone which is the nearest ancestor to QNAME, set the AA bit
    response_.setHeaderFlag(Message::HEADERFLAG_AA);
144
    response_.setRcode(Rcode::NOERROR());
145
146
    while (keep_doing) {
        keep_doing = false;
147
        std::auto_ptr<RRsetList> target(qtype_is_any ? new RRsetList : NULL);
148
        const Zone::FindResult db_result(result.zone->find(qname_, qtype_,
149
            target.get()));
chenzhengzhang's avatar
chenzhengzhang committed
150

151
        switch (db_result.code) {
152
153
154
            case Zone::DNAME: {
                // First, put the dname into the answer
                response_.addRRset(Message::SECTION_ANSWER,
155
                    boost::const_pointer_cast<RRset>(db_result.rrset));
156
157
158
                /*
                 * Empty DNAME should never get in, as it is impossible to
                 * create one in master file.
159
160
                 *
                 * FIXME: Other way to prevent this should be done
161
                 */
162
                assert(db_result.rrset->getRdataCount() > 0);
163
164
165
                // Get the data of DNAME
                const rdata::generic::DNAME& dname(
                    dynamic_cast<const rdata::generic::DNAME&>(
166
                    db_result.rrset->getRdataIterator()->getCurrent()));
167
                // The yet unmatched prefix dname
168
                const Name prefix(qname_.split(0, qname_.getLabelCount() -
169
170
171
                    db_result.rrset->getName().getLabelCount()));
                // If we put it together, will it be too long?
                // (The prefix contains trailing ., which will be removed
172
173
                if (prefix.getLength() - Name::ROOT_NAME().getLength() +
                    dname.getDname().getLength() > Name::MAX_WIRE) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
174
175
176
177
                    /*
                     * In case the synthesized name is too long, section 4.1
                     * of RFC 2672 mandates we return YXDOMAIN.
                     */
178
179
180
                    response_.setRcode(Rcode::YXDOMAIN());
                    return;
                }
181
182
183
184
                // The new CNAME we are creating (it will be unsigned even
                // with DNSSEC, the DNAME is signed and it can be validated
                // by that)
                RRsetPtr cname(new RRset(qname_, db_result.rrset->getClass(),
JINMEI Tatuya's avatar
JINMEI Tatuya committed
185
                    RRType::CNAME(), db_result.rrset->getTTL()));
186
187
188
189
190
191
192
                // Construct the new target by replacing the end
                cname->addRdata(rdata::generic::CNAME(qname_.split(0,
                    qname_.getLabelCount() -
                    db_result.rrset->getName().getLabelCount()).
                    concatenate(dname.getDname())));
                response_.addRRset(Message::SECTION_ANSWER, cname);
                break;
193
            }
194
195
196
197
198
199
200
            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).
201
202
                 *
                 * So, just put it there.
203
                 */
204
205
206
                response_.addRRset(Message::SECTION_ANSWER,
                    boost::const_pointer_cast<RRset>(db_result.rrset));
                break;
207
            case Zone::SUCCESS:
chenzhengzhang's avatar
chenzhengzhang committed
208
209
210
211
212
                if (qtype_is_any) {
                    // If quety type is ANY, insert all RRs under the domain
                    // into answer section.
                    BOOST_FOREACH(RRsetPtr rrset, *target) {
                        response_.addRRset(Message::SECTION_ANSWER, rrset);
213
214
                        // Handle additional for answer section
                        getAdditional(*result.zone, *rrset.get());
chenzhengzhang's avatar
chenzhengzhang committed
215
216
217
                    }
                } else {
                    response_.addRRset(Message::SECTION_ANSWER,
218
                        boost::const_pointer_cast<RRset>(db_result.rrset));
chenzhengzhang's avatar
chenzhengzhang committed
219
                    // Handle additional for answer section
220
                    getAdditional(*result.zone, *db_result.rrset);
chenzhengzhang's avatar
chenzhengzhang committed
221
                }
222
223
224
225
226
                // 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() ||
227
                    db_result.code != Zone::SUCCESS ||
chenzhengzhang's avatar
chenzhengzhang committed
228
                    (qtype_ != RRType::NS() && !qtype_is_any))
229
230
231
                {
                    getAuthAdditional(*result.zone);
                }
232
233
                break;
            case Zone::DELEGATION:
Jerry's avatar
Jerry committed
234
                response_.setHeaderFlag(Message::HEADERFLAG_AA, false);
235
                response_.addRRset(Message::SECTION_AUTHORITY,
236
237
                    boost::const_pointer_cast<RRset>(db_result.rrset));
                getAdditional(*result.zone, *db_result.rrset);
238
239
                break;
            case Zone::NXDOMAIN:
Michal Vaner's avatar
Michal Vaner committed
240
                // Just empty answer with SOA in authority section
241
                response_.setRcode(Rcode::NXDOMAIN());
Michal Vaner's avatar
Michal Vaner committed
242
                putSOA(*result.zone);
243
244
                break;
            case Zone::NXRRSET:
Michal Vaner's avatar
Michal Vaner committed
245
                // Just empty answer with SOA in authority section
Michal Vaner's avatar
Michal Vaner committed
246
                putSOA(*result.zone);
247
248
249
                break;
        }
    }
250
}
Michal Vaner's avatar
Michal Vaner committed
251

252
253
}
}