dccutil.c 32 KB

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