#!/usr/bin/env bash
# Read-only checker for WebPros SolusVM 2 WHMCS provisioning module CVE-2026-136936
# (account takeover via unverified ownership). Compares a version you supply, or the
# official modules/servers/solusvm2vps/VERSION file, with the affected range (all
# versions up to 1.0.74, before 1.0.75) and the fixed release 1.0.75 or later.
# No network, no file writes, no payloads, no database connections.
# It never contacts a SolusVM or WHMCS server and never opens configuration.php,
# .env, config.php or other files that hold database, API or secret values.
# Exit: 0 FIXED, 2 AFFECTED, 3 REVIEW, 5 usage or read error. Pack 1.0.0.
# Usage: check-solusvm2-whmcs-cve-2026-136936.sh [--version V] [--version-file FILE] [--root DIR] [--csv FILE]
#   [--link-users VALUE] [--billing-audit VALUE] [--host NAME]
#   --version V            module version, for example 1.0.74 or 1.0.75
#   --version-file F       saved copy of modules/servers/solusvm2vps/VERSION
#   --root DIR             WHMCS install root; reads only DIR/modules/servers/solusvm2vps/VERSION
#   --csv FILE             inventory CSV with header host,version,link_users,billing_audit
#   --link-users VALUE     done, not-required, pending or unknown (default unknown)
#   --billing-audit VALUE  done, pending or unknown (default unknown)
#   --host NAME            label for a single-row run
set -u
export LC_ALL=C

usage() {
  echo 'usage: check-solusvm2-whmcs-cve-2026-136936.sh [--version V] [--version-file FILE] [--root DIR] [--csv FILE] [--link-users VALUE] [--billing-audit VALUE] [--host NAME]'
  exit 5
}
esc() {
  local s=$1 o='' c i h
  for ((i = 0; i < ${#s}; i++)); do
    c=${s:i:1}
    if [[ $c == [A-Za-z0-9._/-] ]]; then o+=$c; else printf -v h '\\x%02X' "'$c"; o+=$h; fi
  done
  printf '%s' "$o"
}

is_denied_file() {
  local b
  b=$(basename -- "$1")
  case $b in
    configuration.php|configuration.php-dist|configuration.php.dist|.env|.env.local|.env.production|.env.example|config.php|config-mp.php) return 0;;
  esac
  return 1
}

VSTR=''; VFILE=''; ROOT=''; CSV=''; LU='unknown'; BA='unknown'; HOST=''
gv=0; gf=0; gr=0; gc=0; glu=0; gba=0; gh=0
while [ $# -gt 0 ]; do
  case $1 in
    --version) [ $# -ge 2 ] || usage; [ "$gv" = 0 ] || { echo 'ERROR: give each option once'; exit 5; }; gv=1; VSTR=$2; shift 2;;
    --version-file) [ $# -ge 2 ] || usage; [ "$gf" = 0 ] || { echo 'ERROR: give each option once'; exit 5; }; gf=1; VFILE=$2; shift 2;;
    --root) [ $# -ge 2 ] || usage; [ "$gr" = 0 ] || { echo 'ERROR: give each option once'; exit 5; }; gr=1; ROOT=$2; shift 2;;
    --csv) [ $# -ge 2 ] || usage; [ "$gc" = 0 ] || { echo 'ERROR: give each option once'; exit 5; }; gc=1; CSV=$2; shift 2;;
    --link-users) [ $# -ge 2 ] || usage; [ "$glu" = 0 ] || { echo 'ERROR: give each option once'; exit 5; }; glu=1; LU=$2; shift 2;;
    --billing-audit) [ $# -ge 2 ] || usage; [ "$gba" = 0 ] || { echo 'ERROR: give each option once'; exit 5; }; gba=1; BA=$2; shift 2;;
    --host) [ $# -ge 2 ] || usage; [ "$gh" = 0 ] || { echo 'ERROR: give each option once'; exit 5; }; gh=1; HOST=$2; shift 2;;
    -h|--help) usage;;
    *) echo "ERROR: unknown option: $(esc "$1")"; exit 5;;
  esac
done
[ "$gv$gf$gr$gc" != 0000 ] || usage
src=$((gv + gf + gr))
if [ "$gc" = 1 ] && [ "$src" -ne 0 ]; then echo 'ERROR: use only one of --csv or --version/--version-file/--root'; exit 5; fi
if [ "$src" -gt 1 ]; then echo 'ERROR: use only one of --csv or --version/--version-file/--root'; exit 5; fi

norm_link() {
  local e; e=$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]')
  case $e in
    done|completed|yes) printf 'done';;
    not-required|notrequired|no-import|noimport|none) printf 'not-required';;
    pending|no|incomplete) printf 'pending';;
    ''|unknown|unk) printf 'unknown';;
    *) printf 'unknown';;
  esac
}

norm_audit() {
  local e; e=$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]' | tr -d '[:space:]')
  case $e in
    done|completed|yes) printf 'done';;
    pending|no|incomplete) printf 'pending';;
    ''|unknown|unk) printf 'unknown';;
    *) printf 'unknown';;
  esac
}

