1 #ifndef SQL_AUDIT_INCLUDED
2 #define SQL_AUDIT_INCLUDED
20 #include <my_global.h>
22 #include <mysql/plugin_audit.h>
23 #include "sql_class.h"
24 #include "sql_rewrite.h"
26 extern unsigned long mysql_global_audit_mask[];
29 extern void mysql_audit_initialize();
30 extern void mysql_audit_finalize();
33 extern void mysql_audit_init_thd(THD *thd);
34 extern void mysql_audit_free_thd(THD *thd);
35 extern void mysql_audit_acquire_plugins(THD *thd, uint event_class);
38 #ifndef EMBEDDED_LIBRARY
39 extern void mysql_audit_notify(THD *thd, uint event_class,
40 uint event_subtype, ...);
42 #define mysql_audit_notify(...)
44 extern void mysql_audit_release(THD *thd);
46 #define MAX_USER_HOST_SIZE 512
47 static inline uint make_user_name(THD *thd,
char *
buf)
49 Security_context *sctx= thd->security_ctx;
50 return strxnmov(buf, MAX_USER_HOST_SIZE,
51 sctx->priv_user[0] ? sctx->priv_user :
"",
"[",
52 sctx->user ? sctx->user :
"",
"] @ ",
53 sctx->get_host()->length() ? sctx->get_host()->ptr() :
54 "",
" [", sctx->get_ip()->length() ? sctx->get_ip()->ptr() :
55 "",
"]", NullS) - buf;
72 void mysql_audit_general_log(THD *thd, time_t time,
73 const char *user, uint userlen,
74 const char *cmd, uint cmdlen,
75 const char *
query, uint querylen)
77 #ifndef EMBEDDED_LIBRARY
78 if (mysql_global_audit_mask[0] & MYSQL_AUDIT_GENERAL_CLASSMASK)
85 ip.str= (
char *) thd->security_ctx->get_ip()->ptr();
86 ip.length= thd->security_ctx->get_ip()->length();
87 host.str= (
char *) thd->security_ctx->get_host()->ptr();
88 host.length= thd->security_ctx->get_host()->length();
89 external_user.str= (
char *) thd->security_ctx->get_external_user()->ptr();
90 external_user.length= thd->security_ctx->get_external_user()->length();
91 sql_command.str= (
char *) sql_statement_names[thd->lex->sql_command].str;
92 sql_command.length= sql_statement_names[thd->lex->sql_command].length;
101 const CHARSET_INFO *clientcs= thd ? thd->variables.character_set_client
102 : global_system_variables.character_set_client;
104 mysql_audit_notify(thd, MYSQL_AUDIT_GENERAL_CLASS, MYSQL_AUDIT_GENERAL_LOG,
105 0, time, user, userlen, cmd, cmdlen, query, querylen,
106 clientcs, 0, sql_command, host, external_user, ip);
125 void mysql_audit_general(THD *thd, uint event_subtype,
126 int error_code,
const char *
msg)
128 #ifndef EMBEDDED_LIBRARY
129 if (mysql_global_audit_mask[0] & MYSQL_AUDIT_GENERAL_CLASSMASK)
131 time_t time= my_time(0);
132 uint msglen= msg ? strlen(msg) : 0;
135 char user_buff[MAX_USER_HOST_SIZE];
143 if (!thd->rewritten_query.length())
144 mysql_rewrite_query(thd);
145 if (thd->rewritten_query.length())
146 query=
CSET_STRING((
char *) thd->rewritten_query.ptr(),
147 thd->rewritten_query.length(),
148 thd->rewritten_query.charset());
150 query= thd->query_string;
152 userlen= make_user_name(thd, user_buff);
153 rows= thd->get_stmt_da()->current_row_for_warning();
154 ip.str= (
char *) thd->security_ctx->get_ip()->ptr();
155 ip.length= thd->security_ctx->get_ip()->length();
156 host.str= (
char *) thd->security_ctx->get_host()->ptr();
157 host.length= thd->security_ctx->get_host()->length();
158 external_user.str= (
char *) thd->security_ctx->get_external_user()->ptr();
159 external_user.length= thd->security_ctx->get_external_user()->length();
160 sql_command.str= (
char *) sql_statement_names[thd->lex->sql_command].str;
161 sql_command.length= sql_statement_names[thd->lex->sql_command].length;
169 external_user= empty;
174 mysql_audit_notify(thd, MYSQL_AUDIT_GENERAL_CLASS, event_subtype,
175 error_code, time, user, userlen, msg, msglen,
176 query.str(), query.length(), query.charset(), rows,
177 sql_command, host, external_user, ip);
182 #define MYSQL_AUDIT_NOTIFY_CONNECTION_CONNECT(thd) mysql_audit_notify(\
183 (thd), MYSQL_AUDIT_CONNECTION_CLASS, MYSQL_AUDIT_CONNECTION_CONNECT,\
184 (thd)->get_stmt_da()->is_error() ? (thd)->get_stmt_da()->sql_errno() : 0,\
185 (thd)->thread_id, (thd)->security_ctx->user,\
186 (thd)->security_ctx->user ? strlen((thd)->security_ctx->user) : 0,\
187 (thd)->security_ctx->priv_user, strlen((thd)->security_ctx->priv_user),\
188 (thd)->security_ctx->get_external_user()->ptr(),\
189 (thd)->security_ctx->get_external_user()->length(),\
190 (thd)->security_ctx->proxy_user, strlen((thd)->security_ctx->proxy_user),\
191 (thd)->security_ctx->get_host()->ptr(),\
192 (thd)->security_ctx->get_host()->length(),\
193 (thd)->security_ctx->get_ip()->ptr(),\
194 (thd)->security_ctx->get_ip()->length(),\
195 (thd)->db, (thd)->db ? strlen((thd)->db) : 0)
197 #define MYSQL_AUDIT_NOTIFY_CONNECTION_DISCONNECT(thd, errcode)\
199 (thd), MYSQL_AUDIT_CONNECTION_CLASS, MYSQL_AUDIT_CONNECTION_DISCONNECT,\
200 (errcode), (thd)->thread_id, "", 0, "", 0, "", 0, "", 0, "", 0, "", 0, "", 0)
202 #define MYSQL_AUDIT_NOTIFY_CONNECTION_CHANGE_USER(thd) mysql_audit_notify(\
203 (thd), MYSQL_AUDIT_CONNECTION_CLASS, MYSQL_AUDIT_CONNECTION_CHANGE_USER,\
204 (thd)->get_stmt_da()->is_error() ? (thd)->get_stmt_da()->sql_errno() : 0,\
205 (thd)->thread_id, (thd)->security_ctx->user,\
206 (thd)->security_ctx->user ? strlen((thd)->security_ctx->user) : 0,\
207 (thd)->security_ctx->priv_user, strlen((thd)->security_ctx->priv_user),\
208 (thd)->security_ctx->get_external_user()->ptr(),\
209 (thd)->security_ctx->get_external_user()->length(),\
210 (thd)->security_ctx->proxy_user, strlen((thd)->security_ctx->proxy_user),\
211 (thd)->security_ctx->get_host()->ptr(),\
212 (thd)->security_ctx->get_host()->length(),\
213 (thd)->security_ctx->get_ip()->ptr(),\
214 (thd)->security_ctx->get_ip()->length(),\
215 (thd)->db, (thd)->db ? strlen((thd)->db) : 0)