]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/resolve.c
removed Resolve_SUCCESS() Macro and Res_Stat->sucess boolean (no longer used/needed)
[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.23 2006/02/08 15:24:10 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 static void Do_ResolveAddr PARAMS(( struct sockaddr_in *Addr, int Sock, int w_fd ));
46 static void Do_ResolveName PARAMS(( const char *Host, int w_fd ));
47 static bool register_callback PARAMS((RES_STAT *s, void (*cbfunc)(int, short)));
48
49 #ifdef h_errno
50 static char *Get_Error PARAMS(( int H_Error ));
51 #endif
52
53 static int
54 Resolver_fork(int *pipefds)
55 {
56         int pid;
57         if (pipe(pipefds) != 0) {
58                 Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
59                 return -1;
60         }
61
62         pid = fork();
63         switch(pid) {
64                 case -1:
65                         Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
66                         close(pipefds[0]);
67                         close(pipefds[1]);
68                         return -1;
69                 case 0: /* child */
70                         close(pipefds[0]);
71                         Log_Init_Resolver( );
72                         return 0;
73         }
74         /* parent */
75         close(pipefds[1]);
76         return pid; 
77 }
78
79
80 GLOBAL bool
81 Resolve_Addr( RES_STAT *s, struct sockaddr_in *Addr, int identsock, void (*cbfunc)(int, short))
82 {
83         /* Resolve IP (asynchronous!). */
84         int pid, pipefd[2];
85         assert(s != NULL);
86
87         pid = Resolver_fork(pipefd);
88         if (pid > 0) {
89 #ifdef DEBUG
90                 Log( LOG_DEBUG, "Resolver for %s created (PID %d).", inet_ntoa( Addr->sin_addr ), pid );
91 #endif
92                 s->pid = pid;
93                 s->resolver_fd = pipefd[0];
94                 return register_callback(s, cbfunc);
95         } else if( pid == 0 ) {
96                 /* Sub process */
97                 Do_ResolveAddr( Addr, identsock, pipefd[1]);
98                 Log_Exit_Resolver( );
99                 exit(0);
100         }
101         return false;
102 } /* Resolve_Addr */
103
104
105 GLOBAL bool
106 Resolve_Name( RES_STAT *s, const char *Host, void (*cbfunc)(int, short))
107 {
108         /* Resolve hostname (asynchronous!). */
109         int pid, pipefd[2];
110         assert(s != NULL);
111
112         pid = Resolver_fork(pipefd);
113         if (pid > 0) {
114                 /* Main process */
115 #ifdef DEBUG
116                 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
117 #endif
118                 s->pid = pid;
119                 s->resolver_fd = pipefd[0];
120                 return register_callback(s, cbfunc);
121         } else if( pid == 0 ) {
122                 /* Sub process */
123                 Do_ResolveName(Host, pipefd[1]);
124                 Log_Exit_Resolver( );
125                 exit(0);
126         }
127         return false;
128 } /* Resolve_Name */
129
130
131 GLOBAL void
132 Resolve_Init(RES_STAT *s)
133 {
134         assert(s != NULL);
135         s->resolver_fd = -1;
136         s->pid = 0;
137 }
138
139
140 static void
141 Do_ResolveAddr( struct sockaddr_in *Addr, int identsock, int w_fd )
142 {
143         /* Resolver sub-process: resolve IP address and write result into
144          * pipe to parent. */
145
146         char hostname[HOST_LEN];
147         char ipstr[HOST_LEN];
148         struct hostent *h;
149         size_t len;
150         struct in_addr *addr;
151         char *ntoaptr;
152         array resolved_addr;
153 #ifdef IDENTAUTH
154         char *res;
155 #endif
156         array_init(&resolved_addr);
157         /* Resolve IP address */
158 #ifdef DEBUG
159         Log_Resolver( LOG_DEBUG, "Now resolving %s ...", inet_ntoa( Addr->sin_addr ));
160 #endif
161         h = gethostbyaddr( (char *)&Addr->sin_addr, sizeof( Addr->sin_addr ), AF_INET );
162         if (!h) {
163 #ifdef h_errno
164                 Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\": %s!", inet_ntoa( Addr->sin_addr ), Get_Error( h_errno ));
165 #else
166                 Log_Resolver( LOG_WARNING, "Can't resolve address \"%s\"!", inet_ntoa( Addr->sin_addr ));
167 #endif  
168                 strlcpy( hostname, inet_ntoa( Addr->sin_addr ), sizeof( hostname ));
169         } else {
170                 strlcpy( hostname, h->h_name, sizeof( hostname ));
171
172                 h = gethostbyname( hostname );
173                 if ( h ) {
174                         if (memcmp(h->h_addr, &Addr->sin_addr, sizeof (struct in_addr))) {
175                                 addr = (struct in_addr*) h->h_addr;
176                                 strlcpy(ipstr, inet_ntoa(*addr), sizeof ipstr); 
177                                 ntoaptr = inet_ntoa( Addr->sin_addr );
178                                 Log(LOG_WARNING,"Possible forgery: %s resolved to %s (which is at ip %s!)",
179                                                                                 ntoaptr, hostname, ipstr);
180                                 strlcpy( hostname, ntoaptr, sizeof hostname);
181                         }
182                 } else {
183                         ntoaptr = inet_ntoa( Addr->sin_addr );
184                         Log(LOG_WARNING, "Possible forgery: %s resolved to %s (which has no ip address)",
185                                                                                         ntoaptr, hostname);
186                         strlcpy( hostname, ntoaptr, sizeof hostname);
187                 }
188         }
189         Log_Resolver( LOG_DEBUG, "Ok, translated %s to \"%s\".", inet_ntoa( Addr->sin_addr ), hostname );
190
191         len = strlen( hostname ); 
192         hostname[len] = '\n'; len++;
193         if (!array_copyb(&resolved_addr, hostname, len )) {
194                 Log_Resolver( LOG_CRIT, "Resolver: Can't copy resolved name: %s!", strerror( errno ));
195                 close( w_fd );
196                 return;
197         }
198
199 #ifdef IDENTAUTH
200         assert(identsock >= 0);
201         if (identsock >= 0) {
202                 /* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */
203 #ifdef DEBUG
204                 Log_Resolver( LOG_DEBUG, "Doing IDENT lookup on socket %d ...", identsock );
205 #endif
206                 res = ident_id( identsock, 10 );
207 #ifdef DEBUG
208                 Log_Resolver(LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"",
209                                                         identsock, res ? res : "(NULL)" );
210 #endif
211                 if (res && !array_cats(&resolved_addr, res)) {
212                         Log_Resolver(LOG_WARNING, "Resolver: Cannot copy IDENT result: %s!", strerror(errno));
213                         /* omit ident and return hostname only */ 
214                 }
215
216                 if (res) free(res);
217         }
218 #else
219         (void)identsock;
220 #endif
221         len = array_bytes(&resolved_addr);
222         if( (size_t)write( w_fd, array_start(&resolved_addr), len) != len )
223                 Log_Resolver( LOG_CRIT, "Resolver: Can't write result to parent: %s!", strerror( errno ));
224
225         close(w_fd);
226         array_free(&resolved_addr);
227 } /* Do_ResolveAddr */
228
229
230 static void
231 Do_ResolveName( const char *Host, int w_fd )
232 {
233         /* Resolver sub-process: resolve name and write result into pipe
234          * to parent. */
235
236         char ip[16];
237         struct hostent *h;
238         struct in_addr *addr;
239         int len;
240
241         Log_Resolver( LOG_DEBUG, "Now resolving \"%s\" ...", Host );
242
243         /* Resolve hostname */
244         h = gethostbyname( Host );
245         if( h ) {
246                 addr = (struct in_addr *)h->h_addr;
247                 strlcpy( ip, inet_ntoa( *addr ), sizeof( ip ));
248         } else {
249 #ifdef h_errno
250                 Log_Resolver( LOG_WARNING, "Can't resolve \"%s\": %s!", Host, Get_Error( h_errno ));
251 #else
252                 Log_Resolver( LOG_WARNING, "Can't resolve \"%s\"!", Host );
253 #endif
254                 close(w_fd);
255                 return;
256         }
257 #ifdef DEBUG
258         Log_Resolver( LOG_DEBUG, "Ok, translated \"%s\" to %s.", Host, ip );
259 #endif
260         /* Write result into pipe to parent */
261         len = strlen( ip );
262         if( write( w_fd, ip, len ) != len) {
263                 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!", strerror( errno ));
264                 close( w_fd );
265         }
266 } /* Do_ResolveName */
267
268
269 #ifdef h_errno
270
271 static char *
272 Get_Error( int H_Error )
273 {
274         /* Get error message for H_Error */
275
276         switch( H_Error )
277         {
278                 case HOST_NOT_FOUND:
279                         return "host not found";
280                 case NO_DATA:
281                         return "name valid but no IP address defined";
282                 case NO_RECOVERY:
283                         return "name server error";
284                 case TRY_AGAIN:
285                         return "name server temporary not available";
286                 default:
287                         return "unknown error";
288         }
289 } /* Get_Error */
290
291 #endif
292
293
294 static bool
295 register_callback( RES_STAT *s, void (*cbfunc)(int, short))
296 {
297         assert(cbfunc);
298         assert(s != NULL);
299         assert(s->resolver_fd >= 0);
300
301         if (io_setnonblock(s->resolver_fd) &&
302                 io_event_create(s->resolver_fd, IO_WANTREAD, cbfunc))
303                         return true;
304
305         Log( LOG_CRIT, "Resolver: Could not register callback function: %s!", strerror(errno));
306         Resolve_Shutdown(s);
307         return false;
308 }
309
310
311 GLOBAL bool
312 Resolve_Shutdown( RES_STAT *s)
313 {
314         bool ret = false;
315
316         assert(s != NULL);
317         assert(s->resolver_fd >= 0);
318
319         if (s->resolver_fd >= 0)
320                 ret = io_close(s->resolver_fd);
321
322         Resolve_Init(s);
323         return ret;
324 }
325
326                 
327 GLOBAL size_t
328 Resolve_Read( RES_STAT *s, void* readbuf, size_t buflen)
329 {
330         /* Read result of resolver sub-process from pipe */
331         int err, bytes_read;
332         assert(buflen > 0);
333
334         /* Read result from pipe */
335         errno = 0;
336         bytes_read = read(s->resolver_fd, readbuf, buflen);
337         if (bytes_read < 0) {
338                 if (errno != EAGAIN) {
339                         err = errno;
340                         Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror(err));
341                         Resolve_Shutdown(s);
342                         errno = err;
343                         return 0;
344                 }
345                 return 0;
346         }
347
348         Resolve_Shutdown(s);
349         if (bytes_read == 0) {  /* EOF: lookup failed */
350 #ifdef DEBUG
351                 Log( LOG_DEBUG, "Resolver: Can't read result: EOF");
352 #endif
353                 return 0;
354         }
355
356         return bytes_read;
357 }
358 /* -eof- */
359