]> git.proxmox.com Git - mirror_smartmontools-debian.git/blobdiff - update-smart-drivedb.in
import smartmontools 7.0
[mirror_smartmontools-debian.git] / update-smart-drivedb.in
index 98434446c3004195f9c2cc24338df299f33b7215..4b23c7f05bbfe8df938cdcb9acd5cb7fd21999f4 100644 (file)
@@ -2,22 +2,19 @@
 #
 # smartmontools drive database update script
 #
-# Copyright (C) 2010-16 Christian Franke
+# Home page of code is: http://www.smartmontools.org
 #
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2, or (at your option)
-# any later version.
+# Copyright (C) 2010-18 Christian Franke
 #
-# You should have received a copy of the GNU General Public License
-# (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
+# SPDX-License-Identifier: GPL-2.0-or-later
 #
-# $Id: update-smart-drivedb.in 4224 2016-02-26 20:29:24Z chrfranke $
+# $Id: update-smart-drivedb.in 4879 2018-12-28 22:05:12Z chrfranke $
 #
 
 set -e
 
 # Set by config.status
+@ENABLE_SCRIPTPATH_TRUE@export PATH="@scriptpath@"
 PACKAGE="@PACKAGE@"
 VERSION="@VERSION@"
 prefix="@prefix@"
@@ -36,13 +33,22 @@ BRANCH="@DRIVEDB_BRANCH@"
 # Default drivedb location
 DRIVEDB="$drivedbdir/drivedb.h"
 
+# GnuPG used to verify signature (disabled if empty)
+GPG="@gnupg@"
+
 # Smartctl used for syntax check
 SMARTCTL="$sbindir/smartctl"
 
+# PATH information for help and error messages
+@ENABLE_SCRIPTPATH_FALSE@pathinfo='$PATH'
+@ENABLE_SCRIPTPATH_TRUE@pathinfo="'$PATH'"
+
 myname=$0
 
 usage()
 {
+@ENABLE_SCRIPTPATH_TRUE@  pathinfo="
+@ENABLE_SCRIPTPATH_TRUE@                   $pathinfo"
   cat <<EOF
 smartmontools $VERSION drive database update script
 
@@ -51,22 +57,25 @@ Usage: $myname [OPTIONS] [DESTFILE]
   -s SMARTCTL     Use SMARTCTL for syntax check ('-s -' to disable)
                   [default: $SMARTCTL]
   -t TOOL         Use TOOL for download: $os_dltools
-                  [default: first one found in PATH]
+                  [default: first one found in $pathinfo]
   -u LOCATION     Use URL of LOCATION for download:
-                    sf (Sourceforge code browser via HTTPS)
-                    svn (SVN repository via HTTPS) [default]
-                    svni (SVN repository via HTTP)
-                    trac (Trac code browser via HTTPS)
+                    github (GitHub mirror of SVN repository)
+                    sf (Sourceforge code browser)
+                    svn (SVN repository) [default]
+                    svni (SVN repository via HTTP instead of HTTPS)
+                    trac (Trac code browser)
+  --trunk         Download from SVN trunk (may require '--no-verify')
   --cacert FILE   Use CA certificates from FILE to verify the peer
   --capath DIR    Use CA certificate files from DIR to verify the peer
   --insecure      Don't abort download if certificate verification fails
+  --no-verify     Don't verify signature
+  --export-key    Print the OpenPGP/GPG public key block
   --dryrun        Print download commands only
   -v              Verbose output
 
 Updates $DRIVEDB
-or DESTFILE from smartmontools SVN repository.
-Tries to download first from branch $BRANCH
-and then from trunk.
+or DESTFILE from branches/$BRANCH of smartmontools
+SVN repository.
 EOF
   exit 1
 }
@@ -77,6 +86,14 @@ error()
   exit 1
 }
 
