~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to mysql-test/t/trigger_notembedded.test

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Test case(s) in this file contain(s) GRANT/REVOKE statements, which are not
 
2
# supported in embedded server. So, this test should not be run on embedded
 
3
# server.
 
4
 
 
5
-- source include/not_embedded.inc
 
6
 
 
7
###########################################################################
 
8
#
 
9
# Tests for WL#2818:
 
10
#   - Check that triggers are executed under the authorization of the definer.
 
11
#   - Check DEFINER clause of CREATE TRIGGER statement;
 
12
#     - Check that SUPER privilege required to create a trigger with different
 
13
#       definer.
 
14
#     - Check that if the user specified as DEFINER does not exist, a warning
 
15
#       is emitted.
 
16
#     - Check that the definer of a trigger does not exist, the trigger will
 
17
#       not be activated.
 
18
#   - Check that SHOW TRIGGERS statement provides "Definer" column.
 
19
#   - Check that if trigger contains NEW/OLD variables, the definer must have
 
20
#     SELECT privilege on the subject table (aka BUG#15166/BUG#15196).
 
21
#
 
22
#  Let's also check that user name part of definer can contain '@' symbol (to
 
23
#  check that triggers are not affected by BUG#13310 "incorrect user parsing
 
24
#  by SP").
 
25
#
 
26
###########################################################################
 
27
 
 
28
#
 
29
# Prepare environment.
 
30
#
 
31
 
 
32
DELETE FROM mysql.user WHERE User LIKE 'mysqltest_%';
 
33
DELETE FROM mysql.db WHERE User LIKE 'mysqltest_%';
 
34
DELETE FROM mysql.tables_priv WHERE User LIKE 'mysqltest_%';
 
35
DELETE FROM mysql.columns_priv WHERE User LIKE 'mysqltest_%';
 
36
FLUSH PRIVILEGES;
 
37
 
 
38
--disable_warnings
 
39
DROP DATABASE IF EXISTS mysqltest_db1;
 
40
--enable_warnings
 
41
 
 
42
CREATE DATABASE mysqltest_db1;
 
43
 
 
44
CREATE USER mysqltest_dfn@localhost;
 
45
CREATE USER mysqltest_inv@localhost;
 
46
 
 
47
GRANT CREATE ON mysqltest_db1.* TO mysqltest_dfn@localhost;
 
48
 
 
49
--connect (wl2818_definer_con,localhost,mysqltest_dfn,,mysqltest_db1)
 
50
--connection wl2818_definer_con
 
51
--echo
 
52
--echo ---> connection: wl2818_definer_con
 
53
 
 
54
CREATE TABLE t1(num_value INT);
 
55
CREATE TABLE t2(user_str TEXT);
 
56
 
 
57
--disconnect wl2818_definer_con
 
58
 
 
59
--connection default
 
60
--echo
 
61
--echo ---> connection: default
 
62
 
 
63
GRANT INSERT, DROP ON mysqltest_db1.t1 TO mysqltest_dfn@localhost;
 
64
GRANT INSERT, DROP ON mysqltest_db1.t2 TO mysqltest_dfn@localhost;
 
65
 
 
66
#
 
67
# Check that the user must have TRIGGER privilege to create a trigger.
 
68
#
 
69
 
 
70
--connection default
 
71
--echo
 
72
--echo ---> connection: default
 
73
 
 
74
GRANT SUPER ON *.* TO mysqltest_dfn@localhost;
 
75
 
 
76
--connect (wl2818_definer_con,localhost,mysqltest_dfn,,mysqltest_db1)
 
77
--connection wl2818_definer_con
 
78
--echo
 
79
--echo ---> connection: wl2818_definer_con
 
80
 
 
81
--error ER_TABLEACCESS_DENIED_ERROR
 
82
CREATE TRIGGER trg1 AFTER INSERT ON t1
 
83
  FOR EACH ROW
 
84
    INSERT INTO t2 VALUES(CURRENT_USER());
 
85
 
 
86
--disconnect wl2818_definer_con
 
87
 
 
88
#
 
89
# Check that the user must have TRIGGER privilege to drop a trigger.
 
90
#
 
91
 
 
92
--connection default
 
93
--echo
 
94
--echo ---> connection: default
 
95
 
 
96
GRANT TRIGGER ON mysqltest_db1.t1 TO mysqltest_dfn@localhost;
 
97
 
 
98
--connect (wl2818_definer_con,localhost,mysqltest_dfn,,mysqltest_db1)
 
99
--connection wl2818_definer_con
 
100
--echo
 
101
--echo ---> connection: wl2818_definer_con
 
102
 
 
103
CREATE TRIGGER trg1 AFTER INSERT ON t1
 
104
  FOR EACH ROW
 
105
    INSERT INTO t2 VALUES(CURRENT_USER());
 
106
 
 
107
--disconnect wl2818_definer_con
 
108
 
 
109
--connection default
 
110
--echo
 
111
--echo ---> connection: default
 
112
 
 
113
REVOKE TRIGGER ON mysqltest_db1.t1 FROM mysqltest_dfn@localhost;
 
114
 
 
115
--connect (wl2818_definer_con,localhost,mysqltest_dfn,,mysqltest_db1)
 
116
--connection wl2818_definer_con
 
