File: audit_null.cc

package info (click to toggle)
mysql-8.0 8.0.43-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,273,924 kB
  • sloc: cpp: 4,684,605; ansic: 412,450; pascal: 108,398; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; sh: 24,181; python: 21,816; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,076; makefile: 2,194; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (860 lines) | stat: -rw-r--r-- 29,185 bytes parent folder | download
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
/* Copyright (c) 2010, 2025, Oracle and/or its affiliates.

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

   This program is designed to work with certain software (including
   but not limited to OpenSSL) that is licensed under separate terms,
   as designated in a particular file or component or in included license
   documentation.  The authors of MySQL hereby grant you an additional
   permission to link the program and your derivative works with the
   separately licensed software that they have either included with
   the program or referenced in the documentation.

   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, version 2.0, 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 <mysql/plugin.h>
#include <mysql/plugin_audit.h>
#include <mysqld_error.h>
#include <stdio.h>
#include <sys/types.h>

#include "lex_string.h"
#include "m_ctype.h"
#include "my_compiler.h"
#include "my_inttypes.h"
#include "my_macros.h"
#include "my_sys.h"
#include "mysql/psi/mysql_mutex.h"
#include "thr_mutex.h"

/** Event strings. */
LEX_CSTRING event_names[][6] = {
    /** MYSQL_AUDIT_GENERAL_CLASS */
    {
        {STRING_WITH_LEN("MYSQL_AUDIT_GENERAL_LOG")},
        {STRING_WITH_LEN("MYSQL_AUDIT_GENERAL_ERROR")},
        {STRING_WITH_LEN("MYSQL_AUDIT_GENERAL_RESULT")},
        {STRING_WITH_LEN("MYSQL_AUDIT_GENERAL_STATUS")},
    },
    /** MYSQL_AUDIT_CONNECTION_CLASS */
    {
        {STRING_WITH_LEN("MYSQL_AUDIT_CONNECTION_CONNECT")},
        {STRING_WITH_LEN("MYSQL_AUDIT_CONNECTION_DISCONNECT")},
        {STRING_WITH_LEN("MYSQL_AUDIT_CONNECTION_CHANGE_USER")},
        {STRING_WITH_LEN("MYSQL_AUDIT_CONNECTION_PRE_AUTHENTICATE")},
    },
    /** MYSQL_AUDIT_PARSE_CLASS */
    {
        {STRING_WITH_LEN("MYSQL_AUDIT_PARSE_PREPARSE")},
        {STRING_WITH_LEN("MYSQL_AUDIT_PARSE_POSTPARSE")},
    },
    /** MYSQL_AUDIT_AUTHORIZATION_CLASS */
    {
        {STRING_WITH_LEN("MYSQL_AUDIT_AUTHORIZATION_USER")},
        {STRING_WITH_LEN("MYSQL_AUDIT_AUTHORIZATION_DB")},
        {STRING_WITH_LEN("MYSQL_AUDIT_AUTHORIZATION_TABLE")},
        {STRING_WITH_LEN("MYSQL_AUDIT_AUTHORIZATION_COLUMN")},
        {STRING_WITH_LEN("MYSQL_AUDIT_AUTHORIZATION_PROCEDURE")},
        {STRING_WITH_LEN("MYSQL_AUDIT_AUTHORIZATION_PROXY")},
    },
    /** MYSQL_AUDIT_TABLE_ROW_ACCES_CLASS */
    {
        {STRING_WITH_LEN("MYSQL_AUDIT_TABLE_ACCESS_READ")},
        {STRING_WITH_LEN("MYSQL_AUDIT_TABLE_ACCESS_INSERT")},
        {STRING_WITH_LEN("MYSQL_AUDIT_TABLE_ACCESS_UPDATE")},
        {STRING_WITH_LEN("MYSQL_AUDIT_TABLE_ACCESS_DELETE")},
    },
    /** MYSQL_AUDIT_GLOBAL_VARIABLE_CLASS */
    {
        {STRING_WITH_LEN("MYSQL_AUDIT_GLOBAL_VARIABLE_GET")},
        {STRING_WITH_LEN("MYSQL_AUDIT_GLOBAL_VARIABLE_SET")},
    },
    /** MYSQL_AUDIT_SERVER_STARTUP_CLASS */
    {
        {STRING_WITH_LEN("MYSQL_AUDIT_SERVER_STARTUP_STARTUP")},
    },
    /** MYSQL_AUDIT_SERVER_SHUTDOWN_CLASS */
    {
        {STRING_WITH_LEN("MYSQL_AUDIT_SERVER_SHUTDOWN_SHUTDOWN")},
    },
    /** MYSQL_AUDIT_COMMAND_CLASS */
    {
        {STRING_WITH_LEN("MYSQL_AUDIT_COMMAND_START")},
        {STRING_WITH_LEN("MYSQL_AUDIT_COMMAND_END")},
    },
    /** MYSQL_AUDIT_QUERY_CLASS */
    {
        {STRING_WITH_LEN("MYSQL_AUDIT_QUERY_START")},
        {STRING_WITH_LEN("MYSQL_AUDIT_QUERY_NESTED_START")},
        {STRING_WITH_LEN("MYSQL_AUDIT_QUERY_STATUS_END")},
        {STRING_WITH_LEN("MYSQL_AUDIT_QUERY_NESTED_STATUS_END")},
    },
    /** MYSQL_AUDIT_STORED_PROGRAM_CLASS */
    {
        {STRING_WITH_LEN("MYSQL_AUDIT_STORED_PROGRAM_EXECUTE")},
    },
    /** MYSQL_AUDIT_AUTHENTICATION_CLASS */
    {
        {STRING_WITH_LEN("MYSQL_AUDIT_AUTHENTICATION_FLUSH")},
        {STRING_WITH_LEN("MYSQL_AUDIT_AUTHENTICATION_AUTHID_CREATE")},
        {STRING_WITH_LEN("MYSQL_AUDIT_AUTHENTICATION_CREDENTIAL_CHANGE")},
        {STRING_WITH_LEN("MYSQL_AUDIT_AUTHENTICATION_AUTHID_RENAME")},
        {STRING_WITH_LEN("MYSQL_AUDIT_AUTHENTICATION_AUTHID_DROP")},
    },
    /** MYSQL_AUDIT_MESSAGE_CLASS */
    {
        {STRING_WITH_LEN("MYSQL_AUDIT_MESSAGE_INTERNAL")},
        {STRING_WITH_LEN("MYSQL_AUDIT_MESSAGE_USER")},
    }};

