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 3012c232 2004-05-11 alex static char UNUSED id[] = "$Id: resolve.c,v 1.9 2004/05/11 00:01:11 alex 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 b79b315d 2003-12-27 alex LOCAL VOID Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, INT Sock, INT w_fd ));
46 b79b315d 2003-12-27 alex #else
47 46ec0f24 2002-05-27 alex 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 46ec0f24 2002-05-27 alex 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 f7327524 2002-05-30 alex LOCAL CHAR *Get_Error PARAMS(( INT H_Error ));
54 46ec0f24 2002-05-27 alex #endif
55 46ec0f24 2002-05-27 alex
56 46ec0f24 2002-05-27 alex
57 46ec0f24 2002-05-27 alex GLOBAL VOID
58 46ec0f24 2002-05-27 alex Resolve_Init( VOID )
59 46ec0f24 2002-05-27 alex {
60 b79b315d 2003-12-27 alex /* Initialize module */
61 46ec0f24 2002-05-27 alex
62 46ec0f24 2002-05-27 alex FD_ZERO( &Resolver_FDs );
63 46ec0f24 2002-05-27 alex } /* Resolve_Init */
64 46ec0f24 2002-05-27 alex
65 46ec0f24 2002-05-27 alex
66 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
67 46ec0f24 2002-05-27 alex GLOBAL RES_STAT *
68 b79b315d 2003-12-27 alex Resolve_Addr( struct sockaddr_in *Addr, int Sock )
69 b79b315d 2003-12-27 alex #else
70 b79b315d 2003-12-27 alex GLOBAL RES_STAT *
71 46ec0f24 2002-05-27 alex Resolve_Addr( struct sockaddr_in *Addr )
72 b79b315d 2003-12-27 alex #endif
73 46ec0f24 2002-05-27 alex {
74 b79b315d 2003-12-27 alex /* Resolve IP (asynchronous!). On errors, e.g. if the child process
75 b79b315d 2003-12-27 alex * can't be forked, this functions returns NULL. */
76 46ec0f24 2002-05-27 alex
77 46ec0f24 2002-05-27 alex RES_STAT *s;
78 46ec0f24 2002-05-27 alex INT pid;
79 46ec0f24 2002-05-27 alex
80 b79b315d 2003-12-27 alex /* Allocate memory */
81 cb76d96e 2004-03-11 alex s = (RES_STAT *)malloc( sizeof( RES_STAT ));
82 46ec0f24 2002-05-27 alex if( ! s )
83 46ec0f24 2002-05-27 alex {
84 63c36773 2002-06-09 alex Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" );
85 46ec0f24 2002-05-27 alex return NULL;
86 46ec0f24 2002-05-27 alex }
87 46ec0f24 2002-05-27 alex
88 b79b315d 2003-12-27 alex /* Initialize pipe for result */
89 46ec0f24 2002-05-27 alex if( pipe( s->pipe ) != 0 )
90 46ec0f24 2002-05-27 alex {
91 46ec0f24 2002-05-27 alex free( s );
92 46ec0f24 2002-05-27 alex Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
93 46ec0f24 2002-05-27 alex return NULL;
94 46ec0f24 2002-05-27 alex }
95 46ec0f24 2002-05-27 alex
96 b79b315d 2003-12-27 alex /* For sub-process */
97 46ec0f24 2002-05-27 alex pid = fork( );
98 46ec0f24 2002-05-27 alex if( pid > 0 )
99 46ec0f24 2002-05-27 alex {
100 b79b315d 2003-12-27 alex /* Main process */
101 46ec0f24 2002-05-27 alex Log( LOG_DEBUG, "Resolver for %s created (PID %d).", inet_ntoa( Addr->sin_addr ), pid );
102 46ec0f24 2002-05-27 alex FD_SET( s->pipe[0], &Resolver_FDs );
103 46ec0f24 2002-05-27 alex if( s->pipe[0] > Conn_MaxFD ) Conn_MaxFD = s->pipe[0];
104 46ec0f24 2002-05-27 alex s->pid = pid;
105 3012c232 2004-05-11 alex s->stage = 0;
106 3012c232 2004-05-11 alex s->bufpos = 0;
107 46ec0f24 2002-05-27 alex return s;
108 46ec0f24 2002-05-27 alex }
109 46ec0f24 2002-05-27 alex else if( pid == 0 )
110 46ec0f24 2002-05-27 alex {
111 b79b315d 2003-12-27 alex /* Sub process */
112 46ec0f24 2002-05-27 alex Log_Init_Resolver( );
113 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
114 b79b315d 2003-12-27 alex Do_ResolveAddr( Addr, Sock, s->pipe[1] );
115 b79b315d 2003-12-27 alex #else
116 46ec0f24 2002-05-27 alex Do_ResolveAddr( Addr, s->pipe[1] );
117 b79b315d 2003-12-27 alex #endif
118 46ec0f24 2002-05-27 alex Log_Exit_Resolver( );
119 46ec0f24 2002-05-27 alex exit( 0 );
120 46ec0f24 2002-05-27 alex }
121 46ec0f24 2002-05-27 alex else
122 46ec0f24 2002-05-27 alex {
123 b79b315d 2003-12-27 alex /* Error! */
124 46ec0f24 2002-05-27 alex free( s );
125 46ec0f24 2002-05-27 alex Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
126 46ec0f24 2002-05-27 alex return NULL;
127 46ec0f24 2002-05-27 alex }
128 46ec0f24 2002-05-27 alex } /* Resolve_Addr */
129 46ec0f24 2002-05-27 alex
130 46ec0f24 2002-05-27 alex
131 46ec0f24 2002-05-27 alex GLOBAL RES_STAT *
132 46ec0f24 2002-05-27 alex Resolve_Name( CHAR *Host )
133 46ec0f24 2002-05-27 alex {
134 b79b315d 2003-12-27 alex /* Resolve hostname (asynchronous!). On errors, e.g. if the child
135 b79b315d 2003-12-27 alex * process can't be forked, this functions returns NULL. */
136 46ec0f24 2002-05-27 alex
137 46ec0f24 2002-05-27 alex RES_STAT *s;
138 46ec0f24 2002-05-27 alex INT pid;
139 46ec0f24 2002-05-27 alex
140 b79b315d 2003-12-27 alex /* Allocate memory */
141 cb76d96e 2004-03-11 alex s = (RES_STAT *)malloc( sizeof( RES_STAT ));
142 46ec0f24 2002-05-27 alex if( ! s )
143 46ec0f24 2002-05-27 alex {
144 63c36773 2002-06-09 alex Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Name]" );
145 46ec0f24 2002-05-27 alex return NULL;
146 46ec0f24 2002-05-27 alex }
147 46ec0f24 2002-05-27 alex
148 b79b315d 2003-12-27 alex /* Initialize the pipe for the result */
149 46ec0f24 2002-05-27 alex if( pipe( s->pipe ) != 0 )
150 46ec0f24 2002-05-27 alex {
151 46ec0f24 2002-05-27 alex free( s );
152 46ec0f24 2002-05-27 alex Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
153 46ec0f24 2002-05-27 alex return NULL;
154 46ec0f24 2002-05-27 alex }
155 46ec0f24 2002-05-27 alex
156 b79b315d 2003-12-27 alex /* Fork sub-process */
157 46ec0f24 2002-05-27 alex pid = fork( );
158 46ec0f24 2002-05-27 alex if( pid > 0 )
159 46ec0f24 2002-05-27 alex {
160 b79b315d 2003-12-27 alex /* Main process */
161 46ec0f24 2002-05-27 alex Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
162 46ec0f24 2002-05-27 alex FD_SET( s->pipe[0], &Resolver_FDs );
163 46ec0f24 2002-05-27 alex if( s->pipe[0] > Conn_MaxFD ) Conn_MaxFD = s->pipe[0];
164 46ec0f24 2002-05-27 alex s->pid = pid;
165 3012c232 2004-05-11 alex s->stage = 0;
166 3012c232 2004-05-11 alex s->bufpos = 0;
167 46ec0f24 2002-05-27 alex return s;
168 46ec0f24 2002-05-27 alex }
169 46ec0f24 2002-05-27 alex else if( pid == 0 )
170 46ec0f24 2002-05-27 alex {
171 b79b315d 2003-12-27 alex /* Sub process */
172 46ec0f24 2002-05-27 alex Log_Init_Resolver( );
173 46ec0f24 2002-05-27 alex Do_ResolveName( Host, s->pipe[1] );
174 46ec0f24 2002-05-27 alex Log_Exit_Resolver( );
175 46ec0f24 2002-05-27 alex exit( 0 );
176 46ec0f24 2002-05-27 alex }
177 46ec0f24 2002-05-27 alex else
178 46ec0f24 2002-05-27 alex {
179 b79b315d 2003-12-27 alex /* Error! */
180 46ec0f24 2002-05-27 alex free( s );
181 46ec0f24 2002-05-27 alex Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
182 46ec0f24 2002-05-27 alex return NULL;
183 46ec0f24 2002-05-27 alex }
184 46ec0f24 2002-05-27 alex } /* Resolve_Name */
185 46ec0f24 2002-05-27 alex
186 46ec0f24 2002-05-27 alex
187 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
188 46ec0f24 2002-05-27 alex LOCAL VOID
189 b79b315d 2003-12-27 alex Do_ResolveAddr( struct sockaddr_in *Addr, int Sock, INT w_fd )
190 b79b315d 2003-12-27 alex #else
191 b79b315d 2003-12-27 alex LOCAL VOID
192 46ec0f24 2002-05-27 alex Do_ResolveAddr( struct sockaddr_in *Addr, INT w_fd )
193 b79b315d 2003-12-27 alex #endif
194 46ec0f24 2002-05-27 alex {
195 b79b315d 2003-12-27 alex /* Resolver sub-process: resolve IP address and write result into
196 b79b315d 2003-12-27 alex * pipe to parent. */
197 46ec0f24 2002-05-27 alex
198 46ec0f24 2002-05-27 alex CHAR hostname[HOST_LEN];
199 46ec0f24 2002-05-27 alex struct hostent *h;
200 3012c232 2004-05-11 alex INT len;
201 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
202 b79b315d 2003-12-27 alex CHAR *res;
203 b79b315d 2003-12-27 alex #endif
204 46ec0f24 2002-05-27 alex
205 b79b315d 2003-12-27 alex /* Resolve IP address */
206 3012c232 2004-05-11 alex Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
207 46ec0f24 2002-05-27 alex h = gethostbyaddr( (CHAR *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
208 695631b2 2002-12-26 alex if( h ) strlcpy( hostname, h->h_name, sizeof( hostname ));
209 46ec0f24 2002-05-27 alex else
210 46ec0f24 2002-05-27 alex {
211 46ec0f24 2002-05-27 alex #ifdef h_errno
212 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\": %s!", inet_ntoa( Addr->sin_addr ), Get_Error( h_errno ));
213 46ec0f24 2002-05-27 alex #else
214 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\"!", inet_ntoa( Addr->sin_addr ));
215 46ec0f24 2002-05-27 alex #endif
216 695631b2 2002-12-26 alex strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
217 46ec0f24 2002-05-27 alex }
218 3012c232 2004-05-11 alex Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
219 46ec0f24 2002-05-27 alex
220 3012c232 2004-05-11 alex /* Write resolver result into pipe to parent */
221 3012c232 2004-05-11 alex len = strlen( hostname );
222 3012c232 2004-05-11 alex hostname[len] = '\n'; len++;
223 3012c232 2004-05-11 alex if( (size_t)write( w_fd, hostname, len ) != (size_t)len )
224 46ec0f24 2002-05-27 alex {
225 46ec0f24 2002-05-27 alex Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
226 46ec0f24 2002-05-27 alex close( w_fd );
227 46ec0f24 2002-05-27 alex return;
228 46ec0f24 2002-05-27 alex }
229 3012c232 2004-05-11 alex
230 b79b315d 2003-12-27 alex #ifdef IDENTAUTH
231 3012c232 2004-05-11 alex /* Do "IDENT" (aka "AUTH") lookup and write result to parent */
232 3012c232 2004-05-11 alex Log_Resolver( LOG_DEBUG, "Doing IDENT lookup on socket %d ...", Sock );
233 3012c232 2004-05-11 alex res = ident_id( Sock, 10 );
234 3012c232 2004-05-11 alex Log_Resolver( LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"", Sock, res ? res : "" );
235 3012c232 2004-05-11 alex
236 3012c232 2004-05-11 alex /* Write IDENT result into pipe to parent */
237 3012c232 2004-05-11 alex len = strlen( res ? res : "" );
238 3012c232 2004-05-11 alex if( res != NULL ) res[len] = '\n';
239 3012c232 2004-05-11 alex len++;
240 3012c232 2004-05-11 alex if( (size_t)write( w_fd, res ? res : "\n", len ) != (size_t)len )
241 b79b315d 2003-12-27 alex {
242 b79b315d 2003-12-27 alex Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent (IDENT): %s!", strerror( errno ));
243 b79b315d 2003-12-27 alex close( w_fd );
244 b79b315d 2003-12-27 alex }
245 b79b315d 2003-12-27 alex free( res );
246 b79b315d 2003-12-27 alex #endif
247 46ec0f24 2002-05-27 alex } /* Do_ResolveAddr */
248 46ec0f24 2002-05-27 alex
249 46ec0f24 2002-05-27 alex
250 46ec0f24 2002-05-27 alex LOCAL VOID
251 46ec0f24 2002-05-27 alex Do_ResolveName( CHAR *Host, INT w_fd )
252 46ec0f24 2002-05-27 alex {
253 b79b315d 2003-12-27 alex /* Resolver sub-process: resolve name and write result into pipe
254 b79b315d 2003-12-27 alex * to parent. */
255 46ec0f24 2002-05-27 alex
256 46ec0f24 2002-05-27 alex CHAR ip[16];
257 46ec0f24 2002-05-27 alex struct hostent *h;
258 46ec0f24 2002-05-27 alex struct in_addr *addr;
259 3012c232 2004-05-11 alex INT len;
260 46ec0f24 2002-05-27 alex
261 46ec0f24 2002-05-27 alex Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
262 46ec0f24 2002-05-27 alex
263 b79b315d 2003-12-27 alex /* Resolve hostname */
264 46ec0f24 2002-05-27 alex h = gethostbyname( Host );
265 46ec0f24 2002-05-27 alex if( h )
266 46ec0f24 2002-05-27 alex {
267 46ec0f24 2002-05-27 alex addr = (struct in_addr *)h->h_addr;
268 695631b2 2002-12-26 alex strlcpy( ip, inet_ntoa( *addr ), sizeof( ip ));
269 46ec0f24 2002-05-27 alex }
270 46ec0f24 2002-05-27 alex else
271 46ec0f24 2002-05-27 alex {
272 46ec0f24 2002-05-27 alex #ifdef h_errno
273 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve \"%s\": %s!", Host, Get_Error( h_errno ));
274 46ec0f24 2002-05-27 alex #else
275 46ec0f24 2002-05-27 alex Log_Resolver( LOG_WARNING, "Can't resolve \"%s\"!", Host );
276 46ec0f24 2002-05-27 alex #endif
277 46ec0f24 2002-05-27 alex strcpy( ip, "" );
278 46ec0f24 2002-05-27 alex }
279 3012c232 2004-05-11 alex if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
280 46ec0f24 2002-05-27 alex
281 b79b315d 2003-12-27 alex /* Write result into pipe to parent */
282 3012c232 2004-05-11 alex len = strlen( ip );
283 3012c232 2004-05-11 alex ip[len] = '\n'; len++;
284 3012c232 2004-05-11 alex if( (size_t)write( w_fd, ip, len ) != (size_t)len )
285 46ec0f24 2002-05-27 alex {
286 46ec0f24 2002-05-27 alex Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
287 46ec0f24 2002-05-27 alex close( w_fd );
288 46ec0f24 2002-05-27 alex }
289 46ec0f24 2002-05-27 alex } /* Do_ResolveName */
290 46ec0f24 2002-05-27 alex
291 46ec0f24 2002-05-27 alex
292 46ec0f24 2002-05-27 alex #ifdef h_errno
293 46ec0f24 2002-05-27 alex
294 46ec0f24 2002-05-27 alex LOCAL CHAR *
295 46ec0f24 2002-05-27 alex Get_Error( INT H_Error )
296 46ec0f24 2002-05-27 alex {
297 b79b315d 2003-12-27 alex /* Get error message for H_Error */
298 46ec0f24 2002-05-27 alex
299 46ec0f24 2002-05-27 alex switch( H_Error )
300 46ec0f24 2002-05-27 alex {
301 46ec0f24 2002-05-27 alex case HOST_NOT_FOUND:
302 46ec0f24 2002-05-27 alex return "host not found";
303 46ec0f24 2002-05-27 alex case NO_DATA:
304 46ec0f24 2002-05-27 alex return "name valid but no IP address defined";
305 46ec0f24 2002-05-27 alex case NO_RECOVERY:
306 46ec0f24 2002-05-27 alex return "name server error";
307 46ec0f24 2002-05-27 alex case TRY_AGAIN:
308 46ec0f24 2002-05-27 alex return "name server temporary not available";
309 46ec0f24 2002-05-27 alex default:
310 46ec0f24 2002-05-27 alex return "unknown error";
311 46ec0f24 2002-05-27 alex }
312 46ec0f24 2002-05-27 alex } /* Get_Error */
313 46ec0f24 2002-05-27 alex
314 46ec0f24 2002-05-27 alex #endif
315 46ec0f24 2002-05-27 alex
316 46ec0f24 2002-05-27 alex
317 46ec0f24 2002-05-27 alex /* -eof- */