Skip to content

Commit

Permalink
Added missing error messages for wrong protocol
Browse files Browse the repository at this point in the history
moved connection handler into net->extension (ABI break)
  • Loading branch information
9EOR9 committed Feb 23, 2016
1 parent 38b7870 commit 8620b75
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 48 deletions.
8 changes: 5 additions & 3 deletions include/ma_errmsg.h
Expand Up @@ -72,16 +72,18 @@ extern const char *mariadb_client_errors[]; /* Error messages */
#define CR_SHARED_MEMORY_CONNECTION 2037
#define CR_SHARED_MEMORY_CONNECT_ERROR 2038

#define CR_CONN_UNKNOWN_PROTOCOL 2047
#define CR_SECURE_AUTH 2049
#define CR_NO_DATA 2051
#define CR_NO_STMT_METADATA 2052
#define CR_NOT_IMPLEMENTED 2054
#define CR_SERVER_LOST_EXTENDED 2055
#define CR_STMT_CLOSED 2056
#define CR_NEW_STMT_METADATA 2057
#define CR_AUTH_PLUGIN_CANNOT_LOAD 2058
#define CR_ALREADY_CONNECTED 2059
#define CR_PLUGIN_FUNCTION_NOT_SUPPORTED 2060
#define CR_ALREADY_CONNECTED 2058
#define CR_AUTH_PLUGIN_CANNOT_LOAD 2059
#define CR_DUPLICATE_CONNECTION_ATTR 2060
#define CR_AUTH_PLUGIN_ERR 2061

/*
* MariaDB Connector/C errors:
Expand Down
2 changes: 1 addition & 1 deletion include/mariadb_com.h
Expand Up @@ -32,7 +32,7 @@
#define NAME_CHAR_LEN 64
#define NAME_LEN 256 /* Field/table name length */
#define HOSTNAME_LENGTH 60
#define SYSTEM_MB_MAX_CHAR_LENGTH 3
#define SYSTEM_MB_MAX_CHAR_LENGTH 4
#define USERNAME_CHAR_LENGTH 128
#define USERNAME_LENGTH USERNAME_CHAR_LENGTH * SYSTEM_MB_MAX_CHAR_LENGTH
#define SERVER_VERSION_LENGTH 60
Expand Down
2 changes: 1 addition & 1 deletion include/mysql/plugin_auth.h
Expand Up @@ -25,7 +25,7 @@
#define MYSQL_PLUGIN_AUTH_COMMON_INCLUDED

/** the max allowed length for a user name */
#define MYSQL_USERNAME_LENGTH 48
#define MYSQL_USERNAME_LENGTH 512

/**
return values of the plugin authenticate_user() method.
Expand Down
2 changes: 1 addition & 1 deletion include/mysql/plugin_auth_common.h
Expand Up @@ -25,7 +25,7 @@
#define MYSQL_PLUGIN_AUTH_COMMON_INCLUDED

/** the max allowed length for a user name */
#define MYSQL_USERNAME_LENGTH 48
#define MYSQL_USERNAME_LENGTH 512

/**
return values of the plugin authenticate_user() method.
Expand Down
5 changes: 3 additions & 2 deletions libmariadb/ma_errmsg.c
Expand Up @@ -127,7 +127,7 @@ const char *client_errors[]=
/* 2044 */ "",
/* 2045 */ "",
/* 2046 */ "",
/* 2047 */ "",
/* 2047 */ "Wrong or unknown protocol",
/* 2048 */ "",
/* 2049 */ "Connection with old authentication protocol refused.",
/* 2050 */ "",
Expand All @@ -138,8 +138,9 @@ const char *client_errors[]=
/* 2055 */ "Lost connection to MySQL server at '%s', system error: %d",
/* 2056 */ "Server closed statement due to a prior %s function call",
/* 2057 */ "The number of parameters in bound buffers differs from number of columns in resultset",
/* 2058 */ "Plugin %s could not be loaded: %s",
/* 2059 */ "Can't connect twice. Already connected",
/* 2058 */ "Plugin %s could not be loaded: %s",
/* 2059 */ "An attribute with same name already exists"
/* 2060 */ "Plugin doesn't support this function",
""
};
Expand Down
30 changes: 26 additions & 4 deletions libmariadb/mariadb_lib.c
Expand Up @@ -115,6 +115,14 @@ my_context_install_suspend_resume_hook(struct mysql_async_context *b,
uint mysql_port=0;
my_string mysql_unix_port=0;

static char *mariadb_protocols[]= {"TCP",
#ifndef WIN32
"SOCKET",
#else
"PIPE", "MEMORY",
#endif
0};

#ifdef _WIN32
#define CONNECT_TIMEOUT 20
#else
Expand Down Expand Up @@ -441,6 +449,7 @@ static void free_old_query(MYSQL *mysql)
ma_init_ma_alloc_root(&mysql->field_alloc,8192,0); /* Assume rowlength < 8192 */
mysql->fields=0;
mysql->field_count=0; /* For API */
mysql->info= 0;
return;
}

