dccutil.c 31 KB

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