ngircd-tor/src/ngircd/resolve.c

360 lines
8.4 KiB
C
Raw Normal View History

2002-05-27 13:23:27 +02:00
/*
* ngIRCd -- The Next Generation IRC Daemon
* Copyright (c)2001-2003 by Alexander Barton (alex@barton.de)
2002-05-27 13:23:27 +02: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
*
* Asynchronous resolver
2002-05-27 13:23:27 +02:00
*/
#include "portab.h"
static char UNUSED id[] = "$Id: resolve.c,v 1.23 2006/02/08 15:24:10 fw Exp $";
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>
#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"
2005-07-07 20:39:08 +02:00
#include "io.h"
2002-05-27 13:23:27 +02:00
static void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int Sock, int w_fd ));
2005-09-12 21:10:20 +02:00
static void Do_ResolveName PARAMS(( const char *Host, int w_fd ));
static bool register_callback PARAMS((RES_STAT *s, void (*cbfunc)(int, short)));
2002-05-27 13:23:27 +02:00
#ifdef h_errno
static char *Get_Error PARAMS(( int H_Error ));
2002-05-27 13:23:27 +02:00
#endif
2005-09-12 21:10:20 +02:00
static int
Resolver_fork(int *pipefds)
{
int pid;
if (pipe(pipefds) != 0) {
Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
return -1;
}
2002-05-27 13:23:27 +02:00
2005-09-12 21:10:20 +02:00
pid = fork();
switch(pid) {
case -1:
Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
close(pipefds[0]);
close(pipefds[1]);
return -1;
case 0: /* child */
close(pipefds[0]);
Log_Init_Resolver( );
return 0;
}
/* parent */
close(pipefds[1]);
return pid;
2005-07-07 20:39:08 +02:00
}
2002-05-27 13:23:27 +02:00
2005-09-12 21:10:20 +02:00
GLOBAL bool
Resolve_Addr( RES_STAT *s, struct sockaddr_in *Addr, int identsock, void (*cbfunc)(int, short))
2002-05-27 13:23:27 +02:00
{
2005-09-12 21:10:20 +02:00
/* Resolve IP (asynchronous!). */
int pid, pipefd[2];
assert(s != NULL);
2002-05-27 13:23:27 +02:00
2005-09-12 21:10:20 +02:00
pid = Resolver_fork(pipefd);
if (pid > 0) {
2005-09-12 21:10:20 +02:00
#ifdef DEBUG
2002-05-27 13:23:27 +02:00
Log( LOG_DEBUG, "Resolver for %s created (PID %d).", inet_ntoa( Addr->sin_addr ), pid );
2005-09-12 21:10:20 +02:00
#endif
2002-05-27 13:23:27 +02:00
s->pid = pid;
2005-09-12 21:10:20 +02:00
s->resolver_fd = pipefd[0];
return register_callback(s, cbfunc);
} else if( pid == 0 ) {
/* Sub process */
2005-09-12 21:10:20 +02:00
Do_ResolveAddr( Addr, identsock, pipefd[1]);
2002-05-27 13:23:27 +02:00
Log_Exit_Resolver( );
exit(0);
2002-05-27 13:23:27 +02:00
}
2005-09-12 21:10:20 +02:00
return false;
2002-05-27 13:23:27 +02:00
} /* Resolve_Addr */
2005-09-12 21:10:20 +02:00
GLOBAL bool
Resolve_Name( RES_STAT *s, const char *Host, void (*cbfunc)(int, short))
2002-05-27 13:23:27 +02:00
{
2005-09-12 21:10:20 +02:00
/* Resolve hostname (asynchronous!). */
int pid, pipefd[2];
assert(s != NULL);
2002-05-27 13:23:27 +02:00
2005-09-12 21:10:20 +02:00
pid = Resolver_fork(pipefd);
if (pid > 0) {
/* Main process */
2005-09-12 21:10:20 +02:00
#ifdef DEBUG
2002-05-27 13:23:27 +02:00
Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
2005-09-12 21:10:20 +02:00
#endif
2002-05-27 13:23:27 +02:00
s->pid = pid;
2005-09-12 21:10:20 +02:00
s->resolver_fd = pipefd[0];
return register_callback(s, cbfunc);
} else if( pid == 0 ) {
/* Sub process */
2005-09-12 21:10:20 +02:00
Do_ResolveName(Host, pipefd[1]);
2002-05-27 13:23:27 +02:00
Log_Exit_Resolver( );
exit(0);
2002-05-27 13:23:27 +02:00
}
2005-09-12 21:10:20 +02:00
return false;
} /* Resolve_Name */
2005-07-07 20:39:08 +02:00
2005-09-12 21:10:20 +02:00
GLOBAL void
Resolve_Init(RES_STAT *s)
{
assert(s != NULL);
s->resolver_fd = -1;
s->pid = 0;
}
2002-05-27 13:23:27 +02:00
static void
2005-09-12 21:10:20 +02:00
Do_ResolveAddr( struct sockaddr_in *Addr, int identsock, int w_fd )
2002-05-27 13:23:27 +02: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];
2005-07-24 23:42:00 +02:00
char ipstr[HOST_LEN];
2002-05-27 13:23:27 +02:00
struct hostent *h;
2005-05-28 12:46:50 +02:00
size_t len;
2005-07-24 23:42:00 +02:00
struct in_addr *addr;
char *ntoaptr;
array resolved_addr;
#ifdef IDENTAUTH
char *res;
#endif
array_init(&resolved_addr);
/* Resolve IP address */
#ifdef DEBUG
Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
#endif
h = gethostbyaddr( (char *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
2005-07-24 23:42:00 +02:00
if (!h) {
2002-05-27 13:23:27 +02:00
#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
strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
2005-07-24 23:42:00 +02:00
} else {
strlcpy( hostname, h->h_name, sizeof( hostname ));
h = gethostbyname( hostname );
if ( h ) {
if (memcmp(h->h_addr, &Addr->sin_addr, sizeof (struct in_addr))) {
addr = (struct in_addr*) h->h_addr;
strlcpy(ipstr, inet_ntoa(*addr), sizeof ipstr);
ntoaptr = inet_ntoa( Addr->sin_addr );
2005-07-24 23:42:00 +02:00
Log(LOG_WARNING,"Possible forgery: %s resolved to %s (which is at ip %s!)",
ntoaptr, hostname, ipstr);
strlcpy( hostname, ntoaptr, sizeof hostname);
2005-07-24 23:42:00 +02:00
}
} else {
ntoaptr = inet_ntoa( Addr->sin_addr );
2005-07-24 23:42:00 +02:00
Log(LOG_WARNING, "Possible forgery: %s resolved to %s (which has no ip address)",
ntoaptr, hostname);
strlcpy( hostname, ntoaptr, sizeof hostname);
2005-07-24 23:42:00 +02:00
}
2002-05-27 13:23:27 +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
len = strlen( hostname );
hostname[len] = '\n'; len++;
if (!array_copyb(&resolved_addr, hostname, len )) {
Log_Resolver( LOG_CRIT, "Resolver: Can't copy resolved name: %s!", strerror( errno ));
2002-05-27 13:23:27 +02:00
close( w_fd );
return;
}
#ifdef IDENTAUTH
2005-09-12 21:10:20 +02:00
assert(identsock >= 0);
if (identsock >= 0) {
/* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */
#ifdef DEBUG
Log_Resolver( LOG_DEBUG, "Doing IDENT lookup on socket %d ...", identsock );
#endif
res = ident_id( identsock, 10 );
#ifdef DEBUG
Log_Resolver(LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"",
identsock, res ? res : "(NULL)" );
#endif
if (res && !array_cats(&resolved_addr, res)) {
Log_Resolver(LOG_WARNING, "Resolver: Cannot copy IDENT result: %s!", strerror(errno));
2005-09-12 21:10:20 +02:00
/* omit ident and return hostname only */
}
2005-05-28 12:46:50 +02:00
2005-09-12 21:10:20 +02:00
if (res) free(res);
}
2005-09-12 21:10:20 +02:00
#else
(void)identsock;
#endif
len = array_bytes(&resolved_addr);
if( (size_t)write( w_fd, array_start(&resolved_addr), len) != len )
Log_Resolver( LOG_CRIT, "Resolver: Can't write result to parent: %s!", strerror( errno ));
close(w_fd);
array_free(&resolved_addr);
2002-05-27 13:23:27 +02:00
} /* Do_ResolveAddr */
static void
2005-09-12 21:10:20 +02:00
Do_ResolveName( const char *Host, int w_fd )
2002-05-27 13:23:27 +02:00
{
/* Resolver sub-process: resolve name and write result into pipe
* to parent. */
2002-05-27 13:23:27 +02:00
char ip[16];
2002-05-27 13:23:27 +02:00
struct hostent *h;
struct in_addr *addr;
int len;
2002-05-27 13:23:27 +02:00
Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
/* Resolve hostname */
2002-05-27 13:23:27 +02:00
h = gethostbyname( Host );
if( h ) {
2002-05-27 13:23:27 +02:00
addr = (struct in_addr *)h->h_addr;
strlcpy( ip, inet_ntoa( *addr ), sizeof( ip ));
} else {
2002-05-27 13:23:27 +02:00
#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
2005-09-12 21:10:20 +02:00
close(w_fd);
return;
2002-05-27 13:23:27 +02:00
}
2005-09-12 21:10:20 +02:00
#ifdef DEBUG
Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
#endif
/* Write result into pipe to parent */
len = strlen( ip );
2006-02-08 16:20:21 +01:00
if( write( w_fd, ip, len ) != 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
static char *
Get_Error( int H_Error )
2002-05-27 13:23:27 +02: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
2005-09-12 21:10:20 +02:00
static bool
register_callback( RES_STAT *s, void (*cbfunc)(int, short))
{
2005-09-12 21:10:20 +02:00
assert(cbfunc);
assert(s != NULL);
assert(s->resolver_fd >= 0);
2005-09-12 21:10:20 +02:00
if (io_setnonblock(s->resolver_fd) &&
io_event_create(s->resolver_fd, IO_WANTREAD, cbfunc))
return true;
2005-09-12 21:10:20 +02:00
Log( LOG_CRIT, "Resolver: Could not register callback function: %s!", strerror(errno));
Resolve_Shutdown(s);
return false;
}
2005-09-12 21:10:20 +02:00
GLOBAL bool
Resolve_Shutdown( RES_STAT *s)
{
bool ret = false;
assert(s != NULL);
assert(s->resolver_fd >= 0);
2005-09-12 21:10:20 +02:00
if (s->resolver_fd >= 0)
ret = io_close(s->resolver_fd);
2005-09-12 21:10:20 +02:00
Resolve_Init(s);
return ret;
}
GLOBAL size_t
Resolve_Read( RES_STAT *s, void* readbuf, size_t buflen)
{
/* Read result of resolver sub-process from pipe */
int err, bytes_read;
assert(buflen > 0);
/* Read result from pipe */
errno = 0;
bytes_read = read(s->resolver_fd, readbuf, buflen);
if (bytes_read < 0) {
if (errno != EAGAIN) {
err = errno;
Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror(err));
Resolve_Shutdown(s);
errno = err;
return 0;
}
return 0;
}
Resolve_Shutdown(s);
if (bytes_read == 0) { /* EOF: lookup failed */
#ifdef DEBUG
Log( LOG_DEBUG, "Resolver: Can't read result: EOF");
#endif
return 0;
}
return bytes_read;
}
2002-05-27 13:23:27 +02:00
/* -eof- */
2005-09-12 21:10:20 +02:00