dccutil.c 21 KB

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