dccutil.c 22 KB

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