dccutil.c 20 KB

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