dccutil.c 27 KB

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