master_loader.cc 41.7 KB
Newer Older
1
// Copyright (C) 2012-2016 Internet Systems Consortium, Inc. ("ISC")
2
//
3 4 5
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6

7 8
#include <config.h>

9
#include <dns/master_loader.h>
10 11
#include <dns/master_lexer.h>
#include <dns/name.h>
12
#include <dns/rdataclass.h>
13 14 15
#include <dns/rrttl.h>
#include <dns/rrclass.h>
#include <dns/rrtype.h>
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
16
#include <dns/rdata.h>
17

18
#include <boost/format.hpp>
19
#include <boost/algorithm/string/predicate.hpp> // for iequals
20 21
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
22

23
#include <string>
24
#include <memory>
25
#include <vector>
26

27
#include <cstdio> // for sscanf()
28

29
using std::string;
30
using std::unique_ptr;
31 32
using std::vector;
using std::pair;
33
using boost::algorithm::iequals;
34
using boost::shared_ptr;
35 36 37 38

namespace isc {
namespace dns {

39 40
namespace {

41 42 43 44 45 46 47 48 49 50
// An internal exception, used to control the code flow in case of errors.
// It is thrown during the loading and caught later, not to be propagated
// outside of the file.
class InternalException : public isc::Exception {
public:
    InternalException(const char* filename, size_t line, const char* what) :
        Exception(filename, line, what)
    {}
};

Jelte Jansen's avatar
Jelte Jansen committed
51
} // end unnamed namespace
52

53 54 55 56 57
/// \brief Private implementation class for the \c MasterLoader
///
/// This class is used internally by the \c MasterLoader and is not
/// publicly visible. It is present to avoid polluting the public API
/// with internal implementation details of the \c MasterLoader.
58
// cppcheck-suppress noConstructor
59 60
class MasterLoader::MasterLoaderImpl {
public:
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    /// \brief Constructor.
    ///
    /// \param master_file Path to the file to load.
    /// \param zone_origin The origin of zone to be expected inside
    ///     the master file. Currently unused, but it is expected to
    ///     be used for some validation.
    /// \param zone_class The class of zone to be expected inside the
    ///     master file.
    /// \param callbacks The callbacks by which it should report problems.
    ///     Usually, the callback carries a filename and line number of the
    ///     input where the problem happens. There's a special case of empty
    ///     filename and zero line in case the opening of the top-level master
    ///     file fails.
    /// \param add_callback The callback which would be called with each
    ///     loaded RR.
    /// \param options Options for the parsing, which is bitwise-or of
    ///     the Options values or DEFAULT. If the MANY_ERRORS option is
    ///     included, the parser tries to continue past errors. If it
    ///     is not included, it stops at first encountered error.
    /// \throw std::bad_alloc when there's not enough memory.
81 82
    MasterLoaderImpl(const char* master_file,
                     const Name& zone_origin,
83 84
                     const RRClass& zone_class,
                     const MasterLoaderCallbacks& callbacks,
85
                     const AddRRCallback& add_callback,
86 87 88
                     MasterLoader::Options options) :
        lexer_(),
        zone_origin_(zone_origin),
89
        active_origin_(zone_origin),
90 91
        zone_class_(zone_class),
        callbacks_(callbacks),
92
        add_callback_(add_callback),
93 94 95
        options_(options),
        master_file_(master_file),
        initialized_(false),
96
        ok_(true),
97
        many_errors_((options & MANY_ERRORS) != 0),
98
        previous_name_(false),
99
        complete_(false),
100
        seen_error_(false),
101 102
        warn_rfc1035_ttl_(true),
        rr_count_(0)
103 104
    {}

105 106 107 108 109 110 111 112 113 114
    /// \brief Wrapper around \c MasterLexer::pushSource() (file version)
    ///
    /// This method is used as a wrapper around the lexer's
    /// \c pushSource() to also save the current origin and the last
    /// seen name (to be restored upon \c popSource()). It also calls
    /// \c pushSource(). See \c doInclude() implementation for more
    /// details.
    ///
    /// \param filename Path to the file to push as a new source.
    /// \param current_origin The current origin name to save.
115
    void pushSource(const std::string& filename, const Name& current_origin) {
116 117
        std::string error;
        if (!lexer_.pushSource(filename.c_str(), &error)) {
118
            if (initialized_) {
119
                isc_throw(InternalException, error.c_str());
120 121 122 123 124
            } else {
                // Top-level file
                reportError("", 0, error);
                ok_ = false;
            }
125
        }
126
        // Store the current status, so we can recover it upon popSource
127
        include_info_.push_back(IncludeInfo(current_origin, last_name_));
128
        initialized_ = true;
129
        previous_name_ = false;
130 131
    }

132 133 134 135 136 137
    /// \brief Wrapper around \c MasterLexer::pushSource() (stream version)
    ///
    /// Similar to \c pushSource(). This method need not save the
    /// current origin as it is not used with $INCLUDE processing.
    ///
    /// \param stream The input stream to use as a new source.
138 139 140 141 142
    void pushStreamSource(std::istream& stream) {
        lexer_.pushSource(stream);
        initialized_ = true;
    }

143 144 145
    /// \brief Implementation of \c MasterLoader::loadIncremental()
    ///
    /// See \c MasterLoader::loadIncremental() for details.
146 147
    bool loadIncremental(size_t count_limit);

148 149
    /// \brief Return the total size of the input sources pushed so
    /// far. See \c MasterLexer::getTotalSourceSize().
150
    size_t getSize() const { return (lexer_.getTotalSourceSize()); }
151 152 153

