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