dccutil.c 20 KB

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