#!/usr/bin/env bash
# check-asus-acc.sh: offline, read-only checker for the November 2026 ASUS Control Center bulletins
# (CVE-2026-114202, CVE-2026-114204, CVE-2026-114205 in ASUS Control Center Express; CVE-2026-126574 in ASUS Control
# Center Datacenter and Enterprise). Pack 1.0.0 (18 Nov 2026). It opens no sockets, writes no files and runs no
# program except tr, head and wc. It reads only the files you name. It prints product names, version numbers and
# device names from the agent inventory; it never prints other file content.
#
# Usage: check-asus-acc.sh (--product P (--version V | --version-file F) | --installed-file F) [--agents-file F]
#   --product P          express (ASUS Control Center Express), datacenter, enterprise, or acc (edition not known)
#   --version V          the console version, for example 1.7.27 or v1.7.27 (a suffix such as _Hyper-V is ignored)
#   --version-file F     a saved file holding the console version (the first version number in it is used)
#   --installed-file F   a saved installed-programs export (one program per line, name and version on the same
#                        line), for example from Get-Package, the Uninstall registry keys, or a software inventory CSV
#   --agents-file F      a saved agent inventory CSV: device,product,version (a header line is optional)
#
# Exit codes:
#   0 NOT AFFECTED  every console found is at a build ASUS names as fixed, and no ACC agents are listed
#   1           not used (reserved for unexpected runtime errors)
#   2 AFFECTED  at least one console is in a range ASUS lists as affected
#   3 UNKNOWN   no usable version, or a console above the affected range for which ASUS names no fixed build (REVIEW)
#   4 AGENTS TO UPDATE  consoles are fixed, but ACC agents are listed and ASUS names no fixed agent version (REVIEW)
#   5 USAGE OR READ ERROR
set -u
export LC_ALL=C

# Product|affected rule|limit|CVEs. "lt" means affected below the limit; "le" means affected at or below it.
RULES="express|lt|1.7.29|CVE-2026-114202, CVE-2026-114204, CVE-2026-114205
datacenter|le|4.0.0.2|CVE-2026-126574
enterprise|le|3.1.0.8|CVE-2026-126574"
CNA_EXPRESS_LAST="1.7.27"
SRC="https://www.asus.com/security-advisory"
MAX_SMALL=1048576
MAX_LINE=4096
MAX_ROWS=10000
MAX_SHOW=200

