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.
12 #define RESOLVER_TIMEOUT (Conf_PongTimeout*3)/4
18 * Asynchronous resolver
27 #include <sys/socket.h>
28 #include <netinet/in.h>
43 #include "ng_ipaddr.h"
50 static void Do_ResolveAddr PARAMS(( const ng_ipaddr_t *Addr, int Sock, int w_fd ));
51 static void Do_ResolveName PARAMS(( const char *Host, int w_fd ));
54 extern bool Conf_ConnectIPv4;
55 extern bool Conf_ConnectIPv6;
60 * Resolve IP (asynchronous!).
63 Resolve_Addr(PROC_STAT * s, const ng_ipaddr_t *Addr, int identsock,
64 void (*cbfunc) (int, short))
71 pid = Proc_Fork(s, pipefd, cbfunc, RESOLVER_TIMEOUT);
73 LogDebug("Resolver for %s created (PID %d).", ng_ipaddr_tostr(Addr), pid);
75 } else if( pid == 0 ) {
77 Log_Init_Subprocess("Resolver");
78 Do_ResolveAddr( Addr, identsock, pipefd[1]);
79 Log_Exit_Subprocess("Resolver");
87 * Resolve hostname (asynchronous!).
90 Resolve_Name( PROC_STAT *s, const char *Host, void (*cbfunc)(int, short))
97 pid = Proc_Fork(s, pipefd, cbfunc, RESOLVER_TIMEOUT);
101 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
104 } else if( pid == 0 ) {
106 Log_Init_Subprocess("Resolver");
107 Do_ResolveName(Host, pipefd[1]);
108 Log_Exit_Subprocess("Resolver");
115 #if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETNAMEINFO)
116 #if !defined(WANT_IPV6) && defined(h_errno)
118 Get_Error( int H_Error )
120 /* Get error message for H_Error */
123 return "host not found";
125 return "name valid but no IP address defined";
127 return "name server error";
129 return "name server temporary not available";
131 return "unknown error";
137 /* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */
139 Do_IdentQuery(int identsock, array *resolved_addr)
148 Log_Subprocess(LOG_DEBUG, "Doing IDENT lookup on socket %d ...",
151 res = ident_id( identsock, 10 );
153 Log_Subprocess(LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"",
154 identsock, res ? res : "(NULL)");
156 if (!res) /* no result */
158 if (!array_cats(resolved_addr, res))
159 Log_Subprocess(LOG_WARNING,
160 "Resolver: Cannot copy IDENT result: %s!",
166 (void) resolved_addr;
172 * perform reverse DNS lookup and put result string into resbuf.
173 * If no hostname could be obtained, this function stores the string representation of
174 * the IP address in resbuf and returns false.
175 * @param IpAddr ip address to resolve
176 * @param resbuf result buffer to store DNS name/string representation of ip address
177 * @param reslen size of result buffer (must be >= NGT_INET_ADDRSTRLEN)
178 * @return true if reverse lookup successful, false otherwise
181 ReverseLookup(const ng_ipaddr_t *IpAddr, char *resbuf, size_t reslen)
183 char tmp_ip_str[NG_INET_ADDRSTRLEN];
185 #ifdef HAVE_GETNAMEINFO
186 static const char funcname[]="getnameinfo";
191 res = getnameinfo((struct sockaddr *) IpAddr, ng_ipaddr_salen(IpAddr),
192 resbuf, (socklen_t)reslen, NULL, 0, NI_NAMEREQD);
196 if (res == EAI_SYSTEM)
197 errmsg = strerror(errno);
199 errmsg = gai_strerror(res);
201 const struct sockaddr_in *Addr = (const struct sockaddr_in *) IpAddr;
203 static const char funcname[]="gethostbyaddr";
205 h = gethostbyaddr((char *)&Addr->sin_addr, sizeof(Addr->sin_addr), AF_INET);
207 if (strlcpy(resbuf, h->h_name, reslen) < reslen)
209 errmsg = "hostname too long";
212 errmsg = Get_Error(h_errno);
214 errmsg = "unknown error";
215 # endif /* h_errno */
217 #endif /* HAVE_GETNAMEINFO */
220 assert(reslen >= NG_INET_ADDRSTRLEN);
221 ng_ipaddr_tostr_r(IpAddr, tmp_ip_str);
223 Log_Subprocess(LOG_WARNING, "%s: Can't resolve address \"%s\": %s",
224 funcname, tmp_ip_str, errmsg);
225 strlcpy(resbuf, tmp_ip_str, reslen);
231 * perform DNS lookup of given host name and fill IpAddr with a list of
232 * ip addresses associated with that name.
233 * ip addresses found are stored in the "array *IpAddr" argument (type ng_ipaddr_t)
234 * @param hostname The domain name to look up.
235 * @param IpAddr pointer to empty and initialized array to store results
236 * @return true if lookup successful, false if domain name not found
239 ForwardLookup(const char *hostname, array *IpAddr)
243 #ifdef HAVE_GETADDRINFO
245 struct addrinfo *a, *ai_results;
246 static struct addrinfo hints;
249 hints.ai_family = AF_INET;
251 #ifdef AI_ADDRCONFIG /* glibc has this, but not e.g. netbsd 4.0 */
252 hints.ai_flags = AI_ADDRCONFIG;
254 hints.ai_socktype = SOCK_STREAM;
255 hints.ai_protocol = IPPROTO_TCP;
258 assert(Conf_ConnectIPv6 || Conf_ConnectIPv4);
260 if (!Conf_ConnectIPv6)
261 hints.ai_family = AF_INET;
262 if (!Conf_ConnectIPv4)
263 hints.ai_family = AF_INET6;
265 memset(&addr, 0, sizeof(addr));
267 res = getaddrinfo(hostname, NULL, &hints, &ai_results);
271 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, strerror(errno));
274 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, gai_strerror(res));
278 for (a = ai_results; a != NULL; a = a->ai_next) {
279 assert(a->ai_addrlen <= sizeof(addr));
281 if (a->ai_addrlen > sizeof(addr))
284 memcpy(&addr, a->ai_addr, a->ai_addrlen);
286 if (!array_catb(IpAddr, (char *)&addr, sizeof(addr)))
290 freeaddrinfo(ai_results);
293 struct hostent *h = gethostbyname(hostname);
297 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s",
298 hostname, Get_Error(h_errno));
300 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\"", hostname);
304 memset(&addr, 0, sizeof(addr));
306 addr.sin4.sin_family = AF_INET;
307 memcpy(&addr.sin4.sin_addr, h->h_addr, sizeof(struct in_addr));
309 return array_copyb(IpAddr, (char *)&addr, sizeof(addr));
310 #endif /* HAVE_GETADDRINFO */
315 Addr_in_list(const array *resolved_addr, const ng_ipaddr_t *Addr)
317 char tmp_ip_str[NG_INET_ADDRSTRLEN];
318 const ng_ipaddr_t *tmpAddrs = array_start(resolved_addr);
319 size_t len = array_length(resolved_addr, sizeof(*tmpAddrs));
325 if (ng_ipaddr_ipequal(Addr, tmpAddrs))
330 /* failed; print list of addresses */
331 ng_ipaddr_tostr_r(Addr, tmp_ip_str);
332 len = array_length(resolved_addr, sizeof(*tmpAddrs));
333 tmpAddrs = array_start(resolved_addr);
336 Log_Subprocess(LOG_WARNING, "Address mismatch: %s != %s",
337 tmp_ip_str, ng_ipaddr_tostr(tmpAddrs));
347 Log_Forgery_NoIP(const char *ip, const char *host)
349 Log_Subprocess(LOG_WARNING,
350 "Possible forgery: %s resolved to %s (which has no ip address)", ip, host);
354 Log_Forgery_WrongIP(const char *ip, const char *host)
356 Log_Subprocess(LOG_WARNING,
357 "Possible forgery: %s resolved to %s (which points to different address)", ip, host);
362 ArrayWrite(int fd, const array *a)
364 size_t len = array_bytes(a);
365 const char *data = array_start(a);
369 if( (size_t)write(fd, data, len) != len )
370 Log_Subprocess( LOG_CRIT, "Resolver: Can't write to parent: %s!",
376 Do_ResolveAddr(const ng_ipaddr_t *Addr, int identsock, int w_fd)
378 /* Resolver sub-process: resolve IP address and write result into
380 char hostname[CLIENT_HOST_LEN];
381 char tmp_ip_str[NG_INET_ADDRSTRLEN];
385 array_init(&resolved_addr);
386 ng_ipaddr_tostr_r(Addr, tmp_ip_str);
388 Log_Subprocess(LOG_DEBUG, "Now resolving %s ...", tmp_ip_str);
390 if (!ReverseLookup(Addr, hostname, sizeof(hostname)))
393 if (ForwardLookup(hostname, &resolved_addr)) {
394 if (!Addr_in_list(&resolved_addr, Addr)) {
395 Log_Forgery_WrongIP(tmp_ip_str, hostname);
396 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
399 Log_Forgery_NoIP(tmp_ip_str, hostname);
400 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
403 Log_Subprocess(LOG_DEBUG, "Ok, translated %s to \"%s\".", tmp_ip_str, hostname);
406 len = strlen(hostname);
407 hostname[len] = '\n';
408 if (!array_copyb(&resolved_addr, hostname, ++len)) {
409 Log_Subprocess(LOG_CRIT,
410 "Resolver: Can't copy resolved name: %s!",
412 array_free(&resolved_addr);
416 Do_IdentQuery(identsock, &resolved_addr);
418 ArrayWrite(w_fd, &resolved_addr);
420 array_free(&resolved_addr);
421 } /* Do_ResolveAddr */
425 Do_ResolveName( const char *Host, int w_fd )
427 /* Resolver sub-process: resolve name and write result into pipe
434 Log_Subprocess(LOG_DEBUG, "Now resolving \"%s\" ...", Host);
436 array_init(&IpAddrs);
437 /* Resolve hostname */
438 if (!ForwardLookup(Host, &IpAddrs)) {
443 len = array_length(&IpAddrs, sizeof(*addr));
445 addr = array_start(&IpAddrs);
447 for (; len > 0; --len,addr++) {
448 Log_Subprocess(LOG_DEBUG, "translated \"%s\" to %s.",
449 Host, ng_ipaddr_tostr(addr));
452 /* Write result into pipe to parent */
453 ArrayWrite(w_fd, &IpAddrs);
455 array_free(&IpAddrs);
456 } /* Do_ResolveName */