-
Notifications
You must be signed in to change notification settings - Fork 0
/
audit_log.c
1010 lines (860 loc) · 30.1 KB
/
audit_log.c
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2014 Percona LLC 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 <time.h>
#include <string.h>
#include <stdio.h>
#include <my_global.h>
#include <mysql/plugin.h>
#include <mysql/plugin_audit.h>
#include <typelib.h>
#include <mysql_version.h>
#include <mysql_com.h>
#include <my_pthread.h>
#include <syslog.h>
#include "logger.h"
#include "buffer.h"
#include "audit_handler.h"
#define PLUGIN_VERSION 0x0002
enum audit_log_policy_t { ALL, NONE, LOGINS, QUERIES };
enum audit_log_strategy_t
{ ASYNCHRONOUS, PERFORMANCE, SEMISYNCHRONOUS, SYNCHRONOUS };
enum audit_log_format_t { OLD, NEW, JSON, CSV };
#ifdef AUDIT_HAVE_MONGO
enum audit_log_handler_t { HANDLER_FILE, HANDLER_SYSLOG, HANDLER_MONGO };
#else
enum audit_log_handler_t { HANDLER_FILE, HANDLER_SYSLOG };
#endif
typedef void (*escape_buf_func_t)(const char *, size_t *, char *, size_t *);
static audit_handler_t *log_handler= NULL;
static ulonglong record_id= 0;
static time_t log_file_time= 0;
char *audit_log_file;
char default_audit_log_file[]= "audit.log";
ulong audit_log_policy= ALL;
ulong audit_log_strategy= ASYNCHRONOUS;
ulonglong audit_log_buffer_size= 1048576;
ulonglong audit_log_rotate_on_size= 0;
ulonglong audit_log_rotations= 0;
char audit_log_flush= FALSE;
ulong audit_log_format= OLD;
ulong audit_log_handler= HANDLER_FILE;
char *audit_log_syslog_ident;
char default_audit_log_syslog_ident[] = "percona-audit";
ulong audit_log_syslog_facility= 0;
ulong audit_log_syslog_priority= 0;
#ifdef AUDIT_HAVE_MONGO
char default_audit_log_mongo_collection[] = "audit";
char default_audit_log_mongo_uri[] = "mongodb://";
char *audit_log_mongo_collection;
char *audit_log_mongo_uri;
#endif
static int audit_log_syslog_facility_codes[]=
{ LOG_USER, LOG_AUTHPRIV, LOG_CRON, LOG_DAEMON, LOG_FTP,
LOG_KERN, LOG_LPR, LOG_MAIL, LOG_NEWS,
#if (defined LOG_SECURITY)
LOG_SECURITY,
#endif
LOG_SYSLOG, LOG_AUTH, LOG_UUCP, LOG_LOCAL0, LOG_LOCAL1,
LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4, LOG_LOCAL5, LOG_LOCAL6,
LOG_LOCAL7, 0};
static const char *audit_log_syslog_facility_names[]=
{ "LOG_USER", "LOG_AUTHPRIV", "LOG_CRON", "LOG_DAEMON", "LOG_FTP",
"LOG_KERN", "LOG_LPR", "LOG_MAIL", "LOG_NEWS",
#if (defined LOG_SECURITY)
"LOG_SECURITY",
#endif
"LOG_SYSLOG", "LOG_AUTH", "LOG_UUCP", "LOG_LOCAL0", "LOG_LOCAL1",
"LOG_LOCAL2", "LOG_LOCAL3", "LOG_LOCAL4", "LOG_LOCAL5", "LOG_LOCAL6",
"LOG_LOCAL7", 0 };
static const int audit_log_syslog_priority_codes[]=
{ LOG_INFO, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING,
LOG_NOTICE, LOG_EMERG, LOG_DEBUG, 0 };
static const char *audit_log_syslog_priority_names[]=
{ "LOG_INFO", "LOG_ALERT", "LOG_CRIT", "LOG_ERR", "LOG_WARNING",
"LOG_NOTICE", "LOG_EMERG", "LOG_DEBUG", 0 };
static
void init_record_id(off_t size)
{
record_id= size;
}
static
ulonglong next_record_id()
{
return __sync_add_and_fetch(&record_id, 1);
}
#define MAX_RECORD_ID_SIZE 50
#define MAX_TIMESTAMP_SIZE 25
extern
void fprintf_timestamp(FILE *file)
{
char timebuf[50];
struct tm tm;
time_t curtime;
memset(&tm, 0, sizeof(tm));
time(&curtime);
localtime_r(&curtime, &tm);
strftime(timebuf, sizeof(timebuf), "%FT%T", gmtime_r(&curtime, &tm));
fprintf(file, "%s audit_log: ", timebuf);
}
static
char *make_timestamp(char *buf, size_t buf_len, time_t t)
{
struct tm tm;
memset(&tm, 0, sizeof(tm));
strftime(buf, buf_len, "%FT%T UTC", gmtime_r(&t, &tm));
return buf;
}
static
char *make_record_id(char *buf, size_t buf_len, time_t t)
{
struct tm tm;
size_t len;
memset(&tm, 0, sizeof(tm));
len= snprintf(buf, buf_len, "%llu_", next_record_id());
strftime(buf + len, buf_len - len, "%FT%T", gmtime_r(&t, &tm));
return buf;
}
typedef struct
{
char character;
size_t length;
const char *replacement;
} escape_rule_t;
static
void escape_buf(const char *in, size_t *inlen, char *out, size_t *outlen,
const escape_rule_t *escape_rules)
{
char* outstart = out;
const char* base = in;
char* outend = out + *outlen;
const char* inend;
const escape_rule_t *rule;
my_bool replaced;
inend = in + (*inlen);
while ((in < inend) && (out < outend))
{
replaced= FALSE;
for (rule= escape_rules; rule->character; rule++)
{
if (*in == rule->character)
{
if ((outend - out) < (int) rule->length)
goto end_of_buffer;
memcpy(out, rule->replacement, rule->length);
out += rule->length;
replaced= TRUE;
break;
}
}
if (!replaced)
*out++ = *in;
++in;
}
end_of_buffer:
*outlen = out - outstart;
*inlen = in - base;
}
static
void xml_escape(const char *in, size_t *inlen, char *out, size_t *outlen)
{
const escape_rule_t rules[]=
{
{ '<', 4, "<" },
{ '>', 4, ">" },
{ '&', 5, "&" },
{ '\r', 5, " " },
{ '\n', 5, " " },
{ '"', 6, """ },
{ 0, 0, NULL }
};
escape_buf(in, inlen, out, outlen, rules);
}
static
void json_escape(const char *in, size_t *inlen, char *out, size_t *outlen)
{
const escape_rule_t rules[]=
{
{ '\\', 2, "\\\\" },
{ '"', 2, "\\\"" },
{ '\r', 2, "\\r" },
{ '\n', 2, "\\n" },
{ '\t', 2, "\\t" },
{ 0, 0, NULL }
};
escape_buf(in, inlen, out, outlen, rules);
}
static
void csv_escape(const char *in, size_t *inlen, char *out, size_t *outlen)
{
const escape_rule_t rules[]=
{
{ '"', 2, "\"\"" },
{ 0, 0, NULL }
};
escape_buf(in, inlen, out, outlen, rules);
}
static
char *escape_string(const char *in, size_t inlen,
char *out, size_t outlen,
char **endptr)
{
const escape_buf_func_t format_escape_func[]=
{ xml_escape, xml_escape, json_escape, csv_escape };
if (in != NULL)
{
--outlen;
format_escape_func[audit_log_format](in, &inlen, out, &outlen);
out[outlen]= 0;
if (endptr)
*endptr= out + outlen + 1;
}
else
{
*out= 0;
if (endptr)
*endptr= out + 1;
}
return out;
}
static
void audit_log_write(const char *buf, size_t len)
{
static int write_error= 0;
if (audit_handler_write(log_handler, buf, len) < 0)
{
if (!write_error)
{
write_error= 1;
fprintf_timestamp(stderr);
fprintf(stderr, "Error writing to file %s. ", audit_log_file);
perror("Error: ");
}
}
else
{
write_error= 0;
}
}
/* Defined in MySQL server */
extern int orig_argc;
extern char **orig_argv;
extern char server_version[SERVER_VERSION_LENGTH];
static
char *make_argv(char *buf, size_t len, int argc, char **argv)
{
size_t left= len;
buf[0]= 0;
while (argc > 0 && left > 0)
{
left-= my_snprintf(buf + len - left, left,
"%s%c", *argv, argc > 1 ? ' ' : 0);
argc--; argv++;
}
return buf;
}
static
size_t audit_log_audit_record(char *buf, size_t buflen,
const char *name, time_t t)
{
char id_str[MAX_RECORD_ID_SIZE];
char timestamp[MAX_TIMESTAMP_SIZE];
char arg_buf[512];
const char *format_string[] = {
"<AUDIT_RECORD\n"
" NAME=\"%s\"\n"
" RECORD=\"%s\"\n"
" TIMESTAMP=\"%s\"\n"
" MYSQL_VERSION=\"%s\"\n"
" STARTUP_OPTIONS=\"%s\"\n"
" OS_VERSION=\""MACHINE_TYPE"-"SYSTEM_TYPE"\"\n"
"/>\n",
"<AUDIT_RECORD>\n"
" <NAME>%s</NAME>\n"
" <RECORD>%s</RECORD>\n"
" <TIMESTAMP>%s</TIMESTAMP>\n"
" <MYSQL_VERSION>%s</MYSQL_VERSION>\n"
" <STARTUP_OPTIONS>%s</STARTUP_OPTIONS>\n"
" <OS_VERSION>"MACHINE_TYPE"-"SYSTEM_TYPE"</OS_VERSION>\n"
"</AUDIT_RECORD>\n",
"{\"audit_record\":{\"name\":\"%s\",\"record\":\"%s\","
"\"timestamp\":\"%s\",\"mysql_version\":\"%s\","
"\"startup_optionsi\":\"%s\","
"\"os_version\":\""MACHINE_TYPE"-"SYSTEM_TYPE"\"}}\n",
"\"%s\",\"%s\",\"%s\",\"%s\",\"%s\","
"\""MACHINE_TYPE"-"SYSTEM_TYPE"\"\n" };
return my_snprintf(buf, buflen,
format_string[audit_log_format],
name,
make_record_id(id_str, sizeof(id_str), t),
make_timestamp(timestamp, sizeof(timestamp), t),
server_version,
make_argv(arg_buf, sizeof(arg_buf),
orig_argc - 1, orig_argv + 1));
}
static
size_t audit_log_general_record(char *buf, size_t buflen,
const char *name, time_t t, int status,
const struct mysql_event_general *event)
{
char id_str[MAX_RECORD_ID_SIZE];
char timestamp[MAX_TIMESTAMP_SIZE];
char query[512], tmp[128];
char *endptr= tmp, *endtmp= tmp + sizeof(tmp);
const char *format_string[] = {
"<AUDIT_RECORD\n"
" NAME=\"%s\"\n"
" RECORD=\"%s\"\n"
" TIMESTAMP=\"%s\"\n"
" COMMAND_CLASS=\"%s\"\n"
" CONNECTION_ID=\"%lu\"\n"
" STATUS=\"%d\"\n"
" SQLTEXT=\"%s\"\n"
" USER=\"%s\"\n"
" HOST=\"%s\"\n"
" OS_USER=\"%s\"\n"
" IP=\"%s\"\n"
"/>\n",
"<AUDIT_RECORD>\n"
" <NAME>%s</NAME>\n"
" <RECORD>%s</RECORD>\n"
" <TIMESTAMP>%s</TIMESTAMP>\n"
" <COMMAND_CLASS>%s</COMMAND_CLASS>\n"
" <CONNECTION_ID>%lu</CONNECTION_ID>\n"
" <STATUS>%d</STATUS>\n"
" <SQLTEXT>%s</SQLTEXT>\n"
" <USER>%s</USER>\n"
" <HOST>%s</HOST>\n"
" <OS_USER>%s</OS_USER>\n"
" <IP>%s</IP>\n"
"</AUDIT_RECORD>\n",
"{\"audit_record\":"
"{\"name\":\"%s\","
"\"record\":\"%s\","
"\"timestamp\":\"%s\","
"\"command_class\":\"%s\","
"\"connection_id\":\"%lu\","
"\"status\":%d,"
"\"sqltext\":\"%s\","
"\"user\":\"%s\","
"\"host\":\"%s\","
"\"os_user\":\"%s\","
"\"ip\":\"%s\"}}\n",
"\"%s\",\"%s\",\"%s\",\"%s\",\"%lu\",%d,\"%s\",\"%s\","
"\"%s\",\"%s\",\"%s\"\n" };
return my_snprintf(buf, buflen,
format_string[audit_log_format],
name,
make_record_id(id_str, sizeof(id_str), t),
make_timestamp(timestamp, sizeof(timestamp), t),
event->general_sql_command.str,
event->general_thread_id,
status,
escape_string(event->general_query,
event->general_query_length,
query, sizeof(query), NULL),
escape_string(event->general_user,
event->general_user_length,
endptr, endtmp - endptr, &endptr),
escape_string(event->general_host.str,
event->general_host.length,
endptr, endtmp - endptr, &endptr),
escape_string(event->general_external_user.str,
event->general_external_user.length,
endptr, endtmp - endptr, &endptr),
escape_string(event->general_ip.str,
event->general_ip.length,
endptr, endtmp - endptr, &endptr));
}
static
size_t audit_log_connection_record(char *buf, size_t buflen,
const char *name, time_t t,
const struct mysql_event_connection *event)
{
char id_str[MAX_RECORD_ID_SIZE];
char timestamp[MAX_TIMESTAMP_SIZE];
char tmp[128];
char *endptr= tmp, *endtmp= tmp + sizeof(tmp);
const char *format_string[] = {
"<AUDIT_RECORD\n"
" NAME=\"%s\"\n"
" RECORD=\"%s\"\n"
" TIMESTAMP=\"%s\"\n"
" CONNECTION_ID=\"%lu\"\n"
" STATUS=\"%d\"\n"
" USER=\"%s\"\n"
" PRIV_USER=\"%s\"\n"
" OS_LOGIN=\"%s\"\n"
" PROXY_USER=\"%s\"\n"
" HOST=\"%s\"\n"
" IP=\"%s\"\n"
" DB=\"%s\"\n"
"/>\n",
"<AUDIT_RECORD>\n"
" <NAME>%s</NAME>\n"
" <RECORD>%s</RECORD>\n"
" <TIMESTAMP>%s</TIMESTAMP>\n"
" <CONNECTION_ID>%lu</CONNECTION_ID>\n"
" <STATUS>%d</STATUS>\n"
" <USER>%s</USER>\n"
" <PRIV_USER>%s</PRIV_USER>\n"
" <OS_LOGIN>%s</OS_LOGIN>\n"
" <PROXY_USER>%s</PROXY_USER>\n"
" <HOST>%s</HOST>\n"
" <IP>%s</IP>\n"
" <DB>%s</DB>\n"
"</AUDIT_RECORD>\n",
"{\"audit_record\":"
"{\"name\":\"%s\","
"\"record\":\"%s\","
"\"timestamp\":\"%s\","
"\"connection_id\":\"%lu\","
"\"status\":%d,"
"\"user\":\"%s\","
"\"priv_user\":\"%s\","
"\"os_login\":\"%s\","
"\"proxy_user\":\"%s\","
"\"host\":\"%s\","
"\"ip\":\"%s\","
"\"db\":\"%s\"}}\n",
"\"%s\",\"%s\",\"%s\",\"%lu\",%d,\"%s\",\"%s\",\"%s\","
"\"%s\",\"%s\",\"%s\",\"%s\"\n" };
return my_snprintf(buf, buflen,
format_string[audit_log_format],
name,
make_record_id(id_str, sizeof(id_str), t),
make_timestamp(timestamp, sizeof(timestamp), t),
event->thread_id,
event->status,
escape_string(event->user,
event->user_length,
endptr, endtmp - endptr, &endptr),
escape_string(event->priv_user,
event->priv_user_length,
endptr, endtmp - endptr, &endptr),
escape_string(event->external_user,
event->external_user_length,
endptr, endtmp - endptr, &endptr),
escape_string(event->proxy_user,
event->proxy_user_length,
endptr, endtmp - endptr, &endptr),
escape_string(event->host,
event->host_length,
endptr, endtmp - endptr, &endptr),
escape_string(event->ip,
event->ip_length,
endptr, endtmp - endptr, &endptr),
escape_string(event->database,
event->database_length,
endptr, endtmp - endptr, &endptr));
}
static
size_t audit_log_header(MY_STAT *stat, char *buf, size_t buflen)
{
const char *format_string[] = {
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<AUDIT>\n",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<AUDIT>\n",
"",
"" };
log_file_time= stat->st_mtime;
init_record_id(stat->st_size);
if (buf == NULL)
{
return 0;
}
return my_snprintf(buf, buflen, format_string[audit_log_format]);
}
static
size_t audit_log_footer(char *buf, size_t buflen)
{
const char *format_string[] = {
"</AUDIT>\n",
"</AUDIT>\n",
"",
"" };
if (buf == NULL)
{
return 0;
}
return my_snprintf(buf, buflen, format_string[audit_log_format]);
}
static
int init_new_log_file()
{
#ifdef AUDIT_HAVE_MONGO
if (audit_log_handler == HANDLER_MONGO)
{
audit_handler_mongo_config_t opts;
opts.uri= audit_log_mongo_uri;
opts.collection= audit_log_mongo_collection;
opts.header= audit_log_header;
opts.footer= audit_log_footer;
log_handler= audit_handler_mongo_open(&opts);
if (log_handler == NULL)
{
fprintf_timestamp(stderr);
fprintf(stderr, "Cannot open connection to '%s'. ", audit_log_mongo_uri);
perror("Error: ");
return(1);
}
}
else
#endif
if (audit_log_handler == HANDLER_FILE)
{
audit_handler_file_config_t opts;
opts.name= audit_log_file;
opts.rotate_on_size= audit_log_rotate_on_size;
opts.rotations= audit_log_rotations;
opts.sync_on_write= audit_log_strategy == SYNCHRONOUS;
opts.use_buffer= audit_log_strategy < SEMISYNCHRONOUS;
opts.buffer_size= audit_log_buffer_size;
opts.can_drop_data= audit_log_strategy == PERFORMANCE;
opts.header= audit_log_header;
opts.footer= audit_log_footer;
log_handler= audit_handler_file_open(&opts);
if (log_handler == NULL)
{
fprintf_timestamp(stderr);
fprintf(stderr, "Cannot open file %s. ", audit_log_file);
perror("Error: ");
return(1);
}
}
else
{
audit_handler_syslog_config_t opts;
opts.facility= audit_log_syslog_facility_codes[audit_log_syslog_facility];
opts.ident= audit_log_syslog_ident;
opts.priority= audit_log_syslog_priority_codes[audit_log_syslog_priority];
opts.header= audit_log_header;
opts.footer= audit_log_footer;
log_handler= audit_handler_syslog_open(&opts);
if (log_handler == NULL)
{
fprintf_timestamp(stderr);
fprintf(stderr, "Cannot open syslog. ");
perror("Error: ");
return(1);
}
}
return(0);
}
static
int reopen_log_file()
{
if (audit_handler_flush(log_handler))
{
fprintf_timestamp(stderr);
fprintf(stderr, "Cannot open file %s. ", audit_log_file);
perror("Error: ");
return(1);
}
return(0);
}
static
int audit_log_plugin_init(void *arg __attribute__((unused)))
{
char buf[1024];
size_t len;
logger_init_mutexes();
if (init_new_log_file())
return(1);
len= audit_log_audit_record(buf, sizeof(buf), "Audit", time(NULL));
audit_log_write(buf, len);
return(0);
}
static
int audit_log_plugin_deinit(void *arg __attribute__((unused)))
{
char buf[1024];
size_t len;
len= audit_log_audit_record(buf, sizeof(buf), "NoAudit", time(NULL));
audit_log_write(buf, len);
audit_handler_close(log_handler);
return(0);
}
static
int is_event_class_allowed_by_policy(unsigned int class,
enum audit_log_policy_t policy)
{
static unsigned int class_mask[]=
{
MYSQL_AUDIT_GENERAL_CLASSMASK | MYSQL_AUDIT_CONNECTION_CLASSMASK, /* ALL */
0, /* NONE */
MYSQL_AUDIT_CONNECTION_CLASSMASK, /* LOGINS */
MYSQL_AUDIT_GENERAL_CLASSMASK, /* QUERIES */
};
return (class_mask[policy] & (1 << class)) != 0;
}
static
void audit_log_notify(MYSQL_THD thd __attribute__((unused)),
unsigned int event_class,
const void *event)
{
char buf[1024];
size_t len;
if (!is_event_class_allowed_by_policy(event_class, audit_log_policy))
return;
if (event_class == MYSQL_AUDIT_GENERAL_CLASS)
{
const struct mysql_event_general *event_general=
(const struct mysql_event_general *) event;
switch (event_general->event_subclass)
{
case MYSQL_AUDIT_GENERAL_STATUS:
if (event_general->general_command_length == 4 &&
strncmp(event_general->general_command, "Quit", 4) == 0)
break;
len= audit_log_general_record(buf, sizeof(buf),
event_general->general_command,
event_general->general_time,
event_general->general_error_code,
event_general);
audit_log_write(buf, len);
break;
}
}
else if (event_class == MYSQL_AUDIT_CONNECTION_CLASS)
{
const struct mysql_event_connection *event_connection=
(const struct mysql_event_connection *) event;
switch (event_connection->event_subclass)
{
case MYSQL_AUDIT_CONNECTION_CONNECT:
len= audit_log_connection_record(buf, sizeof(buf),
"Connect", time(NULL), event_connection);
audit_log_write(buf, len);
break;
case MYSQL_AUDIT_CONNECTION_DISCONNECT:
len= audit_log_connection_record(buf, sizeof(buf),
"Quit", time(NULL), event_connection);
audit_log_write(buf, len);
break;
case MYSQL_AUDIT_CONNECTION_CHANGE_USER:
len= audit_log_connection_record(buf, sizeof(buf),
"Change user", time(NULL),
event_connection);
audit_log_write(buf, len);
break;
default:
break;
}
}
}
/*
* Plugin system vars
*/
static MYSQL_SYSVAR_STR(file, audit_log_file,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY | PLUGIN_VAR_MEMALLOC,
"The name of the log file.", NULL, NULL, default_audit_log_file);
static const char *audit_log_policy_names[]=
{ "ALL", "NONE", "LOGINS", "QUERIES", 0 };
static TYPELIB audit_log_policy_typelib=
{
array_elements(audit_log_policy_names) - 1, "audit_log_policy_typelib",
audit_log_policy_names, NULL
};
static MYSQL_SYSVAR_ENUM(policy, audit_log_policy, PLUGIN_VAR_RQCMDARG,
"The policy controlling the information written by the audit log "
"plugin to its log file.", NULL, NULL, ALL,
&audit_log_policy_typelib);
static const char *audit_log_strategy_names[]=
{ "ASYNCHRONOUS", "PERFORMANCE", "SEMISYNCHRONOUS", "SYNCHRONOUS", 0 };
static TYPELIB audit_log_strategy_typelib=
{
array_elements(audit_log_strategy_names) - 1, "audit_log_strategy_typelib",
audit_log_strategy_names, NULL
};
static MYSQL_SYSVAR_ENUM(strategy, audit_log_strategy,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"The logging method used by the audit log plugin, "
"if FILE handler is used.", NULL, NULL,
ASYNCHRONOUS, &audit_log_strategy_typelib);
static const char *audit_log_format_names[]=
{ "OLD", "NEW", "JSON", "CSV", 0 };
static TYPELIB audit_log_format_typelib=
{
array_elements(audit_log_format_names) - 1, "audit_log_format_typelib",
audit_log_format_names, NULL
};
static MYSQL_SYSVAR_ENUM(format, audit_log_format,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"The audit log file format.", NULL, NULL,
ASYNCHRONOUS, &audit_log_format_typelib);
#ifdef AUDIT_HAVE_MONGO
static const char *audit_log_handler_names[]=
{ "FILE", "SYSLOG", "MONGO", 0 };
#else
static const char *audit_log_handler_names[]=
{ "FILE", "SYSLOG", 0 };
#endif
static TYPELIB audit_log_handler_typelib=
{
array_elements(audit_log_handler_names) - 1, "audit_log_handler_typelib",
audit_log_handler_names, NULL
};
static MYSQL_SYSVAR_ENUM(handler, audit_log_handler,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"The audit log handler.", NULL, NULL,
HANDLER_FILE, &audit_log_handler_typelib);
static MYSQL_SYSVAR_ULONGLONG(buffer_size, audit_log_buffer_size,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"The size of the buffer for asynchronous logging, "
"if FILE handler is used.",
NULL, NULL, 1048576UL, 4096UL, ULONGLONG_MAX, 4096UL);
static
void audit_log_rotate_on_size_update(
MYSQL_THD thd __attribute__((unused)),
struct st_mysql_sys_var *var __attribute__((unused)),
void *var_ptr __attribute__((unused)),
const void *save)
{
ulonglong new_val= *(ulonglong *)(save);
audit_handler_set_option(log_handler, OPT_ROTATE_ON_SIZE, &new_val);
audit_log_rotate_on_size= new_val;
}
static MYSQL_SYSVAR_ULONGLONG(rotate_on_size, audit_log_rotate_on_size,
PLUGIN_VAR_RQCMDARG,
"Maximum size of the log to start the rotation, if FILE handler is used.",
NULL, audit_log_rotate_on_size_update, 0UL, 0UL, ULONGLONG_MAX, 4096UL);
static
void audit_log_rotations_update(
MYSQL_THD thd __attribute__((unused)),
struct st_mysql_sys_var *var __attribute__((unused)),
void *var_ptr __attribute__((unused)),
const void *save)
{
ulonglong new_val= *(ulonglong *)(save);
audit_handler_set_option(log_handler, OPT_ROTATIONS, &new_val);
audit_log_rotations= new_val;
}
static MYSQL_SYSVAR_ULONGLONG(rotations, audit_log_rotations,
PLUGIN_VAR_RQCMDARG,
"Maximum number of rotations to keep, if FILE handler is used.",
NULL, audit_log_rotations_update, 0UL, 0UL, 999UL, 1UL);
static
void audit_log_flush_update(
MYSQL_THD thd __attribute__((unused)),
struct st_mysql_sys_var *var __attribute__((unused)),
void *var_ptr __attribute__((unused)),
const void *save)
{
char new_val= *(const char *)(save);
if (new_val != audit_log_flush && new_val)
{
audit_log_flush= TRUE;
reopen_log_file();
audit_log_flush= FALSE;
}
}
static MYSQL_SYSVAR_BOOL(flush, audit_log_flush,
PLUGIN_VAR_OPCMDARG, "Flush the log file.", NULL,
audit_log_flush_update, 0);
static MYSQL_SYSVAR_STR(syslog_ident, audit_log_syslog_ident,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY | PLUGIN_VAR_MEMALLOC,
"The string that will be prepended to each log message, "
"if SYSLOG handler is used.",
NULL, NULL, default_audit_log_syslog_ident);
static TYPELIB audit_log_syslog_facility_typelib=
{
array_elements(audit_log_syslog_facility_names) - 1,
"audit_log_syslog_facility_typelib",
audit_log_syslog_facility_names, NULL
};
static MYSQL_SYSVAR_ENUM(syslog_facility, audit_log_syslog_facility,
PLUGIN_VAR_RQCMDARG,
"The syslog facility to assign to messages, if SYSLOG handler is used.",
NULL, NULL, 0,
&audit_log_syslog_facility_typelib);
static TYPELIB audit_log_syslog_priority_typelib=
{
array_elements(audit_log_syslog_priority_names) - 1,
"audit_log_syslog_priority_typelib",
audit_log_syslog_priority_names, NULL
};
static MYSQL_SYSVAR_ENUM(syslog_priority, audit_log_syslog_priority,
PLUGIN_VAR_RQCMDARG,
"Priority to be assigned to all messages written to syslog.",
NULL, NULL, 0,
&audit_log_syslog_priority_typelib);
#ifdef AUDIT_HAVE_MONGO
// TODO: Support changing collection
static MYSQL_SYSVAR_STR(mongo_collection, audit_log_mongo_collection,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_MEMALLOC,
"The mongo collection to be used, if MONGODB handler is used.",
NULL, NULL, default_audit_log_mongo_collection);
static MYSQL_SYSVAR_STR(mongo_uri, audit_log_mongo_uri,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_MEMALLOC,
"The URI of the mogo server to be used.",
NULL, NULL, default_audit_log_mongo_uri);
#endif
static struct st_mysql_sys_var* audit_log_system_variables[] =
{
MYSQL_SYSVAR(file),
MYSQL_SYSVAR(policy),
MYSQL_SYSVAR(strategy),
MYSQL_SYSVAR(format),
MYSQL_SYSVAR(buffer_size),
MYSQL_SYSVAR(rotate_on_size),
MYSQL_SYSVAR(rotations),
MYSQL_SYSVAR(flush),
MYSQL_SYSVAR(handler),
MYSQL_SYSVAR(syslog_ident),
MYSQL_SYSVAR(syslog_priority),
MYSQL_SYSVAR(syslog_facility),
#ifdef AUDIT_HAVE_MONGO
MYSQL_SYSVAR(mongo_uri),
MYSQL_SYSVAR(mongo_collection),
#endif
NULL
};
/*
Plugin type-specific descriptor
*/
static struct st_mysql_audit audit_log_descriptor=
{
MYSQL_AUDIT_INTERFACE_VERSION, /* interface version */
NULL, /* release_thd function */
audit_log_notify, /* notify function */
{ MYSQL_AUDIT_GENERAL_CLASSMASK |
MYSQL_AUDIT_CONNECTION_CLASSMASK } /* class mask */
};
/*
Plugin status variables for SHOW STATUS
*/
static struct st_mysql_show_var audit_log_status_variables[]=
{
{ 0, 0, 0}
};
/*
Plugin library descriptor
*/
mysql_declare_plugin(audit_log)
{
MYSQL_AUDIT_PLUGIN, /* type */
&audit_log_descriptor, /* descriptor */
"audit_log", /* name */
"Percona LLC and/or its affiliates.", /* author */
"Audit log", /* description */
PLUGIN_LICENSE_GPL,