Skip to content

Commit 4386d93

Browse files
committed
MDEV-18297 How to reset a forgotten root password
After FLUSH PRIVILEGES remember if the connection started under --skip-grant-tables and keep it all-powerful, not a lowly anonymous. One could use this connection to reset passwords as needed. Also fix a crash in SHOW CREATE USER
1 parent a94b20a commit 4386d93

File tree

5 files changed

+62
-40
lines changed

5 files changed

+62
-40
lines changed

mysql-test/main/skip_grants.result

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
11
use test;
2-
DROP VIEW IF EXISTS v1;
3-
DROP VIEW IF EXISTS v2;
4-
DROP VIEW IF EXISTS v3;
5-
DROP TABLE IF EXISTS t1;
6-
DROP PROCEDURE IF EXISTS p1;
7-
DROP PROCEDURE IF EXISTS p2;
8-
DROP PROCEDURE IF EXISTS p3;
9-
DROP FUNCTION IF EXISTS f1;
10-
DROP FUNCTION IF EXISTS f2;
11-
DROP FUNCTION IF EXISTS f3;
122
CREATE TABLE t1(c INT);
133
CREATE TRIGGER t1_bi BEFORE INSERT ON t1
144
FOR EACH ROW
@@ -95,3 +85,29 @@ Acl_role_grants 0
9585
Acl_roles 0
9686
Acl_table_grants 0
9787
Acl_users 0
88+
show create user root@localhost;
89+
ERROR HY000: The MariaDB server is running with the --skip-grant-tables option so it cannot execute this statement
90+
insert mysql.global_priv values ('foo', 'bar', '{}');
91+
insert mysql.global_priv values ('baz', 'baz', '{"plugin":"baz"}');
92+
set password for bar@foo = password("pass word");
93+
ERROR HY000: The MariaDB server is running with the --skip-grant-tables option so it cannot execute this statement
94+
flush privileges;
95+
show create user root@localhost;
96+
CREATE USER for root@localhost
97+
CREATE USER 'root'@'localhost'
98+
show create user bar@foo;
99+
CREATE USER for bar@foo
100+
CREATE USER 'bar'@'foo'
101+
show create user baz@baz;
102+
CREATE USER for baz@baz
103+
CREATE USER 'baz'@'baz' IDENTIFIED VIA baz
104+
set password for bar@foo = password("pass word");
105+
show create user bar@foo;
106+
CREATE USER for bar@foo
107+
CREATE USER 'bar'@'foo' IDENTIFIED BY PASSWORD '*EDBBEA7F4E7B5D8B0BC8D7AC5D1936FB7DA10611'
108+
alter user baz@baz identified with mysql_native_password as password("baz");
109+
show create user baz@baz;
110+
CREATE USER for baz@baz
111+
CREATE USER 'baz'@'baz' IDENTIFIED BY PASSWORD '*E52096EF8EB0240275A7FE9E069101C33F98CF07'
112+
drop user bar@foo;
113+
drop user baz@baz;

mysql-test/main/skip_grants.test

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,6 @@ use test;
1515
# - BUG#13504: creation view with DEFINER clause if --skip-grant-tables
1616
#
1717

18-
# Prepare.
19-
20-
--disable_warnings
21-
22-
DROP VIEW IF EXISTS v1;
23-
DROP VIEW IF EXISTS v2;
24-
DROP VIEW IF EXISTS v3;
25-
26-
DROP TABLE IF EXISTS t1;
27-
28-
DROP PROCEDURE IF EXISTS p1;
29-
DROP PROCEDURE IF EXISTS p2;
30-
DROP PROCEDURE IF EXISTS p3;
31-
32-
DROP FUNCTION IF EXISTS f1;
33-
DROP FUNCTION IF EXISTS f2;
34-
DROP FUNCTION IF EXISTS f3;
35-
36-
--enable_warnings
37-
3818
# Test case.
3919

4020
CREATE TABLE t1(c INT);
@@ -137,3 +117,26 @@ select no_such_function(1);
137117
# MDEV-8280 crash in 'show global status' with --skip-grant-tables
138118
#
139119
show global status like 'Acl%';
120+
121+
#
122+
# MDEV-18297
123+
# How to reset a forgotten root password
124+
#
125+
--error ER_OPTION_PREVENTS_STATEMENT
126+
show create user root@localhost;
127+
insert mysql.global_priv values ('foo', 'bar', '{}');
128+
insert mysql.global_priv values ('baz', 'baz', '{"plugin":"baz"}');
129+
--error ER_OPTION_PREVENTS_STATEMENT
130+
set password for bar@foo = password("pass word");
131+
flush privileges;
132+
show create user root@localhost;
133+
show create user bar@foo;
134+
show create user baz@baz;
135+
set password for bar@foo = password("pass word");
136+
show create user bar@foo;
137+
alter user baz@baz identified with mysql_native_password as password("baz");
138+
show create user baz@baz;
139+
drop user bar@foo;
140+
drop user baz@baz;
141+
# need to restart the server to restore the --skip-grant state
142+
--source include/restart_mysqld.inc

sql/sql_acl.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,6 +2557,7 @@ static bool acl_load(THD *thd, const Grant_tables& tables)
25572557

25582558
init_check_host();
25592559

2560+
thd->bootstrap= !initialized; // keep FLUSH PRIVILEGES connection special
25602561
initialized=1;
25612562
DBUG_RETURN(FALSE);
25622563
}
@@ -8908,6 +8909,11 @@ bool mysql_show_create_user(THD *thd, LEX_USER *lex_user)
89088909
uint head_length;
89098910
DBUG_ENTER("mysql_show_create_user");
89108911

8912+
if (!initialized)
8913+
{
8914+
my_error(ER_OPTION_PREVENTS_STATEMENT, MYF(0), "--skip-grant-tables");
8915+
DBUG_RETURN(TRUE);
8916+
}
89118917
if (check_show_access(thd, lex_user, &username, &hostname, NULL))
89128918
DBUG_RETURN(TRUE);
89138919

sql/sql_db.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ mysql_rm_db_internal(THD *thd, const LEX_CSTRING *db, bool if_exists, bool silen
882882
lock_db_routines(thd, dbnorm))
883883
goto exit;
884884

885-
if (!thd->bootstrap && !rm_mysql_schema)
885+
if (!rm_mysql_schema)
886886
{
887887
for (table= tables; table; table= table->next_local)
888888
{

sql/sql_table.cc

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,16 +2098,13 @@ bool mysql_rm_table(THD *thd,TABLE_LIST *tables, bool if_exists,
20982098
}
20992099
}
21002100
/* We remove statistics for table last, after we have the DDL lock */
2101-
if (!thd->bootstrap)
2101+
for (table= tables; table; table= table->next_local)
21022102
{
2103-
for (table= tables; table; table= table->next_local)
2104-
{
2105-
LEX_CSTRING db_name= table->db;
2106-
LEX_CSTRING table_name= table->table_name;
2107-
if (table->open_type == OT_BASE_ONLY ||
2108-
!thd->find_temporary_table(table))
2109-
(void) delete_statistics_for_table(thd, &db_name, &table_name);
2110-
}
2103+
LEX_CSTRING db_name= table->db;
2104+
LEX_CSTRING table_name= table->table_name;
2105+
if (table->open_type == OT_BASE_ONLY ||
2106+
!thd->find_temporary_table(table))
2107+
(void) delete_statistics_for_table(thd, &db_name, &table_name);
21112108
}
21122109
}
21132110

0 commit comments

Comments
 (0)