update.c 13 KB

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