update.c 12 KB

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