update.c 12 KB

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