resolve.c 11 KB

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