master_loader.cc 15.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (C) 2012  Internet Systems Consortium, Inc. ("ISC")
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.

#include <dns/master_loader.h>
16 17 18 19 20
#include <dns/master_lexer.h>
#include <dns/name.h>
#include <dns/rrttl.h>
#include <dns/rrclass.h>
#include <dns/rrtype.h>
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
21
#include <dns/rdata.h>
22

23
#include <string>
24
#include <memory>
25
#include <boost/algorithm/string/predicate.hpp> // for iequals
26
#include <boost/scoped_ptr.hpp>
27

28
using std::string;
29
using std::auto_ptr;
30
using boost::algorithm::iequals;
31
using boost::scoped_ptr;
32 33 34 35

namespace isc {
namespace dns {

36 37
namespace {

38 39 40 41 42 43 44 45 46 47
// 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
48
} // end unnamed namespace
49

50 51 52 53
class MasterLoader::MasterLoaderImpl {
public:
    MasterLoaderImpl(const char* master_file,
                     const Name& zone_origin,
54 55
                     const RRClass& zone_class,
                     const MasterLoaderCallbacks& callbacks,
56
                     const AddRRCallback& add_callback,
57 58 59
                     MasterLoader::Options options) :
        lexer_(),
        zone_origin_(zone_origin),
60
        active_origin_(zone_origin),
61 62
        zone_class_(zone_class),
        callbacks_(callbacks),
63
        add_callback_(add_callback),
64 65 66
        options_(options),
        master_file_(master_file),
        initialized_(false),
67
        ok_(true),
68
        many_errors_((options & MANY_ERRORS) != 0),
69 70
        complete_(false),
        seen_error_(false)
71 72
    {}

73 74 75
    void reportError(const std::string& filename, size_t line,
                     const std::string& reason)
    {
76
        seen_error_ = true;
77
        callbacks_.error(filename, line, reason);
78
        if (!many_errors_) {
79 80 81 82 83 84 85 86
            // 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());
        }
    }

87 88 89
    void pushSource(const std::string& filename) {
        std::string error;
        if (!lexer_.pushSource(filename.c_str(), &error)) {
90
            if (initialized_) {
91
                isc_throw(InternalException, error.c_str());
92 93 94 95 96
            } else {
                // Top-level file
                reportError("", 0, error);
                ok_ = false;
            }
97
        }
98 99 100
        initialized_ = true;
    }

101
    bool popSource() {
102
        if (lexer_.getSourceCount() == 1) {
103 104
            return (false);
        }
105
        lexer_.popSource();
106
        return (true);
107 108 109 110 111
    }

    void pushStreamSource(std::istream& stream) {
        lexer_.pushSource(stream);
        initialized_ = true;
112 113
    }

114 115
    // Get a string token. Handle it as error if it is not string.
    const string getString() {
116
        lexer_.getNextToken(MasterToken::STRING).getString(string_token_);
117
        return (string_token_);
118 119
    }

120
    bool loadIncremental(size_t count_limit);
121

122 123
    void doInclude() {
        // First, get the filename to include
124 125
        const string
            filename(lexer_.getNextToken(MasterToken::QSTRING).getString());
126 127 128 129 130 131 132

        // There could be an origin (or maybe not). So try looking
        const MasterToken name_tok(lexer_.getNextToken(MasterToken::QSTRING,
                                                       true));

        if (name_tok.getType() == MasterToken::QSTRING ||
            name_tok.getType() == MasterToken::STRING) {
133 134 135 136 137 138
            // There's an optional name, meaning origin. Extract it
            // and store.
            const MasterToken::StringRegion&
                name_string(name_tok.getStringRegion());
            active_origin_ = Name(name_string.beg, name_string.len,
                                  &active_origin_);
139 140 141 142 143 144 145 146
        } else {
            // We return the newline there. This is because after we pop
            // the source, we want to call eatUntilEOL and this would
            // eat to the next one.
            lexer_.ungetToken();
        }

        pushSource(filename);
147 148
    }

149 150 151 152 153 154 155 156 157 158 159 160
    void doOrigin() {
        // Parse and create the new origin. It is relative to the previous
        // one.
        const MasterToken::StringRegion&
            name_string(lexer_.getNextToken(MasterToken::QSTRING).
                        getStringRegion());
        active_origin_ = Name(name_string.beg, name_string.len,
                              &active_origin_);
        // Make sure there's the EOLN
        eatUntilEOL(true);
    }

161
    void handleDirective(const char* directive, size_t length) {
162
        if (iequals(directive, "INCLUDE")) {
163
            doInclude();
164
        } else if (iequals(directive, "ORIGIN")) {
165
            doOrigin();
166
        } else if (iequals(directive, "TTL")) {
167 168 169 170 171 172 173 174 175
            // TODO: Implement
            isc_throw(isc::NotImplemented,
                      "TTL directive not implemented yet");
        } else {
            isc_throw(InternalException, "Unknown directive '" <<
                      string(directive, directive + length) << "'");
        }
    }

176 177 178 179 180 181 182 183
    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
184
                                       "File does not end with newline");
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
                    // 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;
            }
        }
    }

