/* {{{ mysqlnd_command::handshake */ static enum_func_status MYSQLND_METHOD(mysqlnd_command, handshake)(MYSQLND_CONN_DATA * const conn, const MYSQLND_CSTRING username, const MYSQLND_CSTRING password, const MYSQLND_CSTRING database, const size_t client_flags) { const char * const user = username.s; const char * const passwd = password.s; const size_t passwd_len = password.l; const char * const db = database.s; const size_t db_len = database.l; const size_t mysql_flags = client_flags; MYSQLND_PACKET_GREET greet_packet; DBG_ENTER("mysqlnd_command::handshake"); DBG_INF_FMT("stream=%p", conn->vio->data->m.get_stream(conn->vio)); DBG_INF_FMT("[user=%s] [db=%s:%d] [flags=%llu]", user, db, db_len, mysql_flags); conn->payload_decoder_factory->m.init_greet_packet(&greet_packet); if (FAIL == PACKET_READ(conn, &greet_packet)) { DBG_ERR("Error while reading greeting packet"); php_error_docref(NULL, E_WARNING, "Error while reading greeting packet. PID=%d", getpid()); goto err; } else if (greet_packet.error_no) { DBG_ERR_FMT("errorno=%u error=%s", greet_packet.error_no, greet_packet.error); SET_CLIENT_ERROR(conn->error_info, greet_packet.error_no, greet_packet.sqlstate, greet_packet.error); goto err; } else if (greet_packet.pre41) { DBG_ERR_FMT("Connecting to 3.22, 3.23 & 4.0 is not supported. Server is %-.32s", greet_packet.server_version); php_error_docref(NULL, E_WARNING, "Connecting to 3.22, 3.23 & 4.0 " " is not supported. Server is %-.32s", greet_packet.server_version); SET_CLIENT_ERROR(conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, "Connecting to 3.22, 3.23 & 4.0 servers is not supported"); goto err; } conn->thread_id = greet_packet.thread_id; conn->protocol_version = greet_packet.protocol_version; conn->server_version = mnd_pestrdup(greet_packet.server_version, conn->persistent); conn->greet_charset = mysqlnd_find_charset_nr(greet_packet.charset_no); if (!conn->greet_charset) { php_error_docref(NULL, E_WARNING, "Server sent charset (%d) unknown to the client. Please, report to the developers", greet_packet.charset_no); SET_CLIENT_ERROR(conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, "Server sent charset unknown to the client. Please, report to the developers"); goto err; } conn->server_capabilities = greet_packet.server_capabilities; if (FAIL == mysqlnd_connect_run_authentication(conn, user, passwd, db, db_len, (size_t) passwd_len, greet_packet.authentication_plugin_data, greet_packet.auth_protocol, greet_packet.charset_no, greet_packet.server_capabilities, conn->options, mysql_flags)) { goto err; } UPSERT_STATUS_RESET(conn->upsert_status); UPSERT_STATUS_SET_SERVER_STATUS(conn->upsert_status, greet_packet.server_status); PACKET_FREE(&greet_packet); DBG_RETURN(PASS); err: conn->server_capabilities = 0; PACKET_FREE(&greet_packet); DBG_RETURN(FAIL); }
/* {{{ mysqlnd_com_handshake_run */ static enum_func_status mysqlnd_com_handshake_run(void *cmd) { struct st_mysqlnd_protocol_com_handshake_command * command = (struct st_mysqlnd_protocol_com_handshake_command *) cmd; const char * user = command->context.user.s; const char * passwd = command->context.passwd.s; size_t passwd_len = command->context.passwd.l; const char * db = command->context.database.s; size_t db_len = command->context.database.l; size_t mysql_flags = command->context.client_flags; MYSQLND_CONN_DATA * conn = command->context.conn; MYSQLND_PACKET_GREET * greet_packet; DBG_ENTER("mysqlnd_conn_data::connect_handshake"); DBG_INF_FMT("stream=%p", conn->vio->data->m.get_stream(conn->vio)); DBG_INF_FMT("[user=%s] [db=%s:%d] [flags=%llu]", user, db, db_len, mysql_flags); greet_packet = conn->payload_decoder_factory->m.get_greet_packet(conn->payload_decoder_factory, FALSE); if (!greet_packet) { SET_OOM_ERROR(conn->error_info); DBG_RETURN(FAIL); /* OOM */ } if (FAIL == PACKET_READ(greet_packet)) { DBG_ERR("Error while reading greeting packet"); php_error_docref(NULL, E_WARNING, "Error while reading greeting packet. PID=%d", getpid()); goto err; } else if (greet_packet->error_no) { DBG_ERR_FMT("errorno=%u error=%s", greet_packet->error_no, greet_packet->error); SET_CLIENT_ERROR(conn->error_info, greet_packet->error_no, greet_packet->sqlstate, greet_packet->error); goto err; } else if (greet_packet->pre41) { DBG_ERR_FMT("Connecting to 3.22, 3.23 & 4.0 is not supported. Server is %-.32s", greet_packet->server_version); php_error_docref(NULL, E_WARNING, "Connecting to 3.22, 3.23 & 4.0 " " is not supported. Server is %-.32s", greet_packet->server_version); SET_CLIENT_ERROR(conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, "Connecting to 3.22, 3.23 & 4.0 servers is not supported"); goto err; } conn->thread_id = greet_packet->thread_id; conn->protocol_version = greet_packet->protocol_version; conn->server_version = mnd_pestrdup(greet_packet->server_version, conn->persistent); conn->greet_charset = mysqlnd_find_charset_nr(greet_packet->charset_no); if (!conn->greet_charset) { php_error_docref(NULL, E_WARNING, "Server sent charset (%d) unknown to the client. Please, report to the developers", greet_packet->charset_no); SET_CLIENT_ERROR(conn->error_info, CR_NOT_IMPLEMENTED, UNKNOWN_SQLSTATE, "Server sent charset unknown to the client. Please, report to the developers"); goto err; } conn->server_capabilities = greet_packet->server_capabilities; if (FAIL == mysqlnd_connect_run_authentication(conn, user, passwd, db, db_len, (size_t) passwd_len, greet_packet->authentication_plugin_data, greet_packet->auth_protocol, greet_packet->charset_no, greet_packet->server_capabilities, conn->options, mysql_flags)) { goto err; } UPSERT_STATUS_RESET(conn->upsert_status); UPSERT_STATUS_SET_SERVER_STATUS(conn->upsert_status, greet_packet->server_status); PACKET_FREE(greet_packet); DBG_RETURN(PASS); err: conn->server_capabilities = 0; PACKET_FREE(greet_packet); DBG_RETURN(FAIL); }
/* {{{ mysqlnd_auth_handshake */ enum_func_status mysqlnd_auth_handshake(MYSQLND_CONN_DATA * conn, const char * const user, const char * const passwd, const size_t passwd_len, const char * const db, const size_t db_len, const MYSQLND_OPTIONS * const options, zend_ulong mysql_flags, unsigned int server_charset_no, zend_bool use_full_blown_auth_packet, const char * const auth_protocol, const zend_uchar * const auth_plugin_data, const size_t auth_plugin_data_len, char ** switch_to_auth_protocol, size_t * switch_to_auth_protocol_len, zend_uchar ** switch_to_auth_protocol_data, size_t * switch_to_auth_protocol_data_len ) { enum_func_status ret = FAIL; const MYSQLND_CHARSET * charset = NULL; MYSQLND_PACKET_CHANGE_AUTH_RESPONSE * change_auth_resp_packet = NULL; MYSQLND_PACKET_AUTH_RESPONSE * auth_resp_packet = NULL; MYSQLND_PACKET_AUTH * auth_packet = NULL; DBG_ENTER("mysqlnd_auth_handshake"); auth_resp_packet = conn->protocol->m.get_auth_response_packet(conn->protocol, FALSE); if (!auth_resp_packet) { SET_OOM_ERROR(*conn->error_info); goto end; } if (use_full_blown_auth_packet != TRUE) { change_auth_resp_packet = conn->protocol->m.get_change_auth_response_packet(conn->protocol, FALSE); if (!change_auth_resp_packet) { SET_OOM_ERROR(*conn->error_info); goto end; } change_auth_resp_packet->auth_data = auth_plugin_data; change_auth_resp_packet->auth_data_len = auth_plugin_data_len; if (!PACKET_WRITE(change_auth_resp_packet, conn)) { CONN_SET_STATE(conn, CONN_QUIT_SENT); SET_CLIENT_ERROR(*conn->error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone); goto end; } } else { auth_packet = conn->protocol->m.get_auth_packet(conn->protocol, FALSE); auth_packet->client_flags = mysql_flags; auth_packet->max_packet_size = options->max_allowed_packet; if (options->charset_name && (charset = mysqlnd_find_charset_name(options->charset_name))) { auth_packet->charset_no = charset->nr; } else { auth_packet->charset_no = server_charset_no; } auth_packet->send_auth_data = TRUE; auth_packet->user = user; auth_packet->db = db; auth_packet->db_len = db_len; auth_packet->auth_data = auth_plugin_data; auth_packet->auth_data_len = auth_plugin_data_len; auth_packet->auth_plugin_name = auth_protocol; if (conn->server_capabilities & CLIENT_CONNECT_ATTRS) { auth_packet->connect_attr = conn->options->connect_attr; } if (!PACKET_WRITE(auth_packet, conn)) { goto end; } } if (use_full_blown_auth_packet == TRUE) { conn->charset = mysqlnd_find_charset_nr(auth_packet->charset_no); } if (FAIL == PACKET_READ(auth_resp_packet, conn) || auth_resp_packet->response_code >= 0xFE) { if (auth_resp_packet->response_code == 0xFE) { /* old authentication with new server !*/ if (!auth_resp_packet->new_auth_protocol) { DBG_ERR(mysqlnd_old_passwd); SET_CLIENT_ERROR(*conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, mysqlnd_old_passwd); } else { *switch_to_auth_protocol = mnd_pestrndup(auth_resp_packet->new_auth_protocol, auth_resp_packet->new_auth_protocol_len, FALSE); *switch_to_auth_protocol_len = auth_resp_packet->new_auth_protocol_len; if (auth_resp_packet->new_auth_protocol_data) { *switch_to_auth_protocol_data_len = auth_resp_packet->new_auth_protocol_data_len; *switch_to_auth_protocol_data = mnd_emalloc(*switch_to_auth_protocol_data_len); memcpy(*switch_to_auth_protocol_data, auth_resp_packet->new_auth_protocol_data, *switch_to_auth_protocol_data_len); } else { *switch_to_auth_protocol_data = NULL; *switch_to_auth_protocol_data_len = 0; } } } else if (auth_resp_packet->response_code == 0xFF) { if (auth_resp_packet->sqlstate[0]) { strlcpy(conn->error_info->sqlstate, auth_resp_packet->sqlstate, sizeof(conn->error_info->sqlstate)); DBG_ERR_FMT("ERROR:%u [SQLSTATE:%s] %s", auth_resp_packet->error_no, auth_resp_packet->sqlstate, auth_resp_packet->error); } SET_CLIENT_ERROR(*conn->error_info, auth_resp_packet->error_no, UNKNOWN_SQLSTATE, auth_resp_packet->error); } goto end; } SET_NEW_MESSAGE(conn->last_message, conn->last_message_len, auth_resp_packet->message, auth_resp_packet->message_len, conn->persistent); ret = PASS; end: PACKET_FREE(change_auth_resp_packet); PACKET_FREE(auth_packet); PACKET_FREE(auth_resp_packet); DBG_RETURN(ret); }
/* {{{ mysqlnd_auth_handshake */ enum_func_status mysqlnd_auth_handshake(MYSQLND_CONN_DATA * conn, const char * const user, const char * const passwd, const size_t passwd_len, const char * const db, const size_t db_len, const MYSQLND_SESSION_OPTIONS * const session_options, const zend_ulong mysql_flags, const unsigned int server_charset_no, const zend_bool use_full_blown_auth_packet, const char * const auth_protocol, struct st_mysqlnd_authentication_plugin * auth_plugin, const zend_uchar * const orig_auth_plugin_data, const size_t orig_auth_plugin_data_len, const zend_uchar * const auth_plugin_data, const size_t auth_plugin_data_len, char ** switch_to_auth_protocol, size_t * const switch_to_auth_protocol_len, zend_uchar ** switch_to_auth_protocol_data, size_t * const switch_to_auth_protocol_data_len ) { enum_func_status ret = FAIL; const MYSQLND_CHARSET * charset = NULL; MYSQLND_PACKET_AUTH_RESPONSE auth_resp_packet; DBG_ENTER("mysqlnd_auth_handshake"); conn->payload_decoder_factory->m.init_auth_response_packet(&auth_resp_packet); if (use_full_blown_auth_packet != TRUE) { MYSQLND_PACKET_CHANGE_AUTH_RESPONSE change_auth_resp_packet; conn->payload_decoder_factory->m.init_change_auth_response_packet(&change_auth_resp_packet); change_auth_resp_packet.auth_data = auth_plugin_data; change_auth_resp_packet.auth_data_len = auth_plugin_data_len; if (!PACKET_WRITE(conn, &change_auth_resp_packet)) { SET_CONNECTION_STATE(&conn->state, CONN_QUIT_SENT); SET_CLIENT_ERROR(conn->error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone); PACKET_FREE(&change_auth_resp_packet); goto end; } PACKET_FREE(&change_auth_resp_packet); } else { MYSQLND_PACKET_AUTH auth_packet; conn->payload_decoder_factory->m.init_auth_packet(&auth_packet); auth_packet.client_flags = mysql_flags; auth_packet.max_packet_size = session_options->max_allowed_packet; if (session_options->charset_name && (charset = mysqlnd_find_charset_name(session_options->charset_name))) { auth_packet.charset_no = charset->nr; } else { auth_packet.charset_no = server_charset_no; } auth_packet.send_auth_data = TRUE; auth_packet.user = user; auth_packet.db = db; auth_packet.db_len = db_len; auth_packet.auth_data = auth_plugin_data; auth_packet.auth_data_len = auth_plugin_data_len; auth_packet.auth_plugin_name = auth_protocol; if (conn->server_capabilities & CLIENT_CONNECT_ATTRS) { auth_packet.connect_attr = conn->options->connect_attr; } if (!PACKET_WRITE(conn, &auth_packet)) { PACKET_FREE(&auth_packet); goto end; } if (use_full_blown_auth_packet == TRUE) { conn->charset = mysqlnd_find_charset_nr(auth_packet.charset_no); } PACKET_FREE(&auth_packet); } if (auth_plugin && auth_plugin->methods.handle_server_response) { auth_plugin->methods.handle_server_response(auth_plugin, conn, orig_auth_plugin_data, orig_auth_plugin_data_len, passwd, passwd_len); } if (FAIL == PACKET_READ(conn, &auth_resp_packet) || auth_resp_packet.response_code >= 0xFE) { if (auth_resp_packet.response_code == 0xFE) { /* old authentication with new server !*/ if (!auth_resp_packet.new_auth_protocol) { DBG_ERR(mysqlnd_old_passwd); SET_CLIENT_ERROR(conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, mysqlnd_old_passwd); } else { *switch_to_auth_protocol = mnd_pestrndup(auth_resp_packet.new_auth_protocol, auth_resp_packet.new_auth_protocol_len, FALSE); *switch_to_auth_protocol_len = auth_resp_packet.new_auth_protocol_len; if (auth_resp_packet.new_auth_protocol_data) { *switch_to_auth_protocol_data_len = auth_resp_packet.new_auth_protocol_data_len; *switch_to_auth_protocol_data = mnd_emalloc(*switch_to_auth_protocol_data_len); memcpy(*switch_to_auth_protocol_data, auth_resp_packet.new_auth_protocol_data, *switch_to_auth_protocol_data_len); } else { *switch_to_auth_protocol_data = NULL; *switch_to_auth_protocol_data_len = 0; } } } else if (auth_resp_packet.response_code == 0xFF) { if (auth_resp_packet.sqlstate[0]) { strlcpy(conn->error_info->sqlstate, auth_resp_packet.sqlstate, sizeof(conn->error_info->sqlstate)); DBG_ERR_FMT("ERROR:%u [SQLSTATE:%s] %s", auth_resp_packet.error_no, auth_resp_packet.sqlstate, auth_resp_packet.error); } SET_CLIENT_ERROR(conn->error_info, auth_resp_packet.error_no, UNKNOWN_SQLSTATE, auth_resp_packet.error); } goto end; } SET_NEW_MESSAGE(conn->last_message.s, conn->last_message.l, auth_resp_packet.message, auth_resp_packet.message_len); ret = PASS; end: PACKET_FREE(&auth_resp_packet); DBG_RETURN(ret); }