dccutil.c 20 KB

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