update.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. #else
  93. sock = getsock(SOCK_BINARY); /* Don't buffer this -> mark binary. */
  94. #endif /* USE_IPV6 */
  95. if (sock < 0 || open_telnet_dcc(sock, ip, port) < 0) {
  96. killsock(sock);
  97. putlog(LOG_BOTS, "*", "Asynchronous connection failed!");
  98. dprintf(idx, "sb e Can't connect to you!\n");
  99. zapfbot(idx);
  100. } else {
  101. putlog(LOG_DEBUG, "*", "Connecting to %s:%s for new binary.", ip, port);
  102. i = new_dcc(&DCC_FORK_SEND, sizeof(struct xfer_info));
  103. dcc[i].addr = my_atoul(ip);
  104. dcc[i].port = atoi(port);
  105. strcpy(dcc[i].nick, "*binary");
  106. dcc[i].u.xfer->filename = strdup(s);
  107. dcc[i].u.xfer->origname = dcc[i].u.xfer->filename;
  108. dcc[i].u.xfer->length = atol(par);
  109. dcc[i].u.xfer->f = f;
  110. dcc[i].sock = sock;
  111. strcpy(dcc[i].host, dcc[idx].nick);
  112. dcc[idx].status |= STAT_GETTINGU;
  113. }
  114. }
  115. }
  116. static void update_version(int idx, char *par)
  117. {
  118. return;
  119. /* Cleanup any share flags */
  120. #ifdef HUB
  121. tand_t *bot = NULL;
  122. if (bupdating) return;
  123. dcc[idx].status &= ~(STAT_GETTINGU | STAT_SENDINGU | STAT_OFFEREDU);
  124. bot = findbot(dcc[idx].nick);
  125. if (bot && (bot->buildts < buildts) && (isupdatehub())) {
  126. putlog(LOG_DEBUG, "@", "Asking %s to accept update from me", dcc[idx].nick);
  127. dprintf(idx, "sb u?\n");
  128. dcc[idx].status |= STAT_OFFEREDU;
  129. }
  130. #endif /* HUB */
  131. }
  132. /* Note: these MUST be sorted. */
  133. static botcmd_t C_update[] =
  134. {
  135. {"u?", (Function) update_fileq},
  136. {"un", (Function) update_ufno},
  137. {"us", (Function) update_ufsend},
  138. {"uy", (Function) update_ufyes},
  139. {"v", (Function) update_version},
  140. {NULL, NULL}
  141. };
  142. static void got_nu(char *botnick, char *code, char *par)
  143. {
  144. /* needupdate? curver */
  145. time_t newts;
  146. #ifdef LEAF
  147. if (!conf.bot->u || !userlist || !get_user_by_handle(userlist, botnick)) /* probably still getting userfile */
  148. return;
  149. if (tandbot && tandbot->bot && !strcmp(tandbot->bot, botnick)) /* dont listen to our uplink.. use normal upate system.. */
  150. return;
  151. if (!localhub)
  152. return;
  153. if (localhub && updated)
  154. return;
  155. #endif /* LEAF */
  156. if (!par || !*par)
  157. return;
  158. newts = atol(newsplit(&par));
  159. if (newts > buildts) {
  160. #ifdef LEAF
  161. struct bot_addr *bi = NULL, *obi = NULL;
  162. obi = get_user(&USERENTRY_BOTADDR, conf.bot->u);
  163. bi = 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 = NULL;
  188. int f, i;
  189. code = newsplit(&msg);
  190. for (f = 0, i = 0; C_update[i].name && !f; i++) {
  191. int y = egg_strcasecmp(code, C_update[i].name);
  192. if (!y)
  193. /* Found a match */
  194. (C_update[i].func)(idx, msg);
  195. if (y < 0)
  196. f = 1;
  197. }
  198. }
  199. void finish_update(int idx)
  200. {
  201. char buf[1024] = "", *buf2 = NULL;
  202. /* NO
  203. ic = 0;
  204. next:;
  205. ic++;
  206. if (ic > 5) {
  207. putlog(LOG_MISC, "*", "COULD NOT UNCOMPRESS BINARY");
  208. return;
  209. }
  210. result = 0;
  211. result = is_compressedfile(dcc[idx].u.xfer->filename);
  212. if (result == COMPF_COMPRESSED) {
  213. uncompress_file(dcc[idx].u.xfer->filename);
  214. usleep(1000 * 500);
  215. result = is_compressedfile(dcc[idx].u.xfer->filename);
  216. if (result == COMPF_COMPRESSED)
  217. goto next;
  218. }
  219. */
  220. {
  221. FILE *f = NULL;
  222. f = fopen(dcc[idx].u.xfer->filename, "rb");
  223. fseek(f, 0, SEEK_END);
  224. putlog(LOG_DEBUG, "*", "Update binary is %lu bytes and its length: %lu status: %lu", ftell(f), dcc[idx].u.xfer->length, dcc[idx].u.xfer->length);
  225. fclose(f);
  226. }
  227. sprintf(buf, "%s%s", dirname(binname), strrchr(dcc[idx].u.xfer->filename, '/'));
  228. movefile(dcc[idx].u.xfer->filename, buf);
  229. fixmod(buf);
  230. sprintf(buf, "%s", strrchr(buf, '/'));
  231. buf2 = buf;
  232. buf2++;
  233. putlog(LOG_MISC, "*", "Updating with binary: %s", buf2);
  234. if (updatebin(0, buf2, 1))
  235. putlog(LOG_MISC, "*", "Failed to update to new binary..");
  236. #ifdef LEAF
  237. else
  238. updated = 1;
  239. #endif /* LEAF */
  240. }
  241. static void start_sending_binary(int idx)
  242. {
  243. /* module_entry *me; */
  244. #ifdef HUB
  245. char update_file[DIRMAX] = "", tmpFile[1024] = "", *sysname = NULL;
  246. int i = 1;
  247. dcc[idx].status &= ~(STAT_OFFEREDU | STAT_SENDINGU);
  248. if (bupdating) return;
  249. bupdating = 1;
  250. dcc[idx].status |= STAT_SENDINGU;
  251. putlog(LOG_BOTS, "*", "Sending binary send request to %s", dcc[idx].nick);
  252. sysname = get_user(&USERENTRY_OS, dcc[idx].user);
  253. /* FIXME: remove after 1.1.8 */
  254. if (!sysname || !sysname[0] || !sysname[1] || !sysname[2] || !strcmp(sysname, ""))
  255. sysname = dcc[idx].u.bot->sysname;
  256. if (!sysname || !sysname[0] || !strcmp("*", sysname)) {
  257. putlog(LOG_MISC, "*", "Cannot update \002%s\002 automatically, uname not returning os name.", dcc[idx].nick);
  258. return;
  259. }
  260. sprintf(update_file, "%s%s.%s-%s", dirname(binname), (bot_hublevel(dcc[idx].user) == 999) ? "leaf" : "hub",
  261. sysname, egg_version);
  262. if (!can_stat(update_file)) {
  263. putlog(LOG_MISC, "*", "Need to update \002%s\002 with %s, but it cannot be accessed", dcc[idx].nick, update_file);
  264. bupdating = 0;
  265. return;
  266. }
  267. /* copy the binary to our tempdir and send that one. */
  268. sprintf(tmpFile, "%s.%s", tempdir, update_file);
  269. unlink(tmpFile);
  270. copyfile(update_file, tmpFile);
  271. /* NO
  272. ic = 0;
  273. next:;
  274. ic++;
  275. if (ic > 5) {
  276. putlog(LOG_MISC, "*", "COULD NOT COMPRESS BINARY");
  277. goto end;
  278. }
  279. result = 0;
  280. result = is_compressedfile(tmpFile);
  281. if (result == COMPF_UNCOMPRESSED) {
  282. compress_file(buf3, 9);
  283. usleep(1000 * 500);
  284. }
  285. result = is_compressedfile(buf3);
  286. if (result == COMPF_UNCOMPRESSED)
  287. goto next;
  288. end:;
  289. */
  290. if ((i = raw_dcc_send(tmpFile, "*binary", "(binary)", tmpFile)) > 0) {
  291. putlog(LOG_BOTS, "*", "%s -- can't send new binary",
  292. i == DCCSEND_FULL ? "NO MORE DCC CONNECTIONS" :
  293. i == DCCSEND_NOSOCK ? "CAN'T OPEN A LISTENING SOCKET" :
  294. i == DCCSEND_BADFN ? "BAD FILE" :
  295. i == DCCSEND_FEMPTY ? "EMPTY FILE" : "UNKNOWN REASON!");
  296. dcc[idx].status &= ~(STAT_SENDINGU);
  297. } else {
  298. dcc[idx].status |= STAT_SENDINGU;
  299. i = dcc_total - 1;
  300. strcpy(dcc[i].host, dcc[idx].nick); /* Store bot's nick */
  301. dprintf(idx, "sb us %lu %d %lu\n",
  302. iptolong(natip[0] ? (IP) inet_addr(natip) : getmyip()),
  303. dcc[i].port, dcc[i].u.xfer->length);
  304. }
  305. #endif /* HUB */
  306. }
  307. #ifdef HUB
  308. int cnt = 0;
  309. static void check_updates()
  310. {
  311. if (isupdatehub()) {
  312. int i;
  313. char buf[1024] = "";
  314. tand_t *bot = NULL;
  315. cnt++;
  316. if ((cnt > 5) && bupdating) bupdating = 0; /* 2 minutes should be plenty. */
  317. if (bupdating) return;
  318. cnt = 0;
  319. for (i = 0; i < dcc_total; i++) {
  320. if (dcc[i].type->flags & DCT_BOT && (dcc[i].status & STAT_SHARE) &&
  321. !(dcc[i].status & STAT_SENDINGU) && !(dcc[i].status & STAT_OFFEREDU) &&
  322. !(dcc[i].status & STAT_UPDATED)) { /* only offer binary to bots that are sharing */
  323. bot = findbot(dcc[i].nick);
  324. dcc[i].status &= ~(STAT_GETTINGU | STAT_SENDINGU | STAT_OFFEREDU);
  325. if (bot && (bot->buildts < buildts) && (isupdatehub())) {
  326. putlog(LOG_DEBUG, "@", "Bot: %s has build %lu, offering them %lu", dcc[i].nick, bot->buildts, buildts);
  327. dprintf(i, "sb u?\n");
  328. dcc[i].status |= STAT_OFFEREDU;
  329. }
  330. }
  331. }
  332. /* send out notice to update remote bots ... */
  333. egg_snprintf(buf, sizeof buf, "nu? %lu", buildts);
  334. putallbots(buf);
  335. }
  336. }
  337. #endif /* HUB */
  338. void update_report(int idx, int details)
  339. {
  340. int i, j;
  341. if (details) {
  342. for (i = 0; i < dcc_total; i++)
  343. if (dcc[i].type == &DCC_BOT) {
  344. if (dcc[i].status & STAT_GETTINGU) {
  345. int ok = 0;
  346. for (j = 0; j < dcc_total; j++)
  347. if (((dcc[j].type->flags & (DCT_FILETRAN | DCT_FILESEND))
  348. == (DCT_FILETRAN | DCT_FILESEND)) &&
  349. !egg_strcasecmp(dcc[j].host, dcc[i].nick)) {
  350. dprintf(idx, "Downloading binary from %s (%d%% done)\n",
  351. dcc[i].nick,
  352. (int) (100.0 * ((float) dcc[j].status) /
  353. ((float) dcc[j].u.xfer->length)));
  354. ok = 1;
  355. break;
  356. }
  357. if (!ok)
  358. dprintf(idx, "Download binary from %s (negotiating "
  359. "botentries)\n", dcc[i].nick);
  360. #ifdef HUB
  361. } else if (dcc[i].status & STAT_SENDINGU) {
  362. for (j = 0; j < dcc_total; j++) {
  363. if (((dcc[j].type->flags & (DCT_FILETRAN | DCT_FILESEND))
  364. == DCT_FILETRAN)
  365. && !egg_strcasecmp(dcc[j].host, dcc[i].nick)) {
  366. if (dcc[j].type == &DCC_GET)
  367. dprintf(idx, "Sending binary to %s (%d%% done)\n",
  368. dcc[i].nick,
  369. (int) (100.0 * ((float) dcc[j].status) /
  370. ((float) dcc[j].u.xfer->length)));
  371. else
  372. dprintf(idx,
  373. "Sending binary to %s (waiting for connect)\n",
  374. dcc[i].nick);
  375. }
  376. }
  377. #endif /* HUB */
  378. }
  379. }
  380. }
  381. }
  382. #ifdef HUB
  383. static void cmd_bupdate(struct userrec *u, int idx, char *par)
  384. {
  385. int i;
  386. for (i = 0; i < dcc_total; i++) {
  387. if (!egg_strcasecmp(dcc[i].nick, par)) {
  388. dprintf(i, "sb u?\n");
  389. dcc[i].status &= ~(STAT_SENDINGU | STAT_UPDATED);
  390. dcc[i].status |= STAT_OFFEREDU;
  391. }
  392. }
  393. }
  394. cmd_t update_cmds[] =
  395. {
  396. {"bupdate", "a", (Function) cmd_bupdate, NULL},
  397. {NULL, NULL, NULL, NULL}
  398. };
  399. #endif /* HUB */
  400. void update_init()
  401. {
  402. add_builtins("bot", update_bot);
  403. #ifdef HUB
  404. add_builtins("dcc", update_cmds);
  405. timer_create_secs(30, "check_updates", (Function) check_updates);
  406. #endif /* HUB */
  407. }