add-sftp-user/fix-webroot-permissions.sh

96 lines
3.2 KiB
Bash
Raw Permalink Normal View History

#!/bin/bash
# ==============================================================================
# Standalone script to fix webroot permissions for existing installations
# This ensures /var/www/webroot/ROOT has correct group ownership and permissions
# ==============================================================================
WEBROOT_DIR="/var/www/webroot/ROOT"
echo "=============================================================================="
echo "Webroot Permissions Fix Script"
echo "Fixing permissions for: $WEBROOT_DIR"
echo "=============================================================================="
echo
# Check if webroot exists
if [ ! -d "$WEBROOT_DIR" ]; then
echo "ERROR: Webroot directory does not exist: $WEBROOT_DIR"
exit 1
fi
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "ERROR: This script must be run as root"
echo "Please run: sudo $0"
exit 1
fi
# Check if litespeed group exists
if ! getent group litespeed > /dev/null; then
echo "Creating litespeed group..."
groupadd litespeed
if [ $? -eq 0 ]; then
echo "✓ Created litespeed group"
else
echo "ERROR: Failed to create litespeed group"
exit 1
fi
else
echo "✓ litespeed group exists"
fi
# Check current group ownership
CURRENT_GROUP=$(stat -c '%G' "$WEBROOT_DIR" 2>/dev/null)
echo "Current group ownership: $CURRENT_GROUP"
if [ "$CURRENT_GROUP" != "litespeed" ]; then
echo "Setting group ownership to litespeed..."
chgrp -R litespeed "$WEBROOT_DIR"
if [ $? -eq 0 ]; then
echo "✓ Set group ownership to litespeed"
else
echo "ERROR: Failed to set group ownership"
exit 1
fi
else
echo "✓ Group ownership is already litespeed"
fi
# Check current permissions
CURRENT_PERMS=$(stat -c '%a' "$WEBROOT_DIR" 2>/dev/null)
echo "Current permissions: $CURRENT_PERMS"
# Extract group write bit (second digit)
GROUP_WRITE_BIT=$(echo "$CURRENT_PERMS" | cut -c2)
echo "Group permissions bit: $GROUP_WRITE_BIT"
# Check if write bit is NOT set (4=r--, 5=r-x, 1=--x, 0=---)
if [ "$GROUP_WRITE_BIT" = "4" ] || [ "$GROUP_WRITE_BIT" = "5" ] || [ "$GROUP_WRITE_BIT" = "1" ] || [ "$GROUP_WRITE_BIT" = "0" ]; then
echo "Adding group write permissions (current group bit: $GROUP_WRITE_BIT)..."
chmod -R g+w "$WEBROOT_DIR"
if [ $? -eq 0 ]; then
echo "✓ Added group write permissions"
NEW_PERMS=$(stat -c '%a' "$WEBROOT_DIR" 2>/dev/null)
echo "New permissions: $NEW_PERMS"
else
echo "ERROR: Failed to add group write permissions"
exit 1
fi
else
echo "✓ Group already has write permissions"
fi
echo
echo "=============================================================================="
echo "Verification:"
echo "=============================================================================="
echo "Directory: $WEBROOT_DIR"
echo "Ownership: $(stat -c '%U:%G' "$WEBROOT_DIR")"
echo "Permissions: $(stat -c '%a' "$WEBROOT_DIR") ($(stat -c '%A' "$WEBROOT_DIR"))"
echo
echo "=============================================================================="
echo "Fix complete! Users in the litespeed group should now be able to write."
echo "=============================================================================="