117
--echo
 
118
--echo ---> connection: wl2818_definer_con
 
119
 
 
120
--error ER_TABLEACCESS_DENIED_ERROR
 
121
DROP TRIGGER trg1;
 
122
 
 
123
--disconnect wl2818_definer_con
 
124
 
 
125
#
 
126
# Check that the definer must have TRIGGER privilege to activate a trigger.
 
127
#
 
128
 
 
129
--connect (wl2818_definer_con,localhost,mysqltest_dfn,,mysqltest_db1)
 
130
--connection wl2818_definer_con
 
131
--echo
 
132
--echo ---> connection: wl2818_definer_con
 
133
 
 
134
--error ER_TABLEACCESS_DENIED_ERROR
 
135
INSERT INTO t1 VALUES(0);
 
136
 
 
137
--disconnect wl2818_definer_con
 
138
 
 
139
--connection default
 
140
--echo
 
141
--echo ---> connection: default
 
142
 
 
143
GRANT TRIGGER ON mysqltest_db1.t1 TO mysqltest_dfn@localhost;
 
144
 
 
145
--connect (wl2818_definer_con,localhost,mysqltest_dfn,,mysqltest_db1)
 
146
--connection wl2818_definer_con
 
147
--echo
 
148
--echo ---> connection: wl2818_definer_con
 
149
 
 
150
INSERT INTO t1 VALUES(0);
 
151
 
 
152
# Cleanup for further tests.
 
153
DROP TRIGGER trg1;
 
154
TRUNCATE TABLE t1;
 
155
TRUNCATE TABLE t2;
 
156
 
 
157
--disconnect wl2818_definer_con
 
158
 
 
159
--connection default
 
160
--echo
 
161
--echo ---> connection: default
 
162
 
 
163
REVOKE SUPER ON *.* FROM mysqltest_dfn@localhost;
 
164
 
 
165
#
 
166
# Check that triggers are executed under the authorization of the definer:
 
167
#   - create two tables under "definer";
 
168
#   - grant all privileges on the test db to "definer";
 
169
#   - grant all privileges on the first table to "invoker";
 
170
#   - grant only select privilege on the second table to "invoker";
 
171
#   - create a trigger, which inserts a row into the second table after
 
172
#     inserting into the first table.
 
173
#   - insert a row into the first table under "invoker". A row also should be
 
174
#     inserted into the second table.
 
175
#
 
176
 
 
177
--connect (wl2818_definer_con,localhost,mysqltest_dfn,,mysqltest_db1)
 
178
--connection wl2818_definer_con
 
179
--echo
 
180
--echo ---> connection: wl2818_definer_con
 
181
 
 
182
CREATE TRIGGER trg1 AFTER INSERT ON t1
 
183
  FOR EACH ROW
 
184
    INSERT INTO t2 VALUES(CURRENT_USER());
 
185
 
 
186
--connection default
 
187
--echo
 
188
--echo ---> connection: default
 
189
 
 
190
# Setup definer's privileges.
 
191
 
 
192
GRANT ALL PRIVILEGES ON mysqltest_db1.t1 TO mysqltest_dfn@localhost;
 
193
GRANT ALL PRIVILEGES ON mysqltest_db1.t2 TO mysqltest_dfn@localhost;
 
194
 
 
195
# Setup invoker's privileges.
 
196
 
 
197
GRANT ALL PRIVILEGES ON mysqltest_db1.t1
 
198
  TO 'mysqltest_inv'@localhost;
 
199
  
 
200
GRANT SELECT ON mysqltest_db1.t2
 
201
  TO 'mysqltest_inv'@localhost;
 
202
 
 
203
--connection wl2818_definer_con
 
204
--echo
 
205
--echo ---> connection: wl2818_definer_con
 
206
 
 
207
use mysqltest_db1;
 
208
 
 
209
INSERT INTO t1 VALUES(1);
 
210
 
 
211
SELECT * FROM t1;
 
212
SELECT * FROM t2;
 
213
 
 
214
--connect (wl2818_invoker_con,localhost,mysqltest_inv,,mysqltest_db1)
 
215
--connection wl2818_invoker_con
 
216
--echo
 
217
--echo ---> connection: wl2818_invoker_con
 
218
 
 
219
use mysqltest_db1;
 
220
 
 
221
INSERT INTO t1 VALUES(2);
 
222
 
 
223
SELECT * FROM t1;
 
224
SELECT * FROM t2;
 
225
 
 
226
#
 
227
# Check that if definer lost some privilege required to execute (activate) a
 
228
# trigger, the trigger will not be activated:
 
229
#  - create a trigger on insert into the first table, which will insert a row
 
230
#    into the second table;
 
231
#  - revoke INSERT privilege on the second table from the definer;
 
232
#  - insert a row into the first table;
 
233
#  - check that an error has been risen;
 
234
#  - check that no row has been inserted into the second table;
 
235
#
 
236
 
 
237
--connection default
 
238
--echo
 
239
--echo ---> connection: default
 
240
 
 
241
use mysqltest_db1;
 
242
 
 
243
REVOKE INSERT ON mysqltest_db1.t2 FROM mysqltest_dfn@localhost;
 
244
 
 
245
--connection wl2818_invoker_con
 
246
--echo
 
