update.c 12 KB

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