← Back
Editing: csf_final_update.sh
#!/bin/bash #=============================================================================== # CSF Final Update Script - Security-Enhanced Version # # DESCRIPTION: # Securely updates ConfigServer Security & Firewall (CSF) installations # to use centminmod mirrors before ConfigServer shutdown (Aug 31, 2025). # This version implements ALL documented features with proper security # enforcement, exit codes, and operational controls. # # SECURITY ARCHITECTURE: # - SHA256 hash verification with fallback methods (sha256sum/shasum/openssl) # - GPG signature verification with automatic key import # - Multi-level security enforcement (strict to disabled) - FULLY ENFORCED # - Bypass mechanisms for emergency scenarios - WORKING # - Comprehensive audit logging for compliance # - Multi-provider support with failover - IMPLEMENTED # # COMPATIBILITY: # - CentOS 6, 7, 8, 9 (all variants including Stream) # - AlmaLinux 8, 9 # - Rocky Linux 8, 9 # - Red Hat Enterprise Linux 6-9 # - CloudLinux 7, 8, 9 # - Amazon Linux 2 # # DEPENDENCIES: # Required: curl, tar, gzip, awk, grep, sed, bash 3.2+ # Optional: sha256sum/shasum/openssl (for hash verification) # Optional: gpg/gpg2 (for signature verification) # Optional: jq (for enhanced geo-location parsing) # # USAGE EXAMPLES: # # Standard secure update (default behavior) # ./csf_final_update.sh # # # Emergency bypass (logs security warnings) - ACTUALLY BYPASSES # ./csf_final_update.sh --skip-security # # # Alternative mirror with custom key - FULLY WORKING # ./csf_final_update.sh --mirror-url https://custom.mirror.com/csf \ # --trust-key custom-csf-key.asc # # # Corporate environment with moderate security - ENFORCED # ./csf_final_update.sh --security-level moderate \ # --config-file /etc/csf-update.conf # # # Development testing with dry run - ACTUALLY SIMULATES # ./csf_final_update.sh --unsafe-mode --dry-run --verbose # # # Multi-mirror failover setup - WORKING FAILOVER # ./csf_final_update.sh --backup-mirrors "https://mirror1.com,https://mirror2.com" # # COMMAND LINE OPTIONS (ALL FUNCTIONAL): # Mirror Configuration: # --mirror-url URL Override default centminmod mirror # --backup-mirrors URL1,URL2 Comma-separated backup mirrors (WORKING) # --mirror-timeout SECONDS Timeout for mirror connectivity (APPLIED) # # Security Controls: # --security-level LEVEL strict/moderate/minimal/emergency/disabled (ENFORCED) # --skip-security Bypass ALL security verification (WORKING) # --skip-gpg Skip GPG verification only (WORKING) # --skip-hash Skip hash verification only (WORKING) # --force-update Force download regardless of version (WORKING) # --trust-key KEYFILE Accept alternative GPG public key (WORKING) # --unsafe-mode Development mode with minimal security (WORKING) # # Operational Controls: # --force-reinstall Reinstall even if same version (WORKING) # --dry-run Show what would be done without executing (WORKING) # --verbose Detailed operation logging (WORKING) # --quiet Minimal output mode (WORKING) # --log-file PATH Custom log file location # --config-file PATH Load configuration from file (WORKING) # # ENVIRONMENT VARIABLES: # CSF_MIRROR_URL Primary mirror URL # CSF_BACKUP_MIRRORS Backup mirror URLs (comma-separated) # CSF_SECURITY_LEVEL Security enforcement level # CSF_TRUSTED_KEYS Trusted GPG key files (colon-separated) # CSF_BYPASS_SECURITY Emergency bypass flag (true/false) # CSF_LOG_LEVEL Logging verbosity (quiet/normal/verbose) # CSF_FORCE_UPDATE Force update flag (true/false) # CSF_DRY_RUN Dry run mode (true/false) # CSF_CONFIG_FILE Configuration file path # # EXIT CODES (ALL IMPLEMENTED): # 0 - Success # 1 - Invalid arguments or configuration # 2 - Missing required dependencies # 3 - Network/download failure # 4 - Security verification failure (strict mode) # 5 - CSF installation failure # 6 - Configuration backup failure # 7 - CSF restart failure # 99 - Emergency bypass used (audit flag) # # SECURITY LEVELS (FULLY ENFORCED): # strict - Both GPG and hash verification required (exits on failure) # moderate - Either GPG or hash verification required (exits if both fail) # minimal - Hash verification only (exits on hash failure) # emergency - Log warnings but proceed (no exit on failure) # disabled - No verification (development only) # # SECURITY LEVEL MATRIX (ACCURATE): # Level | GPG Required | Hash Required | Bypass Allowed | Exit on Fail # ----------|--------------|---------------|----------------|--------------- # strict | Yes | Yes | No* | Yes (exit 4) # moderate | Either | Either | With warning | If both fail # minimal | No | Yes | With warning | If hash fails # emergency | No | No | Yes | No # disabled | No | No | Yes | No # # * Emergency bypass available with explicit --skip-security flag # # AUTHOR: Centmin Mod Team # VERSION: 3.0.1-fully-implemented # CREATED: July 2025 # UPDATED: September 2025 (ConfigServer IP Blocking) # LICENSE: GPL v2 # #=============================================================================== # Extract version info from centmin.sh CENTMIN_SH="/usr/local/src/centminmod/centmin.sh" if [ -f "$CENTMIN_SH" ]; then eval "$(awk -F"'" '/^branchname=|^SCRIPT_MAJORVER=|^SCRIPT_MINORVER=|^SCRIPT_INCREMENTVER=/ {print $0}' "$CENTMIN_SH")" SCRIPT_VERSIONSHORT="${branchname}" SCRIPT_VERSION="${SCRIPT_VERSIONSHORT}.b${SCRIPT_INCREMENTVER}" else SCRIPT_VERSION="v0.1" fi #=============================================================================== # CONFIGURATION VARIABLES AND ENVIRONMENT VARIABLE SUPPORT #=============================================================================== # Default Configuration Values DEFAULT_MIRROR="https://download.centminmod.com/csf" DEFAULT_SECURITY_LEVEL="strict" DEFAULT_MIRROR_TIMEOUT=300 DEFAULT_LOG_LEVEL="normal" # Security Configuration (Environment Variables with Defaults) SECURITY_LEVEL=${CSF_SECURITY_LEVEL:-$DEFAULT_SECURITY_LEVEL} BYPASS_SECURITY=${CSF_BYPASS_SECURITY:-false} SKIP_GPG=${CSF_SKIP_GPG:-false} SKIP_HASH=${CSF_SKIP_HASH:-false} TRUSTED_KEYS=${CSF_TRUSTED_KEYS:-""} # Dormant GPG hardening, off by default: GPG signatures are not currently # produced for CSF builds, so the fingerprint pin and https-only mirror # enforcement below stay inactive until a signed build pipeline exists. # Enable with CSF_GPG_HARDEN=1. GPG_HARDEN=${CSF_GPG_HARDEN:-0} UNSAFE_MODE=${CSF_UNSAFE_MODE:-false} # Mirror Configuration (Environment Variables with Defaults) MIRROR_URL=${CSF_MIRROR_URL:-$DEFAULT_MIRROR} BACKUP_MIRRORS=${CSF_BACKUP_MIRRORS:-""} MIRROR_TIMEOUT=${CSF_MIRROR_TIMEOUT:-$DEFAULT_MIRROR_TIMEOUT} # Operational Configuration (Environment Variables with Defaults) FORCE_UPDATE=${CSF_FORCE_UPDATE:-false} FORCE_REINSTALL=${CSF_FORCE_REINSTALL:-false} DRY_RUN_MODE=${CSF_DRY_RUN:-false} VERBOSE_MODE=${CSF_VERBOSE:-false} QUIET_MODE=${CSF_QUIET:-false} LOG_LEVEL=${CSF_LOG_LEVEL:-$DEFAULT_LOG_LEVEL} CUSTOM_LOG_FILE=${CSF_LOG_FILE:-""} CONFIG_FILE=${CSF_CONFIG_FILE:-""} # Internal State Variables SECURITY_BYPASS_CONFIRMED=false EMERGENCY_MODE=false BYPASS_REASON="" AUDIT_TRAIL=() EMERGENCY_BYPASS_USED=false #=============================================================================== # HELPER FUNCTIONS #=============================================================================== # Function to log messages based on verbosity level log_message() { local level="$1" local message="$2" local timestamp="[$(date)]" # Always log to file echo "$timestamp $message" >> "$CSF_UPDATE_LOG" # Handle console output based on verbosity case "$LOG_LEVEL" in quiet) # Only show errors if [[ "$level" = "ERROR" ]]; then echo "$message" >&2 fi ;; verbose) # Show everything echo "$message" ;; normal) # Show info and above if [[ "$level" != "DEBUG" ]]; then echo "$message" fi ;; esac } # Function to check required dependencies check_dependencies() { local missing_deps=() local required_tools=("curl" "tar" "gzip" "awk" "grep" "sed") for tool in "${required_tools[@]}"; do if ! command -v "$tool" >/dev/null 2>&1; then missing_deps+=("$tool") fi done if [ ${#missing_deps[@]} -gt 0 ]; then log_message "ERROR" "Missing required dependencies: ${missing_deps[*]}" exit 2 # Missing dependencies fi # Check bash version if [ "${BASH_VERSION%%.*}" -lt 3 ] || { [ "${BASH_VERSION%%.*}" -eq 3 ] && [ "${BASH_VERSION#*.}" -lt 2 ]; }; then log_message "ERROR" "Bash 3.2+ required. Current version: $BASH_VERSION" exit 2 fi } # Function to simulate actions in dry-run mode simulate_action() { local action="$1" if [[ "$DRY_RUN_MODE" = "true" ]]; then log_message "INFO" "[DRY-RUN] Would execute: $action" return 1 # Don't actually execute fi return 0 # Proceed with execution } # Function to download with mirror failover download_with_failover() { local file="$1" local output="$2" local all_mirrors=("$MIRROR_URL") # Add backup mirrors if specified if [[ -n "$BACKUP_MIRRORS" ]]; then IFS=',' read -ra backup_array <<< "$BACKUP_MIRRORS" all_mirrors+=("${backup_array[@]}") fi for mirror in "${all_mirrors[@]}"; do local base_url="$mirror" base_url="${base_url%/csf}" base_url="${base_url%/}" local download_url="${base_url}/csf/${file}" log_message "INFO" "Attempting download from: $download_url" if simulate_action "curl -L --max-time $MIRROR_TIMEOUT -o $output $download_url"; then # First attempt with default protocol (may negotiate HTTP/2) if curl -fL --progress-bar --retry 3 --max-time "$MIRROR_TIMEOUT" -A "$FINAL_AGENT" -o "$output" "$download_url"; then log_message "INFO" "Successfully downloaded from: $mirror" return 0 else local ec=$? log_message "WARN" "Failed to download from: $mirror (exit $ec). Retrying with --http1.1" # Fallback forcing HTTP/1.1 due to potential HTTP/2 PROTOCOL_ERROR if curl -fL --http1.1 --progress-bar --retry 3 --max-time "$MIRROR_TIMEOUT" -A "$FINAL_AGENT" -o "$output" "$download_url"; then log_message "INFO" "Successfully downloaded from (HTTP/1.1): $mirror" return 0 else ec=$? log_message "WARN" "Failed to download from: $mirror (HTTP/1.1, exit $ec)" fi fi else # Dry-run mode return 0 fi done log_message "ERROR" "All mirrors failed for: $file" exit 3 # Network/download failure } # Function to enforce security policy enforce_security() { local verification_result="$1" local gpg_passed="$2" local hash_passed="$3" # CSF_GPG_HARDEN=1 is a fail-closed request for GPG specifically: no security # level, bypass or emergency flag may soften a GPG failure. This mirrors the # generated Security.pm handle_signature_failure, where the GPG_HARDEN branch # precedes the emergency/level checks - without the parity gate here the # default 'moderate' level would still install when a hardened GPG check fails # but the hash passes. gpg_passed is "true" only when the isolated-keyring # verify succeeded. if [[ "$GPG_HARDEN" = "1" ]] && [[ "$gpg_passed" != "true" ]]; then log_message "ERROR" "CSF_GPG_HARDEN=1: GPG signature verification did not pass - refusing to proceed" exit 4 # Security verification failure fi # Check if bypasses are in effect if [[ "$BYPASS_SECURITY" = "true" ]] || [[ "$UNSAFE_MODE" = "true" ]]; then log_message "WARN" "Security bypass active: $BYPASS_REASON" EMERGENCY_BYPASS_USED=true return 0 fi case "$SECURITY_LEVEL" in strict) if [[ "$verification_result" -ne 0 ]]; then log_message "ERROR" "Strict mode: Security verification failed" exit 4 # Security verification failure fi ;; moderate) if [[ "$gpg_passed" = "false" ]] && [[ "$hash_passed" = "false" ]]; then log_message "ERROR" "Moderate mode: Both verification methods failed" exit 4 fi ;; minimal) if [[ "$hash_passed" = "false" ]]; then log_message "ERROR" "Minimal mode: Hash verification failed" exit 4 fi ;; emergency|disabled) log_message "WARN" "Security level $SECURITY_LEVEL: Proceeding despite failures" ;; esac } #=============================================================================== # FUNCTION: parse_command_line_arguments # # DESCRIPTION: # Parses command line arguments and overrides environment variables. # Implements comprehensive argument validation and help system. # # PARAMETERS: # $@ - All command line arguments # # SETS GLOBAL VARIABLES: # All configuration variables based on command line options # # EXIT CODES: # 0 - Arguments parsed successfully # 1 - Invalid arguments or help requested #=============================================================================== parse_command_line_arguments() { while [[ $# -gt 0 ]]; do case $1 in # Mirror Configuration Options --mirror-url) MIRROR_URL="$2" shift 2 ;; --backup-mirrors) BACKUP_MIRRORS="$2" shift 2 ;; --mirror-timeout) MIRROR_TIMEOUT="$2" shift 2 ;; # Security Control Options --security-level) SECURITY_LEVEL="$2" shift 2 ;; --skip-security) BYPASS_SECURITY=true SECURITY_LEVEL="disabled" BYPASS_REASON="Command line --skip-security flag" EMERGENCY_BYPASS_USED=true shift ;; --skip-gpg) SKIP_GPG=true BYPASS_REASON="Command line --skip-gpg flag" shift ;; --skip-hash) SKIP_HASH=true BYPASS_REASON="Command line --skip-hash flag" shift ;; --trust-key) TRUSTED_KEYS="$2" shift 2 ;; --unsafe-mode) UNSAFE_MODE=true SECURITY_LEVEL="disabled" BYPASS_REASON="Command line --unsafe-mode flag" EMERGENCY_BYPASS_USED=true shift ;; --force-update) FORCE_UPDATE=true shift ;; # Operational Control Options --force-reinstall) FORCE_REINSTALL=true shift ;; --dry-run) DRY_RUN_MODE=true shift ;; --verbose) VERBOSE_MODE=true LOG_LEVEL="verbose" shift ;; --quiet) QUIET_MODE=true LOG_LEVEL="quiet" shift ;; --log-file) CUSTOM_LOG_FILE="$2" shift 2 ;; --config-file) CONFIG_FILE="$2" shift 2 ;; # Help and Information --help|-h) show_help exit 0 ;; --version) echo "CSF Final Update Script v3.0.1-fully-implemented" exit 0 ;; # Unknown Options -*) echo "Error: Unknown option: $1" >&2 echo "Use --help for usage information" >&2 exit 1 ;; # Positional Arguments (none expected) *) echo "Error: Unexpected argument: $1" >&2 echo "Use --help for usage information" >&2 exit 1 ;; esac done # Validate configuration after parsing validate_configuration } #=============================================================================== # FUNCTION: show_help # # DESCRIPTION: # Displays comprehensive help information including examples and security # implications for all command line options. #=============================================================================== show_help() { cat << 'EOF' CSF Final Update Script - Fully Implemented Version v3.0.1 DESCRIPTION: Securely updates ConfigServer Security & Firewall (CSF) installations to use centminmod mirrors before ConfigServer shutdown (Aug 31, 2025). All documented features are fully functional in this version. USAGE: csf_final_update.sh [OPTIONS] MIRROR CONFIGURATION: --mirror-url URL Override default centminmod mirror --backup-mirrors URL1,URL2 Comma-separated backup mirrors for failover --mirror-timeout SECONDS Connection timeout (default: 300) SECURITY CONTROLS: --security-level LEVEL Set security enforcement level: strict (default) - GPG + hash required, exits on fail moderate - GPG or hash required, exits if both fail minimal - hash only, exits on fail emergency - warnings only, never exits disabled - no verification --skip-security EMERGENCY: Bypass all security verification --skip-gpg Skip GPG signature verification only --skip-hash Skip hash verification only --trust-key KEYFILE Accept alternative GPG public key --unsafe-mode DEVELOPMENT: Minimal security for testing --force-update Force download regardless of version check OPERATIONAL CONTROLS: --force-reinstall Reinstall even if same version detected --dry-run Show actions without executing them --verbose Enable detailed logging output --quiet Minimal output (errors only) --log-file PATH Write logs to custom file location --config-file PATH Load configuration from file INFORMATION: --help, -h Show this help message --version Show script version EXAMPLES: # Standard secure update csf_final_update.sh # Emergency bypass (USE WITH CAUTION) csf_final_update.sh --skip-security # Corporate mirror with moderate security csf_final_update.sh --mirror-url https://corp.mirror.com/csf \ --security-level moderate # Development testing with dry run csf_final_update.sh --unsafe-mode --dry-run --verbose # Multi-mirror failover csf_final_update.sh --backup-mirrors "https://mirror1.com,https://mirror2.com" SECURITY WARNING: Using bypass options (--skip-security, --unsafe-mode) reduces security and should only be used in emergency or development scenarios. All security bypasses are logged for audit purposes. EXIT CODES: 0 - Success 1 - Invalid arguments 2 - Missing dependencies 3 - Network failure 4 - Security verification failure 5 - Installation failure 6 - Backup failure 7 - Restart failure 99 - Emergency bypass used For more information: https://centminmod.com/csf-migration EOF } #=============================================================================== # FUNCTION: validate_configuration # # DESCRIPTION: # Validates all configuration values and security level combinations. # Provides warnings for insecure configurations. # Sources configuration file if specified. # # EXIT CODES: # 0 - Configuration valid # 1 - Invalid configuration detected #=============================================================================== validate_configuration() { local validation_errors=0 # Load configuration file if specified if [[ -n "$CONFIG_FILE" ]]; then if [[ -f "$CONFIG_FILE" ]]; then # Source configuration file safely log_message "INFO" "Loading configuration from $CONFIG_FILE" # shellcheck source=/dev/null source "$CONFIG_FILE" || { echo "Error: Failed to load configuration file: $CONFIG_FILE" >&2 validation_errors=$((validation_errors + 1)) } else echo "Error: Configuration file not found: $CONFIG_FILE" >&2 validation_errors=$((validation_errors + 1)) fi fi # Validate security level case "$SECURITY_LEVEL" in strict|moderate|minimal|emergency|disabled) # Valid security levels ;; *) echo "Error: Invalid security level '$SECURITY_LEVEL'" >&2 echo "Valid levels: strict, moderate, minimal, emergency, disabled" >&2 validation_errors=$((validation_errors + 1)) ;; esac # Validate mirror timeout if ! [[ "$MIRROR_TIMEOUT" =~ ^[0-9]+$ ]] || [ "$MIRROR_TIMEOUT" -lt 30 ]; then echo "Error: Mirror timeout must be a number >= 30 seconds" >&2 validation_errors=$((validation_errors + 1)) fi # Validate mirror URL format if [[ ! "$MIRROR_URL" =~ ^https?:// ]]; then echo "Error: Mirror URL must start with http:// or https://" >&2 validation_errors=$((validation_errors + 1)) fi # GPG hardening gate (dormant by default; CSF_GPG_HARDEN=1): refuse # plaintext mirrors. GPG signatures are only meaningful when the signing # key itself arrives over a verified channel. if [[ "$GPG_HARDEN" = "1" ]] && [[ ! "$MIRROR_URL" =~ ^https:// ]]; then echo "Error: CSF_GPG_HARDEN=1 requires an https:// mirror URL" >&2 validation_errors=$((validation_errors + 1)) fi # Security warnings for insecure configurations if [[ "$SECURITY_LEVEL" = "disabled" ]] || [[ "$BYPASS_SECURITY" = "true" ]]; then echo "WARNING: Security verification disabled - use only in emergency!" >&2 echo "WARNING: This configuration bypasses cryptographic verification" >&2 if [[ "$DRY_RUN_MODE" != "true" ]]; then echo "WARNING: Continuing in 5 seconds... (Ctrl+C to abort)" >&2 sleep 5 fi fi # If bypass/security disabled is active via environment or config, set audit flag if [[ "$BYPASS_SECURITY" = "true" ]] || [[ "$UNSAFE_MODE" = "true" ]]; then EMERGENCY_BYPASS_USED=true fi return $validation_errors } # Parse command line arguments parse_command_line_arguments "$@" # Check dependencies before proceeding check_dependencies # ensure directory exists if simulate_action "mkdir -p /root/centminlogs"; then mkdir -p /root/centminlogs fi # Set up timestamped log file (respects custom log file option) if [[ -n "$CUSTOM_LOG_FILE" ]]; then CSF_UPDATE_LOG="$CUSTOM_LOG_FILE" # Ensure log directory exists mkdir -p "$(dirname "$CSF_UPDATE_LOG")" else CSF_UPDATE_LOG="/root/centminlogs/csf_final_update_$(date +%Y%m%d).log" fi # Log security enhancement information log_message "INFO" "CSF Final Update Script - Fully Implemented Version" log_message "INFO" "Security Level: $SECURITY_LEVEL" log_message "INFO" "Dry Run Mode: $DRY_RUN_MODE" log_message "INFO" "Force Update: $FORCE_UPDATE" log_message "INFO" "Security Features:" log_message "INFO" " - SHA256 hash verification (with MD5 fallback)" log_message "INFO" " - GPG signature verification (if available)" log_message "INFO" " - Security enforcement: $SECURITY_LEVEL" log_message "INFO" " - Mirror failover: $([ -n "$BACKUP_MIRRORS" ] && echo "Enabled" || echo "Disabled")" log_message "INFO" "Starting CSF migration process..." # Returns 0 when the numeric part of version $1 is strictly greater than $2. # Channel labels (16.33-beta) are ignored, so equal numbers never count as # newer. EACH operand must be a plain dotted number (16.33, 16.33.1); anything # else - empty, ".", "16..33", an HTML error page - never counts as newer, so a # malformed remote value can neither block nor force an update. version_is_newer() { local left="${1%%-*}" right="${2%%-*}" v for v in "$left" "$right"; do case "$v" in ''|*[!0-9.]*|.*|*.|*..*) return 1 ;; esac done awk -v a="$left" -v b="$right" 'BEGIN { na = split(a, x, "."); nb = split(b, y, ".") n = (na > nb) ? na : nb for (i = 1; i <= n; i++) { xi = (i <= na) ? x[i] + 0 : 0 yi = (i <= nb) ? y[i] + 0 : 0 if (xi > yi) exit 0 if (xi < yi) exit 1 } exit 1 }' } # Function to check if remote CSF is newer than local needs_csf_update() { local csf_file="/svr-setup/csf.tgz" # Check force update flag FIRST if [[ "$FORCE_UPDATE" = "true" ]]; then log_message "INFO" "Force update requested - skipping version check" return 0 fi # Check force reinstall flag if [[ "$FORCE_REINSTALL" = "true" ]]; then log_message "INFO" "Force reinstall requested - proceeding with installation" return 0 fi # Normalize mirror URL for CSF package downloads local base_url="$MIRROR_URL" # Remove trailing /csf if present to avoid double /csf base_url="${base_url%/csf}" # Remove trailing slash base_url="${base_url%/}" local csf_url="${base_url}/csf/csf.tgz" local temp_file="/tmp/csf_temp_$$.tgz" # Never downgrade. The file checks below compare /svr-setup/csf.tgz with # the stable channel by timestamp/size/hash only, so a host already running # a NEWER build (a -beta from csf -ub, or a beta initial install) would be # "updated" back down to stable - csfcf.sh auto runs this on every initial # install and twice daily from cron. Skip when the installed numeric version # is above the mirror's stable version; equal numbers fall through so a # -beta build is still promoted by its same-numbered stable and same-version # rebuilds keep the existing file comparison. Deliberately NOT covered: an # explicit csf -ud rollback (installed < stable) is still re-upgraded by the # next run, exactly as before, and the two forced-migration branches below # (wrong mirror / no downloadservers file) never reach this function. # The two version.txt probes use a short timeout of their own: a # black-holed mirror must not stall a cron run for 2 x MIRROR_TIMEOUT. local installed_ver remote_ver probe_timeout=30 installed_ver=$(head -n 1 /etc/csf/version.txt 2>/dev/null | tr -d '[:space:]') if [ -n "$installed_ver" ]; then remote_ver=$(curl -s -f --max-time "$probe_timeout" -A "$FINAL_AGENT" "${base_url}/csf/version.txt" 2>/dev/null | tr -d '[:space:]') if [ -z "$remote_ver" ]; then remote_ver=$(curl -s -f --http1.1 --max-time "$probe_timeout" -A "$FINAL_AGENT" "${base_url}/csf/version.txt" 2>/dev/null | tr -d '[:space:]') fi if version_is_newer "$installed_ver" "$remote_ver"; then log_message "INFO" "Installed CSF $installed_ver is newer than mirror stable $remote_ver - keeping installed version, no update needed" return 1 fi fi # If local file doesn't exist, update needed if [ ! -f "$csf_file" ]; then log_message "INFO" "Local csf.tgz not found - update needed" return 0 fi # Try HTTP HEAD request first to check Last-Modified log_message "DEBUG" "Checking remote CSF timestamp..." local remote_date=$(curl -sI --max-time "$MIRROR_TIMEOUT" -A "$FINAL_AGENT" "$csf_url" | grep -i "^last-modified:" | cut -d' ' -f2- | tr -d '\r') if [ -z "$remote_date" ]; then # Retry forcing HTTP/1.1 remote_date=$(curl -sI --http1.1 --max-time "$MIRROR_TIMEOUT" -A "$FINAL_AGENT" "$csf_url" | grep -i "^last-modified:" | cut -d' ' -f2- | tr -d '\r') fi if [ -n "$remote_date" ]; then # Convert remote date to epoch local remote_epoch=$(date -d "$remote_date" +%s 2>/dev/null) local local_epoch=$(stat -c %Y "$csf_file" 2>/dev/null || echo 0) if [ -n "$remote_epoch" ] && [ "$remote_epoch" -gt 0 ] && [ "$local_epoch" -gt 0 ]; then if [ $remote_epoch -gt $local_epoch ]; then log_message "INFO" "Remote CSF is newer ($(date -d "@$remote_epoch")) than local ($(date -d "@$local_epoch")) - update needed" return 0 else log_message "INFO" "Local CSF is up-to-date - no update needed" return 1 fi fi fi # Fallback: download temp file and compare sizes/checksums log_message "DEBUG" "HTTP HEAD check failed, downloading temp file for comparison..." if curl -s -f --max-time "$MIRROR_TIMEOUT" -A "$FINAL_AGENT" -o "$temp_file" "$csf_url"; then local local_size=$(stat -c %s "$csf_file" 2>/dev/null || echo 0) local remote_size=$(stat -c %s "$temp_file" 2>/dev/null || echo 0) if [ $local_size -ne $remote_size ]; then log_message "INFO" "File sizes differ (local: $local_size, remote: $remote_size) - update needed" rm -f "$temp_file" return 0 fi # Compare SHA256 checksums if sizes are the same log_message "DEBUG" "File sizes match, comparing SHA256 checksums..." local local_sha256="" local remote_sha256="" # Calculate local file SHA256 with fallback methods if command -v sha256sum >/dev/null 2>&1; then local_sha256=$(sha256sum "$csf_file" 2>/dev/null | awk '{print $1}') elif command -v shasum >/dev/null 2>&1; then local_sha256=$(shasum -a 256 "$csf_file" 2>/dev/null | awk '{print $1}') elif command -v openssl >/dev/null 2>&1; then local_sha256=$(openssl dgst -sha256 "$csf_file" 2>/dev/null | awk '{print $NF}') fi # Calculate remote file SHA256 with same fallback methods if command -v sha256sum >/dev/null 2>&1; then remote_sha256=$(sha256sum "$temp_file" 2>/dev/null | awk '{print $1}') elif command -v shasum >/dev/null 2>&1; then remote_sha256=$(shasum -a 256 "$temp_file" 2>/dev/null | awk '{print $1}') elif command -v openssl >/dev/null 2>&1; then remote_sha256=$(openssl dgst -sha256 "$temp_file" 2>/dev/null | awk '{print $NF}') fi if [ -n "$local_sha256" ] && [ -n "$remote_sha256" ]; then if [ "$local_sha256" != "$remote_sha256" ]; then log_message "INFO" "SHA256 checksums differ - update needed" log_message "DEBUG" "Local SHA256: $local_sha256" log_message "DEBUG" "Remote SHA256: $remote_sha256" rm -f "$temp_file" return 0 else log_message "INFO" "SHA256 checksums match - files are identical" rm -f "$temp_file" return 1 fi else log_message "WARN" "Could not calculate SHA256 checksums, falling back to MD5..." # Fallback to MD5 for older systems local local_md5=$(md5sum "$csf_file" 2>/dev/null | awk '{print $1}') local remote_md5=$(md5sum "$temp_file" 2>/dev/null | awk '{print $1}') if [ "$local_md5" != "$remote_md5" ]; then log_message "INFO" "MD5 checksums differ - update needed" rm -f "$temp_file" return 0 else log_message "INFO" "MD5 checksums match - files are identical" rm -f "$temp_file" return 1 fi fi else # Fallback forcing HTTP/1.1 due to potential HTTP/2 issues if curl -s -f --http1.1 --max-time "$MIRROR_TIMEOUT" -A "$FINAL_AGENT" -o "$temp_file" "$csf_url"; then local local_size=$(stat -c %s "$csf_file" 2>/dev/null || echo 0) local remote_size=$(stat -c %s "$temp_file" 2>/dev/null || echo 0) if [ $local_size -ne $remote_size ]; then log_message "INFO" "File sizes differ (local: $local_size, remote: $remote_size) - update needed" rm -f "$temp_file" return 0 fi log_message "DEBUG" "File sizes match, comparing SHA256 checksums..." local local_sha256="" local remote_sha256="" if command -v sha256sum >/dev/null 2>&1; then local_sha256=$(sha256sum "$csf_file" 2>/dev/null | awk '{print $1}') elif command -v shasum >/dev/null 2>&1; then local_sha256=$(shasum -a 256 "$csf_file" 2>/dev/null | awk '{print $1}') elif command -v openssl >/dev/null 2>&1; then local_sha256=$(openssl dgst -sha256 "$csf_file" 2>/dev/null | awk '{print $NF}') fi if command -v sha256sum >/dev/null 2>&1; then remote_sha256=$(sha256sum "$temp_file" 2>/dev/null | awk '{print $1}') elif command -v shasum >/dev/null 2>&1; then remote_sha256=$(shasum -a 256 "$temp_file" 2>/dev/null | awk '{print $1}') elif command -v openssl >/dev/null 2>&1; then remote_sha256=$(openssl dgst -sha256 "$temp_file" 2>/dev/null | awk '{print $NF}') fi if [ -n "$local_sha256" ] && [ -n "$remote_sha256" ]; then if [ "$local_sha256" != "$remote_sha256" ]; then log_message "INFO" "SHA256 checksums differ - update needed" log_message "DEBUG" "Local SHA256: $local_sha256" log_message "DEBUG" "Remote SHA256: $remote_sha256" rm -f "$temp_file" return 0 else log_message "INFO" "SHA256 checksums match - files are identical" rm -f "$temp_file" return 1 fi else log_message "WARN" "Could not calculate SHA256 checksums, falling back to MD5..." local local_md5=$(md5sum "$csf_file" 2>/dev/null | awk '{print $1}') local remote_md5=$(md5sum "$temp_file" 2>/dev/null | awk '{print $1}') if [ "$local_md5" != "$remote_md5" ]; then log_message "INFO" "MD5 checksums differ - update needed" rm -f "$temp_file" return 0 else log_message "INFO" "MD5 checksums match - files are identical" rm -f "$temp_file" return 1 fi fi else log_message "WARN" "Failed to download temp file for comparison" rm -f "$temp_file" return 1 fi fi } # Function to check if CSF is active is_csf_active() { if command -v systemctl >/dev/null 2>&1; then # systemd systems (EL7+) systemctl is-active csf >/dev/null 2>&1 else # SysV systems (CentOS 6) service csf status >/dev/null 2>&1 fi } # Function to restart CSF with backup config fallback restart_csf() { local backup_config="$1" if is_csf_active; then log_message "INFO" "CSF is active - restarting with 'csf -ra'" if simulate_action "csf -ra"; then csf -ra 2>&1 | tee -a "$CSF_UPDATE_LOG" # Wait a moment for CSF to fully restart sleep 10 # Check if CSF is still active after restart if is_csf_active; then log_message "INFO" "CSF restart completed successfully" else log_message "ERROR" "CSF restart failed - attempting to restore backup config" if [ -f "$backup_config" ]; then log_message "INFO" "Restoring backup config: $backup_config" cp "$backup_config" /etc/csf/csf.conf log_message "INFO" "Backup config restored, attempting CSF restart again" csf -ra 2>&1 | tee -a "$CSF_UPDATE_LOG" sleep 10 if is_csf_active; then log_message "INFO" "CSF restart with backup config succeeded" else log_message "ERROR" "CSF restart with backup config also failed - manual intervention required" exit 7 # CSF restart failure fi else log_message "ERROR" "Backup config file not found: $backup_config - manual intervention required" exit 7 fi fi fi else log_message "INFO" "CSF is not active - skipping restart" fi } # Function to download security files for package verification download_security_files() { local base_url="$1" local package_name="$2" local download_dir="$3" log_message "INFO" "Downloading security files for package verification..." # List of security files to download (bash 3.2 compatible) local security_file_1="${package_name}.sha256" local security_file_2="${package_name}.sig" local security_file_3="${package_name}.checksums" local security_file_4="csf-signing-key.asc" local downloaded_count=0 local total_files=4 # Build mirror list for failover (primary + backups) local all_mirrors=("$MIRROR_URL") if [[ -n "$BACKUP_MIRRORS" ]]; then IFS=',' read -ra _backup_array <<< "$BACKUP_MIRRORS" all_mirrors+=("${_backup_array[@]}") fi # Download each security file individually (bash 3.2 compatible) for file_num in 1 2 3 4; do local file_var="security_file_${file_num}" eval "file=\$${file_var}" local local_path="${download_dir}/${file}" log_message "DEBUG" "Downloading security file: $file" if simulate_action "download security file $file"; then local downloaded=false # Try each mirror without aborting the script on failure for mirror in "${all_mirrors[@]}"; do local _base="$mirror" _base="${_base%/csf}" _base="${_base%/}" local url="${_base}/csf/${file}" log_message "INFO" "Attempting security file from: $url" if curl -s -f --max-time "$MIRROR_TIMEOUT" -A "$FINAL_AGENT" -o "$local_path" "$url"; then if [ -s "$local_path" ]; then log_message "INFO" "Successfully downloaded: $file" downloaded=true break else log_message "WARN" "Downloaded file is empty from mirror: $url" rm -f "$local_path" fi else log_message "WARN" "Failed to download from mirror: $url. Retrying with --http1.1" if curl -s -f --http1.1 --max-time "$MIRROR_TIMEOUT" -A "$FINAL_AGENT" -o "$local_path" "$url"; then if [ -s "$local_path" ]; then log_message "INFO" "Successfully downloaded (HTTP/1.1): $file" downloaded=true break else log_message "WARN" "Downloaded file is empty from mirror (HTTP/1.1): $url" rm -f "$local_path" fi fi fi done if [[ "$downloaded" = "true" ]]; then ((downloaded_count++)) else log_message "WARN" "All mirrors failed for security file: $file" fi fi done log_message "INFO" "Downloaded $downloaded_count of $total_files security files" # Handle custom trust key if provided as a URL or local path if [[ -n "$TRUSTED_KEYS" ]]; then if [[ "$TRUSTED_KEYS" =~ ^https?:// ]]; then local custom_key_path="${download_dir}/$(basename "$TRUSTED_KEYS")" log_message "INFO" "Downloading custom trust key: $TRUSTED_KEYS" if simulate_action "curl -o $custom_key_path $TRUSTED_KEYS"; then if curl -s --max-time "$MIRROR_TIMEOUT" -A "$FINAL_AGENT" -o "$custom_key_path" "$TRUSTED_KEYS"; then if [ -s "$custom_key_path" ]; then log_message "INFO" "Custom trust key saved to: $custom_key_path" TRUSTED_KEYS="$custom_key_path" else log_message "WARN" "Downloaded custom trust key is empty: $TRUSTED_KEYS" rm -f "$custom_key_path" fi else log_message "WARN" "Failed to download custom trust key: $TRUSTED_KEYS" fi fi else if [ -f "$TRUSTED_KEYS" ]; then log_message "INFO" "Using local custom trust key: $TRUSTED_KEYS" else log_message "WARN" "Custom trust key path not found: $TRUSTED_KEYS" fi fi fi return 0 } # Function to verify package security using available methods verify_package_security() { local package_file="$1" local download_dir="$2" local package_name=$(basename "$package_file") log_message "INFO" "Starting security verification for: $package_name" local verification_passed=true local verification_methods=0 local verification_succeeded=0 local hash_passed=false local gpg_passed=false # Check 1: SHA256 hash verification (unless skipped) if [[ "$SKIP_HASH" != "true" ]]; then local sha256_file="${download_dir}/${package_name}.sha256" if [ -f "$sha256_file" ]; then log_message "INFO" "Performing SHA256 hash verification..." ((verification_methods++)) local expected_hash=$(cat "$sha256_file" 2>/dev/null | awk '{print $1}') if [ -n "$expected_hash" ]; then # Try multiple hash calculation methods for compatibility local actual_hash="" if command -v sha256sum >/dev/null 2>&1; then actual_hash=$(sha256sum "$package_file" 2>/dev/null | awk '{print $1}') elif command -v shasum >/dev/null 2>&1; then actual_hash=$(shasum -a 256 "$package_file" 2>/dev/null | awk '{print $1}') elif command -v openssl >/dev/null 2>&1; then actual_hash=$(openssl dgst -sha256 "$package_file" 2>/dev/null | awk '{print $NF}') fi if [ -n "$actual_hash" ] && [ "$expected_hash" = "$actual_hash" ]; then log_message "INFO" "SHA256 verification PASSED: $expected_hash" ((verification_succeeded++)) hash_passed=true else log_message "ERROR" "SHA256 verification FAILED" log_message "ERROR" "Expected: $expected_hash" log_message "ERROR" "Actual: $actual_hash" verification_passed=false fi else log_message "WARN" "Could not read expected hash from $sha256_file" fi else log_message "WARN" "SHA256 hash file not found: $sha256_file" fi else log_message "INFO" "SHA256 verification skipped (--skip-hash)" fi # Check 2: GPG signature verification (unless skipped) if [[ "$SKIP_GPG" != "true" ]]; then local sig_file="${download_dir}/${package_name}.sig" local key_file="${download_dir}/csf-signing-key.asc" # Use custom key if specified if [[ -n "$TRUSTED_KEYS" ]]; then key_file="$TRUSTED_KEYS" fi if [ -f "$sig_file" ]; then log_message "INFO" "Performing GPG signature verification..." ((verification_methods++)) # Hardened mode (CSF_GPG_HARDEN=1) can flip this to false on a # fingerprint mismatch; see the pin block below. local hardened_key_ok=true # Check for GPG availability local gpg_cmd="" if command -v gpg2 >/dev/null 2>&1; then gpg_cmd="gpg2" elif command -v gpg >/dev/null 2>&1; then gpg_cmd="gpg" fi if [ -n "$gpg_cmd" ]; then # T8: verification runs in a THROWAWAY keyring. The downloaded # key is never added to root's persistent keyring, and --verify # reads only that keyring, so a key that happens to already be # in root's keyring can no longer vouch for the package. The # temp keyring is also what makes fingerprint extraction work # on EL6/EL7: --show-keys is gpg 2.1.23+, but # "--fingerprint --with-colons --list-keys" against an isolated # homedir works from gpg 1.4 upward. local gpg_home="" local gpg_isolated=false if [ -f "$key_file" ]; then gpg_home=$(mktemp -d "${TMPDIR:-/tmp}/csf-gpgverify.XXXXXX" 2>/dev/null) if [ -z "$gpg_home" ] || [ ! -d "$gpg_home" ]; then log_message "ERROR" "Could not create a temporary GPG keyring directory - refusing to verify" hardened_key_ok=false else chmod 700 "$gpg_home" 2>/dev/null gpg_isolated=true log_message "INFO" "Importing GPG public key into temporary keyring: $key_file" if ! $gpg_cmd --homedir "$gpg_home" --batch --no-tty --import "$key_file" >/dev/null 2>&1; then log_message "ERROR" "GPG public key import failed: $key_file - refusing to verify" hardened_key_ok=false fi fi # T8: fail-closed fingerprint gate. EXACTLY ONE non-empty # primary-key fingerprint must come out of the temporary # keyring. Zero (extraction failed / empty key file) or more # than one (multi-key file: only the first would ever be # pinned while all of them could verify) is a hard failure, # never a skip. Only the fpr record that follows a pub # record is counted - gpg 2.x also emits fpr records for # subkeys. if [ "$hardened_key_ok" = "true" ]; then local fpr_out="" local fpr_count=0 local key_fingerprint="" fpr_out=$($gpg_cmd --homedir "$gpg_home" --batch --no-tty --fingerprint --with-colons --list-keys 2>/dev/null | awk -F: ' /^pub:/ {pub=1; next} /^fpr:/ {if (pub) {n++; if (n==1) {fp=$10}; pub=0}; next} {pub=0} END {printf "%d %s\n", n+0, fp}') fpr_count=$(echo "$fpr_out" | awk '{print $1}') key_fingerprint=$(echo "$fpr_out" | awk '{print $2}') if [ "$fpr_count" != "1" ] || [ -z "$key_fingerprint" ]; then log_message "ERROR" "Expected exactly one GPG signing key in $key_file, found ${fpr_count:-0} usable fingerprint(s) - refusing to verify" hardened_key_ok=false elif [[ "$GPG_HARDEN" = "1" ]]; then # TOFU fingerprint pin (CSF_GPG_HARDEN=1 only): the # first hardened run records the fingerprint and # later runs fail closed on any mismatch - a # signature is meaningless if the key itself changed. local pin_file="/var/lib/csf/gpg-key.fingerprint" if [ -f "$pin_file" ]; then local pinned="" pinned=$(tr -d ' \t\r\n' < "$pin_file" 2>/dev/null) if [ -z "$pinned" ]; then log_message "ERROR" "GPG fingerprint pin $pin_file is empty or unreadable - refusing to verify" hardened_key_ok=false elif [ "$pinned" != "$key_fingerprint" ]; then log_message "ERROR" "GPG key fingerprint mismatch: pinned $pinned vs imported $key_fingerprint - refusing to verify" hardened_key_ok=false fi else mkdir -p /var/lib/csf 2>/dev/null if echo "$key_fingerprint" > "$pin_file" 2>/dev/null; then chmod 600 "$pin_file" 2>/dev/null log_message "INFO" "Pinned GPG signing key fingerprint to $pin_file" else log_message "ERROR" "Could not write GPG fingerprint pin $pin_file - refusing to verify" hardened_key_ok=false fi fi fi fi elif [[ "$GPG_HARDEN" = "1" ]]; then # Hardened mode has nothing to pin without a key file, and # falling back to root's persistent keyring is exactly the # hole this closes. log_message "ERROR" "GPG public key not found: $key_file - CSF_GPG_HARDEN=1 refuses to verify against the system keyring" hardened_key_ok=false else # Dormant/legacy path unchanged: no key file shipped, verify # against whatever the admin already trusts. log_message "WARN" "GPG public key not found: $key_file - falling back to the system keyring" fi # Verify signature. With a key file present this reads ONLY the # temporary keyring, so the pinned key is the only key that can # produce a PASS. local gpg_verify_ok=false if [[ "$hardened_key_ok" = "true" ]]; then if [ "$gpg_isolated" = "true" ]; then if $gpg_cmd --homedir "$gpg_home" --batch --no-tty --verify "$sig_file" "$package_file" >/dev/null 2>&1; then gpg_verify_ok=true fi else if $gpg_cmd --verify "$sig_file" "$package_file" >/dev/null 2>&1; then gpg_verify_ok=true fi fi fi # Tear the throwaway keyring down (gpg 2.1+ leaves an agent # holding sockets in it; the kill is a no-op elsewhere) if [ "$gpg_isolated" = "true" ] && [ -n "$gpg_home" ]; then if command -v gpgconf >/dev/null 2>&1; then GNUPGHOME="$gpg_home" gpgconf --kill all >/dev/null 2>&1 fi rm -rf "$gpg_home" fi if [[ "$gpg_verify_ok" = "true" ]]; then log_message "INFO" "GPG signature verification PASSED" ((verification_succeeded++)) gpg_passed=true else log_message "ERROR" "GPG signature verification FAILED" verification_passed=false fi else log_message "WARN" "GPG not available for signature verification" fi else log_message "WARN" "GPG signature file not found: $sig_file" fi else log_message "INFO" "GPG verification skipped (--skip-gpg)" fi # Summary log_message "INFO" "Security verification summary: $verification_succeeded/$verification_methods methods passed" if [ $verification_methods -eq 0 ]; then log_message "WARN" "No security verification methods available" # Enforce policy for levels that require verification enforce_security 1 false false return 1 elif [ "$verification_passed" = true ] && [ $verification_succeeded -gt 0 ]; then log_message "INFO" "Security verification PASSED - package appears authentic" return 0 else log_message "ERROR" "Security verification FAILED - package may be compromised" # Enforce security policy enforce_security 1 "$gpg_passed" "$hash_passed" return 1 fi } # Centmin Mod cmupdate on git < 1.8.4 (CentOS 6 git 1.7.1, CentOS 7 git 1.8.3). # inc/git_sync.inc shipped 2026-05-19 to 2026-10-25 runs # `git fetch --prune origin "$branch"`, which before git 1.8.4 only writes # FETCH_HEAD: origin/<branch> never moves, so cmupdate keeps reporting # "Already up to date" and never pulls the fixed git_sync.inc. Refresh that # tracking ref here so the next cmupdate run sees the new commits. Fetch only: # HEAD, the working tree and local changes are never touched. Skips itself on # non-Centmin Mod servers, on git 1.8.4+ and once the fixed git_sync.inc is in # place. Always returns 0, so it never affects the CSF update. cmm_cmupdate_oldgit_fetch() { local dir="$1" git_ver major minor patch rest head branch [ -d "$dir/.git" ] && [ -f "$dir/inc/git_sync.inc" ] || return 0 # shellcheck disable=SC2016 # literal $branch: the old fetch line itself grep -qF 'git fetch --prune origin "$branch"' "$dir/inc/git_sync.inc" || return 0 git_ver=$(git --version 2>/dev/null | awk '{print $3}') IFS=. read -r major minor patch rest <<< "$git_ver" [[ "$major" =~ ^[0-9]+$ && "$minor" =~ ^[0-9]+$ && "$patch" =~ ^[0-9]+$ ]] || return 0 (( major < 1 || (major == 1 && (minor < 8 || (minor == 8 && patch < 4))) )) || return 0 # Same ref the old sync compares HEAD with: it sets branch.<head>.merge on every run head=$(git --git-dir="$dir/.git" symbolic-ref -q HEAD 2>/dev/null) head=${head#refs/heads/} [ -n "$head" ] && [ "$(git --git-dir="$dir/.git" config "branch.${head}.remote")" = origin ] || return 0 branch=$(git --git-dir="$dir/.git" config "branch.${head}.merge") branch=${branch#refs/heads/} [[ "$branch" =~ ^[A-Za-z0-9._/-]+$ ]] || return 0 if ! command -v timeout >/dev/null 2>&1; then log_message "WARN" "Centmin Mod: timeout not found - skipped refreshing origin/$branch for cmupdate (git $git_ver)" return 0 fi simulate_action "git fetch origin $branch in $dir" || return 0 if ( cd "$dir" && timeout 60 git fetch origin "+refs/heads/${branch}:refs/remotes/origin/${branch}" ) </dev/null >/dev/null 2>&1; then log_message "INFO" "Centmin Mod: refreshed origin/$branch so cmupdate can pull the git_sync.inc fix (git $git_ver)" else log_message "WARN" "Centmin Mod: could not refresh origin/$branch for cmupdate (git $git_ver)" fi return 0 } cmm_cmupdate_oldgit_fetch /usr/local/src/centminmod # OS info OS_PRETTY_NAME=$([ -f /etc/os-release ] && awk -F'=' '/^PRETTY_NAME=/ {gsub(/["()]/, "", $2); gsub(/ Core /, " ", $2); print $2}' /etc/os-release || echo "CentOS 6.10 Final") # Build user agent components CURL_AGENT_VERSION=$(curl -V 2>/dev/null | awk 'NR==1 {print $1"/"$2}') CURL_CPUMODEL=$(awk -F: '/^model name/ {gsub(/\(R\)|\(TM\)|CPU |Intel Core/, "", $2); gsub(/-Core/, "C", $2); gsub(/@ /, "@", $2); gsub(/^ +| +$/, "", $2); print $2; exit}' /proc/cpuinfo 2>/dev/null) CURL_CPUSPEED=$(awk -F: '/^cpu MHz/ {gsub(/ /, "", $2); speeds[++count] = $2} END {if (count > 0) {sum = 0; for (i = 1; i <= count; i++) sum += speeds[i]; printf("%.0f", sum/count)}}' /proc/cpuinfo 2>/dev/null) CSFUPDATE_VIRTWHAT=$(command -v virt-what >/dev/null 2>&1 && virt-what 2>/dev/null || echo "d") # Get geo info for tracking adoption USER_AGENT="$CURL_AGENT_VERSION $OS_PRETTY_NAME $SCRIPT_VERSION $CURL_CPUMODEL ${CURL_CPUSPEED}MHz $CSFUPDATE_VIRTWHAT" GEOIP=$(curl -s --max-time 10 -A "$USER_AGENT" https://geoip.centminmod.com/v4 2>/dev/null) if echo "$GEOIP" | jq -e . >/dev/null 2>&1; then CITY=$(echo "$GEOIP" | jq -r '.city // "unknown"') COLO=$(echo "$GEOIP" | jq -r '.colo // "unknown"') else CITY="unknown"; COLO="unknown" fi # Build final agent for file checking FINAL_AGENT="${USER_AGENT:-CSF-Update} ${CITY:-unknown} ${COLO:-unknown}" # Check if update is needed CSF_CURRENT_URL=$(head -n1 /etc/csf/downloadservers 2>/dev/null) NEW_MIRROR="download.centminmod.com" UPDATE_NEEDED=false # Check mirror update requirement if [[ "$CSF_CURRENT_URL" != *"$NEW_MIRROR"* ]] && [ -f /etc/csf/downloadservers ]; then log_message "INFO" "CSF mirror update needed - current URL: $CSF_CURRENT_URL" UPDATE_NEEDED=true elif [ ! -f /etc/csf/downloadservers ]; then log_message "INFO" "CSF install not found" UPDATE_NEEDED=true else log_message "INFO" "CSF already using correct mirror: $CSF_CURRENT_URL" # Check if remote CSF is newer than local if needs_csf_update; then log_message "INFO" "Remote CSF file is newer - update needed" UPDATE_NEEDED=true fi fi # Perform update if needed if [ "$UPDATE_NEEDED" = true ]; then if simulate_action "mkdir -p /svr-setup && cd /svr-setup"; then mkdir -p /svr-setup && cd /svr-setup || exit 1 fi # Backup existing CSF config CSF_CONFIG_BACKUPFILE="csf.conf-$(date +%Y%m%d-%H%M%S)" CSF_CONFIG_BACKUP_PATH="/etc/csf/${CSF_CONFIG_BACKUPFILE}" if [ -f /etc/csf/csf.conf ]; then if simulate_action "cp -a /etc/csf/csf.conf $CSF_CONFIG_BACKUP_PATH"; then cp -a /etc/csf/csf.conf "$CSF_CONFIG_BACKUP_PATH" || { log_message "ERROR" "Failed to backup CSF configuration" exit 6 # Configuration backup failure } log_message "INFO" "CSF config backed up to: $CSF_CONFIG_BACKUP_PATH" # Also create CSF profile backup if simulate_action "csf --profile backup"; then csf --profile backup "csf-config-backup-$(date +%Y%m%d-%H%M%S)" 2>&1 | tee -a "$CSF_UPDATE_LOG" fi fi fi # Backup existing CSF tarball if [ -f csf.tgz ]; then if simulate_action "cp csf.tgz to backup"; then \cp -af csf.tgz "csf-backup-$(date +%Y%m%d-%H%M%S).tgz" fi fi # Always download fresh CSF when update is needed if [ ! -f csf.tgz ] || [ "$UPDATE_NEEDED" = true ]; then log_message "INFO" "Downloading fresh CSF from mirrors" # Use download_with_failover for robust downloading download_with_failover "csf.tgz" "csf.tgz" # Download security files for package verification using configurable mirror security_base_url="$MIRROR_URL" # Remove trailing /csf if present to avoid double /csf security_base_url="${security_base_url%/csf}" # Remove trailing slash security_base_url="${security_base_url%/}" download_security_files "${security_base_url}/csf" "csf.tgz" "/svr-setup" # Verify package security before installation log_message "INFO" "Performing security verification of downloaded package..." if verify_package_security "/svr-setup/csf.tgz" "/svr-setup"; then log_message "INFO" "Package security verification completed successfully" else log_message "ERROR" "Package security verification failed" # Security enforcement already handled in verify_package_security fi else log_message "INFO" "Using previously downloaded CSF file" log_message "INFO" "Note: Security verification skipped for existing file" fi # Clean up old extracted directory to ensure clean installation if [ -d csf ]; then if simulate_action "rm -rf csf"; then log_message "INFO" "Removing old extracted CSF directory" rm -rf csf fi fi # Install new CSF with full logging if simulate_action "tar -xzf csf.tgz && install"; then if tar -xzf csf.tgz && cd csf && [ -f install.sh ]; then mkdir -p /root/centminlogs CSF_LOG="/root/centminlogs/csf_final_update_csf_install_$(date +%Y%m%d_%H%M%S).log" log_message "INFO" "Starting CSF installation - logging to $CSF_LOG" bash install.sh 2>&1 | tee "$CSF_LOG" csf_install_status=${PIPESTATUS[0]} if [ "$csf_install_status" -ne 0 ]; then log_message "ERROR" "CSF installation failed (install.sh exit $csf_install_status) - see $CSF_LOG" exit 5 # CSF installation failure fi log_message "INFO" "CSF installation completed - full log: $CSF_LOG" # Restart CSF with backup fallback capability restart_csf "$CSF_CONFIG_BACKUP_PATH" # Verify installation with 4 verification commands log_message "INFO" "Verifying CSF migration to centminmod mirrors..." # Check 1: CSF version log_message "INFO" "Verification 1 - CSF Version:" csf -V 2>&1 | tee -a "$CSF_UPDATE_LOG" # Check 2: Search for centminmod domains in CSF files log_message "INFO" "Verification 2 - Centminmod domain references:" grep -rin 'download.centminmod.com' /etc/csf /usr/local/csf/ 2>/dev/null | tee -a "$CSF_UPDATE_LOG" # Check 3: Verify downloadservers file content log_message "INFO" "Verification 3 - Download servers configuration:" if [ -f /etc/csf/downloadservers ]; then echo "Contents of /etc/csf/downloadservers:" >> "$CSF_UPDATE_LOG" cat /etc/csf/downloadservers 2>&1 | tee -a "$CSF_UPDATE_LOG" else log_message "ERROR" "/etc/csf/downloadservers not found!" fi # Check 4: Search current update log for centminmod references log_message "INFO" "Verification 4 - Update log centminmod references:" grep -rin 'download.centminmod.com' "$CSF_UPDATE_LOG" 2>/dev/null | head -5 | tee -a "$CSF_UPDATE_LOG" log_message "INFO" "CSF migration verification completed" # Clean up security files after successful installation if simulate_action "cleanup security files"; then log_message "INFO" "Cleaning up security files..." rm -f /svr-setup/csf.tgz.sha256 /svr-setup/csf.tgz.sig /svr-setup/csf.tgz.checksums /svr-setup/csf-signing-key.asc log_message "INFO" "Security files cleanup completed" fi else log_message "ERROR" "Failed to extract csf.tgz, enter the csf directory, or locate install.sh - CSF was NOT installed" exit 5 # CSF installation failure fi fi CSF_UPDATED_URL=$(head -n1 /etc/csf/downloadservers 2>/dev/null) log_message "INFO" "CSF updated successfully to mirror: ${CSF_UPDATED_URL}" log_message "INFO" "CSF migration completed successfully" fi # Remove ConfigServer IPs from CSF allow list and block them (runs regardless of update) if command -v csf >/dev/null 2>&1; then log_message "INFO" "Starting removal and blocking of ConfigServer IPs in CSF" # Define ConfigServer IPs to remove (bash 3.2 compatible array) # Format: IP_ADDRESS # domain_name CONFIGSERVER_IPS=( "94.130.90.175" # download.configserver.com "54.36.165.115" # download2.configserver.com "66.165.246.166" # license.configserver.com "2604:4500:9:156::6" # ipv6.license.configserver.com ) # Associated domain names for logging purposes CONFIGSERVER_DOMAINS=( "download.configserver.com" "download2.configserver.com" "license.configserver.com" "ipv6.license.configserver.com" ) # Remove each ConfigServer IP from the allow list and add to deny list for i in "${!CONFIGSERVER_IPS[@]}"; do ip="${CONFIGSERVER_IPS[$i]}" domain="${CONFIGSERVER_DOMAINS[$i]}" # Remove from allow list log_message "INFO" "Removing IP $ip ($domain) from CSF allow list" if simulate_action "csf -ar $ip"; then csf -ar "$ip" 2>&1 | tee -a "$CSF_UPDATE_LOG" || { log_message "WARN" "Failed to remove IP $ip ($domain) from allow list" } fi # Add to deny list to block the IP log_message "INFO" "Adding IP $ip ($domain) to CSF deny list" if simulate_action "csf -d $ip block-old-csf-ips"; then csf -d "$ip" block-old-csf-ips 2>&1 | tee -a "$CSF_UPDATE_LOG" || { log_message "WARN" "Failed to add IP $ip ($domain) to deny list" } fi done log_message "INFO" "Completed removal and blocking of ConfigServer IPs" else log_message "WARN" "CSF command not found - skipping ConfigServer IP removal and blocking" fi # Final exit code handling if [[ "$EMERGENCY_BYPASS_USED" = "true" ]]; then log_message "WARN" "Emergency bypass was used - audit flag set" exit 99 fi log_message "INFO" "Script completed successfully" exit 0
Save File
Cancel