mysql_common.c and mysql_client.c astyled

This commit is contained in:
Johan Wikman
2016-10-18 20:42:24 +03:00
parent 623acfe1b5
commit e9f94dcd9d
2 changed files with 164 additions and 164 deletions

View File

@ -59,9 +59,9 @@
#include <maxscale/gw_authenticator.h> #include <maxscale/gw_authenticator.h>
/* @see function load_module in load_utils.c for explanation of the following /* @see function load_module in load_utils.c for explanation of the following
* lint directives. * lint directives.
*/ */
/*lint -e14 */ /*lint -e14 */
MODULE_INFO info = MODULE_INFO info =
{ {
@ -90,7 +90,7 @@ static void mysql_client_auth_error_handling(DCB *dcb, int auth_val);
static int gw_read_do_authentication(DCB *dcb, GWBUF *read_buffer, int nbytes_read); static int gw_read_do_authentication(DCB *dcb, GWBUF *read_buffer, int nbytes_read);
static int gw_read_normal_data(DCB *dcb, GWBUF *read_buffer, int nbytes_read); static int gw_read_normal_data(DCB *dcb, GWBUF *read_buffer, int nbytes_read);
static int gw_read_finish_processing(DCB *dcb, GWBUF *read_buffer, uint8_t capabilities); static int gw_read_finish_processing(DCB *dcb, GWBUF *read_buffer, uint8_t capabilities);
extern char* create_auth_fail_str(char *username, char *hostaddr, char *sha1, char *db,int); extern char* create_auth_fail_str(char *username, char *hostaddr, char *sha1, char *db, int);
static bool ensure_complete_packet(DCB *dcb, GWBUF **read_buffer, int nbytes_read); static bool ensure_complete_packet(DCB *dcb, GWBUF **read_buffer, int nbytes_read);
static void gw_process_one_new_client(DCB *client_dcb); static void gw_process_one_new_client(DCB *client_dcb);
@ -185,7 +185,7 @@ int MySQLSendHandshake(DCB* dcb)
uint8_t mysql_scramble_len = 21; uint8_t mysql_scramble_len = 21;
uint8_t mysql_filler_ten[10]; uint8_t mysql_filler_ten[10];
/* uint8_t mysql_last_byte = 0x00; not needed */ /* uint8_t mysql_last_byte = 0x00; not needed */
char server_scramble[GW_MYSQL_SCRAMBLE_SIZE + 1]=""; char server_scramble[GW_MYSQL_SCRAMBLE_SIZE + 1] = "";
char *version_string; char *version_string;
int len_version_string = 0; int len_version_string = 0;
int id_num; int id_num;
@ -249,7 +249,7 @@ int MySQLSendHandshake(DCB* dcb)
gw_mysql_set_byte3(mysql_packet_header, mysql_payload_size); gw_mysql_set_byte3(mysql_packet_header, mysql_payload_size);
// write packet number, now is 0 // write packet number, now is 0
mysql_packet_header[3]= mysql_packet_id; mysql_packet_header[3] = mysql_packet_id;
memcpy(outbuf, mysql_packet_header, sizeof(mysql_packet_header)); memcpy(outbuf, mysql_packet_header, sizeof(mysql_packet_header));
// current buffer pointer // current buffer pointer
@ -427,19 +427,19 @@ int gw_read_client_event(DCB* dcb)
* will be changed to MYSQL_IDLE (see below). * will be changed to MYSQL_IDLE (see below).
* *
*/ */
case MXS_AUTH_STATE_MESSAGE_READ: case MXS_AUTH_STATE_MESSAGE_READ:
/* After this call read_buffer will point to freed data */ /* After this call read_buffer will point to freed data */
if (nbytes_read < 3 || (0 == max_bytes && nbytes_read < if (nbytes_read < 3 || (0 == max_bytes && nbytes_read <
(MYSQL_GET_PACKET_LEN((uint8_t *) GWBUF_DATA(read_buffer)) + 4)) || (MYSQL_GET_PACKET_LEN((uint8_t *) GWBUF_DATA(read_buffer)) + 4)) ||
(0 != max_bytes && nbytes_read < max_bytes)) (0 != max_bytes && nbytes_read < max_bytes))
{ {
spinlock_acquire(&dcb->authlock); spinlock_acquire(&dcb->authlock);
dcb->dcb_readqueue = read_buffer; dcb->dcb_readqueue = read_buffer;
spinlock_release(&dcb->authlock); spinlock_release(&dcb->authlock);
return 0; return 0;
} }
return_code = gw_read_do_authentication(dcb, read_buffer, nbytes_read); return_code = gw_read_do_authentication(dcb, read_buffer, nbytes_read);
break; break;
/** /**
* *
@ -448,19 +448,19 @@ int gw_read_client_event(DCB* dcb)
* result in a call that comes to this section of code. * result in a call that comes to this section of code.
* *
*/ */
case MXS_AUTH_STATE_COMPLETE: case MXS_AUTH_STATE_COMPLETE:
/* After this call read_buffer will point to freed data */ /* After this call read_buffer will point to freed data */
return_code = gw_read_normal_data(dcb, read_buffer, nbytes_read); return_code = gw_read_normal_data(dcb, read_buffer, nbytes_read);
break; break;
case MXS_AUTH_STATE_FAILED: case MXS_AUTH_STATE_FAILED:
gwbuf_free(read_buffer); gwbuf_free(read_buffer);
return_code = 1; return_code = 1;
break; break;
default: default:
MXS_ERROR("In mysql_client.c unexpected protocol authentication state"); MXS_ERROR("In mysql_client.c unexpected protocol authentication state");
break; break;
} }
return return_code; return return_code;
@ -799,7 +799,7 @@ gw_read_normal_data(DCB *dcb, GWBUF *read_buffer, int nbytes_read)
/** Ask what type of input the router expects */ /** Ask what type of input the router expects */
capabilities = session->service->router->getCapabilities( capabilities = session->service->router->getCapabilities(
session->service->router_instance, session->router_session); session->service->router_instance, session->router_session);
/** Update the current protocol command being executed */ /** Update the current protocol command being executed */
if (!process_client_commands(dcb, nbytes_read, &read_buffer)) if (!process_client_commands(dcb, nbytes_read, &read_buffer))
@ -933,84 +933,84 @@ mysql_client_auth_error_handling(DCB *dcb, int auth_val)
switch (auth_val) switch (auth_val)
{ {
case MXS_AUTH_NO_SESSION: case MXS_AUTH_NO_SESSION:
MXS_DEBUG("%lu [gw_read_client_event] session " MXS_DEBUG("%lu [gw_read_client_event] session "
"creation failed. fd %d, " "creation failed. fd %d, "
"state = MYSQL_AUTH_NO_SESSION.", "state = MYSQL_AUTH_NO_SESSION.",
pthread_self(), pthread_self(),
dcb->fd); dcb->fd);
/** Send ERR 1045 to client */ /** Send ERR 1045 to client */
mysql_send_auth_error(dcb, mysql_send_auth_error(dcb,
packet_number, packet_number,
0, 0,
"failed to create new session"); "failed to create new session");
break; break;
case MXS_AUTH_FAILED_DB: case MXS_AUTH_FAILED_DB:
MXS_DEBUG("%lu [gw_read_client_event] database " MXS_DEBUG("%lu [gw_read_client_event] database "
"specified was not valid. fd %d, " "specified was not valid. fd %d, "
"state = MYSQL_FAILED_AUTH_DB.", "state = MYSQL_FAILED_AUTH_DB.",
pthread_self(), pthread_self(),
dcb->fd); dcb->fd);
/** Send error 1049 to client */ /** Send error 1049 to client */
message_len = 25 + MYSQL_DATABASE_MAXLEN; message_len = 25 + MYSQL_DATABASE_MAXLEN;
fail_str = MXS_CALLOC(1, message_len+1); fail_str = MXS_CALLOC(1, message_len + 1);
MXS_ABORT_IF_NULL(fail_str); MXS_ABORT_IF_NULL(fail_str);
snprintf(fail_str, message_len, "Unknown database '%s'", snprintf(fail_str, message_len, "Unknown database '%s'",
(char*)((MYSQL_session *)dcb->data)->db); (char*)((MYSQL_session *)dcb->data)->db);
modutil_send_mysql_err_packet(dcb, packet_number, 0, 1049, "42000", fail_str); modutil_send_mysql_err_packet(dcb, packet_number, 0, 1049, "42000", fail_str);
break; break;
case MXS_AUTH_FAILED_SSL: case MXS_AUTH_FAILED_SSL:
MXS_DEBUG("%lu [gw_read_client_event] client is " MXS_DEBUG("%lu [gw_read_client_event] client is "
"not SSL capable for SSL listener. fd %d, " "not SSL capable for SSL listener. fd %d, "
"state = MYSQL_FAILED_AUTH_SSL.", "state = MYSQL_FAILED_AUTH_SSL.",
pthread_self(), pthread_self(),
dcb->fd); dcb->fd);
/** Send ERR 1045 to client */ /** Send ERR 1045 to client */
mysql_send_auth_error(dcb, mysql_send_auth_error(dcb,
packet_number, packet_number,
0, 0,
"failed to complete SSL authentication"); "failed to complete SSL authentication");
break; break;
case MXS_AUTH_SSL_INCOMPLETE: case MXS_AUTH_SSL_INCOMPLETE:
MXS_DEBUG("%lu [gw_read_client_event] unable to " MXS_DEBUG("%lu [gw_read_client_event] unable to "
"complete SSL authentication. fd %d, " "complete SSL authentication. fd %d, "
"state = MYSQL_AUTH_SSL_INCOMPLETE.", "state = MYSQL_AUTH_SSL_INCOMPLETE.",
pthread_self(), pthread_self(),
dcb->fd); dcb->fd);
/** Send ERR 1045 to client */ /** Send ERR 1045 to client */
mysql_send_auth_error(dcb, mysql_send_auth_error(dcb,
packet_number, packet_number,
0, 0,
"failed to complete SSL authentication"); "failed to complete SSL authentication");
break; break;
case MXS_AUTH_FAILED: case MXS_AUTH_FAILED:
MXS_DEBUG("%lu [gw_read_client_event] authentication failed. fd %d, " MXS_DEBUG("%lu [gw_read_client_event] authentication failed. fd %d, "
"state = MYSQL_FAILED_AUTH.", "state = MYSQL_FAILED_AUTH.",
pthread_self(), pthread_self(),
dcb->fd); dcb->fd);
/** Send error 1045 to client */ /** Send error 1045 to client */
fail_str = create_auth_fail_str((char *)((MYSQL_session *)dcb->data)->user, fail_str = create_auth_fail_str((char *)((MYSQL_session *)dcb->data)->user,
dcb->remote, dcb->remote,
(char*)((MYSQL_session *)dcb->data)->client_sha1, (char*)((MYSQL_session *)dcb->data)->client_sha1,
(char*)((MYSQL_session *)dcb->data)->db, auth_val); (char*)((MYSQL_session *)dcb->data)->db, auth_val);
modutil_send_mysql_err_packet(dcb, packet_number, 0, 1045, "28000", fail_str); modutil_send_mysql_err_packet(dcb, packet_number, 0, 1045, "28000", fail_str);
break; break;
default: default:
MXS_DEBUG("%lu [gw_read_client_event] authentication failed. fd %d, " MXS_DEBUG("%lu [gw_read_client_event] authentication failed. fd %d, "
"state unrecognized.", "state unrecognized.",
pthread_self(), pthread_self(),
dcb->fd); dcb->fd);
/** Send error 1045 to client */ /** Send error 1045 to client */
fail_str = create_auth_fail_str((char *)((MYSQL_session *)dcb->data)->user, fail_str = create_auth_fail_str((char *)((MYSQL_session *)dcb->data)->user,
dcb->remote, dcb->remote,
(char*)((MYSQL_session *)dcb->data)->client_sha1, (char*)((MYSQL_session *)dcb->data)->client_sha1,
(char*)((MYSQL_session *)dcb->data)->db, auth_val); (char*)((MYSQL_session *)dcb->data)->db, auth_val);
modutil_send_mysql_err_packet(dcb, packet_number, 0, 1045, "28000", fail_str); modutil_send_mysql_err_packet(dcb, packet_number, 0, 1045, "28000", fail_str);
} }
MXS_FREE(fail_str); MXS_FREE(fail_str);
} }
@ -1145,8 +1145,8 @@ static void gw_process_one_new_client(DCB *client_dcb)
/** delete client_dcb */ /** delete client_dcb */
dcb_close(client_dcb); dcb_close(client_dcb);
MXS_ERROR("%lu [gw_MySQLAccept] Failed to create " MXS_ERROR("%lu [gw_MySQLAccept] Failed to create "
"protocol object for client connection.", "protocol object for client connection.",
pthread_self()); pthread_self());
return; return;
} }
CHK_PROTOCOL(protocol); CHK_PROTOCOL(protocol);
@ -1174,29 +1174,29 @@ static void gw_process_one_new_client(DCB *client_dcb)
{ {
/* Send a custom error as MySQL command reply */ /* Send a custom error as MySQL command reply */
mysql_send_custom_error(client_dcb, mysql_send_custom_error(client_dcb,
1, 1,
0, 0,
"MaxScale encountered system limit while " "MaxScale encountered system limit while "
"attempting to register on an epoll instance."); "attempting to register on an epoll instance.");
/** close client_dcb */ /** close client_dcb */
dcb_close(client_dcb); dcb_close(client_dcb);
/** Previous state is recovered in poll_add_dcb. */ /** Previous state is recovered in poll_add_dcb. */
MXS_ERROR("%lu [gw_MySQLAccept] Failed to add dcb %p for " MXS_ERROR("%lu [gw_MySQLAccept] Failed to add dcb %p for "
"fd %d to epoll set.", "fd %d to epoll set.",
pthread_self(), pthread_self(),
client_dcb, client_dcb,
client_dcb->fd); client_dcb->fd);
return; return;
} }
else else
{ {
MXS_DEBUG("%lu [gw_MySQLAccept] Added dcb %p for fd " MXS_DEBUG("%lu [gw_MySQLAccept] Added dcb %p for fd "
"%d to epoll set.", "%d to epoll set.",
pthread_self(), pthread_self(),
client_dcb, client_dcb,
client_dcb->fd); client_dcb->fd);
} }
return; return;
} }
@ -1342,7 +1342,7 @@ static int route_by_statement(SESSION* session, GWBUF** p_readbuf)
while (tmpbuf != NULL) while (tmpbuf != NULL)
{ {
ss_dassert(GWBUF_IS_TYPE_MYSQL(tmpbuf)); ss_dassert(GWBUF_IS_TYPE_MYSQL(tmpbuf));
tmpbuf=tmpbuf->next; tmpbuf = tmpbuf->next;
} }
#endif #endif
do do

