#!/usr/bin/env bash
# check-oracle-cspu.sh: offline, read-only checker for the Oracle Critical Security Patch Update of 17 November 2026
# (CSPU Nov 2026). Pack 1.0.0 (17 Nov 2026). It opens no sockets, writes no files and runs no other program except
# tr and wc. It reads only the files you name. It prints product keys, version numbers, CVE IDs and patch numbers only;
# it never prints host names, paths or other content from an OPatch inventory.
#
# Usage: check-oracle-cspu.sh --product KEY [--version V | --version-file FILE] [--lsinventory FILE] [--expected-patches FILE]
#   (-Product, -Version, -VersionFile, -LsInventory and -ExpectedPatches are accepted too)
#   --product KEY        weblogic, oam, opss, obiee, ebs, apex, ords, database, hyperion, autovue, mysql-shell or sdp
#   --version V          the product version, for example 14.1.2.0.0 or 19.32
#   --version-file F     a saved product/version line; the last word of the first non-empty line is the version
#                        (a line over 4096 bytes is not read as a version)
#   --lsinventory F      saved text output of: opatch lsinventory (only the "Patch NNNN : applied on" lines are read)
#   --expected-patches F the Nov 2026 patch numbers for your product and version, one per line (# starts a comment),
#                        copied from Oracle's Patch Availability Document on My Oracle Support (login required)
#
# Exit codes:
#   0 PATCHED   every expected patch number is applied in the saved inventory
#   1           not used (reserved for unexpected runtime errors)
#   2 AFFECTED  the version is listed by Oracle as affected and the Nov 2026 patches are not confirmed
#   3 UNKNOWN   no version, or a version Oracle does not list, and the Nov 2026 patches are not confirmed
#   4 MISSING   at least one expected patch number is not applied in the saved inventory
#   5 USAGE OR READ ERROR
set -u
export LC_ALL=C

