#!/usr/bin/env bash
# check-whmcs-2026-11-23.sh: offline, read-only checker for the WHMCS security update of 23 Nov 2026:
# CVE-2026-93700 (email campaigns), CVE-2026-82981 (shopping cart SQL injection) and CVE-2026-82979
# (Client Area SQL injection). Pack 1.0.0.
# It never runs WHMCS, never touches the network, never writes a file and never prints secrets: lines that look like
# secrets or configuration.php settings are skipped unread, and a --version value that is not a version is not echoed.
# Bash and PowerShell (Check-Whmcs20261123.ps1) give the same output and exit code.
#
# Ranges, from the WHMCS advisories and the cve.org records (23 Nov 2026):
#   CVE-2026-93700: 8.0.0 up to 8.13.7, 9.0.0 up to 9.0.9, 9.1.0 up to 9.1.1. Fixed in 8.13.8, 9.0.10, 9.1.2.
#   CVE-2026-82981: 9.0.0 up to 9.0.9, 9.1.0 up to 9.1.1. Fixed in 9.0.10, 9.1.2. 8.x is not affected.
#   CVE-2026-82979: 8.13.0 up to 8.13.7, 9.0.0 up to 9.0.9, 9.1.0 up to 9.1.1. Fixed in 8.13.8, 9.0.10, 9.1.2.
# WHMCS shows the installed version at Configuration > System Health. WHMCS's canonical form is X.Y.Z-release.N
# (for example in its "Database version ... does not match file version ..." error and the WhmcsDetails API).
set -u
export LC_ALL=C

VersionTag='check-whmcs-2026-11-23 1.0.0'
MAX_FILE=1048576

usage() {
  echo 'Usage: scripts/check-whmcs-2026-11-23.sh (bash) or scripts/Check-Whmcs20261123.ps1 (PowerShell 7) --version X.Y.Z[-release.N] | --version-file FILE'
  echo 'Offline and read-only. Does not contact WHMCS, does not use the network, and does not write files.'
  echo 'FILE is a saved copy of the version text (for example from Configuration > System Health); its exact layout is UNVERIFIED.'
  echo 'Lines that look like secrets or configuration.php settings are skipped and never printed.'
  echo ''
  echo 'Exit codes: 0 FIXED, 2 AFFECTED, 3 REVIEW, 5 USAGE OR READ ERROR (1 and 4 are not used).'
}

die() { echo "ERROR: $*"; usage; exit 5; }

trim() {
  local s=$1
  s=${s#"${s%%[![:space:]]*}"}
  s=${s%"${s##*[![:space:]]}"}
  printf '%s' "$s"
}

# Normalises a token (already lower case). Prints "X.Y.Z" for a release, "X.Y.Z-suffix.N" for a pre-release, or nothing.
norm_token() {
  local t=$1 num suf="" a b c
  if [[ $t =~ ^([0-9]+\.[0-9]+\.[0-9]+)(-([a-z]+)\.([0-9]+))?$ ]]; then
    num=${BASH_REMATCH[1]}
    if [ -n "${BASH_REMATCH[2]}" ] && [ "${BASH_REMATCH[3]}" != "release" ]; then suf=${BASH_REMATCH[2]}; fi
  else
    return 0
  fi
  IFS=. read -r a b c <<< "$num"
  if [ ${#a} -gt 3 ] || [ ${#b} -gt 3 ] || [ ${#c} -gt 3 ]; then return 0; fi
  printf '%s.%s.%s%s' "$((10#$a))" "$((10#$b))" "$((10#$c))" "$suf"
}

SECRET_RE='(db_password|db_username|db_host|db_name|cc_encryption_hash|api_access_key|api_identifier|api_secret|license|password|passwd|secret|token|private|^[[:space:]]*\$[a-z_]+[[:space:]]*=)'
OTHER_RE='(^|[^a-z])(php|mysql|mariadb|curl|openssl|ioncube|apache|nginx|tls|smarty)([^a-z]|$)'
TOKEN_RE='([0-9]+(\.[0-9]+)+(-[a-z]+\.[0-9]+)?)'

Found=""
Skipped=0
scan_line() {
  local l=$1 rest tok n
  if [[ $l =~ $SECRET_RE ]]; then Skipped=$((Skipped + 1)); return 0; fi
  if [[ $l =~ [0-9]+\.[0-9]+\.[0-9]+-[a-z]+\.[0-9]+ ]] || { [[ $l == *version* ]] && ! [[ $l =~ $OTHER_RE ]]; }; then
    rest=$l
    while [[ $rest =~ $TOKEN_RE ]]; do
      tok=${BASH_REMATCH[1]}
      rest=${rest#*"$tok"}
      n=$(norm_token "$tok")
      if [ -n "$n" ]; then
        case " $Found " in *" $n "*) ;; *) Found="${Found:+$Found }$n" ;; esac
      fi
    done
  fi
}

VersionOpt=""
HaveVersion=0
VersionFile=""
HaveFile=0
ShowHelp=0
while [ $# -gt 0 ]; do
  case "$1" in
    --version)
      if [ $# -lt 2 ] || [ -z "$(trim "$2")" ]; then die "version option needs a value"; fi
      VersionOpt=$(trim "$2"); HaveVersion=1; shift 2 ;;
    --version-file)
      if [ $# -lt 2 ] || [ -z "$(trim "$2")" ]; then die "version-file option needs a value"; fi
      VersionFile=$2; HaveFile=1; shift 2 ;;
    --help|-h)
      ShowHelp=1; shift ;;
    *)
      die "unknown argument (not echoed)" ;;
  esac
done

if [ "$ShowHelp" -eq 1 ]; then usage; exit 5; fi
if [ "$HaveVersion" -eq 0 ] && [ "$HaveFile" -eq 0 ]; then usage; exit 5; fi
if [ "$HaveVersion" -eq 1 ] && [ "$HaveFile" -eq 1 ]; then die "use --version or --version-file, not both"; fi

Parsed=""
Detail=""
if [ "$HaveVersion" -eq 1 ]; then
  lv=$(printf '%s' "$VersionOpt" | tr '[:upper:]' '[:lower:]')
  n=""
  if [[ $lv =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[a-z]+\.[0-9]+)?$ ]]; then n=$(norm_token "$lv"); fi
  if [ -n "$n" ]; then
    Input="--version $VersionOpt"
    Parsed=$n
  else
    Input='--version (value not echoed: not a WHMCS version string)'
    Detail='the --version value is not X.Y.Z or X.Y.Z-release.N (for example 9.1.1 or 8.13.7-release.1)'
  fi