247
--echo ---> connection: wl2818_invoker_con
 
248
 
 
249
use mysqltest_db1;
 
250
 
 
251
--error ER_TABLEACCESS_DENIED_ERROR
 
252
INSERT INTO t1 VALUES(3);
 
253
 
 
254
SELECT * FROM t1;
 
255
SELECT * FROM t2;
 
256
 
 
257
#
 
258
# Check DEFINER clause of CREATE TRIGGER statement.
 
259
#
 
260
#   - Check that SUPER privilege required to create a trigger with different
 
261
#     definer:
 
262
#     - try to create a trigger with DEFINER="definer@localhost" under
 
263
#       "invoker";
 
264
#     - analyze error code;
 
265
#   - Check that if the user specified as DEFINER does not exist, a warning is
 
266
#     emitted:
 
267
#     - create a trigger with DEFINER="non_existent_user@localhost" from
 
268
#       "definer";
 
269
#     - check that a warning emitted;
 
270
#   - Check that the definer of a trigger does not exist, the trigger will not
 
271
#     be activated:
 
272
#     - activate just created trigger;
 
273
#     - check error code;
 
274
#
 
275
 
 
276
--connection wl2818_definer_con
 
277
--echo
 
278
--echo ---> connection: wl2818_definer_con
 
279
 
 
280
use mysqltest_db1;
 
281
 
 
282
DROP TRIGGER trg1;
 
283
 
 
284
# Check that SUPER is required to specify different DEFINER.
 
285
 
 
286
--error ER_SPECIFIC_ACCESS_DENIED_ERROR
 
287
CREATE DEFINER='mysqltest_inv'@'localhost'
 
288
  TRIGGER trg1 BEFORE INSERT ON t1
 
289
  FOR EACH ROW
 
290
    SET @new_sum = 0;
 
291
 
 
292
--connection default
 
293
--echo
 
294
--echo ---> connection: default
 
295
 
 
296
use mysqltest_db1;
 
297
 
 
298
GRANT SUPER ON *.* TO mysqltest_dfn@localhost;
 
299
 
 
300
--disconnect wl2818_definer_con
 
301
--connect (wl2818_definer_con,localhost,mysqltest_dfn,,mysqltest_db1)
 
302
--connection wl2818_definer_con
 
303
--echo
 
304
--echo ---> connection: wl2818_definer_con
 
305
 
 
306
CREATE DEFINER='mysqltest_inv'@'localhost'
 
307
  TRIGGER trg1 BEFORE INSERT ON t1
 
308
  FOR EACH ROW
 
309
    SET @new_sum = 0;
 
310
 
 
311
# Create with non-existent user.
 
312
 
 
313
CREATE DEFINER='mysqltest_nonexs'@'localhost'
 
314
  TRIGGER trg2 AFTER INSERT ON t1
 
315
  FOR EACH ROW
 
316
    SET @new_sum = 0;
 
317
 
 
318
# Check that trg2 will not be activated.
 
319
 
 
320
--error ER_NO_SUCH_USER
 
321
INSERT INTO t1 VALUES(6);
 
322
 
 
323
#
 
324
# Check that SHOW TRIGGERS statement provides "Definer" column.
 
325
#
 
326
 
 
327
SHOW TRIGGERS;
 
328
 
 
329
#
 
330
# Check that weird definer values do not break functionality. I.e. check the
 
331
# following definer values:
 
332
#   - '';
 
333
#   - '@';
 
334
#   - '@abc@def@@';
 
335
#   - '@hostname';
 
336
#   - '@abc@def@@@hostname';
 
337
#
 
338
 
 
339
DROP TRIGGER trg1;
 
340
DROP TRIGGER trg2;
 
341
 
 
342
CREATE TRIGGER trg1 BEFORE INSERT ON t1
 
343
  FOR EACH ROW
 
344
    SET @a = 1;
 
345
 
 
346
CREATE TRIGGER trg2 AFTER INSERT ON t1
 
347
  FOR EACH ROW
 
348
    SET @a = 2;
 
349
 
 
350
CREATE TRIGGER trg3 BEFORE UPDATE ON t1
 
351
  FOR EACH ROW
 
352
    SET @a = 3;
 
353
 
 
354
CREATE TRIGGER trg4 AFTER UPDATE ON t1
 
355
  FOR EACH ROW
 
356
    SET @a = 4;
 
357
 
 
358
CREATE TRIGGER trg5 BEFORE DELETE ON t1
 
359
  FOR EACH ROW
 
360
    SET @a = 5;
 
361
 
 
362
# Replace definers with the "weird" definers
 
363
let MYSQLD_DATADIR= `select @@datadir`;
 
364
perl;
 
365
use strict;
 
366
use warnings;
 
367
my $fname= "$ENV{'MYSQLD_DATADIR'}/mysqltest_db1/t1.TRG";
 
368
open(FILE, "<", $fname) or die;
 
369
my @content= grep($_ !~ /^definers=/, <FILE>);
 
370
close FILE;
 
371
open(FILE, ">", $fname) or die;
 
372
# Use binary file mode to avoid CR/LF's being added on windows
 
373
binmode FILE;
 
374
print FILE @content;
 
