mariadb/sql/hostname.cc
unknown 84f81dc593 Fixes for Netware
Call pthread_mutex_destroy() on not used mutex.
Changed comments in .h and .c files from // -> /* */
Added detection of mutex on which one didn't call pthread_mutex_destroy()
Fixed bug in create_tmp_field() which causes a memory overrun in queries that uses "ORDER BY constant_expression"
Added optimisation for ORDER BY NULL


BitKeeper/deleted/.del-ChangeLog~dfc92e15bee6fc75:
  Delete: sql/ChangeLog
BUILD/compile-pentium-valgrind-max:
  Don't use valgrind with safemalloc as this can hide some bugs
Makefile.am:
  Added platform dirs
bdb/os/os_handle.c:
  Portability fix
client/mysql.cc:
  Fixes for Netware.
  Fixed duplicate output when using 'tee'
  Simple optimisations
client/mysqldump.c:
  Portability fix
client/mysqltest.c:
  Portability fix
configure.in:
  Fixes for Netware
extra/resolveip.c:
  Fixes for Netware
include/Makefile.am:
  Fixes for Netware
include/config-win.h:
  Portability fix
include/my_global.h:
  Fixes for Netware
include/my_net.h:
  Fixes for Netware
include/my_pthread.h:
  Fixes for Netware
  Added detection of mutexes that was not destroyed
include/my_sys.h:
  Fixes for Netware
  Added 'extern' before external functions
include/mysql.h:
  Fixes for Netware
innobase/configure.in:
  Fixes for Netware
innobase/include/os0thread.h:
  Fixes for Netware
innobase/os/os0sync.c:
  Fixes for Netware
innobase/os/os0thread.c:
  Fixes for Netware
innobase/srv/srv0srv.c:
  Fixes for Netware
innobase/srv/srv0start.c:
  Fixes for Netware
innobase/sync/sync0sync.c:
  Fixes for Netware
isam/test3.c:
  Disable test on Netware
libmysql/Makefile.shared:
  Added my_sleep
libmysql/get_password.c:
  Fixes for Netware
libmysql/libmysql.c:
  Fixes for Netware
  Made mysql_once_init() global
libmysql/manager.c:
  Fixes for Netware
myisam/mi_test3.c:
  Disable test for netware
mysql-test/mysql-test-run.sh:
  Give warning if output file contains errors
mysql-test/r/count_distinct.result:
  More tests
mysql-test/r/group_by.result:
  Test of ORDER BY NULL
mysql-test/t/backup.test:
  Fixes for Netware
mysql-test/t/count_distinct.test:
  More tests
mysql-test/t/func_crypt.test:
  Fixes for Netware
mysql-test/t/grant_cache.test:
  Fixes for Netware
mysql-test/t/group_by.test:
  Tests of ORDER BY NULL
mysql-test/t/rpl000015.test:
  Fixes for Netware
mysql-test/t/rpl000017.test:
  Fixes for Netware
mysql-test/t/rpl_rotate_logs.test:
  Fixes for Netware
mysys/Makefile.am:
  Added my_sleep.c and my_netware.c
mysys/charset.c:
  Fixes for Netware
mysys/default.c:
  Fixes for Netware
mysys/mf_tempfile.c:
  Fixes for Netware
mysys/my_clock.c:
  Fixes for Netware
mysys/my_copy.c:
  Fixes for Netware
mysys/my_getwd.c:
  Changed function comment from // -> /* */
mysys/my_init.c:
  Fixes for Netware
mysys/my_lock.c:
  Fixes for Netware
mysys/my_messnc.c:
  Fixes for Netware
mysys/my_os2cond.c:
  Removed comment
mysys/my_os2dirsrch.c:
  Changed function comment from // -> /* */
  Fixed indentation
mysys/my_os2dirsrch.h:
  Changed function comment from // -> /* */
  Fixed indentation
mysys/my_os2file64.c:
  Changed function comment from // -> /* */
  Fixed indentation
mysys/my_os2mutex.c:
  Changed function comment from // -> /* */
  Fixed indentation
mysys/my_os2thread.c:
  Changed function comment from // -> /* */
  Fixed indentation
mysys/my_os2tls.c:
  Changed function comment from // -> /* */
  Fixed indentation
mysys/my_pthread.c:
  Fixes for Netware
mysys/my_redel.c:
  Fixes for Netware
mysys/my_tempnam.c:
  Fixes for Netware
mysys/my_thr_init.c:
  Remove created mutexes when program ends.
mysys/mysys_priv.h:
  Cleanup
mysys/safemalloc.c:
  Prefix error messages with "Error:"
mysys/thr_alarm.c:
  Destroy internal mutex on end_thr_alarm.
mysys/thr_mutex.c:
  Added detection of mutex on which one didn't call pthread_mutex_destroy()
scripts/make_binary_distribution.sh:
  Fixes for Netware
