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 Init_Subprocess PARAMS(( void ));
46 static void Do_ResolveAddr PARAMS(( const ng_ipaddr_t *Addr, int Sock, int w_fd ));
47 static void Do_ResolveName PARAMS(( const char *Host, int w_fd ));
50 extern bool Conf_ConnectIPv4;
51 extern bool Conf_ConnectIPv6;
56 * Resolve IP (asynchronous!).
59 Resolve_Addr(PROC_STAT * s, const ng_ipaddr_t *Addr, int identsock,
60 void (*cbfunc) (int, short))
67 pid = Proc_Fork(s, pipefd, cbfunc);
69 LogDebug("Resolver for %s created (PID %d).", ng_ipaddr_tostr(Addr), pid);
71 } else if( pid == 0 ) {
74 Do_ResolveAddr( Addr, identsock, pipefd[1]);
75 Log_Exit_Subprocess("Resolver");
83 * Resolve hostname (asynchronous!).
86 Resolve_Name( PROC_STAT *s, const char *Host, void (*cbfunc)(int, short))
93 pid = Proc_Fork(s, pipefd, cbfunc);
97 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
100 } else if( pid == 0 ) {
103 Do_ResolveName(Host, pipefd[1]);
104 Log_Exit_Subprocess("Resolver");
112 * Signal handler for the forked resolver subprocess.
115 Signal_Handler(int Signal)
120 Log_Subprocess(LOG_DEBUG, "Resolver: Got TERM signal, exiting.");
128 * Initialize forked resolver subprocess.
131 Init_Subprocess(void)
133 signal(SIGTERM, Signal_Handler);
134 Log_Init_Subprocess("Resolver");
138 #if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETNAMEINFO)
139 #if !defined(WANT_IPV6) && defined(h_errno)
141 Get_Error( int H_Error )
143 /* Get error message for H_Error */
146 return "host not found";
148 return "name valid but no IP address defined";
150 return "name server error";
152 return "name server temporary not available";
154 return "unknown error";
160 /* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */
162 Do_IdentQuery(int identsock, array *resolved_addr)
171 Log_Subprocess(LOG_DEBUG, "Doing IDENT lookup on socket %d ...",
174 res = ident_id( identsock, 10 );
176 Log_Subprocess(LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"",
177 identsock, res ? res : "(NULL)");
179 if (!res) /* no result */
181 if (!array_cats(resolved_addr, res))
182 Log_Subprocess(LOG_WARNING,
183 "Resolver: Cannot copy IDENT result: %s!",
189 (void) resolved_addr;
195 * perform reverse DNS lookup and put result string into resbuf.
196 * If no hostname could be obtained, this function stores the string representation of
197 * the IP address in resbuf and returns false.
198 * @param IpAddr ip address to resolve
199 * @param resbuf result buffer to store DNS name/string representation of ip address
200 * @param reslen size of result buffer (must be >= NGT_INET_ADDRSTRLEN)
201 * @return true if reverse lookup successful, false otherwise
204 ReverseLookup(const ng_ipaddr_t *IpAddr, char *resbuf, size_t reslen)
206 char tmp_ip_str[NG_INET_ADDRSTRLEN];
208 #ifdef HAVE_GETNAMEINFO
209 static const char funcname[]="getnameinfo";
214 res = getnameinfo((struct sockaddr *) IpAddr, ng_ipaddr_salen(IpAddr),
215 resbuf, (socklen_t)reslen, NULL, 0, NI_NAMEREQD);
219 if (res == EAI_SYSTEM)
220 errmsg = strerror(errno);
222 errmsg = gai_strerror(res);
224 const struct sockaddr_in *Addr = (const struct sockaddr_in *) IpAddr;
226 static const char funcname[]="gethostbyaddr";
228 h = gethostbyaddr((char *)&Addr->sin_addr, sizeof(Addr->sin_addr), AF_INET);
230 if (strlcpy(resbuf, h->h_name, reslen) < reslen)
232 errmsg = "hostname too long";
235 errmsg = Get_Error(h_errno);
237 errmsg = "unknown error";
238 # endif /* h_errno */
240 #endif /* HAVE_GETNAMEINFO */
243 assert(reslen >= NG_INET_ADDRSTRLEN);
244 ng_ipaddr_tostr_r(IpAddr, tmp_ip_str);
246 Log_Subprocess(LOG_WARNING, "%s: Can't resolve address \"%s\": %s",
247 funcname, tmp_ip_str, errmsg);
248 strlcpy(resbuf, tmp_ip_str, reslen);
254 * perform DNS lookup of given host name and fill IpAddr with a list of
255 * ip addresses associated with that name.
256 * ip addresses found are stored in the "array *IpAddr" argument (type ng_ipaddr_t)
257 * @param hostname The domain name to look up.
258 * @param IpAddr pointer to empty and initialized array to store results
259 * @return true if lookup successful, false if domain name not found
262 ForwardLookup(const char *hostname, array *IpAddr)
266 #ifdef HAVE_GETADDRINFO
268 struct addrinfo *a, *ai_results;
269 static struct addrinfo hints;
272 hints.ai_family = AF_INET;
274 #ifdef AI_ADDRCONFIG /* glibc has this, but not e.g. netbsd 4.0 */
275 hints.ai_flags = AI_ADDRCONFIG;
277 hints.ai_socktype = SOCK_STREAM;
278 hints.ai_protocol = IPPROTO_TCP;
281 assert(Conf_ConnectIPv6 || Conf_ConnectIPv4);
283 if (!Conf_ConnectIPv6)
284 hints.ai_family = AF_INET;
285 if (!Conf_ConnectIPv4)
286 hints.ai_family = AF_INET6;
288 memset(&addr, 0, sizeof(addr));
290 res = getaddrinfo(hostname, NULL, &hints, &ai_results);
294 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, strerror(errno));
297 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, gai_strerror(res));
301 for (a = ai_results; a != NULL; a = a->ai_next) {
302 assert(a->ai_addrlen <= sizeof(addr));
304 if (a->ai_addrlen > sizeof(addr))
307 memcpy(&addr, a->ai_addr, a->ai_addrlen);
309 if (!array_catb(IpAddr, (char *)&addr, sizeof(addr)))
313 freeaddrinfo(ai_results);
316 struct hostent *h = gethostbyname(hostname);
320 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s",
321 hostname, Get_Error(h_errno));
323 Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\"", hostname);
327 memset(&addr, 0, sizeof(addr));
329 addr.sin4.sin_family = AF_INET;
330 memcpy(&addr.sin4.sin_addr, h->h_addr, sizeof(struct in_addr));
332 return array_copyb(IpAddr, (char *)&addr, sizeof(addr));
333 #endif /* HAVE_GETADDRINFO */
338 Addr_in_list(const array *resolved_addr, const ng_ipaddr_t *Addr)
340 char tmp_ip_str[NG_INET_ADDRSTRLEN];
341 const ng_ipaddr_t *tmpAddrs = array_start(resolved_addr);
342 size_t len = array_length(resolved_addr, sizeof(*tmpAddrs));
348 if (ng_ipaddr_ipequal(Addr, tmpAddrs))
353 /* failed; print list of addresses */
354 ng_ipaddr_tostr_r(Addr, tmp_ip_str);
355 len = array_length(resolved_addr, sizeof(*tmpAddrs));
356 tmpAddrs = array_start(resolved_addr);
359 Log_Subprocess(LOG_WARNING, "Address mismatch: %s != %s",
360 tmp_ip_str, ng_ipaddr_tostr(tmpAddrs));
370 Log_Forgery_NoIP(const char *ip, const char *host)
372 Log_Subprocess(LOG_WARNING, "Possible forgery: %s resolved to %s "
373 "(which has no ip address)", ip, host);
377 Log_Forgery_WrongIP(const char *ip, const char *host)
379 Log_Subprocess(LOG_WARNING,"Possible forgery: %s resolved to %s "
380 "(which points to different address)", ip, host);
385 ArrayWrite(int fd, const array *a)
387 size_t len = array_bytes(a);
388 const char *data = array_start(a);
392 if( (size_t)write(fd, data, len) != len )
393 Log_Subprocess( LOG_CRIT, "Resolver: Can't write to parent: %s!",
399 Do_ResolveAddr(const ng_ipaddr_t *Addr, int identsock, int w_fd)
401 /* Resolver sub-process: resolve IP address and write result into
403 char hostname[CLIENT_HOST_LEN];
404 char tmp_ip_str[NG_INET_ADDRSTRLEN];
408 array_init(&resolved_addr);
409 ng_ipaddr_tostr_r(Addr, tmp_ip_str);
411 Log_Subprocess(LOG_DEBUG, "Now resolving %s ...", tmp_ip_str);
413 if (!ReverseLookup(Addr, hostname, sizeof(hostname)))
416 if (ForwardLookup(hostname, &resolved_addr)) {
417 if (!Addr_in_list(&resolved_addr, Addr)) {
418 Log_Forgery_WrongIP(tmp_ip_str, hostname);
419 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
422 Log_Forgery_NoIP(tmp_ip_str, hostname);
423 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
426 Log_Subprocess(LOG_DEBUG, "Ok, translated %s to \"%s\".", tmp_ip_str, hostname);
429 len = strlen(hostname);
430 hostname[len] = '\n';
431 if (!array_copyb(&resolved_addr, hostname, ++len)) {
432 Log_Subprocess(LOG_CRIT,
433 "Resolver: Can't copy resolved name: %s!",
435 array_free(&resolved_addr);
439 Do_IdentQuery(identsock, &resolved_addr);
441 ArrayWrite(w_fd, &resolved_addr);
443 array_free(&resolved_addr);
444 } /* Do_ResolveAddr */
448 Do_ResolveName( const char *Host, int w_fd )
450 /* Resolver sub-process: resolve name and write result into pipe
457 Log_Subprocess(LOG_DEBUG, "Now resolving \"%s\" ...", Host);
459 array_init(&IpAddrs);
460 /* Resolve hostname */
461 if (!ForwardLookup(Host, &IpAddrs)) {
466 len = array_length(&IpAddrs, sizeof(*addr));
468 addr = array_start(&IpAddrs);
470 for (; len > 0; --len,addr++) {
471 Log_Subprocess(LOG_DEBUG, "translated \"%s\" to %s.",
472 Host, ng_ipaddr_tostr(addr));
475 /* Write result into pipe to parent */
476 ArrayWrite(w_fd, &IpAddrs);
478 array_free(&IpAddrs);
479 } /* Do_ResolveName */
483 * Read result of resolver sub-process from pipe
486 Resolve_Read( PROC_STAT *s, void* readbuf, size_t buflen)
492 /* Read result from pipe */
493 bytes_read = read(Proc_GetPipeFd(s), readbuf, buflen);
494 if (bytes_read < 0) {
498 Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror(errno));
502 else if (bytes_read == 0)
503 Log( LOG_DEBUG, "Resolver: Can't read result: EOF");
506 return (size_t)bytes_read;