]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/resolve.c
include <arpa/inet.h> inside tool.h
[ngircd-alex.git] / src / ngircd / resolve.c
index 9fefbec359bddd2e5318ce36100811a29d0bffe9..dbf28736ab8c2329705be524c5d893be5266e230 100644 (file)
@@ -1,22 +1,21 @@
 /*
  * ngIRCd -- The Next Generation IRC Daemon
- * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
+ * Copyright (c)2001-2003 by Alexander Barton (alex@barton.de)
  *
- * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
- * der GNU General Public License (GPL), wie von der Free Software Foundation
- * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
- * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
- * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
- * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
+ * 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.
  *
- * $Id: resolve.c,v 1.2 2002/05/30 16:52:21 alex Exp $
- *
- * resolve.c: asyncroner Resolver
+ * Asynchronous resolver
  */
 
 
 #include "portab.h"
 
+static char UNUSED id[] = "$Id: resolve.c,v 1.27 2007/11/25 18:42:37 fw Exp $";
+
 #include "imp.h"
 #include <assert.h>
 #include <errno.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
+
 #include "conn.h"
 #include "defines.h"
 #include "log.h"
+#include "tool.h"
 
 #include "exp.h"
 #include "resolve.h"
+#include "io.h"
 
 
-LOCAL VOID Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, INT w_fd ));
-LOCAL VOID Do_ResolveName PARAMS(( CHAR *Host, INT w_fd ));
+static void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int Sock, int w_fd ));
+static void Do_ResolveName PARAMS(( const char *Host, int w_fd ));
+static bool register_callback PARAMS((RES_STAT *s, void (*cbfunc)(int, short)));
 
 #ifdef h_errno
-LOCAL CHAR *Get_Error PARAMS(( INT H_Error ));
+static char *Get_Error PARAMS(( int H_Error ));
 #endif
 
-
-GLOBAL VOID
-Resolve_Init( VOID )
+static pid_t
+Resolver_fork(int *pipefds)
 {
-       /* Modul initialisieren */
-
-       FD_ZERO( &Resolver_FDs );
-} /* Resolve_Init */
+       pid_t pid;
 
+       if (pipe(pipefds) != 0) {
+                Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
+                return -1;
+       }
 
-GLOBAL RES_STAT *
-Resolve_Addr( struct sockaddr_in *Addr )
-{
-       /* 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. */
+       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; 
+}
 
-       RES_STAT *s;
-       INT pid;
 
-       /* Speicher anfordern */
-       s = malloc( sizeof( RES_STAT ));
-       if( ! s )
-       {
-               Log( LOG_EMERG, "Resolver: Can't allocate memory!" );
-               return NULL;
-       }
+/**
+ * Resolve IP (asynchronous!).
+ */
+GLOBAL bool
+Resolve_Addr(RES_STAT * s, struct sockaddr_in *Addr, int identsock,
+            void (*cbfunc) (int, short))
+{
+       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 */
+       pid = Resolver_fork(pipefd);
+       if (pid > 0) {
+#ifdef DEBUG
                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];
+#endif
                s->pid = pid;
-               return s;
-       }
-       else if( pid == 0 )
-       {
-               /* Sub-Prozess */
-               Log_Init_Resolver( );
-               Do_ResolveAddr( Addr, s->pipe[1] );
+               s->resolver_fd = pipefd[0];
+               return register_callback(s, cbfunc);
+       } else if( pid == 0 ) {
+               /* Sub process */
+               Do_ResolveAddr( Addr, identsock, pipefd[1]);
                Log_Exit_Resolver( );
-               exit( 0 );
-       }
-       else
-       {
-               /* Fehler */
-               free( s );
-               Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
-               return NULL;
+               exit(0);
        }
+       return false;
 } /* Resolve_Addr */
 
 
-GLOBAL RES_STAT *
-Resolve_Name( CHAR *Host )
+/**
+ * Resolve hostname (asynchronous!).
+ */
+GLOBAL bool
+Resolve_Name( RES_STAT *s, const char *Host, void (*cbfunc)(int, short))
 {
-       /* 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;
+       int pipefd[2];
+       pid_t pid;
 
-       /* Speicher anfordern */
-       s = malloc( sizeof( RES_STAT ));
-       if( ! s )
-       {
-               Log( LOG_EMERG, "Resolver: Can't allocate memory!" );
-               return NULL;
-       }
+       assert(s != NULL);
 
-       /* 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 */
+       pid = Resolver_fork(pipefd);
+       if (pid > 0) {
+               /* Main process */
+#ifdef DEBUG
                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];
+#endif
                s->pid = pid;
-               return s;
-       }
-       else if( pid == 0 )
-       {
-               /* Sub-Prozess */
-               Log_Init_Resolver( );
-               Do_ResolveName( Host, s->pipe[1] );
+               s->resolver_fd = pipefd[0];
+               return register_callback(s, cbfunc);
+       } else if( pid == 0 ) {
+               /* Sub process */
+               Do_ResolveName(Host, pipefd[1]);
                Log_Exit_Resolver( );
-               exit( 0 );
-       }
-       else
-       {
-               /* Fehler */
-               free( s );
-               Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
-               return NULL;
+               exit(0);
        }
+       return false;
 } /* Resolve_Name */
 
 
-LOCAL VOID
-Do_ResolveAddr( struct sockaddr_in *Addr, INT w_fd )
+GLOBAL void
+Resolve_Init(RES_STAT *s)
 {
-       /* Resolver Sub-Prozess: IP aufloesen und Ergebnis in Pipe schreiben. */
+       assert(s != NULL);
+       s->resolver_fd = -1;
+       s->pid = 0;
+}
 
