#! /bin/sh
#
# smartmontools drive database update script
#
# Home page of code is: https://www.smartmontools.org
#
# Copyright (C) 2010-25 Christian Franke
#
# SPDX-License-Identifier: GPL-2.0-or-later
#

set -e

# Set by config.status
export PATH="/usr/local/bin:/usr/bin:/bin"
PACKAGE="smartmontools"
VERSION="7.5"
prefix="/usr"
exec_prefix="/usr"
sbindir="/usr/sbin"
datarootdir="${prefix}/share"
datadir="/usr/share"
# shellcheck disable=SC2034
localstatedir="/var"
drivedbinstdir="${datadir}/${PACKAGE}"
drivedbdir="${localstatedir}/lib/${PACKAGE}"

# Download tools
os_dltools="curl wget lynx svn"

# Default drivedb.h update branch
default_branch="7.5"

# Default drivedb location
default_drivedb="$drivedbdir/drivedb.h"

# GnuPG used to verify signature (disabled if empty)
gpg="gpg"

# Default command used for syntax check
default_smartctl="$sbindir/smartctl"

# PATH information for help and error messages
#pathinfo='$PATH'
pathinfo="'$PATH'"

myname=$0

print_help()
{
  pathinfo="
                     $pathinfo"
  cat <<EOF
smartmontools $VERSION drive database update script

Usage: $myname [OPTIONS] [DESTFILE]

  -s, --smartctl SMARTCTL
                    Use SMARTCTL for syntax check ('-s -' to disable)
                    [default: $default_smartctl]
  -t, --tool [DIR/]TOOL
                    Use TOOL for download: $os_dltools
                    [default: first one found in $pathinfo]
  -u, --url-of LOCATION
                    Use URL of LOCATION for download:
                      github (upstream git repository) [default]
                      svn (previous SVN repository)
                      trac (Trac code browser)
  --url URL         Download from URL
  --file FILE       Copy from local FILE
EOF
  test "$drivedbinstdir" = "$drivedbdir" || cat <<EOF
  --install         Copy from originally installed drive database file
                    This is the same as:
                    '--no-verify --file $drivedbinstdir/drivedb.h'
EOF
  cat <<EOF
  --main            Download from main branch (requires '--no-verify')
  --branch X.Y      Download from branch X.Y
  --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
  --force           Allow downgrades
  --export-key      Print the OpenPGP/GPG public key block
  --dryrun          Print download commands only
  -q, --quiet       Suppress info messages
  -v, --verbose     Verbose output
  -h, --help        Print this help text

Updates $default_drivedb
or DESTFILE from branch 'drivedb/$default_branch' of smartmontools git
repository.
EOF
}

error()
{
  echo "$myname: $*" >&2
  test -z "$usageerr" || echo "Try '$myname -h' for help" >&2
  exit 1
}

