mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / mysql-test / t / sp-security.test
blobb7a99ad4103a602ba6d77684d0dcfd6bb1e81d15
2 # Testing SQL SECURITY of stored procedures
5 # Can't test with embedded server that doesn't support grants
6 -- source include/not_embedded.inc
8 # Save the initial number of concurrent sessions
9 --source include/count_sessions.inc
11 connect (con1root,localhost,root,,);
13 connection con1root;
14 use test;
16 # Create user user1 with no particular access rights
17 grant usage on *.* to user1@localhost;
18 flush privileges;
20 --disable_warnings
21 drop table if exists t1;
22 drop database if exists db1_secret;
23 --enable_warnings
24 # Create our secret database
25 create database db1_secret;
27 # Can create a procedure in other db
28 create procedure db1_secret.dummy() begin end;
29 drop procedure db1_secret.dummy;
31 use db1_secret;
33 create table t1 ( u varchar(64), i int );
34 insert into t1 values('test', 0);
36 # A test procedure and function
37 create procedure stamp(i int)
38   insert into db1_secret.t1 values (user(), i);
39 --replace_column 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
40 show procedure status like 'stamp';
42 delimiter |;
43 create function db() returns varchar(64)
44 begin
45   declare v varchar(64);
47   select u into v from t1 limit 1;
49   return v;
50 end|
51 delimiter ;|
52 --replace_column 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
53 show function status like 'db';
55 # root can, of course
56 call stamp(1);
57 select * from t1;
58 select db();
60 grant execute on procedure db1_secret.stamp to user1@'%';
61 grant execute on function db1_secret.db to user1@'%';
62 grant execute on procedure db1_secret.stamp to ''@'%';
63 grant execute on function db1_secret.db to ''@'%';
65 connect (con2user1,localhost,user1,,);
66 connect (con3anon,localhost,anon,,);
70 # User1 can
72 connection con2user1;
74 # This should work...
75 call db1_secret.stamp(2);
76 select db1_secret.db();
78 # ...but not this
79 --error ER_TABLEACCESS_DENIED_ERROR
80 select * from db1_secret.t1;
82 # ...and not this
83 --error ER_DBACCESS_DENIED_ERROR
84 create procedure db1_secret.dummy() begin end;
85 --error ER_SP_DOES_NOT_EXIST
86 drop procedure db1_secret.dummy;
87 --error ER_PROCACCESS_DENIED_ERROR
88 drop procedure db1_secret.stamp;
89 --error ER_PROCACCESS_DENIED_ERROR
90 drop function db1_secret.db;
94 # Anonymous can
96 connection con3anon;
98 # This should work...
99 call db1_secret.stamp(3);
100 select db1_secret.db();
102 # ...but not this
103 --error ER_TABLEACCESS_DENIED_ERROR
104 select * from db1_secret.t1;
106 # ...and not this
107 --error ER_DBACCESS_DENIED_ERROR
108 create procedure db1_secret.dummy() begin end;
109 --error ER_SP_DOES_NOT_EXIST
110 drop procedure db1_secret.dummy;
111 --error ER_PROCACCESS_DENIED_ERROR
112 drop procedure db1_secret.stamp;
113 --error ER_PROCACCESS_DENIED_ERROR
114 drop function db1_secret.db;
118 # Check it out
120 connection con1root;
121 select * from t1;
124 # Change to invoker's rights
126 alter procedure stamp sql security invoker;
127 --replace_column 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
128 show procedure status like 'stamp';
130 alter function db sql security invoker;
131 --replace_column 5 '0000-00-00 00:00:00' 6 '0000-00-00 00:00:00'
132 show function status like 'db';
134 # root still can
135 call stamp(4);
136 select * from t1;
137 select db();
140 # User1 cannot
142 connection con2user1;
144 # This should not work
145 --error ER_TABLEACCESS_DENIED_ERROR
146 call db1_secret.stamp(5);
147 --error ER_TABLEACCESS_DENIED_ERROR
148 select db1_secret.db();
151 # Anonymous cannot
153 connection con3anon;
155 # This should not work
156 --error ER_TABLEACCESS_DENIED_ERROR
157 call db1_secret.stamp(6);
158 --error ER_TABLEACCESS_DENIED_ERROR
159 select db1_secret.db();
162 # Bug#2777 Stored procedure doesn't observe definer's rights
165 connection con1root;
166 --disable_warnings
167 drop database if exists db2;
168 --enable_warnings
169 create database db2;
171 use db2;
173 create table t2 (s1 int);
174 insert into t2 values (0);
176 grant usage on db2.* to user1@localhost;
177 grant select on db2.* to user1@localhost;
178 grant usage on db2.* to user2@localhost;
179 grant select,insert,update,delete,create routine on db2.* to user2@localhost;
180 grant create routine on db2.* to user1@localhost;
181 flush privileges;
183 connection con2user1;
184 use db2;
186 create procedure p () insert into t2 values (1);
188 # Check that this doesn't work.
189 --error ER_TABLEACCESS_DENIED_ERROR
190 call p();
192 connect (con4user2,localhost,user2,,);
194 connection con4user2;
195 use db2;
197 # This should not work, since p is executed with definer's (user1's) rights.
198 --error ER_PROCACCESS_DENIED_ERROR
199 call p();
200 select * from t2;
202 create procedure q () insert into t2 values (2);
204 call q();
205 select * from t2;
207 connection con1root;
208 grant usage on procedure db2.q to user2@localhost with grant option;
210 connection con4user2;
211 grant execute on procedure db2.q to user1@localhost;
213 connection con2user1;
214 use db2;
216 # This should work
217 call q();
218 select * from t2;
221 # Bug#6030 Stored procedure has no appropriate DROP privilege
222 # (or ALTER for that matter)
224 # still connection con2user1 in db2
226 # This should work:
227 alter procedure p modifies sql data;
228 drop procedure p;
230 # This should NOT work
231 --error ER_PROCACCESS_DENIED_ERROR
232 alter procedure q modifies sql data;
233 --error ER_PROCACCESS_DENIED_ERROR
234 drop procedure q;
236 connection con1root;
237 use db2;
238 # But root always can
239 alter procedure q modifies sql data;
240 drop procedure q;
243 # Clean up
244 #Still connection con1root;
245 disconnect con2user1;
246 disconnect con3anon;
247 disconnect con4user2;
248 use test;
249 select type,db,name from mysql.proc where db like 'db%';
250 drop database db1_secret;
251 drop database db2;
252 # Make sure the routines are gone
253 select type,db,name from mysql.proc where db like 'db%';
254 # Get rid of the users
255 delete from mysql.user where user='user1' or user='user2';
256 delete from mysql.user where user='' and host='%';
257 # And any routine privileges
258 delete from mysql.procs_priv where user='user1' or user='user2';
259 # Delete the grants to user ''@'%' that was created above
260 delete from mysql.procs_priv where user='' and host='%';
261 delete from mysql.db where user='user2';
262 flush privileges;
264 # Test the new security acls
266 grant usage on *.* to usera@localhost;
267 grant usage on *.* to userb@localhost;
268 grant usage on *.* to userc@localhost;
269 create database sptest;
270 create table t1 ( u varchar(64), i int );
271 create procedure sptest.p1(i int) insert into test.t1 values (user(), i);
272 grant insert on t1 to usera@localhost;
273 grant execute on procedure sptest.p1 to usera@localhost;
274 show grants for usera@localhost;
275 grant execute on procedure sptest.p1 to userc@localhost with grant option;
276 show grants for userc@localhost;
278 connect (con2usera,localhost,usera,,);
279 connect (con3userb,localhost,userb,,);
280 connect (con4userc,localhost,userc,,);
282 connection con2usera;
283 call sptest.p1(1);
284 --error ER_PROCACCESS_DENIED_ERROR
285 grant execute on procedure sptest.p1 to userb@localhost;
286 --error ER_PROCACCESS_DENIED_ERROR
287 drop procedure sptest.p1;
289 connection con3userb;
290 --error ER_PROCACCESS_DENIED_ERROR
291 call sptest.p1(2);
292 --error ER_PROCACCESS_DENIED_ERROR
293 grant execute on procedure sptest.p1 to userb@localhost;
294 --error ER_PROCACCESS_DENIED_ERROR
295 drop procedure sptest.p1;
297 connection con4userc;
298 call sptest.p1(3);
299 grant execute on procedure sptest.p1 to userb@localhost;
300 --error ER_PROCACCESS_DENIED_ERROR
301 drop procedure sptest.p1;
303 connection con3userb;
304 call sptest.p1(4);
305 --error ER_PROCACCESS_DENIED_ERROR
306 grant execute on procedure sptest.p1 to userb@localhost;
307 --error ER_PROCACCESS_DENIED_ERROR
308 drop procedure sptest.p1;
310 connection con1root;
311 select * from t1;
313 grant all privileges on procedure sptest.p1 to userc@localhost;
314 show grants for userc@localhost;
315 show grants for userb@localhost;
317 connection con4userc;
318 revoke all privileges on procedure sptest.p1 from userb@localhost;
320 connection con1root;
321 show grants for userb@localhost;
323 #cleanup
324 disconnect con4userc;
325 disconnect con3userb;
326 disconnect con2usera;
327 use test;
328 drop database sptest;
329 delete from mysql.user where user='usera' or user='userb' or user='userc';
330 delete from mysql.procs_priv where user='usera' or user='userb' or user='userc';
331 delete from mysql.tables_priv where user='usera';
332 flush privileges;
333 drop table t1;
336 # Bug#9503 reseting correct parameters of thread after error in SP function
338 connect (root,localhost,root,,test);
339 connection root;
341 --disable_warnings
342 drop function if exists bug_9503;
343 --enable_warnings
344 delimiter //;
345 create database mysqltest//
346 use mysqltest//
347 create table t1 (s1 int)//
348 grant select on t1 to user1@localhost//
349 create function bug_9503 () returns int sql security invoker begin declare v int;
350 select min(s1) into v from t1; return v; end//
351 delimiter ;//
353 connect (user1,localhost,user1,,test);
354 connection user1;
355 use mysqltest;
356 -- error ER_PROCACCESS_DENIED_ERROR
357 select bug_9503();
359 connection root;
360 grant execute on function bug_9503 to user1@localhost;
362 connection user1;
363 do 1;
364 use test;
366 disconnect user1;
367 connection root;
368 REVOKE ALL PRIVILEGES, GRANT OPTION FROM user1@localhost;
369 drop function bug_9503;
370 use test;
371 drop database mysqltest;
372 connection default;
373 disconnect root;
376 # correct value from current_user() in function run from "security definer"
377 # (Bug#7291 Stored procedures: wrong CURRENT_USER value)
379 connection con1root;
380 use test;
382 select current_user();
383 select user();
384 create procedure bug7291_0 () sql security invoker select current_user(), user();
385 create procedure bug7291_1 () sql security definer call bug7291_0();
386 create procedure bug7291_2 () sql security invoker call bug7291_0();
387 grant execute on procedure bug7291_0 to user1@localhost;
388 grant execute on procedure bug7291_1 to user1@localhost;
389 grant execute on procedure bug7291_2 to user1@localhost;
391 connect (user1,localhost,user1,,);
392 connection user1;
394 call bug7291_2();
395 call bug7291_1();
397 connection con1root;
398 drop procedure bug7291_1;
399 drop procedure bug7291_2;
400 drop procedure bug7291_0;
401 disconnect user1;
402 REVOKE ALL PRIVILEGES, GRANT OPTION FROM user1@localhost;
403 drop user user1@localhost;
406 # Bug#12318 Wrong error message when accessing an inaccessible stored
407 # procedure in another database when the current database is
408 # information_schema.
411 --disable_warnings
412 drop database if exists mysqltest_1;
413 --enable_warnings
415 create database mysqltest_1;
416 delimiter //;
417 create procedure mysqltest_1.p1()
418 begin
419    select 1 from dual;
420 end//
421 delimiter ;//
423 grant usage on *.* to mysqltest_1@localhost;
425 connect (n1,localhost,mysqltest_1,,information_schema,$MASTER_MYPORT,$MASTER_MYSOCK);
426 connection n1;
427 --error ER_PROCACCESS_DENIED_ERROR
428 call mysqltest_1.p1();
429 disconnect n1;
430 # Test also without a current database
431 connect (n2,localhost,mysqltest_1,,*NO-ONE*,$MASTER_MYPORT,$MASTER_MYSOCK);
432 connection n2;
433 --error ER_PROCACCESS_DENIED_ERROR
434 call mysqltest_1.p1();
435 disconnect n2;
437 connection default;
439 drop procedure mysqltest_1.p1;
440 drop database mysqltest_1;
442 revoke usage on *.* from mysqltest_1@localhost;
443 drop user mysqltest_1@localhost;
446 # Bug#12812 create view calling a function works without execute right
447 #           on function
448 delimiter |;
449 --disable_warnings
450 drop function if exists bug12812|
451 --enable_warnings
452 create function bug12812() returns char(2)
453 begin
454   return 'ok';
455 end;
456 create user user_bug12812@localhost IDENTIFIED BY 'ABC'|
457 --replace_result $MASTER_MYPORT MYSQL_PORT $MASTER_MYSOCK MYSQL_SOCK
458 connect (test_user_12812,localhost,user_bug12812,ABC,test)|
459 --error ER_PROCACCESS_DENIED_ERROR
460 SELECT test.bug12812()|
461 --error ER_PROCACCESS_DENIED_ERROR
462 CREATE VIEW v1 AS SELECT test.bug12812()|
463 # Cleanup
464 connection default|
465 disconnect test_user_12812|
466 DROP USER user_bug12812@localhost|
467 drop function bug12812|
468 delimiter ;|
472 # Bug#14834 Server denies to execute Stored Procedure
474 # The problem here was with '_' in the database name.
476 create database db_bug14834;
478 create user user1_bug14834@localhost identified by '';
479 # The exact name of the database (no wildcard)
480 grant all on `db\_bug14834`.* to user1_bug14834@localhost;
482 create user user2_bug14834@localhost identified by '';
483 # The exact name of the database (no wildcard)
484 grant all on `db\_bug14834`.* to user2_bug14834@localhost;
486 create user user3_bug14834@localhost identified by '';
487 # Wildcards in the database name
488 grant all on `db__ug14834`.* to user3_bug14834@localhost;
490 connect (user1_bug14834,localhost,user1_bug14834,,db_bug14834);
491 # Create the procedure and check that we can call it
492 create procedure p_bug14834() select user(), current_user();
493 call p_bug14834();
495 connect (user2_bug14834,localhost,user2_bug14834,,db_bug14834);
496 # This didn't work before
497 call p_bug14834();
499 connect (user3_bug14834,localhost,user3_bug14834,,db_bug14834);
500 # Should also work
501 call p_bug14834();
503 # Cleanup
504 connection default;
505 disconnect user1_bug14834;
506 disconnect user2_bug14834;
507 disconnect user3_bug14834;
508 drop user user1_bug14834@localhost;
509 drop user user2_bug14834@localhost;
510 drop user user3_bug14834@localhost;
511 drop database db_bug14834;
515 # Bug#14533 'desc tbl' in stored procedure causes error
516 # ER_TABLEACCESS_DENIED_ERROR
518 create database db_bug14533;
519 use db_bug14533;
520 create table t1 (id int);
521 create user user_bug14533@localhost identified by '';
523 create procedure bug14533_1()
524     sql security definer
525   desc db_bug14533.t1;
527 create procedure bug14533_2()
528     sql security definer
529    select * from db_bug14533.t1;
531 grant execute on procedure db_bug14533.bug14533_1 to user_bug14533@localhost;
532 grant execute on procedure db_bug14533.bug14533_2 to user_bug14533@localhost;
534 connect (user_bug14533,localhost,user_bug14533,,test);
536 # These should work
537 call db_bug14533.bug14533_1();
538 call db_bug14533.bug14533_2();
540 # For reference, these should not work
541 --error ER_TABLEACCESS_DENIED_ERROR
542 desc db_bug14533.t1;
543 --error ER_TABLEACCESS_DENIED_ERROR
544 select * from db_bug14533.t1;
546 # Cleanup
547 connection default;
548 disconnect user_bug14533;
549 drop user user_bug14533@localhost;
550 drop database db_bug14533;
554 # WL#2897 Complete definer support in the stored routines.
556 # The following cases are tested:
557 #   1. check that if DEFINER-clause is not explicitly specified, stored routines
558 #     are created with CURRENT_USER privileges;
559 #   2. check that if DEFINER-clause specifies non-current user, SUPER privilege
560 #     is required to create a stored routine;
561 #   3. check that if DEFINER-clause specifies non-existent user, a warning is
562 #     emitted.
563 #   4. check that SHOW CREATE PROCEDURE | FUNCTION works correctly;
565 # The following cases are tested in other test suites:
566 #   - check that mysqldump dumps new attribute correctly;
567 #   - check that slave replicates CREATE-statements with explicitly specified
568 #     DEFINER correctly.
571 # Setup the environment.
573 --echo
574 --echo ---> connection: root
575 --connection con1root
577 --disable_warnings
578 DROP DATABASE IF EXISTS mysqltest;
579 --enable_warnings
581 CREATE DATABASE mysqltest;
583 CREATE USER mysqltest_1@localhost;
584 GRANT ALL PRIVILEGES ON mysqltest.* TO mysqltest_1@localhost;
586 CREATE USER mysqltest_2@localhost;
587 GRANT SUPER ON *.* TO mysqltest_2@localhost;
588 GRANT ALL PRIVILEGES ON mysqltest.* TO mysqltest_2@localhost;
590 --connect (mysqltest_2_con,localhost,mysqltest_2,,mysqltest)
591 --connect (mysqltest_1_con,localhost,mysqltest_1,,mysqltest)
593 # test case (1).
595 --echo
596 --echo ---> connection: mysqltest_2_con
597 --connection mysqltest_2_con
599 USE mysqltest;
601 CREATE PROCEDURE wl2897_p1() SELECT 1;
603 CREATE FUNCTION wl2897_f1() RETURNS INT RETURN 1;
605 # test case (2).
607 --echo
608 --echo ---> connection: mysqltest_1_con
609 --connection mysqltest_1_con
611 USE mysqltest;
613 --error ER_SPECIFIC_ACCESS_DENIED_ERROR
614 CREATE DEFINER=root@localhost PROCEDURE wl2897_p2() SELECT 2;
616 --error ER_SPECIFIC_ACCESS_DENIED_ERROR
617 CREATE DEFINER=root@localhost FUNCTION wl2897_f2() RETURNS INT RETURN 2;
619 # test case (3).
621 --echo
622 --echo ---> connection: mysqltest_2_con
623 --connection mysqltest_2_con
625 use mysqltest;
627 CREATE DEFINER='a @ b @ c'@localhost PROCEDURE wl2897_p3() SELECT 3;
629 CREATE DEFINER='a @ b @ c'@localhost FUNCTION wl2897_f3() RETURNS INT RETURN 3;
631 # test case (4).
633 --echo
634 --echo ---> connection: con1root
635 --connection con1root
637 USE mysqltest;
639 SHOW CREATE PROCEDURE wl2897_p1;
640 SHOW CREATE PROCEDURE wl2897_p3;
642 SHOW CREATE FUNCTION wl2897_f1;
643 SHOW CREATE FUNCTION wl2897_f3;
645 # Cleanup.
647 DROP USER mysqltest_1@localhost;
648 DROP USER mysqltest_2@localhost;
650 DROP DATABASE mysqltest;
652 --disconnect mysqltest_1_con
653 --disconnect mysqltest_2_con
657 # Bug#13198 SP executes if definer does not exist
660 # Prepare environment.
662 --echo
663 --echo ---> connection: root
664 --connection con1root
666 --disable_warnings
667 DROP DATABASE IF EXISTS mysqltest;
668 --enable_warnings
670 CREATE DATABASE mysqltest;
672 CREATE USER mysqltest_1@localhost;
673 GRANT ALL PRIVILEGES ON mysqltest.* TO mysqltest_1@localhost;
675 CREATE USER mysqltest_2@localhost;
676 GRANT ALL PRIVILEGES ON mysqltest.* TO mysqltest_2@localhost;
678 --connect (mysqltest_1_con,localhost,mysqltest_1,,mysqltest)
679 --connect (mysqltest_2_con,localhost,mysqltest_2,,mysqltest)
681 # Create a procedure/function under u1.
683 --echo
684 --echo ---> connection: mysqltest_1_con
685 --connection mysqltest_1_con
687 USE mysqltest;
689 CREATE PROCEDURE bug13198_p1()
690   SELECT 1;
692 CREATE FUNCTION bug13198_f1() RETURNS INT
693   RETURN 1;
695 CALL bug13198_p1();
697 SELECT bug13198_f1();
699 # Check that u2 can call the procedure/function.
701 --echo
702 --echo ---> connection: mysqltest_2_con
703 --connection mysqltest_2_con
705 USE mysqltest;
707 CALL bug13198_p1();
709 SELECT bug13198_f1();
711 # Drop user u1 (definer of the object);
713 --echo
714 --echo ---> connection: root
715 --connection con1root
717 --disconnect mysqltest_1_con
719 DROP USER mysqltest_1@localhost;
721 # Check that u2 can not call the procedure/function.
723 --echo
724 --echo ---> connection: mysqltest_2_con
725 --connection mysqltest_2_con
727 USE mysqltest;
729 --error ER_NO_SUCH_USER
730 CALL bug13198_p1();
732 --error ER_NO_SUCH_USER
733 SELECT bug13198_f1();
735 # Cleanup.
737 --echo
738 --echo ---> connection: root
739 --connection con1root
741 --disconnect mysqltest_2_con
743 DROP USER mysqltest_2@localhost;
745 DROP DATABASE mysqltest;
748 # Bug#19857 When a user with CREATE ROUTINE priv creates a routine,
749 #           it results in NULL p/w
752 # Can't test with embedded server that doesn't support grants
754 GRANT USAGE ON *.* TO user19857@localhost IDENTIFIED BY 'meow';
755 GRANT SELECT, INSERT, UPDATE, DELETE, CREATE ROUTINE, ALTER ROUTINE ON test.* TO
756 user19857@localhost;
757 SELECT Host,User,Password FROM mysql.user WHERE User='user19857';
759 --connect (mysqltest_2_con,localhost,user19857,meow,test)
760 --echo
761 --echo ---> connection: mysqltest_2_con
762 --connection mysqltest_2_con
764 USE test;
766 DELIMITER //;
767   CREATE PROCEDURE sp19857() DETERMINISTIC
768   BEGIN
769     DECLARE a INT;
770     SET a=1;
771     SELECT a;
772   END //
773 DELIMITER ;//
775 SHOW CREATE PROCEDURE test.sp19857;
777 --disconnect mysqltest_2_con
778 --connect (mysqltest_2_con,localhost,user19857,meow,test)
779 --connection mysqltest_2_con
781 DROP PROCEDURE IF EXISTS test.sp19857;
783 --echo
784 --echo ---> connection: root
785 --connection con1root
787 --disconnect mysqltest_2_con
789 SELECT Host,User,Password FROM mysql.user WHERE User='user19857';
791 DROP USER user19857@localhost;
793 --disconnect con1root
794 --connection default
795 use test;
798 # Bug#18630 Arguments of suid routine calculated in wrong security context
800 # Arguments of suid routines were calculated in definer's security
801 # context instead of caller's context thus creating security hole.
803 --disable_warnings
804 DROP TABLE IF EXISTS t1;
805 DROP VIEW IF EXISTS v1;
806 DROP FUNCTION IF EXISTS f_suid;
807 DROP PROCEDURE IF EXISTS p_suid;
808 DROP FUNCTION IF EXISTS f_evil;
809 --enable_warnings
810 DELETE FROM mysql.user WHERE user LIKE 'mysqltest\_%';
811 DELETE FROM mysql.db WHERE user LIKE 'mysqltest\_%';
812 DELETE FROM mysql.tables_priv WHERE user LIKE 'mysqltest\_%';
813 DELETE FROM mysql.columns_priv WHERE user LIKE 'mysqltest\_%';
814 FLUSH PRIVILEGES;
816 CREATE TABLE t1 (i INT);
817 CREATE FUNCTION f_suid(i INT) RETURNS INT SQL SECURITY DEFINER RETURN 0;
818 CREATE PROCEDURE p_suid(IN i INT) SQL SECURITY DEFINER SET @c:= 0;
820 CREATE USER mysqltest_u1@localhost;
821 # Thanks to this grant statement privileges of anonymous users on
822 # 'test' database are not applicable for mysqltest_u1@localhost.
823 GRANT EXECUTE ON test.* TO mysqltest_u1@localhost;
825 delimiter |;
826 CREATE DEFINER=mysqltest_u1@localhost FUNCTION f_evil () RETURNS INT
827   SQL SECURITY INVOKER
828 BEGIN
829   SET @a:= CURRENT_USER();
830   SET @b:= (SELECT COUNT(*) FROM t1);
831   RETURN @b;
832 END|
833 delimiter ;|
835 CREATE SQL SECURITY INVOKER VIEW v1 AS SELECT f_evil();
837 connect (conn1, localhost, mysqltest_u1,,);
839 --error ER_TABLEACCESS_DENIED_ERROR
840 SELECT COUNT(*) FROM t1;
842 --error ER_TABLEACCESS_DENIED_ERROR
843 SELECT f_evil();
844 SELECT @a, @b;
846 --error ER_TABLEACCESS_DENIED_ERROR
847 SELECT f_suid(f_evil());
848 SELECT @a, @b;
850 --error ER_TABLEACCESS_DENIED_ERROR
851 CALL p_suid(f_evil());
852 SELECT @a, @b;
854 --error ER_TABLEACCESS_DENIED_ERROR
855 SELECT * FROM v1;
856 SELECT @a, @b;
858 disconnect conn1;
859 connection default;
861 DROP VIEW v1;
862 DROP FUNCTION f_evil;
863 DROP USER mysqltest_u1@localhost;
864 DROP PROCEDURE p_suid;
865 DROP FUNCTION f_suid;
866 DROP TABLE t1;
868 --echo #
869 --echo # Bug #48872 : Privileges for stored functions ignored if function name 
870 --echo #  is mixed case
871 --echo #
873 CREATE DATABASE B48872;
874 USE B48872;
875 CREATE TABLE `TestTab` (id INT);
876 INSERT INTO `TestTab` VALUES (1),(2);
877 CREATE FUNCTION `f_Test`() RETURNS INT RETURN 123;
878 CREATE FUNCTION `f_Test_denied`() RETURNS INT RETURN 123;
879 CREATE USER 'tester';
880 CREATE USER 'Tester';
881 GRANT SELECT ON TABLE `TestTab` TO 'tester';
882 GRANT EXECUTE ON FUNCTION `f_Test` TO 'tester';
883 GRANT EXECUTE ON FUNCTION `f_Test_denied` TO 'Tester';
885 SELECT f_Test();
886 SELECT * FROM TestTab;
888 CONNECT (con_tester,localhost,tester,,B48872);
889 CONNECT (con_tester_denied,localhost,Tester,,B48872);
890 CONNECTION con_tester;
892 SELECT * FROM TestTab;
893 SELECT `f_Test`();
894 SELECT `F_TEST`();
895 SELECT f_Test();
896 SELECT F_TEST();
898 CONNECTION con_tester_denied;
900 --disable_result_log
901 --error ER_TABLEACCESS_DENIED_ERROR
902 SELECT * FROM TestTab;
903 --error ER_PROCACCESS_DENIED_ERROR
904 SELECT `f_Test`();
905 --error ER_PROCACCESS_DENIED_ERROR
906 SELECT `F_TEST`();
907 --error ER_PROCACCESS_DENIED_ERROR
908 SELECT f_Test();
909 --error ER_PROCACCESS_DENIED_ERROR
910 SELECT F_TEST();
911 --enable_result_log
912 SELECT `f_Test_denied`();
913 SELECT `F_TEST_DENIED`();
915 CONNECTION default;
916 DISCONNECT con_tester;
917 DISCONNECT con_tester_denied;
918 DROP TABLE `TestTab`;
919 DROP FUNCTION `f_Test`;
920 DROP FUNCTION `f_Test_denied`;
922 USE test;
923 DROP USER 'tester';
924 DROP USER 'Tester';
925 DROP DATABASE B48872;
927 --echo End of 5.0 tests.
929 --echo #
930 --echo # Bug#11882603 SELECT_ACL ON ANY COLUMN IN MYSQL.PROC ALLOWS TO SEE
931 --echo #              DEFINITION OF ANY ROUTINE. 
932 --echo #
934 --disable_warnings
935 DROP DATABASE IF EXISTS db1;
936 --enable_warnings
938 CREATE DATABASE db1;
939 CREATE PROCEDURE db1.p1() SELECT 1;
940 CREATE USER user2@localhost IDENTIFIED BY '';
941 GRANT SELECT(db) ON mysql.proc TO user2@localhost;
943 --echo # Connection con2 as user2
944 connect (con2, localhost, user2);
945 --echo # The statement below before disclosed info from body_utf8 column.
946 --error ER_SP_DOES_NOT_EXIST
947 SHOW CREATE PROCEDURE db1.p1;
949 --echo # Check that SHOW works with SELECT grant on whole table
950 --echo # Connection default
951 connection default;
952 GRANT SELECT ON mysql.proc TO user2@localhost;
954 --echo # Connection con2
955 connection con2;
956 --echo # This should work
957 SHOW CREATE PROCEDURE db1.p1;
959 --echo # Connection default
960 connection default;
961 disconnect con2;
962 DROP USER user2@localhost;
963 DROP DATABASE db1;
966 # Wait till all disconnects are completed
967 --source include/wait_until_count_sessions.inc