    /// \brief Return the line number being parsed in the pushed input
    /// sources. See \c MasterLexer::getPosition().
154 155
    size_t getPosition() const { return (lexer_.getPosition()); }

156
private:
157 158 159 160
    /// \brief Report an error using the callbacks that were supplied
    /// during \c MasterLoader construction. Note that this method also
    /// throws \c MasterLoaderError exception if necessary, so the
    /// caller need not throw it.
161 162 163 164 165 166 167 168 169 170 171 172 173 174
    void reportError(const std::string& filename, size_t line,
                     const std::string& reason)
    {
        seen_error_ = true;
        callbacks_.error(filename, line, reason);
        if (!many_errors_) {
            // In case we don't have the lenient mode, every error is fatal
            // and we throw
            ok_ = false;
            complete_ = true;
            isc_throw(MasterLoaderError, reason.c_str());
        }
    }

175 176 177 178 179 180
    /// \brief Wrapper around \c MasterLexer::popSource()
    ///
    /// This method is used as a wrapper around the lexer's
    /// \c popSource() to also restore the current origin and the last
    /// seen name (at time of push). It also calls \c popSource(). See
    /// \c doInclude() implementation for more details.
181
    bool popSource() {
182
        if (lexer_.getSourceCount() == 1) {
183 184
            return (false);
        }
185
        lexer_.popSource();
186 187 188 189 190 191 192 193 194
        // Restore original origin and last seen name

        // We move in tandem, there's an extra item included during the
        // initialization, so we can never run out of them
        assert(!include_info_.empty());
        const IncludeInfo& info(include_info_.back());
        active_origin_ = info.first;
        last_name_ = info.second;
        include_info_.pop_back();
195
        previous_name_ = false;
196
        return (true);
197 198
    }

199
    /// \brief Get a string token. Handle it as error if it is not string.
200
    const string getString() {
201
        lexer_.getNextToken(MasterToken::STRING).getString(string_token_);
202
        return (string_token_);
203 204
    }

205 206 207 208 209 210 211 212 213 214 215 216 217 218
    /// \brief Parse the initial token at the beginning of a line in a
    /// master file (or stream).
    ///
    /// A helper method of \c loadIncremental(), parsing the first token
    /// of a new line.  If it looks like an RR, detect its owner name
    /// and return a string token for the next field of the RR.
    ///
    /// Otherwise, return either \c END_OF_LINE or \c END_OF_FILE token
    /// depending on whether the loader continues to the next line or
    /// completes the load, respectively.  Other corner cases including
    /// $-directive handling is done here.
    ///
    /// For unexpected errors, it throws an exception, which will be
    /// handled in \c loadIncremental().
219
    MasterToken handleInitialToken();
220 221 222 223 224 225 226 227

    /// \brief Helper method for \c doGenerate().
    ///
    /// This is a helper method for \c doGenerate() that processes the
    /// LHS or RHS for a single iteration in the range that is requested
    /// by the $GENERATE directive and returns a generated string (that
    /// is used to build a name (LHS) or RDATA (RHS) for an RR). See the
    /// commented implementation for details.
228
    std::string generateForIter(const std::string& str, const int it);
229 230 231 232

