]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - etc/rc.firewall
Create group ftp by default. This is gid 14 as this is the historical
[FreeBSD/FreeBSD.git] / etc / rc.firewall
1 #!/bin/sh -
2 # Copyright (c) 1996  Poul-Henning Kamp
3 # All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 # 1. Redistributions of source code must retain the above copyright
9 #    notice, this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright
11 #    notice, this list of conditions and the following disclaimer in the
12 #    documentation and/or other materials provided with the distribution.
13 #
14 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 # SUCH DAMAGE.
25 #
26 # $FreeBSD$
27 #
28
29 #
30 # Setup system for ipfw(4) firewall service.
31 #
32
33 # Suck in the configuration variables.
34 if [ -z "${source_rc_confs_defined}" ]; then
35         if [ -r /etc/defaults/rc.conf ]; then
36                 . /etc/defaults/rc.conf
37                 source_rc_confs
38         elif [ -r /etc/rc.conf ]; then
39                 . /etc/rc.conf
40         fi
41 fi
42
43 ############
44 # Define the firewall type in /etc/rc.conf.  Valid values are:
45 #   open        - will allow anyone in
46 #   client      - will try to protect just this machine
47 #   simple      - will try to protect a whole network
48 #   closed      - totally disables IP services except via lo0 interface
49 #   workstation - will try to protect just this machine using statefull
50 #                 firewalling. See below for rc.conf variables used
51 #   UNKNOWN     - disables the loading of firewall rules.
52 #   filename    - will load the rules in the given filename (full path required)
53 #
54 # For ``client'' and ``simple'' the entries below should be customized
55 # appropriately.
56
57 ############
58 #
59 # If you don't know enough about packet filtering, we suggest that you
60 # take time to read this book:
61 #
62 #       Building Internet Firewalls, 2nd Edition
63 #       Brent Chapman and Elizabeth Zwicky
64 #
65 #       O'Reilly & Associates, Inc
66 #       ISBN 1-56592-871-7
67 #       http://www.ora.com/
68 #       http://www.oreilly.com/catalog/fire2/
69 #
70 # For a more advanced treatment of Internet Security read:
71 #
72 #       Firewalls & Internet Security
73 #       Repelling the wily hacker
74 #       William R. Cheswick, Steven M. Bellowin
75 #
76 #       Addison-Wesley
77 #       ISBN 0-201-63357-4
78 #       http://www.awl.com/
79 #       http://www.awlonline.com/product/0%2C2627%2C0201633574%2C00.html
80 #
81
82 setup_loopback () {
83         ############
84         # Only in rare cases do you want to change these rules
85         #
86         ${fwcmd} add 100 pass all from any to any via lo0
87         ${fwcmd} add 200 deny all from any to 127.0.0.0/8
88         ${fwcmd} add 300 deny ip from 127.0.0.0/8 to any
89 }
90
91 if [ -n "${1}" ]; then
92         firewall_type="${1}"
93 fi
94
95 ############
96 # Set quiet mode if requested
97 #
98 case ${firewall_quiet} in
99 [Yy][Ee][Ss])
100         fwcmd="/sbin/ipfw -q"
101         ;;
102 *)
103         fwcmd="/sbin/ipfw"
104         ;;
105 esac
106
107 ############
108 # Flush out the list before we begin.
109 #
110 ${fwcmd} -f flush
111
112 setup_loopback
113
114 ############
115 # Network Address Translation.  All packets are passed to natd(8)
116 # before they encounter your remaining rules.  The firewall rules
117 # will then be run again on each packet after translation by natd
118 # starting at the rule number following the divert rule.
119 #
120 # For ``simple'' firewall type the divert rule should be put to a
121 # different place to not interfere with address-checking rules.
122 #
123 case ${firewall_type} in
124 [Oo][Pp][Ee][Nn]|[Cc][Ll][Ii][Ee][Nn][Tt])
125         case ${natd_enable} in
126         [Yy][Ee][Ss])
127                 if [ -n "${natd_interface}" ]; then
128                         ${fwcmd} add 50 divert natd ip4 from any to any via ${natd_interface}
129                 fi
130                 ;;
131         esac
132         case ${firewall_nat_enable} in
133         [Yy][Ee][Ss])
134                 if [ -n "${firewall_nat_interface}" ]; then
135                         ${fwcmd} nat 123 config if ${firewall_nat_interface} log
136                         ${fwcmd} add 50 nat 123 ip4 from any to any via ${firewall_nat_interface}
137                 fi
138                 ;;
139         esac
140 esac
141
142 ############
143 # If you just configured ipfw in the kernel as a tool to solve network
144 # problems or you just want to disallow some particular kinds of traffic
145 # then you will want to change the default policy to open.  You can also
146 # do this as your only action by setting the firewall_type to ``open''.
147 #
148 # ${fwcmd} add 65000 pass all from any to any
149
150
151 # Prototype setups.
152 #
153 case ${firewall_type} in
154 [Oo][Pp][Ee][Nn])
155         ${fwcmd} add 65000 pass all from any to any
156         ;;
157
158 [Cc][Ll][Ii][Ee][Nn][Tt])
159         ############
160         # This is a prototype setup that will protect your system somewhat
161         # against people from outside your own network.
162         ############
163
164         # set these to your network and netmask and ip
165         net="192.0.2.0"
166         mask="255.255.255.0"
167         ip="192.0.2.1"
168
169         # Allow any traffic to or from my own net.
170         ${fwcmd} add pass all from ${ip} to ${net}:${mask}
171         ${fwcmd} add pass all from ${net}:${mask} to ${ip}
172
173         # Allow TCP through if setup succeeded
174         ${fwcmd} add pass tcp from any to any established
175
176         # Allow IP fragments to pass through
177         ${fwcmd} add pass all from any to any frag
178
179         # Allow setup of incoming email
180         ${fwcmd} add pass tcp from any to me 25 setup
181
182         # Allow setup of outgoing TCP connections only
183         ${fwcmd} add pass tcp from me to any setup
184
185         # Disallow setup of all other TCP connections
186         ${fwcmd} add deny tcp from any to any setup
187
188         # Allow DNS queries out in the world
189         ${fwcmd} add pass udp from me to any 53 keep-state
190
191         # Allow NTP queries out in the world
192         ${fwcmd} add pass udp from me to any 123 keep-state
193
194         # Everything else is denied by default, unless the
195         # IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
196         # config file.
197         ;;
198
199 [Ss][Ii][Mm][Pp][Ll][Ee])
200         ############
201         # This is a prototype setup for a simple firewall.  Configure this
202         # machine as a DNS and NTP server, and point all the machines
203         # on the inside at this machine for those services.
204         ############
205
206         # set these to your outside interface network and netmask and ip
207         oif="ed0"
208         onet="192.0.2.0"
209         omask="255.255.255.240"
210         oip="192.0.2.1"
211
212         # set these to your inside interface network and netmask and ip
213         iif="ed1"
214         inet="192.0.2.16"
215         imask="255.255.255.240"
216         iip="192.0.2.17"
217
218         # Stop spoofing
219         ${fwcmd} add deny all from ${inet}:${imask} to any in via ${oif}
220         ${fwcmd} add deny all from ${onet}:${omask} to any in via ${iif}
221
222         # Stop RFC1918 nets on the outside interface
223         ${fwcmd} add deny all from any to 10.0.0.0/8 via ${oif}
224         ${fwcmd} add deny all from any to 172.16.0.0/12 via ${oif}
225         ${fwcmd} add deny all from any to 192.168.0.0/16 via ${oif}
226
227         # Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
228         # DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
229         # on the outside interface
230         ${fwcmd} add deny all from any to 0.0.0.0/8 via ${oif}
231         ${fwcmd} add deny all from any to 169.254.0.0/16 via ${oif}
232         ${fwcmd} add deny all from any to 192.0.2.0/24 via ${oif}
233         ${fwcmd} add deny all from any to 224.0.0.0/4 via ${oif}
234         ${fwcmd} add deny all from any to 240.0.0.0/4 via ${oif}
235
236         # Network Address Translation.  This rule is placed here deliberately
237         # so that it does not interfere with the surrounding address-checking
238         # rules.  If for example one of your internal LAN machines had its IP
239         # address set to 192.0.2.1 then an incoming packet for it after being
240         # translated by natd(8) would match the `deny' rule above.  Similarly
241         # an outgoing packet originated from it before being translated would
242         # match the `deny' rule below.
243         case ${natd_enable} in
244         [Yy][Ee][Ss])
245                 if [ -n "${natd_interface}" ]; then
246                         ${fwcmd} add divert natd all from any to any via ${natd_interface}
247                 fi
248                 ;;
249         esac
250
251         # Stop RFC1918 nets on the outside interface
252         ${fwcmd} add deny all from 10.0.0.0/8 to any via ${oif}
253         ${fwcmd} add deny all from 172.16.0.0/12 to any via ${oif}
254         ${fwcmd} add deny all from 192.168.0.0/16 to any via ${oif}
255
256         # Stop draft-manning-dsua-03.txt (1 May 2000) nets (includes RESERVED-1,
257         # DHCP auto-configuration, NET-TEST, MULTICAST (class D), and class E)
258         # on the outside interface
259         ${fwcmd} add deny all from 0.0.0.0/8 to any via ${oif}
260         ${fwcmd} add deny all from 169.254.0.0/16 to any via ${oif}
261         ${fwcmd} add deny all from 192.0.2.0/24 to any via ${oif}
262         ${fwcmd} add deny all from 224.0.0.0/4 to any via ${oif}
263         ${fwcmd} add deny all from 240.0.0.0/4 to any via ${oif}
264
265         # Allow TCP through if setup succeeded
266         ${fwcmd} add pass tcp from any to any established
267
268         # Allow IP fragments to pass through
269         ${fwcmd} add pass all from any to any frag
270
271         # Allow setup of incoming email
272         ${fwcmd} add pass tcp from any to ${oip} 25 setup
273
274         # Allow access to our DNS
275         ${fwcmd} add pass tcp from any to ${oip} 53 setup
276         ${fwcmd} add pass udp from any to ${oip} 53
277         ${fwcmd} add pass udp from ${oip} 53 to any
278
279         # Allow access to our WWW
280         ${fwcmd} add pass tcp from any to ${oip} 80 setup
281
282         # Reject&Log all setup of incoming connections from the outside
283         ${fwcmd} add deny log tcp from any to any in via ${oif} setup
284
285         # Allow setup of any other TCP connection
286         ${fwcmd} add pass tcp from any to any setup
287
288         # Allow DNS queries out in the world
289         ${fwcmd} add pass udp from ${oip} to any 53 keep-state
290
291         # Allow NTP queries out in the world
292         ${fwcmd} add pass udp from ${oip} to any 123 keep-state
293
294         # Everything else is denied by default, unless the
295         # IPFIREWALL_DEFAULT_TO_ACCEPT option is set in your kernel
296         # config file.
297         ;;
298
299 [Ww][Oo][Rr][Kk][Ss][Tt][Aa][Tt][Ii][Oo][Nn])
300         # Configuration:
301         #  firewall_myservices:         List of TCP ports on which this host
302         #                                offers services.
303         #  firewall_allowservices:      List of IPs which has access to
304         #                                $firewall_myservices.
305         #  firewall_trusted:            List of IPs which has full access 
306         #                                to this host. Be very carefull 
307         #                                when setting this. This option can
308         #                                seriously degrade the level of 
309         #                                protection provided by the firewall.
310         #  firewall_logdeny:            Boolean (YES/NO) specifying if the
311         #                                default denied packets should be
312         #                                logged (in /var/log/security).
313         #  firewall_nologports:         List of TCP/UDP ports for which
314         #                                denied incomming packets are not
315         #                                logged.
316         
317         # Allow packets for which a state has been built.
318         ${fwcmd} add check-state
319
320         # For services permitted below.
321         ${fwcmd} add pass tcp  from me to any established
322
323         # Allow any connection out, adding state for each.
324         ${fwcmd} add pass tcp  from me to any setup keep-state
325         ${fwcmd} add pass udp  from me to any       keep-state
326         ${fwcmd} add pass icmp from me to any       keep-state
327
328         # Allow DHCP.
329         ${fwcmd} add pass udp  from 0.0.0.0 68 to 255.255.255.255 67 out
330         ${fwcmd} add pass udp  from any 67     to me 68 in
331         ${fwcmd} add pass udp  from any 67     to 255.255.255.255 68 in
332         # Some servers will ping the IP while trying to decide if it's 
333         # still in use.
334         ${fwcmd} add pass icmp from any to any icmptype 8
335
336         # Allow "mandatory" ICMP in.
337         ${fwcmd} add pass icmp from any to any icmptype 3,4,11
338         
339         # Add permits for this workstations published services below
340         # Only IPs and nets in firewall_allowservices is allowed in.
341         # If you really wish to let anyone use services on your 
342         # workstation, then set "firewall_allowservices='any'" in /etc/rc.conf
343         #
344         # Note: We don't use keep-state as that would allow DoS of
345         #       our statetable. 
346         #       You can add 'keep-state' to the lines for slightly
347         #       better performance if you fell that DoS of your
348         #       workstation won't be a problem.
349         #
350         for i in ${firewall_allowservices} ; do
351           for j in ${firewall_myservices} ; do
352             ${fwcmd} add pass tcp from $i to me $j
353           done
354         done
355
356         # Allow all connections from trusted IPs.
357         # Playing with the content of firewall_trusted could seriously
358         # degrade the level of protection provided by the firewall.
359         for i in ${firewall_trusted} ; do
360           ${fwcmd} add pass ip from $i to me
361         done
362         
363         ${fwcmd} add 65000 count ip from any to any
364
365         # Drop packets to ports where we don't want logging
366         for i in ${firewall_nologports} ; do
367           ${fwcmd} add deny { tcp or udp } from any to any $i in
368         done
369
370         # Broadcasts and muticasts
371         ${fwcmd} add deny ip  from any to 255.255.255.255
372         ${fwcmd} add deny ip  from any to 224.0.0.0/24 in       # XXX
373
374         # Noise from routers
375         ${fwcmd} add deny udp from any to any 520 in
376
377         # Noise from webbrowsing.
378         # The statefull filter is a bit agressive, and will cause some
379         #  connection teardowns to be logged.
380         ${fwcmd} add deny tcp from any 80,443 to any 1024-65535 in
381
382         # Deny and (if wanted) log the rest unconditionally.
383         log=""
384         if [ ${firewall_logdeny:-x} = "YES" -o ${firewall_logdeny:-x} = "yes" ] ; then
385           log="log logamount 500"       # The default of 100 is too low.
386           sysctl net.inet.ip.fw.verbose=1 >/dev/null
387         fi
388         ${fwcmd} add deny $log ip from any to any
389         ;;
390
391 [Cc][Ll][Oo][Ss][Ee][Dd])
392         ${fwcmd} add 65000 deny ip from any to any
393         ;;
394 [Uu][Nn][Kk][Nn][Oo][Ww][Nn])
395         ;;
396 *)
397         if [ -r "${firewall_type}" ]; then
398                 ${fwcmd} ${firewall_flags} ${firewall_type}
399         fi
400         ;;
401 esac