dns.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * dns.c -- handles:
  3. * DNS resolve calls and events
  4. * provides the code used by the bot if the DNS module is not loaded
  5. * DNS Tcl commands
  6. *
  7. */
  8. #include "main.h"
  9. #include <netdb.h>
  10. #include <setjmp.h>
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #include <arpa/inet.h>
  14. #include "dns.h"
  15. extern struct dcc_t *dcc;
  16. extern int dcc_total;
  17. extern int resolve_timeout;
  18. extern time_t now;
  19. extern jmp_buf alarmret;
  20. devent_t *dns_events = NULL;
  21. /*
  22. * DCC functions
  23. */
  24. void dcc_dnswait(int idx, char *buf, int len)
  25. {
  26. /* Ignore anything now. */
  27. }
  28. void eof_dcc_dnswait(int idx)
  29. {
  30. putlog(LOG_MISC, "*", "Lost connection while resolving hostname [%s/%d]",
  31. iptostr(htonl(dcc[idx].addr)), dcc[idx].port);
  32. killsock(dcc[idx].sock);
  33. lostdcc(idx);
  34. }
  35. static void display_dcc_dnswait(int idx, char *buf)
  36. {
  37. sprintf(buf, "dns waited %lus", now - dcc[idx].timeval);
  38. }
  39. static void kill_dcc_dnswait(int idx, void *x)
  40. {
  41. register struct dns_info *p = (struct dns_info *) x;
  42. if (p) {
  43. if (p->host)
  44. free(p->host);
  45. if (p->cbuf)
  46. free(p->cbuf);
  47. free(p);
  48. }
  49. }
  50. struct dcc_table DCC_DNSWAIT =
  51. {
  52. "DNSWAIT",
  53. DCT_VALIDIDX,
  54. eof_dcc_dnswait,
  55. dcc_dnswait,
  56. 0,
  57. 0,
  58. display_dcc_dnswait,
  59. kill_dcc_dnswait,
  60. 0
  61. };
  62. /*
  63. * DCC events
  64. */
  65. /* Walk through every dcc entry and look for waiting DNS requests
  66. * of RES_HOSTBYIP for our IP address.
  67. */
  68. static void dns_dcchostbyip(IP ip, char *hostn, int ok, void *other)
  69. {
  70. int idx;
  71. for (idx = 0; idx < dcc_total; idx++) {
  72. if ((dcc[idx].type == &DCC_DNSWAIT) &&
  73. (dcc[idx].u.dns->dns_type == RES_HOSTBYIP) &&
  74. (dcc[idx].u.dns->ip == ip)) {
  75. if (dcc[idx].u.dns->host)
  76. free(dcc[idx].u.dns->host);
  77. dcc[idx].u.dns->host = calloc(1, strlen(hostn) + 1);
  78. strcpy(dcc[idx].u.dns->host, hostn);
  79. if (ok)
  80. dcc[idx].u.dns->dns_success(idx);
  81. else
  82. dcc[idx].u.dns->dns_failure(idx);
  83. }
  84. }
  85. }
  86. /* Walk through every dcc entry and look for waiting DNS requests
  87. * of RES_IPBYHOST for our hostname.
  88. */
  89. static void dns_dccipbyhost(IP ip, char *hostn, int ok, void *other)
  90. {
  91. int idx;
  92. for (idx = 0; idx < dcc_total; idx++) {
  93. if ((dcc[idx].type == &DCC_DNSWAIT) &&
  94. (dcc[idx].u.dns->dns_type == RES_IPBYHOST) &&
  95. !egg_strcasecmp(dcc[idx].u.dns->host, hostn)) {
  96. dcc[idx].u.dns->ip = ip;
  97. if (ok)
  98. dcc[idx].u.dns->dns_success(idx);
  99. else
  100. dcc[idx].u.dns->dns_failure(idx);
  101. }
  102. }
  103. }
  104. devent_type DNS_DCCEVENT_HOSTBYIP = {
  105. "DCCEVENT_HOSTBYIP",
  106. dns_dcchostbyip
  107. };
  108. devent_type DNS_DCCEVENT_IPBYHOST = {
  109. "DCCEVENT_IPBYHOST",
  110. dns_dccipbyhost
  111. };
  112. void dcc_dnsipbyhost(char *hostn)
  113. {
  114. devent_t *de;
  115. for (de = dns_events; de; de = de->next) {
  116. if (de->type && (de->type == &DNS_DCCEVENT_IPBYHOST) &&
  117. (de->lookup == RES_IPBYHOST)) {
  118. if (de->res_data.hostname &&
  119. !egg_strcasecmp(de->res_data.hostname, hostn))
  120. /* No need to add anymore. */
  121. return;
  122. }
  123. }
  124. de = malloc(sizeof(devent_t));
  125. egg_bzero(de, sizeof(devent_t));
  126. /* Link into list. */
  127. de->next = dns_events;
  128. dns_events = de;
  129. de->type = &DNS_DCCEVENT_IPBYHOST;
  130. de->lookup = RES_IPBYHOST;
  131. de->res_data.hostname = strdup(hostn);
  132. /* Send request. */
  133. dns_ipbyhost(hostn);
  134. }
  135. void dcc_dnshostbyip(IP ip)
  136. {
  137. devent_t *de;
  138. for (de = dns_events; de; de = de->next) {
  139. if (de->type && (de->type == &DNS_DCCEVENT_HOSTBYIP) &&
  140. (de->lookup == RES_HOSTBYIP)) {
  141. if (de->res_data.ip_addr == ip)
  142. /* No need to add anymore. */
  143. return;
  144. }
  145. }
  146. de = malloc(sizeof(devent_t));
  147. egg_bzero(de, sizeof(devent_t));
  148. /* Link into list. */
  149. de->next = dns_events;
  150. dns_events = de;
  151. de->type = &DNS_DCCEVENT_HOSTBYIP;
  152. de->lookup = RES_HOSTBYIP;
  153. de->res_data.ip_addr = ip;
  154. /* Send request. */
  155. dns_hostbyip(ip);
  156. }
  157. /*
  158. * Event functions
  159. */
  160. void call_hostbyip(IP ip, char *hostn, int ok)
  161. {
  162. devent_t *de = dns_events, *ode = NULL, *nde = NULL;
  163. while (de) {
  164. nde = de->next;
  165. if ((de->lookup == RES_HOSTBYIP) &&
  166. (!de->res_data.ip_addr || (de->res_data.ip_addr == ip))) {
  167. /* Remove the event from the list here, to avoid conflicts if one of
  168. * the event handlers re-adds another event. */
  169. if (ode)
  170. ode->next = de->next;
  171. else
  172. dns_events = de->next;
  173. if (de->type && de->type->event)
  174. de->type->event(ip, hostn, ok, de->other);
  175. else
  176. putlog(LOG_MISC, "*", "(!) Unknown DNS event type found: %s",
  177. (de->type && de->type->name) ? de->type->name : "<empty>");
  178. free(de);
  179. de = ode;
  180. }
  181. ode = de;
  182. de = nde;
  183. }
  184. }
  185. void call_ipbyhost(char *hostn, IP ip, int ok)
  186. {
  187. devent_t *de = dns_events, *ode = NULL, *nde = NULL;
  188. while (de) {
  189. nde = de->next;
  190. if ((de->lookup == RES_IPBYHOST) &&
  191. (!de->res_data.hostname ||
  192. !egg_strcasecmp(de->res_data.hostname, hostn))) {
  193. /* Remove the event from the list here, to avoid conflicts if one of
  194. * the event handlers re-adds another event. */
  195. if (ode)
  196. ode->next = de->next;
  197. else
  198. dns_events = de->next;
  199. if (de->type && de->type->event)
  200. de->type->event(ip, hostn, ok, de->other);
  201. else
  202. putlog(LOG_MISC, "*", "(!) Unknown DNS event type found: %s",
  203. (de->type && de->type->name) ? de->type->name : "<empty>");
  204. if (de->res_data.hostname)
  205. free(de->res_data.hostname);
  206. free(de);
  207. de = ode;
  208. }
  209. ode = de;
  210. de = nde;
  211. }
  212. }
  213. /*
  214. * Async DNS emulation functions
  215. */
  216. void block_dns_hostbyip(IP ip)
  217. {
  218. struct hostent *hp;
  219. unsigned long addr = htonl(ip);
  220. static char s[UHOSTLEN];
  221. if (!setjmp(alarmret)) {
  222. alarm(resolve_timeout);
  223. hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
  224. alarm(0);
  225. if (hp) {
  226. strncpyz(s, hp->h_name, sizeof s);
  227. } else
  228. strcpy(s, iptostr(addr));
  229. } else {
  230. hp = NULL;
  231. strcpy(s, iptostr(addr));
  232. }
  233. /* Call hooks. */
  234. call_hostbyip(ip, s, hp ? 1 : 0);
  235. }
  236. void block_dns_ipbyhost(char *host)
  237. {
  238. struct in_addr inaddr;
  239. /* Check if someone passed us an IP address as hostname
  240. * and return it straight away */
  241. if (egg_inet_aton(host, &inaddr)) {
  242. call_ipbyhost(host, ntohl(inaddr.s_addr), 1);
  243. return;
  244. }
  245. if (!setjmp(alarmret)) {
  246. struct hostent *hp;
  247. struct in_addr *in;
  248. IP ip = 0;
  249. alarm(resolve_timeout);
  250. hp = gethostbyname(host);
  251. alarm(0);
  252. if (hp) {
  253. in = (struct in_addr *) (hp->h_addr_list[0]);
  254. ip = (IP) (in->s_addr);
  255. call_ipbyhost(host, ntohl(ip), 1);
  256. return;
  257. }
  258. /* Fall through. */
  259. }
  260. call_ipbyhost(host, 0, 0);
  261. }