#!/usr/bin/env bash
# check-goanywhere-mft-cve-2026-18543.sh: read-only checker for Fortra GoAnywhere MFT
# CVE-2026-18543 (FI-2026-021). Compares a version you supply, or a saved version file, with 7.10.3.
# No network, no file writes, no payloads, never prints secrets, never opens secrets or config files.
# Exit: 0 FIXED, 2 AFFECTED, 3 REVIEW, 5 usage or read error. Pack 1.0.0.
set -eu
usage() {
  echo 'usage: check-goanywhere-mft-cve-2026-18543.sh [--version V] [--version-file FILE] [--csv FILE] [--host NAME]'
  exit 5
}
esc() {
  local s="$1" o="" c
  local i
  for ((i=0; i<${#s}; i++)); do
    c=${s:i:1}
    if [[ $c =~ [A-Za-z0-9._/-] ]]; then o+=$c; else printf -v o '%s\\x%02X' "$o" "'$c"; fi
  done
  printf '%s' "$o"
}
is_refused() {
  local b
  b=$(basename -- "$1" | tr '[:upper:]' '[:lower:]')
  case $b in
    settings.py|local_settings.py|secrets.py|credentials.py|database.xml|https.xml|.env|.env.local|.env.prod|.env.production|.env.development|.env.test|*.key|*.pem|*.pfx|*.p12|*.jks|credentials*|secrets*|license*) return 0 ;;
  esac
  case $b in
    *.key|*.pem|*.pfx|*.p12|*.jks) return 0 ;;
  esac
  return 1
}

VERSION_STRING=; VERSION_FILE=; CSV_PATH=; HOST_LABEL=
HAS_V=0; HAS_F=0; HAS_C=0; HAS_H=0
while [ $# -gt 0 ]; do
  case $1 in
    --version)
      [ $# -ge 2 ] || usage
      [ "$HAS_V" = 0 ] || { echo 'ERROR: give each option once'; exit 5; }
      VERSION_STRING=$2; HAS_V=1; shift 2 ;;
    --version-file)
      [ $# -ge 2 ] || usage
      [ "$HAS_F" = 0 ] || { echo 'ERROR: give each option once'; exit 5; }
      VERSION_FILE=$2; HAS_F=1; shift 2 ;;
    --csv)
      [ $# -ge 2 ] || usage
      [ "$HAS_C" = 0 ] || { echo 'ERROR: give each option once'; exit 5; }
      CSV_PATH=$2; HAS_C=1; shift 2 ;;
    --host)
      [ $# -ge 2 ] || usage
      [ "$HAS_H" = 0 ] || { echo 'ERROR: give each option once'; exit 5; }
      HOST_LABEL=$2; HAS_H=1; shift 2 ;;
    -h|--help) usage ;;
    *) echo "ERROR: unknown option: $(esc "$1")"; exit 5 ;;
  esac
done
ninputs=0
[ "$HAS_V" = 1 ] && ninputs=$((ninputs+1))
[ "$HAS_F" = 1 ] && ninputs=$((ninputs+1))
[ "$HAS_C" = 1 ] && ninputs=$((ninputs+1))
[ "$ninputs" -eq 0 ] && usage
if [ "$ninputs" -ne 1 ]; then echo 'ERROR: use only one of --csv or --version/--version-file'; exit 5; fi
if [ "$HAS_C" = 1 ] && [ "$HAS_H" = 1 ]; then echo 'ERROR: use only one of --csv or --version/--version-file'; exit 5; fi

if [ "$HAS_F" = 1 ]; then
  if is_refused "$VERSION_FILE"; then echo 'ERROR: refused to open a settings or secrets file'; exit 5; fi
  if [ ! -f "$VERSION_FILE" ] || [ ! -r "$VERSION_FILE" ]; then echo 'ERROR: version file missing or unreadable'; exit 5; fi
fi
if [ "$HAS_C" = 1 ]; then
  if is_refused "$CSV_PATH"; then echo 'ERROR: refused to open a settings or secrets file'; exit 5; fi
  if [ ! -f "$CSV_PATH" ] || [ ! -r "$CSV_PATH" ]; then echo 'ERROR: csv missing or unreadable'; exit 5; fi
fi

# Official CVE.org range is semver version 0, lessThan 7.10.3. Official form is three-part dotted 7.10.3.
# First segment above 20 is REVIEW so IPv4-like three-part values are not classed as FIXED.
# Four-part strings are REVIEW (so IPv4 addresses are not classed as FIXED).
VRX='^([0-9]{1,2})\.([0-9]{1,3})\.([0-9]{1,3})$'

showver() {
  local v=$1
  if [[ $v =~ $VRX ]]; then
    if [ "${BASH_REMATCH[1]}" -le 20 ]; then printf '%s' "$v"; return; fi
  fi
  printf '%s' '(not shown)'
}

