1
0

update.c 12 KB

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