sql/des_key_file.cc:
  Free mutex at end
sql/ha_innodb.cc:
  Free mutex at end
sql/ha_myisam.cc:
  Changed warnings from REPAIR Note:
  (For mysql-test-run)
sql/hostname.cc:
  Fixes for Netware
sql/item.h:
  Fixed bug in create_tmp_field() which causes a memory overrun
sql/item_func.cc:
  Free used mutexes
sql/item_sum.cc:
  Fixed bug in create_tmp_field() which causes a memory overrun
sql/log.cc:
  Free used mutexes
sql/my_lock.c:
  Fixes for Netware
sql/mysql_priv.h:
  Fixes for Netware
sql/mysqld.cc:
  Fixes for Netware
  Added Have_crypt
  Properly free mutexes from MYSQL_LOG by calling cleanup
  Free mutex before exit
sql/repl_failsafe.cc:
  Fixes for Netware
sql/set_var.cc:
  Added have_crypt
sql/share/english/errmsg.txt:
  Added version socket and port to stderr log
sql/slave.cc:
  Remove global MASTER_INFO variable and use instead an allocated variable.
  This allows us to correctly free used mutex.
sql/slave.h:
  Move constructors and destuctors to slave.cc
  (To make it easier to clear all needed variables)
sql/sql_base.cc:
  Safety fix
sql/sql_class.h:
  Portability fixes.
  Added 'cleanup' to log handling to be able to free mutexes.
sql/sql_insert.cc:
  Fixes for Netware
mysys/my_sleep.c:
  E
sql/sql_parse.cc:
  Fixes for Netware
sql/sql_select.cc:
  Added optimisation for ORDER BY NULL
sql/sql_select.h:
  Fixed bug in create_tmp_field() which causes a memory overrun
sql/sql_table.cc:
  Fixed bug in create_tmp_field() which causes a memory overrun
sql/sql_udf.cc:
  Free mutex on end
vio/test-ssl.c:
  Simple code cleanup
vio/test-sslclient.c:
  Simple code cleanup
vio/test-sslserver.c:
  Simple code cleanup
vio/viotest-ssl.c:
  Simple code cleanup
2003-01-28 08:38:28 +02:00

253 lines
6.6 KiB
C++