# Return a canonical x.y.z or empty.
extract_ver() {
  local s=$1
  s=$(printf '%s' "$s" | tr -d '\r')
  if printf '%s' "$s" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$'; then
    printf '%s' "$s"
    return
  fi
  return 0
}

# Parse the official VERSION file (a single x.y.z line). Never run PHP or open secrets.
parse_version_file() {
  local f=$1 line t first=''
  while IFS= read -r line || [ -n "$line" ]; do
    line=${line%$'\r'}
    case $line in \#*|//*) continue;; esac
    t=$(printf '%s' "$line" | tr -d '[:space:]')
    if printf '%s' "$t" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$'; then
      printf '%s' "$t"
      return
    fi
    if [ -z "$first" ] && [ -n "$t" ]; then first=$t; fi
  done < "$f"
  if printf '%s' "$first" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+$'; then
    printf '%s' "$first"
  fi
}

cmp_ver() {
  local a=$1 b=$2
  local IFS=.
  # shellcheck disable=SC2086
  set -- $a
  local a1=${1:-0} a2=${2:-0} a3=${3:-0}
  # shellcheck disable=SC2086
  set -- $b
  local b1=${1:-0} b2=${2:-0} b3=${3:-0}
  if [ "$a1" -lt "$b1" ]; then echo -1; return; fi
  if [ "$a1" -gt "$b1" ]; then echo 1; return; fi
  if [ "$a2" -lt "$b2" ]; then echo -1; return; fi
  if [ "$a2" -gt "$b2" ]; then echo 1; return; fi
  if [ "$a3" -lt "$b3" ]; then echo -1; return; fi
  if [ "$a3" -gt "$b3" ]; then echo 1; return; fi
  echo 0
}

# Return AFFECTED, FIXED or empty (unparseable). FIXED here means the version is 1.0.75 or later;
# post-update operator steps are applied in decide_verdict.
base_verdict() {
  local v=$1
  [ -n "$v" ] || { printf ''; return; }
  if [ "$(cmp_ver "$v" 1.0.75)" -lt 0 ]; then
    printf 'AFFECTED'
    return
  fi
  printf 'FIXED'
}

showver() {
  if [ -n "$1" ]; then printf '%s' "$1"; else printf '(not shown)'; fi
}

rank() {
  case $1 in
    2) echo 3;;
    3) echo 2;;
    0) echo 1;;
    *) echo 0;;
  esac
}

NA=0; NF=0; NR=0; N=0; WORST=-1
tally() {
  case $1 in
    AFFECTED) NA=$((NA + 1));;
    FIXED) NF=$((NF + 1));;
    *) NR=$((NR + 1));;
  esac
}

decide_verdict() {
  local ev=$1 lu=$2 ba=$3
  local base
  base=$(base_verdict "$ev")
  if [ -z "$base" ]; then
    printf 'REVIEW'
    return
  fi
  if [ "$base" = AFFECTED ]; then
    printf 'AFFECTED'
    return
  fi
  # Version is 1.0.75 or later. FIXED only when official post-update steps are confirmed.
  if { [ "$lu" = 'done' ] || [ "$lu" = 'not-required' ]; } && [ "$ba" = 'done' ]; then
    printf 'FIXED'
    return
  fi
  printf 'REVIEW'
}

note_row() {
  local ev=$1 lu=$2 ba=$3 verd=$4
  if [ "$verd" = AFFECTED ]; then
    echo 'NOTE: this version is in the affected range in the SolusVM advisory (all versions up to 1.0.74, before 1.0.75). Update the SolusVM 2 WHMCS provisioning module to 1.0.75 or later.'
    return
  fi
  if [ "$verd" = FIXED ]; then
    echo 'NOTE: this version is 1.0.75 or later, the fixed release named in the SolusVM advisory, and the operator confirmed the official post-update steps.'
    return
  fi
  if [ -n "$ev" ] && [ "$(cmp_ver "$ev" 1.0.75)" -ge 0 ]; then
    echo 'NOTE: this version is 1.0.75 or later, but official post-update steps are unconfirmed. If you imported customers from SolusVM 1, run php converter.php link-users --dry-run and then php converter.php link-users in modules/servers/solusvm2vps. Audit every SolusVM 2 user with a billing_user_id (one-to-one mapping, matching email, no administrator role). Pass --link-users done or not-required and --billing-audit done when those steps are complete.'
    return
  fi
  echo 'NOTE: the version string could not be parsed as x.y.z, so the input is hidden and the row is REVIEW.'
}

add_row() {
  local h=$1 shown=$2 lu=$3 ba=$4 ev=$5
  local verd
  verd=$(decide_verdict "$ev" "$lu" "$ba")
  tally "$verd"
  local rc
  case $verd in
    AFFECTED) rc=2;;
    FIXED) rc=0;;
    *) rc=3;;
  esac
  if [ "$WORST" -lt 0 ] || [ "$(rank "$rc")" -gt "$(rank "$WORST")" ]; then WORST=$rc; fi
  if [ "$h" = input ]; then
    echo "VERSION: $shown LINK_USERS: $lu BILLING_AUDIT: $ba $verd"
  else
    echo "VERSION: $h $shown LINK_USERS: $lu BILLING_AUDIT: $ba $verd"
  fi
  note_row "$ev" "$lu" "$ba" "$verd"
  N=$((N + 1))
}

