Skip to content

Commit 6652da2

Browse files
authored
Allow setting VERIFY_IDENTITY for MariaDB (#1205)
This adds support for setting the VERIFY_IDENTITY mode with MariaDB. On MariaDB, the `MYSQL_OPT_SSL_VERIFY_SERVER_CERT` option is available which is equivalent to `VERIFY_IDENTITY`. Also removed the check for a potential MariaDB 11.x since there's no indication that this behavior will change in MariaDB. Many containers with Ruby apps are based on Debian where MariaDB is the standard provided, so this improves support there significantly.
1 parent 9307dd9 commit 6652da2

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

ext/mysql2/client.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,12 @@ static ID intern_brackets, intern_merge, intern_merge_bang, intern_new_with_args
6969
#endif
7070

7171
/*
72-
* compatibility with mysql-connector-c 6.1.x, and with MySQL 5.7.3 - 5.7.10.
72+
* compatibility with mysql-connector-c 6.1.x, MySQL 5.7.3 - 5.7.10 & with MariaDB 10.x and later.
7373
*/
74+
#ifdef HAVE_CONST_MYSQL_OPT_SSL_VERIFY_SERVER_CERT
75+
#define SSL_MODE_VERIFY_IDENTITY 5
76+
#define HAVE_CONST_SSL_MODE_VERIFY_IDENTITY
77+
#endif
7478
#ifdef HAVE_CONST_MYSQL_OPT_SSL_ENFORCE
7579
#define SSL_MODE_DISABLED 1
7680
#define SSL_MODE_REQUIRED 3
@@ -121,19 +125,27 @@ static VALUE rb_set_ssl_mode_option(VALUE self, VALUE setting) {
121125
rb_warn( "Your mysql client library does not support setting ssl_mode; full support comes with 5.7.11." );
122126
return Qnil;
123127
}
124-
#ifdef HAVE_CONST_MYSQL_OPT_SSL_ENFORCE
128+
#if defined(HAVE_CONST_MYSQL_OPT_SSL_VERIFY_SERVER_CERT) || defined(HAVE_CONST_MYSQL_OPT_SSL_ENFORCE)
125129
GET_CLIENT(self);
126130
int val = NUM2INT( setting );
127-
// Either MySQL 5.7.3 - 5.7.10, or Connector/C 6.1.3 - 6.1.x, or MariaDB 10.x
128-
if ((version >= 50703 && version < 50711) || (version >= 60103 && version < 60200) || (version >= 100000 && version < 110000)) {
131+
// Either MySQL 5.7.3 - 5.7.10, or Connector/C 6.1.3 - 6.1.x, or MariaDB 10.x and later
132+
if ((version >= 50703 && version < 50711) || (version >= 60103 && version < 60200) || version >= 100000) {
133+
#ifdef HAVE_CONST_MYSQL_OPT_SSL_VERIFY_SERVER_CERT
134+
if (val == SSL_MODE_VERIFY_IDENTITY) {
135+
my_bool b = 1;
136+
int result = mysql_options( wrapper->client, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, &b );
137+
return INT2NUM(result);
138+
}
139+
#endif
140+
#ifdef HAVE_CONST_MYSQL_OPT_SSL_ENFORCE
129141
if (val == SSL_MODE_DISABLED || val == SSL_MODE_REQUIRED) {
130142
my_bool b = ( val == SSL_MODE_REQUIRED );
131143
int result = mysql_options( wrapper->client, MYSQL_OPT_SSL_ENFORCE, &b );
132144
return INT2NUM(result);
133-
} else {
134-
rb_warn( "MySQL client libraries between 5.7.3 and 5.7.10 only support SSL_MODE_DISABLED and SSL_MODE_REQUIRED" );
135-
return Qnil;
136145
}
146+
#endif
147+
rb_warn( "Your mysql client library does not support ssl_mode %d.", val );
148+
return Qnil;
137149
} else {
138150
rb_warn( "Your mysql client library does not support ssl_mode as expected." );
139151
return Qnil;
@@ -151,6 +163,7 @@ static VALUE rb_set_ssl_mode_option(VALUE self, VALUE setting) {
151163
return INT2NUM(result);
152164
#endif
153165
#ifdef NO_SSL_MODE_SUPPORT
166+
rb_warn( "Your mysql client library does not support setting ssl_mode; full support comes with 5.7.11." );
154167
return Qnil;
155168
#endif
156169
}
@@ -1676,10 +1689,15 @@ void init_mysql2_client() {
16761689
rb_const_set(cMysql2Client, rb_intern("SSL_MODE_REQUIRED"), INT2NUM(SSL_MODE_REQUIRED));
16771690
rb_const_set(cMysql2Client, rb_intern("SSL_MODE_VERIFY_CA"), INT2NUM(SSL_MODE_VERIFY_CA));
16781691
rb_const_set(cMysql2Client, rb_intern("SSL_MODE_VERIFY_IDENTITY"), INT2NUM(SSL_MODE_VERIFY_IDENTITY));
1679-
#elif defined(HAVE_CONST_MYSQL_OPT_SSL_ENFORCE) // MySQL 5.7.3 - 5.7.10
1692+
#else
1693+
#ifdef HAVE_CONST_MYSQL_OPT_SSL_VERIFY_SERVER_CERT // MySQL 5.7.3 - 5.7.10 & MariaDB 10.x and later
1694+
rb_const_set(cMysql2Client, rb_intern("SSL_MODE_VERIFY_IDENTITY"), INT2NUM(SSL_MODE_VERIFY_IDENTITY));
1695+
#endif
1696+
#ifdef HAVE_CONST_MYSQL_OPT_SSL_ENFORCE // MySQL 5.7.3 - 5.7.10 & MariaDB 10.x and later
16801697
rb_const_set(cMysql2Client, rb_intern("SSL_MODE_DISABLED"), INT2NUM(SSL_MODE_DISABLED));
16811698
rb_const_set(cMysql2Client, rb_intern("SSL_MODE_REQUIRED"), INT2NUM(SSL_MODE_REQUIRED));
16821699
#endif
1700+
#endif
16831701

16841702
#ifndef HAVE_CONST_SSL_MODE_DISABLED
16851703
rb_const_set(cMysql2Client, rb_intern("SSL_MODE_DISABLED"), INT2NUM(0));

ext/mysql2/extconf.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@ def add_ssl_defines(header)
1515
all_modes_found = %w[SSL_MODE_DISABLED SSL_MODE_PREFERRED SSL_MODE_REQUIRED SSL_MODE_VERIFY_CA SSL_MODE_VERIFY_IDENTITY].inject(true) do |m, ssl_mode|
1616
m && have_const(ssl_mode, header)
1717
end
18-
$CFLAGS << ' -DFULL_SSL_MODE_SUPPORT' if all_modes_found
19-
# if we only have ssl toggle (--ssl,--disable-ssl) from 5.7.3 to 5.7.10
20-
has_no_support = all_modes_found ? false : !have_const('MYSQL_OPT_SSL_ENFORCE', header)
21-
$CFLAGS << ' -DNO_SSL_MODE_SUPPORT' if has_no_support
18+
if all_modes_found
19+
$CFLAGS << ' -DFULL_SSL_MODE_SUPPORT'
20+
else
21+
# if we only have ssl toggle (--ssl,--disable-ssl) from 5.7.3 to 5.7.10
22+
# and the verify server cert option. This is also the case for MariaDB.
23+
has_verify_support = have_const('MYSQL_OPT_SSL_VERIFY_SERVER_CERT', header)
24+
has_enforce_support = have_const('MYSQL_OPT_SSL_ENFORCE', header)
25+
$CFLAGS << ' -DNO_SSL_MODE_SUPPORT' if !has_verify_support && !has_enforce_support
26+
end
2227
end
2328

2429
# Homebrew openssl
2530
if RUBY_PLATFORM =~ /darwin/ && system("command -v brew")
2631
openssl_location = `brew --prefix openssl`.strip
27-
if openssl_location
28-
$LDFLAGS << " -L#{openssl_location}/lib"
29-
end
32+
$LDFLAGS << " -L#{openssl_location}/lib" if openssl_location
3033
end
3134

3235
# 2.1+

0 commit comments

Comments
 (0)