summaryrefslogtreecommitdiff
path: root/sql/sql_audit.h
blob: 8172610607ae8eab416730f910e45c3070364bae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#ifndef SQL_AUDIT_INCLUDED
#define SQL_AUDIT_INCLUDED

/* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */


#include <my_global.h>

#include <mysql/plugin_audit.h>
#include "sql_class.h"

extern unsigned long mysql_global_audit_mask[];


extern void mysql_audit_initialize();
extern void mysql_audit_finalize();


extern void mysql_audit_init_thd(THD *thd);
extern void mysql_audit_free_thd(THD *thd);
extern void mysql_audit_acquire_plugins(THD *thd, ulong *event_class_mask);


#ifndef EMBEDDED_LIBRARY
extern void mysql_audit_notify(THD *thd, uint event_class,
                               uint event_subtype, ...);

static inline bool mysql_audit_general_enabled()
{
  return mysql_global_audit_mask[0] & MYSQL_AUDIT_GENERAL_CLASSMASK;
}

static inline bool mysql_audit_connection_enabled()
{
  return mysql_global_audit_mask[0] & MYSQL_AUDIT_CONNECTION_CLASSMASK;
}

static inline bool mysql_audit_table_enabled()
{
  return mysql_global_audit_mask[0] & MYSQL_AUDIT_TABLE_CLASSMASK;
}

#else
static inline void mysql_audit_notify(THD *thd, uint event_class,
                                      uint event_subtype, ...) { }
#define mysql_audit_general_enabled() 0
#define mysql_audit_connection_enabled() 0
#define mysql_audit_table_enabled() 0
#endif
extern void mysql_audit_release(THD *thd);

#define MAX_USER_HOST_SIZE 512
static inline uint make_user_name(THD *thd, char *buf)
{
  const Security_context *sctx= thd->security_ctx;
  return strxnmov(buf, MAX_USER_HOST_SIZE,
                  sctx->priv_user[0] ? sctx->priv_user : "", "[",
                  sctx->user ? sctx->user : "", "] @ ",
                  sctx->host ? sctx->host : "", " [",
                  sctx->ip ? sctx->ip : "", "]", NullS) - buf;
}

/**
  Call audit plugins of GENERAL audit class, MYSQL_AUDIT_GENERAL_LOG subtype.
  
  @param[in] thd
  @param[in] time             time that event occurred
  @param[in] user             User name
  @param[in] userlen          User name length
  @param[in] cmd              Command name
  @param[in] cmdlen           Command name length
  @param[in] query            Query string
  @param[in] querylen         Query string length
*/
 
static inline
void mysql_audit_general_log(THD *thd, time_t time,
                             const char *user, uint userlen,
                             const char *cmd, uint cmdlen,
                             const char *query, uint querylen)
{
  if (mysql_audit_general_enabled())
  {
    CHARSET_INFO *clientcs= thd ? thd->variables.character_set_client
                                : global_system_variables.character_set_client;

    mysql_audit_notify(thd, MYSQL_AUDIT_GENERAL_CLASS, MYSQL_AUDIT_GENERAL_LOG,
                       0, time, user, userlen, cmd, cmdlen,
                       query, querylen, clientcs, (ha_rows) 0,
                       thd->db, thd->db_length);
  }
}

/**
  Call audit plugins of GENERAL audit class.
  event_subtype should be set to one of:
    MYSQL_AUDIT_GENERAL_ERROR
    MYSQL_AUDIT_GENERAL_RESULT
    MYSQL_AUDIT_GENERAL_STATUS
  
  @param[in] thd
  @param[in] event_subtype    Type of general audit event.
  @param[in] error_code       Error code
  @param[in] msg              Message
*/
static inline
void mysql_audit_general(THD *thd, uint event_subtype,
                         int error_code, const char *msg)
{
  if (mysql_audit_general_enabled())
  {
    time_t time= my_time(0);
    uint msglen= msg ? strlen(msg) : 0;
    const char *user;
    uint userlen;
    char user_buff[MAX_USER_HOST_SIZE];
    CSET_STRING query;
    ha_rows rows;

    if (thd)
    {
      query= thd->query_string;
      user= user_buff;
      userlen= make_user_name(thd, user_buff);
      rows= thd->warning_info->current_row_for_warning();
    }
    else
    {
      user= 0;
      userlen= 0;
      rows= 0;
    }

    mysql_audit_notify(thd, MYSQL_AUDIT_GENERAL_CLASS, event_subtype,
                       error_code, time, user, userlen, msg, msglen,
                       query.str(), query.length(), query.charset(), rows,
                       thd->db, thd->db_length);
  }
}

static inline
void mysql_audit_notify_connection_connect(THD *thd)
{
  if (mysql_audit_connection_enabled())
  {
    const Security_context *sctx= thd->security_ctx;
    mysql_audit_notify(thd, MYSQL_AUDIT_CONNECTION_CLASS,
                       MYSQL_AUDIT_CONNECTION_CONNECT,
                       thd->stmt_da->is_error() ? thd->stmt_da->sql_errno() : 0,
                       thd->thread_id,
                       sctx->user, sctx->user ? strlen(sctx->user) : 0,
                       sctx->priv_user, strlen(sctx->priv_user),
                       sctx->external_user,
                       sctx->external_user ?  strlen(sctx->external_user) : 0,
                       sctx->proxy_user, strlen(sctx->proxy_user),
                       sctx->host, sctx->host ? strlen(sctx->host) : 0,
                       sctx->ip, sctx->ip ? strlen(sctx->ip) : 0,
                       thd->db, thd->db ? strlen(thd->db) : 0);
  }
}