# Validate inputs that open files
if [ "$gf" = 1 ]; then
  if is_denied_file "$VFILE"; then echo 'ERROR: refused to open a WHMCS configuration or secret file'; exit 5; fi
  if [ ! -f "$VFILE" ] || [ ! -r "$VFILE" ]; then echo 'ERROR: version file missing or unreadable'; exit 5; fi
fi
if [ "$gr" = 1 ]; then
  if [ ! -d "$ROOT" ]; then echo 'ERROR: root directory missing or unreadable'; exit 5; fi
  if is_denied_file "$ROOT"; then echo 'ERROR: refused to open a WHMCS configuration or secret file'; exit 5; fi
fi
if [ "$gc" = 1 ]; then
  if is_denied_file "$CSV"; then echo 'ERROR: refused to open a WHMCS configuration or secret file'; exit 5; fi
  if [ ! -f "$CSV" ] || [ ! -r "$CSV" ]; then echo 'ERROR: csv missing or unreadable'; exit 5; fi
fi

LU=$(norm_link "$LU")
BA=$(norm_audit "$BA")

echo 'SolusVM 2 WHMCS CVE-2026-136936 checker (read only; no network, no file writes, no secrets printed)'
echo 'NOTE: no exploitation reported by SolusVM as of 1 Dec 2026; not listed in CISA KEV as of 1 Dec 2026 (catalogue 2026.11.30).'
echo 'NOTE: CISA KEV due dates bind US federal agencies only (BOD 26-04). Patch now regardless.'
echo 'FIXED RELEASE: SolusVM 2 WHMCS provisioning module 1.0.75 or later. After the upgrade, customers imported from SolusVM 1 must run php converter.php link-users (dry-run first) in modules/servers/solusvm2vps, and operators should audit SolusVM 2 users that have a billing_user_id. CVE.org CVSS 4.0 9.2 Critical (PR:N, AT:P).'

if [ "$gc" = 1 ]; then
  head -n1 "$CSV" | tr -d '\r' | sed 's/^\xef\xbb\xbf//' | tr '[:upper:]' '[:lower:]' | grep -qxF 'host,version,link_users,billing_audit' || { echo 'ERROR: CSV header must be: host,version,link_users,billing_audit'; exit 5; }
  n=0
  while IFS= read -r line || [ -n "$line" ]; do
    line=${line%$'\r'}
    [ -z "$line" ] && continue
    case $line in \#*) continue;; esac
    IFS=, read -r h ver lu ba rest <<< "$line"
    h=$(printf '%s' "$h" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
    [ -n "$h" ] || h='host not given'
    ver=$(printf '%s' "$ver" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
    lu=$(printf '%s' "$lu" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
    ba=$(printf '%s' "$ba" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
    [ -n "$lu" ] || lu=unknown
    [ -n "$ba" ] || ba=unknown
    lu=$(norm_link "$lu")
    ba=$(norm_audit "$ba")
    ev=$(extract_ver "$ver")
    add_row "$h" "$(showver "$ev")" "$lu" "$ba" "$ev"
    n=$((n + 1))
  done < <(tail -n +2 "$CSV")
  [ "$n" -gt 0 ] || { echo 'ERROR: CSV has no rows'; exit 5; }
elif [ "$gf" = 1 ] || [ "$gr" = 1 ]; then
  if [ "$gr" = 1 ]; then
    VFILE="$ROOT/modules/servers/solusvm2vps/VERSION"
    if is_denied_file "$VFILE"; then echo 'ERROR: refused to open a WHMCS configuration or secret file'; exit 5; fi
    if [ ! -f "$VFILE" ] || [ ! -r "$VFILE" ]; then
      echo 'ERROR: version file missing or unreadable'
      exit 5
    fi
  fi
  ev=$(parse_version_file "$VFILE")
  ev=$(extract_ver "$ev")
  h=${HOST:-input}
  if [ -z "$ev" ]; then
    add_row "$h" '(not shown)' "$LU" "$BA" ''
  else
    add_row "$h" "$(showver "$ev")" "$LU" "$BA" "$ev"
  fi
else
  ev=$(extract_ver "$VSTR")
  h=${HOST:-input}
  if [ -z "$ev" ]; then
    add_row "$h" '(not shown)' "$LU" "$BA" ''
  else
    add_row "$h" "$(showver "$ev")" "$LU" "$BA" "$ev"
  fi
fi

w=REVIEW
case $WORST in
  0) w=FIXED;;
  2) w=AFFECTED;;
  3) w=REVIEW;;
esac
echo "RESULT: $w ($N checked: $NF FIXED, $NA AFFECTED, $NR REVIEW) (exit $WORST)"
echo 'Patch now regardless.'
exit "$WORST"
