2 # Deal with debian's iptables save file and startup script
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";
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";
22 # Applies the current iptables configuration from the save file
25 if ($has_debian_iptables) {
26 local $out = &backquote_logged("cd / ; /etc/init.d/iptables start 2>&1");
27 return $? ? "<pre>$out</pre>" : undef;
30 return &iptables_restore();
35 # Writes the current iptables configuration to the save file
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;
43 return &iptables_save();
50 &foreign_require("init", "init-lib.pl");
51 if ($has_debian_iptables) {
52 # Check Debian init script
53 return &init::action_status("iptables") == 2;
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) {
69 # Check Webmin init script
70 return &init::action_status("webmin-iptables") == 2;
76 &foreign_require("init", "init-lib.pl");
77 if ($has_debian_iptables) {
78 &init::enable_at_boot("iptables"); # Assumes init script exists
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()) {
87 [ "post-up", "iptables-restore < $iptables_save_file" ]);
88 &net::modify_interface_def(@$debpri);
92 &create_webmin_init();
98 &foreign_require("init", "init-lib.pl");
99 if ($has_debian_iptables) {
100 &init::disable_at_boot("iptables");
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);
113 &init::disable_at_boot("webmin-iptables");
117 sub get_primary_network_interface
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];