Kea  1.9.9-git
masterload.cc
Go to the documentation of this file.
1 // Copyright (C) 2010-2020 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #include <config.h>
8 
9 #include <dns/masterload.h>
10 #include <dns/master_loader.h>
11 #include <dns/name.h>
12 #include <dns/rdata.h>
13 #include <dns/rdataclass.h>
14 #include <dns/rrclass.h>
15 #include <dns/rrset.h>
16 #include <dns/rrttl.h>
17 #include <dns/rrtype.h>
18 #include <dns/rrcollator.h>
19 #include <exceptions/exceptions.h>
20 
21 #include <boost/scoped_ptr.hpp>
22 
23 #include <functional>
24 #include <istream>
25 #include <fstream>
26 #include <sstream>
27 #include <string>
28 #include <cctype>
29 #include <cerrno>
30 
31 using namespace isc::dns::rdata;
32 
33 using namespace std;
34 namespace ph = std::placeholders;
35 
36 namespace isc {
37 namespace dns {
38 namespace {
39 void
40 callbackWrapper(const RRsetPtr& rrset, MasterLoadCallback callback,
41  const Name* origin)
42 {
43  // Origin related validation:
44  // - reject out-of-zone data
45  // - reject SOA whose owner is not at the top of zone
46  const NameComparisonResult cmp_result =
47  rrset->getName().compare(*origin);
48  if (cmp_result.getRelation() != NameComparisonResult::EQUAL &&
49  cmp_result.getRelation() != NameComparisonResult::SUBDOMAIN) {
50  isc_throw(MasterLoadError, "Out-of-zone data for " << *origin
51  << "/" << rrset->getClass() << ": " << rrset->getName());
52  }
53  if (rrset->getType() == RRType::SOA() &&
54  cmp_result.getRelation() != NameComparisonResult::EQUAL) {
55  isc_throw(MasterLoadError, "SOA not at top of zone: "
56  << *rrset);
57  }
58 
59  callback(rrset);
60 }
61 
62 template <typename InputType>
63 void
64 loadHelper(InputType input, const Name& origin,
65  const RRClass& zone_class, MasterLoadCallback callback)
66 {
67  RRCollator rr_collator(std::bind(callbackWrapper, ph::_1, callback,
68  &origin));
69  MasterLoader loader(input, origin, zone_class,
71  rr_collator.getCallback());
72  try {
73  loader.load();
74  } catch (const MasterLoaderError& ex) {
75  isc_throw(MasterLoadError, ex.what());
76  }
77  rr_collator.flush();
78 }
79 }
80 
81 void
82 masterLoad(const char* const filename, const Name& origin,
83  const RRClass& zone_class, MasterLoadCallback callback)
84 {
85  if ((filename == NULL) || (*filename == '\0')) {
86  isc_throw(MasterLoadError, "Name of master file must not be null");
87  }
88 
89  loadHelper<const char*>(filename, origin, zone_class, callback);
90 }
91 
92 void
93 masterLoad(istream& input, const Name& origin, const RRClass& zone_class,
94  MasterLoadCallback callback, const char*)
95 {
96  loadHelper<istream&>(input, origin, zone_class, callback);
97 }
98 
99 } // namespace dns
100 } // namespace isc
The Name class encapsulates DNS names.
Definition: name.h:223
static const RRType & SOA()
Definition: rrtype.h:461
STL namespace.
static MasterLoaderCallbacks getNullCallbacks()
Return a callbacks instance with null callbacks.
An exception that is thrown if an error occurs while loading a master zone data.
Definition: masterload.h:23
The RRClass class encapsulates DNS resource record classes.
Definition: rrclass.h:98
std::function< void(RRsetPtr)> MasterLoadCallback
The type of the callback parameter of masterLoad().
Definition: masterload.h:33
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
Defines the logger used by the top-level component of kea-dhcp-ddns.
boost::shared_ptr< AbstractRRset > RRsetPtr
A pointer-like type pointing to an RRset object.
Definition: rrset.h:47
void masterLoad(const char *const filename, const Name &origin, const RRClass &zone_class, MasterLoadCallback callback)
Master zone file loader from a file.
Definition: masterload.cc:82