This repo is obsolete, please see git://git.code.sf.net/p/dummynet/code@master
[ipfw.git] / configuration / change_rules.sh
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: src/share/examples/ipfw/change_rules.sh,v 1.6 2003/09/07 07:52:56 jmg Exp $
28
29 # Change ipfw(8) rules with safety guarantees for remote operation
30 #
31 # Invoke this script to edit ${firewall_script}. It will call ${EDITOR},
32 # or vi(1) if the environment variable is not set, for you to edit
33 # ${firewall_script}, ask for confirmation, and then run
34 # ${firewall_script}. You can then examine the output of ipfw list and
35 # confirm whether you want the new version or not.
36 #
37 # If no answer is received in 30 seconds, the previous
38 # ${firewall_script} is run, restoring the old rules (this assumes ipfw
39 # flush is present in it).
40 #
41 # If the new rules are confirmed, they'll replace ${firewall_script} and
42 # the previous ones will be copied to ${firewall_script}.{date}. Mail
43 # will also be sent to root with a unified diff of the rule change.
44 #
45 # Unapproved rules are kept in ${firewall_script}.new, and you are
46 # offered the option of changing them instead of the present rules when
47 # you call this script.
48 #
49 # This script could be improved by using version control
50 # software.
51
52 # XXX on linux /etc/rc.conf defines:
53 # firewall_type and firewall_script
54
55 if [ -r /etc/defaults/rc.conf ]; then
56         . /etc/defaults/rc.conf
57         source_rc_confs
58 elif [ -r /etc/rc.conf ]; then
59         . /etc/rc.conf
60 fi
61
62 EDITOR=${EDITOR:-/usr/bin/vi}
63 PAGER=${PAGER:-/usr/bin/more}
64
65 # on linux the default mktemp invocation behavior
66 # is different, we should change the temporary file creation
67 tempfoo=`basename $0`
68 #TMPFILE=`mktemp -t ${tempfoo}` || exit 1
69 TMPFILE=`mktemp -t ${tempfoo}.XXXXX` || exit 1
70
71 get_yes_no() {
72         while true
73         do
74                 echo -n "$1 (Y/N) ? " 
75                 read -t 30 a
76                 if [ $? != 0 ]; then
77                         a="No";
78                         return;
79                 fi
80                 case $a in
81                         [Yy]) a="Yes";
82                               return;;
83                         [Nn]) a="No";
84                               return;;
85                         *);;
86                 esac
87         done
88 }
89
90 restore_rules() {
91         nohup sh ${firewall_script} </dev/null >/dev/null 2>&1
92         rm ${TMPFILE}
93         exit 1
94 }
95
96 case "${firewall_type}" in
97 [Cc][Ll][Ii][Ee][Nn][Tt]|\
98 [Cc][Ll][Oo][Ss][Ee][Dd]|\
99 [Oo][Pp][Ee][Nn]|\
100 [Ss][Ii][Mm][Pp][Ll][Ee]|\
101 [Uu][Nn][Kk][Nn][Oo][Ww][Nn])
102         edit_file="${firewall_script}"
103         rules_edit=no
104         ;;
105 *)
106         if [ -r "${firewall_type}" ]; then
107                 edit_file="${firewall_type}"
108                 rules_edit=yes
109         fi
110         ;;
111 esac
112
113 if [ -f ${edit_file}.new ]; then
114         get_yes_no "A new rules file already exists, do you want to use it"
115         [ $a = 'No' ] && cp ${edit_file} ${edit_file}.new
116 else 
117         cp ${edit_file} ${edit_file}.new
118 fi
119
120 trap restore_rules SIGHUP
121
122 ${EDITOR} ${edit_file}.new
123
124 get_yes_no "Do you want to install the new rules"
125
126 [ $a = 'No' ] && exit 1
127
128 cat <<!
129 The rules will be changed now. If the message 'Type y to keep the new
130 rules' does not appear on the screen or the y key is not pressed in 30
131 seconds, the original rules will be restored.
132 The TCP/IP connections might be broken during the change. If so, restore
133 the ssh/telnet connection being used.
134 !
135
136 if [ ${rules_edit} = yes ]; then
137         nohup sh ${firewall_script} ${firewall_type}.new \
138             < /dev/null > ${TMPFILE} 2>&1
139 else
140         nohup sh ${firewall_script}.new \
141             < /dev/null > ${TMPFILE} 2>&1
142 fi
143 sleep 2;
144 get_yes_no "Would you like to see the resulting new rules"
145 [ $a = 'Yes' ] && ${PAGER} ${TMPFILE}
146 get_yes_no "Type y to keep the new rules"
147 [ $a != 'Yes' ] && restore_rules
148
149 DATE=`date "+%Y%m%d%H%M"`
150 cp ${edit_file} ${edit_file}.$DATE
151 mv ${edit_file}.new ${edit_file} 
152 cat <<!
153 The new rules are now installed. The previous rules have been preserved in
154 the file ${edit_file}.$DATE
155 !
156 diff -F "^# .*[A-Za-z]" -u ${edit_file}.$DATE ${edit_file} \
157     | mail -s "`hostname` Firewall rule change" root
158 rm ${TMPFILE}
159 exit 0