check-hash 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env bash
  2. set -e
  3. # Helper to check a file matches its known hash
  4. # Call it with:
  5. # $1: the path of the file containing all the expected hashes
  6. # $2: the full path to the temporary file that was downloaded, and
  7. # that is to be checked
  8. # $3: the final basename of the file, to which it will be ultimately
  9. # saved as, to be able to match it to the corresponding hashes
  10. # in the .hash file
  11. #
  12. # Exit codes:
  13. # 0: the hash file exists and the file to check matches all its hashes,
  14. # or the hash file does not exist
  15. # 1: unknown command-line option
  16. # 2: the hash file exists and the file to check does not match at least
  17. # one of its hashes
  18. # 3: the hash file exists and there was no hash to check the file against
  19. # 4: the hash file exists and at least one hash type is unknown
  20. while getopts :q OPT; do
  21. case "${OPT}" in
  22. q) exec >/dev/null;;
  23. \?) exit 1;;
  24. esac
  25. done
  26. shift $((OPTIND-1))
  27. h_file="${1}"
  28. file="${2}"
  29. base="${3}"
  30. # Bail early if no hash to check
  31. if [ -z "${h_file}" ]; then
  32. exit 0
  33. fi
  34. # Does the hash-file exist?
  35. if [ ! -f "${h_file}" ]; then
  36. printf "WARNING: no hash file for %s\n" "${base}" >&2
  37. exit 0
  38. fi
  39. # Check one hash for a file
  40. # $1: algo hash
  41. # $2: known hash
  42. # $3: file (full path)
  43. check_one_hash() {
  44. _h="${1}"
  45. _known="${2}"
  46. _file="${3}"
  47. # Note: md5 is supported, but undocumented on purpose.
  48. # Note: sha3 is not supported, since there is currently no implementation
  49. # (the NIST has yet to publish the parameters).
  50. case "${_h}" in
  51. md5|sha1) ;;
  52. sha224|sha256|sha384|sha512) ;;
  53. *) # Unknown hash, exit with error
  54. printf "ERROR: unknown hash '%s' for '%s'\n" \
  55. "${_h}" "${base}" >&2
  56. exit 4
  57. ;;
  58. esac
  59. # Do the hashes match?
  60. _hash=$( ${_h}sum "${_file}" |cut -d ' ' -f 1 )
  61. if [ "${_hash}" = "${_known}" ]; then
  62. printf "%s: OK (%s: %s)\n" "${base}" "${_h}" "${_hash}"
  63. return 0
  64. fi
  65. printf "ERROR: %s has wrong %s hash:\n" "${base}" "${_h}" >&2
  66. printf "ERROR: expected: %s\n" "${_known}" >&2
  67. printf "ERROR: got : %s\n" "${_hash}" >&2
  68. printf "ERROR: Incomplete download, or man-in-the-middle (MITM) attack\n" >&2
  69. exit 2
  70. }
  71. # Do we know one or more hashes for that file?
  72. nb_checks=0
  73. while read t h f; do
  74. case "${t}" in
  75. ''|'#'*)
  76. # Skip comments and empty lines
  77. continue
  78. ;;
  79. *)
  80. if [ "${f}" = "${base}" ]; then
  81. check_one_hash "${t}" "${h}" "${file}"
  82. : $((nb_checks++))
  83. fi
  84. ;;
  85. esac
  86. done <"${h_file}"
  87. if [ ${nb_checks} -eq 0 ]; then
  88. case " ${BR_NO_CHECK_HASH_FOR} " in
  89. *" ${base} "*)
  90. # File explicitly has no hash
  91. exit 0
  92. ;;
  93. esac
  94. printf "ERROR: No hash found for %s\n" "${base}" >&2
  95. exit 3
  96. fi