extract_ver() {
  local s=$1 t
  s=${s//$'\r'/}
  if [[ $s =~ (^|[[:space:]:=Vv])([0-9]{1,2}\.[0-9]{1,3}\.[0-9]{1,3})([^0-9]|$) ]]; then
    printf '%s' "${BASH_REMATCH[2]}"; return
  fi
  t=${s//[[:space:]]/}
  t=${t#v}; t=${t#V}
  if [[ $t =~ $VRX ]]; then printf '%s' "$t"; return; fi
  printf ''
}

verdict_of() {
  local v=$1
  if ! [[ $v =~ $VRX ]]; then echo REVIEW; return; fi
  local a=${BASH_REMATCH[1]} b=${BASH_REMATCH[2]} c=${BASH_REMATCH[3]}
  if [ "$a" -gt 20 ]; then echo REVIEW; return; fi
  # Fixed: 7.10.3 and later, compared numerically (7, 10, 3).
  if [ "$a" -gt 7 ]; then echo FIXED; return; fi
  if [ "$a" -lt 7 ]; then echo AFFECTED; return; fi
  if [ "$b" -gt 10 ]; then echo FIXED; return; fi
  if [ "$b" -lt 10 ]; then echo AFFECTED; return; fi
  if [ "$c" -ge 3 ]; then echo FIXED; return; fi
  echo AFFECTED
}

NA=0; NF=0; NR=0; WORST=-1
rank_of() { case $1 in 2) echo 3;; 3) echo 2;; 0) echo 1;; *) echo 0;; esac; }
code_of() { case $1 in AFFECTED) echo 2;; FIXED) echo 0;; *) echo 3;; esac; }
tally() { case $1 in AFFECTED) NA=$((NA+1));; FIXED) NF=$((NF+1));; *) NR=$((NR+1));; esac; }
add_row() {
  local h=$1 shown=$2 verd=$3
  tally "$verd"
  local rc; rc=$(code_of "$verd")
  if [ "$WORST" -lt 0 ] || [ "$(rank_of "$rc")" -gt "$(rank_of "$WORST")" ]; then WORST=$rc; fi
  if [ "$h" = input ]; then echo "VERSION: $shown $verd"
  else echo "VERSION: $h $shown $verd"
  fi
}

echo 'GoAnywhere MFT CVE-2026-18543 checker (read only; no network, no file writes, no secrets printed)'
echo 'NOTE: no exploitation reported by Fortra as of 2 Dec 2026; not listed in CISA KEV as of 2 Dec 2026 (catalogue 2026.11.30).'
echo 'NOTE: CISA KEV due dates bind US federal agencies only (BOD 26-04). Patch now regardless.'
echo 'FIXED RELEASE: GoAnywhere MFT 7.10.3 or later (CVE.org lessThan 7.10.3; official release notes 24 Nov 2026). Fortra and CVE.org rate CVE-2026-18543 CVSS 3.1 9.3 Critical. Official scores are Fortra and CVE.org CVSS v3.1 only.'

if [ "$HAS_C" = 1 ]; then
  local_hdr=
  n=0
  while IFS= read -r line || [ -n "$line" ]; do
    line=${line//$'\r'/}
    if [ -z "$local_hdr" ]; then
      line=${line#$'\xef\xbb\xbf'}
      hdr=$(printf '%s' "$line" | tr '[:upper:]' '[:lower:]')
      if [ "$hdr" != 'host,version' ]; then echo 'ERROR: CSV header must be: host,version'; exit 5; fi
      local_hdr=$hdr
      continue
    fi
    [ -z "$line" ] && continue
    case $line in \#*) continue ;; esac
    h=${line%%,*}; ver=${line#*,}
    ver=${ver%%,*}
    h=${h%"${h##*[![:space:]]}"}; h=${h#"${h%%[![:space:]]*}"}
    ver=${ver%"${ver##*[![:space:]]}"}; ver=${ver#"${ver%%[![:space:]]*}"}
    [ -z "$h" ] && h='host not given'
    ev=$(extract_ver "$ver")
    if [ -z "$ev" ]; then add_row "$h" '(not shown)' REVIEW
    else add_row "$h" "$(showver "$ev")" "$(verdict_of "$ev")"
    fi
    n=$((n+1))
  done < "$CSV_PATH"
  if [ "$n" -eq 0 ]; then echo 'ERROR: CSV has no rows'; exit 5; fi
elif [ "$HAS_F" = 1 ]; then
  h=${HOST_LABEL:-input}
  ev=
  while IFS= read -r line || [ -n "$line" ]; do
    line=${line//$'\r'/}
    case $line in \#*) continue ;; esac
    t=$(extract_ver "$line")
    if [ -n "$t" ]; then ev=$t; break; fi
  done < "$VERSION_FILE"
  if [ -z "$ev" ]; then add_row "$h" '(not shown)' REVIEW
  else add_row "$h" "$(showver "$ev")" "$(verdict_of "$ev")"
  fi
else
  h=${HOST_LABEL:-input}
  vs=${VERSION_STRING//$'\r'/}
  vs=${vs#"${vs%%[![:space:]]*}"}
  vs=${vs%"${vs##*[![:space:]]}"}
  ev=
  if [[ $vs == *$'\n'* ]] || [[ $vs == *$'\t'* ]]; then ev=
  else
    t=$vs; t=${t#v}; t=${t#V}
    if [[ $t =~ $VRX ]]; then ev=$t; fi
  fi
  if [ -z "$ev" ]; then add_row "$h" '(not shown)' REVIEW
  else add_row "$h" "$(showver "$ev")" "$(verdict_of "$ev")"
  fi
fi

N=$((NA+NF+NR))
case $WORST in
  0) w=FIXED ;;
  2) w=AFFECTED ;;
  *) w=REVIEW; WORST=3 ;;
esac
echo "RESULT: $w ($N checked: $NF FIXED, $NA AFFECTED, $NR REVIEW) (exit $WORST)"
exit "$WORST"
