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