dns.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. extern Tcl_Interp *interp;
  21. devent_t *dns_events = NULL;
  22. /*
  23. * DCC functions
  24. */
  25. void dcc_dnswait(int idx, char *buf, int len)
  26. {
  27. /* Ignore anything now. */
  28. }
  29. void eof_dcc_dnswait(int idx)
  30. {
  31. putlog(LOG_MISC, "*", "Lost connection while resolving hostname [%s/%d]",
  32. iptostr(htonl(dcc[idx].addr)), dcc[idx].port);
  33. killsock(dcc[idx].sock);
  34. lostdcc(idx);
  35. }
  36. static void display_dcc_dnswait(int idx, char *buf)
  37. {
  38. sprintf(buf, "dns waited %lus", now - dcc[idx].timeval);
  39. }
  40. static int expmem_dcc_dnswait(void *x)
  41. {
  42. register struct dns_info *p = (struct dns_info *) x;
  43. int size = 0;
  44. if (p) {
  45. size = sizeof(struct dns_info);
  46. if (p->host)
  47. size += strlen(p->host) + 1;
  48. if (p->cbuf)
  49. size += strlen(p->cbuf) + 1;
  50. }
  51. return size;
  52. }
  53. static void kill_dcc_dnswait(int idx, void *x)
  54. {
  55. register struct dns_info *p = (struct dns_info *) x;
  56. if (p) {
  57. if (p->host)
  58. nfree(p->host);
  59. if (p->cbuf)
  60. nfree(p->cbuf);
  61. nfree(p);
  62. }
  63. }
  64. struct dcc_table DCC_DNSWAIT =
  65. {
  66. "DNSWAIT",
  67. DCT_VALIDIDX,
  68. eof_dcc_dnswait,
  69. dcc_dnswait,
  70. 0,
  71. 0,
  72. display_dcc_dnswait,
  73. expmem_dcc_dnswait,
  74. kill_dcc_dnswait,
  75. 0
  76. };
  77. /*
  78. * DCC events
  79. */
  80. /* Walk through every dcc entry and look for waiting DNS requests
  81. * of RES_HOSTBYIP for our IP address.
  82. */
  83. static void dns_dcchostbyip(IP ip, char *hostn, int ok, void *other)
  84. {
  85. int idx;
  86. for (idx = 0; idx < dcc_total; idx++) {
  87. if ((dcc[idx].type == &DCC_DNSWAIT) &&
  88. (dcc[idx].u.dns->dns_type == RES_HOSTBYIP) &&
  89. (dcc[idx].u.dns->ip == ip)) {
  90. if (dcc[idx].u.dns->host)
  91. nfree(dcc[idx].u.dns->host);
  92. dcc[idx].u.dns->host = get_data_ptr(strlen(hostn) + 1);
  93. strcpy(dcc[idx].u.dns->host, hostn);
  94. if (ok)
  95. dcc[idx].u.dns->dns_success(idx);
  96. else
  97. dcc[idx].u.dns->dns_failure(idx);
  98. }
  99. }
  100. }
  101. /* Walk through every dcc entry and look for waiting DNS requests
  102. * of RES_IPBYHOST for our hostname.
  103. */
  104. static void dns_dccipbyhost(IP ip, char *hostn, int ok, void *other)
  105. {
  106. int idx;
  107. for (idx = 0; idx < dcc_total; idx++) {
  108. if ((dcc[idx].type == &DCC_DNSWAIT) &&
  109. (dcc[idx].u.dns->dns_type == RES_IPBYHOST) &&
  110. !egg_strcasecmp(dcc[idx].u.dns->host, hostn)) {
  111. dcc[idx].u.dns->ip = ip;
  112. if (ok)
  113. dcc[idx].u.dns->dns_success(idx);
  114. else
  115. dcc[idx].u.dns->dns_failure(idx);
  116. }
  117. }
  118. }
  119. static int dns_dccexpmem(void *other)
  120. {
  121. return 0;
  122. }
  123. devent_type DNS_DCCEVENT_HOSTBYIP = {
  124. "DCCEVENT_HOSTBYIP",
  125. dns_dccexpmem,
  126. dns_dcchostbyip
  127. };
  128. devent_type DNS_DCCEVENT_IPBYHOST = {
  129. "DCCEVENT_IPBYHOST",
  130. dns_dccexpmem,
  131. dns_dccipbyhost
  132. };
  133. void dcc_dnsipbyhost(char *hostn)
  134. {
  135. devent_t *de;
  136. for (de = dns_events; de; de = de->next) {
  137. if (de->type && (de->type == &DNS_DCCEVENT_IPBYHOST) &&
  138. (de->lookup == RES_IPBYHOST)) {
  139. if (de->res_data.hostname &&
  140. !egg_strcasecmp(de->res_data.hostname, hostn))
  141. /* No need to add anymore. */
  142. return;
  143. }
  144. }
  145. de = nmalloc(sizeof(devent_t));
  146. egg_bzero(de, sizeof(devent_t));
  147. /* Link into list. */
  148. de->next = dns_events;
  149. dns_events = de;
  150. de->type = &DNS_DCCEVENT_IPBYHOST;
  151. de->lookup = RES_IPBYHOST;
  152. de->res_data.hostname = nmalloc(strlen(hostn) + 1);
  153. strcpy(de->res_data.hostname, hostn);
  154. /* Send request. */
  155. dns_ipbyhost(hostn);
  156. }
  157. void dcc_dnshostbyip(IP ip)
  158. {
  159. devent_t *de;
  160. for (de = dns_events; de; de = de->next) {
  161. if (de->type && (de->type == &DNS_DCCEVENT_HOSTBYIP) &&
  162. (de->lookup == RES_HOSTBYIP)) {
  163. if (de->res_data.ip_addr == ip)
  164. /* No need to add anymore. */
  165. return;
  166. }
  167. }
  168. de = nmalloc(sizeof(devent_t));
  169. egg_bzero(de, sizeof(devent_t));
  170. /* Link into list. */
  171. de->next = dns_events;
  172. dns_events = de;
  173. de->type = &DNS_DCCEVENT_HOSTBYIP;
  174. de->lookup = RES_HOSTBYIP;
  175. de->res_data.ip_addr = ip;
  176. /* Send request. */
  177. dns_hostbyip(ip);
  178. }
  179. /*
  180. * Tcl events
  181. */
  182. static void dns_tcl_iporhostres(IP ip, char *hostn, int ok, void *other)
  183. {
  184. devent_tclinfo_t *tclinfo = (devent_tclinfo_t *) other;
  185. if (Tcl_VarEval(interp, tclinfo->proc, " ", iptostr(htonl(ip)), " ",
  186. hostn, ok ? " 1" : " 0", tclinfo->paras, NULL) == TCL_ERROR)
  187. putlog(LOG_MISC, "*", DCC_TCLERROR, tclinfo->proc, interp->result);
  188. /* Free the memory. It will be unused after this event call. */
  189. nfree(tclinfo->proc);
  190. if (tclinfo->paras)
  191. nfree(tclinfo->paras);
  192. nfree(tclinfo);
  193. }
  194. static int dns_tclexpmem(void *other)
  195. {
  196. devent_tclinfo_t *tclinfo = (devent_tclinfo_t *) other;
  197. int l = 0;
  198. if (tclinfo) {
  199. l = sizeof(devent_tclinfo_t);
  200. if (tclinfo->proc)
  201. l += strlen(tclinfo->proc) + 1;
  202. if (tclinfo->paras)
  203. l += strlen(tclinfo->paras) + 1;
  204. }
  205. return l;
  206. }
  207. devent_type DNS_TCLEVENT_HOSTBYIP = {
  208. "TCLEVENT_HOSTBYIP",
  209. dns_tclexpmem,
  210. dns_tcl_iporhostres
  211. };
  212. devent_type DNS_TCLEVENT_IPBYHOST = {
  213. "TCLEVENT_IPBYHOST",
  214. dns_tclexpmem,
  215. dns_tcl_iporhostres
  216. };
  217. static void tcl_dnsipbyhost(char *hostn, char *proc, char *paras)
  218. {
  219. devent_t *de;
  220. devent_tclinfo_t *tclinfo;
  221. de = nmalloc(sizeof(devent_t));
  222. egg_bzero(de, sizeof(devent_t));
  223. /* Link into list. */
  224. de->next = dns_events;
  225. dns_events = de;
  226. de->type = &DNS_TCLEVENT_IPBYHOST;
  227. de->lookup = RES_IPBYHOST;
  228. de->res_data.hostname = nmalloc(strlen(hostn) + 1);
  229. strcpy(de->res_data.hostname, hostn);
  230. /* Store additional data. */
  231. tclinfo = nmalloc(sizeof(devent_tclinfo_t));
  232. tclinfo->proc = nmalloc(strlen(proc) + 1);
  233. strcpy(tclinfo->proc, proc);
  234. if (paras) {
  235. tclinfo->paras = nmalloc(strlen(paras) + 1);
  236. strcpy(tclinfo->paras, paras);
  237. } else
  238. tclinfo->paras = NULL;
  239. de->other = tclinfo;
  240. /* Send request. */
  241. dns_ipbyhost(hostn);
  242. }
  243. static void tcl_dnshostbyip(IP ip, char *proc, char *paras)
  244. {
  245. devent_t *de;
  246. devent_tclinfo_t *tclinfo;
  247. de = nmalloc(sizeof(devent_t));
  248. egg_bzero(de, sizeof(devent_t));
  249. /* Link into list. */
  250. de->next = dns_events;
  251. dns_events = de;
  252. de->type = &DNS_TCLEVENT_HOSTBYIP;
  253. de->lookup = RES_HOSTBYIP;
  254. de->res_data.ip_addr = ip;
  255. /* Store additional data. */
  256. tclinfo = nmalloc(sizeof(devent_tclinfo_t));
  257. tclinfo->proc = nmalloc(strlen(proc) + 1);
  258. strcpy(tclinfo->proc, proc);
  259. if (paras) {
  260. tclinfo->paras = nmalloc(strlen(paras) + 1);
  261. strcpy(tclinfo->paras, paras);
  262. } else
  263. tclinfo->paras = NULL;
  264. de->other = tclinfo;
  265. /* Send request. */
  266. dns_hostbyip(ip);
  267. }
  268. /*
  269. * Event functions
  270. */
  271. inline static int dnsevent_expmem(void)
  272. {
  273. devent_t *de;
  274. int tot = 0;
  275. for (de = dns_events; de; de = de->next) {
  276. tot += sizeof(devent_t);
  277. if ((de->lookup == RES_IPBYHOST) && de->res_data.hostname)
  278. tot += strlen(de->res_data.hostname) + 1;
  279. if (de->type && de->type->expmem)
  280. tot += de->type->expmem(de->other);
  281. }
  282. return tot;
  283. }
  284. void call_hostbyip(IP ip, char *hostn, int ok)
  285. {
  286. devent_t *de = dns_events, *ode = NULL, *nde = NULL;
  287. while (de) {
  288. nde = de->next;
  289. if ((de->lookup == RES_HOSTBYIP) &&
  290. (!de->res_data.ip_addr || (de->res_data.ip_addr == ip))) {
  291. /* Remove the event from the list here, to avoid conflicts if one of
  292. * the event handlers re-adds another event. */
  293. if (ode)
  294. ode->next = de->next;
  295. else
  296. dns_events = de->next;
  297. if (de->type && de->type->event)
  298. de->type->event(ip, hostn, ok, de->other);
  299. else
  300. putlog(LOG_MISC, "*", "(!) Unknown DNS event type found: %s",
  301. (de->type && de->type->name) ? de->type->name : "<empty>");
  302. nfree(de);
  303. de = ode;
  304. }
  305. ode = de;
  306. de = nde;
  307. }
  308. }
  309. void call_ipbyhost(char *hostn, IP ip, int ok)
  310. {
  311. devent_t *de = dns_events, *ode = NULL, *nde = NULL;
  312. while (de) {
  313. nde = de->next;
  314. if ((de->lookup == RES_IPBYHOST) &&
  315. (!de->res_data.hostname ||
  316. !egg_strcasecmp(de->res_data.hostname, hostn))) {
  317. /* Remove the event from the list here, to avoid conflicts if one of
  318. * the event handlers re-adds another event. */
  319. if (ode)
  320. ode->next = de->next;
  321. else
  322. dns_events = de->next;
  323. if (de->type && de->type->event)
  324. de->type->event(ip, hostn, ok, de->other);
  325. else
  326. putlog(LOG_MISC, "*", "(!) Unknown DNS event type found: %s",
  327. (de->type && de->type->name) ? de->type->name : "<empty>");
  328. if (de->res_data.hostname)
  329. nfree(de->res_data.hostname);
  330. nfree(de);
  331. de = ode;
  332. }
  333. ode = de;
  334. de = nde;
  335. }
  336. }
  337. /*
  338. * Async DNS emulation functions
  339. */
  340. void block_dns_hostbyip(IP ip)
  341. {
  342. struct hostent *hp;
  343. unsigned long addr = htonl(ip);
  344. static char s[UHOSTLEN];
  345. if (!setjmp(alarmret)) {
  346. alarm(resolve_timeout);
  347. hp = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET);
  348. alarm(0);
  349. if (hp) {
  350. strncpyz(s, hp->h_name, sizeof s);
  351. } else
  352. strcpy(s, iptostr(addr));
  353. } else {
  354. hp = NULL;
  355. strcpy(s, iptostr(addr));
  356. }
  357. /* Call hooks. */
  358. call_hostbyip(ip, s, hp ? 1 : 0);
  359. }
  360. void block_dns_ipbyhost(char *host)
  361. {
  362. struct in_addr inaddr;
  363. /* Check if someone passed us an IP address as hostname
  364. * and return it straight away */
  365. if (egg_inet_aton(host, &inaddr)) {
  366. call_ipbyhost(host, ntohl(inaddr.s_addr), 1);
  367. return;
  368. }
  369. if (!setjmp(alarmret)) {
  370. struct hostent *hp;
  371. struct in_addr *in;
  372. IP ip = 0;
  373. alarm(resolve_timeout);
  374. hp = gethostbyname(host);
  375. alarm(0);
  376. if (hp) {
  377. in = (struct in_addr *) (hp->h_addr_list[0]);
  378. ip = (IP) (in->s_addr);
  379. call_ipbyhost(host, ntohl(ip), 1);
  380. return;
  381. }
  382. /* Fall through. */
  383. }
  384. call_ipbyhost(host, 0, 0);
  385. }
  386. /*
  387. * Misc functions
  388. */
  389. int expmem_dns(void)
  390. {
  391. return dnsevent_expmem();
  392. }
  393. /*
  394. * Tcl functions
  395. */
  396. /* dnslookup <ip-address> <proc> */
  397. static int tcl_dnslookup STDVAR
  398. {
  399. struct in_addr inaddr;
  400. char *paras = NULL;
  401. /* This function should be using BADARGS, FIXME -poptix */
  402. if (argc < 3) {
  403. Tcl_AppendResult(irp, "wrong # args: should be \"", argv[0],
  404. " ip-address/hostname proc ?args...?\"", NULL);
  405. return TCL_ERROR;
  406. }
  407. if (argc > 3) {
  408. int l = 0, p;
  409. /* Create a string with a leading space out of all provided
  410. * additional parameters.
  411. */
  412. paras = nmalloc(1);
  413. paras[0] = 0;
  414. for (p = 3; p < argc; p++) {
  415. l += strlen(argv[p]) + 1;
  416. paras = nrealloc(paras, l + 1);
  417. strcat(paras, " ");
  418. strcat(paras, argv[p]);
  419. }
  420. }
  421. if (egg_inet_aton(argv[1], &inaddr))
  422. tcl_dnshostbyip(ntohl(inaddr.s_addr), argv[2], paras);
  423. else
  424. tcl_dnsipbyhost(argv[1], argv[2], paras);
  425. if (paras)
  426. nfree(paras);
  427. return TCL_OK;
  428. }
  429. tcl_cmds tcldns_cmds[] =
  430. {
  431. {"dnslookup", tcl_dnslookup},
  432. {NULL, NULL}
  433. };