-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_nfttables.sh
executable file
·138 lines (122 loc) · 4.33 KB
/
change_nfttables.sh
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
#!/usr/bin/env bash
#
# Copyright (c) 2000 Alexandre Peixoto
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD: stable/10/share/examples/ipfw/change_rules.sh 119833 2003-09-07 07:52:56Z jmg $
# Change ipfw(8) rules with safety guarantees for remote operation
#
# Invoke this script to edit ${firewall_script}. It will call ${EDITOR},
# or vi(1) if the environment variable is not set, for you to edit
# ${firewall_script}, ask for confirmation, and then run
# ${firewall_script}. You can then examine the output of ipfw list and
# confirm whether you want the new version or not.
#
# If no answer is received in 30 seconds, the previous
# ${firewall_script} is run, restoring the old rules (this assumes ipfw
# flush is present in it).
#
# If the new rules are confirmed, they'll replace ${firewall_script} and
# the previous ones will be copied to ${firewall_script}.{date}. Mail
# will also be sent to root with a unified diff of the rule change.
#
# Unapproved rules are kept in ${firewall_script}.new, and you are
# offered the option of changing them instead of the present rules when
# you call this script.
#
# This script could be improved by using version control
# software.
#if [ -r /etc/defaults/rc.conf ]; then
# . /etc/defaults/rc.conf
# source_rc_confs
#elif [ -r /etc/rc.conf ]; then
# . /etc/rc.conf
#fi
EDITOR=${EDITOR:-/bin/vi}
PAGER=${PAGER:-/bin/more}
FWDIR=${FWDIR:-/etc/firewall}
tempfoo=`basename $0`
TMPFILE=`mktemp -t ${tempfoo}XXXX` || exit 1
get_yes_no() {
while true
do
echo -n "$1 (Y/N) ? "
read -t 30 a
if [ $? != 0 ]; then
a="No";
return;
fi
case $a in
[Yy]) a="Yes";
return;;
[Nn]) a="No";
return;;
*);;
esac
done
}
restore_rules() {
nft -f ${firewall_script} </dev/null >/dev/null 2>&1
rm ${TMPFILE}
exit 1
}
firewall_script="/etc/nftables.conf"
edit_file="${firewall_script}"
if [ -f ${edit_file}.new ]; then
get_yes_no "A new rules file already exists, do you want to use it"
[ $a = 'No' ] && cp ${edit_file} ${edit_file}.new
else
cp ${edit_file} ${edit_file}.new
fi
trap restore_rules SIGHUP
${EDITOR} ${edit_file}.new
get_yes_no "Do you want to install the new rules"
[ $a = 'No' ] && exit 1
cat <<!
The rules will be changed now. If the message 'Type y to keep the new
rules' does not appear on the screen or the y key is not pressed in 30
seconds, the original rules will be restored.
The TCP/IP connections might be broken during the change. If so, restore
the ssh/telnet connection being used.
!
nft -f ${firewall_script}.new \
< /dev/null > ${TMPFILE} 2>&1 &&
nft list ruleset < /dev/null |
tr '\t' ' ' > ${TMPFILE} 2>&1
get_yes_no "Would you like to see the resulting new rules"
[ $a = 'Yes' ] && ${PAGER} ${TMPFILE}
get_yes_no "Type y to keep the new rules"
[ $a != 'Yes' ] && restore_rules
arch_file=${FWDIR}/`basename ${edit_file}`.`date "+%Y%m%d%H%M"`
cp ${edit_file} ${arch_file}
mv ${edit_file}.new ${edit_file}
cat <<!
The new rules are now installed. The previous rules have been preserved in
the file ${arch_file}
!
diff -F "^# .*[A-Za-z]" -u ${arch_file} ${edit_file}
#\
# | mail -s "`hostname` Firewall rule change" root
rm ${TMPFILE}
exit 0