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; |
| 9 | my $ebtables = "/sbin/ebtables"; |
| 10 | my $table; |
| 11 | my $rc; |
| 12 | my $line; |
| 13 | |
| 14 | # ============================== |
| 15 | # Check table |
| 16 | # Creates user chains. |
| 17 | # ============================== |
| 18 | sub check_chain { |
| 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}; |
| 34 | } |
| 35 | # ============================== |
| 36 | |
| 37 | unless (-x $ebtables) { print "ERROR: $ebtables isn't executable\n"; exit -1; }; |
| 38 | $rc = `$ebtables -t filter --init-table`; |
| 39 | unless($? == 0) { print "ERROR: $rc\n"; exit -1 }; |
| 40 | $rc = `$ebtables -t nat --init-table`; |
| 41 | unless($? == 0) { print "ERROR: $rc\n"; exit -1 }; |
| 42 | $rc = `$ebtables -t broute --init-table`; |
| 43 | unless($? == 0) { print "ERROR: $rc\n"; exit -1 }; |
| 44 | |
| 45 | $line = 0; |
| 46 | while(<>) { |
| 47 | $line++; |
| 48 | if(m/^#/) { next; }; |
| 49 | if(m/^$/) { next; }; |
| 50 | if(m/^\*(.*)/) { |
| 51 | $table = $1; |
| 52 | next; |
| 53 | } |
| 54 | if(m/^\:(.*?)\s(.*)/) { |
| 55 | &check_chain($table,$1); |
| 56 | $rc = `$ebtables -t $table -P $1 $2`; |
| 57 | unless($? == 0) {print "ERROR(line $line): $rc\n"; exit -1}; |
| 58 | next; |
| 59 | } |
| 60 | $rc = `$ebtables -t $table $_`; |
| 61 | unless($? == 0) {print "ERROR(line $line): $rc\n"; exit -1}; |
| 62 | } |