2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2011 Alexander Barton (alex@barton.de) and Contributors.
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.
12 #define RESOLVER_TIMEOUT (Conf_PongTimeout*3)/4
18 * Asynchronous resolver
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
41 #include "ng_ipaddr.h"
43 static void Do_ResolveAddr PARAMS(( const ng_ipaddr_t *Addr, int Sock, int w_fd ));
44 static void Do_ResolveName PARAMS(( const char *Host, int w_fd ));
47 extern bool Conf_ConnectIPv4;
48 extern bool Conf_ConnectIPv6;
53 * Resolve IP (asynchronous!).
56 Resolve_Addr(PROC_STAT * s, const ng_ipaddr_t *Addr, int identsock,
57 void (*cbfunc) (int, short))
64 pid = Proc_Fork(s, pipefd, cbfunc, RESOLVER_TIMEOUT);
66 LogDebug("Resolver for %s created (PID %d).", ng_ipaddr_tostr(Addr), pid);
68 } else if( pid == 0 ) {
70 Log_Init_Subprocess("Resolver");
71 Conn_CloseAllSockets(identsock);
72 Do_ResolveAddr(Addr, identsock, pipefd[1]);
73 Log_Exit_Subprocess("Resolver");
81 * Resolve hostname (asynchronous!).
84 Resolve_Name( PROC_STAT *s, const char *Host, void (*cbfunc)(int, short))
91 pid = Proc_Fork(s, pipefd, cbfunc, RESOLVER_TIMEOUT);
95 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
98 } else if( pid == 0 ) {
100 Log_Init_Subprocess("Resolver");
101 Conn_CloseAllSockets(NONE);
102 Do_ResolveName(Host, pipefd[1]);
103 Log_Exit_Subprocess("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_Subprocess(LOG_DEBUG, "Doing IDENT lookup on socket %d ...",
146 res = ident_id( identsock, 10 );
148 Log_Subprocess(LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"",
149 identsock, res ? res : "(NULL)");
151 if (!res) /* no result */
153 if (!array_cats(resolved_addr, res))
154 Log_Subprocess(LOG_WARNING,
155 "Resolver: Cannot copy IDENT result: %s!",
161 (void) resolved_addr;
167 * perform reverse DNS lookup and put result string into resbuf.
168 * If no hostname could be obtained, this function stores the string representation of
169 * the IP address in resbuf and returns false.
170 * @param IpAddr ip address to resolve
171 * @param resbuf result buffer to store DNS name/string representation of ip address
172 * @param reslen size of result buffer (must be >= NGT_INET_ADDRSTRLEN)
173 * @return true if reverse lookup successful, false otherwise
176 ReverseLookup(const ng_ipaddr_t *IpAddr, char *resbuf, size_t reslen)
178 char tmp_ip_str[NG_INET_ADDRSTRLEN];
180 #ifdef HAVE_GETNAMEINFO
181 static const char funcname[]="getnameinfo";
186 res = getnameinfo((struct sockaddr *) IpAddr, ng_ipaddr_salen(IpAddr),
187 resbuf, (socklen_t)reslen, NULL, 0, NI_NAMEREQD);
191 if (res == EAI_SYSTEM)
192 errmsg = strerror(errno);
194 errmsg = gai_strerror(res);
196 const struct sockaddr_in *Addr = (const struct sockaddr_in *) IpAddr;
198 static const char funcname[]="gethostbyaddr";
200 h = gethostbyaddr((char *)&Addr->sin_addr, sizeof(Addr->sin_addr), AF_INET);
202 if (strlcpy(resbuf, h->h_name, reslen) < reslen)
204 errmsg = "hostname too long";
207 errmsg = Get_Error(h_errno);
209 errmsg = "unknown error";
210 # endif /* h_errno */
212 #endif /* HAVE_GETNAMEINFO */
215 assert(reslen >= NG_INET_ADDRSTRLEN);
216 ng_ipaddr_tostr_r(IpAddr, tmp_ip_str);
218 Log_Subprocess(LOG_WARNING, "%s: Can't resolve address \"%s\": %s",
219 funcname, tmp_ip_str, errmsg);
220 strlcpy(resbuf, tmp_ip_str, reslen);
226 * perform DNS lookup of given host name and fill IpAddr with a list of
227 * ip addresses associated with that name.
228 * ip addresses found are stored in the "array *IpAddr" argument (type ng_ipaddr_t)
229 * @param hostname The domain name to look up.
230 * @param IpAddr pointer to empty and initialized array to store results
231 * @return true if lookup successful, false if domain name not found
234 ForwardLookup(const char *hostname, array *IpAddr, int af)
238 #ifdef HAVE_WORKING_GETADDRINFO
240 struct addrinfo *a, *ai_results;
241 static struct addrinfo hints;
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;
248 hints.ai_family = af;
250 memset(&addr, 0, sizeof(addr));
252 res = getaddrinfo(hostname, NULL, &hints, &ai_results);
256 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, strerror(errno));
259 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, gai_strerror(res));
263 for (a = ai_results; a != NULL; a = a->ai_next) {
264 assert((size_t)a->ai_addrlen <= sizeof(addr));
266 if ((size_t)a->ai_addrlen > sizeof(addr))
269 memcpy(&addr, a->ai_addr, a->ai_addrlen);
271 if (!array_catb(IpAddr, (char *)&addr, sizeof(addr)))
275 freeaddrinfo(ai_results);
278 struct hostent *h = gethostbyname(hostname);
282 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s",
283 hostname, Get_Error(h_errno));
285 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\"", hostname);
289 memset(&addr, 0, sizeof(addr));
291 addr.sin4.sin_family = AF_INET;
292 memcpy(&addr.sin4.sin_addr, h->h_addr, sizeof(struct in_addr));
294 return array_copyb(IpAddr, (char *)&addr, sizeof(addr));
295 #endif /* HAVE_GETADDRINFO */
300 Addr_in_list(const array *resolved_addr, const ng_ipaddr_t *Addr)
302 char tmp_ip_str[NG_INET_ADDRSTRLEN];
303 const ng_ipaddr_t *tmpAddrs = array_start(resolved_addr);
304 size_t len = array_length(resolved_addr, sizeof(*tmpAddrs));
310 if (ng_ipaddr_ipequal(Addr, tmpAddrs))
315 /* failed; print list of addresses */
316 ng_ipaddr_tostr_r(Addr, tmp_ip_str);
317 len = array_length(resolved_addr, sizeof(*tmpAddrs));
318 tmpAddrs = array_start(resolved_addr);
321 Log_Subprocess(LOG_WARNING, "Address mismatch: %s != %s",
322 tmp_ip_str, ng_ipaddr_tostr(tmpAddrs));
332 Log_Forgery_NoIP(const char *ip, const char *host)
334 Log_Subprocess(LOG_WARNING,
335 "Possible forgery: %s resolved to %s (which has no ip address)", ip, host);
339 Log_Forgery_WrongIP(const char *ip, const char *host)
341 Log_Subprocess(LOG_WARNING,
342 "Possible forgery: %s resolved to %s (which points to different address)", ip, host);
347 ArrayWrite(int fd, const array *a)
349 size_t len = array_bytes(a);
350 const char *data = array_start(a);
354 if( (size_t)write(fd, data, len) != len )
355 Log_Subprocess( LOG_CRIT, "Resolver: Can't write to parent: %s!",
361 Do_ResolveAddr(const ng_ipaddr_t *Addr, int identsock, int w_fd)
363 /* Resolver sub-process: resolve IP address and write result into
365 char hostname[CLIENT_HOST_LEN];
366 char tmp_ip_str[NG_INET_ADDRSTRLEN];
370 array_init(&resolved_addr);
371 ng_ipaddr_tostr_r(Addr, tmp_ip_str);
373 Log_Subprocess(LOG_DEBUG, "Now resolving %s ...", tmp_ip_str);
375 if (!ReverseLookup(Addr, hostname, sizeof(hostname)))
378 if (ForwardLookup(hostname, &resolved_addr, ng_ipaddr_af(Addr))) {
379 if (!Addr_in_list(&resolved_addr, Addr)) {
380 Log_Forgery_WrongIP(tmp_ip_str, hostname);
381 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
384 Log_Forgery_NoIP(tmp_ip_str, hostname);
385 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
388 Log_Subprocess(LOG_DEBUG, "Ok, translated %s to \"%s\".", tmp_ip_str, hostname);
391 len = strlen(hostname);
392 hostname[len] = '\n';
393 if (!array_copyb(&resolved_addr, hostname, ++len)) {
394 Log_Subprocess(LOG_CRIT,
395 "Resolver: Can't copy resolved name: %s!",
397 array_free(&resolved_addr);
401 Do_IdentQuery(identsock, &resolved_addr);
403 ArrayWrite(w_fd, &resolved_addr);
405 array_free(&resolved_addr);
406 } /* Do_ResolveAddr */
410 Do_ResolveName( const char *Host, int w_fd )
412 /* Resolver sub-process: resolve name and write result into pipe
420 Log_Subprocess(LOG_DEBUG, "Now resolving \"%s\" ...", Host);
422 array_init(&IpAddrs);
426 assert(Conf_ConnectIPv6 || Conf_ConnectIPv4);
428 if (!Conf_ConnectIPv6)
430 if (!Conf_ConnectIPv4)
435 if (!ForwardLookup(Host, &IpAddrs, af)) {
440 len = array_length(&IpAddrs, sizeof(*addr));
442 addr = array_start(&IpAddrs);
444 for (; len > 0; --len,addr++) {
445 Log_Subprocess(LOG_DEBUG, "translated \"%s\" to %s.",
446 Host, ng_ipaddr_tostr(addr));
449 /* Write result into pipe to parent */
450 ArrayWrite(w_fd, &IpAddrs);
452 array_free(&IpAddrs);
453 } /* Do_ResolveName */