dccutil.c 22 KB

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