binary.c 3.0 KB

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