#!/usr/bin/env bash
# Read-only checker for HPE Networking Management Software (AirWave) advisory HPESBNW05186
# (CVE-2026-133971, CVE-2026-133972, CVE-2026-133973 and 20 further CVEs). It compares a version
# you supply, or a saved amp_version / CLI banner file, with the fixed release AirWave 8.3.0.8.
# No network, no file writes, no payloads, never contacts AirWave, never prints secrets.
# Exit: 0 FIXED, 2 AFFECTED, 3 REVIEW, 5 usage or read error. Pack 1.0.0.
# Usage: check-hpe-airwave-hpesbnw05186.sh [--version V] [--version-file FILE] [--csv FILE]
#   [--lifecycle supported|eom|eos] [--host NAME]
#   --version V          AirWave version, for example 8.3.0.7 or 8.3.0.8
#   --version-file F     saved output of the AMP CLI command amp_version (8 Advanced, 2 Enter
#                        Commands) or a saved AMP CLI login banner
#   --csv FILE           inventory CSV with header host,version,lifecycle
#   --lifecycle VALUE    supported (default), eom (End of Maintenance) or eos (End of Support)
#   --host NAME          label for a single --version or --version-file row
set -u
export LC_ALL=C

usage() {
  echo 'usage: check-hpe-airwave-hpesbnw05186.sh [--version V] [--version-file FILE] [--csv FILE] [--lifecycle supported|eom|eos] [--host NAME]'
  exit 5
}
esc() {
  local s=$1 o='' c i h
  for ((i = 0; i < ${#s}; i++)); do
    c=${s:i:1}
    if [[ $c == [A-Za-z0-9._/-] ]]; then o+=$c; else printf -v h '\\x%02X' "'$c"; o+=$h; fi
  done
  printf '%s' "$o"
}

VSTR=''; VFILE=''; CSV=''; LIFE='supported'; HOST=''; gv=0; gf=0; gc=0; gl=0; gh=0
while [ $# -gt 0 ]; do
  case $1 in
    --version) [ $# -ge 2 ] || usage; [ "$gv" = 0 ] || { echo 'ERROR: give each option once'; exit 5; }; gv=1; VSTR=$2; shift 2;;
    --version-file) [ $# -ge 2 ] || usage; [ "$gf" = 0 ] || { echo 'ERROR: give each option once'; exit 5; }; gf=1; VFILE=$2; shift 2;;
    --csv) [ $# -ge 2 ] || usage; [ "$gc" = 0 ] || { echo 'ERROR: give each option once'; exit 5; }; gc=1; CSV=$2; shift 2;;
    --lifecycle) [ $# -ge 2 ] || usage; [ "$gl" = 0 ] || { echo 'ERROR: give each option once'; exit 5; }; gl=1; LIFE=$2; shift 2;;
    --host) [ $# -ge 2 ] || usage; [ "$gh" = 0 ] || { echo 'ERROR: give each option once'; exit 5; }; gh=1; HOST=$2; shift 2;;
    -h|--help) usage;;
    *) echo "ERROR: unknown option: $(esc "$1")"; exit 5;;
  esac
done
[ "$gv$gf$gc" != 000 ] || usage
[ "$gc" = 1 ] && [ "$gv$gf$gh" != 000 ] && { echo 'ERROR: use only one of --csv or --version/--version-file'; exit 5; }
[ "$gv" = 1 ] && [ "$gf" = 1 ] && { echo 'ERROR: use only one of --csv or --version/--version-file'; exit 5; }

norm_life() {
  local e; e=$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]')
  case $e in
    ''|supported|support) printf 'supported';;
    eom|endofmaintenance|eol) printf 'eom';;
    eos|endofsupport) printf 'eos';;
    *) printf 'bad';;
  esac
}
LIFE=$(norm_life "$LIFE")
[ "$LIFE" != bad ] || { echo 'ERROR: --lifecycle must be supported, eom or eos'; exit 5; }

