grant2.test 16.2 KB
Newer Older
1 2 3
# Grant tests not performed with embedded server
-- source include/not_embedded.inc

4 5
SET NAMES binary;

6 7 8 9 10 11
#
# GRANT tests that require several connections
# (usually it's GRANT, reconnect as another user, try something)
#


12 13 14
# prepare playground before tests
--disable_warnings
drop database if exists mysqltest;
serg@serg.mylan's avatar
serg@serg.mylan committed
15
drop database if exists mysqltest_1;
16 17 18 19 20 21 22 23
--enable_warnings
delete from mysql.user where user like 'mysqltest\_%';
delete from mysql.db where user like 'mysqltest\_%';
delete from mysql.tables_priv where user like 'mysqltest\_%';
delete from mysql.columns_priv where user like 'mysqltest\_%';
flush privileges;


24
grant all privileges on `my\_1`.* to mysqltest_1@localhost with grant option;
25
grant create user on *.* to mysqltest_1@localhost;
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
create user mysqltest_2@localhost;
connect (user_a,localhost,mysqltest_1,,);
connection user_a;
grant select on `my\_1`.* to mysqltest_2@localhost;
--error 1132
grant select on `my\_1`.* to mysqltest_2@localhost identified by 'pass';
disconnect user_a;
connection default;
grant update on mysql.* to mysqltest_1@localhost;
connect (user_b,localhost,mysqltest_1,,);
connection user_b;
grant select on `my\_1`.* to mysqltest_2@localhost identified by 'pass';
grant select on `my\_1`.* to mysqltest_3@localhost;
disconnect user_b;
connection default;
grant insert on mysql.* to mysqltest_1@localhost;
connect (user_c,localhost,mysqltest_1,,);
connection user_c;
grant select on `my\_1`.* to mysqltest_3@localhost;
grant select on `my\_1`.* to mysqltest_4@localhost identified by 'pass';
disconnect user_c;
connection default;
delete from mysql.user where user like 'mysqltest\_%';
delete from mysql.db where user like 'mysqltest\_%';
delete from mysql.tables_priv where user like 'mysqltest\_%';
delete from mysql.columns_priv where user like 'mysqltest\_%';
flush privileges;

54 55 56 57 58
#
# wild_compare fun
#

grant all privileges on `my\_%`.* to mysqltest_1@localhost with grant option;
59
grant create user on *.* to mysqltest_1@localhost;
60 61 62
connect (user1,localhost,mysqltest_1,,);
connection user1;
select current_user();
63
grant all privileges on `my\_1`.* to mysqltest_2@localhost with grant option;
64
--error 1044
65
grant all privileges on `my_%`.* to mysqltest_3@localhost with grant option;
66

67 68 69 70 71
#
# NO_AUTO_CREATE_USER mode
#
set @@sql_mode='NO_AUTO_CREATE_USER';
select @@sql_mode;
serg@serg.mylan's avatar
serg@serg.mylan committed
72 73 74 75
#
# GRANT without IDENTIFIED BY does not create new users
#
--error 1133
76 77 78
grant select on `my\_1`.* to mysqltest_4@localhost with grant option;
grant select on `my\_1`.* to mysqltest_4@localhost identified by 'mypass'
with grant option;
79
disconnect user1;
serg@serg.mylan's avatar
serg@serg.mylan committed
80
connection default;
81 82 83 84
show grants for mysqltest_1@localhost;
show grants for mysqltest_2@localhost;
--error 1141
show grants for mysqltest_3@localhost;
85 86 87 88
delete from mysql.user where user like 'mysqltest\_%';
delete from mysql.db where user like 'mysqltest\_%';
flush privileges;

serg@serg.mylan's avatar
serg@serg.mylan committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
#
# wild_compare part two - acl_cache
#
create database mysqltest_1;
grant all privileges on `mysqltest\_1`.* to mysqltest_1@localhost with grant option;
connect (user2,localhost,mysqltest_1,,);
connection user2;
select current_user();
show databases;
--error 1044
grant all privileges on `mysqltest_1`.* to mysqltest_1@localhost with grant option;
disconnect user2;
connection default;
show grants for mysqltest_1@localhost;
delete from mysql.user where user like 'mysqltest\_%';
delete from mysql.db where user like 'mysqltest\_%';
drop database mysqltest_1;
flush privileges;
107 108 109 110 111 112 113 114 115 116 117

