From c845661c79e68f24908d0e427083d5717986a639 Mon Sep 17 00:00:00 2001 From: Francis Dupont Date: Fri, 27 Jan 2017 16:20:50 +0100 Subject: [PATCH] [5061] Updated DB parser and applied change to MySQL --- src/lib/dhcpsrv/mysql_connection.cc | 29 +++++++++++++++++++++- src/lib/dhcpsrv/parsers/dbaccess_parser.cc | 16 ++++++++++++ src/lib/dhcpsrv/parsers/dbaccess_parser.h | 9 +++---- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/lib/dhcpsrv/mysql_connection.cc b/src/lib/dhcpsrv/mysql_connection.cc index 7de0869f0d..b027c6e009 100644 --- a/src/lib/dhcpsrv/mysql_connection.cc +++ b/src/lib/dhcpsrv/mysql_connection.cc @@ -66,6 +66,33 @@ MySqlConnection::openDatabase() { // No host. Fine, we'll use "localhost" } + unsigned int port = 0; + string sport; + try { + sport = getParameter("port"); + } catch (...) { + // No port parameter, we are going to use the default port. + sport = ""; + } + + if (sport.size() > 0) { + // Port was given, so try to convert it to an integer. + + try { + port = boost::lexical_cast(sport); + } catch (...) { + // Port given but could not be converted to an unsigned int. + // Just fall back to the default value. + port = 0; + } + + // The port is only valid when it is in the 0..65535 range. + // Again fall back to the default when the given value is invalid. + if (port > numeric_limits::max()) { + port = 0; + } + } + const char* user = NULL; string suser; try { @@ -173,7 +200,7 @@ MySqlConnection::openDatabase() { // because no row matching the WHERE clause was found, or because a // row was found but no data was altered. MYSQL* status = mysql_real_connect(mysql_, host, user, password, name, - 0, NULL, CLIENT_FOUND_ROWS); + port, NULL, CLIENT_FOUND_ROWS); if (status != mysql_) { isc_throw(DbOpenError, mysql_error(mysql_)); } diff --git a/src/lib/dhcpsrv/parsers/dbaccess_parser.cc b/src/lib/dhcpsrv/parsers/dbaccess_parser.cc index dd51a9c3eb..e4231dc1e2 100644 --- a/src/lib/dhcpsrv/parsers/dbaccess_parser.cc +++ b/src/lib/dhcpsrv/parsers/dbaccess_parser.cc @@ -55,6 +55,7 @@ DbAccessParser::parse(CfgDbAccessPtr& cfg_db, int64_t lfc_interval = 0; int64_t timeout = 0; + int64_t port = 0; // 2. Update the copy with the passed keywords. BOOST_FOREACH(ConfigPair param, database_config->mapValue()) { try { @@ -72,6 +73,11 @@ DbAccessParser::parse(CfgDbAccessPtr& cfg_db, values_copy[param.first] = boost::lexical_cast(timeout); + } else if (param.first == "port") { + port = param.second->intValue(); + values_copy[param.first] = + boost::lexical_cast(port); + } else { values_copy[param.first] = param.second->stringValue(); } @@ -128,6 +134,16 @@ DbAccessParser::parse(CfgDbAccessPtr& cfg_db, << " (" << value->getPosition() << ")"); } + // e. Check that the port is within a reasonable range. + if ((port < 0) || + (port > std::numeric_limits::max())) { + ConstElementPtr value = database_config->get("port"); + isc_throw(DhcpConfigError, "port value: " << port + << " is out of range, expected value: 0.." + << std::numeric_limits::max() + << " (" << value->getPosition() << ")"); + } + // 4. If all is OK, update the stored keyword/value pairs. We do this by // swapping contents - values_copy is destroyed immediately after the // operation (when the method exits), so we are not interested in its new diff --git a/src/lib/dhcpsrv/parsers/dbaccess_parser.h b/src/lib/dhcpsrv/parsers/dbaccess_parser.h index 8cb716d7d2..5460504d79 100644 --- a/src/lib/dhcpsrv/parsers/dbaccess_parser.h +++ b/src/lib/dhcpsrv/parsers/dbaccess_parser.h @@ -57,6 +57,7 @@ public: /// - "type" is "memfile", "mysql" or "postgresql" /// - "lfc-interval" is a number from the range of 0 to 4294967295. /// - "connect-timeout" is a number from the range of 0 to 4294967295. + /// - "port" is a number from the range of 0 to 65535. /// /// Once all has been validated, constructs the database access string /// expected by the lease manager. @@ -65,10 +66,9 @@ public: /// @param database_config The configuration value for the "*-database" /// identifier. /// - /// @throw isc::BadValue The 'type' keyword contains an unknown database - /// type. - /// @throw isc::dhcp::MissingTypeKeyword The 'type' keyword is missing from - /// the list of database access keywords. + /// @throw isc::dhcp::DhcpConfigError The 'type' keyword contains an + /// unknown database type or is missing from the list of + /// database access keywords. void parse(isc::dhcp::CfgDbAccessPtr& cfg_db, isc::data::ConstElementPtr database_config); @@ -94,7 +94,6 @@ protected: /// @return Database access string std::string getDbAccessString() const; - private: std::map values_; ///< Stored parameter values -- 2.47.3