if [ "$gf" = 1 ] && { [ ! -f "$VFILE" ] || [ ! -r "$VFILE" ]; }; then echo 'ERROR: version file missing or unreadable'; exit 5; fi
if [ "$gc" = 1 ] && { [ ! -f "$CSV" ] || [ ! -r "$CSV" ]; }; then echo 'ERROR: csv missing or unreadable'; exit 5; fi

# Four-part AirWave version (major 1-15) with optional documented build suffix .YYYYMMDD.HHMM
# from the official CLI banner example (8.2.13.1.20210712.0336). IPv4-like values with major
# above 15 (for example 192.0.2.7) are not treated as AirWave versions.
VRX='^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{1,3})(\.[0-9]{8}\.[0-9]{4})?$'

showver() { if [[ $1 =~ $VRX ]]; then printf '%s' "$1"; else printf '(not shown)'; fi; }

# Parse a raw token or line into a canonical four-part string, or empty if none.
extract_ver() {
  local s=$1 t
  s=$(printf '%s' "$s" | tr -d '\r')
  # Official CLI banner: "AirWave Management Platform 8.2.13.1.20210712.0336 on host"
  if [[ $s =~ AirWave[[:space:]]+Management[[:space:]]+Platform[[:space:]]+([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3}(\.[0-9]{8}\.[0-9]{4})?) ]]; then
    printf '%s' "${BASH_REMATCH[1]}"; return
  fi
  # A token that is only the version (amp_version output format is otherwise UNVERIFIED).
  t=$(printf '%s' "$s" | tr -d '[:space:]')
  t=${t#[vV]}
  if [[ $t =~ $VRX ]]; then printf '%s' "$t"; return; fi
  # A line containing a 4-part version after amp_version or Version
  if [[ $s =~ [Aa][Mm][Pp]_[Vv]ersion[^0-9]*([0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3}(\.[0-9]{8}\.[0-9]{4})?) ]]; then
    printf '%s' "${BASH_REMATCH[1]}"; return
  fi
  printf ''
}

# Compare four-part version with 8.3.0.8. Returns FIXED, AFFECTED or REVIEW.
verdict_of() {
  local v=$1
  if ! [[ $v =~ $VRX ]]; then echo REVIEW; return; fi
  local a=$((10#${BASH_REMATCH[1]})) b=$((10#${BASH_REMATCH[2]})) c=$((10#${BASH_REMATCH[3]})) d=$((10#${BASH_REMATCH[4]}))
  if [ "$a" -lt 1 ] || [ "$a" -gt 15 ]; then echo REVIEW; return; fi
  if [ "$a" -gt 8 ]; then echo FIXED; return; fi
  if [ "$a" -lt 8 ]; then echo AFFECTED; return; fi
  if [ "$b" -gt 3 ]; then echo FIXED; return; fi
  if [ "$b" -lt 3 ]; then echo AFFECTED; return; fi
  if [ "$c" -gt 0 ]; then echo FIXED; return; fi
  if [ "$d" -ge 8 ]; then echo FIXED; else echo AFFECTED; fi
}

# Apply EoM / EoS wording. EoS is REVIEW (HPE has not assessed). EoM stays AFFECTED (presumed).
apply_life() {
  local v=$1 life=$2
  if [ "$v" = FIXED ]; then echo FIXED; return; fi
  if [ "$life" = eos ]; then echo REVIEW; return; fi
  printf '%s' "$v"
}

note_life() {
  local life=$1 verd=$2
  if [ "$verd" = FIXED ]; then return; fi
  case $life in
    eom) echo "NOTE: HPE says product software versions that have reached End of Maintenance are presumed to be affected unless explicitly stated otherwise, and are not covered by this advisory. Upgrade to a supported release (8.3.0.8 or later).";;
    eos) echo "NOTE: HPE has not assessed exposure for deployments past End of Support and says such installations should be considered potentially impacted. Upgrade to a supported release (8.3.0.8 or later).";;
  esac
}

NA=0; NF=0; NR=0
tally() { case $1 in AFFECTED) NA=$((NA + 1));; FIXED) NF=$((NF + 1));; *) NR=$((NR + 1));; esac; }
rank() { 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; }