    /// \brief Process the $GENERATE directive.
    ///
    /// See the commented implementation for details.
233
    void doGenerate();
234

235
    /// \brief Process the $ORIGIN directive.
236 237 238 239 240
    void doOrigin(bool is_optional) {
        // Parse and create the new origin. It is relative to the previous
        // one.
        const MasterToken&
            name_tok(lexer_.getNextToken(MasterToken::QSTRING, is_optional));
241 242 243

        if (name_tok.getType() == MasterToken::QSTRING ||
            name_tok.getType() == MasterToken::STRING) {
244

245 246 247 248
            const MasterToken::StringRegion&
                name_string(name_tok.getStringRegion());
            active_origin_ = Name(name_string.beg, name_string.len,
                                  &active_origin_);
249 250 251 252 253 254 255
            if (name_string.len > 0 &&
                name_string.beg[name_string.len - 1] != '.') {
                callbacks_.warning(lexer_.getSourceName(),
                                   lexer_.getSourceLine(),
                                   "The new origin is relative, did you really"
                                   " mean " + active_origin_.toText() + "?");
            }
256
        } else {
257 258 259 260 261 262 263
            // If it is not optional, we must not get anything but
            // a string token.
            assert(is_optional);

            // We return the newline there. This is because we want to
            // behave the same if there is or isn't the name, leaving the
            // newline there.
264 265
            lexer_.ungetToken();
        }
266 267
    }

268
    /// \brief Process the $INCLUDE directive.
269 270 271 272 273 274
    void doInclude() {
        // First, get the filename to include
        const string
            filename(lexer_.getNextToken(MasterToken::QSTRING).getString());

        // There optionally can be an origin, that applies before the include.
275 276 277 278 279 280 281 282 283
        // We need to save the currently active origin before calling
        // doOrigin(), because it would update active_origin_ while we need
        // to pass the active origin before recognizing the new origin to
        // pushSource.  Note: RFC 1035 is not really clear on this: it reads
        // "regardless of changes... within the included file", but the new
        // origin is not really specified "within the included file".
        // Nevertheless, this behavior is probably more likely to be the
        // intent of the RFC, and it's compatible with BIND 9.
        const Name current_origin = active_origin_;
284
        doOrigin(true);
285

286
        pushSource(filename, current_origin);
287 288
    }

289 290 291 292 293 294 295 296 297 298
    /// \brief Parse RR fields (TTL, CLASS and TYPE).
    ///
    /// A helper method for \c loadIncremental(). It parses part of an
    /// RR until it finds the RR type field.  If TTL or RR class is
    /// specified before the RR type, it also recognizes and validates
    /// them.
    ///
    /// \param explicit_ttl will be set to true if this method finds a
    /// valid TTL field.
    /// \param rrparam_token Pass the current (parsed) token here.
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
299
    RRType parseRRParams(bool& explicit_ttl, MasterToken rrparam_token) {
300 301 302 303 304 305 306
        // Find TTL, class and type.  Both TTL and class are
        // optional and may occur in any order if they exist. TTL
        // and class come before type which must exist.
        //
        // [<TTL>] [<class>] <type> <RDATA>
        // [<class>] [<TTL>] <type> <RDATA>

Mukund Sivaraman's avatar
Mukund Sivaraman committed
307 308
        // named-signzone outputs TTL first, so try parsing it in order
        // first.
309 310 311 312 313 314 315 316
        if (setCurrentTTL(rrparam_token.getString())) {
            explicit_ttl = true;
            rrparam_token = lexer_.getNextToken(MasterToken::STRING);
        } else {
            // If it's not a TTL here, continue and try again
            // after the RR class below.
        }

317 318
        boost::scoped_ptr<RRClass> rrclass
            (RRClass::createFromText(rrparam_token.getString()));
319
        if (rrclass) {
320
            if (*rrclass != zone_class_) {
321 322
                isc_throw(InternalException, "Class mismatch: " << *rrclass <<
                          " vs. " << zone_class_);
323
            }
324 325 326
            rrparam_token = lexer_.getNextToken(MasterToken::STRING);
        }

Mukund Sivaraman's avatar
Mukund Sivaraman committed
327 328
        // If we couldn't parse TTL earlier in the stream (above), try
        // again at current location.
329
        if (!explicit_ttl && setCurrentTTL(rrparam_token.getString())) {
330 331 332
            explicit_ttl = true;
            rrparam_token = lexer_.getNextToken(MasterToken::STRING);
        }
Mukund Sivaraman's avatar
Mukund Sivaraman committed
333 334

        // Return the current string token's value as the RRType.
335 336 337
        return (RRType(rrparam_token.getString()));
    }

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
    /// \brief Check and limit TTL to maximum value.
    ///
    /// Upper limit check when recognizing a specific TTL value from the
    /// zone file ($TTL, the RR's TTL field, or the SOA minimum).  RFC2181
    /// Section 8 limits the range of TTL values to 2^31-1 (0x7fffffff),
    /// and prohibits transmitting a TTL field exceeding this range.  We
    /// guarantee that by limiting the value at the time of zone
    /// parsing/loading, following what BIND 9 does.  Resetting it to 0
    /// at this point may not be exactly what the RFC states (depending on
    /// the meaning of 'received'), but the end result would be the same (i.e.,
    /// the guarantee on transmission).  Again, we follow the BIND 9's behavior
    /// here.
    ///
    /// \param ttl the TTL to check. If it is larger than the maximum
    /// allowed, it is set to 0.
    /// \param post_parsing should be true iff this method is called
    /// after parsing the entire RR and the lexer is positioned at the
    /// next line. It's just for calculating the accurate source line
    /// when callback is necessary.
357
    void limitTTL(RRTTL& ttl, bool post_parsing) {
358
        if (ttl > RRTTL::MAX_TTL()) {
359 360 361 362 363 364 365 366 367
            const size_t src_line = lexer_.getSourceLine() -
                (post_parsing ? 1 : 0);
            callbacks_.warning(lexer_.getSourceName(), src_line,
                               "TTL " + ttl.toText() + " > MAXTTL, "
                               "setting to 0 per RFC2181");
            ttl = RRTTL(0);
        }
    }

368 369 370 371 372
    /// \brief Set/reset the default TTL.
    ///
    /// This should be from either $TTL or SOA minimum TTL (it's the
    /// caller's responsibility; this method doesn't care about where it
    /// comes from). See \c limitTTL() for parameter post_parsing.
373
    void setDefaultTTL(const RRTTL& ttl, bool post_parsing) {
374
        assignTTL(default_ttl_, ttl);
375
        limitTTL(*default_ttl_, post_parsing);
376 377
    }

378 379 380 381 382 383 384 385 386
    /// \brief Try to set/reset the current TTL from candidate TTL text.
    ///
    /// It's possible it that the text does not actually represent a TTL
    /// (which is not immediately considered an error). Returns \c true
    /// iff it's recognized as a valid TTL (and only in which case the
    /// current TTL is set).
    ///
    /// \param ttl_txt The text to parse as a TTL.
    /// \return true if a TTL was parsed (and set as the current TTL).
387
    bool setCurrentTTL(const string& ttl_txt) {
388 389 390
        // We use the factory version instead of RRTTL constructor as we
        // need to expect cases where ttl_txt does not actually represent a TTL
        // but an RR class or type.
391 392 393
        RRTTL* rrttl = RRTTL::createFromText(ttl_txt);
        if (rrttl) {
            current_ttl_.reset(rrttl);
394
            limitTTL(*current_ttl_, false);
395 396
            return (true);
        }
397
        return (false);
398 399
    }

400 401 402 403 404 405 406 407 408
    /// \brief Determine the TTL of the current RR based on the given
    /// parsing context.
    ///
    /// \c explicit_ttl is true iff the TTL is explicitly specified for that RR
    /// (in which case current_ttl_ is set to that TTL).
    /// \c rrtype is the type of the current RR, and \c rdata is its RDATA.  They
    /// only matter if the type is SOA and no available TTL is known.  In this
    /// case the minimum TTL of the SOA will be used as the TTL of that SOA
    /// and the default TTL for subsequent RRs.
409 410
    const RRTTL& getCurrentTTL(bool explicit_ttl, const RRType& rrtype,
                               const rdata::ConstRdataPtr& rdata) {
411 412 413 414 415
        // We've completed parsing the full of RR, and the lexer is already
        // positioned at the next line.  If we need to call callback,
        // we need to adjust the line number.
        const size_t current_line = lexer_.getSourceLine() - 1;

416 417
        if (!current_ttl_ && !default_ttl_) {
            if (rrtype == RRType::SOA()) {
418
                callbacks_.warning(lexer_.getSourceName(), current_line,
419 420 421 422 423
                                   "no TTL specified; "
                                   "using SOA MINTTL instead");
                const uint32_t ttl_val =
                    dynamic_cast<const rdata::generic::SOA&>(*rdata).
                    getMinimum();
424
                setDefaultTTL(RRTTL(ttl_val), true);
425
                assignTTL(current_ttl_, *default_ttl_);
426 427 428 429 430 431
            } else {
                // On catching the exception we'll try to reach EOL again,
                // so we need to unget it now.
                lexer_.ungetToken();
                throw InternalException(__FILE__, __LINE__,
                                        "no TTL specified; load rejected");
432 433
            }
        } else if (!explicit_ttl && default_ttl_) {
434
            assignTTL(current_ttl_, *default_ttl_);
435 436 437
        } else if (!explicit_ttl && warn_rfc1035_ttl_) {
            // Omitted (class and) TTL values are default to the last
            // explicitly stated values (RFC 1035, Sec. 5.1).
438
            callbacks_.warning(lexer_.getSourceName(), current_line,
439 440
                               "using RFC1035 TTL semantics; default to the "
                               "last explicitly stated TTL");
441 442 443 444 445 446
            warn_rfc1035_ttl_ = false; // we only warn about this once
        }
        assert(current_ttl_);
        return (*current_ttl_);
    }

447 448 449 450
    /// \brief Handle a $DIRECTIVE
    ///
    /// This method is called when a $DIRECTIVE is encountered in the
    /// input stream.
451
    void handleDirective(const char* directive, size_t length) {
452
        if (iequals(directive, "INCLUDE")) {
453
            doInclude();
454
        } else if (iequals(directive, "ORIGIN")) {
455 456
            doOrigin(false);
            eatUntilEOL(true);
457 458 459
        } else if (iequals(directive, "GENERATE")) {
            doGenerate();
            eatUntilEOL(true);
460
        } else if (iequals(directive, "TTL")) {
461
            setDefaultTTL(RRTTL(getString()), false);
462
            eatUntilEOL(true);
463 464 465 466 467 468
        } else {
            isc_throw(InternalException, "Unknown directive '" <<
                      string(directive, directive + length) << "'");
        }
    }

469
    /// \brief Skip tokens until end-of-line.
470 471 472 473 474 475 476 477
    void eatUntilEOL(bool reportExtra) {
        // We want to continue. Try to read until the end of line
        for (;;) {
            const MasterToken& token(lexer_.getNextToken());
            switch (token.getType()) {
                case MasterToken::END_OF_FILE:
                    callbacks_.warning(lexer_.getSourceName(),
                                       lexer_.getSourceLine(),
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
478
                                       "File does not end with newline");
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
                    // We don't pop here. The End of file will stay there,
                    // and we'll handle it in the next iteration of
                    // loadIncremental properly.
                    return;
                case MasterToken::END_OF_LINE:
                    // Found the end of the line. Good.
                    return;
                default:
                    // Some other type of token.
                    if (reportExtra) {
                        reportExtra = false;
                        reportError(lexer_.getSourceName(),
                                    lexer_.getSourceLine(),
                                    "Extra tokens at the end of line");
                    }
                    break;
            }
        }
    }

499 500 501 502 503 504 505 506 507 508 509
    /// \brief Assign the right RRTTL's value to the left RRTTL. If one
    /// doesn't exist in the scoped_ptr, make a new RRTTL copy of the
    /// right argument.
    static void assignTTL(boost::scoped_ptr<RRTTL>& left, const RRTTL& right) {
        if (!left) {
            left.reset(new RRTTL(right));
        } else {
            *left = right;
        }
    }

510 511
private:
    MasterLexer lexer_;
512
    const Name zone_origin_;
513 514
    Name active_origin_; // The origin used during parsing
                         // (modifiable by $ORIGIN)
515
    shared_ptr<Name> last_name_; // Last seen name (for INITIAL_WS handling)
516 517
    const RRClass zone_class_;
    MasterLoaderCallbacks callbacks_;
518
    const AddRRCallback add_callback_;
519 520 521
    boost::scoped_ptr<RRTTL> default_ttl_; // Default TTL of RRs used when
                                           // unspecified.  If NULL no default
                                           // is known.
522 523 524 525
    boost::scoped_ptr<RRTTL> current_ttl_; // The TTL used most recently.
                                           // Initially unset. Once set
                                           // always stores a valid
                                           // RRTTL.
JINMEI Tatuya's avatar
JINMEI Tatuya committed
526
    const MasterLoader::Options options_;
527
    const std::string master_file_;
528
    std::string string_token_;
529
    bool initialized_;
530 531 532
    bool ok_;                   // Is it OK to continue loading?
    const bool many_errors_;    // Are many errors allowed (or should we abort
                                // on the first)
533 534 535 536 537
    // Some info about the outer files from which we include.
    // The first one is current origin, the second is the last seen name
    // in that file.
    typedef pair<Name, shared_ptr<Name> > IncludeInfo;
    vector<IncludeInfo> include_info_;
538 539
    bool previous_name_; // True if there was a previous name in this file
                         // (false at the beginning or after an $INCLUDE line)
540

541
public:
542 543 544
    bool complete_;             // All work done.
    bool seen_error_;           // Was there at least one error during the
                                // load?
545 546
    bool warn_rfc1035_ttl_;     // should warn if implicit TTL determination
                                // from the previous RR is used.
547
    size_t rr_count_;    // number of RRs successfully loaded
548 549
};

550 551
namespace { // begin unnamed namespace

552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
/// \brief Generate a dotted nibble sequence.
///
/// This method generates a dotted nibble sequence and returns it as a
/// string. The nibbles are appended from the least significant digit
/// (in hex representation of \c num) to the most significant digit with
/// dots ('.') to separate the digits. If \c width is non-zero and the
/// dotted nibble sequence has not filled the requested width, the rest
/// of the width is filled with a dotted nibble sequence of 0 nibbles.
///
/// Some sample representations:
///
/// num = 0x1234, width = 0
/// "4.3.2.1"
///
/// num = 0x1234, width = 1
/// "4.3.2.1"
///
/// num = 0x1234, width = 8
/// "4.3.2.1"
///
/// num = 0x1234, width = 9
/// "4.3.2.1."
///
/// num = 0x1234, width = 10
/// "4.3.2.1.0"
///
/// num = 0x1234, width = 11
/// "4.3.2.1.0."
///
/// num = 0xabcd, width = 0, uppercase = true
/// "D.C.B.A"
///
584 585 586 587 588 589 590 591 592 593 594 595
/// num = 0, width = 0
/// "0"
///
/// num = 0, width = 1
/// "0"
///
/// num = 0, width = 2
/// "0."
///
/// num = 0, width = 3
/// "0.0"
///
596 597
/// \param num The number for which the dotted nibble sequence should be
/// generated.
598 599 600
/// \param width The width of the generated string. This is only
/// meaningful when it is larger than the dotted nibble sequence
/// representation of \c num.
601 602 603
/// \param uppercase Whether to use uppercase characters in nibble
/// sequence.
/// \return A string containing the dotted nibble sequence.
604
std::string
605
genNibbles(int num, unsigned int width, bool uppercase) {
606 607 608 609
    static const char *hex = "0123456789abcdef0123456789ABCDEF";
    std::string rstr;

    do {
610 611
        char ch = hex[(num & 0x0f) + (uppercase ? 16 : 0)];
        num >>= 4;
612 613 614 615 616 617
        rstr.push_back(ch);

        if (width > 0) {
            --width;
        }

618
        // If width is non zero then we need to add a label separator.
619
        // If value is non zero then we need to add another label and
620
        // that requires a label separator.
621
        if (width > 0 || num != 0) {
622 623 624 625 626 627
            rstr.push_back('.');

            if (width > 0) {
                --width;
            }
        }
628
    } while ((num != 0) || (width > 0));
629 630 631 632 633 634

    return (rstr);
}

} // end unnamed namespace

635 636
std::string
MasterLoader::MasterLoaderImpl::generateForIter(const std::string& str,
637
                                                const int num)
638 639 640 641 642
{
  std::string rstr;

  for (std::string::const_iterator it = str.begin(); it != str.end();) {
      switch (*it) {
643
      case '$':
Mukund Sivaraman's avatar
Mukund Sivaraman committed
644 645 646
          // This is the case when the '$' character is encountered in
          // the LHS or RHS. A computed value is added in its place in
          // the generated string.
647 648 649 650 651 652 653
          ++it;
          if ((it != str.end()) && (*it == '$')) {
              rstr.push_back('$');
              ++it;
              continue;
          }

Francis Dupont's avatar
Francis Dupont committed
654 655
          // The str.end() check is required.
          if ((it == str.end()) || (*it != '{')) {
Mukund Sivaraman's avatar
Mukund Sivaraman committed
656 657
              // There is no modifier (between {}), so just copy the
              // passed number into the generated string.
658
              rstr += boost::str(boost::format("%d") % num);
659
          } else {
Mukund Sivaraman's avatar
Mukund Sivaraman committed
660 661
              // There is a modifier (between {}). Parse it and handle
              // the various cases below.
662 663
              const char* scan_str =
                  str.c_str() + std::distance(str.begin(), it);
664
              int offset = 0;
665
              unsigned int width;
666
              char base[2] = {'d', 0}; // char plus null byte
667
              // cppcheck-suppress invalidscanf_libc
668
              const int n = sscanf(scan_str, "{%d,%u,%1[doxXnN]}",
669
                                   &offset, &width, base);
670 671
              switch (n) {
              case 1:
Mukund Sivaraman's avatar
Mukund Sivaraman committed
672 673
                  // Only 1 item was matched (the offset). Copy (num +
                  // offset) into the generated string.
674
                  rstr += boost::str(boost::format("%d") % (num + offset));
675 676
                  break;

677
              case 2: {
Mukund Sivaraman's avatar
Mukund Sivaraman committed
678 679 680
                  // 2 items were matched (the offset and width). Copy
                  // (num + offset) and format it according to the width
                  // into the generated string.
681 682
                  const std::string fmt =
                      boost::str(boost::format("%%0%ud") % width);
683
                  rstr += boost::str(boost::format(fmt) % (num + offset));
684
                  break;
685
              }
686 687

              case 3:
Mukund Sivaraman's avatar
Mukund Sivaraman committed
688
                  // 3 items were matched (offset, width and base).
689
                  if ((base[0] == 'n') || (base[0] == 'N')) {
Mukund Sivaraman's avatar
Mukund Sivaraman committed
690 691
                      // The base is requesting nibbles. Format it
                      // specially (see genNibbles() documentation).
692
                      rstr += genNibbles(num + offset, width, (base[0] == 'N'));
693
                  } else {
Mukund Sivaraman's avatar
Mukund Sivaraman committed
694 695 696
                      // The base is not requesting nibbles. Copy (num +
                      // offset) and format it according to the width
                      // and base into the generated string.
697
                      const std::string fmt =
698
                          boost::str(boost::format("%%0%u%c") % width % base[0]);
699
                      rstr += boost::str(boost::format(fmt) % (num + offset));
700 701 702 703
                  }
                  break;

              default:
Mukund Sivaraman's avatar
Mukund Sivaraman committed
704
                  // Any other case in the modifiers is an error.
705 706 707 708 709
                  reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
                              "Invalid $GENERATE format modifiers");
                  return ("");
              }

Mukund Sivaraman's avatar
Mukund Sivaraman committed
710 711
              // Find the closing brace. Careful that 'it' can be equal
              // to str.end() here.
712 713 714
              while ((it != str.end()) && (*it != '}')) {
                  ++it;
              }
Mukund Sivaraman's avatar
Mukund Sivaraman committed
715
              // Skip past the closing brace (if there is one).
716 717 718 719
              if (it != str.end()) {
                  ++it;
              }
          }
720 721 722
          break;

      case '\\':
Mukund Sivaraman's avatar
Mukund Sivaraman committed
723 724 725 726
          // This is the case when the '\' character is encountered in
          // the LHS or RHS. The '\' and the following character are
          // copied as-is into the generated string. This is usually
          // used for escaping the $ character.
727 728 729 730 731 732 733 734 735 736
          rstr.push_back(*it);
          ++it;
          if (it == str.end()) {
              continue;
          }
          rstr.push_back(*it);
          ++it;
          break;

      default:
Mukund Sivaraman's avatar
Mukund Sivaraman committed
737 738 739
          // This is the default case that handles all other
          // characters. They are copied as-is into the generated
          // string.
740 741 742 743 744 745 746 747 748 749 750
          rstr.push_back(*it);
          ++it;
          break;
      }
  }

