#!/usr/bin/env bash
# Read-only targeted audit, baseline: 2026-10-24.
set -u
export LC_ALL=C

status() { printf '  %-19s %s\n' "$1" "$2"; }
version_ge() {
  # Numeric x.y.z comparison; returns success if first >= second.
  local a=${1#v} b=${2#v} i
  local -a av bv
  IFS=. read -r -a av <<< "${a%%[^0-9.]*}"
  IFS=. read -r -a bv <<< "$b"
  for i in 0 1 2 3; do
    local x=${av[i]:-0} y=${bv[i]:-0}
    (( 10#$x > 10#$y )) && return 0
    (( 10#$x < 10#$y )) && return 1
  done
  return 0
}

printf '\nSECURITY PATCH AUDIT | baseline 2026-10-24 | READ ONLY\n'
printf 'Host: %s | Date: %s\n' "$(hostname 2>/dev/null)" "$(date -Is)"
[ -r /etc/os-release ] && ( . /etc/os-release; printf 'OS: %s\n' "${PRETTY_NAME:-unknown}" )

printf '\n=== PHP: CVE-2026-109618 / CVE-2026-109619 ===\n'
mapfile -t phps < <( {
  command -v php 2>/dev/null || :
  find /opt/cpanel /opt/alt /usr/local/lsws /usr/bin /usr/local/bin \
    -maxdepth 6 \( -type f -o -type l \) \
    \( -name php -o -name lsphp \) -print 2>/dev/null || :
} | sort -u )
php_seen=0
for bin in "${phps[@]}"; do
  [ -x "$bin" ] || continue
  line=$(timeout 5 "$bin" -v 2>/dev/null | head -n 1) || :
  ver=$(printf '%s' "$line" | sed -nE 's/.*PHP ([0-9]+\.[0-9]+\.[0-9]+).*/\1/p')
  [ -n "$ver" ] || continue
  php_seen=$((php_seen+1))
  branch=${ver%.*}
  case "$branch" in
    8.2) minimum=8.2.35;;
    8.3) minimum=8.3.36;;
    8.4) minimum=8.4.27;;
    8.5) minimum=8.5.12;;
    *) minimum='';;
  esac
  if [ -z "$minimum" ]; then
    verdict='MANUAL VERIFICATION: unsupported/other branch; check vendor backports'
  elif version_ge "$ver" "$minimum"; then
    verdict='PATCHED VERSION (upstream comparison)'
  else
    verdict="PATCH REQUIRED by upstream version (minimum $minimum); check vendor backports"
  fi
  printf '  %s | PHP %s\n' "$bin" "$ver"
  status "$verdict" ''
done
[ "$php_seen" -gt 0 ] || status 'NOT DETECTED' 'PHP binary in scanned paths'

printf '\n=== NODE.JS: CVE-2026-111497 ===\n'
mapfile -t nodes < <( {
  command -v node 2>/dev/null || :
  find /opt/cpanel /opt/alt /usr/local/bin /usr/bin /opt/node /root/.nvm /home \
    -maxdepth 6 \( -type f -o -type l \) -name node -print 2>/dev/null || :
} | sort -u )
node_seen=0
for bin in "${nodes[@]}"; do
  [ -x "$bin" ] || continue
  ver=$(timeout 5 "$bin" --version 2>/dev/null | head -n 1) || :
  [[ "$ver" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+$ ]] || continue
  node_seen=$((node_seen+1))
  v=${ver#v}
  major=${v%%.*}
  case "$major" in
    22) minimum=22.23.4;;
    24) minimum=24.22.1;;
    26) minimum=26.12.1;;
    *) minimum='';;
  esac
  if [ -z "$minimum" ]; then
    verdict='MANUAL VERIFICATION: EOL/other branch; update to supported patched Node.js'
  elif version_ge "$v" "$minimum"; then
    verdict='PATCHED VERSION (upstream comparison)'
  else
    verdict="PATCH REQUIRED by upstream version (minimum $minimum)"
  fi
  printf '  %s | Node.js %s\n' "$bin" "$ver"
  status "$verdict" ''
done
[ "$node_seen" -gt 0 ] || status 'NOT DETECTED' 'Node.js binary in scanned paths'

printf '\n=== LITESPEED SERVER / CPANEL PLUGIN ===\n'
lsws_found=0
for bin in /usr/local/lsws/bin/lshttpd /usr/local/lsws/bin/openlitespeed; do
  if [ -x "$bin" ]; then
    lsws_found=1
    printf '  Binary: %s\n' "$bin"
    timeout 5 "$bin" -v 2>&1 | head -n 5 || :
  fi
done
[ "$lsws_found" -gt 0 ] || status 'NOT DETECTED' 'standard LiteSpeed server binaries'
for file in /usr/local/lsws/BUILD /usr/local/lsws/VERSION; do
  if [ -r "$file" ]; then
    printf '  %s: ' "$file"
    head -n 1 "$file"
  fi
done
printf '  Plugin package candidates (version numbers require verification):\n'
if command -v rpm >/dev/null 2>&1; then
  rpm -qa 2>/dev/null | grep -Ei 'litespeed|lsws' | head -n 25 || :
elif command -v dpkg-query >/dev/null 2>&1; then
  dpkg-query -W -f='${Package} ${Version}\n' '*litespeed*' '*lsws*' 2>/dev/null | head -n 25 || :
fi
printf '  LiteSpeed plugin version files, if available:\n'
find /usr/local/cpanel/whostmgr/docroot/cgi/lsws /usr/local/lsws/admin/misc \
  -maxdepth 3 -type f \( -iname '*version*' -o -name 'VERSION' \) \
  -print 2>/dev/null | head -n 15 || :
status 'MANUAL VERIFICATION' 'cPanel user-end plugin needs >=2.4.8; WHM plugin >=5.3.2.1 (if installed)'

printf '\n=== VENDOR BACKPORT / PACKAGE EVIDENCE ===\n'
if command -v rpm >/dev/null 2>&1; then
  rpm -qa --qf '%{NAME} %{VERSION}-%{RELEASE}\n' 2>/dev/null \
    | grep -Ei '^(ea-php|alt-php|php|lsphp|nodejs|litespeed|lsws)' | head -n 100 || :
elif command -v dpkg-query >/dev/null 2>&1; then
  dpkg-query -W -f='${Package} ${Version}\n' 'php*' 'nodejs*' 'lsphp*' '*litespeed*' 2>/dev/null | head -n 100 || :
fi
printf '\nImportant: packages with backported security fixes may show older upstream versions.\n'
printf 'This audit does not prove vulnerability/exploitation or inspect every container/user runtime.\n'
printf 'Review vendor security advisories and package changelogs before applying patches.\n'
