change_rules.sh revision 77815
1#!/bin/sh 2# 3# Copyright (c) 2000 Alexandre Peixoto 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: 9# 1. Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# 2. Redistributions in binary form must reproduce the above copyright 12# notice, this list of conditions and the following disclaimer in the 13# documentation and/or other materials provided with the distribution. 14# 15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25# SUCH DAMAGE. 26# 27# $FreeBSD: head/share/examples/ipfw/change_rules.sh 77815 2001-06-06 15:45:04Z dcs $ 28 29# Change ipfw(8) rules with safety guarantees for remote operation 30# 31# Invoke this script to edit rc.firewall. It will call ${EDITOR}, or 32# vi(1) if the environment variable is not set, for you to edit rc.firewall, 33# asks for confirmation and then run rc.firewall. You can then examine 34# the output of ipfw list and confirm whether you want the new version or 35# not. 36# 37# If no answer is received in 30 seconds, the previous rc.firewall is 38# run, restoring the old rules (this assumes ipfw flush is present in 39# it). 40# 41# If the new rules are confirmed, they'll replace rc.firewall and the 42# previous ones will be copied to rc.firewall.{date}. A mail will also 43# be sent to root with the unified diffs of the rule change. 44# 45# Non-approved rules are kept in rc.firewall.new, and you are offered 46# the option of changing them instead of the present rules when you 47# call this script. 48# 49# It is suggested improving this script by using some version control 50# software. 51 52get_yes_no() { 53 while true 54 do 55 echo -n "$1 (Y/N) ? " 56 read -t 30 a 57 if [ $? != 0 ]; then 58 a="No"; 59 return; 60 fi 61 case $a in 62 [Yy]) a="Yes"; 63 return;; 64 [Nn]) a="No"; 65 return;; 66 *);; 67 esac 68 done 69} 70 71restore_rules() { 72 nohup sh /etc/rc.firewall >/dev/null 2>&1 73 exit 74} 75 76if [ -f /etc/rc.firewall.new ]; then 77 get_yes_no "A new rules file already exists, do you want to use it" 78 [ $a = 'No' ] && cp /etc/rc.firewall /etc/rc.firewall.new 79else 80 cp /etc/rc.firewall /etc/rc.firewall.new 81fi 82 83trap restore_rules SIGHUP 84 85vi /etc/rc.firewall.new 86 87get_yes_no "Do you want to install the new rules" 88 89[ $a = 'No' ] && exit 90 91cat <<! 92The rules will be changed now. If the message 'Type y to keep the new rules' 93do not appear on the screen or the y key is not pressed in 30 seconds, the 94former rules will be restored. 95The TCP/IP connections might be broken during the change. If so, restore 96the ssh/telnet connection being used. 97! 98 99nohup sh /etc/rc.firewall.new > /tmp/rc.firewall.out 2>&1; 100sleep 2; 101get_yes_no "Would you like to see the resulting new rules" 102[ $a = 'Yes' ] && vi /tmp/rc.firewall.out 103get_yes_no "Type y to keep the new rules" 104[ $a != 'Yes' ] && restore_rules 105 106DATE=`date "+%Y%m%d%H%M"` 107cp /etc/rc.firewall /etc/rc.firewall.$DATE 108mv /etc/rc.firewall.new /etc/rc.firewall 109cat <<! 110The new rules are now default. The previous rules have been preserved 111in the file /etc/rc.firewall.$DATE 112! 113diff -F "^# .*[A-Za-z]" -u /etc/rc.firewall.$DATE /etc/rc.firewall | mail -s "`hostname` Firewall rule change" root 114 115