#!/usr/bin/env bash
# check-roller.sh: offline, read-only checker for Apache Roller XML-RPC deserialization, CVE-2026-129448 (Roller
# 4.0.0 through 6.1.4) and CVE-2026-82384 (Roller 6.1.5). Pack 1.0.0 (17 Nov 2026). It opens no sockets, writes no
# files and runs no other program except grep, tr and wc. It reads only two files under the webapp folder you name:
# WEB-INF/web.xml and WEB-INF/classes/roller-version.properties (the ro.version line only). It never prints file
# content beyond the version number, the XML-RPC servlet and filter names and their URL patterns.
#
# Usage: check-roller.sh --webapp PATH [--version X.Y.Z]   (-Webapp and -Version are accepted too)
#   --webapp PATH     the unpacked Roller webapp folder (the one that holds WEB-INF/web.xml), for example a folder
#                     under Tomcat's webapps. A .war file must be unpacked first (see the README).
#   --version X.Y.Z   the Roller version, used only when roller-version.properties is missing or unreadable.
#
# Exit codes:
#   0 FIXED                     6.1.6 or later, and the XML-RPC extension types are not switched on
#   1 VULNERABLE                4.0.0 to 6.1.5, enabledForExtensions on and the XML-RPC servlet mapped
#   2 MITIGATED, UPGRADE NEEDED 4.0.0 to 6.1.5, with Apache's workaround in place (extensions off or servlet unmapped)
#   3 REVIEW                    6.1.6 or later, but web.xml still switches the extension types on with the servlet mapped
#   4 UNKNOWN                   version not found, not a plain number, or below 4.0.0 (not stated by Apache)
#   5 USAGE OR READ ERROR
set -u
export LC_ALL=C

FIRST_AFFECTED="4.0.0"   # CVE-2026-129448: 4.0.0 before 6.1.5 (Apache CNA record)
ONLY_82384="6.1.5"       # CVE-2026-82384: 6.1.5 (Apache CNA record)
FIXED="6.1.6"            # Apache: upgrade to 6.1.6 or later
XMLRPC_CLASS="org.apache.xmlrpc.webserver.XmlRpcServlet"
XMLRPC_PATH="/roller-services/xmlrpc"
GATE_FILTER="XmlRpcEnabledFilter"   # added in 6.1.6 by the Apache fix (pull request 171)
MAX_XML=2097152
MAX_PROPS=65536

