Skip to content

Commit

Permalink
Disable the use of SSL options in MySQL backend with MariaDB
Browse files Browse the repository at this point in the history
MariaDB doesn't have these options, so the code didn't compile when
using it -- not supporting the options is not great, but better than
failing to build.
  • Loading branch information
vadz committed Jan 3, 2024
1 parent 65a7e4d commit 1b63f94
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/backends/mysql.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ The set of parameters used in the connection string for MySQL is:
* `connect_timeout` - should be positive integer value that means seconds corresponding to `MYSQL_OPT_CONNECT_TIMEOUT`.
* `read_timeout` - should be positive integer value that means seconds corresponding to `MYSQL_OPT_READ_TIMEOUT`.
* `write_timeout` - should be positive integer value that means seconds corresponding to `MYSQL_OPT_WRITE_TIMEOUT`.
* `ssl_mode` - should be one of the name constants `DISABLED`, `PREFERRED`, `REQUIRED`, `VERIFY_CA` or `VERIFY_IDENTITY` corresponding to `MYSQL_OPT_SSL_MODE` options.
* `ssl_mode` - should be one of the name constants `DISABLED`, `PREFERRED`, `REQUIRED`, `VERIFY_CA` or `VERIFY_IDENTITY` corresponding to `MYSQL_OPT_SSL_MODE` options (note that this option is currently not supported when using MariaDB).

Once you have created a `session` object as shown above, you can use it to access the database, for example:

Expand Down
11 changes: 11 additions & 0 deletions src/backends/mysql/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ using namespace soci;
using namespace soci::details;
using std::string;

// SSL options existing in all supported MySQL versions but not in MariaDB.
#ifndef MARIADB_VERSION_ID
#define SOCI_HAS_MYSQL_SSL_OPT
#endif

namespace
{ // anonymous
Expand Down Expand Up @@ -339,13 +343,18 @@ void parse_connect_string(const string & connectString,
*write_timeout_p = true;
} else if (par == "ssl_mode" && !*ssl_mode_p)
{
#ifdef SOCI_HAS_MYSQL_SSL_OPT
if (val=="DISABLED") *ssl_mode = SSL_MODE_DISABLED;
else if (val=="PREFERRED") *ssl_mode = SSL_MODE_PREFERRED;
else if (val=="REQUIRED") *ssl_mode = SSL_MODE_REQUIRED;
else if (val=="VERIFY_CA") *ssl_mode = SSL_MODE_VERIFY_CA;
else if (val=="VERIFY_IDENTITY") *ssl_mode = SSL_MODE_VERIFY_IDENTITY;
else throw soci_error("\"ssl_mode\" setting is invalid");
*ssl_mode_p = true;
#else
SOCI_UNUSED(ssl_mode);
throw soci_error("SSL options not supported with MariaDB");
#endif
}
else
{
Expand Down Expand Up @@ -472,6 +481,7 @@ mysql_session_backend::mysql_session_backend(
throw soci_error("mysql_options(MYSQL_OPT_WRITE_TIMEOUT) failed.");
}
}
#ifdef SOCI_HAS_MYSQL_SSL_OPT
if (ssl_mode_p)
{
if (0 != mysql_options(conn_, MYSQL_OPT_SSL_MODE, &ssl_mode))
Expand All @@ -480,6 +490,7 @@ mysql_session_backend::mysql_session_backend(
throw soci_error("mysql_options(MYSQL_OPT_SSL_MODE) failed.");
}
}
#endif
if (mysql_real_connect(conn_,
host_p ? host.c_str() : NULL,
user_p ? user.c_str() : NULL,
Expand Down

0 comments on commit 1b63f94

Please sign in to comment.