static volatile int number_of_calls;

/*
  Plugin has been installed.
*/
static bool g_plugin_installed = false;

/*
  Record buffer mutex.
*/
static mysql_mutex_t g_record_buffer_mutex;

/*
  Event recording buffer.
*/
static char *g_record_buffer;

#define AUDIT_NULL_VAR(x) static volatile int number_of_calls_##x;
#include "plugin/audit_null/audit_null_variables.h"

#undef AUDIT_NULL_VAR

/*
  Plugin status variables for SHOW STATUS
*/

static SHOW_VAR simple_status[] = {
    {"Audit_null_called",
     const_cast<char *>(reinterpret_cast<volatile char *>(&number_of_calls)),
     SHOW_INT, SHOW_SCOPE_GLOBAL},

#define AUDIT_NULL_VAR(x)                                        \
  {"Audit_null_" #x,                                             \
   const_cast<char *>(                                           \
       reinterpret_cast<volatile char *>(&number_of_calls_##x)), \
   SHOW_INT, SHOW_SCOPE_GLOBAL},
#include "plugin/audit_null/audit_null_variables.h"

#undef AUDIT_NULL_VAR

    {nullptr, nullptr, SHOW_UNDEF, SHOW_SCOPE_GLOBAL}};

/*
  Define plugin variables.
*/

static MYSQL_THDVAR_STR(abort_message,
                        PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_MEMALLOC,
                        "Custom message for event abort.", nullptr, nullptr,
                        nullptr);

static MYSQL_THDVAR_INT(abort_value, PLUGIN_VAR_RQCMDARG, "Event abort value.",
                        nullptr, nullptr, 1, -1, 150, 0);

static MYSQL_THDVAR_STR(event_order_check,
                        PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_MEMALLOC,
                        "Event order check string", nullptr, nullptr, nullptr);

static MYSQL_THDVAR_UINT(event_order_check_consume_ignore_count,
                         PLUGIN_VAR_RQCMDARG,
                         "Do not consume event order string specified "
                         "number of times.",
                         nullptr, nullptr, 0, 0, UINT_MAX, 1);

static MYSQL_THDVAR_INT(event_order_started, PLUGIN_VAR_RQCMDARG,
                        "Plugin is in the event order check.", nullptr, nullptr,
                        0, 0, 1, 0);

static MYSQL_THDVAR_INT(event_order_check_exact, PLUGIN_VAR_RQCMDARG,
                        "Plugin checks exact event order.", nullptr, nullptr, 1,
                        0, 1, 0);

static MYSQL_THDVAR_STR(event_record_def,
                        PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_MEMALLOC,
                        "Event recording definition", nullptr, nullptr,
                        nullptr);

static MYSQL_THDVAR_STR(event_record,
                        PLUGIN_VAR_READONLY | PLUGIN_VAR_RQCMDARG |
                            PLUGIN_VAR_MEMALLOC,
                        "Event recording", nullptr, nullptr, nullptr);
/*
  Initialize the plugin at server start or plugin installation.

  SYNOPSIS
    audit_null_plugin_init()

  DESCRIPTION
    Does nothing.

  RETURN VALUE
    0                    success
    1                    failure (cannot happen)
*/

static int audit_null_plugin_init(void *arg [[maybe_unused]]) {
  SHOW_VAR *var;

  for (var = simple_status; var->value != nullptr; var++) {
    *((int *)var->value) = 0;
  }

  mysql_mutex_init(PSI_NOT_INSTRUMENTED, &g_record_buffer_mutex,
                   MY_MUTEX_INIT_FAST);

  g_record_buffer = nullptr;
  g_plugin_installed = true;

  return (0);
}

/*
  Terminate the plugin at server shutdown or plugin deinstallation.

  SYNOPSIS
    audit_null_plugin_deinit()
    Does nothing.

  RETURN VALUE
    0                    success
    1                    failure (cannot happen)

*/

static int audit_null_plugin_deinit(void *arg [[maybe_unused]]) {
  assert(arg);
  if (g_plugin_installed == true) {
    my_free((void *)(g_record_buffer));

    g_record_buffer = nullptr;

    mysql_mutex_destroy(&g_record_buffer_mutex);

    g_plugin_installed = false;
  }

  return (0);
}

/**
  @brief Converts event_class and event_subclass into a string.

  @param [in] event_class    Event class value.
  @param [in] event_subclass Event subclass value.

  @retval Event name.
*/
static LEX_CSTRING event_to_str(unsigned int event_class,
                                unsigned long event_subclass) {
  int count;
  for (count = 0; event_subclass; count++, event_subclass >>= 1)
    ;

  return event_names[event_class][count - 1];
}

/**
  @brief Read token delimited by a semicolon from a string.

  @param [in,out] str Pointer to a string containing text.
                     Pointer is moved to a new token after the function ends.

  @retval Token retrieved from a string.
*/
static LEX_CSTRING get_token(char **str) {
  LEX_CSTRING ret = {nullptr, 0};

  if (*str != nullptr) {
    ret.str = *str;

    if (*ret.str != '\0') {
      /* Find a param delimiter. */
      while (**str && **str != ';') (*str)++;

      ret.length = *str - ret.str;

      /* Skip a delimiter. */
      if (**str == ';') (*str)++;
    }
  }

  return ret;
}

static char *add_event(const char *var, LEX_CSTRING event, const char *data,
                       size_t data_length) {
  LEX_CSTRING str;
  size_t size;
  char *buffer;

  lex_cstring_set(&str, var);

  size = str.length + event.length + data_length + 4;
  buffer = (char *)my_malloc(PSI_NOT_INSTRUMENTED, size, MYF(MY_FAE));

  snprintf(buffer, size, "%s%s%s;%s;", var, str.length == 0 ? "" : "\n",
           event.str, data);

  buffer[size - (str.length == 0 ? 2 : 1)] = '\0';

  return buffer;
}

static void process_event_record(MYSQL_THD thd, LEX_CSTRING event_name,
                                 const char *data, size_t data_length) {
  char *record_str = THDVAR(thd, event_record_def);
  LEX_CSTRING record_begin = get_token(&record_str);
  LEX_CSTRING record_end = get_token(&record_str);

  if (record_str == nullptr) {
    return;
  }

  if (record_end.length == 0) {
    /* We are already in the consuming phase. Add a new event name into
       a record variable */

    const char *buffer = THDVAR(thd, event_record);
    char *new_buffer = nullptr;

    /* Add event. */
    mysql_mutex_lock(&g_record_buffer_mutex);

    /* Only one THD is capable of adding events into the buffer. */
    if (buffer && (buffer == g_record_buffer)) {
      new_buffer = add_event(buffer, event_name, data, data_length);
      g_record_buffer = new_buffer;
      my_free(const_cast<char *>(buffer));
    }

    mysql_mutex_unlock(&g_record_buffer_mutex);

    THDVAR(thd, event_record) = new_buffer;

    if (!my_charset_latin1.coll->strnncoll(
            &my_charset_latin1, (const uchar *)record_begin.str,
            record_begin.length, (const uchar *)event_name.str,
            event_name.length, false)) {
      /* Do not expect any more events. */
      THDVAR(thd, event_record_def) = nullptr;
    }
  } else {
    const char *buffer;

    /* We have not started recording of events yet. */
    if (my_charset_latin1.coll->strnncoll(
            &my_charset_latin1, (const uchar *)record_begin.str,
            record_begin.length, (const uchar *)event_name.str,
            event_name.length, false)) {
      /* Event not matching. */
      return;
    }

    buffer = THDVAR(thd, event_record);

    mysql_mutex_lock(&g_record_buffer_mutex);

    if (buffer == g_record_buffer) {
      my_free(const_cast<char *>(buffer));

      g_record_buffer = add_event("", event_name, data, data_length);

      THDVAR(thd, event_record) = g_record_buffer;
    }

    mysql_mutex_unlock(&g_record_buffer_mutex);

    /* Add event. */

    record_str = THDVAR(thd, event_record_def);

    /* Remove starting event. */
    memmove(record_str, record_end.str, record_end.length + 1);
  }
}

static int process_command(MYSQL_THD thd, LEX_CSTRING event_command,
                           bool consume_event) {
  LEX_CSTRING abort_ret_command = {STRING_WITH_LEN("ABORT_RET")};

  if (!my_charset_latin1.coll->strnncoll(
          &my_charset_latin1, (const uchar *)event_command.str,
          event_command.length, (const uchar *)abort_ret_command.str,
          abort_ret_command.length, false)) {
    int ret_code = (int)THDVAR(thd, abort_value);
    const char *err_message = (const char *)THDVAR(thd, abort_message);
    LEX_CSTRING status = {STRING_WITH_LEN("EVENT-ORDER-ABORT")};

    char *order_str = THDVAR(thd, event_order_check);

    /* Do not replace order string yet. */
    if (consume_event) {
      memmove(order_str, status.str, status.length + 1);

      THDVAR(thd, abort_value) = 1;
      THDVAR(thd, abort_message) = nullptr;
    }

    if (err_message) {
      my_message(ER_AUDIT_API_ABORT, err_message, MYF(0));
      THDVAR(thd, event_order_check) = order_str;
    }

    return ret_code;
  }

  return 0;
}

/**
  @brief Plugin function handler.

  @param [in] thd         Connection context.
  @param [in] event_class Event class value.
  @param [in] event       Event data.

  @retval Value indicating, whether the server should abort continuation
          of the current operation.
*/
static int audit_null_notify(MYSQL_THD thd, mysql_event_class_t event_class,
                             const void *event) {
  char buffer[2000] = {
      0,
  };
  int buffer_data = 0;
  unsigned long event_subclass =
      static_cast<unsigned long>(*static_cast<const int *>(event));
  char *order_str = THDVAR(thd, event_order_check);
  int event_order_started = (int)THDVAR(thd, event_order_started);
  int exact_check = (int)THDVAR(thd, event_order_check_exact);
  LEX_CSTRING event_name = event_to_str(event_class, event_subclass);
  LEX_CSTRING event_token = get_token(&order_str);
  LEX_CSTRING event_data = get_token(&order_str);
  LEX_CSTRING event_command = get_token(&order_str);
  bool consume_event = true;

  /* prone to races, oh well */
  number_of_calls++;

  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_LOG:
        number_of_calls_general_log++;
        break;
      case MYSQL_AUDIT_GENERAL_ERROR:
        number_of_calls_general_error++;
        break;
      case MYSQL_AUDIT_GENERAL_RESULT:
        number_of_calls_general_result++;
        break;
      case MYSQL_AUDIT_GENERAL_STATUS:
        number_of_calls_general_status++;
        break;
      default:
        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:
        number_of_calls_connection_connect++;
        break;
      case MYSQL_AUDIT_CONNECTION_DISCONNECT:
        number_of_calls_connection_disconnect++;
        break;
      case MYSQL_AUDIT_CONNECTION_CHANGE_USER:
        number_of_calls_connection_change_user++;
        break;
      case MYSQL_AUDIT_CONNECTION_PRE_AUTHENTICATE:
        number_of_calls_connection_pre_authenticate++;
        break;
      default:
        break;
    }
  } else if (event_class == MYSQL_AUDIT_PARSE_CLASS) {
    const struct mysql_event_parse *event_parse =
        (const struct mysql_event_parse *)event;

    switch (event_parse->event_subclass) {
      case MYSQL_AUDIT_PARSE_PREPARSE:
        number_of_calls_parse_preparse++;
        break;
      case MYSQL_AUDIT_PARSE_POSTPARSE:
        number_of_calls_parse_postparse++;
        break;
      default:
        break;
    }
  }
#if 0
  /**
    Currently events not active.

  else if (event_class == MYSQL_AUDIT_AUTHORIZATION_CLASS)
  {
    const struct mysql_event_authorization *event_grant =
                             (const struct mysql_event_authorization *)event;

    buffer_data= sprintf(buffer, "db=\"%s\" table=\"%s\" object=\"%s\" "
                         "requested=\"0x%08x\" granted=\"0x%08x\"",
                         event_grant->database.str ? event_grant->database.str : "<NULL>",
                         event_grant->table.str ? event_grant->table.str : "<NULL>",
                         event_grant->object.str ? event_grant->object.str : "<NULL>",
                         event_grant->requested_privilege,
                         event_grant->granted_privilege);

    switch (event_grant->event_subclass)
    {
    case MYSQL_AUDIT_AUTHORIZATION_USER:
      number_of_calls_authorization_user++;
      break;
    case MYSQL_AUDIT_AUTHORIZATION_DB:
      number_of_calls_authorization_db++;
      break;
    case MYSQL_AUDIT_AUTHORIZATION_TABLE:
      number_of_calls_authorization_table++;
      break;
    case MYSQL_AUDIT_AUTHORIZATION_COLUMN:
      number_of_calls_authorization_column++;
      break;
    case MYSQL_AUDIT_AUTHORIZATION_PROCEDURE:
      number_of_calls_authorization_procedure++;
      break;
    case MYSQL_AUDIT_AUTHORIZATION_PROXY:
      number_of_calls_authorization_proxy++;
      break;
    default:
      break;
    }
  }
  */
#endif
  else if (event_class == MYSQL_AUDIT_SERVER_STARTUP_CLASS) {
    /* const struct mysql_event_server_startup *event_startup=
       (const struct mysql_event_server_startup *) event; */
    number_of_calls_server_startup++;
  } else if (event_class == MYSQL_AUDIT_SERVER_SHUTDOWN_CLASS) {
    /* const struct mysql_event_server_shutdown *event_startup=
       (const struct mysql_event_server_shutdown *) event; */
    number_of_calls_server_shutdown++;
  } else if (event_class == MYSQL_AUDIT_COMMAND_CLASS) {
    const struct mysql_event_command *local_event_command =
        (const struct mysql_event_command *)event;

    buffer_data =
        sprintf(buffer, "command_id=\"%d\"", local_event_command->command_id);

    switch (local_event_command->event_subclass) {
      case MYSQL_AUDIT_COMMAND_START:
        number_of_calls_command_start++;
        break;
      case MYSQL_AUDIT_COMMAND_END:
        number_of_calls_command_end++;
        break;
      default:
        break;
    }
  } else if (event_class == MYSQL_AUDIT_QUERY_CLASS) {
    const struct mysql_event_query *event_query =
        (const struct mysql_event_query *)event;

    buffer_data = sprintf(buffer, "sql_command_id=\"%d\"",
                          (int)event_query->sql_command_id);

    switch (event_query->event_subclass) {
      case MYSQL_AUDIT_QUERY_START:
        number_of_calls_query_start++;
        break;
      case MYSQL_AUDIT_QUERY_NESTED_START:
        number_of_calls_query_nested_start++;
        break;
      case MYSQL_AUDIT_QUERY_STATUS_END:
        number_of_calls_query_status_end++;
        break;
      case MYSQL_AUDIT_QUERY_NESTED_STATUS_END:
        number_of_calls_query_nested_status_end++;
        break;
      default:
        break;
    }
  } else if (event_class == MYSQL_AUDIT_TABLE_ACCESS_CLASS) {
    const struct mysql_event_table_access *event_table =
        (const struct mysql_event_table_access *)event;

    buffer_data =
        sprintf(buffer, "db=\"%s\" table=\"%s\"",
                event_table->table_database.str, event_table->table_name.str);

    switch (event_table->event_subclass) {
      case MYSQL_AUDIT_TABLE_ACCESS_INSERT:
        number_of_calls_table_access_insert++;
        break;
      case MYSQL_AUDIT_TABLE_ACCESS_DELETE:
        number_of_calls_table_access_delete++;
        break;
      case MYSQL_AUDIT_TABLE_ACCESS_UPDATE:
        number_of_calls_table_access_update++;
        break;
      case MYSQL_AUDIT_TABLE_ACCESS_READ:
        number_of_calls_table_access_read++;
        break;
      default:
        break;
    }
  } else if (event_class == MYSQL_AUDIT_GLOBAL_VARIABLE_CLASS) {
    const struct mysql_event_global_variable *event_gvar =
        (const struct mysql_event_global_variable *)event;

    /* Copy the variable content into the buffer. We do not guarantee that the
       variable value will fit into buffer. The buffer should be large enough
       to be used for the test purposes. */
    buffer_data =
        sprintf(buffer, "name=\"%.*s\"",
                static_cast<int>(std::min(event_gvar->variable_name.length,
                                          (sizeof(buffer) - 8))),
                event_gvar->variable_name.str);

    buffer_data +=
        sprintf(buffer + buffer_data, " value=\"%.*s\"",
                static_cast<int>(std::min(event_gvar->variable_value.length,
                                          (sizeof(buffer) - 16))),
                event_gvar->variable_value.str);
    buffer[buffer_data] = '\0';

    switch (event_gvar->event_subclass) {
      case MYSQL_AUDIT_GLOBAL_VARIABLE_GET:
        number_of_calls_global_variable_get++;
        break;
      case MYSQL_AUDIT_GLOBAL_VARIABLE_SET:
        number_of_calls_global_variable_set++;
        break;
      default:
        break;
    }
  } else if (event_class == MYSQL_AUDIT_MESSAGE_CLASS) {
    const struct mysql_event_message *evt =
        reinterpret_cast<const struct mysql_event_message *>(event);

    buffer_data +=
        snprintf(buffer, sizeof(buffer) - 1,
                 "component=\"%.*s\" producer=\"%.*s\" message=\"%.*s\"",
                 static_cast<int>(evt->component.length), evt->component.str,
                 static_cast<int>(evt->producer.length), evt->producer.str,
                 static_cast<int>(evt->message.length), evt->message.str);

    for (size_t i = 0; i < evt->key_value_map_length; ++i) {
      if (evt->key_value_map[i].value_type ==
              MYSQL_AUDIT_MESSAGE_VALUE_TYPE_STR &&
          evt->key_value_map[i].value.str.str == nullptr)
        buffer_data +=
            snprintf(buffer + buffer_data, sizeof(buffer) - buffer_data - 1,
                     " key[%zu]=\"%.*s\" value[%zu]=null", i,
                     static_cast<int>(evt->key_value_map[i].key.length),
                     evt->key_value_map[i].key.str, i);
      else if (evt->key_value_map[i].value_type ==
               MYSQL_AUDIT_MESSAGE_VALUE_TYPE_STR)
        buffer_data +=
            snprintf(buffer + buffer_data, sizeof(buffer) - buffer_data - 1,
                     " key[%zu]=\"%.*s\" value[%zu]=\"%.*s\"", i,
                     static_cast<int>(evt->key_value_map[i].key.length),
                     evt->key_value_map[i].key.str, i,
                     static_cast<int>(evt->key_value_map[i].value.str.length),
                     evt->key_value_map[i].value.str.str);
      else if (evt->key_value_map[i].value_type ==
               MYSQL_AUDIT_MESSAGE_VALUE_TYPE_NUM)
        buffer_data += snprintf(
            buffer + buffer_data, sizeof(buffer) - buffer_data - 1,
            " key[%zu]=\"%.*s\" value[%zu]=%lld", i,
            static_cast<int>(evt->key_value_map[i].key.length),
            evt->key_value_map[i].key.str, i, evt->key_value_map[i].value.num);
    }

    buffer[buffer_data] = '\0';

    switch (evt->event_subclass) {
      case MYSQL_AUDIT_MESSAGE_INTERNAL:
        number_of_calls_message_internal++;
        break;
      case MYSQL_AUDIT_MESSAGE_USER:
        number_of_calls_message_user++;
        break;
      default:
        break;
    }
  }

  process_event_record(thd, event_name, buffer, buffer_data);

  if (my_charset_latin1.coll->strnncoll(
          &my_charset_latin1, (const uchar *)event_name.str, event_name.length,
          (const uchar *)event_token.str, event_token.length, false)) {
    /* Clear event command. */
    event_command.str = nullptr;
    event_command.length = 0;

    if (exact_check == 1 && event_order_started == 1) {
      if (!(event_class == MYSQL_AUDIT_GENERAL_CLASS &&
            event_subclass == MYSQL_AUDIT_GENERAL_ERROR)) {
        strxnmov(buffer, sizeof(buffer), event_name.str, " instead of ",
                 event_token.str, NullS);
        my_message(ER_AUDIT_API_ABORT, buffer, MYF(0));
      }

      THDVAR(thd, event_order_started) = 0;
      THDVAR(thd, event_order_check) = nullptr;

      return 1;
    }
  } else {
    LEX_CSTRING ignore = {STRING_WITH_LEN("<IGNORE>")};

    /* When we are not in the event order check, check if the specified
       data corresponds to the actual event data. */
    if (my_charset_latin1.coll->strnncoll(
            &my_charset_latin1, (const uchar *)event_data.str,
            event_data.length, (const uchar *)ignore.str, ignore.length,
            false) &&
        my_charset_latin1.coll->strnncoll(
            &my_charset_latin1, (const uchar *)event_data.str,
            event_data.length, (const uchar *)buffer, (size_t)buffer_data,
            false)) {
      if (exact_check == 1 && event_order_started == 1) {
        char invalid_data_buffer[sizeof(buffer)] = {
            0,
        };
        LEX_CSTRING status = {STRING_WITH_LEN("EVENT-ORDER-INVALID-DATA")};

        char *local_order_str = THDVAR(thd, event_order_check);

        memmove(local_order_str, status.str, status.length + 1);

        strxnmov(invalid_data_buffer, sizeof(invalid_data_buffer),
                 "Invalid data for '", event_name.str, "' -> ", buffer, NullS);
        my_message(ER_AUDIT_API_ABORT, invalid_data_buffer, MYF(0));

        THDVAR(thd, event_order_started) = 0;
        THDVAR(thd, event_order_check) = local_order_str;

        return 1;
      }

      /* Clear event command. */
      event_command.str = nullptr;
      event_command.length = 0;
    } else {
      LEX_STRING order_cstr;
      ulong consume = THDVAR(thd, event_order_check_consume_ignore_count);
      lex_string_set(&order_cstr, THDVAR(thd, event_order_check));

      THDVAR(thd, event_order_started) = 1;

      if (consume) {
        /*
          Do not consume event this time. Just decrease value and wait until
          the next event is matched.
        */
        THDVAR(thd, event_order_check_consume_ignore_count) = consume - 1;
        consume_event = false;
      } else {
        /* Consume matched event. */
        memmove(order_cstr.str, order_str,
                order_cstr.length - (order_str - order_cstr.str) + 1);

        /* Count new length. */
        lex_string_set(&order_cstr, order_cstr.str);

        if (order_cstr.length == 0) {
          LEX_CSTRING status = {STRING_WITH_LEN("EVENT-ORDER-OK")};

          memmove(order_cstr.str, status.str, status.length + 1);

          /* event_order_started contains message. Do not verify it. */
          THDVAR(thd, event_order_started) = 0;
        }
      }
    }
  }

  return process_command(thd, event_command, consume_event);
}

