| 1 |
#!/usr/bin/perl -w |
| 2 |
# A script that imports text arptables rules. Similar to iptables-restore. |
| 3 |
|
| 4 |
use strict; |
| 5 |
my $tool = "__EXEC_PATH__/arptables"; |
| 6 |
my $table; |
| 7 |
my $rc; |
| 8 |
my $line; |
| 9 |
|
| 10 |
# ============================== |
| 11 |
# clear_arptables |
| 12 |
# - sets policy to accept |
| 13 |
# - flushes chains |
| 14 |
# - removes custom chains |
| 15 |
# ============================== |
| 16 |
sub clear_arptables { |
| 17 |
$rc = `$tool -P INPUT ACCEPT`; |
| 18 |
unless($? == 0) { print "ERROR: $rc\n"; exit -1 }; |
| 19 |
$rc = `$tool -P FORWARD ACCEPT`; |
| 20 |
unless($? == 0) { print "ERROR: $rc\n"; exit -1 }; |
| 21 |
$rc = `$tool -P OUTPUT ACCEPT`; |
| 22 |
unless($? == 0) { print "ERROR: $rc\n"; exit -1 }; |
| 23 |
|
| 24 |
$rc = `$tool -F`; |
| 25 |
unless($? == 0) { print "ERROR: $rc\n"; exit -1 }; |
| 26 |
|
| 27 |
$rc = `$tool -L`; |
| 28 |
unless($? == 0) { print "ERROR: $rc\n"; exit -1 }; |
| 29 |
|
| 30 |
foreach $line (split("\n",$rc)) { |
| 31 |
unless ($line =~ m/Chain\s(.*?)\s\(.*references\)/) { next; } |
| 32 |
$rc = `$tool -X $1`; |
| 33 |
unless($? == 0) { print "ERROR: $rc\n"; exit -1 }; |
| 34 |
} |
| 35 |
} |
| 36 |
# ============================== |
| 37 |
|
| 38 |
|
| 39 |
unless (-x $tool) { print "ERROR: $tool isn't executable\n"; exit -1; }; |
| 40 |
&clear_arptables(); |
| 41 |
|
| 42 |
$line = 0; |
| 43 |
while(<>) { |
| 44 |
$line++; |
| 45 |
if(m/^#/) { next; }; |
| 46 |
if(m/^$/) { next; }; |
| 47 |
|
| 48 |
if(m/^\*(.*)/) { |
| 49 |
$table = $1; |
| 50 |
next; |
| 51 |
} |
| 52 |
|
| 53 |
# Process a chain directive |
| 54 |
if(m/^\:(.*?)\s(.*)/) { |
| 55 |
# is it a user or a built in chain ? |
| 56 |
if ("$2" eq "-") { |
| 57 |
$rc = `$tool -t $table -N $1`; |
| 58 |
unless($? == 0) {print "ERROR(line $line): $rc\n"; exit -1}; |
| 59 |
next; |
| 60 |
} |
| 61 |
$rc = `$tool -t $table -P $1 $2`; |
| 62 |
unless($? == 0) {print "ERROR(line $line): $rc\n"; exit -1}; |
| 63 |
next; |
| 64 |
} |
| 65 |
$rc = `$tool -t $table $_`; |
| 66 |
unless($? == 0) {print "ERROR(line $line): $rc\n"; exit -1}; |
| 67 |
} |