Skip to content

Commit 5543909

Browse files
thomassagaborigloi
authored andcommitted
CP-24777: A script to configure firewall for NBD
A new script that can be used to specify the addresses (if any) on which incoming connections to the NBD server will be permitted by the iptables firewall. Signed-off-by: Thomas Sanders <[email protected]>
1 parent cd1d650 commit 5543909

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

scripts/nbd-firewall-config.sh

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/bin/bash
2+
#
3+
# Copyright (c) Citrix Systems. All rights reserved.
4+
5+
set -e
6+
7+
##############################################################
8+
# Use this script to open/close the specified interfaces
9+
# (IP addresses) for incoming TCP connections to the NBD port.
10+
#
11+
# Usage:
12+
# ./nbd-firewall-config set [address...]
13+
#
14+
##############################################################
15+
16+
# Require the "set" argument because otherwise there would be a risk
17+
# of someone running the script with no arguments and accidentally
18+
# setting NBD to be available on no addresses.
19+
# This way, omitting "set" (or supplying "--help") causes the script
20+
# to print usage instructions.
21+
22+
OP="$1"
23+
if [ _"${OP}" != _set ]; then
24+
echo "Usage: $(basename ${0}) set [address...]
25+
will set the firewall to allow incoming TCP connections
26+
to the NBD port on only the specified addresses (if any)." 1>&2
27+
exit 1
28+
fi
29+
shift 1
30+
31+
set -eu
32+
33+
TMPSET=xapi_nbd_ipset_ephemeral
34+
NBDSET=xapi_nbd_ipset
35+
NBDPORT=10809
36+
SETTYPE=hash:ip
37+
38+
# Rule to accept new NBD connections on the appropriate addresses
39+
NBD_ACCEPT="-p tcp --dport $NBDPORT -m conntrack --ctstate NEW -m set --match-set $NBDSET dst -j ACCEPT"
40+
41+
# Rules to reject NBD packets on disallowed addresses,
42+
# inbound and outbound,
43+
# even if part of an established connection.
44+
NBD_REJECT_IN="-p tcp --dport $NBDPORT -m set ! --match-set $NBDSET dst -j REJECT"
45+
NBD_REJECT_OUT="-p tcp --sport $NBDPORT -m set ! --match-set $NBDSET src -j REJECT"
46+
47+
function destroy_tmp {
48+
ipset destroy $TMPSET 2>/dev/null || true
49+
}
50+
51+
destroy_tmp
52+
trap destroy_tmp ERR EXIT
53+
54+
# Same principle as double-buffering a display:
55+
# add the items one by one to a temporary ipset,
56+
# then swap it into use atomically.
57+
ipset create $TMPSET $SETTYPE
58+
59+
while [ "$#" -ne 0 ]; do
60+
addr="${1}"
61+
shift 1
62+
ipset add $TMPSET "${addr}"
63+
done
64+
65+
ipset create -exist $NBDSET $SETTYPE
66+
ipset swap $TMPSET $NBDSET
67+
ipset destroy $TMPSET
68+
69+
# Now create the rules if they do not exist already.
70+
# Note: the ip set must exist before we can add an iptables rule that uses it.
71+
for rule in "$NBD_ACCEPT" "$NBD_REJECT_IN"; do
72+
if ! iptables --check INPUT $rule 2>/dev/null
73+
then
74+
iptables --insert INPUT $rule
75+
fi
76+
done
77+
78+
if ! iptables --check OUTPUT $NBD_REJECT_OUT 2>/dev/null
79+
then
80+
iptables --insert OUTPUT $NBD_REJECT_OUT
81+
fi
82+
83+
exit 0

0 commit comments

Comments
 (0)