]> git.lizzy.rs Git - rust.git/commitdiff
rustup: Add support for verifying remote hashes
authorErick Tryzelaar <erick.tryzelaar@gmail.com>
Mon, 1 Dec 2014 07:14:37 +0000 (23:14 -0800)
committerErick Tryzelaar <erick.tryzelaar@gmail.com>
Mon, 29 Dec 2014 14:58:49 +0000 (09:58 -0500)
src/etc/rustup.sh

index 85e15e363271be452c852b4a9d13002a14216e6f..ab6823cf6c4a438441e60930eff9f829430ce26c 100755 (executable)
@@ -244,6 +244,7 @@ create_tmp_dir() {
 probe_need CFG_CURL  curl
 probe_need CFG_TAR   tar
 probe_need CFG_FILE  file
+probe_need CFG_SHASUM shasum
 
 CFG_SRC_DIR="$(cd $(dirname $0) && pwd)/"
 CFG_SELF="$0"
@@ -431,10 +432,39 @@ CARGO_TARBALL_NAME="${CARGO_PACKAGE_NAME_AND_TRIPLE}.tar.gz"
 CARGO_LOCAL_INSTALL_DIR="${CFG_TMP_DIR}/${CARGO_PACKAGE_NAME_AND_TRIPLE}"
 CARGO_LOCAL_INSTALL_SCRIPT="${CARGO_LOCAL_INSTALL_DIR}/install.sh"
 
+verify_hash() {
+    remote_sha256="$1"
+    local_file="$2"
+
+    msg "Downloading ${remote_sha256}"
+    remote_sha256=`"${CFG_CURL}" -f "${remote_sha256}"`
+    if [ "$?" -ne 0 ]; then
+        rm -Rf "${CFG_TMP_DIR}"
+        err "Failed to download ${remote_url}"
+    fi
+
+    msg "Verifying hash"
+    local_sha256=`"${CFG_SHASUM}" -a 256 "${local_file}"`
+    if [ "$?" -ne 0 ]; then
+        rm -Rf "${CFG_TMP_DIR}"
+        err "Failed to compute hash for ${local_tarball}"
+    fi
+
+    # We only need the sha, not the filenames
+    remote_sha256=`echo ${remote_sha256} | cut -f 1 -d ' '`
+    local_sha256=`echo ${local_sha256} | cut -f 1 -d ' '`
+
+    if [ "${remote_sha256}" != "${local_sha256}" ]; then
+        rm -Rf "${CFG_TMP_DIR}"
+        err "invalid sha256.\n  ${remote_sha256}\t${remote_tarball}\n  ${local_sha256}\t${local_tarball}"
+    fi
+}
+
 # Fetch the package.
 download_package() {
     remote_tarball="$1"
     local_tarball="$2"
+    remote_sha256="${remote_tarball}.sha256"
 
     msg "Downloading ${remote_tarball} to ${local_tarball}"
 
@@ -444,6 +474,8 @@ download_package() {
         rm -Rf "${CFG_TMP_DIR}"
         err "failed to download installer"
     fi
+
+    verify_hash "${remote_sha256}" "${local_tarball}"
 }
 
 # Wrap all the commands needed to install a package.