#!/usr/bin/env bash
# Read-only checker for Juniper Session Smart Conductor CVE-2026-103406 (JSA112183): unauthenticated OS command
# injection in the AutomatedProvisioner through a crafted Salt minion ID (CVSS 3.1 9.8 Critical, Juniper).
# Runs on the Conductor (--root /) or against a collected copy of its files (--root DIR). It gives a version verdict and
# audits Salt minion key IDs. It reads directory listings, the Salt master config and /srv/salt/top.sls only; it never
# opens a key file, and no key material, file contents or passwords are printed. It 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-juniper-ssr-conductor-cve-2026-103406.sh [--version V] [--version-file FILE] [--root DIR]
#   --version V        SSR software version, for example 7.1.6-7-sts or 7.2.3-8r2
#   --version-file F   saved output of the PCLI command "show system version" or "show system version detail"
#   --root DIR         / on the Conductor, or the folder holding a collected copy (etc/128technology/..., srv/salt/...)
set -u
export LC_ALL=C
VSTR=''; VFILE=''; ROOT=''; gv=0; gf=0; gr=0
usage() { echo "usage: check-juniper-ssr-conductor-cve-2026-103406.sh [--version V] [--version-file FILE] [--root DIR]"; 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 ] || { 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;;
    -h|--help) usage;;
    *) echo "ERROR: unknown option: $(esc "$1")"; exit 5;;
  esac
done
[ "$gv$gf$gr" != 000 ] || usage
if [ "$gf" = 1 ] && { [ ! -f "$VFILE" ] || [ ! -r "$VFILE" ]; }; then echo 'ERROR: version file missing or unreadable'; exit 5; fi
if [ "$gr" = 1 ] && { [ -z "$ROOT" ] || [ ! -d "$ROOT" ]; }; then echo 'ERROR: root folder missing'; exit 5; fi
if [ "$gr" = 1 ]; then while [ "${#ROOT}" -gt 1 ] && [ "${ROOT%/}" != "$ROOT" ]; do ROOT=${ROOT%/}; done; fi
[ "$ROOT" = / ] && ROOT=''

VRX='^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{1,3})(-[A-Za-z0-9]{1,12}){0,2}(\.[A-Za-z0-9_]{1,12}){0,2}$'
showver() { if [[ $1 =~ $VRX ]]; then printf '%s' "$1"; else printf '(not shown)'; fi; }
verdict() { # AFFECTED below 7.0.5, 7.1.0 to 7.1.6 and 7.2.0 to 7.2.2; FIXED 7.0.5+, 7.1.7+, 7.2.3+; REVIEW otherwise
  if ! [[ $1 =~ $VRX ]]; then echo REVIEW; return; fi
  local a=$((10#${BASH_REMATCH[1]})) b=$((10#${BASH_REMATCH[2]})) c=$((10#${BASH_REMATCH[3]}))
  if [ "$a" -lt 7 ]; then echo AFFECTED; return; fi
  if [ "$a" -gt 7 ] || [ "$b" -gt 2 ]; then echo REVIEW; return; fi
  case $b in
    0) if [ "$c" -lt 5 ]; then echo AFFECTED; else echo FIXED; fi;;
    1) if [ "$c" -lt 7 ]; then echo AFFECTED; else echo FIXED; fi;;
    2) if [ "$c" -lt 3 ]; then echo AFFECTED; else echo FIXED; fi;;
  esac
}
NA=0; NF=0; NR=0
tally() { case $1 in AFFECTED) NA=$((NA + 1));; FIXED) NF=$((NF + 1));; *) NR=$((NR + 1));; esac; }

