Handle hostnames with upper-case letters
[webmin.git] / ipfilter / index.cgi
1 #!/usr/local/bin/perl
2 # Show all firewall rules
3
4 require './ipfilter-lib.pl';
5 &ReadParse();
6
7 # Make sure the ipf command is installed
8 $cmd = &missing_firewall_commands();
9 if ($cmd) {
10         &ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1);
11         &ui_print_endpage(
12                 &ui_config_link('index_ecmd',
13                         [ "<tt>$cmd</tt>", undef ]));
14         }
15
16 # Get the version number
17 $vout = `$config{'ipf'} -V 2>&1`;
18 if ($vout =~ /IP\s+Filter:\s+v?(\S+)/i) {
19         $ipf_version = $1;
20         }
21 open(VERSION, ">$module_config_directory/version");
22 print VERSION $ipf_version,"\n";
23 close(VERSION);
24 &ui_print_header(undef, $text{'index_title'}, "", undef, 1, 1, 0,
25                  &help_search_link("ipf", "man", "doc", "google"),
26                  undef, undef,
27                  &text('index_version', $ipf_version));
28
29 # See if enabled at boot
30 $atboot = &check_firewall_init();
31
32 # Get the saved and active rules
33 $live = &get_live_config();
34 $rules = &get_config();
35
36 if (!-r $config{'ipf_conf'} && @$live) {
37         # Offer to save existing rules
38         print &text('index_existing', scalar(@$live),
39                     "<tt>$config{'ipf_conf'}</tt>"),"<p>\n";
40         print &ui_form_start("convert.cgi");
41         print "<center>",&ui_submit($text{'index_saveex'}),"<p>\n";
42         print &ui_checkbox("atboot", 1, $text{'index_atboot'},
43                            $atboot == 2),"\n";
44         print "</center>",&ui_form_end(),"<p>\n";
45
46         print "<table border width=100%>\n";
47         print "<tr $tb><td><b>$text{'index_headerex'}</b></td></tr>\n";
48         print "<tr $cb> <td><pre>";
49         foreach $dir ("i", "o") {
50                 open(OUT, "$config{'ipfstat'} -$dir |");
51                 while(<OUT>) {
52                         print &html_escape($_);
53                         }
54                 close(OUT);
55                 }
56         print "</pre></td> </tr></table>\n";
57         }
58 elsif (@$rules && !$in{'reset'}) {
59         # Show the rules
60         print &ui_form_start("edit_rule.cgi");
61         local @widths = ( "width=10", "width=5%", "width=10%", undef );
62         push(@widths, undef) if ($config{'view_condition'});
63         push(@widths, undef) if ($config{'view_comment'});
64         push(@widths, "width=5%", "width=5%");
65         @links = ( &select_all_link("d", 0),
66                    &select_invert_link("d", 0) );
67         print &ui_links_row(\@links);
68         print &ui_columns_start([ "",
69                                   $text{'index_active'},
70                                   $text{'index_action'},
71                                   $text{'index_dir'},
72                                   $config{'view_condition'} ?
73                                         ( $text{'index_desc'} ) : ( ),
74                                   $config{'view_comment'} ?
75                                         ( $text{'index_cmt'} ) : ( ),
76                                   $text{'index_move'},
77                                   $text{'index_radd'} ], 100, 0,
78                                 \@widths);
79         foreach $r (@$rules) {
80                 local ($mover, $adder);
81                 if ($r eq $rules->[@$rules-1]) {
82                         $mover .= "<img src=images/gap.gif>";
83                         }
84                 else {
85                         $mover .= "<a href='move.cgi?idx=$r->{'index'}&".
86                                   "down=1'><img src=".
87                                   "images/down.gif border=0></a>";
88                         }
89                 if ($r eq $rules->[0]) {
90                         $mover .= "<img src=images/gap.gif>";
91                         }
92                 else {
93                         $mover .= "<a href='move.cgi?idx=$r->{'index'}&".
94                                   "up=1'><img src=images/up.gif ".
95                                   "border=0></a>";
96                         }
97                 $adder .= "<a href='edit_rule.cgi?new=1&".
98                           "after=$r->{'index'}'>".
99                           "<img src=images/after.gif border=0></a>";
100                 $adder .= "<a href='edit_rule.cgi?new=1&".
101                           "before=$r->{'index'}'>".
102                           "<img src=images/before.gif border=0></a>";
103
104                 local $ls = "<a href='edit_rule.cgi?idx=$r->{'index'}'>";
105                 local $le = "</a>";
106                 local $act = $amap{int($r->{'num'})};
107                 print &ui_checked_columns_row(
108                         [ $ls.($r->{'active'} ? $text{'yes'} :
109                                "<font color=#ff0000>$text{'no'}</font>").$le,
110                           $ls.($text{'action_'.$r->{'action'}} ||
111                            uc($r->{'action'})).$le,
112                           $ls.$text{'dir_'.$r->{'dir'}}.$le,
113                           $config{'view_condition'} ?
114                                 ( &describe_rule($r) ) : ( ),
115                           $config{'view_comment'} ?
116                                 ( $r->{'cmt'} || "<br>" ) : ( ),
117                           $mover,
118                           $adder ],
119                         \@widths, "d", $r->{'index'});
120                 }
121         print &ui_columns_end();
122         print &ui_links_row(\@links);
123
124         # Buttons to delete and add
125         print "<table width=100%><tr>\n";
126         print "<td align=left>",
127               &ui_submit($text{'index_delete'}, "delsel"),"</td>\n";
128         print "<td align=right>",
129               &ui_submit($text{'index_add2'}, "new"),"</td>\n";
130         print "</tr></table>\n";
131         print &ui_form_end();
132
133         # Show NAT rules
134         print &ui_hr();
135         $natrules = &get_ipnat_config();
136         print &ui_form_start("edit_nat.cgi");
137         if (@$natrules) {
138                 local @widths = ( "width=10", "width=5%", "width=10%", undef );
139                 push(@widths, undef) if ($config{'view_condition'});
140                 push(@widths, undef) if ($config{'view_comment'});
141                 push(@widths, "width=5%", "width=5%");
142                 print &select_all_link("d", 1),"\n";
143                 print &select_invert_link("d", 1),"<br>\n";
144                 print &ui_columns_start([ "",
145                                           $text{'index_active'},
146                                           $text{'index_nataction'},
147                                           $config{'view_condition'} ?
148                                                 ( $text{'index_natfrom'},
149                                                   $text{'index_natto'} ) : ( ),
150                                           $config{'view_comment'} ?
151                                                 ( $text{'index_cmt'} ) : ( ),
152                                           $text{'index_move'} ], 100, 0,
153                                         \@widths);
154                 foreach $r (@$natrules) {
155                         local ($mover, $adder);
156                         if ($r eq $natrules->[@$natrules-1]) {
157                                 $mover .= "<img src=images/gap.gif>";
158                                 }
159                         else {
160                                 $mover .= "<a href='natmove.cgi?idx=$r->{'index'}&".
161                                           "down=1'><img src=".
162                                           "images/down.gif border=0></a>";
163                                 }
164                         if ($r eq $natrules->[0]) {
165                                 $mover .= "<img src=images/gap.gif>";
166                                 }
167                         else {
168                                 $mover .= "<a href='natmove.cgi?idx=$r->{'index'}&".
169                                           "up=1'><img src=images/up.gif ".
170                                           "border=0></a>";
171                                 }
172
173                         local $ls = "<a href='edit_nat.cgi?idx=$r->{'index'}'>";
174                         local $le = "</a>";
175                         print &ui_columns_row(
176                                 [ &ui_checkbox("d", $r->{'index'}, "", 0),
177                                   $ls.($r->{'active'} ? $text{'yes'} :
178                                        "<font color=#ff0000>$text{'no'}</font>").$le,
179                                   $ls.($text{'action_'.$r->{'action'}} ||
180                                    uc($r->{'action'})).$le,
181                                   $config{'view_condition'} ?
182                                         ( &describe_from($r), &describe_to($r) ) : ( ),
183                                   $config{'view_comment'} ?
184                                         ( $r->{'cmt'} || "<br>" ) : ( ),
185                                   $mover ],
186                                 \@widths);
187                         }
188                 print &ui_columns_end();
189                 print &select_all_link("d", 1),"\n";
190                 print &select_invert_link("d", 1),"<br>\n";
191                 print "<table width=100%><tr>\n";
192                 print "<td align=left>",
193                       &ui_submit($text{'index_delete'}, "delsel"),"</td>\n";
194                 print "<td align=right>",
195                       &ui_submit($text{'index_add3'}, "newmap"),"\n",
196                       &ui_submit($text{'index_add4'}, "newrdr"),"</td>\n";
197                 print "</tr></table>\n";
198                 }
199         else {
200                 print "<b>$text{'index_natnone'}</b><p>\n";
201                 print "<table width=100%><tr>\n";
202                 print "<td align=right>",
203                       &ui_submit($text{'index_add3'}, "newmap"),"\n",
204                       &ui_submit($text{'index_add4'}, "newrdr"),"</td>\n";
205                 print "</tr></table>\n";
206                 }
207         print &ui_form_end();
208
209         # Show buttons to apply configuration and start at boot
210         print &ui_hr();
211
212         print &ui_buttons_start();
213         if (&foreign_check("servers")) {
214                 @servers = &list_cluster_servers();
215                 }
216         print &ui_buttons_row("apply.cgi", $text{'index_apply'},
217                               @servers ? $text{'index_applydesc2'}
218                                        : $text{'index_applydesc'});
219         print &ui_buttons_row("unapply.cgi", $text{'index_unapply'},
220                               $text{'index_unapplydesc'});
221         print &ui_buttons_row("bootup.cgi", $text{'index_boot'},
222                               $text{'index_bootdesc'}, undef,
223                               &ui_radio("boot", $atboot == 2 ? 1 : 0,
224                                         [ [ 1, $text{'yes'} ],
225                                           [ 0, $text{'no'} ] ]));
226         print &ui_buttons_row("index.cgi", $text{'index_reset'},
227                               $text{'index_resetdesc'}, undef,
228                               &ui_hidden("reset", 1));
229         # Show button for cluster page
230         if (&foreign_check("servers")) {
231                 &foreign_require("servers", "servers-lib.pl");
232                 @allservers = grep { $_->{'user'} }
233                                 &servers::list_servers();
234                 }
235         if (@allservers) {
236                 print &ui_buttons_row("cluster.cgi", $text{'index_cluster'},
237                                       $text{'index_clusterdesc'});
238                 }
239         print &ui_buttons_end();
240         }
241 else {
242         # Offer to setup simple firewall
243         print &text($in{'reset'} ? 'index_rsetup' : 'index_setup',
244                     "<tt>$ipfw_file</tt>"),"<p>\n";
245         print &ui_form_start("setup.cgi");
246         print &ui_hidden("reset", $in{'reset'});
247         print "<center><table><tr><td>\n";
248         print &ui_oneradio("auto", 0, $text{'index_auto0'}, 1),"<p>\n";
249         foreach $a (1 .. 4) {
250                 print &ui_oneradio("auto", $a, $text{'index_auto'.$a}, 0)," ",
251                       &interface_choice("iface".$a, undef, 1),"<p>\n";
252                 }
253         print "</td></tr></table>\n";
254         print &ui_submit($text{'index_auto'}),"<p>\n";
255         print &ui_checkbox("atboot", 1, $text{'index_atboot'},
256                            $atboot == 2),"\n";
257         print "</center>",&ui_form_end(),"\n";
258         }
259
260 &ui_print_footer("/", $text{'index'});
261