#!/usr/bin/env bash
# check-servu-2026-4.sh: offline, read-only triage of one SolarWinds Serv-U version against Serv-U 2026.4 (released
# 24 Nov 2026), which fixes 18 SolarWinds CVEs, including nine privilege escalation issues that SolarWinds scores
# CVSS 3.1 9.1 Critical on Linux. Takes the version as a string (for example 2026.3, 2026.4 or 15.5.2), or reads a
# saved copy of Serv-U-StartupLog.txt, the file SolarWinds documents as holding the server log, which records the
# current build at start-up. It never connects to anything, never writes a file, runs no program and prints only
# versions, line numbers and cleaned file names. Pack 1.0.0. Bash and PowerShell (Check-ServU20264.ps1) give the same
# output and exit code.
#
# Affected (SolarWinds advisories and CVE records): Serv-U 2026.3 and below, Windows and Linux. Fixed: Serv-U 2026.4.
# OUR PATTERN: in a file, the first line holding "Serv-U" followed (within 40 non-digit characters) by a year-style
# version such as 2026.3 is read; older numbering and addresses in a file are never read as a version. The startup log line format
# is UNVERIFIED: SolarWinds documents that the build is logged, not how the line looks.
# OUR READING: older numbering (for example 15.5.2) is below 2026.3, so inside the affected range.
set -u
export LC_ALL=C

ADV="https://documentation.solarwinds.com/en/success_center/servu/content/release_notes/servu_2026-4_release_notes.htm"
MAX_FILE=4194304
MAX_LINE=65536
# CVE:score(Linux, or the only score):Windows score:scope (both, one, win, linux). Scores are SolarWinds' (CNA) scores.
CVES="CVE-2026-123167:9.1:4.7:both CVE-2026-123168:9.1:4.7:both CVE-2026-123172:9.1:4.7:both CVE-2026-82315:9.1:3.8:both CVE-2026-82316:9.1:3.8:both CVE-2026-82319:9.1:7.2:both CVE-2026-82320:9.1:7.2:both CVE-2026-82321:9.1:-:one CVE-2026-82322:9.1:4.7:both CVE-2026-82313:8.4:6.8:both CVE-2026-123166:7.2:-:win CVE-2026-82317:6.8:-:linux CVE-2026-123173:6.8:-:one CVE-2026-123170:6.5:-:one CVE-2026-123169:5.4:-:one CVE-2026-123171:5.3:-:one CVE-2026-82314:4.9:-:one CVE-2026-123165:4.7:-:one"

usage() {
  echo 'Usage: check-servu-2026-4.sh (--version-string S | --file F | --data-dir D) [--platform linux|windows]'
  echo 'Offline and read-only. --data-dir reads D/Serv-U-StartupLog.txt. 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"; }

VSTR=""; MODE=""; FILE=""; PLAT=""
while [ $# -gt 0 ]; do
  case $1 in
    --version-string|-VersionString) [ $# -ge 2 ] || die "version-string option needs a value"; [ -z "$MODE" ] || die "give only one of version-string, file and data-dir"; VSTR=$2; MODE=v; shift 2;;
    --file|-File) { [ $# -ge 2 ] && [ -n "$2" ]; } || die "file option needs a value"; [ -z "$MODE" ] || die "give only one of version-string, file and data-dir"; FILE=$2; MODE=f; shift 2;;
    --data-dir|-DataDir) { [ $# -ge 2 ] && [ -n "$2" ]; } || die "data-dir option needs a value"; [ -z "$MODE" ] || die "give only one of version-string, file and data-dir"; FILE="${2%/}/Serv-U-StartupLog.txt"; MODE=f; shift 2;;
    --platform|-Platform) [ $# -ge 2 ] || die "platform option needs a value"
      case $2 in linux|windows) PLAT=$2;; *) die "platform must be linux or windows";; esac; shift 2;;
    -h|--help|-Help) usage; exit 5;;
    *) die "unknown option: $(clean_name "$1")";;
  esac
done
[ -n "$MODE" ] || { usage; exit 5; }

classify() { # sets SHOWV W WHY
  local s M m
  s=$(trim "$1"); s=${s#SolarWinds }; s=${s#Serv-U }
  if [ "${#s}" -le 40 ] && [[ "$s" =~ ^([0-9]{1,4})\.([0-9]{1,2})((\.[0-9]{1,4}){0,2})$ ]]; then
    M=$((10#${BASH_REMATCH[1]})); m=$((10#${BASH_REMATCH[2]})); SHOWV=$s
    if [ "$M" -ge 2027 ] && [ "$M" -le 2099 ]; then W=REVIEW; WHY="$M.x is later than the 2026.4 advisories cover; confirm with SolarWinds"
    elif [ "$M" -eq 2026 ] && [ "$m" -eq 4 ]; then W=FIXED; WHY="Serv-U 2026.4 is the fixed release"
    elif [ "$M" -eq 2026 ] && [ "$m" -ge 5 ] && [ "$m" -le 12 ]; then W=REVIEW; WHY="2026.$m is later than 2026.4 and was not released when this pack was written; confirm with SolarWinds"
    elif [ "$M" -eq 2026 ] && [ "$m" -le 3 ]; then W=AFFECTED; WHY="SolarWinds lists Serv-U 2026.3 and below as affected; 2026.4 is the fixed release"
    elif [ "$M" -ge 2000 ] && [ "$M" -le 2025 ] && [ "$m" -le 12 ]; then W=AFFECTED; WHY="$M.$m is below 2026.3, inside the affected range (2026.3 and below); 2026.4 is the fixed release"
    elif [ "$M" -ge 10 ] && [ "$M" -le 99 ]; then W=AFFECTED; WHY="$M.$m is older numbering, below 2026.3 (OUR READING), so inside the affected range; 2026.4 is the fixed release"
    else SHOWV="unrecognised"; W=REVIEW; WHY="the number is not a Serv-U release number this checker knows"; fi
  else
    SHOWV="unreadable"; W=REVIEW; WHY="the version is not like 2026.4, 2026.3 or 15.5.2"
  fi
}

if [ "$MODE" = v ]; then
  LABEL="version string"; classify "$VSTR"
else
  { [ -r "$FILE" ] && [ ! -d "$FILE" ]; } || die "file not found or unreadable: $(clean_name "$FILE")"
  sz=$(head -c "$((MAX_FILE + 1))" < "$FILE" 2>/dev/null | wc -c) || die "file unreadable: $(clean_name "$FILE")"
  [ "$sz" -le "$MAX_FILE" ] || die "file too large: $(clean_name "$FILE")"
  RX='Serv-U[^0-9]{0,40}(20[0-9]{2}\.[0-9]{1,2}(\.[0-9]{1,4}){0,2})([^0-9.]|$)'
  n=0; hit=""; ver=""
  while IFS= read -r l || [ -n "$l" ]; do
    n=$((n + 1)); l=${l%$'\r'}
    [ "${#l}" -le "$MAX_LINE" ] || continue
    if [[ "$l" =~ $RX ]]; then hit=$n; ver=${BASH_REMATCH[1]}; break; fi
  done < <(head -c "$MAX_FILE" < "$FILE" | tr '\000' '\n')
  if [ -n "$hit" ]; then LABEL="file $(clean_name "$FILE"), line $hit"; classify "$ver"
  else LABEL="file $(clean_name "$FILE")"; SHOWV="not found"; W=REVIEW; WHY="no line with Serv-U followed by a year-style version such as 2026.3; OUR PATTERN, and the startup log format is UNVERIFIED"; fi
fi
case $W in FIXED) RC=0;; AFFECTED) RC=2;; *) RC=3;; esac
echo "TARGET 1: $LABEL"
echo "  VERSION: $SHOWV"
echo "  PLATFORM: ${PLAT:-not given}"
echo "  RESULT: $W ($WHY)"
for c in $CVES; do
  IFS=: read -r id sc ws scope <<< "$c"
  case $scope in
    both) lab="SolarWinds CVSS 3.1: Linux $sc, Windows $ws";;
    win) lab="SolarWinds CVSS 3.1: $sc, Windows only";;
    linux) lab="SolarWinds CVSS 3.1: $sc, Linux only";;
    *) lab="SolarWinds CVSS 3.1: $sc";;
  esac
  v=$W
  if { [ "$scope" = win ] && [ "$PLAT" = linux ]; } || { [ "$scope" = linux ] && [ "$PLAT" = windows ]; }; then v="NOT APPLICABLE (${lab##*, })"; fi
  echo "  $id ($lab): $v"
done
if [ "$MODE" = f ]; then echo "NOTE: the version came from a file through OUR PATTERN; the startup log format is UNVERIFIED. Confirm the version against your upgrade records."; fi
echo "NOTE: SolarWinds lists Serv-U 2026.3 and below as affected on Windows and Linux, and Serv-U 2026.4 as the fixed release. Most of these issues need a Serv-U domain administrator or higher; CVE-2026-82322 starts from a domain group administrator. 2026.4 also fixes 11 third-party CVEs that SolarWinds lists."
echo "NOTE: no exploitation reported by SolarWinds or CISA as of 24 Nov 2026. None of the 18 SolarWinds CVEs fixed in 2026.4 is in CISA KEV (catalogue 2026.11.23; KEV due dates bind US federal agencies only, BOD 26-04). KEV does list four earlier Serv-U CVEs. Patch now regardless."
echo "SOURCE: $ADV"
echo "RESULT: $W (exit $RC)"
exit "$RC"
