Skip to content

Commit 318000c

Browse files
committed
MDEV-33506 Show original IP in the "aborted" message.
Add "real ip:<ip_or_localhost>" part to the aborted message Only for proxy-protocoled connection, so it does not not to cause confusion to normal users.
1 parent b762541 commit 318000c

File tree

8 files changed

+52
-19
lines changed

8 files changed

+52
-19
lines changed

include/mysql_com.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ typedef struct st_net {
478478
my_bool thread_specific_malloc;
479479
unsigned char compress;
480480
my_bool pkt_nr_can_be_reset;
481+
my_bool using_proxy_protocol;
481482
/*
482483
Pointer to query object in query cache, do not equal NULL (0) for
483484
queries in cache that have not stored its results yet

mysql-test/main/mysql_client_test.result

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,4 @@ SET @@global.character_set_server= @save_character_set_server;
261261
SET @@global.collation_server= @save_collation_server;
262262
SET @@global.character_set_client= @save_character_set_client;
263263
SET @@global.collation_connection= @save_collation_connection;
264+
FOUND 1 /Aborted connection.*'u' host: '192.0.2.1' real ip: '(localhost|::1)'/ in mysqld.1.err

mysql-test/main/mysql_client_test.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,9 @@ SET @@global.character_set_server= @save_character_set_server;
5757
SET @@global.collation_server= @save_collation_server;
5858
SET @@global.character_set_client= @save_character_set_client;
5959
SET @@global.collation_connection= @save_collation_connection;
60+
61+
# Search for "real ip" in Aborted message
62+
# This is indicator for abort of the proxied connections.
63+
let SEARCH_FILE=$MYSQLTEST_VARDIR/log/mysqld.1.err;
64+
let SEARCH_PATTERN= Aborted connection.*'u' host: '192.0.2.1' real ip: '(localhost|::1)';
65+
source include/search_pattern_in_file.inc;

sql/net_serv.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ my_bool my_net_init(NET *net, Vio *vio, void *thd, uint my_flags)
163163
net->net_skip_rest_factor= 0;
164164
net->last_errno=0;
165165
net->pkt_nr_can_be_reset= 0;
166+
net->using_proxy_protocol= 0;
166167
net->thread_specific_malloc= MY_TEST(my_flags & MY_THREAD_SPECIFIC);
167168
net->thd= 0;
168169
#ifdef MYSQL_SERVER
@@ -217,6 +218,7 @@ void net_end(NET *net)
217218
DBUG_ENTER("net_end");
218219
my_free(net->buff);
219220
net->buff=0;
221+
net->using_proxy_protocol= 0;
220222
DBUG_VOID_RETURN;
221223
}
222224

@@ -955,6 +957,7 @@ static handle_proxy_header_result handle_proxy_header(NET *net)
955957
return RETRY;
956958
/* Change peer address in THD and ACL structures.*/
957959
uint host_errors;
960+
net->using_proxy_protocol= 1;
958961
return (handle_proxy_header_result)thd_set_peer_addr(thd,
959962
&(peer_info.peer_addr), NULL, peer_info.port,
960963
false, &host_errors);

sql/share/errmsg-utf8.txt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4258,22 +4258,22 @@ ER_ERROR_DURING_CHECKPOINT
42584258
swe "Fick fel %M vid CHECKPOINT"
42594259
ukr "Отримано помилку %M під час CHECKPOINT"
42604260
ER_NEW_ABORTING_CONNECTION 08S01
4261-
chi "终止的连接 %lld 到数据库: '%-.192s' 用户: '%-.48s' 主机: '%-.64s' (%-.64s)"
4262-
cze "Spojení %lld do databáze: '%-.192s' uživatel: '%-.48s' stroj: '%-.64s' (%-.64s) bylo přerušeno"
4263-
dan "Afbrød forbindelsen %lld til databasen '%-.192s' bruger: '%-.48s' vært: '%-.64s' (%-.64s)"
4264-
eng "Aborted connection %lld to db: '%-.192s' user: '%-.48s' host: '%-.64s' (%-.64s)"
4265-
est "Ühendus katkestatud %lld andmebaas: '%-.192s' kasutaja: '%-.48s' masin: '%-.64s' (%-.64s)"
4266-
fre "Connection %lld avortée vers la bd: '%-.192s' utilisateur: '%-.48s' hôte: '%-.64s' (%-.64s)"
4267-
ger "Abbruch der Verbindung %lld zur Datenbank '%-.192s'. Benutzer: '%-.48s', Host: '%-.64s' (%-.64s)"
4268-
ita "Interrotta la connessione %lld al db: ''%-.192s' utente: '%-.48s' host: '%-.64s' (%-.64s)"
4269-
jpn "接続 %lld が中断されました。データベース: '%-.192s' ユーザー: '%-.48s' ホスト: '%-.64s' (%-.64s)"
4270-
nla "Afgebroken verbinding %lld naar db: '%-.192s' gebruiker: '%-.48s' host: '%-.64s' (%-.64s)"
4271-
por "Conexão %lld abortada para banco de dados '%-.192s' - usuário '%-.48s' - 'host' '%-.64s' ('%-.64s')"
4272-
rus "Прервано соединение %lld к базе данных '%-.192s' пользователя '%-.48s' с хоста '%-.64s' (%-.64s)"
4273-
serbian "Prekinuta konekcija broj %lld ka bazi: '%-.192s' korisnik je bio: '%-.48s' a host: '%-.64s' (%-.64s)"
4274-
spa "Abortada conexión %lld a la base de datos: '%-.192s' usuario: '%-.48s' equipo: '%-.64s' (%-.64s)"
4275-
swe "Avbröt länken för tråd %lld till db '%-.192s', användare '%-.48s', host '%-.64s' (%-.64s)"
4276-
ukr "Перервано з'єднання %lld до бази данних: '%-.192s' користувач: '%-.48s' хост: '%-.64s' (%-.64s)"
4261+
chi "终止的连接 %lld 到数据库: '%-.192s' 用户: '%-.48s' 主机: '%-.64s'%-.64s (%-.64s)"
4262+
cze "Spojení %lld do databáze: '%-.192s' uživatel: '%-.48s' stroj: '%-.64s'%-.64s (%-.64s) bylo přerušeno"
4263+
dan "Afbrød forbindelsen %lld til databasen '%-.192s' bruger: '%-.48s' vært: '%-.64s'%-.64s (%-.64s)"
4264+
eng "Aborted connection %lld to db: '%-.192s' user: '%-.48s' host: '%-.64s'%-.64s (%-.64s)"
4265+
est "Ühendus katkestatud %lld andmebaas: '%-.192s' kasutaja: '%-.48s' masin: '%-.64s'%-.64s (%-.64s)"
4266+
fre "Connection %lld avortée vers la bd: '%-.192s' utilisateur: '%-.48s' hôte: '%-.64s'%-.64s (%-.64s)"
4267+
ger "Abbruch der Verbindung %lld zur Datenbank '%-.192s'. Benutzer: '%-.48s', Host: '%-.64s'%-.64s (%-.64s)"
4268+
ita "Interrotta la connessione %lld al db: ''%-.192s' utente: '%-.48s' host: '%-.64s'%-.64s (%-.64s)"
4269+
jpn "接続 %lld が中断されました。データベース: '%-.192s' ユーザー: '%-.48s' ホスト: '%-.64s'%-.64s (%-.64s)"
4270+
nla "Afgebroken verbinding %lld naar db: '%-.192s' gebruiker: '%-.48s' host: '%-.64s'%-.64s (%-.64s)"
4271+
por "Conexão %lld abortada para banco de dados '%-.192s' - usuário '%-.48s' - 'host' '%-.64s'%-.64s ('%-.64s')"
4272+
rus "Прервано соединение %lld к базе данных '%-.192s' пользователя '%-.48s' с хоста '%-.64s'%-.64s (%-.64s)"
4273+
serbian "Prekinuta konekcija broj %lld ka bazi: '%-.192s' korisnik je bio: '%-.48s' a host: '%-.64s'%-.64s (%-.64s)"
4274+
spa "Abortada conexión %lld a la base de datos: '%-.192s' usuario: '%-.48s' equipo: '%-.64s'%-.64s (%-.64s)"
4275+
swe "Avbröt länken för tråd %lld till db '%-.192s', användare '%-.48s', host '%-.64s'%-.64s (%-.64s)"
4276+
ukr "Перервано з'єднання %lld до бази данних: '%-.192s' користувач: '%-.48s' хост: '%-.64s'%-.64s (%-.64s)"
42774277
ER_UNUSED_10
42784278
eng "You should never see it"
42794279
spa "Nunca lo debería vd de ver"

sql/sql_class.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5218,11 +5218,29 @@ class THD: public THD_count, /* this must be first */
52185218
{
52195219
if (global_system_variables.log_warnings > threshold)
52205220
{
5221+
char real_ip_str[64];
5222+
real_ip_str[0]= 0;
5223+
5224+
/* For proxied connections, add the real IP to the warning message */
5225+
if (net.using_proxy_protocol && net.vio)
5226+
{
5227+
if(net.vio->localhost)
5228+
snprintf(real_ip_str, sizeof(real_ip_str), " real ip: 'localhost'");
5229+
else
5230+
{
5231+
char buf[INET6_ADDRSTRLEN];
5232+
if (!vio_getnameinfo((sockaddr *)&(net.vio->remote), buf,
5233+
sizeof(buf),NULL, 0, NI_NUMERICHOST))
5234+
{
5235+
snprintf(real_ip_str, sizeof(real_ip_str), " real ip: '%s'",buf);
5236+
}
5237+
}
5238+
}
52215239
Security_context *sctx= &main_security_ctx;
52225240
sql_print_warning(ER_THD(this, ER_NEW_ABORTING_CONNECTION),
52235241
thread_id, (db.str ? db.str : "unconnected"),
52245242
sctx->user ? sctx->user : "unauthenticated",
5225-
sctx->host_or_ip, reason);
5243+
sctx->host_or_ip, real_ip_str, reason);
52265244
}
52275245
}
52285246

