dccutil.c 19 KB

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