]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/resolve.c
Wait for DNS reverse lookup (and ident request) before writing results to pipe.
[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.19 2005/09/03 11:17:16 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         array resolved_addr;
196 #ifdef IDENTAUTH
197         char *res;
198 #endif
199
200         array_init(&resolved_addr);
201
202         /* Resolve IP address */
203 #ifdef DEBUG
204         Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
205 #endif
206         h = gethostbyaddr( (char *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
207         if (!h) {
208 #ifdef h_errno
209                 Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\": %s!", inet_ntoa( Addr->sin_addr ), Get_Error( h_errno ));
210 #else
211                 Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\"!", inet_ntoa( Addr->sin_addr ));
212 #endif  
213                 strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
214         } else {
215                 strlcpy( hostname, h->h_name, sizeof( hostname ));
216
217                 h = gethostbyname( hostname );
218                 if ( h ) {
219                         if (memcmp(h->h_addr, &Addr->sin_addr, sizeof (struct in_addr))) {
220                                 addr = (struct in_addr*) h->h_addr;
221                                 strlcpy(ipstr, inet_ntoa(*addr), sizeof ipstr); 
222                                 ntoaptr = inet_ntoa( Addr->sin_addr );
223                                 Log(LOG_WARNING,"Possible forgery: %s resolved to %s (which is at ip %s!)",
224                                                                                 ntoaptr, hostname, ipstr);
225                                 strlcpy( hostname, ntoaptr, sizeof hostname);
226                         }
227                 } else {
228                         ntoaptr = inet_ntoa( Addr->sin_addr );
229                         Log(LOG_WARNING, "Possible forgery: %s resolved to %s (which has no ip address)",
230                                                                                         ntoaptr, hostname);
231                         strlcpy( hostname, ntoaptr, sizeof hostname);
232                 }
233         }
234         Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
235
236         len = strlen( hostname ); 
237         hostname[len] = '\n'; len++;
238         if (!array_copyb(&resolved_addr, hostname, len )) {
239                 Log_Resolver( LOG_CRIT, "Resolver: Can't copy resolved name: %s!", strerror( errno ));
240                 close( w_fd );
241                 return;
242         }
243
244 #ifdef IDENTAUTH
245         /* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */
246         Log_Resolver( LOG_DEBUG, "Doing IDENT lookup on socket %d ...", Sock );
247         res = ident_id( Sock, 10 );
248         Log_Resolver( LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"", Sock, res ? res : "" );
249
250         if (res) {
251                 if (!array_cats(&resolved_addr, res))
252                         Log_Resolver(LOG_WARNING, "Resolver: Cannot copy IDENT result: %s!", strerror(errno));
253                 /* try to omit ident and return hostname only */ 
254         }
255
256         if (!array_catb(&resolved_addr, "\n", 1)) {
257                 close(w_fd);
258                 Log_Resolver(LOG_CRIT, "Resolver: Cannot copy result: %s!", strerror(errno));
259                 array_free(&resolved_addr);
260                 return;
261         }
262
263         if (res) free(res);
264 #endif
265         len = array_bytes(&resolved_addr);
266         if( (size_t)write( w_fd, array_start(&resolved_addr), len) != len )
267                 Log_Resolver( LOG_CRIT, "Resolver: Can't write result to parent: %s!", strerror( errno ));
268
269         close(w_fd);
270         array_free(&resolved_addr);
271 } /* Do_ResolveAddr */
272
273
274 static void
275 Do_ResolveName( char *Host, int w_fd )
276 {
277         /* Resolver sub-process: resolve name and write result into pipe
278          * to parent. */
279
280         char ip[16];
281         struct hostent *h;
282         struct in_addr *addr;
283         int len;
284
285         Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
286
287         /* Resolve hostname */
288         h = gethostbyname( Host );
289         if( h )
290         {
291                 addr = (struct in_addr *)h->h_addr;
292                 strlcpy( ip, inet_ntoa( *addr ), sizeof( ip ));
293         }
294         else
295         {
296 #ifdef h_errno
297                 Log_Resolver( LOG_WARNING, "Can't resolve \"%s\": %s!", Host, Get_Error( h_errno ));
298 #else
299                 Log_Resolver( LOG_WARNING, "Can't resolve \"%s\"!", Host );
300 #endif
301                 ip[0] = '\0';
302         }
303         if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
304
305         /* Write result into pipe to parent */
306         len = strlen( ip );
307         ip[len] = '\n'; len++;
308         if( (size_t)write( w_fd, ip, len ) != (size_t)len )
309         {
310                 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
311                 close( w_fd );
312         }
313 } /* Do_ResolveName */
314
315
316 #ifdef h_errno
317
318 static char *
319 Get_Error( int H_Error )
320 {
321         /* Get error message for H_Error */
322
323         switch( H_Error )
324         {
325                 case HOST_NOT_FOUND:
326                         return "host not found";
327                 case NO_DATA:
328                         return "name valid but no IP address defined";
329                 case NO_RECOVERY:
330                         return "name server error";
331                 case TRY_AGAIN:
332                         return "name server temporary not available";
333                 default:
334                         return "unknown error";
335         }
336 } /* Get_Error */
337
338 #endif
339
340
341 static RES_STAT *
342 New_Res_Stat( void )
343 {
344         RES_STAT *s;
345
346         /* Allocate memory */
347         s = (RES_STAT *)malloc( sizeof( RES_STAT ));
348         if( ! s )
349         {
350                 Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" );
351                 return NULL;
352         }
353
354         /* Initialize pipe for result */
355         if( pipe( s->pipe ) != 0 )
356         {
357                 free( s );
358                 Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
359                 return NULL;
360         }
361
362         s->stage = 0;
363         array_init(&s->buffer);
364         s->pid = -1;
365
366         return s;
367 } /* New_Res_Stat */
368
369
370 /* -eof- */