Blame


1 46ec0f24 2002-05-27 alex /*
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)
4 46ec0f24 2002-05-27 alex *
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.
10 46ec0f24 2002-05-27 alex *
11 490f28ff 2002-12-12 alex * Asynchronous resolver
12 46ec0f24 2002-05-27 alex */
13 46ec0f24 2002-05-27 alex
14 46ec0f24 2002-05-27 alex
15 46ec0f24 2002-05-27 alex #include "portab.h"
16 46ec0f24 2002-05-27 alex
17 158bf554 2005-05-28 fw static char UNUSED id[] = "$Id: resolve.c,v 1.12 2005/05/28 10:46:50 fw Exp $";
18 490f28ff 2002-12-12 alex
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>
29 46ec0f24 2002-05-27 alex
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>
33 b79b315d 2003-12-27 alex #endif
34 b79b315d 2003-12-27 alex #endif
35 b79b315d 2003-12-27 alex
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"
39 46ec0f24 2002-05-27 alex
40 46ec0f24 2002-05-27 alex #include "exp.h"
41 46ec0f24 2002-05-27 alex #include "resolve.h"
42 46ec0f24 2002-05-27 alex
43 46ec0f24 2002-05-27 alex
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 ));
46 b79b315d 2003-12-27 alex #else
47 8adff592 2005-03-19 fw LOCAL void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int w_fd ));
48 b79b315d 2003-12-27 alex #endif
49 b79b315d 2003-12-27 alex
50 8adff592 2005-03-19 fw LOCAL void Do_ResolveName PARAMS(( char *Host, int w_fd ));
51 46ec0f24 2002-05-27 alex
52 46ec0f24 2002-05-27 alex #ifdef h_errno
53 8adff592 2005-03-19 fw LOCAL char *Get_Error PARAMS(( int H_Error ));
54 46ec0f24 2002-05-27 alex #endif
55 46ec0f24 2002-05-27 alex
56 8adff592 2005-03-19 fw LOCAL RES_STAT *New_Res_Stat PARAMS(( void ));
57 46ec0f24 2002-05-27 alex
58 5e929eff 2005-03-05 alex
59 8adff592 2005-03-19 fw GLOBAL void
60 8adff592 2005-03-19 fw Resolve_Init( void )
61 46ec0f24 2002-05-27 alex {
62 b79b315d 2003-12-27 alex /* Initialize module */
63 46ec0f24 2002-05-27 alex
64 46ec0f24 2002-05-27 alex FD_ZERO( &Resolver_FDs );
65 46ec0f24 2002-05-27 alex } /* Resolve_Init */
66 46ec0f24 2002-05-27 alex
67 46ec0f24 2002-05-27 alex
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 )
71 b79b315d 2003-12-27 alex #else
72 b79b315d 2003-12-27 alex GLOBAL RES_STAT *
73 46ec0f24 2002-05-27 alex Resolve_Addr( struct sockaddr_in *Addr )
74 b79b315d 2003-12-27 alex #endif
75 46ec0f24 2002-05-27 alex {
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. */
78 46ec0f24 2002-05-27 alex
79 46ec0f24 2002-05-27 alex RES_STAT *s;
80 8adff592 2005-03-19 fw int pid;
81 46ec0f24 2002-05-27 alex
82 5e929eff 2005-03-05 alex s = New_Res_Stat( );
83 5e929eff 2005-03-05 alex if( ! s ) return NULL;
84 46ec0f24 2002-05-27 alex
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 )
88 46ec0f24 2002-05-27 alex {
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;
94 46ec0f24 2002-05-27 alex return s;
95 46ec0f24 2002-05-27 alex }
96 46ec0f24 2002-05-27 alex else if( pid == 0 )
97 46ec0f24 2002-05-27 alex {
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] );
102 b79b315d 2003-12-27 alex #else
103 46ec0f24 2002-05-27 alex Do_ResolveAddr( Addr, s->pipe[1] );
104 b79b315d 2003-12-27 alex #endif
105 46ec0f24 2002-05-27 alex Log_Exit_Resolver( );
106 46ec0f24 2002-05-27 alex exit( 0 );
107 46ec0f24 2002-05-27 alex }
108 46ec0f24 2002-05-27 alex else
109 46ec0f24 2002-05-27 alex {
110 b79b315d 2003-12-27 alex /* Error! */
111 46ec0f24 2002-05-27 alex free( s );
112 46ec0f24 2002-05-27 alex Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
113 46ec0f24 2002-05-27 alex return NULL;
114 46ec0f24 2002-05-27 alex }
115 46ec0f24 2002-05-27 alex } /* Resolve_Addr */
116 46ec0f24 2002-05-27 alex
117 46ec0f24 2002-05-27 alex
118 46ec0f24 2002-05-27 alex GLOBAL RES_STAT *
119 8adff592 2005-03-19 fw Resolve_Name( char *Host )
120 46ec0f24 2002-05-27 alex {
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. */
123 46ec0f24 2002-05-27 alex
124 46ec0f24 2002-05-27 alex RES_STAT *s;
125 8adff592 2005-03-19 fw int pid;
126 46ec0f24 2002-05-27 alex
127 5e929eff 2005-03-05 alex s = New_Res_Stat( );
128 5e929eff 2005-03-05 alex if( ! s ) return NULL;
129 46ec0f24 2002-05-27 alex
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 )
133 46ec0f24 2002-05-27 alex {
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;
139 46ec0f24 2002-05-27 alex return s;
140 46ec0f24 2002-05-27 alex }
141 46ec0f24 2002-05-27 alex else if( pid == 0 )
142 46ec0f24 2002-05-27 alex {
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( );
147 46ec0f24 2002-05-27 alex exit( 0 );
148 46ec0f24 2002-05-27 alex }
149 46ec0f24 2002-05-27 alex else
150 46ec0f24 2002-05-27 alex {
151 b79b315d 2003-12-27 alex /* Error! */
152 46ec0f24 2002-05-27 alex free( s );
153 46ec0f24 2002-05-27 alex Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
154 46ec0f24 2002-05-27 alex return NULL;
155 46ec0f24 2002-05-27 alex }
156 46ec0f24 2002-05-27 alex } /* Resolve_Name */
157 46ec0f24 2002-05-27 alex
158 46ec0f24 2002-05-27 alex
159 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
160 8adff592 2005-03-19 fw LOCAL void
161 8adff592 2005-03-19 fw Do_ResolveAddr( struct sockaddr_in *Addr, int Sock, int w_fd )
162 b79b315d 2003-12-27 alex #else
163 8adff592 2005-03-19 fw LOCAL void
164 8adff592 2005-03-19 fw Do_ResolveAddr( struct sockaddr_in *Addr, int w_fd )
165 b79b315d 2003-12-27 alex #endif
166 46ec0f24 2002-05-27 alex {
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. */
169 46ec0f24 2002-05-27 alex
170 8adff592 2005-03-19 fw char hostname[HOST_LEN];
171 46ec0f24 2002-05-27 alex struct hostent *h;
172 158bf554 2005-05-28 fw size_t len;
173 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
174 8adff592 2005-03-19 fw char *res;
175 b79b315d 2003-12-27 alex #endif
176 46ec0f24 2002-05-27 alex
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 ));
181 46ec0f24 2002-05-27 alex else
182 46ec0f24 2002-05-27 alex {
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 ));
185 46ec0f24 2002-05-27 alex #else
186 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\"!", inet_ntoa( Addr->sin_addr ));
187 46ec0f24 2002-05-27 alex #endif
188 695631b2 2002-12-26 alex strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
189 46ec0f24 2002-05-27 alex }
190 3012c232 2004-05-11 alex Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
191 46ec0f24 2002-05-27 alex
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 )
196 46ec0f24 2002-05-27 alex {
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 );
199 46ec0f24 2002-05-27 alex return;
200 46ec0f24 2002-05-27 alex }
201 3012c232 2004-05-11 alex
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 : "" );
207 3012c232 2004-05-11 alex
208 3012c232 2004-05-11 alex /* Write IDENT result into pipe to parent */
209 158bf554 2005-05-28 fw if (res) {
210 158bf554 2005-05-28 fw len = strlen(res);
211 158bf554 2005-05-28 fw res[len] = '\n';
212 158bf554 2005-05-28 fw len++;
213 158bf554 2005-05-28 fw } else len = 1;
214 158bf554 2005-05-28 fw
215 3012c232 2004-05-11 alex if( (size_t)write( w_fd, res ? res : "\n", len ) != (size_t)len )
216 b79b315d 2003-12-27 alex {
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 );
219 b79b315d 2003-12-27 alex }
220 b79b315d 2003-12-27 alex free( res );
221 b79b315d 2003-12-27 alex #endif
222 46ec0f24 2002-05-27 alex } /* Do_ResolveAddr */
223 46ec0f24 2002-05-27 alex
224 46ec0f24 2002-05-27 alex
225 8adff592 2005-03-19 fw LOCAL void
226 8adff592 2005-03-19 fw Do_ResolveName( char *Host, int w_fd )
227 46ec0f24 2002-05-27 alex {
228 b79b315d 2003-12-27 alex /* Resolver sub-process: resolve name and write result into pipe
229 b79b315d 2003-12-27 alex * to parent. */
230 46ec0f24 2002-05-27 alex
231 8adff592 2005-03-19 fw char ip[16];
232 46ec0f24 2002-05-27 alex struct hostent *h;
233 46ec0f24 2002-05-27 alex struct in_addr *addr;
234 8adff592 2005-03-19 fw int len;
235 46ec0f24 2002-05-27 alex
236 46ec0f24 2002-05-27 alex Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
237 46ec0f24 2002-05-27 alex
238 b79b315d 2003-12-27 alex /* Resolve hostname */
239 46ec0f24 2002-05-27 alex h = gethostbyname( Host );
240 46ec0f24 2002-05-27 alex if( h )
241 46ec0f24 2002-05-27 alex {
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 ));
244 46ec0f24 2002-05-27 alex }
245 46ec0f24 2002-05-27 alex else
246 46ec0f24 2002-05-27 alex {
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 ));
249 46ec0f24 2002-05-27 alex #else
250 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve \"%s\"!", Host );
251 46ec0f24 2002-05-27 alex #endif
252 5e929eff 2005-03-05 alex ip[0] = '\0';
253 46ec0f24 2002-05-27 alex }
254 3012c232 2004-05-11 alex if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
255 46ec0f24 2002-05-27 alex
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 )
260 46ec0f24 2002-05-27 alex {
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 );
263 46ec0f24 2002-05-27 alex }
264 46ec0f24 2002-05-27 alex } /* Do_ResolveName */
265 46ec0f24 2002-05-27 alex
266 46ec0f24 2002-05-27 alex
267 46ec0f24 2002-05-27 alex #ifdef h_errno
268 46ec0f24 2002-05-27 alex
269 8adff592 2005-03-19 fw LOCAL char *
270 8adff592 2005-03-19 fw Get_Error( int H_Error )
271 46ec0f24 2002-05-27 alex {
272 b79b315d 2003-12-27 alex /* Get error message for H_Error */
273 46ec0f24 2002-05-27 alex
274 46ec0f24 2002-05-27 alex switch( H_Error )
275 46ec0f24 2002-05-27 alex {
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";
284 46ec0f24 2002-05-27 alex default:
285 46ec0f24 2002-05-27 alex return "unknown error";
286 46ec0f24 2002-05-27 alex }
287 46ec0f24 2002-05-27 alex } /* Get_Error */
288 46ec0f24 2002-05-27 alex
289 46ec0f24 2002-05-27 alex #endif
290 46ec0f24 2002-05-27 alex
291 46ec0f24 2002-05-27 alex
292 5e929eff 2005-03-05 alex LOCAL RES_STAT *
293 8adff592 2005-03-19 fw New_Res_Stat( void )
294 5e929eff 2005-03-05 alex {
295 5e929eff 2005-03-05 alex RES_STAT *s;
296 5e929eff 2005-03-05 alex
297 5e929eff 2005-03-05 alex /* Allocate memory */
298 5e929eff 2005-03-05 alex s = (RES_STAT *)malloc( sizeof( RES_STAT ));
299 5e929eff 2005-03-05 alex if( ! s )
300 5e929eff 2005-03-05 alex {
301 5e929eff 2005-03-05 alex Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" );
302 5e929eff 2005-03-05 alex return NULL;
303 5e929eff 2005-03-05 alex }
304 5e929eff 2005-03-05 alex
305 5e929eff 2005-03-05 alex /* Initialize pipe for result */
306 5e929eff 2005-03-05 alex if( pipe( s->pipe ) != 0 )
307 5e929eff 2005-03-05 alex {
308 5e929eff 2005-03-05 alex free( s );
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;
311 5e929eff 2005-03-05 alex }
312 5e929eff 2005-03-05 alex
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;
316 5e929eff 2005-03-05 alex
317 5e929eff 2005-03-05 alex return s;
318 5e929eff 2005-03-05 alex } /* New_Res_Stat */
319 5e929eff 2005-03-05 alex
320 5e929eff 2005-03-05 alex
321 46ec0f24 2002-05-27 alex /* -eof- */