Handle hostnames with upper-case letters
[webmin.git] / firewall / debian-linux-lib.pl
1 # debians-linux-lib.pl
2 # Deal with debian's iptables save file and startup script
3
4 if ($gconfig{'os_version'} >= 3.1 &&
5     !-r "/etc/init.d/iptables" &&
6     !-r "/etc/init.d/webmin-iptables") {
7         # In newer Debians, IPtable is started by the network init script
8         $has_new_debian_iptables = 1;
9         $iptables_save_file = "/etc/iptables.up.rules";
10         }
11 else {
12         # Older Debians use an init script
13         $has_debian_iptables = -r "/etc/init.d/iptables";
14         $debian_iptables_dir = "/var/lib/iptables";
15         if ($has_debian_iptables) {
16                 mkdir($debian_iptables_dir, 0755) if (!-d $debian_iptables_dir);
17                 $iptables_save_file = "$debian_iptables_dir/active";
18                 }
19         }
20
21 # apply_iptables()
22 # Applies the current iptables configuration from the save file
23 sub apply_iptables
24 {
25 if ($has_debian_iptables) {
26         local $out = &backquote_logged("cd / ; /etc/init.d/iptables start 2>&1");
27         return $? ? "<pre>$out</pre>" : undef;
28         }
29 else {
30         return &iptables_restore();
31         }
32 }
33
34 # unapply_iptables()
35 # Writes the current iptables configuration to the save file
36 sub unapply_iptables
37 {
38 if ($has_debian_iptables) {
39         $out = &backquote_logged("cd / ; /etc/init.d/iptables save active 2>&1 </dev/null");
40         return $? ? "<pre>$out</pre>" : undef;
41         }
42 else {
43         return &iptables_save();
44         }
45 }
46
47 # started_at_boot()
48 sub started_at_boot
49 {
50 &foreign_require("init", "init-lib.pl");
51 if ($has_debian_iptables) {
52         # Check Debian init script
53         return &init::action_status("iptables") == 2;
54         }
55 elsif ($has_new_debian_iptables) {
56         # Check network interface config
57         local $pri = &get_primary_network_interface();
58         local ($debpri) = grep { $_->[0] eq $pri->{'fullname'} }
59                                &net::get_interface_defs();
60         foreach my $o (@{$debpri->[3]}) {
61                 if (($o->[0] eq "pre-up" || $o->[0] eq "post-up") &&
62                     $o->[1] =~ /\S*iptables-restore\s+<\s+(\S+)/ &&
63                     $1 eq $iptables_save_file) {
64                         return 1;
65                         }
66                 }
67         }
68 else {
69         # Check Webmin init script
70         return &init::action_status("webmin-iptables") == 2;
71         }
72 }
73
74 sub enable_at_boot
75 {
76 &foreign_require("init", "init-lib.pl");
77 if ($has_debian_iptables) {
78         &init::enable_at_boot("iptables");       # Assumes init script exists
79         }
80 elsif ($has_new_debian_iptables) {
81         # Add to network interface config
82         local $pri = &get_primary_network_interface();
83         local ($debpri) = grep { $_->[0] eq $pri->{'fullname'} }
84                                &net::get_interface_defs();
85         if ($debpri && !&started_at_boot()) {
86                 push(@{$debpri->[3]},
87                      [ "post-up", "iptables-restore < $iptables_save_file" ]);
88                 &net::modify_interface_def(@$debpri);
89                 }
90         }
91 else {
92         &create_webmin_init();
93         }
94 }
95
96 sub disable_at_boot
97 {
98 &foreign_require("init", "init-lib.pl");
99 if ($has_debian_iptables) {
100         &init::disable_at_boot("iptables");
101         }
102 elsif ($has_new_debian_iptables) {
103         # Remove from network interface config
104         local $pri = &get_primary_network_interface();
105         local ($debpri) = grep { $_->[0] eq $pri->{'fullname'} }
106                                &net::get_interface_defs();
107         @{$debpri->[3]} = grep {
108                         ($_->[0] ne "pre-up" && $_->[0] ne "post-up") ||
109                          $_->[1] !~ /^\S*iptables/ } @{$debpri->[3]};
110         &net::modify_interface_def(@$debpri);
111         }
112 else {
113         &init::disable_at_boot("webmin-iptables");
114         }
115 }
116
117 sub get_primary_network_interface
118 {
119 &foreign_require("net", "net-lib.pl");
120 local @boot = sort { $a->{'fullname'} cmp $b->{'fullname'} }
121                    &net::boot_interfaces();
122 local ($eth) = grep { $_->{'fullname'} =~ /^eth\d+$/ } @boot;
123 local ($ppp) = grep { $_->{'fullname'} =~ /^ppp\d+$/ } @boot;
124 local ($venetn) = grep { $_->{'fullname'} =~ /^venet\d+:\d+$/ } @boot;
125 local ($venet) = grep { $_->{'fullname'} =~ /^venet\d+$/ } @boot;
126 return $eth || $ppp || $venetn || $venet || $boot[0];
127 }
128
129 1;
130