#
# Bug #6173: One can circumvent missing UPDATE privilege if he has SELECT
# and INSERT privilege for table with primary key
#
create database mysqltest;
grant INSERT, SELECT on mysqltest.* to mysqltest_1@localhost;
flush privileges;
use mysqltest;
create table t1 (id int primary key, data varchar(255));

serg@sergbook.mysql.com's avatar
serg@sergbook.mysql.com committed
118
connect (mrbad, localhost, mysqltest_1,,mysqltest);
119 120 121
connection mrbad;
show grants for current_user();
insert into t1 values (1, 'I can''t change it!');
122
--error 1142
123 124
update t1 set data='I can change it!' where id = 1;
# This should not be allowed since it too require UPDATE privilege.
125
--error 1142
126 127
insert into t1 values (1, 'XXX') on duplicate key update data= 'I can change it!';
select * from t1;
serg@sergbook.mysql.com's avatar
serg@sergbook.mysql.com committed
128
disconnect mrbad;
129 130 131 132 133 134

connection default;
drop table t1;
delete from mysql.user where user like 'mysqltest\_%';
delete from mysql.db where user like 'mysqltest\_%';
flush privileges;
serg@sergbook.mysql.com's avatar
serg@sergbook.mysql.com committed
135 136 137 138 139 140
#
#
create table t1 (a int, b int);
grant select (a) on t1 to mysqltest_1@localhost with grant option;
connect (mrugly, localhost, mysqltest_1,,mysqltest);
connection mrugly;
serg@serg.mylan's avatar
serg@serg.mylan committed
141
--error 1143
serg@sergbook.mysql.com's avatar
serg@sergbook.mysql.com committed
142
grant select (a,b) on t1 to mysqltest_2@localhost;
serg@serg.mylan's avatar
serg@serg.mylan committed
143
--error 1142
serg@sergbook.mysql.com's avatar
serg@sergbook.mysql.com committed
144 145 146 147 148 149 150 151 152 153 154 155 156
grant select on t1 to mysqltest_3@localhost;
disconnect mrugly;

connection default;
drop table t1;
delete from mysql.user where user like 'mysqltest\_%';
delete from mysql.db where user like 'mysqltest\_%';
delete from mysql.tables_priv where user like 'mysqltest\_%';
delete from mysql.columns_priv where user like 'mysqltest\_%';
flush privileges;

drop database mysqltest;
use test;
157

158 159 160 161 162 163

#
# Bug #15775: "drop user" command does not refresh acl_check_hosts
#

# Create some test users
164 165 166 167 168 169 170
create user mysqltest_1@host1;
create user mysqltest_2@host2;
create user mysqltest_3@host3;
create user mysqltest_4@host4;
create user mysqltest_5@host5;
create user mysqltest_6@host6;
create user mysqltest_7@host7;
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
flush privileges;

# Drop one user
drop user mysqltest_3@host3;

# This connect failed before fix since the acl_check_hosts list was corrupted by the "drop user"
connect (con8,127.0.0.1,root,,test,$MASTER_MYPORT,);
disconnect con8;
connection default;

# Clean up - Drop all of the remaining users at once
drop user mysqltest_1@host1, mysqltest_2@host2, mysqltest_4@host4,
  mysqltest_5@host5, mysqltest_6@host6, mysqltest_7@host7;

# Check that it's still possible to connect
connect (con9,127.0.0.1,root,,test,$MASTER_MYPORT,);
disconnect con9;
connection default;

