@@ -34,6 +34,14 @@ PERFORMANCE OF THIS SOFTWARE.
34
34
#define my_bool _Bool
35
35
#endif
36
36
37
+ #if ((MYSQL_VERSION_ID >= 50555 && MYSQL_VERSION_ID <= 50599 ) || \
38
+ (MYSQL_VERSION_ID >= 50636 && MYSQL_VERSION_ID <= 50699 ) || \
39
+ (MYSQL_VERSION_ID >= 50711 && MYSQL_VERSION_ID <= 50799 ) || \
40
+ (MYSQL_VERSION_ID >= 80000 )) && \
41
+ !defined(MARIADB_BASE_VERSION ) && !defined(MARIADB_VERSION_ID )
42
+ #define HAVE_ENUM_MYSQL_OPT_SSL_MODE
43
+ #endif
44
+
37
45
#define PY_SSIZE_T_CLEAN 1
38
46
#include "Python.h"
39
47
@@ -371,6 +379,23 @@ static int _mysql_ResultObject_clear(_mysql_ResultObject *self)
371
379
return 0 ;
372
380
}
373
381
382
+ #ifdef HAVE_ENUM_MYSQL_OPT_SSL_MODE
383
+ static int
384
+ _get_ssl_mode_num (char * ssl_mode )
385
+ {
386
+ static char * ssl_mode_list [] = { "DISABLED" , "PREFERRED" ,
387
+ "REQUIRED" , "VERIFY_CA" , "VERIFY_IDENTITY" };
388
+ unsigned int i ;
389
+ for (i = 0 ; i < sizeof (ssl_mode_list )/sizeof (ssl_mode_list [0 ]); i ++ ) {
390
+ if (strcmp (ssl_mode , ssl_mode_list [i ]) == 0 ) {
391
+ // SSL_MODE one-based
392
+ return i + 1 ;
393
+ }
394
+ }
395
+ return -1 ;
396
+ }
397
+ #endif
398
+
374
399
static int
375
400
_mysql_ConnectionObject_Initialize (
376
401
_mysql_ConnectionObject * self ,
@@ -380,6 +405,7 @@ _mysql_ConnectionObject_Initialize(
380
405
MYSQL * conn = NULL ;
381
406
PyObject * conv = NULL ;
382
407
PyObject * ssl = NULL ;
408
+ char * ssl_mode = NULL ;
383
409
const char * key = NULL , * cert = NULL , * ca = NULL ,
384
410
* capath = NULL , * cipher = NULL ;
385
411
PyObject * ssl_keepref [5 ] = {NULL };
@@ -393,7 +419,7 @@ _mysql_ConnectionObject_Initialize(
393
419
"connect_timeout" , "compress" ,
394
420
"named_pipe" , "init_command" ,
395
421
"read_default_file" , "read_default_group" ,
396
- "client_flag" , "ssl" ,
422
+ "client_flag" , "ssl" , "ssl_mode" ,
397
423
"local_infile" ,
398
424
"read_timeout" , "write_timeout" , "charset" ,
399
425
"auth_plugin" ,
@@ -412,15 +438,15 @@ _mysql_ConnectionObject_Initialize(
412
438
self -> open = 0 ;
413
439
414
440
if (!PyArg_ParseTupleAndKeywords (args , kwargs ,
415
- "|ssssisOiiisssiOiiiss :connect" ,
441
+ "|ssssisOiiisssiOsiiiss :connect" ,
416
442
kwlist ,
417
443
& host , & user , & passwd , & db ,
418
444
& port , & unix_socket , & conv ,
419
445
& connect_timeout ,
420
446
& compress , & named_pipe ,
421
447
& init_command , & read_default_file ,
422
448
& read_default_group ,
423
- & client_flag , & ssl ,
449
+ & client_flag , & ssl , & ssl_mode ,
424
450
& local_infile ,
425
451
& read_timeout ,
426
452
& write_timeout ,
@@ -441,6 +467,17 @@ _mysql_ConnectionObject_Initialize(
441
467
_stringsuck (key , value , ssl );
442
468
_stringsuck (cipher , value , ssl );
443
469
}
470
+ if (ssl_mode ) {
471
+ #ifdef HAVE_ENUM_MYSQL_OPT_SSL_MODE
472
+ if (_get_ssl_mode_num (ssl_mode ) <= 0 ) {
473
+ PyErr_SetString (_mysql_NotSupportedError , "Unknown ssl_mode specification" );
474
+ return -1 ;
475
+ }
476
+ #else
477
+ PyErr_SetString (_mysql_NotSupportedError , "MySQL client library does not support ssl_mode specification" );
478
+ return -1 ;
479
+ #endif
480
+ }
444
481
445
482
conn = mysql_init (& (self -> connection ));
446
483
if (!conn ) {
@@ -483,6 +520,12 @@ _mysql_ConnectionObject_Initialize(
483
520
if (ssl ) {
484
521
mysql_ssl_set (& (self -> connection ), key , cert , ca , capath , cipher );
485
522
}
523
+ #ifdef HAVE_ENUM_MYSQL_OPT_SSL_MODE
524
+ if (ssl_mode ) {
525
+ int ssl_mode_num = _get_ssl_mode_num (ssl_mode );
526
+ mysql_options (& (self -> connection ), MYSQL_OPT_SSL_MODE , & ssl_mode_num );
527
+ }
528
+ #endif
486
529
if (charset ) {
487
530
mysql_options (& (self -> connection ), MYSQL_SET_CHARSET_NAME , charset );
488
531
}
0 commit comments