]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/resolve.c
Minimal changes (needed for merging resolver changes)
[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.20 2005/09/11 11:42:48 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                 close( s->pipe[1] );
88                 /* Main process */
89                 Log( LOG_DEBUG, "Resolver for %s created (PID %d).", inet_ntoa( Addr->sin_addr ), pid );
90                 if (!io_setnonblock( s->pipe[0] )) {
91                         Log( LOG_DEBUG, "Could not set Non-Blocking mode for pipefd %d", s->pipe[0] );
92                         goto out;
93                 }
94                 if (!io_event_create( s->pipe[0], IO_WANTREAD, cb_resolver )) {
95                         Log( LOG_DEBUG, "Could not add pipefd %dto event watchlist: %s",
96                                                                 s->pipe[0], strerror(errno) );
97                         goto out;
98                 }
99                 s->pid = pid;
100                 return s;
101         } else if( pid == 0 ) {
102                 close( s->pipe[0] );
103                 /* Sub process */
104                 Log_Init_Resolver( );
105 #ifdef IDENTAUTH
106                 Do_ResolveAddr( Addr, Sock, s->pipe[1] );
107 #else
108                 Do_ResolveAddr( Addr, s->pipe[1] );
109 #endif
110                 Log_Exit_Resolver( );
111                 exit(0);
112         }
113         
114         Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
115
116 out: /* Error! */
117         close( s->pipe[0] );
118         close( s->pipe[1] );
119         free( s );
120 return NULL;
121 } /* Resolve_Addr */
122
123
124 GLOBAL RES_STAT *
125 Resolve_Name( char *Host )
126 {
127         /* Resolve hostname (asynchronous!). On errors, e.g. if the child
128          * process can't be forked, this functions returns NULL. */
129
130         RES_STAT *s;
131         int pid;
132
133         s = New_Res_Stat( );
134         if( ! s ) return NULL;
135
136         /* Fork sub-process */
137         pid = fork( );
138         if (pid > 0) {
139                 close( s->pipe[1] );
140                 /* Main process */
141                 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
142                 if (!io_setnonblock( s->pipe[0] )) {
143                         Log( LOG_DEBUG, "Could not set Non-Blocking mode for pipefd %d", s->pipe[0] );
144                         goto out;
145                 }
146                 if (!io_event_create( s->pipe[0], IO_WANTREAD, cb_resolver )) {
147                         Log( LOG_DEBUG, "Could not add pipefd %dto event watchlist: %s",
148                                                                 s->pipe[0], strerror(errno) );
149                         goto out;
150                 }
151                 s->pid = pid;
152                 return s;
153         } else if( pid == 0 ) {
154                 close( s->pipe[0] );
155                 /* Sub process */
156                 Log_Init_Resolver( );
157                 Do_ResolveName( Host, s->pipe[1] );
158                 Log_Exit_Resolver( );
159                 exit(0);
160         }
161
162         Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
163
164 out: /* Error! */
165         close( s->pipe[0] );
166         close( s->pipe[1] );
167         free( s );
168         return NULL;
169 } /* Resolve_Name */
170
171
172 #ifdef IDENTAUTH
173 static void
174 Do_ResolveAddr( struct sockaddr_in *Addr, int Sock, int w_fd )
175 #else
176 static void
177 Do_ResolveAddr( struct sockaddr_in *Addr, int w_fd )
178 #endif
179 {
180         /* Resolver sub-process: resolve IP address and write result into
181          * pipe to parent. */
182
183         char hostname[HOST_LEN];
184         char ipstr[HOST_LEN];
185         struct hostent *h;
186         size_t len;
187         struct in_addr *addr;
188         char *ntoaptr;
189         array resolved_addr;
190 #ifdef IDENTAUTH
191         char *res;
192 #endif
193
194         array_init(&resolved_addr);
195
196         /* Resolve IP address */
197 #ifdef DEBUG
198         Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
199 #endif
200         h = gethostbyaddr( (char *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
201         if (!h) {
202 #ifdef h_errno
203                 Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\": %s!", inet_ntoa( Addr->sin_addr ), Get_Error( h_errno ));
204 #else
205                 Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\"!", inet_ntoa( Addr->sin_addr ));
206 #endif  
207                 strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
208         } else {
209                 strlcpy( hostname, h->h_name, sizeof( hostname ));
210
211                 h = gethostbyname( hostname );
212                 if ( h ) {
213                         if (memcmp(h->h_addr, &Addr->sin_addr, sizeof (struct in_addr))) {
214                                 addr = (struct in_addr*) h->h_addr;
215                                 strlcpy(ipstr, inet_ntoa(*addr), sizeof ipstr); 
216                                 ntoaptr = inet_ntoa( Addr->sin_addr );
217                                 Log(LOG_WARNING,"Possible forgery: %s resolved to %s (which is at ip %s!)",
218                                                                                 ntoaptr, hostname, ipstr);
219                                 strlcpy( hostname, ntoaptr, sizeof hostname);
220                         }
221                 } else {
222                         ntoaptr = inet_ntoa( Addr->sin_addr );
223                         Log(LOG_WARNING, "Possible forgery: %s resolved to %s (which has no ip address)",
224                                                                                         ntoaptr, hostname);
225                         strlcpy( hostname, ntoaptr, sizeof hostname);
226                 }
227         }
228         Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
229
230         len = strlen( hostname ); 
231         hostname[len] = '\n'; len++;
232         if (!array_copyb(&resolved_addr, hostname, len )) {
233                 Log_Resolver( LOG_CRIT, "Resolver: Can't copy resolved name: %s!", strerror( errno ));
234                 close( w_fd );
235                 return;
236         }
237
238 #ifdef IDENTAUTH
239         /* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */
240         Log_Resolver( LOG_DEBUG, "Doing IDENT lookup on socket %d ...", Sock );
241         res = ident_id( Sock, 10 );
242         Log_Resolver( LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"", Sock, res ? res : "" );
243
244         if (res) {
245                 if (!array_cats(&resolved_addr, res))
246                         Log_Resolver(LOG_WARNING, "Resolver: Cannot copy IDENT result: %s!", strerror(errno));
247                 /* try to omit ident and return hostname only */ 
248         }
249
250         if (!array_catb(&resolved_addr, "\n", 1)) {
251                 close(w_fd);
252                 Log_Resolver(LOG_CRIT, "Resolver: Cannot copy result: %s!", strerror(errno));
253                 array_free(&resolved_addr);
254                 return;
255         }
256
257         if (res) free(res);
258 #endif
259         len = array_bytes(&resolved_addr);
260         if( (size_t)write( w_fd, array_start(&resolved_addr), len) != len )
261                 Log_Resolver( LOG_CRIT, "Resolver: Can't write result to parent: %s!", strerror( errno ));
262
263         close(w_fd);
264         array_free(&resolved_addr);
265 } /* Do_ResolveAddr */
266
267
268 static void
269 Do_ResolveName( char *Host, int w_fd )
270 {
271         /* Resolver sub-process: resolve name and write result into pipe
272          * to parent. */
273
274         char ip[16];
275         struct hostent *h;
276         struct in_addr *addr;
277         int len;
278
279         Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
280
281         /* Resolve hostname */
282         h = gethostbyname( Host );
283         if( h ) {
284                 addr = (struct in_addr *)h->h_addr;
285                 strlcpy( ip, inet_ntoa( *addr ), sizeof( ip ));
286         } else {
287 #ifdef h_errno
288                 Log_Resolver( LOG_WARNING, "Can't resolve \"%s\": %s!", Host, Get_Error( h_errno ));
289 #else
290                 Log_Resolver( LOG_WARNING, "Can't resolve \"%s\"!", Host );
291 #endif
292                 ip[0] = '\0';
293         }
294         if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
295
296         /* Write result into pipe to parent */
297         len = strlen( ip );
298         ip[len] = '\n'; len++;
299         if( (size_t)write( w_fd, ip, len ) != (size_t)len ) {
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 static 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 static 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         array_init(&s->buffer);
354         s->pid = -1;
355
356         return s;
357 } /* New_Res_Stat */
358
359
360 /* -eof- */