From 1b63f942d00ee58940aaf6094adb7071b0326460 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 3 Jan 2024 00:59:58 +0100 Subject: [PATCH] Disable the use of SSL options in MySQL backend with MariaDB 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. --- docs/backends/mysql.md | 2 +- src/backends/mysql/session.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/docs/backends/mysql.md b/docs/backends/mysql.md index 24fc796d9..071586ff3 100644 --- a/docs/backends/mysql.md +++ b/docs/backends/mysql.md @@ -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: diff --git a/src/backends/mysql/session.cpp b/src/backends/mysql/session.cpp index a292f17d9..91784ef32 100644 --- a/src/backends/mysql/session.cpp +++ b/src/backends/mysql/session.cpp @@ -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 @@ -339,6 +343,7 @@ 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; @@ -346,6 +351,10 @@ void parse_connect_string(const string & connectString, 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 { @@ -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)) @@ -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,