resolve.c 12 KB

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