config_parser.cc 22.9 KB
Newer Older
1
// Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//
// 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 <config/ccsession.h>
#include <dhcp4/dhcp4_log.h>
17 18 19
#include <dhcp/libdhcp++.h>
#include <dhcp/option_definition.h>
#include <dhcpsrv/cfgmgr.h>
20
#include <dhcp4/config_parser.h>
21
#include <dhcpsrv/dbaccess_parser.h>
22
#include <dhcpsrv/dhcp_parsers.h>
23
#include <dhcpsrv/option_space_container.h>
24
#include <util/encode/hex.h>
25
#include <util/strutil.h>
26

Tomek Mrugalski's avatar
Tomek Mrugalski committed
27 28 29
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
30

Tomek Mrugalski's avatar
Tomek Mrugalski committed
31
#include <limits>
Tomek Mrugalski's avatar
Tomek Mrugalski committed
32 33 34 35
#include <iostream>
#include <vector>
#include <map>

36
using namespace std;
37 38
using namespace isc;
using namespace isc::dhcp;
39 40 41
using namespace isc::data;
using namespace isc::asiolink;

42 43
namespace {

44
/// @brief Parser for DHCP4 option data value.
45
///
46 47 48 49
/// This parser parses configuration entries that specify value of
/// a single option specific to DHCP4.  It provides the DHCP4-specific
/// implementation of the abstract class OptionDataParser.
class Dhcp4OptionDataParser : public OptionDataParser {
50
public:
51
    /// @brief Constructor.
52
    ///
53 54 55
    /// @param dummy first param, option names are always "Dhcp4/option-data[n]"
    /// @param options is the option storage in which to store the parsed option
    /// upon "commit".
56
    /// @param global_context is a pointer to the global context which
57
    /// stores global scope parameters, options, option defintions.
58 59
    Dhcp4OptionDataParser(const std::string&,
        OptionStoragePtr options, ParserContextPtr global_context)
60
        :OptionDataParser("", options, global_context) {
61 62
    }

63
    /// @brief static factory method for instantiating Dhcp4OptionDataParsers
64
    ///
65
    /// @param param_name name of the parameter to be parsed.
66
    /// @param options storage where the parameter value is to be stored.
67
    /// @param global_context is a pointer to the global context which
68 69 70 71 72 73
    /// stores global scope parameters, options, option defintions.
    /// @return returns a pointer to a new OptionDataParser. Caller is
    /// is responsible for deleting it when it is no longer needed.
    static OptionDataParser* factory(const std::string& param_name,
        OptionStoragePtr options, ParserContextPtr global_context) {
        return (new Dhcp4OptionDataParser(param_name, options, global_context));
74 75
    }

76 77
protected:
    /// @brief Finds an option definition within the server's option space
78 79
    ///
    /// Given an option space and an option code, find the correpsonding
80 81
    /// option defintion within the server's option defintion storage.
    ///
82 83 84
    /// @param option_space name of the parameter option space
    /// @param option_code numeric value of the parameter to find
    /// @return OptionDefintionPtr of the option defintion or an
85
    /// empty OptionDefinitionPtr if not found.
86 87
    /// @throw DhcpConfigError if the option space requested is not valid
    /// for this server.
88 89 90 91 92 93 94 95 96
    virtual OptionDefinitionPtr findServerSpaceOptionDefinition (
                std::string& option_space, uint32_t option_code) {
        OptionDefinitionPtr def;
        if (option_space == "dhcp4" &&
            LibDHCP::isStandardOption(Option::V4, option_code)) {
            def = LibDHCP::getOptionDef(Option::V4, option_code);
        } else if (option_space == "dhcp6") {
            isc_throw(DhcpConfigError, "'dhcp6' option space name is reserved"
                     << " for DHCPv6 server");
97
        }
98

99
        return (def);
100 101 102
    }
};

103
/// @brief Parser for IPv4 pool definitions.
104
///
105 106 107
/// This is the IPv4 derivation of the PoolParser class and handles pool
/// definitions, i.e. a list of entries of one of two syntaxes: min-max and
/// prefix/len for IPv4 pools. Pool4 objects are created and stored in chosen
108
/// PoolStorage container.
109
///
110 111
/// It is useful for parsing Dhcp4/subnet4[X]/pool parameters.
class Pool4Parser : public PoolParser {
112 113
public:

114 115 116 117 118 119 120 121
    /// @brief Constructor.
    ///
    /// @param param_name name of the parameter. Note, it is passed through
    /// but unused, parameter is currently always "Dhcp4/subnet4[X]/pool"
    /// @param pools storage container in which to store the parsed pool
    /// upon "commit"
    Pool4Parser(const std::string& param_name,  PoolStoragePtr pools)
        :PoolParser(param_name, pools) {
122 123
    }

124 125
protected:
    /// @brief Creates a Pool4 object given a IPv4 prefix and the prefix length.
126
    ///
127 128
    /// @param addr is the IPv4 prefix of the pool.
    /// @param len is the prefix length.
129
    /// @param ignored dummy parameter to provide symmetry between the
130
    /// PoolParser derivations. The V6 derivation requires a third value.
131
    /// @return returns a PoolPtr to the new Pool4 object.
132
    PoolPtr poolMaker (IOAddress &addr, uint32_t len, int32_t) {
133 134
        return (PoolPtr(new Pool4(addr, len)));
    }
135

136 137 138 139
    /// @brief Creates a Pool4 object given starting and ending IPv4 addresses.
    ///
    /// @param min is the first IPv4 address in the pool.
    /// @param max is the last IPv4 address in the pool.
140
    /// @param ignored dummy parameter to provide symmetry between the
141
    /// PoolParser derivations. The V6 derivation requires a third value.
142
    /// @return returns a PoolPtr to the new Pool4 object.
143
    PoolPtr poolMaker (IOAddress &min, IOAddress &max, int32_t) {
144
        return (PoolPtr(new Pool4(min, max)));
145
    }
146
};
147

148 149
/// @brief This class parses a single IPv4 subnet.
///
150 151
/// This is the IPv4 derivation of the SubnetConfigParser class and it parses
/// the whole subnet definition. It creates parsersfor received configuration
152 153 154 155
/// parameters as needed.
class Subnet4ConfigParser : public SubnetConfigParser {
public:
    /// @brief Constructor
156
    ///
157 158
    /// @param ignored first parameter
    /// stores global scope parameters, options, option defintions.
159 160
    Subnet4ConfigParser(const std::string&)
        :SubnetConfigParser("", globalContext()) {
161
    }
162 163

