Skip to content

Commit dc33727

Browse files
committed
WL#8048 - Non-intrusive refactoring of a ACL related context class.
WL description: ================ Most of the members of this class are public. Code accessing and modifying them is spread in many files of MySQL code. As part of this work log, * All public data members of this context class are converted to private ones and public accessor methods are introduced for them. Code accessing data members directly will be replaced to use the getter method. And code modifying these members will be replaced with setter method. * The THD class holds pointer to the object of ACL related context class. Accessor methods are introduced for this. And code accessing and modifying this member will be replaced with accessor methods.
1 parent 221ccb0 commit dc33727

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1719
-846
lines changed

libmysqld/lib_sql.cc

+12-13
Original file line numberDiff line numberDiff line change
@@ -728,8 +728,8 @@ void *create_embedded_thd(int client_flag)
728728

729729
thd->reset_db(NULL_CSTR);
730730
#ifndef NO_EMBEDDED_ACCESS_CHECKS
731-
thd->security_ctx->db_access= DB_ACLS;
732-
thd->security_ctx->master_access= ~NO_ACCESS;
731+
thd->security_context()->set_db_access(DB_ACLS);
732+
thd->security_context()->set_master_access(~NO_ACCESS);
733733
#endif
734734
thd->cur_data= 0;
735735
thd->first_data= 0;
@@ -778,15 +778,14 @@ int check_embedded_connection(MYSQL *mysql, const char *db)
778778

779779
thd_init_client_charset(thd, mysql->charset->number);
780780
thd->update_charset();
781-
Security_context *sctx= thd->security_ctx;
782-
sctx->set_host(my_localhost);
783-
sctx->host_or_ip= sctx->get_host()->ptr();
784-
strmake(sctx->priv_host, (char*) my_localhost, MAX_HOSTNAME-1);
785-
strmake(sctx->priv_user, mysql->user, USERNAME_LENGTH-1);
786-
sctx->user= my_strdup(PSI_NOT_INSTRUMENTED,
787-
mysql->user, MYF(0));
788-
sctx->proxy_user[0]= 0;
789-
sctx->master_access= GLOBAL_ACLS; // Full rights
781+
Security_context *sctx= thd->security_context();
782+
sctx->set_host_ptr(my_localhost, strlen(my_localhost));
783+
sctx->set_host_or_ip_ptr(sctx->host().str, sctx->host().length);
784+
sctx->assign_priv_user(mysql->user, strlen(mysql->user));
785+
sctx->assign_user(mysql->user, strlen(mysql->user));
786+
sctx->assign_proxy_user("", 0);
787+
sctx->assign_priv_host(my_localhost, strlen(my_localhost));
788+
sctx->set_master_access(GLOBAL_ACLS); // Full rights
790789
emb_transfer_connect_attrs(mysql);
791790
/* Change database if necessary */
792791
if (!(result= (db && db[0] && mysql_change_db(thd, db_lex_cstr, false))))
@@ -806,7 +805,7 @@ int check_embedded_connection(MYSQL *mysql, const char *db)
806805
char *buf, *end;
807806
NET *net= &mysql->net;
808807
THD *thd= (THD*)mysql->thd;
809-
Security_context *sctx= thd->security_ctx;
808+
Security_context *sctx= thd->security_context();
810809
size_t connect_attrs_len=
811810
(mysql->server_capabilities & CLIENT_CONNECT_ATTRS &&
812811
mysql->options.extension) ?
@@ -858,7 +857,7 @@ int check_embedded_connection(MYSQL *mysql, const char *db)
858857

859858
if (acl_authenticate(thd, 0, end - buf))
860859
{
861-
x_free(thd->security_ctx->user);
860+
x_free(thd->security_context()->user().str);
862861
goto err;
863862
}
864863
my_afree(buf);

sql/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ SET(SQL_SHARED_SOURCES
6666
auth/sql_user_table.cc
6767
auth/sql_user.cc
6868
auth/password.c
69+
auth/sql_security_ctx.cc
6970
bootstrap.cc
7071
conn_handler/connection_handler_manager.cc
7172
datadict.cc

sql/auth/sql_auth_cache.cc

+18-23
Original file line numberDiff line numberDiff line change
@@ -1052,11 +1052,10 @@ bool acl_getroot(Security_context *sctx, char *user, char *host,
10521052
DBUG_PRINT("enter", ("Host: '%s', Ip: '%s', User: '%s', db: '%s'",
10531053
(host ? host : "(NULL)"), (ip ? ip : "(NULL)"),
10541054
user, (db ? db : "(NULL)")));
1055-
sctx->user= user;
1056-
sctx->set_host(host);
1057-
sctx->set_ip(ip);
1058-
1059-
sctx->host_or_ip= host ? host : (ip ? ip : "");
1055+
sctx->set_user_ptr(user, user ? strlen(user) : 0);
1056+
sctx->set_host_ptr(host, host ? strlen(host) : 0);
1057+
sctx->set_ip_ptr(ip, ip? strlen(ip) : 0);
1058+
sctx->set_host_or_ip_ptr();
10601059

10611060
if (!initialized)
10621061
{
@@ -1069,9 +1068,10 @@ bool acl_getroot(Security_context *sctx, char *user, char *host,
10691068

10701069
mysql_mutex_lock(&acl_cache->lock);
10711070

1072-
sctx->master_access= 0;
1073-
sctx->db_access= 0;
1074-
*sctx->priv_user= *sctx->priv_host= 0;
1071+
sctx->set_master_access(0);
1072+
sctx->set_db_access(0);
1073+
sctx->assign_priv_user("", 0);
1074+
sctx->assign_priv_host("", 0);
10751075

10761076
/*
10771077
Find acl entry in user database.
@@ -1105,25 +1105,20 @@ bool acl_getroot(Security_context *sctx, char *user, char *host,
11051105
{
11061106
if (!acl_db->db || (db && !wild_compare(db, acl_db->db, 0)))
11071107
{
1108-
sctx->db_access= acl_db->access;
1108+
sctx->set_db_access(acl_db->access);
11091109
break;
11101110
}
11111111
}
11121112
}
11131113
}
1114-
sctx->master_access= acl_user->access;
1115-
1116-
if (acl_user->user)
1117-
strmake(sctx->priv_user, user, USERNAME_LENGTH);
1118-
else
1119-
*sctx->priv_user= 0;
1114+
sctx->set_master_access(acl_user->access);
1115+
sctx->assign_priv_user(user, user ? strlen(user) : 0);
11201116

1121-
if (acl_user->host.get_host())
1122-
strmake(sctx->priv_host, acl_user->host.get_host(), MAX_HOSTNAME - 1);
1123-
else
1124-
*sctx->priv_host= 0;
1117+
sctx->assign_priv_host(acl_user->host.get_host(),
1118+
acl_user->host.get_host() ?
1119+
strlen(acl_user->host.get_host()) : 0);
11251120

1126-
sctx->password_expired= acl_user->password_expired;
1121+
sctx->set_password_expired(acl_user->password_expired);
11271122
}
11281123
mysql_mutex_unlock(&acl_cache->lock);
11291124
DBUG_RETURN(res);
@@ -2639,8 +2634,8 @@ update_sctx_cache(Security_context *sctx, ACL_USER *acl_user_ptr, bool expired)
26392634
{
26402635
const char *acl_host= acl_user_ptr->host.get_host();
26412636
const char *acl_user= acl_user_ptr->user;
2642-
const char *sctx_user= sctx->priv_user;
2643-
const char *sctx_host= sctx->priv_host;
2637+
const char *sctx_user= sctx->priv_user().str;
2638+
const char *sctx_host= sctx->priv_host().str;
26442639

26452640
if (!acl_host)
26462641
acl_host= "";
@@ -2653,7 +2648,7 @@ update_sctx_cache(Security_context *sctx, ACL_USER *acl_user_ptr, bool expired)
26532648

26542649
if (!strcmp(acl_user, sctx_user) && !strcmp(acl_host, sctx_host))
26552650
{
2656-
sctx->password_expired= expired;
2651+
sctx->set_password_expired(expired);
26572652
return true;
26582653
}
26592654

sql/auth/sql_authentication.cc

+43-33
Original file line numberDiff line numberDiff line change
@@ -1944,13 +1944,14 @@ static void
19441944
server_mpvio_initialize(THD *thd, MPVIO_EXT *mpvio,
19451945
Thd_charset_adapter *charset_adapter)
19461946
{
1947+
LEX_CSTRING sctx_host_or_ip= thd->security_context()->host_or_ip();
1948+
19471949
memset(mpvio, 0, sizeof(MPVIO_EXT));
19481950
mpvio->read_packet= server_mpvio_read_packet;
19491951
mpvio->write_packet= server_mpvio_write_packet;
19501952
mpvio->info= server_mpvio_info;
1951-
mpvio->auth_info.host_or_ip= thd->security_ctx->host_or_ip;
1952-
mpvio->auth_info.host_or_ip_length=
1953-
(unsigned int) strlen(thd->security_ctx->host_or_ip);
1953+
mpvio->auth_info.host_or_ip= sctx_host_or_ip.str;
1954+
mpvio->auth_info.host_or_ip_length= sctx_host_or_ip.length;
19541955
mpvio->auth_info.user_name= NULL;
19551956
mpvio->auth_info.user_name_length= 0;
19561957
#if defined(HAVE_OPENSSL) && !defined(EMBEDDED_LIBRARY)
@@ -1968,8 +1969,8 @@ server_mpvio_initialize(THD *thd, MPVIO_EXT *mpvio,
19681969
mpvio->thread_id= thd->thread_id();
19691970
mpvio->server_status= &thd->server_status;
19701971
mpvio->net= &thd->net;
1971-
mpvio->ip= (char *) thd->security_ctx->get_ip()->ptr();
1972-
mpvio->host= (char *) thd->security_ctx->get_host()->ptr();
1972+
mpvio->ip= (char *) thd->security_context()->ip().str;
1973+
mpvio->host= (char *) thd->security_context()->host().str;
19731974
mpvio->charset_adapter= charset_adapter;
19741975
}
19751976

@@ -1982,7 +1983,14 @@ server_mpvio_update_thd(THD *thd, MPVIO_EXT *mpvio)
19821983
thd->max_client_packet_length= mpvio->max_client_packet_length;
19831984
if (mpvio->client_capabilities & CLIENT_INTERACTIVE)
19841985
thd->variables.net_wait_timeout= thd->variables.net_interactive_timeout;
1985-
thd->security_ctx->user= mpvio->auth_info.user_name;
1986+
thd->security_context()->assign_user(
1987+
mpvio->auth_info.user_name,
1988+
(mpvio->auth_info.user_name ? strlen(mpvio->auth_info.user_name) : 0));
1989+
if (mpvio->auth_info.user_name)
1990+
my_free(mpvio->auth_info.user_name);
1991+
LEX_CSTRING sctx_user= thd->security_context()->user();
1992+
mpvio->auth_info.user_name= (char *) sctx_user.str;
1993+
mpvio->auth_info.user_name_length= sctx_user.length;
19861994
if (thd->client_capabilities & CLIENT_IGNORE_SPACE)
19871995
thd->variables.sql_mode|= MODE_IGNORE_SPACE;
19881996
}
@@ -2133,7 +2141,7 @@ acl_authenticate(THD *thd, size_t com_change_user_pkt_len)
21332141

21342142
server_mpvio_update_thd(thd, &mpvio);
21352143

2136-
Security_context *sctx= thd->security_ctx;
2144+
Security_context *sctx= thd->security_context();
21372145
const ACL_USER *acl_user= mpvio.acl_user;
21382146

21392147
thd->password= mpvio.auth_info.password_used; // remember for error messages
@@ -2193,7 +2201,7 @@ acl_authenticate(THD *thd, size_t com_change_user_pkt_len)
21932201
DBUG_RETURN (1);
21942202
}
21952203

2196-
sctx->proxy_user[0]= 0;
2204+
sctx->assign_proxy_user("", 0);
21972205

21982206
if (initialized) // if not --skip-grant-tables
21992207
{
@@ -2203,13 +2211,13 @@ acl_authenticate(THD *thd, size_t com_change_user_pkt_len)
22032211
const char *auth_user = acl_user->user ? acl_user->user : "";
22042212
ACL_PROXY_USER *proxy_user;
22052213
/* check if the user is allowed to proxy as another user */
2206-
proxy_user= acl_find_proxy_user(auth_user, sctx->get_host()->ptr(),
2207-
sctx->get_ip()->ptr(),
2214+
proxy_user= acl_find_proxy_user(auth_user, sctx->host().str, sctx->ip().str,
22082215
mpvio.auth_info.authenticated_as,
22092216
&is_proxy_user);
22102217
if (is_proxy_user)
22112218
{
22122219
ACL_USER *acl_proxy_user;
2220+
char proxy_user_buf[USERNAME_LENGTH + MAX_HOSTNAME + 5];
22132221

22142222
/* we need to find the proxy user, but there was none */
22152223
if (!proxy_user)
@@ -2222,9 +2230,10 @@ acl_authenticate(THD *thd, size_t com_change_user_pkt_len)
22222230
DBUG_RETURN(1);
22232231
}
22242232

2225-
my_snprintf(sctx->proxy_user, sizeof(sctx->proxy_user) - 1,
2233+
my_snprintf(proxy_user_buf, sizeof(proxy_user_buf) - 1,
22262234
"'%s'@'%s'", auth_user,
22272235
acl_user->host.get_host() ? acl_user->host.get_host() : "");
2236+
sctx->assign_proxy_user(proxy_user_buf, strlen(proxy_user_buf));
22282237

22292238
/* we're proxying : find the proxy user definition */
22302239
mysql_mutex_lock(&acl_cache->lock);
@@ -2248,18 +2257,14 @@ acl_authenticate(THD *thd, size_t com_change_user_pkt_len)
22482257
}
22492258
#endif /* NO_EMBEDDED_ACCESS_CHECKS */
22502259

2251-
sctx->master_access= acl_user->access;
2252-
if (acl_user->user)
2253-
strmake(sctx->priv_user, acl_user->user, USERNAME_LENGTH - 1);
2254-
else
2255-
*sctx->priv_user= 0;
2256-
2257-
if (acl_user->host.get_host())
2258-
strmake(sctx->priv_host, acl_user->host.get_host(), MAX_HOSTNAME - 1);
2259-
else
2260-
*sctx->priv_host= 0;
2260+
sctx->set_master_access(acl_user->access);
2261+
sctx->assign_priv_user(acl_user->user, acl_user->user ?
2262+
strlen(acl_user->user) : 0);
2263+
sctx->assign_priv_host(acl_user->host.get_host(),
2264+
acl_user->host.get_host() ?
2265+
strlen(acl_user->host.get_host()) : 0);
22612266

2262-
if (!(sctx->master_access & SUPER_ACL) && !thd->is_error())
2267+
if (!(sctx->check_access(SUPER_ACL)) && !thd->is_error())
22632268
{
22642269
mysql_mutex_lock(&LOCK_offline_mode);
22652270
bool tmp_offline_mode= MY_TEST(offline_mode);
@@ -2318,8 +2323,10 @@ acl_authenticate(THD *thd, size_t com_change_user_pkt_len)
23182323
acl_user->user_resource.user_conn ||
23192324
global_system_variables.max_user_connections) &&
23202325
get_or_create_user_conn(thd,
2321-
(opt_old_style_user_limits ? sctx->user : sctx->priv_user),
2322-
(opt_old_style_user_limits ? sctx->host_or_ip : sctx->priv_host),
2326+
(opt_old_style_user_limits ? sctx->user().str :
2327+
sctx->priv_user().str),
2328+
(opt_old_style_user_limits ? sctx->host_or_ip().str :
2329+
sctx->priv_host().str),
23232330
&acl_user->user_resource))
23242331
DBUG_RETURN(1); // The error is set by get_or_create_user_conn()
23252332

@@ -2329,7 +2336,8 @@ acl_authenticate(THD *thd, size_t com_change_user_pkt_len)
23292336
This allows proxy user to execute queries even if proxied user password
23302337
expires.
23312338
*/
2332-
sctx->password_expired= mpvio.acl_user->password_expired || password_time_expired;
2339+
sctx->set_password_expired(mpvio.acl_user->password_expired ||
2340+
password_time_expired);
23332341
#endif /* NO_EMBEDDED_ACCESS_CHECKS */
23342342
}
23352343
else
@@ -2349,12 +2357,12 @@ acl_authenticate(THD *thd, size_t com_change_user_pkt_len)
23492357
"Login user: '%s' Priv_user: '%s' Using password: %s "
23502358
"Access: %lu db: '%s'",
23512359
thd->client_capabilities, thd->max_client_packet_length,
2352-
sctx->host_or_ip, sctx->user, sctx->priv_user,
2360+
sctx->host_or_ip().str, sctx->user().str, sctx->priv_user().str,
23532361
thd->password ? "yes": "no",
2354-
sctx->master_access, mpvio.db.str));
2362+
sctx->master_access(), mpvio.db.str));
23552363

23562364
if (command == COM_CONNECT &&
2357-
!(thd->main_security_ctx.master_access & SUPER_ACL))
2365+
!(thd->m_main_security_ctx.check_access(SUPER_ACL)))
23582366
{
23592367
#ifndef EMBEDDED_LIBRARY
23602368
if (!Connection_handler_manager::get_instance()->valid_connection_count())
@@ -2371,7 +2379,7 @@ acl_authenticate(THD *thd, size_t com_change_user_pkt_len)
23712379
set to 0 here because we don't have an active database yet (and we
23722380
may not have an active database to set.
23732381
*/
2374-
sctx->db_access=0;
2382+
sctx->set_db_access(0);
23752383

23762384
/* Change a database if necessary */
23772385
if (mpvio.db.length)
@@ -2388,8 +2396,8 @@ acl_authenticate(THD *thd, size_t com_change_user_pkt_len)
23882396
}
23892397

23902398
if (mpvio.auth_info.external_user[0])
2391-
sctx->set_external_user(my_strdup(key_memory_MPVIO_EXT_auth_info,
2392-
mpvio.auth_info.external_user, MYF(0)));
2399+
sctx->assign_external_user(mpvio.auth_info.external_user,
2400+
strlen(mpvio.auth_info.external_user));
23932401

23942402

23952403
if (res == CR_OK_HANDSHAKE_COMPLETE)
@@ -2398,9 +2406,11 @@ acl_authenticate(THD *thd, size_t com_change_user_pkt_len)
23982406
my_ok(thd);
23992407

24002408
#ifdef HAVE_PSI_THREAD_INTERFACE
2409+
LEX_CSTRING main_sctx_user= thd->m_main_security_ctx.user();
2410+
LEX_CSTRING main_sctx_host_or_ip= thd->m_main_security_ctx.host_or_ip();
24012411
PSI_THREAD_CALL(set_thread_account)
2402-
(thd->main_security_ctx.user, strlen(thd->main_security_ctx.user),
2403-
thd->main_security_ctx.host_or_ip, strlen(thd->main_security_ctx.host_or_ip));
2412+
(main_sctx_user.str, main_sctx_user.length,
2413+
main_sctx_host_or_ip.str, main_sctx_host_or_ip.length);
24042414
#endif /* HAVE_PSI_THREAD_INTERFACE */
24052415

24062416
/* Ready to handle queries */

0 commit comments

Comments
 (0)