dccutil.c 29 KB

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