#!/usr/bin/env bash
# check-mail-assure-cve-2026-134624.sh: offline, read-only checker for N-able MailAssure / SpamExperts
# CVE-2026-134624 (SSRF in the relay LDAP synchronisation connection check). Pack 1.0.0.
# It never runs the product, never touches the network, never writes a file and never prints secrets.
# Bash and PowerShell (Check-MailAssureCve2026134624.ps1) give the same output and exit code.
#
# N-able (CNA) and NVD: affected before build 20261121.1943; fixed in 20261121.1943.
# The exact admin-page or API field that shows the build is [UNVERIFIED]; this checker reads a build
# string or extracts the first YYYYMMDD.HHMM token from a saved copy of whatever you were given.
set -u
export LC_ALL=C

VersionTag='check-mail-assure-cve-2026-134624 1.0.0'
FIX='20261121.1943'
SOURCE='https://me.n-able.com/s/security-advisory/aArVy0000002mrlKAA'
MAX_FILE=1048576

usage() {
  echo 'Usage: check-mail-assure-cve-2026-134624.sh [--build X] [--version-file FILE] [--deployment hosted|local-cloud|unknown]'
  echo 'Offline and read-only. Does not call N-able, does not use the network, and does not write files.'
  echo 'The exact page or API field that shows the build is [UNVERIFIED]; pass the build string or a saved copy.'
  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"
}

# Prints YYYYMMDD.HHMM or nothing. HHMM is 3 or 4 digits, as in N-able's 20261121.1943 form.
extract_builds() {
  printf '%s' "$1" | grep -oE '[0-9]{8}\.[0-9]{3,4}' || true
}

cmp_build() {
  # args: A B; prints -1, 0 or 1
  local a=$1 b=$2
  local ad=${a%%.*} at=${a#*.}
  local bd=${b%%.*} bt=${b#*.}
  if [ "$ad" -lt "$bd" ]; then echo -1; return; fi
  if [ "$ad" -gt "$bd" ]; then echo 1; return; fi
  if [ "$at" -lt "$bt" ]; then echo -1; return; fi
  if [ "$at" -gt "$bt" ]; then echo 1; return; fi
  echo 0
}

BuildOpt=""
VersionFile=""
Deployment="unknown"
ShowHelp=0

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

if [ "$ShowHelp" -eq 1 ]; then usage; exit 5; fi
if [ -z "$BuildOpt" ] && [ -z "$VersionFile" ]; then usage; exit 5; fi

case "$Deployment" in
  hosted|local-cloud|unknown) ;;
  *) die "deployment must be hosted, local-cloud or unknown" ;;
esac

FileBuilds=""
if [ -n "$VersionFile" ]; then
  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
  body=$(tr '\000' '\n' < "$VersionFile" | tr -d '\r')
  FileBuilds=$(extract_builds "$body")
fi

# Unique builds from file
FileUnique=""
if [ -n "$FileBuilds" ]; then
  FileUnique=$(printf '%s\n' "$FileBuilds" | awk 'NF && !seen[$0]++')
fi
nfile=0
if [ -n "$FileUnique" ]; then
  nfile=$(printf '%s\n' "$FileUnique" | grep -c . || true)
fi

Parsed=""
Status="REVIEW"
Detail=""
InputBuild="not given"

if [ -n "$BuildOpt" ]; then
  optb=$(extract_builds "$BuildOpt" | head -n1)
  if [ -z "$optb" ] || [ "$(extract_builds "$BuildOpt" | awk 'NF && !seen[$0]++' | grep -c .)" -ne 1 ]; then
    Parsed="unreadable"
    Detail="the --build value is not a single YYYYMMDD.HHMM token (N-able form, for example 20261121.1943)"
    InputBuild="$BuildOpt (from --build)"
  else
    Parsed=$optb
    InputBuild="$optb (from --build)"
  fi
fi

if [ -n "$VersionFile" ]; then
  if [ "$nfile" -eq 0 ]; then
    if [ -z "$Parsed" ]; then
      Parsed="unreadable"
      Detail="no YYYYMMDD.HHMM build token was found in the file (the exact page or API layout is UNVERIFIED)"
    fi
  elif [ "$nfile" -gt 1 ]; then
    Parsed="unreadable"
    Detail="the file holds more than one different build token; check one instance at a time"
  else
    fb=$(printf '%s\n' "$FileUnique" | head -n1)
    if [ -n "$Parsed" ] && [ "$Parsed" != "unreadable" ] && [ "$Parsed" != "$fb" ]; then
      Parsed="unreadable"
      Detail="--build and --version-file hold different builds"
      InputBuild="$BuildOpt (from --build) and $fb (from --version-file $VersionFile)"
    elif [ -z "$Parsed" ] || [ "$Parsed" = "unreadable" ] && [ -z "$BuildOpt" ]; then
      Parsed=$fb
      InputBuild="$fb (from --version-file $VersionFile)"
    elif [ -n "$BuildOpt" ] && [ "$Parsed" = "$fb" ]; then
      InputBuild="$fb (from --version-file and --build)"
    fi
  fi
  if [ -z "$BuildOpt" ] && [ "$nfile" -eq 1 ] && [ "$Parsed" != "unreadable" ]; then
    InputBuild="$Parsed (from --version-file $VersionFile)"
  fi
fi

if [ "$Parsed" != "unreadable" ] && [ -n "$Parsed" ]; then
  c=$(cmp_build "$Parsed" "$FIX")
  if [ "$c" -lt 0 ]; then Status=AFFECTED
  else Status=FIXED
  fi
  Detail=""
else
  Status=REVIEW
  [ -n "$Parsed" ] || Parsed="unreadable"
  [ -n "$Detail" ] || Detail="no YYYYMMDD.HHMM build token could be read"
fi

echo "$VersionTag"
echo 'N-able MailAssure / SpamExperts CVE-2026-134624 checker (read only)'
echo "INPUT build: $InputBuild"
echo "PARSED build: $Parsed"
echo "DEPLOYMENT: $Deployment"
if [ -n "$Detail" ]; then
  echo "VERSION STATUS: $Status  $Detail"
else
  echo "VERSION STATUS: $Status"
fi
echo "CVE CVE-2026-134624 $Status  CVSS 4.0 9.3 Critical  Server-Side Request Forgery in relay LDAP synchronisation connection check"
echo "FIXED BUILD: $FIX"
echo 'NOTE: N-able says the Mail Assure and SpamExperts Hosted Cloud service was remediated, and dashboard and API access was restored at 23:45 UTC on 21 Nov 2026. Confirm the live build anyway.'
echo 'NOTE: Local Cloud and any self-hosted operators must check this build themselves. N-able documents weekly Local Cloud software updates (Tuesday around 12:30 CET, scheduled under Server > Settings). The fixed build is dated 21 Nov 2026 (a Saturday), so do not assume the weekly cycle has already applied it.'
echo 'NOTE: N-able, in its 22 Nov 2026 incident update, reports no evidence of exploitation beyond the single N-able-hosted server identified in the initial investigation. CISA added CVE-2026-134624 to the KEV catalogue on 23 Nov 2026 (catalogue 2026.11.23, released 19:43 UTC). The KEV due date is 26 Nov 2026 and applies to US federal agencies only (BOD 26-04). CISA lists known ransomware campaign use as Unknown. Patch now regardless.'
echo "SOURCE: $SOURCE"
case "$Status" 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
