-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathaudit_handler.h
441 lines (377 loc) · 10.7 KB
/
audit_handler.h
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
430
431
432
433
434
435
436
437
438
439
440
441
/*
* audit_handler.h
*
* Created on: Feb 6, 2011
* Author: guyl
*/
#ifndef AUDIT_HANDLER_H_
#define AUDIT_HANDLER_H_
#include "mysql_inc.h"
#include <yajl/yajl_gen.h>
#define AUDIT_LOG_PREFIX "Audit Plugin:"
class THD;
#define MAX_NUM_QUERY_TABLE_ELEM 100
typedef struct _QueryTableInf {
int num_of_elem;
char *db[MAX_NUM_QUERY_TABLE_ELEM];
char *table_name[MAX_NUM_QUERY_TABLE_ELEM];
const char *object_type [MAX_NUM_QUERY_TABLE_ELEM];
} QueryTableInf;
#define MAX_NUM_QUEUE_ELEM 1024
typedef struct _THDPRINTED {
size_t cur_index;
char is_thd_printed_queue [MAX_NUM_QUEUE_ELEM];
} THDPRINTED;
#define MAX_COMMAND_CHAR_NUMBERS 40
const char * retrieve_command (THD * thd);
typedef size_t OFFSET;
#define MAX_COM_STATUS_VARS_RECORDS 512
/**
* The struct usd to hold offsets. We should have one per version.
*/
typedef struct ThdOffsets
{
const char * version;
const char * md5digest;
OFFSET query_id;
OFFSET thread_id;
OFFSET main_security_ctx;
OFFSET command;
OFFSET lex;
OFFSET lex_comment;
} ThdOffsets;
/*
* On success, the number of bytes written are returned (zero indicates nothing was written). On error, -1 is returned,
*/
typedef ssize_t (*audit_write_func)(const char *, size_t);
/**
* Interface for an io writer
*/
class IWriter
{
public:
virtual ~IWriter() {}
virtual ssize_t write(const char * data, size_t size) = 0;
inline ssize_t write_str(const char * str)
{
return write(str, strlen(str));
}
};
class ThdSesData {
public:
ThdSesData(THD *pTHD);
THD* getTHD () { return m_pThd;}
const char * getCmdName () { return m_CmdName; }
private:
THD *m_pThd;
const char *m_CmdName;
protected:
ThdSesData (const ThdSesData& );
ThdSesData &operator =(const ThdSesData& );
};
/**
* Base for audit formatter
*/
class Audit_formatter
{
public:
virtual ~Audit_formatter() {}
/**
* static offsets to use for fetching THD data. Set by the audit plugin during startup.
*/
static ThdOffsets thd_offsets;
/**
* Format an audit event from the passed THD. Will write out its output using the audit_write_func.
*
* @return -1 on a failure
*/
virtual ssize_t event_format(ThdSesData *pThdData, IWriter * writer) =0;
/**
* format a message when handler is started
* @return -1 on a failure
*/
virtual ssize_t start_msg_format(IWriter * writer) { return 0; }
/**
* format a message when handler is stopped
* @return -1 on a failure
*/
virtual ssize_t stop_msg_format(IWriter * writer) { return 0; }
static const char * retrive_object_type (TABLE_LIST *pObj);
static QueryTableInf* getQueryCacheTableList1 (THD *thd);
//utility functions for fetching thd stuff
static inline my_thread_id thd_inst_thread_id(THD * thd)
{
return *(my_thread_id *) (((unsigned char *) thd)
+ Audit_formatter::thd_offsets.thread_id);
}
static inline query_id_t thd_inst_query_id(THD * thd)
{
return *(query_id_t *) (((unsigned char *) thd)
+ Audit_formatter::thd_offsets.query_id);
}
static inline Security_context * thd_inst_main_security_ctx(THD * thd)
{
return (Security_context *) (((unsigned char *) thd)
+ Audit_formatter::thd_offsets.main_security_ctx);
}
static inline int thd_inst_command(THD * thd)
{
return *(int *) (((unsigned char *) thd) + Audit_formatter::thd_offsets.command);
}
static inline LEX* thd_lex(THD * thd)
{
return *(LEX**) (((unsigned char *) thd) + Audit_formatter::thd_offsets.lex);
}
//will return a pointer to the query and set len with the length of the query
static inline const char * thd_query(THD * thd, size_t * len)
{
#if MYSQL_VERSION_ID >= 50505
MYSQL_LEX_STRING * str = thd_query_string(thd);
if(str)
{
*len = str->length;
return str->str;
}
*len = 0;
return NULL;
#else
*len = thd->query_length;
return thd->query;
#endif
}
};
/**
* Format the audit even in json format
*/
class Audit_json_formatter: public Audit_formatter
{
public:
static const char * DEF_MSG_DELIMITER;
Audit_json_formatter(): m_msg_delimiter(NULL)
{
config.beautify = 0;
config.indentString = NULL;
}
virtual ~Audit_json_formatter() {}
virtual ssize_t event_format(ThdSesData *pThdData, IWriter * writer);
/**
* Message delimiter. Should point to a valid json string (supporting the json escapping format).
* Will only be checked at the start. Public so can be set by sysvar.
*
* We only support a delimiter up to 32 chars
*/
char * m_msg_delimiter;
/**
* Configuration of yajl. Leave public so sysvar can update this directly.
*/
yajl_gen_config config;
protected:
Audit_json_formatter& operator =(const Audit_json_formatter& b) {};
Audit_json_formatter(const Audit_json_formatter& ) {};
};
/**
* Base class for audit handlers. Provides basic locking setup.
*/
class Audit_handler
{
public:
static const size_t MAX_AUDIT_HANDLERS_NUM = 4;
static const size_t BSON_FILE_HANDLER = 1;
static const size_t BSON_SOCKET_HANDLER = 3;
static Audit_handler * m_audit_handler_list[];
/**
* Will iterate the handler list and log using each handler
*/
static void log_audit_all(ThdSesData *pThdData);
/**
* Will iterate the handler list and stop all handlers
*/
static void stop_all();
Audit_handler() :
m_initialized(false), m_enabled(false), m_print_offset_err(true), m_formatter(NULL)
{
}
virtual ~Audit_handler()
{
if (m_initialized)
{
rwlock_destroy(&LOCK_audit);
}
}
/**
* Should be called to initialize. We don't init in constructor in order to provide indication if
* pthread stuff failed init.
*
* Will internally call handler_init();
*
* @frmt the formatter to use in this handler (does not manage distruction of this object)
* @return 0 on success
*/
int init(Audit_formatter * frmt)
{
m_formatter = frmt;
if (m_initialized)
{
return 0;
}
int res = my_rwlock_init(&LOCK_audit, NULL);
if (res)
{
return res;
}
res = handler_init();
if (res)
{
return res;
}
m_initialized = true;
return res;
}
bool is_init()
{
return m_initialized;
}
void set_enable(bool val);
/**
* Will get relevant shared lock and call internal method of handler
*/
void log_audit(ThdSesData *pThdData);
protected:
Audit_formatter * m_formatter;
virtual void handler_start() = 0;
virtual void handler_stop() = 0;
virtual int handler_init() = 0;
virtual void handler_log_audit(ThdSesData *pThdData) =0;
bool m_initialized;
bool m_enabled;
rw_lock_t LOCK_audit;
inline void lock_shared()
{
rw_rdlock(&LOCK_audit);
}
inline void lock_exclusive()
{
rw_wrlock(&LOCK_audit);
}
inline void unlock()
{
rw_unlock(&LOCK_audit);
}
Audit_handler & operator=(const Audit_handler&) {};
Audit_handler(const Audit_handler&) {};
private:
//bool indicating if to print offset errors to log or not
bool m_print_offset_err;
};
/**
* Base class for handler which have io and need a lock
*/
class Audit_io_handler: public Audit_handler, public IWriter
{
public:
virtual ~Audit_io_handler()
{
if (m_initialized)
{
pthread_mutex_destroy(&LOCK_io);
}
}
protected:
virtual int handler_init()
{
return pthread_mutex_init(&LOCK_io, MY_MUTEX_INIT_SLOW);
}
//mutex we need sync writes on file
pthread_mutex_t LOCK_io;
};
class Audit_file_handler: public Audit_io_handler
{
public:
Audit_file_handler() :
m_log_file(NULL), m_sync_period(0), m_sync_counter(0), m_filename(NULL)
{
}
virtual ~Audit_file_handler()
{
}
/**
* The period to use for syncing to the file system. 0 means we don't sync.
* 1 means each write we sync. Larger than 1 means every sync_period we sync.
*
* We leave this public so the mysql sysvar function can update this variable directly.
*/
unsigned int m_sync_period;
/**
* File name of the file we write to. Public so sysvar function can update this directly.
*/
char * m_filename;
/**
* Write function we pass to formatter
*/
ssize_t write(const char * data, size_t size);
//static void print_sleep (THD *thd, int delay_ms);
protected:
Audit_file_handler & operator=(const Audit_file_handler&) {};
Audit_file_handler(const Audit_file_handler&) {};
virtual void handler_start();
virtual void handler_stop();
/**
* Will acquire locks and call handler_write
*/
virtual void handler_log_audit(ThdSesData *pThdData);
FILE * m_log_file;
//the period to use for syncing
unsigned int m_sync_counter;
void close_file()
{
if (m_log_file)
{
my_fclose(m_log_file, MYF(0));
}
m_log_file = NULL;
}
};
class Audit_socket_handler: public Audit_io_handler
{
public:
Audit_socket_handler() :
m_sockname(NULL), m_vio(NULL), m_connect_timeout(1)
{
}
virtual ~Audit_socket_handler()
{
}
/**
* Socket name of the UNIX socket we write to. Public so sysvar function can update this directly.
*/
char * m_sockname;
/**
* Connect timeout in secconds
*/
unsigned int m_connect_timeout;
/**
* Write function we pass to formatter
*/
ssize_t write(const char * data, size_t size);
protected:
Audit_socket_handler & operator=(const Audit_socket_handler&) {};
Audit_socket_handler(const Audit_socket_handler&) {};
virtual void handler_start();
virtual void handler_stop();
/**
* Will acquire locks and call handler_write
*/
virtual void handler_log_audit(ThdSesData *pThdData);
//Vio we write to
Vio * m_vio;
void close_vio()
{
if (m_vio)
{
vio_close(m_vio);
vio_delete(m_vio);
}
m_vio = NULL;
}
};
#endif /* AUDIT_HANDLER_H_ */