MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sql_audit.h
1 #ifndef SQL_AUDIT_INCLUDED
2 #define SQL_AUDIT_INCLUDED
3 
4 /* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; version 2 of the License.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
18 
19 
20 #include <my_global.h>
21 
22 #include <mysql/plugin_audit.h>
23 #include "sql_class.h"
24 #include "sql_rewrite.h"
25 
26 extern unsigned long mysql_global_audit_mask[];
27 
28 
29 extern void mysql_audit_initialize();
30 extern void mysql_audit_finalize();
31 
32 
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);
36 
37 
38 #ifndef EMBEDDED_LIBRARY
39 extern void mysql_audit_notify(THD *thd, uint event_class,
40  uint event_subtype, ...);
41 #else
42 #define mysql_audit_notify(...)
43 #endif
44 extern void mysql_audit_release(THD *thd);
45 
46 #define MAX_USER_HOST_SIZE 512
47 static inline uint make_user_name(THD *thd, char *buf)
48 {
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;
56 }
57 
71 static inline
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)
76 {
77 #ifndef EMBEDDED_LIBRARY
78  if (mysql_global_audit_mask[0] & MYSQL_AUDIT_GENERAL_CLASSMASK)
79  {
80  MYSQL_LEX_STRING sql_command, ip, host, external_user;
81  static MYSQL_LEX_STRING empty= { C_STRING_WITH_LEN("") };
82 
83  if (thd)
84  {
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;
93  }
94  else
95  {
96  ip= empty;
97  host= empty;
98  external_user= empty;
99  sql_command= empty;
100  }
101  const CHARSET_INFO *clientcs= thd ? thd->variables.character_set_client
102  : global_system_variables.character_set_client;
103 
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);
107  }
108 #endif
109 }
110 
111 
124 static inline
125 void mysql_audit_general(THD *thd, uint event_subtype,
126  int error_code, const char *msg)
127 {
128 #ifndef EMBEDDED_LIBRARY
129  if (mysql_global_audit_mask[0] & MYSQL_AUDIT_GENERAL_CLASSMASK)
130  {
131  time_t time= my_time(0);
132  uint msglen= msg ? strlen(msg) : 0;
133  uint userlen;
134  const char *user;
135  char user_buff[MAX_USER_HOST_SIZE];
136  CSET_STRING query;
137  MYSQL_LEX_STRING ip, host, external_user, sql_command;
138  ha_rows rows;
139  static MYSQL_LEX_STRING empty= { C_STRING_WITH_LEN("") };
140 
141  if (thd)
142  {
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());
149  else
150  query= thd->query_string;
151  user= user_buff;
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;
162  }
163  else
164  {
165  user= 0;
166  userlen= 0;
167  ip= empty;
168  host= empty;
169  external_user= empty;
170  sql_command= empty;
171  rows= 0;
172  }
173 
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);
178  }
179 #endif
180 }
181 
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)
196 
197 #define MYSQL_AUDIT_NOTIFY_CONNECTION_DISCONNECT(thd, errcode)\
198  mysql_audit_notify(\
199  (thd), MYSQL_AUDIT_CONNECTION_CLASS, MYSQL_AUDIT_CONNECTION_DISCONNECT,\
200  (errcode), (thd)->thread_id, "", 0, "", 0, "", 0, "", 0, "", 0, "", 0, "", 0)
201 
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)
216 
217 #endif /* SQL_AUDIT_INCLUDED */