]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/resolve.c
Reverse lookup now checks result by additional forward lookup
[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.15 2005/07/25 09:20:10 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 LOCAL void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int Sock, int w_fd ));
47 #else
48 LOCAL void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int w_fd ));
49 #endif
50
51 LOCAL void Do_ResolveName PARAMS(( char *Host, int w_fd ));
52
53 #ifdef h_errno
54 LOCAL char *Get_Error PARAMS(( int H_Error ));
55 #endif
56
57 LOCAL 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         free( s );
122 return NULL;
123 } /* Resolve_Addr */
124
125
126 GLOBAL RES_STAT *
127 Resolve_Name( char *Host )
128 {
129         /* Resolve hostname (asynchronous!). On errors, e.g. if the child
130          * process can't be forked, this functions returns NULL. */
131
132         RES_STAT *s;
133         int pid;
134
135         s = New_Res_Stat( );
136         if( ! s ) return NULL;
137
138         /* Fork sub-process */
139         pid = fork( );
140         if( pid > 0 )
141         {
142                 close( s->pipe[1] );
143                 /* Main process */
144                 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
145                 if (!io_setnonblock( s->pipe[0] )) {
146                         Log( LOG_DEBUG, "Could not set Non-Blocking mode for pipefd %d", s->pipe[0] );
147                         goto out;
148                 }
149                 if (!io_event_create( s->pipe[0], IO_WANTREAD, cb_resolver )) {
150                         Log( LOG_DEBUG, "Could not add pipefd %dto event watchlist: %s",
151                                                                 s->pipe[0], strerror(errno) );
152                         goto out;
153                 }
154                 s->pid = pid;
155                 return s;
156         }
157         else if( pid == 0 )
158         {
159                 close( s->pipe[0] );
160                 /* Sub process */
161                 Log_Init_Resolver( );
162                 Do_ResolveName( Host, s->pipe[1] );
163                 Log_Exit_Resolver( );
164                 exit( 0 );
165         }
166
167         Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
168
169 out: /* Error! */
170         close( s->pipe[0] );
171         free( s );
172         return NULL;
173 } /* Resolve_Name */
174
175
176 #ifdef IDENTAUTH
177 LOCAL void
178 Do_ResolveAddr( struct sockaddr_in *Addr, int Sock, int w_fd )
179 #else
180 LOCAL void
181 Do_ResolveAddr( struct sockaddr_in *Addr, int w_fd )
182 #endif
183 {
184         /* Resolver sub-process: resolve IP address and write result into
185          * pipe to parent. */
186
187         char hostname[HOST_LEN];
188         char ipstr[HOST_LEN];
189         struct hostent *h;
190         size_t len;
191         struct in_addr *addr;
192         char *ntoaptr;
193 #ifdef IDENTAUTH
194         char *res;
195 #endif
196
197         /* Resolve IP address */
198 #ifdef DEBUG
199         Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
200 #endif
201         h = gethostbyaddr( (char *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
202         if (!h) {
203 #ifdef h_errno
204                 Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\": %s!", inet_ntoa( Addr->sin_addr ), Get_Error( h_errno ));
205 #else
206                 Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\"!", inet_ntoa( Addr->sin_addr ));
207 #endif  
208                 strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
209         } else {
210                 strlcpy( hostname, h->h_name, sizeof( hostname ));
211
212                 h = gethostbyname( hostname );
213                 if ( h ) {
214                         if (memcmp(h->h_addr, &Addr->sin_addr, sizeof (struct in_addr))) {
215                                 addr = (struct in_addr*) h->h_addr;
216                                 strlcpy(ipstr, inet_ntoa(*addr), sizeof ipstr); 
217                                 ntoaptr = inet_ntoa( Addr->sin_addr );
218                                 Log(LOG_WARNING,"Possible forgery: %s resolved to %s (which is at ip %s!)",
219                                                                                 ntoaptr, hostname, ipstr);
220                                 strlcpy( hostname, ntoaptr, sizeof hostname);
221                         }
222                 } else {
223                         ntoaptr = inet_ntoa( Addr->sin_addr );
224                         Log(LOG_WARNING, "Possible forgery: %s resolved to %s (which has no ip address)",
225                                                                                         ntoaptr, hostname);
226                         strlcpy( hostname, ntoaptr, sizeof hostname);
227                 }
228         }
229         Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
230
231         /* Write resolver result into pipe to parent */
232         len = strlen( hostname ); 
233         hostname[len] = '\n'; len++;
234         if( (size_t)write( w_fd, hostname, len ) != (size_t)len )
235         {
236                 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
237                 close( w_fd );
238                 return;
239         }
240
241 #ifdef IDENTAUTH
242         /* Do "IDENT" (aka "AUTH") lookup and write result to parent */
243         Log_Resolver( LOG_DEBUG, "Doing IDENT lookup on socket %d ...", Sock );
244         res = ident_id( Sock, 10 );
245         Log_Resolver( LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"", Sock, res ? res : "" );
246
247         /* Write IDENT result into pipe to parent */
248         if (res) {
249                 len = strlen(res);
250                 res[len] = '\n';
251                 len++;
252         } else len = 1;
253
254         if( (size_t)write( w_fd, res ? res : "\n", len ) != (size_t)len )
255         {
256                 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent (IDENT): %s!", strerror( errno ));
257                 close( w_fd );
258         }
259         free( res );
260 #endif
261 } /* Do_ResolveAddr */
262
263
264 LOCAL void
265 Do_ResolveName( char *Host, int w_fd )
266 {
267         /* Resolver sub-process: resolve name and write result into pipe
268          * to parent. */
269
270         char ip[16];
271         struct hostent *h;
272         struct in_addr *addr;
273         int len;
274
275         Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
276
277         /* Resolve hostname */
278         h = gethostbyname( Host );
279         if( h )
280         {
281                 addr = (struct in_addr *)h->h_addr;
282                 strlcpy( ip, inet_ntoa( *addr ), sizeof( ip ));
283         }
284         else
285         {
286 #ifdef h_errno
287                 Log_Resolver( LOG_WARNING, "Can't resolve \"%s\": %s!", Host, Get_Error( h_errno ));
288 #else
289                 Log_Resolver( LOG_WARNING, "Can't resolve \"%s\"!", Host );
290 #endif
291                 ip[0] = '\0';
292         }
293         if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
294
295         /* Write result into pipe to parent */
296         len = strlen( ip );
297         ip[len] = '\n'; len++;
298         if( (size_t)write( w_fd, ip, len ) != (size_t)len )
299         {
300                 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
301                 close( w_fd );
302         }
303 } /* Do_ResolveName */
304
305
306 #ifdef h_errno
307
308 LOCAL char *
309 Get_Error( int H_Error )
310 {
311         /* Get error message for H_Error */
312
313         switch( H_Error )
314         {
315                 case HOST_NOT_FOUND:
316                         return "host not found";
317                 case NO_DATA:
318                         return "name valid but no IP address defined";
319                 case NO_RECOVERY:
320                         return "name server error";
321                 case TRY_AGAIN:
322                         return "name server temporary not available";
323                 default:
324                         return "unknown error";
325         }
326 } /* Get_Error */
327
328 #endif
329
330
331 LOCAL RES_STAT *
332 New_Res_Stat( void )
333 {
334         RES_STAT *s;
335
336         /* Allocate memory */
337         s = (RES_STAT *)malloc( sizeof( RES_STAT ));
338         if( ! s )
339         {
340                 Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" );
341                 return NULL;
342         }
343
344         /* Initialize pipe for result */
345         if( pipe( s->pipe ) != 0 )
346         {
347                 free( s );
348                 Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
349                 return NULL;
350         }
351
352         s->stage = 0;
353         s->bufpos = 0;
354         s->pid = -1;
355
356         return s;
357 } /* New_Res_Stat */
358
359
360 /* -eof- */