190 191 192 193 194 195 196 197 198 199 200 201
#
# Create and drop user
#
set sql_mode='maxdb';
--disable_warnings
drop table if exists t1, t2;
--enable_warnings
create table t1(c1 int);
create table t2(c1 int, c2 int);
#
# Three forms of CREATE USER
create user 'mysqltest_1';
202
--error 1396
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
create user 'mysqltest_1';
create user 'mysqltest_2' identified by 'Mysqltest-2';
create user 'mysqltest_3' identified by password 'fffffffffffffffffffffffffffffffffffffffff';
grant select on *.* to 'mysqltest_2';
grant insert on test.* to 'mysqltest_2';
grant update on test.t1 to 'mysqltest_2';
grant update (c2) on test.t2 to 'mysqltest_2';
select host,user,password from mysql.user where user like 'mysqltest_%' order by host,user,password;
select host,db,user from mysql.db where user like 'mysqltest_%' order by host,db,user;
select host,db,user,table_name from mysql.tables_priv where user like 'mysqltest_%' order by host,db,user,table_name;
select host,db,user,table_name,column_name from mysql.columns_priv where user like 'mysqltest_%' order by host,db,user,table_name,column_name;
show grants for 'mysqltest_1';
show grants for 'mysqltest_2';
#
# Drop
drop user 'mysqltest_1';
select host,user,password from mysql.user where user like 'mysqltest_%' order by host,user,password;
select host,db,user from mysql.db where user like 'mysqltest_%' order by host,db,user;
select host,db,user,table_name from mysql.tables_priv where user like 'mysqltest_%' order by host,db,user,table_name;
select host,db,user,table_name,column_name from mysql.columns_priv where user like 'mysqltest_%' order by host,db,user,table_name,column_name;
--error 1141
show grants for 'mysqltest_1';
#
# Rename
rename user 'mysqltest_2' to 'mysqltest_1';
select host,user,password from mysql.user where user like 'mysqltest_%' order by host,user,password;
select host,db,user from mysql.db where user like 'mysqltest_%' order by host,db,user;
select host,db,user,table_name from mysql.tables_priv where user like 'mysqltest_%' order by host,db,user,table_name;
select host,db,user,table_name,column_name from mysql.columns_priv where user like 'mysqltest_%' order by host,db,user,table_name,column_name;
show grants for 'mysqltest_1';
drop user 'mysqltest_1', 'mysqltest_3';
234
--error 1396
235
drop user 'mysqltest_1';
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
#
# Cleanup
drop table t1, t2;
#
# Add a stray record
insert into mysql.db set user='mysqltest_1', db='%', host='%';
flush privileges;
--error 1141
show grants for 'mysqltest_1';
--error 1269
revoke all privileges, grant option from 'mysqltest_1';
drop user 'mysqltest_1';
select host,db,user from mysql.db where user = 'mysqltest_1' order by host,db,user;
#
# Add a stray record
insert into mysql.tables_priv set host='%', db='test', user='mysqltest_1', table_name='t1';
flush privileges;
--error 1141
show grants for 'mysqltest_1';
drop user 'mysqltest_1';
select host,db,user,table_name from mysql.tables_priv where user = 'mysqltest_1' order by host,db,user,table_name;
#
# Add a stray record
insert into mysql.columns_priv set host='%', db='test', user='mysqltest_1', table_name='t1', column_name='c1';
flush privileges;
--error 1141
show grants for 'mysqltest_1';
drop user 'mysqltest_1';
select host,db,user,table_name,column_name from mysql.columns_priv where user = 'mysqltest_1' order by host,db,user,table_name,column_name;
#
# Handle multi user lists
create user 'mysqltest_1', 'mysqltest_2', 'mysqltest_3';
drop user 'mysqltest_1', 'mysqltest_2', 'mysqltest_3';
create user 'mysqltest_1', 'mysqltest_2' identified by 'Mysqltest-2', 'mysqltest_3' identified by password 'fffffffffffffffffffffffffffffffffffffffff';
rename user 'mysqltest_1' to 'mysqltest_1a', 'mysqltest_2' TO 'mysqltest_2a', 'mysqltest_3' TO 'mysqltest_3a';
271
--error 1396
272 273 274 275 276
drop user 'mysqltest_1', 'mysqltest_2', 'mysqltest_3';
drop user 'mysqltest_1a', 'mysqltest_2a', 'mysqltest_3a';
#
# Let one of multiple users fail
create user 'mysqltest_1', 'mysqltest_2', 'mysqltest_3';
277
--error 1396
278
create user 'mysqltest_1a', 'mysqltest_2', 'mysqltest_3a';
279
--error 1396
280 281
rename user 'mysqltest_1a' to 'mysqltest_1b', 'mysqltest_2a' TO 'mysqltest_2b', 'mysqltest_3a' TO 'mysqltest_3b';
drop user 'mysqltest_1', 'mysqltest_2', 'mysqltest_3';
282
--error 1396
283 284 285 286 287 288
drop user 'mysqltest_1b', 'mysqltest_2b', 'mysqltest_3b';
#
# Obsolete syntax has been dropped
create user 'mysqltest_2' identified by 'Mysqltest-2';
--error 1064
drop user 'mysqltest_2' identified by 'Mysqltest-2';
289
drop user 'mysqltest_2';
290 291 292 293 294 295 296 297 298 299 300 301
#
# Strange user names
create user '%@b'@'b';
show grants for '%@b'@'b';
grant select on mysql.* to '%@b'@'b';
show grants for '%@b'@'b';
rename user '%@b'@'b' to '%@a'@'a';
--error 1141
show grants for '%@b'@'b';
show grants for '%@a'@'a';
drop user '%@a'@'a';
#
302 303
# CREATE USER privilege is enough
#
304
create user mysqltest_2@localhost;
305
grant create user on *.* to mysqltest_2@localhost;
serg@serg.mylan's avatar
serg@serg.mylan committed
306 307
connect (user3,localhost,mysqltest_2,,);
connection user3;
308
--error 1142
309
select host,user,password from mysql.user where user like 'mysqltest_%' order by host,user,password;
310 311 312
create user mysqltest_A@'%';
rename user mysqltest_A@'%' to mysqltest_B@'%';
drop user mysqltest_B@'%';
serg@serg.mylan's avatar
serg@serg.mylan committed
313
disconnect user3;
314 315 316
connection default;
drop user mysqltest_2@localhost;
#
317
# INSERT/UPDATE/DELETE is ok too
318
create user mysqltest_3@localhost;
319
grant INSERT,DELETE,UPDATE on mysql.* to mysqltest_3@localhost;
serg@serg.mylan's avatar
serg@serg.mylan committed
320 321
connect (user4,localhost,mysqltest_3,,);
connection user4;
322 323
show grants;
--error 1142
324
select host,user,password from mysql.user where user like 'mysqltest_%' order by host,user,password;
325 326 327
insert into mysql.user set host='%', user='mysqltest_B';
create user mysqltest_A@'%';
rename user mysqltest_B@'%' to mysqltest_C@'%';
328
drop user mysqltest_C@'%';
329
drop user mysqltest_A@'%';
serg@serg.mylan's avatar
serg@serg.mylan committed
330
disconnect user4;
331 332 333
connection default;
drop user mysqltest_3@localhost;
#
334
# Bug #3309: Test IP addresses with netmask
335
set @@sql_mode='';
336 337 338 339 340 341 342 343 344 345 346
create database mysqltest_1;
create table mysqltest_1.t1 (i int);
insert into mysqltest_1.t1 values (1),(2),(3);
GRANT ALL ON mysqltest_1.t1 TO mysqltest_1@'127.0.0.0/255.0.0.0';
connect (n1,127.0.0.1,mysqltest_1,,mysqltest_1,$MASTER_MYPORT,$MASTER_MYSOCK);
connection n1;
show grants for current_user();
select * from t1;
disconnect n1;
connection default;
REVOKE ALL ON mysqltest_1.t1 FROM mysqltest_1@'127.0.0.0/255.0.0.0';
347 348
delete from mysql.user where user like 'mysqltest\_1';
flush privileges;
349
drop table mysqltest_1.t1;
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

