From 351d8e39ca6cc8f76b6163593624e13e8f33b30a Mon Sep 17 00:00:00 2001
From: unknown <dlenev@mysql.com>
Date: Thu, 22 Sep 2005 23:03:53 +0400
Subject: [PATCH] Fix for bug #13407 "Remote connecting crashes server" and
 couple of similar problems which were introduced during work on WL#2787 "Add
 view definer/owner to the view definition (.frm) to check privileges on used
 tables and stored routines when using a VIEW."

In some cases it is better to use value from Security_context::host_or_ip
instead of value from Security_context::host since the latter can be NULL
in certain situations (When we can't resolve hostname or we have not done
this yet).


mysql-test/r/skip_name_resolve.result:
  Added test for bug #13407 "Remote connecting crashes server".
mysql-test/t/skip_name_resolve.test:
  Added test for bug #13407 "Remote connecting crashes server".
sql/item_strfunc.cc:
  Item_func_user::val_str():
    It is better to use value from Security_context::host_or_ip instead of
    value from Security_context::host in the result of USER() function since
    the latter can be NULL in certain situations (When we can't resolve hostname
    or we have not done this yet. Reverted back to old code).
sql/mysqld.cc:
  handle_connections_shared_memory():
   THD::host member has moved to the Security_context class.
   New THD::security_ctx member points to active security context.
sql/sql_show.cc:
  mysqld_list_processes():
    It is better to use value from Security_context::host_or_ip instead of
    value from Security_context::host in the processlist since the latter
    can be NULL in certain situations (When we can't resolve hostname or we
    have not done this yet. Reverted back to old code).
---
 mysql-test/r/skip_name_resolve.result |  7 +++++++
 mysql-test/t/skip_name_resolve.test   | 10 ++++++++++
 sql/item_strfunc.cc                   |  2 +-
 sql/mysqld.cc                         |  2 +-
 sql/sql_show.cc                       |  2 +-
 5 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/mysql-test/r/skip_name_resolve.result b/mysql-test/r/skip_name_resolve.result
index d8d873699a5..a969c5c9ae0 100644
--- a/mysql-test/r/skip_name_resolve.result
+++ b/mysql-test/r/skip_name_resolve.result
@@ -5,3 +5,10 @@ GRANT USAGE ON *.* TO 'mysqltest_1'@'127.0.0.1/255.255.255.255'
 GRANT ALL PRIVILEGES ON `test`.* TO 'mysqltest_1'@'127.0.0.1/255.255.255.255'
 REVOKE ALL ON test.* FROM mysqltest_1@'127.0.0.1/255.255.255.255';
 DROP USER mysqltest_1@'127.0.0.1/255.255.255.255';
+select user();
+user()
+#
+show processlist;
+Id	User	Host	db	Command	Time	State	Info
+#	root	#	test	Sleep	#		NULL
+#	root	#	test	Query	#	NULL	show processlist
diff --git a/mysql-test/t/skip_name_resolve.test b/mysql-test/t/skip_name_resolve.test
index 02339ca14c5..b67869692d2 100644
--- a/mysql-test/t/skip_name_resolve.test
+++ b/mysql-test/t/skip_name_resolve.test
@@ -8,3 +8,13 @@ REVOKE ALL ON test.* FROM mysqltest_1@'127.0.0.1/255.255.255.255';
 DROP USER mysqltest_1@'127.0.0.1/255.255.255.255';
 
 # End of 4.1 tests
+
+# Bug #13407 "Remote connecting crashes server".
+# Server crashed when one used USER() function in connection for which
+# was impossible to obtain peer hostname.
+connect (con1, 127.0.0.1, root, , test, $MASTER_MYPORT, );
+--replace_column 1 #
+select user();
+--replace_column 1 # 6 # 3 #
+show processlist;
+connection default;
diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc
index 54c476ce176..f0486246a65 100644
--- a/sql/item_strfunc.cc
+++ b/sql/item_strfunc.cc
@@ -1608,7 +1608,7 @@ String *Item_func_user::val_str(String *str)
   else
   {
     user= thd->main_security_ctx.user;
-    host= thd->main_security_ctx.host;
+    host= thd->main_security_ctx.host_or_ip;
   }
 
   // For system threads (e.g. replication SQL thread) user may be empty
diff --git a/sql/mysqld.cc b/sql/mysqld.cc
index 097eb9ccf17..677b68ca946 100644
--- a/sql/mysqld.cc
+++ b/sql/mysqld.cc
@@ -4208,7 +4208,7 @@ pthread_handler_decl(handle_connections_shared_memory,arg)
       errmsg= 0;
       goto errorconn;
     }
-    thd->host= my_strdup(my_localhost,MYF(0)); /* Host is unknown */
+    thd->security_ctx->host= my_strdup(my_localhost, MYF(0)); /* Host is unknown */
     create_new_thread(thd);
     connect_number++;
     continue;
diff --git a/sql/sql_show.cc b/sql/sql_show.cc
index cdbe0194303..cdd2818d897 100644
--- a/sql/sql_show.cc
+++ b/sql/sql_show.cc
@@ -1205,7 +1205,7 @@ void mysqld_list_processes(THD *thd,const char *user, bool verbose)
 			"%s:%u", tmp_sctx->host_or_ip, tmp->peer_port);
 	}
 	else
-	  thd_info->host= thd->strdup(tmp_sctx->host);
+	  thd_info->host= thd->strdup(tmp_sctx->host_or_ip);
         if ((thd_info->db=tmp->db))             // Safe test
           thd_info->db=thd->strdup(thd_info->db);
         thd_info->command=(int) tmp->command;
-- 
2.30.9