  return (rstr);
}

void
MasterLoader::MasterLoaderImpl::doGenerate() {
751
    // Parse the range token
752 753 754 755 756 757 758 759
    const MasterToken& range_token = lexer_.getNextToken(MasterToken::STRING);
    if (range_token.getType() != MasterToken::STRING) {
        reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
                    "Invalid $GENERATE syntax");
        return;
    }
    const std::string range = range_token.getString();

760
    // Parse the LHS token
761 762 763 764 765 766 767 768
    const MasterToken& lhs_token = lexer_.getNextToken(MasterToken::STRING);
    if (lhs_token.getType() != MasterToken::STRING) {
        reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
                    "Invalid $GENERATE syntax");
        return;
    }
    const std::string lhs = lhs_token.getString();

769
    // Parse the TTL, RR class and RR type tokens. Note that TTL and RR
Mukund Sivaraman's avatar
Mukund Sivaraman committed
770 771 772 773 774 775 776
    // class may come in any order, or may be missing (either or
    // both). If TTL is missing, we expect that it was either specified
    // explicitly using $TTL, or is implicitly known from a previous RR,
    // or that this is the SOA RR from which the MINIMUM field is
    // used. It's unlikely that $GENERATE will be used with an SOA RR,
    // but it's possible. The parsing happens within the parseRRParams()
    // helper method which is called below.
