update.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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/egg_timer.h"
  18. #include "src/misc.h"
  19. #include <netinet/in.h>
  20. #include <arpa/inet.h>
  21. #include <sys/stat.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25. #include "src/mod/transfer.mod/transfer.h"
  26. #include "src/mod/compress.mod/compress.h"
  27. /* Prototypes */
  28. static void start_sending_binary(int);
  29. static void cancel_user_xfer(int, void *);
  30. #include "update.h"
  31. extern struct userrec *userlist;
  32. extern tand_t *tandbot;
  33. extern int localhub, max_dcc;
  34. extern char tempdir[], natip[], egg_version[];
  35. extern time_t buildts;
  36. extern struct dcc_table DCC_FORK_SEND, DCC_GET;
  37. #ifdef HUB
  38. int bupdating = 0;
  39. #endif /* HUB */
  40. #ifdef LEAF
  41. int updated = 0;
  42. #endif /* LEAF */
  43. /*
  44. * Botnet commands
  45. */
  46. static void update_ufno(int idx, char *par)
  47. {
  48. putlog(LOG_BOTS, "*", "binary file rejected by %s: %s",
  49. dcc[idx].nick, par);
  50. dcc[idx].status &= ~STAT_OFFEREDU;
  51. }
  52. static void update_ufyes(int idx, char *par)
  53. {
  54. if (dcc[idx].status & STAT_OFFEREDU) {
  55. start_sending_binary(idx);
  56. }
  57. }
  58. static void update_fileq(int idx, char *par)
  59. {
  60. if (dcc[idx].status & STAT_GETTINGU) return;
  61. #ifdef LEAF
  62. if (updated) return;
  63. if (localhub) {
  64. #else
  65. if (!isupdatehub()) {
  66. #endif /* LEAF */
  67. dprintf(idx, "sb uy\n");
  68. } else if (isupdatehub()) {
  69. dprintf(idx, "sb un I am the update hub, NOT YOU.\n");
  70. }
  71. }
  72. /* us <ip> <port> <length>
  73. */
  74. static void update_ufsend(int idx, char *par)
  75. {
  76. char *ip = NULL, *port = NULL, s[1024] = "";
  77. int i, sock;
  78. FILE *f = NULL;
  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, conf.bot->nick);
  82. #else
  83. egg_snprintf(s, sizeof s, "%s.update.%s.leaf", tempdir, conf.bot->nick);
  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 = NULL;
  152. struct bot_addr *bi = NULL, *obi = NULL;
  153. bot = tandbot;
  154. if (!strcmp(bot->bot, botnick)) /* dont listen to our uplink.. use normal upate system.. */
  155. return;
  156. if (!localhub)
  157. return;
  158. if (localhub && updated)
  159. return;
  160. #endif /* LEAF */
  161. if (!par[0]) return;
  162. newts = atoi(newsplit(&par));
  163. if (newts > buildts) {
  164. #ifdef LEAF
  165. obi = get_user(&USERENTRY_BOTADDR, conf.bot->u);
  166. bi = calloc(1, sizeof(struct bot_addr));
  167. bi->uplink = strdup(botnick);
  168. bi->address = strdup(obi->address);
  169. bi->telnet_port = obi->telnet_port;
  170. bi->relay_port = obi->relay_port;
  171. bi->hublevel = obi->hublevel;
  172. set_user(&USERENTRY_BOTADDR, conf.bot->u, bi);
  173. /* Change our uplink to them */
  174. /* let cont_link restructure us.. */
  175. putlog(LOG_MISC, "*", "Changed uplink to %s for update.", botnick);
  176. botunlink(-2, bot->bot, "Restructure for update.");
  177. usleep(1000 * 500);
  178. botlink("", -3, botnick);
  179. #else
  180. putlog(LOG_MISC, "*", "I need to be updated with %lu", newts);
  181. #endif /* LEAF */
  182. }
  183. }
  184. static cmd_t update_bot[] = {
  185. {"nu?", "", (Function) got_nu, NULL}, //need update?
  186. {0, 0, 0, 0}
  187. };
  188. void updatein(int idx, char *msg)
  189. {
  190. char *code = NULL;
  191. int f, i;
  192. code = newsplit(&msg);
  193. for (f = 0, i = 0; C_update[i].name && !f; i++) {
  194. int y = egg_strcasecmp(code, C_update[i].name);
  195. if (!y)
  196. /* Found a match */
  197. (C_update[i].func)(idx, msg);
  198. if (y < 0)
  199. f = 1;
  200. }
  201. }
  202. void finish_update(int idx)
  203. {
  204. /* module_entry *me; */
  205. char buf[1024] = "", *buf2 = NULL;
  206. int i, j = -1;
  207. for (i = 0; i < dcc_total; i++)
  208. if (!egg_strcasecmp(dcc[i].nick, dcc[idx].host) && (dcc[i].type->flags & DCT_BOT))
  209. j = i;
  210. if (j == -1)
  211. return;
  212. /* NO
  213. ic = 0;
  214. next:;
  215. ic++;
  216. if (ic > 5) {
  217. putlog(LOG_MISC, "*", "COULD NOT UNCOMPRESS BINARY");
  218. return;
  219. }
  220. result = 0;
  221. result = is_compressedfile(dcc[idx].u.xfer->filename);
  222. if (result == COMPF_COMPRESSED) {
  223. uncompress_file(dcc[idx].u.xfer->filename);
  224. usleep(1000 * 500);
  225. result = is_compressedfile(dcc[idx].u.xfer->filename);
  226. if (result == COMPF_COMPRESSED)
  227. goto next;
  228. }
  229. */
  230. sprintf(buf, "%s%s", conf.homedir, strrchr(dcc[idx].u.xfer->filename, '/'));
  231. movefile(dcc[idx].u.xfer->filename, buf);
  232. chmod(buf, S_IRUSR | S_IWUSR | S_IXUSR);
  233. sprintf(buf, "%s", strrchr(buf, '/'));
  234. buf2 = buf;
  235. buf2++;
  236. putlog(LOG_MISC, "*", "Updating with binary: %s", buf2);
  237. if (updatebin(0, buf2, 1))
  238. putlog(LOG_MISC, "*", "Failed to update to new binary..");
  239. #ifdef LEAF
  240. else
  241. updated = 1;
  242. #endif /* LEAF */
  243. }
  244. static void start_sending_binary(int idx)
  245. {
  246. /* module_entry *me; */
  247. #ifdef HUB
  248. char update_file[1024] = "", buf2[1024] = "", buf3[1024] = "";
  249. struct stat sb;
  250. int i = 1;
  251. dcc[idx].status &= ~(STAT_OFFEREDU | STAT_SENDINGU);
  252. if (bupdating) return;
  253. bupdating = 1;
  254. dcc[idx].status |= STAT_SENDINGU;
  255. putlog(LOG_BOTS, "*", "Sending binary send request to %s", dcc[idx].nick);
  256. if (!strcmp("*", dcc[idx].u.bot->sysname)) {
  257. putlog(LOG_MISC, "*", "Cannot update \002%s\002 automatically, uname not returning os name.", dcc[idx].nick);
  258. return;
  259. }
  260. if (bot_hublevel(dcc[idx].user) == 999) { /* send them the leaf binary.. */
  261. sprintf(buf2, "leaf");
  262. } else {
  263. sprintf(buf2, "hub");
  264. }
  265. sprintf(update_file, "%s.%s-%s", buf2,dcc[idx].u.bot->sysname, egg_version);
  266. if (stat(update_file, &sb)) {
  267. putlog(LOG_MISC, "*", "Need to update \002%s\002 with %s, but it cannot be accessed", dcc[idx].nick, update_file);
  268. bupdating = 0;
  269. return;
  270. }
  271. sprintf(buf3, "%s.%s", tempdir, update_file);
  272. unlink(buf3);
  273. copyfile(update_file, buf3);
  274. /* NO
  275. ic = 0;
  276. next:;
  277. ic++;
  278. if (ic > 5) {
  279. putlog(LOG_MISC, "*", "COULD NOT COMPRESS BINARY");
  280. goto end;
  281. }
  282. result = 0;
  283. result = is_compressedfile(buf3);
  284. if (result == COMPF_UNCOMPRESSED) {
  285. compress_file(buf3, 9);
  286. usleep(1000 * 500);
  287. }
  288. result = is_compressedfile(buf3);
  289. if (result == COMPF_UNCOMPRESSED)
  290. goto next;
  291. end:;
  292. */
  293. if ((i = raw_dcc_send(buf3, "*binary", "(binary)", buf3)) > 0) {
  294. putlog(LOG_BOTS, "*", "%s -- can't send new binary",
  295. i == DCCSEND_FULL ? "NO MORE DCC CONNECTIONS" :
  296. i == DCCSEND_NOSOCK ? "CAN'T OPEN A LISTENING SOCKET" :
  297. i == DCCSEND_BADFN ? "BAD FILE" :
  298. i == DCCSEND_FEMPTY ? "EMPTY FILE" : "UNKNOWN REASON!");
  299. dcc[idx].status &= ~(STAT_SENDINGU);
  300. } else {
  301. dcc[idx].status |= STAT_SENDINGU;
  302. i = dcc_total - 1;
  303. strcpy(dcc[i].host, dcc[idx].nick); /* Store bot's nick */
  304. dprintf(idx, "sb us %lu %d %lu\n",
  305. iptolong(natip[0] ? (IP) inet_addr(natip) : getmyip()),
  306. dcc[i].port, dcc[i].u.xfer->length);
  307. }
  308. #endif /* HUB */
  309. }
  310. static void (*def_dcc_bot_kill) (int, void *) = 0;
  311. static void cancel_user_xfer(int idx, void *x)
  312. {
  313. int i, j, k = 0;
  314. if (idx < 0) {
  315. idx = -idx;
  316. k = 1;
  317. updatebot(-1, dcc[idx].nick, '-', 0);
  318. }
  319. if (dcc[idx].status & STAT_SHARE) {
  320. if (dcc[idx].status & STAT_GETTINGU) {
  321. j = 0;
  322. for (i = 0; i < dcc_total; i++)
  323. if (!egg_strcasecmp(dcc[i].host, dcc[idx].nick) &&
  324. ((dcc[i].type->flags & (DCT_FILETRAN | DCT_FILESEND)) ==
  325. (DCT_FILETRAN | DCT_FILESEND)))
  326. j = i;
  327. if (j != 0) {
  328. killsock(dcc[j].sock);
  329. unlink(dcc[j].u.xfer->filename);
  330. lostdcc(j);
  331. }
  332. putlog(LOG_BOTS, "*", "(Userlist download aborted.)");
  333. }
  334. if (dcc[idx].status & STAT_SENDINGU) {
  335. j = 0;
  336. for (i = 0; i < dcc_total; i++)
  337. if ((!egg_strcasecmp(dcc[i].host, dcc[idx].nick)) &&
  338. ((dcc[i].type->flags & (DCT_FILETRAN | DCT_FILESEND))
  339. == DCT_FILETRAN))
  340. j = i;
  341. if (j != 0) {
  342. killsock(dcc[j].sock);
  343. unlink(dcc[j].u.xfer->filename);
  344. lostdcc(j);
  345. }
  346. putlog(LOG_BOTS, "*", "(Userlist transmit aborted.)");
  347. }
  348. }
  349. if (!k)
  350. def_dcc_bot_kill(idx, x);
  351. }
  352. #ifdef HUB
  353. int cnt = 0;
  354. static void check_updates()
  355. {
  356. if (isupdatehub()) {
  357. int i;
  358. char buf[1024] = "";
  359. cnt++;
  360. if ((cnt > 5) && bupdating) bupdating = 0; /* 2 minutes should be plenty. */
  361. if (bupdating) return;
  362. cnt = 0;
  363. for (i = 0; i < dcc_total; i++) {
  364. if (dcc[i].type->flags & DCT_BOT && (dcc[i].status & STAT_SHARE) &&
  365. !(dcc[i].status & STAT_SENDINGU) && !(dcc[i].status & STAT_OFFEREDU) &&
  366. !(dcc[i].status & STAT_UPDATED)) { /* only offer binary to bots that are sharing */
  367. dcc[i].status &= ~(STAT_GETTINGU | STAT_SENDINGU | STAT_OFFEREDU);
  368. if ((dcc[i].u.bot->bts < buildts) && (isupdatehub())) {
  369. putlog(LOG_DEBUG, "@", "Bot: %s has build %lu, offering them %lu", dcc[i].nick, dcc[i].u.bot->bts, buildts);
  370. dprintf(i, "sb u?\n");
  371. dcc[i].status |= STAT_OFFEREDU;
  372. }
  373. }
  374. }
  375. /* send out notice to update remote bots ... */
  376. egg_snprintf(buf, sizeof buf, "nu? %lu", buildts);
  377. putallbots(buf);
  378. }
  379. }
  380. #endif /* HUB */
  381. void update_report(int idx, int details)
  382. {
  383. int i, j;
  384. if (details) {
  385. for (i = 0; i < dcc_total; i++)
  386. if (dcc[i].type == &DCC_BOT) {
  387. if (dcc[i].status & STAT_GETTINGU) {
  388. int ok = 0;
  389. for (j = 0; j < dcc_total; j++)
  390. if (((dcc[j].type->flags & (DCT_FILETRAN | DCT_FILESEND))
  391. == (DCT_FILETRAN | DCT_FILESEND)) &&
  392. !egg_strcasecmp(dcc[j].host, dcc[i].nick)) {
  393. dprintf(idx, "Downloading binary from %s (%d%% done)\n",
  394. dcc[i].nick,
  395. (int) (100.0 * ((float) dcc[j].status) /
  396. ((float) dcc[j].u.xfer->length)));
  397. ok = 1;
  398. break;
  399. }
  400. if (!ok)
  401. dprintf(idx, "Download binary from %s (negotiating "
  402. "botentries)\n", dcc[i].nick);
  403. } else if (dcc[i].status & STAT_SENDINGU) {
  404. for (j = 0; j < dcc_total; j++) {
  405. if (((dcc[j].type->flags & (DCT_FILETRAN | DCT_FILESEND))
  406. == DCT_FILETRAN)
  407. && !egg_strcasecmp(dcc[j].host, dcc[i].nick)) {
  408. if (dcc[j].type == &DCC_GET)
  409. dprintf(idx, "Sending binary to %s (%d%% done)\n",
  410. dcc[i].nick,
  411. (int) (100.0 * ((float) dcc[j].status) /
  412. ((float) dcc[j].u.xfer->length)));
  413. else
  414. dprintf(idx,
  415. "Sending binary to %s (waiting for connect)\n",
  416. dcc[i].nick);
  417. }
  418. }
  419. }
  420. }
  421. }
  422. }
  423. void update_init()
  424. {
  425. add_builtins("bot", update_bot);
  426. #ifdef HUB
  427. timer_create_secs(30, "check_updates", (Function) check_updates);
  428. #endif /* HUB */
  429. def_dcc_bot_kill = DCC_BOT.kill;
  430. DCC_BOT.kill = cancel_user_xfer;
  431. }