2001-06-06 15:45:04 +00:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# 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$
|
|
|
|
|
|
|
|
# Change ipfw(8) rules with safety guarantees for remote operation
|
|
|
|
#
|
2001-06-06 19:34:02 +00:00
|
|
|
# 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
|
2003-02-13 12:02:51 +00:00
|
|
|
# ${firewall_script}, ask for confirmation, and then run
|
2001-06-06 19:34:02 +00:00
|
|
|
# ${firewall_script}. You can then examine the output of ipfw list and
|
|
|
|
# confirm whether you want the new version or not.
|
2001-06-06 15:45:04 +00:00
|
|
|
#
|
2001-06-06 19:34:02 +00:00
|
|
|
# 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).
|
2001-06-06 15:45:04 +00:00
|
|
|
#
|
2001-06-06 19:34:02 +00:00
|
|
|
# If the new rules are confirmed, they'll replace ${firewall_script} and
|
2003-02-13 12:02:51 +00:00
|
|
|
# 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.
|
2001-06-06 15:45:04 +00:00
|
|
|
#
|
2003-02-13 12:02:51 +00:00
|
|
|
# Unapproved rules are kept in ${firewall_script}.new, and you are
|
2001-06-06 19:34:02 +00:00
|
|
|
# offered the option of changing them instead of the present rules when
|
|
|
|
# you call this script.
|
2001-06-06 15:45:04 +00:00
|
|
|
#
|
2003-02-13 12:02:51 +00:00
|
|
|
# This script could be improved by using version control
|
2001-06-06 15:45:04 +00:00
|
|
|
# software.
|
|
|
|
|
2001-06-06 19:34:02 +00:00
|
|
|
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:-/usr/bin/vi}
|
2001-09-30 14:30:55 +00:00
|
|
|
PAGER=${PAGER:-/usr/bin/more}
|
|
|
|
|
|
|
|
tempfoo=`basename $0`
|
|
|
|
TMPFILE=`mktemp -t ${tempfoo}` || exit 1
|
2001-06-06 19:34:02 +00:00
|
|
|
|
2001-06-06 15:45:04 +00:00
|
|
|
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() {
|
2003-09-07 07:52:56 +00:00
|
|
|
nohup sh ${firewall_script} </dev/null >/dev/null 2>&1
|
2001-09-30 14:30:55 +00:00
|
|
|
rm ${TMPFILE}
|
|
|
|
exit 1
|
2001-06-06 15:45:04 +00:00
|
|
|
}
|
|
|
|
|
2001-09-30 14:30:55 +00:00
|
|
|
case "${firewall_type}" in
|
|
|
|
[Cc][Ll][Ii][Ee][Nn][Tt]|\
|
|
|
|
[Cc][Ll][Oo][Ss][Ee][Dd]|\
|
|
|
|
[Oo][Pp][Ee][Nn]|\
|
|
|
|
[Ss][Ii][Mm][Pp][Ll][Ee]|\
|
|
|
|
[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
|
|
|
|
edit_file="${firewall_script}"
|
|
|
|
rules_edit=no
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
if [ -r "${firewall_type}" ]; then
|
|
|
|
edit_file="${firewall_type}"
|
|
|
|
rules_edit=yes
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
if [ -f ${edit_file}.new ]; then
|
2001-06-06 15:45:04 +00:00
|
|
|
get_yes_no "A new rules file already exists, do you want to use it"
|
2001-09-30 14:30:55 +00:00
|
|
|
[ $a = 'No' ] && cp ${edit_file} ${edit_file}.new
|
2001-06-06 15:45:04 +00:00
|
|
|
else
|
2001-09-30 14:30:55 +00:00
|
|
|
cp ${edit_file} ${edit_file}.new
|
2001-06-06 15:45:04 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
trap restore_rules SIGHUP
|
|
|
|
|
2001-09-30 14:30:55 +00:00
|
|
|
${EDITOR} ${edit_file}.new
|
2001-06-06 15:45:04 +00:00
|
|
|
|
|
|
|
get_yes_no "Do you want to install the new rules"
|
|
|
|
|
2001-09-30 14:30:55 +00:00
|
|
|
[ $a = 'No' ] && exit 1
|
2001-06-06 15:45:04 +00:00
|
|
|
|
|
|
|
cat <<!
|
2001-06-06 19:34:02 +00:00
|
|
|
The rules will be changed now. If the message 'Type y to keep the new
|
2003-02-13 12:02:51 +00:00
|
|
|
rules' does not appear on the screen or the y key is not pressed in 30
|
|
|
|
seconds, the original rules will be restored.
|
2001-06-06 15:45:04 +00:00
|
|
|
The TCP/IP connections might be broken during the change. If so, restore
|
|
|
|
the ssh/telnet connection being used.
|
|
|
|
!
|
|
|
|
|
2001-09-30 14:30:55 +00:00
|
|
|
if [ ${rules_edit} = yes ]; then
|
|
|
|
nohup sh ${firewall_script} ${firewall_type}.new \
|
2003-09-07 07:52:56 +00:00
|
|
|
< /dev/null > ${TMPFILE} 2>&1
|
2001-09-30 14:30:55 +00:00
|
|
|
else
|
|
|
|
nohup sh ${firewall_script}.new \
|
2003-09-07 07:52:56 +00:00
|
|
|
< /dev/null > ${TMPFILE} 2>&1
|
2001-09-30 14:30:55 +00:00
|
|
|
fi
|
2001-06-06 15:45:04 +00:00
|
|
|
sleep 2;
|
|
|
|
get_yes_no "Would you like to see the resulting new rules"
|
2001-09-30 14:30:55 +00:00
|
|
|
[ $a = 'Yes' ] && ${PAGER} ${TMPFILE}
|
2001-06-06 15:45:04 +00:00
|
|
|
get_yes_no "Type y to keep the new rules"
|
|
|
|
[ $a != 'Yes' ] && restore_rules
|
|
|
|
|
|
|
|
DATE=`date "+%Y%m%d%H%M"`
|
2001-09-30 14:30:55 +00:00
|
|
|
cp ${edit_file} ${edit_file}.$DATE
|
|
|
|
mv ${edit_file}.new ${edit_file}
|
2001-06-06 15:45:04 +00:00
|
|
|
cat <<!
|
2003-02-13 12:02:51 +00:00
|
|
|
The new rules are now installed. The previous rules have been preserved in
|
2001-09-30 14:30:55 +00:00
|
|
|
the file ${edit_file}.$DATE
|
2001-06-06 15:45:04 +00:00
|
|
|
!
|
2001-09-30 14:30:55 +00:00
|
|
|
diff -F "^# .*[A-Za-z]" -u ${edit_file}.$DATE ${edit_file} \
|
|
|
|
| mail -s "`hostname` Firewall rule change" root
|
|
|
|
rm ${TMPFILE}
|
|
|
|
exit 0
|