]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/resolve.c
Remove INT, LONG, BOOLEAN, STATIC, CONST, CHAR datatypes.
[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.11 2005/03/19 18:43:49 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
43
44 #ifdef IDENTAUTH
45 LOCAL void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int Sock, int w_fd ));
46 #else
47 LOCAL void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int w_fd ));
48 #endif
49
50 LOCAL void Do_ResolveName PARAMS(( char *Host, int w_fd ));
51
52 #ifdef h_errno
53 LOCAL char *Get_Error PARAMS(( int H_Error ));
54 #endif
55
56 LOCAL RES_STAT *New_Res_Stat PARAMS(( void ));
57
58
59 GLOBAL void
60 Resolve_Init( void )
61 {
62         /* Initialize module */
63
64         FD_ZERO( &Resolver_FDs );
65 } /* Resolve_Init */
66
67
68 #ifdef IDENTAUTH
69 GLOBAL RES_STAT *
70 Resolve_Addr( struct sockaddr_in *Addr, int Sock )
71 #else
72 GLOBAL RES_STAT *
73 Resolve_Addr( struct sockaddr_in *Addr )
74 #endif
75 {
76         /* Resolve IP (asynchronous!). On errors, e.g. if the child process
77          * can't be forked, this functions returns NULL. */
78
79         RES_STAT *s;
80         int pid;
81
82         s = New_Res_Stat( );
83         if( ! s ) return NULL;
84
85         /* For sub-process */
86         pid = fork( );
87         if( pid > 0 )
88         {
89                 /* Main process */
90                 Log( LOG_DEBUG, "Resolver for %s created (PID %d).", inet_ntoa( Addr->sin_addr ), pid );
91                 FD_SET( s->pipe[0], &Resolver_FDs );
92                 if( s->pipe[0] > Conn_MaxFD ) Conn_MaxFD = s->pipe[0];
93                 s->pid = pid;
94                 return s;
95         }
96         else if( pid == 0 )
97         {
98                 /* Sub process */
99                 Log_Init_Resolver( );
100 #ifdef IDENTAUTH
101                 Do_ResolveAddr( Addr, Sock, s->pipe[1] );
102 #else
103                 Do_ResolveAddr( Addr, s->pipe[1] );
104 #endif
105                 Log_Exit_Resolver( );
106                 exit( 0 );
107         }
108         else
109         {
110                 /* Error! */
111                 free( s );
112                 Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
113                 return NULL;
114         }
115 } /* Resolve_Addr */
116
117
118 GLOBAL RES_STAT *
119 Resolve_Name( char *Host )
120 {
121         /* Resolve hostname (asynchronous!). On errors, e.g. if the child
122          * process can't be forked, this functions returns NULL. */
123
124         RES_STAT *s;
125         int pid;
126
127         s = New_Res_Stat( );
128         if( ! s ) return NULL;
129
130         /* Fork sub-process */
131         pid = fork( );
132         if( pid > 0 )
133         {
134                 /* Main process */
135                 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
136                 FD_SET( s->pipe[0], &Resolver_FDs );
137                 if( s->pipe[0] > Conn_MaxFD ) Conn_MaxFD = s->pipe[0];
138                 s->pid = pid;
139                 return s;
140         }
141         else if( pid == 0 )
142         {
143                 /* Sub process */
144                 Log_Init_Resolver( );
145                 Do_ResolveName( Host, s->pipe[1] );
146                 Log_Exit_Resolver( );
147                 exit( 0 );
148         }
149         else
150         {
151                 /* Error! */
152                 free( s );
153                 Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
154                 return NULL;
155         }
156 } /* Resolve_Name */
157
158
159 #ifdef IDENTAUTH
160 LOCAL void
161 Do_ResolveAddr( struct sockaddr_in *Addr, int Sock, int w_fd )
162 #else
163 LOCAL void
164 Do_ResolveAddr( struct sockaddr_in *Addr, int w_fd )
165 #endif
166 {
167         /* Resolver sub-process: resolve IP address and write result into
168          * pipe to parent. */
169
170         char hostname[HOST_LEN];
171         struct hostent *h;
172         int len;
173 #ifdef IDENTAUTH
174         char *res;
175 #endif
176
177         /* Resolve IP address */
178         Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
179         h = gethostbyaddr( (char *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
180         if( h ) strlcpy( hostname, h->h_name, sizeof( hostname ));
181         else
182         {
183 #ifdef h_errno
184                 Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\": %s!", inet_ntoa( Addr->sin_addr ), Get_Error( h_errno ));
185 #else
186                 Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\"!", inet_ntoa( Addr->sin_addr ));
187 #endif  
188                 strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
189         }
190         Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
191
192         /* Write resolver result into pipe to parent */
193         len = strlen( hostname ); 
194         hostname[len] = '\n'; len++;
195         if( (size_t)write( w_fd, hostname, len ) != (size_t)len )
196         {
197                 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
198                 close( w_fd );
199                 return;
200         }
201
202 #ifdef IDENTAUTH
203         /* Do "IDENT" (aka "AUTH") lookup and write result to parent */
204         Log_Resolver( LOG_DEBUG, "Doing IDENT lookup on socket %d ...", Sock );
205         res = ident_id( Sock, 10 );
206         Log_Resolver( LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"", Sock, res ? res : "" );
207
208         /* Write IDENT result into pipe to parent */
209         len = strlen( res ? res : "" );
210         if( res != NULL ) res[len] = '\n';
211         len++;
212         if( (size_t)write( w_fd, res ? res : "\n", len ) != (size_t)len )
213         {
214                 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent (IDENT): %s!", strerror( errno ));
215                 close( w_fd );
216         }
217         free( res );
218 #endif
219 } /* Do_ResolveAddr */
220
221
222 LOCAL void
223 Do_ResolveName( char *Host, int w_fd )
224 {
225         /* Resolver sub-process: resolve name and write result into pipe
226          * to parent. */
227
228         char ip[16];
229         struct hostent *h;
230         struct in_addr *addr;
231         int len;
232
233         Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
234
235         /* Resolve hostname */
236         h = gethostbyname( Host );
237         if( h )
238         {
239                 addr = (struct in_addr *)h->h_addr;
240                 strlcpy( ip, inet_ntoa( *addr ), sizeof( ip ));
241         }
242         else
243         {
244 #ifdef h_errno
245                 Log_Resolver( LOG_WARNING, "Can't resolve \"%s\": %s!", Host, Get_Error( h_errno ));
246 #else
247                 Log_Resolver( LOG_WARNING, "Can't resolve \"%s\"!", Host );
248 #endif
249                 ip[0] = '\0';
250         }
251         if( ip[0] ) Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
252
253         /* Write result into pipe to parent */
254         len = strlen( ip );
255         ip[len] = '\n'; len++;
256         if( (size_t)write( w_fd, ip, len ) != (size_t)len )
257         {
258                 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
259                 close( w_fd );
260         }
261 } /* Do_ResolveName */
262
263
264 #ifdef h_errno
265
266 LOCAL char *
267 Get_Error( int H_Error )
268 {
269         /* Get error message for H_Error */
270
271         switch( H_Error )
272         {
273                 case HOST_NOT_FOUND:
274                         return "host not found";
275                 case NO_DATA:
276                         return "name valid but no IP address defined";
277                 case NO_RECOVERY:
278                         return "name server error";
279                 case TRY_AGAIN:
280                         return "name server temporary not available";
281                 default:
282                         return "unknown error";
283         }
284 } /* Get_Error */
285
286 #endif
287
288
289 LOCAL RES_STAT *
290 New_Res_Stat( void )
291 {
292         RES_STAT *s;
293
294         /* Allocate memory */
295         s = (RES_STAT *)malloc( sizeof( RES_STAT ));
296         if( ! s )
297         {
298                 Log( LOG_EMERG, "Resolver: Can't allocate memory! [Resolve_Addr]" );
299                 return NULL;
300         }
301
302         /* Initialize pipe for result */
303         if( pipe( s->pipe ) != 0 )
304         {
305                 free( s );
306                 Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
307                 return NULL;
308         }
309
310         s->stage = 0;
311         s->bufpos = 0;
312         s->pid = -1;
313
314         return s;
315 } /* New_Res_Stat */
316
317
318 /* -eof- */