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.
13 #define RESOLVER_TIMEOUT (Conf_PongTimeout*3)/4
19 * Asynchronous resolver
28 #include <sys/socket.h>
29 #include <netinet/in.h>
44 #include "ng_ipaddr.h"
51 static void Do_ResolveAddr PARAMS(( const ng_ipaddr_t *Addr, int Sock, int w_fd ));
52 static void Do_ResolveName PARAMS(( const char *Host, int w_fd ));
55 extern bool Conf_ConnectIPv4;
56 extern bool Conf_ConnectIPv6;
61 * Resolve IP (asynchronous!).
64 Resolve_Addr(PROC_STAT * s, const ng_ipaddr_t *Addr, int identsock,
65 void (*cbfunc) (int, short))
72 pid = Proc_Fork(s, pipefd, cbfunc, RESOLVER_TIMEOUT);
74 LogDebug("Resolver for %s created (PID %d).", ng_ipaddr_tostr(Addr), pid);
76 } else if( pid == 0 ) {
78 Log_Init_Subprocess("Resolver");
79 Do_ResolveAddr( Addr, identsock, pipefd[1]);
80 Log_Exit_Subprocess("Resolver");
88 * Resolve hostname (asynchronous!).
91 Resolve_Name( PROC_STAT *s, const char *Host, void (*cbfunc)(int, short))
98 pid = Proc_Fork(s, pipefd, cbfunc, RESOLVER_TIMEOUT);
102 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
105 } else if( pid == 0 ) {
107 Log_Init_Subprocess("Resolver");
108 Do_ResolveName(Host, pipefd[1]);
109 Log_Exit_Subprocess("Resolver");
116 #if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETNAMEINFO)
117 #if !defined(WANT_IPV6) && defined(h_errno)
119 Get_Error( int H_Error )
121 /* Get error message for H_Error */
124 return "host not found";
126 return "name valid but no IP address defined";
128 return "name server error";
130 return "name server temporary not available";
132 return "unknown error";
138 /* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */
140 Do_IdentQuery(int identsock, array *resolved_addr)
149 Log_Subprocess(LOG_DEBUG, "Doing IDENT lookup on socket %d ...",
152 res = ident_id( identsock, 10 );
154 Log_Subprocess(LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"",
155 identsock, res ? res : "(NULL)");
157 if (!res) /* no result */
159 if (!array_cats(resolved_addr, res))
160 Log_Subprocess(LOG_WARNING,
161 "Resolver: Cannot copy IDENT result: %s!",
167 (void) resolved_addr;
173 * perform reverse DNS lookup and put result string into resbuf.
174 * If no hostname could be obtained, this function stores the string representation of
175 * the IP address in resbuf and returns false.
176 * @param IpAddr ip address to resolve
177 * @param resbuf result buffer to store DNS name/string representation of ip address
178 * @param reslen size of result buffer (must be >= NGT_INET_ADDRSTRLEN)
179 * @return true if reverse lookup successful, false otherwise
182 ReverseLookup(const ng_ipaddr_t *IpAddr, char *resbuf, size_t reslen)
184 char tmp_ip_str[NG_INET_ADDRSTRLEN];
186 #ifdef HAVE_GETNAMEINFO
187 static const char funcname[]="getnameinfo";
192 res = getnameinfo((struct sockaddr *) IpAddr, ng_ipaddr_salen(IpAddr),
193 resbuf, (socklen_t)reslen, NULL, 0, NI_NAMEREQD);
197 if (res == EAI_SYSTEM)
198 errmsg = strerror(errno);
200 errmsg = gai_strerror(res);
202 const struct sockaddr_in *Addr = (const struct sockaddr_in *) IpAddr;
204 static const char funcname[]="gethostbyaddr";
206 h = gethostbyaddr((char *)&Addr->sin_addr, sizeof(Addr->sin_addr), AF_INET);
208 if (strlcpy(resbuf, h->h_name, reslen) < reslen)
210 errmsg = "hostname too long";
213 errmsg = Get_Error(h_errno);
215 errmsg = "unknown error";
216 # endif /* h_errno */
218 #endif /* HAVE_GETNAMEINFO */
221 assert(reslen >= NG_INET_ADDRSTRLEN);
222 ng_ipaddr_tostr_r(IpAddr, tmp_ip_str);
224 Log_Subprocess(LOG_WARNING, "%s: Can't resolve address \"%s\": %s",
225 funcname, tmp_ip_str, errmsg);
226 strlcpy(resbuf, tmp_ip_str, reslen);
232 * perform DNS lookup of given host name and fill IpAddr with a list of
233 * ip addresses associated with that name.
234 * ip addresses found are stored in the "array *IpAddr" argument (type ng_ipaddr_t)
235 * @param hostname The domain name to look up.
236 * @param IpAddr pointer to empty and initialized array to store results
237 * @return true if lookup successful, false if domain name not found
240 ForwardLookup(const char *hostname, array *IpAddr)
244 #ifdef HAVE_GETADDRINFO
246 struct addrinfo *a, *ai_results;
247 static struct addrinfo hints;
250 hints.ai_family = AF_INET;
252 #ifdef AI_ADDRCONFIG /* glibc has this, but not e.g. netbsd 4.0 */
253 hints.ai_flags = AI_ADDRCONFIG;
255 hints.ai_socktype = SOCK_STREAM;
256 hints.ai_protocol = IPPROTO_TCP;
259 assert(Conf_ConnectIPv6 || Conf_ConnectIPv4);
261 if (!Conf_ConnectIPv6)
262 hints.ai_family = AF_INET;
263 if (!Conf_ConnectIPv4)
264 hints.ai_family = AF_INET6;
266 memset(&addr, 0, sizeof(addr));
268 res = getaddrinfo(hostname, NULL, &hints, &ai_results);
272 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, strerror(errno));
275 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, gai_strerror(res));
279 for (a = ai_results; a != NULL; a = a->ai_next) {
280 assert(a->ai_addrlen <= sizeof(addr));
282 if (a->ai_addrlen > sizeof(addr))
285 memcpy(&addr, a->ai_addr, a->ai_addrlen);
287 if (!array_catb(IpAddr, (char *)&addr, sizeof(addr)))
291 freeaddrinfo(ai_results);
294 struct hostent *h = gethostbyname(hostname);
298 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s",
299 hostname, Get_Error(h_errno));
301 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\"", hostname);
305 memset(&addr, 0, sizeof(addr));
307 addr.sin4.sin_family = AF_INET;
308 memcpy(&addr.sin4.sin_addr, h->h_addr, sizeof(struct in_addr));
310 return array_copyb(IpAddr, (char *)&addr, sizeof(addr));
311 #endif /* HAVE_GETADDRINFO */
316 Addr_in_list(const array *resolved_addr, const ng_ipaddr_t *Addr)
318 char tmp_ip_str[NG_INET_ADDRSTRLEN];
319 const ng_ipaddr_t *tmpAddrs = array_start(resolved_addr);
320 size_t len = array_length(resolved_addr, sizeof(*tmpAddrs));
326 if (ng_ipaddr_ipequal(Addr, tmpAddrs))
331 /* failed; print list of addresses */
332 ng_ipaddr_tostr_r(Addr, tmp_ip_str);
333 len = array_length(resolved_addr, sizeof(*tmpAddrs));
334 tmpAddrs = array_start(resolved_addr);
337 Log_Subprocess(LOG_WARNING, "Address mismatch: %s != %s",
338 tmp_ip_str, ng_ipaddr_tostr(tmpAddrs));
348 Log_Forgery_NoIP(const char *ip, const char *host)
350 Log_Subprocess(LOG_WARNING,
351 "Possible forgery: %s resolved to %s (which has no ip address)", ip, host);
355 Log_Forgery_WrongIP(const char *ip, const char *host)
357 Log_Subprocess(LOG_WARNING,
358 "Possible forgery: %s resolved to %s (which points to different address)", ip, host);
363 ArrayWrite(int fd, const array *a)
365 size_t len = array_bytes(a);
366 const char *data = array_start(a);
370 if( (size_t)write(fd, data, len) != len )
371 Log_Subprocess( LOG_CRIT, "Resolver: Can't write to parent: %s!",
377 Do_ResolveAddr(const ng_ipaddr_t *Addr, int identsock, int w_fd)
379 /* Resolver sub-process: resolve IP address and write result into
381 char hostname[CLIENT_HOST_LEN];
382 char tmp_ip_str[NG_INET_ADDRSTRLEN];
386 array_init(&resolved_addr);
387 ng_ipaddr_tostr_r(Addr, tmp_ip_str);
389 Log_Subprocess(LOG_DEBUG, "Now resolving %s ...", tmp_ip_str);
391 if (!ReverseLookup(Addr, hostname, sizeof(hostname)))
394 if (ForwardLookup(hostname, &resolved_addr)) {
395 if (!Addr_in_list(&resolved_addr, Addr)) {
396 Log_Forgery_WrongIP(tmp_ip_str, hostname);
397 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
400 Log_Forgery_NoIP(tmp_ip_str, hostname);
401 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
404 Log_Subprocess(LOG_DEBUG, "Ok, translated %s to \"%s\".", tmp_ip_str, hostname);
407 len = strlen(hostname);
408 hostname[len] = '\n';
409 if (!array_copyb(&resolved_addr, hostname, ++len)) {
410 Log_Subprocess(LOG_CRIT,
411 "Resolver: Can't copy resolved name: %s!",
413 array_free(&resolved_addr);
417 Do_IdentQuery(identsock, &resolved_addr);
419 ArrayWrite(w_fd, &resolved_addr);
421 array_free(&resolved_addr);
422 } /* Do_ResolveAddr */
426 Do_ResolveName( const char *Host, int w_fd )
428 /* Resolver sub-process: resolve name and write result into pipe
435 Log_Subprocess(LOG_DEBUG, "Now resolving \"%s\" ...", Host);
437 array_init(&IpAddrs);
438 /* Resolve hostname */
439 if (!ForwardLookup(Host, &IpAddrs)) {
444 len = array_length(&IpAddrs, sizeof(*addr));
446 addr = array_start(&IpAddrs);
448 for (; len > 0; --len,addr++) {
449 Log_Subprocess(LOG_DEBUG, "translated \"%s\" to %s.",
450 Host, ng_ipaddr_tostr(addr));
453 /* Write result into pipe to parent */
454 ArrayWrite(w_fd, &IpAddrs);
456 array_free(&IpAddrs);
457 } /* Do_ResolveName */