1
0

dccutil.c 20 KB

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