Skip to content

Commit dd5047d

Browse files
author
Teodor Moroz
committed
Add ability to customize ssl mode settings PyMySQL#474(Added specific handling for MariaDB lib)
1 parent 24aaa72 commit dd5047d

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

MySQLdb/_mysql.c

+23-1
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,10 @@ _mysql_ConnectionObject_Initialize(
474474
return -1;
475475
}
476476
#else
477+
#ifndef MARIADB_BASE_VERSION
477478
PyErr_SetString(_mysql_NotSupportedError, "MySQL client library does not support ssl_mode specification");
478479
return -1;
480+
#endif
479481
#endif
480482
}
481483

@@ -486,6 +488,21 @@ _mysql_ConnectionObject_Initialize(
486488
}
487489
Py_BEGIN_ALLOW_THREADS ;
488490
self->open = 1;
491+
492+
#ifdef MARIADB_BASE_VERSION
493+
if (ssl_mode) {
494+
if (strcmp(ssl_mode, "PREFERRED") != 0)
495+
{
496+
int enforce_tls= 0;
497+
if (strcmp(ssl_mode, "REQUIRED") == 0)
498+
enforce_tls = 1;
499+
#ifdef MYSQL_OPT_SSL_ENFORCE
500+
mysql_optionsv(&(self->connection), MYSQL_OPT_SSL_ENFORCE, (void *)&enforce_tls);
501+
mysql_optionsv(&(self->connection), MYSQL_OPT_SSL_VERIFY_SERVER_CERT, (void *)&enforce_tls);
502+
#endif
503+
}
504+
}
505+
#endif
489506
if (connect_timeout) {
490507
unsigned int timeout = connect_timeout;
491508
mysql_options(&(self->connection), MYSQL_OPT_CONNECT_TIMEOUT,
@@ -522,7 +539,12 @@ _mysql_ConnectionObject_Initialize(
522539
}
523540
#ifdef HAVE_ENUM_MYSQL_OPT_SSL_MODE
524541
if (ssl_mode) {
525-
int ssl_mode_num = _get_ssl_mode_num(ssl_mode);
542+
char *corrected_ssl_mode = NULL;
543+
if (strcmp(ssl_mode, "REQUIRED") == 0 || strcmp(ssl_mode, "VERIFY_CA"))
544+
corrected_ssl_mode = "VERIFY_IDENTITY";
545+
else
546+
corrected_ssl_mode = ssl_mode;
547+
int ssl_mode_num = _get_ssl_mode_num(corrected_ssl_mode);
526548
mysql_options(&(self->connection), MYSQL_OPT_SSL_MODE, &ssl_mode_num);
527549
}
528550
#endif

0 commit comments

Comments
 (0)