# Oracle CSPU Nov 2026 risk matrices: product key|CVE|CVSS 3.1 base score|authentication|supported versions affected.
# Only the 9.0 and above rows for the products in this pack. Ranges are inclusive; a single value is an exact version.
DATA="weblogic|CVE-2026-91569|10.0|none|12.2.1.4.0;14.1.1.0.0;14.1.2.0.0;15.1.1.0.0
weblogic|CVE-2026-91570|9.8|none|12.2.1.4.0;14.1.1.0.0;14.1.2.0.0;15.1.1.0.0
weblogic|CVE-2026-91583|9.8|none|12.2.1.4.0;14.1.1.0.0;14.1.2.0.0;15.1.1.0.0
weblogic|CVE-2026-91456|9.8|none|12.2.1.4.0;14.1.1.0.0;14.1.2.0.0;15.1.1.0.0
weblogic|CVE-2026-91470|9.8|none|12.2.1.4.0;14.1.1.0.0;14.1.2.0.0;15.1.1.0.0
weblogic|CVE-2026-91585|9.8|none|12.2.1.4.0;14.1.1.0.0;14.1.2.0.0;15.1.1.0.0
weblogic|CVE-2026-91469|9.4|none|12.2.1.4.0;14.1.1.0.0;14.1.2.0.0;15.1.1.0.0
oam|CVE-2026-114835|10.0|none|12.2.1.4.0;14.1.2.1.0
oam|CVE-2026-91500|10.0|none|12.2.1.4.0;14.1.2.1.0
oam|CVE-2026-91510|10.0|none|12.2.1.4.0;14.1.2.1.0
oam|CVE-2026-91507|9.3|none|12.2.1.4.0;14.1.2.1.0
opss|CVE-2026-114824|10.0|none|12.2.1.4.0;14.1.2.0.0
obiee|CVE-2026-114784|10.0|none|8.2.0.0.0;26.01.0.0.0
ebs|CVE-2026-114880|9.8|none|12.2.3-12.2.15
ebs|CVE-2026-114924|9.8|none|12.2.3-12.2.15
ebs|CVE-2026-114925|9.8|none|12.2.3-12.2.15
ebs|CVE-2026-114863|9.8|none|12.2.3-12.2.15
ebs|CVE-2026-101691|9.3|none|12.2.3-12.2.15
ebs|CVE-2026-114881|9.1|none|12.2.3-12.2.15
apex|CVE-2026-114849|9.8|none|24.1.22;24.2.21;26.1.5;26.2.0
apex|CVE-2026-114850|9.8|none|24.1.22;24.2.21;26.1.5;26.2.0
ords|CVE-2026-114849|9.8|none|24.2.0-26.3.0
ords|CVE-2026-114856|9.6|none|24.2.0-26.3.0
database|CVE-2026-114819|9.9|required|19.3-19.32;21.3-21.24;23.4.0-23.26.3
database|CVE-2026-114848|9.9|required|19.3-19.33;21.3-21.24;23.4.0-23.26.3
database|CVE-2026-114822|9.9|required|23.4.0-23.26.4
hyperion|CVE-2026-114978|9.9|required|11.2.26.0.000
hyperion|CVE-2026-115048|9.8|none|11.2.26.0.000
hyperion|CVE-2026-115047|9.8|none|11.2.26.0.000
hyperion|CVE-2026-115080|9.8|none|11.2.26.0.000
hyperion|CVE-2026-115089|9.8|none|11.2.26.0.000
hyperion|CVE-2026-115086|9.8|none|11.2.26.0.000
hyperion|CVE-2026-114785|9.6|none|11.2.26.0.000
hyperion|CVE-2026-115139|9.3|none|11.2.26.0.000
hyperion|CVE-2026-114956|9.3|none|11.2.26.0.000
hyperion|CVE-2026-127362|9.1|none|11.2.26.0.000
hyperion|CVE-2026-127369|9.1|none|11.2.26.0.000
hyperion|CVE-2026-115050|9.1|none|11.2.26.0.000
autovue|CVE-2026-114980|9.9|required|21.1.0
autovue|CVE-2026-127324|9.8|none|21.1.0
autovue|CVE-2026-127325|9.8|none|21.1.0
autovue|CVE-2026-127326|9.8|none|21.1.0
autovue|CVE-2026-127327|9.8|none|21.1.0
mysql-shell|CVE-2026-115557|9.8|none|26.10.0
sdp|CVE-2026-114826|9.8|none|14.1.2.0.0"
PAD="https://www.oracle.com/security-alerts/cspunov2026.html"
MAX_SMALL=1048576
MAX_INV=8388608
MAX_LINE=4096

