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 Do_ResolveAddr PARAMS(( const ng_ipaddr_t *Addr, int Sock, int w_fd ));
46 static void Do_ResolveName PARAMS(( const char *Host, int w_fd ));
49 extern bool Conf_ConnectIPv4;
50 extern bool Conf_ConnectIPv6;
55 * Resolve IP (asynchronous!).
58 Resolve_Addr(PROC_STAT * s, const ng_ipaddr_t *Addr, int identsock,
59 void (*cbfunc) (int, short))
66 pid = Proc_Fork(s, pipefd, cbfunc);
68 LogDebug("Resolver for %s created (PID %d).", ng_ipaddr_tostr(Addr), pid);
70 } else if( pid == 0 ) {
73 Do_ResolveAddr( Addr, identsock, pipefd[1]);
82 * Resolve hostname (asynchronous!).
85 Resolve_Name( PROC_STAT *s, const char *Host, void (*cbfunc)(int, short))
92 pid = Proc_Fork(s, pipefd, cbfunc);
96 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
99 } else if( pid == 0 ) {
102 Do_ResolveName(Host, pipefd[1]);
103 Log_Exit_Resolver( );
110 #if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETNAMEINFO)
111 #if !defined(WANT_IPV6) && defined(h_errno)
113 Get_Error( int H_Error )
115 /* Get error message for H_Error */
118 return "host not found";
120 return "name valid but no IP address defined";
122 return "name server error";
124 return "name server temporary not available";
126 return "unknown error";
132 /* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */
134 Do_IdentQuery(int identsock, array *resolved_addr)
143 Log_Resolver(LOG_DEBUG, "Doing IDENT lookup on socket %d ...", identsock);
145 res = ident_id( identsock, 10 );
147 Log_Resolver(LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"",
148 identsock, res ? res : "(NULL)" );
150 if (!res) /* no result */
152 if (!array_cats(resolved_addr, res))
153 Log_Resolver(LOG_WARNING, "Resolver: Cannot copy IDENT result: %s!", strerror(errno));
158 (void) resolved_addr;
164 * perform reverse DNS lookup and put result string into resbuf.
165 * If no hostname could be obtained, this function stores the string representation of
166 * the IP address in resbuf and returns false.
167 * @param IpAddr ip address to resolve
168 * @param resbuf result buffer to store DNS name/string representation of ip address
169 * @param reslen size of result buffer (must be >= NGT_INET_ADDRSTRLEN)
170 * @return true if reverse lookup successful, false otherwise
173 ReverseLookup(const ng_ipaddr_t *IpAddr, char *resbuf, size_t reslen)
175 char tmp_ip_str[NG_INET_ADDRSTRLEN];
177 #ifdef HAVE_GETNAMEINFO
178 static const char funcname[]="getnameinfo";
183 res = getnameinfo((struct sockaddr *) IpAddr, ng_ipaddr_salen(IpAddr),
184 resbuf, (socklen_t)reslen, NULL, 0, NI_NAMEREQD);
188 if (res == EAI_SYSTEM)
189 errmsg = strerror(errno);
191 errmsg = gai_strerror(res);
193 const struct sockaddr_in *Addr = (const struct sockaddr_in *) IpAddr;
195 static const char funcname[]="gethostbyaddr";
197 h = gethostbyaddr((char *)&Addr->sin_addr, sizeof(Addr->sin_addr), AF_INET);
199 if (strlcpy(resbuf, h->h_name, reslen) < reslen)
201 errmsg = "hostname too long";
204 errmsg = Get_Error(h_errno);
206 errmsg = "unknown error";
207 # endif /* h_errno */
209 #endif /* HAVE_GETNAMEINFO */
212 assert(reslen >= NG_INET_ADDRSTRLEN);
213 ng_ipaddr_tostr_r(IpAddr, tmp_ip_str);
215 Log_Resolver(LOG_WARNING, "%s: Can't resolve address \"%s\": %s",
216 funcname, tmp_ip_str, errmsg);
217 strlcpy(resbuf, tmp_ip_str, reslen);
223 * perform DNS lookup of given host name and fill IpAddr with a list of
224 * ip addresses associated with that name.
225 * ip addresses found are stored in the "array *IpAddr" argument (type ng_ipaddr_t)
226 * @param hostname The domain name to look up.
227 * @param IpAddr pointer to empty and initialized array to store results
228 * @return true if lookup successful, false if domain name not found
231 ForwardLookup(const char *hostname, array *IpAddr)
235 #ifdef HAVE_GETADDRINFO
237 struct addrinfo *a, *ai_results;
238 static struct addrinfo hints;
241 hints.ai_family = AF_INET;
243 #ifdef AI_ADDRCONFIG /* glibc has this, but not e.g. netbsd 4.0 */
244 hints.ai_flags = AI_ADDRCONFIG;
246 hints.ai_socktype = SOCK_STREAM;
247 hints.ai_protocol = IPPROTO_TCP;
250 assert(Conf_ConnectIPv6 || Conf_ConnectIPv4);
252 if (!Conf_ConnectIPv6)
253 hints.ai_family = AF_INET;
254 if (!Conf_ConnectIPv4)
255 hints.ai_family = AF_INET6;
257 memset(&addr, 0, sizeof(addr));
259 res = getaddrinfo(hostname, NULL, &hints, &ai_results);
263 Log_Resolver(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, strerror(errno));
266 Log_Resolver(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, gai_strerror(res));
270 for (a = ai_results; a != NULL; a = a->ai_next) {
271 assert(a->ai_addrlen <= sizeof(addr));
273 if (a->ai_addrlen > sizeof(addr))
276 memcpy(&addr, a->ai_addr, a->ai_addrlen);
278 if (!array_catb(IpAddr, (char *)&addr, sizeof(addr)))
282 freeaddrinfo(ai_results);
285 struct hostent *h = gethostbyname(hostname);
289 Log_Resolver(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, Get_Error(h_errno));
291 Log_Resolver(LOG_WARNING, "Can't resolve \"%s\"", hostname);
295 memset(&addr, 0, sizeof(addr));
297 addr.sin4.sin_family = AF_INET;
298 memcpy(&addr.sin4.sin_addr, h->h_addr, sizeof(struct in_addr));
300 return array_copyb(IpAddr, (char *)&addr, sizeof(addr));
301 #endif /* HAVE_GETADDRINFO */
306 Addr_in_list(const array *resolved_addr, const ng_ipaddr_t *Addr)
308 char tmp_ip_str[NG_INET_ADDRSTRLEN];
309 const ng_ipaddr_t *tmpAddrs = array_start(resolved_addr);
310 size_t len = array_length(resolved_addr, sizeof(*tmpAddrs));
316 if (ng_ipaddr_ipequal(Addr, tmpAddrs))
321 /* failed; print list of addresses */
322 ng_ipaddr_tostr_r(Addr, tmp_ip_str);
323 len = array_length(resolved_addr, sizeof(*tmpAddrs));
324 tmpAddrs = array_start(resolved_addr);
327 Log_Resolver(LOG_WARNING, "Address mismatch: %s != %s",
328 tmp_ip_str, ng_ipaddr_tostr(tmpAddrs));
338 Log_Forgery_NoIP(const char *ip, const char *host)
340 Log_Resolver(LOG_WARNING, "Possible forgery: %s resolved to %s "
341 "(which has no ip address)", ip, host);
345 Log_Forgery_WrongIP(const char *ip, const char *host)
347 Log_Resolver(LOG_WARNING,"Possible forgery: %s resolved to %s "
348 "(which points to different address)", ip, host);
353 ArrayWrite(int fd, const array *a)
355 size_t len = array_bytes(a);
356 const char *data = array_start(a);
360 if( (size_t)write(fd, data, len) != len )
361 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!",
367 Do_ResolveAddr(const ng_ipaddr_t *Addr, int identsock, int w_fd)
369 /* Resolver sub-process: resolve IP address and write result into
371 char hostname[CLIENT_HOST_LEN];
372 char tmp_ip_str[NG_INET_ADDRSTRLEN];
376 array_init(&resolved_addr);
377 ng_ipaddr_tostr_r(Addr, tmp_ip_str);
379 Log_Resolver(LOG_DEBUG, "Now resolving %s ...", tmp_ip_str);
381 if (!ReverseLookup(Addr, hostname, sizeof(hostname)))
384 if (ForwardLookup(hostname, &resolved_addr)) {
385 if (!Addr_in_list(&resolved_addr, Addr)) {
386 Log_Forgery_WrongIP(tmp_ip_str, hostname);
387 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
390 Log_Forgery_NoIP(tmp_ip_str, hostname);
391 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
394 Log_Resolver(LOG_DEBUG, "Ok, translated %s to \"%s\".", tmp_ip_str, hostname);
397 len = strlen(hostname);
398 hostname[len] = '\n';
399 if (!array_copyb(&resolved_addr, hostname, ++len)) {
400 Log_Resolver(LOG_CRIT, "Resolver: Can't copy resolved name: %s!", strerror(errno));
401 array_free(&resolved_addr);
405 Do_IdentQuery(identsock, &resolved_addr);
407 ArrayWrite(w_fd, &resolved_addr);
409 array_free(&resolved_addr);
410 } /* Do_ResolveAddr */
414 Do_ResolveName( const char *Host, int w_fd )
416 /* Resolver sub-process: resolve name and write result into pipe
423 Log_Resolver(LOG_DEBUG, "Now resolving \"%s\" ...", Host);
425 array_init(&IpAddrs);
426 /* Resolve hostname */
427 if (!ForwardLookup(Host, &IpAddrs)) {
432 len = array_length(&IpAddrs, sizeof(*addr));
434 addr = array_start(&IpAddrs);
436 for (; len > 0; --len,addr++) {
437 Log_Resolver(LOG_DEBUG, "translated \"%s\" to %s.",
438 Host, ng_ipaddr_tostr(addr));
441 /* Write result into pipe to parent */
442 ArrayWrite(w_fd, &IpAddrs);
444 array_free(&IpAddrs);
445 } /* Do_ResolveName */
449 * Read result of resolver sub-process from pipe
452 Resolve_Read( PROC_STAT *s, void* readbuf, size_t buflen)
458 /* Read result from pipe */
459 bytes_read = read(Proc_GetPipeFd(s), readbuf, buflen);
460 if (bytes_read < 0) {
464 Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror(errno));
468 else if (bytes_read == 0)
469 Log( LOG_DEBUG, "Resolver: Can't read result: EOF");
472 return (size_t)bytes_read;