2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2009 by Alexander Barton (alex@barton.de)
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.
11 * Asynchronous resolver
23 #include <sys/socket.h>
24 #include <netinet/in.h>
42 static void Do_ResolveAddr PARAMS(( const ng_ipaddr_t *Addr, int Sock, int w_fd ));
43 static void Do_ResolveName PARAMS(( const char *Host, int w_fd ));
44 static bool register_callback PARAMS((RES_STAT *s, void (*cbfunc)(int, short)));
47 extern bool Conf_ConnectIPv4;
48 extern bool Conf_ConnectIPv6;
52 Resolver_fork(int *pipefds)
56 if (pipe(pipefds) != 0) {
57 Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
64 Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
80 * Resolve IP (asynchronous!).
83 Resolve_Addr(RES_STAT * s, const ng_ipaddr_t *Addr, int identsock,
84 void (*cbfunc) (int, short))
91 pid = Resolver_fork(pipefd);
93 LogDebug("Resolver for %s created (PID %d).", ng_ipaddr_tostr(Addr), pid);
96 s->resolver_fd = pipefd[0];
97 return register_callback(s, cbfunc);
98 } else if( pid == 0 ) {
100 Do_ResolveAddr( Addr, identsock, pipefd[1]);
101 Log_Exit_Resolver( );
109 * Resolve hostname (asynchronous!).
112 Resolve_Name( RES_STAT *s, const char *Host, void (*cbfunc)(int, short))
119 pid = Resolver_fork(pipefd);
123 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
126 s->resolver_fd = pipefd[0];
127 return register_callback(s, cbfunc);
128 } else if( pid == 0 ) {
130 Do_ResolveName(Host, pipefd[1]);
131 Log_Exit_Resolver( );
139 Resolve_Init(RES_STAT *s)
150 Get_Error( int H_Error )
152 /* Get error message for H_Error */
155 return "host not found";
157 return "name valid but no IP address defined";
159 return "name server error";
161 return "name server temporary not available";
163 return "unknown error";
166 #endif /* WANT_IPV6 */
169 /* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */
171 Do_IdentQuery(int identsock, array *resolved_addr)
180 Log_Resolver(LOG_DEBUG, "Doing IDENT lookup on socket %d ...", identsock);
182 res = ident_id( identsock, 10 );
184 Log_Resolver(LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"",
185 identsock, res ? res : "(NULL)" );
187 if (!res) /* no result */
189 if (!array_cats(resolved_addr, res))
190 Log_Resolver(LOG_WARNING, "Resolver: Cannot copy IDENT result: %s!", strerror(errno));
195 (void) resolved_addr;
201 * perform reverse DNS lookup and put result string into resbuf.
202 * If no hostname could be obtained, this function stores the string representation of
203 * the IP address in resbuf and returns false.
204 * @param IpAddr ip address to resolve
205 * @param resbuf result buffer to store DNS name/string representation of ip address
206 * @reslen size of result buffer (must be >= NGT_INET_ADDRSTRLEN)
207 * @return true if reverse lookup successful, false otherwise
210 ReverseLookup(const ng_ipaddr_t *IpAddr, char *resbuf, size_t reslen)
212 char tmp_ip_str[NG_INET_ADDRSTRLEN];
214 #ifdef HAVE_GETNAMEINFO
215 static const char funcname[]="getnameinfo";
220 res = getnameinfo((struct sockaddr *) IpAddr, ng_ipaddr_salen(IpAddr),
221 resbuf, (socklen_t)reslen, NULL, 0, NI_NAMEREQD);
225 if (res == EAI_SYSTEM)
226 errmsg = strerror(errno);
228 errmsg = gai_strerror(res);
230 const struct sockaddr_in *Addr = (const struct sockaddr_in *) IpAddr;
232 static const char funcname[]="gethostbyaddr";
234 h = gethostbyaddr((char *)&Addr->sin_addr, sizeof(Addr->sin_addr), AF_INET);
236 if (strlcpy(resbuf, h->h_name, reslen) < reslen)
238 errmsg = "hostname too long";
241 errmsg = Get_Error(h_errno);
243 errmsg = "unknown error";
244 # endif /* h_errno */
246 #endif /* HAVE_GETNAMEINFO */
249 assert(reslen >= NG_INET_ADDRSTRLEN);
250 ng_ipaddr_tostr_r(IpAddr, tmp_ip_str);
252 Log_Resolver(LOG_WARNING, "%s: Can't resolve address \"%s\": %s",
253 funcname, tmp_ip_str, errmsg);
254 strlcpy(resbuf, tmp_ip_str, reslen);
260 * perform DNS lookup of given host name and fill IpAddr with a list of
261 * ip addresses associated with that name.
262 * ip addresses found are stored in the "array *IpAddr" argument (type ng_ipaddr_t)
263 * @param hostname The domain name to look up.
264 * @param IpAddr pointer to empty and initialized array to store results
265 * @return true if lookup successful, false if domain name not found
268 ForwardLookup(const char *hostname, array *IpAddr)
272 #ifdef HAVE_GETADDRINFO
274 struct addrinfo *a, *ai_results;
275 static struct addrinfo hints;
278 hints.ai_family = AF_INET;
280 #ifdef AI_ADDRCONFIG /* glibc has this, but not e.g. netbsd 4.0 */
281 hints.ai_flags = AI_ADDRCONFIG;
283 hints.ai_socktype = SOCK_STREAM;
284 hints.ai_protocol = IPPROTO_TCP;
287 assert(Conf_ConnectIPv6 || Conf_ConnectIPv4);
289 if (!Conf_ConnectIPv6)
290 hints.ai_family = AF_INET;
291 if (!Conf_ConnectIPv4)
292 hints.ai_family = AF_INET6;
294 res = getaddrinfo(hostname, NULL, &hints, &ai_results);
298 Log_Resolver(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, strerror(errno));
301 Log_Resolver(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, gai_strerror(res));
305 for (a = ai_results; a != NULL; a = a->ai_next) {
306 assert(a->ai_addrlen <= sizeof(addr));
308 if (a->ai_addrlen > sizeof(addr))
311 memcpy(&addr, a->ai_addr, a->ai_addrlen);
313 if (!array_catb(IpAddr, (char *)&addr, sizeof(addr)))
317 freeaddrinfo(ai_results);
320 struct hostent *h = gethostbyname(hostname);
324 Log_Resolver(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, Get_Error(h_errno));
326 Log_Resolver(LOG_WARNING, "Can't resolve \"%s\"", hostname);
330 memset(&addr, 0, sizeof(addr));
332 addr.sin4.sin_family = AF_INET;
333 memcpy(&addr.sin4.sin_addr, h->h_addr, sizeof(struct in_addr));
335 return array_copyb(IpAddr, (char *)&addr, sizeof(addr));
336 #endif /* HAVE_GETADDRINFO */
341 Addr_in_list(const array *resolved_addr, const ng_ipaddr_t *Addr)
343 char tmp_ip_str[NG_INET_ADDRSTRLEN];
344 const ng_ipaddr_t *tmpAddrs = array_start(resolved_addr);
345 size_t len = array_length(resolved_addr, sizeof(*tmpAddrs));
351 if (ng_ipaddr_ipequal(Addr, tmpAddrs))
356 /* failed; print list of addresses */
357 ng_ipaddr_tostr_r(Addr, tmp_ip_str);
358 len = array_length(resolved_addr, sizeof(*tmpAddrs));
359 tmpAddrs = array_start(resolved_addr);
362 Log_Resolver(LOG_WARNING, "Address mismatch: %s != %s",
363 tmp_ip_str, ng_ipaddr_tostr(tmpAddrs));
373 Log_Forgery_NoIP(const char *ip, const char *host)
375 Log_Resolver(LOG_WARNING, "Possible forgery: %s resolved to %s "
376 "(which has no ip address)", ip, host);
380 Log_Forgery_WrongIP(const char *ip, const char *host)
382 Log_Resolver(LOG_WARNING,"Possible forgery: %s resolved to %s "
383 "(which points to different address)", ip, host);
388 ArrayWrite(int fd, const array *a)
390 size_t len = array_bytes(a);
391 const char *data = array_start(a);
395 if( (size_t)write(fd, data, len) != len )
396 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!",
402 Do_ResolveAddr(const ng_ipaddr_t *Addr, int identsock, int w_fd)
404 /* Resolver sub-process: resolve IP address and write result into
406 char hostname[CLIENT_HOST_LEN];
407 char tmp_ip_str[NG_INET_ADDRSTRLEN];
411 array_init(&resolved_addr);
412 ng_ipaddr_tostr_r(Addr, tmp_ip_str);
414 Log_Resolver(LOG_DEBUG, "Now resolving %s ...", tmp_ip_str);
416 if (!ReverseLookup(Addr, hostname, sizeof(hostname)))
419 if (ForwardLookup(hostname, &resolved_addr)) {
420 if (!Addr_in_list(&resolved_addr, Addr)) {
421 Log_Forgery_WrongIP(tmp_ip_str, hostname);
422 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
425 Log_Forgery_NoIP(tmp_ip_str, hostname);
426 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
429 Log_Resolver(LOG_DEBUG, "Ok, translated %s to \"%s\".", tmp_ip_str, hostname);
432 len = strlen(hostname);
433 hostname[len] = '\n';
434 if (!array_copyb(&resolved_addr, hostname, ++len)) {
435 Log_Resolver(LOG_CRIT, "Resolver: Can't copy resolved name: %s!", strerror(errno));
436 array_free(&resolved_addr);
440 Do_IdentQuery(identsock, &resolved_addr);
442 ArrayWrite(w_fd, &resolved_addr);
444 array_free(&resolved_addr);
445 } /* Do_ResolveAddr */
449 Do_ResolveName( const char *Host, int w_fd )
451 /* Resolver sub-process: resolve name and write result into pipe
458 Log_Resolver(LOG_DEBUG, "Now resolving \"%s\" ...", Host);
460 array_init(&IpAddrs);
461 /* Resolve hostname */
462 if (!ForwardLookup(Host, &IpAddrs)) {
467 len = array_length(&IpAddrs, sizeof(*addr));
469 addr = array_start(&IpAddrs);
471 for (; len > 0; --len,addr++) {
472 Log_Resolver(LOG_DEBUG, "translated \"%s\" to %s.",
473 Host, ng_ipaddr_tostr(addr));
476 /* Write result into pipe to parent */
477 ArrayWrite(w_fd, &IpAddrs);
479 array_free(&IpAddrs);
480 } /* Do_ResolveName */
484 register_callback( RES_STAT *s, void (*cbfunc)(int, short))
486 assert(cbfunc != NULL);
488 assert(s->resolver_fd >= 0);
490 if (io_setnonblock(s->resolver_fd) &&
491 io_event_create(s->resolver_fd, IO_WANTREAD, cbfunc))
494 Log( LOG_CRIT, "Resolver: Could not register callback function: %s!", strerror(errno));
495 close(s->resolver_fd);
502 Resolve_Shutdown( RES_STAT *s)
507 assert(s->resolver_fd >= 0);
509 if (s->resolver_fd >= 0)
510 ret = io_close(s->resolver_fd);
518 * Read result of resolver sub-process from pipe
521 Resolve_Read( RES_STAT *s, void* readbuf, size_t buflen)
527 /* Read result from pipe */
528 bytes_read = read(s->resolver_fd, readbuf, buflen);
529 if (bytes_read < 0) {
533 Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror(errno));
537 else if (bytes_read == 0)
538 Log( LOG_DEBUG, "Resolver: Can't read result: EOF");
541 return (size_t)bytes_read;