2002-05-27 13:23:27 +02:00
|
|
|
/*
|
|
|
|
* ngIRCd -- The Next Generation IRC Daemon
|
2011-09-07 14:51:16 +02:00
|
|
|
* Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
|
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
|
|
|
*/
|
|
|
|
|
2010-07-14 10:29:05 +02:00
|
|
|
#define RESOLVER_TIMEOUT (Conf_PongTimeout*3)/4
|
|
|
|
|
2002-05-27 13:23:27 +02:00
|
|
|
#include "portab.h"
|
|
|
|
|
2010-12-27 17:14:14 +01:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Asynchronous resolver
|
|
|
|
*/
|
|
|
|
|
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 <netdb.h>
|
2010-07-12 13:22:19 +02:00
|
|
|
#include <signal.h>
|
2002-05-27 13:23:27 +02:00
|
|
|
|
2003-12-27 14:01:12 +01:00
|
|
|
#ifdef IDENTAUTH
|
|
|
|
#ifdef HAVE_IDENT_H
|
|
|
|
#include <ident.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2010-05-18 17:38:12 +02:00
|
|
|
#include "array.h"
|
2002-05-27 13:23:27 +02:00
|
|
|
#include "conn.h"
|
2010-07-14 10:29:05 +02:00
|
|
|
#include "conf.h"
|
2002-05-27 13:23:27 +02:00
|
|
|
#include "defines.h"
|
|
|
|
#include "log.h"
|
2010-05-18 17:38:12 +02:00
|
|
|
#include "ng_ipaddr.h"
|
2002-05-27 13:23:27 +02:00
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
2008-02-26 23:50:35 +01:00
|
|
|
static void Do_ResolveAddr PARAMS(( const ng_ipaddr_t *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 ));
|
2002-05-27 13:23:27 +02:00
|
|
|
|
2008-04-21 00:45:19 +02:00
|
|
|
#ifdef WANT_IPV6
|
|
|
|
extern bool Conf_ConnectIPv4;
|
|
|
|
extern bool Conf_ConnectIPv6;
|
|
|
|
#endif
|
2002-05-27 13:23:27 +02:00
|
|
|
|
|
|
|
|
2006-05-10 23:24:01 +02:00
|
|
|
/**
|
|
|
|
* Resolve IP (asynchronous!).
|
|
|
|
*/
|
2005-09-12 21:10:20 +02:00
|
|
|
GLOBAL bool
|
2010-05-18 17:38:12 +02:00
|
|
|
Resolve_Addr(PROC_STAT * s, const ng_ipaddr_t *Addr, int identsock,
|
2006-05-10 23:24:01 +02:00
|
|
|
void (*cbfunc) (int, short))
|
2002-05-27 13:23:27 +02:00
|
|
|
{
|
2006-05-10 23:24:01 +02:00
|
|
|
int pipefd[2];
|
|
|
|
pid_t pid;
|
|
|
|
|
2005-09-12 21:10:20 +02:00
|
|
|
assert(s != NULL);
|
2002-05-27 13:23:27 +02:00
|
|
|
|
2010-07-14 10:29:05 +02:00
|
|
|
pid = Proc_Fork(s, pipefd, cbfunc, RESOLVER_TIMEOUT);
|
2005-09-11 13:42:48 +02:00
|
|
|
if (pid > 0) {
|
2009-01-10 00:44:34 +01:00
|
|
|
LogDebug("Resolver for %s created (PID %d).", ng_ipaddr_tostr(Addr), pid);
|
2010-05-18 17:38:12 +02:00
|
|
|
return true;
|
2005-09-11 13:42:48 +02:00
|
|
|
} else if( pid == 0 ) {
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Sub process */
|
2010-07-13 22:04:35 +02:00
|
|
|
Log_Init_Subprocess("Resolver");
|
2011-09-07 14:51:16 +02:00
|
|
|
Conn_CloseAllSockets(identsock);
|
|
|
|
Do_ResolveAddr(Addr, identsock, pipefd[1]);
|
2010-07-01 00:34:56 +02:00
|
|
|
Log_Exit_Subprocess("Resolver");
|
2005-09-11 13:42:48 +02:00
|
|
|
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 */
|
|
|
|
|
|
|
|
|
2006-05-10 23:24:01 +02:00
|
|
|
/**
|
|
|
|
* Resolve hostname (asynchronous!).
|
|
|
|
*/
|
2005-09-12 21:10:20 +02:00
|
|
|
GLOBAL bool
|
2010-05-18 17:38:12 +02:00
|
|
|
Resolve_Name( PROC_STAT *s, const char *Host, void (*cbfunc)(int, short))
|
2002-05-27 13:23:27 +02:00
|
|
|
{
|
2006-05-10 23:24:01 +02:00
|
|
|
int pipefd[2];
|
|
|
|
pid_t pid;
|
|
|
|
|
2005-09-12 21:10:20 +02:00
|
|
|
assert(s != NULL);
|
2002-05-27 13:23:27 +02:00
|
|
|
|
2010-07-14 10:29:05 +02:00
|
|
|
pid = Proc_Fork(s, pipefd, cbfunc, RESOLVER_TIMEOUT);
|
2005-09-11 13:42:48 +02:00
|
|
|
if (pid > 0) {
|
2003-12-27 14:01:12 +01:00
|
|
|
/* 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
|
2010-05-18 17:38:12 +02:00
|
|
|
return true;
|
2005-09-11 13:42:48 +02:00
|
|
|
} else if( pid == 0 ) {
|
2003-12-27 14:01:12 +01:00
|
|
|
/* Sub process */
|
2010-07-13 22:04:35 +02:00
|
|
|
Log_Init_Subprocess("Resolver");
|
2011-09-07 14:51:16 +02:00
|
|
|
Conn_CloseAllSockets(NONE);
|
2005-09-12 21:10:20 +02:00
|
|
|
Do_ResolveName(Host, pipefd[1]);
|
2010-07-01 00:34:56 +02:00
|
|
|
Log_Exit_Subprocess("Resolver");
|
2005-09-11 13:42:48 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
2010-04-09 20:06:44 +02:00
|
|
|
#if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETNAMEINFO)
|
|
|
|
#if !defined(WANT_IPV6) && defined(h_errno)
|
2008-02-26 23:50:35 +01:00
|
|
|
static char *
|
|
|
|
Get_Error( int H_Error )
|
2002-05-27 13:23:27 +02:00
|
|
|
{
|
2008-02-26 23:50:35 +01:00
|
|
|
/* Get error message for H_Error */
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
return "unknown error";
|
|
|
|
}
|
2010-04-09 20:06:44 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
2002-05-27 13:23:27 +02:00
|
|
|
|
2008-02-26 23:50:35 +01:00
|
|
|
|
|
|
|
/* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */
|
|
|
|
static void
|
|
|
|
Do_IdentQuery(int identsock, array *resolved_addr)
|
|
|
|
{
|
2003-12-27 14:01:12 +01:00
|
|
|
#ifdef IDENTAUTH
|
2005-03-19 19:43:48 +01:00
|
|
|
char *res;
|
2008-02-26 23:50:35 +01:00
|
|
|
|
2008-11-19 19:11:39 +01:00
|
|
|
if (identsock < 0)
|
|
|
|
return;
|
2008-02-26 23:50:35 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2010-07-01 00:34:56 +02:00
|
|
|
Log_Subprocess(LOG_DEBUG, "Doing IDENT lookup on socket %d ...",
|
|
|
|
identsock);
|
2003-12-27 14:01:12 +01:00
|
|
|
#endif
|
2008-02-26 23:50:35 +01:00
|
|
|
res = ident_id( identsock, 10 );
|
2005-07-25 11:20:10 +02:00
|
|
|
#ifdef DEBUG
|
2010-07-01 00:34:56 +02:00
|
|
|
Log_Subprocess(LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"",
|
|
|
|
identsock, res ? res : "(NULL)");
|
2005-07-25 11:20:10 +02:00
|
|
|
#endif
|
2008-02-26 23:50:35 +01:00
|
|
|
if (!res) /* no result */
|
|
|
|
return;
|
|
|
|
if (!array_cats(resolved_addr, res))
|
2010-07-01 00:34:56 +02:00
|
|
|
Log_Subprocess(LOG_WARNING,
|
|
|
|
"Resolver: Cannot copy IDENT result: %s!",
|
|
|
|
strerror(errno));
|
2008-02-26 23:50:35 +01:00
|
|
|
|
|
|
|
free(res);
|
|
|
|
#else
|
|
|
|
(void) identsock;
|
|
|
|
(void) resolved_addr;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* perform reverse DNS lookup and put result string into resbuf.
|
|
|
|
* If no hostname could be obtained, this function stores the string representation of
|
|
|
|
* the IP address in resbuf and returns false.
|
|
|
|
* @param IpAddr ip address to resolve
|
|
|
|
* @param resbuf result buffer to store DNS name/string representation of ip address
|
2009-12-30 23:45:02 +01:00
|
|
|
* @param reslen size of result buffer (must be >= NGT_INET_ADDRSTRLEN)
|
2008-02-26 23:50:35 +01:00
|
|
|
* @return true if reverse lookup successful, false otherwise
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
ReverseLookup(const ng_ipaddr_t *IpAddr, char *resbuf, size_t reslen)
|
|
|
|
{
|
|
|
|
char tmp_ip_str[NG_INET_ADDRSTRLEN];
|
|
|
|
const char *errmsg;
|
|
|
|
#ifdef HAVE_GETNAMEINFO
|
|
|
|
static const char funcname[]="getnameinfo";
|
|
|
|
int res;
|
|
|
|
|
|
|
|
*resbuf = 0;
|
|
|
|
|
|
|
|
res = getnameinfo((struct sockaddr *) IpAddr, ng_ipaddr_salen(IpAddr),
|
2009-09-11 22:52:12 +02:00
|
|
|
resbuf, (socklen_t)reslen, NULL, 0, NI_NAMEREQD);
|
2008-02-26 23:50:35 +01:00
|
|
|
if (res == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (res == EAI_SYSTEM)
|
|
|
|
errmsg = strerror(errno);
|
|
|
|
else
|
|
|
|
errmsg = gai_strerror(res);
|
2002-05-27 13:23:27 +02:00
|
|
|
#else
|
2008-02-26 23:50:35 +01:00
|
|
|
const struct sockaddr_in *Addr = (const struct sockaddr_in *) IpAddr;
|
|
|
|
struct hostent *h;
|
|
|
|
static const char funcname[]="gethostbyaddr";
|
|
|
|
|
|
|
|
h = gethostbyaddr((char *)&Addr->sin_addr, sizeof(Addr->sin_addr), AF_INET);
|
|
|
|
if (h) {
|
|
|
|
if (strlcpy(resbuf, h->h_name, reslen) < reslen)
|
|
|
|
return true;
|
|
|
|
errmsg = "hostname too long";
|
2005-07-24 23:42:00 +02:00
|
|
|
} else {
|
2008-02-26 23:50:35 +01:00
|
|
|
# ifdef h_errno
|
|
|
|
errmsg = Get_Error(h_errno);
|
|
|
|
# else
|
|
|
|
errmsg = "unknown error";
|
|
|
|
# endif /* h_errno */
|
2002-05-27 13:23:27 +02:00
|
|
|
}
|
2008-02-26 23:50:35 +01:00
|
|
|
#endif /* HAVE_GETNAMEINFO */
|
2002-05-27 13:23:27 +02:00
|
|
|
|
2008-02-26 23:50:35 +01:00
|
|
|
assert(errmsg);
|
|
|
|
assert(reslen >= NG_INET_ADDRSTRLEN);
|
|
|
|
ng_ipaddr_tostr_r(IpAddr, tmp_ip_str);
|
2004-05-11 02:01:11 +02:00
|
|
|
|
2010-07-01 00:34:56 +02:00
|
|
|
Log_Subprocess(LOG_WARNING, "%s: Can't resolve address \"%s\": %s",
|
2008-02-26 23:50:35 +01:00
|
|
|
funcname, tmp_ip_str, errmsg);
|
|
|
|
strlcpy(resbuf, tmp_ip_str, reslen);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* perform DNS lookup of given host name and fill IpAddr with a list of
|
|
|
|
* ip addresses associated with that name.
|
|
|
|
* ip addresses found are stored in the "array *IpAddr" argument (type ng_ipaddr_t)
|
|
|
|
* @param hostname The domain name to look up.
|
|
|
|
* @param IpAddr pointer to empty and initialized array to store results
|
|
|
|
* @return true if lookup successful, false if domain name not found
|
|
|
|
*/
|
|
|
|
static bool
|
2011-04-25 18:00:10 +02:00
|
|
|
ForwardLookup(const char *hostname, array *IpAddr, int af)
|
2008-02-26 23:50:35 +01:00
|
|
|
{
|
|
|
|
ng_ipaddr_t addr;
|
2008-10-19 20:06:45 +02:00
|
|
|
|
2008-02-26 23:50:35 +01:00
|
|
|
#ifdef HAVE_GETADDRINFO
|
|
|
|
int res;
|
|
|
|
struct addrinfo *a, *ai_results;
|
2008-10-19 20:06:45 +02:00
|
|
|
static struct addrinfo hints;
|
|
|
|
|
2008-02-26 23:50:35 +01:00
|
|
|
#ifdef AI_ADDRCONFIG /* glibc has this, but not e.g. netbsd 4.0 */
|
2008-10-19 20:06:45 +02:00
|
|
|
hints.ai_flags = AI_ADDRCONFIG;
|
2005-09-12 21:10:20 +02:00
|
|
|
#endif
|
2008-10-19 20:06:45 +02:00
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
2011-04-25 18:00:10 +02:00
|
|
|
hints.ai_family = af;
|
2008-10-19 20:06:45 +02:00
|
|
|
|
2009-09-11 23:09:11 +02:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
|
2008-02-26 23:50:35 +01:00
|
|
|
res = getaddrinfo(hostname, NULL, &hints, &ai_results);
|
|
|
|
switch (res) {
|
|
|
|
case 0: break;
|
|
|
|
case EAI_SYSTEM:
|
2010-07-01 00:34:56 +02:00
|
|
|
Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, strerror(errno));
|
2008-02-26 23:50:35 +01:00
|
|
|
return false;
|
|
|
|
default:
|
2010-07-01 00:34:56 +02:00
|
|
|
Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, gai_strerror(res));
|
2008-02-26 23:50:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
2005-05-28 12:46:50 +02:00
|
|
|
|
2008-02-26 23:50:35 +01:00
|
|
|
for (a = ai_results; a != NULL; a = a->ai_next) {
|
2012-12-25 19:21:40 +01:00
|
|
|
assert((size_t)a->ai_addrlen <= sizeof(addr));
|
2008-02-26 23:50:35 +01:00
|
|
|
|
2012-12-25 18:49:28 +01:00
|
|
|
if ((size_t)a->ai_addrlen > sizeof(addr))
|
2008-02-26 23:50:35 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
memcpy(&addr, a->ai_addr, a->ai_addrlen);
|
|
|
|
|
|
|
|
if (!array_catb(IpAddr, (char *)&addr, sizeof(addr)))
|
|
|
|
break;
|
2003-12-27 14:01:12 +01:00
|
|
|
}
|
2008-02-26 23:50:35 +01:00
|
|
|
|
|
|
|
freeaddrinfo(ai_results);
|
|
|
|
return a == NULL;
|
2005-09-12 21:10:20 +02:00
|
|
|
#else
|
2008-02-26 23:50:35 +01:00
|
|
|
struct hostent *h = gethostbyname(hostname);
|
|
|
|
|
|
|
|
if (!h) {
|
|
|
|
#ifdef h_errno
|
2010-07-01 00:34:56 +02:00
|
|
|
Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s",
|
|
|
|
hostname, Get_Error(h_errno));
|
2008-02-26 23:50:35 +01:00
|
|
|
#else
|
2010-07-01 00:34:56 +02:00
|
|
|
Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\"", hostname);
|
2003-12-27 14:01:12 +01:00
|
|
|
#endif
|
2008-02-26 23:50:35 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
2005-09-03 13:17:16 +02:00
|
|
|
|
2008-02-26 23:50:35 +01:00
|
|
|
addr.sin4.sin_family = AF_INET;
|
|
|
|
memcpy(&addr.sin4.sin_addr, h->h_addr, sizeof(struct in_addr));
|
|
|
|
|
|
|
|
return array_copyb(IpAddr, (char *)&addr, sizeof(addr));
|
|
|
|
#endif /* HAVE_GETADDRINFO */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
Addr_in_list(const array *resolved_addr, const ng_ipaddr_t *Addr)
|
|
|
|
{
|
|
|
|
char tmp_ip_str[NG_INET_ADDRSTRLEN];
|
|
|
|
const ng_ipaddr_t *tmpAddrs = array_start(resolved_addr);
|
|
|
|
size_t len = array_length(resolved_addr, sizeof(*tmpAddrs));
|
|
|
|
|
|
|
|
assert(len > 0);
|
|
|
|
assert(tmpAddrs);
|
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
if (ng_ipaddr_ipequal(Addr, tmpAddrs))
|
|
|
|
return true;
|
|
|
|
tmpAddrs++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
/* failed; print list of addresses */
|
|
|
|
ng_ipaddr_tostr_r(Addr, tmp_ip_str);
|
|
|
|
len = array_length(resolved_addr, sizeof(*tmpAddrs));
|
|
|
|
tmpAddrs = array_start(resolved_addr);
|
|
|
|
|
|
|
|
while (len > 0) {
|
2010-07-01 00:34:56 +02:00
|
|
|
Log_Subprocess(LOG_WARNING, "Address mismatch: %s != %s",
|
2008-02-26 23:50:35 +01:00
|
|
|
tmp_ip_str, ng_ipaddr_tostr(tmpAddrs));
|
|
|
|
tmpAddrs++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2002-05-27 13:23:27 +02:00
|
|
|
|
|
|
|
|
2005-07-31 22:13:07 +02:00
|
|
|
static void
|
2008-02-26 23:50:35 +01:00
|
|
|
Log_Forgery_NoIP(const char *ip, const char *host)
|
2002-05-27 13:23:27 +02:00
|
|
|
{
|
2010-10-24 21:48:32 +02:00
|
|
|
Log_Subprocess(LOG_WARNING,
|
|
|
|
"Possible forgery: %s resolved to %s (which has no ip address)", ip, host);
|
2008-02-26 23:50:35 +01:00
|
|
|
}
|
2002-05-27 13:23:27 +02:00
|
|
|
|
2008-02-26 23:50:35 +01:00
|
|
|
static void
|
|
|
|
Log_Forgery_WrongIP(const char *ip, const char *host)
|
|
|
|
{
|
2010-10-24 21:48:32 +02:00
|
|
|
Log_Subprocess(LOG_WARNING,
|
|
|
|
"Possible forgery: %s resolved to %s (which points to different address)", ip, host);
|
2008-02-26 23:50:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ArrayWrite(int fd, const array *a)
|
|
|
|
{
|
|
|
|
size_t len = array_bytes(a);
|
|
|
|
const char *data = array_start(a);
|
|
|
|
|
|
|
|
assert(data);
|
|
|
|
|
|
|
|
if( (size_t)write(fd, data, len) != len )
|
2010-07-01 00:34:56 +02:00
|
|
|
Log_Subprocess( LOG_CRIT, "Resolver: Can't write to parent: %s!",
|
2008-02-26 23:50:35 +01:00
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
Do_ResolveAddr(const ng_ipaddr_t *Addr, int identsock, int w_fd)
|
|
|
|
{
|
|
|
|
/* Resolver sub-process: resolve IP address and write result into
|
|
|
|
* pipe to parent. */
|
|
|
|
char hostname[CLIENT_HOST_LEN];
|
|
|
|
char tmp_ip_str[NG_INET_ADDRSTRLEN];
|
2006-05-10 23:24:01 +02:00
|
|
|
size_t len;
|
2008-02-26 23:50:35 +01:00
|
|
|
array resolved_addr;
|
2002-05-27 13:23:27 +02:00
|
|
|
|
2008-02-26 23:50:35 +01:00
|
|
|
array_init(&resolved_addr);
|
|
|
|
ng_ipaddr_tostr_r(Addr, tmp_ip_str);
|
|
|
|
#ifdef DEBUG
|
2010-07-01 00:34:56 +02:00
|
|
|
Log_Subprocess(LOG_DEBUG, "Now resolving %s ...", tmp_ip_str);
|
2008-02-26 23:50:35 +01:00
|
|
|
#endif
|
|
|
|
if (!ReverseLookup(Addr, hostname, sizeof(hostname)))
|
|
|
|
goto dns_done;
|
2002-05-27 13:23:27 +02:00
|
|
|
|
2011-04-26 11:09:59 +02:00
|
|
|
if (ForwardLookup(hostname, &resolved_addr, ng_ipaddr_af(Addr))) {
|
2008-02-26 23:50:35 +01:00
|
|
|
if (!Addr_in_list(&resolved_addr, Addr)) {
|
|
|
|
Log_Forgery_WrongIP(tmp_ip_str, hostname);
|
|
|
|
strlcpy(hostname, tmp_ip_str, sizeof(hostname));
|
|
|
|
}
|
2005-09-11 13:42:48 +02:00
|
|
|
} else {
|
2008-02-26 23:50:35 +01:00
|
|
|
Log_Forgery_NoIP(tmp_ip_str, hostname);
|
|
|
|
strlcpy(hostname, tmp_ip_str, sizeof(hostname));
|
2002-05-27 13:23:27 +02:00
|
|
|
}
|
2005-09-12 21:10:20 +02:00
|
|
|
#ifdef DEBUG
|
2010-07-01 00:34:56 +02:00
|
|
|
Log_Subprocess(LOG_DEBUG, "Ok, translated %s to \"%s\".", tmp_ip_str, hostname);
|
2005-09-12 21:10:20 +02:00
|
|
|
#endif
|
2008-02-26 23:50:35 +01:00
|
|
|
dns_done:
|
|
|
|
len = strlen(hostname);
|
|
|
|
hostname[len] = '\n';
|
|
|
|
if (!array_copyb(&resolved_addr, hostname, ++len)) {
|
2010-07-01 00:34:56 +02:00
|
|
|
Log_Subprocess(LOG_CRIT,
|
|
|
|
"Resolver: Can't copy resolved name: %s!",
|
|
|
|
strerror(errno));
|
2008-02-26 23:50:35 +01:00
|
|
|
array_free(&resolved_addr);
|
|
|
|
return;
|
2002-05-27 13:23:27 +02:00
|
|
|
}
|
|
|
|
|
2008-02-26 23:50:35 +01:00
|
|
|
Do_IdentQuery(identsock, &resolved_addr);
|
2002-05-27 13:23:27 +02:00
|
|
|
|
2008-02-26 23:50:35 +01:00
|
|
|
ArrayWrite(w_fd, &resolved_addr);
|
2002-05-27 13:23:27 +02:00
|
|
|
|
2008-02-26 23:50:35 +01:00
|
|
|
array_free(&resolved_addr);
|
|
|
|
} /* Do_ResolveAddr */
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
Do_ResolveName( const char *Host, int w_fd )
|
2002-05-27 13:23:27 +02:00
|
|
|
{
|
2008-02-26 23:50:35 +01:00
|
|
|
/* Resolver sub-process: resolve name and write result into pipe
|
|
|
|
* to parent. */
|
|
|
|
array IpAddrs;
|
2011-04-25 18:00:10 +02:00
|
|
|
int af;
|
2008-02-26 23:50:35 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
ng_ipaddr_t *addr;
|
|
|
|
size_t len;
|
|
|
|
#endif
|
2010-07-01 00:34:56 +02:00
|
|
|
Log_Subprocess(LOG_DEBUG, "Now resolving \"%s\" ...", Host);
|
2002-05-27 13:23:27 +02:00
|
|
|
|
2008-02-26 23:50:35 +01:00
|
|
|
array_init(&IpAddrs);
|
2011-04-25 18:00:10 +02:00
|
|
|
|
|
|
|
#ifdef WANT_IPV6
|
|
|
|
af = AF_UNSPEC;
|
|
|
|
assert(Conf_ConnectIPv6 || Conf_ConnectIPv4);
|
|
|
|
|
|
|
|
if (!Conf_ConnectIPv6)
|
|
|
|
af = AF_INET;
|
|
|
|
if (!Conf_ConnectIPv4)
|
|
|
|
af = AF_INET6;
|
|
|
|
#else
|
|
|
|
af = AF_INET;
|
|
|
|
#endif
|
|
|
|
if (!ForwardLookup(Host, &IpAddrs, af)) {
|
2008-02-26 23:50:35 +01:00
|
|
|
close(w_fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
len = array_length(&IpAddrs, sizeof(*addr));
|
|
|
|
assert(len > 0);
|
|
|
|
addr = array_start(&IpAddrs);
|
|
|
|
assert(addr);
|
|
|
|
for (; len > 0; --len,addr++) {
|
2010-07-01 00:34:56 +02:00
|
|
|
Log_Subprocess(LOG_DEBUG, "translated \"%s\" to %s.",
|
2008-02-26 23:50:35 +01:00
|
|
|
Host, ng_ipaddr_tostr(addr));
|
2002-05-27 13:23:27 +02:00
|
|
|
}
|
|
|
|
#endif
|
2008-02-26 23:50:35 +01:00
|
|
|
/* Write result into pipe to parent */
|
|
|
|
ArrayWrite(w_fd, &IpAddrs);
|
|
|
|
|
|
|
|
array_free(&IpAddrs);
|
|
|
|
} /* Do_ResolveName */
|
2002-05-27 13:23:27 +02:00
|
|
|
|
|
|
|
|
2009-09-11 22:52:12 +02:00
|
|
|
/* -eof- */
|