X7ROOT File Manager
Current Path:
/opt/lscwp_patcher
opt
/
lscwp_patcher
/
📁
..
📄
CHANGELOG.md
(1.54 KB)
📄
README.md
(1.11 KB)
📄
VERSION
(4 B)
📄
lscwp_patcher.sh
(16.05 KB)
Editing: lscwp_patcher.sh
#!/bin/bash # ============================================================================= # lscwp_patcher.sh — fleet patcher for the LiteSpeed Cache WordPress plugin # # Finds every WordPress install with litespeed-cache, reports the version, # and (with --apply) updates installs below the target version. # # Panels : cPanel (docroots from /var/cpanel/userdata) and Enhance # (/var/www) — auto-detected # Methods: wpcli - wp plugin update, run as the site owner # zip - replace plugin dir from the official wordpress.org zip # (no PHP from the site is executed — works on broken / # infected installs) # auto - wpcli first, zip fallback if wpcli fails (default) # # Default mode is DRY-RUN (report only). Nothing changes without --apply. # Compatible with bash 4.2 (CentOS 7). # ============================================================================= set -o pipefail TOOL_VERSION="0.2" PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin export PATH SLUG="litespeed-cache" TARGET="7.9.2" APPLY=0 METHOD="auto" ROOTS=() ROOTS_GIVEN=0 ONLY_USER="" ONLY_PATH="" WP_BIN="" PHP_BIN="" ZIP_FILE="" TIMEOUT=120 JOBS=4 KEEP_BACKUP=1 BASE="/opt/lscwp_patcher" LOG_BASE="/var/log/lscwp_patcher" LOCK_FILE="/run/lscwp_patcher.lock" usage() { cat <<EOF lscwp_patcher.sh v${TOOL_VERSION} Usage: $0 [options] --apply Make changes (default: dry-run / report only) --target VER Minimum safe version (default: ${TARGET}) --method M auto | wpcli | zip (default: auto) --user NAME Only this account (cPanel user / Enhance site user) --path DIR Only this WordPress root (dir containing wp-content) --root DIR Search root (repeatable), globbed up to 4 levels deep. Default: cPanel userdata docroots, or /var/www on Enhance --jobs N Sites patched in parallel with --apply (default: ${JOBS}) --wp PATH wp-cli binary (default: auto-detect) --php PATH PHP binary to run wp-cli with (default: wp shebang) --zip FILE Pre-staged plugin zip (default: download ${SLUG}.<target>.zip from downloads.wordpress.org) --timeout SEC Per-site wp-cli timeout (default: ${TIMEOUT}) --no-backup Do not keep tar.gz of the old plugin dir (zip method) -h, --help This help Output: ${LOG_BASE}/<timestamp>_<pid>/{run.log,report.csv,sites/,backup/} EOF } die() { echo "ERROR: $*" >&2; exit 1; } while [ $# -gt 0 ]; do case "$1" in --apply) APPLY=1 ;; --target) TARGET="$2"; shift ;; --method) METHOD="$2"; shift ;; --user) ONLY_USER="$2"; shift ;; --path) ONLY_PATH="${2%/}"; shift ;; --root) ROOTS+=("${2%/}"); ROOTS_GIVEN=1; shift ;; --jobs) JOBS="$2"; shift ;; --wp) WP_BIN="$2"; shift ;; --php) PHP_BIN="$2"; shift ;; --zip) ZIP_FILE="$2"; shift ;; --timeout) TIMEOUT="$2"; shift ;; --no-backup) KEEP_BACKUP=0 ;; -h|--help) usage; exit 0 ;; *) usage; die "unknown option: $1" ;; esac shift done [ "$(/usr/bin/id -u)" -eq 0 ] || die "must run as root" case "$METHOD" in auto|wpcli|zip) ;; *) die "--method must be auto, wpcli or zip" ;; esac [[ "$TARGET" =~ ^[0-9]+(\.[0-9]+)*$ ]] || die "invalid --target: $TARGET" [[ "$TIMEOUT" =~ ^[0-9]+$ ]] || die "invalid --timeout: $TIMEOUT" [[ "$JOBS" =~ ^[1-9][0-9]*$ ]] || die "invalid --jobs: $JOBS" exec 9>"$LOCK_FILE" || die "cannot open lock $LOCK_FILE" /usr/bin/flock -n 9 || die "another lscwp_patcher run is active" # ---------------------------------------------------------------- panel/roots PANEL="generic" if [ -d /usr/local/cpanel ] && [ -d /var/cpanel/userdata ]; then PANEL="cpanel" elif [ -d /var/local/enhance ] || [ -d /var/www ]; then PANEL="enhance" fi if [ -n "$ONLY_USER" ]; then /usr/bin/getent passwd "$ONLY_USER" >/dev/null || die "user not found: $ONLY_USER" fi if [ "$ROOTS_GIVEN" -eq 0 ] && [ "$PANEL" != "cpanel" ]; then ROOTS=("/var/www") fi # ---------------------------------------------------------------- wp-cli if [ -z "$WP_BIN" ]; then for c in /usr/local/bin/wp /usr/bin/wp /usr/local/bin/wp-cli /opt/wp-cli/wp; do [ -x "$c" ] && { WP_BIN="$c"; break; } done fi if [ "$METHOD" != "zip" ] && [ -z "$WP_BIN" ]; then if [ "$METHOD" = "wpcli" ]; then die "wp-cli not found; pass --wp or use --method zip" fi METHOD="zip" fi # ---------------------------------------------------------------- run dirs TS=$(/usr/bin/date +%Y%m%d_%H%M%S) RUN_DIR="${LOG_BASE}/${TS}_$$" /usr/bin/mkdir -p "$RUN_DIR/sites" "$RUN_DIR/backup" || die "cannot create $RUN_DIR" /usr/bin/chmod 700 "$LOG_BASE" "$RUN_DIR" LOG="$RUN_DIR/run.log" REPORT="$RUN_DIR/report.csv" MARKER="$RUN_DIR/.start" /usr/bin/touch "$MARKER" echo "path,owner,before,after,status,note" > "$REPORT" log() { echo "[$(/usr/bin/date '+%F %T')] $*" | /usr/bin/tee -a "$LOG"; } # ---------------------------------------------------------------- helpers get_ver() { # Read "Version:" from the plugin header without executing PHP [ -f "$1" ] || return 0 /usr/bin/head -n 60 "$1" 2>/dev/null \ | /usr/bin/grep -m1 -iE '^[[:space:]/*#@]*Version:' \ | /usr/bin/sed -E 's/.*[Vv]ersion:[[:space:]]*//; s/[[:space:]\r].*//' } ver_lt() { # true if $1 < $2 [ "$1" != "$2" ] && \ [ "$(printf '%s\n%s\n' "$1" "$2" | /usr/bin/sort -V | /usr/bin/head -n1)" = "$1" ] } csv() { # path,owner,before,after,status,note — one append per row (atomic, safe # with parallel jobs) local out="" f for f in "$@"; do out+="\"${f//\"/\"\"}\","; done printf '%s\n' "${out%,}" >> "$REPORT" } site_id() { echo "$1" | /usr/bin/sed 's#^/##; s#/#__#g'; } run_wp() { local owner="$1" root="$2" slog="$3"; shift 3 local home home=$(/usr/bin/getent passwd "$owner" | /usr/bin/cut -d: -f6) [ -n "$home" ] && [ -d "$home" ] || home="/tmp" local -a cmd=("$WP_BIN") [ -n "$PHP_BIN" ] && cmd=("$PHP_BIN" -d memory_limit=512M "$WP_BIN") ( cd "$root" || exit 1 /usr/sbin/runuser -u "$owner" -- /usr/bin/env -i \ HOME="$home" PATH="$PATH" LANG=C \ /usr/bin/timeout -k 10 "$TIMEOUT" "${cmd[@]}" \ --path="$root" --skip-themes --skip-plugins --no-color "$@" ) >>"$slog" 2>&1 } # ZIP_READY: 0 = not tried, 1 = staged ok, 2 = unavailable ZIP_READY=0 STAGE_DIR="" prepare_zip() { [ "$ZIP_READY" -eq 1 ] && return 0 [ "$ZIP_READY" -eq 2 ] && return 1 ZIP_READY=2 if [ -z "$ZIP_FILE" ]; then /usr/bin/mkdir -p "$BASE/cache" ZIP_FILE="$BASE/cache/${SLUG}.${TARGET}.zip" if [ ! -s "$ZIP_FILE" ]; then local url="https://downloads.wordpress.org/plugin/${SLUG}.${TARGET}.zip" log "Downloading $url" if ! /usr/bin/curl -fsSL --max-time 120 -o "$ZIP_FILE.tmp" "$url"; then /usr/bin/rm -f "$ZIP_FILE.tmp" log "ZIP: download failed — zip fallback disabled for this run" return 1 fi /usr/bin/mv -f "$ZIP_FILE.tmp" "$ZIP_FILE" fi fi [ -s "$ZIP_FILE" ] || { log "ZIP: $ZIP_FILE missing"; return 1; } /usr/bin/unzip -tq "$ZIP_FILE" >/dev/null 2>&1 || { log "ZIP: integrity test failed"; return 1; } STAGE_DIR="$RUN_DIR/stage" /usr/bin/rm -rf "$STAGE_DIR" /usr/bin/mkdir -p "$STAGE_DIR" /usr/bin/unzip -q "$ZIP_FILE" -d "$STAGE_DIR" || { log "ZIP: extract failed"; return 1; } local zv zv=$(get_ver "$STAGE_DIR/$SLUG/$SLUG.php") if [ -z "$zv" ] || ver_lt "$zv" "$TARGET"; then log "ZIP: contains version '${zv:-unknown}', below target $TARGET" return 1 fi log "ZIP: $ZIP_FILE ok (version $zv, sha256 $(/usr/bin/sha256sum "$ZIP_FILE" | /usr/bin/cut -d' ' -f1))" ZIP_READY=1 return 0 } apply_zip() { local plugdir="$1" owner="$2" group="$3" slog="$4" local pdir="${plugdir%/*}" local id="$BASHPID" local tmp="$pdir/.${SLUG}.lscwp_new.$id" local old="$pdir/.${SLUG}.lscwp_old.$id" [ "$ZIP_READY" -eq 1 ] || return 1 /usr/bin/rm -rf "$tmp" "$old" /usr/bin/cp -a "$STAGE_DIR/$SLUG" "$tmp" || { /usr/bin/rm -rf "$tmp"; return 1; } /usr/bin/chown -R "$owner:$group" "$tmp" /usr/bin/find "$tmp" -type d -exec /usr/bin/chmod 755 {} + /usr/bin/find "$tmp" -type f -exec /usr/bin/chmod 644 {} + # Same-filesystem renames: plugin dir is missing only between two mv calls /usr/bin/mv "$plugdir" "$old" || { /usr/bin/rm -rf "$tmp"; return 1; } if ! /usr/bin/mv "$tmp" "$plugdir"; then /usr/bin/mv "$old" "$plugdir" /usr/bin/rm -rf "$tmp" return 1 fi if [ "$KEEP_BACKUP" -eq 1 ]; then /usr/bin/tar -C "$pdir" -czf "$RUN_DIR/backup/$(site_id "$plugdir").tgz" \ --transform "s#^\.${SLUG}\.lscwp_old\.$id#${SLUG}#" \ ".${SLUG}.lscwp_old.$id" >>"$slog" 2>&1 fi /usr/bin/rm -rf "$old" echo "zip: replaced $plugdir from $ZIP_FILE" >>"$slog" return 0 } cleanup_maintenance() { # Remove .maintenance only if this run created it (timeout/kill mid-update) local root="$1" if [ -f "$root/.maintenance" ] && [ "$root/.maintenance" -nt "$MARKER" ]; then /usr/bin/rm -f "$root/.maintenance" echo "removed stale .maintenance" >>"$2" fi } # ---------------------------------------------------------------- discovery # Bounded glob lookups instead of a recursive find: only directory listings # down to a fixed depth are read, never uploads/cache/backup trees. REL="wp-content/plugins/${SLUG}/${SLUG}.php" glob_depth() { # $1 = base dir, $2 = max extra levels below base local b="$1" max="$2" pat="" i f for ((i = 0; i <= max; i++)); do for f in "$b"/$pat"$REL"; do [ -f "$f" ] && echo "$f" done pat+="*/" done } cpanel_docroots() { local u f dr local -a users=() if [ -n "$ONLY_USER" ]; then users=("$ONLY_USER") else for f in /var/cpanel/userdata/*; do u="${f##*/}" [ -d "$f" ] && [ "$u" != "nobody" ] && users+=("$u") done fi for u in "${users[@]}"; do for f in /var/cpanel/userdata/"$u"/*; do case "${f##*/}" in main|cache|*_SSL|*.cache|*.yaml|*.json|*.db) continue ;; esac [ -f "$f" ] || continue dr=$(/usr/bin/grep -m1 '^documentroot:' "$f" 2>/dev/null | /usr/bin/sed 's/^documentroot:[[:space:]]*//') [ -n "$dr" ] && [ -d "$dr" ] && echo "${dr%/}" done # Fallback for installs outside any docroot, e.g. ~/public_html/x dr=$(/usr/bin/getent passwd "$u" | /usr/bin/cut -d: -f6) [ -n "$dr" ] && [ -d "$dr/public_html" ] && echo "$dr/public_html" done | /usr/bin/sort -u } discover() { shopt -s nullglob if [ -n "$ONLY_PATH" ]; then [ -f "$ONLY_PATH/$REL" ] && echo "$ONLY_PATH/$REL" return 0 fi { if [ "$PANEL" = "cpanel" ] && [ "$ROOTS_GIVEN" -eq 0 ]; then local d while IFS= read -r d; do glob_depth "$d" 2 # docroot, docroot/x, docroot/x/y done < <(cpanel_docroots) else local r for r in "${ROOTS[@]}"; do [ -d "$r" ] && glob_depth "$r" 4 done fi } | /usr/bin/grep -vE '/virtfs/|/\.trash/|/\.sc_|/(backup|backups|softaculous_backups|node_modules)/' \ | /usr/bin/sort -u } # ---------------------------------------------------------------- per site # Writes exactly one CSV row per site (or none if filtered out by --user). process_site() { local pfile="$1" local plugdir="${pfile%/*}" local root="${pfile%/wp-content/plugins/*}" local owner group uid before after status note slog slog="$RUN_DIR/sites/$(site_id "$root").log" owner=$(/usr/bin/stat -c %U "$root" 2>/dev/null) group=$(/usr/bin/stat -c %G "$root" 2>/dev/null) uid=$(/usr/bin/stat -c %u "$root" 2>/dev/null) [ -n "$ONLY_USER" ] && [ "$owner" != "$ONLY_USER" ] && return if [ ! -f "$root/wp-includes/version.php" ]; then csv "$root" "$owner" "" "" "skipped" "not a WordPress root" return fi before=$(get_ver "$pfile") if [ -z "$before" ]; then csv "$root" "$owner" "" "" "skipped" "version header unreadable — inspect manually" log "SKIP $root ($owner): version header unreadable" return fi if ! ver_lt "$before" "$TARGET"; then csv "$root" "$owner" "$before" "$before" "ok" "" return fi if [ "$APPLY" -eq 0 ]; then csv "$root" "$owner" "$before" "" "vulnerable" "dry-run" log "VULN $root ($owner) $before" return fi if [ "$owner" = "root" ] || [ -z "$uid" ] || [ "$uid" -lt 500 ] || [ "$owner" = "UNKNOWN" ]; then csv "$root" "$owner" "$before" "" "skipped" "owned by system/root user" log "SKIP $root: owner '$owner' (uid $uid)" return fi echo "=== $(/usr/bin/date '+%F %T') $root owner=$owner before=$before target=$TARGET" >>"$slog" note="" status="failed" if [ "$METHOD" = "wpcli" ] || [ "$METHOD" = "auto" ]; then run_wp "$owner" "$root" "$slog" plugin update "$SLUG" || note="wpcli rc=$?" cleanup_maintenance "$root" "$slog" after=$(get_ver "$pfile") if [ -n "$after" ] && ! ver_lt "$after" "$TARGET"; then status="updated_wpcli" fi fi if [ "$status" = "failed" ] && { [ "$METHOD" = "zip" ] || [ "$METHOD" = "auto" ]; }; then if apply_zip "$plugdir" "$owner" "$group" "$slog"; then after=$(get_ver "$pfile") if [ -n "$after" ] && ! ver_lt "$after" "$TARGET"; then status="updated_zip" else note="${note:+$note; }zip applied but version '$after'" fi else note="${note:+$note; }zip unavailable/failed" fi fi after=$(get_ver "$pfile") csv "$root" "$owner" "$before" "$after" "$status" "$note" if [ "$status" = "failed" ]; then log "FAIL $root ($owner) $before -> ${after:-?} [$note] see sites/$(site_id "$root").log" else log "UPD $root ($owner) $before -> $after [$status]" fi } # ---------------------------------------------------------------- main log "lscwp_patcher v${TOOL_VERSION} | host=$(/usr/bin/hostname) panel=$PANEL mode=$([ "$APPLY" -eq 1 ] && echo APPLY || echo DRY-RUN) method=$METHOD target=$TARGET jobs=$JOBS" if [ -n "$ONLY_PATH" ]; then log "scope: path $ONLY_PATH" elif [ "$PANEL" = "cpanel" ] && [ "$ROOTS_GIVEN" -eq 0 ]; then log "scope: cPanel userdata docroots${ONLY_USER:+ (user $ONLY_USER)}" else log "scope: roots ${ROOTS[*]}${ONLY_USER:+ (user $ONLY_USER)}" fi log "wp=${WP_BIN:-none} php=${PHP_BIN:-default}" SECONDS=0 mapfile -t FOUND < <(discover) log "discovered ${#FOUND[@]} ${SLUG} install(s) in ${SECONDS}s" # Stage the zip once, before forking, so every job sees the same state if [ "$APPLY" -eq 1 ] && [ "$METHOD" != "wpcli" ] && [ "${#FOUND[@]}" -gt 0 ]; then prepare_zip || true fi if [ "$APPLY" -eq 1 ] && [ "$JOBS" -gt 1 ]; then for f in "${FOUND[@]}"; do while [ "$(jobs -rp | /usr/bin/wc -l)" -ge "$JOBS" ]; do /usr/bin/sleep 0.2 done process_site "$f" & done wait else for f in "${FOUND[@]}"; do process_site "$f" done fi # Summary from the report (works for both serial and parallel runs) read -r C_TOTAL C_OK C_VULN C_WP C_ZIP C_FAIL C_SKIP < <( /usr/bin/awk -F'","' 'NR>1 { n++; s[$5]++ } END { printf "%d %d %d %d %d %d %d\n", n, s["ok"], s["vulnerable"], s["updated_wpcli"], s["updated_zip"], s["failed"], s["skipped"] }' "$REPORT" ) log "------------------------------------------------------------" log "total=$C_TOTAL ok=$C_OK vulnerable=$C_VULN updated_wpcli=$C_WP updated_zip=$C_ZIP failed=$C_FAIL skipped=$C_SKIP elapsed=${SECONDS}s" log "report: $REPORT" [ "$APPLY" -eq 0 ] && [ "$C_VULN" -gt 0 ] && log "dry-run: re-run with --apply to update" /usr/bin/rm -rf "$RUN_DIR/stage" "$MARKER" [ "$C_FAIL" -gt 0 ] && exit 2 exit 0
Upload File
Create Folder