Handle hostnames with upper-case letters
[webmin.git] / frox / save_iptables.cgi
1 #!/usr/local/bin/perl
2 # Enable or disable the iptables rule
3
4 require './frox-lib.pl';
5 &foreign_require("firewall", "firewall-lib.pl");
6 $conf = &get_config();
7 $port = &find_value("Port", $conf);
8 &error_setup($text{'iptables_err'});
9 &ReadParse();
10
11 # Validate inputs
12 if ($in{'enabled'} == 1) {
13         &to_ipaddress($in{'net'}) ||
14             ($in{'net'} =~ /^([0-9\.]+)\/(\d+)$/ &&
15              &check_ipaddress($1) && $2 > 0 && $2 <= 32) ||
16                 &error($text{'iptables_enet'});
17         }
18 elsif ($in{'enabled'} == 2) {
19         $iface = $in{'iface'} eq 'other' ? $in{'iface_other'} : $in{'iface'};
20         $iface =~ /^\S+$/ || &error($text{'iptables_eiface'});
21         }
22
23 # Get the old rule
24 @tables = &firewall::get_iptables_save();
25 ($nat) = grep { $_->{'name'} eq 'nat'} @tables;
26 if ($in{'rule'} ne "") {
27         ($rule) = $nat->{'rules'}->[$in{'rule'}];
28         }
29
30 if ($in{'enabled'} && !$rule) {
31         # Need to create
32         $rule = { 'chain' => 'PREROUTING',
33                   'j' => [ '', 'REDIRECT' ],
34                   'p' => [ '', 'tcp' ],
35                   'm' => [ '', 'tcp' ],
36                   'dport' => [ '', 21 ],
37                   'to-ports' => [ '', $port ],
38                   ($iface ? ( 'i' => [ '', $iface ] )
39                           : ( 's' => [ '', $in{'net'} ] ) ),
40                   'cmt' => 'Forward FTP connections to Frox proxy' };
41         push(@{$nat->{'rules'}}, $rule);
42         $apply = 1;
43         }
44 elsif ($in{'enabled'} && $rule) {
45         # Need to update rule
46         if ($iface) {
47                 delete($rule->{'s'});
48                 $rule->{'i'} = [ '', $iface ];
49                 }
50         else {
51                 delete($rule->{'i'});
52                 $rule->{'s'} = [ '', $in{'net'} ];
53                 }
54         $apply = 1;
55         }
56 elsif (!$in{'enabled'} && $rule) {
57         # Need to delete
58         splice(@{$nat->{'rules'}}, $in{'rule'}, 1);
59         $apply = 2;
60         }
61 else {
62         $apply = 0;
63         }
64 if ($apply) {
65         # Save and apply
66         &lock_file($firewall::iptables_save_file);
67         &firewall::save_table($nat);
68         &unlock_file($firewall::iptables_save_file);
69         $err = &firewall::apply_configuration();
70         &error(&text('iptables_eapply', $err)) if ($err);
71         &webmin_log("iptables", $apply);
72         }
73
74 &redirect("");
75
76