]> 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 14d53804853e281cfc6cb219cafbcbe890d30ae5..dbf28736ab8c2329705be524c5d893be5266e230 100644 (file)
@@ -14,7 +14,7 @@
 
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: resolve.c,v 1.12 2005/05/28 10:46:50 fw Exp $";
+static char UNUSED id[] = "$Id: resolve.c,v 1.27 2007/11/25 18:42:37 fw Exp $";
 
 #include "imp.h"
 #include <assert.h>
@@ -24,7 +24,6 @@ static char UNUSED id[] = "$Id: resolve.c,v 1.12 2005/05/28 10:46:50 fw Exp $";
 #include <unistd.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
-#include <arpa/inet.h>
 #include <netdb.h>
 
 #ifdef IDENTAUTH
@@ -36,194 +35,210 @@ static char UNUSED id[] = "$Id: resolve.c,v 1.12 2005/05/28 10:46:50 fw Exp $";
 #include "conn.h"
 #include "defines.h"
 #include "log.h"
+#include "tool.h"
 
 #include "exp.h"
 #include "resolve.h"
+#include "io.h"
 
 
-#ifdef IDENTAUTH
-LOCAL void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int Sock, int w_fd ));
-#else
-LOCAL void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int w_fd ));
-#endif
-
-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
 
-LOCAL RES_STAT *New_Res_Stat PARAMS(( void ));
-
-
-GLOBAL void
-Resolve_Init( void )
+static pid_t
+Resolver_fork(int *pipefds)
 {
-       /* Initialize module */
+       pid_t pid;
 
-       FD_ZERO( &Resolver_FDs );
-} /* Resolve_Init */
+       if (pipe(pipefds) != 0) {
+                Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
+                return -1;
+       }
 
+       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; 
+}
 
-#ifdef IDENTAUTH
-GLOBAL RES_STAT *
-Resolve_Addr( struct sockaddr_in *Addr, int Sock )
-#else
-GLOBAL RES_STAT *
-Resolve_Addr( struct sockaddr_in *Addr )
-#endif
-{
-       /* Resolve IP (asynchronous!). On errors, e.g. if the child process
-        * can't be forked, this functions returns NULL. */
 
-       RES_STAT *s;
-       int pid;
+/**
+ * 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;
 
-       s = New_Res_Stat( );
-       if( ! s ) return NULL;
+       assert(s != NULL);
 
-       /* For sub-process */
-       pid = fork( );
-       if( pid > 0 )
-       {
-               /* Main process */
+       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 )
-       {
+               s->resolver_fd = pipefd[0];
+               return register_callback(s, cbfunc);
+       } else if( pid == 0 ) {
                /* Sub process */
-               Log_Init_Resolver( );
-#ifdef IDENTAUTH
-               Do_ResolveAddr( Addr, Sock, s->pipe[1] );
-#else
-               Do_ResolveAddr( Addr, s->pipe[1] );
-#endif
+               Do_ResolveAddr( Addr, identsock, pipefd[1]);
                Log_Exit_Resolver( );
-               exit( 0 );
-       }
-       else
-       {
-               /* Error! */
-               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))
 {
-       /* Resolve hostname (asynchronous!). On errors, e.g. if the child
-        * process can't be forked, this functions returns NULL. */
-
-       RES_STAT *s;
-       int pid;
+       int pipefd[2];
+       pid_t pid;
 
-       s = New_Res_Stat( );
-       if( ! s ) return NULL;
+       assert(s != NULL);
 
-       /* Fork sub-process */
-       pid = fork( );
-       if( pid > 0 )
-       {
+       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 )
-       {
+               s->resolver_fd = pipefd[0];
+               return register_callback(s, cbfunc);
+       } else if( pid == 0 ) {
                /* Sub process */
-               Log_Init_Resolver( );
-               Do_ResolveName( Host, s->pipe[1] );
+               Do_ResolveName(Host, pipefd[1]);
                Log_Exit_Resolver( );
-               exit( 0 );
-       }
-       else
-       {
-               /* Error! */
-               free( s );
-               Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
-               return NULL;
+               exit(0);
        }
+       return false;
 } /* Resolve_Name */
 
 