375
print FILE "definers='' '\@' '\@abc\@def\@\@' '\@hostname' '\@abcdef\@\@\@hostname'\n";
 
376
close FILE;
 
377
EOF
 
378
 
 
379
--echo
 
380
 
 
381
SELECT trigger_name, definer FROM INFORMATION_SCHEMA.TRIGGERS ORDER BY trigger_name;
 
382
 
 
383
--echo
 
384
 
 
385
SELECT * FROM INFORMATION_SCHEMA.TRIGGERS ORDER BY trigger_name;
 
386
 
 
387
#
 
388
# Cleanup
 
389
#
 
390
 
 
391
--connection default
 
392
--echo
 
393
--echo ---> connection: default
 
394
 
 
395
DROP USER mysqltest_dfn@localhost;
 
396
DROP USER mysqltest_inv@localhost;
 
397
 
 
398
DROP DATABASE mysqltest_db1;
 
399
 
 
400
 
 
401
###########################################################################
 
402
#
 
403
# BUG#15166: Wrong update [was: select/update] permissions required to execute
 
404
# triggers.
 
405
#
 
406
# BUG#15196: Wrong select permission required to execute triggers.
 
407
#
 
408
###########################################################################
 
409
 
 
410
#
 
411
# Prepare environment.
 
412
#
 
413
 
 
414
DELETE FROM mysql.user WHERE User LIKE 'mysqltest_%';
 
415
DELETE FROM mysql.db WHERE User LIKE 'mysqltest_%';
 
416
DELETE FROM mysql.tables_priv WHERE User LIKE 'mysqltest_%';
 
417
DELETE FROM mysql.columns_priv WHERE User LIKE 'mysqltest_%';
 
418
FLUSH PRIVILEGES;
 
419
 
 
420
--disable_warnings
 
421
DROP DATABASE IF EXISTS mysqltest_db1;
 
422
--enable_warnings
 
423
 
 
424
CREATE DATABASE mysqltest_db1;
 
425
 
 
426
use mysqltest_db1;
 
427
 
 
428
# Tables for tesing table-level privileges:
 
429
CREATE TABLE t1(col CHAR(20)); # table for "read-value" trigger
 
430
CREATE TABLE t2(col CHAR(20)); # table for "write-value" trigger
 
431
 
 
432
# Tables for tesing column-level privileges:
 
433
CREATE TABLE t3(col CHAR(20)); # table for "read-value" trigger
 
434
CREATE TABLE t4(col CHAR(20)); # table for "write-value" trigger
 
435
 
 
436
CREATE USER mysqltest_u1@localhost;
 
437
REVOKE ALL PRIVILEGES, GRANT OPTION FROM mysqltest_u1@localhost;
 
438
GRANT TRIGGER ON mysqltest_db1.* TO mysqltest_u1@localhost;
 
439
 
 
440
SET @mysqltest_var = NULL;
 
441
 
 
442
--connect (bug15166_u1_con,localhost,mysqltest_u1,,mysqltest_db1)
 
443
 
 
444
# parsing (CREATE TRIGGER) time:
 
445
#   - check that nor SELECT either UPDATE is required to execute triggger w/o
 
446
#     NEW/OLD variables.
 
447
 
 
448
--connection default
 
449
--echo
 
450
--echo ---> connection: default
 
451
 
 
452
use mysqltest_db1;
 
453
 
 
454
GRANT DELETE ON mysqltest_db1.* TO mysqltest_u1@localhost;
 
455
SHOW GRANTS FOR mysqltest_u1@localhost;
 
456
 
 
457
--connection bug15166_u1_con
 
458
--echo
 
459
--echo ---> connection: bug15166_u1_con
 
460
 
 
461
use mysqltest_db1;
 
462
 
 
463
CREATE TRIGGER t1_trg_after_delete AFTER DELETE ON t1
 
464
  FOR EACH ROW
 
465
    SET @mysqltest_var = 'Hello, world!';
 
466
 
 
467
# parsing (CREATE TRIGGER) time:
 
468
#   - check that UPDATE is not enough to read the value;
 
469
#   - check that UPDATE is required to modify the value;
 
470
 
 
471
--connection default
 
472
--echo
 
473
--echo ---> connection: default
 
474
 
 
475
use mysqltest_db1;
 
476
 
 
477
GRANT UPDATE ON mysqltest_db1.t1 TO mysqltest_u1@localhost;
 
478
GRANT UPDATE ON mysqltest_db1.t2 TO mysqltest_u1@localhost;
 
479
 
 
480
GRANT UPDATE(col) ON mysqltest_db1.t3 TO mysqltest_u1@localhost;
 
481
GRANT UPDATE(col) ON mysqltest_db1.t4 TO mysqltest_u1@localhost;
 
482
 
 
483
--connection bug15166_u1_con
 
484
--echo
 
485
--echo ---> connection: bug15166_u1_con
 
486
 
 
487
use mysqltest_db1;
 
488
 
 
489
# - table-level privileges
 
490
 
 
491
# TODO: check privileges at CREATE TRIGGER time.
 
492
# --error ER_COLUMNACCESS_DENIED_ERROR
 
493
CREATE TRIGGER t1_trg_err_1 BEFORE INSERT ON t1
 
494
  FOR EACH ROW
 
495
    SET @mysqltest_var = NEW.col;
 
