xref: /freebsd/share/examples/ipfw/change_rules.sh (revision c165f4ab694b3f8157d52867930b92e1dc0180cd)
17b4d3c72SDaniel C. Sobral#!/bin/sh
27b4d3c72SDaniel C. Sobral#
3f0cfa1b1SPedro F. Giffuni# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4f0cfa1b1SPedro F. Giffuni#
57b4d3c72SDaniel C. Sobral# Copyright (c) 2000 Alexandre Peixoto
67b4d3c72SDaniel C. Sobral# All rights reserved.
77b4d3c72SDaniel C. Sobral#
87b4d3c72SDaniel C. Sobral# Redistribution and use in source and binary forms, with or without
97b4d3c72SDaniel C. Sobral# modification, are permitted provided that the following conditions
107b4d3c72SDaniel C. Sobral# are met:
117b4d3c72SDaniel C. Sobral# 1. Redistributions of source code must retain the above copyright
127b4d3c72SDaniel C. Sobral#    notice, this list of conditions and the following disclaimer.
137b4d3c72SDaniel C. Sobral# 2. Redistributions in binary form must reproduce the above copyright
147b4d3c72SDaniel C. Sobral#    notice, this list of conditions and the following disclaimer in the
157b4d3c72SDaniel C. Sobral#    documentation and/or other materials provided with the distribution.
167b4d3c72SDaniel C. Sobral#
177b4d3c72SDaniel C. Sobral# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
187b4d3c72SDaniel C. Sobral# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
197b4d3c72SDaniel C. Sobral# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
207b4d3c72SDaniel C. Sobral# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
217b4d3c72SDaniel C. Sobral# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
227b4d3c72SDaniel C. Sobral# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
237b4d3c72SDaniel C. Sobral# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
247b4d3c72SDaniel C. Sobral# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
257b4d3c72SDaniel C. Sobral# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
267b4d3c72SDaniel C. Sobral# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
277b4d3c72SDaniel C. Sobral# SUCH DAMAGE.
287b4d3c72SDaniel C. Sobral#
297b4d3c72SDaniel C. Sobral# $FreeBSD$
307b4d3c72SDaniel C. Sobral
317b4d3c72SDaniel C. Sobral# Change ipfw(8) rules with safety guarantees for remote operation
327b4d3c72SDaniel C. Sobral#
33db97c662SDaniel C. Sobral# Invoke this script to edit ${firewall_script}. It will call ${EDITOR},
34db97c662SDaniel C. Sobral# or vi(1) if the environment variable is not set, for you to edit
35f212b184SChristian Brueffer# ${firewall_script}, ask for confirmation, and then run
36db97c662SDaniel C. Sobral# ${firewall_script}. You can then examine the output of ipfw list and
37db97c662SDaniel C. Sobral# confirm whether you want the new version or not.
387b4d3c72SDaniel C. Sobral#
39db97c662SDaniel C. Sobral# If no answer is received in 30 seconds, the previous
40db97c662SDaniel C. Sobral# ${firewall_script} is run, restoring the old rules (this assumes ipfw
41db97c662SDaniel C. Sobral# flush is present in it).
427b4d3c72SDaniel C. Sobral#
43db97c662SDaniel C. Sobral# If the new rules are confirmed, they'll replace ${firewall_script} and
44f212b184SChristian Brueffer# the previous ones will be copied to ${firewall_script}.{date}. Mail
45f212b184SChristian Brueffer# will also be sent to root with a unified diff of the rule change.
467b4d3c72SDaniel C. Sobral#
47f212b184SChristian Brueffer# Unapproved rules are kept in ${firewall_script}.new, and you are
48db97c662SDaniel C. Sobral# offered the option of changing them instead of the present rules when
49db97c662SDaniel C. Sobral# you call this script.
507b4d3c72SDaniel C. Sobral#
51f212b184SChristian Brueffer# This script could be improved by using version control
527b4d3c72SDaniel C. Sobral# software.
537b4d3c72SDaniel C. Sobral
54db97c662SDaniel C. Sobralif [ -r /etc/defaults/rc.conf ]; then
55db97c662SDaniel C. Sobral	. /etc/defaults/rc.conf
56db97c662SDaniel C. Sobral	source_rc_confs
57db97c662SDaniel C. Sobralelif [ -r /etc/rc.conf ]; then
58db97c662SDaniel C. Sobral	. /etc/rc.conf
59db97c662SDaniel C. Sobralfi
60db97c662SDaniel C. Sobral
61db97c662SDaniel C. SobralEDITOR=${EDITOR:-/usr/bin/vi}
62256ab0efSSheldon HearnPAGER=${PAGER:-/usr/bin/more}
63256ab0efSSheldon Hearn
64256ab0efSSheldon Hearntempfoo=`basename $0`
65256ab0efSSheldon HearnTMPFILE=`mktemp -t ${tempfoo}` || exit 1
66db97c662SDaniel C. Sobral
677b4d3c72SDaniel C. Sobralget_yes_no() {
687b4d3c72SDaniel C. Sobral	while true
697b4d3c72SDaniel C. Sobral	do
707b4d3c72SDaniel C. Sobral		echo -n "$1 (Y/N) ? "
717b4d3c72SDaniel C. Sobral		read -t 30 a
727b4d3c72SDaniel C. Sobral		if [ $? != 0 ]; then
737b4d3c72SDaniel C. Sobral			a="No";
747b4d3c72SDaniel C. Sobral		        return;
757b4d3c72SDaniel C. Sobral		fi
767b4d3c72SDaniel C. Sobral		case $a in
777b4d3c72SDaniel C. Sobral			[Yy]) a="Yes";
787b4d3c72SDaniel C. Sobral			      return;;
797b4d3c72SDaniel C. Sobral			[Nn]) a="No";
807b4d3c72SDaniel C. Sobral			      return;;
817b4d3c72SDaniel C. Sobral			*);;
827b4d3c72SDaniel C. Sobral		esac
837b4d3c72SDaniel C. Sobral	done
847b4d3c72SDaniel C. Sobral}
857b4d3c72SDaniel C. Sobral
867b4d3c72SDaniel C. Sobralrestore_rules() {
87978243bdSJohn-Mark Gurney	nohup sh ${firewall_script} </dev/null >/dev/null 2>&1
88256ab0efSSheldon Hearn	rm ${TMPFILE}
89256ab0efSSheldon Hearn	exit 1
907b4d3c72SDaniel C. Sobral}
917b4d3c72SDaniel C. Sobral
92256ab0efSSheldon Hearncase "${firewall_type}" in
93256ab0efSSheldon Hearn[Cc][Ll][Ii][Ee][Nn][Tt]|\
94256ab0efSSheldon Hearn[Cc][Ll][Oo][Ss][Ee][Dd]|\
95256ab0efSSheldon Hearn[Oo][Pp][Ee][Nn]|\
96256ab0efSSheldon Hearn[Ss][Ii][Mm][Pp][Ll][Ee]|\
97256ab0efSSheldon Hearn[Uu][Nn][Kk][Nn][Oo][Ww][Nn])
98256ab0efSSheldon Hearn	edit_file="${firewall_script}"
99256ab0efSSheldon Hearn	rules_edit=no
100256ab0efSSheldon Hearn	;;
101256ab0efSSheldon Hearn*)
102256ab0efSSheldon Hearn	if [ -r "${firewall_type}" ]; then
103256ab0efSSheldon Hearn		edit_file="${firewall_type}"
104256ab0efSSheldon Hearn		rules_edit=yes
105256ab0efSSheldon Hearn	fi
106256ab0efSSheldon Hearn	;;
107256ab0efSSheldon Hearnesac
108256ab0efSSheldon Hearn
109256ab0efSSheldon Hearnif [ -f ${edit_file}.new ]; then
1107b4d3c72SDaniel C. Sobral	get_yes_no "A new rules file already exists, do you want to use it"
111256ab0efSSheldon Hearn	[ $a = 'No' ] && cp ${edit_file} ${edit_file}.new
1127b4d3c72SDaniel C. Sobralelse
113256ab0efSSheldon Hearn	cp ${edit_file} ${edit_file}.new
1147b4d3c72SDaniel C. Sobralfi
1157b4d3c72SDaniel C. Sobral
1167b4d3c72SDaniel C. Sobraltrap restore_rules SIGHUP
1177b4d3c72SDaniel C. Sobral
118256ab0efSSheldon Hearn${EDITOR} ${edit_file}.new
1197b4d3c72SDaniel C. Sobral
1207b4d3c72SDaniel C. Sobralget_yes_no "Do you want to install the new rules"
1217b4d3c72SDaniel C. Sobral
122256ab0efSSheldon Hearn[ $a = 'No' ] && exit 1
1237b4d3c72SDaniel C. Sobral
1247b4d3c72SDaniel C. Sobralcat <<!
125db97c662SDaniel C. SobralThe rules will be changed now. If the message 'Type y to keep the new
126f212b184SChristian Bruefferrules' does not appear on the screen or the y key is not pressed in 30
127f212b184SChristian Bruefferseconds, the original rules will be restored.
1287b4d3c72SDaniel C. SobralThe TCP/IP connections might be broken during the change. If so, restore
1297b4d3c72SDaniel C. Sobralthe ssh/telnet connection being used.
1307b4d3c72SDaniel C. Sobral!
1317b4d3c72SDaniel C. Sobral
132256ab0efSSheldon Hearnif [ ${rules_edit} = yes ]; then
133256ab0efSSheldon Hearn	nohup sh ${firewall_script} ${firewall_type}.new \
134978243bdSJohn-Mark Gurney	    < /dev/null > ${TMPFILE} 2>&1
135256ab0efSSheldon Hearnelse
136256ab0efSSheldon Hearn	nohup sh ${firewall_script}.new \
137978243bdSJohn-Mark Gurney	    < /dev/null > ${TMPFILE} 2>&1
138256ab0efSSheldon Hearnfi
1397b4d3c72SDaniel C. Sobralsleep 2;
1407b4d3c72SDaniel C. Sobralget_yes_no "Would you like to see the resulting new rules"
141256ab0efSSheldon Hearn[ $a = 'Yes' ] && ${PAGER} ${TMPFILE}
1427b4d3c72SDaniel C. Sobralget_yes_no "Type y to keep the new rules"
1437b4d3c72SDaniel C. Sobral[ $a != 'Yes' ] && restore_rules
1447b4d3c72SDaniel C. Sobral
1457b4d3c72SDaniel C. SobralDATE=`date "+%Y%m%d%H%M"`
146256ab0efSSheldon Hearncp ${edit_file} ${edit_file}.$DATE
147256ab0efSSheldon Hearnmv ${edit_file}.new ${edit_file}
1487b4d3c72SDaniel C. Sobralcat <<!
149f212b184SChristian BruefferThe new rules are now installed. The previous rules have been preserved in
150256ab0efSSheldon Hearnthe file ${edit_file}.$DATE
1517b4d3c72SDaniel C. Sobral!
152*c165f4abSHiroki Satodiff -u ${edit_file}.$DATE ${edit_file} \
153256ab0efSSheldon Hearn    | mail -s "`hostname` Firewall rule change" root
154256ab0efSSheldon Hearnrm ${TMPFILE}
155256ab0efSSheldon Hearnexit 0
156