+err_notfound()
+{
+  case $1 in
+    */*) error "$1: not found $2" ;;
+    *)   error "$1: not found in $pathinfo $2" ;;
+  esac
+}
+
 warning()
 {
   echo "$myname: (Warning) $*" >&2
@@ -85,6 +102,10 @@ warning()
 selecturl()
 {
   case $1 in
+    github)  # https://github.com/smartmontools/smartmontools/raw/origin/$BRANCH/smartmontools/drivedb.h
+             # https://github.com/smartmontools/smartmontools/raw/master/smartmontools/drivedb.h
+          # redirected to:
+          url='https://raw.githubusercontent.com/smartmontools/smartmontools/master/smartmontools/drivedb.h' ;;
     sf)   url='https://sourceforge.net/p/smartmontools/code/HEAD/tree/trunk/smartmontools/drivedb.h?format=raw' ;;
     svn)  url='https://svn.code.sf.net/p/smartmontools/code/trunk/smartmontools/drivedb.h' ;;
     svni) url='http://svn.code.sf.net/p/smartmontools/code/trunk/smartmontools/drivedb.h' ;;
@@ -149,7 +170,7 @@ vrun2()
 # download URL FILE
 download()
 {
-  local f u se rc
+  local f u rc
   u=$1; f=$2
   rc=0
 
@@ -207,15 +228,138 @@ download()
   return $rc
 }
 
+# check_file FILE FIRST_CHAR MIN_SIZE MAX_SIZE
+check_file()
+{
+  local firstchar f maxsize minsize size
+  test -z "$dryrun" || return 0
+  f=$1; firstchar=$2; minsize=$3; maxsize=$4
+
+  # Check first chars
+  case `dd if="$f" bs=1 count=1 2>/dev/null` in
+    $firstchar) ;;
+    \<) echo "HTML error message"; return 1 ;;
+    *)   echo "unknown file contents"; return 1 ;;
+  esac
+
+  # Check file size
+  size=`wc -c < "$f"`
+  if test "$size" -lt $minsize; then
+    echo "too small file size $size bytes"
+    return 1
+  fi
+  if test "$size" -gt $maxsize; then
+    echo "too large file size $size bytes"
+    return 1
+  fi
+  return 0
+}
+
+# unexpand_svn_id < INFILE > OUTFILE
+unexpand_svn_id()
+{
+  sed 's,\$''Id'': drivedb\.h [0-9][0-9]* 2[-0-9]* [012][:0-9]*Z [a-z][a-z0-9]* \$,$''Id''$,'
+}
+
+# Smartmontools Signing Key (through 2020)
+# <smartmontools-database@listi.jpberlin.de>
+# Key ID 721042C5
+public_key="\
+-----BEGIN PGP PUBLIC KEY BLOCK-----
+
+mQINBFwmhpUBEADRoOZaXq13MrqyAmbGe6FlHi6P9ujsT/SJGhTiAoN3W1X56Dbm
+KP21nO9ZAjdXnvA2OmzppfCUX7v5Q3/TG3vN3WwfyQIO/dgSaTrGa1E8odbHEGc7
+rhzYA8ekAn3TmxhOrEUTcRIogumW0zlQewHOlTe0OYsxat6/N8l3Cqn28HwZUpRH
+MrJW3RgefFihQGEhXlnfzo+Tltl14IriURbwBZIDeZOk2AWLGweI0+zqTgYSbF5A
+tI5rXO1QDeoyBYZhSX3MtnncwPdCnxoRasizU5w3KoZWYyKAc5bxJBJgUUp9HDOu
+ATgNqekc8j28x/cUAWerXe183SBYQp0QkzMPbmE9TCGW3GjtW+Kk/NDbNe8ufj6O
+hk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V/2Ut
+/5Hl/83zFFoNA/kQKVz8kIDqgRcxC+zY2VJ4eTKHyQDvXygVk8wnKTBae3gX+CIZ
+qJHPXiiygHlbl31Mi3G1Iaxu57dP6ocV0vX1dytKSwd4Rbviwwb4L76o/tVT9t3G
+wFM15uK1SqtnAaiaktEdMi3XI4d01H3VUVz/iR0XQbf13RZoEM6CJWmsQ/qvYlwk
+bKOdlahjoHrFlkhADSBaO9N1OZp3OYDjziIujMdt2IPKnmM=
+=0uFV
+-----END PGP PUBLIC KEY BLOCK-----
+"
+
+# gpg_verify FILE.asc FILE
+gpg_verify()
+{
+  local gnupgtmp opts out rc
+  opts="--quiet ${q:+--no-secmem-warnin} --batch --no-tty"
+
+  # Create temp home dir
+  gnupgtmp="$tmpdir/.gnupg.$$.tmp"
+  rm -f -r "$gnupgtmp"
+  mkdir "$gnupgtmp" || exit 1
+  chmod 0700 "$gnupgtmp"
+
+  # Import public key
+  "$GPG" $opts --homedir="$gnupgtmp" --import <<EOF
+$public_key
+EOF
+  test $? = 0 || exit 1
+
+  # Verify
+  rc=0
+  out=`"$GPG" $opts --homedir="$gnupgtmp" --verify "$1" "$2" </dev/null 2>&1` || rc=1
+  vecho "$out"
+
+  rm -f -r "$gnupgtmp"
+  return $rc
+}
+
+# mv_all PREFIX OLD NEW
+mv_all()
+{
+  mv "${1}${2}"         "${1}${3}"
+  mv "${1}${2}.raw"     "${1}${3}.raw"
+  mv "${1}${2}.raw.asc" "${1}${3}.raw.asc"
+}
+
 # Parse options
 smtctl=$SMARTCTL
 tool=
 url=
 q="-q"
 dryrun=
+trunk=
 cacert=
 capath=
 insecure=
+no_verify=
 
 while true; do case $1 in
   -s)
@@ -237,6 +381,9 @@ while true; do case $1 in
   --dryrun)
     dryrun=t ;;
 
+  --trunk)
+    trunk=trunk ;;
+
   --cacert)
     shift; test -n "$1" || usage
     cacert=$1 ;;
@@ -248,6 +395,15 @@ while true; do case $1 in
   --insecure)
     insecure=t ;;
 
+  --no-verify)
+    no_verify=t ;;
+
+  --export-key)
+    cat <<EOF
+$public_key
+EOF
+    exit 0 ;;
+
   -*)
     usage ;;
 
@@ -269,7 +425,7 @@ if [ -z "$tool" ]; then
       break
     fi
   done
-  test -n "$tool" || error "found none of: $os_dltools"
+  test -n "$tool" || error "found none of '$os_dltools' in $pathinfo"
 fi
 
 test -n "$url" || selecturl "svn"
@@ -291,95 +447,105 @@ case "$tool:$insecure" in
   lynx:t) warning "'--insecure' is ignored if '-t lynx' is used" ;;
 esac
 
-# Try possible branch first, then trunk
-errmsg=
-errmsg2=
-for location in "branches/$BRANCH" "trunk"; do
-  test -z "$errmsg" || errmsg2=$errmsg
-  vecho "Download from $location with $tool"
-
-  # Adjust URL
-  case $location in
-    trunk) src=$url ;;
-    *)     src=`echo "$url" | sed "s,/trunk/,/$location/,"` ;;
-  esac
-
-  # Download
-  test -n "$dryrun" || rm -f "$DEST.new" || exit 1
-  rc=0
-  download "$src" "$DEST.new" || rc=$?
-  test -z "$dryrun" || continue
+# Check for smartctl
+if [ "$smtctl" != "-" ]; then
+  "$smtctl" -V >/dev/null 2>&1 \
+  || err_notfound "$smtctl" "('-s -' to ignore)"
+fi
 
-  errmsg=
-  if [ $rc != 0 ]; then
-    errmsg="download from $location failed ($tool: exit $rc)"
-    continue
-  fi
+# Check for GnuPG
+if [ -z "$no_verify" ]; then
+  test -n "$GPG" \
+  || error "GnuPG is not available ('--no-verify' to ignore)"
+  "$GPG" --version >/dev/null 2>&1 \
+  || err_notfound "$GPG" "('--no-verify' to ignore)"
+fi
 
-  # Check file contents
-  case `sed 1q "$DEST.new"` in
-    /*) ;;
-    \<*)
-      errmsg="download from $location failed (HTML error message)"
-      continue ;;
-    *)
-      errmsg="download from $location failed (Unknown file contents)"
-      continue ;;
-  esac
+# Use destination directory as temp directory for gpg
+tmpdir=`dirname "$DEST"`
 
-  # Check file size
-  size=`wc -c < "$DEST.new"`
-  if [ "$size" -lt 10000 ]; then
-    errmsg="download from $location failed (too small file size $size bytes)"
-    continue
-  fi
-  if [ "$size" -gt 1000000 ]; then
-    errmsg="download from $location failed (too large file size $size bytes)"
-    break
-  fi
+# Adjust URLs
+src=`echo "$url" | sed -e "s,/trunk/,/branches/$BRANCH/," \
+                       -e "s,/master/,/origin/$BRANCH/,"`
+src_asc=`echo "$src" | sed "s,/drivedb\.h,/drivedb.h.raw.asc,"`
+test -z "$trunk" || src=$url
 
-  break
-done
+# Download
+test -n "$dryrun" || rm -f "$DEST.new" "$DEST.new.raw" "$DEST.new.raw.asc"
 
-test -z "$dryrun" || exit 0
+vecho "Download ${trunk:-branches/$BRANCH}/drivedb.h with $tool"
+rc=0
+download "$src" "$DEST.new" || rc=$?
+if [ $rc != 0 ]; then
+  rm -f "$DEST.new"
+  error "${trunk:-$BRANCH}/drivedb.h: download failed ($tool: exit $rc)"
+fi
+if ! errmsg=`check_file "$DEST.new" '/' 10000 1000000`; then
+  mv "$DEST.new" "$DEST.error"
+  error "$DEST.error: $errmsg"
+fi
 
-if [ -n "$errmsg" ]; then
+vecho "Download branches/$BRANCH/drivedb.h.raw.asc with $tool"
+rc=0
+download "$src_asc" "$DEST.new.raw.asc" || rc=$?
+if [ $rc != 0 ]; then
+  rm -f "$DEST.new" "$DEST.new.raw.asc"
+  error "$BRANCH/drivedb.h.raw.asc: download failed ($tool: exit $rc)"
+fi
+if ! errmsg=`check_file "$DEST.new.raw.asc" '-' 200 2000`; then
   rm -f "$DEST.new"
-  test -z "$errmsg2" || echo "$myname: $errmsg2" >&2
-  error "$errmsg"
+  mv "$DEST.new.raw.asc" "$DEST.error.raw.asc"
+  error "$DEST.error.raw.asc: $errmsg"
 fi
 
-# Adjust timestamp and permissions
-touch "$DEST.new"
-chmod 0644 "$DEST.new"
+test -z "$dryrun" || exit 0
+
+# Create raw file with unexpanded SVN Id
+# (This assumes newlines are LF and not CR/LF)
+unexpand_svn_id < "$DEST.new" > "$DEST.new.raw"
+
+# Adjust timestamps and permissions
+touch "$DEST.new" "$DEST.new.raw" "$DEST.new.raw.asc"
+chmod 0644 "$DEST.new" "$DEST.new.raw" "$DEST.new.raw.asc"
+
+if [ -z "$no_verify" ]; then
+  # Verify raw file
+  if ! gpg_verify "$DEST.new.raw.asc" "$DEST.new.raw"; then
+    mv_all "$DEST" ".new" ".error"
+    test -n "$trunk" || error "$DEST.error.raw: *** BAD signature ***"
+    error "$DEST.error.raw: signature from branch no longer valid ('--no-verify' to ignore)"
+  fi
+fi
 
 if [ "$smtctl" != "-" ]; then
   # Check syntax
-  rm -f "$DEST.error"
-  if "$smtctl" -B "$DEST.new" -P showall >/dev/null; then
-    test -n "$q" || echo "$smtctl: syntax OK"
-  else
-    mv "$DEST.new" "$DEST.error"
-    echo "$DEST.error: rejected by $smtctl, probably no longer compatible" >&2
-    exit 1
+  if ! "$smtctl" -B "$DEST.new" -P showall >/dev/null; then
+    mv_all "$DEST" ".new" ".error"
+    error "$DEST.error: rejected by $smtctl, probably no longer compatible"
   fi
+  vecho "$smtctl: syntax OK"
 fi
 
 # Keep old file if identical, ignore missing Id keyword expansion in new file
 rm -f "$DEST.lastcheck"
 if [ -f "$DEST" ]; then
-  if    cmp "$DEST" "$DEST.new" >/dev/null 2>/dev/null \
-     || cat "$DEST" | sed 's|\$''Id''[^$]*\$|$''Id''$|' \
-        | cmp - "$DEST.new" >/dev/null 2>/dev/null; then
-    rm -f "$DEST.new"
-    touch "$DEST.lastcheck"
-    echo "$DEST is already up to date"
-    exit 0
+  if [ -f "$DEST.raw" ] && [ -f "$DEST.raw.asc" ]; then
+    if    cmp "$DEST.raw"     "$DEST.new.raw"     >/dev/null 2>&1 \
+       && cmp "$DEST.raw.asc" "$DEST.new.raw.asc" >/dev/null 2>&1 \
+       && {   cmp "$DEST"     "$DEST.new" >/dev/null 2>&1 \
+           || cmp "$DEST.raw" "$DEST.new" >/dev/null 2>&1; }
+    then
+      rm -f "$DEST.new" "$DEST.new.raw" "$DEST.new.raw.asc"
+      touch "$DEST.lastcheck"
+      echo "$DEST is already up to date"
+      exit 0
+    fi
+    mv_all "$DEST" "" ".old"
+  else
+    mv "$DEST" "$DEST.old"
   fi
-  mv "$DEST" "$DEST.old"
 fi
 
-mv "$DEST.new" "$DEST"
-
-echo "$DEST updated from $location"
+mv_all "$DEST" ".new" ""
 
+echo "$DEST updated from ${trunk:-branches/$BRANCH}${no_verify:+ (NOT VERIFIED)}"