update.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2013 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * update.c -- part of update.mod
  22. *
  23. */
  24. #include "src/common.h"
  25. #include "src/chanprog.h"
  26. #include "src/users.h"
  27. #include "src/dcc.h"
  28. #include "src/botnet.h"
  29. #include "src/main.h"
  30. #include "src/userrec.h"
  31. #include "src/botmsg.h"
  32. #include "src/tandem.h"
  33. #include "src/misc_file.h"
  34. #include "src/net.h"
  35. #include "src/binds.h"
  36. #include "src/egg_timer.h"
  37. #include "src/misc.h"
  38. #include "src/EncryptedStream.h"
  39. #include <bdlib/src/String.h>
  40. #include <bdlib/src/base64.h>
  41. #include <netinet/in.h>
  42. #include <arpa/inet.h>
  43. #include <sys/types.h>
  44. #include <unistd.h>
  45. #include "src/mod/transfer.mod/transfer.h"
  46. #include "src/mod/compress.mod/compress.h"
  47. /* Prototypes */
  48. static void start_sending_binary(int, bool);
  49. void finish_update_stream(int idx, bd::Stream& stream);
  50. #include "update.h"
  51. extern struct dcc_table DCC_FORK_SEND, DCC_GET;
  52. static bd::Stream stream_in;
  53. int bupdating = 0;
  54. int updated = 0;
  55. /*
  56. * Botnet commands
  57. */
  58. static void update_ufno(int idx, char *par)
  59. {
  60. putlog(LOG_BOTS, "*", "binary file rejected by %s: %s",
  61. dcc[idx].nick, par);
  62. dcc[idx].status &= ~STAT_OFFEREDU;
  63. }
  64. static void update_ufyes(int idx, char *par)
  65. {
  66. if (dcc[idx].status & STAT_OFFEREDU) {
  67. // Disable update streaming until it is deemed necesary - way too slow currently
  68. if (0 && strstr(par, "stream")) {
  69. start_sending_binary(idx, 1);
  70. } else {
  71. start_sending_binary(idx, 0);
  72. }
  73. }
  74. }
  75. static void update_fileq(int idx, char *par)
  76. {
  77. if (dcc[idx].status & STAT_GETTINGU || updated)
  78. return;
  79. if (!dcc[idx].hub) {
  80. putlog(LOG_ERRORS, "*", "%s attemped to offer binary transfer - they are not a hub [likely a hack]", dcc[idx].nick);
  81. dprintf(idx, "s un You are not allowed to send me binaries.\n");
  82. botunlink(-2, dcc[idx].nick, "You are not allowed to send me binaries.");
  83. return;
  84. }
  85. if (!conf.bot->hub) {
  86. if (!conf.bot->localhub)
  87. return;
  88. } else {
  89. if (isupdatehub()) {
  90. dprintf(idx, "sb un I am the update hub, NOT YOU.\n");
  91. return;
  92. }
  93. }
  94. dprintf(idx, "sb uy stream\n");
  95. }
  96. /* us <ip> <port> <length>
  97. */
  98. static void update_ufsend(int idx, char *par)
  99. {
  100. if (!dcc[idx].hub) {
  101. putlog(LOG_ERRORS, "*", "%s attempted to initiate binary transfer - they are not a hub [likely a hack]", dcc[idx].nick);
  102. dprintf(idx, "s un You are not allowed to send me binaries.\n");
  103. botunlink(-2, dcc[idx].nick, "You are not allowed to send me binaries.");
  104. return;
  105. }
  106. char *ip = NULL, *port = NULL, s[1024] = "";
  107. int i, sock;
  108. FILE *f = NULL;
  109. putlog(LOG_BOTS, "*", "Downloading updated binary from %s", dcc[idx].nick);
  110. simple_snprintf(s, sizeof s, "%s.update.%s", tempdir, conf.bot->nick);
  111. unlink(s); /* make sure there isnt already a new binary here.. */
  112. if (dcc_total == max_dcc) {
  113. putlog(LOG_MISC, "*", "NO MORE DCC CONNECTIONS -- can't grab new binary");
  114. dprintf(idx, "sb e I can't open a DCC to you; I'm full.\n");
  115. zapfbot(idx);
  116. } else if (!(f = fopen(s, "wb"))) {
  117. putlog(LOG_MISC, "*", "CAN'T WRITE BINARY DOWNLOAD FILE!");
  118. zapfbot(idx);
  119. } else {
  120. ip = newsplit(&par);
  121. port = newsplit(&par);
  122. #ifdef USE_IPV6
  123. sock = getsock(SOCK_BINARY, AF_INET);
  124. #else
  125. sock = getsock(SOCK_BINARY); /* Don't buffer this -> mark binary. */
  126. #endif /* USE_IPV6 */
  127. int open_telnet_return = 0;
  128. if (sock < 0 || (open_telnet_return = open_telnet_dcc(sock, ip, port)) < 0) {
  129. if (open_telnet_return != -1 && sock != -1)
  130. killsock(sock);
  131. putlog(LOG_BOTS, "*", "Asynchronous connection failed!");
  132. dprintf(idx, "sb e Can't connect to you!\n");
  133. zapfbot(idx);
  134. } else {
  135. putlog(LOG_DEBUG, "*", "Connecting to %s:%s for new binary.", ip, port);
  136. i = new_dcc(&DCC_FORK_SEND, sizeof(struct xfer_info));
  137. dcc[i].addr = my_atoul(ip);
  138. dcc[i].port = atoi(port);
  139. strlcpy(dcc[i].nick, "*binary", sizeof(dcc[i].nick));
  140. dcc[i].u.xfer->filename = strdup(s);
  141. dcc[i].u.xfer->origname = dcc[i].u.xfer->filename;
  142. dcc[i].u.xfer->length = atol(par);
  143. dcc[i].u.xfer->f = f;
  144. dcc[i].sock = sock;
  145. strlcpy(dcc[i].host, dcc[idx].nick, sizeof(dcc[i].host));
  146. dcc[idx].status |= STAT_GETTINGU;
  147. }
  148. }
  149. }
  150. static void update_stream_line(int idx, char *par) {
  151. char *size = newsplit(&par);
  152. stream_in << bd::base64Decode(bd::String(par, atoi(size)));
  153. }
  154. static void update_stream_start(int idx, char *par) {
  155. if (!dcc[idx].hub) {
  156. putlog(LOG_ERRORS, "*", "%s attempted to initiate streaming binary transfer - they are not a hub [likely a hack]", dcc[idx].nick);
  157. dprintf(idx, "s un You are not allowed to send me binaries.\n");
  158. botunlink(-2, dcc[idx].nick, "You are not allowed to send me binaries.");
  159. return;
  160. }
  161. putlog(LOG_BOTS, "*", "Downloading updated binary from %s", dcc[idx].nick);
  162. dcc[idx].status |= STAT_GETTINGU;
  163. stream_in.clear();
  164. }
  165. static void update_stream_end(int idx, char *par) {
  166. if (!dcc[idx].hub) {
  167. putlog(LOG_ERRORS, "*", "%s attempted to end streaming binary transfer - they are not a hub [likely a hack]", dcc[idx].nick);
  168. dprintf(idx, "s un You are not allowed to send me binaries.\n");
  169. botunlink(-2, dcc[idx].nick, "You are not allowed to send me binaries.");
  170. return;
  171. }
  172. stream_in.seek(0, SEEK_SET);
  173. finish_update_stream(idx, stream_in);
  174. }
  175. /* Note: these MUST be sorted. */
  176. static botcmd_t C_update[] =
  177. {
  178. {"l", update_stream_line, 0},
  179. {"le", update_stream_end, 0},
  180. {"ls", update_stream_start, 0},
  181. {"u?", update_fileq, 0},
  182. {"un", update_ufno, 0},
  183. {"us", update_ufsend, 0},
  184. {"uy", update_ufyes, 0},
  185. {NULL, NULL, 0}
  186. };
  187. static void got_nu(char *botnick, char *code, char *par)
  188. {
  189. if (!par || !*par || updated)
  190. return;
  191. if (!is_hub(botnick)) {
  192. putlog(LOG_ERRORS, "*", "%s offered binary transfer - they are not a hub [likely a hack]", botnick);
  193. return;
  194. }
  195. if (!conf.bot->hub) {
  196. if (!conf.bot->localhub)
  197. return;
  198. if (!conf.bot->u || !userlist || !get_user_by_handle(userlist, botnick)) /* probably still getting userfile */
  199. return;
  200. if (uplink_idx == -1) return; // No uplink?
  201. if (!strcmp(dcc[uplink_idx].nick, botnick)) /* dont listen to our uplink.. use normal upate system.. */
  202. return;
  203. }
  204. /* needupdate? curver */
  205. time_t newbuildts = atol(newsplit(&par)); //newts
  206. newsplit(&par); //Not used (old svn revision)
  207. newsplit(&par); //Not used (new commit)
  208. if (newbuildts > buildts) {
  209. if (!conf.bot->hub) {
  210. dont_restructure = 1;
  211. putlog(LOG_MISC, "*", "Linking to %s for binary update.", botnick);
  212. botunlink(-2, dcc[uplink_idx].nick, "Restructure for update.");
  213. usleep(1000 * 500);
  214. botlink("", -3, botnick);
  215. }
  216. }
  217. }
  218. static cmd_t update_bot[] = {
  219. {"nu?", "", (Function) got_nu, NULL, 0},
  220. {NULL, NULL, NULL, NULL, 0}
  221. };
  222. void updatein(int idx, char *msg)
  223. {
  224. char *code = newsplit(&msg);
  225. const botcmd_t *cmd = search_botcmd_t((const botcmd_t*)&C_update, code, lengthof(C_update) - 1);
  226. if (cmd) {
  227. /* Found a match */
  228. (cmd->func)(idx, msg);
  229. }
  230. }
  231. void finish_update_stream(int idx, bd::Stream& stream)
  232. {
  233. char buf[1024] = "", *buf2 = NULL;
  234. char rand[7] = "";
  235. make_rand_str(rand, sizeof(rand) - 1, 0);
  236. simple_snprintf(buf, sizeof(buf), "%s/.update-%s", dirname(binname), rand);
  237. stream.writeFile(buf);
  238. fixmod(buf);
  239. buf2 = strrchr(buf, '/') + 1;
  240. putlog(LOG_DEBUG, "*", "Update binary is %zu bytes.", stream.length());
  241. putlog(LOG_MISC, "*", "Updating with binary: %s", buf2);
  242. if (updatebin(-1, buf2, 120)) {
  243. putlog(LOG_MISC, "*", "Failed to update to new binary..");
  244. unlink(buf2);
  245. } else
  246. updated = 1;
  247. }
  248. static void
  249. ulsend(int idx, const char* data, size_t datalen)
  250. {
  251. char buf[1400] = "";
  252. size_t len = simple_snprintf(buf, sizeof(buf), "sb l %zu %s", datalen-1, data);/* -1 for newline */
  253. tputs(dcc[idx].sock, buf, len);
  254. }
  255. void finish_update(int idx) {
  256. bd::Stream stream;
  257. stream.loadFile(dcc[idx].u.xfer->filename);
  258. unlink(dcc[idx].u.xfer->filename);
  259. finish_update_stream(idx, stream);
  260. }
  261. static void start_sending_binary(int idx, bool streamable)
  262. {
  263. /* module_entry *me; */
  264. char update_file[51] = "", update_fpath[DIRMAX] = "", *sysname = NULL;
  265. int i = 1, j = -1;
  266. dcc[idx].status &= ~(STAT_OFFEREDU | STAT_SENDINGU);
  267. if (bupdating) return;
  268. bupdating = 1;
  269. dcc[idx].status |= STAT_SENDINGU;
  270. putlog(LOG_BOTS, "*", "Sending binary send request to %s", dcc[idx].nick);
  271. sysname = (char *) get_user(&USERENTRY_OS, dcc[idx].user);
  272. if (!sysname || !sysname[0] || !strcmp("*", sysname)) {
  273. putlog(LOG_MISC, "*", "Cannot update \002%s\002 automatically, `uname` not returning os name.", dcc[idx].nick);
  274. return;
  275. }
  276. simple_snprintf(update_file, sizeof update_file, "wraith.%s-%s", sysname, egg_version);
  277. simple_snprintf(update_fpath, sizeof update_fpath, "%s/bins/%s", dirname(binname), update_file);
  278. // if (!can_stat(update_fpath))
  279. // simple_snprintf(update_fpath, sizeof update_fpath, "%s/%s", dirname(binname), update_file);
  280. if (!can_stat(update_fpath)) {
  281. putlog(LOG_MISC, "*", "Need to update \002%s\002 with %s but there was an error: %s", dcc[idx].nick, update_fpath,
  282. strerror(errno));
  283. dcc[idx].status &= ~(STAT_SENDINGU);
  284. bupdating = 0;
  285. return;
  286. }
  287. if (streamable) {
  288. dcc[idx].status |= STAT_SENDINGU;
  289. bd::Stream stream;
  290. stream.loadFile(update_fpath);
  291. dprintf(idx, "sb ls\n");
  292. bd::String buf;
  293. while (stream.tell() < stream.length()) {
  294. buf = bd::base64Encode(stream.read(1024));
  295. ulsend(idx, buf.c_str(), buf.length());
  296. }
  297. dprintf(idx, "sb le\n");
  298. putlog(LOG_BOTS, "*", "Completed binary file send to %s", dcc[idx].nick);
  299. dcc[idx].status &= ~STAT_SENDINGU;
  300. dcc[idx].status |= STAT_UPDATED;
  301. bupdating = 0;
  302. } else {
  303. if ((i = raw_dcc_send(update_fpath, "*binary", "(binary)", &j)) > 0) {
  304. putlog(LOG_BOTS, "*", "%s -- can't send new binary",
  305. i == DCCSEND_FULL ? "NO MORE DCC CONNECTIONS" :
  306. i == DCCSEND_NOSOCK ? "CAN'T OPEN A LISTENING SOCKET" :
  307. i == DCCSEND_BADFN ? "BAD FILE" :
  308. i == DCCSEND_FEMPTY ? "EMPTY FILE" : "UNKNOWN REASON!");
  309. dcc[idx].status &= ~(STAT_SENDINGU);
  310. bupdating = 0;
  311. } else {
  312. dcc[idx].status |= STAT_SENDINGU;
  313. strlcpy(dcc[j].host, dcc[idx].nick, sizeof(dcc[j].host)); /* Store bot's nick */
  314. dprintf(idx, "sb us %lu %d %lu\n", iptolong(getmyip()), dcc[j].port, dcc[j].u.xfer->length);
  315. }
  316. }
  317. }
  318. int cnt = 0;
  319. static void check_updates()
  320. {
  321. if (isupdatehub()) {
  322. int i;
  323. char buf[1024] = "";
  324. tand_t *bot = NULL;
  325. cnt++;
  326. if ((cnt > 5) && bupdating) bupdating = 0; /* 2 minutes should be plenty. */
  327. if (bupdating) return;
  328. cnt = 0;
  329. for (i = 0; i < dcc_total; i++) {
  330. if (dcc[i].type && dcc[i].type->flags & DCT_BOT && (dcc[i].status & STAT_SHARE) &&
  331. !(dcc[i].status & STAT_SENDINGU) && !(dcc[i].status & STAT_OFFEREDU) &&
  332. !(dcc[i].status & STAT_UPDATED)) { /* only offer binary to bots that are sharing */
  333. bot = findbot(dcc[i].nick);
  334. dcc[i].status &= ~(STAT_GETTINGU | STAT_SENDINGU | STAT_OFFEREDU);
  335. if (bot && (bot->buildts < buildts)) {
  336. putlog(LOG_DEBUG, "@", "Bot: %s has build %li/%s, offering them %li/%s", dcc[i].nick, (long)bot->buildts, bot->commit, (long) buildts, commit);
  337. dprintf(i, "sb u?\n");
  338. dcc[i].status |= STAT_OFFEREDU;
  339. }
  340. }
  341. }
  342. /* send out notice to update remote bots ... */
  343. /* 9999 is a hack to force all bots from old svn-revisions to upgrade to new git style */
  344. simple_snprintf(buf, sizeof buf, "nu? %li 9999 %s", (long) buildts, commit);
  345. putallbots(buf);
  346. }
  347. }
  348. void update_report(int idx, int details)
  349. {
  350. int i, j;
  351. if (details) {
  352. for (i = 0; i < dcc_total; i++)
  353. if (dcc[i].type && dcc[i].type == &DCC_BOT) {
  354. if (dcc[i].status & STAT_GETTINGU) {
  355. int ok = 0;
  356. for (j = 0; j < dcc_total; j++)
  357. if (dcc[j].type && ((dcc[j].type->flags & (DCT_FILETRAN | DCT_FILESEND))
  358. == (DCT_FILETRAN | DCT_FILESEND)) &&
  359. !strcasecmp(dcc[j].host, dcc[i].nick)) {
  360. dprintf(idx, "Downloading binary from %s (%d%% done)\n",
  361. dcc[i].nick,
  362. (int) (100.0 * ((float) dcc[j].status) /
  363. ((float) dcc[j].u.xfer->length)));
  364. ok = 1;
  365. break;
  366. }
  367. if (!ok)
  368. dprintf(idx, "Download binary from %s (negotiating "
  369. "botentries)\n", dcc[i].nick);
  370. } else if (dcc[i].status & STAT_SENDINGU) {
  371. for (j = 0; j < dcc_total; j++) {
  372. if (dcc[j].type && ((dcc[j].type->flags & (DCT_FILETRAN | DCT_FILESEND))
  373. == DCT_FILETRAN)
  374. && !strcasecmp(dcc[j].host, dcc[i].nick)) {
  375. if (dcc[j].type == &DCC_GET)
  376. dprintf(idx, "Sending binary to %s (%d%% done)\n",
  377. dcc[i].nick,
  378. (int) (100.0 * ((float) dcc[j].status) /
  379. ((float) dcc[j].u.xfer->length)));
  380. else
  381. dprintf(idx,
  382. "Sending binary to %s (waiting for connect)\n",
  383. dcc[i].nick);
  384. }
  385. }
  386. }
  387. }
  388. }
  389. }
  390. static void cmd_bupdate(int idx, char *par)
  391. {
  392. for (int i = 0; i < dcc_total; i++) {
  393. if (dcc[i].type && !strcasecmp(dcc[i].nick, par)) {
  394. dprintf(i, "sb u?\n");
  395. dcc[i].status &= ~(STAT_SENDINGU | STAT_UPDATED);
  396. dcc[i].status |= STAT_OFFEREDU;
  397. }
  398. }
  399. }
  400. cmd_t update_cmds[] =
  401. {
  402. {"bupdate", "a", (Function) cmd_bupdate, NULL, HUB},
  403. {NULL, NULL, NULL, NULL, 0}
  404. };
  405. void update_init()
  406. {
  407. add_builtins("bot", update_bot);
  408. add_builtins("dcc", update_cmds);
  409. if (conf.bot->hub) {
  410. timer_create_secs(30, "check_updates", (Function) check_updates);
  411. }
  412. }