-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirewall_allow.sh
More file actions
141 lines (134 loc) · 4.06 KB
/
firewall_allow.sh
File metadata and controls
141 lines (134 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# scriptlet:_common/get_firewall.sh
##
# Add an "allow" rule to the firewall in the INPUT chain
#
# Arguments:
# --port <port> Port(s) to allow
# --source <source> Source IP to allow (default: any)
# --zone <zone> Zone to allow (default: public)
# --tcp|--udp Protocol to allow (default: tcp)
# --proto <tcp|udp> Protocol to allow (alternative method)
# --comment <comment> (only UFW) Comment for the rule
#
# Specify multiple ports with `--port '#,#,#'` or a range `--port '#:#'`
#
# CHANGELOG:
# 2025.11.23 - Use return codes instead of exit to allow the caller to handle errors
# 2025.04.10 - Add "--proto" argument as alternative to "--tcp|--udp"
#
function firewall_allow() {
# Defaults and argument processing
local PORT=""
local PROTO="tcp"
local SOURCE="any"
local FIREWALL=$(get_available_firewall)
local ZONE="public"
local COMMENT=""
while [ $# -ge 1 ]; do
case $1 in
--port)
shift
PORT=$1
;;
--tcp|--udp)
PROTO=${1:2}
;;
--proto)
shift
PROTO=$1
;;
--source|--from)
shift
SOURCE=$1
;;
--zone)
shift
ZONE=$1
;;
--comment)
shift
COMMENT=$1
;;
*)
PORT=$1
;;
esac
shift
done
if [ "$PORT" == "" -a "$ZONE" != "trusted" ]; then
echo "firewall_allow: No port specified!" >&2
return 2
fi
if [ "$PORT" != "" -a "$ZONE" == "trusted" ]; then
echo "firewall_allow: Trusted zones do not use ports!" >&2
return 2
fi
if [ "$ZONE" == "trusted" -a "$SOURCE" == "any" ]; then
echo "firewall_allow: Trusted zones require a source!" >&2
return 2
fi
if [ "$FIREWALL" == "ufw" ]; then
if [ "$SOURCE" == "any" ]; then
echo "firewall_allow/UFW: Allowing $PORT/$PROTO from any..."
ufw allow proto $PROTO to any port $PORT comment "$COMMENT"
elif [ "$ZONE" == "trusted" ]; then
echo "firewall_allow/UFW: Allowing all connections from $SOURCE..."
ufw allow from $SOURCE comment "$COMMENT"
else
echo "firewall_allow/UFW: Allowing $PORT/$PROTO from $SOURCE..."
ufw allow from $SOURCE proto $PROTO to any port $PORT comment "$COMMENT"
fi
return 0
elif [ "$FIREWALL" == "firewalld" ]; then
if [ "$SOURCE" != "any" ]; then
# Firewalld uses Zones to specify sources
echo "firewall_allow/firewalld: Adding $SOURCE to $ZONE zone..."
firewall-cmd --zone=$ZONE --add-source=$SOURCE --permanent
fi
if [ "$PORT" != "" ]; then
echo "firewall_allow/firewalld: Allowing $PORT/$PROTO in $ZONE zone..."
if [[ "$PORT" =~ ":" ]]; then
# firewalld expects port ranges to be in the format of "#-#" vs "#:#"
local DPORTS="${PORT/:/-}"
firewall-cmd --zone=$ZONE --add-port=$DPORTS/$PROTO --permanent
elif [[ "$PORT" =~ "," ]]; then
# Firewalld cannot handle multiple ports all that well, so split them by the comma
# and run the add command separately for each port
local DPORTS="$(echo $PORT | sed 's:,: :g')"
for P in $DPORTS; do
firewall-cmd --zone=$ZONE --add-port=$P/$PROTO --permanent
done
else
firewall-cmd --zone=$ZONE --add-port=$PORT/$PROTO --permanent
fi
fi
firewall-cmd --reload
return 0
elif [ "$FIREWALL" == "iptables" ]; then
echo "firewall_allow/iptables: WARNING - iptables is untested"
# iptables doesn't natively support multiple ports, so we have to get creative
if [[ "$PORT" =~ ":" ]]; then
local DPORTS="-m multiport --dports $PORT"
elif [[ "$PORT" =~ "," ]]; then
local DPORTS="-m multiport --dports $PORT"
else
local DPORTS="--dport $PORT"
fi
if [ "$SOURCE" == "any" ]; then
echo "firewall_allow/iptables: Allowing $PORT/$PROTO from any..."
iptables -A INPUT -p $PROTO $DPORTS -j ACCEPT
else
echo "firewall_allow/iptables: Allowing $PORT/$PROTO from $SOURCE..."
iptables -A INPUT -p $PROTO $DPORTS -s $SOURCE -j ACCEPT
fi
iptables-save > /etc/iptables/rules.v4
return 0
elif [ "$FIREWALL" == "none" ]; then
echo "firewall_allow: No firewall detected" >&2
return 1
else
echo "firewall_allow: Unsupported or unknown firewall" >&2
echo 'Please report this at https://github.com/cdp1337/ScriptsCollection/issues' >&2
return 1
fi
}