dccutil.c 32 KB

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