dccutil.cc 32 KB

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