dccutil.cc 32 KB

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