#
# Bug #12302: 'SET PASSWORD = ...' didn't work if connecting hostname !=
# hostname the current user is authenticated as. Note that a test for this
# was also added to the test above.
#
grant all on mysqltest_1.* to mysqltest_1@'127.0.0.1';
connect (b12302,127.0.0.1,mysqltest_1,,mysqltest_1,$MASTER_MYPORT,);
connection b12302;
select current_user();
set password = password('changed');
disconnect b12302;
connection default;
select host, length(password) from mysql.user where user like 'mysqltest\_1';
revoke all on mysqltest_1.* from mysqltest_1@'127.0.0.1';
delete from mysql.user where user like 'mysqltest\_1';
flush privileges;
grant all on mysqltest_1.* to mysqltest_1@'127.0.0.0/255.0.0.0';
connect (b12302_2,127.0.0.1,mysqltest_1,,mysqltest_1,$MASTER_MYPORT,);
connection b12302_2;
select current_user();
set password = password('changed');
disconnect b12302_2;
connection default;
select host, length(password) from mysql.user where user like 'mysqltest\_1';
revoke all on mysqltest_1.* from mysqltest_1@'127.0.0.0/255.0.0.0';
delete from mysql.user where user like 'mysqltest\_1';
flush privileges;
378
drop database mysqltest_1;
379

