Skip to content

Instantly share code, notes, and snippets.

@greenlion
Last active December 30, 2018 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greenlion/8a735be2ae59dbfcac890c674b7d90c5 to your computer and use it in GitHub Desktop.
Save greenlion/8a735be2ae59dbfcac890c674b7d90c5 to your computer and use it in GitHub Desktop.
#include "../../components/mysql_server/my_plugin_shim.h"
PSI_memory_key key_memory_shim_iterator;
/**
Its a dummy initialization function. And it will be called from
mysql_component_infrastructure_init(). Else linker, is cutting out (as
library optimization) the string service code because libsql code is not
calling any functions of it.
*/
void my_plugin_shim_init() { return; }
/*
Query rewriter that allows queries to run on the THD inside of the plugin to get internal resultsets using SQLClient
@param [in] thd - the THD object on which to run queries
@param [in] com_data - the original COM_DATA* from the do_command function
@param [in] command - the original command from the do_command function
@param [out] new_com_data - pointer to a new COM_DATA to run when the function returns
@param [out] new_command - pointer to a new enum_server_command to run when the function returns
@return return value indicates wether or not the command was rewritten
@retval false no rewrite
@retval true query was rewritten
*/
DEFINE_BOOL_METHOD(my_plugin_shim_imp::query_injection_point,(THD* thd, COM_DATA *com_data, enum enum_server_command command,
COM_DATA* new_com_data, enum enum_server_command* new_command)) {
std::string new_query;
SQLClient conn(thd);
SQLCursor* stmt;
SQLRow* row;
try {
/* example rewrite rule for SHOW PASSWORD*/
if(command == COM_QUERY)
{
std::locale loc;
std::string old_query(com_data->com_query.query,com_data->com_query.length);
for(unsigned int i=0;i<com_data->com_query.length;++i) {
old_query[i] = std::toupper(old_query[i], loc);
}
if(old_query == "SHOW PASSWORD")
{
if(conn.query("pw,user","select authentication_string as pw,user from mysql.user where concat(user,'@',host) = USER() or user = USER() LIMIT 1", &stmt))
{
if(stmt != NULL)
{
if((row = stmt->next()))
{
new_query = "SELECT '" + row->at(0) + "'";
}
} else
{
delete stmt;
return false;
}
} else {
return false;
}
/* replace the command sent to the server */
if(new_query != "")
{
thd->get_protocol_classic()->create_command(new_com_data, COM_QUERY, (uchar *) strdup(new_query.c_str()), new_query.length());
*new_command = COM_QUERY;
} else {
if(stmt) delete stmt;
return false;
}
if(stmt) delete stmt;
return true;
}
}
/* don't replace command */
return false;
} catch (...) {
if(stmt != NULL) delete stmt;
mysql_components_handle_std_exception(__func__);
return false;
}
return true;
};
#ifndef MYSQL_SHIM_SERVICE_H
#define MYSQL_SHIM_SERVICE_H
#include "mysql/components/service_implementation.h"
#include "../../components/mysql_server/server_component.h"
#include "mysql/psi/psi_memory.h"
#include <locale>
#include "sql/sql_parse.h"
#include "sql/sql_class.h"
#include "sql/sql_client.h"
void my_plugin_shim_init();
class my_plugin_shim_imp {
public: /* service implementations */
/**
Query rewriter that allows queries to run on the THD inside of the plugin to get internal resultsets using SQLClient
@param [in] thd - the THD object on which to run queries
@param [in] com_data - the original COM_DATA* from the do_command function
@param [in] command - the original command from the do_command function
@param [out] new_com_data - pointer to a new COM_DATA to run when the function returns
@param [out] new_command - pointer to a new enum_server_command to run when the function returns
@return return value indicates wether or not the command was rewritten
@retval false no rewrite
@retval true query was rewritten
*/
static DEFINE_BOOL_METHOD(query_injection_point,(THD* thd, COM_DATA *com_data, enum enum_server_command command,
COM_DATA* new_com_data, enum enum_server_command* new_command));
};
#endif /* MYSQL_SHIM_SERVICE_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment