#!/usr/bin/env bash
# check-dimension-86130.sh: offline, read-only triage of WatchGuard Dimension CVE-2026-86130 and the other 14 CVEs
# fixed in Dimension 2.3.2. Reads a Dimension version given as a string (for example 2.3.2(752514), 2.3.1 or
# "2.1.2 Update 4 (622159)", the forms WatchGuard's release notes show under System Settings > Dimension System
# Information) and/or a build, or a saved text copy of that page. It never connects to anything, never writes a
# file and prints only a version rebuilt from digits, build digits and a cleaned file name.
# Pack 1.0.0. Bash and PowerShell (Check-Dimension86130.ps1) give the same output and exit code.
#
# WatchGuard PSIRT (19 Nov 2026), all 15 CVEs: affected Dimension >= 2.0, < 2.3.2; not affected >= 2.3.2.
# Builds from WatchGuard's Dimension release notes: 2.3.2 752514, 2.3.1 744770, 2.3 717220, 2.2.1 664502,
# 2.2 641253, 2.1.2 Update 4 622159. [UNVERIFIED] WatchGuard does not document a text export of the page, so
# the file reader accepts "Version", "Build" and "Linux" lines in any simple key and value layout.
set -u
export LC_ALL=C

ADV="https://psirt.watchguard.com/CVE-2026-86130"
FIX_MINOR=3; FIX_PATCH=2; FIX_BUILD=752514
KNOWN_BUILDS="752514 744770 717220 664502 641253 622159"
KNOWN_VERS=("2.3.2" "2.3.1" "2.3" "2.2.1" "2.2" "2.1.2 Update 4")
MAX_FILE=1048576
MAX_LINE=65536

usage() {
  echo 'Usage: check-dimension-86130.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 (1, 4 and 6 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')
}
known_ver() { # build: prints WatchGuard's documented version for the build, or nothing
  local i=0 k
  for k in $KNOWN_BUILDS; do [ "$k" = "$1" ] && { printf '%s' "${KNOWN_VERS[$i]}"; return; }; i=$((i + 1)); done
}

# classify VERSION BUILD: sets SHOWV, VERD, CODE (0 fixed, 2 affected, 3 review) and OLD (1 for 2.x below 2.3).
classify() {
  local raw=$1 s M m p u kv pb=""
  B=$2; SHOWV="not given"; CODE=3; OLD=0
  s=$(trim "$raw")
  for pre in 'WatchGuard Dimension ' 'Dimension '; do s=${s#"$pre"}; done
  s=${s#[vV]}; s=${s%.}
  if [[ "$s" =~ ^(.*)\(([0-9]{5,9})\)$ ]]; then pb=${BASH_REMATCH[2]}; s=$(trim "${BASH_REMATCH[1]}"); fi
  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 [ -n "$pb" ] && [ -n "$B" ] && [ "$pb" != "$B" ]; then SHOWV="not checked"; VERD="REVIEW (two different builds were given)"; return; fi
  [ -n "$B" ] || B=$pb
  if [ -z "$s" ]; then
    if [ -z "$B" ]; then VERD="REVIEW (no version or build read)"; return; fi
    kv=$(known_ver "$B")
    if [ -z "$kv" ]; then VERD="REVIEW (build $B is not one of WatchGuard's documented Dimension release builds; give the version)"; return; fi
    s=$kv
  fi
  if ! [[ "$s" =~ ^([0-9]{1,2})\.([0-9]{1,2})(\.([0-9]{1,2}))?([[:space:]]+[Uu]pdate[[:space:]]+([0-9]{1,2}))?$ ]]; then
    SHOWV="unreadable"; VERD="REVIEW (the version is not in a form this checker reads)"; return
  fi
  M=$((10#${BASH_REMATCH[1]})); m=$((10#${BASH_REMATCH[2]})); p=${BASH_REMATCH[4]}; u=${BASH_REMATCH[6]}
  SHOWV="$M.$m"; [ -z "$p" ] || { p=$((10#$p)); SHOWV="$SHOWV.$p"; }; [ -z "$u" ] || { u=$((10#$u)); SHOWV="$SHOWV Update $u"; }
  [ -n "$p" ] || p=0
  if [ -n "$B" ]; then
    kv=$(known_ver "$B")
    if [ -n "$kv" ] && [ "$kv" != "$SHOWV" ]; then VERD="REVIEW (build $B is WatchGuard's documented build of $kv, not $SHOWV)"; return; fi
  fi
  if [ "$M" -lt 2 ]; then VERD="REVIEW (WatchGuard lists Dimension 2.0 and later only; $SHOWV is not listed, confirm with WatchGuard)"; return; fi
  if [ "$M" -gt 2 ]; then VERD="REVIEW ($SHOWV is not covered by WatchGuard's 19 Nov 2026 advisories; confirm with WatchGuard)"; return; fi
  if [ "$m" -lt "$FIX_MINOR" ] || { [ "$m" = "$FIX_MINOR" ] && [ "$p" -lt "$FIX_PATCH" ]; }; then
    VERD="AFFECTED ($SHOWV is below 2.3.2; WatchGuard: Dimension 2.0 to below 2.3.2 is affected by all 15 CVEs)"; CODE=2
    [ "$m" -ge "$FIX_MINOR" ] || OLD=1
    return
  fi
  if [ "$m" = "$FIX_MINOR" ] && [ "$p" = "$FIX_PATCH" ] && [ -z "$u" ]; then
    if [ -n "$B" ] && [ "$B" != "$FIX_BUILD" ]; then VERD="REVIEW (build $B is not WatchGuard's documented build $FIX_BUILD for 2.3.2)"; return; fi
    VERD="FIXED (2.3.2 is the fixed version for all 15 CVEs; WatchGuard: 2.3.2 and later are not affected)"; CODE=0; return
  fi
  VERD="FIXED ($SHOWV is later than 2.3.2; WatchGuard: 2.3.2 and later are not affected)"; CODE=0
}

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; }

LX=""; MULTI=""
if [ -z "$FILE" ]; then
  LABEL="version string"; V=$VSTR; BB=$BSTR
else
  read_lines "$FILE"; LABEL="file $(clean_name "$FILE")"
  VS=(); BS=()
  for l in "${LINES[@]}"; do
    [ "${#l}" -le "$MAX_LINE" ] || continue
    l=${l#$'\xef\xbb\xbf'}
    if [[ "$l" =~ ^[[:space:]]*([Dd]imension[[:space:]]+)?[Vv]ersion([[:space:]]+is)?[[:space:]]*[:=]?[[:space:]]*(.*)$ ]]; then
      val=$(trim "${BASH_REMATCH[3]}"); [ -z "$val" ] || VS+=("$val")
    elif [[ "$l" =~ ^[[:space:]]*[Bb]uild([[:space:]]+[Nn]umber)?[[:space:]]*[:=]?[[:space:]]*(.*)$ ]]; then
      val=$(trim "${BASH_REMATCH[2]}"); [ -z "$val" ] || BS+=("$val")
    fi
    if [ -z "$LX" ] && [[ "$l" =~ [Ll]inux([[:space:]]+[Vv]ersion)?[[:space:]]*[:=]?[[:space:]]*[vV]?([0-9]{1,2})\.([0-9]{1,3}) ]]; then
      LX="$((10#${BASH_REMATCH[2]})).$((10#${BASH_REMATCH[3]}))"
    fi
  done
  V=""; BB=""
  [ "${#VS[@]}" = 0 ] || V=${VS[0]}
  [ "${#BS[@]}" = 0 ] || BB=${BS[0]}
  for x in "${VS[@]}"; do [ "$x" = "$V" ] || MULTI="Version"; done
  for x in "${BS[@]}"; do [ "$x" = "$BB" ] || MULTI="${MULTI:+$MULTI and }Build"; done
fi

if [ -n "$MULTI" ]; then
  SHOWV="not checked"; B=""; VERD="REVIEW (the file holds more than one different $MULTI value; check one Dimension at a time)"; CODE=3; OLD=0
else
  classify "$V" "$BB"
fi
if [ -n "$B" ] && [[ "$B" =~ ^[0-9]{5,9}$ ]]; then bt="build $B"; elif [ -n "$B" ]; then bt="build unreadable"; else bt="build not given"; fi
echo "TARGET: $LABEL"
echo "  VERSION: $SHOWV, $bt"
[ -z "$LX" ] || echo "  OS: Linux $LX"
echo "  RESULT: $VERD"
if [ "$OLD" = 1 ]; then
  echo "  NOTE: WatchGuard: to reach 2.3 or higher, deploy a new 2.3 or higher instance (Ubuntu 24.04 LTS) and migrate the database; from 2.1.2 Update 3 or lower, upgrade to 2.1.2 Update 4 first."
fi
if [ -n "$LX" ] && [ "${LX%%.*}" -lt 6 ]; then
  echo "  NOTE: WatchGuard: only a 2.3 or higher instance deployed from the OVA template (Linux 6.8.x) can take upgrade files; deploy a new instance."
fi
echo "NOTE: Dimension 2.3.2 (build 752514, 19 Nov 2026) fixes 15 CVEs. WatchGuard rates three Critical (CVSS 4.0 9.3): CVE-2026-86130, CVE-2026-84322 and CVE-2026-86094."
echo "NOTE: no exploitation reported by WatchGuard or CISA as of 19 Nov 2026 (not in CISA KEV, catalogue 2026.11.18). Patch now regardless."
echo "SOURCE: $ADV"
case $CODE in 0) RES="FIXED"; RC=0;; 2) RES="AFFECTED"; RC=2;; *) RES="REVIEW"; RC=3;; esac
echo "RESULT: $RES (exit $RC)"
exit "$RC"