usage() {
  echo 'Usage: check-oracle-cspu.sh --product weblogic|oam|opss|obiee|ebs|apex|ords|database|hyperion|autovue|mysql-shell|sdp [--version V | --version-file FILE] [--lsinventory FILE] [--expected-patches FILE]'
  echo 'Offline and read-only. Exit codes: 0 PATCHED, 2 AFFECTED, 3 UNKNOWN, 4 MISSING, 5 USAGE OR READ ERROR (1 is not used).'
}
die() { echo "ERROR: $*"; exit 5; }
clean_path() { printf '%s' "$1" | tr -c '[:print:]' '?'; }
trim() { local s=$1; s="${s#"${s%%[![:space:]]*}"}"; s="${s%"${s##*[![:space:]]}"}"; printf '%s' "$s"; }
VER_RE='^[0-9]{1,5}(\.[0-9]{1,5}){0,5}$'
PATCH_RE='^[0-9]{6,10}$'
ver_cmp() {
  local IFS=. i x y; local -a a b; read -ra a <<< "$1"; read -ra b <<< "$2"
  for i in 0 1 2 3 4 5; do x=$((10#${a[i]:-0})); y=$((10#${b[i]:-0})); if [ "$x" -lt "$y" ]; then echo -1; return; elif [ "$x" -gt "$y" ]; then echo 1; return; fi; done
  echo 0
}
# in_ranges VERSION "a;b-c;..." prints yes or no
in_ranges() {
  local v=$1 r lo hi; local IFS=';'; local -a rs; read -ra rs <<< "$2"
  for r in "${rs[@]}"; do
    if [[ "$r" == *-* ]]; then lo=${r%-*}; hi=${r#*-}; else lo=$r; hi=$r; fi
    if [ "$(ver_cmp "$v" "$lo")" -ge 0 ] && [ "$(ver_cmp "$v" "$hi")" -le 0 ]; then echo yes; return; fi
  done
  echo no
}
# read_lines FILE MAX: fills LINES with the file's lines (NUL becomes space, trailing CR dropped).
read_lines() {
  local f=$1 max=$2 sz l
  { [ -f "$f" ] && [ -r "$f" ]; } || die "file not found or unreadable: $(clean_path "$f")"
  sz=$(wc -c < "$f" 2>/dev/null) || die "file unreadable: $(clean_path "$f")"
  [ "$sz" -le "$max" ] || die "file too large: $(clean_path "$f")"
  LINES=()
  while IFS= read -r l || [ -n "$l" ]; do LINES+=("${l%$'\r'}"); done < <(tr '\000' ' ' < "$f")
}

PRODUCT=""; OPT_VER=""; VFILE=""; INV=""; EXP=""
need() { [ "$1" -ge 2 ] || die "$2 option needs a value"; }
while [ $# -gt 0 ]; do
  case $1 in
    --product|-Product) need $# product; PRODUCT=$2; shift 2;;
    --version|-Version) need $# version
      [[ "$2" =~ $VER_RE ]] || die "version must be numbers and dots, for example 14.1.2.0.0 or 19.32"
      OPT_VER=$2; shift 2;;
    --version-file|-VersionFile) need $# version-file; VFILE=$2; [ -n "$VFILE" ] || die "version-file option needs a value"; shift 2;;
    --lsinventory|-LsInventory) need $# lsinventory; INV=$2; [ -n "$INV" ] || die "lsinventory option needs a value"; shift 2;;
    --expected-patches|-ExpectedPatches) need $# expected-patches; EXP=$2; [ -n "$EXP" ] || die "expected-patches option needs a value"; shift 2;;
    -h|--help|-Help) usage; exit 5;;
    *) die "unknown option: $(clean_path "$1")";;
  esac
done
case $PRODUCT in
  weblogic) PNAME="Oracle WebLogic Server";;
  oam) PNAME="Oracle Access Manager";;
  opss) PNAME="Oracle Platform Security for Java";;
  obiee) PNAME="Oracle Business Intelligence Enterprise Edition";;
  ebs) PNAME="Oracle E-Business Suite";;
  apex) PNAME="Oracle APEX";;
  ords) PNAME="Oracle REST Data Services";;
  database) PNAME="Oracle Database Server";;
  hyperion) PNAME="Oracle Hyperion";;
  autovue) PNAME="Oracle AutoVue";;
  mysql-shell) PNAME="MySQL Shell";;
  sdp) PNAME="Service Delivery Platform";;
  "") usage; exit 5;;
  *) die "product must be weblogic, oam, opss, obiee, ebs, apex, ords, database, hyperion, autovue, mysql-shell or sdp";;
esac
[ -n "$OPT_VER" ] && [ -n "$VFILE" ] && die "give --version or --version-file, not both"
[ -n "$EXP" ] && [ -z "$INV" ] && die "--expected-patches needs --lsinventory as well"

