#!/usr/bin/env bash
# check-vcenter-123890.sh: offline, read-only triage of VMware vCenter CVE-2026-123890 (VMSA-2026-0009).
# Reads a vCenter version and build given as strings, or a saved file: PowerCLI Format-List output or an Export-Csv
# file with Version and Build columns (the $global:DefaultVIServer properties Broadcom's VMSA-2026-0009 FAQ names).
# It never connects to anything, never writes a file and prints only versions, builds, row numbers and cleaned names.
# Pack 1.0.0. Bash and PowerShell (Check-Vcenter123890.ps1) give the same output and exit code.
#
# Fixed versions from VMSA-2026-0009 (19 Nov 2026); builds from Broadcom's release notes for each fixed version:
#   9.1.x: 9.1.1.0200 (build 25901347); 9.0.x: 9.0.2.0200 (build 25901331); 8.0: 8.0 U3m (appliance version
#   8.0.3.01200, build 25934264); VCF 5.x applies vCenter 8.0 U3m. 7.0: affected, patches only under extended support.
#   6.5 and 6.7: Broadcom's FAQ says presume affected (past End of General Support).
# OUR ASSUMPTION: within one release branch, a higher build or appliance patch number is a later release.
set -u
export LC_ALL=C

ADV="https://support.broadcom.com/web/ecx/support-content-notification/-/external/content/SecurityAdvisories/0/39495"
FIX80_PATCH=1200; FIX80_BUILD=25934264
FIX90_PATCH=200; FIX90_BUILD=25901331
FIX91_PATCH=200; FIX91_BUILD=25901347
MAX_FILE=4194304
MAX_LINE=65536
MAX_TARGETS=2000