echo 'Juniper Session Smart Conductor CVE-2026-103406 checker (read only; no key material, file contents or passwords are printed)'
echo 'NOTE: exploitation reported to Juniper SIRT (Juniper) and listed in CISA KEV on 25 Nov 2026.'
echo 'NOTE: the CISA KEV due date (28 Nov 2026) binds US federal agencies only (BOD 26-04). Patch now regardless.'
echo 'FIXED RELEASES: 7.0.5, 7.1.7 and 7.2.3 and all subsequent releases (JSA112183). No workarounds (Juniper).'
echo "NOTE: Juniper's CVE record lists Session Smart Conductor only; CISA KEV names Session Smart Conductor and Session Smart Router."

if [ "$gv" = 1 ]; then r=$(verdict "$VSTR"); tally "$r"; echo "VERSION OPTION: $(showver "$VSTR") $r"; fi
if [ "$gf" = 1 ]; then
  n=0; out=''
  while IFS= read -r -d $'\n' l || [ -n "$l" ]; do
    l=${l%$'\r'}
    if [[ $l =~ ^[[:space:]]*Version:[[:space:]]+([^[:space:]]+)[[:space:]]*$ ]]; then k=VERSION; v=${BASH_REMATCH[1]}
    elif [[ $l =~ ^[[:space:]]*Package:[[:space:]]+128T-([^[:space:]]+)[[:space:]]*$ ]]; then k=PACKAGE; v=${BASH_REMATCH[1]}
    else continue; fi
    n=$((n + 1)); r=$(verdict "$v"); tally "$r"
    [ "$n" -le 20 ] && out+="  $k LINE: $(showver "$v") $r"$'\n'
  done < <(tr '\000' '\n' < "$VFILE")
  echo "VERSION FILE: $n Version or Package line(s)"
  printf '%s' "$out"
  [ "$n" -le 20 ] || echo "  ... and $((n - 20)) more"
  if [ "$n" = 0 ]; then NR=$((NR + 1)); echo '  VERSION FILE: no "Version:" or "Package: 128T-" line REVIEW'; fi
fi