    /// @brief Adds the created subnet to a server's configuration.
164
    /// @throw throws Unexpected if dynamic cast fails.
165
    void commit() {
166
        if (subnet_) {
167 168 169
            Subnet4Ptr sub4ptr = boost::dynamic_pointer_cast<Subnet4>(subnet_);
            if (!sub4ptr) {
                // If we hit this, it is a programming error.
170
                isc_throw(Unexpected,
171 172 173 174
                          "Invalid cast in Subnet4ConfigParser::commit");
            }

            isc::dhcp::CfgMgr::instance().addSubnet4(sub4ptr);
175 176 177
        }
    }

178
protected:
179

180
    /// @brief Creates parsers for entries in subnet definition.
181
    ///
182 183 184 185 186 187 188 189 190 191 192 193
    /// @param config_id name of the entry
    ///
    /// @return parser object for specified entry name. Note the caller is
    /// responsible for deleting the parser created.
    /// @throw isc::dhcp::DhcpConfigError if trying to create a parser
    /// for unknown config element
    DhcpConfigParser* createSubnetConfigParser(const std::string& config_id) {
        DhcpConfigParser* parser = NULL;
        if ((config_id.compare("valid-lifetime") == 0)  ||
            (config_id.compare("renew-timer") == 0)  ||
            (config_id.compare("rebind-timer") == 0))  {
            parser = new Uint32Parser(config_id, uint32_values_);
194
        } else if ((config_id.compare("subnet") == 0) ||
195 196
                   (config_id.compare("interface") == 0) ||
                   (config_id.compare("next-server") == 0)) {
197 198 199 200
            parser = new StringParser(config_id, string_values_);
        } else if (config_id.compare("pool") == 0) {
            parser = new Pool4Parser(config_id, pools_);
        } else if (config_id.compare("option-data") == 0) {
201
           parser = new OptionDataListParser(config_id, options_,
202 203
                                             global_context_,
                                             Dhcp4OptionDataParser::factory);
204
        } else {
205 206
            isc_throw(NotImplemented,
                "parser error: Subnet4 parameter not supported: " << config_id);
207 208
        }

209
        return (parser);
210 211
    }

212 213