496
DROP TRIGGER t1_trg_err_1;
 
497
 
 
498
# TODO: check privileges at CREATE TRIGGER time.
 
499
# --error ER_COLUMNACCESS_DENIED_ERROR
 
500
CREATE TRIGGER t1_trg_err_2 BEFORE DELETE ON t1
 
501
  FOR EACH ROW
 
502
    SET @mysqltest_var = OLD.col;
 
503
DROP TRIGGER t1_trg_err_2;
 
504
 
 
505
CREATE TRIGGER t2_trg_before_insert BEFORE INSERT ON t2
 
506
  FOR EACH ROW
 
507
    SET NEW.col = 't2_trg_before_insert';
 
508
 
 
509
# - column-level privileges
 
510
 
 
511
# TODO: check privileges at CREATE TRIGGER time.
 
512
# --error ER_COLUMNACCESS_DENIED_ERROR
 
513
CREATE TRIGGER t3_trg_err_1 BEFORE INSERT ON t3
 
514
  FOR EACH ROW
 
515
    SET @mysqltest_var = NEW.col;
 
516
DROP TRIGGER t3_trg_err_1;
 
517
 
 
518
# TODO: check privileges at CREATE TRIGGER time.
 
519
# --error ER_COLUMNACCESS_DENIED_ERROR
 
520
CREATE TRIGGER t3_trg_err_2 BEFORE DELETE ON t3
 
521
  FOR EACH ROW
 
522
    SET @mysqltest_var = OLD.col;
 
523
DROP TRIGGER t3_trg_err_2;
 
524
 
 
525
CREATE TRIGGER t4_trg_before_insert BEFORE INSERT ON t4
 
526
  FOR EACH ROW
 
527
    SET NEW.col = 't4_trg_before_insert';
 
528
 
 
529
# parsing (CREATE TRIGGER) time:
 
530
#   - check that SELECT is required to read the value;
 
531
#   - check that SELECT is not enough to modify the value;
 
532
 
 
533
--connection default
 
534
--echo
 
535
--echo ---> connection: default
 
536
 
 
537
use mysqltest_db1;
 
538
 
 
539
REVOKE UPDATE ON mysqltest_db1.t1 FROM mysqltest_u1@localhost;
 
540
REVOKE UPDATE ON mysqltest_db1.t2 FROM mysqltest_u1@localhost;
 
541
GRANT SELECT ON mysqltest_db1.t1 TO mysqltest_u1@localhost;
 
542
GRANT SELECT ON mysqltest_db1.t2 TO mysqltest_u1@localhost;
 
543
 
 
544
REVOKE UPDATE(col) ON mysqltest_db1.t3 FROM mysqltest_u1@localhost;
 
545
REVOKE UPDATE(col) ON mysqltest_db1.t4 FROM mysqltest_u1@localhost;
 
546
GRANT SELECT(col) on mysqltest_db1.t3 TO mysqltest_u1@localhost;
 
547
GRANT SELECT(col) on mysqltest_db1.t4 TO mysqltest_u1@localhost;
 
548
 
 
549
--connection bug15166_u1_con
 
550
--echo
 
551
--echo ---> connection: bug15166_u1_con
 
552
 
 
553
use mysqltest_db1;
 
554
 
 
555
# - table-level privileges
 
556
 
 
557
CREATE TRIGGER t1_trg_after_insert AFTER INSERT ON t1
 
558
 FOR EACH ROW
 
559
  SET @mysqltest_var = NEW.col;
 
560
 
 
561
CREATE TRIGGER t1_trg_after_update AFTER UPDATE ON t1
 
562
 FOR EACH ROW
 
563
  SET @mysqltest_var = OLD.col;
 
564
 
 
565
# TODO: check privileges at CREATE TRIGGER time.
 
566
# --error ER_COLUMNACCESS_DENIED_ERROR
 
567
CREATE TRIGGER t2_trg_err_1 BEFORE UPDATE ON t2
 
568
 FOR EACH ROW
 
569
  SET NEW.col = 't2_trg_err_1';
 
570
DROP TRIGGER t2_trg_err_1;
 
571
 
 
572
# TODO: check privileges at CREATE TRIGGER time.
 
573
# --error ER_COLUMNACCESS_DENIED_ERROR
 
574
CREATE TRIGGER t2_trg_err_2 BEFORE UPDATE ON t2
 
575
 FOR EACH ROW
 
576
  SET NEW.col = CONCAT(OLD.col, '(updated)');
 
577
DROP TRIGGER t2_trg_err_2;
 
578
 
 
579
# - column-level privileges
 
580
 
 
581
CREATE TRIGGER t3_trg_after_insert AFTER INSERT ON t3
 
582
  FOR EACH ROW
 
583
    SET @mysqltest_var = NEW.col;
 
584
 
 
585
CREATE TRIGGER t3_trg_after_update AFTER UPDATE ON t3
 
586
  FOR EACH ROW
 
587
    SET @mysqltest_var = OLD.col;
 
588
 
 
589
# TODO: check privileges at CREATE TRIGGER time.
 
590
# --error ER_COLUMNACCESS_DENIED_ERROR
 
591
CREATE TRIGGER t4_trg_err_1 BEFORE UPDATE ON t4
 
