query.cc 6.98 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
69
70
71
72
73
    if (qname_ != qname || qtype_ != RRType::A()) {
        Zone::FindResult a_result = zone.find(qname, RRType::A(), options);
        if (a_result.code == Zone::SUCCESS) {
            response_.addRRset(Message::SECTION_ADDITIONAL,
                    boost::const_pointer_cast<RRset>(a_result.rrset));
        }
74
    }
75

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

Michal Vaner's avatar
Michal Vaner committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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));
    }
}

105
106
107
108
109
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
110
111
112
113
114
    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,
115
116
            boost::const_pointer_cast<RRset>(ns_result.rrset));
        // Handle additional for authority section
Jerry's avatar
Jerry committed
117
118
        getAdditional(zone, *ns_result.rrset);
    }
119
120
}

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

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

Jerry's avatar
Jerry committed
139
140
    // Found a zone which is the nearest ancestor to QNAME, set the AA bit
    response_.setHeaderFlag(Message::HEADERFLAG_AA);
141
142
    while (keep_doing) {
        keep_doing = false;
143
        Zone::FindResult db_result = result.zone->find(qname_, qtype_);
144
145
        switch (db_result.code) {
            case Zone::SUCCESS:
146
147
                response_.setRcode(Rcode::NOERROR());
                response_.addRRset(Message::SECTION_ANSWER,
148
                    boost::const_pointer_cast<RRset>(db_result.rrset));
149
                // Handle additional for answer section
150
                getAdditional(*result.zone, *db_result.rrset);
151
152
153
154
155
156
157
158
159
                // 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() ||
                    (qtype_ != RRType::NS() && qtype_ != RRType::ANY()))
                {
                    getAuthAdditional(*result.zone);
                }
160
161
                break;
            case Zone::DELEGATION:
Jerry's avatar
Jerry committed
162
                response_.setHeaderFlag(Message::HEADERFLAG_AA, false);
163
164
                response_.setRcode(Rcode::NOERROR());
                response_.addRRset(Message::SECTION_AUTHORITY,
165
                    boost::const_pointer_cast<RRset>(db_result.rrset));
166
                getAdditional(*result.zone, *db_result.rrset);
167
168
                break;
            case Zone::NXDOMAIN:
Michal Vaner's avatar
Michal Vaner committed
169
                // Just empty answer with SOA in authority section
170
                response_.setRcode(Rcode::NXDOMAIN());
Michal Vaner's avatar
Michal Vaner committed
171
                putSOA(*result.zone);
172
173
                break;
            case Zone::NXRRSET:
Michal Vaner's avatar
Michal Vaner committed
174
                // Just empty answer with SOA in authority section
175
                response_.setRcode(Rcode::NOERROR());
Michal Vaner's avatar
Michal Vaner committed
176
                putSOA(*result.zone);
177
178
179
                break;
            case Zone::CNAME:
            case Zone::DNAME:
Jerry's avatar
Jerry committed
180
                // TODO : replace qname, continue lookup
181
182
183
                break;
        }
    }
184
}
Michal Vaner's avatar
Michal Vaner committed
185

186
187
}
}