205 206
private:
    MasterLexer lexer_;
207
    const Name zone_origin_;
208 209
    Name active_origin_; // The origin used during parsing
                         // (modifiable by $ORIGIN)
210
    scoped_ptr<Name> last_name_; // Last seen name (for INITAL_WS handling)
211 212
    const RRClass zone_class_;
    MasterLoaderCallbacks callbacks_;
213
    AddRRCallback add_callback_;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
214
    const MasterLoader::Options options_;
215
    const std::string master_file_;
216
    std::string string_token_;
217
    bool initialized_;
218 219 220
    bool ok_;                   // Is it OK to continue loading?
    const bool many_errors_;    // Are many errors allowed (or should we abort
                                // on the first)
221
public:
222 223 224
    bool complete_;             // All work done.
    bool seen_error_;           // Was there at least one error during the
                                // load?
225 226
};

227 228
bool
MasterLoader::MasterLoaderImpl::loadIncremental(size_t count_limit) {
229 230 231
    if (count_limit == 0) {
        isc_throw(isc::InvalidParameter, "Count limit set to 0");
    }
232 233 234 235 236 237 238 239 240 241
    if (complete_) {
        isc_throw(isc::InvalidOperation,
                  "Trying to load when already loaded");
    }
    if (!initialized_) {
        pushSource(master_file_);
    }
    size_t count = 0;
    while (ok_ && count < count_limit) {
        try {
242
            MasterToken initial_token(MasterToken::NO_TOKEN_PRODUCED);
243 244
            // Skip all EOLNs (empty lines) and finish on EOF
            do {
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
                initial_token = lexer_.getNextToken(MasterLexer::QSTRING |
                                                    MasterLexer::INITIAL_WS);
                if (initial_token.getType() == MasterToken::INITIAL_WS) {
                    // The INITIAL_WS is interesting only if something is
                    // after it. So peek there and if there's EOLN or EOF,
                    // ignore it.
                    const MasterToken& peek_token(lexer_.getNextToken());
                    if (peek_token.getType() == MasterToken::END_OF_LINE ||
                        peek_token.getType() == MasterToken::END_OF_FILE) {
                        initial_token = peek_token;
                    } else {
                        // It is something interesting. Return it back and
                        // keep the whitespace.
                        lexer_.ungetToken();
                    }
                }
                if (initial_token.getType() == MasterToken::END_OF_FILE) {
262 263 264 265
                    if (!popSource()) {
                        return (true);
                    } else {
                        // We try to read a token from the popped source
266 267 268
                        // So retry the loop, but first, make sure the source
                        // is at EOL
                        eatUntilEOL(true);
269 270
                        continue;
                    }
271
                }
272 273
            } while (initial_token.getType() == MasterToken::END_OF_LINE ||
                     initial_token.getType() == MasterToken::END_OF_FILE);
274

275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
            if (initial_token.getType() == MasterToken::QSTRING ||
                initial_token.getType() == MasterToken::STRING) {
                // 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.
                    continue;
                }

293 294
                last_name_.reset(new Name(name_string.beg, name_string.len,
                                          &active_origin_));
295 296
            } else if (initial_token.getType() == MasterToken::INITIAL_WS) {
                // This means the same name as previous.
297
                if (last_name_.get() == NULL) {
298 299 300 301 302 303 304 305 306 307
                    isc_throw(InternalException, "No previous name to use in "
                              "place of initial whitespace");
                }
            } else if (initial_token.getType() == MasterToken::ERROR) {
                // Error token here.
                isc_throw(InternalException, initial_token.getErrorText());
            } else {
                // Some other token (what could that be?)
                isc_throw(InternalException, "Parser got confused (unexpected "
                          "token " << initial_token.getType() << ")");
308 309
            }

310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
            // TODO: Some more flexibility. We don't allow omitting
            // anything yet

            // The parameters
            const RRTTL ttl(getString());
            const RRClass rrclass(getString());
            const RRType rrtype(getString());

            // TODO: Some more validation?
            if (rrclass != zone_class_) {
                // It doesn't really matter much what type of exception
                // we throw, we catch it just below.
                isc_throw(isc::BadValue, "Class mismatch: " << rrclass <<
                          "vs. " << zone_class_);
            }
Michal 'vorner' Vaner's avatar
Michal 'vorner' Vaner committed
325
            // TODO: Check if it is SOA, it should be at the origin.
326 327 328

            const rdata::RdataPtr data(rdata::createRdata(rrtype, rrclass,
                                                          lexer_,
329
                                                          &active_origin_,
330 331 332 333 334 335
                                                          options_,
                                                          callbacks_));
            // 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.
336
            if (data) {
337
                add_callback_(*last_name_, rrclass, rrtype, ttl, data);
338 339 340

                // Good, we loaded another one
                ++count;
341 342 343 344 345 346 347 348 349
            } 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");
                }
350 351 352 353 354 355 356 357 358 359 360 361
            }
        } catch (const MasterLoaderError&) {
            // This is a hack. We exclude the MasterLoaderError from the
            // below case. Once we restrict the below to some smaller
            // exception, we should remove this.
            throw;
        } catch (const isc::Exception& e) {
            // TODO: Once we do #2518, catch only the DNSTextError here,
            // not isc::Exception. The rest should be just simply
            // propagated.
            reportError(lexer_.getSourceName(), lexer_.getSourceLine(),
                        e.what());
362
            eatUntilEOL(false);
363 364 365 366 367 368
        }
    }
    // When there was a fatal error and ok is false, we say we are done.
    return (!ok_);
}