592
 FOR EACH ROW
 
593
  SET NEW.col = 't4_trg_err_1';
 
594
DROP TRIGGER t4_trg_err_1;
 
595
 
 
596
# TODO: check privileges at CREATE TRIGGER time.
 
597
# --error ER_COLUMNACCESS_DENIED_ERROR
 
598
CREATE TRIGGER t4_trg_err_2 BEFORE UPDATE ON t4
 
599
 FOR EACH ROW
 
600
  SET NEW.col = CONCAT(OLD.col, '(updated)');
 
601
DROP TRIGGER t4_trg_err_2;
 
602
 
 
603
# execution time:
 
604
#   - check that UPDATE is not enough to read the value;
 
605
#   - check that UPDATE is required to modify the value;
 
606
 
 
607
--connection default
 
608
--echo
 
609
--echo ---> connection: default
 
610
 
 
611
use mysqltest_db1;
 
612
 
 
613
REVOKE SELECT ON mysqltest_db1.t1 FROM mysqltest_u1@localhost;
 
614
REVOKE SELECT ON mysqltest_db1.t2 FROM mysqltest_u1@localhost;
 
615
GRANT UPDATE ON mysqltest_db1.t1 TO mysqltest_u1@localhost;
 
616
GRANT UPDATE ON mysqltest_db1.t2 TO mysqltest_u1@localhost;
 
617
 
 
618
REVOKE SELECT(col) ON mysqltest_db1.t3 FROM mysqltest_u1@localhost;
 
619
REVOKE SELECT(col) ON mysqltest_db1.t4 FROM mysqltest_u1@localhost;
 
620
GRANT UPDATE(col) ON mysqltest_db1.t3 TO mysqltest_u1@localhost;
 
621
GRANT UPDATE(col) ON mysqltest_db1.t4 TO mysqltest_u1@localhost;
 
622
 
 
623
# - table-level privileges
 
624
 
 
625
--error ER_COLUMNACCESS_DENIED_ERROR
 
626
INSERT INTO t1 VALUES('line1');
 
627
 
 
628
SELECT * FROM t1;
 
629
SELECT @mysqltest_var;
 
630
 
 
631
INSERT INTO t2 VALUES('line2');
 
632
 
 
633
SELECT * FROM t2;
 
634
 
 
635
# - column-level privileges
 
636
 
 
637
--error ER_COLUMNACCESS_DENIED_ERROR
 
638
INSERT INTO t3 VALUES('t3_line1');
 
639
 
 
640
SELECT * FROM t3;
 
641
SELECT @mysqltest_var;
 
642
 
 
643
INSERT INTO t4 VALUES('t4_line2');
 
644
 
 
645
SELECT * FROM t4;
 
646
 
 
647
# execution time:
 
648
#   - check that SELECT is required to read the value;
 
649
#   - check that SELECT is not enough to modify the value;
 
650
 
 
651
--connection default
 
652
--echo
 
653
--echo ---> connection: default
 
654
 
 
655
use mysqltest_db1;
 
656
 
 
657
REVOKE UPDATE ON mysqltest_db1.t1 FROM mysqltest_u1@localhost;
 
658
REVOKE UPDATE ON mysqltest_db1.t2 FROM mysqltest_u1@localhost;
 
659
GRANT SELECT ON mysqltest_db1.t1 TO mysqltest_u1@localhost;
 
660
GRANT SELECT ON mysqltest_db1.t2 TO mysqltest_u1@localhost;
 
661
 
 
662
REVOKE UPDATE(col) ON mysqltest_db1.t3 FROM mysqltest_u1@localhost;
 
663
REVOKE UPDATE(col) ON mysqltest_db1.t4 FROM mysqltest_u1@localhost;
 
664
GRANT SELECT(col) ON mysqltest_db1.t3 TO mysqltest_u1@localhost;
 
665
GRANT SELECT(col) ON mysqltest_db1.t4 TO mysqltest_u1@localhost;
 
666
 
 
667
# - table-level privileges
 
668
 
 
669
INSERT INTO t1 VALUES('line3');
 
670
 
 
671
SELECT * FROM t1;
 
672
SELECT @mysqltest_var;
 
673
 
 
674
--error ER_COLUMNACCESS_DENIED_ERROR
 
675
INSERT INTO t2 VALUES('line4');
 
676
 
 
677
SELECT * FROM t2;
 
678
 
 
679
# - column-level privileges
 
680
 
 
681
INSERT INTO t3 VALUES('t3_line2');
 
682
 
 
683
SELECT * FROM t3;
 
684
SELECT @mysqltest_var;
 
685
 
 
686
--error ER_COLUMNACCESS_DENIED_ERROR
 
687
INSERT INTO t4 VALUES('t4_line2');
 
688
 
 
689
SELECT * FROM t4;
 
690
 
 
691
# execution time:
 
692
#   - check that nor SELECT either UPDATE is required to execute triggger w/o
 
693
#     NEW/OLD variables.
 
694
 
 
695
DELETE FROM t1;
 
696
 
 
697
SELECT @mysqltest_var;
 
698
 
 
699
#
 
700
# Cleanup.
 
701
#
 
702
 
 
703
DROP USER mysqltest_u1@localhost;
 
704
 
 
705
DROP DATABASE mysqltest_db1;
 
