dccutil.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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 "common.h"
  12. #include "color.h"
  13. #include "userrec.h"
  14. #include "dcc.h"
  15. #include "auth.h"
  16. #include "botnet.h"
  17. #include "adns.h"
  18. #include "net.h"
  19. #include "main.h"
  20. #include "dccutil.h"
  21. #include "misc.h"
  22. #include "botcmd.h"
  23. #include <errno.h>
  24. #include "chan.h"
  25. #include "tandem.h"
  26. #include "core_binds.h"
  27. #include "egg_timer.h"
  28. #include "src/mod/server.mod/server.h"
  29. #include "src/mod/notes.mod/notes.h"
  30. #include <stdarg.h>
  31. static struct portmap *root = NULL;
  32. time_t connect_timeout = 40; /* How long to wait before a telnet connection times out */
  33. int max_dcc = 200;
  34. static int dcc_flood_thr = 3;
  35. void
  36. init_dcc()
  37. {
  38. if (!conf.bot->hub)
  39. protect_telnet = 0;
  40. if (max_dcc < 1)
  41. max_dcc = 1;
  42. if (dcc)
  43. dcc = (struct dcc_t *) my_realloc(dcc, sizeof(struct dcc_t) * max_dcc);
  44. else
  45. dcc = (struct dcc_t *) my_calloc(1, sizeof(struct dcc_t) * max_dcc);
  46. }
  47. /* Replace \n with \r\n */
  48. char *
  49. add_cr(char *buf)
  50. {
  51. static char WBUF[1024] = "";
  52. char *p = NULL, *q = NULL;
  53. for (p = buf, q = WBUF; *p; p++, q++) {
  54. if (*p == '\n')
  55. *q++ = '\r';
  56. *q = *p;
  57. }
  58. *q = *p;
  59. return WBUF;
  60. }
  61. static void
  62. colorbuf(char *buf, size_t len, int idx)
  63. {
  64. // char *buf = *bufp;
  65. int cidx = coloridx(idx);
  66. static int cflags;
  67. int schar = 0;
  68. char buf3[1024] = "", buf2[1024] = "", c = 0;
  69. bool upper = 0, aqua = 0;
  70. if (idx != -1 && !egg_strcasecmp(dcc[idx].nick, "aqua"))
  71. aqua = 1;
  72. for (size_t i = 0; i < len; i++) {
  73. c = buf[i];
  74. buf2[0] = 0;
  75. if (aqua) {
  76. if (upper) {
  77. upper = 0;
  78. c = toupper(c);
  79. } else {
  80. upper = 1;
  81. c = tolower(c);
  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_STDOUT:
  244. tputs(STDOUT, buf, len);
  245. break;
  246. case DP_STDERR:
  247. tputs(STDERR, buf, len);
  248. break;
  249. case DP_SERVER:
  250. case DP_HELP:
  251. case DP_MODE:
  252. case DP_MODE_NEXT:
  253. case DP_SERVER_NEXT:
  254. case DP_HELP_NEXT:
  255. case DP_DUMP:
  256. if (conf.bot->hub)
  257. break;
  258. len -= remove_crlf_r(buf);
  259. if ((idx == DP_DUMP || floodless)) {
  260. if (serv != -1) {
  261. if (debug_output)
  262. putlog(LOG_SRVOUT, "@", "[d->] %s", buf);
  263. write_to_server(buf, len);
  264. }
  265. } else
  266. queue_server(idx, buf, len);
  267. break;
  268. }
  269. return;
  270. } else { /* normal chat text */
  271. colorbuf(buf, len, idx);
  272. buf[sizeof(buf) - 1] = 0;
  273. len = strlen(buf);
  274. if (len > 1000) { /* Truncate to fit */
  275. buf[1000] = 0;
  276. strcat(buf, "\n");
  277. len = 1001;
  278. }
  279. if (dcc[idx].simul >= 0 && !dcc[idx].irc) {
  280. bounce_simul(idx, buf);
  281. } else if (dcc[idx].irc) {
  282. // size_t size = strlen(dcc[idx].simulbot) + strlen(buf) + 20;
  283. // char *ircbuf = (char *) my_calloc(1, size);
  284. // egg_snprintf(ircbuf, size, "PRIVMSG %s :%s", dcc[idx].simulbot, buf);
  285. // tputs(dcc[idx].sock, ircbuf, strlen(ircbuf));
  286. dprintf(DP_HELP, "PRIVMSG %s :%s\n", dcc[idx].simulbot, buf);
  287. // free(ircbuf);
  288. } else {
  289. if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
  290. char *p = add_cr(buf);
  291. tputs(dcc[idx].sock, p, strlen(p));
  292. } else if (dcc[idx].type && dcc[idx].type->output) {
  293. dcc[idx].type->output(idx, buf, dcc[idx].u.other);
  294. } else {
  295. tputs(dcc[idx].sock, buf, len);
  296. }
  297. }
  298. }
  299. }
  300. void
  301. chatout(const char *format, ...)
  302. {
  303. char s[1024] = "", *p = NULL;
  304. va_list va;
  305. va_start(va, format);
  306. egg_vsnprintf(s, sizeof(s), format, va);
  307. va_end(va);
  308. if ((p = strrchr(s, '\n')))
  309. *p++ = 0;
  310. for (int i = 0; i < dcc_total; i++)
  311. if (dcc[i].type && (dcc[i].type == &DCC_CHAT) && (dcc[i].simul == -1))
  312. if (dcc[i].u.chat->channel >= 0)
  313. dprintf(i, "%s\n", s);
  314. }
  315. /* Print to all on this channel but one.
  316. */
  317. void
  318. chanout_but(int x, int chan, const char *format, ...)
  319. {
  320. char s[1024] = "", *p = NULL;
  321. va_list va;
  322. va_start(va, format);
  323. egg_vsnprintf(s, sizeof(s), format, va);
  324. va_end(va);
  325. if ((p = strrchr(s, '\n')))
  326. *p = 0;
  327. for (int i = 0; i < dcc_total; i++)
  328. if (dcc[i].type && (dcc[i].type == &DCC_CHAT) && (i != x) && (dcc[i].simul == -1))
  329. if (dcc[i].u.chat->channel == chan)
  330. dprintf(i, "%s\n", s);
  331. }
  332. void
  333. dcc_chatter(int idx)
  334. {
  335. int i;
  336. int j;
  337. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  338. strcpy(dcc[idx].u.chat->con_chan, "***");
  339. check_bind_chon(dcc[idx].nick, idx);
  340. dprintf(idx, "Connected to %s, running %s\n", conf.bot->nick, version);
  341. show_banner(idx); /* check STAT_BANNER inside function */
  342. get_user_flagrec(dcc[idx].user, &fr, NULL);
  343. if ((dcc[idx].status & STAT_BOTS) && glob_master(fr)) {
  344. if ((tands + 1) > 1)
  345. dprintf(idx, "There are %s-%d- bots%s currently linked.\n", BOLD(idx), tands + 1, BOLD_END(idx));
  346. else
  347. dprintf(idx, "There is %s-%d- bot%s currently linked.\n", BOLD(idx), tands + 1, BOLD_END(idx));
  348. dprintf(idx, " \n");
  349. }
  350. if (dcc[idx].status & STAT_WHOM) {
  351. answer_local_whom(idx, -1);
  352. dprintf(idx, " \n");
  353. }
  354. if (dcc[idx].status & STAT_CHANNELS) {
  355. show_channels(idx, NULL);
  356. dprintf(idx, " \n");
  357. }
  358. show_motd(idx);
  359. notes_chon(idx);
  360. if (glob_party(fr)) {
  361. i = dcc[idx].u.chat->channel;
  362. } else {
  363. dprintf(idx, "You don't have partyline chat access; commands only.\n\n");
  364. i = -1;
  365. }
  366. j = dcc[idx].sock;
  367. dcc[idx].u.chat->channel = 234567;
  368. /* Still there? */
  369. if ((idx >= dcc_total) || (dcc[idx].sock != j))
  370. return; /* Nope */
  371. if (dcc[idx].type == &DCC_CHAT) {
  372. if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
  373. strcpy(dcc[idx].u.chat->con_chan, "*");
  374. if (dcc[idx].u.chat->channel == 234567) {
  375. /* If the chat channel has already been altered it's *highly*
  376. * probably join/part messages have been broadcast everywhere,
  377. * so dont bother sending them
  378. */
  379. if (i == -2)
  380. i = 0;
  381. dcc[idx].u.chat->channel = i;
  382. if (dcc[idx].u.chat->channel >= 0) {
  383. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  384. botnet_send_join_idx(idx);
  385. }
  386. }
  387. }
  388. /* But *do* bother with sending it locally */
  389. if (!dcc[idx].u.chat->channel) {
  390. chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
  391. } else if (dcc[idx].u.chat->channel > 0) {
  392. chanout_but(-1, dcc[idx].u.chat->channel, "*** %s joined the channel.\n", dcc[idx].nick);
  393. }
  394. }
  395. }
  396. void trim_dcclist(int top_index)
  397. {
  398. dcc_total = top_index + 1;
  399. }
  400. int
  401. dcc_read(FILE *f)
  402. {
  403. char inbuf[1024] = "", *type = NULL, *buf = NULL;
  404. int idx = -1;
  405. bool isserv = 0;
  406. while (fgets(inbuf, sizeof(inbuf), f) != NULL) {
  407. remove_crlf(inbuf);
  408. buf = inbuf;
  409. if (!strcmp(buf, "+dcc"))
  410. return idx;
  411. type = newsplit(&buf);
  412. if (!strcmp(type, "type")) {
  413. struct dcc_table *dcc_type = NULL;
  414. size_t dcc_size = 0;
  415. // if (!strcmp(buf, "CHAT"))
  416. // dcc_type = &DCC_CHAT;
  417. if (!strcmp(buf, "SERVER")) {
  418. dcc_type = &SERVER_SOCKET;
  419. isserv = 1;
  420. }
  421. // if (!strcmp(buf, "BOT"))
  422. // dcc_type = &DCC_BOT;
  423. if (dcc_type) {
  424. idx = new_dcc(dcc_type, dcc_size);
  425. if (isserv)
  426. servidx = idx;
  427. }
  428. }
  429. if (idx >= 0) {
  430. if (!strcmp(type, "addr"))
  431. dcc[idx].addr = my_atoul(buf);
  432. if (!strcmp(type, "sock")) {
  433. dcc[idx].sock = atoi(buf);
  434. if (isserv)
  435. serv = dcc[idx].sock;
  436. }
  437. if (!strcmp(type, "port"))
  438. dcc[idx].port = atoi(buf);
  439. if (!strcmp(type, "nick"))
  440. strlcpy(dcc[idx].nick, buf, NICKLEN);
  441. if (!strcmp(type, "host")) {
  442. strlcpy(dcc[idx].host, buf, UHOSTLEN);
  443. }
  444. }
  445. }
  446. return -1;
  447. }
  448. void
  449. dcc_write(FILE *f, int idx)
  450. {
  451. if (dcc[idx].sock > 0) {
  452. fprintf(f, "-dcc\n");
  453. if (dcc[idx].type)
  454. fprintf(f, "type %s\n", dcc[idx].type->name);
  455. // if (user)
  456. // fprintf(f, "user %s\n", dcc[idx].user->handle);
  457. if (dcc[idx].addr)
  458. fprintf(f, "addr %u\n", dcc[idx].addr);
  459. if (dcc[idx].status)
  460. fprintf(f, "status %lu\n", dcc[idx].status);
  461. fprintf(f, "sock %d\n", dcc[idx].sock);
  462. // fprintf(f, "simul %d\n", dcc[idx].simul);
  463. if (dcc[idx].port)
  464. fprintf(f, "port %d\n", dcc[idx].port);
  465. if (dcc[idx].nick[0])
  466. fprintf(f, "nick %s\n", dcc[idx].nick);
  467. if (dcc[idx].host[0])
  468. fprintf(f, "host %s\n", dcc[idx].host);
  469. fprintf(f, "+dcc\n");
  470. }
  471. }
  472. /* Mark an entry as lost and deconstruct it's contents. It will be securely
  473. * removed from the dcc list in the main loop.
  474. */
  475. void
  476. lostdcc(int n)
  477. {
  478. sdprintf("lostdcc(%d)", n);
  479. /* Make sure it's a valid dcc index. */
  480. if (n < 0 || n >= max_dcc)
  481. return;
  482. if (dcc[n].type && dcc[n].type->kill)
  483. dcc[n].type->kill(n, dcc[n].u.other);
  484. else if (dcc[n].u.other)
  485. free(dcc[n].u.other);
  486. dcc[n].u.other = NULL;
  487. // 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
  488. // parsed by anything.
  489. // egg_bzero(&dcc[n], sizeof(struct dcc_t));
  490. dcc[n].sock = -1;
  491. dcc[n].type = NULL;
  492. dccn--;
  493. /* last entry! make table smaller :) */
  494. if (n == (dcc_total - 1))
  495. dcc_total--;
  496. }
  497. /* Show list of current dcc's to a dcc-chatter
  498. * positive value: idx given -- negative value: sock given
  499. */
  500. void
  501. tell_dcc(int idx)
  502. {
  503. int i;
  504. size_t j, nicklen = 0;
  505. char other[160] = "", format[81] = "";
  506. /* calculate max nicklen */
  507. for (i = 0; i < dcc_total; i++) {
  508. if (dcc[i].type && strlen(dcc[i].nick) > (unsigned) nicklen)
  509. nicklen = strlen(dcc[i].nick);
  510. }
  511. if (nicklen < 9)
  512. nicklen = 9;
  513. egg_snprintf(format, sizeof format, "%%-4s %%-4s %%-8s %%-5s %%-%us %%-40s %%s\n", nicklen);
  514. dprintf(idx, format, "SOCK", "IDX", "ADDR", "PORT", "NICK", "HOST", "TYPE");
  515. dprintf(idx, format, "----", "---", "--------", "-----", "---------",
  516. "----------------------------------------", "----");
  517. egg_snprintf(format, sizeof format, "%%-4d %%-4d %%08X %%5u %%-%us %%-40s %%s\n", nicklen);
  518. dprintf(idx, "dccn: %d, dcc_total: %d\n", dccn, dcc_total);
  519. dprintf(idx, "dns_idx: %d, servidx: %d\n", dns_idx, servidx);
  520. for (i = 0; i < dcc_total; i++) {
  521. if (dcc[i].type) {
  522. j = strlen(dcc[i].host);
  523. if (j > 40)
  524. j -= 40;
  525. else
  526. j = 0;
  527. if (dcc[i].type && dcc[i].type->display)
  528. dcc[i].type->display(i, other);
  529. else {
  530. sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
  531. break;
  532. }
  533. dprintf(idx, format, dcc[i].sock, i, dcc[i].addr, dcc[i].port, dcc[i].nick, dcc[i].host + j, other);
  534. }
  535. }
  536. }
  537. /* Mark someone on dcc chat as no longer away
  538. */
  539. void
  540. not_away(int idx)
  541. {
  542. if (dcc[idx].u.chat->away == NULL) {
  543. dprintf(idx, "You weren't away!\n");
  544. return;
  545. }
  546. if (dcc[idx].u.chat->channel >= 0) {
  547. chanout_but(-1, dcc[idx].u.chat->channel, "*** %s is no longer away.\n", dcc[idx].nick);
  548. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  549. botnet_send_away(-1, conf.bot->nick, dcc[idx].sock, NULL, idx);
  550. }
  551. }
  552. dprintf(idx, "You're not away any more.\n");
  553. free(dcc[idx].u.chat->away);
  554. dcc[idx].u.chat->away = NULL;
  555. check_bind_away(conf.bot->nick, idx, NULL);
  556. }
  557. void
  558. set_away(int idx, char *s)
  559. {
  560. if (s == NULL) {
  561. not_away(idx);
  562. return;
  563. }
  564. if (!s[0]) {
  565. not_away(idx);
  566. return;
  567. }
  568. if (dcc[idx].u.chat->away != NULL)
  569. free(dcc[idx].u.chat->away);
  570. dcc[idx].u.chat->away = strdup(s);
  571. if (dcc[idx].u.chat->channel >= 0) {
  572. chanout_but(-1, dcc[idx].u.chat->channel, "*** %s is now away: %s\n", dcc[idx].nick, s);
  573. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  574. botnet_send_away(-1, conf.bot->nick, dcc[idx].sock, s, idx);
  575. }
  576. }
  577. dprintf(idx, "You are now away. (%s)\n", s);
  578. check_bind_away(conf.bot->nick, idx, s);
  579. }
  580. /* Make a password, 10-14 random letters and digits
  581. */
  582. void
  583. makepass(char *s)
  584. {
  585. make_rand_str(s, 10 + randint(5));
  586. }
  587. void
  588. flush_lines(int idx, struct chat_info *ci)
  589. {
  590. int c = ci->line_count;
  591. struct msgq *p = ci->buffer, *o;
  592. while (p && c < (ci->max_line)) {
  593. ci->current_lines--;
  594. tputs(dcc[idx].sock, p->msg, p->len);
  595. free(p->msg);
  596. o = p->next;
  597. free(p);
  598. p = o;
  599. c++;
  600. }
  601. if (p != NULL) {
  602. if (dcc[idx].status & STAT_TELNET)
  603. tputs(dcc[idx].sock, "[More]: ", 8);
  604. else
  605. tputs(dcc[idx].sock, "[More]\n", 7);
  606. }
  607. ci->buffer = p;
  608. ci->line_count = 0;
  609. }
  610. int
  611. new_dcc(struct dcc_table *type, int xtra_size)
  612. {
  613. if (!type)
  614. return -1;
  615. if (dcc_total == max_dcc)
  616. return -1;
  617. int i = 0;
  618. /* Find the first gap */
  619. for (i = 0; i <= dcc_total; i++)
  620. if (!dcc[i].type)
  621. break;
  622. /* we managed to get to the end of the list! */
  623. if (i == dcc_total) {
  624. i = dcc_total;
  625. dcc_total++;
  626. }
  627. dccn++;
  628. /* empty out the memory for the entry */
  629. egg_bzero((char *) &dcc[i], sizeof(struct dcc_t));
  630. dcc[i].type = type;
  631. if (xtra_size)
  632. dcc[i].u.other = (char *) my_calloc(1, xtra_size);
  633. else
  634. dcc[i].u.other = NULL;
  635. dcc[i].simul = -1;
  636. dcc[i].sock = -1;
  637. sdprintf("new_dcc (%s): %d (dccn/dcc_total: %d/%d)", type->name, i, dccn, dcc_total);
  638. return i;
  639. }
  640. /* Changes the given dcc entry to another type.
  641. */
  642. void
  643. changeover_dcc(int i, struct dcc_table *type, int xtra_size)
  644. {
  645. /* Free old structure. */
  646. if (dcc[i].type && dcc[i].type->kill)
  647. dcc[i].type->kill(i, dcc[i].u.other);
  648. else if (dcc[i].u.other) {
  649. free(dcc[i].u.other);
  650. dcc[i].u.other = NULL;
  651. }
  652. dcc[i].type = type;
  653. if (xtra_size)
  654. dcc[i].u.other = (char *) my_calloc(1, xtra_size);
  655. }
  656. int
  657. detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
  658. {
  659. if (!dcc_flood_thr)
  660. return 0;
  661. time_t t = now;
  662. if (*timer != t) {
  663. *timer = t;
  664. chat->msgs_per_sec = 0;
  665. } else {
  666. chat->msgs_per_sec++;
  667. if (chat->msgs_per_sec > dcc_flood_thr) {
  668. /* FLOOD */
  669. dprintf(idx, "*** FLOOD: Goodbye.\n");
  670. /* Evil assumption here that flags&DCT_CHAT implies chat type */
  671. if ((dcc[idx].type->flags & DCT_CHAT) && chat && (chat->channel >= 0)) {
  672. char x[1024];
  673. egg_snprintf(x, sizeof x, DCC_FLOODBOOT, dcc[idx].nick);
  674. chanout_but(idx, chat->channel, "*** %s", x);
  675. if (chat->channel < GLOBAL_CHANS)
  676. botnet_send_part_idx(idx, x);
  677. }
  678. check_bind_chof(dcc[idx].nick, idx);
  679. if ((dcc[idx].sock != STDOUT) || backgrd) {
  680. killsock(dcc[idx].sock);
  681. lostdcc(idx);
  682. } else {
  683. dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
  684. dcc_chatter(idx);
  685. }
  686. return 1; /* <- flood */
  687. }
  688. }
  689. return 0;
  690. }
  691. /* Handle someone being booted from dcc chat.
  692. */
  693. void
  694. do_boot(int idx, char *by, char *reason)
  695. {
  696. int files = (dcc[idx].type != &DCC_CHAT);
  697. dprintf(idx, DCC_BOOTED1);
  698. dprintf(idx, DCC_BOOTED2, files ? "file section" : "bot", by, reason[0] ? ": " : ".", reason);
  699. /* If it's a partyliner (chatterer :) */
  700. /* Horrible assumption that DCT_CHAT using structure uses same format
  701. * as DCC_CHAT */
  702. if ((dcc[idx].type->flags & DCT_CHAT) && (dcc[idx].u.chat->channel >= 0)) {
  703. char x[1024] = "";
  704. egg_snprintf(x, sizeof x, DCC_BOOTED3, by, dcc[idx].nick, reason[0] ? ": " : "", reason);
  705. chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
  706. if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
  707. botnet_send_part_idx(idx, x);
  708. }
  709. check_bind_chof(dcc[idx].nick, idx);
  710. if (dcc[idx].u.chat->su_nick) {
  711. dcc[idx].user = get_user_by_handle(userlist, dcc[idx].u.chat->su_nick);
  712. strcpy(dcc[idx].nick, dcc[idx].u.chat->su_nick);
  713. dcc[idx].type = &DCC_CHAT;
  714. dprintf(idx, "Returning to real nick %s!\n", dcc[idx].u.chat->su_nick);
  715. free(dcc[idx].u.chat->su_nick);
  716. dcc[idx].u.chat->su_nick = NULL;
  717. dcc_chatter(idx);
  718. if (dcc[idx].u.chat->channel < GLOBAL_CHANS && dcc[idx].u.chat->channel >= 0) {
  719. botnet_send_join_idx(idx);
  720. }
  721. } else if ((dcc[idx].sock != STDOUT) || backgrd) {
  722. killsock(dcc[idx].sock);
  723. lostdcc(idx);
  724. /* Entry must remain in the table so it can be logged by the caller */
  725. } else {
  726. dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
  727. dcc_chatter(idx);
  728. }
  729. return;
  730. }
  731. port_t
  732. listen_all(port_t lport, bool off)
  733. {
  734. int idx = (-1);
  735. port_t port, realport;
  736. #ifdef USE_IPV6
  737. int i6 = 0;
  738. #endif /* USE_IPV6 */
  739. int i;
  740. struct portmap *pmap = NULL, *pold = NULL;
  741. port = realport = lport;
  742. for (pmap = root; pmap; pold = pmap, pmap = pmap->next)
  743. if (pmap->realport == port) {
  744. port = pmap->mappedto;
  745. break;
  746. }
  747. for (int ii = 0; ii < dcc_total; ii++) {
  748. if (dcc[ii].type && (dcc[ii].type == &DCC_TELNET) && (dcc[ii].port == port)) {
  749. idx = ii;
  750. if (off) {
  751. if (pmap) {
  752. if (pold)
  753. pold->next = pmap->next;
  754. else
  755. root = pmap->next;
  756. free(pmap);
  757. }
  758. #ifdef USE_IPV6
  759. if (sockprotocol(dcc[idx].sock) == AF_INET6)
  760. putlog(LOG_DEBUG, "*", "Closing IPv6 listening port %d", dcc[idx].port);
  761. else
  762. #endif /* USE_IPV6 */
  763. putlog(LOG_DEBUG, "*", "Closing IPv4 listening port %d", dcc[idx].port);
  764. killsock(dcc[idx].sock);
  765. lostdcc(idx);
  766. return idx;
  767. }
  768. }
  769. }
  770. if (idx < 0) {
  771. if (off) {
  772. putlog(LOG_ERRORS, "*", "No such listening port open - %d", lport);
  773. return idx;
  774. }
  775. /* make new one */
  776. if (dcc_total >= max_dcc) {
  777. putlog(LOG_ERRORS, "*", "Can't open listening port - no more DCC Slots");
  778. } else {
  779. #ifdef USE_IPV6
  780. i6 = open_listen_by_af(&port, AF_INET6);
  781. if (i6 < 0) {
  782. putlog(LOG_ERRORS, "*", "Can't open IPv6 listening port %d - %s", port,
  783. i6 == -1 ? "it's taken." : "couldn't assign ip.");
  784. } else {
  785. idx = new_dcc(&DCC_TELNET, 0);
  786. dcc[idx].addr = 0L;
  787. strcpy(dcc[idx].host6, myipstr(6));
  788. dcc[idx].port = port;
  789. dcc[idx].sock = i6;
  790. dcc[idx].timeval = now;
  791. strcpy(dcc[idx].nick, "(telnet6)");
  792. strcpy(dcc[idx].host, "*");
  793. putlog(LOG_DEBUG, "*", "Listening on IPv6 at telnet port %d", port);
  794. }
  795. i = open_listen_by_af(&port, AF_INET);
  796. #else
  797. i = open_listen(&port);
  798. #endif /* USE_IPV6 */
  799. if (i < 0) {
  800. putlog(LOG_ERRORS, "*", "Can't open IPv4 listening port %d - %s", port,
  801. i == -1 ? "it's taken." : "couldn't assign ip.");
  802. } else {
  803. idx = (-1); /* now setup ipv4 listening port */
  804. idx = new_dcc(&DCC_TELNET, 0);
  805. dcc[idx].addr = iptolong(getmyip());
  806. dcc[idx].port = port;
  807. dcc[idx].sock = i;
  808. dcc[idx].timeval = now;
  809. strcpy(dcc[idx].nick, "(telnet)");
  810. strcpy(dcc[idx].host, "*");
  811. putlog(LOG_DEBUG, "*", "Listening on IPv4 at telnet port %d", port);
  812. }
  813. #ifdef USE_IPV6
  814. if (i > 0 || i6 > 0) {
  815. #else
  816. if (i > 0) {
  817. #endif /* USE_IPV6 */
  818. if (!pmap) {
  819. pmap = (struct portmap *) my_calloc(1, sizeof(struct portmap));
  820. pmap->next = root;
  821. root = pmap;
  822. }
  823. pmap->realport = realport;
  824. pmap->mappedto = port;
  825. }
  826. }
  827. }
  828. /* if one of the protocols failed, the one which worked will be returned
  829. * if both were successful, it wont matter which idx is returned, because the
  830. * code reading listen_all will only be reading dcc[idx].port, which would be
  831. * open on both protocols.
  832. * -bryan (10/29/03)
  833. */
  834. return idx;
  835. }
  836. void
  837. identd_open()
  838. {
  839. int idx;
  840. int i = -1;
  841. port_t port = 113;
  842. for (idx = 0; idx < dcc_total; idx++)
  843. if (dcc[idx].type == &DCC_IDENTD_CONNECT)
  844. return; /* it's already open :) */
  845. idx = -1;
  846. identd_hack = 1;
  847. #ifdef USE_IPV6
  848. i = open_listen_by_af(&port, AF_INET6);
  849. #else
  850. i = open_listen(&port);
  851. #endif /* USE_IPV6 */
  852. identd_hack = 0;
  853. if (i >= 0) {
  854. idx = new_dcc(&DCC_IDENTD_CONNECT, 0);
  855. if (idx >= 0) {
  856. egg_timeval_t howlong;
  857. dcc[idx].addr = iptolong(getmyip());
  858. dcc[idx].port = port;
  859. dcc[idx].sock = i;
  860. dcc[idx].timeval = now;
  861. strcpy(dcc[idx].nick, "(identd)");
  862. strcpy(dcc[idx].host, "*");
  863. putlog(LOG_DEBUG, "*", "Identd daemon started.");
  864. howlong.sec = 15;
  865. howlong.usec = 0;
  866. timer_create(&howlong, "identd_close()", (Function) identd_close);
  867. } else
  868. killsock(i);
  869. }
  870. }
  871. void
  872. identd_close()
  873. {
  874. for (int idx = 0; idx < dcc_total; idx++) {
  875. if (dcc[idx].type == &DCC_IDENTD_CONNECT) {
  876. killsock(dcc[idx].sock);
  877. lostdcc(idx);
  878. putlog(LOG_DEBUG, "*", "Identd daemon stopped.");
  879. break;
  880. }
  881. }
  882. }
  883. bool
  884. valid_idx(int idx)
  885. {
  886. if ((idx == -1) || (idx >= dcc_total) || (!dcc[idx].type))
  887. return 0;
  888. return 1;
  889. }
  890. int check_cmd_pass(const char *cmd, char *pass)
  891. {
  892. if (check_master_hash(NULL, pass))
  893. return 1;
  894. struct cmd_pass *cp = NULL;
  895. for (cp = cmdpass; cp; cp = cp->next)
  896. if (!egg_strcasecmp(cmd, cp->name)) {
  897. char tmp[32] = "";
  898. encrypt_cmd_pass(pass, tmp);
  899. if (!strcmp(tmp, cp->pass))
  900. return 1;
  901. return 0;
  902. }
  903. return 0;
  904. }
  905. int has_cmd_pass(const char *cmd)
  906. {
  907. struct cmd_pass *cp = NULL;
  908. for (cp = cmdpass; cp; cp = cp->next)
  909. if (!egg_strcasecmp(cmd, cp->name))
  910. return 1;
  911. return 0;
  912. }
  913. void set_cmd_pass(char *ln, int shareit)
  914. {
  915. struct cmd_pass *cp = NULL;
  916. char *cmd = NULL;
  917. cmd = newsplit(&ln);
  918. for (cp = cmdpass; cp; cp = cp->next)
  919. if (!strcmp(cmd, cp->name))
  920. break;
  921. if (cp)
  922. if (ln[0]) {
  923. /* change */
  924. strcpy(cp->pass, ln);
  925. if (shareit)
  926. botnet_send_cmdpass(-1, cp->name, cp->pass);
  927. } else {
  928. if (cp == cmdpass)
  929. cmdpass = cp->next;
  930. else {
  931. struct cmd_pass *cp2;
  932. cp2 = cmdpass;
  933. while (cp2->next != cp)
  934. cp2 = cp2->next;
  935. cp2->next = cp->next;
  936. }
  937. if (shareit)
  938. botnet_send_cmdpass(-1, cp->name, "");
  939. free(cp->name);
  940. free(cp);
  941. } else if (ln[0]) {
  942. /* create */
  943. cp = (struct cmd_pass *) my_calloc(1, sizeof(struct cmd_pass));
  944. cp->next = cmdpass;
  945. cmdpass = cp;
  946. cp->name = strdup(cmd);
  947. strcpy(cp->pass, ln);
  948. if (shareit)
  949. botnet_send_cmdpass(-1, cp->name, cp->pass);
  950. }
  951. }