]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/resolve.c
New configuration option "Mask" for [Operator] sections to limit OPER command.
[ngircd-alex.git] / src / ngircd / resolve.c
index c833609afdc99fad0e981dac4d4e130d0576ae8c..36e16e56a9dad85a477f4f089acb9d263089c276 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * 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)
  *
  * 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
@@ -14,7 +14,7 @@
 
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: resolve.c,v 1.5 2002/12/26 17:04:54 alex Exp $";
+static char UNUSED id[] = "$Id: resolve.c,v 1.9 2004/05/11 00:01:11 alex Exp $";
 
 #include "imp.h"
 #include <assert.h>
@@ -27,6 +27,12 @@ static char UNUSED id[] = "$Id: resolve.c,v 1.5 2002/12/26 17:04:54 alex Exp $";
 #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"
@@ -35,7 +41,12 @@ static char UNUSED id[] = "$Id: resolve.c,v 1.5 2002/12/26 17:04:54 alex Exp $";
 #include "resolve.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 ));
 
 #ifdef h_errno
@@ -46,31 +57,35 @@ LOCAL CHAR *Get_Error PARAMS(( INT H_Error ));
 GLOBAL VOID
 Resolve_Init( VOID )
 {
-       /* Modul initialisieren */
+       /* Initialize module */
 
        FD_ZERO( &Resolver_FDs );
 } /* Resolve_Init */
 
 
+#ifdef IDENTAUTH
+GLOBAL RES_STAT *
+Resolve_Addr( struct sockaddr_in *Addr, int Sock )
+#else
 GLOBAL RES_STAT *
 Resolve_Addr( struct sockaddr_in *Addr )
+#endif
 {
-       /* 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. */
+       /* Resolve IP (asynchronous!). On errors, e.g. if the child process
+        * can't be forked, this functions returns NULL. */
 
        RES_STAT *s;
        INT pid;
 
-       /* Speicher anfordern */
-       s = malloc( sizeof( RES_STAT ));
+       /* Allocate memory */
+       s = (RES_STAT *)malloc( sizeof( RES_STAT ));
        if( ! s )
        {
                Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" );
                return NULL;
        }
 
-       /* Pipe fuer Antwort initialisieren */
+       /* Initialize pipe for result */
        if( pipe( s->pipe ) != 0 )
        {
                free( s );
@@ -78,28 +93,34 @@ Resolve_Addr( struct sockaddr_in *Addr )
                return NULL;
        }
 
-       /* Sub-Prozess erzeugen */
+       /* For sub-process */
        pid = fork( );
        if( pid > 0 )
        {
-               /* Haupt-Prozess */
+               /* Main process */
                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;
+               s->stage = 0;
+               s->bufpos = 0;
                return s;
        }
        else if( pid == 0 )
        {
-               /* Sub-Prozess */
+               /* Sub process */
                Log_Init_Resolver( );
+#ifdef IDENTAUTH
+               Do_ResolveAddr( Addr, Sock, s->pipe[1] );
+#else
                Do_ResolveAddr( Addr, s->pipe[1] );
+#endif
                Log_Exit_Resolver( );
                exit( 0 );
        }
        else
        {
-               /* Fehler */
+               /* Error! */
                free( s );
                Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
                return NULL;
@@ -110,22 +131,21 @@ Resolve_Addr( struct sockaddr_in *Addr )
 GLOBAL RES_STAT *
 Resolve_Name( CHAR *Host )
 {
-       /* 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. */
+       /* Resolve hostname (asynchronous!). On errors, e.g. if the child
+        * process can't be forked, this functions returns NULL. */
 
        RES_STAT *s;
        INT pid;
 
-       /* Speicher anfordern */
-       s = malloc( sizeof( RES_STAT ));
+       /* Allocate memory */
+       s = (RES_STAT *)malloc( sizeof( RES_STAT ));
        if( ! s )
        {
                Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Name]" );
                return NULL;
        }
 
-       /* Pipe fuer Antwort initialisieren */
+       /* Initialize the pipe for the result */
        if( pipe( s->pipe ) != 0 )
        {
                free( s );
@@ -133,20 +153,22 @@ Resolve_Name( CHAR *Host )
                return NULL;
        }
 
-       /* Sub-Prozess erzeugen */
+       /* Fork sub-process */
        pid = fork( );
        if( pid > 0 )
        {
-               /* Haupt-Prozess */
+               /* Main process */
                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;
+               s->stage = 0;
+               s->bufpos = 0;
                return s;
        }
        else if( pid == 0 )
        {
-               /* Sub-Prozess */
+               /* Sub process */
                Log_Init_Resolver( );
                Do_ResolveName( Host, s->pipe[1] );
                Log_Exit_Resolver( );
@@ -154,7 +176,7 @@ Resolve_Name( CHAR *Host )
        }
        else
        {
-               /* Fehler */
+               /* Error! */
                free( s );
                Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
                return NULL;
@@ -162,17 +184,26 @@ Resolve_Name( CHAR *Host )
 } /* 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
 {
-       /* Resolver Sub-Prozess: IP aufloesen und Ergebnis in Pipe schreiben. */
+       /* Resolver sub-process: resolve IP address and write result into
+        * pipe to parent. */
 
        CHAR hostname[HOST_LEN];
        struct hostent *h;
+       INT len;
+#ifdef IDENTAUTH
+       CHAR *res;
+#endif
 
+       /* Resolve IP address */
        Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
-
-       /* Namen aufloesen */
        h = gethostbyaddr( (CHAR *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
        if( h ) strlcpy( hostname, h->h_name, sizeof( hostname ));
        else
@@ -184,31 +215,52 @@ Do_ResolveAddr( struct sockaddr_in *Addr, INT w_fd )
 #endif 
                strlcpy( hostname, inet_ntoa( Addr->sin_addr ), 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 ))
+       /* 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 ));
                close( w_fd );
                return;
        }
 
-       Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
+#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 */
+       len = strlen( res ? res : "" );
+       if( res != NULL ) res[len] = '\n';
+       len++;
+       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 );
+       }
+       free( res );
+#endif
 } /* Do_ResolveAddr */
 
 
 LOCAL VOID
 Do_ResolveName( 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];
        struct hostent *h;
        struct in_addr *addr;
+       INT len;
 
        Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
 
-       /* Namen aufloesen */
+       /* Resolve hostname */
        h = gethostbyname( Host );
        if( h )
        {
@@ -224,16 +276,16 @@ Do_ResolveName( CHAR *Host, INT w_fd )
 #endif
                strcpy( ip, "" );
        }
+       if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
 
-       /* Antwort an Parent schreiben */
-       if( write( w_fd, ip, strlen( ip ) + 1 ) != ( strlen( ip ) + 1 ))
+       /* Write result into pipe to parent */
+       len = strlen( ip );
+       ip[len] = '\n'; len++;
+       if( (size_t)write( w_fd, ip, len ) != (size_t)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 */
 
 
@@ -242,7 +294,7 @@ Do_ResolveName( CHAR *Host, INT w_fd )
 LOCAL CHAR *
 Get_Error( INT H_Error )
 {
-       /* Fehlerbeschreibung fuer H_Error liefern */
+       /* Get error message for H_Error */
 
        switch( H_Error )
        {