2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2003 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
17 static char UNUSED id[] = "$Id: resolve.c,v 1.29 2008/02/26 22:04:17 fw Exp $";
25 #include <sys/socket.h>
26 #include <netinet/in.h>
44 static void Do_ResolveAddr PARAMS(( const ng_ipaddr_t *Addr, int Sock, int w_fd ));
45 static void Do_ResolveName PARAMS(( const char *Host, int w_fd ));
46 static bool register_callback PARAMS((RES_STAT *s, void (*cbfunc)(int, short)));
49 extern bool Conf_ConnectIPv4;
50 extern bool Conf_ConnectIPv6;
54 Resolver_fork(int *pipefds)
58 if (pipe(pipefds) != 0) {
59 Log( LOG_ALERT, "Resolver: Can't create output pipe: %s!", strerror( errno ));
66 Log( LOG_CRIT, "Resolver: Can't fork: %s!", strerror( errno ));
82 * Resolve IP (asynchronous!).
85 Resolve_Addr(RES_STAT * s, const ng_ipaddr_t *Addr, int identsock,
86 void (*cbfunc) (int, short))
93 pid = Resolver_fork(pipefd);
95 Log(LOG_DEBUG, "Resolver for %s created (PID %d).", ng_ipaddr_tostr(Addr), pid);
98 s->resolver_fd = pipefd[0];
99 return register_callback(s, cbfunc);
100 } else if( pid == 0 ) {
102 Do_ResolveAddr( Addr, identsock, pipefd[1]);
103 Log_Exit_Resolver( );
111 * Resolve hostname (asynchronous!).
114 Resolve_Name( RES_STAT *s, const char *Host, void (*cbfunc)(int, short))
121 pid = Resolver_fork(pipefd);
125 Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
128 s->resolver_fd = pipefd[0];
129 return register_callback(s, cbfunc);
130 } else if( pid == 0 ) {
132 Do_ResolveName(Host, pipefd[1]);
133 Log_Exit_Resolver( );
141 Resolve_Init(RES_STAT *s)
152 Get_Error( int H_Error )
154 /* Get error message for H_Error */
157 return "host not found";
159 return "name valid but no IP address defined";
161 return "name server error";
163 return "name server temporary not available";
165 return "unknown error";
168 #endif /* WANT_IPV6 */
171 /* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */
173 Do_IdentQuery(int identsock, array *resolved_addr)
178 assert(identsock >= 0);
181 Log_Resolver(LOG_DEBUG, "Doing IDENT lookup on socket %d ...", identsock);
185 res = ident_id( identsock, 10 );
187 Log_Resolver(LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"",
188 identsock, res ? res : "(NULL)" );
190 if (!res) /* no result */
192 if (!array_cats(resolved_addr, res))
193 Log_Resolver(LOG_WARNING, "Resolver: Cannot copy IDENT result: %s!", strerror(errno));
198 (void) resolved_addr;
204 * perform reverse DNS lookup and put result string into resbuf.
205 * If no hostname could be obtained, this function stores the string representation of
206 * the IP address in resbuf and returns false.
207 * @param IpAddr ip address to resolve
208 * @param resbuf result buffer to store DNS name/string representation of ip address
209 * @reslen size of result buffer (must be >= NGT_INET_ADDRSTRLEN)
210 * @return true if reverse lookup successful, false otherwise
213 ReverseLookup(const ng_ipaddr_t *IpAddr, char *resbuf, size_t reslen)
215 char tmp_ip_str[NG_INET_ADDRSTRLEN];
217 #ifdef HAVE_GETNAMEINFO
218 static const char funcname[]="getnameinfo";
223 res = getnameinfo((struct sockaddr *) IpAddr, ng_ipaddr_salen(IpAddr),
224 resbuf, reslen, NULL, 0, NI_NAMEREQD);
228 if (res == EAI_SYSTEM)
229 errmsg = strerror(errno);
231 errmsg = gai_strerror(res);
233 const struct sockaddr_in *Addr = (const struct sockaddr_in *) IpAddr;
235 static const char funcname[]="gethostbyaddr";
237 h = gethostbyaddr((char *)&Addr->sin_addr, sizeof(Addr->sin_addr), AF_INET);
239 if (strlcpy(resbuf, h->h_name, reslen) < reslen)
241 errmsg = "hostname too long";
244 errmsg = Get_Error(h_errno);
246 errmsg = "unknown error";
247 # endif /* h_errno */
249 #endif /* HAVE_GETNAMEINFO */
252 assert(reslen >= NG_INET_ADDRSTRLEN);
253 ng_ipaddr_tostr_r(IpAddr, tmp_ip_str);
255 Log_Resolver(LOG_WARNING, "%s: Can't resolve address \"%s\": %s",
256 funcname, tmp_ip_str, errmsg);
257 strlcpy(resbuf, tmp_ip_str, reslen);
263 * perform DNS lookup of given host name and fill IpAddr with a list of
264 * ip addresses associated with that name.
265 * ip addresses found are stored in the "array *IpAddr" argument (type ng_ipaddr_t)
266 * @param hostname The domain name to look up.
267 * @param IpAddr pointer to empty and initialized array to store results
268 * @return true if lookup successful, false if domain name not found
271 ForwardLookup(const char *hostname, array *IpAddr)
274 #ifdef HAVE_GETADDRINFO
276 struct addrinfo *a, *ai_results;
277 static struct addrinfo hints = {
279 .ai_family = AF_INET,
281 #ifdef AI_ADDRCONFIG /* glibc has this, but not e.g. netbsd 4.0 */
282 .ai_flags = AI_ADDRCONFIG,
284 .ai_socktype = SOCK_STREAM,
285 .ai_protocol = IPPROTO_TCP
288 assert(Conf_ConnectIPv6 || Conf_ConnectIPv4);
290 if (!Conf_ConnectIPv6)
291 hints.ai_family = AF_INET;
292 if (!Conf_ConnectIPv4)
293 hints.ai_family = AF_INET6;
295 res = getaddrinfo(hostname, NULL, &hints, &ai_results);
299 Log_Resolver(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, strerror(errno));
302 Log_Resolver(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, gai_strerror(res));
306 for (a = ai_results; a != NULL; a = a->ai_next) {
307 assert(a->ai_addrlen <= sizeof(addr));
309 if (a->ai_addrlen > sizeof(addr))
312 memcpy(&addr, a->ai_addr, a->ai_addrlen);
314 if (!array_catb(IpAddr, (char *)&addr, sizeof(addr)))
318 freeaddrinfo(ai_results);
321 struct hostent *h = gethostbyname(hostname);
325 Log_Resolver(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, Get_Error(h_errno));
327 Log_Resolver(LOG_WARNING, "Can't resolve \"%s\"", hostname);
331 memset(&addr, 0, sizeof(addr));
333 addr.sin4.sin_family = AF_INET;
334 memcpy(&addr.sin4.sin_addr, h->h_addr, sizeof(struct in_addr));
336 return array_copyb(IpAddr, (char *)&addr, sizeof(addr));
337 #endif /* HAVE_GETADDRINFO */
342 Addr_in_list(const array *resolved_addr, const ng_ipaddr_t *Addr)
344 char tmp_ip_str[NG_INET_ADDRSTRLEN];
345 const ng_ipaddr_t *tmpAddrs = array_start(resolved_addr);
346 size_t len = array_length(resolved_addr, sizeof(*tmpAddrs));
352 if (ng_ipaddr_ipequal(Addr, tmpAddrs))
357 /* failed; print list of addresses */
358 ng_ipaddr_tostr_r(Addr, tmp_ip_str);
359 len = array_length(resolved_addr, sizeof(*tmpAddrs));
360 tmpAddrs = array_start(resolved_addr);
363 Log_Resolver(LOG_WARNING, "Address mismatch: %s != %s",
364 tmp_ip_str, ng_ipaddr_tostr(tmpAddrs));
374 Log_Forgery_NoIP(const char *ip, const char *host)
376 Log_Resolver(LOG_WARNING, "Possible forgery: %s resolved to %s "
377 "(which has no ip address)", ip, host);
381 Log_Forgery_WrongIP(const char *ip, const char *host)
383 Log_Resolver(LOG_WARNING,"Possible forgery: %s resolved to %s "
384 "(which points to different address)", ip, host);
389 ArrayWrite(int fd, const array *a)
391 size_t len = array_bytes(a);
392 const char *data = array_start(a);
396 if( (size_t)write(fd, data, len) != len )
397 Log_Resolver( LOG_CRIT, "Resolver: Can't write to parent: %s!",
403 Do_ResolveAddr(const ng_ipaddr_t *Addr, int identsock, int w_fd)
405 /* Resolver sub-process: resolve IP address and write result into
407 char hostname[CLIENT_HOST_LEN];
408 char tmp_ip_str[NG_INET_ADDRSTRLEN];
412 array_init(&resolved_addr);
413 ng_ipaddr_tostr_r(Addr, tmp_ip_str);
415 Log_Resolver(LOG_DEBUG, "Now resolving %s ...", tmp_ip_str);
417 if (!ReverseLookup(Addr, hostname, sizeof(hostname)))
420 if (ForwardLookup(hostname, &resolved_addr)) {
421 if (!Addr_in_list(&resolved_addr, Addr)) {
422 Log_Forgery_WrongIP(tmp_ip_str, hostname);
423 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
426 Log_Forgery_NoIP(tmp_ip_str, hostname);
427 strlcpy(hostname, tmp_ip_str, sizeof(hostname));
430 Log_Resolver(LOG_DEBUG, "Ok, translated %s to \"%s\".", tmp_ip_str, hostname);
433 len = strlen(hostname);
434 hostname[len] = '\n';
435 if (!array_copyb(&resolved_addr, hostname, ++len)) {
436 Log_Resolver(LOG_CRIT, "Resolver: Can't copy resolved name: %s!", strerror(errno));
437 array_free(&resolved_addr);
441 Do_IdentQuery(identsock, &resolved_addr);
443 ArrayWrite(w_fd, &resolved_addr);
445 array_free(&resolved_addr);
446 } /* Do_ResolveAddr */
450 Do_ResolveName( const char *Host, int w_fd )
452 /* Resolver sub-process: resolve name and write result into pipe
459 Log_Resolver(LOG_DEBUG, "Now resolving \"%s\" ...", Host);
461 array_init(&IpAddrs);
462 /* Resolve hostname */
463 if (!ForwardLookup(Host, &IpAddrs)) {
468 len = array_length(&IpAddrs, sizeof(*addr));
470 addr = array_start(&IpAddrs);
472 for (; len > 0; --len,addr++) {
473 Log_Resolver(LOG_DEBUG, "translated \"%s\" to %s.",
474 Host, ng_ipaddr_tostr(addr));
477 /* Write result into pipe to parent */
478 ArrayWrite(w_fd, &IpAddrs);
480 array_free(&IpAddrs);
481 } /* Do_ResolveName */
485 register_callback( RES_STAT *s, void (*cbfunc)(int, short))
487 assert(cbfunc != NULL);
489 assert(s->resolver_fd >= 0);
491 if (io_setnonblock(s->resolver_fd) &&
492 io_event_create(s->resolver_fd, IO_WANTREAD, cbfunc))
495 Log( LOG_CRIT, "Resolver: Could not register callback function: %s!", strerror(errno));
496 close(s->resolver_fd);
503 Resolve_Shutdown( RES_STAT *s)
508 assert(s->resolver_fd >= 0);
510 if (s->resolver_fd >= 0)
511 ret = io_close(s->resolver_fd);
519 * Read result of resolver sub-process from pipe
522 Resolve_Read( RES_STAT *s, void* readbuf, size_t buflen)
528 /* Read result from pipe */
529 bytes_read = read(s->resolver_fd, readbuf, buflen);
530 if (bytes_read < 0) {
534 Log( LOG_CRIT, "Resolver: Can't read result: %s!", strerror(errno));
538 else if (bytes_read == 0)
539 Log( LOG_DEBUG, "Resolver: Can't read result: EOF");
542 return (size_t)bytes_read;