]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/resolve.c
fix fd leak (happens on failure of fork() ).
[ngircd-alex.git] / src / ngircd / resolve.c
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  */
13
14
15 #include "portab.h"
16
17 static char UNUSED id[] = "$Id: resolve.c,v 1.18 2005/09/02 21:47:30 fw Exp $";
18
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>
29
30 #ifdef IDENTAUTH
31 #ifdef HAVE_IDENT_H
32 #include <ident.h>
33 #endif
34 #endif
35
36 #include "conn.h"
37 #include "defines.h"
38 #include "log.h"
39
40 #include "exp.h"
41 #include "resolve.h"
42 #include "io.h"
43
44
45 #ifdef IDENTAUTH
46 static void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int Sock, int w_fd ));
47 #else
48 static void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int w_fd ));
49 #endif
50
51 static void Do_ResolveName PARAMS(( char *Host, int w_fd ));
52
53 #ifdef h_errno
54 static char *Get_Error PARAMS(( int H_Error ));
55 #endif
56
57 static RES_STAT *New_Res_Stat PARAMS(( void ));
58
59
60 static void
61 cb_resolver(int fd, short unused) {
62         (void) unused;  /* shut up compiler warning */
63         Read_Resolver_Result(fd);
64 }
65
66
67 #ifdef IDENTAUTH
68 GLOBAL RES_STAT *
69 Resolve_Addr( struct sockaddr_in *Addr, int Sock )
70 #else
71 GLOBAL RES_STAT *
72 Resolve_Addr( struct sockaddr_in *Addr )
73 #endif
74 {
75         /* Resolve IP (asynchronous!). On errors, e.g. if the child process
76          * can't be forked, this functions returns NULL. */
77
78         RES_STAT *s;
79         int pid;
80
81         s = New_Res_Stat( );
82         if( ! s ) return NULL;
83
84         /* For sub-process */
85         pid = fork( );
86         if( pid > 0 )
87         {
88                 close( s->pipe[1] );
89                 /* Main process */
90                 Log( LOG_DEBUG, "Resolver for %s created (PID %d).", inet_ntoa( Addr->sin_addr ), pid );
91                 if (!io_setnonblock( s->pipe[0] )) {
92                         Log( LOG_DEBUG, "Could not set Non-Blocking mode for pipefd %d", s->pipe[0] );
93                         goto out;
94                 }
95                 if (!io_event_create( s->pipe[0], IO_WANTREAD, cb_resolver )) {
96                         Log( LOG_DEBUG, "Could not add pipefd %dto event watchlist: %s",
97                                                                 s->pipe[0], strerror(errno) );
98                         goto out;
99                 }
100                 s->pid = pid;
101                 return s;
102         }
103         else if( pid == 0 )
104         {
105                 close( s->pipe[0] );
106                 /* Sub process */
107                 Log_Init_Resolver( );
108 #ifdef IDENTAUTH
109                 Do_ResolveAddr( Addr, Sock, s->pipe[1] );
110 #else
111                 Do_ResolveAddr( Addr, s->pipe[1] );
112 #endif
113                 Log_Exit_Resolver( );
114                 exit( 0 );
115         }
116         
117         Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
118
119 out: /* Error! */
120         close( s->pipe[0] );
121         close( s->pipe[1] );
122         free( s );
123 return NULL;
124 } /* Resolve_Addr */
125
126
127 GLOBAL RES_STAT *
128 Resolve_Name( char *Host )
129 {
130         /* Resolve hostname (asynchronous!). On errors, e.g. if the child
131          * process can't be forked, this functions returns NULL. */
132
133         RES_STAT *s;
134         int pid;
135
136         s = New_Res_Stat( );
137         if( ! s ) return NULL;
138
139         /* Fork sub-process */
140         pid = fork( );
141         if( pid > 0 )
142         {
143                 close( s->pipe[1] );
144                 /* Main process */
145                 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
146                 if (!io_setnonblock( s->pipe[0] )) {
147                         Log( LOG_DEBUG, "Could not set Non-Blocking mode for pipefd %d", s->pipe[0] );
148                         goto out;
149                 }
150                 if (!io_event_create( s->pipe[0], IO_WANTREAD, cb_resolver )) {
151                         Log( LOG_DEBUG, "Could not add pipefd %dto event watchlist: %s",
152                                                                 s->pipe[0], strerror(errno) );
153                         goto out;
154                 }
155                 s->pid = pid;
156                 return s;
157         }
158         else if( pid == 0 )
159         {
160                 close( s->pipe[0] );
161                 /* Sub process */
162                 Log_Init_Resolver( );
163                 Do_ResolveName( Host, s->pipe[1] );
164                 Log_Exit_Resolver( );
165                 exit( 0 );
166         }
167
168         Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
169
170 out: /* Error! */
171         close( s->pipe[0] );
172         close( s->pipe[1] );
173         free( s );
174         return NULL;
175 } /* Resolve_Name */
176
177
178 #ifdef IDENTAUTH
179 static void
180 Do_ResolveAddr( struct sockaddr_in *Addr, int Sock, int w_fd )
181 #else
182 static void
183 Do_ResolveAddr( struct sockaddr_in *Addr, int w_fd )
184 #endif
185 {
186         /* Resolver sub-process: resolve IP address and write result into
187          * pipe to parent. */
188
189         char hostname[HOST_LEN];
190         char ipstr[HOST_LEN];
191         struct hostent *h;
192         size_t len;
193         struct in_addr *addr;
194         char *ntoaptr;
195 #ifdef IDENTAUTH
196         char *res;
197 #endif
198
199         /* Resolve IP address */
200 #ifdef DEBUG
201         Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
202 #endif
203         h = gethostbyaddr( (char *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
204         if (!h) {
205 #ifdef h_errno
206                 Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\": %s!", inet_ntoa( Addr->sin_addr ), Get_Error( h_errno ));
207 #else
208                 Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\"!", inet_ntoa( Addr->sin_addr ));
209 #endif  
210                 strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
211         } else {
212                 strlcpy( hostname, h->h_name, sizeof( hostname ));
213
214                 h = gethostbyname( hostname );
215                 if ( h ) {
216                         if (memcmp(h->h_addr, &Addr->sin_addr, sizeof (struct in_addr))) {
217                                 addr = (struct in_addr*) h->h_addr;
218                                 strlcpy(ipstr, inet_ntoa(*addr), sizeof ipstr); 
219                                 ntoaptr = inet_ntoa( Addr->sin_addr );
220                                 Log(LOG_WARNING,"Possible forgery: %s resolved to %s (which is at ip %s!)",
221                                                                                 ntoaptr, hostname, ipstr);
222                                 strlcpy( hostname, ntoaptr, sizeof hostname);
223                         }
224                 } else {
225                         ntoaptr = inet_ntoa( Addr->sin_addr );
226                         Log(LOG_WARNING, "Possible forgery: %s resolved to %s (which has no ip address)",
227                                                                                         ntoaptr, hostname);
228                         strlcpy( hostname, ntoaptr, sizeof hostname);
229                 }
230         }
231         Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
232
233         /* Write resolver result into pipe to parent */
234         len = strlen( hostname ); 
235         hostname[len] = '\n'; len++;
236         if( (size_t)write( w_fd, hostname, len ) != (size_t)len )
237         {
238                 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
239                 close( w_fd );
240                 return;
241         }
242
243 #ifdef IDENTAUTH
244         /* Do "IDENT" (aka "AUTH") lookup and write result to parent */
245         Log_Resolver( LOG_DEBUG, "Doing IDENT lookup on socket %d ...", Sock );
246         res = ident_id( Sock, 10 );
247         Log_Resolver( LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"", Sock, res ? res : "" );
248
249         /* Write IDENT result into pipe to parent */
250         if (res) {
251                 len = strlen(res);
252                 res[len] = '\n';
253                 len++;
254         } else len = 1;
255
256         if( (size_t)write( w_fd, res ? res : "\n", len ) != (size_t)len )
257         {
258                 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent (IDENT): %s!", strerror( errno ));
259                 close( w_fd );
260         }
261         free( res );
262 #endif
263 } /* Do_ResolveAddr */
264
265
266 static void
267 Do_ResolveName( char *Host, int w_fd )
268 {
269         /* Resolver sub-process: resolve name and write result into pipe
270          * to parent. */
271
272         char ip[16];
273         struct hostent *h;
274         struct in_addr *addr;
275         int len;
276
277         Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
278
279         /* Resolve hostname */
280         h = gethostbyname( Host );
281         if( h )
282         {
283                 addr = (struct in_addr *)h->h_addr;
284                 strlcpy( ip, inet_ntoa( *addr ), sizeof( ip ));
285         }
286         else
287         {
288 #ifdef h_errno
289                 Log_Resolver( LOG_WARNING, "Can't resolve \"%s\": %s!", Host, Get_Error( h_errno ));
290 #else
291                 Log_Resolver( LOG_WARNING, "Can't resolve \"%s\"!", Host );
292 #endif
293                 ip[0] = '\0';
294         }
295         if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
296
297         /* Write result into pipe to parent */
298         len = strlen( ip );
299         ip[len] = '\n'; len++;
300         if( (size_t)write( w_fd, ip, len ) != (size_t)len )
301         {
302                 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
303                 close( w_fd );
304         }
305 } /* Do_ResolveName */
306
307
308 #ifdef h_errno
309
310 static char *
311 Get_Error( int H_Error )
312 {
313         /* Get error message for H_Error */
314
315         switch( H_Error )
316         {
317                 case HOST_NOT_FOUND:
318                         return "host not found";
319                 case NO_DATA:
320                         return "name valid but no IP address defined";
321                 case NO_RECOVERY:
322                         return "name server error";
323                 case TRY_AGAIN:
324                         return "name server temporary not available";
325                 default:
326                         return "unknown error";
327         }
328 } /* Get_Error */
329
330 #endif
331
332
333 static RES_STAT *
334 New_Res_Stat( void )
335 {
336         RES_STAT *s;
337
338         /* Allocate memory */
339         s = (RES_STAT *)malloc( sizeof( RES_STAT ));
340         if( ! s )
341         {
342                 Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" );
343                 return NULL;
344         }
345
346         /* Initialize pipe for result */
347         if( pipe( s->pipe ) != 0 )
348         {
349                 free( s );
350                 Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
351                 return NULL;
352         }
353
354         s->stage = 0;
355         array_init(&s->buffer);
356         s->pid = -1;
357
358         return s;
359 } /* New_Res_Stat */
360
361
362 /* -eof- */