err_notfound()
{
  case $1 in
    */*) error "$1: not found $2" ;;
    *)   error "$1: not found in $pathinfo $2" ;;
  esac
}

# check_optarg "$@"
check_optarg()
{
  test $# -gt 1 || error "option '$1' requires an argument"
}

warning()
{
  echo "$myname: (Warning) $*" >&2
}

# selecturl URL_OF
selecturl()
{
  case $1 in
    github) # https://github.com/smartmontools/smartmontools/raw/refs/heads/main/src/drivedb.h
            # redirected to:
            url='https://raw.githubusercontent.com/smartmontools/smartmontools/refs/heads/main/src/drivedb.h' ;;
    svn)    url='https://svn.code.sf.net/p/smartmontools/code/trunk/smartmontools/drivedb.h' ;;
    trac)   url='https://www.smartmontools.org/browser/src/drivedb.h?format=raw&rev=main' ;;
    *) error "$1: is none of 'github svn trac'"
  esac
}

inpath()
{
  local d rc save
  rc=1
  save=$IFS
  IFS=':'
  for d in $PATH; do
    test -f "$d/$1" || continue
    test -x "$d/$1" || continue
    rc=0
    break
  done
  IFS=$save
  return $rc
}

iecho()
{
  test -n "$quiet" || echo "$*"
}

vecho()
{
  test -n "$q" || echo "$*"
}

# vrun COMMAND ARGS...
vrun()
{
  if [ -n "$dryrun" ]; then
    echo "$*"
  elif [ -n "$q" ]; then
    "$@" 2>/dev/null
  else
    echo "$*"
    "$@"
  fi
}

# vrun2 OUTFILE COMMAND ARGS...
vrun2()
{
  local f err rc
  f=$1; shift
  rc=0
  if [ -n "$dryrun" ]; then
    echo "$* > $f"
  else
    vecho "$* > $f"
    err=`"$@" 2>&1 > $f` || rc=$?
    if [ -n "$err" ]; then
      vecho "$err" >&2
      test $rc != 0 || rc=42
    fi
  fi
  return $rc
}

# rm_f FILE...
rm_f()
{
  while [ $# -ne 0 ]; do
    if [ -f "$1" ]; then
      vrun rm -f "$1"
    fi
    shift
  done
}

# download URL FILE
download()
{
  local f u rc
  u=$1; f=$2
  rc=0

  case ${tool##*/} in
    curl*)
      # "Accept-Encoding" header avoids caching problems with svn URL
      vrun "$tool" ${q:+-s} -f --max-redirs 0 \
        -H "Accept-Encoding: identity" \
        ${cacert:+--cacert "$cacert"} \
        ${capath:+--capath "$capath"} \
        ${insecure:+--insecure} \
        -o "$f" "$u" || rc=$?
      ;;

    wget*)
      # wget >= 1.16.1 sets "Accept-Encoding: identity" header by default
      vrun "$tool" $q --max-redirect=0 \
        ${cacert:+--ca-certificate="$cacert"} \
        ${capath:+--ca-directory="$capath"} \
        ${insecure:+--no-check-certificate} \
        -O "$f" "$u" || rc=$?
      ;;

    lynx*)
      test -z "$cacert" || vrun export SSL_CERT_FILE="$cacert"
      test -z "$capath" || vrun export SSL_CERT_DIR="$capath"
      # Check also stderr as lynx does not return != 0 on HTTP error
      vrun2 "$f" "$tool" -stderr -noredir -source "$u" || rc=$?
      ;;

    fetch*) # FreeBSD
      vrun "$tool" $q --no-redirect \
        ${cacert:+--ca-cert "$cacert"} \
        ${capath:+--ca-path "$capath"} \
        ${insecure:+--no-verify-hostname} \
        -o "$f" "$u" || rc=$?
      ;;

    ftp*) # OpenBSD
      vrun "$tool" \
        ${cacert:+-S cafile="$cacert"} \
        ${capath:+-S capath="$capath"} \
        ${insecure:+-S dont} \
        -o "$f" "$u" || rc=$?
      ;;

    *) error "$tool: unknown (internal error)" ;;
  esac
  return $rc
}

# check_file FILE FIRST_CHAR MIN_SIZE MAX_SIZE
check_file()
{
  local firstchar f maxsize minsize size
  f=$1; firstchar=$2; minsize=$3; maxsize=$4

  # Check first chars
  # shellcheck disable=SC2254
  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
}

# selectkey BRANCH
selectkey()
{
  case $1 in
    7.[0235])
# Smartmontools Signing Key (through 2025) <smartmontools-database@listi.jpberlin.de>
# 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
hk0r7EbGyBO0qvgzHLzSsQiSsgaMCkLc5Xt4NzB4g2DvnReFU2WwgRh031lHOVLm
mvFqRtHzJb20dKufyjOmSMzNKRzURVmobECKARaBlGNP0wHYhq97n4OxM1o0eq7a
4ugaSp2q+6BSaAQhbZN8ULCF/oGA/376Sz7RNuoOmQwl9aFqnfl3YgopBIqKvnSP
h4j0QynN45rUFOe/VywTmpWKj+DonGCupxe9VvyZ87NKRgKiHprXGDrhdB0GcNXM
wV66WbjKBV7qlpSh/GH3oiHwlcYT8LNyZbxTJXcVF5ODtlZfc9zqRtUBWQARAQAB
tFNTbWFydG1vbnRvb2xzIFNpZ25pbmcgS2V5ICh0aHJvdWdoIDIwMjUpIDxzbWFy
dG1vbnRvb2xzLWRhdGFiYXNlQGxpc3RpLmpwYmV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01H3VUVz/iR0XQbf13RZoEM6CJWms
Q/qvYlwkbKOdlahjoHrFlkhADSBaO9N1OZp3OYDjziIujMdt2IPKnmM=
=7MQk
-----END PGP PUBLIC KEY BLOCK-----
"
      ;;

    *) error "No known public key for branch '$1'" ;;
  esac
}