-       CHAR hostname[HOST_LEN];
-       struct hostent *h;
 
-       Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
+static void
+Do_ResolveAddr( struct sockaddr_in *Addr, int identsock, int w_fd )
+{
+       /* Resolver sub-process: resolve IP address and write result into
+        * pipe to parent. */
 
-       /* Namen aufloesen */
-       h = gethostbyaddr( (CHAR *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
-       if( h ) strcpy( hostname, h->h_name );
-       else
-       {
+       char hostname[HOST_LEN];
+       char ipstr[HOST_LEN];
+       struct hostent *h;
+       size_t len;
+       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 );
+       if (!h) {
 #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 
-               strcpy( hostname, inet_ntoa( Addr->sin_addr ));
+               strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
+       } 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 );
+                               Log(LOG_WARNING,"Possible forgery: %s resolved to %s (which is at ip %s!)",
+                                                                               ntoaptr, hostname, ipstr);
+                               strlcpy( hostname, ntoaptr, sizeof hostname);
+                       }
+               } else {
+                       ntoaptr = inet_ntoa( Addr->sin_addr );
+                       Log(LOG_WARNING, "Possible forgery: %s resolved to %s (which has no ip address)",
+                                                                                       ntoaptr, hostname);
+                       strlcpy( hostname, ntoaptr, sizeof hostname);
+               }
        }
+       Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
 
-       /* 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 ));
+       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 ));
                close( w_fd );
                return;
        }
 
-       Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
+#ifdef IDENTAUTH
+       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));
+                       /* omit ident and return hostname only */ 
+               }
+
+               if (res) free(res);
+       }
+#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);
 } /* Do_ResolveAddr */
 
 
-LOCAL VOID
-Do_ResolveName( CHAR *Host, INT w_fd )
+static void
+Do_ResolveName( const char *Host, int w_fd )
 {
-       /* Resolver Sub-Prozess: Name aufloesen und Ergebnis in Pipe schreiben. */
+       /* Resolver sub-process: resolve name and write result into pipe
+        * to parent. */
 
-       CHAR ip[16];
+       char ip[16];
        struct hostent *h;
        struct in_addr *addr;
+       size_t len;
 
        Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
 
-       /* Namen aufloesen */
+       /* Resolve hostname */
        h = gethostbyname( Host );
-       if( h )
-       {
+       if( h ) {
                addr = (struct in_addr *)h->h_addr;
-               strcpy( ip, inet_ntoa( *addr ));
-       }
-       else
-       {
+               strlcpy( ip, inet_ntoa( *addr ), sizeof( ip ));
+       } 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, "" );
+               close(w_fd);
+               return;
        }
-
-       /* Antwort an Parent schreiben */
-       if( write( w_fd, ip, strlen( ip ) + 1 ) != ( strlen( ip ) + 1 ))
-       {
+#ifdef DEBUG
+       Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
+#endif
+       /* Write result into pipe to parent */
+       len = strlen( ip );
+       if ((size_t)write( w_fd, ip, len ) != len) {
                Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
                close( w_fd );
-               return;
        }
-
-       if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
 } /* Do_ResolveName */
 
 
 #ifdef h_errno
 
-LOCAL CHAR *
-Get_Error( INT H_Error )
+static char *
+Get_Error( int H_Error )
 {
-       /* Fehlerbeschreibung fuer H_Error liefern */
+       /* Get error message for H_Error */
 
        switch( H_Error )
        {
@@ -263,4 +301,65 @@ Get_Error( INT H_Error )
 #endif
 
 
+static bool
+register_callback( RES_STAT *s, void (*cbfunc)(int, short))
+{
+       assert(cbfunc != NULL);
+       assert(s != NULL);
+       assert(s->resolver_fd >= 0);
+
+       if (io_setnonblock(s->resolver_fd) &&
+               io_event_create(s->resolver_fd, IO_WANTREAD, cbfunc))
+                       return true;
+
+       Log( LOG_CRIT, "Resolver: Could not register callback function: %s!", strerror(errno));
+       close(s->resolver_fd);
+       Resolve_Init(s);
+       return false;
+}
+
+
+GLOBAL bool
+Resolve_Shutdown( RES_STAT *s)
+{
+       bool ret = false;
+
+       assert(s != NULL);
+       assert(s->resolver_fd >= 0);
+
+       if (s->resolver_fd >= 0)
+               ret = io_close(s->resolver_fd);
+
+       Resolve_Init(s);
+       return ret;
+}
+
+
+/**
+ * Read result of resolver sub-process from pipe
+ */
+GLOBAL size_t
+Resolve_Read( RES_STAT *s, void* readbuf, size_t buflen)
+{
+       ssize_t bytes_read;
+
+       assert(buflen > 0);
+
+       /* Read result from pipe */
+       bytes_read = read(s->resolver_fd, readbuf, buflen);
+       if (bytes_read < 0) {
+               if (errno == EAGAIN)
+                       return 0;
+
+               Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror(errno));
+               bytes_read = 0;
+       }
+#ifdef DEBUG
+       else if (bytes_read == 0)
+               Log( LOG_DEBUG, "Resolver: Can't read result: EOF");
+#endif
+       Resolve_Shutdown(s);
+       return (size_t)bytes_read;
+}
 /* -eof- */
+