X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fngircd%2Fresolve.c;h=d2c01775dfa4aa280d81edc9e10c89c2cee795fa;hb=47ca178a219d682c589b27e64ee1a4e936cc7bdc;hp=d261fd0a5873c2c7242b01d190d6bddc7dde7428;hpb=afef7dee82264a5c0981ac66584c8888a1329827;p=ngircd-alex.git diff --git a/src/ngircd/resolve.c b/src/ngircd/resolve.c index d261fd0a..d2c01775 100644 --- a/src/ngircd/resolve.c +++ b/src/ngircd/resolve.c @@ -14,7 +14,7 @@ #include "portab.h" -static char UNUSED id[] = "$Id: resolve.c,v 1.13 2005/07/07 18:39:08 fw Exp $"; +static char UNUSED id[] = "$Id: resolve.c,v 1.26 2006/12/17 22:50:51 fw Exp $"; #include "imp.h" #include @@ -42,205 +42,203 @@ static char UNUSED id[] = "$Id: resolve.c,v 1.13 2005/07/07 18:39:08 fw Exp $"; #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 )); +static pid_t +Resolver_fork(int *pipefds) +{ + pid_t pid; + if (pipe(pipefds) != 0) { + Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno )); + return -1; + } -static void -cb_resolver(int fd, short unused) { - (void) unused; /* shut up compiler warning */ - Read_Resolver_Result(fd); + 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!). + */ +GLOBAL bool +Resolve_Addr(RES_STAT * s, struct sockaddr_in *Addr, int identsock, + void (*cbfunc) (int, short)) { - /* Resolve IP (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); - /* For sub-process */ - pid = fork( ); - if( pid > 0 ) - { - close( s->pipe[1] ); - /* 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 ); - if (!io_setnonblock( s->pipe[0] )) { - Log( LOG_DEBUG, "Could not set Non-Blocking mode for pipefd %d", s->pipe[0] ); - goto out; - } - if (!io_event_create( s->pipe[0], IO_WANTREAD, cb_resolver )) { - Log( LOG_DEBUG, "Could not add pipefd %dto event watchlist: %s", - s->pipe[0], strerror(errno) ); - goto out; - } +#endif s->pid = pid; - return s; - } - else if( pid == 0 ) - { - close( s->pipe[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 ); + exit(0); } - - Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno )); - -out: /* Error! */ - close( s->pipe[0] ); - free( s ); -return NULL; + 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 ) - { - close( s->pipe[1] ); + pid = Resolver_fork(pipefd); + if (pid > 0) { /* Main process */ +#ifdef DEBUG Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid ); - if (!io_setnonblock( s->pipe[0] )) { - Log( LOG_DEBUG, "Could not set Non-Blocking mode for pipefd %d", s->pipe[0] ); - goto out; - } - if (!io_event_create( s->pipe[0], IO_WANTREAD, cb_resolver )) { - Log( LOG_DEBUG, "Could not add pipefd %dto event watchlist: %s", - s->pipe[0], strerror(errno) ); - goto out; - } +#endif s->pid = pid; - return s; - } - else if( pid == 0 ) - { - close( s->pipe[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 ); + exit(0); } + return false; +} /* Resolve_Name */ - Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno )); -out: /* Error! */ - close( s->pipe[0] ); - free( s ); - return NULL; -} /* Resolve_Name */ +GLOBAL void +Resolve_Init(RES_STAT *s) +{ + assert(s != NULL); + s->resolver_fd = -1; + s->pid = 0; +} -#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 +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. */ @@ -248,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 ); } @@ -283,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 */ @@ -306,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); + + 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; +} - /* Allocate memory */ - s = (RES_STAT *)malloc( sizeof( RES_STAT )); - if( ! s ) - { - Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" ); - return NULL; - } - /* 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; - } +GLOBAL bool +Resolve_Shutdown( RES_STAT *s) +{ + bool ret = false; + + assert(s != NULL); + assert(s->resolver_fd >= 0); - s->stage = 0; - s->bufpos = 0; - s->pid = -1; + if (s->resolver_fd >= 0) + ret = io_close(s->resolver_fd); - return s; -} /* New_Res_Stat */ + 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- */ +