/*
  Plugin type-specific descriptor
*/

static struct st_mysql_audit audit_null_descriptor = {
    MYSQL_AUDIT_INTERFACE_VERSION, /* interface version    */
    nullptr,                       /* release_thd function */
    audit_null_notify,             /* notify function      */
    {(unsigned long)MYSQL_AUDIT_GENERAL_ALL,
     (unsigned long)MYSQL_AUDIT_CONNECTION_ALL,
     (unsigned long)MYSQL_AUDIT_PARSE_ALL,
     0, /* This event class is currently not supported. */
     (unsigned long)MYSQL_AUDIT_TABLE_ACCESS_ALL,
     (unsigned long)MYSQL_AUDIT_GLOBAL_VARIABLE_ALL,
     (unsigned long)MYSQL_AUDIT_SERVER_STARTUP_ALL,
     (unsigned long)MYSQL_AUDIT_SERVER_SHUTDOWN_ALL,
     (unsigned long)MYSQL_AUDIT_COMMAND_ALL,
     (unsigned long)MYSQL_AUDIT_QUERY_ALL,
     (unsigned long)MYSQL_AUDIT_STORED_PROGRAM_ALL,
     (unsigned long)MYSQL_AUDIT_AUTHENTICATION_ALL,
     (unsigned long)MYSQL_AUDIT_MESSAGE_ALL}};

