dccutil.c 30 KB

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