Blob


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