Bart De Schuymer | 865444d | 2005-06-14 19:17:48 +0000 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
| 2 | # |
| 3 | # |
| 4 | # A script that imports text ebtables rules. Similar to iptables-restore. |
| 5 | # It can be used to restore configuration from /etc/sysconfig/ebtables. |
| 6 | # |
| 7 | |
| 8 | use strict; |
Bart De Schuymer | 50441e9 | 2005-08-30 21:20:55 +0000 | [diff] [blame] | 9 | my $ebtables = "__EXEC_PATH__/ebtables"; |
| 10 | my $table = ""; |
Bart De Schuymer | 865444d | 2005-06-14 19:17:48 +0000 | [diff] [blame] | 11 | my $rc; |
| 12 | my $line; |
| 13 | |
| 14 | # ============================== |
| 15 | # Check table |
| 16 | # Creates user chains. |
| 17 | # ============================== |
| 18 | sub check_chain { |
Bart De Schuymer | 50441e9 | 2005-08-30 21:20:55 +0000 | [diff] [blame] | 19 | if ($table eq "filter") { |
| 20 | if ($_[1] eq "INPUT") { return; } |
| 21 | if ($_[1] eq "FORWARD") { return; } |
| 22 | if ($_[1] eq "OUTPUT") { return; } |
| 23 | } |
| 24 | if ($table eq "nat") { |
| 25 | if ($_[1] eq "PREROUTING") { return; } |
| 26 | if ($_[1] eq "POSTROUTING") { return; } |
| 27 | if ($_[1] eq "OUTPUT") { return; } |
| 28 | } |
| 29 | if ($table eq "broute") { |
| 30 | if ($_[1] eq "BROUTING") { return; } |
| 31 | } |
| 32 | $rc = `$ebtables -t $_[0] -N $_[1]`; |
| 33 | unless($? == 0) {print "ERROR: $rc\n"; exit -1}; |
Bart De Schuymer | 865444d | 2005-06-14 19:17:48 +0000 | [diff] [blame] | 34 | } |
| 35 | # ============================== |
| 36 | |
| 37 | unless (-x $ebtables) { print "ERROR: $ebtables isn't executable\n"; exit -1; }; |
Bart De Schuymer | 865444d | 2005-06-14 19:17:48 +0000 | [diff] [blame] | 38 | $line = 0; |
| 39 | while(<>) { |
| 40 | $line++; |
| 41 | if(m/^#/) { next; }; |
| 42 | if(m/^$/) { next; }; |
| 43 | if(m/^\*(.*)/) { |
Bart De Schuymer | 50441e9 | 2005-08-30 21:20:55 +0000 | [diff] [blame] | 44 | if (defined($ENV{'EBTABLES_SAVE_COUNTER'}) && !($ENV{'EBTABLES_SAVE_COUNTER'} eq "yes") && !($table eq "") ) { |
| 45 | $rc = `$ebtables -t $table -Z`; |
| 46 | unless($? == 0) {print "ERROR: $rc\n"; exit -1}; |
| 47 | } |
Bart De Schuymer | 865444d | 2005-06-14 19:17:48 +0000 | [diff] [blame] | 48 | $table = $1; |
Bart De Schuymer | 50441e9 | 2005-08-30 21:20:55 +0000 | [diff] [blame] | 49 | $rc = `$ebtables -t filter --init-table`; |
| 50 | unless($? == 0) {print "ERROR: $rc\n"; exit -1}; |
Bart De Schuymer | 865444d | 2005-06-14 19:17:48 +0000 | [diff] [blame] | 51 | next; |
| 52 | } |
| 53 | if(m/^\:(.*?)\s(.*)/) { |
Bart De Schuymer | 50441e9 | 2005-08-30 21:20:55 +0000 | [diff] [blame] | 54 | &check_chain($table,$1); |
Bart De Schuymer | 865444d | 2005-06-14 19:17:48 +0000 | [diff] [blame] | 55 | $rc = `$ebtables -t $table -P $1 $2`; |
| 56 | unless($? == 0) {print "ERROR(line $line): $rc\n"; exit -1}; |
| 57 | next; |
| 58 | } |
| 59 | $rc = `$ebtables -t $table $_`; |
| 60 | unless($? == 0) {print "ERROR(line $line): $rc\n"; exit -1}; |
| 61 | } |
Bart De Schuymer | 50441e9 | 2005-08-30 21:20:55 +0000 | [diff] [blame] | 62 | if (defined($ENV{'EBTABLES_SAVE_COUNTER'}) && !($ENV{'EBTABLES_SAVE_COUNTER'} eq "yes") && !($table eq "")) { |
| 63 | $rc = `$ebtables -t $table -Z`; |
| 64 | unless($? == 0) {print "ERROR: '-t $table -Z' failed\n"; exit -1}; |
| 65 | } |