dccutil.c 29 KB

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