blob: abe049d58ff6a3466a680454db111b1e1208a207 [file] [log] [blame]
Bart De Schuymer865444d2005-06-14 19:17:48 +00001#!/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
8use strict;
Bart De Schuymer50441e92005-08-30 21:20:55 +00009my $ebtables = "__EXEC_PATH__/ebtables";
10my $table = "";
Bart De Schuymer865444d2005-06-14 19:17:48 +000011my $rc;
12my $line;
13
14# ==============================
15# Check table
16# Creates user chains.
17# ==============================
18sub check_chain {
Bart De Schuymer50441e92005-08-30 21:20:55 +000019 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 Schuymer865444d2005-06-14 19:17:48 +000034}
35# ==============================
36
37unless (-x $ebtables) { print "ERROR: $ebtables isn't executable\n"; exit -1; };
Bart De Schuymer865444d2005-06-14 19:17:48 +000038$line = 0;
39while(<>) {
40 $line++;
41 if(m/^#/) { next; };
42 if(m/^$/) { next; };
43 if(m/^\*(.*)/) {
Bart De Schuymer50441e92005-08-30 21:20:55 +000044 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 Schuymer865444d2005-06-14 19:17:48 +000048 $table = $1;
Bart De Schuymer50441e92005-08-30 21:20:55 +000049 $rc = `$ebtables -t filter --init-table`;
50 unless($? == 0) {print "ERROR: $rc\n"; exit -1};
Bart De Schuymer865444d2005-06-14 19:17:48 +000051 next;
52 }
53 if(m/^\:(.*?)\s(.*)/) {
Bart De Schuymer50441e92005-08-30 21:20:55 +000054 &check_chain($table,$1);
Bart De Schuymer865444d2005-06-14 19:17:48 +000055 $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 Schuymer50441e92005-08-30 21:20:55 +000062if (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}