1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
// Copyright (C) 2012 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.
#include <dns/zone_checker.h>
#include <dns/name.h>
#include <dns/rdataclass.h>
#include <dns/rrclass.h>
#include <dns/rrtype.h>
#include <dns/rrset.h>
#include <dns/rrset_collection_base.h>
#include <boost/bind.hpp>
#include <boost/lexical_cast.hpp>
#include <string>
using boost::lexical_cast;
using std::string;
namespace isc {
namespace dns {
namespace {
std::string
zoneText(const Name& zone_name, const RRClass& zone_class) {
return (zone_name.toText(true) + "/" + zone_class.toText());
}
void
checkSOA(const Name& zone_name, const RRClass& zone_class,
const RRsetCollectionBase& zone_rrsets,
ZoneCheckerCallbacks& callback) {
ConstRRsetPtr rrset =
zone_rrsets.find(zone_name, zone_class, RRType::SOA());
size_t count = 0;
if (rrset) {
for (RdataIteratorPtr rit = rrset->getRdataIterator();
!rit->isLast();
rit->next(), ++count) {
if (dynamic_cast<const rdata::generic::SOA*>(&rit->getCurrent()) ==
NULL) {
isc_throw(Unexpected, "Zone checker found bad RDATA in SOA");
}
}
if (count == 0) {
// this should be an implementation bug, not an operational error.
isc_throw(Unexpected, "Zone checker found an empty SOA RRset");
}
}
if (count != 1) {
callback.error("zone " + zoneText(zone_name, zone_class) + ": has " +
lexical_cast<string>(count) + " SOA records");
}
}
// Check if a target name is beyond zone cut, either due to delegation or
// DNAME. Note that DNAME works on the origin but not on the name itself,
// while delegation works on the name itself (but the NS at the origin is not
// delegation).
ConstRRsetPtr
findZoneCut(const Name& zone_name, const RRClass& zone_class,
const RRsetCollectionBase& zone_rrsets, const Name& target_name) {
const unsigned int origin_count = zone_name.getLabelCount();
const unsigned int target_count = target_name.getLabelCount();
assert(origin_count <= target_count);
for (unsigned int l = origin_count; l <= target_count; ++l) {
const Name& mid_name = (l == target_count) ? target_name :
target_name.split(target_count - l);
ConstRRsetPtr found;
if (l != origin_count &&
(found = zone_rrsets.find(mid_name, zone_class, RRType::NS())) !=
NULL) {
return (found);
}
if (l != target_count &&
(found = zone_rrsets.find(mid_name, zone_class, RRType::DNAME()))
!= NULL) {
return (found);
}
}
return (ConstRRsetPtr());
}
// Check if each "in-zone" NS name has an address record, identifying some
// error cases.
void
checkNSNames(const Name& zone_name, const RRClass& zone_class,
const RRsetCollectionBase& zone_rrsets,
ConstRRsetPtr ns_rrset, ZoneCheckerCallbacks& callbacks) {
if (ns_rrset->getRdataCount() == 0) {
// this should be an implementation bug, not an operational error.
isc_throw(Unexpected, "Zone checker found an empty NS RRset");
}
for (RdataIteratorPtr rit = ns_rrset->getRdataIterator();
!rit->isLast();
rit->next()) {
const rdata::generic::NS* ns_data =
dynamic_cast<const rdata::generic::NS*>(&rit->getCurrent());
if (ns_data == NULL) {
isc_throw(Unexpected, "Zone checker found bad RDATA in NS");
}
const Name& ns_name = ns_data->getNSName();
const NameComparisonResult::NameRelation reln =
ns_name.compare(zone_name).getRelation();
if (reln != NameComparisonResult::EQUAL &&
reln != NameComparisonResult::SUBDOMAIN) {
continue; // not in the zone. we can ignore it.
}
// Check if there's a zone cut between the origin and the NS name.
ConstRRsetPtr cut_rrset = findZoneCut(zone_name, zone_class,
zone_rrsets, ns_name);
if (cut_rrset) {
if (cut_rrset->getType() == RRType::NS()) {
continue; // delegation; making the NS name "out of zone".
} else if (cut_rrset->getType() == RRType::DNAME()) {
callbacks.error("zone " + zoneText(zone_name, zone_class) +
": NS '" + ns_name.toText(true) + "' is " +
"below a DNAME '" +
cut_rrset->getName().toText(true) +
"' (illegal per RFC6672)");
continue;
} else {
assert(false);
}
}
if (zone_rrsets.find(ns_name, zone_class, RRType::CNAME()) != NULL) {
callbacks.error("zone " + zoneText(zone_name, zone_class) +
": NS '" + ns_name.toText(true) + "' is a CNAME " +
"(illegal per RFC2181)");
continue;
}
if (zone_rrsets.find(ns_name, zone_class, RRType::A()) == NULL &&
zone_rrsets.find(ns_name, zone_class, RRType::AAAA()) == NULL) {
callbacks.warn("zone " + zoneText(zone_name, zone_class) +
": NS has no address records (A or AAAA)");
}
}
}
void
checkNS(const Name& zone_name, const RRClass& zone_class,
const RRsetCollectionBase& zone_rrsets,
ZoneCheckerCallbacks& callbacks) {
ConstRRsetPtr rrset =
zone_rrsets.find(zone_name, zone_class, RRType::NS());
if (rrset == NULL) {
callbacks.error("zone " + zoneText(zone_name, zone_class) +
": has no NS records");
return;
}
checkNSNames(zone_name, zone_class, zone_rrsets, rrset, callbacks);
}
// The following is a simple wrapper of checker callback so checkZone()
// can also remember any critical errors.
void
errorWrapper(const string& reason, const ZoneCheckerCallbacks* callbacks,
bool* had_error) {
*had_error = true;
callbacks->error(reason);
}
}
bool
checkZone(const Name& zone_name, const RRClass& zone_class,
const RRsetCollectionBase& zone_rrsets,
const ZoneCheckerCallbacks& callbacks) {
bool had_error = false;
ZoneCheckerCallbacks my_callbacks(
boost::bind(errorWrapper, _1, &callbacks, &had_error),
boost::bind(&ZoneCheckerCallbacks::warn, &callbacks, _1));
checkSOA(zone_name, zone_class, zone_rrsets, my_callbacks);
checkNS(zone_name, zone_class, zone_rrsets, my_callbacks);
return (!had_error);
}
} // end namespace dns
} // end namespace isc
|