706
 
 
707
 
 
708
#
 
709
# Test for bug #14635 Accept NEW.x as INOUT parameters to stored
 
710
# procedures from within triggers
 
711
#
 
712
# We require UPDATE privilege when NEW.x passed as OUT parameter, and
 
713
# SELECT and UPDATE when NEW.x passed as INOUT parameter.
 
714
#
 
715
DELETE FROM mysql.user WHERE User LIKE 'mysqltest_%';
 
716
DELETE FROM mysql.db WHERE User LIKE 'mysqltest_%';
 
717
DELETE FROM mysql.tables_priv WHERE User LIKE 'mysqltest_%';
 
718
DELETE FROM mysql.columns_priv WHERE User LIKE 'mysqltest_%';
 
719
FLUSH PRIVILEGES;
 
720
 
 
721
--disable_warnings
 
722
DROP DATABASE IF EXISTS mysqltest_db1;
 
723
--enable_warnings
 
724
 
 
725
CREATE DATABASE mysqltest_db1;
 
726
USE mysqltest_db1;
 
727
 
 
728
CREATE TABLE t1 (i1 INT);
 
729
CREATE TABLE t2 (i1 INT);
 
730
 
 
731
CREATE USER mysqltest_dfn@localhost;
 
732
CREATE USER mysqltest_inv@localhost;
 
733
 
 
734
GRANT EXECUTE, CREATE ROUTINE, TRIGGER ON *.* TO mysqltest_dfn@localhost;
 
735
GRANT INSERT ON mysqltest_db1.* TO mysqltest_inv@localhost;
 
736
 
 
737
connect (definer,localhost,mysqltest_dfn,,mysqltest_db1);
 
738
connect (invoker,localhost,mysqltest_inv,,mysqltest_db1);
 
739
 
 
740
connection definer;
 
741
CREATE PROCEDURE p1(OUT i INT) DETERMINISTIC NO SQL SET i = 3;
 
742
CREATE PROCEDURE p2(INOUT i INT) DETERMINISTIC NO SQL SET i = i * 5;
 
743
 
 
744
# Check that having no privilege won't work.
 
745
connection definer;
 
746
CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW
 
747
  CALL p1(NEW.i1);
 
748
CREATE TRIGGER t2_bi BEFORE INSERT ON t2 FOR EACH ROW
 
749
  CALL p2(NEW.i1);
 
750
 
 
751
connection invoker;
 
752
--error ER_COLUMNACCESS_DENIED_ERROR
 
753
INSERT INTO t1 VALUES (7);
 
754
--error ER_COLUMNACCESS_DENIED_ERROR
 
755
INSERT INTO t2 VALUES (11);
 
756
 
 
757
connection definer;
 
758
DROP TRIGGER t2_bi;
 
759
DROP TRIGGER t1_bi;
 
760
 
 
761
# Check that having only SELECT privilege is not enough.
 
762
connection default;
 
763
GRANT SELECT ON mysqltest_db1.* TO mysqltest_dfn@localhost;
 
764
 
 
765
connection definer;
 
766
CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW
 
767
  CALL p1(NEW.i1);
 
768
CREATE TRIGGER t2_bi BEFORE INSERT ON t2 FOR EACH ROW
 
769
  CALL p2(NEW.i1);
 
770
 
 
771
connection invoker;
 
772
--error ER_COLUMNACCESS_DENIED_ERROR
 
773
INSERT INTO t1 VALUES (13);
 
774
--error ER_COLUMNACCESS_DENIED_ERROR
 
775
INSERT INTO t2 VALUES (17);
 
776
 
 
777
connection default;
 
778
REVOKE SELECT ON mysqltest_db1.* FROM mysqltest_dfn@localhost;
 
779
 
 
780
connection definer;
 
781
DROP TRIGGER t2_bi;
 
782
DROP TRIGGER t1_bi;
 
783
 
 
784
# Check that having only UPDATE privilege is enough for OUT parameter,
 
785
# but not for INOUT parameter.
 
786
connection default;
 
787
GRANT UPDATE ON mysqltest_db1.* TO mysqltest_dfn@localhost;
 
788
 
 
789
connection definer;
 
790
CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW
 
791
  CALL p1(NEW.i1);
 
792
CREATE TRIGGER t2_bi BEFORE INSERT ON t2 FOR EACH ROW
 
793
  CALL p2(NEW.i1);
 
794
 
 
795
connection invoker;
 
796
INSERT INTO t1 VALUES (19);
 
797
--error ER_COLUMNACCESS_DENIED_ERROR
 
798
INSERT INTO t2 VALUES (23);
 
799
 
 
800
connection default;
 
801
REVOKE UPDATE ON mysqltest_db1.* FROM mysqltest_dfn@localhost;
 
802
 
 
803
connection definer;
 
804
DROP TRIGGER t2_bi;
 
805
DROP TRIGGER t1_bi;
 
806
 
 
807
# Check that having SELECT and UPDATE privileges is enough.
 
808
connection default;
 
809
GRANT SELECT, UPDATE ON mysqltest_db1.* TO mysqltest_dfn@localhost;
 
810
 
 
811
connection definer;
 
812
CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW
 
813
  CALL p1(NEW.i1);
 
