dccutil.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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_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. notes_chon(idx);
  365. if (dcc[idx].type == &DCC_CHAT) {
  366. if (!strcmp(dcc[idx].u.chat->con_chan, "***"))
  367. strcpy(dcc[idx].u.chat->con_chan, "*");
  368. if (dcc[idx].u.chat->channel == -2)
  369. dcc[idx].u.chat->channel = 0;
  370. if (dcc[idx].u.chat->channel >= 0) {
  371. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  372. botnet_send_join_idx(idx);
  373. }
  374. }
  375. /* But *do* bother with sending it locally */
  376. if (!dcc[idx].u.chat->channel) {
  377. chanout_but(-1, 0, "*** %s joined the party line.\n", dcc[idx].nick);
  378. } else if (dcc[idx].u.chat->channel > 0) {
  379. chanout_but(-1, dcc[idx].u.chat->channel, "*** %s joined the channel.\n", dcc[idx].nick);
  380. }
  381. }
  382. }
  383. void trim_dcclist(int top_index)
  384. {
  385. dcc_total = top_index + 1;
  386. }
  387. int
  388. dcc_read(FILE *f)
  389. {
  390. char inbuf[1024] = "", *type = NULL, *buf = NULL;
  391. int idx = -1;
  392. bool isserv = 0;
  393. while (fgets(inbuf, sizeof(inbuf), f) != NULL) {
  394. remove_crlf(inbuf);
  395. buf = inbuf;
  396. if (!strcmp(buf, "+dcc"))
  397. return idx;
  398. type = newsplit(&buf);
  399. if (!strcmp(type, "type")) {
  400. struct dcc_table *dcc_type = NULL;
  401. size_t dcc_size = 0;
  402. // if (!strcmp(buf, "CHAT"))
  403. // dcc_type = &DCC_CHAT;
  404. if (!strcmp(buf, "SERVER")) {
  405. dcc_type = &SERVER_SOCKET;
  406. isserv = 1;
  407. }
  408. // if (!strcmp(buf, "BOT"))
  409. // dcc_type = &DCC_BOT;
  410. if (dcc_type) {
  411. idx = new_dcc(dcc_type, dcc_size);
  412. if (isserv)
  413. servidx = idx;
  414. }
  415. }
  416. if (idx >= 0) {
  417. if (!strcmp(type, "addr"))
  418. dcc[idx].addr = my_atoul(buf);
  419. if (!strcmp(type, "sock")) {
  420. dcc[idx].sock = atoi(buf);
  421. if (isserv)
  422. serv = dcc[idx].sock;
  423. }
  424. if (!strcmp(type, "port"))
  425. dcc[idx].port = atoi(buf);
  426. if (!strcmp(type, "nick"))
  427. strlcpy(dcc[idx].nick, buf, NICKLEN);
  428. if (!strcmp(type, "host")) {
  429. strlcpy(dcc[idx].host, buf, UHOSTLEN);
  430. }
  431. }
  432. }
  433. return -1;
  434. }
  435. void
  436. dcc_write(FILE *f, int idx)
  437. {
  438. if (dcc[idx].sock > 0) {
  439. fprintf(f, "-dcc\n");
  440. if (dcc[idx].type)
  441. fprintf(f, "type %s\n", dcc[idx].type->name);
  442. // if (user)
  443. // fprintf(f, "user %s\n", dcc[idx].user->handle);
  444. if (dcc[idx].addr)
  445. fprintf(f, "addr %u\n", dcc[idx].addr);
  446. if (dcc[idx].status)
  447. fprintf(f, "status %lu\n", dcc[idx].status);
  448. fprintf(f, "sock %d\n", dcc[idx].sock);
  449. // fprintf(f, "simul %d\n", dcc[idx].simul);
  450. if (dcc[idx].port)
  451. fprintf(f, "port %d\n", dcc[idx].port);
  452. if (dcc[idx].nick[0])
  453. fprintf(f, "nick %s\n", dcc[idx].nick);
  454. if (dcc[idx].host[0])
  455. fprintf(f, "host %s\n", dcc[idx].host);
  456. fprintf(f, "+dcc\n");
  457. }
  458. }
  459. /* Mark an entry as lost and deconstruct it's contents. It will be securely
  460. * removed from the dcc list in the main loop.
  461. */
  462. void
  463. lostdcc(int n)
  464. {
  465. sdprintf("lostdcc(%d)", n);
  466. /* Make sure it's a valid dcc index. */
  467. if (n < 0 || n >= max_dcc)
  468. return;
  469. if (dcc[n].type && dcc[n].type->kill)
  470. dcc[n].type->kill(n, dcc[n].u.other);
  471. else if (dcc[n].u.other)
  472. free(dcc[n].u.other);
  473. dcc[n].u.other = NULL;
  474. // 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
  475. // parsed by anything.
  476. // egg_bzero(&dcc[n], sizeof(struct dcc_t));
  477. dcc[n].sock = -1;
  478. dcc[n].type = NULL;
  479. dccn--;
  480. /* last entry! make table smaller :) */
  481. if (n == (dcc_total - 1))
  482. dcc_total--;
  483. }
  484. /* Show list of current dcc's to a dcc-chatter
  485. * positive value: idx given -- negative value: sock given
  486. */
  487. void
  488. tell_dcc(int idx)
  489. {
  490. int i;
  491. size_t j, nicklen = 0;
  492. char other[160] = "", format[81] = "";
  493. /* calculate max nicklen */
  494. for (i = 0; i < dcc_total; i++) {
  495. if (dcc[i].type && strlen(dcc[i].nick) > (unsigned) nicklen)
  496. nicklen = strlen(dcc[i].nick);
  497. }
  498. if (nicklen < 9)
  499. nicklen = 9;
  500. egg_snprintf(format, sizeof format, "%%-4s %%-4s %%-8s %%-5s %%-%us %%-40s %%s\n", nicklen);
  501. dprintf(idx, format, "SOCK", "IDX", "ADDR", "PORT", "NICK", "HOST", "TYPE");
  502. dprintf(idx, format, "----", "---", "--------", "-----", "---------",
  503. "----------------------------------------", "----");
  504. egg_snprintf(format, sizeof format, "%%-4d %%-4d %%08X %%5u %%-%us %%-40s %%s\n", nicklen);
  505. dprintf(idx, "dccn: %d, dcc_total: %d\n", dccn, dcc_total);
  506. dprintf(idx, "dns_idx: %d, servidx: %d\n", dns_idx, servidx);
  507. for (i = 0; i < dcc_total; i++) {
  508. if (dcc[i].type) {
  509. j = strlen(dcc[i].host);
  510. if (j > 40)
  511. j -= 40;
  512. else
  513. j = 0;
  514. if (dcc[i].type && dcc[i].type->display)
  515. dcc[i].type->display(i, other);
  516. else {
  517. sprintf(other, "?:%lX !! ERROR !!", (long) dcc[i].type);
  518. break;
  519. }
  520. dprintf(idx, format, dcc[i].sock, i, dcc[i].addr, dcc[i].port, dcc[i].nick, dcc[i].host + j, other);
  521. }
  522. }
  523. }
  524. /* Mark someone on dcc chat as no longer away
  525. */
  526. void
  527. not_away(int idx)
  528. {
  529. if (dcc[idx].u.chat->away == NULL) {
  530. dprintf(idx, "You weren't away!\n");
  531. return;
  532. }
  533. if (dcc[idx].u.chat->channel >= 0) {
  534. chanout_but(-1, dcc[idx].u.chat->channel, "*** %s is no longer away.\n", dcc[idx].nick);
  535. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  536. botnet_send_away(-1, conf.bot->nick, dcc[idx].sock, NULL, idx);
  537. }
  538. }
  539. dprintf(idx, "You're not away any more.\n");
  540. free(dcc[idx].u.chat->away);
  541. dcc[idx].u.chat->away = NULL;
  542. check_bind_away(conf.bot->nick, idx, NULL);
  543. }
  544. void
  545. set_away(int idx, char *s)
  546. {
  547. if (s == NULL) {
  548. not_away(idx);
  549. return;
  550. }
  551. if (!s[0]) {
  552. not_away(idx);
  553. return;
  554. }
  555. if (dcc[idx].u.chat->away != NULL)
  556. free(dcc[idx].u.chat->away);
  557. dcc[idx].u.chat->away = strdup(s);
  558. if (dcc[idx].u.chat->channel >= 0) {
  559. chanout_but(-1, dcc[idx].u.chat->channel, "*** %s is now away: %s\n", dcc[idx].nick, s);
  560. if (dcc[idx].u.chat->channel < GLOBAL_CHANS) {
  561. botnet_send_away(-1, conf.bot->nick, dcc[idx].sock, s, idx);
  562. }
  563. }
  564. dprintf(idx, "You are now away. (%s)\n", s);
  565. check_bind_away(conf.bot->nick, idx, s);
  566. }
  567. /* Make a password, 10-14 random letters and digits
  568. */
  569. void
  570. makepass(char *s)
  571. {
  572. make_rand_str(s, 10 + randint(5));
  573. }
  574. void
  575. flush_lines(int idx, struct chat_info *ci)
  576. {
  577. int c = ci->line_count;
  578. struct msgq *p = ci->buffer, *o;
  579. while (p && c < (ci->max_line)) {
  580. ci->current_lines--;
  581. tputs(dcc[idx].sock, p->msg, p->len);
  582. free(p->msg);
  583. o = p->next;
  584. free(p);
  585. p = o;
  586. c++;
  587. }
  588. if (p != NULL) {
  589. if (dcc[idx].status & STAT_TELNET)
  590. tputs(dcc[idx].sock, "[More]: ", 8);
  591. else
  592. tputs(dcc[idx].sock, "[More]\n", 7);
  593. }
  594. ci->buffer = p;
  595. ci->line_count = 0;
  596. }
  597. int
  598. new_dcc(struct dcc_table *type, int xtra_size)
  599. {
  600. if (!type)
  601. return -1;
  602. if (dcc_total == max_dcc)
  603. return -1;
  604. int i = 0;
  605. /* Find the first gap */
  606. for (i = 0; i <= dcc_total; i++)
  607. if (!dcc[i].type)
  608. break;
  609. /* we managed to get to the end of the list! */
  610. if (i == dcc_total) {
  611. i = dcc_total;
  612. dcc_total++;
  613. }
  614. dccn++;
  615. /* empty out the memory for the entry */
  616. egg_bzero((char *) &dcc[i], sizeof(struct dcc_t));
  617. dcc[i].type = type;
  618. if (xtra_size)
  619. dcc[i].u.other = (char *) my_calloc(1, xtra_size);
  620. else
  621. dcc[i].u.other = NULL;
  622. dcc[i].simul = -1;
  623. dcc[i].sock = -1;
  624. sdprintf("new_dcc (%s): %d (dccn/dcc_total: %d/%d)", type->name, i, dccn, dcc_total);
  625. return i;
  626. }
  627. /* Changes the given dcc entry to another type.
  628. */
  629. void
  630. changeover_dcc(int i, struct dcc_table *type, int xtra_size)
  631. {
  632. /* Free old structure. */
  633. if (dcc[i].type && dcc[i].type->kill)
  634. dcc[i].type->kill(i, dcc[i].u.other);
  635. else if (dcc[i].u.other) {
  636. free(dcc[i].u.other);
  637. dcc[i].u.other = NULL;
  638. }
  639. dcc[i].type = type;
  640. if (xtra_size)
  641. dcc[i].u.other = (char *) my_calloc(1, xtra_size);
  642. }
  643. int
  644. detect_dcc_flood(time_t * timer, struct chat_info *chat, int idx)
  645. {
  646. if (!dcc_flood_thr)
  647. return 0;
  648. time_t t = now;
  649. if (*timer != t) {
  650. *timer = t;
  651. chat->msgs_per_sec = 0;
  652. } else {
  653. chat->msgs_per_sec++;
  654. if (chat->msgs_per_sec > dcc_flood_thr) {
  655. /* FLOOD */
  656. dprintf(idx, "*** FLOOD: Goodbye.\n");
  657. /* Evil assumption here that flags&DCT_CHAT implies chat type */
  658. if ((dcc[idx].type->flags & DCT_CHAT) && chat && (chat->channel >= 0)) {
  659. char x[1024];
  660. egg_snprintf(x, sizeof x, DCC_FLOODBOOT, dcc[idx].nick);
  661. chanout_but(idx, chat->channel, "*** %s", x);
  662. if (chat->channel < GLOBAL_CHANS)
  663. botnet_send_part_idx(idx, x);
  664. }
  665. check_bind_chof(dcc[idx].nick, idx);
  666. if ((dcc[idx].sock != STDOUT) || backgrd) {
  667. killsock(dcc[idx].sock);
  668. lostdcc(idx);
  669. } else {
  670. dprintf(DP_STDOUT, "\n### SIMULATION RESET ###\n\n");
  671. dcc_chatter(idx);
  672. }
  673. return 1; /* <- flood */
  674. }
  675. }
  676. return 0;
  677. }
  678. /* Handle someone being booted from dcc chat.
  679. */
  680. void
  681. do_boot(int idx, char *by, char *reason)
  682. {
  683. int files = (dcc[idx].type != &DCC_CHAT);
  684. dprintf(idx, DCC_BOOTED1);
  685. dprintf(idx, DCC_BOOTED2, files ? "file section" : "bot", by, reason[0] ? ": " : ".", reason);
  686. /* If it's a partyliner (chatterer :) */
  687. /* Horrible assumption that DCT_CHAT using structure uses same format
  688. * as DCC_CHAT */
  689. if ((dcc[idx].type->flags & DCT_CHAT) && (dcc[idx].u.chat->channel >= 0)) {
  690. char x[1024] = "";
  691. egg_snprintf(x, sizeof x, DCC_BOOTED3, by, dcc[idx].nick, reason[0] ? ": " : "", reason);
  692. chanout_but(idx, dcc[idx].u.chat->channel, "*** %s.\n", x);
  693. if (dcc[idx].u.chat->channel < GLOBAL_CHANS)
  694. botnet_send_part_idx(idx, x);
  695. }
  696. check_bind_chof(dcc[idx].nick, idx);
  697. if (dcc[idx].u.chat->su_nick) {
  698. dcc[idx].user = get_user_by_handle(userlist, dcc[idx].u.chat->su_nick);
  699. strcpy(dcc[idx].nick, dcc[idx].u.chat->su_nick);
  700. dcc[idx].type = &DCC_CHAT;
  701. dprintf(idx, "Returning to real nick %s!\n", dcc[idx].u.chat->su_nick);
  702. free(dcc[idx].u.chat->su_nick);
  703. dcc[idx].u.chat->su_nick = NULL;
  704. dcc_chatter(idx);
  705. if (dcc[idx].u.chat->channel < GLOBAL_CHANS && dcc[idx].u.chat->channel >= 0) {
  706. botnet_send_join_idx(idx);
  707. }
  708. } else if ((dcc[idx].sock != STDOUT) || backgrd) {
  709. killsock(dcc[idx].sock);
  710. lostdcc(idx);
  711. /* Entry must remain in the table so it can be logged by the caller */
  712. } else {
  713. dprintf(DP_STDOUT, "\n### SIMULATION RESET\n\n");
  714. dcc_chatter(idx);
  715. }
  716. return;
  717. }
  718. int
  719. listen_all(port_t lport, bool off)
  720. {
  721. int idx = -1;
  722. port_t port, realport;
  723. #ifdef USE_IPV6
  724. int i6 = 0;
  725. #endif /* USE_IPV6 */
  726. int i = 0, ii = 0;
  727. struct portmap *pmap = NULL, *pold = NULL;
  728. port = realport = lport;
  729. for (pmap = root; pmap; pold = pmap, pmap = pmap->next)
  730. if (pmap->realport == port) {
  731. port = pmap->mappedto;
  732. break;
  733. }
  734. for (ii = 0; ii < dcc_total; ii++) {
  735. if (dcc[ii].type && (dcc[ii].type == &DCC_TELNET) && (dcc[ii].port == port)) {
  736. idx = ii;
  737. if (off) {
  738. if (pmap) {
  739. if (pold)
  740. pold->next = pmap->next;
  741. else
  742. root = pmap->next;
  743. free(pmap);
  744. }
  745. #ifdef USE_IPV6
  746. if (sockprotocol(dcc[idx].sock) == AF_INET6)
  747. putlog(LOG_DEBUG, "*", "Closing IPv6 listening port %d", dcc[idx].port);
  748. else
  749. #endif /* USE_IPV6 */
  750. putlog(LOG_DEBUG, "*", "Closing IPv4 listening port %d", dcc[idx].port);
  751. killsock(dcc[idx].sock);
  752. lostdcc(idx);
  753. return idx;
  754. }
  755. }
  756. }
  757. if (idx < 0) {
  758. if (off) {
  759. putlog(LOG_ERRORS, "*", "No such listening port open - %d", lport);
  760. return idx;
  761. }
  762. /* make new one */
  763. if (dcc_total >= max_dcc) {
  764. putlog(LOG_ERRORS, "*", "Can't open listening port - no more DCC Slots");
  765. } else {
  766. #ifdef USE_IPV6
  767. i6 = open_listen_by_af(&port, AF_INET6);
  768. if (i6 < 0) {
  769. putlog(LOG_ERRORS, "*", "Can't open IPv6 listening port %d - %s", port,
  770. i6 == -1 ? "it's taken." : "couldn't assign ip.");
  771. } else {
  772. /* now setup ipv4/ipv6 listening port */
  773. idx = new_dcc(&DCC_TELNET, 0);
  774. dcc[idx].addr = 0L;
  775. strcpy(dcc[idx].host6, myipstr(6));
  776. dcc[idx].port = port;
  777. dcc[idx].sock = i6;
  778. dcc[idx].timeval = now;
  779. strcpy(dcc[idx].nick, "(telnet6)");
  780. strcpy(dcc[idx].host, "*");
  781. putlog(LOG_DEBUG, "*", "Listening on IPv6 at telnet port %d", port);
  782. }
  783. i = open_listen_by_af(&port, AF_INET);
  784. #else
  785. i = open_listen(&port);
  786. #endif /* USE_IPV6 */
  787. if (i < 0) {
  788. putlog(LOG_ERRORS, "*", "Can't open IPv4 listening port %d - %s", port,
  789. i == -1 ? "it's taken." : "couldn't assign ip.");
  790. } else {
  791. /* now setup ipv4 listening port */
  792. idx = new_dcc(&DCC_TELNET, 0);
  793. dcc[idx].addr = iptolong(getmyip());
  794. dcc[idx].port = port;
  795. dcc[idx].sock = i;
  796. dcc[idx].timeval = now;
  797. strcpy(dcc[idx].nick, "(telnet)");
  798. strcpy(dcc[idx].host, "*");
  799. putlog(LOG_DEBUG, "*", "Listening on IPv4 at telnet port %d", port);
  800. }
  801. #ifdef USE_IPV6
  802. if (i > 0 || i6 > 0) {
  803. #else
  804. if (i > 0) {
  805. #endif /* USE_IPV6 */
  806. if (!pmap) {
  807. pmap = (struct portmap *) my_calloc(1, sizeof(struct portmap));
  808. pmap->next = root;
  809. root = pmap;
  810. }
  811. pmap->realport = realport;
  812. pmap->mappedto = port;
  813. }
  814. }
  815. }
  816. /* if one of the protocols failed, the one which worked will be returned
  817. * if both were successful, it wont matter which idx is returned, because the
  818. * code reading listen_all will only be reading dcc[idx].port, which would be
  819. * open on both protocols.
  820. * -bryan (10/29/03)
  821. */
  822. return idx;
  823. }
  824. void
  825. identd_open()
  826. {
  827. int idx;
  828. int i = -1;
  829. port_t port = 113;
  830. for (idx = 0; idx < dcc_total; idx++)
  831. if (dcc[idx].type == &DCC_IDENTD_CONNECT)
  832. return; /* it's already open :) */
  833. idx = -1;
  834. identd_hack = 1;
  835. #ifdef USE_IPV6
  836. i = open_listen_by_af(&port, AF_INET6);
  837. #else
  838. i = open_listen(&port);
  839. #endif /* USE_IPV6 */
  840. identd_hack = 0;
  841. if (i >= 0) {
  842. idx = new_dcc(&DCC_IDENTD_CONNECT, 0);
  843. if (idx >= 0) {
  844. egg_timeval_t howlong;
  845. dcc[idx].addr = iptolong(getmyip());
  846. dcc[idx].port = port;
  847. dcc[idx].sock = i;
  848. dcc[idx].timeval = now;
  849. strcpy(dcc[idx].nick, "(identd)");
  850. strcpy(dcc[idx].host, "*");
  851. putlog(LOG_DEBUG, "*", "Identd daemon started.");
  852. howlong.sec = 15;
  853. howlong.usec = 0;
  854. timer_create(&howlong, "identd_close()", (Function) identd_close);
  855. } else
  856. killsock(i);
  857. }
  858. }
  859. void
  860. identd_close()
  861. {
  862. for (int idx = 0; idx < dcc_total; idx++) {
  863. if (dcc[idx].type == &DCC_IDENTD_CONNECT) {
  864. killsock(dcc[idx].sock);
  865. lostdcc(idx);
  866. putlog(LOG_DEBUG, "*", "Identd daemon stopped.");
  867. break;
  868. }
  869. }
  870. }
  871. bool
  872. valid_idx(int idx)
  873. {
  874. if ((idx == -1) || (idx >= dcc_total) || (!dcc[idx].type))
  875. return 0;
  876. return 1;
  877. }
  878. int check_cmd_pass(const char *cmd, char *pass)
  879. {
  880. if (check_master_hash(NULL, pass))
  881. return 1;
  882. struct cmd_pass *cp = NULL;
  883. for (cp = cmdpass; cp; cp = cp->next)
  884. if (!egg_strcasecmp(cmd, cp->name)) {
  885. char tmp[32] = "";
  886. encrypt_cmd_pass(pass, tmp);
  887. if (!strcmp(tmp, cp->pass))
  888. return 1;
  889. return 0;
  890. }
  891. return 0;
  892. }
  893. int has_cmd_pass(const char *cmd)
  894. {
  895. struct cmd_pass *cp = NULL;
  896. for (cp = cmdpass; cp; cp = cp->next)
  897. if (!egg_strcasecmp(cmd, cp->name))
  898. return 1;
  899. return 0;
  900. }
  901. void set_cmd_pass(char *ln, int shareit)
  902. {
  903. struct cmd_pass *cp = NULL;
  904. char *cmd = NULL;
  905. cmd = newsplit(&ln);
  906. for (cp = cmdpass; cp; cp = cp->next)
  907. if (!strcmp(cmd, cp->name))
  908. break;
  909. if (cp)
  910. if (ln[0]) {
  911. /* change */
  912. strcpy(cp->pass, ln);
  913. if (shareit)
  914. botnet_send_cmdpass(-1, cp->name, cp->pass);
  915. } else {
  916. if (cp == cmdpass)
  917. cmdpass = cp->next;
  918. else {
  919. struct cmd_pass *cp2;
  920. cp2 = cmdpass;
  921. while (cp2->next != cp)
  922. cp2 = cp2->next;
  923. cp2->next = cp->next;
  924. }
  925. if (shareit)
  926. botnet_send_cmdpass(-1, cp->name, "");
  927. free(cp->name);
  928. free(cp);
  929. } else if (ln[0]) {
  930. /* create */
  931. cp = (struct cmd_pass *) my_calloc(1, sizeof(struct cmd_pass));
  932. cp->next = cmdpass;
  933. cmdpass = cp;
  934. cp->name = strdup(cmd);
  935. strcpy(cp->pass, ln);
  936. if (shareit)
  937. botnet_send_cmdpass(-1, cp->name, cp->pass);
  938. }
  939. }
  940. void cmdpass_free(struct cmd_pass *x)
  941. {
  942. struct cmd_pass *cp = NULL, *cp_n = NULL;
  943. for (cp = x; cp; cp = cp_n) {
  944. cp_n = cp->next;
  945. list_delete((struct list_type **) &x, (struct list_type *) cp);
  946. free(cp->name);
  947. free(cp);
  948. }
  949. }