2 46ec0f24 2002-05-27 alex * ngIRCd -- The Next Generation IRC Daemon
3 b79b315d 2003-12-27 alex * Copyright (c)2001-2003 by Alexander Barton (alex@barton.de)
5 490f28ff 2002-12-12 alex * This program is free software; you can redistribute it and/or modify
6 490f28ff 2002-12-12 alex * it under the terms of the GNU General Public License as published by
7 490f28ff 2002-12-12 alex * the Free Software Foundation; either version 2 of the License, or
8 490f28ff 2002-12-12 alex * (at your option) any later version.
9 490f28ff 2002-12-12 alex * Please read the file COPYING, README and AUTHORS for more information.
11 490f28ff 2002-12-12 alex * Asynchronous resolver
15 46ec0f24 2002-05-27 alex #include "portab.h"
17 158bf554 2005-05-28 fw static char UNUSED id[] = "$Id: resolve.c,v 1.12 2005/05/28 10:46:50 fw Exp $";
19 46ec0f24 2002-05-27 alex #include "imp.h"
20 46ec0f24 2002-05-27 alex #include <assert.h>
21 46ec0f24 2002-05-27 alex #include <errno.h>
22 46ec0f24 2002-05-27 alex #include <stdlib.h>
23 46ec0f24 2002-05-27 alex #include <string.h>
24 46ec0f24 2002-05-27 alex #include <unistd.h>
25 46ec0f24 2002-05-27 alex #include <sys/socket.h>
26 46ec0f24 2002-05-27 alex #include <netinet/in.h>
27 46ec0f24 2002-05-27 alex #include <arpa/inet.h>
28 46ec0f24 2002-05-27 alex #include <netdb.h>
30 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
31 b79b315d 2003-12-27 alex #ifdef HAVE_IDENT_H
32 b79b315d 2003-12-27 alex #include <ident.h>
36 46ec0f24 2002-05-27 alex #include "conn.h"
37 46ec0f24 2002-05-27 alex #include "defines.h"
38 46ec0f24 2002-05-27 alex #include "log.h"
40 46ec0f24 2002-05-27 alex #include "exp.h"
41 46ec0f24 2002-05-27 alex #include "resolve.h"
44 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
45 8adff592 2005-03-19 fw LOCAL void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int Sock, int w_fd ));
47 8adff592 2005-03-19 fw LOCAL void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int w_fd ));
50 8adff592 2005-03-19 fw LOCAL void Do_ResolveName PARAMS(( char *Host, int w_fd ));
52 46ec0f24 2002-05-27 alex #ifdef h_errno
53 8adff592 2005-03-19 fw LOCAL char *Get_Error PARAMS(( int H_Error ));
56 8adff592 2005-03-19 fw LOCAL RES_STAT *New_Res_Stat PARAMS(( void ));
60 8adff592 2005-03-19 fw Resolve_Init( void )
62 b79b315d 2003-12-27 alex /* Initialize module */
64 46ec0f24 2002-05-27 alex FD_ZERO( &Resolver_FDs );
65 46ec0f24 2002-05-27 alex } /* Resolve_Init */
68 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
69 46ec0f24 2002-05-27 alex GLOBAL RES_STAT *
70 b79b315d 2003-12-27 alex Resolve_Addr( struct sockaddr_in *Addr, int Sock )
72 b79b315d 2003-12-27 alex GLOBAL RES_STAT *
73 46ec0f24 2002-05-27 alex Resolve_Addr( struct sockaddr_in *Addr )
76 b79b315d 2003-12-27 alex /* Resolve IP (asynchronous!). On errors, e.g. if the child process
77 b79b315d 2003-12-27 alex * can't be forked, this functions returns NULL. */
79 46ec0f24 2002-05-27 alex RES_STAT *s;
82 5e929eff 2005-03-05 alex s = New_Res_Stat( );
83 5e929eff 2005-03-05 alex if( ! s ) return NULL;
85 b79b315d 2003-12-27 alex /* For sub-process */
86 46ec0f24 2002-05-27 alex pid = fork( );
87 46ec0f24 2002-05-27 alex if( pid > 0 )
89 b79b315d 2003-12-27 alex /* Main process */
90 46ec0f24 2002-05-27 alex Log( LOG_DEBUG, "Resolver for %s created (PID %d).", inet_ntoa( Addr->sin_addr ), pid );
91 46ec0f24 2002-05-27 alex FD_SET( s->pipe[0], &Resolver_FDs );
92 46ec0f24 2002-05-27 alex if( s->pipe[0] > Conn_MaxFD ) Conn_MaxFD = s->pipe[0];
93 46ec0f24 2002-05-27 alex s->pid = pid;
96 46ec0f24 2002-05-27 alex else if( pid == 0 )
98 b79b315d 2003-12-27 alex /* Sub process */
99 46ec0f24 2002-05-27 alex Log_Init_Resolver( );
100 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
101 b79b315d 2003-12-27 alex Do_ResolveAddr( Addr, Sock, s->pipe[1] );
103 46ec0f24 2002-05-27 alex Do_ResolveAddr( Addr, s->pipe[1] );
105 46ec0f24 2002-05-27 alex Log_Exit_Resolver( );
110 b79b315d 2003-12-27 alex /* Error! */
112 46ec0f24 2002-05-27 alex Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
113 46ec0f24 2002-05-27 alex return NULL;
115 46ec0f24 2002-05-27 alex } /* Resolve_Addr */
118 46ec0f24 2002-05-27 alex GLOBAL RES_STAT *
119 8adff592 2005-03-19 fw Resolve_Name( char *Host )
121 b79b315d 2003-12-27 alex /* Resolve hostname (asynchronous!). On errors, e.g. if the child
122 b79b315d 2003-12-27 alex * process can't be forked, this functions returns NULL. */
124 46ec0f24 2002-05-27 alex RES_STAT *s;
127 5e929eff 2005-03-05 alex s = New_Res_Stat( );
128 5e929eff 2005-03-05 alex if( ! s ) return NULL;
130 b79b315d 2003-12-27 alex /* Fork sub-process */
131 46ec0f24 2002-05-27 alex pid = fork( );
132 46ec0f24 2002-05-27 alex if( pid > 0 )
134 b79b315d 2003-12-27 alex /* Main process */
135 46ec0f24 2002-05-27 alex Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
136 46ec0f24 2002-05-27 alex FD_SET( s->pipe[0], &Resolver_FDs );
137 46ec0f24 2002-05-27 alex if( s->pipe[0] > Conn_MaxFD ) Conn_MaxFD = s->pipe[0];
138 46ec0f24 2002-05-27 alex s->pid = pid;
141 46ec0f24 2002-05-27 alex else if( pid == 0 )
143 b79b315d 2003-12-27 alex /* Sub process */
144 46ec0f24 2002-05-27 alex Log_Init_Resolver( );
145 46ec0f24 2002-05-27 alex Do_ResolveName( Host, s->pipe[1] );
146 46ec0f24 2002-05-27 alex Log_Exit_Resolver( );
151 b79b315d 2003-12-27 alex /* Error! */
153 46ec0f24 2002-05-27 alex Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
154 46ec0f24 2002-05-27 alex return NULL;
156 46ec0f24 2002-05-27 alex } /* Resolve_Name */
159 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
161 8adff592 2005-03-19 fw Do_ResolveAddr( struct sockaddr_in *Addr, int Sock, int w_fd )
164 8adff592 2005-03-19 fw Do_ResolveAddr( struct sockaddr_in *Addr, int w_fd )
167 b79b315d 2003-12-27 alex /* Resolver sub-process: resolve IP address and write result into
168 b79b315d 2003-12-27 alex * pipe to parent. */
170 8adff592 2005-03-19 fw char hostname[HOST_LEN];
171 46ec0f24 2002-05-27 alex struct hostent *h;
173 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
177 b79b315d 2003-12-27 alex /* Resolve IP address */
178 3012c232 2004-05-11 alex Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
179 8adff592 2005-03-19 fw h = gethostbyaddr( (char *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
180 695631b2 2002-12-26 alex if( h ) strlcpy( hostname, h->h_name, sizeof( hostname ));
183 46ec0f24 2002-05-27 alex #ifdef h_errno
184 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\": %s!", inet_ntoa( Addr->sin_addr ), Get_Error( h_errno ));
186 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\"!", inet_ntoa( Addr->sin_addr ));
188 695631b2 2002-12-26 alex strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
190 3012c232 2004-05-11 alex Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
192 3012c232 2004-05-11 alex /* Write resolver result into pipe to parent */
193 3012c232 2004-05-11 alex len = strlen( hostname );
194 3012c232 2004-05-11 alex hostname[len] = '\n'; len++;
195 3012c232 2004-05-11 alex if( (size_t)write( w_fd, hostname, len ) != (size_t)len )
197 46ec0f24 2002-05-27 alex Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
198 46ec0f24 2002-05-27 alex close( w_fd );
202 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
203 3012c232 2004-05-11 alex /* Do "IDENT" (aka "AUTH") lookup and write result to parent */
204 3012c232 2004-05-11 alex Log_Resolver( LOG_DEBUG, "Doing IDENT lookup on socket %d ...", Sock );
205 3012c232 2004-05-11 alex res = ident_id( Sock, 10 );
206 3012c232 2004-05-11 alex Log_Resolver( LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"", Sock, res ? res : "" );
208 3012c232 2004-05-11 alex /* Write IDENT result into pipe to parent */
210 158bf554 2005-05-28 fw len = strlen(res);
211 158bf554 2005-05-28 fw res[len] = '\n';
213 158bf554 2005-05-28 fw } else len = 1;
215 3012c232 2004-05-11 alex if( (size_t)write( w_fd, res ? res : "\n", len ) != (size_t)len )
217 b79b315d 2003-12-27 alex Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent (IDENT): %s!", strerror( errno ));
218 b79b315d 2003-12-27 alex close( w_fd );
220 b79b315d 2003-12-27 alex free( res );
222 46ec0f24 2002-05-27 alex } /* Do_ResolveAddr */
226 8adff592 2005-03-19 fw Do_ResolveName( char *Host, int w_fd )
228 b79b315d 2003-12-27 alex /* Resolver sub-process: resolve name and write result into pipe
229 b79b315d 2003-12-27 alex * to parent. */
232 46ec0f24 2002-05-27 alex struct hostent *h;
233 46ec0f24 2002-05-27 alex struct in_addr *addr;
236 46ec0f24 2002-05-27 alex Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
238 b79b315d 2003-12-27 alex /* Resolve hostname */
239 46ec0f24 2002-05-27 alex h = gethostbyname( Host );
242 46ec0f24 2002-05-27 alex addr = (struct in_addr *)h->h_addr;
243 695631b2 2002-12-26 alex strlcpy( ip, inet_ntoa( *addr ), sizeof( ip ));
247 46ec0f24 2002-05-27 alex #ifdef h_errno
248 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve \"%s\": %s!", Host, Get_Error( h_errno ));
250 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve \"%s\"!", Host );
252 5e929eff 2005-03-05 alex ip[0] = '\0';
254 3012c232 2004-05-11 alex if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
256 b79b315d 2003-12-27 alex /* Write result into pipe to parent */
257 3012c232 2004-05-11 alex len = strlen( ip );
258 3012c232 2004-05-11 alex ip[len] = '\n'; len++;
259 3012c232 2004-05-11 alex if( (size_t)write( w_fd, ip, len ) != (size_t)len )
261 46ec0f24 2002-05-27 alex Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
262 46ec0f24 2002-05-27 alex close( w_fd );
264 46ec0f24 2002-05-27 alex } /* Do_ResolveName */
267 46ec0f24 2002-05-27 alex #ifdef h_errno
270 8adff592 2005-03-19 fw Get_Error( int H_Error )
272 b79b315d 2003-12-27 alex /* Get error message for H_Error */
274 46ec0f24 2002-05-27 alex switch( H_Error )
276 46ec0f24 2002-05-27 alex case HOST_NOT_FOUND:
277 46ec0f24 2002-05-27 alex return "host not found";
278 46ec0f24 2002-05-27 alex case NO_DATA:
279 46ec0f24 2002-05-27 alex return "name valid but no IP address defined";
280 46ec0f24 2002-05-27 alex case NO_RECOVERY:
281 46ec0f24 2002-05-27 alex return "name server error";
282 46ec0f24 2002-05-27 alex case TRY_AGAIN:
283 46ec0f24 2002-05-27 alex return "name server temporary not available";
285 46ec0f24 2002-05-27 alex return "unknown error";
287 46ec0f24 2002-05-27 alex } /* Get_Error */
292 5e929eff 2005-03-05 alex LOCAL RES_STAT *
293 8adff592 2005-03-19 fw New_Res_Stat( void )
295 5e929eff 2005-03-05 alex RES_STAT *s;
297 5e929eff 2005-03-05 alex /* Allocate memory */
298 5e929eff 2005-03-05 alex s = (RES_STAT *)malloc( sizeof( RES_STAT ));
301 5e929eff 2005-03-05 alex Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" );
302 5e929eff 2005-03-05 alex return NULL;
305 5e929eff 2005-03-05 alex /* Initialize pipe for result */
306 5e929eff 2005-03-05 alex if( pipe( s->pipe ) != 0 )
309 5e929eff 2005-03-05 alex Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
310 5e929eff 2005-03-05 alex return NULL;
313 5e929eff 2005-03-05 alex s->stage = 0;
314 5e929eff 2005-03-05 alex s->bufpos = 0;
315 5e929eff 2005-03-05 alex s->pid = -1;
318 5e929eff 2005-03-05 alex } /* New_Res_Stat */
321 46ec0f24 2002-05-27 alex /* -eof- */