summaryrefslogtreecommitdiffstats
path: root/userspace/arptables/arptables-restore
blob: c5097027081d7e7351d36e515ea51ea38ee307b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/perl -w
# A script that imports text arptables rules. Similar to iptables-restore.

use strict;
my $tool = "__EXEC_PATH__/arptables";
my $table;
my $rc;
my $line;

# ==============================
# clear_arptables
# - sets policy to accept
# - flushes chains
# - removes custom chains
# ==============================
sub clear_arptables {
	$rc = `$tool -P INPUT ACCEPT`;
	unless($? == 0) { print "ERROR: $rc\n"; exit -1 };
	$rc = `$tool -P FORWARD ACCEPT`;
	unless($? == 0) { print "ERROR: $rc\n"; exit -1 };
	$rc = `$tool -P OUTPUT ACCEPT`;
	unless($? == 0) { print "ERROR: $rc\n"; exit -1 };

	$rc = `$tool -F`;
	unless($? == 0) { print "ERROR: $rc\n"; exit -1 };

	$rc = `$tool -L`;
	unless($? == 0) { print "ERROR: $rc\n"; exit -1 };
	
	foreach $line (split("\n",$rc)) {
		unless ($line =~ m/Chain\s(.*?)\s\(.*references\)/) { next; }
		$rc = `$tool -X $1`;
		unless($? == 0) { print "ERROR: $rc\n"; exit -1 };
	}
}
# ==============================


unless (-x $tool) { print "ERROR: $tool isn't executable\n"; exit -1; };
&clear_arptables();

$line = 0;
while(<>) {
    $line++;
    if(m/^#/) { next; };
    if(m/^$/) { next; };

    if(m/^\*(.*)/) {
        $table = $1;
        next;
    }

    # Process a chain directive
    if(m/^\:(.*?)\s(.*)/) {
	# is it a user or a built in chain ?
	if ("$2" eq "-") { 
        	$rc = `$tool -t $table -N $1`;
	        unless($? == 0) {print "ERROR(line $line): $rc\n"; exit -1};
		next; 
	}
        $rc = `$tool -t $table -P $1 $2`;
        unless($? == 0) {print "ERROR(line $line): $rc\n"; exit -1};
        next;
    }
    $rc = `$tool -t $table $_`;
    unless($? == 0) {print "ERROR(line $line): $rc\n"; exit -1};
}