resolve.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. #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!",
  288. ip, host);
  289. }
  290. static void
  291. Log_Forgery_WrongIP(const char *ip, const char *host)
  292. {
  293. Log_Subprocess(LOG_WARNING,
  294. "Possible forgery: %s resolved to \"%s\", which points to a different address!",
  295. ip, host);
  296. }
  297. static void
  298. ArrayWrite(int fd, const array *a)
  299. {
  300. size_t len = array_bytes(a);
  301. const char *data = array_start(a);
  302. assert(data);
  303. if( (size_t)write(fd, data, len) != len )
  304. Log_Subprocess( LOG_CRIT, "Resolver: Can't write to parent: %s!",
  305. strerror(errno));
  306. }
  307. static void
  308. Do_ResolveAddr(const ng_ipaddr_t *Addr, int identsock, int w_fd)
  309. {
  310. /* Resolver sub-process: resolve IP address and write result into
  311. * pipe to parent. */
  312. char hostname[CLIENT_HOST_LEN];
  313. char tmp_ip_str[NG_INET_ADDRSTRLEN];
  314. size_t len;
  315. array resolved_addr;
  316. array_init(&resolved_addr);
  317. ng_ipaddr_tostr_r(Addr, tmp_ip_str);
  318. #ifdef DEBUG
  319. Log_Subprocess(LOG_DEBUG, "Now resolving %s ...", tmp_ip_str);
  320. #endif
  321. if (!ReverseLookup(Addr, hostname, sizeof(hostname)))
  322. goto dns_done;
  323. if (ForwardLookup(hostname, &resolved_addr, ng_ipaddr_af(Addr))) {
  324. if (!Addr_in_list(&resolved_addr, Addr)) {
  325. Log_Forgery_WrongIP(tmp_ip_str, hostname);
  326. strlcpy(hostname, tmp_ip_str, sizeof(hostname));
  327. }
  328. } else {
  329. Log_Forgery_NoIP(tmp_ip_str, hostname);
  330. strlcpy(hostname, tmp_ip_str, sizeof(hostname));
  331. }
  332. #ifdef DEBUG
  333. Log_Subprocess(LOG_DEBUG, "Ok, translated %s to \"%s\".", tmp_ip_str, hostname);
  334. #endif
  335. dns_done:
  336. len = strlen(hostname);
  337. hostname[len] = '\n';
  338. if (!array_copyb(&resolved_addr, hostname, ++len)) {
  339. Log_Subprocess(LOG_CRIT,
  340. "Resolver: Can't copy resolved name: %s!",
  341. strerror(errno));
  342. array_free(&resolved_addr);
  343. return;
  344. }
  345. Do_IdentQuery(identsock, &resolved_addr);
  346. ArrayWrite(w_fd, &resolved_addr);
  347. array_free(&resolved_addr);
  348. } /* Do_ResolveAddr */
  349. static void
  350. Do_ResolveName( const char *Host, int w_fd )
  351. {
  352. /* Resolver sub-process: resolve name and write result into pipe
  353. * to parent. */
  354. array IpAddrs;
  355. int af;
  356. #ifdef DEBUG
  357. ng_ipaddr_t *addr;
  358. size_t len;
  359. #endif
  360. Log_Subprocess(LOG_DEBUG, "Now resolving \"%s\" ...", Host);
  361. array_init(&IpAddrs);
  362. #ifdef WANT_IPV6
  363. af = AF_UNSPEC;
  364. assert(Conf_ConnectIPv6 || Conf_ConnectIPv4);
  365. if (!Conf_ConnectIPv6)
  366. af = AF_INET;
  367. if (!Conf_ConnectIPv4)
  368. af = AF_INET6;
  369. #else
  370. af = AF_INET;
  371. #endif
  372. if (!ForwardLookup(Host, &IpAddrs, af)) {
  373. close(w_fd);
  374. return;
  375. }
  376. #ifdef DEBUG
  377. len = array_length(&IpAddrs, sizeof(*addr));
  378. assert(len > 0);
  379. addr = array_start(&IpAddrs);
  380. assert(addr);
  381. for (; len > 0; --len,addr++) {
  382. Log_Subprocess(LOG_DEBUG, "translated \"%s\" to %s.",
  383. Host, ng_ipaddr_tostr(addr));
  384. }
  385. #endif
  386. /* Write result into pipe to parent */
  387. ArrayWrite(w_fd, &IpAddrs);
  388. array_free(&IpAddrs);
  389. } /* Do_ResolveName */
  390. /* -eof- */