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
15 #define RESOLVER_TIMEOUT (Conf_PongTimeout*3)/4
25 #include <sys/socket.h>
26 #include <netinet/in.h>
41 #include "ng_ipaddr.h"
48 static void Do_ResolveAddr PARAMS(( const ng_ipaddr_t *Addr, int Sock, int w_fd ));
49 static void Do_ResolveName PARAMS(( const char *Host, int w_fd ));
52 extern bool Conf_ConnectIPv4;
53 extern bool Conf_ConnectIPv6;
58 * Resolve IP (asynchronous!).
61 Resolve_Addr(PROC_STAT * s, const ng_ipaddr_t *Addr, int identsock,
62 void (*cbfunc) (int, short))
69 pid = Proc_Fork(s, pipefd, cbfunc, RESOLVER_TIMEOUT);
71 LogDebug("Resolver for %s created (PID %d).", ng_ipaddr_tostr(Addr), pid);
73 } else if( pid == 0 ) {
75 Log_Init_Subprocess("Resolver");
76 Do_ResolveAddr( Addr, identsock, pipefd[1]);
77 Log_Exit_Subprocess("Resolver");
85 * Resolve hostname (asynchronous!).
88 Resolve_Name( PROC_STAT *s, const char *Host, void (*cbfunc)(int, short))
95 pid = Proc_Fork(s, pipefd, cbfunc, RESOLVER_TIMEOUT);
99 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
102 } else if( pid == 0 ) {
104 Log_Init_Subprocess("Resolver");
105 Do_ResolveName(Host, pipefd[1]);
106 Log_Exit_Subprocess("Resolver");
113 #if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETNAMEINFO)
114 #if !defined(WANT_IPV6) && defined(h_errno)
116 Get_Error( int H_Error )
118 /* Get error message for H_Error */
121 return "host not found";
123 return "name valid but no IP address defined";
125 return "name server error";
127 return "name server temporary not available";
129 return "unknown error";
135 /* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */
137 Do_IdentQuery(int identsock, array *resolved_addr)
146 Log_Subprocess(LOG_DEBUG, "Doing IDENT lookup on socket %d ...",
149 res = ident_id( identsock, 10 );
151 Log_Subprocess(LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"",
152 identsock, res ? res : "(NULL)");
154 if (!res) /* no result */
156 if (!array_cats(resolved_addr, res))
157 Log_Subprocess(LOG_WARNING,
158 "Resolver: Cannot copy IDENT result: %s!",
164 (void) resolved_addr;
170 * perform reverse DNS lookup and put result string into resbuf.
171 * If no hostname could be obtained, this function stores the string representation of
172 * the IP address in resbuf and returns false.
173 * @param IpAddr ip address to resolve
174 * @param resbuf result buffer to store DNS name/string representation of ip address
175 * @param reslen size of result buffer (must be >= NGT_INET_ADDRSTRLEN)
176 * @return true if reverse lookup successful, false otherwise
179 ReverseLookup(const ng_ipaddr_t *IpAddr, char *resbuf, size_t reslen)
181 char tmp_ip_str[NG_INET_ADDRSTRLEN];
183 #ifdef HAVE_GETNAMEINFO
184 static const char funcname[]="getnameinfo";
189 res = getnameinfo((struct sockaddr *) IpAddr, ng_ipaddr_salen(IpAddr),
190 resbuf, (socklen_t)reslen, NULL, 0, NI_NAMEREQD);
194 if (res == EAI_SYSTEM)
195 errmsg = strerror(errno);
197 errmsg = gai_strerror(res);
199 const struct sockaddr_in *Addr = (const struct sockaddr_in *) IpAddr;
201 static const char funcname[]="gethostbyaddr";
203 h = gethostbyaddr((char *)&Addr->sin_addr, sizeof(Addr->sin_addr), AF_INET);
205 if (strlcpy(resbuf, h->h_name, reslen) < reslen)
207 errmsg = "hostname too long";
210 errmsg = Get_Error(h_errno);
212 errmsg = "unknown error";
213 # endif /* h_errno */
215 #endif /* HAVE_GETNAMEINFO */
218 assert(reslen >= NG_INET_ADDRSTRLEN);
219 ng_ipaddr_tostr_r(IpAddr, tmp_ip_str);
221 Log_Subprocess(LOG_WARNING, "%s: Can't resolve address \"%s\": %s",
222 funcname, tmp_ip_str, errmsg);
223 strlcpy(resbuf, tmp_ip_str, reslen);
229 * perform DNS lookup of given host name and fill IpAddr with a list of
230 * ip addresses associated with that name.
231 * ip addresses found are stored in the "array *IpAddr" argument (type ng_ipaddr_t)
232 * @param hostname The domain name to look up.
233 * @param IpAddr pointer to empty and initialized array to store results
234 * @return true if lookup successful, false if domain name not found
237 ForwardLookup(const char *hostname, array *IpAddr)
241 #ifdef HAVE_GETADDRINFO
243 struct addrinfo *a, *ai_results;
244 static struct addrinfo hints;
247 hints.ai_family = AF_INET;
249 #ifdef AI_ADDRCONFIG /* glibc has this, but not e.g. netbsd 4.0 */
250 hints.ai_flags = AI_ADDRCONFIG;
252 hints.ai_socktype = SOCK_STREAM;
253 hints.ai_protocol = IPPROTO_TCP;
256 assert(Conf_ConnectIPv6 || Conf_ConnectIPv4);
258 if (!Conf_ConnectIPv6)
259 hints.ai_family = AF_INET;
260 if (!Conf_ConnectIPv4)
261 hints.ai_family = AF_INET6;
263 memset(&addr, 0, sizeof(addr));
265 res = getaddrinfo(hostname, NULL, &hints, &ai_results);
269 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, strerror(errno));
272 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, gai_strerror(res));
276 for (a = ai_results; a != NULL; a = a->ai_next) {
277 assert(a->ai_addrlen <= sizeof(addr));
279 if (a->ai_addrlen > sizeof(addr))
282 memcpy(&addr, a->ai_addr, a->ai_addrlen);
284 if (!array_catb(IpAddr, (char *)&addr, sizeof(addr)))
288 freeaddrinfo(ai_results);
291 struct hostent *h = gethostbyname(hostname);
295 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s",
296 hostname, Get_Error(h_errno));
298 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\"", hostname);
302 memset(&addr, 0, sizeof(addr));
304 addr.sin4.sin_family = AF_INET;
305 memcpy(&addr.sin4.sin_addr, h->h_addr, sizeof(struct in_addr));
307 return array_copyb(IpAddr, (char *)&addr, sizeof(addr));
308 #endif /* HAVE_GETADDRINFO */
313 Addr_in_list(const array *resolved_addr, const ng_ipaddr_t *Addr)
315 char tmp_ip_str[NG_INET_ADDRSTRLEN];
316 const ng_ipaddr_t *tmpAddrs = array_start(resolved_addr);
317 size_t len = array_length(resolved_addr, sizeof(*tmpAddrs));
323 if (ng_ipaddr_ipequal(Addr, tmpAddrs))
328 /* failed; print list of addresses */
329 ng_ipaddr_tostr_r(Addr, tmp_ip_str);
330 len = array_length(resolved_addr, sizeof(*tmpAddrs));
331 tmpAddrs = array_start(resolved_addr);
334 Log_Subprocess(LOG_WARNING, "Address mismatch: %s != %s",
335 tmp_ip_str, ng_ipaddr_tostr(tmpAddrs));
345 Log_Forgery_NoIP(const char *ip, const char *host)
347 Log_Subprocess(LOG_WARNING, "Possible forgery: %s resolved to %s "
348 "(which has no ip address)", ip, host);
352 Log_Forgery_WrongIP(const char *ip, const char *host)
354 Log_Subprocess(LOG_WARNING,"Possible forgery: %s resolved to %s "
355 "(which points to different address)", ip, host);
360 ArrayWrite(int fd, const array *a)
362 size_t len = array_bytes(a);
363 const char *data = array_start(a);
367 if( (size_t)write(fd, data, len) != len )
368 Log_Subprocess( LOG_CRIT, "Resolver: Can't write to parent: %s!",
374 Do_ResolveAddr(const ng_ipaddr_t *Addr, int identsock, int w_fd)
376 /* Resolver sub-process: resolve IP address and write result into
378 char hostname[CLIENT_HOST_LEN];
379 char tmp_ip_str[NG_INET_ADDRSTRLEN];
383 array_init(&resolved_addr);
384 ng_ipaddr_tostr_r(Addr, tmp_ip_str);
386 Log_Subprocess(LOG_DEBUG, "Now resolving %s ...", tmp_ip_str);
388 if (!ReverseLookup(Addr, hostname, sizeof(hostname)))
391 if (ForwardLookup(hostname, &resolved_addr)) {
392 if (!Addr_in_list(&resolved_addr, Addr)) {
393 Log_Forgery_WrongIP(tmp_ip_str, hostname);
394 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
397 Log_Forgery_NoIP(tmp_ip_str, hostname);
398 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
401 Log_Subprocess(LOG_DEBUG, "Ok, translated %s to \"%s\".", tmp_ip_str, hostname);
404 len = strlen(hostname);
405 hostname[len] = '\n';
406 if (!array_copyb(&resolved_addr, hostname, ++len)) {
407 Log_Subprocess(LOG_CRIT,
408 "Resolver: Can't copy resolved name: %s!",
410 array_free(&resolved_addr);
414 Do_IdentQuery(identsock, &resolved_addr);
416 ArrayWrite(w_fd, &resolved_addr);
418 array_free(&resolved_addr);
419 } /* Do_ResolveAddr */
423 Do_ResolveName( const char *Host, int w_fd )
425 /* Resolver sub-process: resolve name and write result into pipe
432 Log_Subprocess(LOG_DEBUG, "Now resolving \"%s\" ...", Host);
434 array_init(&IpAddrs);
435 /* Resolve hostname */
436 if (!ForwardLookup(Host, &IpAddrs)) {
441 len = array_length(&IpAddrs, sizeof(*addr));
443 addr = array_start(&IpAddrs);
445 for (; len > 0; --len,addr++) {
446 Log_Subprocess(LOG_DEBUG, "translated \"%s\" to %s.",
447 Host, ng_ipaddr_tostr(addr));
450 /* Write result into pipe to parent */
451 ArrayWrite(w_fd, &IpAddrs);
453 array_free(&IpAddrs);
454 } /* Do_ResolveName */