dccutil.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * dccutil.c -- handles:
  22. * lots of little functions to send formatted text to
  23. * varying types of connections
  24. * '.who', '.whom', and '.dccstat' code
  25. * memory management for dcc structures
  26. * timeout checking for dcc connections
  27. *
  28. */
  29. #include <sys/stat.h>
  30. #include <sys/file.h>
  31. #include "common.h"
  32. #include "color.h"
  33. #include "chanprog.h"
  34. #include "userrec.h"
  35. #include "dcc.h"
  36. #include "auth.h"
  37. #include "botnet.h"
  38. #include "adns.h"
  39. #include "net.h"
  40. #include "main.h"
  41. #include "dccutil.h"
  42. #include "misc.h"
  43. #include "botcmd.h"
  44. #include <errno.h>
  45. #include "chan.h"
  46. #include "tandem.h"
  47. #include "core_binds.h"
  48. #include "egg_timer.h"
  49. #include "src/mod/server.mod/server.h"
  50. #include <stdarg.h>
  51. static struct portmap *root = NULL;
  52. interval_t connect_timeout = 40; /* How long to wait before a telnet connection times out */
  53. int max_dcc = 200;
  54. static int dcc_flood_thr = 3;
  55. void
  56. init_dcc()
  57. {
  58. if (!conf.bot->hub)
  59. protect_telnet = 0;
  60. if (max_dcc < 1)
  61. max_dcc = 1;
  62. if (dcc)
  63. dcc = (struct dcc_t *) my_realloc(dcc, sizeof(struct dcc_t) * max_dcc);
  64. else
  65. dcc = (struct dcc_t *) my_calloc(1, sizeof(struct dcc_t) * max_dcc);
  66. }
  67. /* Replace \n with \r\n */
  68. char *
  69. add_cr(char *buf)
  70. {
  71. static char WBUF[1024] = "";
  72. char *p = NULL, *q = NULL;
  73. for (p = buf, q = WBUF; *p; p++, q++) {
  74. if (*p == '\n')
  75. *q++ = '\r';
  76. *q = *p;
  77. }
  78. *q = *p;
  79. return WBUF;
  80. }
  81. static size_t
  82. colorbuf(char *buf, size_t len, int idx, size_t bufsiz)
  83. {
  84. // char *buf = *bufp;
  85. int cidx = coloridx(idx);
  86. static int cflags;
  87. int schar = 0;
  88. char buf3[1024] = "", buf2[15] = "", c = 0;
  89. for (size_t i = 0; i < len; i++) {
  90. c = buf[i];
  91. buf2[0] = 0;
  92. /*
  93. if (aqua) {
  94. if (upper) {
  95. upper = 0;
  96. c = toupper(c);
  97. } else {
  98. upper = 1;
  99. c = tolower(c);
  100. }
  101. }
  102. */
  103. if (cidx) {
  104. if (schar) { /* These are for $X replacements */
  105. schar--; /* Unset identifier int */
  106. switch (c) {
  107. case 'b':
  108. if (cflags & CFLGS_BOLD) {
  109. strlcpy(buf2, BOLD_END(idx), sizeof(buf2));
  110. cflags &= ~CFLGS_BOLD;
  111. } else {
  112. cflags |= CFLGS_BOLD;
  113. strlcpy(buf2, BOLD(idx), sizeof(buf2));
  114. }
  115. break;
  116. case 'u':
  117. if (cflags & CFLGS_UNDERLINE) {
  118. strlcpy(buf2, UNDERLINE_END(idx), sizeof(buf2));
  119. cflags &= ~CFLGS_UNDERLINE;
  120. } else {
  121. strlcpy(buf2, UNDERLINE(idx), sizeof(buf2));
  122. cflags |= CFLGS_UNDERLINE;
  123. }
  124. break;
  125. case 'f':
  126. if (cflags & CFLGS_FLASH) {
  127. strlcpy(buf2, FLASH_END(idx), sizeof(buf2));
  128. cflags &= ~CFLGS_FLASH;
  129. } else {
  130. strlcpy(buf2, FLASH(idx), sizeof(buf2));
  131. cflags |= CFLGS_FLASH;
  132. }
  133. break;
  134. default:
  135. /* No identifier, put the '$' back in */
  136. buf2[0] = '$';
  137. buf2[1] = c;
  138. buf2[2] = 0;
  139. break;
  140. }
  141. } else { /* These are character replacements */
  142. switch (c) {
  143. case '$':
  144. schar++;
  145. break;
  146. case ':':
  147. simple_snprintf(buf2, sizeof(buf2), "%s%c%s", LIGHTGREY(idx), c, COLOR_END(idx));
  148. break;
  149. case '@':
  150. simple_snprintf(buf2, sizeof(buf2), "%s%c%s", BOLD(idx), c, BOLD_END(idx));
  151. break;
  152. case '>':
  153. case ')':
  154. case '<':
  155. case '(':
  156. simple_snprintf(buf2, sizeof(buf2), "%s%c%s", GREEN(idx), c, COLOR_END(idx));
  157. break;
  158. default:
  159. buf2[0] = c;
  160. buf2[1] = 0;
  161. break;
  162. }
  163. }
  164. } else {
  165. if (schar) {
  166. schar--;
  167. switch (c) {
  168. case 'b':
  169. case 'u':
  170. case 'f':
  171. break;
  172. default:
  173. /* No identifier, put the '$' back in */
  174. buf2[0] = '$';
  175. buf2[1] = c;
  176. buf2[2] = 0;
  177. }
  178. } else {
  179. switch (c) {
  180. case '$':
  181. schar++;
  182. break;
  183. default:
  184. buf2[0] = c;
  185. buf2[1] = 0;
  186. break;
  187. }
  188. }
  189. }
  190. if (buf2[0])
  191. strlcat(buf3, buf2, sizeof(buf3));
  192. }
  193. return strlcpy(buf, buf3, bufsiz);
  194. }
  195. /* Dump a potentially super-long string of text.
  196. */
  197. void dumplots(int idx, const char *prefix, const char *data)
  198. {
  199. if (!*data) {
  200. dprintf(idx, "%s\n", prefix);
  201. return;
  202. }
  203. char *p = (char*)data, *q = NULL, *n = NULL, c = 0;
  204. const size_t max_data_len = 120 - strlen(prefix);
  205. while ((strlen(p) - ansi_len(p)) > max_data_len) {
  206. q = p + max_data_len;
  207. /* Search for embedded linefeed first */
  208. n = strchr(p, '\n');
  209. if (n && n < q) {
  210. /* Great! dump that first line then start over */
  211. *n = 0;
  212. dprintf(idx, "%s%s\n", prefix, p);
  213. *n = '\n';
  214. p = n + 1;
  215. } else {
  216. /* Search backwards for the last space */
  217. while (*q != ' ' && q != p)
  218. q--;
  219. if (q == p)
  220. q = p + max_data_len;
  221. c = *q;
  222. *q = 0;
  223. dprintf(idx, "%s%s\n", prefix, p);
  224. *q = c;
  225. p = q;
  226. if (c == ' ')
  227. p++;
  228. }
  229. }
  230. /* Last trailing bit: split by linefeeds if possible */
  231. n = strchr(p, '\n');
  232. while (n) {
  233. *n = 0;
  234. dprintf(idx, "%s%s\n", prefix, p);
  235. *n = '\n';
  236. p = n + 1;
  237. n = strchr(p, '\n');
  238. }
  239. if (*p)
  240. dprintf(idx, "%s%s\n", prefix, p); /* Last trailing bit */
  241. }
  242. void
  243. dprintf(int idx, const char *format, ...)
  244. {
  245. char buf[1024] = "";
  246. size_t len;
  247. va_list va;
  248. va_start(va, format);
  249. egg_vsnprintf(buf, sizeof(buf), format, va);
  250. va_end(va);
  251. /* We can not use the return value vsnprintf() to determine where
  252. * to null terminate. The C99 standard specifies that vsnprintf()
  253. * shall return the number of bytes that would be written if the
  254. * buffer had been large enough, rather then -1.
  255. */
  256. /* We actually can, since if it's < 0 or >= sizeof(buf), we know it wrote
  257. * sizeof(buf) bytes. But we're not doing that anyway.
  258. */
  259. len = strlen(buf);
  260. /* this is for color on dcc :P */
  261. if (idx < 0) {
  262. tputs(-idx, buf, len);
  263. } else if (idx > 0x7FF0) {
  264. if (idx == DP_STDOUT || idx == DP_STDOUT) {
  265. len = colorbuf(buf, len, -1, sizeof(buf));
  266. }
  267. switch (idx) {
  268. case DP_DEBUG:
  269. sdprintf("%s", buf);
  270. break;
  271. case DP_STDOUT:
  272. tputs(STDOUT, buf, len);
  273. break;
  274. case DP_STDERR:
  275. tputs(STDERR, buf, len);
  276. break;
  277. case DP_SERVER:
  278. case DP_HELP:
  279. case DP_MODE:
  280. case DP_MODE_NEXT:
  281. case DP_SERVER_NEXT:
  282. case DP_HELP_NEXT:
  283. case DP_DUMP:
  284. case DP_CACHE:
  285. if (conf.bot->hub)
  286. break;
  287. len -= remove_crlf_r(buf);
  288. if ((idx == DP_DUMP || (floodless && idx != DP_CACHE))) {
  289. if (serv != -1) {
  290. if (debug_output)
  291. putlog(LOG_SRVOUT, "@", "[d->] %s", buf);
  292. write_to_server(buf, len);
  293. }
  294. } else
  295. queue_server(idx, buf, len);
  296. break;
  297. }
  298. return;
  299. } else { /* normal chat text */
  300. len = colorbuf(buf, len, idx, sizeof(buf));
  301. if (len > 1000) { /* Truncate to fit */
  302. len = 1000;
  303. buf[len - 1] = '\n';
  304. buf[len] = 0;
  305. }
  306. if (dcc[idx].simul >= 0 && !dcc[idx].irc) {
  307. bounce_simul(idx, buf);
  308. } else if (dcc[idx].irc) {
  309. // size_t size = strlen(dcc[idx].simulbot) + strlen(buf) + 20;
  310. // char *ircbuf = (char *) my_calloc(1, size);
  311. // simple_snprintf(ircbuf, size, "PRIVMSG %s :%s", dcc[idx].simulbot, buf);
  312. // tputs(dcc[idx].sock, ircbuf, strlen(ircbuf));
  313. dprintf(DP_HELP, "PRIVMSG %s :%s\n", dcc[idx].simulbot, buf);
  314. // free(ircbuf);
  315. } else {
  316. if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
  317. char *p = add_cr(buf);
  318. tputs(dcc[idx].sock, p, strlen(p));
  319. } else if (dcc[idx].type && dcc[idx].type->output) {
  320. dcc[idx].type->output(idx, buf, dcc[idx].u.other);
  321. } else {
  322. tputs(dcc[idx].sock, buf, len);
  323. }
  324. }
  325. }
  326. }
  327. void
  328. chatout(const char *format, ...)
  329. {
  330. char s[1024] = "", *p = NULL;
  331. va_list va;
  332. va_start(va, format);
  333. egg_vsnprintf(s, sizeof(s), format, va);
  334. va_end(va);
  335. if ((p = strrchr(s, '\n')))
  336. *p++ = 0;
  337. for (int i = 0; i < dcc_total; i++)
  338. if (dcc[i].type && (dcc[i].type == &DCC_CHAT) && (dcc[i].simul == -1))
  339. if (dcc[i].u.chat->channel >= 0)
  340. dprintf(i, "%s\n", s);
  341. }
  342. /* Print to all on this channel but one.
  343. */
  344. void
  345. chanout_but(int x, int chan, const char *format, ...)
  346. {
  347. char s[1024] = "", *p = NULL;
  348. va_list va;
  349. va_start(va, format);
  350. egg_vsnprintf(s, sizeof(s), format, va);
  351. va_end(va);
  352. if ((p = strrchr(s, '\n')))
  353. *p = 0;
  354. for (int i = 0; i < dcc_total; i++)
  355. if (dcc[i].type && (dcc[i].type == &DCC_CHAT) && (i != x) && (dcc[i].simul == -1))
  356. if (dcc[i].u.chat->channel == chan)
  357. dprintf(i, "%s\n", s);
  358. }
  359. void
  360. dcc_chatter(int idx)
  361. {
  362. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  363. get_user_flagrec(dcc[idx].user, &fr, NULL);
  364. if (!glob_party(fr)) {
  365. dcc[idx].u.chat->channel = -1;
  366. dprintf(idx, "You don't have partyline chat access; commands only.\n\n");
  367. }
  368. struct chat_info dummy;
  369. strlcpy(dcc[idx].u.chat->con_chan, "***", sizeof(dummy.con_chan));
  370. check_bind_chon(dcc[idx].nick, idx);
  371. dprintf(idx, "Connected to %s, running %s\n", conf.bot->nick, version);
  372. show_banner(idx); /* check STAT_BANNER inside function */
  373. if ((dcc[idx].status & STAT_BOTS) && glob_master(fr)) {
  374. if ((tands + 1) > 1)
  375. dprintf(idx, "There are %s-%d- bots%s currently linked.\n", BOLD(idx), tands + 1, BOLD_END(idx));
  376. else
  377. dprintf(idx, "There is %s-%d- bot%s currently linked.\n", BOLD(idx), tands + 1, BOLD_END(idx));
  378. dprintf(idx, " \n");
  379. }
  380. if (dcc[idx].status & STAT_WHOM) {
  381. answer_local_whom(idx, -1);
  382. dprintf(idx, " \n");
  383. }
  384. if (dcc[idx].status & STAT_CHANNELS) {
  385. show_channels(idx, NULL);
  386. dprintf(idx, " \n");
  387. }
  388. show_motd(idx);
  389. if (dcc[idx].type == &DCC_CHAT) {
  390. if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
  391. strlcpy(dcc[idx].u.chat->con_chan, "*", sizeof(dummy.con_chan));
  392. if (dcc[idx].u.chat->channel == -2)
  393. dcc[idx].u.chat->channel = 0;
  394. if (dcc[idx].u.chat->channel >= 0) {
  395. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  396. botnet_send_join_idx(idx);
  397. }
  398. }
  399. /* But *do* bother with sending it locally */
  400. if (!dcc[idx].u.chat->channel) {
  401. chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
  402. } else if (dcc[idx].u.chat->channel > 0) {
  403. chanout_but(-1, dcc[idx].u.chat->channel, "*** %s joined the channel.\n", dcc[idx].nick);
  404. }
  405. }
  406. }
  407. int
  408. dcc_read(FILE *f, bool enc)
  409. {
  410. char inbuf[1024] = "", *type = NULL, *buf = NULL, *buf_ptr = NULL;
  411. int idx = -1;
  412. bool isserv = 0;
  413. const char salt1[] = SALT1;
  414. while (fgets(inbuf, sizeof(inbuf), f) != NULL) {
  415. remove_crlf(inbuf);
  416. if (enc)
  417. buf = buf_ptr = decrypt_string(salt1, inbuf);
  418. else
  419. buf = inbuf;
  420. if (!strcmp(buf, "+dcc")) {
  421. if (enc)
  422. free(buf_ptr);
  423. return idx;
  424. }
  425. type = newsplit(&buf);
  426. if (!strcmp(type, "type")) {
  427. struct dcc_table *dcc_type = NULL;
  428. size_t dcc_size = 0;
  429. // if (!strcmp(buf, "CHAT"))
  430. // dcc_type = &DCC_CHAT;
  431. if (!strcmp(buf, "SERVER")) {
  432. dcc_type = &SERVER_SOCKET;
  433. isserv = 1;
  434. }
  435. // if (!strcmp(buf, "BOT"))
  436. // dcc_type = &DCC_BOT;
  437. if (dcc_type) {
  438. idx = new_dcc(dcc_type, dcc_size);
  439. if (isserv)
  440. servidx = idx;
  441. }
  442. }
  443. if (idx >= 0) {
  444. if (!strcmp(type, "addr"))
  445. dcc[idx].addr = my_atoul(buf);
  446. if (!strcmp(type, "sock")) {
  447. dcc[idx].sock = atoi(buf);
  448. if (isserv)
  449. serv = dcc[idx].sock;
  450. }
  451. if (!strcmp(type, "port"))
  452. dcc[idx].port = atoi(buf);
  453. if (!strcmp(type, "nick"))
  454. strlcpy(dcc[idx].nick, buf, NICKLEN);
  455. if (!strcmp(type, "host")) {
  456. strlcpy(dcc[idx].host, buf, UHOSTLEN);
  457. }
  458. }
  459. if (enc)
  460. free(buf_ptr);
  461. }
  462. return -1;
  463. }
  464. void
  465. dcc_write(FILE *f, int idx)
  466. {
  467. if (dcc[idx].sock > 0) {
  468. lfprintf(f, "-dcc\n");
  469. if (dcc[idx].type)
  470. lfprintf(f, "type %s\n", dcc[idx].type->name);
  471. // if (user)
  472. // lfprintf(f, "user %s\n", dcc[idx].user->handle);
  473. if (dcc[idx].addr)
  474. lfprintf(f, "addr %u\n", dcc[idx].addr);
  475. if (dcc[idx].status)
  476. lfprintf(f, "status %lu\n", dcc[idx].status);
  477. lfprintf(f, "sock %d\n", dcc[idx].sock);
  478. // lfprintf(f, "simul %d\n", dcc[idx].simul);
  479. if (dcc[idx].port)
  480. lfprintf(f, "port %d\n", dcc[idx].port);
  481. if (dcc[idx].nick[0])
  482. lfprintf(f, "nick %s\n", dcc[idx].nick);
  483. if (dcc[idx].host[0])
  484. lfprintf(f, "host %s\n", dcc[idx].host);
  485. lfprintf(f, "+dcc\n");
  486. }
  487. }
  488. /* Mark an entry as lost and deconstruct it's contents. It will be securely
  489. * removed from the dcc list in the main loop.
  490. */
  491. void
  492. lostdcc(int n)
  493. {
  494. sdprintf("lostdcc(%d)", n);
  495. /* Make sure it's a valid dcc index. */
  496. if (n < 0 || n >= max_dcc)
  497. return;
  498. if (n == uplink_idx)
  499. uplink_idx = -1;
  500. if (dcc[n].type && dcc[n].type->kill)
  501. dcc[n].type->kill(n, dcc[n].u.other);
  502. else if (dcc[n].u.other)
  503. free(dcc[n].u.other);
  504. dcc[n].u.other = NULL;
  505. // 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
  506. // parsed by anything.
  507. // egg_bzero(&dcc[n], sizeof(struct dcc_t));
  508. dcc[n].sock = -1;
  509. dcc[n].type = NULL;
  510. dccn--;
  511. /* last entry! make table smaller :) */
  512. if (n == (dcc_total - 1))
  513. dcc_total--;
  514. }
  515. /* Show list of current dcc's to a dcc-chatter
  516. * positive value: idx given -- negative value: sock given
  517. */
  518. void
  519. tell_dcc(int idx)
  520. {
  521. int i;
  522. size_t j, nicklen = 0;
  523. char other[160] = "", format[81] = "";
  524. /* calculate max nicklen */
  525. for (i = 0; i < dcc_total; i++) {
  526. if (dcc[i].type && strlen(dcc[i].nick) > (unsigned) nicklen)
  527. nicklen = strlen(dcc[i].nick);
  528. }
  529. if (nicklen < 9)
  530. nicklen = 9;
  531. simple_snprintf(format, sizeof format, "%%-4s %%-4s %%-8s %%-5s %%-%zus %%-40s %%s\n", nicklen);
  532. dprintf(idx, format, "SOCK", "IDX", "ADDR", "PORT", "NICK", "HOST", "TYPE");
  533. dprintf(idx, format, "----", "---", "--------", "-----", "---------",
  534. "----------------------------------------", "----");
  535. simple_snprintf(format, sizeof format, "%%-4d %%-4d %%08X %%5u %%-%zus %%-40s %%s\n", nicklen);
  536. dprintf(idx, "dccn: %d, dcc_total: %d\n", dccn, dcc_total);
  537. dprintf(idx, "dns_idx: %d, servidx: %d\n", dns_idx, servidx);
  538. for (i = 0; i < dcc_total; i++) {
  539. if (dcc[i].type) {
  540. j = strlen(dcc[i].host);
  541. if (j > 40)
  542. j -= 40;
  543. else
  544. j = 0;
  545. if (dcc[i].type && dcc[i].type->display)
  546. dcc[i].type->display(i, other, sizeof(other));
  547. else {
  548. simple_snprintf(other, sizeof(other), "?:%lX !! ERROR !!", (long) dcc[i].type);
  549. break;
  550. }
  551. dprintf(idx, format, dcc[i].sock, i, dcc[i].addr, dcc[i].port, dcc[i].nick, dcc[i].host + j, other);
  552. }
  553. }
  554. }
  555. /* Mark someone on dcc chat as no longer away
  556. */
  557. void
  558. not_away(int idx)
  559. {
  560. if (dcc[idx].u.chat->away == NULL) {
  561. dprintf(idx, "You weren't away!\n");
  562. return;
  563. }
  564. if (dcc[idx].u.chat->channel >= 0) {
  565. chanout_but(-1, dcc[idx].u.chat->channel, "*** %s is no longer away.\n", dcc[idx].nick);
  566. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  567. botnet_send_away(-1, conf.bot->nick, dcc[idx].sock, NULL, idx);
  568. }
  569. }
  570. dprintf(idx, "You're not away any more.\n");
  571. free(dcc[idx].u.chat->away);
  572. dcc[idx].u.chat->away = NULL;
  573. }
  574. void
  575. set_away(int idx, char *s)
  576. {
  577. if (s == NULL) {
  578. not_away(idx);
  579. return;
  580. }
  581. if (!s[0]) {
  582. not_away(idx);
  583. return;
  584. }
  585. if (dcc[idx].u.chat->away != NULL)
  586. free(dcc[idx].u.chat->away);
  587. dcc[idx].u.chat->away = strdup(s);
  588. if (dcc[idx].u.chat->channel >= 0) {
  589. chanout_but(-1, dcc[idx].u.chat->channel, "*** %s is now away: %s\n", dcc[idx].nick, s);
  590. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  591. botnet_send_away(-1, conf.bot->nick, dcc[idx].sock, s, idx);
  592. }
  593. }
  594. dprintf(idx, "You are now away. (%s)\n", s);
  595. }
  596. /* Make a password, 10-14 random letters and digits
  597. */
  598. void
  599. makepass(char *s)
  600. {
  601. make_rand_str(s, 10 + randint(5));
  602. }
  603. void
  604. flush_lines(int idx, struct chat_info *ci)
  605. {
  606. int c = ci->line_count;
  607. struct msgq *p = ci->buffer, *o;
  608. while (p && c < (ci->max_line)) {
  609. ci->current_lines--;
  610. tputs(dcc[idx].sock, p->msg, p->len);
  611. free(p->msg);
  612. o = p->next;
  613. free(p);
  614. p = o;
  615. c++;
  616. }
  617. if (p != NULL) {
  618. if (dcc[idx].status & STAT_TELNET)
  619. tputs(dcc[idx].sock, "[More]: ", 8);
  620. else
  621. tputs(dcc[idx].sock, "[More]\n", 7);
  622. }
  623. ci->buffer = p;
  624. ci->line_count = 0;
  625. }
  626. int
  627. new_dcc(struct dcc_table *type, int xtra_size)
  628. {
  629. if (!type)
  630. return -1;
  631. if (dcc_total == max_dcc)
  632. return -1;
  633. int i = 0;
  634. /* Find the first gap */
  635. for (i = 0; i <= dcc_total; i++)
  636. if (!dcc[i].type)
  637. break;
  638. /* we managed to get to the end of the list! */
  639. if (i == dcc_total) {
  640. i = dcc_total;
  641. dcc_total++;
  642. }
  643. dccn++;
  644. /* empty out the memory for the entry */
  645. egg_bzero((char *) &dcc[i], sizeof(struct dcc_t));
  646. dcc[i].type = type;
  647. if (xtra_size)
  648. dcc[i].u.other = (char *) my_calloc(1, xtra_size);
  649. else
  650. dcc[i].u.other = NULL;
  651. dcc[i].simul = -1;
  652. dcc[i].sock = -1;
  653. sdprintf("new_dcc (%s): %d (dccn/dcc_total: %d/%d)", type->name, i, dccn, dcc_total);
  654. return i;
  655. }
  656. /* Changes the given dcc entry to another type.
  657. */
  658. void
  659. changeover_dcc(int i, struct dcc_table *type, int xtra_size)
  660. {
  661. /* Free old structure. */
  662. if (dcc[i].type && dcc[i].type->kill)
  663. dcc[i].type->kill(i, dcc[i].u.other);
  664. else if (dcc[i].u.other) {
  665. free(dcc[i].u.other);
  666. dcc[i].u.other = NULL;
  667. }
  668. dcc[i].type = type;
  669. if (xtra_size)
  670. dcc[i].u.other = (char *) my_calloc(1, xtra_size);
  671. }
  672. int
  673. detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
  674. {
  675. if (!dcc_flood_thr)
  676. return 0;
  677. time_t t = now;
  678. if (*timer != t) {
  679. *timer = t;
  680. chat->msgs_per_sec = 0;
  681. } else {
  682. chat->msgs_per_sec++;
  683. if (chat->msgs_per_sec > dcc_flood_thr) {
  684. /* FLOOD */
  685. dprintf(idx, "*** FLOOD: Goodbye.\n");
  686. /* Evil assumption here that flags&DCT_CHAT implies chat type */
  687. if ((dcc[idx].type->flags & DCT_CHAT) && chat && (chat->channel >= 0)) {
  688. char x[1024];
  689. simple_snprintf(x, sizeof x, "%s has been forcibly removed for flooding.\n", dcc[idx].nick);
  690. chanout_but(idx, chat->channel, "*** %s", x);
  691. if (chat->channel < GLOBAL_CHANS)
  692. botnet_send_part_idx(idx, x);
  693. }
  694. check_bind_chof(dcc[idx].nick, idx);
  695. if ((dcc[idx].sock != STDOUT) || backgrd) {
  696. killsock(dcc[idx].sock);
  697. lostdcc(idx);
  698. } else {
  699. dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
  700. dcc_chatter(idx);
  701. }
  702. return 1; /* <- flood */
  703. }
  704. }
  705. return 0;
  706. }
  707. /* Handle someone being booted from dcc chat.
  708. */
  709. void
  710. do_boot(int idx, const char *by, const char *reason)
  711. {
  712. dprintf(idx, "-=- poof -=-\n");
  713. dprintf(idx, "You've been booted from the bot by %s%s%s\n", by, reason[0] ? ": " : ".", reason);
  714. /* If it's a partyliner (chatterer :) */
  715. /* Horrible assumption that DCT_CHAT using structure uses same format
  716. * as DCC_CHAT */
  717. if ((dcc[idx].type->flags & DCT_CHAT) && (dcc[idx].u.chat->channel >= 0)) {
  718. char x[1024] = "";
  719. simple_snprintf(x, sizeof x, "%s booted %s from the party line%s%s", by, dcc[idx].nick, reason[0] ? ": " : "", reason);
  720. chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
  721. if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
  722. botnet_send_part_idx(idx, x);
  723. }
  724. check_bind_chof(dcc[idx].nick, idx);
  725. if (dcc[idx].u.chat->su_nick) {
  726. dcc[idx].user = get_user_by_handle(userlist, dcc[idx].u.chat->su_nick);
  727. strlcpy(dcc[idx].nick, dcc[idx].u.chat->su_nick, NICKLEN);
  728. dcc[idx].type = &DCC_CHAT;
  729. dprintf(idx, "Returning to real nick %s!\n", dcc[idx].u.chat->su_nick);
  730. free(dcc[idx].u.chat->su_nick);
  731. dcc[idx].u.chat->su_nick = NULL;
  732. dcc_chatter(idx);
  733. if (dcc[idx].u.chat->channel < GLOBAL_CHANS && dcc[idx].u.chat->channel >= 0) {
  734. botnet_send_join_idx(idx);
  735. }
  736. } else if ((dcc[idx].sock != STDOUT) || backgrd) {
  737. killsock(dcc[idx].sock);
  738. lostdcc(idx);
  739. /* Entry must remain in the table so it can be logged by the caller */
  740. } else {
  741. dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
  742. dcc_chatter(idx);
  743. }
  744. return;
  745. }
  746. int
  747. listen_all(port_t lport, bool off)
  748. {
  749. int idx = -1;
  750. port_t port, realport;
  751. #ifdef USE_IPV6
  752. int i6 = 0;
  753. #endif /* USE_IPV6 */
  754. int i = 0, ii = 0;
  755. struct portmap *pmap = NULL, *pold = NULL;
  756. port = realport = lport;
  757. for (pmap = root; pmap; pold = pmap, pmap = pmap->next)
  758. if (pmap->realport == port) {
  759. port = pmap->mappedto;
  760. break;
  761. }
  762. for (ii = 0; ii < dcc_total; ii++) {
  763. if (dcc[ii].type && (dcc[ii].type == &DCC_TELNET) && (dcc[ii].port == port)) {
  764. idx = ii;
  765. if (off) {
  766. if (pmap) {
  767. if (pold)
  768. pold->next = pmap->next;
  769. else
  770. root = pmap->next;
  771. free(pmap);
  772. }
  773. #ifdef USE_IPV6
  774. if (sockprotocol(dcc[idx].sock) == AF_INET6)
  775. putlog(LOG_DEBUG, "*", "Closing IPv6 listening port %d", dcc[idx].port);
  776. else
  777. #endif /* USE_IPV6 */
  778. putlog(LOG_DEBUG, "*", "Closing IPv4 listening port %d", dcc[idx].port);
  779. killsock(dcc[idx].sock);
  780. lostdcc(idx);
  781. return idx;
  782. }
  783. }
  784. }
  785. if (idx < 0) {
  786. if (off) {
  787. putlog(LOG_ERRORS, "*", "No such listening port open - %d", lport);
  788. return idx;
  789. }
  790. /* make new one */
  791. if (dcc_total >= max_dcc) {
  792. putlog(LOG_ERRORS, "*", "Can't open listening port - no more DCC Slots");
  793. } else {
  794. #ifdef USE_IPV6
  795. i6 = open_listen_by_af(&port, AF_INET6);
  796. if (i6 < 0) {
  797. putlog(LOG_ERRORS, "*", "Can't open IPv6 listening port %d - %s", port,
  798. i6 == -1 ? "it's taken." : "couldn't assign ip.");
  799. } else {
  800. /* now setup ipv4/ipv6 listening port */
  801. idx = new_dcc(&DCC_TELNET, 0);
  802. dcc[idx].addr = 0L;
  803. strlcpy(dcc[idx].host6, myipstr(AF_INET6), sizeof(dcc[idx].host6));
  804. dcc[idx].port = port;
  805. dcc[idx].sock = i6;
  806. dcc[idx].timeval = now;
  807. strlcpy(dcc[idx].nick, "(telnet6)", NICKLEN);
  808. strlcpy(dcc[idx].host, "*", UHOSTLEN);
  809. putlog(LOG_DEBUG, "*", "Listening on IPv6 at telnet port %d", port);
  810. }
  811. i = open_listen_by_af(&port, AF_INET);
  812. #else
  813. i = open_listen(&port);
  814. #endif /* USE_IPV6 */
  815. if (i < 0) {
  816. #ifdef USE_IPV6
  817. if (i6 < 0)
  818. #endif /* USE_IPV6 */
  819. putlog(LOG_ERRORS, "*", "Can't open IPv4 listening port %d - %s", port,
  820. i == -1 ? "it's taken." : "couldn't assign ip.");
  821. } else {
  822. /* now setup ipv4 listening port */
  823. idx = new_dcc(&DCC_TELNET, 0);
  824. dcc[idx].addr = iptolong(getmyip());
  825. dcc[idx].port = port;
  826. dcc[idx].sock = i;
  827. dcc[idx].timeval = now;
  828. strlcpy(dcc[idx].nick, "(telnet)", NICKLEN);
  829. strlcpy(dcc[idx].host, "*", UHOSTLEN);
  830. putlog(LOG_DEBUG, "*", "Listening on IPv4 at telnet port %d", port);
  831. }
  832. #ifdef USE_IPV6
  833. if (i > 0 || i6 > 0) {
  834. #else
  835. if (i > 0) {
  836. #endif /* USE_IPV6 */
  837. if (!pmap) {
  838. pmap = (struct portmap *) my_calloc(1, sizeof(struct portmap));
  839. pmap->next = root;
  840. root = pmap;
  841. }
  842. pmap->realport = realport;
  843. pmap->mappedto = port;
  844. }
  845. }
  846. }
  847. /* if one of the protocols failed, the one which worked will be returned
  848. * if both were successful, it wont matter which idx is returned, because the
  849. * code reading listen_all will only be reading dcc[idx].port, which would be
  850. * open on both protocols.
  851. * -bryan (10/29/03)
  852. */
  853. return idx;
  854. }
  855. void
  856. identd_open(const char *sourceIp, const char *destIp)
  857. {
  858. int idx;
  859. int i = -1;
  860. port_t port = 113;
  861. for (idx = 0; idx < dcc_total; idx++)
  862. if (dcc[idx].type == &DCC_IDENTD_CONNECT)
  863. return; /* it's already open :) */
  864. idx = -1;
  865. identd_hack = 1;
  866. #ifdef USE_IPV6
  867. i = open_listen_by_af(&port, AF_INET6);
  868. #else
  869. i = open_listen(&port);
  870. #endif /* USE_IPV6 */
  871. identd_hack = 0;
  872. if (i >= 0) {
  873. idx = new_dcc(&DCC_IDENTD_CONNECT, 0);
  874. if (idx >= 0) {
  875. egg_timeval_t howlong;
  876. dcc[idx].addr = iptolong(getmyip());
  877. dcc[idx].port = port;
  878. dcc[idx].sock = i;
  879. dcc[idx].timeval = now;
  880. strlcpy(dcc[idx].nick, "(identd)", NICKLEN);
  881. strlcpy(dcc[idx].host, "*", UHOSTLEN);
  882. putlog(LOG_DEBUG, "*", "Identd daemon started.");
  883. howlong.sec = 15;
  884. howlong.usec = 0;
  885. timer_create(&howlong, "identd_close()", (Function) identd_close);
  886. } else
  887. killsock(i);
  888. }
  889. /* Only makes sense if we're spoofing by nick */
  890. if (conf.homedir && oidentd && ident_botnick) {
  891. char oidentd_conf[1024] = "";
  892. simple_snprintf(oidentd_conf, sizeof(oidentd_conf), "%s/.oidentd.conf", conf.homedir);
  893. sdprintf("Attempting to spoof ident with oidentd (%s)", oidentd_conf);
  894. FILE *f = NULL;
  895. /* Wait for any locks to finish up */
  896. while ((f = fopen(oidentd_conf, "a+")) == NULL)
  897. ;
  898. if (f) {
  899. flock(fileno(f), LOCK_EX);
  900. fseek(f, 0, SEEK_SET);
  901. char inbuf[100] = "", outbuf[2048] = "";
  902. /* Clear any of my matching ips and username */
  903. while (fgets(inbuf, sizeof(inbuf), f) != NULL) {
  904. if(inbuf[0] == '\n') continue;
  905. char *line = strdup(inbuf), *p = line;
  906. newsplit(&line); /* to */
  907. newsplit(&line); /* ip */
  908. if (!strcmp(newsplit(&line), "from") && !strcmp(newsplit(&line), sourceIp)) {
  909. free(p);
  910. continue;
  911. }
  912. char reply[100] = "";
  913. simple_snprintf(reply, sizeof(reply), "reply \"%s\"", origbotname);
  914. if (strstr(line, reply)) {
  915. free(p);
  916. continue;
  917. }
  918. strlcat(outbuf, inbuf, sizeof(outbuf));
  919. free(p);
  920. }
  921. ftruncate(fileno(f), 0);
  922. fwrite(outbuf, 1, strlen(outbuf), f);
  923. //And make a record in the oidentd.conf to spoof the ident request from this port->dest-prot
  924. fprintf(f, "to %s from %s { reply \"%s\" }\n", destIp, sourceIp, origbotname);
  925. fflush(f);
  926. flock(fileno(f), LOCK_UN);
  927. fclose(f);
  928. }
  929. }
  930. }
  931. void
  932. identd_close()
  933. {
  934. for (int idx = 0; idx < dcc_total; idx++) {
  935. if (dcc[idx].type == &DCC_IDENTD_CONNECT) {
  936. killsock(dcc[idx].sock);
  937. lostdcc(idx);
  938. putlog(LOG_DEBUG, "*", "Identd daemon stopped.");
  939. break;
  940. }
  941. }
  942. }
  943. bool
  944. valid_idx(int idx)
  945. {
  946. if ((idx == -1) || (idx >= dcc_total) || (!dcc[idx].type))
  947. return 0;
  948. return 1;
  949. }
  950. int check_cmd_pass(const char *cmd, char *pass)
  951. {
  952. struct cmd_pass *cp = NULL;
  953. for (cp = cmdpass; cp; cp = cp->next)
  954. if (!egg_strcasecmp(cmd, cp->name)) {
  955. char *epass = NULL;
  956. /* Does the old pass need to be converted? */
  957. if (strlen(cp->pass) < SHA1_SALTED_LEN) {
  958. char out[MAXPASSLEN + 1] = "", *tmp = encrypt_string(pass, pass);
  959. strlcpy(out, "+", 2);
  960. strlcat(out, tmp, MAXPASSLEN + 1);
  961. out[MAXPASSLEN] = 0;
  962. free(tmp);
  963. /* No match */
  964. if (strcmp(out, cp->pass))
  965. return 0;
  966. /* Successful match on the old version, convert it and save it */
  967. char ctmp[256] = "";
  968. epass = salted_sha1(pass);
  969. simple_snprintf(ctmp, sizeof(ctmp), "%s %s", cmd, epass);
  970. free(epass);
  971. set_cmd_pass(tmp, 1);
  972. return 1;
  973. }
  974. epass = salted_sha1(pass, &(cp->pass)[1]);
  975. if (!strcmp(epass, cp->pass)) {
  976. free(epass);
  977. return 1;
  978. }
  979. free(epass);
  980. return 0;
  981. }
  982. return 0;
  983. }
  984. int has_cmd_pass(const char *cmd)
  985. {
  986. struct cmd_pass *cp = NULL;
  987. for (cp = cmdpass; cp; cp = cp->next)
  988. if (!egg_strcasecmp(cmd, cp->name))
  989. return 1;
  990. return 0;
  991. }
  992. void set_cmd_pass(char *ln, int shareit)
  993. {
  994. struct cmd_pass *cp = NULL;
  995. char *cmd = NULL;
  996. cmd = newsplit(&ln);
  997. for (cp = cmdpass; cp; cp = cp->next)
  998. if (!strcmp(cmd, cp->name))
  999. break;
  1000. if (cp)
  1001. if (ln[0]) {
  1002. /* change */
  1003. strlcpy(cp->pass, ln, sizeof(cp->pass));
  1004. if (shareit)
  1005. botnet_send_cmdpass(-1, cp->name, cp->pass);
  1006. } else {
  1007. if (cp == cmdpass)
  1008. cmdpass = cp->next;
  1009. else {
  1010. struct cmd_pass *cp2;
  1011. cp2 = cmdpass;
  1012. while (cp2->next != cp)
  1013. cp2 = cp2->next;
  1014. cp2->next = cp->next;
  1015. }
  1016. if (shareit)
  1017. botnet_send_cmdpass(-1, cp->name, "");
  1018. free(cp->name);
  1019. free(cp);
  1020. } else if (ln[0]) {
  1021. /* create */
  1022. cp = (struct cmd_pass *) my_calloc(1, sizeof(struct cmd_pass));
  1023. cp->next = cmdpass;
  1024. cmdpass = cp;
  1025. cp->name = strdup(cmd);
  1026. strlcpy(cp->pass, ln, sizeof(cp->pass));
  1027. if (shareit)
  1028. botnet_send_cmdpass(-1, cp->name, cp->pass);
  1029. }
  1030. }
  1031. void cmdpass_free(struct cmd_pass *x)
  1032. {
  1033. struct cmd_pass *cp = NULL, *cp_n = NULL;
  1034. for (cp = x; cp; cp = cp_n) {
  1035. cp_n = cp->next;
  1036. list_delete((struct list_type **) &x, (struct list_type *) cp);
  1037. free(cp->name);
  1038. free(cp);
  1039. }
  1040. }