-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaudit_tripwire.cc
165 lines (141 loc) · 6.22 KB
/
audit_tripwire.cc
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
#include <stdio.h>
#include <string.h>
#include <mysql/plugin.h>
#include <mysql/plugin_audit.h>
#include <mysql/service_my_plugin_log.h>
#include <mysql/service_security_context.h>
static char *audit_tripwire_table_value= NULL;
static char *audit_tripwire_db_value= NULL;
static my_bool panic_mode_value= FALSE;
static MYSQL_PLUGIN plugin= NULL;
static bool is_super(MYSQL_THD thd)
{
MYSQL_SECURITY_CONTEXT ctx;
my_svc_bool is_super= FALSE;
/* setting panic mode requires super */
return (
thd != NULL &&
!thd_get_security_context(thd, &ctx) &&
!security_context_get_option(ctx, "privilege_super", &is_super) &&
is_super);
}
static int
audit_tripwire_notify(MYSQL_THD thd,
mysql_event_class_t event_class,
const void *event)
{
/* if we're in panic mode stop all commands from non-supers */
if (panic_mode_value && !is_super(thd))
return TRUE;
/* Check if the table (if specified) is accessed */
if (event_class == MYSQL_AUDIT_TABLE_ACCESS_CLASS &&
(audit_tripwire_table_value || audit_tripwire_db_value))
{
const struct mysql_event_table_access *table_access=
(const struct mysql_event_table_access *)event;
if (!is_super(thd))
{
/* check for a matching table name */
if (audit_tripwire_table_value &&
strncmp(table_access->table_name.str,
audit_tripwire_table_value,
table_access->table_name.length))
return FALSE;
/* check for a matching database name */
if (audit_tripwire_db_value &&
strncmp(table_access->table_database.str,
audit_tripwire_db_value,
table_access->table_database.length))
return FALSE;
/* table is accessed. Time to panic ! */
my_plugin_log_message(&plugin, MY_WARNING_LEVEL,
"Tripwire table `%s`.`%s` accessed from "
"connection id %d. Switching to panic mode",
audit_tripwire_db_value ?
audit_tripwire_db_value : "*",
audit_tripwire_table_value ?
audit_tripwire_table_value : "*",
table_access->connection_id
);
panic_mode_value= TRUE;
return TRUE;
}
}
return FALSE;
}
static struct st_mysql_audit audit_tripwire_descriptor=
{
MYSQL_AUDIT_INTERFACE_VERSION, /* interface version */
NULL, /* release_thd function */
audit_tripwire_notify, /* notify function */
{
0, /* general */
0, /* connection */
0, /* parse */
0, /* authorization */
MYSQL_AUDIT_TABLE_ACCESS_ALL, /* table access */
0, /* global variables */
0, /* server startup */
0, /* server shutdown */
MYSQL_AUDIT_COMMAND_START, /* command */
0, /* query */
0 /* stored program */
}
};
/* plumbing */
static MYSQL_SYSVAR_STR(
table, /* name */
audit_tripwire_table_value, /* value */
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_MEMALLOC, /* flags */
"Table to watch for.", /* comment */
NULL, /* check() */
NULL, /* update() */
NULL /* default */
);
static MYSQL_SYSVAR_STR(
db, /* name */
audit_tripwire_db_value, /* value */
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_MEMALLOC, /* flags */
"DB of the table to watch for", /* comment */
NULL, /* check() */
NULL, /* update() */
NULL /* default */
);
static MYSQL_SYSVAR_BOOL(
panic_mode, /* name */
panic_mode_value, /* value */
PLUGIN_VAR_RQCMDARG, /* flags */
"Table to watch for", /* comment */
NULL, /* check() */
NULL, /* update() */
FALSE /* default */
);
static struct st_mysql_sys_var* system_variables[] = {
MYSQL_SYSVAR(db),
MYSQL_SYSVAR(table),
MYSQL_SYSVAR(panic_mode),
NULL
};
static int audit_tripwire_init(MYSQL_PLUGIN p)
{
plugin= p;
return 0;
}
/** Plugin declaration */
mysql_declare_plugin(audit_tripwire)
{
MYSQL_AUDIT_PLUGIN, /* type */
&audit_tripwire_descriptor, /* descriptor */
"audit_tripwire", /* name */
"Oracle Corp", /* author */
"Tripwire service", /* description */
PLUGIN_LICENSE_GPL,
audit_tripwire_init, /* init function (when loaded) */
NULL, /* deinit function (when unloaded) */
0x0001, /* version */
NULL, /* status variables */
system_variables, /* system variables */
NULL,
0,
}
mysql_declare_plugin_end;