xref: /freebsd/share/examples/ipfw/change_rules.sh (revision db97c662cfa859e88631f6956bf5a8456985cede)
17b4d3c72SDaniel C. Sobral#!/bin/sh
27b4d3c72SDaniel C. Sobral#
37b4d3c72SDaniel C. Sobral# Copyright (c) 2000 Alexandre Peixoto
47b4d3c72SDaniel C. Sobral# All rights reserved.
57b4d3c72SDaniel C. Sobral#
67b4d3c72SDaniel C. Sobral# Redistribution and use in source and binary forms, with or without
77b4d3c72SDaniel C. Sobral# modification, are permitted provided that the following conditions
87b4d3c72SDaniel C. Sobral# are met:
97b4d3c72SDaniel C. Sobral# 1. Redistributions of source code must retain the above copyright
107b4d3c72SDaniel C. Sobral#    notice, this list of conditions and the following disclaimer.
117b4d3c72SDaniel C. Sobral# 2. Redistributions in binary form must reproduce the above copyright
127b4d3c72SDaniel C. Sobral#    notice, this list of conditions and the following disclaimer in the
137b4d3c72SDaniel C. Sobral#    documentation and/or other materials provided with the distribution.
147b4d3c72SDaniel C. Sobral#
157b4d3c72SDaniel C. Sobral# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
167b4d3c72SDaniel C. Sobral# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
177b4d3c72SDaniel C. Sobral# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
187b4d3c72SDaniel C. Sobral# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
197b4d3c72SDaniel C. Sobral# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
207b4d3c72SDaniel C. Sobral# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
217b4d3c72SDaniel C. Sobral# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
227b4d3c72SDaniel C. Sobral# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
237b4d3c72SDaniel C. Sobral# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
247b4d3c72SDaniel C. Sobral# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
257b4d3c72SDaniel C. Sobral# SUCH DAMAGE.
267b4d3c72SDaniel C. Sobral#
277b4d3c72SDaniel C. Sobral# $FreeBSD$
287b4d3c72SDaniel C. Sobral
297b4d3c72SDaniel C. Sobral# Change ipfw(8) rules with safety guarantees for remote operation
307b4d3c72SDaniel C. Sobral#
31db97c662SDaniel C. Sobral# Invoke this script to edit ${firewall_script}. It will call ${EDITOR},
32db97c662SDaniel C. Sobral# or vi(1) if the environment variable is not set, for you to edit
33db97c662SDaniel C. Sobral# ${firewall_script}, asks for confirmation and then run
34db97c662SDaniel C. Sobral# ${firewall_script}. You can then examine the output of ipfw list and
35db97c662SDaniel C. Sobral# confirm whether you want the new version or not.
367b4d3c72SDaniel C. Sobral#
37db97c662SDaniel C. Sobral# If no answer is received in 30 seconds, the previous
38db97c662SDaniel C. Sobral# ${firewall_script} is run, restoring the old rules (this assumes ipfw
39db97c662SDaniel C. Sobral# flush is present in it).
407b4d3c72SDaniel C. Sobral#
41db97c662SDaniel C. Sobral# If the new rules are confirmed, they'll replace ${firewall_script} and
42db97c662SDaniel C. Sobral# the previous ones will be copied to ${firewall_script}.{date}. A mail
43db97c662SDaniel C. Sobral# will also be sent to root with the unified diffs of the rule change.
447b4d3c72SDaniel C. Sobral#
45db97c662SDaniel C. Sobral# Non-approved rules are kept in ${firewall_script}.new, and you are
46db97c662SDaniel C. Sobral# offered the option of changing them instead of the present rules when
47db97c662SDaniel C. Sobral# you call this script.
487b4d3c72SDaniel C. Sobral#
497b4d3c72SDaniel C. Sobral# It is suggested improving this script by using some version control
507b4d3c72SDaniel C. Sobral# software.
517b4d3c72SDaniel C. Sobral
52db97c662SDaniel C. Sobralif [ -r /etc/defaults/rc.conf ]; then
53db97c662SDaniel C. Sobral	. /etc/defaults/rc.conf
54db97c662SDaniel C. Sobral	source_rc_confs
55db97c662SDaniel C. Sobralelif [ -r /etc/rc.conf ]; then
56db97c662SDaniel C. Sobral	. /etc/rc.conf
57db97c662SDaniel C. Sobralfi
58db97c662SDaniel C. Sobral
59db97c662SDaniel C. SobralEDITOR=${EDITOR:-/usr/bin/vi}
60db97c662SDaniel C. Sobral
617b4d3c72SDaniel C. Sobralget_yes_no() {
627b4d3c72SDaniel C. Sobral	while true
637b4d3c72SDaniel C. Sobral	do
647b4d3c72SDaniel C. Sobral		echo -n "$1 (Y/N) ? "
657b4d3c72SDaniel C. Sobral		read -t 30 a
667b4d3c72SDaniel C. Sobral		if [ $? != 0 ]; then
677b4d3c72SDaniel C. Sobral			a="No";
687b4d3c72SDaniel C. Sobral		        return;
697b4d3c72SDaniel C. Sobral		fi
707b4d3c72SDaniel C. Sobral		case $a in
717b4d3c72SDaniel C. Sobral			[Yy]) a="Yes";
727b4d3c72SDaniel C. Sobral			      return;;
737b4d3c72SDaniel C. Sobral			[Nn]) a="No";
747b4d3c72SDaniel C. Sobral			      return;;
757b4d3c72SDaniel C. Sobral			*);;
767b4d3c72SDaniel C. Sobral		esac
777b4d3c72SDaniel C. Sobral	done
787b4d3c72SDaniel C. Sobral}
797b4d3c72SDaniel C. Sobral
807b4d3c72SDaniel C. Sobralrestore_rules() {
81db97c662SDaniel C. Sobral	nohup sh ${firewall_script} >/dev/null 2>&1
827b4d3c72SDaniel C. Sobral	exit
837b4d3c72SDaniel C. Sobral}
847b4d3c72SDaniel C. Sobral
85db97c662SDaniel C. Sobralif [ -f /etc/${firewall_script}.new ]; then
867b4d3c72SDaniel C. Sobral	get_yes_no "A new rules file already exists, do you want to use it"
87db97c662SDaniel C. Sobral	[ $a = 'No' ] && cp ${firewall_script} /etc/${firewall_script}.new
887b4d3c72SDaniel C. Sobralelse
89db97c662SDaniel C. Sobral	cp ${firewall_script} /etc/${firewall_script}.new
907b4d3c72SDaniel C. Sobralfi
917b4d3c72SDaniel C. Sobral
927b4d3c72SDaniel C. Sobraltrap restore_rules SIGHUP
937b4d3c72SDaniel C. Sobral
94db97c662SDaniel C. Sobral${EDITOR} /etc/${firewall_script}.new
957b4d3c72SDaniel C. Sobral
967b4d3c72SDaniel C. Sobralget_yes_no "Do you want to install the new rules"
977b4d3c72SDaniel C. Sobral
987b4d3c72SDaniel C. Sobral[ $a = 'No' ] && exit
997b4d3c72SDaniel C. Sobral
1007b4d3c72SDaniel C. Sobralcat <<!
101db97c662SDaniel C. SobralThe rules will be changed now. If the message 'Type y to keep the new
102db97c662SDaniel C. Sobralrules' do not appear on the screen or the y key is not pressed in 30
103db97c662SDaniel C. Sobralseconds, the former rules will be restored.
1047b4d3c72SDaniel C. SobralThe TCP/IP connections might be broken during the change. If so, restore
1057b4d3c72SDaniel C. Sobralthe ssh/telnet connection being used.
1067b4d3c72SDaniel C. Sobral!
1077b4d3c72SDaniel C. Sobral
108db97c662SDaniel C. Sobralnohup sh /etc/${firewall_script}.new > /tmp/${firewall_script}.out 2>&1;
1097b4d3c72SDaniel C. Sobralsleep 2;
1107b4d3c72SDaniel C. Sobralget_yes_no "Would you like to see the resulting new rules"
111db97c662SDaniel C. Sobral[ $a = 'Yes' ] && ${EDITOR} /tmp/${firewall_script}.out
1127b4d3c72SDaniel C. Sobralget_yes_no "Type y to keep the new rules"
1137b4d3c72SDaniel C. Sobral[ $a != 'Yes' ] && restore_rules
1147b4d3c72SDaniel C. Sobral
1157b4d3c72SDaniel C. SobralDATE=`date "+%Y%m%d%H%M"`
116db97c662SDaniel C. Sobralcp ${firewall_script} /etc/${firewall_script}.$DATE
117db97c662SDaniel C. Sobralmv /etc/${firewall_script}.new ${firewall_script}
1187b4d3c72SDaniel C. Sobralcat <<!
119db97c662SDaniel C. SobralThe new rules are now default. The previous rules have been preserved in
120db97c662SDaniel C. Sobralthe file /etc/${firewall_script}.$DATE
1217b4d3c72SDaniel C. Sobral!
122db97c662SDaniel C. Sobraldiff -F "^# .*[A-Za-z]" -u /etc/${firewall_script}.$DATE ${firewall_script} | mail -s "`hostname` Firewall rule change" root
1237b4d3c72SDaniel C. Sobral
124