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