Commit c88cb0b7 authored by Christian Gonzalez's avatar Christian Gonzalez Committed by Sergei Golubchik

Make SESSION_USER() comparable with CURRENT_USER()

Update `SESSION_USER()` behaviour to be comparable with `CURRENT_USER()`.
`SESSION_USER()` will return the user and host columns from `mysql.user`
used to authenticate the user when the session was created.

Historically `SESSION_USER()` was an alias of `USER()` function. The
main difference with `USER()` behaviour after this changes is that
`SESSION_USER()` now returns the host column from `mysql.user` instead of
the client host or ip.

NOTE: `SESSION_USER_IS_USER` old mode is added to make the change
backward compatible.

All new code of the whole pull request, including one or several files
that are either new files or modified ones, are contributed under the
BSD-new license. I am contributing on behalf of my employer
Amazon Web Services, Inc.
parent 4870d740
...@@ -769,7 +769,8 @@ The following specify which files/extra groups are read (specified before remain ...@@ -769,7 +769,8 @@ The following specify which files/extra groups are read (specified before remain
ZERO_DATE_TIME_CAST, UTF8_IS_UTF8MB3, ZERO_DATE_TIME_CAST, UTF8_IS_UTF8MB3,
IGNORE_INDEX_ONLY_FOR_JOIN, COMPAT_5_1_CHECKSUM, IGNORE_INDEX_ONLY_FOR_JOIN, COMPAT_5_1_CHECKSUM,
NO_NULL_COLLATION_IDS, LOCK_ALTER_TABLE_COPY, NO_NULL_COLLATION_IDS, LOCK_ALTER_TABLE_COPY,
OLD_FLUSH_STATUS, or ALL to set all combinations OLD_FLUSH_STATUS, SESSION_USER_IS_USER, or ALL to set all
combinations
--old-passwords Use old password encryption method (needed for 4.0 and --old-passwords Use old password encryption method (needed for 4.0 and
older clients) older clients)
--old-style-user-limits --old-style-user-limits
......
# Create a stored procedure which calls the different functions to retrieve the user
CREATE FUNCTION test.func_session_user() RETURNS CHAR(80) SQL SECURITY DEFINER RETURN CONCAT(USER(), '; ', CURRENT_USER(), '; ', SESSION_USER());
# Create a view which calls the different functions to retrieve the user
CREATE SQL SECURITY DEFINER VIEW test.view_session_user AS SELECT USER(), CURRENT_USER(), SESSION_USER();
# Create test_user
CREATE USER 'test_user'@'%';
GRANT EXECUTE, CREATE, DROP, SELECT ON test.* TO 'test_user'@'%';
# Connect as test_user
connect test_user_con,localhost,test_user,,;
# Check the function called directly
SELECT USER(), CURRENT_USER(), SESSION_USER();
USER() CURRENT_USER() SESSION_USER()
test_user@localhost test_user@% test_user@%
# Check the function inside of a stored procedure
SELECT test.func_session_user();
test.func_session_user()
test_user@localhost; root@localhost; test_user@%
# Check the function inside of a view
SELECT * FROM test.view_session_user;
USER() CURRENT_USER() SESSION_USER()
test_user@localhost root@localhost test_user@%
# Check SESSION_USER is allowed in virtual columns
CREATE OR REPLACE TABLE t1 (a int, b varchar(100) GENERATED ALWAYS AS (SESSION_USER()));
# Check SESSION_USER is not allowed for indexed virtual columns
CREATE OR REPLACE TABLE t1 (a int, b varchar(100) GENERATED ALWAYS AS (SESSION_USER()), INDEX idx (b));
ERROR HY000: Function or expression 'session_user()' cannot be used in the GENERATED ALWAYS AS clause of `b`
# Check SESSION_USER is not allowed in virtual columns when CHECK constrains are provided
CREATE OR REPLACE TABLE t1 (a int, b varchar(100) GENERATED ALWAYS AS (SESSION_USER()) CHECK (b <> ''));
ERROR HY000: Function or expression 'b' cannot be used in the CHECK clause of `b`
# Check SESSION_USER is not allowed for stored virtual columns
CREATE OR REPLACE TABLE t1 (a int, b varchar(100) GENERATED ALWAYS AS (SESSION_USER()) STORED);
ERROR HY000: Function or expression 'session_user()' cannot be used in the GENERATED ALWAYS AS clause of `b`
# Check SESSION_USER is allowed as default
CREATE OR REPLACE TABLE t1 (a int, b varchar(100) DEFAULT (SESSION_USER()));
# Test old mode SESSION_USER_IS_USER behaves properly
SELECT @@OLD_MODE, @@GLOBAL.OLD_MODE;
@@OLD_MODE @@GLOBAL.OLD_MODE
UTF8_IS_UTF8MB3 UTF8_IS_UTF8MB3
SELECT USER(), SESSION_USER();
USER() SESSION_USER()
test_user@localhost test_user@%
SET @@OLD_MODE = CONCAT(@@OLD_MODE, ',SESSION_USER_IS_USER');
Warnings:
Warning 1287 'SESSION_USER_IS_USER' is deprecated and will be removed in a future release
SELECT @@OLD_MODE, @@GLOBAL.OLD_MODE;
@@OLD_MODE @@GLOBAL.OLD_MODE
UTF8_IS_UTF8MB3,SESSION_USER_IS_USER UTF8_IS_UTF8MB3
SELECT USER(), SESSION_USER();
USER() SESSION_USER()
test_user@localhost test_user@localhost
# Cleanup
connection default;
DROP USER 'test_user'@'%';
DROP FUNCTION test.func_session_user;
DROP VIEW test.view_session_user;
DROP TABLE test.t1;
# MDEV-30908 - Test SESSION_USER() function
#
# Check that SESSION_USER() returns the right user and host matching the
# content of`mysql.user` table for the user executing the stored procedure.
-- source include/not_embedded.inc
--echo # Create a stored procedure which calls the different functions to retrieve the user
CREATE FUNCTION test.func_session_user() RETURNS CHAR(80) SQL SECURITY DEFINER RETURN CONCAT(USER(), '; ', CURRENT_USER(), '; ', SESSION_USER());
--echo # Create a view which calls the different functions to retrieve the user
CREATE SQL SECURITY DEFINER VIEW test.view_session_user AS SELECT USER(), CURRENT_USER(), SESSION_USER();
--echo # Create test_user
CREATE USER 'test_user'@'%';
GRANT EXECUTE, CREATE, DROP, SELECT ON test.* TO 'test_user'@'%';
--echo # Connect as test_user
connect (test_user_con,localhost,test_user,,);
--echo # Check the function called directly
SELECT USER(), CURRENT_USER(), SESSION_USER();
--echo # Check the function inside of a stored procedure
SELECT test.func_session_user();
--echo # Check the function inside of a view
SELECT * FROM test.view_session_user;
--echo # Check SESSION_USER is allowed in virtual columns
CREATE OR REPLACE TABLE t1 (a int, b varchar(100) GENERATED ALWAYS AS (SESSION_USER()));
--echo # Check SESSION_USER is not allowed for indexed virtual columns
--error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED
CREATE OR REPLACE TABLE t1 (a int, b varchar(100) GENERATED ALWAYS AS (SESSION_USER()), INDEX idx (b));
--echo # Check SESSION_USER is not allowed in virtual columns when CHECK constrains are provided
--error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED
CREATE OR REPLACE TABLE t1 (a int, b varchar(100) GENERATED ALWAYS AS (SESSION_USER()) CHECK (b <> ''));
--echo # Check SESSION_USER is not allowed for stored virtual columns
--error ER_GENERATED_COLUMN_FUNCTION_IS_NOT_ALLOWED
CREATE OR REPLACE TABLE t1 (a int, b varchar(100) GENERATED ALWAYS AS (SESSION_USER()) STORED);
--echo # Check SESSION_USER is allowed as default
CREATE OR REPLACE TABLE t1 (a int, b varchar(100) DEFAULT (SESSION_USER()));
--echo # Test old mode SESSION_USER_IS_USER behaves properly
SELECT @@OLD_MODE, @@GLOBAL.OLD_MODE;
SELECT USER(), SESSION_USER();
SET @@OLD_MODE = CONCAT(@@OLD_MODE, ',SESSION_USER_IS_USER');
SELECT @@OLD_MODE, @@GLOBAL.OLD_MODE;
SELECT USER(), SESSION_USER();
--echo # Cleanup
--connection default
DROP USER 'test_user'@'%';
DROP FUNCTION test.func_session_user;
DROP VIEW test.view_session_user;
DROP TABLE test.t1;
...@@ -2399,7 +2399,7 @@ VARIABLE_COMMENT Used to emulate old behavior from earlier MariaDB or MySQL vers ...@@ -2399,7 +2399,7 @@ VARIABLE_COMMENT Used to emulate old behavior from earlier MariaDB or MySQL vers
NUMERIC_MIN_VALUE NULL NUMERIC_MIN_VALUE NULL
NUMERIC_MAX_VALUE NULL NUMERIC_MAX_VALUE NULL
NUMERIC_BLOCK_SIZE NULL NUMERIC_BLOCK_SIZE NULL
ENUM_VALUE_LIST NO_DUP_KEY_WARNINGS_WITH_IGNORE,NO_PROGRESS_INFO,ZERO_DATE_TIME_CAST,UTF8_IS_UTF8MB3,IGNORE_INDEX_ONLY_FOR_JOIN,COMPAT_5_1_CHECKSUM,NO_NULL_COLLATION_IDS,LOCK_ALTER_TABLE_COPY,OLD_FLUSH_STATUS ENUM_VALUE_LIST NO_DUP_KEY_WARNINGS_WITH_IGNORE,NO_PROGRESS_INFO,ZERO_DATE_TIME_CAST,UTF8_IS_UTF8MB3,IGNORE_INDEX_ONLY_FOR_JOIN,COMPAT_5_1_CHECKSUM,NO_NULL_COLLATION_IDS,LOCK_ALTER_TABLE_COPY,OLD_FLUSH_STATUS,SESSION_USER_IS_USER
READ_ONLY NO READ_ONLY NO
COMMAND_LINE_ARGUMENT REQUIRED COMMAND_LINE_ARGUMENT REQUIRED
VARIABLE_NAME OLD_PASSWORDS VARIABLE_NAME OLD_PASSWORDS
......
...@@ -2609,7 +2609,7 @@ VARIABLE_COMMENT Used to emulate old behavior from earlier MariaDB or MySQL vers ...@@ -2609,7 +2609,7 @@ VARIABLE_COMMENT Used to emulate old behavior from earlier MariaDB or MySQL vers
NUMERIC_MIN_VALUE NULL NUMERIC_MIN_VALUE NULL
NUMERIC_MAX_VALUE NULL NUMERIC_MAX_VALUE NULL
NUMERIC_BLOCK_SIZE NULL NUMERIC_BLOCK_SIZE NULL
ENUM_VALUE_LIST NO_DUP_KEY_WARNINGS_WITH_IGNORE,NO_PROGRESS_INFO,ZERO_DATE_TIME_CAST,UTF8_IS_UTF8MB3,IGNORE_INDEX_ONLY_FOR_JOIN,COMPAT_5_1_CHECKSUM,NO_NULL_COLLATION_IDS,LOCK_ALTER_TABLE_COPY,OLD_FLUSH_STATUS ENUM_VALUE_LIST NO_DUP_KEY_WARNINGS_WITH_IGNORE,NO_PROGRESS_INFO,ZERO_DATE_TIME_CAST,UTF8_IS_UTF8MB3,IGNORE_INDEX_ONLY_FOR_JOIN,COMPAT_5_1_CHECKSUM,NO_NULL_COLLATION_IDS,LOCK_ALTER_TABLE_COPY,OLD_FLUSH_STATUS,SESSION_USER_IS_USER
READ_ONLY NO READ_ONLY NO
COMMAND_LINE_ARGUMENT REQUIRED COMMAND_LINE_ARGUMENT REQUIRED
VARIABLE_NAME OLD_PASSWORDS VARIABLE_NAME OLD_PASSWORDS
......
...@@ -119,7 +119,7 @@ create or replace table t1 (a varchar(32) as (schema()) PERSISTENT); ...@@ -119,7 +119,7 @@ create or replace table t1 (a varchar(32) as (schema()) PERSISTENT);
ERROR HY000: Function or expression 'database()' cannot be used in the GENERATED ALWAYS AS clause of `a` ERROR HY000: Function or expression 'database()' cannot be used in the GENERATED ALWAYS AS clause of `a`
# SESSION_USER() # SESSION_USER()
create or replace table t1 (a varchar(32) as (session_user()) PERSISTENT); create or replace table t1 (a varchar(32) as (session_user()) PERSISTENT);
ERROR HY000: Function or expression 'user()' cannot be used in the GENERATED ALWAYS AS clause of `a` ERROR HY000: Function or expression 'session_user()' cannot be used in the GENERATED ALWAYS AS clause of `a`
# SLEEP() # SLEEP()
create or replace table t1 (a int, b int as (sleep(a))); create or replace table t1 (a int, b int as (sleep(a)));
ERROR HY000: Function or expression 'sleep()' cannot be used in the GENERATED ALWAYS AS clause of `b` ERROR HY000: Function or expression 'sleep()' cannot be used in the GENERATED ALWAYS AS clause of `b`
......
...@@ -2925,6 +2925,17 @@ bool Item_func_current_user::fix_fields(THD *thd, Item **ref) ...@@ -2925,6 +2925,17 @@ bool Item_func_current_user::fix_fields(THD *thd, Item **ref)
return init(ctx->priv_user, ctx->priv_host); return init(ctx->priv_user, ctx->priv_host);
} }
bool Item_func_session_user::fix_fields(THD *thd, Item **ref)
{
if (thd->variables.old_behavior & OLD_MODE_SESSION_USER_IS_USER)
return Item_func_user::fix_fields(thd, ref);
if (Item_func_sysconst::fix_fields(thd, ref))
return TRUE;
return init(thd->main_security_ctx.priv_user, thd->main_security_ctx.priv_host);
}
bool Item_func_current_role::fix_fields(THD *thd, Item **ref) bool Item_func_current_role::fix_fields(THD *thd, Item **ref)
{ {
if (Item_func_sysconst::fix_fields(thd, ref)) if (Item_func_sysconst::fix_fields(thd, ref))
......
...@@ -1279,6 +1279,23 @@ class Item_func_current_user :public Item_func_user ...@@ -1279,6 +1279,23 @@ class Item_func_current_user :public Item_func_user
} }
}; };
class Item_func_session_user :public Item_func_user
{
public:
Item_func_session_user(THD *thd):
Item_func_user(thd) {}
bool fix_fields(THD *thd, Item **ref) override;
LEX_CSTRING func_name_cstring() const override
{
static LEX_CSTRING name= {STRING_WITH_LEN("session_user") };
return name;
}
const Lex_ident_routine fully_qualified_func_name() const override
{ return Lex_ident_routine("session_user()"_LEX_CSTRING); }
Item *do_get_copy(THD *thd) const override
{ return get_item_copy<Item_func_session_user>(thd, this); }
};
class Item_func_current_role :public Item_func_sysconst class Item_func_current_role :public Item_func_sysconst
{ {
......
...@@ -783,7 +783,7 @@ SYMBOL sql_functions[] = { ...@@ -783,7 +783,7 @@ SYMBOL sql_functions[] = {
{ "PERCENTILE_CONT", SYM(PERCENTILE_CONT_SYM)}, { "PERCENTILE_CONT", SYM(PERCENTILE_CONT_SYM)},
{ "PERCENTILE_DISC", SYM(PERCENTILE_DISC_SYM)}, { "PERCENTILE_DISC", SYM(PERCENTILE_DISC_SYM)},
{ "RANK", SYM(RANK_SYM)}, { "RANK", SYM(RANK_SYM)},
{ "SESSION_USER", SYM(USER_SYM)}, { "SESSION_USER", SYM(SESSION_USER_SYM)},
{ "STD", SYM(STD_SYM)}, { "STD", SYM(STD_SYM)},
{ "STDDEV", SYM(STD_SYM)}, { "STDDEV", SYM(STD_SYM)},
{ "STDDEV_POP", SYM(STD_SYM)}, { "STDDEV_POP", SYM(STD_SYM)},
......
...@@ -208,6 +208,7 @@ enum enum_binlog_row_image { ...@@ -208,6 +208,7 @@ enum enum_binlog_row_image {
#define OLD_MODE_NO_NULL_COLLATION_IDS (1 << 6) #define OLD_MODE_NO_NULL_COLLATION_IDS (1 << 6)
#define OLD_MODE_LOCK_ALTER_TABLE_COPY (1 << 7) #define OLD_MODE_LOCK_ALTER_TABLE_COPY (1 << 7)
#define OLD_MODE_OLD_FLUSH_STATUS (1 << 8) #define OLD_MODE_OLD_FLUSH_STATUS (1 << 8)
#define OLD_MODE_SESSION_USER_IS_USER (1 << 9)
#define OLD_MODE_DEFAULT_VALUE OLD_MODE_UTF8_IS_UTF8MB3 #define OLD_MODE_DEFAULT_VALUE OLD_MODE_UTF8_IS_UTF8MB3
......
...@@ -1078,6 +1078,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, size_t *yystacksize); ...@@ -1078,6 +1078,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b, size_t *yystacksize);
%token <kwd> SERIALIZABLE_SYM /* SQL-2003-N */ %token <kwd> SERIALIZABLE_SYM /* SQL-2003-N */
%token <kwd> SERIAL_SYM %token <kwd> SERIAL_SYM
%token <kwd> SESSION_SYM /* SQL-2003-N */ %token <kwd> SESSION_SYM /* SQL-2003-N */
%token <kwd> SESSION_USER_SYM /* SQL-2003-R */
%token <kwd> SERVER_SYM %token <kwd> SERVER_SYM
%token <kwd> SETVAL_SYM /* PostgreSQL sequence function */ %token <kwd> SETVAL_SYM /* PostgreSQL sequence function */
%token <kwd> SHARE_SYM %token <kwd> SHARE_SYM
...@@ -10277,6 +10278,14 @@ function_call_keyword: ...@@ -10277,6 +10278,14 @@ function_call_keyword:
Lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_SYSTEM_FUNCTION); Lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_SYSTEM_FUNCTION);
Lex->safe_to_cache_query= 0; Lex->safe_to_cache_query= 0;
} }
| SESSION_USER_SYM '(' ')'
{
$$= new (thd->mem_root) Item_func_session_user(thd);
if (unlikely($$ == NULL))
MYSQL_YYABORT;
Lex->set_stmt_unsafe(LEX::BINLOG_STMT_UNSAFE_SYSTEM_FUNCTION);
Lex->safe_to_cache_query=0;
}
| TIME_SYM '(' expr ')' | TIME_SYM '(' expr ')'
{ {
$$= new (thd->mem_root) Item_time_typecast(thd, $3, $$= new (thd->mem_root) Item_time_typecast(thd, $3,
...@@ -16468,6 +16477,7 @@ keyword_sp_var_and_label: ...@@ -16468,6 +16477,7 @@ keyword_sp_var_and_label:
| USER_SYM %prec PREC_BELOW_CONTRACTION_TOKEN2 | USER_SYM %prec PREC_BELOW_CONTRACTION_TOKEN2
| VALUE_SYM | VALUE_SYM
| WEIGHT_STRING_SYM | WEIGHT_STRING_SYM
| SESSION_USER_SYM
; ;
......
...@@ -3999,6 +3999,7 @@ static const char *old_mode_names[]= ...@@ -3999,6 +3999,7 @@ static const char *old_mode_names[]=
"NO_NULL_COLLATION_IDS", // 6: deprecated since 11.3 "NO_NULL_COLLATION_IDS", // 6: deprecated since 11.3
"LOCK_ALTER_TABLE_COPY", // 7: deprecated since 11.3 "LOCK_ALTER_TABLE_COPY", // 7: deprecated since 11.3
"OLD_FLUSH_STATUS", // 8: deprecated since 11.5 "OLD_FLUSH_STATUS", // 8: deprecated since 11.5
"SESSION_USER_IS_USER", // 9: deprecated since 11.7
0 0
}; };
...@@ -4021,6 +4022,9 @@ static bool old_mode_deprecated(sys_var *self, THD *thd, set_var *var) ...@@ -4021,6 +4022,9 @@ static bool old_mode_deprecated(sys_var *self, THD *thd, set_var *var)
for (; i <= 8; i++) for (; i <= 8; i++)
if ((1ULL<<i) & v) if ((1ULL<<i) & v)
warn_deprecated<1105>(thd, old_mode_names[i]); warn_deprecated<1105>(thd, old_mode_names[i]);
for (; i <= 9; i++)
if ((1ULL<<i) & v)
warn_deprecated<1107>(thd, old_mode_names[i]);
return false; return false;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment