xref: /freebsd/share/examples/ipfw/change_rules.sh (revision 7b4d3c72cc4ba8071f0fde23be4a06e3515c3ffc)
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#
317b4d3c72SDaniel C. Sobral# Invoke this script to edit rc.firewall. It will call ${EDITOR}, or
327b4d3c72SDaniel C. Sobral# vi(1) if the environment variable is not set, for you to edit rc.firewall,
337b4d3c72SDaniel C. Sobral# asks for confirmation and then run rc.firewall. You can then examine
347b4d3c72SDaniel C. Sobral# the output of ipfw list and confirm whether you want the new version or
357b4d3c72SDaniel C. Sobral# not.
367b4d3c72SDaniel C. Sobral#
377b4d3c72SDaniel C. Sobral# If no answer is received in 30 seconds, the previous rc.firewall is
387b4d3c72SDaniel C. Sobral# run, restoring the old rules (this assumes ipfw flush is present in
397b4d3c72SDaniel C. Sobral# it).
407b4d3c72SDaniel C. Sobral#
417b4d3c72SDaniel C. Sobral# If the new rules are confirmed, they'll replace rc.firewall and the
427b4d3c72SDaniel C. Sobral# previous ones will be copied to rc.firewall.{date}. A mail will also
437b4d3c72SDaniel C. Sobral# be sent to root with the unified diffs of the rule change.
447b4d3c72SDaniel C. Sobral#
457b4d3c72SDaniel C. Sobral# Non-approved rules are kept in rc.firewall.new, and you are offered
467b4d3c72SDaniel C. Sobral# the option of changing them instead of the present rules when you
477b4d3c72SDaniel C. Sobral# 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
527b4d3c72SDaniel C. Sobralget_yes_no() {
537b4d3c72SDaniel C. Sobral	while true
547b4d3c72SDaniel C. Sobral	do
557b4d3c72SDaniel C. Sobral		echo -n "$1 (Y/N) ? "
567b4d3c72SDaniel C. Sobral		read -t 30 a
577b4d3c72SDaniel C. Sobral		if [ $? != 0 ]; then
587b4d3c72SDaniel C. Sobral			a="No";
597b4d3c72SDaniel C. Sobral		        return;
607b4d3c72SDaniel C. Sobral		fi
617b4d3c72SDaniel C. Sobral		case $a in
627b4d3c72SDaniel C. Sobral			[Yy]) a="Yes";
637b4d3c72SDaniel C. Sobral			      return;;
647b4d3c72SDaniel C. Sobral			[Nn]) a="No";
657b4d3c72SDaniel C. Sobral			      return;;
667b4d3c72SDaniel C. Sobral			*);;
677b4d3c72SDaniel C. Sobral		esac
687b4d3c72SDaniel C. Sobral	done
697b4d3c72SDaniel C. Sobral}
707b4d3c72SDaniel C. Sobral
717b4d3c72SDaniel C. Sobralrestore_rules() {
727b4d3c72SDaniel C. Sobral	nohup sh /etc/rc.firewall >/dev/null 2>&1
737b4d3c72SDaniel C. Sobral	exit
747b4d3c72SDaniel C. Sobral}
757b4d3c72SDaniel C. Sobral
767b4d3c72SDaniel C. Sobralif [ -f /etc/rc.firewall.new ]; then
777b4d3c72SDaniel C. Sobral	get_yes_no "A new rules file already exists, do you want to use it"
787b4d3c72SDaniel C. Sobral	[ $a = 'No' ] && cp /etc/rc.firewall /etc/rc.firewall.new
797b4d3c72SDaniel C. Sobralelse
807b4d3c72SDaniel C. Sobral	cp /etc/rc.firewall /etc/rc.firewall.new
817b4d3c72SDaniel C. Sobralfi
827b4d3c72SDaniel C. Sobral
837b4d3c72SDaniel C. Sobraltrap restore_rules SIGHUP
847b4d3c72SDaniel C. Sobral
857b4d3c72SDaniel C. Sobralvi /etc/rc.firewall.new
867b4d3c72SDaniel C. Sobral
877b4d3c72SDaniel C. Sobralget_yes_no "Do you want to install the new rules"
887b4d3c72SDaniel C. Sobral
897b4d3c72SDaniel C. Sobral[ $a = 'No' ] && exit
907b4d3c72SDaniel C. Sobral
917b4d3c72SDaniel C. Sobralcat <<!
927b4d3c72SDaniel C. SobralThe rules will be changed now. If the message 'Type y to keep the new rules'
937b4d3c72SDaniel C. Sobraldo not appear on the screen or the y key is not pressed in 30 seconds, the
947b4d3c72SDaniel C. Sobralformer rules will be restored.
957b4d3c72SDaniel C. SobralThe TCP/IP connections might be broken during the change. If so, restore
967b4d3c72SDaniel C. Sobralthe ssh/telnet connection being used.
977b4d3c72SDaniel C. Sobral!
987b4d3c72SDaniel C. Sobral
997b4d3c72SDaniel C. Sobralnohup sh /etc/rc.firewall.new > /tmp/rc.firewall.out 2>&1;
1007b4d3c72SDaniel C. Sobralsleep 2;
1017b4d3c72SDaniel C. Sobralget_yes_no "Would you like to see the resulting new rules"
1027b4d3c72SDaniel C. Sobral[ $a = 'Yes' ] && vi /tmp/rc.firewall.out
1037b4d3c72SDaniel C. Sobralget_yes_no "Type y to keep the new rules"
1047b4d3c72SDaniel C. Sobral[ $a != 'Yes' ] && restore_rules
1057b4d3c72SDaniel C. Sobral
1067b4d3c72SDaniel C. SobralDATE=`date "+%Y%m%d%H%M"`
1077b4d3c72SDaniel C. Sobralcp /etc/rc.firewall /etc/rc.firewall.$DATE
1087b4d3c72SDaniel C. Sobralmv /etc/rc.firewall.new /etc/rc.firewall
1097b4d3c72SDaniel C. Sobralcat <<!
1107b4d3c72SDaniel C. SobralThe new rules are now default. The previous rules have been preserved
1117b4d3c72SDaniel C. Sobralin the file /etc/rc.firewall.$DATE
1127b4d3c72SDaniel C. Sobral!
1137b4d3c72SDaniel C. Sobraldiff -F "^# .*[A-Za-z]" -u /etc/rc.firewall.$DATE /etc/rc.firewall | mail -s "`hostname` Firewall rule change" root
1147b4d3c72SDaniel C. Sobral
115