    /// @brief Determines if the given option space name and code describe
214
    /// a standard option for the DCHP4 server.
215
    ///
216 217 218 219 220 221 222 223
    /// @param option_space is the name of the option space to consider
    /// @param code is the numeric option code to consider
    /// @return returns true if the space and code are part of the server's
    /// standard options.
    bool isServerStdOption(std::string option_space, uint32_t code) {
        return ((option_space.compare("dhcp4") == 0)
                && LibDHCP::isStandardOption(Option::V4, code));
    }
224

225 226 227 228 229 230 231
    /// @brief Returns the option definition for a given option code from
    /// the DHCP4 server's standard set of options.
    /// @param code is the numeric option code of the desired option definition.
    /// @return returns a pointer the option definition
    OptionDefinitionPtr getServerStdOptionDefinition (uint32_t code) {
        return (LibDHCP::getOptionDef(Option::V4, code));
    }
232

233
    /// @brief Issues a DHCP4 server specific warning regarding duplicate subnet
234 235
    /// options.
    ///
236
    /// @param code is the numeric option code of the duplicate option
237
    /// @param addr is the subnet address
238
    /// @todo a means to know the correct logger and perhaps a common
239
    /// message would allow this method to be emitted by the base class.
240 241 242 243 244
    virtual void duplicate_option_warning(uint32_t code,
                                         isc::asiolink::IOAddress& addr) {
        LOG_WARN(dhcp4_logger, DHCP4_CONFIG_OPTION_DUPLICATE)
            .arg(code).arg(addr.toText());
    }
245

246
    /// @brief Instantiates the IPv4 Subnet based on a given IPv4 address
247 248
    /// and prefix length.
    ///
249
    /// @param addr is IPv4 address of the subnet.
250
    /// @param len is the prefix length
251
    void initSubnet(isc::asiolink::IOAddress addr, uint8_t len) {
252 253 254 255 256
        // Get all 'time' parameters using inheritance.
        // If the subnet-specific value is defined then use it, else
        // use the global value. The global value must always be
        // present. If it is not, it is an internal error and exception
        // is thrown.
257 258 259 260 261 262 263 264 265 266
        Triplet<uint32_t> t1 = getParam("renew-timer");
        Triplet<uint32_t> t2 = getParam("rebind-timer");
        Triplet<uint32_t> valid = getParam("valid-lifetime");

        stringstream tmp;
        tmp << addr.toText() << "/" << (int)len
            << " with params t1=" << t1 << ", t2=" << t2 << ", valid=" << valid;

        LOG_INFO(dhcp4_logger, DHCP4_CONFIG_NEW_SUBNET).arg(tmp.str());

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
        Subnet4* subnet4 = new Subnet4(addr, len, t1, t2, valid);
        subnet_.reset(subnet4);

        // Try global value first
        try {
            string next_server = globalContext()->string_values_->getParam("next-server");
            subnet4->setSiaddr(IOAddress(next_server));
        } catch (const DhcpConfigError&) {
            // don't care. next_server is optional. We can live without it
        }

        // Try subnet specific value if it's available
        try {
            string next_server = string_values_->getParam("next-server");
            subnet4->setSiaddr(IOAddress(next_server));
        } catch (const DhcpConfigError&) {
            // don't care. next_server is optional. We can live without it
        }
285 286 287
    }
};

288
/// @brief this class parses list of DHCP4 subnets
289 290 291 292
///
/// This is a wrapper parser that handles the whole list of Subnet4
/// definitions. It iterates over all entries and creates Subnet4ConfigParser
/// for each entry.
293
class Subnets4ListConfigParser : public DhcpConfigParser {
294 295 296 297
public:

    /// @brief constructor
    ///
298
    /// @param dummy first argument, always ignored. All parsers accept a
299
    /// string parameter "name" as their first argument.
300 301 302 303 304 305 306 307 308 309 310
    Subnets4ListConfigParser(const std::string&) {
    }

    /// @brief parses contents of the list
    ///
    /// Iterates over all entries on the list and creates Subnet4ConfigParser
    /// for each entry.
    ///
    /// @param subnets_list pointer to a list of IPv4 subnets
    void build(ConstElementPtr subnets_list) {
        BOOST_FOREACH(ConstElementPtr subnet, subnets_list->listValue()) {
311
            ParserPtr parser(new Subnet4ConfigParser("subnet"));
312 313 314 315 316 317 318
            parser->build(subnet);
            subnets_.push_back(parser);
        }
    }

    /// @brief commits subnets definitions.
    ///
319 320
    /// Iterates over all Subnet4 parsers. Each parser contains definitions of
    /// a single subnet and its parameters and commits each subnet separately.
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    void commit() {
        // @todo: Implement more subtle reconfiguration than toss
        // the old one and replace with the new one.

        // remove old subnets
        CfgMgr::instance().deleteSubnets4();

        BOOST_FOREACH(ParserPtr subnet, subnets_) {
            subnet->commit();
        }

    }

    /// @brief Returns Subnet4ListConfigParser object
    /// @param param_name name of the parameter
    /// @return Subnets4ListConfigParser object
337
    static DhcpConfigParser* factory(const std::string& param_name) {
338 339 340 341 342
        return (new Subnets4ListConfigParser(param_name));
    }

    /// @brief collection of subnet parsers.
    ParserCollection subnets_;
343

344 345
};

346 347 348 349 350
} // anonymous namespace

