master_loader.cc 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. // Copyright (C) 2012-2014 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <dns/master_loader.h>
  15. #include <dns/master_lexer.h>
  16. #include <dns/name.h>
  17. #include <dns/rdataclass.h>
  18. #include <dns/rrttl.h>
  19. #include <dns/rrclass.h>
  20. #include <dns/rrtype.h>
  21. #include <dns/rdata.h>
  22. #include <boost/format.hpp>
  23. #include <boost/algorithm/string/predicate.hpp> // for iequals
  24. #include <boost/scoped_ptr.hpp>
  25. #include <boost/shared_ptr.hpp>
  26. #include <string>
  27. #include <memory>
  28. #include <vector>
  29. #include <cstdio> // for sscanf()
  30. using std::string;
  31. using std::auto_ptr;
  32. using std::vector;
  33. using std::pair;
  34. using boost::algorithm::iequals;
  35. using boost::shared_ptr;
  36. namespace isc {
  37. namespace dns {
  38. namespace {
  39. // An internal exception, used to control the code flow in case of errors.
  40. // It is thrown during the loading and caught later, not to be propagated
  41. // outside of the file.
  42. class InternalException : public isc::Exception {
  43. public:
  44. InternalException(const char* filename, size_t line, const char* what) :
  45. Exception(filename, line, what)
  46. {}
  47. };
  48. } // end unnamed namespace
  49. /// \brief Private implementation class for the \c MasterLoader
  50. ///
  51. /// This class is used internally by the \c MasterLoader and is not
  52. /// publicly visible. It is present to avoid polluting the public API
  53. /// with internal implementation details of the \c MasterLoader.
  54. // cppcheck-suppress noConstructor
  55. class MasterLoader::MasterLoaderImpl {
  56. public:
  57. /// \brief Constructor.
  58. ///
  59. /// \param master_file Path to the file to load.
  60. /// \param zone_origin The origin of zone to be expected inside
  61. /// the master file. Currently unused, but it is expected to
  62. /// be used for some validation.
  63. /// \param zone_class The class of zone to be expected inside the
  64. /// master file.
  65. /// \param callbacks The callbacks by which it should report problems.
  66. /// Usually, the callback carries a filename and line number of the
  67. /// input where the problem happens. There's a special case of empty
  68. /// filename and zero line in case the opening of the top-level master
  69. /// file fails.
  70. /// \param add_callback The callback which would be called with each
  71. /// loaded RR.
  72. /// \param options Options for the parsing, which is bitwise-or of
  73. /// the Options values or DEFAULT. If the MANY_ERRORS option is
  74. /// included, the parser tries to continue past errors. If it
  75. /// is not included, it stops at first encountered error.
  76. /// \throw std::bad_alloc when there's not enough memory.
  77. MasterLoaderImpl(const char* master_file,
  78. const Name& zone_origin,
  79. const RRClass& zone_class,
  80. const MasterLoaderCallbacks& callbacks,
  81. const AddRRCallback& add_callback,
  82. MasterLoader::Options options) :
  83. lexer_(),
  84. zone_origin_(zone_origin),
  85. active_origin_(zone_origin),
  86. zone_class_(zone_class),
  87. callbacks_(callbacks),
  88. add_callback_(add_callback),
  89. options_(options),
  90. master_file_(master_file),
  91. initialized_(false),
  92. ok_(true),
  93. many_errors_((options & MANY_ERRORS) != 0),
  94. previous_name_(false),
  95. complete_(false),
  96. seen_error_(false),
  97. warn_rfc1035_ttl_(true),
  98. rr_count_(0)
  99. {}
  100. /// \brief Wrapper around \c MasterLexer::pushSource() (file version)
  101. ///
  102. /// This method is used as a wrapper around the lexer's
  103. /// \c pushSource() to also save the current origin and the last
  104. /// seen name (to be restored upon \c popSource()). It also calls
  105. /// \c pushSource(). See \c doInclude() implementation for more
  106. /// details.
  107. ///
  108. /// \param filename Path to the file to push as a new source.
  109. /// \param current_origin The current origin name to save.
  110. void pushSource(const std::string& filename, const Name& current_origin) {
  111. std::string error;
  112. if (!lexer_.pushSource(filename.c_str(), &error)) {
  113. if (initialized_) {
  114. isc_throw(InternalException, error.c_str());
  115. } else {
  116. // Top-level file
  117. reportError("", 0, error);
  118. ok_ = false;
  119. }
  120. }
  121. // Store the current status, so we can recover it upon popSource
  122. include_info_.push_back(IncludeInfo(current_origin, last_name_));
  123. initialized_ = true;
  124. previous_name_ = false;
  125. }
  126. /// \brief Wrapper around \c MasterLexer::pushSource() (stream version)
  127. ///
  128. /// Similar to \c pushSource(). This method need not save the
  129. /// current origin as it is not used with $INCLUDE processing.
  130. ///
  131. /// \param stream The input stream to use as a new source.
  132. void pushStreamSource(std::istream& stream) {
  133. lexer_.pushSource(stream);
  134. initialized_ = true;
  135. }
  136. /// \brief Implementation of \c MasterLoader::loadIncremental()
  137. ///
  138. /// See \c MasterLoader::loadIncremental() for details.
  139. bool loadIncremental(size_t count_limit);
  140. /// \brief Return the total size of the input sources pushed so
  141. /// far. See \c MasterLexer::getTotalSourceSize().
  142. size_t getSize() const { return (lexer_.getTotalSourceSize()); }
  143. /// \brief Return the line number being parsed in the pushed input
  144. /// sources. See \c MasterLexer::getPosition().
  145. size_t getPosition() const { return (lexer_.getPosition()); }
  146. private:
  147. /// \brief Report an error using the callbacks that were supplied
  148. /// during \c MasterLoader construction. Note that this method also
  149. /// throws \c MasterLoaderError exception if necessary, so the
  150. /// caller need not throw it.
  151. void reportError(const std::string& filename, size_t line,
  152. const std::string& reason)
  153. {
  154. seen_error_ = true;
  155. callbacks_.error(filename, line, reason);
  156. if (!many_errors_) {
  157. // In case we don't have the lenient mode, every error is fatal
  158. // and we throw
  159. ok_ = false;
  160. complete_ = true;
  161. isc_throw(MasterLoaderError, reason.c_str());
  162. }
  163. }
  164. /// \brief Wrapper around \c MasterLexer::popSource()
  165. ///
  166. /// This method is used as a wrapper around the lexer's
  167. /// \c popSource() to also restore the current origin and the last
  168. /// seen name (at time of push). It also calls \c popSource(). See
  169. /// \c doInclude() implementation for more details.
  170. bool popSource() {
  171. if (lexer_.getSourceCount() == 1) {
  172. return (false);
  173. }
  174. lexer_.popSource();
  175. // Restore original origin and last seen name
  176. // We move in tandem, there's an extra item included during the
  177. // initialization, so we can never run out of them
  178. assert(!include_info_.empty());
  179. const IncludeInfo& info(include_info_.back());
  180. active_origin_ = info.first;
  181. last_name_ = info.second;
  182. include_info_.pop_back();
  183. previous_name_ = false;
  184. return (true);
  185. }
  186. /// \brief Get a string token. Handle it as error if it is not string.
  187. const string getString() {
  188. lexer_.getNextToken(MasterToken::STRING).getString(string_token_);
  189. return (string_token_);
  190. }
  191. /// \brief Parse the initial token at the beginning of a line in a
  192. /// master file (or stream).
  193. ///
  194. /// A helper method of \c loadIncremental(), parsing the first token
  195. /// of a new line. If it looks like an RR, detect its owner name
  196. /// and return a string token for the next field of the RR.
  197. ///
  198. /// Otherwise, return either \c END_OF_LINE or \c END_OF_FILE token
  199. /// depending on whether the loader continues to the next line or
  200. /// completes the load, respectively. Other corner cases including
  201. /// $-directive handling is done here.
  202. ///
  203. /// For unexpected errors, it throws an exception, which will be
  204. /// handled in \c loadIncremental().
  205. MasterToken handleInitialToken();
  206. /// \brief Helper method for \c doGenerate().
  207. ///
  208. /// This is a helper method for \c doGenerate() that processes the
  209. /// LHS or RHS for a single iteration in the range that is requested
  210. /// by the $GENERATE directive and returns a generated string (that
  211. /// is used to build a name (LHS) or RDATA (RHS) for an RR). See the
  212. /// commented implementation for details.
  213. std::string generateForIter(const std::string& str, const int it);
  214. /// \brief Process the $GENERATE directive.
  215. ///
  216. /// See the commented implementation for details.
  217. void doGenerate();
  218. /// \brief Process the $ORIGIN directive.
  219. void doOrigin(bool is_optional) {
  220. // Parse and create the new origin. It is relative to the previous
  221. // one.
  222. const MasterToken&
  223. name_tok(lexer_.getNextToken(MasterToken::QSTRING, is_optional));
  224. if (name_tok.getType() == MasterToken::QSTRING ||
  225. name_tok.getType() == MasterToken::STRING) {
  226. const MasterToken::StringRegion&
  227. name_string(name_tok.getStringRegion());
  228. active_origin_ = Name(name_string.beg, name_string.len,
  229. &active_origin_);
  230. if (name_string.len > 0 &&
  231. name_string.beg[name_string.len - 1] != '.') {
  232. callbacks_.warning(lexer_.getSourceName(),
  233. lexer_.getSourceLine(),
  234. "The new origin is relative, did you really"
  235. " mean " + active_origin_.toText() + "?");
  236. }
  237. } else {
  238. // If it is not optional, we must not get anything but
  239. // a string token.
  240. assert(is_optional);
  241. // We return the newline there. This is because we want to
  242. // behave the same if there is or isn't the name, leaving the
  243. // newline there.
  244. lexer_.ungetToken();
  245. }
  246. }
  247. /// \brief Process the $INCLUDE directive.
  248. void doInclude() {
  249. // First, get the filename to include
  250. const string
  251. filename(lexer_.getNextToken(MasterToken::QSTRING).getString());
  252. // There optionally can be an origin, that applies before the include.
  253. // We need to save the currently active origin before calling
  254. // doOrigin(), because it would update active_origin_ while we need
  255. // to pass the active origin before recognizing the new origin to
  256. // pushSource. Note: RFC 1035 is not really clear on this: it reads
  257. // "regardless of changes... within the included file", but the new
  258. // origin is not really specified "within the included file".
  259. // Nevertheless, this behavior is probably more likely to be the
  260. // intent of the RFC, and it's compatible with BIND 9.
  261. const Name current_origin = active_origin_;
  262. doOrigin(true);
  263. pushSource(filename, current_origin);
  264. }
  265. /// \brief Parse RR fields (TTL, CLASS and TYPE).
  266. ///
  267. /// A helper method for \c loadIncremental(). It parses part of an
  268. /// RR until it finds the RR type field. If TTL or RR class is
  269. /// specified before the RR type, it also recognizes and validates
  270. /// them.
  271. ///
  272. /// \param explicit_ttl will be set to true if this method finds a
  273. /// valid TTL field.
  274. /// \param rrparam_token Pass the current (parsed) token here.
  275. RRType parseRRParams(bool& explicit_ttl, MasterToken rrparam_token) {
  276. // Find TTL, class and type. Both TTL and class are
  277. // optional and may occur in any order if they exist. TTL
  278. // and class come before type which must exist.
  279. //
  280. // [<TTL>] [<class>] <type> <RDATA>
  281. // [<class>] [<TTL>] <type> <RDATA>
  282. // named-signzone outputs TTL first, so try parsing it in order
  283. // first.
  284. if (setCurrentTTL(rrparam_token.getString())) {
  285. explicit_ttl = true;
  286. rrparam_token = lexer_.getNextToken(MasterToken::STRING);
  287. } else {
  288. // If it's not a TTL here, continue and try again
  289. // after the RR class below.
  290. }
  291. boost::scoped_ptr<RRClass> rrclass
  292. (RRClass::createFromText(rrparam_token.getString()));
  293. if (rrclass) {
  294. if (*rrclass != zone_class_) {
  295. isc_throw(InternalException, "Class mismatch: " << *rrclass <<
  296. " vs. " << zone_class_);
  297. }
  298. rrparam_token = lexer_.getNextToken(MasterToken::STRING);
  299. }
  300. // If we couldn't parse TTL earlier in the stream (above), try
  301. // again at current location.
  302. if (!explicit_ttl && setCurrentTTL(rrparam_token.getString())) {
  303. explicit_ttl = true;
  304. rrparam_token = lexer_.getNextToken(MasterToken::STRING);
  305. }
  306. // Return the current string token's value as the RRType.
  307. return (RRType(rrparam_token.getString()));
  308. }
  309. /// \brief Check and limit TTL to maximum value.
  310. ///
  311. /// Upper limit check when recognizing a specific TTL value from the
  312. /// zone file ($TTL, the RR's TTL field, or the SOA minimum). RFC2181
  313. /// Section 8 limits the range of TTL values to 2^31-1 (0x7fffffff),
  314. /// and prohibits transmitting a TTL field exceeding this range. We
  315. /// guarantee that by limiting the value at the time of zone
  316. /// parsing/loading, following what BIND 9 does. Resetting it to 0
  317. /// at this point may not be exactly what the RFC states (depending on
  318. /// the meaning of 'received'), but the end result would be the same (i.e.,
  319. /// the guarantee on transmission). Again, we follow the BIND 9's behavior
  320. /// here.
  321. ///
  322. /// \param ttl the TTL to check. If it is larger than the maximum
  323. /// allowed, it is set to 0.
  324. /// \param post_parsing should be true iff this method is called
  325. /// after parsing the entire RR and the lexer is positioned at the
  326. /// next line. It's just for calculating the accurate source line
  327. /// when callback is necessary.
  328. void limitTTL(RRTTL& ttl, bool post_parsing) {
  329. if (ttl > RRTTL::MAX_TTL()) {
  330. const size_t src_line = lexer_.getSourceLine() -
  331. (post_parsing ? 1 : 0);
  332. callbacks_.warning(lexer_.getSourceName(), src_line,
  333. "TTL " + ttl.toText() + " > MAXTTL, "
  334. "setting to 0 per RFC2181");
  335. ttl = RRTTL(0);
  336. }
  337. }
  338. /// \brief Set/reset the default TTL.
  339. ///
  340. /// This should be from either $TTL or SOA minimum TTL (it's the
  341. /// caller's responsibility; this method doesn't care about where it
  342. /// comes from). See \c limitTTL() for parameter post_parsing.
  343. void setDefaultTTL(const RRTTL& ttl, bool post_parsing) {
  344. assignTTL(default_ttl_, ttl);
  345. limitTTL(*default_ttl_, post_parsing);
  346. }
  347. /// \brief Try to set/reset the current TTL from candidate TTL text.
  348. ///
  349. /// It's possible it that the text does not actually represent a TTL
  350. /// (which is not immediately considered an error). Returns \c true
  351. /// iff it's recognized as a valid TTL (and only in which case the
  352. /// current TTL is set).
  353. ///
  354. /// \param ttl_txt The text to parse as a TTL.
  355. /// \return true if a TTL was parsed (and set as the current TTL).
  356. bool setCurrentTTL(const string& ttl_txt) {
  357. // We use the factory version instead of RRTTL constructor as we
  358. // need to expect cases where ttl_txt does not actually represent a TTL
  359. // but an RR class or type.
  360. RRTTL* rrttl = RRTTL::createFromText(ttl_txt);
  361. if (rrttl) {
  362. current_ttl_.reset(rrttl);
  363. limitTTL(*current_ttl_, false);
  364. return (true);
  365. }
  366. return (false);
  367. }
  368. /// \brief Determine the TTL of the current RR based on the given
  369. /// parsing context.
  370. ///
  371. /// \c explicit_ttl is true iff the TTL is explicitly specified for that RR
  372. /// (in which case current_ttl_ is set to that TTL).
  373. /// \c rrtype is the type of the current RR, and \c rdata is its RDATA. They
  374. /// only matter if the type is SOA and no available TTL is known. In this
  375. /// case the minimum TTL of the SOA will be used as the TTL of that SOA
  376. /// and the default TTL for subsequent RRs.
  377. const RRTTL& getCurrentTTL(bool explicit_ttl, const RRType& rrtype,
  378. const rdata::ConstRdataPtr& rdata) {
  379. // We've completed parsing the full of RR, and the lexer is already
  380. // positioned at the next line. If we need to call callback,
  381. // we need to adjust the line number.
  382. const size_t current_line = lexer_.getSourceLine() - 1;
  383. if (!current_ttl_ && !default_ttl_) {
  384. if (rrtype == RRType::SOA()) {
  385. callbacks_.warning(lexer_.getSourceName(), current_line,
  386. "no TTL specified; "
  387. "using SOA MINTTL instead");
  388. const uint32_t ttl_val =
  389. dynamic_cast<const rdata::generic::SOA&>(*rdata).
  390. getMinimum();
  391. setDefaultTTL(RRTTL(ttl_val), true);
  392. assignTTL(current_ttl_, *default_ttl_);
  393. } else {
  394. // On catching the exception we'll try to reach EOL again,
  395. // so we need to unget it now.
  396. lexer_.ungetToken();
  397. throw InternalException(__FILE__, __LINE__,
  398. "no TTL specified; load rejected");
  399. }
  400. } else if (!explicit_ttl && default_ttl_) {
  401. assignTTL(current_ttl_, *default_ttl_);
  402. } else if (!explicit_ttl && warn_rfc1035_ttl_) {
  403. // Omitted (class and) TTL values are default to the last
  404. // explicitly stated values (RFC 1035, Sec. 5.1).
  405. callbacks_.warning(lexer_.getSourceName(), current_line,
  406. "using RFC1035 TTL semantics; default to the "
  407. "last explicitly stated TTL");
  408. warn_rfc1035_ttl_ = false; // we only warn about this once
  409. }
  410. assert(current_ttl_);
  411. return (*current_ttl_);
  412. }
  413. /// \brief Handle a $DIRECTIVE
  414. ///
  415. /// This method is called when a $DIRECTIVE is encountered in the
  416. /// input stream.
  417. void handleDirective(const char* directive, size_t length) {
  418. if (iequals(directive, "INCLUDE")) {
  419. doInclude();
  420. } else if (iequals(directive, "ORIGIN")) {
  421. doOrigin(false);
  422. eatUntilEOL(true);
  423. } else if (iequals(directive, "GENERATE")) {
  424. doGenerate();
  425. eatUntilEOL(true);
  426. } else if (iequals(directive, "TTL")) {
  427. setDefaultTTL(RRTTL(getString()), false);
  428. eatUntilEOL(true);
  429. } else {
  430. isc_throw(InternalException, "Unknown directive '" <<
  431. string(directive, directive + length) << "'");
  432. }
  433. }
  434. /// \brief Skip tokens until end-of-line.
  435. void eatUntilEOL(bool reportExtra) {
  436. // We want to continue. Try to read until the end of line
  437. for (;;) {
  438. const MasterToken& token(lexer_.getNextToken());
  439. switch (token.getType()) {
  440. case MasterToken::END_OF_FILE:
  441. callbacks_.warning(lexer_.getSourceName(),
  442. lexer_.getSourceLine(),
  443. "File does not end with newline");
  444. // We don't pop here. The End of file will stay there,
  445. // and we'll handle it in the next iteration of
  446. // loadIncremental properly.
  447. return;
  448. case MasterToken::END_OF_LINE:
  449. // Found the end of the line. Good.
  450. return;
  451. default:
  452. // Some other type of token.
  453. if (reportExtra) {
  454. reportExtra = false;
  455. reportError(lexer_.getSourceName(),
  456. lexer_.getSourceLine(),
  457. "Extra tokens at the end of line");
  458. }
  459. break;
  460. }
  461. }
  462. }
  463. /// \brief Assign the right RRTTL's value to the left RRTTL. If one
  464. /// doesn't exist in the scoped_ptr, make a new RRTTL copy of the
  465. /// right argument.
  466. static void assignTTL(boost::scoped_ptr<RRTTL>& left, const RRTTL& right) {
  467. if (!left) {
  468. left.reset(new RRTTL(right));
  469. } else {
  470. *left = right;
  471. }
  472. }
  473. private:
  474. MasterLexer lexer_;
  475. const Name zone_origin_;
  476. Name active_origin_; // The origin used during parsing
  477. // (modifiable by $ORIGIN)
  478. shared_ptr<Name> last_name_; // Last seen name (for INITIAL_WS handling)
  479. const RRClass zone_class_;
  480. MasterLoaderCallbacks callbacks_;
  481. const AddRRCallback add_callback_;
  482. boost::scoped_ptr<RRTTL> default_ttl_; // Default TTL of RRs used when
  483. // unspecified. If NULL no default
  484. // is known.
  485. boost::scoped_ptr<RRTTL> current_ttl_; // The TTL used most recently.
  486. // Initially unset. Once set
  487. // always stores a valid
  488. // RRTTL.
  489. const MasterLoader::Options options_;
  490. const std::string master_file_;
  491. std::string string_token_;
  492. bool initialized_;
  493. bool ok_; // Is it OK to continue loading?
  494. const bool many_errors_; // Are many errors allowed (or should we abort
  495. // on the first)
  496. // Some info about the outer files from which we include.
  497. // The first one is current origin, the second is the last seen name
  498. // in that file.
  499. typedef pair<Name, shared_ptr<Name> > IncludeInfo;
  500. vector<IncludeInfo> include_info_;
  501. bool previous_name_; // True if there was a previous name in this file
  502. // (false at the beginning or after an $INCLUDE line)
  503. public:
  504. bool complete_; // All work done.
  505. bool seen_error_; // Was there at least one error during the
  506. // load?
  507. bool warn_rfc1035_ttl_; // should warn if implicit TTL determination
  508. // from the previous RR is used.
  509. size_t rr_count_; // number of RRs successfully loaded
  510. };
  511. namespace { // begin unnamed namespace
  512. /// \brief Generate a dotted nibble sequence.
  513. ///
  514. /// This method generates a dotted nibble sequence and returns it as a
  515. /// string. The nibbles are appended from the least significant digit
  516. /// (in hex representation of \c num) to the most significant digit with
  517. /// dots ('.') to separate the digits. If \c width is non-zero and the
  518. /// dotted nibble sequence has not filled the requested width, the rest
  519. /// of the width is filled with a dotted nibble sequence of 0 nibbles.
  520. ///
  521. /// Some sample representations:
  522. ///
  523. /// num = 0x1234, width = 0
  524. /// "4.3.2.1"
  525. ///
  526. /// num = 0x1234, width = 1
  527. /// "4.3.2.1"
  528. ///
  529. /// num = 0x1234, width = 8
  530. /// "4.3.2.1"
  531. ///
  532. /// num = 0x1234, width = 9
  533. /// "4.3.2.1."
  534. ///
  535. /// num = 0x1234, width = 10
  536. /// "4.3.2.1.0"
  537. ///
  538. /// num = 0x1234, width = 11
  539. /// "4.3.2.1.0."
  540. ///
  541. /// num = 0xabcd, width = 0, uppercase = true
  542. /// "D.C.B.A"
  543. ///
  544. /// num = 0, width = 0
  545. /// "0"
  546. ///
  547. /// num = 0, width = 1
  548. /// "0"
  549. ///
  550. /// num = 0, width = 2
  551. /// "0."
  552. ///
  553. /// num = 0, width = 3
  554. /// "0.0"
  555. ///
  556. /// \param num The number for which the dotted nibble sequence should be
  557. /// generated.
  558. /// \param width The width of the generated string. This is only
  559. /// meaningful when it is larger than the dotted nibble sequence
  560. /// representation of \c num.
  561. /// \param uppercase Whether to use uppercase characters in nibble
  562. /// sequence.
  563. /// \return A string containing the dotted nibble sequence.
  564. std::string
  565. genNibbles(int num, unsigned int width, bool uppercase) {
  566. static const char *hex = "0123456789abcdef0123456789ABCDEF";
  567. std::string rstr;
  568. do {
  569. char ch = hex[(num & 0x0f) + (uppercase ? 16 : 0)];
  570. num >>= 4;
  571. rstr.push_back(ch);
  572. if (width > 0) {
  573. --width;
  574. }
  575. // If width is non zero then we need to add a label separator.
  576. // If value is non zero then we need to add another label and
  577. // that requires a label separator.
  578. if (width > 0 || num != 0) {
  579. rstr.push_back('.');
  580. if (width > 0) {
  581. --width;
  582. }
  583. }
  584. } while ((num != 0) || (width > 0));
  585. return (rstr);
  586. }
  587. } // end unnamed namespace
  588. std::string
  589. MasterLoader::MasterLoaderImpl::generateForIter(const std::string& str,
  590. const int num)
  591. {
  592. std::string rstr;
  593. for (std::string::const_iterator it = str.begin(); it != str.end();) {
  594. switch (*it) {
  595. case '$':
  596. // This is the case when the '$' character is encountered in
  597. // the LHS or RHS. A computed value is added in its place in
  598. // the generated string.
  599. ++it;
  600. if ((it != str.end()) && (*it == '$')) {
  601. rstr.push_back('$');
  602. ++it;
  603. continue;
  604. }
  605. // 'it' can be equal to str.end() here, but it is handled
  606. // correctly.
  607. if (*it != '{') {
  608. // There is no modifier (between {}), so just copy the
  609. // passed number into the generated string.
  610. rstr += boost::str(boost::format("%d") % num);
  611. } else {
  612. // There is a modifier (between {}). Parse it and handle
  613. // the various cases below.
  614. const char* scan_str =
  615. str.c_str() + std::distance(str.begin(), it);
  616. int offset = 0;
  617. unsigned int width;
  618. char base[2] = {'d', 0}; // char plus null byte
  619. const int n = sscanf(scan_str, "{%d,%u,%1[doxXnN]}",
  620. &offset, &width, base);
  621. switch (n) {
  622. case 1:
  623. // Only 1 item was matched (the offset). Copy (num +
  624. // offset) into the generated string.
  625. rstr += boost::str(boost::format("%d") % (num + offset));
  626. break;
  627. case 2: {
  628. // 2 items were matched (the offset and width). Copy
  629. // (num + offset) and format it according to the width
  630. // into the generated string.
  631. const std::string fmt =
  632. boost::str(boost::format("%%0%ud") % width);
  633. rstr += boost::str(boost::format(fmt) % (num + offset));
  634. break;
  635. }
  636. case 3:
  637. // 3 items were matched (offset, width and base).
  638. if ((base[0] == 'n') || (base[0] == 'N')) {
  639. // The base is requesting nibbles. Format it
  640. // specially (see genNibbles() documentation).
  641. rstr += genNibbles(num + offset, width, (base[0] == 'N'));
  642. } else {
  643. // The base is not requesting nibbles. Copy (num +
  644. // offset) and format it according to the width
  645. // and base into the generated string.
  646. const std::string fmt =
  647. boost::str(boost::format("%%0%u%c") % width % base[0]);
  648. rstr += boost::str(boost::format(fmt) % (num + offset));
  649. }
  650. break;
  651. default:
  652. // Any other case in the modifiers is an error.
  653. reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
  654. "Invalid $GENERATE format modifiers");
  655. return ("");
  656. }
  657. // Find the closing brace. Careful that 'it' can be equal
  658. // to str.end() here.
  659. while ((it != str.end()) && (*it != '}')) {
  660. ++it;
  661. }
  662. // Skip past the closing brace (if there is one).
  663. if (it != str.end()) {
  664. ++it;
  665. }
  666. }
  667. break;
  668. case '\\':
  669. // This is the case when the '\' character is encountered in
  670. // the LHS or RHS. The '\' and the following character are
  671. // copied as-is into the generated string. This is usually
  672. // used for escaping the $ character.
  673. rstr.push_back(*it);
  674. ++it;
  675. if (it == str.end()) {
  676. continue;
  677. }
  678. rstr.push_back(*it);
  679. ++it;
  680. break;
  681. default:
  682. // This is the default case that handles all other
  683. // characters. They are copied as-is into the generated
  684. // string.
  685. rstr.push_back(*it);
  686. ++it;
  687. break;
  688. }
  689. }
  690. return (rstr);
  691. }
  692. void
  693. MasterLoader::MasterLoaderImpl::doGenerate() {
  694. // Parse the range token
  695. const MasterToken& range_token = lexer_.getNextToken(MasterToken::STRING);
  696. if (range_token.getType() != MasterToken::STRING) {
  697. reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
  698. "Invalid $GENERATE syntax");
  699. return;
  700. }
  701. const std::string range = range_token.getString();
  702. // Parse the LHS token
  703. const MasterToken& lhs_token = lexer_.getNextToken(MasterToken::STRING);
  704. if (lhs_token.getType() != MasterToken::STRING) {
  705. reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
  706. "Invalid $GENERATE syntax");
  707. return;
  708. }
  709. const std::string lhs = lhs_token.getString();
  710. // Parse the TTL, RR class and RR type tokens. Note that TTL and RR
  711. // class may come in any order, or may be missing (either or
  712. // both). If TTL is missing, we expect that it was either specified
  713. // explicitly using $TTL, or is implicitly known from a previous RR,
  714. // or that this is the SOA RR from which the MINIMUM field is
  715. // used. It's unlikely that $GENERATE will be used with an SOA RR,
  716. // but it's possible. The parsing happens within the parseRRParams()
  717. // helper method which is called below.
  718. const MasterToken& param_token = lexer_.getNextToken(MasterToken::STRING);
  719. if (param_token.getType() != MasterToken::STRING) {
  720. reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
  721. "Invalid $GENERATE syntax");
  722. return;
  723. }
  724. bool explicit_ttl = false;
  725. const RRType rrtype = parseRRParams(explicit_ttl, param_token);
  726. // Parse the RHS token. It can be a quoted string.
  727. const MasterToken& rhs_token = lexer_.getNextToken(MasterToken::QSTRING);
  728. if ((rhs_token.getType() != MasterToken::QSTRING) &&
  729. (rhs_token.getType() != MasterToken::STRING))
  730. {
  731. reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
  732. "Invalid $GENERATE syntax");
  733. return;
  734. }
  735. const std::string rhs = rhs_token.getString();
  736. // Range can be one of two forms: start-stop or start-stop/step. If
  737. // the first form is used, then step is set to 1. All of start, stop
  738. // and step must be positive.
  739. unsigned int start;
  740. unsigned int stop;
  741. unsigned int step;
  742. const int n = sscanf(range.c_str(), "%u-%u/%u", &start, &stop, &step);
  743. if ((n < 2) || (stop < start)) {
  744. reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
  745. "$GENERATE: invalid range: " + range);
  746. return;
  747. }
  748. if (n == 2) {
  749. step = 1;
  750. }
  751. // Generate and add the records.
  752. for (int i = start; i <= stop; i += step) {
  753. // Get generated strings for LHS and RHS. LHS goes to form the
  754. // name, RHS goes to form the RDATA of the RR.
  755. const std::string generated_name = generateForIter(lhs, i);
  756. const std::string generated_rdata = generateForIter(rhs, i);
  757. if (generated_name.empty() || generated_rdata.empty()) {
  758. // The error should have been sent to the callbacks already
  759. // by generateForIter().
  760. reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
  761. "$GENERATE error");
  762. return;
  763. }
  764. // generateForIter() can return a string with a trailing '.' in
  765. // case of a nibble representation. So we cannot use the
  766. // relative Name constructor. We use concatenate() which is
  767. // expensive, but keeps the generated LHS-based Name within the
  768. // active origin.
  769. last_name_.reset
  770. (new Name(Name(generated_name).concatenate(active_origin_)));
  771. previous_name_ = true;
  772. const rdata::RdataPtr rdata =
  773. rdata::createRdata(rrtype, zone_class_, generated_rdata);
  774. // In case we get NULL, it means there was error creating the
  775. // Rdata. The errors should have been reported by callbacks_
  776. // already. We need to decide if we want to continue or not.
  777. if (rdata) {
  778. add_callback_(*last_name_, zone_class_, rrtype,
  779. getCurrentTTL(explicit_ttl, rrtype, rdata),
  780. rdata);
  781. // Good, we added another one
  782. ++rr_count_;
  783. } else {
  784. seen_error_ = true;
  785. if (!many_errors_) {
  786. ok_ = false;
  787. complete_ = true;
  788. // We don't have the exact error here, but it was
  789. // reported by the error callback.
  790. isc_throw(MasterLoaderError, "Invalid RR data");
  791. }
  792. }
  793. }
  794. }
  795. MasterToken
  796. MasterLoader::MasterLoaderImpl::handleInitialToken() {
  797. const MasterToken& initial_token =
  798. lexer_.getNextToken(MasterLexer::QSTRING | MasterLexer::INITIAL_WS);
  799. // The most likely case is INITIAL_WS, and then string/qstring. We
  800. // handle them first.
  801. if (initial_token.getType() == MasterToken::INITIAL_WS) {
  802. const MasterToken& next_token = lexer_.getNextToken();
  803. if (next_token.getType() == MasterToken::END_OF_LINE) {
  804. return (next_token); // blank line
  805. } else if (next_token.getType() == MasterToken::END_OF_FILE) {
  806. lexer_.ungetToken(); // handle it in the next iteration.
  807. eatUntilEOL(true); // effectively warn about the unexpected EOF.
  808. return (MasterToken(MasterToken::END_OF_LINE));
  809. }
  810. // This means the same name as previous.
  811. if (last_name_.get() == NULL) {
  812. isc_throw(InternalException, "No previous name to use in "
  813. "place of initial whitespace");
  814. } else if (!previous_name_) {
  815. callbacks_.warning(lexer_.getSourceName(), lexer_.getSourceLine(),
  816. "Owner name omitted around $INCLUDE, the result "
  817. "might not be as expected");
  818. }
  819. return (next_token);
  820. } else if (initial_token.getType() == MasterToken::STRING ||
  821. initial_token.getType() == MasterToken::QSTRING) {
  822. // If it is name (or directive), handle it.
  823. const MasterToken::StringRegion&
  824. name_string(initial_token.getStringRegion());
  825. if (name_string.len > 0 && name_string.beg[0] == '$') {
  826. // This should have either thrown (and the error handler
  827. // will read up until the end of line) or read until the
  828. // end of line.
  829. // Exclude the $ from the string on this point.
  830. handleDirective(name_string.beg + 1, name_string.len - 1);
  831. // So, get to the next line, there's nothing more interesting
  832. // in this one.
  833. return (MasterToken(MasterToken::END_OF_LINE));
  834. }
  835. // This should be an RR, starting with an owner name. Construct the
  836. // name, and some string token should follow.
  837. last_name_.reset(new Name(name_string.beg, name_string.len,
  838. &active_origin_));
  839. previous_name_ = true;
  840. return (lexer_.getNextToken(MasterToken::STRING));
  841. }
  842. switch (initial_token.getType()) { // handle less common cases
  843. case MasterToken::END_OF_FILE:
  844. if (!popSource()) {
  845. return (initial_token);
  846. } else {
  847. // We try to read a token from the popped source
  848. // So continue to the next line of that source, but first, make
  849. // sure the source is at EOL
  850. eatUntilEOL(true);
  851. return (MasterToken(MasterToken::END_OF_LINE));
  852. }
  853. case MasterToken::END_OF_LINE:
  854. return (initial_token); // empty line
  855. case MasterToken::ERROR:
  856. // Error token here.
  857. isc_throw(InternalException, initial_token.getErrorText());
  858. default:
  859. // Some other token (what could that be?)
  860. isc_throw(InternalException, "Parser got confused (unexpected "
  861. "token " << initial_token.getType() << ")");
  862. }
  863. }
  864. bool
  865. MasterLoader::MasterLoaderImpl::loadIncremental(size_t count_limit) {
  866. if (count_limit == 0) {
  867. isc_throw(isc::InvalidParameter, "Count limit set to 0");
  868. }
  869. if (complete_) {
  870. isc_throw(isc::InvalidOperation,
  871. "Trying to load when already loaded");
  872. }
  873. if (!initialized_) {
  874. pushSource(master_file_, active_origin_);
  875. }
  876. size_t count = 0;
  877. while (ok_ && count < count_limit) {
  878. try {
  879. const MasterToken next_token = handleInitialToken();
  880. if (next_token.getType() == MasterToken::END_OF_FILE) {
  881. return (true); // we are done
  882. } else if (next_token.getType() == MasterToken::END_OF_LINE) {
  883. continue; // nothing more to do in this line
  884. }
  885. // We are going to parse an RR, have known the owner name,
  886. // and are now seeing the next string token in the rest of the RR.
  887. assert(next_token.getType() == MasterToken::STRING);
  888. bool explicit_ttl = false;
  889. const RRType rrtype = parseRRParams(explicit_ttl, next_token);
  890. // TODO: Check if it is SOA, it should be at the origin.
  891. const rdata::RdataPtr rdata =
  892. rdata::createRdata(rrtype, zone_class_, lexer_,
  893. &active_origin_, options_, callbacks_);
  894. // In case we get NULL, it means there was error creating
  895. // the Rdata. The errors should have been reported by
  896. // callbacks_ already. We need to decide if we want to continue
  897. // or not.
  898. if (rdata) {
  899. add_callback_(*last_name_, zone_class_, rrtype,
  900. getCurrentTTL(explicit_ttl, rrtype, rdata),
  901. rdata);
  902. // Good, we loaded another one
  903. ++count;
  904. ++rr_count_;
  905. } else {
  906. seen_error_ = true;
  907. if (!many_errors_) {
  908. ok_ = false;
  909. complete_ = true;
  910. // We don't have the exact error here, but it was reported
  911. // by the error callback.
  912. isc_throw(MasterLoaderError, "Invalid RR data");
  913. }
  914. }
  915. } catch (const isc::dns::DNSTextError& e) {
  916. reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
  917. e.what());
  918. eatUntilEOL(false);
  919. } catch (const MasterLexer::ReadError& e) {
  920. reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
  921. e.what());
  922. eatUntilEOL(false);
  923. } catch (const MasterLexer::LexerError& e) {
  924. reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
  925. e.what());
  926. eatUntilEOL(false);
  927. } catch (const InternalException& e) {
  928. reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
  929. e.what());
  930. eatUntilEOL(false);
  931. }
  932. }
  933. // When there was a fatal error and ok is false, we say we are done.
  934. return (!ok_);
  935. }
  936. MasterLoader::MasterLoader(const char* master_file,
  937. const Name& zone_origin,
  938. const RRClass& zone_class,
  939. const MasterLoaderCallbacks& callbacks,
  940. const AddRRCallback& add_callback,
  941. Options options)
  942. {
  943. if (add_callback.empty()) {
  944. isc_throw(isc::InvalidParameter, "Empty add RR callback");
  945. }
  946. impl_ = new MasterLoaderImpl(master_file, zone_origin,
  947. zone_class, callbacks, add_callback, options);
  948. }
  949. MasterLoader::MasterLoader(std::istream& stream,
  950. const Name& zone_origin,
  951. const RRClass& zone_class,
  952. const MasterLoaderCallbacks& callbacks,
  953. const AddRRCallback& add_callback,
  954. Options options)
  955. {
  956. if (add_callback.empty()) {
  957. isc_throw(isc::InvalidParameter, "Empty add RR callback");
  958. }
  959. auto_ptr<MasterLoaderImpl> impl(new MasterLoaderImpl("", zone_origin,
  960. zone_class, callbacks,
  961. add_callback,
  962. options));
  963. impl->pushStreamSource(stream);
  964. impl_ = impl.release();
  965. }
  966. MasterLoader::~MasterLoader() {
  967. delete impl_;
  968. }
  969. bool
  970. MasterLoader::loadIncremental(size_t count_limit) {
  971. const bool result = impl_->loadIncremental(count_limit);
  972. impl_->complete_ = result;
  973. return (result);
  974. }
  975. bool
  976. MasterLoader::loadedSucessfully() const {
  977. return (impl_->complete_ && !impl_->seen_error_);
  978. }
  979. size_t
  980. MasterLoader::getSize() const {
  981. return (impl_->getSize());
  982. }
  983. size_t
  984. MasterLoader::getPosition() const {
  985. return (impl_->getPosition());
  986. }
  987. } // end namespace dns
  988. } // end namespace isc