]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/resolve.c
Change log message for "Can't resolve address"
[ngircd-alex.git] / src / ngircd / resolve.c
index 367692d6c48cfdb085e379ace4d049c79863ccc9..32791901215c475c95982616e8d95b321efe7680 100644 (file)
 /*
  * ngIRCd -- The Next Generation IRC Daemon
- * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
+ * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
  *
  * 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.
- *
- * Asynchronous resolver
  */
 
+#define RESOLVER_TIMEOUT (Conf_PongTimeout*3)/4
 
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: resolve.c,v 1.4 2002/12/12 12:24:18 alex Exp $";
+/**
+ * @file
+ * Asynchronous resolver
+ */
 
-#include "imp.h"
 #include <assert.h>
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.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
+
 #include "conn.h"
-#include "defines.h"
+#include "conf.h"
 #include "log.h"
+#include "ng_ipaddr.h"
 
-#include "exp.h"
 #include "resolve.h"
 
+static void Do_ResolveAddr PARAMS(( const ng_ipaddr_t *Addr, int Sock, int w_fd ));
+static void Do_ResolveName PARAMS(( const char *Host, int w_fd ));
 
-LOCAL VOID Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, INT w_fd ));
-LOCAL VOID Do_ResolveName PARAMS(( CHAR *Host, INT w_fd ));
-
-#ifdef h_errno
-LOCAL CHAR *Get_Error PARAMS(( INT H_Error ));
+#ifdef WANT_IPV6
+extern bool Conf_ConnectIPv4;
+extern bool Conf_ConnectIPv6;
 #endif
 
 