814
CREATE TRIGGER t2_bi BEFORE INSERT ON t2 FOR EACH ROW
 
815
  CALL p2(NEW.i1);
 
816
 
 
817
connection invoker;
 
818
INSERT INTO t1 VALUES (29);
 
819
INSERT INTO t2 VALUES (31);
 
820
 
 
821
connection default;
 
822
REVOKE SELECT, UPDATE ON mysqltest_db1.* FROM mysqltest_dfn@localhost;
 
823
 
 
824
connection definer;
 
825
DROP TRIGGER t2_bi;
 
826
DROP TRIGGER t1_bi;
 
827
 
 
828
connection default;
 
829
DROP PROCEDURE p2;
 
830
DROP PROCEDURE p1;
 
831
 
 
832
# Check that late procedure redefining won't open a security hole.
 
833
connection default;
 
834
GRANT UPDATE ON mysqltest_db1.* TO mysqltest_dfn@localhost;
 
835
 
 
836
connection definer;
 
837
CREATE PROCEDURE p1(OUT i INT) DETERMINISTIC NO SQL SET i = 37;
 
838
CREATE TRIGGER t1_bi BEFORE INSERT ON t1 FOR EACH ROW
 
839
  CALL p1(NEW.i1);
 
840
 
 
841
connection invoker;
 
842
INSERT INTO t1 VALUES (41);
 
843
 
 
844
connection definer;
 
845
DROP PROCEDURE p1;
 
846
CREATE PROCEDURE p1(IN i INT) DETERMINISTIC NO SQL SET @v1 = i + 43;
 
847
 
 
848
connection invoker;
 
849
--error ER_COLUMNACCESS_DENIED_ERROR
 
850
INSERT INTO t1 VALUES (47);
 
851
 
 
852
connection definer;
 
853
DROP PROCEDURE p1;
 
854
CREATE PROCEDURE p1(INOUT i INT) DETERMINISTIC NO SQL SET i = i + 51;
 
855
 
 
856
connection invoker;
 
857
--error ER_COLUMNACCESS_DENIED_ERROR
 
858
INSERT INTO t1 VALUES (53);
 
859
 
 
860
connection default;
 
861
DROP PROCEDURE p1;
 
862
REVOKE UPDATE ON mysqltest_db1.* FROM mysqltest_dfn@localhost;
 
863
 
 
864
connection definer;
 
865
DROP TRIGGER t1_bi;
 
866
 
 
867
# Cleanup.
 
868
disconnect definer;
 
869
disconnect invoker;
 
870
connection default;
 
871
DROP USER mysqltest_inv@localhost;
 
872
DROP USER mysqltest_dfn@localhost;
 
873
DROP TABLE t2;
 
874
DROP TABLE t1;
 
875
DROP DATABASE mysqltest_db1;
 
876
USE test;
 
877
 
 
878
--echo End of 5.0 tests.
 
879
 
 
880
#
 
881
# Bug#23713 LOCK TABLES + CREATE TRIGGER + FLUSH TABLES WITH READ LOCK = deadlock
 
882
#
 
883
 
 
884
--disable_warnings
 
885
drop table if exists t1;
 
886
--enable_warnings
 
887
create table t1 (i int);
 
888
connect (flush,localhost,root,,test,,);
 
889
connection default;
 
890
--echo connection: default
 
891
lock tables t1 write;
 
892
connection flush;
 
893
--echo connection: flush
 
894
--send flush tables with read lock;
 
895
connection default;
 
896
--echo connection: default
 
897
let $wait_condition=
 
898
  select count(*) = 1 from information_schema.processlist
 
899
  where state = "Flushing tables";
 
900
--source include/wait_condition.inc
 
901
create trigger t1_bi before insert on t1 for each row begin end;
 
902
unlock tables;
 
903
connection flush;
 
904
--echo connection: flush
 
905
--reap
 
906
unlock tables;
 
907
connection default;
 
908
select * from t1;
 
909
drop table t1;
 
910
disconnect flush;
 
911
 
 
912
#
 
913
# Bug#45412 SHOW CREATE TRIGGER does not require privileges to disclose trigger data
 
914
#
 
915
CREATE DATABASE db1;
 
916
CREATE TABLE db1.t1 (a char(30)) ENGINE=MEMORY;
 
917
CREATE TRIGGER db1.trg AFTER INSERT ON db1.t1 FOR EACH ROW
 
918
 INSERT INTO db1.t1 VALUES('Some very sensitive data goes here');
 
919
 
 
920
CREATE USER 'no_rights'@'localhost';
 
921
REVOKE ALL ON *.* FROM 'no_rights'@'localhost';
 
922
FLUSH PRIVILEGES;
 
923
 
 
924
connect (con1,localhost,no_rights,,);
 
925
SELECT trigger_name FROM INFORMATION_SCHEMA.TRIGGERS
 
926
 WHERE trigger_schema = 'db1';
 
927
--error ER_SPECIFIC_ACCESS_DENIED_ERROR
 
928
SHOW CREATE TRIGGER db1.trg;
 
929
 
 
930
connection default;
 
931
disconnect con1;
 
932
DROP USER 'no_rights'@'localhost';
 
933
DROP DATABASE db1;
 
934
 
 
935
--echo End of 5.1 tests.