From 9615073df2e0b8c947b1b9441ec08fe4f3026500 Mon Sep 17 00:00:00 2001 From: Anthony Date: Sat, 22 Mar 2025 01:51:15 +0800 Subject: [PATCH] Fix bash and jps --- mbadmin.jps | 10 ++++- scripts/ssl-manager/ssl_manager.sh | 67 ++++++++++++++++++------------ 2 files changed, 50 insertions(+), 27 deletions(-) diff --git a/mbadmin.jps b/mbadmin.jps index 337e6ab..9c8de72 100644 --- a/mbadmin.jps +++ b/mbadmin.jps @@ -587,7 +587,15 @@ actions: - cmd[cp]: user: root commands: - - bash /home/litespeed/mbmanager/ssl-manager/ssl_manager.sh --domain="${settings.domain}" --remove-cert=yes --confirm=yes + - | + OUTPUT=$(bash /home/litespeed/mbmanager/ssl-manager/ssl_manager.sh --domain="${settings.domain}" --remove-cert=yes --confirm=yes 2>&1) + echo "$OUTPUT" + # Check if certificate was actually removed regardless of script exit code + if echo "$OUTPUT" | grep -q "Certificate for.*successfully revoked and removed"; then + exit 0 + else + exit 1 + fi - if: ${response.exitcode} == 0 return: diff --git a/scripts/ssl-manager/ssl_manager.sh b/scripts/ssl-manager/ssl_manager.sh index 2f586fc..c102735 100644 --- a/scripts/ssl-manager/ssl_manager.sh +++ b/scripts/ssl-manager/ssl_manager.sh @@ -764,15 +764,12 @@ remove_ssl_certificate() { return 1 fi - # Remove the HTTPS-domain listener section - find both and tags to handle inconsistencies - grep -n -E "<(name|n)>HTTPS-${domain}" "$config_file" > /dev/null || { - log "No listener found for domain ${domain}, continuing with certificate removal only."; - } - - awk -v domain="$domain" ' + # Remove the HTTPS-domain listener section + local domain_pattern="HTTPS-${domain}" + awk -v domain="$domain" -v pattern="$domain_pattern" ' BEGIN { skip = 0; } - /HTTPS-'"$domain"'/,/<\/listener>/ { - if ($0 ~ /HTTPS-'"$domain"'/) { + $0 ~ pattern,/<\/listener>/ { + if ($0 ~ pattern) { skip = 1; print ""; } @@ -789,17 +786,14 @@ remove_ssl_certificate() { log "Removing domain-specific virtual host if it exists..." local vhost_name="${domain//[.]/_}" - # Check if virtualhost exists - grep -n -E "<(name|n)>${vhost_name}" "$config_file" > /dev/null || { - log "No virtual host found for ${vhost_name}, skipping this step."; - cp "$temp_file" "${temp_file}.new"; - } - - if [ ! -f "${temp_file}.new" ]; then - awk -v vhost="$vhost_name" ' + # Check if virtualhost exists (safer approach) + if grep -q "$vhost_name" "$config_file"; then + # Process only if virtual host might exist + local vhost_pattern="$vhost_name" + awk -v vhost="$vhost_name" -v pattern="$vhost_pattern" ' BEGIN { skip = 0; } - /'"$vhost"'/,/<\/virtualHost>/ { - if ($0 ~ /'"$vhost"'/) { + $0 ~ pattern,/<\/virtualHost>/ { + if ($0 ~ pattern) { skip = 1; print ""; } @@ -811,6 +805,9 @@ remove_ssl_certificate() { } { if (!skip) print; } ' "$temp_file" > "${temp_file}.new" + else + log "No virtual host found for ${vhost_name}, skipping this step."; + cp "$temp_file" "${temp_file}.new" fi # 4. Remove any domain mappings from shared listeners @@ -899,18 +896,36 @@ remove_ssl_certificate() { # 8. Clean up LiteSpeed logs for this domain log "Cleaning up log files for $domain..." - find /var/log/lsws/ -name "*$domain*" -type f -delete + if [ -d "/var/log/lsws/" ]; then + find /var/log/lsws/ -name "*$domain*" -type f -delete 2>/dev/null || true + else + log "LiteSpeed log directory '/var/log/lsws/' not found, skipping log cleanup." + fi # 9. Clean related cache files log "Cleaning related cache files..." - find /var/www/webroot/ROOT/.well-known/acme-challenge/ -type f -delete 2>/dev/null - - # 10. Restart LiteSpeed to apply configuration changes - if restart_litespeed; then - log "LiteSpeed restarted successfully after certificate removal." + if [ -d "/var/www/webroot/ROOT/.well-known/acme-challenge/" ]; then + find /var/www/webroot/ROOT/.well-known/acme-challenge/ -type f -delete 2>/dev/null || true else - log "ERROR: Failed to restart LiteSpeed after certificate removal." - return 1 + log "ACME challenge directory not found, skipping cache cleanup." + fi + + # 10. Restart LiteSpeed only if it's running and configuration was changed + local config_changed=false + if grep -q "removed by ssl_manager.sh" "$config_file"; then + config_changed=true + fi + + if $config_changed; then + log "Configuration changes detected. Restarting LiteSpeed to apply changes..." + if restart_litespeed; then + log "LiteSpeed restarted successfully after certificate removal." + else + log "WARNING: Failed to restart LiteSpeed after certificate removal. Manual restart may be required." + # Don't return error, continue with the successful certificate removal + fi + else + log "No configuration changes detected. Skipping LiteSpeed restart." fi # 11. Send email notification if configured