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>
37 #include "ng_ipaddr.h"
45 static void Init_Subprocess PARAMS(( void ));
46 static void Do_ResolveAddr PARAMS(( const ng_ipaddr_t *Addr, int Sock, int w_fd ));
47 static void Do_ResolveName PARAMS(( const char *Host, int w_fd ));
50 extern bool Conf_ConnectIPv4;
51 extern bool Conf_ConnectIPv6;
56 * Resolve IP (asynchronous!).
59 Resolve_Addr(PROC_STAT * s, const ng_ipaddr_t *Addr, int identsock,
60 void (*cbfunc) (int, short))
67 pid = Proc_Fork(s, pipefd, cbfunc);
69 LogDebug("Resolver for %s created (PID %d).", ng_ipaddr_tostr(Addr), pid);
71 } else if( pid == 0 ) {
74 Do_ResolveAddr( Addr, identsock, pipefd[1]);
83 * Resolve hostname (asynchronous!).
86 Resolve_Name( PROC_STAT *s, const char *Host, void (*cbfunc)(int, short))
93 pid = Proc_Fork(s, pipefd, cbfunc);
97 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
100 } else if( pid == 0 ) {
103 Do_ResolveName(Host, pipefd[1]);
104 Log_Exit_Resolver( );
112 * Signal handler for the forked resolver subprocess.
115 Signal_Handler(int Signal)
120 Log_Resolver(LOG_DEBUG, "Resolver: Got TERM signal, exiting.");
128 * Initialize forked resolver subprocess.
131 Init_Subprocess(void)
133 signal(SIGTERM, Signal_Handler);
138 #if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETNAMEINFO)
139 #if !defined(WANT_IPV6) && defined(h_errno)
141 Get_Error( int H_Error )
143 /* Get error message for H_Error */
146 return "host not found";
148 return "name valid but no IP address defined";
150 return "name server error";
152 return "name server temporary not available";
154 return "unknown error";
160 /* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */
162 Do_IdentQuery(int identsock, array *resolved_addr)
171 Log_Resolver(LOG_DEBUG, "Doing IDENT lookup on socket %d ...", identsock);
173 res = ident_id( identsock, 10 );
175 Log_Resolver(LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"",
176 identsock, res ? res : "(NULL)" );
178 if (!res) /* no result */
180 if (!array_cats(resolved_addr, res))
181 Log_Resolver(LOG_WARNING, "Resolver: Cannot copy IDENT result: %s!", strerror(errno));
186 (void) resolved_addr;
192 * perform reverse DNS lookup and put result string into resbuf.
193 * If no hostname could be obtained, this function stores the string representation of
194 * the IP address in resbuf and returns false.
195 * @param IpAddr ip address to resolve
196 * @param resbuf result buffer to store DNS name/string representation of ip address
197 * @param reslen size of result buffer (must be >= NGT_INET_ADDRSTRLEN)
198 * @return true if reverse lookup successful, false otherwise
201 ReverseLookup(const ng_ipaddr_t *IpAddr, char *resbuf, size_t reslen)
203 char tmp_ip_str[NG_INET_ADDRSTRLEN];
205 #ifdef HAVE_GETNAMEINFO
206 static const char funcname[]="getnameinfo";
211 res = getnameinfo((struct sockaddr *) IpAddr, ng_ipaddr_salen(IpAddr),
212 resbuf, (socklen_t)reslen, NULL, 0, NI_NAMEREQD);
216 if (res == EAI_SYSTEM)
217 errmsg = strerror(errno);
219 errmsg = gai_strerror(res);
221 const struct sockaddr_in *Addr = (const struct sockaddr_in *) IpAddr;
223 static const char funcname[]="gethostbyaddr";
225 h = gethostbyaddr((char *)&Addr->sin_addr, sizeof(Addr->sin_addr), AF_INET);
227 if (strlcpy(resbuf, h->h_name, reslen) < reslen)
229 errmsg = "hostname too long";
232 errmsg = Get_Error(h_errno);
234 errmsg = "unknown error";
235 # endif /* h_errno */
237 #endif /* HAVE_GETNAMEINFO */
240 assert(reslen >= NG_INET_ADDRSTRLEN);
241 ng_ipaddr_tostr_r(IpAddr, tmp_ip_str);
243 Log_Resolver(LOG_WARNING, "%s: Can't resolve address \"%s\": %s",
244 funcname, tmp_ip_str, errmsg);
245 strlcpy(resbuf, tmp_ip_str, reslen);
251 * perform DNS lookup of given host name and fill IpAddr with a list of
252 * ip addresses associated with that name.
253 * ip addresses found are stored in the "array *IpAddr" argument (type ng_ipaddr_t)
254 * @param hostname The domain name to look up.
255 * @param IpAddr pointer to empty and initialized array to store results
256 * @return true if lookup successful, false if domain name not found
259 ForwardLookup(const char *hostname, array *IpAddr)
263 #ifdef HAVE_GETADDRINFO
265 struct addrinfo *a, *ai_results;
266 static struct addrinfo hints;
269 hints.ai_family = AF_INET;
271 #ifdef AI_ADDRCONFIG /* glibc has this, but not e.g. netbsd 4.0 */
272 hints.ai_flags = AI_ADDRCONFIG;
274 hints.ai_socktype = SOCK_STREAM;
275 hints.ai_protocol = IPPROTO_TCP;
278 assert(Conf_ConnectIPv6 || Conf_ConnectIPv4);
280 if (!Conf_ConnectIPv6)
281 hints.ai_family = AF_INET;
282 if (!Conf_ConnectIPv4)
283 hints.ai_family = AF_INET6;
285 memset(&addr, 0, sizeof(addr));
287 res = getaddrinfo(hostname, NULL, &hints, &ai_results);
291 Log_Resolver(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, strerror(errno));
294 Log_Resolver(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, gai_strerror(res));
298 for (a = ai_results; a != NULL; a = a->ai_next) {
299 assert(a->ai_addrlen <= sizeof(addr));
301 if (a->ai_addrlen > sizeof(addr))
304 memcpy(&addr, a->ai_addr, a->ai_addrlen);
306 if (!array_catb(IpAddr, (char *)&addr, sizeof(addr)))
310 freeaddrinfo(ai_results);
313 struct hostent *h = gethostbyname(hostname);
317 Log_Resolver(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, Get_Error(h_errno));
319 Log_Resolver(LOG_WARNING, "Can't resolve \"%s\"", hostname);
323 memset(&addr, 0, sizeof(addr));
325 addr.sin4.sin_family = AF_INET;
326 memcpy(&addr.sin4.sin_addr, h->h_addr, sizeof(struct in_addr));
328 return array_copyb(IpAddr, (char *)&addr, sizeof(addr));
329 #endif /* HAVE_GETADDRINFO */
334 Addr_in_list(const array *resolved_addr, const ng_ipaddr_t *Addr)
336 char tmp_ip_str[NG_INET_ADDRSTRLEN];
337 const ng_ipaddr_t *tmpAddrs = array_start(resolved_addr);
338 size_t len = array_length(resolved_addr, sizeof(*tmpAddrs));
344 if (ng_ipaddr_ipequal(Addr, tmpAddrs))
349 /* failed; print list of addresses */
350 ng_ipaddr_tostr_r(Addr, tmp_ip_str);
351 len = array_length(resolved_addr, sizeof(*tmpAddrs));
352 tmpAddrs = array_start(resolved_addr);
355 Log_Resolver(LOG_WARNING, "Address mismatch: %s != %s",
356 tmp_ip_str, ng_ipaddr_tostr(tmpAddrs));
366 Log_Forgery_NoIP(const char *ip, const char *host)
368 Log_Resolver(LOG_WARNING, "Possible forgery: %s resolved to %s "
369 "(which has no ip address)", ip, host);
373 Log_Forgery_WrongIP(const char *ip, const char *host)
375 Log_Resolver(LOG_WARNING,"Possible forgery: %s resolved to %s "
376 "(which points to different address)", ip, host);
381 ArrayWrite(int fd, const array *a)
383 size_t len = array_bytes(a);
384 const char *data = array_start(a);
388 if( (size_t)write(fd, data, len) != len )
389 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!",
395 Do_ResolveAddr(const ng_ipaddr_t *Addr, int identsock, int w_fd)
397 /* Resolver sub-process: resolve IP address and write result into
399 char hostname[CLIENT_HOST_LEN];
400 char tmp_ip_str[NG_INET_ADDRSTRLEN];
404 array_init(&resolved_addr);
405 ng_ipaddr_tostr_r(Addr, tmp_ip_str);
407 Log_Resolver(LOG_DEBUG, "Now resolving %s ...", tmp_ip_str);
409 if (!ReverseLookup(Addr, hostname, sizeof(hostname)))
412 if (ForwardLookup(hostname, &resolved_addr)) {
413 if (!Addr_in_list(&resolved_addr, Addr)) {
414 Log_Forgery_WrongIP(tmp_ip_str, hostname);
415 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
418 Log_Forgery_NoIP(tmp_ip_str, hostname);
419 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
422 Log_Resolver(LOG_DEBUG, "Ok, translated %s to \"%s\".", tmp_ip_str, hostname);
425 len = strlen(hostname);
426 hostname[len] = '\n';
427 if (!array_copyb(&resolved_addr, hostname, ++len)) {
428 Log_Resolver(LOG_CRIT, "Resolver: Can't copy resolved name: %s!", strerror(errno));
429 array_free(&resolved_addr);
433 Do_IdentQuery(identsock, &resolved_addr);
435 ArrayWrite(w_fd, &resolved_addr);
437 array_free(&resolved_addr);
438 } /* Do_ResolveAddr */
442 Do_ResolveName( const char *Host, int w_fd )
444 /* Resolver sub-process: resolve name and write result into pipe
451 Log_Resolver(LOG_DEBUG, "Now resolving \"%s\" ...", Host);
453 array_init(&IpAddrs);
454 /* Resolve hostname */
455 if (!ForwardLookup(Host, &IpAddrs)) {
460 len = array_length(&IpAddrs, sizeof(*addr));
462 addr = array_start(&IpAddrs);
464 for (; len > 0; --len,addr++) {
465 Log_Resolver(LOG_DEBUG, "translated \"%s\" to %s.",
466 Host, ng_ipaddr_tostr(addr));
469 /* Write result into pipe to parent */
470 ArrayWrite(w_fd, &IpAddrs);
472 array_free(&IpAddrs);
473 } /* Do_ResolveName */
477 * Read result of resolver sub-process from pipe
480 Resolve_Read( PROC_STAT *s, void* readbuf, size_t buflen)
486 /* Read result from pipe */
487 bytes_read = read(Proc_GetPipeFd(s), readbuf, buflen);
488 if (bytes_read < 0) {
492 Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror(errno));
496 else if (bytes_read == 0)
497 Log( LOG_DEBUG, "Resolver: Can't read result: EOF");
500 return (size_t)bytes_read;