#!/usr/bin/env bash
# check-vmsa-2026-0011-vcenter.sh: offline, read-only triage of VMware vCenter for Broadcom VMSA-2026-0011 (24 Nov 2026):
# CVE-2026-123903 to CVE-2026-123909. 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-0011 FAQ question 31 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-Vmsa20260011Vcenter.ps1)
# give the same output and exit code.
#
# Fixed versions from the VMSA-2026-0011 response matrix; builds from Broadcom's release notes and KB 326316:
#   9.1.x: 9.1.2 (9.1.2.0, build 25946533); 9.0.x: 9.0.2.0300 (build 25937208); 8.0: 8.0 U3m (appliance version
#   8.0.3.01200, build 25934264); VCF 5.x: 5.2.5 (vCenter 8.0 U3m through KB 88287). 7.0: affected, fixes only with an
#   extended support contract. Versions the VMSA does not list (6.7 and earlier): Broadcom's FAQ says presume affected.
# 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/39626"
FIX80_PATCH=1200; FIX80_BUILD=25934264
FIX90_PATCH=300; FIX90_BUILD=25937208
FIX91_BUILD=25946533
OLD90_BUILD=25901331; OLD91_BUILD=25901347
CVES="CVE-2026-123903:9.8 CVE-2026-123904:7.5 CVE-2026-123905:7.5 CVE-2026-123906:7.5 CVE-2026-123907:7.5 CVE-2026-123908:6.7 CVE-2026-123909:5.2"
MAX_FILE=4194304
MAX_LINE=65536
MAX_TARGETS=2000

usage() {
  echo 'Usage: check-vmsa-2026-0011-vcenter.sh (--version-string S [--build N] | --build N | --file F)'
  echo 'Offline and read-only. Exit codes: 0 FIXED, 2 AFFECTED, 3 REVIEW, 5 USAGE OR READ ERROR (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')
}