777 778 779 780 781 782
    const MasterToken& param_token = lexer_.getNextToken(MasterToken::STRING);
    if (param_token.getType() != MasterToken::STRING) {
        reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
                    "Invalid $GENERATE syntax");
        return;
    }
783

784 785 786
    bool explicit_ttl = false;
    const RRType rrtype = parseRRParams(explicit_ttl, param_token);

787
    // Parse the RHS token. It can be a quoted string.
788 789 790 791 792 793 794 795 796 797
    const MasterToken& rhs_token = lexer_.getNextToken(MasterToken::QSTRING);
    if ((rhs_token.getType() != MasterToken::QSTRING) &&
        (rhs_token.getType() != MasterToken::STRING))
    {
        reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
                    "Invalid $GENERATE syntax");
        return;
    }
    const std::string rhs = rhs_token.getString();

798 799 800
    // Range can be one of two forms: start-stop or start-stop/step. If
    // the first form is used, then step is set to 1. All of start, stop
    // and step must be positive.
801 802 803
    unsigned int start;
    unsigned int stop;
    unsigned int step;
804
    // cppcheck-suppress invalidscanf_libc
805 806 807 808 809 810 811 812 813 814 815
    const int n = sscanf(range.c_str(), "%u-%u/%u", &start, &stop, &step);
    if ((n < 2) || (stop < start)) {
        reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
                    "$GENERATE: invalid range: " + range);
        return;
    }

    if (n == 2) {
        step = 1;
    }

