set -euo pipefail if [ "$USER" != "guest" ]; then exit 0 fi GUEST_HOME="/home/guest" SKELETON="/home/guest-freeze" # Validate guest home directory: check if it's a symlink if [ -L "$GUEST_HOME" ]; then echo "Home directory is a symlink. Removing it and restoring as a directory." rm -f "$GUEST_HOME" # Remove the symlink # Recreate the home directory as a proper directory mkdir -p "$GUEST_HOME" fi # Validate guest home directory exists and is not a symlink if [ ! -d "$GUEST_HOME" ]; then echo "Unsafe guest home or home directory creation failed." exit 1 fi # Remove contents safely (if any) while ignoring "file not found" errors find "$GUEST_HOME" -mindepth 1 -xdev -exec rm -rf -- {} + 2>/dev/null || true # Restore skeleton safely rsync -a --safe-links --no-devices --no-specials \ "$SKELETON"/ "$GUEST_HOME"/ # Fix ownership and permissions chown -R guest:guest "$GUEST_HOME" chmod 700 "$GUEST_HOME"