update.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. static void update_version(int idx, char *par)
  126. {
  127. return;
  128. /* Cleanup any share flags */
  129. #ifdef HUBNO
  130. tand_t *bot = NULL;
  131. if (bupdating) return;
  132. dcc[idx].status &= ~(STAT_GETTINGU | STAT_SENDINGU | STAT_OFFEREDU);
  133. bot = findbot(dcc[idx].nick);
  134. if (bot && (bot->revision < revision) && (isupdatehub())) {
  135. putlog(LOG_DEBUG, "@", "Asking %s to accept update from me", dcc[idx].nick);
  136. dprintf(idx, "sb u?\n");
  137. dcc[idx].status |= STAT_OFFEREDU;
  138. }
  139. #endif /* HUB */
  140. }
  141. /* Note: these MUST be sorted. */
  142. static botcmd_t C_update[] =
  143. {
  144. {"u?", update_fileq, 0},
  145. {"un", update_ufno, 0},
  146. {"us", update_ufsend, 0},
  147. {"uy", update_ufyes, 0},
  148. {"v", update_version, 0}
  149. };
  150. static void got_nu(char *botnick, char *code, char *par)
  151. {
  152. if (!par || !*par || updated)
  153. return;
  154. if (!conf.bot->hub) {
  155. if (!conf.bot->localhub)
  156. return;
  157. if (!conf.bot->u || !userlist || !get_user_by_handle(userlist, botnick)) /* probably still getting userfile */
  158. return;
  159. if (tandbot && tandbot->bot && !strcmp(tandbot->bot, botnick)) /* dont listen to our uplink.. use normal upate system.. */
  160. return;
  161. }
  162. /* needupdate? curver */
  163. newsplit(&par); //newts
  164. int newrevision = atol(newsplit(&par));
  165. if (newrevision > revision) {
  166. if (!conf.bot->hub) {
  167. dont_restructure = 1;
  168. putlog(LOG_MISC, "*", "Linking to %s for binary update.", botnick);
  169. botunlink(-2, tandbot->bot, "Restructure for update.");
  170. usleep(1000 * 500);
  171. botlink("", -3, botnick);
  172. } else
  173. putlog(LOG_MISC, "*", "I need to be updated with %d", newrevision);
  174. }
  175. }
  176. static cmd_t update_bot[] = {
  177. {"nu?", "", (Function) got_nu, NULL, 0},
  178. {NULL, NULL, NULL, NULL, 0}
  179. };
  180. void updatein(int idx, char *msg)
  181. {
  182. char *code = newsplit(&msg);
  183. const botcmd_t *cmd = search_botcmd_t((const botcmd_t*)&C_update, code, sizeof(C_update)/sizeof(botcmd_t));
  184. if (cmd) {
  185. /* Found a match */
  186. (cmd->func)(idx, msg);
  187. }
  188. }
  189. void finish_update(int idx)
  190. {
  191. char buf[1024] = "", *buf2 = NULL;
  192. /* NO
  193. ic = 0;
  194. next:;
  195. ic++;
  196. if (ic > 5) {
  197. putlog(LOG_MISC, "*", "COULD NOT UNCOMPRESS BINARY");
  198. return;
  199. }
  200. result = 0;
  201. result = is_compressedfile(dcc[idx].u.xfer->filename);
  202. if (result == COMPF_COMPRESSED) {
  203. uncompress_file(dcc[idx].u.xfer->filename);
  204. usleep(1000 * 500);
  205. result = is_compressedfile(dcc[idx].u.xfer->filename);
  206. if (result == COMPF_COMPRESSED)
  207. goto next;
  208. }
  209. */
  210. {
  211. FILE *f = NULL;
  212. f = fopen(dcc[idx].u.xfer->filename, "rb");
  213. fseek(f, 0, SEEK_END);
  214. 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);
  215. fclose(f);
  216. }
  217. strlcpy(buf, conf.binpath, sizeof(buf));
  218. strlcat(buf, strrchr(dcc[idx].u.xfer->filename, '/'), sizeof(buf));
  219. movefile(dcc[idx].u.xfer->filename, buf);
  220. fixmod(buf);
  221. strlcpy(buf, strrchr(buf, '/'), sizeof(buf));
  222. buf2 = buf;
  223. buf2++;
  224. putlog(LOG_MISC, "*", "Updating with binary: %s", buf2);
  225. if (updatebin(0, buf2, 120))
  226. putlog(LOG_MISC, "*", "Failed to update to new binary..");
  227. else
  228. updated = 1;
  229. }
  230. static void start_sending_binary(int idx)
  231. {
  232. /* module_entry *me; */
  233. char update_file[51] = "", update_fpath[DIRMAX] = "", *sysname = NULL;
  234. int i = 1, j = -1;
  235. dcc[idx].status &= ~(STAT_OFFEREDU | STAT_SENDINGU);
  236. if (bupdating) return;
  237. bupdating = 1;
  238. dcc[idx].status |= STAT_SENDINGU;
  239. putlog(LOG_BOTS, "*", "Sending binary send request to %s", dcc[idx].nick);
  240. sysname = (char *) get_user(&USERENTRY_OS, dcc[idx].user);
  241. if (!sysname || !sysname[0] || !strcmp("*", sysname)) {
  242. putlog(LOG_MISC, "*", "Cannot update \002%s\002 automatically, `uname` not returning os name.", dcc[idx].nick);
  243. return;
  244. }
  245. simple_snprintf(update_file, sizeof update_file, "wraith.%s-%s", sysname, egg_version);
  246. simple_snprintf(update_fpath, sizeof update_fpath, "%s/bins/%s", conf.binpath, update_file);
  247. if (!can_stat(update_fpath))
  248. simple_snprintf(update_fpath, sizeof update_fpath, "%s/%s", conf.binpath, update_file);
  249. if (!can_stat(update_fpath)) {
  250. putlog(LOG_MISC, "*", "Need to update \002%s\002 with %s but there was an error: %s", dcc[idx].nick, update_fpath,
  251. strerror(errno));
  252. dcc[idx].status &= ~(STAT_SENDINGU);
  253. bupdating = 0;
  254. return;
  255. }
  256. #ifdef old
  257. /* copy the binary to our tempdir and send that one. */
  258. //simple_snprintf(tmpFile, sizeof(tmpFile), "%s.%s", tempdir, update_file);
  259. unlink(tmpFile);
  260. copyfile(update_fpath, tmpFile);
  261. #endif
  262. /* NO
  263. ic = 0;
  264. next:;
  265. ic++;
  266. if (ic > 5) {
  267. putlog(LOG_MISC, "*", "COULD NOT COMPRESS BINARY");
  268. goto end;
  269. }
  270. result = 0;
  271. result = is_compressedfile(tmpFile);
  272. if (result == COMPF_UNCOMPRESSED) {
  273. compress_file(buf3, 9);
  274. usleep(1000 * 500);
  275. }
  276. result = is_compressedfile(buf3);
  277. if (result == COMPF_UNCOMPRESSED)
  278. goto next;
  279. end:;
  280. */
  281. if ((i = raw_dcc_send(update_fpath, "*binary", "(binary)", &j)) > 0) {
  282. putlog(LOG_BOTS, "*", "%s -- can't send new binary",
  283. i == DCCSEND_FULL ? "NO MORE DCC CONNECTIONS" :
  284. i == DCCSEND_NOSOCK ? "CAN'T OPEN A LISTENING SOCKET" :
  285. i == DCCSEND_BADFN ? "BAD FILE" :
  286. i == DCCSEND_FEMPTY ? "EMPTY FILE" : "UNKNOWN REASON!");
  287. dcc[idx].status &= ~(STAT_SENDINGU);
  288. bupdating = 0;
  289. } else {
  290. dcc[idx].status |= STAT_SENDINGU;
  291. strlcpy(dcc[j].host, dcc[idx].nick, UHOSTLEN); /* Store bot's nick */
  292. dprintf(idx, "sb us %lu %hd %lu\n", iptolong(getmyip()), dcc[j].port, dcc[j].u.xfer->length);
  293. }
  294. }
  295. int cnt = 0;
  296. static void check_updates()
  297. {
  298. if (isupdatehub()) {
  299. int i;
  300. char buf[1024] = "";
  301. tand_t *bot = NULL;
  302. cnt++;
  303. if ((cnt > 5) && bupdating) bupdating = 0; /* 2 minutes should be plenty. */
  304. if (bupdating) return;
  305. cnt = 0;
  306. for (i = 0; i < dcc_total; i++) {
  307. if (dcc[i].type && dcc[i].type->flags & DCT_BOT && (dcc[i].status & STAT_SHARE) &&
  308. !(dcc[i].status & STAT_SENDINGU) && !(dcc[i].status & STAT_OFFEREDU) &&
  309. !(dcc[i].status & STAT_UPDATED)) { /* only offer binary to bots that are sharing */
  310. bot = findbot(dcc[i].nick);
  311. dcc[i].status &= ~(STAT_GETTINGU | STAT_SENDINGU | STAT_OFFEREDU);
  312. if (bot && (bot->revision < revision)) {
  313. putlog(LOG_DEBUG, "@", "Bot: %s has build %d, offering them %d", dcc[i].nick, bot->revision, revision);
  314. dprintf(i, "sb u?\n");
  315. dcc[i].status |= STAT_OFFEREDU;
  316. }
  317. }
  318. }
  319. /* send out notice to update remote bots ... */
  320. simple_snprintf(buf, sizeof buf, "nu? . %d", revision);
  321. putallbots(buf);
  322. }
  323. }
  324. void update_report(int idx, int details)
  325. {
  326. int i, j;
  327. if (details) {
  328. for (i = 0; i < dcc_total; i++)
  329. if (dcc[i].type && dcc[i].type == &DCC_BOT) {
  330. if (dcc[i].status & STAT_GETTINGU) {
  331. int ok = 0;
  332. for (j = 0; j < dcc_total; j++)
  333. if (dcc[j].type && ((dcc[j].type->flags & (DCT_FILETRAN | DCT_FILESEND))
  334. == (DCT_FILETRAN | DCT_FILESEND)) &&
  335. !egg_strcasecmp(dcc[j].host, dcc[i].nick)) {
  336. dprintf(idx, "Downloading binary from %s (%d%% done)\n",
  337. dcc[i].nick,
  338. (int) (100.0 * ((float) dcc[j].status) /
  339. ((float) dcc[j].u.xfer->length)));
  340. ok = 1;
  341. break;
  342. }
  343. if (!ok)
  344. dprintf(idx, "Download binary from %s (negotiating "
  345. "botentries)\n", dcc[i].nick);
  346. } else if (dcc[i].status & STAT_SENDINGU) {
  347. for (j = 0; j < dcc_total; j++) {
  348. if (dcc[j].type && ((dcc[j].type->flags & (DCT_FILETRAN | DCT_FILESEND))
  349. == DCT_FILETRAN)
  350. && !egg_strcasecmp(dcc[j].host, dcc[i].nick)) {
  351. if (dcc[j].type == &DCC_GET)
  352. dprintf(idx, "Sending binary to %s (%d%% done)\n",
  353. dcc[i].nick,
  354. (int) (100.0 * ((float) dcc[j].status) /
  355. ((float) dcc[j].u.xfer->length)));
  356. else
  357. dprintf(idx,
  358. "Sending binary to %s (waiting for connect)\n",
  359. dcc[i].nick);
  360. }
  361. }
  362. }
  363. }
  364. }
  365. }
  366. static void cmd_bupdate(int idx, char *par)
  367. {
  368. for (int i = 0; i < dcc_total; i++) {
  369. if (dcc[i].type && !egg_strcasecmp(dcc[i].nick, par)) {
  370. dprintf(i, "sb u?\n");
  371. dcc[i].status &= ~(STAT_SENDINGU | STAT_UPDATED);
  372. dcc[i].status |= STAT_OFFEREDU;
  373. }
  374. }
  375. }
  376. cmd_t update_cmds[] =
  377. {
  378. {"bupdate", "a", (Function) cmd_bupdate, NULL, HUB},
  379. {NULL, NULL, NULL, NULL, 0}
  380. };
  381. void update_init()
  382. {
  383. add_builtins("bot", update_bot);
  384. add_builtins("dcc", update_cmds);
  385. if (conf.bot->hub) {
  386. timer_create_secs(30, "check_updates", (Function) check_updates);
  387. }
  388. }