View File

@ -160,24 +160,24 @@ retblock:
*/ */
const char* gw_mysql_protocol_state2string (int state) const char* gw_mysql_protocol_state2string (int state)
{ {
switch(state) switch (state)
{ {
case MXS_AUTH_STATE_INIT: case MXS_AUTH_STATE_INIT:
return "Authentication initialized"; return "Authentication initialized";
case MXS_AUTH_STATE_PENDING_CONNECT: case MXS_AUTH_STATE_PENDING_CONNECT:
return "Network connection pending"; return "Network connection pending";
case MXS_AUTH_STATE_CONNECTED: case MXS_AUTH_STATE_CONNECTED:
return "Network connection created"; return "Network connection created";
case MXS_AUTH_STATE_MESSAGE_READ: case MXS_AUTH_STATE_MESSAGE_READ:
return "Read server handshake"; return "Read server handshake";
case MXS_AUTH_STATE_RESPONSE_SENT: case MXS_AUTH_STATE_RESPONSE_SENT:
return "Response to handshake sent"; return "Response to handshake sent";
case MXS_AUTH_STATE_FAILED: case MXS_AUTH_STATE_FAILED:
return "Authentication failed"; return "Authentication failed";
case MXS_AUTH_STATE_COMPLETE: case MXS_AUTH_STATE_COMPLETE:
return "Authentication is complete."; return "Authentication is complete.";
default: default:
return "MySQL (unknown protocol state)"; return "MySQL (unknown protocol state)";
} }
} }
@ -267,7 +267,7 @@ GWBUF* mysql_create_custom_error(int packet_number,
field_count = 0xff; field_count = 0xff;
gw_mysql_set_byte2(mysql_err, /* mysql_errno */ 2003); gw_mysql_set_byte2(mysql_err, /* mysql_errno */ 2003);
mysql_statemsg[0]='#'; mysql_statemsg[0] = '#';
memcpy(mysql_statemsg + 1, mysql_state, 5); memcpy(mysql_statemsg + 1, mysql_state, 5);
if (msg != NULL) if (msg != NULL)
@ -333,8 +333,8 @@ GWBUF* mysql_create_custom_error(int packet_number,
*/ */
GWBUF * GWBUF *
mysql_create_standard_error(int packet_number, mysql_create_standard_error(int packet_number,
int error_number, int error_number,
const char *error_message) const char *error_message)
{ {
uint8_t *outbuf = NULL; uint8_t *outbuf = NULL;
uint32_t mysql_payload_size = 0; uint32_t mysql_payload_size = 0;
@ -356,7 +356,7 @@ mysql_create_standard_error(int packet_number,
gw_mysql_set_byte3(mysql_packet_header, mysql_payload_size); gw_mysql_set_byte3(mysql_packet_header, mysql_payload_size);
// write packet number, now is 0 // write packet number, now is 0
mysql_packet_header[3]= 0; mysql_packet_header[3] = 0;
memcpy(outbuf, mysql_packet_header, sizeof(mysql_packet_header)); memcpy(outbuf, mysql_packet_header, sizeof(mysql_packet_header));
// current buffer pointer // current buffer pointer
@ -392,9 +392,9 @@ mysql_create_standard_error(int packet_number,
*/ */
int int
mysql_send_standard_error(DCB *dcb, mysql_send_standard_error(DCB *dcb,
int packet_number, int packet_number,
int error_number, int error_number,
const char *error_message) const char *error_message)
{ {
GWBUF *buf; GWBUF *buf;
buf = mysql_create_standard_error(packet_number, error_number, error_message); buf = mysql_create_standard_error(packet_number, error_number, error_message);
@ -469,7 +469,7 @@ int mysql_send_auth_error(DCB *dcb,
field_count = 0xff; field_count = 0xff;
gw_mysql_set_byte2(mysql_err, /*mysql_errno */ 1045); gw_mysql_set_byte2(mysql_err, /*mysql_errno */ 1045);
mysql_statemsg[0]='#'; mysql_statemsg[0] = '#';
memcpy(mysql_statemsg + 1, mysql_state, 5); memcpy(mysql_statemsg + 1, mysql_state, 5);
if (mysql_message != NULL) if (mysql_message != NULL)
@ -837,27 +837,27 @@ void init_response_status(GWBUF* buf,
{ {
switch (cmd) switch (cmd)
{ {
case MYSQL_COM_STMT_PREPARE: case MYSQL_COM_STMT_PREPARE:
gwbuf_copy_data(buf, 9, 2, readbuf); gwbuf_copy_data(buf, 9, 2, readbuf);
nparam = gw_mysql_get_byte2(readbuf); nparam = gw_mysql_get_byte2(readbuf);
gwbuf_copy_data(buf, 11, 2, readbuf); gwbuf_copy_data(buf, 11, 2, readbuf);
nattr = gw_mysql_get_byte2(readbuf); nattr = gw_mysql_get_byte2(readbuf);
*npackets = 1 + nparam + MXS_MIN(1, nparam) + nattr + MXS_MIN(nattr, 1); *npackets = 1 + nparam + MXS_MIN(1, nparam) + nattr + MXS_MIN(nattr, 1);
break; break;
case MYSQL_COM_QUIT: case MYSQL_COM_QUIT:
case MYSQL_COM_STMT_SEND_LONG_DATA: case MYSQL_COM_STMT_SEND_LONG_DATA:
case MYSQL_COM_STMT_CLOSE: case MYSQL_COM_STMT_CLOSE:
*npackets = 0; /*< these don't reply anything */ *npackets = 0; /*< these don't reply anything */
break; break;
default: default:
/** /**
* assume that other session commands respond * assume that other session commands respond
* OK or ERR * OK or ERR
*/ */
*npackets = 1; *npackets = 1;
break; break;
} }
} }
@ -923,7 +923,7 @@ char* create_auth_failed_msg(GWBUF*readbuf,
uint8_t* sha1) uint8_t* sha1)
{ {
char* errstr; char* errstr;
char* uname=(char *)GWBUF_DATA(readbuf) + 5; char* uname = (char *)GWBUF_DATA(readbuf) + 5;
const char* ferrstr = "Access denied for user '%s'@'%s' (using password: %s)"; const char* ferrstr = "Access denied for user '%s'@'%s' (using password: %s)";
/** -4 comes from 2X'%s' minus terminating char */ /** -4 comes from 2X'%s' minus terminating char */