/contrib/verifybinaries/verify.sh

https://github.com/vergecurrency/VERGE · Shell · 178 lines · 134 code · 22 blank · 22 comment · 20 complexity · 110f3ebbf4bbcbb5375701254c4d573b MD5 · raw file

  1. #!/bin/bash
  2. # Copyright (c) 2016 The Bitcoin Core developers
  3. # Distributed under the MIT software license, see the accompanying
  4. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. ### This script attempts to download the signature file SHA256SUMS.asc from
  6. ### HOST1 and HOST2 and compares them.
  7. ### It first checks if the signature passes, and then downloads the files specified in
  8. ### the file, and checks if the hashes of these files match those that are specified
  9. ### in the signature file.
  10. ### The script returns 0 if everything passes the checks. It returns 1 if either the
  11. ### signature check or the hash check doesn't pass. If an error occurs the return value is 2
  12. export LC_ALL=C
  13. function clean_up {
  14. for file in $*
  15. do
  16. rm "$file" 2> /dev/null
  17. done
  18. }
  19. WORKINGDIR="/tmp/verge_verify_binaries"
  20. TMPFILE="hashes.tmp"
  21. SIGNATUREFILENAME="SHA256SUMS.asc"
  22. RCSUBDIR="test"
  23. HOST1=""
  24. HOST2=""
  25. BASEDIR="/bin/"
  26. VERSIONPREFIX="verge-core-"
  27. RCVERSIONSTRING="rc"
  28. if [ ! -d "$WORKINGDIR" ]; then
  29. mkdir "$WORKINGDIR"
  30. fi
  31. cd "$WORKINGDIR" || exit 1
  32. #test if a version number has been passed as an argument
  33. if [ -n "$1" ]; then
  34. #let's also check if the version number includes the prefix 'verge-',
  35. # and add this prefix if it doesn't
  36. if [[ $1 == "$VERSIONPREFIX"* ]]; then
  37. VERSION="$1"
  38. else
  39. VERSION="$VERSIONPREFIX$1"
  40. fi
  41. STRIPPEDLAST="${VERSION%-*}"
  42. #now let's see if the version string contains "rc" or a platform name (e.g. "osx")
  43. if [[ "$STRIPPEDLAST-" == "$VERSIONPREFIX" ]]; then
  44. BASEDIR="$BASEDIR$VERSION/"
  45. else
  46. # let's examine the last part to see if it's rc and/or platform name
  47. STRIPPEDNEXTTOLAST="${STRIPPEDLAST%-*}"
  48. if [[ "$STRIPPEDNEXTTOLAST-" == "$VERSIONPREFIX" ]]; then
  49. LASTSUFFIX="${VERSION##*-}"
  50. VERSION="$STRIPPEDLAST"
  51. if [[ $LASTSUFFIX == *"$RCVERSIONSTRING"* ]]; then
  52. RCVERSION="$LASTSUFFIX"
  53. else
  54. PLATFORM="$LASTSUFFIX"
  55. fi
  56. else
  57. RCVERSION="${STRIPPEDLAST##*-}"
  58. PLATFORM="${VERSION##*-}"
  59. VERSION="$STRIPPEDNEXTTOLAST"
  60. fi
  61. BASEDIR="$BASEDIR$VERSION/"
  62. if [[ $RCVERSION == *"$RCVERSIONSTRING"* ]]; then
  63. BASEDIR="$BASEDIR$RCSUBDIR.$RCVERSION/"
  64. fi
  65. fi
  66. else
  67. echo "Error: need to specify a version on the command line"
  68. exit 2
  69. fi
  70. #first we fetch the file containing the signature
  71. WGETOUT=$(wget -N "$HOST1$BASEDIR$SIGNATUREFILENAME" 2>&1)
  72. #and then see if wget completed successfully
  73. if [ $? -ne 0 ]; then
  74. echo "Error: couldn't fetch signature file. Have you specified the version number in the following format?"
  75. echo "[$VERSIONPREFIX]<version>-[${RCVERSIONSTRING}[0-9]] (example: ${VERSIONPREFIX}0.10.4-${RCVERSIONSTRING}1)"
  76. echo "wget output:"
  77. echo "$WGETOUT"|sed 's/^/\t/g'
  78. exit 2
  79. fi
  80. WGETOUT=$(wget -N -O "$SIGNATUREFILENAME.2" "$HOST2$BASEDIR$SIGNATUREFILENAME" 2>&1)
  81. if [ $? -ne 0 ]; then
  82. echo "HOST1 failed to provide signature file, but HOST2 did?"
  83. echo "wget output:"
  84. echo "$WGETOUT"|sed 's/^/\t/g'
  85. clean_up $SIGNATUREFILENAME
  86. exit 3
  87. fi
  88. SIGFILEDIFFS="$(diff $SIGNATUREFILENAME $SIGNATUREFILENAME.2)"
  89. if [ "$SIGFILEDIFFS" != "" ]; then
  90. echo "HOST1 and HOST2 signature files were not equal?"
  91. clean_up $SIGNATUREFILENAME $SIGNATUREFILENAME.2
  92. exit 4
  93. fi
  94. #then we check it
  95. GPGOUT=$(gpg --yes --decrypt --output "$TMPFILE" "$SIGNATUREFILENAME" 2>&1)
  96. #return value 0: good signature
  97. #return value 1: bad signature
  98. #return value 2: gpg error
  99. RET="$?"
  100. if [ $RET -ne 0 ]; then
  101. if [ $RET -eq 1 ]; then
  102. #and notify the user if it's bad
  103. echo "Bad signature."
  104. elif [ $RET -eq 2 ]; then
  105. #or if a gpg error has occurred
  106. echo "gpg error. Do you have the VERGE Core binary release signing key installed?"
  107. fi
  108. echo "gpg output:"
  109. echo "$GPGOUT"|sed 's/^/\t/g'
  110. clean_up $SIGNATUREFILENAME $SIGNATUREFILENAME.2 $TMPFILE
  111. exit "$RET"
  112. fi
  113. if [ -n "$PLATFORM" ]; then
  114. grep $PLATFORM $TMPFILE > "$TMPFILE-plat"
  115. TMPFILESIZE=$(stat -c%s "$TMPFILE-plat")
  116. if [ $TMPFILESIZE -eq 0 ]; then
  117. echo "error: no files matched the platform specified" && exit 3
  118. fi
  119. mv "$TMPFILE-plat" $TMPFILE
  120. fi
  121. #here we extract the filenames from the signature file
  122. FILES=$(awk '{print $2}' "$TMPFILE")
  123. #and download these one by one
  124. for file in $FILES
  125. do
  126. echo "Downloading $file"
  127. wget --quiet -N "$HOST1$BASEDIR$file"
  128. done
  129. #check hashes
  130. DIFF=$(diff <(sha256sum $FILES) "$TMPFILE")
  131. if [ $? -eq 1 ]; then
  132. echo "Hashes don't match."
  133. echo "Offending files:"
  134. echo "$DIFF"|grep "^<"|awk '{print "\t"$3}'
  135. exit 1
  136. elif [ $? -gt 1 ]; then
  137. echo "Error executing 'diff'"
  138. exit 2
  139. fi
  140. if [ -n "$2" ]; then
  141. echo "Clean up the binaries"
  142. clean_up $FILES $SIGNATUREFILENAME $SIGNATUREFILENAME.2 $TMPFILE
  143. else
  144. echo "Keep the binaries in $WORKINGDIR"
  145. clean_up $TMPFILE
  146. fi
  147. echo -e "Verified hashes of \n$FILES"
  148. exit 0