dccutil.c 30 KB

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