usage() {
  echo 'Usage: check-roller.sh --webapp PATH [--version X.Y.Z], or Check-Roller.ps1 -Webapp PATH [-Version X.Y.Z]'
  echo 'Offline and read-only. Exit codes: 0 FIXED, 1 VULNERABLE, 2 MITIGATED, UPGRADE NEEDED, 3 REVIEW, 4 UNKNOWN, 5 USAGE OR READ ERROR.'
}
die() { echo "ERROR: $*"; exit 5; }
clean_path() { printf '%s' "$1" | tr -c '[:print:]' '?'; }
show() { if [[ "$1" =~ ^[A-Za-z0-9._/*-]{1,80}$ ]]; then printf '%s' "$1"; else printf '(value withheld: unexpected characters)'; fi; }
trim() { local s=$1; s="${s#"${s%%[![:space:]]*}"}"; s="${s%"${s##*[![:space:]]}"}"; printf '%s' "$s"; }
# ver_cmp A B: prints -1, 0 or 1 (numeric, missing parts are 0).
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; 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_RE='^[0-9]{1,4}(\.[0-9]{1,4}){0,2}$'

WEBAPP=""; OPT_VER=""; HAVE_WEBAPP=0
while [ $# -gt 0 ]; do
  case $1 in
    --webapp|-Webapp) [ $# -ge 2 ] || die "webapp option needs a path"; WEBAPP=$2; HAVE_WEBAPP=1; shift 2;;
    --version|-Version) [ $# -ge 2 ] || die "version option needs a value"
      [[ "$2" =~ $VER_RE ]] || die "version must be numbers and dots, for example 6.1.5"
      OPT_VER=$2; shift 2;;
    -h|--help|-Help) usage; exit 5;;
    *) die "unknown option: $(clean_path "$1")";;
  esac
done
[ "$HAVE_WEBAPP" = 1 ] || { usage; exit 5; }
WEBAPP=${WEBAPP%/}; [ -n "$WEBAPP" ] || WEBAPP=/
[ -d "$WEBAPP" ] || die "webapp folder not found (a .war file must be unpacked first): $(clean_path "$WEBAPP")"
WX="$WEBAPP/WEB-INF/web.xml"
{ [ -f "$WX" ] && [ -r "$WX" ]; } || die "WEB-INF/web.xml not found or unreadable under: $(clean_path "$WEBAPP")"
sz=$(wc -c < "$WX" 2>/dev/null) || die "WEB-INF/web.xml unreadable"
[ "$sz" -le "$MAX_XML" ] || die "WEB-INF/web.xml is larger than 2 MiB"

echo "WEBAPP: $(clean_path "$WEBAPP")"
# Version: WEB-INF/classes/roller-version.properties, ro.version line (Roller's Maven builds write it there).
PROPS="$WEBAPP/WEB-INF/classes/roller-version.properties"
VER=""; VSRC=""
if [ -f "$PROPS" ] && [ -r "$PROPS" ]; then
  psz=$(wc -c < "$PROPS" 2>/dev/null) || psz=$((MAX_PROPS + 1))
  if [ "$psz" -le "$MAX_PROPS" ]; then
    line=$(grep -a -m1 -E '^[[:space:]]*ro\.version[[:space:]]*[=:]' -- "$PROPS" 2>/dev/null | tr -d '\r') || line=""
    if [ -n "$line" ]; then
      raw=$(trim "${line#*[=:]}")
      if [[ "$raw" =~ $VER_RE ]]; then VER=$raw; VSRC="WEB-INF/classes/roller-version.properties"
      else echo "NOTE: ro.version in roller-version.properties is not a plain version number (value withheld)."; fi
    else echo "NOTE: roller-version.properties has no ro.version line."; fi
  else echo "NOTE: roller-version.properties is larger than 64 KiB; ignored."; fi
else echo "NOTE: WEB-INF/classes/roller-version.properties not found (older builds may keep it inside a jar)."; fi
if [ -n "$VER" ] && [ -n "$OPT_VER" ] && [ "$(ver_cmp "$VER" "$OPT_VER")" != 0 ]; then
  echo "NOTE: --version $OPT_VER ignored; the properties file says $VER."
elif [ -z "$VER" ] && [ -n "$OPT_VER" ]; then VER=$OPT_VER; VSRC="--version"; fi
if [ -n "$VER" ]; then echo "VERSION: $VER (from $VSRC)"; else echo "VERSION: not found"; fi

VSTATE=unknown
if [ -n "$VER" ]; then
  if [ "$(ver_cmp "$VER" "$FIRST_AFFECTED")" -lt 0 ]; then VSTATE=below
  elif [ "$(ver_cmp "$VER" "$ONLY_82384")" -lt 0 ]; then VSTATE=c129448
  elif [ "$(ver_cmp "$VER" "$FIXED")" -lt 0 ]; then VSTATE=c82384
  else VSTATE=fixed; fi
fi
case $VSTATE in
  c129448) echo "RANGE: affected by CVE-2026-129448 (Apache: 4.0.0 through 6.1.4)";;
  c82384) echo "RANGE: affected by CVE-2026-82384 (Apache: 6.1.5)";;
  fixed) echo "RANGE: at or above the fixed release 6.1.6 (Apache)";;
  below) echo "RANGE: below 4.0.0; Apache does not state whether these versions are affected";;
  *) echo "RANGE: unknown (no version)";;
esac

# web.xml: flatten, drop comments, split into servlet, servlet-mapping, filter and filter-mapping elements.
flat=$(tr '\000\001\r\n\t' '     ' < "$WX" 2>/dev/null) || die "WEB-INF/web.xml unreadable"
out=""
while [[ "$flat" == *'<!--'* ]]; do
  pre=${flat%%'<!--'*}; rest=${flat#*'<!--'}
  [[ "$rest" == *'-->'* ]] || break
  out+="$pre "; flat=${rest#*'-->'}
done
flat="$out$flat"
for t in '<servlet>' '<servlet-mapping>' '<filter>' '<filter-mapping>'; do flat=${flat//"$t"/$'\001'"$t"}; done
mapfile -t -d $'\001' CH <<< "$flat"
tag() { if [[ "$1" =~ \<$2\>([^\<]*)\</$2\> ]]; then trim "${BASH_REMATCH[1]}"; fi; }
patterns() { local s=$1; while [[ "$s" =~ \<url-pattern\>([^\<]*)\</url-pattern\> ]]; do trim "${BASH_REMATCH[1]}"; echo; s=${s#*"${BASH_REMATCH[0]}"}; done; }
NAMES=(); EXT=false_none; SERVLETS=0
ext_rank() { case $1 in true) echo 3;; other) echo 2;; absent) echo 1;; *) echo 0;; esac; }
for c in "${CH[@]}"; do
  case $c in
    '<servlet>'*)
      c=${c%%'</servlet>'*}; cls=$(tag "$c" servlet-class); nm=$(tag "$c" servlet-name)
      if [ "$cls" = "$XMLRPC_CLASS" ] || [ "$nm" = "XmlRpcServlet" ]; then
        SERVLETS=$((SERVLETS + 1)); NAMES+=("$nm")
        if [[ "$c" =~ \<param-name\>[[:space:]]*enabledForExtensions[[:space:]]*\</param-name\>[[:space:]]*\<param-value\>([^\<]*)\</param-value\> ]]; then
          v=$(trim "${BASH_REMATCH[1]}" | tr '[:upper:]' '[:lower:]'); case $v in true|false) ;; *) v=other;; esac
        elif [[ "$c" =~ \<param-name\>[[:space:]]*enabledForExtensions[[:space:]]*\</param-name\> ]]; then v=other
        else v=absent; fi
        [ "$(ext_rank "$v")" -gt "$(ext_rank "$EXT")" ] && EXT=$v
        echo "WEB.XML: XML-RPC servlet \"$(show "$nm")\" declared; enabledForExtensions: $(case $v in true) echo 'true (extension types on)';; false) echo 'false (extension types off)';; absent) echo 'not set (the library default is off; the Apache fix allows dropping the parameter)';; *) echo 'unrecognised value (withheld); OUR ASSUMPTION: treated as on';; esac)"
      fi;;
  esac
done
[ "$SERVLETS" -gt 0 ] || echo "WEB.XML: no XML-RPC servlet declared"
MAPPED=0; GATE=0
for c in "${CH[@]}"; do
  case $c in
    '<servlet-mapping>'*)
      c=${c%%'</servlet-mapping>'*}; nm=$(tag "$c" servlet-name)
      for k in "${NAMES[@]}"; do
        if [ "$nm" = "$k" ]; then
          while IFS= read -r up; do
            [ -n "$up" ] || continue; MAPPED=1
            if [ "$up" = "$XMLRPC_PATH" ]; then echo "WEB.XML: servlet mapping $XMLRPC_PATH present"
            else echo "WEB.XML: XML-RPC servlet mapped at $(show "$up")"; fi
          done < <(patterns "$c")
        fi
      done;;
    '<filter-mapping>'*)
      c=${c%%'</filter-mapping>'*}; nm=$(tag "$c" filter-name)
      if [ "$nm" = "$GATE_FILTER" ]; then
        while IFS= read -r up; do [ "$up" = "$XMLRPC_PATH" ] && GATE=1; done < <(patterns "$c")
      fi;;
  esac
done
[ "$SERVLETS" -gt 0 ] && [ "$MAPPED" = 0 ] && echo "WEB.XML: XML-RPC servlet has no servlet mapping"
if [ "$GATE" = 1 ]; then echo "WEB.XML: $GATE_FILTER guards $XMLRPC_PATH (added in 6.1.6)"
else echo "WEB.XML: no $GATE_FILTER on $XMLRPC_PATH (6.1.6 adds one)"; fi

EXPOSED=0
{ [ "$SERVLETS" -gt 0 ] && [ "$MAPPED" = 1 ] && { [ "$EXT" = true ] || [ "$EXT" = other ]; }; } && EXPOSED=1
case $VSTATE in
  c129448|c82384) if [ "$EXPOSED" = 1 ]; then rc=1; else rc=2; fi;;
  fixed) if [ "$EXPOSED" = 1 ]; then rc=3; else rc=0; fi
    [ "$GATE" = 0 ] && echo "NOTE: OUR ADVICE: deploy the web.xml shipped with your release; 6.1.6 maps $GATE_FILTER.";;
  *) rc=4
    [ "$EXPOSED" = 1 ] && echo "NOTE: web.xml matches the vulnerable configuration (extension types on, servlet mapped); find the version.";;
esac
case $rc in
  0) msg="FIXED";;
  1) msg="VULNERABLE: upgrade to 6.1.6 or later now; until then set enabledForExtensions to false or remove the $XMLRPC_PATH mapping and restart";;
  2) msg="MITIGATED, UPGRADE NEEDED: Apache's workaround is in place; upgrade to 6.1.6 or later";;
  3) msg="REVIEW: fixed version, but web.xml still switches the extension types on; set enabledForExtensions to false";;
  *) msg="UNKNOWN: version not established; rerun with --version from the Roller admin pages or your deployment records";;
esac
[ "$VSTATE" = fixed ] || echo "NOTE: disabling XML-RPC in the Roller admin UI is not enough on versions before 6.1.6 (Apache)."
echo "RESULT: $msg (exit $rc)"
exit "$rc"