echo 'HPE Networking Management Software (AirWave) HPESBNW05186 checker (read only; no network, no file writes, no secrets printed)'
echo 'NOTE: no exploitation reported by HPE as of 25 Nov 2026; not listed in CISA KEV as of 26 Nov 2026 (catalogue 2026.11.25).'
echo 'NOTE: CISA KEV due dates bind US federal agencies only (BOD 26-04). Patch now regardless.'
echo 'FIXED RELEASE: AirWave 8.3.0.8 and above (HPESBNW05186). Software is available from the HPE Networking Support Portal.'

WORST=-1
add_row() { # host shown_ver verdict life
  local h=$1 shown=$2 verd=$3 life=$4 rc
  verd=$(apply_life "$verd" "$life")
  tally "$verd"
  rc=$(code_of "$verd")
  if [ "$WORST" -lt 0 ] || [ "$(rank "$rc")" -gt "$(rank "$WORST")" ]; then WORST=$rc; fi
  if [ "$h" = input ]; then
    echo "VERSION: $shown $verd"
  else
    echo "VERSION: $h $shown $verd"
  fi
  note_life "$life" "$verd"
}

if [ "$gc" = 1 ]; then
  head -n1 "$CSV" | tr -d '\r' | sed 's/^\xef\xbb\xbf//' | tr '[:upper:]' '[:lower:]' | grep -qxF 'host,version,lifecycle' || { echo 'ERROR: CSV header must be: host,version,lifecycle'; exit 5; }
  n=0
  while IFS= read -r line || [ -n "$line" ]; do
    line=${line%$'\r'}
    [ -z "$line" ] && continue
    case $line in \#*) continue;; esac
    IFS=, read -r h ver lf rest <<< "$line"
    h=$(printf '%s' "$h" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
    [ -n "$h" ] || h='host not given'
    ver=$(printf '%s' "$ver" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
    lf=$(printf '%s' "$lf" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
    [ -n "$lf" ] || lf=supported
    lf=$(norm_life "$lf")
    if [ "$lf" = bad ]; then
      echo "VERSION: $h (not shown) REVIEW"
      echo 'NOTE: lifecycle value not recognised (use supported, eom or eos).'
      tally REVIEW
      if [ "$WORST" -lt 0 ] || [ "$(rank 3)" -gt "$(rank "$WORST")" ]; then WORST=3; fi
      n=$((n + 1))
      continue
    fi
    ev=$(extract_ver "$ver"); shown=$(showver "$ev")
    if [ -z "$ev" ]; then ev=''; shown='(not shown)'; vd=REVIEW; else vd=$(verdict_of "$ev"); shown=$(showver "$ev"); fi
    add_row "$h" "$shown" "$vd" "$lf"
    n=$((n + 1))
  done < <(tail -n +2 "$CSV")
  [ "$n" -gt 0 ] || { echo 'ERROR: CSV has no rows'; exit 5; }
elif [ "$gf" = 1 ]; then
  ev=''; found=0
  while IFS= read -r line || [ -n "$line" ]; do
    line=${line%$'\r'}
    t=$(extract_ver "$line")
    if [ -n "$t" ]; then ev=$t; found=1; break; fi
  done < "$VFILE"
  h=${HOST:-input}
  if [ "$found" = 0 ]; then
    add_row "$h" '(not shown)' REVIEW "$LIFE"
  else
    add_row "$h" "$(showver "$ev")" "$(verdict_of "$ev")" "$LIFE"
  fi
else
  ev=$(extract_ver "$VSTR")
  h=${HOST:-input}
  if [ -z "$ev" ]; then
    add_row "$h" '(not shown)' REVIEW "$LIFE"
  else
    add_row "$h" "$(showver "$ev")" "$(verdict_of "$ev")" "$LIFE"
  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"