else
  if [ ! -f "$VersionFile" ] || [ ! -r "$VersionFile" ]; then die "cannot read --version-file $VersionFile"; fi
  sz=$(wc -c < "$VersionFile")
  if [ "$sz" -gt "$MAX_FILE" ]; then die "file too large: $VersionFile"; fi
  Input="--version-file $VersionFile"
  while IFS= read -r line || [ -n "$line" ]; do
    scan_line "$(printf '%s' "$line" | tr '[:upper:]' '[:lower:]')"
  done < <(tr '\000\r' '\n' < "$VersionFile")
  set -f
  # shellcheck disable=SC2086  # word split on purpose
  set -- $Found
  set +f
  if [ $# -eq 0 ]; then
    Detail='no WHMCS version was found in the file (the saved page layout is UNVERIFIED)'
  elif [ $# -gt 1 ]; then
    Detail="the file holds more than one different version ($(printf '%s' "$Found" | sed 's/ /, /g')); check one installation at a time"
  else
    Parsed=$1
  fi
fi

S1=REVIEW; S2=REVIEW; S3=REVIEW
Branch="unknown"
Pre=0
if [ -n "$Parsed" ]; then
  num=${Parsed%%-*}
  [ "$num" = "$Parsed" ] || Pre=1
  IFS=. read -r MA MI PA <<< "$num"
  v=$((MA * 1000000 + MI * 1000 + PA))
  case "$MA.$MI" in
    9.1) Branch='9.1 (General Availability; WHMCS anticipated End of Life 2027-09-30)' ;;
    9.0) Branch='9.0 (Long-Term Support; WHMCS anticipated End of Life 2026-12-31)' ;;
    8.13) Branch='8.13 (Long-Term Support; WHMCS anticipated End of Life 2027-01-31)' ;;
    *)
      if [ "$v" -lt 8013000 ]; then Branch="$MA.$MI (End of Life: WHMCS no longer releases updates for it)"
      else Branch="$MA.$MI (not in WHMCS's supported list of 23 Nov 2026; check the advisories)"; fi ;;
  esac
  known=1
  if [ "$v" -ge 8014000 ] && [ "$v" -lt 9000000 ]; then known=0; fi
  if [ "$v" -ge 9002000 ]; then known=0; fi
  if [ "$Pre" -eq 1 ]; then
    Detail='pre-release build; the WHMCS advisories list release builds only'
  elif [ "$known" -eq 0 ]; then
    Detail='branch not covered by the 23 Nov 2026 advisories'
  else
    # CVE-2026-93700
    if [ "$v" -lt 8000000 ]; then S1='NOT APPLICABLE'
    elif [ "$v" -lt 8013008 ]; then S1=AFFECTED
    elif [ "$v" -lt 9000000 ]; then S1=FIXED
    elif [ "$v" -lt 9000010 ]; then S1=AFFECTED
    elif [ "$v" -lt 9001000 ]; then S1=FIXED
    elif [ "$v" -lt 9001002 ]; then S1=AFFECTED
    else S1=FIXED; fi
    # CVE-2026-82981
    if [ "$v" -lt 9000000 ]; then S2='NOT APPLICABLE'
    elif [ "$v" -lt 9000010 ]; then S2=AFFECTED
    elif [ "$v" -lt 9001000 ]; then S2=FIXED
    elif [ "$v" -lt 9001002 ]; then S2=AFFECTED
    else S2=FIXED; fi
    # CVE-2026-82979
    if [ "$v" -lt 8013000 ]; then S3='NOT APPLICABLE'
    elif [ "$v" -lt 8013008 ]; then S3=AFFECTED
    elif [ "$v" -lt 9000000 ]; then S3=FIXED
    elif [ "$v" -lt 9000010 ]; then S3=AFFECTED
    elif [ "$v" -lt 9001000 ]; then S3=FIXED
    elif [ "$v" -lt 9001002 ]; then S3=AFFECTED
    else S3=FIXED; fi
  fi