echo "PRODUCT: $PNAME ($PRODUCT)"
VER=""; VSRC=""
if [ -n "$OPT_VER" ]; then VER=$OPT_VER; VSRC="--version"
elif [ -n "$VFILE" ]; then
  read_lines "$VFILE" "$MAX_SMALL"
  first=""
  for l in "${LINES[@]}"; do
    if [ "${#l}" -gt "$MAX_LINE" ]; then first="(long line)"; break; fi
    t=$(trim "$l"); [ -n "$t" ] && { first=$t; break; }
  done
  last=${first##*[[:space:]]}
  if [[ "$last" =~ $VER_RE ]]; then VER=$last; VSRC=$(clean_path "$VFILE")
  else echo "NOTE: the version file's first line does not end in a plain version number (value withheld)."; fi
fi
if [ -n "$VER" ]; then echo "VERSION: $VER (from $VSRC)"; else echo "VERSION: not found"; fi

NCVE=0; NHIT=0
while IFS='|' read -r key cve score auth ranges; do
  [ "$key" = "$PRODUCT" ] || continue
  NCVE=$((NCVE + 1))
  if [ "$auth" = none ]; then authtxt="no authentication"; else authtxt="authentication required"; fi
  if [ -n "$VER" ] && [ "$(in_ranges "$VER" "$ranges")" = yes ]; then
    NHIT=$((NHIT + 1)); echo "LISTED: $cve (CVSS 3.1 $score, $authtxt) lists this version as affected"
  fi
done <<< "$DATA"
echo "HEADLINE CVES: $NCVE for this product (CVSS 3.1 9.0 or higher in the Oracle risk matrices)"
if [ -z "$VER" ]; then VSTATE=unknown
elif [ "$NHIT" -gt 0 ]; then VSTATE=affected; echo "RANGE: affected ($NHIT of $NCVE headline CVEs list $VER)"
else VSTATE=outside
  echo "RANGE: $VER is not in the supported versions Oracle lists; Oracle tests only supported versions and says earlier versions are likely affected"
fi

PSTATE=unknown
if [ -n "$INV" ]; then
  read_lines "$INV" "$MAX_INV"
  declare -A APPLIED=(); NAPP=0
  for l in "${LINES[@]}"; do
    if [[ "$l" =~ ^Patch[[:space:]]+([0-9]{6,10})[[:space:]]*:[[:space:]]*applied[[:space:]]on ]]; then
      p=${BASH_REMATCH[1]}
      if [ -z "${APPLIED["$p"]:-}" ]; then APPLIED["$p"]=1; NAPP=$((NAPP + 1)); fi
    fi
  done
  echo "INVENTORY: $NAPP applied patch numbers found"
  [ "$NAPP" = 0 ] && echo "NOTE: no 'Patch NNNN : applied on' lines found; check that the file is opatch lsinventory output"
  if [ -n "$EXP" ]; then
    read_lines "$EXP" "$MAX_SMALL"
    NEXP=0; NMISS=0
    for l in "${LINES[@]}"; do
      [ "${#l}" -le "$MAX_LINE" ] || die "expected-patches lines must be patch numbers of 6 to 10 digits (value withheld)"
      t=$(trim "$l"); [ -n "$t" ] || continue; [[ "$t" == '#'* ]] && continue
      [[ "$t" =~ $PATCH_RE ]] || die "expected-patches lines must be patch numbers of 6 to 10 digits (value withheld)"
      NEXP=$((NEXP + 1))
      if [ -n "${APPLIED["$t"]:-}" ]; then echo "PATCH PRESENT: $t"; else NMISS=$((NMISS + 1)); echo "PATCH MISSING: $t"; fi
    done
    [ "$NEXP" -gt 0 ] || die "expected-patches file lists no patch numbers"
    if [ "$NMISS" -gt 0 ]; then PSTATE=missing; else PSTATE=present; fi
  else
    echo "PATCH STATE: UNKNOWN: Oracle publishes the Nov 2026 patch numbers in the Patch Availability Document on My Oracle Support (login required); copy them into a file and pass --expected-patches"
  fi
else
  echo "PATCH STATE: UNKNOWN: pass --lsinventory (saved opatch lsinventory output) and --expected-patches"
fi

case $PSTATE in
  missing) rc=4;;
  present) rc=0;;
  *) if [ "$VSTATE" = affected ]; then rc=2; else rc=3; fi;;
esac
case $rc in
  0) msg="PATCHED: every expected Nov 2026 patch number is applied in the saved inventory";;
  2) msg="AFFECTED: apply the Nov 2026 CSPU patches from the Patch Availability Document now; until then apply the mitigations in the README (OUR advice)";;
  4) msg="MISSING: apply the missing Nov 2026 patches, then save a new opatch lsinventory and run this again";;
  *) msg="UNKNOWN: confirm the version and the Nov 2026 patch numbers from the Patch Availability Document";;
esac
echo "SOURCE: $PAD"
echo "RESULT: $msg (exit $rc)"
exit "$rc"