AFF70="AFFECTED (Broadcom lists vCenter 7.0 as affected; End of General Support was 2 Oct 2025, so fixes come only through an extended support contract (contact Broadcom Support). Broadcom encourages upgrading to VCF 9.1)"
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-0011)"; 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" -ge "$1" ]; then
    if [ "$B" = "$1" ]; then VERD="FIXED (build $B is Broadcom's documented build of $2)"; else VERD="FIXED (build $B is at or above $1, the build of $2; OUR ASSUMPTION: builds rise within a branch)"; fi
    CODE=0
  else VERD="AFFECTED (build $B is below $1, the build of $2)"; CODE=2; fi
}
# classify VERSION BUILD: sets SHOWV, VERD, CODE (0 fixed, 2 affected, 3 review) and BR (8.0, 9.0, 9.1 or empty).
classify() {
  local raw=$1 s M m p q u lt pre
  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.0300 in Broadcom's release notes)"; CODE=0; BR=9.0;;
      "$FIX91_BUILD") VERD="FIXED (build $B is vCenter 9.1.2 in Broadcom's release notes)"; CODE=0; BR=9.1;;
      "$OLD90_BUILD") VERD="AFFECTED (build $B is vCenter 9.0.2.0200 from VMSA-2026-0009, which is not enough; 9.0.2.0300 is the fixed 9.0 release)"; CODE=2; BR=9.0;;
      "$OLD91_BUILD") VERD="AFFECTED (build $B is vCenter 9.1.1.0200 from VMSA-2026-0009, which is not enough; 9.1.2 is the fixed 9.1 release)"; CODE=2; 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" -eq 7 ] && [ "$m" -eq 0 ]; then VERD=$AFF70; CODE=2; return; fi
    if [ "$M" -ne 8 ] || [ "$m" -ne 0 ]; then VERD="REVIEW (update names are only checked for vCenter 7.0 and 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)"; BR=""
    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.0300, 9.1.2 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="AFFECTED (presumed: the VMSA does not list $M.$m and Broadcom's FAQ question 39 says to presume such versions are affected; upgrade)"; CODE=2; return; fi
  if [ "$M" -eq 7 ]; then
    if [ "$m" -ne 0 ]; then VERD="REVIEW (7.$m is not a vCenter release)"; return; fi
    VERD=$AFF70; 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.0300" "9.0"
    elif [ "$p" -lt 2 ]; then VERD="AFFECTED (9.0.2.0300 is the fixed 9.0 release)"; CODE=2
    elif [ "$p" -gt 2 ]; then VERD="FIXED ($s is later than 9.0.2.0300; Broadcom: patches are cumulative)"; CODE=0
    elif [ -z "$q" ]; then by_build "$FIX90_BUILD" "9.0.2.0300" "9.0.2"
    elif [ "$q" -lt "$FIX90_PATCH" ]; then VERD="AFFECTED ($s is below 9.0.2.0300; 9.0.2.0200 from VMSA-2026-0009 is not enough)"; CODE=2
    elif [ "$q" -eq "$FIX90_PATCH" ]; then fixed_exact "$FIX90_BUILD" "9.0.2.0300"
    else VERD="FIXED ($s is later than 9.0.2.0300; 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.2" "9.1"
    elif [ "$p" -lt 2 ]; then VERD="AFFECTED (9.1.2 is the fixed 9.1 release; 9.1.1 and 9.1.1.0200 are not enough)"; CODE=2
    elif [ "$p" -gt 2 ]; then VERD="FIXED ($s is later than 9.1.2; Broadcom: patches are cumulative)"; CODE=0
    elif [ -z "$q" ] || [ "$q" -eq 0 ]; then fixed_exact "$FIX91_BUILD" "9.1.2"
    else VERD="FIXED ($s is later than 9.1.2; Broadcom: patches are cumulative)"; CODE=0; fi
    return
  fi
  VERD="REVIEW ($M.$m is not covered by VMSA-2026-0011; 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; }

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; nr=0; nf=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
  case $CODE in 0) w=FIXED; nf=$((nf + 1));; 2) w=AFFECTED; na=$((na + 1));; *) w=REVIEW; nr=$((nr + 1));; esac
  echo "TARGET $n: ${TL[$idx]}"
  echo "  VERSION: $SHOWV, $bt"
  echo "  RESULT: $VERD"
  for c in $CVES; do echo "  ${c%%:*} (CVSS 3.1 ${c#*:}): $w"; done
  if [ "$BR" = 8.0 ] && [ "$CODE" = 0 ]; then
    echo "  NOTE: vCenter 8.0 U3m is also the 8.0 fix for VMSA-2026-0009. If it was applied for VMSA-2026-0009 (including on VCF 5.2.x), vCenter needs no further update for this advisory (Broadcom FAQ question 8). ESX still needs this advisory's update."
  fi
done
if [ "$na" -gt 0 ]; then RES="AFFECTED"; RC=2
elif [ "$nr" -gt 0 ]; then RES="REVIEW"; RC=3
else RES="FIXED"; RC=0; fi
echo "NOTE: Broadcom lists no workaround for the vCenter issues. VCF 5.x: apply vCenter 8.0 U3m through the asynchronous patching process in KB 88287. Telco Cloud Platform and Telco Cloud Infrastructure: follow KB 463501."
echo "NOTE: CVE-2026-123903 is a new and distinct issue, not a fix for CVE-2026-123890 (Broadcom FAQ question 7)."
echo "NOTE: no exploitation reported by Broadcom or CISA as of 24 Nov 2026. None of the 23 CVEs is in CISA KEV (catalogue 2026.11.23; KEV due dates bind US federal agencies only, BOD 26-04). Broadcom's FAQ treats the four Critical issues (CVSS 3.1 9.8, 9.8, 9.8 and 9.3) as an emergency change. Patch now regardless."
echo "NOTE: ESX hosts need this advisory's ESX update even when vCenter is fixed, and VMware Tools is updated separately inside each VM."
echo "SOURCE: $ADV"
echo "RESULT: $RES (targets: $na affected, $nr review, $nf fixed) (exit $RC)"
exit "$RC"