usage() {
  echo 'Usage: check-asus-acc.sh (--product P (--version V | --version-file F) | --installed-file F) [--agents-file F]'
  echo 'Offline and read-only. Exit codes: 0 NOT AFFECTED, 2 AFFECTED, 3 UNKNOWN, 4 AGENTS TO UPDATE, 5 USAGE OR READ ERROR (1 is not used).'
}
die() { echo "ERROR: $*"; exit 5; }
clean_name() { local b=${1##*/}; [ -n "$b" ] || b=$1; printf '%s' "$b" | tr -c '[:alnum:]._-' '?' | head -c 80; }
printable() { local s=${1//[^[:print:]]/?}; printf '%s' "${s:0:64}"; }
lower() { printf '%s' "$1" | tr '[:upper:]' '[:lower:]'; }
ver_cmp() {
  local IFS=. i x y; local -a a b; read -ra a <<< "$1"; read -ra b <<< "$2"
  for i in 0 1 2 3; do x=$((10#${a[i]:-0})); y=$((10#${b[i]:-0})); if [ "$x" -lt "$y" ]; then echo -1; return; elif [ "$x" -gt "$y" ]; then echo 1; return; fi; done
  echo 0
}
# ver_tok S: prints the first version number in S (1 to 4 dotted parts, at least 2), without a leading v.
VTOK_RE='(^|[^0-9A-Za-z.])[vV]?([0-9]{1,4}(\.[0-9]{1,6}){1,3})([^0-9.]|\.[^0-9]|\.?$)'
ver_tok() { if [[ "$1" =~ $VTOK_RE ]]; then printf '%s' "${BASH_REMATCH[2]}"; fi; }
read_lines() {
  local f=$1 max=$2 sz l
  { [ -r "$f" ] && [ ! -d "$f" ]; } || die "file not found or unreadable: $(clean_name "$f")"
  sz=$(head -c "$((max + 1))" < "$f" 2>/dev/null | wc -c) || die "file unreadable: $(clean_name "$f")"
  [ "$sz" -le "$max" ] || die "file too large: $(clean_name "$f")"
  LINES=()
  while IFS= read -r l || [ -n "$l" ]; do LINES+=("${l%$'\r'}"); done < <(head -c "$max" < "$f" | tr '\000' '\n')
}

PROD=""; VER=""; VSET=0; VFILE=""; IFILE=""; AFILE=""
need() { [ "$1" -ge 2 ] || die "$2 option needs a value"; }
while [ $# -gt 0 ]; do
  case $1 in
    --product|-Product) need $# product; PROD=$(lower "$2"); shift 2;;
    --version|-Version) need $# version; VER=$2; VSET=1; shift 2;;
    --version-file|-VersionFile) need $# version-file; VFILE=$2; [ -n "$VFILE" ] || die "version-file option needs a value"; shift 2;;
    --installed-file|-InstalledFile) need $# installed-file; IFILE=$2; [ -n "$IFILE" ] || die "installed-file option needs a value"; shift 2;;
    --agents-file|-AgentsFile) need $# agents-file; AFILE=$2; [ -n "$AFILE" ] || die "agents-file option needs a value"; shift 2;;
    -h|--help|-Help) usage; exit 5;;
    *) die "unknown option: $(clean_name "$1")";;
  esac
done
nv=0; [ "$VSET" = 1 ] && nv=$((nv + 1)); [ -n "$VFILE" ] && nv=$((nv + 1))
[ "$nv" -le 1 ] || die "give --version or --version-file, not both"
if [ -n "$IFILE" ]; then
  { [ "$nv" = 0 ] && [ -z "$PROD" ]; } || die "--installed-file cannot be combined with --product, --version or --version-file"
elif [ "$nv" = 1 ] || [ -n "$PROD" ]; then
  [ -n "$PROD" ] || die "--version and --version-file need --product"
  [ "$nv" = 1 ] || die "--product needs --version or --version-file"
  case $PROD in express|datacenter|enterprise|acc) ;; *) die "unknown product (use express, datacenter, enterprise or acc)";; esac
else
  usage; exit 5
fi

pname() { case $1 in express) echo "ASUS Control Center Express";; datacenter) echo "ASUS Control Center Datacenter";; enterprise) echo "ASUS Control Center Enterprise";; *) echo "ASUS Control Center (edition not known)";; esac; }
rule() { local l p; while IFS= read -r l; do p=${l%%|*}; [ "$p" = "$1" ] && { echo "$l"; return; }; done <<< "$RULES"; }

echo "ASUS Control Center checker for CVE-2026-114202, CVE-2026-114204, CVE-2026-114205 and CVE-2026-126574 (pack 1.0.0)"
echo "Offline and read-only. Source: $SRC (ASUS bulletins of 18 Nov 2026) and the cve.org records (ASUS is the CNA)."