SR=0
listdir() { # prints the entries of one key folder as "type<TAB>escaped name", sorted
  local d=$1 t name
  while IFS= read -r -d '' t && IFS= read -r -d '' name; do
    printf '%s\t%s\n' "$t" "$(esc "$name")"
  done < <(find -H "$d" -mindepth 1 -maxdepth 1 -printf '%y\0%f\0' 2>/dev/null) | sort
}
keydir() { # label path
  local label=$1 d=$2 all ne=0 nu=0 nn=0 t e list=''
  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"; SR=$((SR + 1)); return; fi
  all=$(listdir "$d")
  while IFS=$'\t' read -r t e; do
    [ -n "$t" ] || continue
    ne=$((ne + 1))
    if [ "$t" != f ]; then nn=$((nn + 1)); list+="    NOT A REGULAR FILE: $e"$'\n'
    elif ! [[ $e =~ ^[A-Za-z0-9_][A-Za-z0-9._-]{0,127}$ ]]; then nu=$((nu + 1)); list+="    UNSAFE ID: $e"$'\n'
    fi
  done <<< "$all"
  echo "  $label: $ne entr(ies), $nu unsafe ID(s), $nn not regular file(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"
    SR=$((SR + 1))
  fi
}
yn() { # value of one top-level setting in a Salt master config: true, false, not set or other
  local v
  v=$(grep -aE "^$1:" "$2" 2>/dev/null | head -n 1 | tr -d '\000')
  v=${v#*:}; v=${v//[[:space:]]/}
  if ! grep -aqE "^$1:" "$2" 2>/dev/null; then echo 'not set'
  elif [[ $v =~ ^(True|true|yes|Yes|TRUE)$ ]]; then echo true
  elif [[ $v =~ ^(False|false|no|No|FALSE)$ ]]; then echo false
  else echo other; fi
}
if [ "$gr" = 1 ]; then
  echo 'SALT KEY AUDIT (IDs are file names; every byte outside A-Z a-z 0-9 . _ - / is shown as \xHH; key files are never opened)'
  for s in minions minions_pre minions_rejected minions_denied minion_autosign; do
    keydir "PKI etc/128technology/salt/pki/master/$s" "$ROOT/etc/128technology/salt/pki/master/$s"
  done
  for s in minions minions_pre minions_rejected minions_denied minion_autosign; do
    keydir "SALT DEFAULT etc/salt/pki/master/$s" "$ROOT/etc/salt/pki/master/$s"
  done
  M="$ROOT/etc/128technology/salt/master"
  if [ -f "$M" ] && [ -r "$M" ]; then
    aa=$(yn auto_accept "$M"); om=$(yn open_mode "$M")
    if grep -aqE '^autosign_file:[[:space:]]*[^[:space:]]' "$M" 2>/dev/null; then af='set'; else af='not set'; fi
    if grep -aqE '^autosign_grains_dir:[[:space:]]*[^[:space:]]' "$M" 2>/dev/null; then ag='set'; else ag='not set'; fi
    echo "  MASTER CONFIG etc/128technology/salt/master: auto_accept $aa; open_mode $om; autosign_file $af; autosign_grains_dir $ag"
    if [ "$aa" != false ] && [ "$aa" != 'not set' ]; then SR=$((SR + 1)); fi
    if [ "$om" != false ] && [ "$om" != 'not set' ]; then SR=$((SR + 1)); fi
    if [ "$af" = set ] || [ "$ag" = set ]; then SR=$((SR + 1)); fi
  elif [ -e "$M" ]; then echo '  MASTER CONFIG etc/128technology/salt/master: unreadable REVIEW'; SR=$((SR + 1))
  else echo '  MASTER CONFIG etc/128technology/salt/master: absent'; fi
  TS="$ROOT/srv/salt/top.sls"
  if [ -f "$TS" ] && [ -r "$TS" ]; then
    w=$(tr '\000' ' ' < "$TS" | grep -av '^[[:space:]]*#' | tr -s '[:space:]' ' '); w=${w# }; w=${w% }
    if [ "$w" = "base: '*': - dummy" ]; then echo '  SRV SALT top.sls: documented default yes'
    else echo '  SRV SALT top.sls: documented default no REVIEW'; SR=$((SR + 1)); fi
  elif [ -e "$TS" ]; then echo '  SRV SALT top.sls: unreadable REVIEW'; SR=$((SR + 1))
  else echo '  SRV SALT top.sls: absent'; fi
  if [ -d "$ROOT/srv/salt" ]; then
    other=$(while IFS= read -r -d '' f; do
        rel=${f#"$ROOT/srv/salt/"}
        [ "$rel" = top.sls ] || [ "$rel" = dummy.sls ] || { esc "$rel"; printf '\n'; }
      done < <(find -H "$ROOT/srv/salt" -mindepth 1 \( -type f -o -type l \) -print0 2>/dev/null) | sort)
    no=0; [ -z "$other" ] || no=$(printf '%s\n' "$other" | wc -l)
    echo "  SRV SALT OTHER STATE FILES: $no"
    if [ "$no" -gt 0 ]; then
      printf '%s\n' "$other" | head -n 20 | while IFS= read -r x; do echo "    $x"; done
      [ "$no" -le 20 ] || echo "    ... and $((no - 20)) more"
      SR=$((SR + 1))
    fi
  else echo '  SRV SALT OTHER STATE FILES: 0 (no srv/salt folder)'; fi
  echo "  SALT ITEMS TO REVIEW: $SR"
fi

echo "SUMMARY: versions affected $NA, fixed $NF, review $NR"
if [ "$NA" -gt 0 ]; then echo 'RESULT: AFFECTED (exit 2)'; exit 2; fi
if [ "$NR" -gt 0 ] || [ "$SR" -gt 0 ] || [ "$NF" = 0 ]; then echo 'RESULT: REVIEW (exit 3)'; exit 3; fi
echo 'RESULT: FIXED (exit 0)'; exit 0