Expand Down Expand Up @@ -1185,6 +1194,14 @@ MYSQL *mthd_my_real_connect(MYSQL *mysql, const char *host, const char *user,
mysql->options.my_cnf_file=mysql->options.my_cnf_group=0;
}

#ifndef WIN32
if (mysql->options.protocol > MYSQL_PROTOCOL_SOCKET)
{
SET_CLIENT_ERROR(mysql, CR_CONN_UNKNOWN_PROTOCOL, SQLSTATE_UNKNOWN, 0);
return(NULL);
}
#endif

/* Some empty-string-tests are done because of ODBC */
if (!host || !host[0])
host=mysql->options.host;
Expand All @@ -1207,10 +1224,8 @@ MYSQL *mthd_my_real_connect(MYSQL *mysql, const char *host, const char *user,
if (!unix_socket)
unix_socket=mysql->options.unix_socket;


mysql->server_status=SERVER_STATUS_AUTOCOMMIT;


/* try to connect via pvio_init */
cinfo.host= host;
cinfo.unix_socket= unix_socket;
Expand Down Expand Up @@ -1622,6 +1637,7 @@ my_bool STDCALL mysql_reconnect(MYSQL *mysql)
mysql->net.pvio->mysql= mysql;
ma_net_clear(&mysql->net);
mysql->affected_rows= ~(my_ulonglong) 0;
mysql->info= 0;
return(0);
}

Expand Down Expand Up @@ -2502,10 +2518,10 @@ mysql_optionsv(MYSQL *mysql,enum mysql_option option, ...)
OPT_SET_VALUE_STR(&mysql->options, charset_name, (char *)arg1);
break;
case MYSQL_OPT_RECONNECT:
mysql->options.reconnect= *(uint *)arg1;
mysql->options.reconnect= *(my_bool *)arg1;
break;
case MYSQL_OPT_PROTOCOL:
mysql->options.protocol= *(uint *)arg1;
mysql->options.protocol= *((uint *)arg1);
break;
case MYSQL_OPT_READ_TIMEOUT:
mysql->options.read_timeout= *(uint *)arg1;
Expand Down Expand Up @@ -2579,6 +2595,9 @@ mysql_optionsv(MYSQL *mysql,enum mysql_option option, ...)
case MYSQL_OPT_NET_BUFFER_LENGTH:
net_buffer_length= (*(size_t *)arg1);
break;
case MYSQL_OPT_SSL_ENFORCE:
mysql->options.use_ssl= (*(my_bool *)arg1);
break;
case MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
if (*(my_bool *)arg1)
mysql->options.client_flag |= CLIENT_SSL_VERIFY_SERVER_CERT;
Expand Down Expand Up @@ -2922,6 +2941,9 @@ mysql_get_optionv(MYSQL *mysql, enum mysql_option option, void *arg, ...)
else
return(-1);
break;
case MYSQL_OPT_SSL_ENFORCE:
*((my_bool *)arg)= mysql->options.use_ssl;
break;
case MYSQL_OPT_SSL_VERIFY_SERVER_CERT:
*((my_bool *)arg)= test(mysql->options.client_flag & CLIENT_SSL_VERIFY_SERVER_CERT);
break;
Expand Down
22 changes: 11 additions & 11 deletions libmariadb/secure/openssl.c
Expand Up @@ -64,7 +64,6 @@ static void ma_ssl_set_error(MYSQL *mysql)
}
if ((ssl_error_reason= ERR_reason_error_string(ssl_errno)))
{
printf("reason: %s\n", ssl_error_reason);
pvio->set_error(mysql, CR_SSL_CONNECTION_ERROR, SQLSTATE_UNKNOWN,
0, ssl_error_reason);
return;
Expand Down Expand Up @@ -424,19 +423,20 @@ my_bool ma_ssl_connect(MARIADB_SSL *cssl)
pvio->methods->blocking(pvio, FALSE, 0);
return 1;
}
rc= SSL_get_verify_result(ssl);
if (rc != X509_V_OK)
if ((mysql->client_flag & CLIENT_SSL_VERIFY_SERVER_CERT))
{
printf("--------------------\n");
my_set_error(mysql, CR_SSL_CONNECTION_ERROR, SQLSTATE_UNKNOWN,
ER(CR_SSL_CONNECTION_ERROR), X509_verify_cert_error_string(rc));
/* restore blocking mode */
if (!blocking)
pvio->methods->blocking(pvio, FALSE, 0);
rc= SSL_get_verify_result(ssl);
if (rc != X509_V_OK)
{
my_set_error(mysql, CR_SSL_CONNECTION_ERROR, SQLSTATE_UNKNOWN,
ER(CR_SSL_CONNECTION_ERROR), X509_verify_cert_error_string(rc));
/* restore blocking mode */
if (!blocking)
pvio->methods->blocking(pvio, FALSE, 0);

return 1;
return 1;
}
}

