dccutil.c 21 KB

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