|
| 1 | +#!/usr/bin/env bash |
| 2 | +############################################################################### |
| 3 | +# Script: setfirewall.sh |
| 4 | +# Description: Configure UFW firewall rules on Ubuntu systems (e.g., TDX environment) |
| 5 | +# |
| 6 | +# This script enables UFW and allows ports defined in the environment variable |
| 7 | +# `allowPorts`. Supports single ports and port ranges (e.g., "22,80,3000:3010"). |
| 8 | +# |
| 9 | +# Requirements: |
| 10 | +# - Must be run as root |
| 11 | +# - Must run on Ubuntu |
| 12 | +# - iptables and UFW must be installed |
| 13 | +# |
| 14 | +############################################################################### |
| 15 | + |
| 16 | +log_info() { |
| 17 | + echo -e "[INFO] $*" |
| 18 | +} |
| 19 | + |
| 20 | +log_warn() { |
| 21 | + echo -e "[WARN] $*" |
| 22 | +} |
| 23 | + |
| 24 | +log_fatal() { |
| 25 | + echo -e "[ERROR] $*" >&2 |
| 26 | + exit 1 |
| 27 | +} |
| 28 | + |
| 29 | +log_info "Starting firewall configuration..." |
| 30 | + |
| 31 | +# Check if running on Ubuntu |
| 32 | +if ! grep -q "ID=ubuntu" /etc/os-release; then |
| 33 | + log_fatal "This script supports only Ubuntu. Aborting." |
| 34 | +fi |
| 35 | + |
| 36 | +# Load ip_tables module |
| 37 | +log_info "Loading ip_tables module..." |
| 38 | +modprobe ip_tables 2>/dev/null |
| 39 | +if [ $? -ne 0 ]; then |
| 40 | + log_warn "Could not load ip_tables (module missing or already loaded)." |
| 41 | +else |
| 42 | + log_info "ip_tables loaded successfully." |
| 43 | +fi |
| 44 | + |
| 45 | +# Enable UFW |
| 46 | +log_info "Enabling UFW..." |
| 47 | +ufw --force enable >/dev/null 2>&1 |
| 48 | +if [ $? -ne 0 ]; then |
| 49 | + log_fatal "Failed to enable UFW. Please ensure UFW is installed." |
| 50 | +fi |
| 51 | +log_info "UFW enabled." |
| 52 | + |
| 53 | +# Get ports from environment variable |
| 54 | +if [ -z "${allowPorts}" ]; then |
| 55 | + log_info "No ports specified (allowPorts is empty). Skipping rule creation." |
| 56 | +else |
| 57 | + log_info "Allowing ports: ${allowPorts}" |
| 58 | + IFS=',' read -ra PORT_ARRAY <<< "${allowPorts}" |
| 59 | + |
| 60 | + for port in "${PORT_ARRAY[@]}"; do |
| 61 | + port="$(echo "$port" | xargs)" # trim spaces |
| 62 | + if [[ "${port}" == *:* ]]; then |
| 63 | + start="${port%%:*}" |
| 64 | + end="${port##*:}" |
| 65 | + log_info "Allowing port range ${start}:${end}/tcp..." |
| 66 | + ufw allow "${start}:${end}/tcp" >/dev/null 2>&1 |
| 67 | + if [ $? -eq 0 ]; then |
| 68 | + log_info "Range ${start}:${end}/tcp allowed." |
| 69 | + else |
| 70 | + log_warn "Failed to allow range ${start}:${end}/tcp." |
| 71 | + fi |
| 72 | + else |
| 73 | + log_info "Allowing port ${port}/tcp..." |
| 74 | + ufw allow "${port}/tcp" >/dev/null 2>&1 |
| 75 | + if [ $? -eq 0 ]; then |
| 76 | + log_info "Port ${port}/tcp allowed." |
| 77 | + else |
| 78 | + log_warn "Failed to allow port ${port}/tcp." |
| 79 | + fi |
| 80 | + fi |
| 81 | + done |
| 82 | +fi |
| 83 | + |
| 84 | +log_info "Firewall configuration completed." |
0 commit comments