resolve.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2009 by Alexander Barton (alex@barton.de)
  4. *
  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.
  10. */
  11. #define RESOLVER_TIMEOUT (Conf_PongTimeout*3)/4
  12. #include "portab.h"
  13. /**
  14. * @file
  15. * Asynchronous resolver
  16. */
  17. #include "imp.h"
  18. #include <assert.h>
  19. #include <errno.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/socket.h>
  24. #include <netinet/in.h>
  25. #include <netdb.h>
  26. #include <signal.h>
  27. #ifdef IDENTAUTH
  28. #ifdef HAVE_IDENT_H
  29. #include <ident.h>
  30. #endif
  31. #endif
  32. #include "array.h"
  33. #include "conn.h"
  34. #include "conf.h"
  35. #include "defines.h"
  36. #include "log.h"
  37. #include "ng_ipaddr.h"
  38. #include "exp.h"
  39. #include "resolve.h"
  40. #include "io.h"
  41. static void Do_ResolveAddr PARAMS(( const ng_ipaddr_t *Addr, int Sock, int w_fd ));
  42. static void Do_ResolveName PARAMS(( const char *Host, int w_fd ));
  43. #ifdef WANT_IPV6
  44. extern bool Conf_ConnectIPv4;
  45. extern bool Conf_ConnectIPv6;
  46. #endif
  47. /**
  48. * Resolve IP (asynchronous!).
  49. */
  50. GLOBAL bool
  51. Resolve_Addr(PROC_STAT * s, const ng_ipaddr_t *Addr, int identsock,
  52. void (*cbfunc) (int, short))
  53. {
  54. int pipefd[2];
  55. pid_t pid;
  56. assert(s != NULL);
  57. pid = Proc_Fork(s, pipefd, cbfunc, RESOLVER_TIMEOUT);
  58. if (pid > 0) {
  59. LogDebug("Resolver for %s created (PID %d).", ng_ipaddr_tostr(Addr), pid);
  60. return true;
  61. } else if( pid == 0 ) {
  62. /* Sub process */
  63. Log_Init_Subprocess("Resolver");
  64. Do_ResolveAddr( Addr, identsock, pipefd[1]);
  65. Log_Exit_Subprocess("Resolver");
  66. exit(0);
  67. }
  68. return false;
  69. } /* Resolve_Addr */
  70. /**
  71. * Resolve hostname (asynchronous!).
  72. */
  73. GLOBAL bool
  74. Resolve_Name( PROC_STAT *s, const char *Host, void (*cbfunc)(int, short))
  75. {
  76. int pipefd[2];
  77. pid_t pid;
  78. assert(s != NULL);
  79. pid = Proc_Fork(s, pipefd, cbfunc, RESOLVER_TIMEOUT);
  80. if (pid > 0) {
  81. /* Main process */
  82. #ifdef DEBUG
  83. Log( LOG_DEBUG, "Resolver for \"%s\" created (PID %d).", Host, pid );
  84. #endif
  85. return true;
  86. } else if( pid == 0 ) {
  87. /* Sub process */
  88. Log_Init_Subprocess("Resolver");
  89. Do_ResolveName(Host, pipefd[1]);
  90. Log_Exit_Subprocess("Resolver");
  91. exit(0);
  92. }
  93. return false;
  94. } /* Resolve_Name */
  95. #if !defined(HAVE_GETADDRINFO) || !defined(HAVE_GETNAMEINFO)
  96. #if !defined(WANT_IPV6) && defined(h_errno)
  97. static char *
  98. Get_Error( int H_Error )
  99. {
  100. /* Get error message for H_Error */
  101. switch( H_Error ) {
  102. case HOST_NOT_FOUND:
  103. return "host not found";
  104. case NO_DATA:
  105. return "name valid but no IP address defined";
  106. case NO_RECOVERY:
  107. return "name server error";
  108. case TRY_AGAIN:
  109. return "name server temporary not available";
  110. }
  111. return "unknown error";
  112. }
  113. #endif
  114. #endif
  115. /* Do "IDENT" (aka "AUTH") lookup and append result to resolved_addr array */
  116. static void
  117. Do_IdentQuery(int identsock, array *resolved_addr)
  118. {
  119. #ifdef IDENTAUTH
  120. char *res;
  121. if (identsock < 0)
  122. return;
  123. #ifdef DEBUG
  124. Log_Subprocess(LOG_DEBUG, "Doing IDENT lookup on socket %d ...",
  125. identsock);
  126. #endif
  127. res = ident_id( identsock, 10 );
  128. #ifdef DEBUG
  129. Log_Subprocess(LOG_DEBUG, "Ok, IDENT lookup on socket %d done: \"%s\"",
  130. identsock, res ? res : "(NULL)");
  131. #endif
  132. if (!res) /* no result */
  133. return;
  134. if (!array_cats(resolved_addr, res))
  135. Log_Subprocess(LOG_WARNING,
  136. "Resolver: Cannot copy IDENT result: %s!",
  137. strerror(errno));
  138. free(res);
  139. #else
  140. (void) identsock;
  141. (void) resolved_addr;
  142. #endif
  143. }
  144. /**
  145. * perform reverse DNS lookup and put result string into resbuf.
  146. * If no hostname could be obtained, this function stores the string representation of
  147. * the IP address in resbuf and returns false.
  148. * @param IpAddr ip address to resolve
  149. * @param resbuf result buffer to store DNS name/string representation of ip address
  150. * @param reslen size of result buffer (must be >= NGT_INET_ADDRSTRLEN)
  151. * @return true if reverse lookup successful, false otherwise
  152. */
  153. static bool
  154. ReverseLookup(const ng_ipaddr_t *IpAddr, char *resbuf, size_t reslen)
  155. {
  156. char tmp_ip_str[NG_INET_ADDRSTRLEN];
  157. const char *errmsg;
  158. #ifdef HAVE_GETNAMEINFO
  159. static const char funcname[]="getnameinfo";
  160. int res;
  161. *resbuf = 0;
  162. res = getnameinfo((struct sockaddr *) IpAddr, ng_ipaddr_salen(IpAddr),
  163. resbuf, (socklen_t)reslen, NULL, 0, NI_NAMEREQD);
  164. if (res == 0)
  165. return true;
  166. if (res == EAI_SYSTEM)
  167. errmsg = strerror(errno);
  168. else
  169. errmsg = gai_strerror(res);
  170. #else
  171. const struct sockaddr_in *Addr = (const struct sockaddr_in *) IpAddr;
  172. struct hostent *h;
  173. static const char funcname[]="gethostbyaddr";
  174. h = gethostbyaddr((char *)&Addr->sin_addr, sizeof(Addr->sin_addr), AF_INET);
  175. if (h) {
  176. if (strlcpy(resbuf, h->h_name, reslen) < reslen)
  177. return true;
  178. errmsg = "hostname too long";
  179. } else {
  180. # ifdef h_errno
  181. errmsg = Get_Error(h_errno);
  182. # else
  183. errmsg = "unknown error";
  184. # endif /* h_errno */
  185. }
  186. #endif /* HAVE_GETNAMEINFO */
  187. assert(errmsg);
  188. assert(reslen >= NG_INET_ADDRSTRLEN);
  189. ng_ipaddr_tostr_r(IpAddr, tmp_ip_str);
  190. Log_Subprocess(LOG_WARNING, "%s: Can't resolve address \"%s\": %s",
  191. funcname, tmp_ip_str, errmsg);
  192. strlcpy(resbuf, tmp_ip_str, reslen);
  193. return false;
  194. }
  195. /**
  196. * perform DNS lookup of given host name and fill IpAddr with a list of
  197. * ip addresses associated with that name.
  198. * ip addresses found are stored in the "array *IpAddr" argument (type ng_ipaddr_t)
  199. * @param hostname The domain name to look up.
  200. * @param IpAddr pointer to empty and initialized array to store results
  201. * @return true if lookup successful, false if domain name not found
  202. */
  203. static bool
  204. ForwardLookup(const char *hostname, array *IpAddr, int af)
  205. {
  206. ng_ipaddr_t addr;
  207. #ifdef HAVE_GETADDRINFO
  208. int res;
  209. struct addrinfo *a, *ai_results;
  210. static struct addrinfo hints;
  211. #ifdef AI_ADDRCONFIG /* glibc has this, but not e.g. netbsd 4.0 */
  212. hints.ai_flags = AI_ADDRCONFIG;
  213. #endif
  214. hints.ai_socktype = SOCK_STREAM;
  215. hints.ai_protocol = IPPROTO_TCP;
  216. hints.ai_family = af;
  217. memset(&addr, 0, sizeof(addr));
  218. res = getaddrinfo(hostname, NULL, &hints, &ai_results);
  219. switch (res) {
  220. case 0: break;
  221. case EAI_SYSTEM:
  222. Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, strerror(errno));
  223. return false;
  224. default:
  225. Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s", hostname, gai_strerror(res));
  226. return false;
  227. }
  228. for (a = ai_results; a != NULL; a = a->ai_next) {
  229. assert(a->ai_addrlen <= sizeof(addr));
  230. if (a->ai_addrlen > sizeof(addr))
  231. continue;
  232. memcpy(&addr, a->ai_addr, a->ai_addrlen);
  233. if (!array_catb(IpAddr, (char *)&addr, sizeof(addr)))
  234. break;
  235. }
  236. freeaddrinfo(ai_results);
  237. return a == NULL;
  238. #else
  239. struct hostent *h = gethostbyname(hostname);
  240. if (!h) {
  241. #ifdef h_errno
  242. Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\": %s",
  243. hostname, Get_Error(h_errno));
  244. #else
  245. Log_Subprocess(LOG_WARNING, "Can't resolve \"%s\"", hostname);
  246. #endif
  247. return false;
  248. }
  249. memset(&addr, 0, sizeof(addr));
  250. addr.sin4.sin_family = AF_INET;
  251. memcpy(&addr.sin4.sin_addr, h->h_addr, sizeof(struct in_addr));
  252. return array_copyb(IpAddr, (char *)&addr, sizeof(addr));
  253. #endif /* HAVE_GETADDRINFO */
  254. }
  255. static bool
  256. Addr_in_list(const array *resolved_addr, const ng_ipaddr_t *Addr)
  257. {
  258. char tmp_ip_str[NG_INET_ADDRSTRLEN];
  259. const ng_ipaddr_t *tmpAddrs = array_start(resolved_addr);
  260. size_t len = array_length(resolved_addr, sizeof(*tmpAddrs));
  261. assert(len > 0);
  262. assert(tmpAddrs);
  263. while (len > 0) {
  264. if (ng_ipaddr_ipequal(Addr, tmpAddrs))
  265. return true;
  266. tmpAddrs++;
  267. len--;
  268. }
  269. /* failed; print list of addresses */
  270. ng_ipaddr_tostr_r(Addr, tmp_ip_str);
  271. len = array_length(resolved_addr, sizeof(*tmpAddrs));
  272. tmpAddrs = array_start(resolved_addr);
  273. while (len > 0) {
  274. Log_Subprocess(LOG_WARNING, "Address mismatch: %s != %s",
  275. tmp_ip_str, ng_ipaddr_tostr(tmpAddrs));
  276. tmpAddrs++;
  277. len--;
  278. }
  279. return false;
  280. }
  281. static void
  282. Log_Forgery_NoIP(const char *ip, const char *host)
  283. {
  284. Log_Subprocess(LOG_WARNING,
  285. "Possible forgery: %s resolved to %s (which has no ip address)", ip, host);
  286. }
  287. static void
  288. Log_Forgery_WrongIP(const char *ip, const char *host)
  289. {
  290. Log_Subprocess(LOG_WARNING,
  291. "Possible forgery: %s resolved to %s (which points to different address)", ip, host);
  292. }
  293. static void
  294. ArrayWrite(int fd, const array *a)
  295. {
  296. size_t len = array_bytes(a);
  297. const char *data = array_start(a);
  298. assert(data);
  299. if( (size_t)write(fd, data, len) != len )
  300. Log_Subprocess( LOG_CRIT, "Resolver: Can't write to parent: %s!",
  301. strerror(errno));
  302. }
  303. static void
  304. Do_ResolveAddr(const ng_ipaddr_t *Addr, int identsock, int w_fd)
  305. {
  306. /* Resolver sub-process: resolve IP address and write result into
  307. * pipe to parent. */
  308. char hostname[CLIENT_HOST_LEN];
  309. char tmp_ip_str[NG_INET_ADDRSTRLEN];
  310. size_t len;
  311. array resolved_addr;
  312. array_init(&resolved_addr);
  313. ng_ipaddr_tostr_r(Addr, tmp_ip_str);
  314. #ifdef DEBUG
  315. Log_Subprocess(LOG_DEBUG, "Now resolving %s ...", tmp_ip_str);
  316. #endif
  317. if (!ReverseLookup(Addr, hostname, sizeof(hostname)))
  318. goto dns_done;
  319. if (ForwardLookup(hostname, &resolved_addr, ng_ipaddr_af(Addr))) {
  320. if (!Addr_in_list(&resolved_addr, Addr)) {
  321. Log_Forgery_WrongIP(tmp_ip_str, hostname);
  322. strlcpy(hostname, tmp_ip_str, sizeof(hostname));
  323. }
  324. } else {
  325. Log_Forgery_NoIP(tmp_ip_str, hostname);
  326. strlcpy(hostname, tmp_ip_str, sizeof(hostname));
  327. }
  328. #ifdef DEBUG
  329. Log_Subprocess(LOG_DEBUG, "Ok, translated %s to \"%s\".", tmp_ip_str, hostname);
  330. #endif
  331. dns_done:
  332. len = strlen(hostname);
  333. hostname[len] = '\n';
  334. if (!array_copyb(&resolved_addr, hostname, ++len)) {
  335. Log_Subprocess(LOG_CRIT,
  336. "Resolver: Can't copy resolved name: %s!",
  337. strerror(errno));
  338. array_free(&resolved_addr);
  339. return;
  340. }
  341. Do_IdentQuery(identsock, &resolved_addr);
  342. ArrayWrite(w_fd, &resolved_addr);
  343. array_free(&resolved_addr);
  344. } /* Do_ResolveAddr */
  345. static void
  346. Do_ResolveName( const char *Host, int w_fd )
  347. {
  348. /* Resolver sub-process: resolve name and write result into pipe
  349. * to parent. */
  350. array IpAddrs;
  351. int af;
  352. #ifdef DEBUG
  353. ng_ipaddr_t *addr;
  354. size_t len;
  355. #endif
  356. Log_Subprocess(LOG_DEBUG, "Now resolving \"%s\" ...", Host);
  357. array_init(&IpAddrs);
  358. #ifdef WANT_IPV6
  359. af = AF_UNSPEC;
  360. assert(Conf_ConnectIPv6 || Conf_ConnectIPv4);
  361. if (!Conf_ConnectIPv6)
  362. af = AF_INET;
  363. if (!Conf_ConnectIPv4)
  364. af = AF_INET6;
  365. #else
  366. af = AF_INET;
  367. #endif
  368. if (!ForwardLookup(Host, &IpAddrs, af)) {
  369. close(w_fd);
  370. return;
  371. }
  372. #ifdef DEBUG
  373. len = array_length(&IpAddrs, sizeof(*addr));
  374. assert(len > 0);
  375. addr = array_start(&IpAddrs);
  376. assert(addr);
  377. for (; len > 0; --len,addr++) {
  378. Log_Subprocess(LOG_DEBUG, "translated \"%s\" to %s.",
  379. Host, ng_ipaddr_tostr(addr));
  380. }
  381. #endif
  382. /* Write result into pipe to parent */
  383. ArrayWrite(w_fd, &IpAddrs);
  384. array_free(&IpAddrs);
  385. } /* Do_ResolveName */
  386. /* -eof- */