Skip to content

Commit d695e2d

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 ed027d6 commit d695e2d

File tree

8 files changed

+52
-19
lines changed

8 files changed

+52
-19
lines changed

include/mysql_com.h

+1
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ typedef struct st_net {
456456
my_bool thread_specific_malloc;
457457
unsigned char compress;
458458
my_bool unused3; /* Please remove with the next incompatible ABI change. */
459+
my_bool using_proxy_protocol;
459460
/*
460461
Pointer to query object in query cache, do not equal NULL (0) for
461462
queries in cache that have not stored its results yet

mysql-test/main/mysql_client_test.result

+1
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

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

sql/net_serv.cc

+3
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ my_bool my_net_init(NET *net, Vio *vio, void *thd, uint my_flags)
151151
net->where_b = net->remain_in_buf=0;
152152
net->net_skip_rest_factor= 0;
153153
net->last_errno=0;
154+
net->using_proxy_protocol= 0;
154155
net->thread_specific_malloc= MY_TEST(my_flags & MY_THREAD_SPECIFIC);
155156
net->thd= 0;
156157
#ifdef MYSQL_SERVER
@@ -192,6 +193,7 @@ void net_end(NET *net)
192193
DBUG_ENTER("net_end");
193194
my_free(net->buff);
194195
net->buff=0;
196+
net->using_proxy_protocol= 0;
195197
DBUG_VOID_RETURN;
196198
}
197199

@@ -908,6 +910,7 @@ static handle_proxy_header_result handle_proxy_header(NET *net)
908910
return RETRY;
909911
/* Change peer address in THD and ACL structures.*/
910912
uint host_errors;
913+
net->using_proxy_protocol= 1;
911914
return (handle_proxy_header_result)thd_set_peer_addr(thd,
912915
&(peer_info.peer_addr), NULL, peer_info.port,
913916
false, &host_errors);

sql/share/errmsg-utf8.txt

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

sql/sql_class.h

+19-1
Original file line numberDiff line numberDiff line change
@@ -4773,11 +4773,29 @@ class THD: public THD_count, /* this must be first */
47734773
{
47744774
if (global_system_variables.log_warnings > threshold)
47754775
{
4776+
char real_ip_str[64];
4777+
real_ip_str[0]= 0;
4778+
4779+
/* For proxied connections, add the real IP to the warning message */
4780+
if (net.using_proxy_protocol && net.vio)
4781+
{
4782+
if(net.vio->localhost)
4783+
snprintf(real_ip_str, sizeof(real_ip_str), " real ip: 'localhost'");
4784+
else
4785+
{
4786+
char buf[INET6_ADDRSTRLEN];
4787+
if (!vio_getnameinfo((sockaddr *)&(net.vio->remote), buf,
4788+
sizeof(buf),NULL, 0, NI_NUMERICHOST))
4789+
{
4790+
snprintf(real_ip_str, sizeof(real_ip_str), " real ip: '%s'",buf);
4791+
}
4792+
}
4793+
}
47764794
Security_context *sctx= &main_security_ctx;
47774795
sql_print_warning(ER_THD(this, ER_NEW_ABORTING_CONNECTION),
47784796
thread_id, (db.str ? db.str : "unconnected"),
47794797
sctx->user ? sctx->user : "unauthenticated",
4780-
sctx->host_or_ip, reason);
4798+
sctx->host_or_ip, real_ip_str, reason);
47814799
}
47824800
}
47834801

sql/sql_connect.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ void prepare_new_connection_state(THD* thd)
12821282
thd->thread_id,
12831283
thd->db.str ? thd->db.str : "unconnected",
12841284
sctx->user ? sctx->user : "unauthenticated",
1285-
sctx->host_or_ip, "init_connect command failed");
1285+
sctx->host_or_ip, "", "init_connect command failed");
12861286
thd->server_status&= ~SERVER_STATUS_CLEAR_SET;
12871287
thd->protocol->end_statement();
12881288
thd->killed = KILL_CONNECTION;

tests/mysql_client_test.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -20546,7 +20546,6 @@ typedef struct {
2054620546
#ifndef EMBEDDED_LIBRARY
2054720547
static void test_proxy_header_tcp(const char *ipaddr, int port)
2054820548
{
20549-
2055020549
int rc;
2055120550
MYSQL_RES *result;
2055220551
int family = (strchr(ipaddr,':') == NULL)?AF_INET:AF_INET6;
@@ -20621,6 +20620,11 @@ static void test_proxy_header_tcp(const char *ipaddr, int port)
2062120620
DIE_UNLESS(strncmp(row[0], normalized_addr, addrlen) == 0);
2062220621
DIE_UNLESS(atoi(row[0] + addrlen+1) == port);
2062320622
mysql_free_result(result);
20623+
if (i == 0 && !strcmp(ipaddr,"192.0.2.1"))
20624+
{
20625+
/* do "dirty" close, to get aborted message in error log.*/
20626+
mariadb_cancel(m);
20627+
}
2062420628
mysql_close(m);
2062520629
}
2062620630
sprintf(query,"DROP USER 'u'@'%s'",normalized_addr);

0 commit comments

Comments
 (0)