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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
#ifndef SQL_AUDIT_INCLUDED
#define SQL_AUDIT_INCLUDED
/* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2017, MariaDB Corporation.
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-1335 USA */
#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, const void *event);
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,
const void *event) {}
#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);
static inline unsigned int strlen_uint(const char *s)
{
return (uint)strlen(s);
}
static inline unsigned int safe_strlen_uint(const char *s)
{
return (uint)safe_strlen(s);
}
#define MAX_USER_HOST_SIZE 512
static inline uint make_user_name(THD *thd, char *buf)
{
const Security_context *sctx= thd->security_ctx;
char *end= 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);
return (uint)(end-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())
{
mysql_event_general event;
event.event_subclass= MYSQL_AUDIT_GENERAL_LOG;
event.general_error_code= 0;
event.general_time= time;
event.general_user= user;
event.general_user_length= userlen;
event.general_command= cmd;
event.general_command_length= cmdlen;
event.general_query= query;
event.general_query_length= querylen;
event.general_rows= 0;
if (thd)
{
event.general_thread_id= (unsigned long)thd->thread_id;
event.general_charset= thd->variables.character_set_client;
event.database= thd->db;
event.query_id= thd->query_id;
}
else
{
event.general_thread_id= 0;
event.general_charset= global_system_variables.character_set_client;
event.database= null_clex_str;
event.query_id= 0;
}
mysql_audit_notify(thd, MYSQL_AUDIT_GENERAL_CLASS, &event);
}
}
/**
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)
{
DBUG_ENTER("mysql_audit_general");
if (mysql_audit_general_enabled())
{
char user_buff[MAX_USER_HOST_SIZE];
mysql_event_general event;
event.event_subclass= event_subtype;
event.general_error_code= error_code;
event.general_time= my_time(0);
event.general_command= msg;
event.general_command_length= safe_strlen_uint(msg);
if (thd)
{
event.general_user= user_buff;
event.general_user_length= make_user_name(thd, user_buff);
event.general_thread_id= (unsigned long)thd->thread_id;
event.general_query= thd->query_string.str();
event.general_query_length= (unsigned) thd->query_string.length();
event.general_charset= thd->query_string.charset();
event.general_rows= thd->get_stmt_da()->current_row_for_warning();
event.database= thd->db;
event.query_id= thd->query_id;
}
else
{
event.general_user= NULL;
event.general_user_length= 0;
event.general_thread_id= 0;
event.general_query= NULL;
event.general_query_length= 0;
event.general_charset= &my_charset_bin;
event.general_rows= 0;
event.database= null_clex_str;
event.query_id= 0;
}
mysql_audit_notify(thd, MYSQL_AUDIT_GENERAL_CLASS, &event);
}
DBUG_VOID_RETURN;
}
static inline
void mysql_audit_notify_connection_connect(THD *thd)
{
if (mysql_audit_connection_enabled())
{
const Security_context *sctx= thd->security_ctx;
mysql_event_connection event;
event.event_subclass= MYSQL_AUDIT_CONNECTION_CONNECT;
event.status= thd->get_stmt_da()->is_error() ?
thd->get_stmt_da()->sql_errno() : 0;
event.thread_id= (unsigned long)thd->thread_id;
event.user= sctx->user;
event.user_length= safe_strlen_uint(sctx->user);
event.priv_user= sctx->priv_user;
event.priv_user_length= strlen_uint(sctx->priv_user);
event.external_user= sctx->external_user;
event.external_user_length= safe_strlen_uint(sctx->external_user);
event.proxy_user= sctx->proxy_user;
event.proxy_user_length= strlen_uint(sctx->proxy_user);
event.host= sctx->host;
event.host_length= safe_strlen_uint(sctx->host);
event.ip= sctx->ip;
event.ip_length= safe_strlen_uint(sctx->ip);
event.database= thd->db;
mysql_audit_notify(thd, MYSQL_AUDIT_CONNECTION_CLASS, &event);
}
}
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_event_connection event;
event.event_subclass= MYSQL_AUDIT_CONNECTION_DISCONNECT;
event.status= errcode;
event.thread_id= (unsigned long)thd->thread_id;
event.user= sctx->user;
event.user_length= safe_strlen_uint(sctx->user);
event.priv_user= sctx->priv_user;
event.priv_user_length= strlen_uint(sctx->priv_user);
event.external_user= sctx->external_user;
event.external_user_length= safe_strlen_uint(sctx->external_user);
event.proxy_user= sctx->proxy_user;
event.proxy_user_length= strlen_uint(sctx->proxy_user);
event.host= sctx->host;
event.host_length= safe_strlen_uint(sctx->host);
event.ip= sctx->ip;
event.ip_length= safe_strlen_uint(sctx->ip) ;
event.database= thd->db;
mysql_audit_notify(thd, MYSQL_AUDIT_CONNECTION_CLASS, &event);
}
}
static inline
void mysql_audit_notify_connection_change_user(THD *thd)
{
if (mysql_audit_connection_enabled())
{
const Security_context *sctx= thd->security_ctx;
mysql_event_connection event;
event.event_subclass= MYSQL_AUDIT_CONNECTION_CHANGE_USER;
event.status= thd->get_stmt_da()->is_error() ?
thd->get_stmt_da()->sql_errno() : 0;
event.thread_id= (unsigned long)thd->thread_id;
event.user= sctx->user;
event.user_length= safe_strlen_uint(sctx->user);
event.priv_user= sctx->priv_user;
event.priv_user_length= strlen_uint(sctx->priv_user);
event.external_user= sctx->external_user;
event.external_user_length= safe_strlen_uint(sctx->external_user);
event.proxy_user= sctx->proxy_user;
event.proxy_user_length= strlen_uint(sctx->proxy_user);
event.host= sctx->host;
event.host_length= safe_strlen_uint(sctx->host);
event.ip= sctx->ip;
event.ip_length= safe_strlen_uint(sctx->ip);
event.database= thd->db;
mysql_audit_notify(thd, MYSQL_AUDIT_CONNECTION_CLASS, &event);
}
}
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_event_table event;
event.event_subclass= MYSQL_AUDIT_TABLE_LOCK;
event.read_only= lock == F_RDLCK;
event.thread_id= (unsigned long)thd->thread_id;
event.user= sctx->user;
event.priv_user= sctx->priv_user;
event.priv_host= sctx->priv_host;
event.external_user= sctx->external_user;
event.proxy_user= sctx->proxy_user;
event.host= sctx->host;
event.ip= sctx->ip;
event.database= share->db;
event.table= share->table_name;
event.new_database= null_clex_str;
event.new_table= null_clex_str;
event.query_id= thd->query_id;
mysql_audit_notify(thd, MYSQL_AUDIT_TABLE_CLASS, &event);
}
}
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_event_table event;
event.event_subclass= MYSQL_AUDIT_TABLE_CREATE;
event.read_only= 0;
event.thread_id= (unsigned long)thd->thread_id;
event.user= sctx->user;
event.priv_user= sctx->priv_user;
event.priv_host= sctx->priv_host;
event.external_user= sctx->external_user;
event.proxy_user= sctx->proxy_user;
event.host= sctx->host;
event.ip= sctx->ip;
event.database= share->db;
event.table= share->table_name;
event.new_database= null_clex_str;
event.new_table= null_clex_str;
event.query_id= thd->query_id;
mysql_audit_notify(thd, MYSQL_AUDIT_TABLE_CLASS, &event);
}
}
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_event_table event;
event.event_subclass= MYSQL_AUDIT_TABLE_DROP;
event.read_only= 0;
event.thread_id= (unsigned long)thd->thread_id;
event.user= sctx->user;
event.priv_user= sctx->priv_user;
event.priv_host= sctx->priv_host;
event.external_user= sctx->external_user;
event.proxy_user= sctx->proxy_user;
event.host= sctx->host;
event.ip= sctx->ip;
event.database= table->db;
event.table= table->table_name;
event.new_database= null_clex_str;
event.new_table= null_clex_str;
event.query_id= thd->query_id;
mysql_audit_notify(thd, MYSQL_AUDIT_TABLE_CLASS, &event);
}
}
static inline
void mysql_audit_rename_table(THD *thd, const LEX_CSTRING *old_db,
const LEX_CSTRING *old_tb,
const LEX_CSTRING *new_db, const LEX_CSTRING *new_tb)
{
if (mysql_audit_table_enabled())
{
const Security_context *sctx= thd->security_ctx;
mysql_event_table event;
event.event_subclass= MYSQL_AUDIT_TABLE_RENAME;
event.read_only= 0;
event.thread_id= (unsigned long)thd->thread_id;
event.user= sctx->user;
event.priv_user= sctx->priv_user;
event.priv_host= sctx->priv_host;
event.external_user= sctx->external_user;
event.proxy_user= sctx->proxy_user;
event.host= sctx->host;
event.ip= sctx->ip;
event.database= *old_db;
event.table= *old_tb;
event.new_database= *new_db;
event.new_table= *new_tb;
event.query_id= thd->query_id;
mysql_audit_notify(thd, MYSQL_AUDIT_TABLE_CLASS, &event);
}
}
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_event_table event;
event.event_subclass= MYSQL_AUDIT_TABLE_ALTER;
event.read_only= 0;
event.thread_id= (unsigned long)thd->thread_id;
event.user= sctx->user;
event.priv_user= sctx->priv_user;
event.priv_host= sctx->priv_host;
event.external_user= sctx->external_user;
event.proxy_user= sctx->proxy_user;
event.host= sctx->host;
event.ip= sctx->ip;
event.database= table->db;
event.table= table->table_name;
event.new_database= null_clex_str;
event.new_table= null_clex_str;
event.query_id= thd->query_id;
mysql_audit_notify(thd, MYSQL_AUDIT_TABLE_CLASS, &event);
}
}
#endif /* SQL_AUDIT_INCLUDED */
|