root/5100R/branches/DEV_OpenRaQ/ui/base-firewall.mod/constructor/50_initialize_ruleset.pl

Revision 764, 5.7 kB (checked in by shibuya, 4 years ago)

build for 5100R.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 #!/usr/bin/perl -I /usr/sausalito/perl
2 # $Id$
3 # Copyright 2002, Sun Microsystems, Inc., All rights reserved.
4 #
5 # Builds a set of good-practice firewall rules.
6 # Opens listed ports to restricted_nics.  Denies all connections to
7 # closed_nics, and allows all access to nics.
8 #
9 # Ports are hash integer ranges; base => upper
10 #
11 # This is run once, on first activation of CCE.  Error detection for
12 # failed cce transactions are ignored--They have no context in a
13 # factory constructor.  Oh well.
14
15 my $DEBUG = 0;
16 my $chain = 'input';
17
18 # default description, not used in UI as of 8/02
19 my $description = 'Default Ruleset';   
20
21 # The owner is essential to identifying this default firewall config
22 my $owner = 'default'
23
24 # wide-open, restricted, and closed network interfaces
25 my @nics = ('lo');
26 my @restricted_nics = ('eth0'); # WAN, selective
27 my @closed_nics = ();                   # Denies all
28
29 # each protocol must have a hash port map of the same name
30 my @protocols = ('tcp', 'udp', 'icmp', 'all');
31
32 my %pr;                                 # port range
33 $pr{'udp'} = {
34         7 => 7,         # echo
35         20 => 23,       # ftp, telnet, ssh
36         25 => 25,       # smtp
37         42 => 42,       # nameserver
38         53 => 53,       # dns
39         110 => 110,     # pop
40         123 => 123,     # ntp
41         143 => 143,     # imap
42 #       389 => 389,     # ldap
43         161 => 162,     # snmp and snmp-trap
44         465 => 465,     # smtps
45         520 => 520,     # RIP; not used ???
46         587 => 587,     # submission port
47         993 => 993,     # imaps
48         995 => 995,     # pop3s
49         1024 => 65535,  # active mode ftp
50         };
51 $pr{'tcp'} = {
52         7 => 7,         # echo
53         20 => 23,       # ftp, telnet, ssh
54         25 => 25,       # smtp
55         42 => 42,       # nameserver
56         53 => 53,       # dns
57         80 => 80,       # http
58         81 => 81,       # Cobalt admserv with ssl
59         110 => 110,     # pop
60         123 => 123,     # ntp
61         143 => 143,     # imap
62 #       389 => 389,     # ldap
63         443 => 443,     # HTTPS
64         444 => 444,     # Cobalt admserv
65         465 => 465,     # smtps
66         587 => 587,     # submission port
67         993 => 993,     # imaps
68         995 => 995,     # pop3s
69         1024 => 65535,  # user apps
70         };
71 $pr{'icmp'} = {
72         };
73 $pr{'all'} = {
74         };
75
76 use Sauce::Service;
77 use CCE;
78 my $cce = new CCE;
79 $cce->connectuds();
80
81 my $sysoid = ($cce->find('System'))[0];
82 my ($ok, $fw) = $cce->get($sysoid, 'Firewall');
83
84 if(!$ok || $fw->{initialized}) {
85         Sauce::Service::service_toggle_init('iptables', 1);
86         $cce->bye('SUCCESS');
87         exit 0;
88 }
89
90 # delete an existing default ruleset (Will's paranoia)
91 foreach my $rip ($cce->find('FirewallRule', {'owner' => $owner})) {
92         $cce->destroy($rip);
93 }
94
95 # cycle through each nic, adding rules as necessary
96 foreach my $nic (@nics) {
97         $DEBUG && warn "opening access to $nic";
98         # add all-accept rule
99         $cce->create('FirewallRule', {
100                 'policy' => 'ACCEPT',
101                 'interface' => $nic,
102                 'protocol' => 'all',
103                 'owner' => $owner,
104                 'description' => $description,
105                 }, undef);
106 }
107 foreach my $nic (@restricted_nics) {
108         $DEBUG && warn "selectively access to $nic";
109         foreach my $proto (@protocols) {
110                 while (my($port,$up) = each %{$pr{$proto}}) {
111                         $DEBUG && warn "protocol: $proto, base port: $port, upper: $up\n";
112                         $cce->create('FirewallRule', {
113                                 'policy' => 'ACCEPT',
114                                 'interface' => $nic,
115                                 'dest_ports' => $port.':'.$up,
116                                 'protocol' => $proto,
117                                 'owner' => $owner,
118                                 'description' => $description,
119                                 }, undef);
120                 }
121         }
122 }
123
124
125 # Create/edit the $chain & set default policy and associate the default ruleset
126 my @chains = $cce->find('FirewallChain', {'name' => $chain});
127 my @new = $cce->find('FirewallRule', {'owner' => $owner});
128 if ($chains[0]) {
129         $DEBUG && warn "Found chain policy $oid, setting DENY";
130
131         my ($ok, $chain) = $cce->get($oid);
132         my @old = $cce->scalar_to_array( $chain->{rules} );
133         push (@old, @new);
134         my $rules = $cce->array_to_scalar( @old );
135
136         $cce->set($chains[0], undef, {
137                 'default' => 'DENY',
138                 'rules' => $rules,
139                 });
140 } else {
141         $DEBUG && warn "Creating $chain chain policy";
142
143         my $rules = $cce->array_to_scalar( @new );
144         $cce->create('FirewallChain', {
145                 'name' => $chain,
146                 'default' => 'DENY',
147                 'rules' => $rules,
148                 }, undef);
149 }
150
151 # Mark initialized, enable, commit
152 my $time = time();
153 $DEBUG && warn "commit changes at $time";
154 $cce->set($sysoid, 'Firewall', {
155         'initialized' => 1,
156         'enabled' => 0,
157         });
158 $cce->set($sysoid, 'Firewall', {
159         'commit' => $time,
160         });
161
162 $cce->bye('SUCCESS');
163 exit 0;
164
165
166 # Copyright (c) 2003 Sun Microsystems, Inc. All  Rights Reserved.
167 #
168 # Redistribution and use in source and binary forms, with or without
169 # modification, are permitted provided that the following conditions are met:
170 #
171 # -Redistribution of source code must retain the above copyright notice,
172 # this list of conditions and the following disclaimer.
173 #
174 # -Redistribution in binary form must reproduce the above copyright notice,
175 # this list of conditions and the following disclaimer in the documentation 
176 # and/or other materials provided with the distribution.
177 #
178 # Neither the name of Sun Microsystems, Inc. or the names of contributors may
179 # be used to endorse or promote products derived from this software without
180 # specific prior written permission.
181 #
182 # This software is provided "AS IS," without a warranty of any kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
183 #
184 # You acknowledge that  this software is not designed or intended for use in the design, construction, operation or maintenance of any nuclear facility.
185
Note: See TracBrowser for help on using the browser.