# gpg_verify FILE.asc FILE
gpg_verify()
{
  local gnupgtmp i out rc

  # Create temp home dir
  gnupgtmp="$tmpdir/.gnupg.$$.tmp"
  rm -f -r "$gnupgtmp"
  mkdir "$gnupgtmp" || exit 1
  chmod 0700 "$gnupgtmp"

  # Import public key
  if ! out=`echo "$public_key" | "$gpg" --batch --no-tty --homedir="$gnupgtmp" --import 2>&1`; then
    echo "$out" >&2
    exit 1
  fi
  vecho "$out"

  # Verify
  rc=0
  out=`"$gpg" --batch --no-tty --homedir="$gnupgtmp" --verify "$1" "$2" </dev/null 2>&1` || rc=1
  if [ $rc = 0 ]; then
    vecho "$out"
  else
    # print gpg error always
    echo "$out" >&2
  fi

  # Stop the gpg-agent possibly started by gpg
  if [ -n "$gpgconf" ]; then
    out=`"$gpgconf" --homedir="$gnupgtmp" --kill gpg-agent </dev/null 2>&1` || echo "$out" >&2
  fi

  # Remove temp home dir, retry on failure
  i=0
  while ! out=`rm -f -r "$gnupgtmp" 2>&1`; do
    i=$((i+1))
    if [ $i -ge 10 ]; then
      echo "$out" >&2; break
    fi
    vecho "$out"
    sleep 1
  done

  return $rc
}

# get_db_version DRIVEDB > VERSION
get_db_version()
{
  local v
  v=`sed -n '/^[ {]*"VERSION: .*"/{
       s,^[ {]*"VERSION: \([1-9][./0-9]*\)[ "].*$,\1,p
       q
     }' "$1"` || return 1
  test -n "$v" || return 0
  test "${v%/*}" != "$v" || v="$v/?"
  echo "$v"
}

# drivedb_mv OLD NEW
drivedb_mv()
{
  local ext nf of
  for ext in "" ".asc" ".raw" ".raw.asc"; do
    of="${drivedb}${1}${ext}"
    nf="${drivedb}${2}${ext}"
    if [ -f "$of" ]; then
      vrun mv -f "$of" "$nf"
    else
      rm_f "$nf"
    fi
  done
}

# drivedb_error "MESSAGE"
drivedb_error()
{
  drivedb_mv ".new" ".error"
  error "${drivedb}.error${1}"
}

# Parse options
smartctl=$default_smartctl
tool=
url_of=
url=
file=
quiet=
q="-q"
dryrun=
main=
branch=$default_branch
cacert=
capath=
insecure=
no_verify=
force=
expkey=
usageerr=t

while true; do case $1 in
  -s|--smartctl)
    check_optarg "$@"; shift
    smartctl=$1 ;;

  -t|--tool)
    check_optarg "$@"; shift
    tool=$1 ;;

  -u|--url-of)
    check_optarg "$@"; shift
    url_of=$1 ;;

  -q|--quiet)
    quiet=t ;;

  -v|--verbose)
    q= ;;

  --url)
    check_optarg "$@"; shift
    url=$1 ;;

  --file)
    check_optarg "$@"; shift
    file=$1 ;;

  --install)
    test "$drivedbinstdir" != "$drivedbdir" \
    || error "'$1' is not supported in this configuration"
    file="$drivedbinstdir/drivedb.h"
    no_verify=t ;;

  --dryrun)
    dryrun=t ;;

  --main)
    main=t ;;

  --branch)
    check_optarg "$@"; shift
    branch=$1
    case $branch in
      5.4[0-3]|6.[0-6]|7.[0235]) ;;
      *) error "invalid branch '$branch'" ;;
    esac ;;

  --cacert)
    check_optarg "$@"; shift
    cacert=$1 ;;

  --capath)
    check_optarg "$@"; shift
    capath=$1 ;;

  --insecure)
    insecure=t ;;

  --no-verify)
    no_verify=t ;;

  --force)
    force=t ;;

  --export-key)
    expkey=t ;;

  -h|--help)
    print_help
    exit 0 ;;

  -*)
    error "unknown option '$1'" ;;

  *)
    break ;;
esac; shift; done

