dccutil.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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 == -1))
  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 == -1))
  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. // This is also done when we new_dcc(), so don't bother for now, we set sock/type to NULL, so it won't even be
  401. // parsed by anything.
  402. // egg_bzero(&dcc[n], sizeof(struct dcc_t));
  403. dcc[n].sock = -1;
  404. dcc[n].type = NULL;
  405. dccn--;
  406. /* last entry! make table smaller :) */
  407. if (n == (dcc_total - 1))
  408. dcc_total--;
  409. }
  410. /* Show list of current dcc's to a dcc-chatter
  411. * positive value: idx given -- negative value: sock given
  412. */
  413. void
  414. tell_dcc(int idx)
  415. {
  416. int i;
  417. size_t j, nicklen = 0;
  418. char other[160] = "", format[81] = "";
  419. /* calculate max nicklen */
  420. for (i = 0; i < dcc_total; i++) {
  421. if (dcc[i].type && strlen(dcc[i].nick) > (unsigned) nicklen)
  422. nicklen = strlen(dcc[i].nick);
  423. }
  424. if (nicklen < 9)
  425. nicklen = 9;
  426. egg_snprintf(format, sizeof format, "%%-4s %%-4s %%-8s %%-5s %%-%us %%-40s %%s\n", nicklen);
  427. dprintf(idx, format, "SOCK", "IDX", "ADDR", "PORT", "NICK", "HOST", "TYPE");
  428. dprintf(idx, format, "----", "---", "--------", "-----", "---------",
  429. "----------------------------------------", "----");
  430. egg_snprintf(format, sizeof format, "%%-4d %%-4d %%08X %%5ud %%-%us %%-40s %%s\n", nicklen);
  431. dprintf(idx, "dccn: %d, dcc_total: %d\n", dccn, dcc_total);
  432. dprintf(idx, "dns_idx: %d, servidx: %d\n", dns_idx, servidx);
  433. for (i = 0; i < dcc_total; i++) {
  434. if (dcc[i].type) {
  435. j = strlen(dcc[i].host);
  436. if (j > 40)
  437. j -= 40;
  438. else
  439. j = 0;
  440. if (dcc[i].type && dcc[i].type->display)
  441. dcc[i].type->display(i, other);
  442. else {
  443. sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
  444. break;
  445. }
  446. dprintf(idx, format, dcc[i].sock, i, dcc[i].addr, dcc[i].port, dcc[i].nick, dcc[i].host + j, other);
  447. }
  448. }
  449. }
  450. /* Mark someone on dcc chat as no longer away
  451. */
  452. void
  453. not_away(int idx)
  454. {
  455. if (dcc[idx].u.chat->away == NULL) {
  456. dprintf(idx, "You weren't away!\n");
  457. return;
  458. }
  459. if (dcc[idx].u.chat->channel >= 0) {
  460. chanout_but(-1, dcc[idx].u.chat->channel, "*** %s is no longer away.\n", dcc[idx].nick);
  461. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  462. botnet_send_away(-1, conf.bot->nick, dcc[idx].sock, NULL, idx);
  463. }
  464. }
  465. dprintf(idx, "You're not away any more.\n");
  466. free(dcc[idx].u.chat->away);
  467. dcc[idx].u.chat->away = NULL;
  468. check_bind_away(conf.bot->nick, idx, NULL);
  469. }
  470. void
  471. set_away(int idx, char *s)
  472. {
  473. if (s == NULL) {
  474. not_away(idx);
  475. return;
  476. }
  477. if (!s[0]) {
  478. not_away(idx);
  479. return;
  480. }
  481. if (dcc[idx].u.chat->away != NULL)
  482. free(dcc[idx].u.chat->away);
  483. dcc[idx].u.chat->away = strdup(s);
  484. if (dcc[idx].u.chat->channel >= 0) {
  485. chanout_but(-1, dcc[idx].u.chat->channel, "*** %s is now away: %s\n", dcc[idx].nick, s);
  486. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  487. botnet_send_away(-1, conf.bot->nick, dcc[idx].sock, s, idx);
  488. }
  489. }
  490. dprintf(idx, "You are now away. (%s)\n", s);
  491. check_bind_away(conf.bot->nick, idx, s);
  492. }
  493. /* Make a password, 10-14 random letters and digits
  494. */
  495. void
  496. makepass(char *s)
  497. {
  498. make_rand_str(s, 10 + randint(5));
  499. }
  500. void
  501. flush_lines(int idx, struct chat_info *ci)
  502. {
  503. int c = ci->line_count;
  504. struct msgq *p = ci->buffer, *o;
  505. while (p && c < (ci->max_line)) {
  506. ci->current_lines--;
  507. tputs(dcc[idx].sock, p->msg, p->len);
  508. free(p->msg);
  509. o = p->next;
  510. free(p);
  511. p = o;
  512. c++;
  513. }
  514. if (p != NULL) {
  515. if (dcc[idx].status & STAT_TELNET)
  516. tputs(dcc[idx].sock, "[More]: ", 8);
  517. else
  518. tputs(dcc[idx].sock, "[More]\n", 7);
  519. }
  520. ci->buffer = p;
  521. ci->line_count = 0;
  522. }
  523. int
  524. new_dcc(struct dcc_table *type, int xtra_size)
  525. {
  526. if (dcc_total == max_dcc)
  527. return -1;
  528. int i = 0;
  529. /* Find the first gap */
  530. for (i = 0; i <= dcc_total; i++)
  531. if (!dcc[i].type)
  532. break;
  533. /* we managed to get to the end of the list! */
  534. if (i == dcc_total) {
  535. i = dcc_total;
  536. dcc_total++;
  537. }
  538. dccn++;
  539. /* empty out the memory for the entry */
  540. egg_bzero((char *) &dcc[i], sizeof(struct dcc_t));
  541. dcc[i].type = type;
  542. if (xtra_size)
  543. dcc[i].u.other = (char *) my_calloc(1, xtra_size);
  544. dcc[i].simul = -1;
  545. dcc[i].sock = -1;
  546. sdprintf("new_dcc (%s): %d (dccn/dcc_total: %d/%d)", type->name, i, dccn, dcc_total);
  547. return i;
  548. }
  549. /* Changes the given dcc entry to another type.
  550. */
  551. void
  552. changeover_dcc(int i, struct dcc_table *type, int xtra_size)
  553. {
  554. /* Free old structure. */
  555. if (dcc[i].type && dcc[i].type->kill)
  556. dcc[i].type->kill(i, dcc[i].u.other);
  557. else if (dcc[i].u.other) {
  558. free(dcc[i].u.other);
  559. dcc[i].u.other = NULL;
  560. }
  561. dcc[i].type = type;
  562. if (xtra_size)
  563. dcc[i].u.other = (char *) my_calloc(1, xtra_size);
  564. }
  565. int
  566. detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
  567. {
  568. if (!dcc_flood_thr)
  569. return 0;
  570. time_t t = now;
  571. if (*timer != t) {
  572. *timer = t;
  573. chat->msgs_per_sec = 0;
  574. } else {
  575. chat->msgs_per_sec++;
  576. if (chat->msgs_per_sec > dcc_flood_thr) {
  577. /* FLOOD */
  578. dprintf(idx, "*** FLOOD: Goodbye.\n");
  579. /* Evil assumption here that flags&DCT_CHAT implies chat type */
  580. if ((dcc[idx].type->flags & DCT_CHAT) && chat && (chat->channel >= 0)) {
  581. char x[1024];
  582. egg_snprintf(x, sizeof x, DCC_FLOODBOOT, dcc[idx].nick);
  583. chanout_but(idx, chat->channel, "*** %s", x);
  584. if (chat->channel < GLOBAL_CHANS)
  585. botnet_send_part_idx(idx, x);
  586. }
  587. check_bind_chof(dcc[idx].nick, idx);
  588. if ((dcc[idx].sock != STDOUT) || backgrd) {
  589. killsock(dcc[idx].sock);
  590. lostdcc(idx);
  591. } else {
  592. dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
  593. dcc_chatter(idx);
  594. }
  595. return 1; /* <- flood */
  596. }
  597. }
  598. return 0;
  599. }
  600. /* Handle someone being booted from dcc chat.
  601. */
  602. void
  603. do_boot(int idx, char *by, char *reason)
  604. {
  605. int files = (dcc[idx].type != &DCC_CHAT);
  606. dprintf(idx, DCC_BOOTED1);
  607. dprintf(idx, DCC_BOOTED2, files ? "file section" : "bot", by, reason[0] ? ": " : ".", reason);
  608. /* If it's a partyliner (chatterer :) */
  609. /* Horrible assumption that DCT_CHAT using structure uses same format
  610. * as DCC_CHAT */
  611. if ((dcc[idx].type->flags & DCT_CHAT) && (dcc[idx].u.chat->channel >= 0)) {
  612. char x[1024] = "";
  613. egg_snprintf(x, sizeof x, DCC_BOOTED3, by, dcc[idx].nick, reason[0] ? ": " : "", reason);
  614. chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
  615. if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
  616. botnet_send_part_idx(idx, x);
  617. }
  618. check_bind_chof(dcc[idx].nick, idx);
  619. if ((dcc[idx].sock != STDOUT) || backgrd) {
  620. killsock(dcc[idx].sock);
  621. lostdcc(idx);
  622. /* Entry must remain in the table so it can be logged by the caller */
  623. } else {
  624. dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
  625. dcc_chatter(idx);
  626. }
  627. return;
  628. }
  629. port_t
  630. listen_all(port_t lport, bool off)
  631. {
  632. int idx = (-1);
  633. port_t port, realport;
  634. #ifdef USE_IPV6
  635. int i6 = 0;
  636. #endif /* USE_IPV6 */
  637. int i;
  638. struct portmap *pmap = NULL, *pold = NULL;
  639. port = realport = lport;
  640. for (pmap = root; pmap; pold = pmap, pmap = pmap->next)
  641. if (pmap->realport == port) {
  642. port = pmap->mappedto;
  643. break;
  644. }
  645. for (int ii = 0; ii < dcc_total; ii++) {
  646. if (dcc[ii].type && (dcc[ii].type == &DCC_TELNET) && (dcc[ii].port == port)) {
  647. idx = ii;
  648. if (off) {
  649. if (pmap) {
  650. if (pold)
  651. pold->next = pmap->next;
  652. else
  653. root = pmap->next;
  654. free(pmap);
  655. }
  656. #ifdef USE_IPV6
  657. if (sockprotocol(dcc[idx].sock) == AF_INET6)
  658. putlog(LOG_DEBUG, "*", "Closing IPv6 listening port %d", dcc[idx].port);
  659. else
  660. #endif /* USE_IPV6 */
  661. putlog(LOG_DEBUG, "*", "Closing IPv4 listening port %d", dcc[idx].port);
  662. killsock(dcc[idx].sock);
  663. lostdcc(idx);
  664. return idx;
  665. }
  666. }
  667. }
  668. if (idx < 0) {
  669. if (off) {
  670. putlog(LOG_ERRORS, "*", "No such listening port open - %d", lport);
  671. return idx;
  672. }
  673. /* make new one */
  674. if (dcc_total >= max_dcc) {
  675. putlog(LOG_ERRORS, "*", "Can't open listening port - no more DCC Slots");
  676. } else {
  677. #ifdef USE_IPV6
  678. i6 = open_listen_by_af(&port, AF_INET6);
  679. if (i6 < 0) {
  680. putlog(LOG_ERRORS, "*", "Can't open IPv6 listening port %d - %s", port,
  681. i6 == -1 ? "it's taken." : "couldn't assign ip.");
  682. } else {
  683. idx = new_dcc(&DCC_TELNET, 0);
  684. dcc[idx].addr = 0L;
  685. strcpy(dcc[idx].host6, myipstr(6));
  686. dcc[idx].port = port;
  687. dcc[idx].sock = i6;
  688. dcc[idx].timeval = now;
  689. strcpy(dcc[idx].nick, "(telnet6)");
  690. strcpy(dcc[idx].host, "*");
  691. putlog(LOG_DEBUG, "*", "Listening on IPv6 at telnet port %d", port);
  692. }
  693. i = open_listen_by_af(&port, AF_INET);
  694. #else
  695. i = open_listen(&port);
  696. #endif /* USE_IPV6 */
  697. if (i < 0) {
  698. putlog(LOG_ERRORS, "*", "Can't open IPv4 listening port %d - %s", port,
  699. i == -1 ? "it's taken." : "couldn't assign ip.");
  700. } else {
  701. idx = (-1); /* now setup ipv4 listening port */
  702. idx = new_dcc(&DCC_TELNET, 0);
  703. dcc[idx].addr = iptolong(getmyip());
  704. dcc[idx].port = port;
  705. dcc[idx].sock = i;
  706. dcc[idx].timeval = now;
  707. strcpy(dcc[idx].nick, "(telnet)");
  708. strcpy(dcc[idx].host, "*");
  709. putlog(LOG_DEBUG, "*", "Listening on IPv4 at telnet port %d", port);
  710. }
  711. #ifdef USE_IPV6
  712. if (i > 0 || i6 > 0) {
  713. #else
  714. if (i > 0) {
  715. #endif /* USE_IPV6 */
  716. if (!pmap) {
  717. pmap = (struct portmap *) my_calloc(1, sizeof(struct portmap));
  718. pmap->next = root;
  719. root = pmap;
  720. }
  721. pmap->realport = realport;
  722. pmap->mappedto = port;
  723. }
  724. }
  725. }
  726. /* if one of the protocols failed, the one which worked will be returned
  727. * if both were successful, it wont matter which idx is returned, because the
  728. * code reading listen_all will only be reading dcc[idx].port, which would be
  729. * open on both protocols.
  730. * -bryan (10/29/03)
  731. */
  732. return idx;
  733. }
  734. void
  735. identd_open()
  736. {
  737. int idx;
  738. int i = -1;
  739. port_t port = 113;
  740. for (idx = 0; idx < dcc_total; idx++)
  741. if (dcc[idx].type == &DCC_IDENTD_CONNECT)
  742. return; /* it's already open :) */
  743. idx = -1;
  744. identd_hack = 1;
  745. #ifdef USE_IPV6
  746. i = open_listen_by_af(&port, AF_INET6);
  747. #else
  748. i = open_listen(&port);
  749. #endif /* USE_IPV6 */
  750. identd_hack = 0;
  751. if (i >= 0) {
  752. idx = new_dcc(&DCC_IDENTD_CONNECT, 0);
  753. if (idx >= 0) {
  754. egg_timeval_t howlong;
  755. dcc[idx].addr = iptolong(getmyip());
  756. dcc[idx].port = port;
  757. dcc[idx].sock = i;
  758. dcc[idx].timeval = now;
  759. strcpy(dcc[idx].nick, "(identd)");
  760. strcpy(dcc[idx].host, "*");
  761. putlog(LOG_DEBUG, "*", "Identd daemon started.");
  762. howlong.sec = 15;
  763. howlong.usec = 0;
  764. timer_create(&howlong, "identd_close()", (Function) identd_close);
  765. } else
  766. killsock(i);
  767. }
  768. }
  769. void
  770. identd_close()
  771. {
  772. for (int idx = 0; idx < dcc_total; idx++) {
  773. if (dcc[idx].type == &DCC_IDENTD_CONNECT) {
  774. killsock(dcc[idx].sock);
  775. lostdcc(idx);
  776. putlog(LOG_DEBUG, "*", "Identd daemon stopped.");
  777. break;
  778. }
  779. }
  780. }
  781. bool
  782. valid_idx(int idx)
  783. {
  784. if ((idx == -1) || (idx >= dcc_total) || (!dcc[idx].type))
  785. return 0;
  786. return 1;
  787. }