namespace isc {
namespace dhcp {

351 352 353 354 355 356 357
/// @brief creates global parsers
///
/// This method creates global parsers that parse global parameters, i.e.
/// those that take format of Dhcp4/param1, Dhcp4/param2 and so forth.
///
/// @param config_id pointer to received global configuration entry
/// @return parser for specified global DHCPv4 parameter
358
/// @throw NotImplemented if trying to create a parser for unknown
359
/// config element
360
DhcpConfigParser* createGlobalDhcp4ConfigParser(const std::string& config_id) {
361
    DhcpConfigParser* parser = NULL;
362 363 364
    if ((config_id.compare("valid-lifetime") == 0)  ||
        (config_id.compare("renew-timer") == 0)  ||
        (config_id.compare("rebind-timer") == 0))  {
365
        parser = new Uint32Parser(config_id,
366
                                 globalContext()->uint32_values_);
367
    } else if (config_id.compare("interfaces") == 0) {
368
        parser = new InterfaceListConfigParser(config_id);
369
    } else if (config_id.compare("subnet4") == 0) {
370
        parser = new Subnets4ListConfigParser(config_id);
371
    } else if (config_id.compare("option-data") == 0) {
372 373
        parser = new OptionDataListParser(config_id,
                                          globalContext()->options_,
374
                                          globalContext(),
375
                                          Dhcp4OptionDataParser::factory);
376
    } else if (config_id.compare("option-def") == 0) {
377
        parser  = new OptionDefListParser(config_id,
378
                                          globalContext()->option_defs_);
379 380
    } else if ((config_id.compare("version") == 0) ||
               (config_id.compare("next-server") == 0)) {
381
        parser  = new StringParser(config_id,
382
                                    globalContext()->string_values_);
383
    } else if (config_id.compare("lease-database") == 0) {
384
        parser = new DbAccessParser(config_id);
385 386
    } else if (config_id.compare("hooks-libraries") == 0) {
        parser = new HooksLibrariesParser(config_id);
387
    } else {
388
        isc_throw(NotImplemented,
389 390
                "Parser error: Global configuration parameter not supported: "
                << config_id);
391
    }
392 393

    return (parser);
394 395 396
}

isc::data::ConstElementPtr
397
configureDhcp4Server(Dhcpv4Srv&, isc::data::ConstElementPtr config_set) {
398
    if (!config_set) {
Tomek Mrugalski's avatar
Tomek Mrugalski committed
399 400 401
        ConstElementPtr answer = isc::config::createAnswer(1,
                                 string("Can't parse NULL config"));
        return (answer);
402 403
    }

404
    /// @todo: Append most essential info here (like "2 new subnets configured")
405 406
    string config_details;

407
    LOG_DEBUG(dhcp4_logger, DBG_DHCP4_COMMAND,
408
              DHCP4_CONFIG_START).arg(config_set->str());
409

410
    // Some of the values specified in the configuration depend on
411 412 413
    // other values. Typically, the values in the subnet4 structure
    // depend on the global values. Also, option values configuration
    // must be performed after the option definitions configurations.
414 415 416
    // Thus we group parsers and will fire them in the right order:
    // all parsers other than subnet4 and option-data parser,
    // option-data parser, subnet4 parser.
417
    ParserCollection independent_parsers;
418 419
    ParserPtr subnet_parser;
    ParserPtr option_parser;
420
    ParserPtr iface_parser;
421

422 423
    // Some of the parsers alter the state of the system in a way that can't
    // easily be undone. (Or alter it in a way such that undoing the change has
424
    // the same risk of failure as doing the change.)
425 426
    ParserPtr hooks_parser_;

427
    // The subnet parsers implement data inheritance by directly
428
    // accessing global storage. For this reason the global data
429
    // parsers must store the parsed data into global storages
430 431 432 433
    // immediately. This may cause data inconsistency if the
    // parsing operation fails after the global storage has been
    // modified. We need to preserve the original global data here
    // so as we can rollback changes when an error occurs.
434
    ParserContext original_context(*globalContext());
435

436
    // Answer will hold the result.
437
    ConstElementPtr answer;
438
    // Rollback informs whether error occured and original data
439 440
    // have to be restored to global storages.
    bool rollback = false;
441
    // config_pair holds the details of the current parser when iterating over
442 443
    // the parsers.  It is declared outside the loops so in case of an error,
    // the name of the failing parser can be retrieved in the "catch" clause.
444
    ConfigPair config_pair;
445
    try {
446
        // Make parsers grouping.
447
        const std::map<std::string, ConstElementPtr>& values_map =
448
                                                        config_set->mapValue();
449
        BOOST_FOREACH(config_pair, values_map) {
450
            ParserPtr parser(createGlobalDhcp4ConfigParser(config_pair.first));
451 452
            LOG_DEBUG(dhcp4_logger, DBG_DHCP4_DETAIL, DHCP4_PARSER_CREATED)
                      .arg(config_pair.first);
453 454 455 456
            if (config_pair.first == "subnet4") {
                subnet_parser = parser;
            } else if (config_pair.first == "option-data") {
                option_parser = parser;
457 458 459 460 461
            } else if (config_pair.first == "interfaces") {
                // The interface parser is independent from any other
                // parser and can be run here before any other parsers.
                iface_parser = parser;
                parser->build(config_pair.second);
462 463 464 465 466 467
            } else if (config_pair.first == "hooks-libraries") {
                // Executing commit will alter currently-loaded hooks
                // libraries.  Check if the supplied libraries are valid,
                // but defer the commit until everything else has committed.
                hooks_parser_ = parser;
                parser->build(config_pair.second);
468
            } else {
469 470
                // Those parsers should be started before other
                // parsers so we can call build straight away.
471 472 473 474 475 476 477 478
                independent_parsers.push_back(parser);
                parser->build(config_pair.second);
                // The commit operation here may modify the global storage
                // but we need it so as the subnet6 parser can access the
                // parsed data.
                parser->commit();
            }
        }
479

480
        // The option values parser is the next one to be run.
481 482 483 484 485 486 487
        std::map<std::string, ConstElementPtr>::const_iterator option_config =
            values_map.find("option-data");
        if (option_config != values_map.end()) {
            option_parser->build(option_config->second);
            option_parser->commit();
        }

488
        // The subnet parser is the last one to be run.
489 490 491 492
        std::map<std::string, ConstElementPtr>::const_iterator subnet_config =
            values_map.find("subnet4");
        if (subnet_config != values_map.end()) {
            subnet_parser->build(subnet_config->second);
493
        }
494

495
    } catch (const isc::Exception& ex) {
496
        LOG_ERROR(dhcp4_logger, DHCP4_PARSER_FAIL)
497 498 499
                  .arg(config_pair.first).arg(ex.what());
        answer = isc::config::createAnswer(1,
                     string("Configuration parsing failed: ") + ex.what());
500 501 502 503

        // An error occured, so make sure that we restore original data.
        rollback = true;

504
    } catch (...) {
505
        // For things like bad_cast in boost::lexical_cast
506
        LOG_ERROR(dhcp4_logger, DHCP4_PARSER_EXCEPTION).arg(config_pair.first);
507 508
        answer = isc::config::createAnswer(1,
                     string("Configuration parsing failed"));
509 510 511

        // An error occured, so make sure that we restore original data.
        rollback = true;
512 513
    }

514 515 516 517 518 519
    // So far so good, there was no parsing error so let's commit the
    // configuration. This will add created subnets and option values into
    // the server's configuration.
    // This operation should be exception safe but let's make sure.
    if (!rollback) {
        try {
520 521
            if (subnet_parser) {
                subnet_parser->commit();
522
            }
523 524 525 526

            if (iface_parser) {
                iface_parser->commit();
            }
527 528 529 530 531 532 533

            // This occurs last as if it succeeds, there is no easy way
            // revert it.  As a result, the failure to commit a subsequent
            // change causes problems when trying to roll back.
            if (hooks_parser_) {
                hooks_parser_->commit();
            }
534 535
        }
        catch (const isc::Exception& ex) {
536
            LOG_ERROR(dhcp4_logger, DHCP4_PARSER_COMMIT_FAIL).arg(ex.what());
537 538
            answer = isc::config::createAnswer(2,
                         string("Configuration commit failed: ") + ex.what());
539 540
            rollback = true;
        } catch (...) {
541
            // For things like bad_cast in boost::lexical_cast
542
            LOG_ERROR(dhcp4_logger, DHCP4_PARSER_COMMIT_EXCEPTION);
543 544
            answer = isc::config::createAnswer(2,
                         string("Configuration commit failed"));
545
            rollback = true;
546 547
        }
    }
548 549 550

    // Rollback changes as the configuration parsing failed.
    if (rollback) {
551
        globalContext().reset(new ParserContext(original_context));
552 553 554 555 556
        return (answer);
    }

    LOG_INFO(dhcp4_logger, DHCP4_CONFIG_COMPLETE).arg(config_details);

557
    // Everything was fine. Configuration is successful.
Shane Kerr's avatar
Shane Kerr committed
558
    answer = isc::config::createAnswer(0, "Configuration committed.");
559 560 561
    return (answer);
}

562
ParserContextPtr& globalContext() {
563 564
    static ParserContextPtr global_context_ptr(new ParserContext(Option::V4));
    return (global_context_ptr);
565 566
}

567 568


569 570
}; // end of isc::dhcp namespace
}; // end of isc namespace
571