if [ -n "$expkey" ]; then
  selectkey "$branch"
  cat <<EOF
$public_key
EOF
  exit 0
fi

case $# in
  0) drivedb=$default_drivedb ;;
  1) drivedb=$1 ;;
  *) error "only one DESTFILE argument is allowed" ;;
esac

# Check selected source
case "${url_of:+url_of}${url:+url}${file:+file}" in
  ''|url_of)
    test -n "$url_of" || url_of=github
    selecturl "$url_of"
    case $url in
      */main/*|*rev=main*)
        if [ -z "$main" ]; then
          url=`echo "$url" | sed -e "s,/main/,/drivedb/$branch/," \
                                 -e "s,rev=main,rev=drivedb/$branch,"`
          case $branch in
            [567].*) url=`echo "$url" | sed -e 's,/src/,/smartmontools/,'` ;;
          esac
        elif [ -z "$no_verify" ]; then
          error "'--main' requires '--no-verify'"
        fi
        ;;
      */trunk/*)
        test -z "$main" || error "'--main' is not supported with '-u svn'"
        url=`echo "$url" | sed -e "s,/trunk/,/branches/RELEASE_${branch}_DRIVEDB/," \
                               -e 's,_\([0-9]*\)\.\([0-9]*\)_,_\1_\2_,'`;;
      *) error "$url: invalid URL (internal)" ;;
    esac ;;
  url)
    test -z "`echo "$url" | sed -e 's,^[a-z][a-z0-9]*:[^ ][^ ]*$,,'`" \
    || error "$url: Invalid URL" ;;
  file) ;;
  *) error "only one of '-u', '--url', '--file' is allowed" ;;
esac

# Determine path of signature file
file_asc=
file_raw_asc=
url_asc=
if [ -z "$no_verify" ]; then
  if [ -n "$url" ]; then
    case $branch in
      [567].*) raw=".raw" ;;
      *) raw= ;;
    esac
    case $url in
      *\?*) url_asc=`echo "$url" | sed "s,?,$raw.asc?,"` ;;
      *)    url_asc="$url$raw.asc" ;;
    esac
  else
    file_asc="$file.asc"
    file_raw_asc="$file.raw.asc"
  fi
fi

if [ -z "$file" ]; then
  if [ -z "$tool" ]; then
    # Find download tool in PATH
    for t in $os_dltools; do
      if inpath "$t"; then
        tool=$t
        break
      fi
    done
    test -n "$tool" || error "found none of '$os_dltools' in $pathinfo"
  else
    # Check tool name
    found=
    for t in $os_dltools; do
      case ${tool##*/} in
        $t*) found=t; break ;;
      esac
    done
    test -n "$found" || error "$tool: is none of '$os_dltools'"
  fi
fi

# Check option compatibility
case "$tool:$insecure" in
  lynx:t) warning "'--insecure' is ignored if '-t lynx' is used" ;;
esac

# Check for smartctl
if [ "$smartctl" != "-" ]; then
  "$smartctl" -V >/dev/null 2>&1 \
  || err_notfound "$smartctl" "('-s -' to ignore)"
fi

