Handle hostnames with upper-case letters
[webmin.git] / ipfw / index.cgi
1 #!/usr/local/bin/perl
2 # Show all firewall rules
3
4 require './ipfw-lib.pl';
5 &ReadParse();
6
7 # Make sure the ipfw command is installed
8 if (!&has_command($config{'ipfw'})) {
9         &ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1);
10         &ui_print_endpage(
11                 &ui_config_link('index_eipfw',
12                         [ "<tt>$config{'ipfw'}</tt>", undef ]));
13         }
14
15 # Make sure ipfw works
16 $rules = &get_config();
17 $active = &get_config("$config{'ipfw'} show |", \$out);
18 if ($?) {
19         &ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1);
20         &ui_print_endpage(
21                 &ui_config_link('index_elist',
22                         [ "<tt>$config{'ipfw'} list</tt>",
23                           "<pre>$out</pre>", undef ]));
24         }
25
26 # Get the version number
27 $vout = `$config{'ipfw'} 2>&1`;
28 $vouth = `$config{'ipfw'} -h 2>&1`;
29 if ($vout =~ /preproc/ || $vouth =~ /preproc/) {
30         $ipfw_version = 2;
31         }
32 else {
33         $ipfw_version = 1;
34         }
35 open(VERSION, ">$module_config_directory/version");
36 print VERSION $ipfw_version,"\n";
37 close(VERSION);
38 &ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1, 0,
39                  &help_search_link("ipfw", "man", "doc", "google"),
40                  undef, undef,
41                  &text('index_version', $ipfw_version));
42
43 # Check for an active firewall that is not managed by this module
44 if (!@$rules && @$active > 1) {
45         # Yes .. offer to convert
46         print &text('index_existing', scalar(@$active),
47                     "<tt>$ipfw_file</tt>"),"<p>\n";
48         print &ui_form_start("convert.cgi");
49         print "<center>",&ui_submit($text{'index_saveex'}),"<p>\n";
50         print "</center>\n";
51         print &ui_form_end();
52
53         print "<table border width=100%>\n";
54         print "<tr $tb><td><b>$text{'index_headerex'}</b></td></tr>\n";
55         print "<tr $cb> <td><pre>";
56         print $out;
57         print "</pre></td> </tr></table>\n";
58         }
59 elsif (@$rules && !$in{'reset'}) {
60         # Find last editable rule
61         if ($rules->[@$rules-1]->{'num'} == 65535 &&
62             @$rules > 1) {
63                 $lastidx = $rules->[@$rules-2]->{'index'};
64                 }
65         else {
66                 $lastidx = $rules->[@$rules-1]->{'index'};
67                 }
68
69         # Build map of active rules
70         local %amap = map { int($_->{'num'}), $_ } @$active;
71
72         # Show the rules
73         print &ui_form_start("edit_rule.cgi");
74         local @widths = ( "width=10", "width=5%", undef );
75         push(@widths, undef) if ($config{'view_condition'});
76         push(@widths, undef) if ($config{'view_comment'});
77         push(@widths, "width=5%", "width=5% nowrap") if ($config{'view_counters'});
78         push(@widths, "width=5%", "width=5%");
79         @links = ( &select_all_link("d", 0),
80                    &select_invert_link("d", 0) );
81         print &ui_links_row(\@links);
82         print &ui_columns_start([ "",
83                                   $text{'index_num'},
84                                   $text{'index_action'},
85                                   $config{'view_condition'} ?
86                                         ( $text{'index_desc'} ) : ( ),
87                                   $config{'view_comment'} ?
88                                         ( $text{'index_cmt'} ) : ( ),
89                                   $config{'view_counters'} ?
90                                         ( $text{'index_count1'},
91                                           $text{'index_count2'} ) : ( ),
92                                   $text{'index_move'},
93                                   $text{'index_radd'} ], 100, 0,
94                                 \@widths);
95         foreach $r (@$rules) {
96                 next if ($r->{'other'});        # Not displayable
97                 local ($mover, $adder);
98                 $mover = &ui_up_down_arrows(
99                         "move.cgi?idx=$r->{'index'}&up=1",
100                         "move.cgi?idx=$r->{'index'}&down=1",
101                         $r->{'index'} != 0 && $r->{'index'} <= $lastidx,
102                         $r->{'index'} < $lastidx);
103                 if ($r->{'index'} <= $lastidx) {
104                         $adder .= "<a href='edit_rule.cgi?new=1&".
105                                   "after=$r->{'index'}'>".
106                                   "<img src=images/after.gif border=0></a>";
107                         $adder .= "<a href='edit_rule.cgi?new=1&".
108                                   "before=$r->{'index'}'>".
109                                   "<img src=images/before.gif border=0></a>";
110                         }
111
112                 local ($ls, $le);
113                 if ($r->{'index'} <= $lastidx) {
114                         $ls = "<a href='edit_rule.cgi?idx=$r->{'index'}'>";
115                         $le = "</a>";
116                         }
117                 local $act = $amap{int($r->{'num'})};
118                 print &ui_checked_columns_row(
119                         [ $ls.$r->{'num'}.$le,
120                           $ls.($text{'action_'.&real_action($r->{'action'})} ||
121                            uc($r->{'action'})).
122                           (defined($r->{'aarg'}) ? " $r->{'aarg'}" : "").$le,
123                           $config{'view_condition'} ? ( &describe_rule($r) ) : ( ),
124                           $config{'view_comment'} ? ( $r->{'cmt'} || "<br>" ) : ( ),
125                           $config{'view_counters'} ? ( $act->{'count1'}, &nice_size($act->{'count2'}) ) : ( ),
126                           $mover,
127                           $adder ],
128                         \@widths, "d", $r->{'num'});
129                 }
130         print &ui_columns_end();
131         print &ui_links_row(\@links);
132
133         # Show delete and add buttons
134         print "<table width=100%><tr>\n";
135         print "<td align=left>",
136               &ui_submit($text{'index_delete'}, "delsel"),"</td>\n";
137         print "<td align=right>",
138               &ui_submit($text{'index_add2'}, "new"),"</td>\n";
139         print "</tr></table>\n";
140         print &ui_form_end();
141
142         # Show buttons to apply configuration and start at boot
143         print &ui_hr();
144
145         $atboot = &check_boot();
146         print &ui_buttons_start();
147         if (&foreign_check("servers")) {
148                 @servers = &list_cluster_servers();
149                 }
150         print &ui_buttons_row("apply.cgi", $text{'index_apply'},
151                               @servers ? $text{'index_applydesc2'}
152                                        : $text{'index_applydesc'});
153         print &ui_buttons_row("unapply.cgi", $text{'index_unapply'},
154                               $text{'index_unapplydesc'});
155         if ($atboot != -1) {
156                 print &ui_buttons_row("bootup.cgi", $text{'index_boot'},
157                                       $text{'index_bootdesc'}, undef,
158                                       &ui_radio("boot", $atboot ? 1 : 0,
159                                                 [ [ 1, $text{'yes'} ],
160                                                   [ 0, $text{'no'} ] ]));
161                 }
162         print &ui_buttons_row("index.cgi", $text{'index_reset'},
163                               $text{'index_resetdesc'}, undef,
164                               &ui_hidden("reset", 1));
165         # Show button for cluster page
166         if (&foreign_check("servers")) {
167                 &foreign_require("servers", "servers-lib.pl");
168                 @allservers = grep { $_->{'user'} }
169                                 &servers::list_servers();
170                 }
171         if (@allservers) {
172                 print &ui_buttons_row("cluster.cgi", $text{'index_cluster'},
173                                       $text{'index_clusterdesc'});
174                 }
175         print &ui_buttons_end();
176         }
177 else {
178         # Offer to setup simple firewall
179         print &text($in{'reset'} ? 'index_rsetup' : 'index_setup',
180                     "<tt>$ipfw_file</tt>"),"<p>\n";
181         print "<form action=setup.cgi>\n";
182         print &ui_hidden("reset", $in{'reset'});
183         print "<center><table><tr><td>\n";
184         print "<input type=radio name=auto value=0 checked> ",
185               "$text{'index_auto0'}<p>\n";
186         foreach $a (2 .. 4) {
187                 print "<input type=radio name=auto value=$a> ",
188                       "$text{'index_auto'.$a} ",
189                       &interface_choice("iface".$a, undef, 1),"<p>\n";
190                 }
191         print "</td></tr></table>\n";
192         print "<input type=submit value='$text{'index_auto'}'><p>\n";
193         print "<input type=checkbox name=atboot value=1> ",
194               "$text{'index_atboot'}\n";
195         print "</center></form>\n";
196         }
197
198 &ui_print_footer("/", $text{'index'});
199