2323# user = "dstack-prd1"
2424# ```
2525#
26+ set -e
2627
2728# Default values
2829USERNAME=" "
2930GROUP_NAME=" "
3031NO_FW=false
32+ NO_SERVICE=false
3133ALLOWED_TCP_PORTS=" "
3234ALLOWED_UDP_PORTS=" "
3335
@@ -38,6 +40,10 @@ while [[ $# -gt 0 ]]; do
3840 NO_FW=true
3941 shift
4042 ;;
43+ --no-svc)
44+ NO_SERVICE=true
45+ shift
46+ ;;
4147 --allow-tcp)
4248 ALLOWED_TCP_PORTS=" $ALLOWED_TCP_PORTS $2 "
4349 shift
@@ -54,9 +60,10 @@ while [[ $# -gt 0 ]]; do
5460 shift
5561 ;;
5662 -h | --help)
57- echo " Usage: $0 <username> [--ufw ] [-g|--group ] [--no-fw ] [--allow-tcp <port> --allow-udp <port>]"
63+ echo " Usage: $0 <username> [-g|--group ] [--no-fw ] [--no-svc ] [--allow-tcp <port> --allow-udp <port>]"
5864 echo " Options:"
5965 echo " --no-fw Do not setup/clear firewall rules"
66+ echo " --no-svc Do not setup/clear service rules"
6067 echo " --allow-tcp Allow the specified TCP port to be accessed"
6168 echo " --allow-udp Allow the specified UDP port to be accessed"
6269 echo " -g, --group Add the user to the specified group"
7986# Check if username is provided
8087if [[ -z " $USERNAME " ]]; then
8188 echo " Error: Username is required"
82- echo " Usage: $0 <username> [--ufw ] [--no-fw] [--allow <port>]"
89+ echo " Usage: $0 <username> [-g|--group ] [--no-fw] [--no-svc] [-- allow-tcp <port> --allow-udp <port>]"
8390 exit 1
8491fi
8592
@@ -100,16 +107,38 @@ if [ -n "$GROUP_NAME" ]; then
100107 usermod -aG $GROUP_NAME $USERNAME
101108fi
102109
110+ rule_nums=$( iptables -L OUTPUT --line-numbers | grep $CHAIN_NAME | awk ' {print $1}' | sort -r)
111+ echo $rule_nums
112+
103113if iptables -L $CHAIN_NAME > /dev/null 2>&1 ; then
104114 echo " Removing existing firewall rules"
105- iptables -D OUTPUT -o lo -m owner --uid-owner $USERNAME -j $CHAIN_NAME 2> /dev/null || true
115+ # Delete each rule (in reverse order to avoid index shifting)
116+ if [ -n " $rule_nums " ]; then
117+ echo " Removing rules jumping to $CHAIN_NAME from OUTPUT chain"
118+ for num in $rule_nums ; do
119+ echo " Removing rule $num "
120+ iptables -D OUTPUT $num
121+ done
122+ echo " All rules jumping to $CHAIN_NAME removed"
123+ else
124+ echo " No rules jumping to $CHAIN_NAME found in OUTPUT chain"
125+ fi
106126 iptables -F $CHAIN_NAME 2> /dev/null || true
107127 iptables -X $CHAIN_NAME 2> /dev/null || true
108128 echo " Removed iptables chain $CHAIN_NAME "
109129fi
110130
111131rm -f $RULES_FILE
112132
133+ if [ " $NO_SERVICE " = true ]; then
134+ echo " Removing existing systemd service"
135+ rm -f /etc/systemd/system/iptables-restore.service
136+ rm -f /etc/iptables/dstack-rules-${USERNAME} .v4
137+ systemctl disable iptables-restore.service || true
138+ systemctl daemon-reload
139+ echo " Removed systemd service and rules file"
140+ fi
141+
113142if [ " $NO_FW " = true ]; then
114143 echo " Skipping firewall rules setup"
115144 exit 0
129158# Add rules to allow specific ports
130159for port in $ALLOWED_TCP_PORTS ; do
131160 echo " Adding exception for TCP port $port "
132- iptables -A $CHAIN_NAME -o lo -d 127.0.0.1 - p tcp --dport $port -j ACCEPT
133- iptables -A $CHAIN_NAME -o lo -d 127.0.0.1 - p tcp --sport $port -j ACCEPT
161+ iptables -A $CHAIN_NAME -p tcp --dport $port -j ACCEPT
162+ iptables -A $CHAIN_NAME -p tcp --sport $port -j ACCEPT
134163done
135164for port in $ALLOWED_UDP_PORTS ; do
136165 echo " Adding exception for UDP port $port "
137- iptables -A $CHAIN_NAME -o lo -d 127.0.0.1 - p udp --dport $port -j ACCEPT
138- iptables -A $CHAIN_NAME -o lo -d 127.0.0.1 - p udp --sport $port -j ACCEPT
166+ iptables -A $CHAIN_NAME -p udp --dport $port -j ACCEPT
167+ iptables -A $CHAIN_NAME -p udp --sport $port -j ACCEPT
139168done
140169
141170# Add final DROP rule for all other traffic to localhost
142- iptables -A $CHAIN_NAME -o lo -d 127.0.0.1 -j DROP
171+ iptables -A $CHAIN_NAME -p udp -j DROP
172+ iptables -A $CHAIN_NAME -p tcp -m tcp --syn -j DROP
173+
143174
144175# Ensure our chain is referenced from the OUTPUT chain
145176if ! iptables -C OUTPUT -o lo -m owner --uid-owner $USERNAME -j $CHAIN_NAME 2> /dev/null; then
146- iptables -I OUTPUT -o lo -m owner --uid-owner $USERNAME -j $CHAIN_NAME
177+ iptables -I OUTPUT -o lo -d 127.0.0.1 -m owner --uid-owner $USERNAME -j $CHAIN_NAME
178+ fi
179+
180+ if [ " $NO_SERVICE " = true ]; then
181+ echo " Skipping service rules setup"
182+ exit 0
147183fi
148184
149185# Make iptables rules persistent
0 commit comments