17b4d3c72SDaniel C. Sobral#!/bin/sh 27b4d3c72SDaniel C. Sobral# 3*4d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause 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 307b4d3c72SDaniel C. Sobral# Change ipfw(8) rules with safety guarantees for remote operation 317b4d3c72SDaniel C. Sobral# 32db97c662SDaniel C. Sobral# Invoke this script to edit ${firewall_script}. It will call ${EDITOR}, 33db97c662SDaniel C. Sobral# or vi(1) if the environment variable is not set, for you to edit 34f212b184SChristian Brueffer# ${firewall_script}, ask for confirmation, and then run 35db97c662SDaniel C. Sobral# ${firewall_script}. You can then examine the output of ipfw list and 36db97c662SDaniel C. Sobral# confirm whether you want the new version or not. 377b4d3c72SDaniel C. Sobral# 38db97c662SDaniel C. Sobral# If no answer is received in 30 seconds, the previous 39db97c662SDaniel C. Sobral# ${firewall_script} is run, restoring the old rules (this assumes ipfw 40db97c662SDaniel C. Sobral# flush is present in it). 417b4d3c72SDaniel C. Sobral# 42db97c662SDaniel C. Sobral# If the new rules are confirmed, they'll replace ${firewall_script} and 43f212b184SChristian Brueffer# the previous ones will be copied to ${firewall_script}.{date}. Mail 44f212b184SChristian Brueffer# will also be sent to root with a unified diff of the rule change. 457b4d3c72SDaniel C. Sobral# 46f212b184SChristian Brueffer# Unapproved rules are kept in ${firewall_script}.new, and you are 47db97c662SDaniel C. Sobral# offered the option of changing them instead of the present rules when 48db97c662SDaniel C. Sobral# you call this script. 497b4d3c72SDaniel C. Sobral# 50f212b184SChristian Brueffer# This script could be improved by using version control 517b4d3c72SDaniel C. Sobral# software. 527b4d3c72SDaniel C. Sobral 53db97c662SDaniel C. Sobralif [ -r /etc/defaults/rc.conf ]; then 54db97c662SDaniel C. Sobral . /etc/defaults/rc.conf 55db97c662SDaniel C. Sobral source_rc_confs 56db97c662SDaniel C. Sobralelif [ -r /etc/rc.conf ]; then 57db97c662SDaniel C. Sobral . /etc/rc.conf 58db97c662SDaniel C. Sobralfi 59db97c662SDaniel C. Sobral 60db97c662SDaniel C. SobralEDITOR=${EDITOR:-/usr/bin/vi} 61256ab0efSSheldon HearnPAGER=${PAGER:-/usr/bin/more} 62256ab0efSSheldon Hearn 63256ab0efSSheldon Hearntempfoo=`basename $0` 64256ab0efSSheldon HearnTMPFILE=`mktemp -t ${tempfoo}` || exit 1 65db97c662SDaniel C. Sobral 667b4d3c72SDaniel C. Sobralget_yes_no() { 677b4d3c72SDaniel C. Sobral while true 687b4d3c72SDaniel C. Sobral do 697b4d3c72SDaniel C. Sobral echo -n "$1 (Y/N) ? " 707b4d3c72SDaniel C. Sobral read -t 30 a 717b4d3c72SDaniel C. Sobral if [ $? != 0 ]; then 727b4d3c72SDaniel C. Sobral a="No"; 737b4d3c72SDaniel C. Sobral return; 747b4d3c72SDaniel C. Sobral fi 757b4d3c72SDaniel C. Sobral case $a in 767b4d3c72SDaniel C. Sobral [Yy]) a="Yes"; 777b4d3c72SDaniel C. Sobral return;; 787b4d3c72SDaniel C. Sobral [Nn]) a="No"; 797b4d3c72SDaniel C. Sobral return;; 807b4d3c72SDaniel C. Sobral *);; 817b4d3c72SDaniel C. Sobral esac 827b4d3c72SDaniel C. Sobral done 837b4d3c72SDaniel C. Sobral} 847b4d3c72SDaniel C. Sobral 857b4d3c72SDaniel C. Sobralrestore_rules() { 86978243bdSJohn-Mark Gurney nohup sh ${firewall_script} </dev/null >/dev/null 2>&1 87256ab0efSSheldon Hearn rm ${TMPFILE} 88256ab0efSSheldon Hearn exit 1 897b4d3c72SDaniel C. Sobral} 907b4d3c72SDaniel C. Sobral 91256ab0efSSheldon Hearncase "${firewall_type}" in 92256ab0efSSheldon Hearn[Cc][Ll][Ii][Ee][Nn][Tt]|\ 93256ab0efSSheldon Hearn[Cc][Ll][Oo][Ss][Ee][Dd]|\ 94256ab0efSSheldon Hearn[Oo][Pp][Ee][Nn]|\ 95256ab0efSSheldon Hearn[Ss][Ii][Mm][Pp][Ll][Ee]|\ 96256ab0efSSheldon Hearn[Uu][Nn][Kk][Nn][Oo][Ww][Nn]) 97256ab0efSSheldon Hearn edit_file="${firewall_script}" 98256ab0efSSheldon Hearn rules_edit=no 99256ab0efSSheldon Hearn ;; 100256ab0efSSheldon Hearn*) 101256ab0efSSheldon Hearn if [ -r "${firewall_type}" ]; then 102256ab0efSSheldon Hearn edit_file="${firewall_type}" 103256ab0efSSheldon Hearn rules_edit=yes 104256ab0efSSheldon Hearn fi 105256ab0efSSheldon Hearn ;; 106256ab0efSSheldon Hearnesac 107256ab0efSSheldon Hearn 108256ab0efSSheldon Hearnif [ -f ${edit_file}.new ]; then 1097b4d3c72SDaniel C. Sobral get_yes_no "A new rules file already exists, do you want to use it" 110256ab0efSSheldon Hearn [ $a = 'No' ] && cp ${edit_file} ${edit_file}.new 1117b4d3c72SDaniel C. Sobralelse 112256ab0efSSheldon Hearn cp ${edit_file} ${edit_file}.new 1137b4d3c72SDaniel C. Sobralfi 1147b4d3c72SDaniel C. Sobral 1157b4d3c72SDaniel C. Sobraltrap restore_rules SIGHUP 1167b4d3c72SDaniel C. Sobral 117256ab0efSSheldon Hearn${EDITOR} ${edit_file}.new 1187b4d3c72SDaniel C. Sobral 1197b4d3c72SDaniel C. Sobralget_yes_no "Do you want to install the new rules" 1207b4d3c72SDaniel C. Sobral 121256ab0efSSheldon Hearn[ $a = 'No' ] && exit 1 1227b4d3c72SDaniel C. Sobral 1237b4d3c72SDaniel C. Sobralcat <<! 124db97c662SDaniel C. SobralThe rules will be changed now. If the message 'Type y to keep the new 125f212b184SChristian Bruefferrules' does not appear on the screen or the y key is not pressed in 30 126f212b184SChristian Bruefferseconds, the original rules will be restored. 1277b4d3c72SDaniel C. SobralThe TCP/IP connections might be broken during the change. If so, restore 1287b4d3c72SDaniel C. Sobralthe ssh/telnet connection being used. 1297b4d3c72SDaniel C. Sobral! 1307b4d3c72SDaniel C. Sobral 131256ab0efSSheldon Hearnif [ ${rules_edit} = yes ]; then 132256ab0efSSheldon Hearn nohup sh ${firewall_script} ${firewall_type}.new \ 133978243bdSJohn-Mark Gurney < /dev/null > ${TMPFILE} 2>&1 134256ab0efSSheldon Hearnelse 135256ab0efSSheldon Hearn nohup sh ${firewall_script}.new \ 136978243bdSJohn-Mark Gurney < /dev/null > ${TMPFILE} 2>&1 137256ab0efSSheldon Hearnfi 1387b4d3c72SDaniel C. Sobralsleep 2; 1397b4d3c72SDaniel C. Sobralget_yes_no "Would you like to see the resulting new rules" 140256ab0efSSheldon Hearn[ $a = 'Yes' ] && ${PAGER} ${TMPFILE} 1417b4d3c72SDaniel C. Sobralget_yes_no "Type y to keep the new rules" 1427b4d3c72SDaniel C. Sobral[ $a != 'Yes' ] && restore_rules 1437b4d3c72SDaniel C. Sobral 1447b4d3c72SDaniel C. SobralDATE=`date "+%Y%m%d%H%M"` 145256ab0efSSheldon Hearncp ${edit_file} ${edit_file}.$DATE 146256ab0efSSheldon Hearnmv ${edit_file}.new ${edit_file} 1477b4d3c72SDaniel C. Sobralcat <<! 148f212b184SChristian BruefferThe new rules are now installed. The previous rules have been preserved in 149256ab0efSSheldon Hearnthe file ${edit_file}.$DATE 1507b4d3c72SDaniel C. Sobral! 151c165f4abSHiroki Satodiff -u ${edit_file}.$DATE ${edit_file} \ 152256ab0efSSheldon Hearn | mail -s "`hostname` Firewall rule change" root 153256ab0efSSheldon Hearnrm ${TMPFILE} 154256ab0efSSheldon Hearnexit 0 155