816
    // Generate and add the records.
817
    for (unsigned int i = start; i <= stop; i += step) {
818 819
        // Get generated strings for LHS and RHS. LHS goes to form the
        // name, RHS goes to form the RDATA of the RR.
820 821
        const std::string generated_name = generateForIter(lhs, i);
        const std::string generated_rdata = generateForIter(rhs, i);
822 823 824 825 826 827 828 829
        if (generated_name.empty() || generated_rdata.empty()) {
            // The error should have been sent to the callbacks already
            // by generateForIter().
            reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
                        "$GENERATE error");
            return;
        }

830 831
        // generateForIter() can return a string with a trailing '.' in
        // case of a nibble representation. So we cannot use the
832 833 834
        // relative Name constructor. We use concatenate() which is
        // expensive, but keeps the generated LHS-based Name within the
        // active origin.
835 836
        last_name_.reset
            (new Name(Name(generated_name).concatenate(active_origin_)));
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
        previous_name_ = true;

        const rdata::RdataPtr rdata =
            rdata::createRdata(rrtype, zone_class_, generated_rdata);
        // In case we get NULL, it means there was error creating the
        // Rdata. The errors should have been reported by callbacks_
        // already. We need to decide if we want to continue or not.
        if (rdata) {
            add_callback_(*last_name_, zone_class_, rrtype,
                          getCurrentTTL(explicit_ttl, rrtype, rdata),
                          rdata);
            // Good, we added another one
            ++rr_count_;
        } else {
            seen_error_ = true;
            if (!many_errors_) {
                ok_ = false;
                complete_ = true;
                // We don't have the exact error here, but it was
                // reported by the error callback.
                isc_throw(MasterLoaderError, "Invalid RR data");
            }
        }
    }
}

