forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirewall_stored_procedures.sql
185 lines (174 loc) · 6.87 KB
/
firewall_stored_procedures.sql
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
-- Copyright (c) 2020, 2023, 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 also distributed 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 included with MySQL.
--
-- 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
--
-- Remove existing Firewall stored procedures
--
DROP PROCEDURE IF EXISTS sp_set_firewall_mode;
DROP PROCEDURE IF EXISTS sp_reload_firewall_rules;
DROP PROCEDURE IF EXISTS sp_set_firewall_group_mode;
DROP PROCEDURE IF EXISTS sp_set_firewall_group_mode_and_user;
DROP PROCEDURE IF EXISTS sp_reload_firewall_group_rules;
DROP PROCEDURE IF EXISTS sp_firewall_group_enlist;
DROP PROCEDURE IF EXISTS sp_firewall_group_delist;
--
-- Create new variants of Firewall stored procedures
--
DELIMITER $$
CREATE DEFINER='mysql.sys'@'localhost'
PROCEDURE sp_set_firewall_mode (
IN arg_userhost VARCHAR(288),
IN arg_mode VARCHAR(12))
SQL SECURITY INVOKER
BEGIN
DECLARE result VARCHAR(160);
IF arg_mode = "RECORDING" THEN
SELECT read_firewall_whitelist(arg_userhost,FW.rule) FROM mysql.firewall_whitelist FW WHERE userhost = arg_userhost;
END IF;
SELECT set_firewall_mode(arg_userhost, arg_mode) INTO result;
IF arg_mode = "RESET" THEN
SET arg_mode = "OFF";
END IF;
IF result = "OK" THEN
INSERT IGNORE INTO mysql.firewall_users VALUES (arg_userhost, arg_mode);
UPDATE mysql.firewall_users SET mode=arg_mode WHERE userhost = arg_userhost;
ELSE
SELECT result;
END IF;
IF arg_mode = "PROTECTING" OR arg_mode = "OFF" OR arg_mode = "DETECTING" THEN
DELETE FROM mysql.firewall_whitelist WHERE USERHOST = arg_userhost;
INSERT INTO mysql.firewall_whitelist(USERHOST, RULE) SELECT USERHOST,RULE FROM INFORMATION_SCHEMA.mysql_firewall_whitelist WHERE USERHOST=arg_userhost;
END IF;
END$$
CREATE DEFINER='mysql.sys'@'localhost'
PROCEDURE sp_reload_firewall_rules (
IN arg_userhost VARCHAR(288))
SQL SECURITY INVOKER
BEGIN
DECLARE result VARCHAR(160);
SELECT set_firewall_mode(arg_userhost, "RESET") INTO result;
IF result = "OK" THEN
INSERT IGNORE INTO mysql.firewall_users VALUES (arg_userhost, "OFF");
UPDATE mysql.firewall_users SET mode="OFF" WHERE userhost = arg_userhost;
SELECT read_firewall_whitelist(arg_userhost,FW.rule) FROM mysql.firewall_whitelist FW WHERE FW.userhost=arg_userhost;
ELSE
SELECT result;
END IF;
END$$
CREATE DEFINER='mysql.sys'@'localhost'
PROCEDURE sp_set_firewall_group_mode (
IN arg_group_name VARCHAR(288),
IN arg_mode VARCHAR(12))
SQL SECURITY INVOKER
BEGIN
DECLARE result VARCHAR(160);
IF arg_mode = "RECORDING" THEN
SELECT read_firewall_group_allowlist(arg_group_name,FW.rule) FROM mysql.firewall_group_allowlist FW WHERE name = arg_group_name;
END IF;
SELECT set_firewall_group_mode(arg_group_name, arg_mode) INTO result;
IF arg_mode = "RESET" THEN
SET arg_mode = "OFF";
END IF;
IF result = "OK" THEN
INSERT IGNORE INTO mysql.firewall_groups VALUES (arg_group_name, arg_mode, NULL);
UPDATE mysql.firewall_groups SET mode=arg_mode WHERE name = arg_group_name;
ELSE
SELECT result;
END IF;
IF arg_mode = "PROTECTING" OR arg_mode = "OFF" OR arg_mode = "DETECTING" THEN
DELETE FROM mysql.firewall_group_allowlist WHERE name = arg_group_name;
INSERT INTO mysql.firewall_group_allowlist(name, rule)
SELECT name, rule FROM performance_schema.firewall_group_allowlist
WHERE name=arg_group_name;
END IF;
END$$
CREATE DEFINER='mysql.sys'@'localhost'
PROCEDURE sp_set_firewall_group_mode_and_user(
IN arg_group_name VARCHAR(288),
IN arg_mode VARCHAR(12),
IN arg_userhost VARCHAR(288))
SQL SECURITY INVOKER
BEGIN
DECLARE result VARCHAR(160);
IF arg_mode = "RECORDING" THEN
SELECT read_firewall_group_allowlist(arg_group_name,FW.rule) FROM mysql.firewall_group_allowlist FW WHERE name = arg_group_name;
END IF;
SELECT set_firewall_group_mode(arg_group_name, arg_mode, arg_userhost) INTO result;
IF arg_mode = "RESET" THEN
SET arg_mode = "OFF";
END IF;
IF result = "OK" THEN
INSERT IGNORE INTO mysql.firewall_groups VALUES (arg_group_name, arg_mode, arg_userhost);
UPDATE mysql.firewall_groups SET mode=arg_mode, userhost=arg_userhost WHERE name = arg_group_name;
ELSE
SELECT result;
END IF;
IF arg_mode = "PROTECTING" OR arg_mode = "OFF" OR arg_mode = "DETECTING" THEN
DELETE FROM mysql.firewall_group_allowlist WHERE name = arg_group_name;
INSERT INTO mysql.firewall_group_allowlist(name, rule)
SELECT name, rule FROM performance_schema.firewall_group_allowlist
WHERE name=arg_group_name;
END IF;
END$$
CREATE DEFINER='mysql.sys'@'localhost'
PROCEDURE sp_reload_firewall_group_rules(
IN arg_group_name VARCHAR(288))
SQL SECURITY INVOKER
BEGIN
DECLARE result VARCHAR(160);
SELECT set_firewall_group_mode(arg_group_name, "RESET") INTO result;
IF result = "OK" THEN
INSERT IGNORE INTO mysql.firewall_groups VALUES (arg_group_name, "OFF", NULL);
UPDATE mysql.firewall_groups SET mode="OFF" WHERE name = arg_group_name;
SELECT read_firewall_group_allowlist(arg_group_name,FW.rule) FROM mysql.firewall_group_allowlist FW WHERE FW.name=arg_group_name;
ELSE
SELECT result;
END IF;
END$$
CREATE DEFINER='mysql.sys'@'localhost'
PROCEDURE sp_firewall_group_enlist(
IN arg_group_name VARCHAR(288),
IN arg_userhost VARCHAR(288))
SQL SECURITY INVOKER
BEGIN
DECLARE result VARCHAR(160);
SELECT firewall_group_enlist(arg_group_name, arg_userhost) INTO result;
IF result = "OK" THEN
INSERT IGNORE INTO mysql.firewall_membership VALUES (arg_group_name, arg_userhost);
ELSE
SELECT result;
END IF;
END$$
CREATE DEFINER='mysql.sys'@'localhost'
PROCEDURE sp_firewall_group_delist(
IN arg_group_name VARCHAR(288),
IN arg_userhost VARCHAR(288))
SQL SECURITY INVOKER
BEGIN
DECLARE result VARCHAR(160);
SELECT firewall_group_delist(arg_group_name, arg_userhost) INTO result;
IF result = "OK" THEN
DELETE IGNORE FROM mysql.firewall_membership WHERE group_id = arg_group_name AND member_id = arg_userhost;
ELSE
SELECT result;
END IF;
END$$
delimiter ;