Skip to content

Commit 04b4b9b

Browse files
Will Wattsvadz
Will Watts
authored andcommitted
Add "ssl_mode" connection parameter to MySQL connection string
This allows to set MYSQL_OPT_SSL_MODE. See SOCI#1104.
1 parent e21e8a9 commit 04b4b9b

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

docs/backends/mysql.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ The set of parameters used in the connection string for MySQL is:
5454
* `connect_timeout` - should be positive integer value that means seconds corresponding to `MYSQL_OPT_CONNECT_TIMEOUT`.
5555
* `read_timeout` - should be positive integer value that means seconds corresponding to `MYSQL_OPT_READ_TIMEOUT`.
5656
* `write_timeout` - should be positive integer value that means seconds corresponding to `MYSQL_OPT_WRITE_TIMEOUT`.
57+
* `ssl_mode` - should be one of the name constants `DISABLED`, `PREFERRED`, `REQUIRED`, `VERIFY_CA` or `VERIFY_IDENTITY` corresponding to `MYSQL_OPT_SSL_MODE` options.
5758

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

src/backends/mysql/session.cpp

+24-4
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ void parse_connect_string(const string & connectString,
200200
string *charset, bool *charset_p, bool *reconnect_p,
201201
unsigned int *connect_timeout, bool *connect_timeout_p,
202202
unsigned int *read_timeout, bool *read_timeout_p,
203-
unsigned int *write_timeout, bool *write_timeout_p)
203+
unsigned int *write_timeout, bool *write_timeout_p,
204+
unsigned int *ssl_mode, bool *ssl_mode_p)
204205
{
205206
*host_p = false;
206207
*user_p = false;
@@ -217,6 +218,7 @@ void parse_connect_string(const string & connectString,
217218
*connect_timeout_p = false;
218219
*read_timeout_p = false;
219220
*write_timeout_p = false;
221+
*ssl_mode_p = false;
220222
string err = "Malformed connection string.";
221223
string::const_iterator i = connectString.begin(),
222224
end = connectString.end();
@@ -335,6 +337,15 @@ void parse_connect_string(const string & connectString,
335337
char *endp;
336338
*write_timeout = std::strtoul(val.c_str(), &endp, 10);
337339
*write_timeout_p = true;
340+
} else if (par == "ssl_mode" && !*ssl_mode_p)
341+
{
342+
if (val=="DISABLED") *ssl_mode = SSL_MODE_DISABLED;
343+
else if (val=="PREFERRED") *ssl_mode = SSL_MODE_PREFERRED;
344+
else if (val=="REQUIRED") *ssl_mode = SSL_MODE_REQUIRED;
345+
else if (val=="VERIFY_CA") *ssl_mode = SSL_MODE_VERIFY_CA;
346+
else if (val=="VERIFY_IDENTITY") *ssl_mode = SSL_MODE_VERIFY_IDENTITY;
347+
else throw soci_error("\"ssl_mode\" setting is invalid");
348+
*ssl_mode_p = true;
338349
}
339350
else
340351
{
@@ -365,18 +376,19 @@ mysql_session_backend::mysql_session_backend(
365376
string host, user, password, db, unix_socket, ssl_ca, ssl_cert, ssl_key,
366377
charset;
367378
int port, local_infile;
368-
unsigned int connect_timeout, read_timeout, write_timeout;
379+
unsigned int connect_timeout, read_timeout, write_timeout, ssl_mode;
369380
bool host_p, user_p, password_p, db_p, unix_socket_p, port_p,
370381
ssl_ca_p, ssl_cert_p, ssl_key_p, local_infile_p, charset_p, reconnect_p,
371-
connect_timeout_p, read_timeout_p, write_timeout_p;
382+
connect_timeout_p, read_timeout_p, write_timeout_p, ssl_mode_p;
372383
parse_connect_string(parameters.get_connect_string(), &host, &host_p, &user, &user_p,
373384
&password, &password_p, &db, &db_p,
374385
&unix_socket, &unix_socket_p, &port, &port_p,
375386
&ssl_ca, &ssl_ca_p, &ssl_cert, &ssl_cert_p, &ssl_key, &ssl_key_p,
376387
&local_infile, &local_infile_p, &charset, &charset_p, &reconnect_p,
377388
&connect_timeout, &connect_timeout_p,
378389
&read_timeout, &read_timeout_p,
379-
&write_timeout, &write_timeout_p);
390+
&write_timeout, &write_timeout_p,
391+
&ssl_mode, &ssl_mode_p);
380392
conn_ = mysql_init(NULL);
381393
if (conn_ == NULL)
382394
{
@@ -460,6 +472,14 @@ mysql_session_backend::mysql_session_backend(
460472
throw soci_error("mysql_options(MYSQL_OPT_WRITE_TIMEOUT) failed.");
461473
}
462474
}
475+
if (ssl_mode_p)
476+
{
477+
if (0 != mysql_options(conn_, MYSQL_OPT_SSL_MODE, &ssl_mode))
478+
{
479+
clean_up();
480+
throw soci_error("mysql_options(MYSQL_OPT_SSL_MODE) failed.");
481+
}
482+
}
463483
if (mysql_real_connect(conn_,
464484
host_p ? host.c_str() : NULL,
465485
user_p ? user.c_str() : NULL,

0 commit comments

Comments
 (0)