# ---- Consoles ----
CP=(); CV=(); CS=()
if [ -n "$IFILE" ]; then
  read_lines "$IFILE" "$MAX_SMALL"
  n=0; AG_INST=0
  for l in "${LINES[@]}"; do
    n=$((n + 1)); [ "$n" -le "$MAX_ROWS" ] || break
    [ "${#l}" -le "$MAX_LINE" ] || continue
    lc=$(lower "$l"); rest=""
    p=""; pat=""
    case $lc in
      *"control center express"*) p=express; pat="control center express";;
      *"control center datacenter"*) p=datacenter; pat="control center datacenter";;
      *"control center enterprise"*) p=enterprise; pat="control center enterprise";;
      *"asus control center"*) p=acc; pat="asus control center";;
    esac
    if [ -n "$p" ]; then pre=${lc%%"$pat"*}; rest=${l:$(( ${#pre} + ${#pat} ))}; fi
    [ -n "$p" ] || continue
    case $lc in *agent*) AG_INST=$((AG_INST + 1)); continue;; esac
    [ "${#CP[@]}" -lt 20 ] || continue
    CP+=("$p"); CV+=("$(ver_tok "$rest")"); CS+=("installed-programs file $(clean_name "$IFILE") line $n")
  done
  [ "${#CP[@]}" -gt 0 ] || echo "NOTE: no ASUS Control Center console found in installed-programs file $(clean_name "$IFILE")"
  [ "$AG_INST" = 0 ] || echo "NOTE: $AG_INST ASUS Control Center agent entr$( [ "$AG_INST" = 1 ] && echo y || echo ies) in the installed-programs file; agents are counted below"
else
  AG_INST=0
  if [ "$VSET" = 1 ]; then v=$(ver_tok "$VER"); s="--version"
  else
    read_lines "$VFILE" "$MAX_SMALL"; v=""; n=0
    for l in "${LINES[@]}"; do n=$((n + 1)); [ "$n" -le 50 ] || break; [ "${#l}" -le "$MAX_LINE" ] || continue; v=$(ver_tok "$l"); [ -z "$v" ] || break; done
    s="version file $(clean_name "$VFILE")"
  fi
  CP+=("$PROD"); CV+=("$v"); CS+=("$s")
fi

worst=0  # 0 fixed, 3 review or unknown, 2 affected
i=0
for p in "${CP[@]+"${CP[@]}"}"; do
  v=${CV[i]}; s=${CS[i]}; i=$((i + 1))
  name=$(pname "$p")
  if [ -z "$v" ]; then
    echo "CONSOLE $i: $name, no usable version (from $s): UNKNOWN. Read the version in the console (Settings, General configuration, Software version) and run again."
    [ "$worst" = 2 ] || worst=3; continue
  fi
  if [ "$p" = acc ]; then
    if [ "$(ver_cmp "$v" 4.0.0.2)" -le 0 ]; then
      echo "CONSOLE $i: $name $v (from $s): AFFECTED by CVE-2026-126574 (ASUS: Datacenter 4.0.0.2 and earlier, Enterprise 3.1.0.8 and earlier; the edition is not known, so the wider range is used). Upgrade to the latest version per ASUS."
      worst=2
    else
      echo "CONSOLE $i: $name $v (from $s): REVIEW. Above the ranges ASUS lists for CVE-2026-126574, but ASUS names no fixed build; confirm the edition and that this is the latest version per ASUS."
      [ "$worst" = 2 ] || worst=3
    fi
    continue
  fi
  IFS='|' read -r _ op lim cves <<< "$(rule "$p")"
  c=$(ver_cmp "$v" "$lim")
  if { [ "$op" = lt ] && [ "$c" -lt 0 ]; } || { [ "$op" = le ] && [ "$c" -le 0 ]; }; then
    if [ "$p" = express ]; then
      echo "CONSOLE $i: $name $v (from $s): AFFECTED by $cves (ASUS: versions prior to v$lim). Install ASUS Control Center Express v$lim or later, then update every client agent with Client Agent Updater."
      if [ "$(ver_cmp "$v" "$CNA_EXPRESS_LAST")" -gt 0 ]; then
        echo "NOTE: the cve.org records list Control Center Express \"through v$CNA_EXPRESS_LAST\", while the ASUS bulletin says \"versions prior to v$lim\". Builds after v$CNA_EXPRESS_LAST and before v$lim (the 1.7.28 builds) sit in that gap, so this one is treated as AFFECTED (conservative, per the bulletin)."
      fi
    else
      echo "CONSOLE $i: $name $v (from $s): AFFECTED by $cves (ASUS: $lim and earlier). Upgrade to the latest version per ASUS (ASUS names no fixed build)."
    fi
    worst=2
  elif [ "$p" = express ]; then
    echo "CONSOLE $i: $name $v (from $s): FIXED (ASUS: install v$lim or later). Then confirm every client agent shows the updated version."
  else
    echo "CONSOLE $i: $name $v (from $s): REVIEW. Above the range ASUS lists ($lim and earlier) for $cves, but ASUS names no fixed build; confirm this is the latest version per ASUS."
    [ "$worst" = 2 ] || worst=3
  fi
done

# ---- Agents ----
AG_REV=$AG_INST; AG_OTHER=0; AG_BAD=0
if [ -n "$AFILE" ]; then
  read_lines "$AFILE" "$MAX_SMALL"
  n=0; shown=0; first=1
  for l in "${LINES[@]}"; do
    n=$((n + 1)); [ "$n" -le "$MAX_ROWS" ] || { echo "NOTE: agent inventory truncated after $MAX_ROWS lines"; break; }
    [ "${#l}" -le "$MAX_LINE" ] || { AG_BAD=$((AG_BAD + 1)); continue; }
    t=${l//[[:space:]]/}; [ -n "$t" ] || continue
    [[ "$t" == '#'* ]] && continue
    IFS=',' read -r d pr ve _ <<< "$l"
    d=${d//\"/}; pr=${pr//\"/}; ve=${ve//\"/}
    d=${d#"${d%%[![:space:]]*}"}; d=${d%"${d##*[![:space:]]}"}
    if [ "$first" = 1 ]; then first=0; [ "$(lower "$d")" = device ] && continue; fi
    lp=$(lower "$pr")
    if [ -z "$d" ] || [ -z "${lp//[[:space:]]/}" ]; then AG_BAD=$((AG_BAD + 1)); continue; fi
    case $lp in
      *express*) k="ASUS Control Center Express agent"; act="UPDATE AND VERIFY (REVIEW): run Client Agent Updater from a console at v1.7.29 or later, then verify the agent shows the updated version (ASUS names no agent version number)";;
      *datacenter*|*enterprise*|*"control center"*) k="ASUS Control Center agent"; act="REVIEW: the ASUS bulletin for CVE-2026-126574 names no agent action; upgrade the console and follow ASUS guidance for agents";;
      *) AG_OTHER=$((AG_OTHER + 1)); continue;;
    esac
    AG_REV=$((AG_REV + 1))
    vt=$(ver_tok "$ve"); [ -n "$vt" ] || vt="version unknown"
    if [ "$shown" -lt "$MAX_SHOW" ]; then echo "AGENT $(printable "$d"): $k $vt: $act"; shown=$((shown + 1)); fi
  done
  [ "$shown" -lt "$MAX_SHOW" ] || echo "NOTE: only the first $MAX_SHOW agent rows are shown"
  echo "AGENTS: $AG_REV ACC agent(s) to review, $AG_OTHER other row(s) ignored, $AG_BAD malformed row(s) skipped (from agent inventory $(clean_name "$AFILE"))"
elif [ "$AG_REV" -gt 0 ]; then
  echo "AGENTS: $AG_REV ACC agent(s) to review (from the installed-programs file)"
fi

if [ "${#CP[@]}" = 0 ]; then worst=3; fi
case $worst in
  2) echo "OVERALL: AFFECTED (exit 2). Patch now: Express to v1.7.29 or later, Datacenter and Enterprise to the latest version per ASUS. Not in CISA KEV; patch regardless."; exit 2;;
  3) echo "OVERALL: UNKNOWN OR REVIEW (exit 3). Confirm the product edition, its version and ASUS's fix status."; exit 3;;
esac
if [ "$AG_REV" -gt 0 ]; then echo "OVERALL: AGENTS TO UPDATE (exit 4). Consoles are fixed; update and verify the listed agents."; exit 4; fi
echo "OVERALL: NOT AFFECTED (exit 0). Consoles are at builds ASUS names as fixed."
exit 0
