dccutil.c 32 KB

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