-GLOBAL VOID
-Resolve_Init( VOID )
+/**
+ * Resolve IP (asynchronous!).
+ */
+GLOBAL bool
+Resolve_Addr(PROC_STAT * s, const ng_ipaddr_t *Addr, int identsock,
+            void (*cbfunc) (int, short))
 {
-       /* Modul initialisieren */
-
-       FD_ZERO( &Resolver_FDs );
-} /* Resolve_Init */
+       int pipefd[2];
+       pid_t pid;
+
+       assert(s != NULL);
+
+       pid = Proc_Fork(s, pipefd, cbfunc, RESOLVER_TIMEOUT);
+       if (pid > 0) {
+               LogDebug("Resolver for %s created (PID %d).", ng_ipaddr_tostr(Addr), pid);
+               return true;
+       } else if( pid == 0 ) {
+               /* Sub process */
+               Log_Init_Subprocess("Resolver");
+               Conn_CloseAllSockets(identsock);
+               Do_ResolveAddr(Addr, identsock, pipefd[1]);
+               Log_Exit_Subprocess("Resolver");
+               exit(0);
+       }
+       return false;
+} /* Resolve_Addr */
 
 
-GLOBAL RES_STAT *
-Resolve_Addr( struct sockaddr_in *Addr )
+/**
+ * Resolve hostname (asynchronous!).
+ */
+GLOBAL bool
+Resolve_Name( PROC_STAT *s, const char *Host, void (*cbfunc)(int, short))
 {
-       /* IP (asyncron!) aufloesen. Bei Fehler, z.B. wenn der
-        * Child-Prozess nicht erzeugt werden kann, wird NULL geliefert.
-        * Der Host kann dann nicht aufgeloest werden. */
-
-       RES_STAT *s;
-       INT pid;
-
-       /* Speicher anfordern */
-       s = malloc( sizeof( RES_STAT ));
-       if( ! s )
-       {
-               Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" );
-               return NULL;
-       }
+       int pipefd[2];
+       pid_t pid;
 
-       /* Pipe fuer Antwort initialisieren */
-       if( pipe( s->pipe ) != 0 )
-       {
-               free( s );
-               Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
-               return NULL;
-       }
+       assert(s != NULL);
 
-       /* Sub-Prozess erzeugen */
-       pid = fork( );
-       if( pid > 0 )
-       {
-               /* Haupt-Prozess */
-               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;
-               return s;
-       }
-       else if( pid == 0 )
-       {
-               /* Sub-Prozess */
-               Log_Init_Resolver( );
-               Do_ResolveAddr( Addr, s->pipe[1] );
-               Log_Exit_Resolver( );
-               exit( 0 );
-       }
-       else
-       {
-               /* Fehler */
-               free( s );
-               Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
-               return NULL;
+       pid = Proc_Fork(s, pipefd, cbfunc, RESOLVER_TIMEOUT);
+       if (pid > 0) {
+               /* Main process */
+#ifdef DEBUG
+               Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
+#endif
+               return true;
+       } else if( pid == 0 ) {
+               /* Sub process */
+               Log_Init_Subprocess("Resolver");
+               Conn_CloseAllSockets(NONE);
+               Do_ResolveName(Host, pipefd[1]);
+               Log_Exit_Subprocess("Resolver");
+               exit(0);
        }
-} /* Resolve_Addr */
+       return false;
+} /* Resolve_Name */
 
 
-GLOBAL RES_STAT *
-Resolve_Name( CHAR *Host )
+#if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETNAMEINFO)
+#if !defined(WANT_IPV6) && defined(h_errno)
+static char *
+Get_Error( int H_Error )
 {
-       /* Hostnamen (asyncron!) aufloesen. Bei Fehler, z.B. wenn der
-        * Child-Prozess nicht erzeugt werden kann, wird NULL geliefert.
-        * Der Host kann dann nicht aufgeloest werden. */
-
-       RES_STAT *s;
-       INT pid;
-
-       /* Speicher anfordern */
-       s = malloc( sizeof( RES_STAT ));
-       if( ! s )
-       {
-               Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Name]" );
-               return NULL;
+       /* 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";
+}
+#endif
+#endif
 
-       /* Pipe fuer Antwort initialisieren */
-       if( pipe( s->pipe ) != 0 )
-       {
-               free( s );
-               Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
-               return NULL;
-       }
 
-       /* Sub-Prozess erzeugen */
-       pid = fork( );
-       if( pid > 0 )
-       {
-               /* Haupt-Prozess */
-               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;
-               return s;
-       }
-       else if( pid == 0 )
-       {
-               /* Sub-Prozess */
-               Log_Init_Resolver( );
-               Do_ResolveName( Host, s->pipe[1] );
-               Log_Exit_Resolver( );
-               exit( 0 );
-       }
-       else
-       {
-               /* Fehler */
-               free( s );
-               Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
-               return NULL;
-       }
-} /* Resolve_Name */
+/* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */
+static void
+Do_IdentQuery(int identsock, array *resolved_addr)
+{
+#ifdef IDENTAUTH
+       char *res;
+
+       if (identsock < 0)
+               return;
+
+#ifdef DEBUG
+       Log_Subprocess(LOG_DEBUG, "Doing IDENT lookup on socket %d ...",
+                      identsock);
+#endif
+       res = ident_id( identsock, 10 );
+#ifdef DEBUG
+       Log_Subprocess(LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"",
+                      identsock, res ? res : "(NULL)");
+#endif
+       if (!res) /* no result */
+               return;
+       if (!array_cats(resolved_addr, res))
+               Log_Subprocess(LOG_WARNING,
+                              "Resolver: Cannot copy IDENT result: %s!",
+                              strerror(errno));
+
+       free(res);
+#else
+       (void) identsock;
+       (void) resolved_addr;
+#endif
+}
 
 
-LOCAL VOID
-Do_ResolveAddr( struct sockaddr_in *Addr, INT w_fd )
+/**
+ * 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
+ * @param reslen size of result buffer (must be >= NGT_INET_ADDRSTRLEN)
+ * @return true if reverse lookup successful, false otherwise
+ */
+static bool
+ReverseLookup(const ng_ipaddr_t *IpAddr, char *resbuf, size_t reslen)
 {
-       /* Resolver Sub-Prozess: IP aufloesen und Ergebnis in Pipe schreiben. */
+       char tmp_ip_str[NG_INET_ADDRSTRLEN];
+       const char *errmsg;
+#ifdef HAVE_GETNAMEINFO
+       static const char funcname[]="getnameinfo";
+       int res;
 
-       CHAR hostname[HOST_LEN];
-       struct hostent *h;
+       *resbuf = 0;
 
-       Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
+       res = getnameinfo((struct sockaddr *) IpAddr, ng_ipaddr_salen(IpAddr),
+                         resbuf, (socklen_t)reslen, NULL, 0, NI_NAMEREQD);
+       if (res == 0)
+               return true;
 
-       /* Namen aufloesen */
-       h = gethostbyaddr( (CHAR *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
-       if( h ) strcpy( hostname, h->h_name );
+       if (res == EAI_SYSTEM)
+               errmsg = strerror(errno);
        else
-       {
-#ifdef h_errno
-               Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\": %s!", inet_ntoa( Addr->sin_addr ), Get_Error( h_errno ));
+               errmsg = gai_strerror(res);
 #else
-               Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\"!", inet_ntoa( Addr->sin_addr ));
-#endif 
-               strcpy( hostname, inet_ntoa( Addr->sin_addr ));
+       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";
+       } else {
+# ifdef h_errno
+               errmsg = Get_Error(h_errno);
+# else
+               errmsg = "unknown error";
+# endif /* h_errno */
        }
+#endif /* HAVE_GETNAMEINFO */
+
+       assert(errmsg);
+       assert(reslen >= NG_INET_ADDRSTRLEN);
+       ng_ipaddr_tostr_r(IpAddr, tmp_ip_str);
+
+       Log_Subprocess(LOG_WARNING, "Can't resolve address \"%s\": %s [%s].",
+                      tmp_ip_str, errmsg, funcname);
+       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
+ForwardLookup(const char *hostname, array *IpAddr, int af)
+{
+       ng_ipaddr_t addr;
 
-       /* Antwort an Parent schreiben */
-       if( write( w_fd, hostname, strlen( hostname ) + 1 ) != ( strlen( hostname ) + 1 ))
-       {
-               Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
-               close( w_fd );
-               return;
+#ifdef HAVE_WORKING_GETADDRINFO
+       int res;
+       struct addrinfo *a, *ai_results;
+       static struct addrinfo hints;
+
+#ifdef AI_ADDRCONFIG   /* glibc has this, but not e.g. netbsd 4.0 */
+       hints.ai_flags = AI_ADDRCONFIG;
+#endif
+       hints.ai_socktype = SOCK_STREAM;
+       hints.ai_protocol = IPPROTO_TCP;
+       hints.ai_family = af;
+
+       memset(&addr, 0, sizeof(addr));
+
+       res = getaddrinfo(hostname, NULL, &hints, &ai_results);
+       switch (res) {
+       case 0: break;
+       case EAI_SYSTEM:
+               Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, strerror(errno));
+               return false;
+       default:
+               Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, gai_strerror(res));
+               return false;
        }
 
-       Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
-} /* Do_ResolveAddr */
+       for (a = ai_results; a != NULL; a = a->ai_next) {
+               assert((size_t)a->ai_addrlen <= sizeof(addr));
 
+               if ((size_t)a->ai_addrlen > sizeof(addr))
+                       continue;
 
-LOCAL VOID
-Do_ResolveName( CHAR *Host, INT w_fd )
-{
-       /* Resolver Sub-Prozess: Name aufloesen und Ergebnis in Pipe schreiben. */
+               memcpy(&addr, a->ai_addr, a->ai_addrlen);
 
-       CHAR ip[16];
-       struct hostent *h;
-       struct in_addr *addr;
+               if (!array_catb(IpAddr, (char *)&addr, sizeof(addr)))
+                       break;
+       }
 
-       Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
+       freeaddrinfo(ai_results);
+       return a == NULL;
+#else
+       struct hostent *h = gethostbyname(hostname);
 
-       /* Namen aufloesen */
-       h = gethostbyname( Host );
-       if( h )
-       {
-               addr = (struct in_addr *)h->h_addr;
-               strcpy( ip, inet_ntoa( *addr ));
-       }
-       else
-       {
+       if (!h) {
 #ifdef h_errno
-               Log_Resolver( LOG_WARNING, "Can't resolve \"%s\": %s!", Host, Get_Error( h_errno ));
+               Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s",
+                              hostname, Get_Error(h_errno));
 #else
-               Log_Resolver( LOG_WARNING, "Can't resolve \"%s\"!", Host );
+               Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\"", hostname);
 #endif
-               strcpy( ip, "" );
+               return false;
        }
