#!/usr/bin/env bash
# Read-only checker for Mitel MiCollab AWV (MISA-2026-0012): CVE-2026-133323, CVE-2026-133303, CVE-2026-133275,
# CVE-2026-133311 and CVE-2026-133324. Affected (Mitel): MiCollab 10.3 (10.3.0.18) and earlier; fix: the patch for
# 10.3 (10.3.0.18) in KB000129195, or a later release.
# It gives a version verdict from the version you give (Mitel documents the version in MSL Server Manager, ServiceLink,
# Installed Application Summary) and the patch status you record, and it lists script and archive files by name and
# date under the AWV folder /usr/awc (documented by Mitel) and any folder you add. It never opens a file, makes no
# network connection and writes nothing. Exit codes: 0 FIXED, 2 AFFECTED, 3 REVIEW, 5 usage or read error. Pack 1.0.0.
# Usage: check-mitel-micollab-awv-cve-2026-133323.sh [--version V] [--patch yes|no|unknown] [--root DIR] [--web-dir DIR] [--since YYYY-MM-DD]
#   --version V     MiCollab release, four parts, for example 10.3.0.18
#   --patch P       whether the KB000129195 patch is applied (yes, no or unknown; the checker cannot see it)
#   --root DIR      / on the MiCollab server, or a folder holding a collected copy (usr/awc/...)
#   --web-dir DIR   an extra folder to audit (absolute, or relative to the current folder)
#   --since D       list files dated on or after this UTC date (default 2026-11-16, the advisory's first issue date)
set -u
export LC_ALL=C TZ=UTC
VSTR=''; PAT=''; ROOT=''; WEB=''; SINCE='2026-11-16'; gv=0; gp=0; gr=0; gw=0; gs=0
usage() { echo "usage: check-mitel-micollab-awv-cve-2026-133323.sh [--version V] [--patch yes|no|unknown] [--root DIR] [--web-dir DIR] [--since YYYY-MM-DD]"; exit 5; }
once() { echo 'ERROR: give each option once'; exit 5; }
esc() { # every byte outside [A-Za-z0-9._/-] is printed as \xHH
  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"
}
while [ $# -gt 0 ]; do
  case $1 in
    --version) [ $# -ge 2 ] || usage; [ "$gv" = 0 ] || once; gv=1; VSTR=$2; shift 2;;
    --patch) [ $# -ge 2 ] || usage; [ "$gp" = 0 ] || once; gp=1; PAT=$2; shift 2;;
    --root) [ $# -ge 2 ] || usage; [ "$gr" = 0 ] || once; gr=1; ROOT=$2; shift 2;;
    --web-dir) [ $# -ge 2 ] || usage; [ "$gw" = 0 ] || once; gw=1; WEB=$2; shift 2;;
    --since) [ $# -ge 2 ] || usage; [ "$gs" = 0 ] || once; gs=1; SINCE=$2; shift 2;;
    -h|--help) usage;;
    *) echo "ERROR: unknown option: $(esc "$1")"; exit 5;;
  esac