sql/sql_connect.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,7 @@ void prepare_new_connection_state(THD* thd)
12741274
thd->thread_id,
12751275
thd->db.str ? thd->db.str : "unconnected",
12761276
sctx->user ? sctx->user : "unauthenticated",
1277-
sctx->host_or_ip, "init_connect command failed");
1277+
sctx->host_or_ip, "", "init_connect command failed");
12781278
thd->server_status&= ~SERVER_STATUS_CLEAR_SET;
12791279
thd->protocol->end_statement();
12801280
thd->killed = KILL_CONNECTION;

tests/mysql_client_test.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20564,7 +20564,6 @@ typedef struct {
2056420564
#ifndef EMBEDDED_LIBRARY
2056520565
static void test_proxy_header_tcp(const char *ipaddr, int port)
2056620566
{
20567-
2056820567
int rc;
2056920568
MYSQL_RES *result;
2057020569
int family = (strchr(ipaddr,':') == NULL)?AF_INET:AF_INET6;
@@ -20639,6 +20638,11 @@ static void test_proxy_header_tcp(const char *ipaddr, int port)
2063920638
DIE_UNLESS(strncmp(row[0], normalized_addr, addrlen) == 0);
2064020639
DIE_UNLESS(atoi(row[0] + addrlen+1) == port);
2064120640
mysql_free_result(result);
20641+
if (i == 0 && !strcmp(ipaddr,"192.0.2.1"))
20642+
{
20643+
/* do "dirty" close, to get aborted message in error log.*/
20644+
mariadb_cancel(m);
20645+
}
2064220646
mysql_close(m);
2064320647
}
2064420648
sprintf(query,"DROP USER 'u'@'%s'",normalized_addr);

0 commit comments

Comments
 (0)