dccutil.c 28 KB

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