fi

Overall=REVIEW
if [ "$S1" = AFFECTED ] || [ "$S2" = AFFECTED ] || [ "$S3" = AFFECTED ]; then Overall=AFFECTED
elif [ "$S1" = REVIEW ] || [ "$S2" = REVIEW ] || [ "$S3" = REVIEW ]; then Overall=REVIEW
elif [ "$S1" = FIXED ] || [ "$S2" = FIXED ] || [ "$S3" = FIXED ]; then Overall=FIXED
else Overall=REVIEW; Detail='WHMCS lists none of the three CVEs before 8.0.0, but this version is End of Life and gets no security fixes; upgrade to a supported version'
fi

echo "$VersionTag"
echo 'WHMCS security update 2026-11-23 checker (read only): CVE-2026-93700, CVE-2026-82981, CVE-2026-82979'
echo "INPUT: $Input"
echo "PARSED version: ${Parsed:-unreadable}"
echo "BRANCH: $Branch"
if [ "$Skipped" -gt 0 ]; then echo "NOTE: $Skipped line(s) that look like secrets or configuration.php settings were skipped and not printed."; fi
if [ -n "$Detail" ]; then echo "VERSION STATUS: REVIEW  $Detail"; else echo 'VERSION STATUS: OK'; fi
echo "CVE CVE-2026-93700 $S1  CVSS 3.1 9.0 Critical (CNA)  email campaigns, client-supplied data (CWE-1336): an authenticated client could read configuration files including database credentials and execute code. Fixed in 9.1.2, 9.0.10, 8.13.8."
echo "CVE CVE-2026-82981 $S2  CVSS 4.0 8.7 High (CNA)  shopping cart SQL injection (CWE-89): an unauthenticated user could read client data and admin records including password hashes. Fixed in 9.1.2, 9.0.10; 8.x is not affected."
echo "CVE CVE-2026-82979 $S3  CVSS 4.0 7.1 High (CNA)  Client Area SQL injection (CWE-89): a client with the View & Manage Contacts permission could read other clients' data and admin records including password hashes. Fixed in 9.1.2, 9.0.10, 8.13.8."
if [ "$S1" = AFFECTED ]; then
  echo 'INTERIM (WHMCS, CVE-2026-93700): at Utilities > Email Campaigns, choose Delete from the Manage Campaign menu under Actions for each existing campaign. Then at Utilities > System > System Cleanup, click Go under Empty Template Cache. Update to 9.1.2, 9.0.10 or 8.13.8 as soon as you can.'
  if [ -n "$Parsed" ] && [ "$v" -lt 8013000 ]; then
    echo 'NOTE: this branch is End of Life. WHMCS: "A fix is only available for supported WHMCS versions." Upgrade to 8.13.8, 9.0.10 or 9.1.2.'
  fi
fi
echo 'NOTE: no exploitation reported by WHMCS, NVD or CISA as of 23 Nov 2026; none of the three CVEs is in CISA KEV (catalogue 2026.11.23). Patch now regardless.'
echo 'NOTE: WHMCS Cloud updates WHMCS for you automatically (WHMCS docs); self-hosted installations must update themselves.'
echo 'SOURCE: https://help.whmcs.com/m/125386/l/2151351-cve-2026-93700-whmcs-security-update-2026-11-23'
echo 'SOURCE: https://help.whmcs.com/m/125386/l/2151347-cve-2026-82981-whmcs-security-update-2026-11-23'
echo 'SOURCE: https://help.whmcs.com/m/125386/l/2151340-cve-2026-82979-whmcs-security-update-2026-11-23'
case "$Overall" in
  FIXED) echo 'OVERALL: FIXED (exit 0)'; exit 0 ;;
  AFFECTED) echo 'OVERALL: AFFECTED (exit 2)'; exit 2 ;;
  *) echo 'OVERALL: REVIEW (exit 3)'; exit 3 ;;
esac