380 381 382 383 384 385 386 387
# But anonymous users can't change their password
connect (n5,localhost,test,,test,$MASTER_MYPORT,$MASTER_MYSOCK);
connection n5;
--error 1044
set password = password("changed");
disconnect n5;
connection default;

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

# Bug #12423 "Deadlock when doing FLUSH PRIVILEGES and GRANT in 
# multi-threaded environment". We should be able to execute FLUSH
# PRIVILEGES and SET PASSWORD simultaneously with other account
# management commands (such as GRANT and REVOKE) without causing
# deadlocks. To achieve this we should ensure that all account
# management commands take table and internal locks in the same order.
connect (con2root,localhost,root,,);
connect (con3root,localhost,root,,);
# Check that we can execute FLUSH PRIVILEGES and GRANT simultaneously
# This will check that locks are taken in proper order during both
# user/db-level and table/column-level privileges reloading.
connection default;
lock table mysql.user write;
connection con2root;
send flush privileges;
connection con3root;
send grant all on *.* to 'mysqltest_1'@'localhost';
connection default;
unlock tables;
connection con2root;
reap;
connection con3root;
reap;
# Check for simultaneous SET PASSWORD and REVOKE.
connection default;
lock table mysql.user write;
connection con2root;
send set password for 'mysqltest_1'@'localhost' = password('');
connection con3root;
send revoke all on *.* from 'mysqltest_1'@'localhost';
connection default;
unlock tables;
connection con2root;
reap;
connection con3root;
reap;
connection default;
# Clean-up
drop user 'mysqltest_1'@'localhost';
disconnect con2root;
disconnect con3root;

431
# End of 4.1 tests
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454

#
# Bug#17279 user with no global privs and with create
#           priv in db can create databases
#

create database TESTDB;
create table t2(a int);
create temporary table t1 as select * from mysql.user;
delete from mysql.user where host='localhost';
INSERT INTO mysql.user VALUES
('%','mysqltest_1',password('password'),'N','N','N','N','N','N',
'N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N','N',
'','','','',0,0,0,0);
INSERT INTO mysql.db VALUES
('%','TESTDB','mysqltest_1','Y','Y','Y','Y','Y','Y','N','Y','Y','Y','Y','N','Y','Y','Y','
Y','N');
FLUSH PRIVILEGES;

connect (con1,localhost,mysqltest_1,password,TESTDB);

# The user mysqltest_1 should only be allowed access to
# database TESTDB, not TEStdb
455 456
# On system with "lowercase names" we get error "1007: Can't create db..."
--error 1044, 1007
457 458 459 460 461 462 463 464 465 466 467
create database TEStdb;

# Clean-up
connection default;
delete from mysql.user;
delete from mysql.db where host='%' and user='mysqltest_1' and db='TESTDB';
insert into mysql.user select * from t1;
drop table t1, t2;
drop database TESTDB;
flush privileges;

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
#
# BUG#13310 incorrect user parsing by SP
#

grant all privileges on test.* to `a@`@localhost;
grant execute on * to `a@`@localhost;
connect (bug13310,localhost,'a@',,test);
connection bug13310;
create table t2 (s1 int);
insert into t2 values (1);
--disable_warnings
drop function if exists f2;
--enable_warnings
delimiter //;
create function f2 () returns int begin declare v int; select s1 from t2
into v; return v; end//
delimiter ;//
select f2();

drop function f2;
drop table t2;
disconnect bug13310;

connection default;
REVOKE ALL PRIVILEGES, GRANT OPTION FROM `a@`@localhost;
493
drop user `a@`@localhost;