2002-05-27 13:23:27 +02:00
|
|
|
/*
|
|
|
|
* ngIRCd -- The Next Generation IRC Daemon
|
2003-12-27 14:01:12 +01:00
|
|
|
* Copyright (c)2001-2003 by Alexander Barton (alex@barton.de)
|
2002-05-27 13:23:27 +02:00
|
|
|
*
|
2002-12-12 13:24:18 +01:00
|
|
|
* 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.
|
|
|
|
* Please read the file COPYING, README and AUTHORS for more information.
|
2002-05-27 13:23:27 +02:00
|
|
|
*
|
2002-12-12 13:24:18 +01:00
|
|
|
* Asynchronous resolver
|
2002-05-27 13:23:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "portab.h"
|
|
|
|
|
2004-05-11 02:01:11 +02:00
|
|
|
static char UNUSED id[] = "$Id: resolve.c,v 1.9 2004/05/11 00:01:11 alex Exp $";
|
2002-12-12 13:24:18 +01:00
|
|
|
|
2002-05-27 13:23:27 +02:00
|
|
|
#include "imp.h"
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
2003-12-27 14:01:12 +01:00
|
|
|
#ifdef IDENTAUTH
|
|
|
|
#ifdef HAVE_IDENT_H
|
|
|
|
#include <ident.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2002-05-27 13:23:27 +02:00
|
|
|
#include "conn.h"
|
|
|
|
#include "defines.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
#include "exp.h"
|
|
|
|
#include "resolve.h"
|
|
|
|
|
|
|
|
|
2003-12-27 14:01:12 +01:00
|
|
|
#ifdef IDENTAUTH
|
|
|
|
LOCAL VOID Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, INT Sock, INT w_fd ));
|
|
|
|
#else
|
2002-05-27 13:23:27 +02:00
|
|
|
LOCAL VOID Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, INT w_fd ));
|
2003-12-27 14:01:12 +01:00
|
|
|
#endif
|
|
|
|
|
2002-05-27 13:23:27 +02:00
|
|
|
LOCAL VOID Do_ResolveName PARAMS(( CHAR *Host, INT w_fd ));
|
|
|
|
|
|
|
|
#ifdef h_errno
|
2002-05-30 18:52:20 +02:00
|
|
|
LOCAL CHAR *Get_Error PARAMS(( INT H_Error ));
|
2002-05-27 13:23:27 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
GLOBAL VOID
|
|
|
|
Resolve_Init( VOID )
|
|
|
|
{
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Initialize module */
|
2002-05-27 13:23:27 +02:00
|
|
|
|
|
|
|
FD_ZERO( &Resolver_FDs );
|
|
|
|
} /* Resolve_Init */
|
|
|
|
|
|
|
|
|
2003-12-27 14:01:12 +01:00
|
|
|
#ifdef IDENTAUTH
|
|
|
|
GLOBAL RES_STAT *
|
|
|
|
Resolve_Addr( struct sockaddr_in *Addr, int Sock )
|
|
|
|
#else
|
2002-05-27 13:23:27 +02:00
|
|
|
GLOBAL RES_STAT *
|
|
|
|
Resolve_Addr( struct sockaddr_in *Addr )
|
2003-12-27 14:01:12 +01:00
|
|
|
#endif
|
2002-05-27 13:23:27 +02:00
|
|
|
{
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Resolve IP (asynchronous!). On errors, e.g. if the child process
|
|
|
|
* can't be forked, this functions returns NULL. */
|
2002-05-27 13:23:27 +02:00
|
|
|
|
|
|
|
RES_STAT *s;
|
|
|
|
INT pid;
|
|
|
|
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Allocate memory */
|
2004-03-11 23:16:31 +01:00
|
|
|
s = (RES_STAT *)malloc( sizeof( RES_STAT ));
|
2002-05-27 13:23:27 +02:00
|
|
|
if( ! s )
|
|
|
|
{
|
2002-06-09 15:19:08 +02:00
|
|
|
Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" );
|
2002-05-27 13:23:27 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Initialize pipe for result */
|
2002-05-27 13:23:27 +02:00
|
|
|
if( pipe( s->pipe ) != 0 )
|
|
|
|
{
|
|
|
|
free( s );
|
|
|
|
Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-12-27 14:01:12 +01:00
|
|
|
/* For sub-process */
|
2002-05-27 13:23:27 +02:00
|
|
|
pid = fork( );
|
|
|
|
if( pid > 0 )
|
|
|
|
{
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Main process */
|
2002-05-27 13:23:27 +02:00
|
|
|
Log( LOG_DEBUG, "Resolver for %s created (PID %d).", inet_ntoa( Addr->sin_addr ), pid );
|
|
|
|
FD_SET( s->pipe[0], &Resolver_FDs );
|
|
|
|
if( s->pipe[0] > Conn_MaxFD ) Conn_MaxFD = s->pipe[0];
|
|
|
|
s->pid = pid;
|
2004-05-11 02:01:11 +02:00
|
|
|
s->stage = 0;
|
|
|
|
s->bufpos = 0;
|
2002-05-27 13:23:27 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
else if( pid == 0 )
|
|
|
|
{
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Sub process */
|
2002-05-27 13:23:27 +02:00
|
|
|
Log_Init_Resolver( );
|
2003-12-27 14:01:12 +01:00
|
|
|
#ifdef IDENTAUTH
|
|
|
|
Do_ResolveAddr( Addr, Sock, s->pipe[1] );
|
|
|
|
#else
|
2002-05-27 13:23:27 +02:00
|
|
|
Do_ResolveAddr( Addr, s->pipe[1] );
|
2003-12-27 14:01:12 +01:00
|
|
|
#endif
|
2002-05-27 13:23:27 +02:00
|
|
|
Log_Exit_Resolver( );
|
|
|
|
exit( 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Error! */
|
2002-05-27 13:23:27 +02:00
|
|
|
free( s );
|
|
|
|
Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} /* Resolve_Addr */
|
|
|
|
|
|
|
|
|
|
|
|
GLOBAL RES_STAT *
|
|
|
|
Resolve_Name( CHAR *Host )
|
|
|
|
{
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Resolve hostname (asynchronous!). On errors, e.g. if the child
|
|
|
|
* process can't be forked, this functions returns NULL. */
|
2002-05-27 13:23:27 +02:00
|
|
|
|
|
|
|
RES_STAT *s;
|
|
|
|
INT pid;
|
|
|
|
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Allocate memory */
|
2004-03-11 23:16:31 +01:00
|
|
|
s = (RES_STAT *)malloc( sizeof( RES_STAT ));
|
2002-05-27 13:23:27 +02:00
|
|
|
if( ! s )
|
|
|
|
{
|
2002-06-09 15:19:08 +02:00
|
|
|
Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Name]" );
|
2002-05-27 13:23:27 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Initialize the pipe for the result */
|
2002-05-27 13:23:27 +02:00
|
|
|
if( pipe( s->pipe ) != 0 )
|
|
|
|
{
|
|
|
|
free( s );
|
|
|
|
Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Fork sub-process */
|
2002-05-27 13:23:27 +02:00
|
|
|
pid = fork( );
|
|
|
|
if( pid > 0 )
|
|
|
|
{
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Main process */
|
2002-05-27 13:23:27 +02:00
|
|
|
Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
|
|
|
|
FD_SET( s->pipe[0], &Resolver_FDs );
|
|
|
|
if( s->pipe[0] > Conn_MaxFD ) Conn_MaxFD = s->pipe[0];
|
|
|
|
s->pid = pid;
|
2004-05-11 02:01:11 +02:00
|
|
|
s->stage = 0;
|
|
|
|
s->bufpos = 0;
|
2002-05-27 13:23:27 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
else if( pid == 0 )
|
|
|
|
{
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Sub process */
|
2002-05-27 13:23:27 +02:00
|
|
|
Log_Init_Resolver( );
|
|
|
|
Do_ResolveName( Host, s->pipe[1] );
|
|
|
|
Log_Exit_Resolver( );
|
|
|
|
exit( 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Error! */
|
2002-05-27 13:23:27 +02:00
|
|
|
free( s );
|
|
|
|
Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} /* Resolve_Name */
|
|
|
|
|
|
|
|
|
2003-12-27 14:01:12 +01:00
|
|
|
#ifdef IDENTAUTH
|
|
|
|
LOCAL VOID
|
|
|
|
Do_ResolveAddr( struct sockaddr_in *Addr, int Sock, INT w_fd )
|
|
|
|
#else
|
2002-05-27 13:23:27 +02:00
|
|
|
LOCAL VOID
|
|
|
|
Do_ResolveAddr( struct sockaddr_in *Addr, INT w_fd )
|
2003-12-27 14:01:12 +01:00
|
|
|
#endif
|
2002-05-27 13:23:27 +02:00
|
|
|
{
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Resolver sub-process: resolve IP address and write result into
|
|
|
|
* pipe to parent. */
|
2002-05-27 13:23:27 +02:00
|
|
|
|
|
|
|
CHAR hostname[HOST_LEN];
|
|
|
|
struct hostent *h;
|
2004-05-11 02:01:11 +02:00
|
|
|
INT len;
|
2003-12-27 14:01:12 +01:00
|
|
|
#ifdef IDENTAUTH
|
|
|
|
CHAR *res;
|
|
|
|
#endif
|
2002-05-27 13:23:27 +02:00
|
|
|
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Resolve IP address */
|
2004-05-11 02:01:11 +02:00
|
|
|
Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
|
2002-05-27 13:23:27 +02:00
|
|
|
h = gethostbyaddr( (CHAR *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
|
2002-12-26 18:04:54 +01:00
|
|
|
if( h ) strlcpy( hostname, h->h_name, sizeof( hostname ));
|
2002-05-27 13:23:27 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef h_errno
|
|
|
|
Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\": %s!", inet_ntoa( Addr->sin_addr ), Get_Error( h_errno ));
|
|
|
|
#else
|
|
|
|
Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\"!", inet_ntoa( Addr->sin_addr ));
|
|
|
|
#endif
|
2002-12-26 18:04:54 +01:00
|
|
|
strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
|
2002-05-27 13:23:27 +02:00
|
|
|
}
|
2004-05-11 02:01:11 +02:00
|
|
|
Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
|
2002-05-27 13:23:27 +02:00
|
|
|
|
2004-05-11 02:01:11 +02:00
|
|
|
/* Write resolver result into pipe to parent */
|
|
|
|
len = strlen( hostname );
|
|
|
|
hostname[len] = '\n'; len++;
|
|
|
|
if( (size_t)write( w_fd, hostname, len ) != (size_t)len )
|
2002-05-27 13:23:27 +02:00
|
|
|
{
|
|
|
|
Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
|
|
|
|
close( w_fd );
|
|
|
|
return;
|
|
|
|
}
|
2004-05-11 02:01:11 +02:00
|
|
|
|
2003-12-27 14:01:12 +01:00
|
|
|
#ifdef IDENTAUTH
|
2004-05-11 02:01:11 +02:00
|
|
|
/* Do "IDENT" (aka "AUTH") lookup and write result to parent */
|
|
|
|
Log_Resolver( LOG_DEBUG, "Doing IDENT lookup on socket %d ...", Sock );
|
|
|
|
res = ident_id( Sock, 10 );
|
|
|
|
Log_Resolver( LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"", Sock, res ? res : "" );
|
|
|
|
|
|
|
|
/* Write IDENT result into pipe to parent */
|
|
|
|
len = strlen( res ? res : "" );
|
|
|
|
if( res != NULL ) res[len] = '\n';
|
|
|
|
len++;
|
|
|
|
if( (size_t)write( w_fd, res ? res : "\n", len ) != (size_t)len )
|
2003-12-27 14:01:12 +01:00
|
|
|
{
|
|
|
|
Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent (IDENT): %s!", strerror( errno ));
|
|
|
|
close( w_fd );
|
|
|
|
}
|
|
|
|
free( res );
|
|
|
|
#endif
|
2002-05-27 13:23:27 +02:00
|
|
|
} /* Do_ResolveAddr */
|
|
|
|
|
|
|
|
|
|
|
|
LOCAL VOID
|
|
|
|
Do_ResolveName( CHAR *Host, INT w_fd )
|
|
|
|
{
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Resolver sub-process: resolve name and write result into pipe
|
|
|
|
* to parent. */
|
2002-05-27 13:23:27 +02:00
|
|
|
|
|
|
|
CHAR ip[16];
|
|
|
|
struct hostent *h;
|
|
|
|
struct in_addr *addr;
|
2004-05-11 02:01:11 +02:00
|
|
|
INT len;
|
2002-05-27 13:23:27 +02:00
|
|
|
|
|
|
|
Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
|
|
|
|
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Resolve hostname */
|
2002-05-27 13:23:27 +02:00
|
|
|
h = gethostbyname( Host );
|
|
|
|
if( h )
|
|
|
|
{
|
|
|
|
addr = (struct in_addr *)h->h_addr;
|
2002-12-26 18:04:54 +01:00
|
|
|
strlcpy( ip, inet_ntoa( *addr ), sizeof( ip ));
|
2002-05-27 13:23:27 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef h_errno
|
|
|
|
Log_Resolver( LOG_WARNING, "Can't resolve \"%s\": %s!", Host, Get_Error( h_errno ));
|
|
|
|
#else
|
|
|
|
Log_Resolver( LOG_WARNING, "Can't resolve \"%s\"!", Host );
|
|
|
|
#endif
|
|
|
|
strcpy( ip, "" );
|
|
|
|
}
|
2004-05-11 02:01:11 +02:00
|
|
|
if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
|
2002-05-27 13:23:27 +02:00
|
|
|
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Write result into pipe to parent */
|
2004-05-11 02:01:11 +02:00
|
|
|
len = strlen( ip );
|
|
|
|
ip[len] = '\n'; len++;
|
|
|
|
if( (size_t)write( w_fd, ip, len ) != (size_t)len )
|
2002-05-27 13:23:27 +02:00
|
|
|
{
|
|
|
|
Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
|
|
|
|
close( w_fd );
|
|
|
|
}
|
|
|
|
} /* Do_ResolveName */
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef h_errno
|
|
|
|
|
|
|
|
LOCAL CHAR *
|
|
|
|
Get_Error( INT H_Error )
|
|
|
|
{
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Get error message for H_Error */
|
2002-05-27 13:23:27 +02:00
|
|
|
|
|
|
|
switch( H_Error )
|
|
|
|
{
|
|
|
|
case HOST_NOT_FOUND:
|
|
|
|
return "host not found";
|
|
|
|
case NO_DATA:
|
|
|
|
return "name valid but no IP address defined";
|
|
|
|
case NO_RECOVERY:
|
|
|
|
return "name server error";
|
|
|
|
case TRY_AGAIN:
|
|
|
|
return "name server temporary not available";
|
|
|
|
default:
|
|
|
|
return "unknown error";
|
|
|
|
}
|
|
|
|
} /* Get_Error */
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* -eof- */
|