resolve.c 12 KB

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