863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
MasterToken
MasterLoader::MasterLoaderImpl::handleInitialToken() {
    const MasterToken& initial_token =
        lexer_.getNextToken(MasterLexer::QSTRING | MasterLexer::INITIAL_WS);

    // The most likely case is INITIAL_WS, and then string/qstring.  We
    // handle them first.
    if (initial_token.getType() == MasterToken::INITIAL_WS) {
        const MasterToken& next_token = lexer_.getNextToken();
        if (next_token.getType() == MasterToken::END_OF_LINE) {
            return (next_token); // blank line
        } else if (next_token.getType() == MasterToken::END_OF_FILE) {
            lexer_.ungetToken(); // handle it in the next iteration.
            eatUntilEOL(true);  // effectively warn about the unexpected EOF.
            return (MasterToken(MasterToken::END_OF_LINE));
        }

        // This means the same name as previous.
        if (last_name_.get() == NULL) {
            isc_throw(InternalException, "No previous name to use in "
                      "place of initial whitespace");
        } else if (!previous_name_) {
885
            callbacks_.warning(lexer_.getSourceName(), lexer_.getSourceLine(),
886 887
                               "Owner name omitted around $INCLUDE, the result "
                               "might not be as expected");
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
        }
        return (next_token);
    } else if (initial_token.getType() == MasterToken::STRING ||
               initial_token.getType() == MasterToken::QSTRING) {
        // If it is name (or directive), handle it.
        const MasterToken::StringRegion&
            name_string(initial_token.getStringRegion());

        if (name_string.len > 0 && name_string.beg[0] == '$') {
            // This should have either thrown (and the error handler
            // will read up until the end of line) or read until the
            // end of line.

            // Exclude the $ from the string on this point.
            handleDirective(name_string.beg + 1, name_string.len - 1);
            // So, get to the next line, there's nothing more interesting
            // in this one.
            return (MasterToken(MasterToken::END_OF_LINE));
        }

        // This should be an RR, starting with an owner name.  Construct the
        // name, and some string token should follow.
        last_name_.reset(new Name(name_string.beg, name_string.len,
                                  &active_origin_));
        previous_name_ = true;
        return (lexer_.getNextToken(MasterToken::STRING));
    }

    switch (initial_token.getType()) { // handle less common cases
    case MasterToken::END_OF_FILE:
        if (!popSource()) {
            return (initial_token);
        } else {
            // We try to read a token from the popped source
922 923
            // So continue to the next line of that source, but first, make
            // sure the source is at EOL
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
            eatUntilEOL(true);
            return (MasterToken(MasterToken::END_OF_LINE));
        }
    case MasterToken::END_OF_LINE:
        return (initial_token); // empty line
    case MasterToken::ERROR:
        // Error token here.
        isc_throw(InternalException, initial_token.getErrorText());
    default:
        // Some other token (what could that be?)
        isc_throw(InternalException, "Parser got confused (unexpected "
                  "token " << initial_token.getType() << ")");
    }
}

