update.c 12 KB

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