/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/*
Get hostname for an IP. Hostnames are checked with reverse name lookup and
checked that they doesn't resemble an ip.
*/
#include "mysql_priv.h"
#include "hash_filo.h"
#include <m_ctype.h>
#ifdef __cplusplus
extern "C" { // Because of SCO 3.2V4.2
#endif
#if !defined( __WIN__) && !defined(OS2)
#if !defined(__NETWARE__)
#include <sys/resource.h>
#endif /* __NETWARE__ */
#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
#endif
#include <netdb.h>
#include <sys/utsname.h>
#endif // __WIN__
#ifdef __cplusplus
}
#endif
class host_entry :public hash_filo_element
{
public:
char ip[sizeof(((struct in_addr *) 0)->s_addr)];
uint errors;
char *hostname;
};
static hash_filo *hostname_cache;
static pthread_mutex_t LOCK_hostname;
void hostname_cache_refresh()
{
hostname_cache->clear();
}
bool hostname_cache_init()
{
host_entry tmp;
uint offset= (uint) ((char*) (&tmp.ip) - (char*) &tmp);
(void) pthread_mutex_init(&LOCK_hostname,MY_MUTEX_INIT_SLOW);
if (!(hostname_cache=new hash_filo(HOST_CACHE_SIZE, offset,
sizeof(struct in_addr),NULL,
(hash_free_key) free)))
return 1;
hostname_cache->clear();
return 0;
}
void hostname_cache_free()
{
(void) pthread_mutex_destroy(&LOCK_hostname);
delete hostname_cache;
}
static void add_hostname(struct in_addr *in,const char *name)
{
if (!(specialflag & SPECIAL_NO_HOST_CACHE))
{
VOID(pthread_mutex_lock(&hostname_cache->lock));
host_entry *entry;
if (!(entry=(host_entry*) hostname_cache->search((gptr) &in->s_addr,0)))
{
uint length=name ? (uint) strlen(name) : 0;
if ((entry=(host_entry*) malloc(sizeof(host_entry)+length+1)))
{
char *new_name;
memcpy_fixed(&entry->ip, &in->s_addr, sizeof(in->s_addr));
if (length)
memcpy(new_name= (char *) (entry+1), name, length+1);
else
new_name=0;
entry->hostname=new_name;
entry->errors=0;
(void) hostname_cache->add(entry);
}
}
VOID(pthread_mutex_unlock(&hostname_cache->lock));
}
}
inline void add_wrong_ip(struct in_addr *in)
{
add_hostname(in,NullS);
}
void inc_host_errors(struct in_addr *in)
{
VOID(pthread_mutex_lock(&hostname_cache->lock));
host_entry *entry;
if ((entry=(host_entry*) hostname_cache->search((gptr) &in->s_addr,0)))
entry->errors++;
VOID(pthread_mutex_unlock(&hostname_cache->lock));
}
void reset_host_errors(struct in_addr *in)
{
VOID(pthread_mutex_lock(&hostname_cache->lock));
host_entry *entry;
if ((entry=(host_entry*) hostname_cache->search((gptr) &in->s_addr,0)))
entry->errors=0;
VOID(pthread_mutex_unlock(&hostname_cache->lock));
}
my_string ip_to_hostname(struct in_addr *in, uint *errors)
{
uint i;
host_entry *entry;
DBUG_ENTER("ip_to_hostname");
/* Check first if we have name in cache */
*errors=0;
if (!(specialflag & SPECIAL_NO_HOST_CACHE))
{
VOID(pthread_mutex_lock(&hostname_cache->lock));
if ((entry=(host_entry*) hostname_cache->search((gptr) &in->s_addr,0)))
{
char *name;
if (!entry->hostname)
name=0; // Don't allow connection
else
name=my_strdup(entry->hostname,MYF(0));
*errors= entry->errors;
VOID(pthread_mutex_unlock(&hostname_cache->lock));
DBUG_RETURN(name);
}
VOID(pthread_mutex_unlock(&hostname_cache->lock));
}
struct hostent *hp, *check;
char *name;
LINT_INIT(check);
#if defined(HAVE_GETHOSTBYADDR_R) && defined(HAVE_SOLARIS_STYLE_GETHOST)
char buff[GETHOSTBYADDR_BUFF_SIZE],buff2[GETHOSTBYNAME_BUFF_SIZE];
int tmp_errno;
struct hostent tmp_hostent, tmp_hostent2;
#ifdef HAVE_purify
bzero(buff,sizeof(buff)); // Bug in purify
#endif
if (!(hp=gethostbyaddr_r((char*) in,sizeof(*in),
AF_INET,
&tmp_hostent,buff,sizeof(buff),&tmp_errno)))
{
DBUG_PRINT("error",("gethostbyaddr_r returned %d",tmp_errno));
return 0;
}
if (!(check=my_gethostbyname_r(hp->h_name,&tmp_hostent2,buff2,sizeof(buff2),
&tmp_errno)))
{
DBUG_PRINT("error",("gethostbyname_r returned %d",tmp_errno));
add_wrong_ip(in);
my_gethostbyname_r_free();
DBUG_RETURN(0);
}
if (!hp->h_name[0])
{
DBUG_PRINT("error",("Got an empty hostname"));
add_wrong_ip(in);
my_gethostbyname_r_free();
DBUG_RETURN(0); // Don't allow empty hostnames
}
if (!(name=my_strdup(hp->h_name,MYF(0))))
{
my_gethostbyname_r_free();
DBUG_RETURN(0); // out of memory
}
my_gethostbyname_r_free();
#else
VOID(pthread_mutex_lock(&LOCK_hostname));
if (!(hp=gethostbyaddr((char*) in,sizeof(*in), AF_INET)))
{
VOID(pthread_mutex_unlock(&LOCK_hostname));
DBUG_PRINT("error",("gethostbyaddr returned %d",errno));
goto err;
}
if (!hp->h_name[0]) // Don't allow empty hostnames
{
VOID(pthread_mutex_unlock(&LOCK_hostname));
DBUG_PRINT("error",("Got an empty hostname"));
goto err;
}
if (!(name=my_strdup(hp->h_name,MYF(0))))
{
VOID(pthread_mutex_unlock(&LOCK_hostname));
DBUG_RETURN(0); // out of memory
}
check=gethostbyname(name);
VOID(pthread_mutex_unlock(&LOCK_hostname));
if (!check)
{
DBUG_PRINT("error",("gethostbyname returned %d",errno));
my_free(name,MYF(0));
DBUG_RETURN(0);
}
#endif
/* Don't accept hostnames that starts with digits because they may be
false ip:s */
if (isdigit(name[0]))
{
char *pos;
for (pos= name+1 ; isdigit(*pos); pos++) ;
if (*pos == '.')
{
DBUG_PRINT("error",("mysqld doesn't accept hostnames that starts with a number followed by a '.'"));
my_free(name,MYF(0));
goto err;
}
}
/* Check that 'gethostbyname' returned the used ip */
for (i=0; check->h_addr_list[i]; i++)
{
if (*(uint32*)(check->h_addr_list)[i] == in->s_addr)
{
add_hostname(in,name);
DBUG_RETURN(name);
}
}
DBUG_PRINT("error",("Couldn't verify hostname with gethostbyname"));
my_free(name,MYF(0));
err:
add_wrong_ip(in);
DBUG_RETURN(0);
}