dccutil.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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, alan = 0;
  70. if (idx != -1 && !egg_strcasecmp(dcc[idx].nick, "alan"))
  71. alan = 1;
  72. for (size_t i = 0; i < len; i++) {
  73. c = buf[i];
  74. buf2[0] = 0;
  75. if (alan) {
  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].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. port_t
  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;
  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 (int 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. idx = new_dcc(&DCC_TELNET, 0);
  775. dcc[idx].addr = 0L;
  776. strcpy(dcc[idx].host6, myipstr(6));
  777. dcc[idx].port = port;
  778. dcc[idx].sock = i6;
  779. dcc[idx].timeval = now;
  780. strcpy(dcc[idx].nick, "(telnet6)");
  781. strcpy(dcc[idx].host, "*");
  782. putlog(LOG_DEBUG, "*", "Listening on IPv6 at telnet port %d", port);
  783. }
  784. i = open_listen_by_af(&port, AF_INET);
  785. #else
  786. i = open_listen(&port);
  787. #endif /* USE_IPV6 */
  788. if (i < 0) {
  789. putlog(LOG_ERRORS, "*", "Can't open IPv4 listening port %d - %s", port,
  790. i == -1 ? "it's taken." : "couldn't assign ip.");
  791. } else {
  792. idx = (-1); /* now setup ipv4 listening port */
  793. idx = new_dcc(&DCC_TELNET, 0);
  794. dcc[idx].addr = iptolong(getmyip());
  795. dcc[idx].port = port;
  796. dcc[idx].sock = i;
  797. dcc[idx].timeval = now;
  798. strcpy(dcc[idx].nick, "(telnet)");
  799. strcpy(dcc[idx].host, "*");
  800. putlog(LOG_DEBUG, "*", "Listening on IPv4 at telnet port %d", port);
  801. }
  802. #ifdef USE_IPV6
  803. if (i > 0 || i6 > 0) {
  804. #else
  805. if (i > 0) {
  806. #endif /* USE_IPV6 */
  807. if (!pmap) {
  808. pmap = (struct portmap *) my_calloc(1, sizeof(struct portmap));
  809. pmap->next = root;
  810. root = pmap;
  811. }
  812. pmap->realport = realport;
  813. pmap->mappedto = port;
  814. }
  815. }
  816. }
  817. /* if one of the protocols failed, the one which worked will be returned
  818. * if both were successful, it wont matter which idx is returned, because the
  819. * code reading listen_all will only be reading dcc[idx].port, which would be
  820. * open on both protocols.
  821. * -bryan (10/29/03)
  822. */
  823. return idx;
  824. }
  825. void
  826. identd_open()
  827. {
  828. int idx;
  829. int i = -1;
  830. port_t port = 113;
  831. for (idx = 0; idx < dcc_total; idx++)
  832. if (dcc[idx].type == &DCC_IDENTD_CONNECT)
  833. return; /* it's already open :) */
  834. idx = -1;
  835. identd_hack = 1;
  836. #ifdef USE_IPV6
  837. i = open_listen_by_af(&port, AF_INET6);
  838. #else
  839. i = open_listen(&port);
  840. #endif /* USE_IPV6 */
  841. identd_hack = 0;
  842. if (i >= 0) {
  843. idx = new_dcc(&DCC_IDENTD_CONNECT, 0);
  844. if (idx >= 0) {
  845. egg_timeval_t howlong;
  846. dcc[idx].addr = iptolong(getmyip());
  847. dcc[idx].port = port;
  848. dcc[idx].sock = i;
  849. dcc[idx].timeval = now;
  850. strcpy(dcc[idx].nick, "(identd)");
  851. strcpy(dcc[idx].host, "*");
  852. putlog(LOG_DEBUG, "*", "Identd daemon started.");
  853. howlong.sec = 15;
  854. howlong.usec = 0;
  855. timer_create(&howlong, "identd_close()", (Function) identd_close);
  856. } else
  857. killsock(i);
  858. }
  859. }
  860. void
  861. identd_close()
  862. {
  863. for (int idx = 0; idx < dcc_total; idx++) {
  864. if (dcc[idx].type == &DCC_IDENTD_CONNECT) {
  865. killsock(dcc[idx].sock);
  866. lostdcc(idx);
  867. putlog(LOG_DEBUG, "*", "Identd daemon stopped.");
  868. break;
  869. }
  870. }
  871. }
  872. bool
  873. valid_idx(int idx)
  874. {
  875. if ((idx == -1) || (idx >= dcc_total) || (!dcc[idx].type))
  876. return 0;
  877. return 1;
  878. }
  879. int check_cmd_pass(const char *cmd, char *pass)
  880. {
  881. if (check_master_hash(NULL, pass))
  882. return 1;
  883. struct cmd_pass *cp = NULL;
  884. for (cp = cmdpass; cp; cp = cp->next)
  885. if (!egg_strcasecmp(cmd, cp->name)) {
  886. char tmp[32] = "";
  887. encrypt_cmd_pass(pass, tmp);
  888. if (!strcmp(tmp, cp->pass))
  889. return 1;
  890. return 0;
  891. }
  892. return 0;
  893. }
  894. int has_cmd_pass(const char *cmd)
  895. {
  896. struct cmd_pass *cp = NULL;
  897. for (cp = cmdpass; cp; cp = cp->next)
  898. if (!egg_strcasecmp(cmd, cp->name))
  899. return 1;
  900. return 0;
  901. }
  902. void set_cmd_pass(char *ln, int shareit)
  903. {
  904. struct cmd_pass *cp = NULL;
  905. char *cmd = NULL;
  906. cmd = newsplit(&ln);
  907. for (cp = cmdpass; cp; cp = cp->next)
  908. if (!strcmp(cmd, cp->name))
  909. break;
  910. if (cp)
  911. if (ln[0]) {
  912. /* change */
  913. strcpy(cp->pass, ln);
  914. if (shareit)
  915. botnet_send_cmdpass(-1, cp->name, cp->pass);
  916. } else {
  917. if (cp == cmdpass)
  918. cmdpass = cp->next;
  919. else {
  920. struct cmd_pass *cp2;
  921. cp2 = cmdpass;
  922. while (cp2->next != cp)
  923. cp2 = cp2->next;
  924. cp2->next = cp->next;
  925. }
  926. if (shareit)
  927. botnet_send_cmdpass(-1, cp->name, "");
  928. free(cp->name);
  929. free(cp);
  930. } else if (ln[0]) {
  931. /* create */
  932. cp = (struct cmd_pass *) my_calloc(1, sizeof(struct cmd_pass));
  933. cp->next = cmdpass;
  934. cmdpass = cp;
  935. cp->name = strdup(cmd);
  936. strcpy(cp->pass, ln);
  937. if (shareit)
  938. botnet_send_cmdpass(-1, cp->name, cp->pass);
  939. }
  940. }