usage() {
  echo 'Usage: check-vcenter-123890.sh (--version-string S [--build N] | --build N | --file F)'
  echo 'Offline and read-only. Exit codes: 0 FIXED, 2 AFFECTED, 3 REVIEW OR UNKNOWN, 5 USAGE OR READ ERROR, 6 EOS PRESUMED AFFECTED (1 and 4 are not used).'
}
die() { echo "ERROR: $*"; exit 5; }
clean_name() { local b=${1##*/}; [ -n "$b" ] || b=$1; printf '%s' "$b" | tr -c '[:alnum:]._-' '?' | head -c 64; }
trim() { local s=$1; s=${s#"${s%%[![:space:]]*}"}; s=${s%"${s##*[![:space:]]}"}; printf '%s' "$s"; }
read_lines() {
  local f=$1 sz l
  { [ -r "$f" ] && [ ! -d "$f" ]; } || die "file not found or unreadable: $(clean_name "$f")"
  sz=$(head -c "$((MAX_FILE + 1))" < "$f" 2>/dev/null | wc -c) || die "file unreadable: $(clean_name "$f")"
  [ "$sz" -le "$MAX_FILE" ] || die "file too large: $(clean_name "$f")"
  LINES=()
  while IFS= read -r l || [ -n "$l" ]; do LINES+=("${l%$'\r'}"); done < <(head -c "$MAX_FILE" < "$f" | tr '\000' '\n')
}

# classify VERSION BUILD: sets SHOWV, VERD, CODE (0 fixed, 2 affected, 3 review, 6 eos) and BR (8.0, 9.0, 9.1 or empty).
fixed_exact() { # fixed build label
  if [ -n "$B" ] && [ "$B" != "$1" ]; then VERD="REVIEW (build $B is not Broadcom's documented build $1 for $2)"; CODE=3
  else VERD="FIXED ($2 is the fixed version in VMSA-2026-0009)"; CODE=0; fi
}
by_build() { # fixed_build fixed_label branch_label
  if [ -z "$B" ]; then VERD="REVIEW ($3 alone does not say which patch is installed; give the build or the full appliance version)"; CODE=3
  elif [ "$B" = "$1" ]; then VERD="FIXED (build $B is Broadcom's documented build of $2)"; CODE=0
  elif [ "$B" -ge "$1" ]; then VERD="FIXED (build $B is at or above $1, the build of $2; OUR ASSUMPTION: builds rise within a branch)"; CODE=0
  else VERD="AFFECTED (build $B is below $1, the build of $2)"; CODE=2; fi
}
classify() {
  local raw=$1 s M m p q u lt
  B=$2; SHOWV=""; BR=""; CODE=3
  s=$(trim "$raw")
  for pre in 'VMware vCenter Server ' 'vCenter Server ' 'vCenter '; do s=${s#"$pre"}; done
  if [ -n "$B" ] && ! [[ "$B" =~ ^[0-9]{5,9}$ ]]; then SHOWV="not checked"; VERD="REVIEW (the build is not a number of 5 to 9 digits)"; return; fi
  if [ -z "$s" ]; then
    SHOWV="not given"
    case $B in
      '') VERD="REVIEW (no version or build read)";;
      "$FIX80_BUILD") VERD="FIXED (build $B is vCenter 8.0 U3m in Broadcom's release notes)"; CODE=0; BR=8.0;;
      "$FIX90_BUILD") VERD="FIXED (build $B is vCenter 9.0.2.0200 in Broadcom's release notes)"; CODE=0; BR=9.0;;
      "$FIX91_BUILD") VERD="FIXED (build $B is vCenter 9.1.1.0200 in Broadcom's release notes)"; CODE=0; BR=9.1;;
      *) VERD="REVIEW (a build number alone cannot be placed in a release branch; give the version too)";;
    esac
    return
  fi
  if [ "${#s}" -le 40 ] && [[ "$s" =~ ^([0-9]{1,2})\.([0-9]{1,2})[[:space:]]*(U|Update[[:space:]]*)([0-9])([a-z]?)$ ]]; then
    M=$((10#${BASH_REMATCH[1]})); m=$((10#${BASH_REMATCH[2]})); u=$((10#${BASH_REMATCH[4]})); lt=${BASH_REMATCH[5]}; SHOWV="$M.$m U$u$lt"
    if [ "$M" -ne 8 ] || [ "$m" -ne 0 ]; then VERD="REVIEW (update names are only checked for vCenter 8.0)"; return; fi
    BR=8.0
    if [ "$u" -lt 3 ]; then VERD="AFFECTED (8.0 U3m is the fixed 8.0 release)"; CODE=2
    elif [ "$u" -gt 3 ]; then VERD="REVIEW (8.0 U$u is not a release Broadcom lists)"
    elif [ -z "$lt" ] || [[ "$lt" < m ]]; then VERD="AFFECTED (8.0 U3${lt} is earlier than the fixed 8.0 U3m)"; CODE=2
    elif [ "$lt" = m ]; then fixed_exact "$FIX80_BUILD" "8.0 U3m"
    else VERD="FIXED (8.0 U3$lt is later than 8.0 U3m; Broadcom: patches are cumulative)"; CODE=0; fi
    return
  fi
  if ! { [ "${#s}" -le 40 ] && [[ "$s" =~ ^([0-9]{1,2})\.([0-9]{1,2})(\.([0-9]{1,2}))?(\.([0-9]{1,5}))?$ ]]; }; then
    SHOWV="unreadable"; VERD="REVIEW (the version is not like 8.0.3.01200, 9.0.2.0200 or 8.0 U3m)"; return
  fi
  SHOWV=$s; M=$((10#${BASH_REMATCH[1]})); m=$((10#${BASH_REMATCH[2]})); p=${BASH_REMATCH[4]}; q=${BASH_REMATCH[6]}
  [ -z "$p" ] || p=$((10#$p)); [ -z "$q" ] || q=$((10#$q))
  if [ "$M" -le 6 ]; then VERD="EOS PRESUMED AFFECTED (Broadcom's FAQ: presume 6.5 and 6.7 are affected; they are past End of General Support)"; CODE=6; return; fi
  if [ "$M" -eq 7 ]; then
    if [ "$m" -ne 0 ]; then VERD="REVIEW (7.$m is not a vCenter release)"; return; fi
    VERD="AFFECTED (Broadcom lists 7.0 as affected; End of General Support was 2 Oct 2025, so patches come only through an extended support contract)"; CODE=2; return
  fi
  if [ "$M" -eq 8 ]; then
    if [ "$m" -ne 0 ]; then VERD="REVIEW (8.$m is not a vCenter release)"; return; fi
    BR=8.0
    if [ -z "$p" ]; then by_build "$FIX80_BUILD" "8.0 U3m" "8.0"
    elif [ "$p" -lt 3 ]; then VERD="AFFECTED (8.0 U3m is the fixed 8.0 release)"; CODE=2
    elif [ "$p" -gt 3 ]; then VERD="REVIEW (8.0.$p is not a release Broadcom lists)"; BR=""
    elif [ -z "$q" ]; then by_build "$FIX80_BUILD" "8.0 U3m" "8.0.3"
    elif [ "$q" -lt "$FIX80_PATCH" ]; then VERD="AFFECTED (appliance version $s is below 8.0.3.01200, the 8.0 U3m appliance version)"; CODE=2
    elif [ "$q" -eq "$FIX80_PATCH" ]; then fixed_exact "$FIX80_BUILD" "8.0 U3m (8.0.3.01200)"
    else VERD="FIXED (appliance version $s is above 8.0.3.01200; Broadcom: patches are cumulative)"; CODE=0; fi
    return
  fi
  if [ "$M" -eq 9 ] && [ "$m" -eq 0 ]; then
    BR=9.0
    if [ -z "$p" ]; then by_build "$FIX90_BUILD" "9.0.2.0200" "9.0"
    elif [ "$p" -lt 2 ]; then VERD="AFFECTED (9.0.2.0200 is the fixed 9.0 release)"; CODE=2
    elif [ "$p" -gt 2 ]; then VERD="FIXED ($s is later than 9.0.2.0200; Broadcom: patches are cumulative)"; CODE=0
    elif [ -z "$q" ]; then by_build "$FIX90_BUILD" "9.0.2.0200" "9.0.2"
    elif [ "$q" -lt "$FIX90_PATCH" ]; then VERD="AFFECTED ($s is below 9.0.2.0200)"; CODE=2
    elif [ "$q" -eq "$FIX90_PATCH" ]; then fixed_exact "$FIX90_BUILD" "9.0.2.0200"
    else VERD="FIXED ($s is later than 9.0.2.0200; Broadcom: patches are cumulative)"; CODE=0; fi
    return
  fi
  if [ "$M" -eq 9 ] && [ "$m" -eq 1 ]; then
    BR=9.1
    if [ -z "$p" ]; then by_build "$FIX91_BUILD" "9.1.1.0200" "9.1"
    elif [ "$p" -lt 1 ]; then VERD="AFFECTED (9.1.1.0200 is the fixed 9.1 release)"; CODE=2
    elif [ "$p" -gt 1 ]; then VERD="FIXED ($s is later than 9.1.1.0200; Broadcom: patches are cumulative)"; CODE=0
    elif [ -z "$q" ]; then by_build "$FIX91_BUILD" "9.1.1.0200" "9.1.1"
    elif [ "$q" -lt "$FIX91_PATCH" ]; then VERD="AFFECTED ($s is below 9.1.1.0200)"; CODE=2
    elif [ "$q" -eq "$FIX91_PATCH" ]; then fixed_exact "$FIX91_BUILD" "9.1.1.0200"
    else VERD="FIXED ($s is later than 9.1.1.0200; Broadcom: patches are cumulative)"; CODE=0; fi
    return
  fi
  VERD="REVIEW ($M.$m is not covered by VMSA-2026-0009; confirm with Broadcom)"
}

VSTR=""; VSET=0; BSTR=""; BSET=0; FILE=""
need() { [ "$1" -ge 2 ] || die "$2 option needs a value"; }
while [ $# -gt 0 ]; do
  case $1 in
    --version-string|-VersionString) need $# version-string; VSTR=$2; VSET=1; shift 2;;
    --build|-Build) need $# build; BSTR=$(trim "$2"); BSET=1; [ -n "$BSTR" ] || die "build option needs a value"; shift 2;;
    --file|-File) need $# file; FILE=$2; [ -n "$FILE" ] || die "file option needs a value"; shift 2;;
    -h|--help|-Help) usage; exit 5;;
    *) die "unknown option: $(clean_name "$1")";;
  esac
done
[ -z "$FILE" ] || { [ "$VSET" = 0 ] && [ "$BSET" = 0 ]; } || die "give --file, or --version-string and --build, not both"
[ -n "$FILE" ] || [ "$VSET" = 1 ] || [ "$BSET" = 1 ] || { usage; exit 5; }

# Targets: parallel arrays of label, version and build.
TL=(); TV=(); TB=()
add_target() { [ "${#TL[@]}" -lt "$MAX_TARGETS" ] && { TL+=("$1"); TV+=("$2"); TB+=("$3"); }; return 0; }
unq() { local s; s=$(trim "$1"); s=${s#\"}; s=${s%\"}; trim "$s"; }
if [ -z "$FILE" ]; then
  add_target "version string" "$VSTR" "$BSTR"
else
  read_lines "$FILE"; FN=$(clean_name "$FILE")
  hdr=-1; i=0
  for l in "${LINES[@]}"; do
    l=${l#$'\xef\xbb\xbf'}
    if [ -n "$(trim "$l")" ] && [[ "$l" != '#'* ]]; then hdr=$i; break; fi
    i=$((i + 1))
  done
  ci_v=-1; ci_b=-1; ci_n=-1
  if [ "$hdr" -ge 0 ] && [[ "${LINES[$hdr]}" == *,* ]]; then
    IFS=',' read -r -a H <<< "${LINES[$hdr]#$'\xef\xbb\xbf'}"
    for k in "${!H[@]}"; do
      h=$(unq "${H[$k]}"); h=${h,,}
      case $h in version) ci_v=$k;; build) ci_b=$k;; name) ci_n=$k;; esac
    done
  fi
  if [ "$ci_v" -ge 0 ] || [ "$ci_b" -ge 0 ]; then
    row=1
    for ((j = hdr + 1; j < ${#LINES[@]}; j++)); do
      l=${LINES[$j]}; [ "${#l}" -le "$MAX_LINE" ] || continue
      [ -n "$(trim "$l")" ] || continue
      row=$((row + 1))
      IFS=',' read -r -a F <<< "$l"
      v=""; b=""; n=""
      [ "$ci_v" -lt 0 ] || v=$(unq "${F[$ci_v]:-}")
      [ "$ci_b" -lt 0 ] || b=$(unq "${F[$ci_b]:-}")
      [ "$ci_n" -lt 0 ] || n=$(unq "${F[$ci_n]:-}")
      lab="file $FN, row $row"; [ -z "$n" ] || lab="$lab ($(clean_name "$n"))"
      add_target "$lab" "$v" "$b"
    done
  else
    blk=0; v=""; b=""; n=""; hv=0; hb=0; hn=0
    flush() { if [ "$hv" = 1 ] || [ "$hb" = 1 ]; then blk=$((blk + 1)); lab="file $FN, entry $blk"; [ -z "$n" ] || lab="$lab ($(clean_name "$n"))"; add_target "$lab" "$v" "$b"; fi; v=""; b=""; n=""; hv=0; hb=0; hn=0; }
    for l in "${LINES[@]}"; do
      [ "${#l}" -le "$MAX_LINE" ] || continue
      if [ -z "$(trim "$l")" ]; then flush; continue; fi
      if [[ "$l" =~ ^[[:space:]]*([Nn][Aa][Mm][Ee]|[Vv][Ee][Rr][Ss][Ii][Oo][Nn]|[Bb][Uu][Ii][Ll][Dd])[[:space:]]*:(.*)$ ]]; then
        k=${BASH_REMATCH[1],,}; val=$(trim "${BASH_REMATCH[2]}")
        case $k in
          version) [ "$hv" = 0 ] || flush; v=$val; hv=1;;
          build) [ "$hb" = 0 ] || flush; b=$val; hb=1;;
          name) [ "$hn" = 0 ] || flush; n=$val; hn=1;;
        esac
      fi
    done
    flush
  fi
  [ "${#TL[@]}" -gt 0 ] || add_target "file $FN" "" ""
fi

na=0; ne=0; nr=0; nf=0; vcf=0
for idx in "${!TL[@]}"; do
  classify "${TV[$idx]}" "${TB[$idx]}"
  n=$((idx + 1))
  if [ -n "${TB[$idx]}" ] && [[ "${TB[$idx]}" =~ ^[0-9]{5,9}$ ]]; then bt="build ${TB[$idx]}"; elif [ -n "${TB[$idx]}" ]; then bt="build unreadable"; else bt="build not given"; fi
  echo "TARGET $n: ${TL[$idx]}"
  echo "  VERSION: $SHOWV, $bt"
  echo "  RESULT: $VERD"
  case $CODE in 0) nf=$((nf + 1));; 2) na=$((na + 1));; 6) ne=$((ne + 1));; *) nr=$((nr + 1));; esac
  if { [ "$BR" = 8.0 ] || [ "$BR" = 9.0 ]; } && { [ "$CODE" = 0 ] || [ "$CODE" = 2 ]; }; then
    echo "  NOTE: vCenter 8.0 U3m and 9.0.2.0200 block a later upgrade to VCF 9.1.1 (Broadcom FAQ, Back-in-Time). If you are moving to VCF 9.1, upgrade vCenter to 9.1.1 and apply 9.1.1.0200, or wait for VCF 9.1.2."
    vcf=1
  fi
done
if [ "$na" -gt 0 ]; then RES="AFFECTED"; RC=2
elif [ "$ne" -gt 0 ]; then RES="EOS PRESUMED AFFECTED"; RC=6
elif [ "$nr" -gt 0 ]; then RES="REVIEW"; RC=3
else RES="FIXED"; RC=0; fi
echo "NOTE: VMSA-2026-0009 (19 Nov 2026) lists no workaround. ESX is not affected. vCenter updates in this advisory cannot be installed with vCenter Quick Patch."
echo "NOTE: no exploitation reported by Broadcom or CISA as of 19 Nov 2026 (not in CISA KEV, catalogue 2026.11.18). Patch now regardless."
[ "$vcf" = 0 ] || echo "NOTE: the VCF 9.1.1 upgrade note above applies only if a VCF 9.1.1 upgrade is planned."
echo "SOURCE: $ADV"
echo "RESULT: $RES (targets: $na affected, $ne EOS presumed affected, $nr review, $nf fixed) (exit $RC)"
exit "$RC"
