My Project
|
00001 /* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved. 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; version 2 of the License. 00006 00007 This program is distributed in the hope that it will be useful, 00008 but WITHOUT ANY WARRANTY; without even the implied warranty of 00009 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00010 GNU General Public License for more details. 00011 00012 You should have received a copy of the GNU General Public License 00013 along with this program; if not, write to the Free Software 00014 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ 00015 00016 #ifndef SQL_AUDIT_INCLUDED 00017 #define SQL_AUDIT_INCLUDED 00018 00019 #include <my_global.h> 00020 00021 #include <mysql/plugin_audit.h> 00022 #include "sql_class.h" 00023 #include "sql_rewrite.h" 00024 00025 extern unsigned long mysql_global_audit_mask[]; 00026 00027 00028 extern void mysql_audit_initialize(); 00029 extern void mysql_audit_finalize(); 00030 00031 00032 extern void mysql_audit_init_thd(THD *thd); 00033 extern void mysql_audit_free_thd(THD *thd); 00034 extern void mysql_audit_acquire_plugins(THD *thd, uint event_class); 00035 00036 00037 #ifndef EMBEDDED_LIBRARY 00038 extern void mysql_audit_notify(THD *thd, uint event_class, 00039 uint event_subtype, ...); 00040 bool is_any_audit_plugin_active(THD *thd __attribute__((unused))); 00041 #else 00042 #define mysql_audit_notify(...) 00043 #endif 00044 extern void mysql_audit_release(THD *thd); 00045 00046 #define MAX_USER_HOST_SIZE 512 00047 static inline uint make_user_name(THD *thd, char *buf) 00048 { 00049 Security_context *sctx= thd->security_ctx; 00050 return strxnmov(buf, MAX_USER_HOST_SIZE, 00051 sctx->priv_user[0] ? sctx->priv_user : "", "[", 00052 sctx->user ? sctx->user : "", "] @ ", 00053 sctx->get_host()->length() ? sctx->get_host()->ptr() : 00054 "", " [", sctx->get_ip()->length() ? sctx->get_ip()->ptr() : 00055 "", "]", NullS) - buf; 00056 } 00057 00068 static inline 00069 void mysql_audit_general_log(THD *thd, const char *cmd, uint cmdlen, 00070 const char *query_str, size_t query_len) 00071 { 00072 #ifndef EMBEDDED_LIBRARY 00073 if (mysql_global_audit_mask[0] & MYSQL_AUDIT_GENERAL_CLASSMASK) 00074 { 00075 MYSQL_LEX_STRING sql_command, ip, host, external_user; 00076 MYSQL_LEX_STRING query={ (char *)query_str, query_len }; 00077 static MYSQL_LEX_STRING empty= { C_STRING_WITH_LEN("") }; 00078 ha_rows rows= 0; 00079 int error_code= 0; 00080 char user_buff[MAX_USER_HOST_SIZE + 1]; 00081 const char *user= user_buff; 00082 uint userlen= make_user_name(thd, user_buff); 00083 time_t time= (time_t) thd->start_time.tv_sec; 00084 00085 if (thd) 00086 { 00087 if (!query_len) 00088 { 00089 /* no query specified, fetch from THD */ 00090 if (!thd->rewritten_query.length()) 00091 mysql_rewrite_query(thd); 00092 if (thd->rewritten_query.length()) 00093 { 00094 query.str= (char *) thd->rewritten_query.ptr(); 00095 query.length= thd->rewritten_query.length(); 00096 } 00097 else 00098 { 00099 query.str= thd->query(); 00100 query.length= thd->query_length(); 00101 } 00102 } 00103 ip.str= (char *) thd->security_ctx->get_ip()->ptr(); 00104 ip.length= thd->security_ctx->get_ip()->length(); 00105 host.str= (char *) thd->security_ctx->get_host()->ptr(); 00106 host.length= thd->security_ctx->get_host()->length(); 00107 external_user.str= (char *) thd->security_ctx->get_external_user()->ptr(); 00108 external_user.length= thd->security_ctx->get_external_user()->length(); 00109 sql_command.str= (char *) sql_statement_names[thd->lex->sql_command].str; 00110 sql_command.length= sql_statement_names[thd->lex->sql_command].length; 00111 } 00112 else 00113 { 00114 ip= empty; 00115 host= empty; 00116 external_user= empty; 00117 sql_command= empty; 00118 } 00119 const CHARSET_INFO *clientcs= thd ? thd->variables.character_set_client 00120 : global_system_variables.character_set_client; 00121 00122 mysql_audit_notify(thd, MYSQL_AUDIT_GENERAL_CLASS, MYSQL_AUDIT_GENERAL_LOG, 00123 error_code, time, user, userlen, cmd, cmdlen, query.str, 00124 query.length, clientcs, rows, sql_command, host, 00125 external_user, ip); 00126 } 00127 #endif 00128 } 00129 00130 00143 static inline 00144 void mysql_audit_general(THD *thd, uint event_subtype, 00145 int error_code, const char *msg) 00146 { 00147 #ifndef EMBEDDED_LIBRARY 00148 if (mysql_global_audit_mask[0] & MYSQL_AUDIT_GENERAL_CLASSMASK) 00149 { 00150 time_t time= my_time(0); 00151 uint msglen= msg ? strlen(msg) : 0; 00152 uint userlen; 00153 const char *user; 00154 char user_buff[MAX_USER_HOST_SIZE]; 00155 CSET_STRING query; 00156 MYSQL_LEX_STRING ip, host, external_user, sql_command; 00157 ha_rows rows; 00158 static MYSQL_LEX_STRING empty= { C_STRING_WITH_LEN("") }; 00159 00160 if (thd) 00161 { 00162 if (!thd->rewritten_query.length()) 00163 mysql_rewrite_query(thd); 00164 if (thd->rewritten_query.length()) 00165 query= CSET_STRING((char *) thd->rewritten_query.ptr(), 00166 thd->rewritten_query.length(), 00167 thd->rewritten_query.charset()); 00168 else 00169 query= thd->query_string; 00170 user= user_buff; 00171 userlen= make_user_name(thd, user_buff); 00172 rows= thd->get_stmt_da()->current_row_for_warning(); 00173 ip.str= (char *) thd->security_ctx->get_ip()->ptr(); 00174 ip.length= thd->security_ctx->get_ip()->length(); 00175 host.str= (char *) thd->security_ctx->get_host()->ptr(); 00176 host.length= thd->security_ctx->get_host()->length(); 00177 external_user.str= (char *) thd->security_ctx->get_external_user()->ptr(); 00178 external_user.length= thd->security_ctx->get_external_user()->length(); 00179 sql_command.str= (char *) sql_statement_names[thd->lex->sql_command].str; 00180 sql_command.length= sql_statement_names[thd->lex->sql_command].length; 00181 } 00182 else 00183 { 00184 user= 0; 00185 userlen= 0; 00186 ip= empty; 00187 host= empty; 00188 external_user= empty; 00189 sql_command= empty; 00190 rows= 0; 00191 } 00192 00193 mysql_audit_notify(thd, MYSQL_AUDIT_GENERAL_CLASS, event_subtype, 00194 error_code, time, user, userlen, msg, msglen, 00195 query.str(), query.length(), query.charset(), rows, 00196 sql_command, host, external_user, ip); 00197 } 00198 #endif 00199 } 00200 00201 #define MYSQL_AUDIT_NOTIFY_CONNECTION_CONNECT(thd) mysql_audit_notify(\ 00202 (thd), MYSQL_AUDIT_CONNECTION_CLASS, MYSQL_AUDIT_CONNECTION_CONNECT,\ 00203 (thd)->get_stmt_da()->is_error() ? (thd)->get_stmt_da()->sql_errno() : 0,\ 00204 (thd)->thread_id, (thd)->security_ctx->user,\ 00205 (thd)->security_ctx->user ? strlen((thd)->security_ctx->user) : 0,\ 00206 (thd)->security_ctx->priv_user, strlen((thd)->security_ctx->priv_user),\ 00207 (thd)->security_ctx->get_external_user()->ptr(),\ 00208 (thd)->security_ctx->get_external_user()->length(),\ 00209 (thd)->security_ctx->proxy_user, strlen((thd)->security_ctx->proxy_user),\ 00210 (thd)->security_ctx->get_host()->ptr(),\ 00211 (thd)->security_ctx->get_host()->length(),\ 00212 (thd)->security_ctx->get_ip()->ptr(),\ 00213 (thd)->security_ctx->get_ip()->length(),\ 00214 (thd)->db, (thd)->db ? strlen((thd)->db) : 0) 00215 00216 #define MYSQL_AUDIT_NOTIFY_CONNECTION_DISCONNECT(thd, errcode)\ 00217 mysql_audit_notify(\ 00218 (thd), MYSQL_AUDIT_CONNECTION_CLASS, MYSQL_AUDIT_CONNECTION_DISCONNECT,\ 00219 (errcode), (thd)->thread_id,\ 00220 (thd)->security_ctx->user,\ 00221 (thd)->security_ctx->user ? strlen((thd)->security_ctx->user) : 0,\ 00222 (thd)->security_ctx->priv_user, strlen((thd)->security_ctx->priv_user),\ 00223 (thd)->security_ctx->get_external_user()->ptr(),\ 00224 (thd)->security_ctx->get_external_user()->length(),\ 00225 (thd)->security_ctx->proxy_user, strlen((thd)->security_ctx->proxy_user),\ 00226 (thd)->security_ctx->get_host()->ptr(),\ 00227 (thd)->security_ctx->get_host()->length(),\ 00228 (thd)->security_ctx->get_ip()->ptr(),\ 00229 (thd)->security_ctx->get_ip()->length(),\ 00230 (thd)->db, (thd)->db ? strlen((thd)->db) : 0) 00231 00232 #define MYSQL_AUDIT_NOTIFY_CONNECTION_CHANGE_USER(thd) mysql_audit_notify(\ 00233 (thd), MYSQL_AUDIT_CONNECTION_CLASS, MYSQL_AUDIT_CONNECTION_CHANGE_USER,\ 00234 (thd)->get_stmt_da()->is_error() ? (thd)->get_stmt_da()->sql_errno() : 0,\ 00235 (thd)->thread_id, (thd)->security_ctx->user,\ 00236 (thd)->security_ctx->user ? strlen((thd)->security_ctx->user) : 0,\ 00237 (thd)->security_ctx->priv_user, strlen((thd)->security_ctx->priv_user),\ 00238 (thd)->security_ctx->get_external_user()->ptr(),\ 00239 (thd)->security_ctx->get_external_user()->length(),\ 00240 (thd)->security_ctx->proxy_user, strlen((thd)->security_ctx->proxy_user),\ 00241 (thd)->security_ctx->get_host()->ptr(),\ 00242 (thd)->security_ctx->get_host()->length(),\ 00243 (thd)->security_ctx->get_ip()->ptr(),\ 00244 (thd)->security_ctx->get_ip()->length(),\ 00245 (thd)->db, (thd)->db ? strlen((thd)->db) : 0) 00246 00247 #endif /* SQL_AUDIT_INCLUDED */