static SYS_VAR *system_variables[] = {

    MYSQL_SYSVAR(abort_message),
    MYSQL_SYSVAR(abort_value),

    MYSQL_SYSVAR(event_order_check),
    MYSQL_SYSVAR(event_order_check_consume_ignore_count),
    MYSQL_SYSVAR(event_order_started),
    MYSQL_SYSVAR(event_order_check_exact),

    MYSQL_SYSVAR(event_record_def),
    MYSQL_SYSVAR(event_record),
    nullptr};

/*
  Plugin library descriptor
*/

mysql_declare_plugin(audit_null){
    MYSQL_AUDIT_PLUGIN,     /* type                            */
    &audit_null_descriptor, /* descriptor                      */
    "NULL_AUDIT",           /* name                            */
    PLUGIN_AUTHOR_ORACLE,   /* author                          */
    "Simple NULL Audit",    /* description                     */
    PLUGIN_LICENSE_GPL,
    audit_null_plugin_init,   /* init function (when loaded)     */
    nullptr,                  /* check uninstall function        */
    audit_null_plugin_deinit, /* deinit function (when unloaded) */
    0x0003,                   /* version                         */
    simple_status,            /* status variables                */
    system_variables,         /* system variables                */
    nullptr,
    0,
} mysql_declare_plugin_end;