# Check for GnuPG
gpgconf=
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)"
  selectkey "$branch"
  case $gpg in
    */*) gpgconf="${gpg%/*}/gpgconf" ;;
    *) gpgconf="gpgconf" ;;
  esac
  "$gpgconf" --version >/dev/null 2>&1 || gpgconf=
fi
usageerr=

# Use destination directory as temp directory for gpg
tmpdir=`dirname "$drivedb"`

# Remove possible garbage from last download
rm_f "$drivedb.new" "$drivedb.new.asc" "$drivedb.new.raw" "$drivedb.new.raw.asc" || exit 1

if [ -n "$url" ]; then
  # Download
  vecho "Download drivedb.h with $tool"
  rc=0
  download "$url" "$drivedb.new" || rc=$?
  if [ $rc != 0 ]; then
    rm_f "$drivedb.new"
    error "drivedb.h: download failed ($tool: exit $rc)"
  fi

  if [ -n "$url_asc" ]; then
    vecho "Download drivedb.h.asc with $tool"
    rc=0
    download "$url_asc" "$drivedb.new.asc" || rc=$?
    if [ $rc != 0 ]; then
      rm_f "$drivedb.new" "$drivedb.new.asc"
      error "drivedb.h.asc: download failed ($tool: exit $rc) ('--no-verify' to ignore)"
    fi
  fi
else
  # Copy from local file
  if [ ! -f "$file" ]; then
    error "$file: file not found"
  fi
  if [ -n "$file_asc" ]; then
    if [ ! -f "$file_asc" ]; then
      test -f "$file_raw_asc" \
      || error "$file_asc, $file_raw_asc: file not found ('--no-verify' to ignore)"
      file_asc=$file_raw_asc
    fi
  fi

  if ! vrun cp "$file" "$drivedb.new"; then
    error "$file: copy failed"
  fi
  if [ -n "$file_asc" ]; then
    if ! vrun cp "$file_asc" "$drivedb.new.asc"; then
      rm_f "$drivedb.new"
      error "$file_asc: copy failed"
    fi
  fi
fi

test -z "$dryrun" || exit 0

# Check files, adjust timestamps and permissions
errmsg=`check_file "$drivedb.new" '/' 10000 1000000` \
|| drivedb_error ": $errmsg"
touch "$drivedb.new"
chmod 0644 "$drivedb.new"

if [ -f "$drivedb.new.asc" ]; then
  errmsg=`check_file "$drivedb.new.asc" '-' 200 2000` \
  || drivedb_error ".asc: $errmsg"
  touch "$drivedb.new.asc"
  chmod 0644 "$drivedb.new.asc"
fi

# Check whether installed file is identical
equal=t
cmp "$drivedb" "$drivedb.new" >/dev/null 2>&1 || equal=

if [ -z "$no_verify" ]; then
  # Verify new file
  if ! gpg_verify "$drivedb.new.asc" "$drivedb.new"; then
    test -z "$equal" \
    || warning "$drivedb: *** installed file is identical to broken new file ***"
    drivedb_error ": *** BAD signature or outdated key ***"
  fi
fi

# Get version
newver=`get_db_version "$drivedb.new"`
if [ -z "$newver" ]; then
  test -n "$force" \
  || drivedb_error ": no VERSION information found ('--force' to ignore)"
  newver="?/?"
elif [ "${newver##*/}" = "?" ]; then
  test -n "$main" \
  || drivedb_error ": VERSION information is incomplete ('--main' to ignore)"
fi

if [ "$smartctl" != "-" ]; then
  # Check syntax
  "$smartctl" -B "$drivedb.new" -P showall >/dev/null \
  || drivedb_error ": rejected by $smartctl, probably no longer compatible"
  vecho "$smartctl: syntax OK"
fi

# Always install if missing
rm -f "$drivedb.lastcheck"
if [ ! -f "$drivedb" ]; then
  drivedb_mv ".new" ""
  iecho "$drivedb $newver newly installed${no_verify:+ (NOT VERIFIED)}"
  exit 0
fi

# Keep old file if identical
if [ -n "$equal" ]; then
  if    [ -f "$drivedb.new.asc" ] \
     && ! cmp "$drivedb.new.asc" "$drivedb.asc" >/dev/null 2>&1; then
    vrun mv -f "$drivedb.new.asc" "$drivedb.asc"
    iecho "$drivedb.asc $newver updated"
  fi
  rm_f "$drivedb.new" "$drivedb.new.asc" "$drivedb.raw" "$drivedb.raw.asc"
  touch "$drivedb.lastcheck"
  iecho "$drivedb $newver is already up to date${no_verify:+ (NOT VERIFIED)}"
  exit 0
fi

# Check branch and file version
oldver=`get_db_version "$drivedb"`
test -n "$oldver" || oldver="?/?"
if    [ "${newver##*/}" = "?" ] \
   || [ "${oldver##*/}" = "?" ] \
   || [ "${newver%/*}" != "${oldver%/*}" ]; then
  # Always install if from main or other branch
  updmsg="replaced with"
elif [ "${newver##*/}" -lt "${oldver##*/}" ]; then
  # Install older file only if '--force' is used
  if [ -z "$force" ]; then
    rm_f "$drivedb.new" "$drivedb.new.asc"
    iecho "$drivedb $oldver not downgraded to $newver ('--force' to override)"
    exit 0
  fi
  updmsg="downgraded to"
else
  updmsg="updated to"
fi

drivedb_mv "" ".old"
drivedb_mv ".new" ""
iecho "$drivedb $oldver $updmsg $newver${no_verify:+ (NOT VERIFIED)}"
