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