-#ifdef IDENTAUTH
-LOCAL void
-Do_ResolveAddr( struct sockaddr_in *Addr, int Sock, int w_fd )
-#else
-LOCAL void
-Do_ResolveAddr( struct sockaddr_in *Addr, int w_fd )
-#endif
+GLOBAL void
+Resolve_Init(RES_STAT *s)
+{
+       assert(s != NULL);
+       s->resolver_fd = -1;
+       s->pid = 0;
+}
+
+
+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. */
 
        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 ) strlcpy( hostname, h->h_name, sizeof( hostname ));
-       else
-       {
+       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 
                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 );
 
-       /* 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 )
-       {
-               Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
+       if (!array_copyb(&resolved_addr, hostname, len )) {
+               Log_Resolver( LOG_CRIT, "Resolver: Can't copy resolved name: %s!", strerror( errno ));
                close( w_fd );
                return;
        }
 
 #ifdef IDENTAUTH
-       /* 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 */
-       if (res) {
-               len = strlen(res);
-               res[len] = '\n';
-               len++;
-       } else len = 1;
-
-       if( (size_t)write( w_fd, res ? res : "\n", len ) != (size_t)len )
-       {
-               Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent (IDENT): %s!", strerror( errno ));
-               close( w_fd );
+       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);
        }
-       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-process: resolve name and write result into pipe
         * to parent. */
@@ -231,33 +246,30 @@ Do_ResolveName( char *Host, int w_fd )
        char ip[16];
        struct hostent *h;
        struct in_addr *addr;
-       int len;
+       size_t len;
 
        Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
 
        /* Resolve hostname */
        h = gethostbyname( Host );
-       if( h )
-       {
+       if( h ) {
                addr = (struct in_addr *)h->h_addr;
                strlcpy( ip, inet_ntoa( *addr ), sizeof( ip ));
-       }
-       else
-       {
+       } 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
-               ip[0] = '\0';
+               close(w_fd);
+               return;
        }
-       if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
-
+#ifdef DEBUG
+       Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
+#endif
        /* Write result into pipe to parent */
        len = strlen( ip );
-       ip[len] = '\n'; len++;
-       if( (size_t)write( w_fd, ip, len ) != (size_t)len )
-       {
+       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 );
        }
@@ -266,7 +278,7 @@ Do_ResolveName( char *Host, int w_fd )
 
 #ifdef h_errno
 
-LOCAL char *
+static char *
 Get_Error( int H_Error )
 {
        /* Get error message for H_Error */
@@ -289,33 +301,65 @@ Get_Error( int H_Error )
 #endif
 
 
-LOCAL RES_STAT *
-New_Res_Stat( void )
+static bool
+register_callback( RES_STAT *s, void (*cbfunc)(int, short))
 {
-       RES_STAT *s;
+       assert(cbfunc != NULL);
+       assert(s != NULL);
+       assert(s->resolver_fd >= 0);
 
-       /* Allocate memory */
-       s = (RES_STAT *)malloc( sizeof( RES_STAT ));
-       if( ! s )
-       {
-               Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" );
-               return NULL;
-       }
+       if (io_setnonblock(s->resolver_fd) &&
+               io_event_create(s->resolver_fd, IO_WANTREAD, cbfunc))
+                       return true;
 
-       /* Initialize pipe for result */
-       if( pipe( s->pipe ) != 0 )
-       {
-               free( s );
-               Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
-               return NULL;
-       }
+       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;
 
-       s->stage = 0;
-       s->bufpos = 0;
-       s->pid = -1;
+       assert(s != NULL);
+       assert(s->resolver_fd >= 0);
 
-       return s;
-} /* New_Res_Stat */
+       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- */
+