939 940
bool
MasterLoader::MasterLoaderImpl::loadIncremental(size_t count_limit) {
941 942 943
    if (count_limit == 0) {
        isc_throw(isc::InvalidParameter, "Count limit set to 0");
    }
944 945 946 947 948
    if (complete_) {
        isc_throw(isc::InvalidOperation,
                  "Trying to load when already loaded");
    }
    if (!initialized_) {
949
        pushSource(master_file_, active_origin_);
950 951 952 953
    }
    size_t count = 0;
    while (ok_ && count < count_limit) {
        try {
954 955 956 957 958
            const MasterToken next_token = handleInitialToken();
            if (next_token.getType() == MasterToken::END_OF_FILE) {
                return (true);  // we are done
            } else if (next_token.getType() == MasterToken::END_OF_LINE) {
                continue;       // nothing more to do in this line
959
            }
960 961 962
            // We are going to parse an RR, have known the owner name,
            // and are now seeing the next string token in the rest of the RR.
            assert(next_token.getType() == MasterToken::STRING);
963

964
            bool explicit_ttl = false;
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
965
            const RRType rrtype = parseRRParams(explicit_ttl, next_token);
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
966
            // TODO: Check if it is SOA, it should be at the origin.
967

968
            const rdata::RdataPtr rdata =
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
969 970 971
                rdata::createRdata(rrtype, zone_class_, lexer_,
                                   &active_origin_, options_, callbacks_);

972 973 974 975
            // In case we get NULL, it means there was error creating
            // the Rdata. The errors should have been reported by
            // callbacks_ already. We need to decide if we want to continue
            // or not.
976
            if (rdata) {
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
977
                add_callback_(*last_name_, zone_class_, rrtype,
978 979
                              getCurrentTTL(explicit_ttl, rrtype, rdata),
                              rdata);
980 981
                // Good, we loaded another one
                ++count;
982
                ++rr_count_;
983 984 985 986 987 988 989 990 991
            } else {
                seen_error_ = true;
                if (!many_errors_) {
                    ok_ = false;
                    complete_ = true;
                    // We don't have the exact error here, but it was reported
                    // by the error callback.
                    isc_throw(MasterLoaderError, "Invalid RR data");
                }
992
            }
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
        } catch (const isc::dns::DNSTextError& e) {
            reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
                        e.what());
            eatUntilEOL(false);
        } catch (const MasterLexer::ReadError& e) {
            reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
                        e.what());
            eatUntilEOL(false);
        } catch (const MasterLexer::LexerError& e) {
            reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
                        e.what());
            eatUntilEOL(false);
        } catch (const InternalException& e) {
1006 1007
            reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
                        e.what());
1008
            eatUntilEOL(false);
1009 1010 1011 1012 1013 1014
        }
    }
    // When there was a fatal error and ok is false, we say we are done.
    return (!ok_);
}

1015 1016
MasterLoader::MasterLoader(const char* master_file,
                           const Name& zone_origin,
1017 1018
                           const RRClass& zone_class,
                           const MasterLoaderCallbacks& callbacks,
1019
                           const AddRRCallback& add_callback,
1020 1021
                           Options options)
{
1022 1023 1024
    if (add_callback.empty()) {
        isc_throw(isc::InvalidParameter, "Empty add RR callback");
    }
1025
    impl_ = new MasterLoaderImpl(master_file, zone_origin,
1026
                                 zone_class, callbacks, add_callback, options);
1027 1028
}

1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
MasterLoader::MasterLoader(std::istream& stream,
                           const Name& zone_origin,
                           const RRClass& zone_class,
                           const MasterLoaderCallbacks& callbacks,
                           const AddRRCallback& add_callback,
                           Options options)
{
    if (add_callback.empty()) {
        isc_throw(isc::InvalidParameter, "Empty add RR callback");
    }
1039 1040 1041
    unique_ptr<MasterLoaderImpl>
        impl(new MasterLoaderImpl("", zone_origin, zone_class,
                                  callbacks, add_callback, options));
1042 1043
    impl->pushStreamSource(stream);
    impl_ = impl.release();
1044 1045
}

1046 1047 1048 1049 1050 1051
MasterLoader::~MasterLoader() {
    delete impl_;
}

bool
MasterLoader::loadIncremental(size_t count_limit) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
1052
    const bool result = impl_->loadIncremental(count_limit);
1053 1054
    impl_->complete_ = result;
    return (result);
1055 1056
}

1057 1058 1059 1060 1061
bool
MasterLoader::loadedSucessfully() const {
    return (impl_->complete_ && !impl_->seen_error_);
}

1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
size_t
MasterLoader::getSize() const {
    return (impl_->getSize());
}

size_t
MasterLoader::getPosition() const {
    return (impl_->getPosition());
}

1072 1073
} // end namespace dns
} // end namespace isc