+       memset(&addr, 0, sizeof(addr));
 
-       /* Antwort an Parent schreiben */
-       if( write( w_fd, ip, strlen( ip ) + 1 ) != ( strlen( ip ) + 1 ))
-       {
-               Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
-               close( w_fd );
-               return;
+       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) {
+               Log_Subprocess(LOG_WARNING, "Address mismatch: %s != %s",
+                       tmp_ip_str, ng_ipaddr_tostr(tmpAddrs));
+               tmpAddrs++;
+               len--;
        }
 
-       if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
-} /* Do_ResolveName */
+       return false;
+}
 
 
-#ifdef h_errno
+static void
+Log_Forgery_NoIP(const char *ip, const char *host)
+{
+       Log_Subprocess(LOG_WARNING,
+               "Possible forgery: %s resolved to \"%s\", which has no IP address!",
+               ip, host);
+}
+
+static void
+Log_Forgery_WrongIP(const char *ip, const char *host)
+{
+       Log_Subprocess(LOG_WARNING,
+               "Possible forgery: %s resolved to \"%s\", which points to a different address!",
+               ip, host);
+}
+
+
+static void
+ArrayWrite(int fd, const array *a)
+{
+       size_t len = array_bytes(a);
+       const char *data = array_start(a);
 
-LOCAL CHAR *
-Get_Error( INT H_Error )
+       assert(data);
+
+       if( (size_t)write(fd, data, len) != len )
+               Log_Subprocess( LOG_CRIT, "Resolver: Can't write to parent: %s!",
+                                                       strerror(errno));
+}
+
+
+static void
+Do_ResolveAddr(const ng_ipaddr_t *Addr, int identsock, int w_fd)
 {
-       /* Fehlerbeschreibung fuer H_Error liefern */
-
-       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";
+       /* 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];
+       size_t len;
+       array resolved_addr;
+
+       array_init(&resolved_addr);
+       ng_ipaddr_tostr_r(Addr, tmp_ip_str);
+#ifdef DEBUG
+       Log_Subprocess(LOG_DEBUG, "Now resolving %s ...", tmp_ip_str);
+#endif
+       if (!ReverseLookup(Addr, hostname, sizeof(hostname)))
+               goto dns_done;
+
+       if (ForwardLookup(hostname, &resolved_addr, ng_ipaddr_af(Addr))) {
+               if (!Addr_in_list(&resolved_addr, Addr)) {
+                       Log_Forgery_WrongIP(tmp_ip_str, hostname);
+                       strlcpy(hostname, tmp_ip_str, sizeof(hostname));
+               }
+       } else {
+               Log_Forgery_NoIP(tmp_ip_str, hostname);
+               strlcpy(hostname, tmp_ip_str, sizeof(hostname));
+       }
+#ifdef DEBUG
+       Log_Subprocess(LOG_DEBUG, "Ok, translated %s to \"%s\".", tmp_ip_str, hostname);
+#endif
+ dns_done:
+       len = strlen(hostname);
+       hostname[len] = '\n';
+       if (!array_copyb(&resolved_addr, hostname, ++len)) {
+               Log_Subprocess(LOG_CRIT,
+                              "Resolver: Can't copy resolved name: %s!",
+                              strerror(errno));
+               array_free(&resolved_addr);
+               return;
        }
-} /* Get_Error */
 
+       Do_IdentQuery(identsock, &resolved_addr);
+
+       ArrayWrite(w_fd, &resolved_addr);
+
+       array_free(&resolved_addr);
+} /* Do_ResolveAddr */
+
+
+static void
+Do_ResolveName( const char *Host, int w_fd )
+{
+       /* Resolver sub-process: resolve name and write result into pipe
+        * to parent. */
+       array IpAddrs;
+       int af;
+#ifdef DEBUG
+       ng_ipaddr_t *addr;
+       size_t len;
+#endif
+       Log_Subprocess(LOG_DEBUG, "Now resolving \"%s\" ...", Host);
+
+       array_init(&IpAddrs);
+
+#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)) {
+               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++) {
+               Log_Subprocess(LOG_DEBUG, "translated \"%s\" to %s.",
+                                       Host, ng_ipaddr_tostr(addr));
+       }
+#endif
+       /* Write result into pipe to parent */
+       ArrayWrite(w_fd, &IpAddrs);
+
+       array_free(&IpAddrs);
+} /* Do_ResolveName */
 
 
 /* -eof- */