binary.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * binary.c -- handles:
  3. * misc update functions
  4. * md5 hash verifying
  5. *
  6. */
  7. #include "common.h"
  8. #include "binary.h"
  9. #include "crypt.h"
  10. #include "shell.h"
  11. #include "main.h"
  12. #include "salt.h"
  13. #include "misc_file.h"
  14. #ifndef CYGWIN_HACKS
  15. typedef struct encdata_struct {
  16. char prefix[16];
  17. char data[512];
  18. } encdata_t;
  19. static encdata_t encdata = {
  20. "AAAAAAAAAAAAAAAA",
  21. ""
  22. };
  23. int checked_bin_buf = 0;
  24. static char *
  25. bin_md5(const char *fname, int todo)
  26. {
  27. static char hash[MD5_HASH_LENGTH + 1] = "";
  28. unsigned char md5out[MD5_HASH_LENGTH + 1] = "", buf[17] = "";
  29. FILE *f = NULL;
  30. size_t len = 0;
  31. MD5_CTX ctx;
  32. checked_bin_buf++;
  33. if (!(f = fopen(fname, "rb")))
  34. werr(ERR_BINSTAT);
  35. MD5_Init(&ctx);
  36. while ((len = fread(buf, 1, sizeof buf - 1, f))) {
  37. if (!memcmp(buf, &encdata.prefix, 16)) {
  38. break;
  39. }
  40. MD5_Update(&ctx, buf, len);
  41. }
  42. fclose(f);
  43. MD5_Final(md5out, &ctx);
  44. strncpyz(hash, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(hash));
  45. OPENSSL_cleanse(&ctx, sizeof(ctx));
  46. if (todo == WRITE_MD5) {
  47. char *fname_bak = NULL, s[DIRMAX] = "";
  48. FILE *fn = NULL; /* the new binary */
  49. int i = 0, fd = -1;
  50. size_t size = 0;
  51. size = strlen(fname) + 2;
  52. fname_bak = calloc(1, size);
  53. egg_snprintf(fname_bak, size, "%s~", fname);
  54. size = 0;
  55. if (!(f = fopen(fname, "rb")))
  56. werr(ERR_BINSTAT);
  57. fseek(f, 0, SEEK_END);
  58. size = ftell(f);
  59. fseek(f, 0, SEEK_SET);
  60. egg_snprintf(s, sizeof s, ".bin-XXXXXX");
  61. if ((fd = mkstemp(s)) == -1 || (fn = fdopen(fd, "wb")) == NULL) {
  62. if (fd != -1) {
  63. unlink(s);
  64. close(fd);
  65. }
  66. fatal("Can't create temporary file!", 0);
  67. }
  68. while ((len = fread(buf, 1, sizeof(buf) - 1, f))) {
  69. if (i) { /* to skip bytes for hash */
  70. i -= sizeof(buf) - 1;
  71. continue;
  72. }
  73. if (fwrite(buf, sizeof(buf) - 1, 1, fn) != 1) {
  74. fclose(f);
  75. fclose(fn);
  76. unlink(s);
  77. werr(ERR_BINSTAT);
  78. }
  79. if (!memcmp(buf, &encdata.prefix, 16)) {
  80. /* now we have 512 for data :D */
  81. char *enc_hash = NULL;
  82. enc_hash = encrypt_string(SALT1, hash);
  83. fwrite(enc_hash, strlen(enc_hash), 1, fn);
  84. i = strlen(enc_hash); /* skip the next strlen(enc_hash) bytes */
  85. free(enc_hash);
  86. }
  87. }
  88. fclose(f);
  89. fclose(fn);
  90. if (movefile(fname, fname_bak))
  91. fatal("Crappy os :D", 0);
  92. if (movefile(s, fname))
  93. fatal("Crappy os :D", 0);
  94. fixmod(fname);
  95. unlink(fname_bak);
  96. unlink(s);
  97. }
  98. return hash;
  99. }
  100. void
  101. check_sum(const char *fname)
  102. {
  103. if (!encdata.data[0]) {
  104. printf("* Wrote checksum to binary. (%s)\n", bin_md5(fname, WRITE_MD5));
  105. } else {
  106. char *hash = NULL;
  107. hash = decrypt_string(SALT1, encdata.data);
  108. if (strcmp(bin_md5(fname, GET_MD5), hash)) {
  109. free(hash);
  110. unlink(fname);
  111. fatal("!! Invalid binary", 0);
  112. }
  113. free(hash);
  114. }
  115. }
  116. #endif /* !CYGWIN_HACKS */