dccutil.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 "userrec.h"
  14. #include "dcc.h"
  15. #include "auth.h"
  16. #include "botnet.h"
  17. #include "adns.h"
  18. #include "net.h"
  19. #include "main.h"
  20. #include "dccutil.h"
  21. #include "misc.h"
  22. #include "botcmd.h"
  23. #include <errno.h>
  24. #include "chan.h"
  25. #include "tandem.h"
  26. #include "core_binds.h"
  27. #include "egg_timer.h"
  28. #include "src/mod/server.mod/server.h"
  29. #include "src/mod/notes.mod/notes.h"
  30. #include <stdarg.h>
  31. static struct portmap *root = NULL;
  32. time_t connect_timeout = 40; /* How long to wait before a telnet connection times out */
  33. int max_dcc = 200;
  34. static int dcc_flood_thr = 3;
  35. void
  36. init_dcc()
  37. {
  38. if (!conf.bot->hub)
  39. protect_telnet = 0;
  40. if (max_dcc < 1)
  41. max_dcc = 1;
  42. if (dcc)
  43. dcc = (struct dcc_t *) my_realloc(dcc, sizeof(struct dcc_t) * max_dcc);
  44. else
  45. dcc = (struct dcc_t *) my_calloc(1, sizeof(struct dcc_t) * max_dcc);
  46. }
  47. /* Replace \n with \r\n */
  48. char *
  49. add_cr(char *buf)
  50. {
  51. static char WBUF[1024] = "";
  52. char *p = NULL, *q = NULL;
  53. for (p = buf, q = WBUF; *p; p++, q++) {
  54. if (*p == '\n')
  55. *q++ = '\r';
  56. *q = *p;
  57. }
  58. *q = *p;
  59. return WBUF;
  60. }
  61. static void
  62. colorbuf(char *buf, size_t len, int idx)
  63. {
  64. // char *buf = *bufp;
  65. int cidx = coloridx(idx);
  66. static int cflags;
  67. int schar = 0;
  68. char buf3[1024] = "", buf2[1024] = "", c = 0;
  69. for (size_t i = 0; i < len; i++) {
  70. c = buf[i];
  71. buf2[0] = 0;
  72. if (cidx) {
  73. if (schar) { /* These are for $X replacements */
  74. schar--; /* Unset identifier int */
  75. switch (c) {
  76. case 'b':
  77. if (cflags & CFLGS_BOLD) {
  78. simple_sprintf(buf2, "%s", BOLD_END(idx));
  79. cflags &= ~CFLGS_BOLD;
  80. } else {
  81. cflags |= CFLGS_BOLD;
  82. simple_sprintf(buf2, "%s", BOLD(idx));
  83. }
  84. break;
  85. case 'u':
  86. if (cflags & CFLGS_UNDERLINE) {
  87. simple_sprintf(buf2, "%s", UNDERLINE_END(idx));
  88. cflags &= ~CFLGS_UNDERLINE;
  89. } else {
  90. simple_sprintf(buf2, "%s", UNDERLINE(idx));
  91. cflags |= CFLGS_UNDERLINE;
  92. }
  93. break;
  94. case 'f':
  95. if (cflags & CFLGS_FLASH) {
  96. simple_sprintf(buf2, "%s", FLASH_END(idx));
  97. cflags &= ~CFLGS_FLASH;
  98. } else {
  99. simple_sprintf(buf2, "%s", FLASH(idx));
  100. cflags |= CFLGS_FLASH;
  101. }
  102. break;
  103. default:
  104. simple_sprintf(buf2, "$%c", c); /* No identifier, put the '$' back in */
  105. break;
  106. }
  107. } else { /* These are character replacements */
  108. switch (c) {
  109. case '$':
  110. schar++;
  111. break;
  112. case ':':
  113. simple_sprintf(buf2, "%s%c%s", LIGHTGREY(idx), c, COLOR_END(idx));
  114. break;
  115. case '@':
  116. simple_sprintf(buf2, "%s%c%s", BOLD(idx), c, BOLD_END(idx));
  117. break;
  118. case '>':
  119. case ')':
  120. case '<':
  121. case '(':
  122. simple_sprintf(buf2, "%s%c%s", GREEN(idx), c, COLOR_END(idx));
  123. break;
  124. default:
  125. simple_sprintf(buf2, "%c", c);
  126. break;
  127. }
  128. }
  129. } else {
  130. if (schar) {
  131. schar--;
  132. switch (c) {
  133. case 'b':
  134. case 'u':
  135. case 'f':
  136. break;
  137. default:
  138. simple_sprintf(buf2, "$%c", c); /* No identifier, put the '$' back in */
  139. }
  140. } else {
  141. switch (c) {
  142. case '$':
  143. schar++;
  144. break;
  145. default:
  146. simple_sprintf(buf2, "%c", c);
  147. break;
  148. }
  149. }
  150. }
  151. simple_sprintf(buf3, "%s%s", (buf3 && buf3[0]) ? buf3 : "", (buf2 && buf2[0]) ? buf2 : "");
  152. }
  153. buf3[strlen(buf3)] = 0;
  154. strcpy(buf, buf3);
  155. }
  156. /* Dump a potentially super-long string of text.
  157. */
  158. void dumplots(int idx, const char *prefix, char *data)
  159. {
  160. if (!*data) {
  161. dprintf(idx, "%s\n", prefix);
  162. return;
  163. }
  164. char *p = data, *q = NULL, *n = NULL, c = 0;
  165. const size_t max_data_len = 500 - strlen(prefix);
  166. while (strlen(p) > max_data_len) {
  167. q = p + max_data_len;
  168. /* Search for embedded linefeed first */
  169. n = strchr(p, '\n');
  170. if (n && n < q) {
  171. /* Great! dump that first line then start over */
  172. *n = 0;
  173. dprintf(idx, "%s%s\n", prefix, p);
  174. *n = '\n';
  175. p = n + 1;
  176. } else {
  177. /* Search backwards for the last space */
  178. while (*q != ' ' && q != p)
  179. q--;
  180. if (q == p)
  181. q = p + max_data_len;
  182. c = *q;
  183. *q = 0;
  184. dprintf(idx, "%s%s\n", prefix, p);
  185. *q = c;
  186. p = q;
  187. if (c == ' ')
  188. p++;
  189. }
  190. }
  191. /* Last trailing bit: split by linefeeds if possible */
  192. n = strchr(p, '\n');
  193. while (n) {
  194. *n = 0;
  195. dprintf(idx, "%s%s\n", prefix, p);
  196. *n = '\n';
  197. p = n + 1;
  198. n = strchr(p, '\n');
  199. }
  200. if (*p)
  201. dprintf(idx, "%s%s\n", prefix, p); /* Last trailing bit */
  202. }
  203. void
  204. dprintf(int idx, const char *format, ...)
  205. {
  206. char buf[1024] = "";
  207. size_t len;
  208. va_list va;
  209. va_start(va, format);
  210. egg_vsnprintf(buf, sizeof(buf), format, va);
  211. va_end(va);
  212. /* We can not use the return value vsnprintf() to determine where
  213. * to null terminate. The C99 standard specifies that vsnprintf()
  214. * shall return the number of bytes that would be written if the
  215. * buffer had been large enough, rather then -1.
  216. */
  217. /* We actually can, since if it's < 0 or >= sizeof(buf), we know it wrote
  218. * sizeof(buf) bytes. But we're not doing that anyway.
  219. */
  220. len = strlen(buf);
  221. /* this is for color on dcc :P */
  222. if (idx < 0) {
  223. tputs(-idx, buf, len);
  224. } else if (idx > 0x7FF0) {
  225. switch (idx) {
  226. case DP_LOG:
  227. putlog(LOG_MISC, "*", "%s", buf);
  228. break;
  229. case DP_STDOUT:
  230. tputs(STDOUT, buf, len);
  231. break;
  232. case DP_STDERR:
  233. tputs(STDERR, buf, len);
  234. break;
  235. case DP_SERVER:
  236. case DP_HELP:
  237. case DP_MODE:
  238. case DP_MODE_NEXT:
  239. case DP_SERVER_NEXT:
  240. case DP_HELP_NEXT:
  241. case DP_DUMP:
  242. if (conf.bot->hub)
  243. break;
  244. len -= remove_crlf_r(buf);
  245. if ((idx == DP_DUMP || floodless)) {
  246. if (serv != -1) {
  247. if (debug_output)
  248. putlog(LOG_SRVOUT, "@", "[d->] %s", buf);
  249. write_to_server(buf, len);
  250. }
  251. } else
  252. queue_server(idx, buf, len);
  253. break;
  254. }
  255. return;
  256. } else { /* normal chat text */
  257. colorbuf(buf, len, idx);
  258. buf[sizeof(buf) - 1] = 0;
  259. len = strlen(buf);
  260. if (len > 1000) { /* Truncate to fit */
  261. buf[1000] = 0;
  262. strcat(buf, "\n");
  263. len = 1001;
  264. }
  265. if (dcc[idx].simul >= 0 && !dcc[idx].irc) {
  266. bounce_simul(idx, buf);
  267. } else if (dcc[idx].irc) {
  268. // size_t size = strlen(dcc[idx].simulbot) + strlen(buf) + 20;
  269. // char *ircbuf = (char *) my_calloc(1, size);
  270. // egg_snprintf(ircbuf, size, "PRIVMSG %s :%s", dcc[idx].simulbot, buf);
  271. // tputs(dcc[idx].sock, ircbuf, strlen(ircbuf));
  272. dprintf(DP_HELP, "PRIVMSG %s :%s\n", dcc[idx].simulbot, buf);
  273. // free(ircbuf);
  274. } else {
  275. if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
  276. char *p = add_cr(buf);
  277. tputs(dcc[idx].sock, p, strlen(p));
  278. } else if (dcc[idx].type && dcc[idx].type->output) {
  279. dcc[idx].type->output(idx, buf, dcc[idx].u.other);
  280. } else {
  281. tputs(dcc[idx].sock, buf, len);
  282. }
  283. }
  284. }
  285. }
  286. void
  287. chatout(const char *format, ...)
  288. {
  289. char s[1024] = "", *p = NULL;
  290. va_list va;
  291. va_start(va, format);
  292. egg_vsnprintf(s, sizeof(s), format, va);
  293. va_end(va);
  294. if ((p = strrchr(s, '\n')))
  295. *p++ = 0;
  296. for (int i = 0; i < dcc_total; i++)
  297. if (dcc[i].type && (dcc[i].type == &DCC_CHAT) && (dcc[i].simul == -1))
  298. if (dcc[i].u.chat->channel >= 0)
  299. dprintf(i, "%s\n", s);
  300. }
  301. /* Print to all on this channel but one.
  302. */
  303. void
  304. chanout_but(int x, int chan, const char *format, ...)
  305. {
  306. char s[1024] = "", *p = NULL;
  307. va_list va;
  308. va_start(va, format);
  309. egg_vsnprintf(s, sizeof(s), format, va);
  310. va_end(va);
  311. if ((p = strrchr(s, '\n')))
  312. *p = 0;
  313. for (int i = 0; i < dcc_total; i++)
  314. if (dcc[i].type && (dcc[i].type == &DCC_CHAT) && (i != x) && (dcc[i].simul == -1))
  315. if (dcc[i].u.chat->channel == chan)
  316. dprintf(i, "%s\n", s);
  317. }
  318. void
  319. dcc_chatter(int idx)
  320. {
  321. int i;
  322. int j;
  323. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  324. strcpy(dcc[idx].u.chat->con_chan, "***");
  325. check_bind_chon(dcc[idx].nick, idx);
  326. dprintf(idx, "Connected to %s, running %s\n", conf.bot->nick, version);
  327. show_banner(idx); /* check STAT_BANNER inside function */
  328. get_user_flagrec(dcc[idx].user, &fr, NULL);
  329. if ((dcc[idx].status & STAT_BOTS) && glob_master(fr)) {
  330. if ((tands + 1) > 1)
  331. dprintf(idx, "There are %s-%d- bots%s currently linked.\n", BOLD(idx), tands + 1, BOLD_END(idx));
  332. else
  333. dprintf(idx, "There is %s-%d- bot%s currently linked.\n", BOLD(idx), tands + 1, BOLD_END(idx));
  334. dprintf(idx, " \n");
  335. }
  336. if (dcc[idx].status & STAT_WHOM) {
  337. answer_local_whom(idx, -1);
  338. dprintf(idx, " \n");
  339. }
  340. if (dcc[idx].status & STAT_CHANNELS) {
  341. show_channels(idx, NULL);
  342. dprintf(idx, " \n");
  343. }
  344. show_motd(idx);
  345. notes_chon(idx);
  346. if (glob_party(fr)) {
  347. i = dcc[idx].u.chat->channel;
  348. } else {
  349. dprintf(idx, "You don't have partyline chat access; commands only.\n\n");
  350. i = -1;
  351. }
  352. j = dcc[idx].sock;
  353. dcc[idx].u.chat->channel = 234567;
  354. /* Still there? */
  355. if ((idx >= dcc_total) || (dcc[idx].sock != j))
  356. return; /* Nope */
  357. if (dcc[idx].type == &DCC_CHAT) {
  358. if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
  359. strcpy(dcc[idx].u.chat->con_chan, "*");
  360. if (dcc[idx].u.chat->channel == 234567) {
  361. /* If the chat channel has already been altered it's *highly*
  362. * probably join/part messages have been broadcast everywhere,
  363. * so dont bother sending them
  364. */
  365. if (i == -2)
  366. i = 0;
  367. dcc[idx].u.chat->channel = i;
  368. if (dcc[idx].u.chat->channel >= 0) {
  369. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  370. botnet_send_join_idx(idx);
  371. }
  372. }
  373. }
  374. /* But *do* bother with sending it locally */
  375. if (!dcc[idx].u.chat->channel) {
  376. chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
  377. } else if (dcc[idx].u.chat->channel > 0) {
  378. chanout_but(-1, dcc[idx].u.chat->channel, "*** %s joined the channel.\n", dcc[idx].nick);
  379. }
  380. }
  381. }
  382. void trim_dcclist(int top_index)
  383. {
  384. dcc_total = top_index + 1;
  385. }
  386. int
  387. dcc_read(FILE *f)
  388. {
  389. char inbuf[1024] = "", *type = NULL, *buf = NULL;
  390. int idx = -1;
  391. bool isserv = 0;
  392. while (fgets(inbuf, sizeof(inbuf), f) != NULL) {
  393. remove_crlf(inbuf);
  394. buf = inbuf;
  395. if (!strcmp(buf, "+dcc"))
  396. return idx;
  397. type = newsplit(&buf);
  398. if (!strcmp(type, "type")) {
  399. struct dcc_table *dcc_type = NULL;
  400. size_t dcc_size = 0;
  401. // if (!strcmp(buf, "CHAT"))
  402. // dcc_type = &DCC_CHAT;
  403. if (!strcmp(buf, "SERVER")) {
  404. dcc_type = &SERVER_SOCKET;
  405. isserv = 1;
  406. }
  407. // if (!strcmp(buf, "BOT"))
  408. // dcc_type = &DCC_BOT;
  409. if (dcc_type) {
  410. idx = new_dcc(dcc_type, dcc_size);
  411. if (isserv)
  412. servidx = idx;
  413. }
  414. }
  415. if (idx >= 0) {
  416. if (!strcmp(type, "addr"))
  417. dcc[idx].addr = my_atoul(buf);
  418. if (!strcmp(type, "sock")) {
  419. dcc[idx].sock = atoi(buf);
  420. if (isserv)
  421. serv = dcc[idx].sock;
  422. }
  423. if (!strcmp(type, "port"))
  424. dcc[idx].port = atoi(buf);
  425. if (!strcmp(type, "nick"))
  426. strlcpy(dcc[idx].nick, buf, NICKLEN);
  427. if (!strcmp(type, "host")) {
  428. strlcpy(dcc[idx].host, buf, UHOSTLEN);
  429. }
  430. }
  431. }
  432. return -1;
  433. }
  434. void
  435. dcc_write(FILE *f, int idx)
  436. {
  437. if (dcc[idx].sock > 0) {
  438. fprintf(f, "-dcc\n");
  439. if (dcc[idx].type)
  440. fprintf(f, "type %s\n", dcc[idx].type->name);
  441. // if (user)
  442. // fprintf(f, "user %s\n", dcc[idx].user->handle);
  443. if (dcc[idx].addr)
  444. fprintf(f, "addr %u\n", dcc[idx].addr);
  445. if (dcc[idx].status)
  446. fprintf(f, "status %lu\n", dcc[idx].status);
  447. fprintf(f, "sock %d\n", dcc[idx].sock);
  448. // fprintf(f, "simul %d\n", dcc[idx].simul);
  449. if (dcc[idx].port)
  450. fprintf(f, "port %d\n", dcc[idx].port);
  451. if (dcc[idx].nick[0])
  452. fprintf(f, "nick %s\n", dcc[idx].nick);
  453. if (dcc[idx].host[0])
  454. fprintf(f, "host %s\n", dcc[idx].host);
  455. fprintf(f, "+dcc\n");
  456. }
  457. }
  458. /* Mark an entry as lost and deconstruct it's contents. It will be securely
  459. * removed from the dcc list in the main loop.
  460. */
  461. void
  462. lostdcc(int n)
  463. {
  464. sdprintf("lostdcc(%d)", n);
  465. /* Make sure it's a valid dcc index. */
  466. if (n < 0 || n >= max_dcc)
  467. return;
  468. if (dcc[n].type && dcc[n].type->kill)
  469. dcc[n].type->kill(n, dcc[n].u.other);
  470. else if (dcc[n].u.other)
  471. free(dcc[n].u.other);
  472. dcc[n].u.other = NULL;
  473. // This is also done when we new_dcc(), so don't bother for now, we set sock/type to NULL, so it won't even be
  474. // parsed by anything.
  475. // egg_bzero(&dcc[n], sizeof(struct dcc_t));
  476. dcc[n].sock = -1;
  477. dcc[n].type = NULL;
  478. dccn--;
  479. /* last entry! make table smaller :) */
  480. if (n == (dcc_total - 1))
  481. dcc_total--;
  482. }
  483. /* Show list of current dcc's to a dcc-chatter
  484. * positive value: idx given -- negative value: sock given
  485. */
  486. void
  487. tell_dcc(int idx)
  488. {
  489. int i;
  490. size_t j, nicklen = 0;
  491. char other[160] = "", format[81] = "";
  492. /* calculate max nicklen */
  493. for (i = 0; i < dcc_total; i++) {
  494. if (dcc[i].type && strlen(dcc[i].nick) > (unsigned) nicklen)
  495. nicklen = strlen(dcc[i].nick);
  496. }
  497. if (nicklen < 9)
  498. nicklen = 9;
  499. egg_snprintf(format, sizeof format, "%%-4s %%-4s %%-8s %%-5s %%-%us %%-40s %%s\n", nicklen);
  500. dprintf(idx, format, "SOCK", "IDX", "ADDR", "PORT", "NICK", "HOST", "TYPE");
  501. dprintf(idx, format, "----", "---", "--------", "-----", "---------",
  502. "----------------------------------------", "----");
  503. egg_snprintf(format, sizeof format, "%%-4d %%-4d %%08X %%5u %%-%us %%-40s %%s\n", nicklen);
  504. dprintf(idx, "dccn: %d, dcc_total: %d\n", dccn, dcc_total);
  505. dprintf(idx, "dns_idx: %d, servidx: %d\n", dns_idx, servidx);
  506. for (i = 0; i < dcc_total; i++) {
  507. if (dcc[i].type) {
  508. j = strlen(dcc[i].host);
  509. if (j > 40)
  510. j -= 40;
  511. else
  512. j = 0;
  513. if (dcc[i].type && dcc[i].type->display)
  514. dcc[i].type->display(i, other);
  515. else {
  516. sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
  517. break;
  518. }
  519. dprintf(idx, format, dcc[i].sock, i, dcc[i].addr, dcc[i].port, dcc[i].nick, dcc[i].host + j, other);
  520. }
  521. }
  522. }
  523. /* Mark someone on dcc chat as no longer away
  524. */
  525. void
  526. not_away(int idx)
  527. {
  528. if (dcc[idx].u.chat->away == NULL) {
  529. dprintf(idx, "You weren't away!\n");
  530. return;
  531. }
  532. if (dcc[idx].u.chat->channel >= 0) {
  533. chanout_but(-1, dcc[idx].u.chat->channel, "*** %s is no longer away.\n", dcc[idx].nick);
  534. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  535. botnet_send_away(-1, conf.bot->nick, dcc[idx].sock, NULL, idx);
  536. }
  537. }
  538. dprintf(idx, "You're not away any more.\n");
  539. free(dcc[idx].u.chat->away);
  540. dcc[idx].u.chat->away = NULL;
  541. check_bind_away(conf.bot->nick, idx, NULL);
  542. }
  543. void
  544. set_away(int idx, char *s)
  545. {
  546. if (s == NULL) {
  547. not_away(idx);
  548. return;
  549. }
  550. if (!s[0]) {
  551. not_away(idx);
  552. return;
  553. }
  554. if (dcc[idx].u.chat->away != NULL)
  555. free(dcc[idx].u.chat->away);
  556. dcc[idx].u.chat->away = strdup(s);
  557. if (dcc[idx].u.chat->channel >= 0) {
  558. chanout_but(-1, dcc[idx].u.chat->channel, "*** %s is now away: %s\n", dcc[idx].nick, s);
  559. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  560. botnet_send_away(-1, conf.bot->nick, dcc[idx].sock, s, idx);
  561. }
  562. }
  563. dprintf(idx, "You are now away. (%s)\n", s);
  564. check_bind_away(conf.bot->nick, idx, s);
  565. }
  566. /* Make a password, 10-14 random letters and digits
  567. */
  568. void
  569. makepass(char *s)
  570. {
  571. make_rand_str(s, 10 + randint(5));
  572. }
  573. void
  574. flush_lines(int idx, struct chat_info *ci)
  575. {
  576. int c = ci->line_count;
  577. struct msgq *p = ci->buffer, *o;
  578. while (p && c < (ci->max_line)) {
  579. ci->current_lines--;
  580. tputs(dcc[idx].sock, p->msg, p->len);
  581. free(p->msg);
  582. o = p->next;
  583. free(p);
  584. p = o;
  585. c++;
  586. }
  587. if (p != NULL) {
  588. if (dcc[idx].status & STAT_TELNET)
  589. tputs(dcc[idx].sock, "[More]: ", 8);
  590. else
  591. tputs(dcc[idx].sock, "[More]\n", 7);
  592. }
  593. ci->buffer = p;
  594. ci->line_count = 0;
  595. }
  596. int
  597. new_dcc(struct dcc_table *type, int xtra_size)
  598. {
  599. if (!type)
  600. return -1;
  601. if (dcc_total == max_dcc)
  602. return -1;
  603. int i = 0;
  604. /* Find the first gap */
  605. for (i = 0; i <= dcc_total; i++)
  606. if (!dcc[i].type)
  607. break;
  608. /* we managed to get to the end of the list! */
  609. if (i == dcc_total) {
  610. i = dcc_total;
  611. dcc_total++;
  612. }
  613. dccn++;
  614. /* empty out the memory for the entry */
  615. egg_bzero((char *) &dcc[i], sizeof(struct dcc_t));
  616. dcc[i].type = type;
  617. if (xtra_size)
  618. dcc[i].u.other = (char *) my_calloc(1, xtra_size);
  619. else
  620. dcc[i].u.other = NULL;
  621. dcc[i].simul = -1;
  622. dcc[i].sock = -1;
  623. sdprintf("new_dcc (%s): %d (dccn/dcc_total: %d/%d)", type->name, i, dccn, dcc_total);
  624. return i;
  625. }
  626. /* Changes the given dcc entry to another type.
  627. */
  628. void
  629. changeover_dcc(int i, struct dcc_table *type, int xtra_size)
  630. {
  631. /* Free old structure. */
  632. if (dcc[i].type && dcc[i].type->kill)
  633. dcc[i].type->kill(i, dcc[i].u.other);
  634. else if (dcc[i].u.other) {
  635. free(dcc[i].u.other);
  636. dcc[i].u.other = NULL;
  637. }
  638. dcc[i].type = type;
  639. if (xtra_size)
  640. dcc[i].u.other = (char *) my_calloc(1, xtra_size);
  641. }
  642. int
  643. detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
  644. {
  645. if (!dcc_flood_thr)
  646. return 0;
  647. time_t t = now;
  648. if (*timer != t) {
  649. *timer = t;
  650. chat->msgs_per_sec = 0;
  651. } else {
  652. chat->msgs_per_sec++;
  653. if (chat->msgs_per_sec > dcc_flood_thr) {
  654. /* FLOOD */
  655. dprintf(idx, "*** FLOOD: Goodbye.\n");
  656. /* Evil assumption here that flags&DCT_CHAT implies chat type */
  657. if ((dcc[idx].type->flags & DCT_CHAT) && chat && (chat->channel >= 0)) {
  658. char x[1024];
  659. egg_snprintf(x, sizeof x, DCC_FLOODBOOT, dcc[idx].nick);
  660. chanout_but(idx, chat->channel, "*** %s", x);
  661. if (chat->channel < GLOBAL_CHANS)
  662. botnet_send_part_idx(idx, x);
  663. }
  664. check_bind_chof(dcc[idx].nick, idx);
  665. if ((dcc[idx].sock != STDOUT) || backgrd) {
  666. killsock(dcc[idx].sock);
  667. lostdcc(idx);
  668. } else {
  669. dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
  670. dcc_chatter(idx);
  671. }
  672. return 1; /* <- flood */
  673. }
  674. }
  675. return 0;
  676. }
  677. /* Handle someone being booted from dcc chat.
  678. */
  679. void
  680. do_boot(int idx, char *by, char *reason)
  681. {
  682. int files = (dcc[idx].type != &DCC_CHAT);
  683. dprintf(idx, DCC_BOOTED1);
  684. dprintf(idx, DCC_BOOTED2, files ? "file section" : "bot", by, reason[0] ? ": " : ".", reason);
  685. /* If it's a partyliner (chatterer :) */
  686. /* Horrible assumption that DCT_CHAT using structure uses same format
  687. * as DCC_CHAT */
  688. if ((dcc[idx].type->flags & DCT_CHAT) && (dcc[idx].u.chat->channel >= 0)) {
  689. char x[1024] = "";
  690. egg_snprintf(x, sizeof x, DCC_BOOTED3, by, dcc[idx].nick, reason[0] ? ": " : "", reason);
  691. chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
  692. if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
  693. botnet_send_part_idx(idx, x);
  694. }
  695. check_bind_chof(dcc[idx].nick, idx);
  696. if ((dcc[idx].sock != STDOUT) || backgrd) {
  697. killsock(dcc[idx].sock);
  698. lostdcc(idx);
  699. /* Entry must remain in the table so it can be logged by the caller */
  700. } else {
  701. dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
  702. dcc_chatter(idx);
  703. }
  704. return;
  705. }
  706. port_t
  707. listen_all(port_t lport, bool off)
  708. {
  709. int idx = (-1);
  710. port_t port, realport;
  711. #ifdef USE_IPV6
  712. int i6 = 0;
  713. #endif /* USE_IPV6 */
  714. int i;
  715. struct portmap *pmap = NULL, *pold = NULL;
  716. port = realport = lport;
  717. for (pmap = root; pmap; pold = pmap, pmap = pmap->next)
  718. if (pmap->realport == port) {
  719. port = pmap->mappedto;
  720. break;
  721. }
  722. for (int ii = 0; ii < dcc_total; ii++) {
  723. if (dcc[ii].type && (dcc[ii].type == &DCC_TELNET) && (dcc[ii].port == port)) {
  724. idx = ii;
  725. if (off) {
  726. if (pmap) {
  727. if (pold)
  728. pold->next = pmap->next;
  729. else
  730. root = pmap->next;
  731. free(pmap);
  732. }
  733. #ifdef USE_IPV6
  734. if (sockprotocol(dcc[idx].sock) == AF_INET6)
  735. putlog(LOG_DEBUG, "*", "Closing IPv6 listening port %d", dcc[idx].port);
  736. else
  737. #endif /* USE_IPV6 */
  738. putlog(LOG_DEBUG, "*", "Closing IPv4 listening port %d", dcc[idx].port);
  739. killsock(dcc[idx].sock);
  740. lostdcc(idx);
  741. return idx;
  742. }
  743. }
  744. }
  745. if (idx < 0) {
  746. if (off) {
  747. putlog(LOG_ERRORS, "*", "No such listening port open - %d", lport);
  748. return idx;
  749. }
  750. /* make new one */
  751. if (dcc_total >= max_dcc) {
  752. putlog(LOG_ERRORS, "*", "Can't open listening port - no more DCC Slots");
  753. } else {
  754. #ifdef USE_IPV6
  755. i6 = open_listen_by_af(&port, AF_INET6);
  756. if (i6 < 0) {
  757. putlog(LOG_ERRORS, "*", "Can't open IPv6 listening port %d - %s", port,
  758. i6 == -1 ? "it's taken." : "couldn't assign ip.");
  759. } else {
  760. idx = new_dcc(&DCC_TELNET, 0);
  761. dcc[idx].addr = 0L;
  762. strcpy(dcc[idx].host6, myipstr(6));
  763. dcc[idx].port = port;
  764. dcc[idx].sock = i6;
  765. dcc[idx].timeval = now;
  766. strcpy(dcc[idx].nick, "(telnet6)");
  767. strcpy(dcc[idx].host, "*");
  768. putlog(LOG_DEBUG, "*", "Listening on IPv6 at telnet port %d", port);
  769. }
  770. i = open_listen_by_af(&port, AF_INET);
  771. #else
  772. i = open_listen(&port);
  773. #endif /* USE_IPV6 */
  774. if (i < 0) {
  775. putlog(LOG_ERRORS, "*", "Can't open IPv4 listening port %d - %s", port,
  776. i == -1 ? "it's taken." : "couldn't assign ip.");
  777. } else {
  778. idx = (-1); /* now setup ipv4 listening port */
  779. idx = new_dcc(&DCC_TELNET, 0);
  780. dcc[idx].addr = iptolong(getmyip());
  781. dcc[idx].port = port;
  782. dcc[idx].sock = i;
  783. dcc[idx].timeval = now;
  784. strcpy(dcc[idx].nick, "(telnet)");
  785. strcpy(dcc[idx].host, "*");
  786. putlog(LOG_DEBUG, "*", "Listening on IPv4 at telnet port %d", port);
  787. }
  788. #ifdef USE_IPV6
  789. if (i > 0 || i6 > 0) {
  790. #else
  791. if (i > 0) {
  792. #endif /* USE_IPV6 */
  793. if (!pmap) {
  794. pmap = (struct portmap *) my_calloc(1, sizeof(struct portmap));
  795. pmap->next = root;
  796. root = pmap;
  797. }
  798. pmap->realport = realport;
  799. pmap->mappedto = port;
  800. }
  801. }
  802. }
  803. /* if one of the protocols failed, the one which worked will be returned
  804. * if both were successful, it wont matter which idx is returned, because the
  805. * code reading listen_all will only be reading dcc[idx].port, which would be
  806. * open on both protocols.
  807. * -bryan (10/29/03)
  808. */
  809. return idx;
  810. }
  811. void
  812. identd_open()
  813. {
  814. int idx;
  815. int i = -1;
  816. port_t port = 113;
  817. for (idx = 0; idx < dcc_total; idx++)
  818. if (dcc[idx].type == &DCC_IDENTD_CONNECT)
  819. return; /* it's already open :) */
  820. idx = -1;
  821. identd_hack = 1;
  822. #ifdef USE_IPV6
  823. i = open_listen_by_af(&port, AF_INET6);
  824. #else
  825. i = open_listen(&port);
  826. #endif /* USE_IPV6 */
  827. identd_hack = 0;
  828. if (i >= 0) {
  829. idx = new_dcc(&DCC_IDENTD_CONNECT, 0);
  830. if (idx >= 0) {
  831. egg_timeval_t howlong;
  832. dcc[idx].addr = iptolong(getmyip());
  833. dcc[idx].port = port;
  834. dcc[idx].sock = i;
  835. dcc[idx].timeval = now;
  836. strcpy(dcc[idx].nick, "(identd)");
  837. strcpy(dcc[idx].host, "*");
  838. putlog(LOG_DEBUG, "*", "Identd daemon started.");
  839. howlong.sec = 15;
  840. howlong.usec = 0;
  841. timer_create(&howlong, "identd_close()", (Function) identd_close);
  842. } else
  843. killsock(i);
  844. }
  845. }
  846. void
  847. identd_close()
  848. {
  849. for (int idx = 0; idx < dcc_total; idx++) {
  850. if (dcc[idx].type == &DCC_IDENTD_CONNECT) {
  851. killsock(dcc[idx].sock);
  852. lostdcc(idx);
  853. putlog(LOG_DEBUG, "*", "Identd daemon stopped.");
  854. break;
  855. }
  856. }
  857. }
  858. bool
  859. valid_idx(int idx)
  860. {
  861. if ((idx == -1) || (idx >= dcc_total) || (!dcc[idx].type))
  862. return 0;
  863. return 1;
  864. }