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