scriptdump.pl revision 55505
1#! @LOCALPREFIX@/bin/perl
2# $FreeBSD: head/sbin/setkey/scriptdump.pl 55505 2000-01-06 12:40:54Z shin $
3
4if ($< != 0) {
5	print STDERR "must be root to invoke this\n";
6	exit 1;
7}
8
9$mode = 'add';
10while ($i = shift @ARGV) {
11	if ($i eq '-d') {
12		$mode = 'delete';
13	} else {
14		print STDERR "usage: scriptdump [-d]\n";
15		exit 1;
16	}
17}
18
19open(IN, "setkey -D |") || die;
20foreach $_ (<IN>) {
21	if (/^[^\t]/) {
22		($src, $dst) = split(/\s+/, $_);
23	} elsif (/^\t(esp|ah) mode=(\S+) spi=(\d+).*replay=(\d+)/) {
24		($proto, $ipsecmode, $spi, $replay) = ($1, $2, $3, $4);
25	} elsif (/^\tE: (\S+) (.*)/) {
26		$ealgo = $1;
27		$ekey = $2;
28		$ekey =~ s/\s//g;
29		$ekey =~ s/^/0x/g;
30	} elsif (/^\tA: (\S+) (.*)/) {
31		$aalgo = $1;
32		$akey = $2;
33		$akey =~ s/\s//g;
34		$akey =~ s/^/0x/g;
35	} elsif (/^\tstate=/) {
36		print "$mode $src $dst $proto $spi -m $ipsecmode";
37		print " -r $replay" if $replay;
38		if ($mode eq 'add') {
39			if ($proto eq 'esp') {
40				print " -E $ealgo $ekey" if $ealgo;
41				print " -A $aalgo $akey" if $aalgo;
42			} elsif ($proto eq 'ah') {
43				print " -A $aalgo $akey" if $aalgo;
44			}
45		}
46		print ";\n";
47
48		$src = $dst = $upper = $proxy = '';
49		$ealgo = $ekey = $aalgo = $akey = '';
50	}
51}
52close(IN);
53
54exit 0;
55