update.c 12 KB

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