pvio->cssl->ssl= cssl->ssl= (void *)ssl;

return 0;
Expand Down
2 changes: 1 addition & 1 deletion plugins/auth/my_auth.c
Expand Up @@ -175,7 +175,7 @@ static int send_client_reply_packet(MCPVIO_EXT *mpvio,
#if defined(HAVE_SSL) && !defined(EMBEDDED_LIBRARY)
if (mysql->options.ssl_key || mysql->options.ssl_cert ||
mysql->options.ssl_ca || mysql->options.ssl_capath ||
mysql->options.ssl_cipher ||
mysql->options.ssl_cipher || mysql->options.use_ssl ||
(mysql->options.client_flag & CLIENT_SSL_VERIFY_SERVER_CERT))
mysql->options.use_ssl= 1;
if (mysql->options.use_ssl)
Expand Down
38 changes: 19 additions & 19 deletions plugins/connection/aurora.c
Expand Up @@ -245,21 +245,21 @@ my_bool aurora_parse_url(const char *url, AURORA *aurora)
int aurora_get_instance_type(MYSQL *mysql)
{
int rc= -1;
MA_CONNECTION_HANDLER *save_hdlr= mysql->net.conn_hdlr;
MA_CONNECTION_HANDLER *save_hdlr= mysql->net.extension->conn_hdlr;

char *query= "select variable_value from information_schema.global_variables where variable_name='INNODB_READ_ONLY' AND variable_value='OFF'";

if (!mysql)
return -1;

mysql->net.conn_hdlr= 0;
mysql->net.extension->conn_hdlr= 0;
if (!mariadb_api->mysql_query(mysql, query))
{
MYSQL_RES *res= mariadb_api->mysql_store_result(mysql);
rc= mysql_num_rows(res) ? AURORA_PRIMARY : AURORA_REPLICA;
mariadb_api->mysql_free_result(res);
}
mysql->net.conn_hdlr= save_hdlr;
mysql->net.extension->conn_hdlr= save_hdlr;
return rc;
}
/* }}} */
Expand All @@ -280,9 +280,9 @@ int aurora_get_instance_type(MYSQL *mysql)
my_bool aurora_get_primary_id(MYSQL *mysql, AURORA *aurora)
{
my_bool rc= 0;
MA_CONNECTION_HANDLER *save_hdlr= mysql->net.conn_hdlr;
MA_CONNECTION_HANDLER *save_hdlr= mysql->net.extension->conn_hdlr;

mysql->net.conn_hdlr= 0;
mysql->net.extension->conn_hdlr= 0;
if (!mariadb_api->mysql_query(mysql, "select server_id from information_schema.replica_host_status "
"where session_id = 'MASTER_SESSION_ID'"))
{
Expand All @@ -302,7 +302,7 @@ my_bool aurora_get_primary_id(MYSQL *mysql, AURORA *aurora)
mariadb_api->mysql_free_result(res);
}
}
mysql->net.conn_hdlr= save_hdlr;
mysql->net.extension->conn_hdlr= save_hdlr;
return rc;
}
/* }}} */
Expand Down Expand Up @@ -393,7 +393,7 @@ void aurora_close_internal(MYSQL *mysql)
{
if (mysql)
{
mysql->net.conn_hdlr= 0;
mysql->net.extension->conn_hdlr= 0;
memset(&mysql->options, 0, sizeof(struct st_mysql_options));
mariadb_api->mysql_close(mysql);
}
Expand Down Expand Up @@ -421,7 +421,7 @@ my_bool aurora_find_replica(AURORA *aurora)
mysql->options= aurora->save_mysql.options;

/* don't execute init_command on slave */
// mysql->net.conn_hdlr= aurora->save_mysql.net.conn_hdlr;
// mysql->net.extension->conn_hdlr= aurora->save_mysql.net.extension->conn_hdlr;
if ((aurora_connect_instance(aurora, instance[random_pick], mysql)))
{
switch (instance[random_pick]->type) {
Expand Down Expand Up @@ -522,7 +522,7 @@ MYSQL *aurora_connect(MYSQL *mysql, const char *host, const char *user, const ch
const char *db, unsigned int port, const char *unix_socket, unsigned long client_flag)
{
AURORA *aurora= NULL;
MA_CONNECTION_HANDLER *save_hdlr= mysql->net.conn_hdlr;
MA_CONNECTION_HANDLER *save_hdlr= mysql->net.extension->conn_hdlr;

if (!mariadb_api)
mariadb_api= mysql->methods->api;
Expand Down Expand Up @@ -564,15 +564,15 @@ MYSQL *aurora_connect(MYSQL *mysql, const char *host, const char *user, const ch
if (!aurora_find_replica(aurora))
aurora->mysql[AURORA_REPLICA]= NULL;
else
aurora->mysql[AURORA_REPLICA]->net.conn_hdlr= save_hdlr;
aurora->mysql[AURORA_REPLICA]->net.extension->conn_hdlr= save_hdlr;
}

if (!aurora->mysql[AURORA_PRIMARY])
{
if (!aurora_find_primary(aurora))
aurora->mysql[AURORA_PRIMARY]= NULL;
else
aurora->mysql[AURORA_PRIMARY]->net.conn_hdlr= save_hdlr;
aurora->mysql[AURORA_PRIMARY]->net.extension->conn_hdlr= save_hdlr;
}

if (!aurora->mysql[AURORA_PRIMARY] && !aurora->mysql[AURORA_REPLICA])
Expand All @@ -582,7 +582,7 @@ MYSQL *aurora_connect(MYSQL *mysql, const char *host, const char *user, const ch
aurora_switch_connection(mysql, aurora, AURORA_PRIMARY);
else
aurora_switch_connection(mysql, aurora, AURORA_REPLICA);
mysql->net.conn_hdlr= save_hdlr;
mysql->net.extension->conn_hdlr= save_hdlr;
return mysql;
error:
aurora_close_memory(aurora);
Expand All @@ -594,7 +594,7 @@ MYSQL *aurora_connect(MYSQL *mysql, const char *host, const char *user, const ch
my_bool aurora_reconnect(MYSQL *mysql)
{
AURORA *aurora;
MA_CONNECTION_HANDLER *save_hdlr= mysql->net.conn_hdlr;
MA_CONNECTION_HANDLER *save_hdlr= mysql->net.extension->conn_hdlr;
int i;

/* We can't determine if a new primary was promotoed, or if
Expand Down Expand Up @@ -647,7 +647,7 @@ my_bool aurora_reconnect(MYSQL *mysql)
/* {{{ void aurora_close */
void aurora_close(MYSQL *mysql)
{
MA_CONNECTION_HANDLER *hdlr= mysql->net.conn_hdlr;
MA_CONNECTION_HANDLER *hdlr= mysql->net.extension->conn_hdlr;
AURORA *aurora;
int i;

Expand Down Expand Up @@ -675,7 +675,7 @@ void aurora_close(MYSQL *mysql)
/* free information */
end:
aurora_close_memory(aurora);
mysql->net.conn_hdlr= hdlr;
mysql->net.extension->conn_hdlr= hdlr;
}
/* }}} */

Expand Down Expand Up @@ -718,7 +718,7 @@ my_bool is_replica_stmt(MYSQL *mysql, const char *buffer)
int aurora_command(MYSQL *mysql,enum enum_server_command command, const char *arg,
size_t length, my_bool skipp_check, void *opt_arg)
{
MA_CONNECTION_HANDLER *save_hdlr= mysql->net.conn_hdlr;
MA_CONNECTION_HANDLER *save_hdlr= mysql->net.extension->conn_hdlr;
AURORA *aurora= (AURORA *)save_hdlr->data;

/* if we don't have slave or slave became unavailable root traffic to master */
Expand All @@ -736,9 +736,9 @@ int aurora_command(MYSQL *mysql,enum enum_server_command command, const char *ar
/* we need to change default database on primary and replica */
if (aurora->mysql[AURORA_REPLICA] && mysql->thread_id == aurora->mysql[AURORA_PRIMARY]->thread_id)
{
aurora->mysql[AURORA_REPLICA]->net.conn_hdlr= 0;
aurora->mysql[AURORA_REPLICA]->net.extension->conn_hdlr= 0;
mariadb_api->mysql_select_db(aurora->mysql[AURORA_REPLICA], arg);
aurora->mysql[AURORA_REPLICA]->net.conn_hdlr= mysql->net.conn_hdlr;
aurora->mysql[AURORA_REPLICA]->net.extension->conn_hdlr= mysql->net.extension->conn_hdlr;
}
break;
case COM_QUERY:
Expand All @@ -763,7 +763,7 @@ int aurora_command(MYSQL *mysql,enum enum_server_command command, const char *ar
break;
}
end:
mysql->net.conn_hdlr= save_hdlr;
mysql->net.extension->conn_hdlr= save_hdlr;
return 0;
}
/* }}} */

0 comments on commit 8620b75

Please sign in to comment.