Skip to content

Commit 0f4a35a

Browse files
committed
cleanup: extract reusable code chunks
move user_name parser rule out of user_maybe_role extract setting privileges on login from acl_authenticate() into a separate function
1 parent 78d23a3 commit 0f4a35a

File tree

2 files changed

+69
-79
lines changed

2 files changed

+69
-79
lines changed

sql/sql_acl.cc

Lines changed: 51 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12341,8 +12341,57 @@ inline privilege_t public_access()
1234112341
privilege_t get_column_grant(THD *, GRANT_INFO *, const char *, const char *,
1234212342
const Lex_ident_column &)
1234312343
{ return ALL_KNOWN_ACL; }
12344+
int acl_check_setrole(THD *, const LEX_CSTRING &, privilege_t *) { return 0; }
12345+
int acl_setrole(THD *, const LEX_CSTRING &, privilege_t) { return 0; }
1234412346
#endif /*NO_EMBEDDED_ACCESS_CHECKS */
1234512347

12348+
static int set_privs_on_login(THD *thd, const ACL_USER *acl_user)
12349+
{
12350+
Security_context *sctx= thd->security_ctx;
12351+
strmake_buf(sctx->priv_user, acl_user->user.str);
12352+
12353+
if (acl_user->host.hostname)
12354+
strmake_buf(sctx->priv_host, acl_user->host.hostname);
12355+
12356+
sctx->master_access= acl_user->access | public_access();
12357+
12358+
if (acl_user->default_rolename.length)
12359+
{
12360+
privilege_t access(NO_ACL);
12361+
int result= acl_check_setrole(thd, acl_user->default_rolename, &access);
12362+
if (!result)
12363+
result= acl_setrole(thd, acl_user->default_rolename, access);
12364+
thd->clear_error();
12365+
}
12366+
12367+
/*
12368+
Don't allow the user to connect if he has done too many queries.
12369+
As we are testing max_user_connections == 0 here, it means that we
12370+
can't let the user change max_user_connections from 0 in the server
12371+
without a restart as it would lead to wrong connect counting.
12372+
*/
12373+
if ((acl_user->user_resource.questions ||
12374+
acl_user->user_resource.updates ||
12375+
acl_user->user_resource.conn_per_hour ||
12376+
acl_user->user_resource.user_conn ||
12377+
acl_user->user_resource.max_statement_time != 0.0 ||
12378+
max_user_connections_checking) &&
12379+
get_or_create_user_conn(thd,
12380+
(opt_old_style_user_limits ? sctx->user : sctx->priv_user),
12381+
(opt_old_style_user_limits ? sctx->host_or_ip : sctx->priv_host),
12382+
&acl_user->user_resource))
12383+
return 1; // The error is set by get_or_create_user_conn()
12384+
12385+
if (acl_user->user_resource.max_statement_time != 0.0)
12386+
{
12387+
thd->variables.max_statement_time_double=
12388+
acl_user->user_resource.max_statement_time;
12389+
thd->variables.max_statement_time=
12390+
(ulonglong) (thd->variables.max_statement_time_double * 1e6 + 0.1);
12391+
}
12392+
return 0;
12393+
}
12394+
1234612395

1234712396
#ifdef NO_EMBEDDED_ACCESS_CHECKS
1234812397

@@ -14992,40 +15041,8 @@ bool acl_authenticate(THD *thd, uint com_change_user_pkt_len)
1499215041
}
1499315042
#endif
1499415043

14995-
sctx->master_access= (acl_user->access | public_access());
14996-
strmake_buf(sctx->priv_user, acl_user->user.str);
14997-
14998-
if (acl_user->host.hostname)
14999-
strmake_buf(sctx->priv_host, acl_user->host.hostname);
15000-
else
15001-
*sctx->priv_host= 0;
15002-
15003-
15004-
/*
15005-
Don't allow the user to connect if he has done too many queries.
15006-
As we are testing max_user_connections == 0 here, it means that we
15007-
can't let the user change max_user_connections from 0 in the server
15008-
without a restart as it would lead to wrong connect counting.
15009-
*/
15010-
if ((acl_user->user_resource.questions ||
15011-
acl_user->user_resource.updates ||
15012-
acl_user->user_resource.conn_per_hour ||
15013-
acl_user->user_resource.user_conn ||
15014-
acl_user->user_resource.max_statement_time != 0.0 ||
15015-
max_user_connections_checking) &&
15016-
get_or_create_user_conn(thd,
15017-
(opt_old_style_user_limits ? sctx->user : sctx->priv_user),
15018-
(opt_old_style_user_limits ? sctx->host_or_ip : sctx->priv_host),
15019-
&acl_user->user_resource))
15020-
DBUG_RETURN(1); // The error is set by get_or_create_user_conn()
15021-
15022-
if (acl_user->user_resource.max_statement_time != 0.0)
15023-
{
15024-
thd->variables.max_statement_time_double=
15025-
acl_user->user_resource.max_statement_time;
15026-
thd->variables.max_statement_time=
15027-
(ulonglong) (thd->variables.max_statement_time_double * 1e6 + 0.1);
15028-
}
15044+
if (set_privs_on_login(thd, acl_user))
15045+
DBUG_RETURN(1);
1502915046
}
1503015047
else
1503115048
sctx->skip_grants();
@@ -15061,29 +15078,6 @@ bool acl_authenticate(THD *thd, uint com_change_user_pkt_len)
1506115078
}
1506215079
}
1506315080

15064-
/*
15065-
This is the default access rights for the current database. It's
15066-
set to 0 here because we don't have an active database yet (and we
15067-
may not have an active database to set.
15068-
*/
15069-
sctx->db_access= NO_ACL;
15070-
15071-
#ifndef NO_EMBEDDED_ACCESS_CHECKS
15072-
/*
15073-
In case the user has a default role set, attempt to set that role
15074-
*/
15075-
if (initialized && acl_user->default_rolename.length) {
15076-
privilege_t access(NO_ACL);
15077-
int result;
15078-
result= acl_check_setrole(thd, acl_user->default_rolename, &access);
15079-
if (!result)
15080-
result= acl_setrole(thd, acl_user->default_rolename, access);
15081-
if (result)
15082-
thd->clear_error(); // even if the default role was not granted, do not
15083-
// close the connection
15084-
}
15085-
#endif
15086-
1508715081
/* Change a database if necessary */
1508815082
if (mpvio.db.length)
1508915083
{

sql/sql_yacc.yy

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,7 @@ rule:
17021702

17031703
%type <lex_user> user grant_user grant_role user_or_role current_role
17041704
admin_option_for_role user_maybe_role role_name
1705+
user_name
17051706

17061707
%type <user_auth> opt_auth_str auth_expression auth_token
17071708
text_or_password
@@ -16078,38 +16079,33 @@ user_maybe_role:
1607816079
system_charset_info, 0)))
1607916080
MYSQL_YYABORT;
1608016081
}
16081-
| ident_or_text '@' ident_or_text
16082+
| user_name { $$= $1; }
16083+
| CURRENT_USER optional_braces
1608216084
{
1608316085
if (unlikely(!($$= thd->calloc<LEX_USER>(1))))
1608416086
MYSQL_YYABORT;
16085-
$$->user = $1; $$->host=$3;
16087+
$$->user= current_user;
16088+
$$->auth= new (thd->mem_root) USER_AUTH();
16089+
}
16090+
;
1608616091

16087-
if (unlikely(check_string_char_length(&$$->user, ER_USERNAME,
16088-
username_char_length,
16089-
system_charset_info, 0)) ||
16090-
unlikely(check_host_name(&$$->host)))
16092+
user_name:
16093+
ident_or_text '@' ident_or_text
16094+
{
16095+
if (!($$= thd->calloc<LEX_USER>(1)))
16096+
MYSQL_YYABORT;
16097+
$$->user = $1;
16098+
$$->host=$3;
16099+
16100+
if (check_string_char_length(&$$->user, ER_USERNAME,
16101+
username_char_length, system_charset_info, 0) ||
16102+
check_host_name(&$$->host))
1609116103
MYSQL_YYABORT;
1609216104
if ($$->host.str[0])
16093-
{
1609416105
$$->host= thd->make_ident_casedn($$->host);
16095-
}
1609616106
else
16097-
{
16098-
/*
16099-
fix historical undocumented convention that empty host is the
16100-
same as '%'
16101-
*/
1610216107
$$->host= host_not_specified;
16103-
}
16104-
}
16105-
| CURRENT_USER optional_braces
16106-
{
16107-
if (unlikely(!($$= thd->calloc<LEX_USER>(1))))
16108-
MYSQL_YYABORT;
16109-
$$->user= current_user;
16110-
$$->auth= new (thd->mem_root) USER_AUTH();
1611116108
}
16112-
;
1611316109

1611416110
user_or_role: user_maybe_role | current_role;
1611516111

0 commit comments

Comments
 (0)