1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
|
/*
Copyright (c) 2007, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
Functions to authenticate and handle requests for a connection
*/
#include "sql/sql_connect.h"
#include "my_config.h"
#include "my_loglevel.h"
#include "my_psi_config.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/components/services/log_shared.h"
#include "pfs_thread_provider.h"
#include "sql/table.h"
#ifndef _WIN32
#include <netdb.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#include <stdint.h>
#include <string.h>
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <algorithm>
#include <atomic>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include "lex_string.h"
#include "m_ctype.h"
#include "m_string.h" // my_stpcpy
#include "map_helpers.h"
#include "my_command.h"
#include "my_dbug.h"
#include "my_sqlcommand.h"
#include "my_sys.h"
#include "mysql/plugin_audit.h"
#include "mysql/psi/mysql_mutex.h"
#include "mysql/psi/mysql_thread.h"
#include "mysql/service_mysql_alloc.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "sql-common/net_ns.h" // set_network_namespace
#include "sql/auth/auth_acls.h"
#include "sql/auth/auth_common.h" // SUPER_ACL
#include "sql/auth/sql_security_ctx.h"
#include "sql/debug_sync.h" // DEBUG_SYNC
#include "sql/derror.h" // ER_THD
#include "sql/hostname_cache.h" // Host_errors
#include "sql/item_func.h" // mqh_used
#include "sql/log.h"
#include "sql/mysqld.h" // LOCK_user_conn
#include "sql/protocol.h"
#include "sql/protocol_classic.h"
#include "sql/psi_memory_key.h"
#include "sql/sql_audit.h" // MYSQL_AUDIT_NOTIFY_CONNECTION_CONNECT
#include "sql/sql_class.h" // THD
#include "sql/sql_error.h"
#include "sql/sql_lex.h"
#include "sql/sql_parse.h" // sql_command_flags
#include "sql/sql_plugin.h" // plugin_thdvar_cleanup
#include "sql/system_variables.h"
#include "sql_string.h"
#include "violite.h"
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
using std::max;
using std::min;
/*
With SSL the handshake might consist of two packets. If the first
packet (client capabilities) has CLIENT_SSL flag set, we have to
switch to SSL and read the second packet. The scrambled password
is in the second packet and client_capabilites field will be ignored.
Maybe it is better to accept flags other than CLIENT_SSL from the
second packet?
*/
#define SSL_HANDSHAKE_SIZE 2
#define NORMAL_HANDSHAKE_SIZE 6
#define MIN_HANDSHAKE_SIZE 2
/*
Get structure for logging connection data for the current user
*/
static collation_unordered_map<std::string, unique_ptr_my_free<user_conn>>
*hash_user_connections;
int get_or_create_user_conn(THD *thd, const char *user, const char *host,
const USER_RESOURCES *mqh) {
int return_val = 0;
size_t temp_len, user_len;
char temp_user[USER_HOST_BUFF_SIZE];
struct user_conn *uc = nullptr;
assert(user != nullptr);
assert(host != nullptr);
user_len = strlen(user);
temp_len = (my_stpcpy(my_stpcpy(temp_user, user) + 1, host) - temp_user) + 1;
mysql_mutex_lock(&LOCK_user_conn);
const auto it = hash_user_connections->find(std::string(temp_user, temp_len));
if (it == hash_user_connections->end()) {
/* First connection for user; Create a user connection object */
if (!(uc = ((struct user_conn *)my_malloc(
key_memory_user_conn, sizeof(struct user_conn) + temp_len + 1,
MYF(MY_WME))))) {
/* MY_WME ensures an error is set in THD. */
return_val = 1;
goto end;
}
uc->user = (char *)(uc + 1);
memcpy(uc->user, temp_user, temp_len + 1);
uc->host = uc->user + user_len + 1;
uc->len = temp_len;
uc->connections = uc->questions = uc->updates = uc->conn_per_hour = 0;
uc->user_resources = *mqh;
uc->reset_utime = thd->start_utime;
hash_user_connections->emplace(std::string(temp_user, temp_len),
unique_ptr_my_free<user_conn>(uc));
} else {
uc = it->second.get();
}
thd->set_user_connect(uc);
thd->increment_user_connections_counter();
end:
mysql_mutex_unlock(&LOCK_user_conn);
return return_val;
}
/*
check if user has already too many connections
SYNOPSIS
check_for_max_user_connections()
thd Thread handle
uc User connect object
NOTES
If check fails, we decrease user connection count, which means one
shouldn't call decrease_user_connections() after this function.
RETURN
0 ok
1 error
*/
int check_for_max_user_connections(THD *thd, const USER_CONN *uc) {
int error = 0;
Host_errors errors;
DBUG_TRACE;
mysql_mutex_lock(&LOCK_user_conn);
if (global_system_variables.max_user_connections &&
!uc->user_resources.user_conn &&
global_system_variables.max_user_connections < (uint)uc->connections &&
!thd->is_admin_connection()) {
my_error(ER_TOO_MANY_USER_CONNECTIONS, MYF(0), uc->user);
error = 1;
errors.m_max_user_connection = 1;
goto end;
}
thd->time_out_user_resource_limits();
if (uc->user_resources.user_conn &&
uc->user_resources.user_conn < uc->connections) {
my_error(ER_USER_LIMIT_REACHED, MYF(0), uc->user, "max_user_connections",
(long)uc->user_resources.user_conn);
error = 1;
errors.m_max_user_connection = 1;
goto end;
}
if (uc->user_resources.conn_per_hour &&
uc->user_resources.conn_per_hour <= uc->conn_per_hour) {
my_error(ER_USER_LIMIT_REACHED, MYF(0), uc->user,
"max_connections_per_hour",
(long)uc->user_resources.conn_per_hour);
error = 1;
errors.m_max_user_connection_per_hour = 1;
goto end;
}
thd->increment_con_per_hour_counter();
end:
if (error) {
thd->decrement_user_connections_counter();
/*
The thread may returned back to the pool and assigned to a user
that doesn't have a limit. Ensure the user is not using resources
of someone else.
*/
thd->set_user_connect(nullptr);
}
mysql_mutex_unlock(&LOCK_user_conn);
if (error) {
inc_host_errors(thd->m_main_security_ctx.ip().str, &errors);
}
return error;
}
/*
Decrease user connection count
SYNOPSIS
decrease_user_connections()
uc User connection object
NOTES
If there is a n user connection object for a connection
(which only happens if 'max_user_connections' is defined or
if someone has created a resource grant for a user), then
the connection count is always incremented on connect.
The user connect object is not freed if some users has
'max connections per hour' defined as we need to be able to hold
count over the lifetime of the connection.
*/
void decrease_user_connections(USER_CONN *uc) {
DBUG_TRACE;
mysql_mutex_lock(&LOCK_user_conn);
assert(uc->connections);
if (!--uc->connections && !mqh_used) {
/* Last connection for user; Delete it */
hash_user_connections->erase(std::string(uc->user, uc->len));
}
mysql_mutex_unlock(&LOCK_user_conn);
}
/*
Decrements user connections count from the USER_CONN held by THD
And removes USER_CONN from the hash if no body else is using it.
SYNOPSIS
release_user_connection()
THD Thread context object.
*/
void release_user_connection(THD *thd) {
const USER_CONN *uc = thd->get_user_connect();
DBUG_TRACE;
if (uc) {
mysql_mutex_lock(&LOCK_user_conn);
assert(uc->connections > 0);
thd->decrement_user_connections_counter();
if (!uc->connections && !mqh_used) {
/* Last connection for user; Delete it */
hash_user_connections->erase(std::string(uc->user, uc->len));
}
mysql_mutex_unlock(&LOCK_user_conn);
thd->set_user_connect(nullptr);
}
}
/*
Check if maximum queries per hour limit has been reached
returns 0 if OK.
*/
bool check_mqh(THD *thd, uint check_command) {
bool error = false;
const USER_CONN *uc = thd->get_user_connect();
DBUG_TRACE;
assert(uc != nullptr);
mysql_mutex_lock(&LOCK_user_conn);
thd->time_out_user_resource_limits();
/* Check that we have not done too many questions / hour */
if (uc->user_resources.questions) {
thd->increment_questions_counter();
if ((uc->questions - 1) >= uc->user_resources.questions) {
my_error(ER_USER_LIMIT_REACHED, MYF(0), uc->user, "max_questions",
(long)uc->user_resources.questions);
error = true;
goto end;
}
}
if (check_command < (uint)SQLCOM_END) {
/* Check that we have not done too many updates / hour */
if (uc->user_resources.updates &&
(sql_command_flags[check_command] & CF_CHANGES_DATA)) {
thd->increment_updates_counter();
if ((uc->updates - 1) >= uc->user_resources.updates) {
my_error(ER_USER_LIMIT_REACHED, MYF(0), uc->user, "max_updates",
(long)uc->user_resources.updates);
error = true;
goto end;
}
}
}
end:
mysql_mutex_unlock(&LOCK_user_conn);
return error;
}
void init_max_user_conn(void) {
hash_user_connections =
new collation_unordered_map<std::string, unique_ptr_my_free<user_conn>>(
system_charset_info, key_memory_user_conn);
}
void free_max_user_conn(void) {
delete hash_user_connections;
hash_user_connections = nullptr;
}
void reset_mqh(THD *thd, LEX_USER *lu, bool get_them = false) {
mysql_mutex_lock(&LOCK_user_conn);
DEBUG_SYNC(thd, "in_reset_mqh_flush_privileges");
if (lu) // for GRANT
{
size_t temp_len = lu->user.length + lu->host.length + 2;
char temp_user[USER_HOST_BUFF_SIZE];
memcpy(temp_user, lu->user.str, lu->user.length);
memcpy(temp_user + lu->user.length + 1, lu->host.str, lu->host.length);
temp_user[lu->user.length] = '\0';
temp_user[temp_len - 1] = 0;
const auto it =
hash_user_connections->find(std::string(temp_user, temp_len));
if (it != hash_user_connections->end()) {
USER_CONN *uc = it->second.get();
uc->questions = 0;
get_mqh(thd, temp_user, &temp_user[lu->user.length + 1], uc);
uc->updates = 0;
uc->conn_per_hour = 0;
}
} else {
/* for FLUSH PRIVILEGES and FLUSH USER_RESOURCES */
for (const auto &key_and_value : *hash_user_connections) {
USER_CONN *uc = key_and_value.second.get();
if (get_them) get_mqh(thd, uc->user, uc->host, uc);
uc->questions = 0;
uc->updates = 0;
uc->conn_per_hour = 0;
}
}
mysql_mutex_unlock(&LOCK_user_conn);
}
/**
Set thread character set variables from the given ID
@param thd thread handle
@param cs_number character set and collation ID
@retval 0 OK; character_set_client, collation_connection and
character_set_results are set to the new value,
or to the default global values.
@retval 1 error, e.g. the given ID is not supported by parser.
Corresponding SQL error is sent.
*/
bool thd_init_client_charset(THD *thd, uint cs_number) {
CHARSET_INFO *cs;
/*
Use server character set and collation if
- opt_character_set_client_handshake is not set
- client has not specified a character set
- client character set is the same as the servers
- client character set doesn't exists in server
*/
if (!opt_character_set_client_handshake ||
!(cs = get_charset(cs_number, MYF(0))) ||
!my_strcasecmp(&my_charset_latin1,
global_system_variables.character_set_client->m_coll_name,
cs->m_coll_name)) {
if (!is_supported_parser_charset(
global_system_variables.character_set_client)) {
/* Disallow non-supported parser character sets: UCS2, UTF16, UTF32 */
my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), "character_set_client",
global_system_variables.character_set_client->csname);
return true;
}
thd->variables.character_set_client =
global_system_variables.character_set_client;
thd->variables.collation_connection =
global_system_variables.collation_connection;
thd->variables.character_set_results =
global_system_variables.character_set_results;
} else {
if (!is_supported_parser_charset(cs)) {
/* Disallow non-supported parser character sets: UCS2, UTF16, UTF32 */
my_error(ER_WRONG_VALUE_FOR_VAR, MYF(0), "character_set_client",
cs->csname);
return true;
}
thd->variables.character_set_results = thd->variables.collation_connection =
thd->variables.character_set_client = cs;
}
return false;
}
/*
Perform handshake, authorize client and update thd ACL variables.
SYNOPSIS
check_connection()
thd thread handle
RETURN
0 success, thd is updated.
1 error
*/
static int check_connection(THD *thd) {
uint connect_errors = 0;
int auth_rc;
NET *net = thd->get_protocol_classic()->get_net();
#ifndef NDEBUG
char desc[VIO_DESCRIPTION_SIZE];
vio_description(net->vio, desc);
DBUG_PRINT("info", ("New connection received on %s", desc));
#endif // NDEBUG
thd->set_active_vio(net->vio);
if (!thd->m_main_security_ctx.host().length) // If TCP/IP connection
{
bool peer_rc;
char ip[NI_MAXHOST];
LEX_CSTRING main_sctx_ip;
/* Set the remote (peer) port for this THD. */
peer_rc = vio_peer_addr(net->vio, ip, &thd->peer_port, NI_MAXHOST);
mysql_thread_set_peer_port(thd->peer_port);
/*
===========================================================================
DEBUG code only (begin)
Simulate various output from vio_peer_addr().
===========================================================================
*/
DBUG_EXECUTE_IF("vio_peer_addr_error", { peer_rc = 1; });
DBUG_EXECUTE_IF("vio_peer_addr_fake_ipv4", {
struct sockaddr *sa = (sockaddr *)&net->vio->remote;
sa->sa_family = AF_INET;
struct in_addr *ip4 = &((struct sockaddr_in *)sa)->sin_addr;
/* See RFC 5737, 192.0.2.0/24 is reserved. */
const char *fake = "192.0.2.4";
ip4->s_addr = inet_addr(fake);
strcpy(ip, fake);
peer_rc = 0;
});
DBUG_EXECUTE_IF("vio_peer_addr_fake_ipv6", {
struct sockaddr_in6 *sa = (sockaddr_in6 *)&net->vio->remote;
sa->sin6_family = AF_INET6;
struct in6_addr *ip6 = &sa->sin6_addr;
/* See RFC 3849, ipv6 2001:DB8::/32 is reserved. */
const char *fake = "2001:db8::6:6";
/* inet_pton(AF_INET6, fake, ip6); not available on Windows XP. */
ip6->s6_addr[0] = 0x20;
ip6->s6_addr[1] = 0x01;
ip6->s6_addr[2] = 0x0d;
ip6->s6_addr[3] = 0xb8;
ip6->s6_addr[4] = 0x00;
ip6->s6_addr[5] = 0x00;
ip6->s6_addr[6] = 0x00;
ip6->s6_addr[7] = 0x00;
ip6->s6_addr[8] = 0x00;
ip6->s6_addr[9] = 0x00;
ip6->s6_addr[10] = 0x00;
ip6->s6_addr[11] = 0x00;
ip6->s6_addr[12] = 0x00;
ip6->s6_addr[13] = 0x06;
ip6->s6_addr[14] = 0x00;
ip6->s6_addr[15] = 0x06;
strcpy(ip, fake);
peer_rc = 0;
});
/*
===========================================================================
DEBUG code only (end)
===========================================================================
*/
if (peer_rc) {
/*
Since we can not even get the peer IP address,
there is nothing to show in the host_cache,
so increment the global status variable for peer address errors.
*/
connection_errors_peer_addr++;
my_error(ER_BAD_HOST_ERROR, MYF(0));
return 1;
}
thd->m_main_security_ctx.assign_ip(ip, strlen(ip));
main_sctx_ip = thd->m_main_security_ctx.ip();
if (!(main_sctx_ip.length)) {
/*
No error accounting per IP in host_cache,
this is treated as a global server OOM error.
TODO: remove the need for my_strdup.
*/
connection_errors_internal++;
return 1; /* The error is set by my_strdup(). */
}
thd->m_main_security_ctx.set_host_or_ip_ptr(main_sctx_ip.str,
main_sctx_ip.length);
if (!(specialflag & SPECIAL_NO_RESOLVE)) {
int rc;
char *host = nullptr;
LEX_CSTRING main_sctx_host;
#ifdef HAVE_SETNS
/*
Check whether namespace is specified for a socket being handled.
If it is specified then set the namespace as active before resolving
ip address to host name. Restore original network namespace after
address resolution finished.
*/
std::string network_namespace(net->vio->network_namespace);
if (!network_namespace.empty() &&
set_network_namespace(network_namespace)) {
return 1;
}
#endif
rc = ip_to_hostname(&net->vio->remote, main_sctx_ip.str, &host,
&connect_errors);
#ifdef HAVE_SETNS
if (!network_namespace.empty() && restore_original_network_namespace()) {
if (host && host != my_localhost) {
my_free(host);
}
return 1;
}
#endif
thd->m_main_security_ctx.assign_host(host, host ? strlen(host) : 0);
DBUG_EXECUTE_IF("vio_peer_addr_fake_hostname1", {
thd->m_main_security_ctx.assign_host(
"host_"
"1234567890abcdefghij1234567890abcdefghij1234567890abcdefghij123456"
"7890abcdefghij1234567890abcdefghij1234567890abcdefghij1234567890ab"
"cdefghij1234567890abcdefghij1234567890abcdefghij1234567890abcdefgh"
"ij1234567890abcdefghij1234567890abcdefghij1234567890",
255);
});
main_sctx_host = thd->m_main_security_ctx.host();
if (host && host != my_localhost) {
my_free(host);
}
/* Cut very long hostnames to avoid possible overflows */
if (main_sctx_host.length) {
if (main_sctx_host.str != my_localhost)
thd->m_main_security_ctx.set_host_ptr(
main_sctx_host.str,
min<size_t>(main_sctx_host.length, HOSTNAME_LENGTH));
thd->m_main_security_ctx.set_host_or_ip_ptr(main_sctx_host.str,
main_sctx_host.length);
}
if (rc == RC_LONG_HOSTNAME) {
my_error(ER_HOSTNAME_TOO_LONG, MYF(0), HOSTNAME_LENGTH);
return 1;
}
if (rc == RC_BLOCKED_HOST) {
/* HOST_CACHE stats updated by ip_to_hostname(). */
my_error(ER_HOST_IS_BLOCKED, MYF(0),
thd->m_main_security_ctx.host_or_ip().str);
return 1;
}
}
DBUG_PRINT("info",
("Host: %s ip: %s",
(thd->m_main_security_ctx.host().length
? thd->m_main_security_ctx.host().str
: "unknown host"),
(main_sctx_ip.length ? main_sctx_ip.str : "unknown ip")));
if (acl_check_host(thd, thd->m_main_security_ctx.host().str,
main_sctx_ip.str)) {
/* HOST_CACHE stats updated by acl_check_host(). */
my_error(ER_HOST_NOT_PRIVILEGED, MYF(0),
thd->m_main_security_ctx.host_or_ip().str);
return 1;
}
} else /* Hostname given means that the connection was on a socket */
{
LEX_CSTRING main_sctx_host = thd->m_main_security_ctx.host();
DBUG_PRINT("info", ("Host: %s", main_sctx_host.str));
thd->m_main_security_ctx.set_host_or_ip_ptr(main_sctx_host.str,
main_sctx_host.length);
thd->m_main_security_ctx.set_ip_ptr(STRING_WITH_LEN(""));
/* Reset sin_addr */
memset(&net->vio->remote, 0, sizeof(net->vio->remote));
}
vio_keepalive(net->vio, true);
if (thd->get_protocol_classic()->get_output_packet()->alloc(
thd->variables.net_buffer_length)) {
/*
Important note:
net_buffer_length is a SESSION variable,
so it may be tempting to account OOM conditions per IP in the HOST_CACHE,
in case some clients are more demanding than others ...
However, this session variable is *not* initialized with a per client
value during the initial connection, it is initialized from the
GLOBAL net_buffer_length variable from the server.
Hence, there is no reason to account on OOM conditions per client IP,
we count failures in the global server status instead.
*/
connection_errors_internal++;
return 1; /* The error is set by alloc(). */
}
if (mysql_audit_notify(
thd, AUDIT_EVENT(MYSQL_AUDIT_CONNECTION_PRE_AUTHENTICATE))) {
return 1;
}
auth_rc = acl_authenticate(thd, COM_CONNECT);
if (mysql_audit_notify(thd, AUDIT_EVENT(MYSQL_AUDIT_CONNECTION_CONNECT))) {
return 1;
}
#ifdef HAVE_PSI_THREAD_INTERFACE
PSI_THREAD_CALL(notify_session_connect)(thd->get_psi());
#endif /* HAVE_PSI_THREAD_INTERFACE */
if (auth_rc == 0 && connect_errors != 0) {
/*
A client connection from this IP was successful,
after some previous failures.
Reset the connection error counter.
*/
reset_host_connect_errors(thd->m_main_security_ctx.ip().str);
}
/*
Now that acl_authenticate() is executed,
the SSL info is available.
Advertise it to THD, so SSL status variables
can be inspected.
*/
thd->set_ssl(net->vio);
return auth_rc;
}
/*
Authenticate user, with error reporting
SYNOPSIS
login_connection()
thd Thread handler
NOTES
Connection is not closed in case of errors
RETURN
0 ok
1 error
*/
static bool login_connection(THD *thd) {
int error;
DBUG_TRACE;
DBUG_PRINT("info",
("login_connection called by thread %u", thd->thread_id()));
/* Use "connect_timeout" value during connection phase */
thd->get_protocol_classic()->set_read_timeout(connect_timeout, true);
thd->get_protocol_classic()->set_write_timeout(connect_timeout);
error = check_connection(thd);
thd->send_statement_status();
if (error) { // Wrong permissions
#ifdef _WIN32
if (vio_type(thd->get_protocol_classic()->get_vio()) == VIO_TYPE_NAMEDPIPE)
my_sleep(1000); /* must wait after eof() */
#endif
return true;
}
/* Connect completed, set read/write timeouts back to default */
thd->get_protocol_classic()->set_read_timeout(
thd->variables.net_read_timeout);
thd->get_protocol_classic()->set_write_timeout(
thd->variables.net_write_timeout);
return false;
}
/*
Close an established connection
NOTES
This mainly updates status variables
*/
void end_connection(THD *thd) {
NET *net = thd->get_protocol_classic()->get_net();
mysql_audit_notify(thd, AUDIT_EVENT(MYSQL_AUDIT_CONNECTION_DISCONNECT), 0);
#ifdef HAVE_PSI_THREAD_INTERFACE
PSI_THREAD_CALL(notify_session_disconnect)(thd->get_psi());
#endif /* HAVE_PSI_THREAD_INTERFACE */
plugin_thdvar_cleanup(thd, thd->m_enable_plugins);
/*
The thread may returned back to the pool and assigned to a user
that doesn't have a limit. Ensure the user is not using resources
of someone else.
*/
release_user_connection(thd);
if (thd->killed || (net->error && net->vio != nullptr)) {
aborted_threads++;
}
if (net->error && net->vio != nullptr) {
if (!thd->killed) {
Security_context *sctx = thd->security_context();
LEX_CSTRING sctx_user = sctx->user();
LogErr(
INFORMATION_LEVEL, ER_ABORTING_USER_CONNECTION, thd->thread_id(),
(thd->db().str ? thd->db().str : "unconnected"),
sctx_user.str ? sctx_user.str : "unauthenticated",
sctx->host_or_ip().str,
(thd->get_stmt_da()->is_error() ? thd->get_stmt_da()->message_text()
: ER_DEFAULT(ER_UNKNOWN_ERROR)));
}
}
}
/*
Initialize THD to handle queries
*/
static void prepare_new_connection_state(THD *thd) {
NET *net = thd->get_protocol_classic()->get_net();
Security_context *sctx = thd->security_context();
if (thd->get_protocol()->has_client_capability(CLIENT_COMPRESS) ||
thd->get_protocol()->has_client_capability(
CLIENT_ZSTD_COMPRESSION_ALGORITHM)) {
net->compress = true; // Use compression
enum enum_compression_algorithm algorithm = get_compression_algorithm(
thd->get_protocol()->get_compression_algorithm());
NET_SERVER *server_extn = static_cast<NET_SERVER *>(net->extension);
if (server_extn != nullptr)
mysql_compress_context_init(&server_extn->compress_ctx, algorithm,
thd->get_protocol()->get_compression_level());
if (net->extension == nullptr) {
LEX_CSTRING sctx_user = sctx->user();
Host_errors errors;
my_error(ER_NEW_ABORTING_CONNECTION, MYF(0), thd->thread_id(),
thd->db().str ? thd->db().str : "unconnected",
sctx_user.str ? sctx_user.str : "unauthenticated",
sctx->host_or_ip().str,
"Unable to allocate memory for compression context: Aborting "
"connection.");
thd->server_status &= ~SERVER_STATUS_CLEAR_SET;
thd->send_statement_status();
thd->killed = THD::KILL_CONNECTION;
errors.m_init_connect = 1;
inc_host_errors(thd->m_main_security_ctx.ip().str, &errors);
return;
}
}
// Initializing session system variables.
alloc_and_copy_thd_dynamic_variables(thd, true);
thd->set_proc_info(nullptr);
thd->set_command(COM_SLEEP);
thd->init_query_mem_roots();
const bool is_admin_conn =
(sctx->check_access(SUPER_ACL) ||
sctx->has_global_grant(STRING_WITH_LEN("CONNECTION_ADMIN")).first);
thd->m_mem_cnt.set_orig_mode(is_admin_conn ? MEM_CNT_UPDATE_GLOBAL_COUNTER
: (MEM_CNT_UPDATE_GLOBAL_COUNTER |
MEM_CNT_GENERATE_ERROR |
MEM_CNT_GENERATE_LOG_ERROR));
if (opt_init_connect.length && !is_admin_conn) {
if (sctx->password_expired()) {
LogErr(WARNING_LEVEL, ER_CONN_INIT_CONNECT_IGNORED, sctx->priv_user().str,
sctx->priv_host().str);
return;
}
if (sctx->is_in_registration_sandbox_mode()) {
LogErr(WARNING_LEVEL, ER_CONN_INIT_CONNECT_IGNORED_MFA,
sctx->priv_user().str, sctx->priv_host().str);
return;
}
// Do not print OOM error to error log.
thd->m_mem_cnt.set_curr_mode(
(MEM_CNT_UPDATE_GLOBAL_COUNTER | MEM_CNT_GENERATE_ERROR));
execute_init_command(thd, &opt_init_connect, &LOCK_sys_init_connect);
thd->m_mem_cnt.set_curr_mode(MEM_CNT_DEFAULT);
if (thd->is_error()) {
Host_errors errors;
ulong packet_length;
LEX_CSTRING sctx_user = sctx->user();
Diagnostics_area *da = thd->get_stmt_da();
const char *user = sctx_user.str ? sctx_user.str : "unauthenticated";
const char *what = "init_connect command failed";
LogEvent()
.prio(WARNING_LEVEL)
.subsys(LOG_SUBSYSTEM_TAG)
.thread_id(thd->thread_id())
.user(sctx_user)
.host(sctx->host_or_ip())
.source_file(MY_BASENAME)
.errcode(ER_SERVER_NEW_ABORTING_CONNECTION)
.sqlstate(da->returned_sqlstate())
.string_value(LOG_TAG_DIAG, da->message_text())
.string_value(LOG_TAG_AUX, what)
.lookup(ER_SERVER_NEW_ABORTING_CONNECTION, thd->thread_id(),
thd->db().str ? thd->db().str : "unconnected", user,
sctx->host_or_ip().str, what, da->mysql_errno(),
da->message_text());
thd->lex->set_current_query_block(nullptr);
my_net_set_read_timeout(net, thd->variables.net_wait_timeout);
thd->clear_error();
net_new_transaction(net);
packet_length = my_net_read(net);
/*
If my_net_read() failed, my_error() has been already called,
and the main Diagnostics Area contains an error condition.
*/
if (packet_length != packet_error)
my_error(ER_NEW_ABORTING_CONNECTION, MYF(0), thd->thread_id(),
thd->db().str ? thd->db().str : "unconnected",
sctx_user.str ? sctx_user.str : "unauthenticated",
sctx->host_or_ip().str, "init_connect command failed");
thd->server_status &= ~SERVER_STATUS_CLEAR_SET;
thd->send_statement_status();
thd->killed = THD::KILL_CONNECTION;
errors.m_init_connect = 1;
inc_host_errors(thd->m_main_security_ctx.ip().str, &errors);
NET_SERVER *server_extn = static_cast<NET_SERVER *>(net->extension);
if (server_extn != nullptr)
mysql_compress_context_deinit(&server_extn->compress_ctx);
return;
}
thd->set_proc_info(nullptr);
thd->init_query_mem_roots();
}
}
bool thd_prepare_connection(THD *thd) {
thd->enable_mem_cnt();
bool rc;
lex_start(thd);
rc = login_connection(thd);
if (rc) return rc;
prepare_new_connection_state(thd);
return false;
}
/**
Close a connection.
@param thd Thread handle.
@param sql_errno The error code to send before disconnect.
@param server_shutdown True for a server shutdown
@param generate_event Generate Audit API disconnect event.
@note
For the connection that is doing shutdown, this is called twice
*/
void close_connection(THD *thd, uint sql_errno, bool server_shutdown,
bool generate_event) {
DBUG_TRACE;
if (sql_errno) net_send_error(thd, sql_errno, ER_DEFAULT_NONCONST(sql_errno));
thd->disconnect(server_shutdown);
if (generate_event) {
mysql_audit_notify(thd, AUDIT_EVENT(MYSQL_AUDIT_CONNECTION_DISCONNECT),
sql_errno);
#ifdef HAVE_PSI_THREAD_INTERFACE
PSI_THREAD_CALL(notify_session_disconnect)(thd->get_psi());
#endif /* HAVE_PSI_THREAD_INTERFACE */
}
thd->security_context()->logout();
}
bool thd_connection_alive(THD *thd) {
NET *net = thd->get_protocol_classic()->get_net();
if (!net->error && net->vio != nullptr &&
!(thd->killed == THD::KILL_CONNECTION))
return true;
return false;
}
|