1
0

update.c 12 KB

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