369 370
MasterLoader::MasterLoader(const char* master_file,
                           const Name& zone_origin,
371 372
                           const RRClass& zone_class,
                           const MasterLoaderCallbacks& callbacks,
373
                           const AddRRCallback& add_callback,
374 375
                           Options options)
{
376 377 378
    if (add_callback.empty()) {
        isc_throw(isc::InvalidParameter, "Empty add RR callback");
    }
379
    impl_ = new MasterLoaderImpl(master_file, zone_origin,
380
                                 zone_class, callbacks, add_callback, options);
381 382
}

383 384 385 386 387 388 389 390 391 392
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");
    }
393 394 395 396 397 398
    auto_ptr<MasterLoaderImpl> impl(new MasterLoaderImpl("", zone_origin,
                                                         zone_class, callbacks,
                                                         add_callback,
                                                         options));
    impl->pushStreamSource(stream);
    impl_ = impl.release();
399 400
}

401 402 403 404 405 406
MasterLoader::~MasterLoader() {
    delete impl_;
}

bool
MasterLoader::loadIncremental(size_t count_limit) {
JINMEI Tatuya's avatar
JINMEI Tatuya committed
407
    const bool result = impl_->loadIncremental(count_limit);
408 409
    impl_->complete_ = result;
    return (result);
410 411
}

412 413 414 415 416
bool
MasterLoader::loadedSucessfully() const {
    return (impl_->complete_ && !impl_->seen_error_);
}

417 418
} // end namespace dns
} // end namespace isc