Răsfoiți Sursa

* Rewrote the checksum writing to not malloc() 18 mbs :)

svn: 879
Bryan Drewery 22 ani în urmă
părinte
comite
a6b9fd12d3
2 a modificat fișierele cu 75 adăugiri și 68 ștergeri
  1. 62 46
      src/binary.c
  2. 13 22
      src/main.c

+ 62 - 46
src/binary.c

@@ -24,76 +24,92 @@ bin_md5(const char *fname, int todo)
 {
   static char hash[MD5_HASH_LENGTH + 1] = "";
   unsigned char md5out[MD5_HASH_LENGTH + 1] = "";
-  char *buf = NULL, *p = NULL, *fname_bak = NULL;
+  char buf[17] = "";
   FILE *f = NULL;
-  size_t size = 0, size_p = 0;
+  size_t len = 0;
   MD5_CTX ctx;
 
   if (!(f = fopen(fname, "rb")))
     werr(ERR_BINSTAT);
 
   MD5_Init(&ctx);
-
-  size = strlen(fname) + 2;
-  fname_bak = calloc(1, size);
-  egg_snprintf(fname_bak, size, "%s~", fname);
-  size = 0;
-
-  fseek(f, 0, SEEK_END);
-  size = ftell(f);
-  fseek(f, 0, SEEK_SET);
-
-  buf = calloc(1, size + 1);
-
-  if (fread(buf, 1, size, f) != size)
-    fatal("Can't read binary", 0);
-
-  p = buf;
-  while (p < (buf + size - 4)) {
-    if (!strncmp(p, STR("AAAAAAAA"), 8))        /* this STR() is *REQUIRED* */
+  while ((len = fread(buf, 1, sizeof buf - 1, f))) {
+    if (!strncmp(buf, STR("AAAAAAAAAAAAAAAA"), 16)) {
       break;
-    p += 4;
-    size_p += 4;
+    }
+    MD5_Update(&ctx, buf, len);
   }
 
-  if (p >= (buf + size - 4))
-    fatal("Shit out of luck brotha", 0);
-
-  p += 16;
-  size_p += 16;
-  /* now we have 512 for data :D */
-
-  MD5_Update(&ctx, buf, size_p);
+  fclose(f);
   MD5_Final(md5out, &ctx);
   strncpyz(hash, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(hash));
   OPENSSL_cleanse(&ctx, sizeof(ctx));
 
   if (todo == WRITE_MD5) {
-    char *enc_hash = NULL;
-
-    enc_hash = encrypt_string(SALT1, hash);
-    strncpyz(p, enc_hash, strlen(enc_hash) + 1);
-    size += strlen(enc_hash);
-    free(enc_hash);
-    fclose(f);
-
-    movefile(fname, fname_bak);
-
-    if (!(f = fopen(fname, "wb"))) {
-      movefile(fname_bak, fname);
+    char *fname_bak = NULL, s[DIRMAX] = "";
+    FILE *fn = NULL;			/* the new binary */
+    int i = 0, fd = -1;
+    size_t size = 0;
+   
+
+    size = strlen(fname) + 2;
+    fname_bak = calloc(1, size);
+    egg_snprintf(fname_bak, size, "%s~", fname);
+    size = 0;
+
+    if (!(f = fopen(fname, "rb")))
       werr(ERR_BINSTAT);
+    
+    fseek(f, 0, SEEK_END);
+    size = ftell(f);
+    fseek(f, 0, SEEK_SET);
+
+    egg_snprintf(s, sizeof s, "%s.bin-XXXXXX", tempdir);
+    if ((fd = mkstemp(s)) == -1 || (fn = fdopen(fd, "wb")) == NULL) {
+      if (fd != -1) {
+        unlink(s);
+        close(fd);
+      }
+      fatal("Can't write to tempdir!", 0);
     }
 
-    if (fwrite(buf, 1, size, f) != size) {
-      movefile(fname_bak, fname);
-      fatal("Failed to re-write binary", 0);
+    while ((len = fread(buf, 1, sizeof buf - 1, f))) {
+      if (i) {		/* to skip bytes for hash */
+        i -= 16;
+        continue;
+      }
+      if (fwrite(buf, 1, sizeof buf - 1, fn) != (sizeof(buf) - 1)) {
+        fclose(f);
+        fclose(fn);
+        unlink(s);
+        werr(ERR_BINSTAT);
+      }
+
+      if (!strncmp(buf, STR("AAAAAAAAAAAAAAAA"), 16)) {
+        /* now we have 512 for data :D */
+        char *enc_hash = NULL;
+
+        enc_hash = encrypt_string(SALT1, hash);
+        fwrite(enc_hash, 1, strlen(enc_hash), fn);
+        i = strlen(enc_hash);			/* skip the next strlen(enc_hash) bytes */
+        free(enc_hash);
+      }
     }
+
     fclose(f);
+    fclose(fn);
+
+    if (movefile(fname, fname_bak))
+      fatal("Crappy os :D", 0);
+
+    if (movefile(s, fname))
+      fatal("Crappy os :D", 0);
+
     fixmod(fname);
     unlink(fname_bak);
+    unlink(s);
   }
 
-  free(buf);
   return hash;
 }
 #endif /* !CYGWIN_HACKS */

+ 13 - 22
src/main.c

@@ -146,7 +146,7 @@ void fatal(const char *s, int recoverable)
 #endif /* LEAF */
 
   if (s[0])
-    putlog(LOG_MISC, "*", "* %s", s);
+    putlog(LOG_MISC, "*", "!*! %s", s);
 
 /*  flushlogs(); */
 #ifdef HAVE_SSL
@@ -695,14 +695,22 @@ int main(int argc, char **argv)
   srandom(now % (getpid() + getppid()));
   myuid = geteuid();
 
+#ifdef HUB
+  egg_snprintf(tempdir, sizeof tempdir, "%s/tmp/", confdir());
+#endif /* HUB */
+#ifdef LEAF 
+  egg_snprintf(tempdir, sizeof tempdir, "%s/.../", confdir());
+#endif /* LEAF */
+#ifdef CYGWIN_HACKS
+  egg_snprintf(tempdir, sizeof tempdir, "%s/tmp/", confdir());
+#endif /* CYGWIN_HACKS */
+  clear_tmp();		/* clear out the tmp dir, no matter if we are localhub or not */
+
   binname = getfullbinname(argv[0]);
 
 #ifndef CYGWIN_HACKS
-if (0) {
-  /*printf("Verifying Binary MD5 HASH\n"); */
   if (!encdata.data[1]) {
-    /* printf("Generated Hash (First time ran)\n"); */
-    bin_md5(binname, WRITE_MD5);
+    printf("* Wrote checksum to binary. (%s)\n", bin_md5(binname, WRITE_MD5));
   } else {
     char *hash = NULL;
 
@@ -715,28 +723,11 @@ if (0) {
     }
     free(hash);
   }
-  /*
-  printf("Internal HASH: %s\nShould be: %s\n", encdata.data, bin_md5(binname, GET_MD5));
-  printf("Verified.\n");
-  */
-}
 #endif /* !CYGWIN_HACKS */
 #ifdef HUB
   egg_snprintf(userfile, 121, "%s/.u", confdir());
 #endif /* HUB */
 
-#ifdef HUB
-  egg_snprintf(tempdir, sizeof tempdir, "%s/tmp/", confdir());
-#endif /* HUB */
-#ifdef LEAF 
-  egg_snprintf(tempdir, sizeof tempdir, "%s/.../", confdir());
-#endif /* LEAF */
-#ifdef CYGWIN_HACKS
-  egg_snprintf(tempdir, sizeof tempdir, "%s/tmp/", confdir());
-#endif /* CYGWIN_HACKS */
-
-  clear_tmp();		/* clear out the tmp dir, no matter if we are localhub or not */
-
   /* just load everything now, won't matter if it's loaded if the bot has to suicide on startup */
   init_settings();
   hooks_init();