static inline
void mysql_audit_notify_connection_disconnect(THD *thd, int errcode)
{
  if (mysql_audit_connection_enabled())
  {
    const Security_context *sctx= thd->security_ctx;
    mysql_audit_notify(thd, MYSQL_AUDIT_CONNECTION_CLASS,
                       MYSQL_AUDIT_CONNECTION_DISCONNECT,
                       errcode, thd->thread_id,
                       sctx->user, sctx->user ? strlen(sctx->user) : 0,
                       sctx->priv_user, strlen(sctx->priv_user),
                       sctx->external_user,
                       sctx->external_user ?  strlen(sctx->external_user) : 0,
                       sctx->proxy_user, strlen(sctx->proxy_user),
                       sctx->host, sctx->host ? strlen(sctx->host) : 0,
                       sctx->ip, sctx->ip ? strlen(sctx->ip) : 0,
                       thd->db, thd->db ? strlen(thd->db) : 0);
  }
}

static inline
void mysql_audit_notify_connection_change_user(THD *thd)
{
  if (mysql_audit_connection_enabled())
  {
    const Security_context *sctx= thd->security_ctx;
    mysql_audit_notify(thd, MYSQL_AUDIT_CONNECTION_CLASS,
                       MYSQL_AUDIT_CONNECTION_CHANGE_USER,
                       thd->stmt_da->is_error() ? thd->stmt_da->sql_errno() : 0,
                       thd->thread_id,
                       sctx->user, sctx->user ? strlen(sctx->user) : 0,
                       sctx->priv_user, strlen(sctx->priv_user),
                       sctx->external_user,
                       sctx->external_user ?  strlen(sctx->external_user) : 0,
                       sctx->proxy_user, strlen(sctx->proxy_user),
                       sctx->host, sctx->host ? strlen(sctx->host) : 0,
                       sctx->ip, sctx->ip ? strlen(sctx->ip) : 0,
                       thd->db, thd->db ? strlen(thd->db) : 0);
  }
}

static inline
void mysql_audit_external_lock(THD *thd, TABLE_SHARE *share, int lock)
{
  if (lock != F_UNLCK && mysql_audit_table_enabled())
  {
    const Security_context *sctx= thd->security_ctx;
    mysql_audit_notify(thd, MYSQL_AUDIT_TABLE_CLASS, MYSQL_AUDIT_TABLE_LOCK,
                       (int)(lock == F_RDLCK), (ulong)thd->thread_id,
                       sctx->user, sctx->priv_user, sctx->priv_host,
                       sctx->external_user, sctx->proxy_user, sctx->host,
                       sctx->ip, share->db.str, (uint)share->db.length,
                       share->table_name.str, (uint)share->table_name.length,
                       0,0,0,0);
  }
}

static inline
void mysql_audit_create_table(TABLE *table)
{
  if (mysql_audit_table_enabled())
  {
    THD *thd= table->in_use;
    const TABLE_SHARE *share= table->s;
    const Security_context *sctx= thd->security_ctx;
    mysql_audit_notify(thd, MYSQL_AUDIT_TABLE_CLASS, MYSQL_AUDIT_TABLE_CREATE,
                       0, (ulong)thd->thread_id,
                       sctx->user, sctx->priv_user, sctx->priv_host,
                       sctx->external_user, sctx->proxy_user, sctx->host,
                       sctx->ip, share->db.str, (uint)share->db.length,
                       share->table_name.str, (uint)share->table_name.length,
                       0,0,0,0);
  }
}

static inline
void mysql_audit_drop_table(THD *thd, TABLE_LIST *table)
{
  if (mysql_audit_table_enabled())
  {
    const Security_context *sctx= thd->security_ctx;
    mysql_audit_notify(thd, MYSQL_AUDIT_TABLE_CLASS, MYSQL_AUDIT_TABLE_DROP,
                       0, (ulong)thd->thread_id,
                       sctx->user, sctx->priv_user, sctx->priv_host,
                       sctx->external_user, sctx->proxy_user, sctx->host,
                       sctx->ip, table->db, (uint)table->db_length,
                       table->table_name, (uint)table->table_name_length,
                       0,0,0,0);
  }
}

static inline
void mysql_audit_rename_table(THD *thd, const char *old_db, const char *old_tb,
                              const char *new_db, const char *new_tb)
{
  if (mysql_audit_table_enabled())
  {
    const Security_context *sctx= thd->security_ctx;
    mysql_audit_notify(thd, MYSQL_AUDIT_TABLE_CLASS, MYSQL_AUDIT_TABLE_RENAME,
                       0, (ulong)thd->thread_id,
                       sctx->user, sctx->priv_user, sctx->priv_host,
                       sctx->external_user, sctx->proxy_user, sctx->host,
                       sctx->ip,
                       old_db, (uint)strlen(old_db), old_tb, (uint)strlen(old_tb),
                       new_db, (uint)strlen(new_db), new_tb, (uint)strlen(new_tb));
  }
}

static inline
void mysql_audit_alter_table(THD *thd, TABLE_LIST *table)
{
  if (mysql_audit_table_enabled())
  {
    const Security_context *sctx= thd->security_ctx;
    mysql_audit_notify(thd, MYSQL_AUDIT_TABLE_CLASS, MYSQL_AUDIT_TABLE_ALTER,
                       0, (ulong)thd->thread_id,
                       sctx->user, sctx->priv_user, sctx->priv_host,
                       sctx->external_user, sctx->proxy_user, sctx->host,
                       sctx->ip, table->db, (uint)table->db_length,
                       table->table_name, (uint)table->table_name_length,
                       0,0,0,0);
  }
}

#endif /* SQL_AUDIT_INCLUDED */