resolve.c 11 KB

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