dccutil.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. * dccutil.c -- handles:
  3. * lots of little functions to send formatted text to
  4. * varying types of connections
  5. * '.who', '.whom', and '.dccstat' code
  6. * memory management for dcc structures
  7. * timeout checking for dcc connections
  8. *
  9. */
  10. #include <sys/stat.h>
  11. #include "common.h"
  12. #include "color.h"
  13. #include "dcc.h"
  14. #include "botnet.h"
  15. #include "adns.h"
  16. #include "net.h"
  17. #include "main.h"
  18. #include "dccutil.h"
  19. #include "misc.h"
  20. #include "botcmd.h"
  21. #include <errno.h>
  22. #include "chan.h"
  23. #include "tandem.h"
  24. #include "core_binds.h"
  25. #include "egg_timer.h"
  26. #include "src/mod/server.mod/server.h"
  27. #include "src/mod/notes.mod/notes.h"
  28. #include <stdarg.h>
  29. static struct portmap *root = NULL;
  30. time_t connect_timeout = 15; /* How long to wait before a telnet
  31. connection times out */
  32. int max_dcc = 200;
  33. static int dcc_flood_thr = 3;
  34. void init_dcc_max()
  35. {
  36. int osock = MAXSOCKS;
  37. if (max_dcc < 1)
  38. max_dcc = 1;
  39. if (dcc)
  40. dcc = (struct dcc_t *) realloc(dcc, sizeof(struct dcc_t) * max_dcc);
  41. else
  42. dcc = (struct dcc_t *) calloc(1, sizeof(struct dcc_t) * max_dcc);
  43. MAXSOCKS = max_dcc + 10;
  44. if (socklist)
  45. socklist = (sock_list *) realloc((void *) socklist, sizeof(sock_list) * MAXSOCKS);
  46. else
  47. socklist = (sock_list *) calloc(1, sizeof(sock_list) * MAXSOCKS);
  48. for (; osock < MAXSOCKS; osock++)
  49. socklist[osock].flags = SOCK_UNUSED;
  50. }
  51. /* Replace \n with \r\n */
  52. char *add_cr(char *buf)
  53. {
  54. static char WBUF[1024] = "";
  55. char *p = NULL, *q = NULL;
  56. for (p = buf, q = WBUF; *p; p++, q++) {
  57. if (*p == '\n')
  58. *q++ = '\r';
  59. *q = *p;
  60. }
  61. *q = *p;
  62. return WBUF;
  63. }
  64. void dprintf(int idx, const char *format, ...)
  65. {
  66. static char buf[1024] = "";
  67. size_t len;
  68. va_list va;
  69. va_start(va, format);
  70. egg_vsnprintf(buf, 1023, format, va);
  71. va_end(va);
  72. /* We can not use the return value vsnprintf() to determine where
  73. * to null terminate. The C99 standard specifies that vsnprintf()
  74. * shall return the number of bytes that would be written if the
  75. * buffer had been large enough, rather then -1.
  76. */
  77. /* We actually can, since if it's < 0 or >= sizeof(buf), we know it wrote
  78. * sizeof(buf) bytes. But we're not doing that anyway.
  79. */
  80. buf[sizeof(buf) - 1] = 0;
  81. len = strlen(buf);
  82. /* this is for color on dcc :P */
  83. if (idx < 0) {
  84. tputs(-idx, buf, len);
  85. } else if (idx > 0x7FF0) {
  86. switch (idx) {
  87. case DP_LOG:
  88. putlog(LOG_MISC, "*", "%s", buf);
  89. break;
  90. case DP_STDOUT:
  91. tputs(STDOUT, buf, len);
  92. break;
  93. case DP_STDERR:
  94. tputs(STDERR, buf, len);
  95. break;
  96. #ifdef LEAF
  97. case DP_SERVER:
  98. case DP_HELP:
  99. case DP_MODE:
  100. case DP_MODE_NEXT:
  101. case DP_SERVER_NEXT:
  102. case DP_HELP_NEXT:
  103. queue_server(idx, buf, len);
  104. break;
  105. #endif /* LEAF */
  106. }
  107. return;
  108. } else { /* normal chat text */
  109. if (coloridx(idx)) {
  110. static int cflags;
  111. int schar = 0;
  112. char buf3[1024] = "", buf2[1024] = "", c = 0;
  113. for (size_t i = 0 ; i < len ; i++) {
  114. c = buf[i];
  115. buf2[0] = 0;
  116. if (schar) { /* These are for $X replacements */
  117. schar--; /* Unset identifier int */
  118. switch (c) {
  119. case 'b':
  120. if (cflags & CFLGS_BOLD) {
  121. sprintf(buf2, "%s", BOLD_END(idx));
  122. cflags &= ~CFLGS_BOLD;
  123. } else {
  124. cflags |= CFLGS_BOLD;
  125. sprintf(buf2, "%s", BOLD(idx));
  126. }
  127. break;
  128. case 'u':
  129. if (cflags & CFLGS_UNDERLINE) {
  130. sprintf(buf2, "%s", UNDERLINE_END(idx));
  131. cflags &= ~CFLGS_UNDERLINE;
  132. } else {
  133. sprintf(buf2, "%s", UNDERLINE(idx));
  134. cflags |= CFLGS_UNDERLINE;
  135. }
  136. break;
  137. case 'f':
  138. if (cflags & CFLGS_FLASH) {
  139. sprintf(buf2, "%s", FLASH_END(idx));
  140. cflags &= ~CFLGS_FLASH;
  141. } else {
  142. sprintf(buf2, "%s", FLASH(idx));
  143. cflags |= CFLGS_FLASH;
  144. }
  145. break;
  146. default:
  147. sprintf(buf2, "$%c", c); /* No identifier, put the '$' back in */
  148. break;
  149. }
  150. } else { /* These are character replacements */
  151. switch (c) {
  152. case '$':
  153. schar++;
  154. break;
  155. case ':':
  156. sprintf(buf2, "%s%c%s", LIGHTGREY(idx), c, COLOR_END(idx));
  157. break;
  158. case '@':
  159. sprintf(buf2, "%s%c%s", BOLD(idx), c, BOLD_END(idx));
  160. break;
  161. case '>':
  162. case ')':
  163. case '<':
  164. case '(':
  165. sprintf(buf2, "%s%c%s", GREEN(idx), c, COLOR_END(idx));
  166. break;
  167. default:
  168. sprintf(buf2, "%c", c);
  169. break;
  170. }
  171. }
  172. sprintf(buf3, "%s%s", (buf3 && buf3[0]) ? buf3 : "", (buf2 && buf2[0]) ? buf2 : "");
  173. }
  174. buf3[strlen(buf3)] = 0;
  175. strcpy(buf, buf3);
  176. }
  177. buf[sizeof(buf) - 1] = 0;
  178. len = strlen(buf);
  179. if (len > 1000) { /* Truncate to fit */
  180. buf[1000] = 0;
  181. strcat(buf, "\n");
  182. len = 1001;
  183. }
  184. if (dcc[idx].simul > 0 && !dcc[idx].msgc) {
  185. bounce_simul(idx, buf);
  186. } else if (dcc[idx].msgc > 0) {
  187. size_t size = strlen(dcc[idx].simulbot) + strlen(buf) + 20;
  188. char *ircbuf = (char *) calloc(1, size);
  189. egg_snprintf(ircbuf, size, "PRIVMSG %s :%s", dcc[idx].simulbot, buf);
  190. tputs(dcc[idx].sock, ircbuf, strlen(ircbuf));
  191. free(ircbuf);
  192. } else {
  193. if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
  194. char *p = add_cr(buf);
  195. tputs(dcc[idx].sock, p, strlen(p));
  196. } else if (dcc[idx].type && dcc[idx].type->output) {
  197. dcc[idx].type->output(idx, buf, dcc[idx].u.other);
  198. } else {
  199. tputs(dcc[idx].sock, buf, len);
  200. }
  201. }
  202. }
  203. }
  204. void chatout(const char *format, ...)
  205. {
  206. char s[1025] = "", *p = NULL;
  207. va_list va;
  208. va_start(va, format);
  209. egg_vsnprintf(s, 1024, format, va);
  210. va_end(va);
  211. if ((p = strrchr(s, '\n')))
  212. *p++ = 0;
  213. for (int i = 0; i < dcc_total; i++)
  214. if ((dcc[i].type == &DCC_CHAT) && !(dcc[i].simul))
  215. if (dcc[i].u.chat->channel >= 0)
  216. dprintf(i, "%s\n", s);
  217. }
  218. /* Print to all on this channel but one.
  219. */
  220. void chanout_but(int x, int chan, const char *format, ...)
  221. {
  222. char s[1025] = "", *p = NULL;
  223. va_list va;
  224. va_start(va, format);
  225. egg_vsnprintf(s, 1024, format, va);
  226. va_end(va);
  227. if ((p = strrchr(s, '\n')))
  228. *p = 0;
  229. for (int i = 0; i < dcc_total; i++)
  230. if ((dcc[i].type == &DCC_CHAT) && (i != x) && !(dcc[i].simul))
  231. if (dcc[i].u.chat->channel == chan)
  232. dprintf(i, "%s\n", s);
  233. }
  234. void dcc_chatter(int idx)
  235. {
  236. int i;
  237. int j;
  238. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  239. strcpy(dcc[idx].u.chat->con_chan, "***");
  240. check_bind_chon(dcc[idx].nick, idx);
  241. dprintf(idx, "Connected to %s, running %s\n", conf.bot->nick, version);
  242. show_banner(idx); /* check STAT_BANNER inside function */
  243. get_user_flagrec(dcc[idx].user, &fr, NULL);
  244. if ((dcc[idx].status & STAT_BOTS) && glob_master(fr)) {
  245. if ((tands+1) > 1)
  246. dprintf(idx, "There are %s-%d- bots%s currently linked.\n", BOLD(idx), tands + 1, BOLD_END(idx));
  247. else
  248. dprintf(idx, "There is %s-%d- bot%s currently linked.\n", BOLD(idx), tands + 1, BOLD_END(idx));
  249. dprintf(idx, " \n");
  250. }
  251. if (dcc[idx].status & STAT_WHOM) {
  252. answer_local_whom(idx, -1);
  253. dprintf(idx, " \n");
  254. }
  255. if (dcc[idx].status & STAT_CHANNELS) {
  256. show_channels(idx, NULL);
  257. dprintf(idx, " \n");
  258. }
  259. show_motd(idx);
  260. notes_chon(idx);
  261. if (glob_party(fr)) {
  262. i = dcc[idx].u.chat->channel;
  263. } else {
  264. dprintf(idx, "You don't have partyline chat access; commands only.\n\n");
  265. i = -1;
  266. }
  267. j = dcc[idx].sock;
  268. dcc[idx].u.chat->channel = 234567;
  269. /* Still there? */
  270. if ((idx >= dcc_total) || (dcc[idx].sock != j))
  271. return; /* Nope */
  272. if (dcc[idx].type == &DCC_CHAT) {
  273. if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
  274. strcpy(dcc[idx].u.chat->con_chan, "*");
  275. if (dcc[idx].u.chat->channel == 234567) {
  276. /* If the chat channel has already been altered it's *highly*
  277. * probably join/part messages have been broadcast everywhere,
  278. * so dont bother sending them
  279. */
  280. if (i == -2)
  281. i = 0;
  282. dcc[idx].u.chat->channel = i;
  283. if (dcc[idx].u.chat->channel >= 0) {
  284. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  285. botnet_send_join_idx(idx);
  286. }
  287. }
  288. }
  289. /* But *do* bother with sending it locally */
  290. if (!dcc[idx].u.chat->channel) {
  291. chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
  292. } else if (dcc[idx].u.chat->channel > 0) {
  293. chanout_but(-1, dcc[idx].u.chat->channel, "*** %s joined the channel.\n", dcc[idx].nick);
  294. }
  295. }
  296. }
  297. /* Mark an entry as lost and deconstruct it's contents. It will be securely
  298. * removed from the dcc list in the main loop.
  299. */
  300. void lostdcc(int n)
  301. {
  302. sdprintf("lostdcc(%d)", n);
  303. /* Make sure it's a valid dcc index. */
  304. if (n < 0 || n >= max_dcc)
  305. return;
  306. if (dcc[n].type && dcc[n].type->kill)
  307. dcc[n].type->kill(n, dcc[n].u.other);
  308. else if (dcc[n].u.other)
  309. free(dcc[n].u.other);
  310. egg_bzero(&dcc[n], sizeof(struct dcc_t));
  311. dcc[n].sock = -1;
  312. dcc[n].type = &DCC_LOST;
  313. }
  314. /* Remove entry from dcc list. Think twice before using this function,
  315. * because it invalidates any variables that point to a specific dcc
  316. * entry!
  317. *
  318. * Note: The entry will be deconstructed if it was not deconstructed
  319. * already. This case should normally not occur.
  320. */
  321. void removedcc(int n)
  322. {
  323. sdprintf("removedcc(%d)", n);
  324. if (dcc[n].type && dcc[n].type->kill)
  325. dcc[n].type->kill(n, dcc[n].u.other);
  326. else if (dcc[n].u.other)
  327. free(dcc[n].u.other);
  328. dcc_total--;
  329. if (n < dcc_total) {
  330. egg_memcpy(&dcc[n], &dcc[dcc_total], sizeof(struct dcc_t));
  331. sdprintf("idx: %d -> %d", dcc_total, n);
  332. } else
  333. egg_bzero(&dcc[n], sizeof(struct dcc_t)); /* drummer */
  334. }
  335. /* Clean up sockets that were just left for dead.
  336. */
  337. void dcc_remove_lost(void)
  338. {
  339. for (int i = 0; i < dcc_total; i++) {
  340. if (dcc[i].type == &DCC_LOST) {
  341. dcc[i].type = NULL;
  342. dcc[i].sock = -1;
  343. removedcc(i);
  344. i--;
  345. }
  346. }
  347. #ifdef LEAF
  348. /* check if any of our idx's moved. */
  349. if (serv >= 0 && (servidx >= 0 && servidx < dcc_total && dcc[servidx].sock != serv) || (servidx >= dcc_total)) {
  350. sdprintf("changing serv: %d servidx: %d to ...", serv, servidx);
  351. servidx = findanyidx(serv);
  352. sdprintf("... serv: %d servidx: %d", serv, servidx);
  353. }
  354. if (dns_sock >= 0 && (dns_idx >= 0 && dns_idx < dcc_total && dcc[dns_idx].sock != dns_sock) || (dns_idx >= dcc_total)) {
  355. sdprintf("changing dns_sock: %d dns_idx: %d to ...", dns_sock, dns_idx);
  356. dns_idx = findanyidx(dns_sock);
  357. sdprintf("... dns_sock: %d dns_idx: %d", dns_sock, dns_idx);
  358. }
  359. #endif /* LEAF */
  360. }
  361. /* Show list of current dcc's to a dcc-chatter
  362. * positive value: idx given -- negative value: sock given
  363. */
  364. void tell_dcc(int idx)
  365. {
  366. int i;
  367. size_t j, nicklen = 0;
  368. char other[160] = "", format[81] = "";
  369. /* calculate max nicklen */
  370. for (i = 0; i < dcc_total; i++) {
  371. if(strlen(dcc[i].nick) > (unsigned) nicklen)
  372. nicklen = strlen(dcc[i].nick);
  373. }
  374. if(nicklen < 9)
  375. nicklen = 9;
  376. egg_snprintf(format, sizeof format, "%%-4s %%-4s %%-8s %%-5s %%-%us %%-40s %%s\n", nicklen);
  377. dprintf(idx, format, "SOCK", "IDX", "ADDR", "PORT", "NICK", "HOST", "TYPE");
  378. dprintf(idx, format, "----", "---", "--------", "-----", "---------",
  379. "----------------------------------------", "----");
  380. egg_snprintf(format, sizeof format, "%%-4d %%-4d %%08X %%5ud %%-%us %%-40s %%s\n", nicklen);
  381. dprintf(idx, "dns_idx: %d, servidx: %d\n", dns_idx, servidx);
  382. for (i = 0; i < dcc_total; i++) {
  383. j = strlen(dcc[i].host);
  384. if (j > 40)
  385. j -= 40;
  386. else
  387. j = 0;
  388. if (dcc[i].type && dcc[i].type->display)
  389. dcc[i].type->display(i, other);
  390. else {
  391. sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
  392. break;
  393. }
  394. if (dcc[i].type == &DCC_LOST)
  395. dprintf(idx, "LOST:\n");
  396. dprintf(idx, format, dcc[i].sock, i, dcc[i].addr, dcc[i].port, dcc[i].nick, dcc[i].host + j, other);
  397. }
  398. }
  399. /* Mark someone on dcc chat as no longer away
  400. */
  401. void not_away(int idx)
  402. {
  403. if (dcc[idx].u.chat->away == NULL) {
  404. dprintf(idx, "You weren't away!\n");
  405. return;
  406. }
  407. if (dcc[idx].u.chat->channel >= 0) {
  408. chanout_but(-1, dcc[idx].u.chat->channel,
  409. "*** %s is no longer away.\n", dcc[idx].nick);
  410. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  411. botnet_send_away(-1, conf.bot->nick, dcc[idx].sock, NULL, idx);
  412. }
  413. }
  414. dprintf(idx, "You're not away any more.\n");
  415. free(dcc[idx].u.chat->away);
  416. dcc[idx].u.chat->away = NULL;
  417. check_bind_away(conf.bot->nick, idx, NULL);
  418. }
  419. void set_away(int idx, char *s)
  420. {
  421. if (s == NULL) {
  422. not_away(idx);
  423. return;
  424. }
  425. if (!s[0]) {
  426. not_away(idx);
  427. return;
  428. }
  429. if (dcc[idx].u.chat->away != NULL)
  430. free(dcc[idx].u.chat->away);
  431. dcc[idx].u.chat->away = strdup(s);
  432. if (dcc[idx].u.chat->channel >= 0) {
  433. chanout_but(-1, dcc[idx].u.chat->channel, "*** %s is now away: %s\n", dcc[idx].nick, s);
  434. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  435. botnet_send_away(-1, conf.bot->nick, dcc[idx].sock, s, idx);
  436. }
  437. }
  438. dprintf(idx, "You are now away. (%s)\n", s);
  439. check_bind_away(conf.bot->nick, idx, s);
  440. }
  441. /* Make a password, 10-14 random letters and digits
  442. */
  443. void makepass(char *s)
  444. {
  445. make_rand_str(s, 10 + randint(5));
  446. }
  447. void flush_lines(int idx, struct chat_info *ci)
  448. {
  449. int c = ci->line_count;
  450. struct msgq *p = ci->buffer, *o;
  451. while (p && c < (ci->max_line)) {
  452. ci->current_lines--;
  453. tputs(dcc[idx].sock, p->msg, p->len);
  454. free(p->msg);
  455. o = p->next;
  456. free(p);
  457. p = o;
  458. c++;
  459. }
  460. if (p != NULL) {
  461. if (dcc[idx].status & STAT_TELNET)
  462. tputs(dcc[idx].sock, "[More]: ", 8);
  463. else
  464. tputs(dcc[idx].sock, "[More]\n", 7);
  465. }
  466. ci->buffer = p;
  467. ci->line_count = 0;
  468. }
  469. int new_dcc(struct dcc_table *type, int xtra_size)
  470. {
  471. int i = dcc_total;
  472. if (dcc_total == max_dcc)
  473. return -1;
  474. dcc_total++;
  475. egg_bzero((char *) &dcc[i], sizeof(struct dcc_t));
  476. dcc[i].type = type;
  477. if (xtra_size)
  478. dcc[i].u.other = (char *) calloc(1, xtra_size);
  479. sdprintf("new_dcc: %d (dcc_total: %d)", i, dcc_total);
  480. return i;
  481. }
  482. /* Changes the given dcc entry to another type.
  483. */
  484. void changeover_dcc(int i, struct dcc_table *type, int xtra_size)
  485. {
  486. /* Free old structure. */
  487. if (dcc[i].type && dcc[i].type->kill)
  488. dcc[i].type->kill(i, dcc[i].u.other);
  489. else if (dcc[i].u.other) {
  490. free(dcc[i].u.other);
  491. dcc[i].u.other = NULL;
  492. }
  493. dcc[i].type = type;
  494. if (xtra_size)
  495. dcc[i].u.other = (char *) calloc(1, xtra_size);
  496. }
  497. int detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
  498. {
  499. if (!dcc_flood_thr)
  500. return 0;
  501. time_t t = now;
  502. if (*timer != t) {
  503. *timer = t;
  504. chat->msgs_per_sec = 0;
  505. } else {
  506. chat->msgs_per_sec++;
  507. if (chat->msgs_per_sec > dcc_flood_thr) {
  508. /* FLOOD */
  509. dprintf(idx, "*** FLOOD: %s.\n", IRC_GOODBYE);
  510. /* Evil assumption here that flags&DCT_CHAT implies chat type */
  511. if ((dcc[idx].type->flags & DCT_CHAT) && chat &&
  512. (chat->channel >= 0)) {
  513. char x[1024];
  514. egg_snprintf(x, sizeof x, DCC_FLOODBOOT, dcc[idx].nick);
  515. chanout_but(idx, chat->channel, "*** %s", x);
  516. if (chat->channel < GLOBAL_CHANS)
  517. botnet_send_part_idx(idx, x);
  518. }
  519. check_bind_chof(dcc[idx].nick, idx);
  520. if ((dcc[idx].sock != STDOUT) || backgrd) {
  521. killsock(dcc[idx].sock);
  522. lostdcc(idx);
  523. } else {
  524. dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
  525. dcc_chatter(idx);
  526. }
  527. return 1; /* <- flood */
  528. }
  529. }
  530. return 0;
  531. }
  532. /* Handle someone being booted from dcc chat.
  533. */
  534. void do_boot(int idx, char *by, char *reason)
  535. {
  536. int files = (dcc[idx].type != &DCC_CHAT);
  537. dprintf(idx, DCC_BOOTED1);
  538. dprintf(idx, DCC_BOOTED2, files ? "file section" : "bot", by, reason[0] ? ": " : ".", reason);
  539. /* If it's a partyliner (chatterer :) */
  540. /* Horrible assumption that DCT_CHAT using structure uses same format
  541. * as DCC_CHAT */
  542. if ((dcc[idx].type->flags & DCT_CHAT) && (dcc[idx].u.chat->channel >= 0)) {
  543. char x[1024] = "";
  544. egg_snprintf(x, sizeof x, DCC_BOOTED3, by, dcc[idx].nick, reason[0] ? ": " : "", reason);
  545. chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
  546. if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
  547. botnet_send_part_idx(idx, x);
  548. }
  549. check_bind_chof(dcc[idx].nick, idx);
  550. if ((dcc[idx].sock != STDOUT) || backgrd) {
  551. killsock(dcc[idx].sock);
  552. lostdcc(idx);
  553. /* Entry must remain in the table so it can be logged by the caller */
  554. } else {
  555. dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
  556. dcc_chatter(idx);
  557. }
  558. return;
  559. }
  560. port_t listen_all(port_t lport, bool off)
  561. {
  562. if (!lport)
  563. return 0;
  564. int idx = (-1);
  565. port_t port, realport;
  566. #ifdef USE_IPV6
  567. int i6 = 0;
  568. #endif /* USE_IPV6 */
  569. int i;
  570. struct portmap *pmap = NULL, *pold = NULL;
  571. port = realport = lport;
  572. for (pmap = root; pmap; pold = pmap, pmap = pmap->next)
  573. if (pmap->realport == port) {
  574. port = pmap->mappedto;
  575. break;
  576. }
  577. for (int ii = 0; ii < dcc_total; ii++) {
  578. if ((dcc[ii].type == &DCC_TELNET) && (dcc[ii].port == port)) {
  579. idx = ii;
  580. if (off) {
  581. if (pmap) {
  582. if (pold)
  583. pold->next = pmap->next;
  584. else
  585. root = pmap->next;
  586. free(pmap);
  587. }
  588. #ifdef USE_IPV6
  589. if (sockprotocol(dcc[idx].sock) == AF_INET6)
  590. putlog(LOG_DEBUG, "*", "Closing IPv6 listening port %d", dcc[idx].port);
  591. else
  592. #endif /* USE_IPV6 */
  593. putlog(LOG_DEBUG, "*", "Closing IPv4 listening port %d", dcc[idx].port);
  594. killsock(dcc[idx].sock);
  595. lostdcc(idx);
  596. return idx;
  597. }
  598. }
  599. }
  600. if (idx < 0) {
  601. if (off) {
  602. putlog(LOG_ERRORS, "*", "No such listening port open - %d", lport);
  603. return idx;
  604. }
  605. /* make new one */
  606. if (dcc_total >= max_dcc) {
  607. putlog(LOG_ERRORS, "*", "Can't open listening port - no more DCC Slots");
  608. } else {
  609. #ifdef USE_IPV6
  610. i6 = open_listen_by_af(&port, AF_INET6);
  611. if (i6 < 0) {
  612. putlog(LOG_ERRORS, "*", "Can't open IPv6 listening port %d - %s", port,
  613. i6 == -1 ? "it's taken." : "couldn't assign ip.");
  614. } else {
  615. idx = new_dcc(&DCC_TELNET, 0);
  616. dcc[idx].addr = 0L;
  617. strcpy(dcc[idx].host6, myipstr(6));
  618. dcc[idx].port = port;
  619. dcc[idx].sock = i6;
  620. dcc[idx].timeval = now;
  621. strcpy(dcc[idx].nick, "(telnet6)");
  622. strcpy(dcc[idx].host, "*");
  623. putlog(LOG_DEBUG, "*", "Listening on IPv6 at telnet port %d", port);
  624. }
  625. i = open_listen_by_af(&port, AF_INET);
  626. #else
  627. i = open_listen(&port);
  628. #endif /* USE_IPV6 */
  629. if (i < 0) {
  630. putlog(LOG_ERRORS, "*", "Can't open IPv4 listening port %d - %s", port,
  631. i == -1 ? "it's taken." : "couldn't assign ip.");
  632. } else {
  633. idx = (-1); /* now setup ipv4 listening port */
  634. idx = new_dcc(&DCC_TELNET, 0);
  635. dcc[idx].addr = iptolong(getmyip());
  636. dcc[idx].port = port;
  637. dcc[idx].sock = i;
  638. dcc[idx].timeval = now;
  639. strcpy(dcc[idx].nick, "(telnet)");
  640. strcpy(dcc[idx].host, "*");
  641. putlog(LOG_DEBUG, "*", "Listening on IPv4 at telnet port %d", port);
  642. }
  643. #ifdef USE_IPV6
  644. if (i > 0 || i6 > 0) {
  645. #else
  646. if (i > 0) {
  647. #endif /* USE_IPV6 */
  648. if (!pmap) {
  649. pmap = (struct portmap *) calloc(1, sizeof(struct portmap));
  650. pmap->next = root;
  651. root = pmap;
  652. }
  653. pmap->realport = realport;
  654. pmap->mappedto = port;
  655. }
  656. }
  657. }
  658. /* if one of the protocols failed, the one which worked will be returned
  659. * if both were successful, it wont matter which idx is returned, because the
  660. * code reading listen_all will only be reading dcc[idx].port, which would be
  661. * open on both protocols.
  662. * -bryan (10/29/03)
  663. */
  664. return idx;
  665. }
  666. void identd_open()
  667. {
  668. int idx;
  669. int i = -1;
  670. port_t port = 113;
  671. for (idx = 0; idx < dcc_total; idx++)
  672. if (dcc[idx].type == &DCC_IDENTD_CONNECT)
  673. return; /* it's already open :) */
  674. idx = -1;
  675. identd_hack = 1;
  676. #ifdef USE_IPV6
  677. i = open_listen_by_af(&port, AF_INET6);
  678. #else
  679. i = open_listen(&port);
  680. #endif /* USE_IPV6 */
  681. identd_hack = 0;
  682. if (i >= 0) {
  683. idx = new_dcc(&DCC_IDENTD_CONNECT, 0);
  684. if (idx >= 0) {
  685. egg_timeval_t howlong;
  686. dcc[idx].addr = iptolong(getmyip());
  687. dcc[idx].port = port;
  688. dcc[idx].sock = i;
  689. dcc[idx].timeval = now;
  690. strcpy(dcc[idx].nick, "(identd)");
  691. strcpy(dcc[idx].host, "*");
  692. putlog(LOG_DEBUG, "*", "Identd daemon started.");
  693. howlong.sec = 15;
  694. howlong.usec = 0;
  695. timer_create(&howlong, "identd_close()", (Function) identd_close);
  696. } else
  697. killsock(i);
  698. }
  699. }
  700. void identd_close()
  701. {
  702. for (int idx = 0; idx < dcc_total; idx++) {
  703. if (dcc[idx].type == &DCC_IDENTD_CONNECT) {
  704. killsock(dcc[idx].sock);
  705. lostdcc(idx);
  706. putlog(LOG_DEBUG, "*", "Identd daemon stopped.");
  707. break;
  708. }
  709. }
  710. }