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