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