done
[ "$gv$gr$gw" != 000 ] || usage
if [ "$gp" = 1 ] && ! [[ $PAT =~ ^(yes|no|unknown)$ ]]; then echo 'ERROR: --patch must be yes, no or unknown'; exit 5; fi
if ! [[ $SINCE =~ ^(20[0-9]{2})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$ ]]; then echo 'ERROR: --since must be YYYY-MM-DD'; exit 5; fi
SY=$((10#${BASH_REMATCH[1]})); SM=$((10#${BASH_REMATCH[2]})); SD=$((10#${BASH_REMATCH[3]}))
if [ "$gr" = 1 ] && { [ -z "$ROOT" ] || [ ! -d "$ROOT" ]; }; then echo 'ERROR: root folder missing'; exit 5; fi
if [ "$gw" = 1 ] && { [ -z "$WEB" ] || [ ! -d "$WEB" ]; }; then echo 'ERROR: web folder missing'; exit 5; fi
if [ "$gr" = 1 ]; then while [ "${#ROOT}" -gt 1 ] && [ "${ROOT%/}" != "$ROOT" ]; do ROOT=${ROOT%/}; done; fi
[ "$ROOT" = / ] && ROOT=''
if [ "$gw" = 1 ]; then while [ "${#WEB}" -gt 1 ] && [ "${WEB%/}" != "$WEB" ]; do WEB=${WEB%/}; done; fi
# days from 1970-01-01 to the --since date (civil calendar), then seconds
y=$SY; [ "$SM" -le 2 ] && y=$((y - 1)); era=$((y / 400)); yoe=$((y - era * 400))
mp=$(( (SM + 9) % 12 )); doy=$(( (153 * mp + 2) / 5 + SD - 1 )); doe=$(( yoe * 365 + yoe / 4 - yoe / 100 + doy ))
SEPOCH=$(( (era * 146097 + doe - 719468) * 86400 ))

VRX='^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{1,3})\.([0-9]{1,4})$'
echo 'Mitel MiCollab AWV MISA-2026-0012 checker (read only; files are never opened, only names and dates are shown)'
echo 'NOTE: listed in CISA KEV on 25 Nov 2026; no exploitation described by Mitel as of 25 Nov 2026.'
echo 'NOTE: CISA KEV due dates (28 Nov 2026; 9 Dec 2026 for CVE-2026-133311) bind US federal agencies only (BOD 26-04). Patch now regardless.'
echo 'FIX (Mitel): 10.3 (10.3.0.18) with the patch in KB000129195, or a later release. Mitigation (Mitel): restrict external access to the AWV component.'
echo 'CVES: CVE-2026-133323, CVE-2026-133303, CVE-2026-133275, CVE-2026-133311, CVE-2026-133324'
VR=''
if [ "$gv" = 1 ]; then
  if [[ $VSTR =~ $VRX ]]; then
    a=$((10#${BASH_REMATCH[1]})); b=$((10#${BASH_REMATCH[2]})); c=$((10#${BASH_REMATCH[3]})); d=$((10#${BASH_REMATCH[4]}))
    k=$(( ((a * 100 + b) * 1000 + c) * 10000 + d )); f=$(( ((10 * 100 + 3) * 1000 + 0) * 10000 + 18 ))
    if [ "$k" -lt "$f" ]; then VR=AFFECTED; why='earlier than 10.3.0.18'
    elif [ "$k" -gt "$f" ]; then VR=REVIEW; why='later than 10.3.0.18; confirm with Mitel that this release includes the security update'
    elif [ "$PAT" = yes ]; then VR=FIXED; why='10.3.0.18 with the KB000129195 patch recorded as applied'
    elif [ "$PAT" = no ]; then VR=AFFECTED; why='10.3.0.18 without the KB000129195 patch'
    else VR=REVIEW; why='10.3.0.18 is affected unless the KB000129195 patch is applied'; fi
    echo "VERSION: $VSTR $VR ($why)"
  else VR=REVIEW; echo 'VERSION: (not shown) REVIEW (give four parts, for example 10.3.0.18)'; fi
fi
if [ "$gp" = 1 ]; then echo "PATCH KB000129195: $PAT (as recorded by you; the checker cannot see it)"; else echo 'PATCH KB000129195: not given'; fi

WR=0
XRX='\.(jsp|jspx|jspf|jsw|jsv|php|php[3-8]|phtml|phar|pl|cgi|py|sh|rb|war|jar|asp|aspx|ashx|cfm)$'
audit() { # label folder
  local label=$1 d=$2 t ep dt rel n=0 r=0 l=0 list='' all
  if [ ! -e "$d" ] && [ ! -L "$d" ]; then echo "  $label: absent"; return; fi
  if [ ! -d "$d" ] || [ ! -r "$d" ] || [ ! -x "$d" ]; then echo "  $label: not a readable folder REVIEW"; WR=$((WR + 1)); return; fi
  all=$(while IFS= read -r -d '' t && IFS= read -r -d '' ep && IFS= read -r -d '' dt && IFS= read -r -d '' rel; do
      lc=${rel,,}; [[ $lc =~ $XRX ]] || continue
      printf '%s\t%s\t%s\t%s\n' "$t" "${ep%.*}" "$dt" "$(esc "$rel")"
    done < <(find -H "$d" -mindepth 1 \( -type f -o -type l \) -printf '%y\0%T@\0%TY-%Tm-%Td\0%P\0' 2>/dev/null) | sort -t "$(printf '\t')" -k4,4)
  while IFS=$'\t' read -r t ep dt rel; do
    [ -n "$t" ] || continue
    n=$((n + 1))
    if [ "$t" = l ]; then l=$((l + 1)); list+="    LINK: $rel"$'\n'
    elif [ "$ep" -ge "$SEPOCH" ]; then r=$((r + 1)); list+="    RECENT: $dt $rel"$'\n'; fi
  done <<< "$all"
  echo "  $label: $n script or archive file(s), $r dated on or after $SINCE, $l link(s)"
  if [ -n "$list" ]; then
    printf '%s' "$list" | head -n 20
    k=$(printf '%s' "$list" | wc -l); [ "$k" -le 20 ] || echo "    ... and $((k - 20)) more"
    WR=$((WR + r + l))
  fi
}
if [ "$gr" = 1 ] || [ "$gw" = 1 ]; then
  echo "WEB FILE AUDIT since $SINCE (OUR PATTERN: script and archive file types by name; files are never opened)"
  [ "$gr" = 1 ] && audit 'AWV FOLDER usr/awc' "$ROOT/usr/awc"
  [ "$gw" = 1 ] && audit 'WEB DIR (given)' "$WEB"
  echo "  WEB ITEMS TO REVIEW: $WR"
fi

echo "SUMMARY: version ${VR:-not given}, web items to review $WR"
if [ "$VR" = AFFECTED ]; then echo 'RESULT: AFFECTED (exit 2)'; exit 2; fi
if [ "$VR" != FIXED ] || [ "$WR" -gt 0 ]; then echo 'RESULT: REVIEW (exit 3)'; exit 3; fi
echo 'RESULT: FIXED (exit 0)'; exit 0
